[Tutor] validating user input for cli app

2010-10-07 Thread Rance Hall
I have the following scenario, during a cli app a function is called
whose purpose is to get enough info from user to create a database
record.

Clearly the input values need to be validated.  How do I handle the
situation where validation fails, but the answer to the question is
required.

so far I have something like this

def moduser(clientid):
   if not len(clientid) == 0:
   client = getclient(clientid) # this is a database call to get
the existing record for modification, entering a zero length string
assumes its a new record instead of a mod.
   if len(client) == 0:
   print("client not found")
# more code here to wait a second and then go back to the
menu selection that started this function.
   clienttype = input("Enter client type, there are two choices,
Commercial or Individual\n choose C or I ")
   *** My question is about what goes here ***
..
return

This is where life gets confusing, if clienttype is not a c or an I
(case insensitive) then I have to ask the question again or offer a
way out of my function
There are several questions being asked here, about name, address,
etc., each element has its own validation rules, and each question
needs and answer

I know how to write the if statements to decide if the data entered is
valid or not, but I need a way to deal with what happens when it is
NOT valid.

I'd like to be able to just ask the question again, and re-validate
the new input or offer a "press q to quit" option

q to quit is no big deal, but it doesn't make sense to ME to clutter
up the code with a whole set of functions that each just ask one
question and return a value.  Especially since when my app is done
there will likely be over a hundred of these type of one or two line
functions. the names will be impossible to keep up with.  Whats more,
debugging and future upgrades will be a major PITA.

The old GOTO syntax that everybody hates was ideal for this type of
thing, if validation fails, just goto the question again and re-ask
it.

Based on my Internet research it looks like the only way you get GOTO
capability is with an April Fools joke in 2004.

So how do you do this?

Rance
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pymssql and encoding

2010-10-07 Thread Emmanuel Ruellan
Perfect! Thanks Evert.

I realise now that I don't fully grasp the concepts of encoding and
collation and their relationship.

-- 
Emmanuel


On Wed, Oct 6, 2010 at 10:38 PM, Evert Rol  wrote:

>
>
> > >>> print customerName
> > ImmobiliŠre (whatever)
> > >>> customerName
> > 'Immobili\x8are (whatever)'
> >
> > There should be a small E with a grave accent (è) instead of the capital
> S with a caron (Š) I'm getting.
> >
> > I've tried applying various encodings, but to no avail:
> >
> 
>
> > When executed from MS SQL Server's Management Studio, the same query
> returns "Immobilière (whatever)", with an 'è', as it should.
> >
> > The field in question is of type nvarchar, with collation
> SQL_Latin1_General_CP850_CI_AS.
>
> Well, you're SQL server seems to use CP850, which is (almost) the first
> encoding I tried:
>
> >>> name= 'Immobili\x8are'
> >>> name
> 'Immobili\x8are'
> >>> print name.decode('cp850')
> Immobilière
> >>>
>
> Seems to work for me.
>
>  Evert
>
>
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Prime Numbers

2010-10-07 Thread Ryan Bridges
Hi,
I'm using Python 2.6.2 and I need to write a code so that when I use an
input, it will say if the number is prime or not.  How do I do this?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Prime Numbers

2010-10-07 Thread Shantanoo
On Thu, Oct 7, 2010 at 07:11, Ryan Bridges  wrote:

> Hi,
> I'm using Python 2.6.2 and I need to write a code so that when I use an
> input, it will say if the number is prime or not.  How do I do this?
>
>
Following links would be useful:
http://en.wikipedia.org/wiki/Prime_number
http://en.wikipedia.org/wiki/Primality_test

once you decide which algorithm you want to implement, you should be able to
get the python code in less than week's time :).

HTH.
regards,
shantanoo
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] pymssql and encoding

2010-10-07 Thread Evert Rol
> >>> print customerName
> ImmobiliŠre (whatever)
> >>> customerName
> 'Immobili\x8are (whatever)'
> 
> There should be a small E with a grave accent (è) instead of the capital S 
> with a caron (Š) I'm getting.
> 
> I've tried applying various encodings, but to no avail:
> 


> When executed from MS SQL Server's Management Studio, the same query returns 
> "Immobilière (whatever)", with an 'è', as it should.
> 
> The field in question is of type nvarchar, with collation 
> SQL_Latin1_General_CP850_CI_AS.

Well, you're SQL server seems to use CP850, which is (almost) the first 
encoding I tried:

>>> name= 'Immobili\x8are'
>>> name
'Immobili\x8are'
>>> print name.decode('cp850')
Immobilière
>>> 

Seems to work for me.

  Evert

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Prime Numbers

2010-10-07 Thread Steven D'Aprano
On Thu, 7 Oct 2010 12:41:07 pm Ryan Bridges wrote:
> Hi,
> I'm using Python 2.6.2 and I need to write a code so that when I use
> an input, it will say if the number is prime or not.  How do I do
> this?

Using the keyboard is probably the best way, although at a pinch you 
could copy and paste individual letters from another document.

We won't do your home work for you, but if you ask specific questions, 
we will be happy to help.

-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] validating user input for cli app

2010-10-07 Thread Steven D'Aprano
On Thu, 7 Oct 2010 12:09:31 pm Rance Hall wrote:

> I know how to write the if statements to decide if the data entered
> is valid or not, but I need a way to deal with what happens when it
> is NOT valid.
>
> I'd like to be able to just ask the question again, and re-validate
> the new input or offer a "press q to quit" option

Here is one way:


def ask_again(prompt, condition):
if not prompt.endswith(" "):
prompt = prompt + " "
while True:  # loop forever
response = input(prompt)  # use raw_input in Python 2.x
response = response.strip()
if response.lower() == 'q':
raise SysExit('user quit')
if condition(response):
return response
else:
print("I'm sorry, that is invalid. Please try again.")




And an example of it in use:


>>> ask_again("Enter two letters ", lambda s: len(s) == 2)
Enter two letters abcd
I'm sorry, that is invalid. Please try again.
Enter two letters a
I'm sorry, that is invalid. Please try again.
Enter two letters xy
'xy'
>>>
>>> ask_again("Please enter a number", str.isdigit)
Please enter a number fg
I'm sorry, that is invalid. Please try again.
Please enter a number yu
I'm sorry, that is invalid. Please try again.
Please enter a number 56
'56'
>>> 



You can build in as much intelligence as you want, depending on how much 
effort you care to put into it. For instance, you might like to 
escalate the error messages:

"Please try again."
"I'm sorry, your answer is incorrect, please try again."
"You must enter a number like 42 or 75. Please try again."
"Hey dummy, don't you know what a number is?"

although of course this requires more programming effort.

And naturally the condition functions can be as simple, or as 
complicated, as you need.




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] wrap methods for logging purposes

2010-10-07 Thread Jojo Mwebaze
I used a the recipe
(http://aspn.activestate.com/ASPN/Coo.../Recipe/198078)
used to wrap methods for logging purposes. logging classes. But it does seem
to work well with classes inherit form other classes -  i get recursion
errors!

Here is an example of the classes ..

class Base(MetaClass):

class BFrame(BaseFrame):
def __init__(self, pathname=''):
Base.__init__(self, pathname = pathname)

When i instatiate BiasFrame()

below is the  OUTPUT

logmethod  __init__ () {}
logmethod  __init__ () {'pathname': ''}
logmethod  __init__ () {'pathname': ''}
logmethod  __init__ () {'pathname': ''}
logmethod  __init__ () {'pathname': ''}
.
.
RuntimeError: maximum recursion depth exceeded in cmp

It starts with the __init__ of BFrame, that's correct. In the __init__ of
BFrame is a call to Base.__init__ with the pathname as argument. That is the
second call to __init__ in the output, but the class is wrong ! Instead of
the __init__ of Base the __init__ of BFrame is called.

is there any way i can correct this?

-- 
+= Johnson
--
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] wrap methods for logging purposes

2010-10-07 Thread Evert Rol
> I used a the recipe (http://aspn.activestate.com/ASPN/Coo.../Recipe/198078)  
> used to wrap methods for logging purposes. logging classes. But it does seem 
> to work well with classes inherit form other classes -  i get recursion 
> errors!
> 
> Here is an example of the classes .. 
> 
> class Base(MetaClass):
> 
> class BFrame(BaseFrame):
> def __init__(self, pathname=''):
> Base.__init__(self, pathname = pathname)
> 
> When i instatiate BiasFrame()

Sorry, but I can't see how this code example above can work;
- there is no definition of class Base. There's not even 'pass' statement!
- Where is MetaClass's definition?
- you instantiate BiasFrame, but that's not defined.
- BFrame inherits from BaseFrame, which I don't see defined inherit.
- I definitely would advise against calling Base.__init__ if you inherit BFrame 
from BaseFrame: BaseFrame.__init__ makes more sense.
  But rather, use super instead. In Python 2:
super(Base, self).__init__(pathname=pathname)


  Evert


> 
> below is the  OUTPUT
> 
> logmethod  __init__ () {}
> logmethod  __init__ () {'pathname': ''}
> logmethod  __init__ () {'pathname': ''}
> logmethod  __init__ () {'pathname': ''}
> logmethod  __init__ () {'pathname': ''}
> .
> .
> RuntimeError: maximum recursion depth exceeded in cmp
> 
> It starts with the __init__ of BFrame, that's correct. In the __init__ of 
> BFrame is a call to Base.__init__ with the pathname as argument. That is the 
> second call to __init__ in the output, but the class is wrong ! Instead of 
> the __init__ of Base the __init__ of BFrame is called.
> 
> is there any way i can correct this?
> 
> -- 
> += Johnson
> -- 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] validating user input for cli app

2010-10-07 Thread Alan Gauld


"Rance Hall"  wrote



I'd like to be able to just ask the question again, and re-validate
the new input or offer a "press q to quit" option


Thats what loops are for...


The old GOTO syntax that everybody hates was ideal for this type of
thing, if validation fails, just goto the question again and re-ask
it.


Nope, it wasn't ideal and thats why it was removed from most 
languages.

Structured Loops are better.


So how do you do this?


You either write a loop that cycles until you get valid input or you
could create a heirarchy of Field classes(*) that know how to request 
input
and validate it. If the data is invalid they request input again - 
which

takes us back to loops...

(*) Or even a single Validator class that takes a validation function 
as

an input parameter

One possible loop would look something like:

a = b = c = None
dataOK = False
while not dataOK:
   if not a:
  a = raw_input('Enter a: ')
  if not validA(a):
 a = None
 continue
   if not b:
  b = raw_input('Enter b: ')
  if not validB(b):
 b = None
 continue
   if not c:
  c = raw_input('Enter a: ')
  if not validC(c):
 c = None
 continue

And since the input blocks are repeated you could even
convert them to a function if you really cared or had a lot
of fields.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread Susana Iraiis Delgado Rodriguez
Hello members:

How can I write a statement to execute the following:
C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where
"LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf, I want my uotput to
look like this.
Instead I'm getting this C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr
T21-PUENTES.shp -where LAYER=+line tapalpa_05_plani_line.shp
In miy code line is a string given by the user:

for line in open("unico.txt", "r").readlines():
 p = subprocess.Popen(['C:/Archivos de
programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'",
b+'.shp'])

Any suggestions?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] IDE for Python

2010-10-07 Thread Juan Jose Del Toro
Dear List;

In your experience what is the best IDE for Python?

I've used SPE and IDLE, I've also seen people using Eclipse but which one do
you recommend?

-- 
¡Saludos! / Greetings!
Juan José Del Toro M.
jdeltoro1...@gmail.com
Guadalajara, Jalisco MEXICO
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread Ken Green

I have been using Geany under Ubuntu 10.04.  I rarely use IDLE.

Ken

On 10/07/2010 11:23 AM, Juan Jose Del Toro wrote:

Dear List;

In your experience what is the best IDE for Python?

I've used SPE and IDLE, I've also seen people using Eclipse but which 
one do you recommend?


--
¡Saludos! / Greetings!
Juan José Del Toro M.
jdeltoro1...@gmail.com 
Guadalajara, Jalisco MEXICO


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread Alan Gauld


"Juan Jose Del Toro"  wrote 


In your experience what is the best IDE for Python?


In my experience its vim and a couple of command shells.
But that's to do with 
a)what I use Python for and 
b) many years experience using vim 
It may not work for you (and you could substitute emacs 
for vim if your fingers 'speak' emacs).


I've used SPE and IDLE, 


These are chalk and cheese. 
If you just want to write a few basic scripts then IDLE is 
a good choice. SPE is better if you want tov do GUI work

or bigger projects.

I've also seen people using Eclipse 


PyDev on Eclipse is great if you already use Eclipse or if you 
are doing multi-language projects - and have a big modern PC...



but which one do you recommend?


Whatever best matches what you are trying to do and your 
existing experience. And that may be more than one!


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread taserian
I'm adding some line breaks to make your text a little more readable.

On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez <
susana.delgad...@utzmg.edu.mx> wrote:

> Hello members:
>
> How can I write a statement to execute the following:
>


> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where
> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf
>


> I want my uotput to look like this.
> Instead I'm getting this
>


> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where
> LAYER=+line tapalpa_05_plani_line.shp
>


> In miy code line is a string given by the user:
>
> for line in open("unico.txt", "r").readlines():
>  p = subprocess.Popen(['C:/Archivos de
> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'",
> b+'.shp'])
>
> Any suggestions?
>

Without knowing specifics about what the subprocess.Popen function is
expecting as parameters, I can only speculate, but it seems that the
following *might* work (for extremely generous values of "*might*"):

for line in open("unico.txt", "r").readlines():
 p = subprocess.Popen(['C:/Archivos de
programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" +
line + "'\"", b+'.shp'])

Details about where the changes are:
"\"LAYER='" + line + "'\""

Quote to begin the literal: "
An escaped quote (1) so that there's a quote inside the literal: \"
Some of the text that's meant to be unchanging: LAYER="
Close Quote: "
Add the content of the variable "line" from the unico.txt file:  + line +
Add another literal, composed of just the closing escaped quote to close the
quote above (1) : "\""

See if this works, and let us know how it turns out.

Antonio Rodriguez
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread taserian
On Thu, Oct 7, 2010 at 12:48 PM, taserian  wrote:

> I'm adding some line breaks to make your text a little more readable.
>
> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez <
> susana.delgad...@utzmg.edu.mx> wrote:
>
>  Hello members:
>>
>> How can I write a statement to execute the following:
>>
>
>
>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where
>> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf
>>
>
>
>> I want my uotput to look like this.
>> Instead I'm getting this
>>
>
>
>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where
>> LAYER=+line tapalpa_05_plani_line.shp
>>
>
>
>> In miy code line is a string given by the user:
>>
>> for line in open("unico.txt", "r").readlines():
>>  p = subprocess.Popen(['C:/Archivos de
>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'",
>> b+'.shp'])
>>
>> Any suggestions?
>>
>
> Without knowing specifics about what the subprocess.Popen function is
> expecting as parameters, I can only speculate, but it seems that the
> following *might* work (for extremely generous values of "*might*"):
>
> for line in open("unico.txt", "r").readlines():
>  p = subprocess.Popen(['C:/Archivos de
> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" +
> line + "'\"", b+'.shp'])
>
> Details about where the changes are:
> "\"LAYER='" + line + "'\""
>

Begin corrections (corrections start with a *)

Quote to begin the literal: "
> An escaped quote (1) so that there's a quote inside the literal: \"
> Some of the text that's meant to be unchanging: LAYER=
>
*Single Quote (2) to be included in the literal (which doesn't need to be
escaped): '

> Close Quote: "
>
Add the content of the variable "line" from the unico.txt file:  + line +
>
*Add another literal, composed of the single quote that closes (2) above,
then the closing escaped quote to close (1) : "'\""


>
> See if this works, and let us know how it turns out.
>
> Antonio Rodriguez
>

End of corrections.

Antonio Rodriguez
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] new turtle module

2010-10-07 Thread roberto
hello,
i want to use the turtle module described in
http://docs.python.org/library/turtle.html

since the module in my python2.5 is a little bit outdated;

is it correct to overwrite the turtle.py and turtle.pyc files in my current
'/usr/lib/python2.5/lib-tk/'
directory with the ones holding the same names unzipped from
turtleDemo folder 'TurtleDemo-Python2.x' by author Gregor Lindl ?

Thank you !
-- 
roberto
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Non-ASCII

2010-10-07 Thread Shawn Matlock
Hello,

I'm going through an online tutorial for Jython (www.jython.org). I can't find 
a place to ask a question on that site so I thought I'd try here. I believe the 
code is supposed to traverse a directory, identifying file types. The script is 
failing with the following message:

  File "", line None
SyntaxError: Non-ASCII character in file 
'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see 
http://www.python.org/peps/pep-0263.html for details

Thank you,
Shawn



import os, sys 
from stat import *

def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f) 
mode = os.stat(pathname)[ST_MODE] 
if S_ISDIR(mode):
# It's a directory, recurse into it 
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function 
callback(pathname)
else:
# Unknown file type, print a message 
print 'Skipping %s' % pathname

def visitfile(file):
print 'visiting', file

if __name__ == '__main__':
walktree(sys.argv[1], visitfile)


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new turtle module

2010-10-07 Thread Vern Ceder
On Thu, Oct 7, 2010 at 1:19 PM, roberto  wrote:

> hello,
> i want to use the turtle module described in
> http://docs.python.org/library/turtle.html
>
> since the module in my python2.5 is a little bit outdated;
>
> is it correct to overwrite the turtle.py and turtle.pyc files in my current
> '/usr/lib/python2.5/lib-tk/'
> directory with the ones holding the same names unzipped from
> turtleDemo folder 'TurtleDemo-Python2.x' by author Gregor Lindl ?
>
>
Yes, that should work with Python 2.5, but probably not with any earlier
versions.

Cheers,
Vern Ceder


> Thank you !
> --
> roberto
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
This time for sure!
   -Bullwinkle J. Moose
-
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137

The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread Knacktus

Am 07.10.2010 17:23, schrieb Juan Jose Del Toro:

Dear List;

In your experience what is the best IDE for Python?

I'm using Wing IDE. Very good overall package. I like especially the 
debug probe, which is like an interactive shell in the current stack. To 
me it's a good balance between features and learning curve. The only 
thing I really miss is refactoring support.
That's why I'm currently checking out PyCharm, which is about to be 
released (currently release candidate). It's from the company that 
created IntelliJ. PyCharm is in my opinion by far the most feature-rich 
Python IDE, looks very impressive so far. The only drawback is that it's 
written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a 
while but you get over it, once you discover all those wicked features 
;-)). But Wing isn't excactly eye-candy either.
Both are commercial, but if you code a lot it's worth it. Check out the 
offerings. (I think both are free for Open Source Projects.)


I also tried the free PyDev (an Eclipse plugin), which is very complete 
as well, but I don't get along with the Eclipse world.


So, check out Wing and PyCharm.

Cheers,

JJ
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread Susana Iraiis Delgado Rodriguez
Hello taserian and Antonio!

Thank you both for taking the time to answer my question. With taserian's
code it gives me the next output:
C... ogr2ogr T21-PUENTESshp -where "LAYER=T21-PUENTES"
tapalpa_05_plani_line.shp
but the output I need is:
C... ogr2ogr T21-PUENTESshp -where "LAYER=' T21-PUENTES' "
tapalpa_05_plani_line.shp

I did the Antonio's suggested corrections, and I got the string I wanted,
now the problem is that my subprocess doesn't work properly, I'll give a
look and see whats wrong with it.


2010/10/7 taserian 

>  On Thu, Oct 7, 2010 at 12:48 PM, taserian  wrote:
>
>> I'm adding some line breaks to make your text a little more readable.
>>
>> On Thu, Oct 7, 2010 at 9:55 AM, Susana Iraiis Delgado Rodriguez <
>> susana.delgad...@utzmg.edu.mx> wrote:
>>
>>  Hello members:
>>>
>>> How can I write a statement to execute the following:
>>>
>>
>>
>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr R1G-GEODESIA.shp -where
>>> "LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf
>>>
>>
>>
>>> I want my uotput to look like this.
>>> Instead I'm getting this
>>>
>>
>>
>>> C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr T21-PUENTES.shp -where
>>> LAYER=+line tapalpa_05_plani_line.shp
>>>
>>
>>
>>> In miy code line is a string given by the user:
>>>
>>> for line in open("unico.txt", "r").readlines():
>>>  p = subprocess.Popen(['C:/Archivos de
>>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "LAYER='line'",
>>> b+'.shp'])
>>>
>>> Any suggestions?
>>>
>>
>> Without knowing specifics about what the subprocess.Popen function is
>> expecting as parameters, I can only speculate, but it seems that the
>> following *might* work (for extremely generous values of "*might*"):
>>
>>  for line in open("unico.txt", "r").readlines():
>>  p = subprocess.Popen(['C:/Archivos de
>> programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', "\"LAYER='" +
>> line + "'\"", b+'.shp'])
>>
>> Details about where the changes are:
>> "\"LAYER='" + line + "'\""
>>
>
> Begin corrections (corrections start with a *)
>
>   Quote to begin the literal: "
>> An escaped quote (1) so that there's a quote inside the literal: \"
>> Some of the text that's meant to be unchanging: LAYER=
>>
> *Single Quote (2) to be included in the literal (which doesn't need to be
> escaped): '
>
>>  Close Quote: "
>>
>  Add the content of the variable "line" from the unico.txt file:  + line +
>>
> *Add another literal, composed of the single quote that closes (2) above,
> then the closing escaped quote to close (1) : "'\""
>
>
>>
>> See if this works, and let us know how it turns out.
>>
>> Antonio Rodriguez
>>
>
> End of corrections.
>
> Antonio Rodriguez
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Non-ASCII

2010-10-07 Thread Evert Rol
> I'm going through an online tutorial for Jython (www.jython.org). I can't 
> find a place to ask a question on that site so I thought I'd try here. I 
> believe the code is supposed to traverse a directory, identifying file types. 
> The script is failing with the following message:
> 
>  File "", line None
> SyntaxError: Non-ASCII character in file 
> 'H:\workspace\test\src\root\nested\example.py', but no encoding declared; see 
> http://www.python.org/peps/pep-0263.html for details

Normally, this (obviously) means you have some non-ASCII characters (accented 
characters would already do this) in your source code. Since this is just a 
byte code at the lowest level, it needs to be decoded into something sensible, 
which you specify using an encoding. Since Python refuses to guess which 
encoding you want to use (the same character code could result in totally 
different characters for different encodings), it's bailing out instead of 
continuing (the exception being that by default, the Python interpreter assumes 
your encoding is ASCII, and happily proceeds with that until it comes across a 
character code outside the ASCII range).

In your code below, however, I don't see any non-ASCII characters. So my best 
guess is that there is some invisible control-character outside the ASCII range 
that's causing this error message. 
This could happen because of a particular type of text-editor/IDE you're using.

The odd thing I find about the SyntaxError is actually that there is no line 
number mentioned. In fact, 'File "", line None' is a very weird 
indication for telling where the error occurred. Might again be something to do 
with your IDE.

I've tried your code by simply copy-pasting, and that works fine for me. So the 
control-character didn't reach my mail program. In which case you could try to 
copy-past the code from this reply into a new file and see if that's gotten rid 
of the control-character (presuming that caused the problem).

Of course, if it's eg a known problem in Jython, I can't really tell you, 
because I simply don't know Jython. But try clean code for a start.

HTH,

  Evert


> 
> Thank you,
> Shawn
> 
> 

import os, sys 
from stat import *

def walktree(top, callback):
   '''recursively descend the directory tree rooted at top,
   calling the callback function for each regular file'''
   for f in os.listdir(top):
   pathname = os.path.join(top, f) 
   mode = os.stat(pathname)[ST_MODE] 
   if S_ISDIR(mode):
   # It's a directory, recurse into it 
   walktree(pathname, callback)
   elif S_ISREG(mode):
   # It's a file, call the callback function 
   callback(pathname)
   else:
   # Unknown file type, print a message 
   print 'Skipping %s' % pathname

def visitfile(file):
   print 'visiting', file

if __name__ == '__main__':
   walktree(sys.argv[1], visitfile)

> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread Sayth Renshaw
> Message: 5
> Date: Thu, 07 Oct 2010 20:08:10 +0200
> From: Knacktus 
> To: tutor@python.org
> Subject: Re: [Tutor] IDE for Python
> Message-ID: <4cae0c8a.4040...@googlemail.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro:
> > Dear List;
> >
> > In your experience what is the best IDE for Python?
> >
> I'm using Wing IDE. Very good overall package. I like especially the
> debug probe, which is like an interactive shell in the current stack. To
> me it's a good balance between features and learning curve. The only
> thing I really miss is refactoring support.
> That's why I'm currently checking out PyCharm, which is about to be
> released (currently release candidate). It's from the company that
> created IntelliJ. PyCharm is in my opinion by far the most feature-rich
> Python IDE, looks very impressive so far. The only drawback is that it's
> written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a
> while but you get over it, once you discover all those wicked features
> ;-)). But Wing isn't excactly eye-candy either.
> Both are commercial, but if you code a lot it's worth it. Check out the
> offerings. (I think both are free for Open Source Projects.)
>
> I also tried the free PyDev (an Eclipse plugin), which is very complete
> as well, but I don't get along with the Eclipse world.
>
> So, check out Wing and PyCharm.
>
> Cheers,
>
> JJ
>
>
> --
>
>
I really like Spe, Stani's Python editor found here
http://pythonide.blogspot.com/ .

It really manages to keep everything clear open and accessible whilst still
providing a tonne of features and support. There are some intro videos
avaiable here.
http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE .

I have never seen Pycharm as JJ suggested so am going to check it out, I
only recently installed Eclipse Helios with Aptana 3 which includes Pydev
and Django support so I can't really offer an indepth opinion but it is an
open source community with a lot of support which if you are learning am ide
as well as a language could prove very helpful. A lot of editors don't have
much in the way of documentation or community which I think is important.

Another verygood option I like and have used a lot is DrPython
http://drpython.sourceforge.net/.

Ultimately though for keep the ide learning curve low and providing power I
still go for Spe, if I had the money I would definitely look at wing ide.

Cheers

Sayth
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PYTHON QUOTES ISSUE

2010-10-07 Thread Alan Gauld


"Susana Iraiis Delgado Rodriguez"  
wrote



How can I write a statement to execute the following:
C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr 
R1G-GEODESIA.shp -where

"LAYER = 'R1G-GEODESIA'" tapalpa_05_plani_point.dbf,
I want my uotput to look like this.


erm, like what? The command string above?
So where is this output coming from?
Or are you talking about the output from the command?


Instead I'm getting this
C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr
T21-PUENTES.shp -where LAYER=+line tapalpa_05_plani_line.shp


The difference is that you seem to be getting +line as a literal
string instead of the variables value. Is that the problem?



In miy code line is a string given by the user:

for line in open("unico.txt", "r").readlines():


You donlt need the readlines you can iterate over the open
file.



p = subprocess.Popen(['C:/Archivos de
programa/FWTools2.4.7/bin/ogr2ogr', line+'.shp', '-where', 
"LAYER='line'",

b+'.shp'])


Notice that you have quotes around the line following LAYER=


Any suggestions?


remove the quotes? And shift the double quotes after the = sign.

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] new turtle module

2010-10-07 Thread Alan Gauld


"roberto"  wrote

is it correct to overwrite the turtle.py and turtle.pyc files 


I'd overwrite the .py file but get Python to generate a new .pyc 
for you just to ensure compatibility.


just type import turtle at the >>> prompt.

HTH,

Alan G

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread Mark Weil
There's also eric. It's geared towards pyqt slightly, but I do a lot of
wxpython development in it as well.
It's got project management and svn plugins, too.
http://eric-ide.python-projects.org/

On Fri, Oct 8, 2010 at 6:01 AM, Sayth Renshaw wrote:

>
> Message: 5
>> Date: Thu, 07 Oct 2010 20:08:10 +0200
>> From: Knacktus 
>> To: tutor@python.org
>> Subject: Re: [Tutor] IDE for Python
>> Message-ID: <4cae0c8a.4040...@googlemail.com>
>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>
>>
>> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro:
>> > Dear List;
>> >
>> > In your experience what is the best IDE for Python?
>> >
>> I'm using Wing IDE. Very good overall package. I like especially the
>> debug probe, which is like an interactive shell in the current stack. To
>> me it's a good balance between features and learning curve. The only
>> thing I really miss is refactoring support.
>> That's why I'm currently checking out PyCharm, which is about to be
>> released (currently release candidate). It's from the company that
>> created IntelliJ. PyCharm is in my opinion by far the most feature-rich
>> Python IDE, looks very impressive so far. The only drawback is that it's
>> written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a
>> while but you get over it, once you discover all those wicked features
>> ;-)). But Wing isn't excactly eye-candy either.
>> Both are commercial, but if you code a lot it's worth it. Check out the
>> offerings. (I think both are free for Open Source Projects.)
>>
>> I also tried the free PyDev (an Eclipse plugin), which is very complete
>> as well, but I don't get along with the Eclipse world.
>>
>> So, check out Wing and PyCharm.
>>
>> Cheers,
>>
>> JJ
>>
>>
>> --
>>
>>
> I really like Spe, Stani's Python editor found here
> http://pythonide.blogspot.com/ .
>
> It really manages to keep everything clear open and accessible whilst still
> providing a tonne of features and support. There are some intro videos
> avaiable here.
> http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE .
>
> I have never seen Pycharm as JJ suggested so am going to check it out, I
> only recently installed Eclipse Helios with Aptana 3 which includes Pydev
> and Django support so I can't really offer an indepth opinion but it is an
> open source community with a lot of support which if you are learning am ide
> as well as a language could prove very helpful. A lot of editors don't have
> much in the way of documentation or community which I think is important.
>
> Another verygood option I like and have used a lot is DrPython
> http://drpython.sourceforge.net/.
>
> Ultimately though for keep the ide learning curve low and providing power I
> still go for Spe, if I had the money I would definitely look at wing ide.
>
> Cheers
>
> Sayth
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDE for Python

2010-10-07 Thread aug dawg
I like gedit alot. It has a nice amount of plugins, like a filebrowser and
various embedded terminals. Check it out. It's available for all platforms.
It's at gedit.org.


On Thu, Oct 7, 2010 at 7:42 PM, Mark Weil  wrote:

> There's also eric. It's geared towards pyqt slightly, but I do a lot of
> wxpython development in it as well.
> It's got project management and svn plugins, too.
> http://eric-ide.python-projects.org/
>
> On Fri, Oct 8, 2010 at 6:01 AM, Sayth Renshaw wrote:
>
>>
>> Message: 5
>>> Date: Thu, 07 Oct 2010 20:08:10 +0200
>>> From: Knacktus 
>>> To: tutor@python.org
>>> Subject: Re: [Tutor] IDE for Python
>>> Message-ID: <4cae0c8a.4040...@googlemail.com>
>>> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>>>
>>>
>>> Am 07.10.2010 17:23, schrieb Juan Jose Del Toro:
>>> > Dear List;
>>> >
>>> > In your experience what is the best IDE for Python?
>>> >
>>> I'm using Wing IDE. Very good overall package. I like especially the
>>> debug probe, which is like an interactive shell in the current stack. To
>>> me it's a good balance between features and learning curve. The only
>>> thing I really miss is refactoring support.
>>> That's why I'm currently checking out PyCharm, which is about to be
>>> released (currently release candidate). It's from the company that
>>> created IntelliJ. PyCharm is in my opinion by far the most feature-rich
>>> Python IDE, looks very impressive so far. The only drawback is that it's
>>> written in Java and has a Swing GUI ;-) (ouuch, it hurts your eyes for a
>>> while but you get over it, once you discover all those wicked features
>>> ;-)). But Wing isn't excactly eye-candy either.
>>> Both are commercial, but if you code a lot it's worth it. Check out the
>>> offerings. (I think both are free for Open Source Projects.)
>>>
>>> I also tried the free PyDev (an Eclipse plugin), which is very complete
>>> as well, but I don't get along with the Eclipse world.
>>>
>>> So, check out Wing and PyCharm.
>>>
>>> Cheers,
>>>
>>> JJ
>>>
>>>
>>> --
>>>
>>>
>> I really like Spe, Stani's Python editor found here
>> http://pythonide.blogspot.com/ .
>>
>> It really manages to keep everything clear open and accessible whilst
>> still providing a tonne of features and support. There are some intro videos
>> avaiable here.
>> http://showmedo.com/videotutorials/series?name=PythonDevelopmentWithSPE .
>>
>> I have never seen Pycharm as JJ suggested so am going to check it out, I
>> only recently installed Eclipse Helios with Aptana 3 which includes Pydev
>> and Django support so I can't really offer an indepth opinion but it is an
>> open source community with a lot of support which if you are learning am ide
>> as well as a language could prove very helpful. A lot of editors don't have
>> much in the way of documentation or community which I think is important.
>>
>> Another verygood option I like and have used a lot is DrPython
>> http://drpython.sourceforge.net/.
>>
>> Ultimately though for keep the ide learning curve low and providing power
>> I still go for Spe, if I had the money I would definitely look at wing ide.
>>
>> Cheers
>>
>> Sayth
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor