Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-17 Thread eryksun
On Wed, Apr 17, 2013 at 1:11 AM, Jim Mooney  wrote:
 2to3 loops.py
>   File "", line 1
> 2to3 loops.py
>^
> SyntaxError: invalid syntax

The script 2to3.py is run from the system's terminal/console shell
(e.g. cmd or PowerShell on Windows), not the python shell.

If the current directory has the file "loops.py" and Python is
installed in C:\Python33, then try running the following in cmd:

C:\Python33\Tools\Scripts\2to3.py -w loops.py

If that fails, try running 3.3 explicitly:

C:\Python33\python.exe C:\Python33\Tools\Scripts\2to3.py -w loops.py

The -w option tells 2to3 to write the ported version back to loops.py.
The original will be backed up to loops.py.bak.



Running Scripts

Python 3.3 is distributed on Windows with a "py" launcher that's
associated with .py files. This should be set up by the installer as
follows:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\Windows\py.exe" "%1" %*

The launcher parses a script's shebang line, which is a special
comment on the first line. A shebang begins with an exclamation point
(i.e. a "bang"), followed by the command used to run the script. The
launcher accepts a literal command (e.g. #!C:\Python33\python.exe), a
preset virtual command, or an alias you've added to the configuration
ini.

For example, this shebang runs a script in the default Python version:

#!/usr/bin/env python

"/usr/bin/env" is a Unix-ism included for portability. You can simply
use "#!python3" if you want the script to run in 3.x on Windows.

2.x is the default if it's installed. You can configure the launcher
to instead use 3.x as the default. Just edit the configuration ini:

>notepad %localappdata%\py.ini

to include the following:

[defaults]
python=3

For convenience, you can add your script directories to the
environment PATH. You can also append ;.PY to the environment PATHEXT
to be able to run "script.py" as just "script", without the .py
extension.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] regex to promote Py 2 to Py 3?

2013-04-17 Thread Walter Prins
Hi,


On 17 April 2013 06:11, Jim Mooney  wrote:

> > Generally the 2to3 script does an OK job. If you're using Windows it's
> > [Python_Dir]\Tools\Scripts\2to3.py.
> >
> > http://docs.python.org/3/library/2to3
> 
>
> Thanks. I didn't know where to find it and thought I had to install it.
>
> I opened a command window in my Python33/progs dir, started Python,
> and ran it on a tiny test program called loops.py but I get "invalid
> syntax" Any idea why?:
>
> >>> 2to3 loops.py
>   File "", line 1
> 2to3 loops.py
>^
> SyntaxError: invalid syntax


As additional background to eryksun's response, please read this:
http://www.voidspace.org.uk/python/articles/command_line.shtml

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


Re: [Tutor] Tutor Digest, Vol 110, Issue 74

2013-04-17 Thread Jim Mooney
> The script 2to3.py is run from the system's terminal/console shell
> (e.g. cmd or PowerShell on Windows), not the python shell.

Yay, it worked! A decade of Windows and I'm back to the DOS Command
Line ;')  Well, it worked the second time.I thought I could do without
the -w but nothing happened. Now I'll go edit Path so I'm not typing
in all those directories.  2to3 doesn't seem to do anything without
the -w, which I find a tad puzzling.

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


Re: [Tutor] Tutor Digest, Vol 110, Issue 74

2013-04-17 Thread Timo

Op 17-04-13 18:09, Jim Mooney schreef:

The script 2to3.py is run from the system's terminal/console shell
(e.g. cmd or PowerShell on Windows), not the python shell.

Yay, it worked! A decade of Windows and I'm back to the DOS Command
Line ;')
If you want to program, the command line is your friend. You'll see it 
alot more.



Well, it worked the second time.I thought I could do without
the -w but nothing happened. Now I'll go edit Path so I'm not typing
in all those directories.  2to3 doesn't seem to do anything without
the -w, which I find a tad puzzling.

From the 2to3 --help:

  -w, --write   Write back modified files

So it's normal nothing happens to your files without the -w argument. It 
just prints the changes to stdout.


Timo



Jim
___
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] Sys.argv read parameters

2013-04-17 Thread Danilo Chilene
Dear Python Tutor,

I have the code below(file.py):

import sys

a = 'This is A'
b = 'This is B'
c = 'This is C'

for i in sys.argv[1]:
if sys.argv[1] == 'a':
print a
if sys.argv[1] == 'b':
print b
if sys.argv[1] == 'c':
print c

I run python file.py a and returns the var a, so far so good.

The problem is that i have a bunch of vars(like a to z), how I can handle
this in a pythonic way?

Best Regards,

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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Danny Yoo
What's the part that's "changing"?  What's the part that stays the same?

I would recommend thinking of this in terms of a function.

Can you write a function that consumes a letter l and returns the
string "This is ..." where "..." is the uppercased l?

As an example of a simple function on strings:

##
## double: string -> string
def double(x):
return x + x

## For example, a blast from the past:
print "The Noid says: " + double("pizza")
##


There are functions in Python standard library that know how to do
things to strings.  See:

http://docs.python.org/2/library/stdtypes.html#string-methods
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Dave Angel

On 04/17/2013 03:27 PM, Danilo Chilene wrote:

Dear Python Tutor,

I have the code below(file.py):

import sys

a = 'This is A'
b = 'This is B'
c = 'This is C'

for i in sys.argv[1]:
 if sys.argv[1] == 'a':
 print a
 if sys.argv[1] == 'b':
 print b
 if sys.argv[1] == 'c':
 print c


Since this is a loop, I'll assume that argv[1] was supposed to permit 
multiple characters.  If so, your loop is buggy.  And if not, then you 
don't want a loop.




I run python file.py a and returns the var a, so far so good.


What do you want to happen if you say
  python file.py  ac




The problem is that i have a bunch of vars(like a to z), how I can handle
this in a pythonic way?



Use a dictionary (dict).  Instead of separate 'variables' use one dict. 
 (With version 2.7)


import sys

mystrings = { "a" : "This is A",
  "b" : "This is B",
  "c" : "This is C",
  "d" : "This is some special case D",
}

for parm in sys.argv[1]:
if parm in mystrings:
print mystrings[parm]
else:
print "Invalid parm"

If this isn't what you wanted, then you'll have to make it clearer.

1) tell us what version of python
2) supply us with a non-buggy program,
3) be much more specific about what change you want.  You don't want 
more vars, you presumably want a way to avoid having more vars.  Danny 
assumed you wanted those exact strings, for a, b, and c, while i assumed 
that those were just simple examples.



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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Marc Tompkins
On Wed, Apr 17, 2013 at 1:03 PM, Danny Yoo  wrote:


> ##
> ## double: string -> string
> def double(x):
> return x + x
>
>

> ## For example, a blast from the past:
> print "The Noid says: " + double("pizza")
> ##
>

I'm sorry, but this is a bug.  The Noid tried to make Domino's pizzas get
cold before they arrived; Little Caesar said "Pizza, pizza!"


> ## For example, a blast from the past:
> print "Caesar says: " + double("pizza")
> ##
>

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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Danilo Chilene
Hello Dave,

1) I'm using Python 2.7
2) The program wasn't suppose to really work, was just a example.
3) You assumed correct.

That's was what I looking for, worked like charm.

Thanks!


On Wed, Apr 17, 2013 at 5:18 PM, Dave Angel  wrote:

> On 04/17/2013 03:27 PM, Danilo Chilene wrote:
>
>> Dear Python Tutor,
>>
>> I have the code below(file.py):
>>
>> import sys
>>
>> a = 'This is A'
>> b = 'This is B'
>> c = 'This is C'
>>
>> for i in sys.argv[1]:
>>  if sys.argv[1] == 'a':
>>  print a
>>  if sys.argv[1] == 'b':
>>  print b
>>  if sys.argv[1] == 'c':
>>  print c
>>
>
> Since this is a loop, I'll assume that argv[1] was supposed to permit
> multiple characters.  If so, your loop is buggy.  And if not, then you
> don't want a loop.
>
>
>
>> I run python file.py a and returns the var a, so far so good.
>>
>
> What do you want to happen if you say
>   python file.py  ac
>
>
>
>
>> The problem is that i have a bunch of vars(like a to z), how I can handle
>> this in a pythonic way?
>>
>>
> Use a dictionary (dict).  Instead of separate 'variables' use one dict.
>  (With version 2.7)
>
> import sys
>
> mystrings = { "a" : "This is A",
>   "b" : "This is B",
>   "c" : "This is C",
>   "d" : "This is some special case D",
> }
>
> for parm in sys.argv[1]:
> if parm in mystrings:
> print mystrings[parm]
> else:
> print "Invalid parm"
>
> If this isn't what you wanted, then you'll have to make it clearer.
>
> 1) tell us what version of python
> 2) supply us with a non-buggy program,
> 3) be much more specific about what change you want.  You don't want more
> vars, you presumably want a way to avoid having more vars.  Danny assumed
> you wanted those exact strings, for a, b, and c, while i assumed that those
> were just simple examples.
>
>
> --
> DaveA
>
> __**_
> 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] Sys.argv read parameters

2013-04-17 Thread Danny Yoo
Wait.  If the solution that we're stopping at to use a hashtable here,
that's not quite right. A good solution to this should be _much_
shorter, on the order of a one-liner.  Hashtables are great, but
they're not the answer to everything.


If we're doing something like:

a -> "This is A"
b -> "This is B"
...
z -> "This is Z"

a good solution to this isn't to make a hashtable with 26 entries, and
do a lookup: the approach is more something like this:


###
def getMessage(letter):
return "This is " + letter.upper()
###

For example:

#
>>> getMessage('a')
'This is A'
>>> getMessage('b')
'This is B'
>>> getMessage('z')
'This is Z'
#


The size of your program should be proportional to how "differently"
it has to act on input.  In this case, the program is pretty much the
same on all inputs, modulo the very last chunk of the message.  That's
why thinking about this in terms of functions is crucial: if you
don't, the code ends up being larger than it deserves.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Dave Angel

On 04/17/2013 04:49 PM, Danny Yoo wrote:

Wait.  If the solution that we're stopping at to use a hashtable here,
that's not quite right.


Nothing wrong with a dict, if a proper specification of the problem were 
available.  Notice that in my solution, the messages were not all 
trivially related to each other.


I made some guesses, and you made some different ones.  The OP is happy. 
 Let it rest.




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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Danny Yoo
Yup!  Sorry about my tone for its stridency.  But I just got concerned
that the original poster seemed content about turning the chain of if
statements into a table definition.  To my mind, they're very closely
related, data and control.  I'm trying to push the OP to realizing
that if they are doing this, they are still enumerating all the
decisions.  Maybe it's not in the form of a chain of if statements,
but it's still reflected in the structure of the table.

(It's the same reason I get concerned when I see students not
recognize the similarities between if statements and polymorphism:
it's all about transmogrifying control flow and data structures.)

Let me say that if there's non-uniformity among the choices, then the
table lookup approach (a dispatch table) is probably the right tool.
If there's uniformity, then a computational approach should be
considered.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PyCamp Registration Open for Columbus, Toronto, and Oshkosh

2013-04-17 Thread Chris Calloway
Registration is open for three upcoming PyCamps produced by the Triangle 
Python Users Group:


- A five-day PyOhio PyCamp hosted by the Ohio State University Open 
Source Club, July 22-26, 2013 the week prior to the PyOhio regional 
Python conference weekend. PyCamp is a training program and sponsor of 
PyOhio:


http://trizpug.org/boot-camp/pyohio13/

- A five-day Toronto PyCamp hosted by the University of Toronto 
Department of Physics, August 12-16, 2013 the week after the PyCon 
Canada national Python conference weekend. PyCamp is a Diversity Sponsor 
of PyCon CA:


http://trizpug.org/boot-camp/torpy13/

- A three-day Wisconsin Mini-PyCamp hosted at the University of 
Wisconsin Oshkosh, June 2-4, 2013 as part of the Plone Symposium Midwest 
training days:


http://trizpug.org/boot-camp/wiscpy13/

PyCamp is the original, ultra-low-cost Python Boot Camp created by a 
user group for user groups. For beginners, PyCamp makes you productive 
so you can get your work done quickly. PyCamp emphasizes the features 
which make Python a simpler and more efficient language. Following along 
with example Python PushUps speeds your learning process. Become a 
self-sufficient Python developer at PyCamp. PyCamps are conducted in 
state of the art high technology classrooms on university campuses.


Registration will open soon also for two additional Triangle Python User 
Group boot camp events. An additional announcement will follow when 
registration opens for these events, but mark your calendars now:


- A five-day Seattle PyCamp hosted by the University of Washington 
Department of Computer Science and Engineering and UW Marketing, 
September 9-13, 2013. PyCamp is a sponsor of the Seattle Plone Users Group.


- A five-day special Python Web Programming boot camp hosted by the 
University of North Carolina Department of Marine Sciences, August 5-9, 
2013.


--
Sincerely,

Chris Calloway http://nccoos.org/Members/cbc
office: 3313 Venable Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Dave Angel

On 04/17/2013 05:15 PM, Danny Yoo wrote:

Yup!  Sorry about my tone for its stridency.  But I just got concerned
that the original poster seemed content about turning the chain of if
statements into a table definition.  To my mind, they're very closely
related, data and control.  I'm trying to push the OP to realizing
that if they are doing this, they are still enumerating all the
decisions.  Maybe it's not in the form of a chain of if statements,
but it's still reflected in the structure of the table.

(It's the same reason I get concerned when I see students not
recognize the similarities between if statements and polymorphism:
it's all about transmogrifying control flow and data structures.)

Let me say that if there's non-uniformity among the choices, then the
table lookup approach (a dispatch table) is probably the right tool.
If there's uniformity, then a computational approach should be
considered.




But look at the original question.  It was about having many variables. 
 And a dict is the way to take many variables out of the namespace and 
replacing them with one.  And it was unlikely to my mind that the final 
intent was to produce such a simplistic message for each letter.


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


[Tutor] Fwd: Sys.argv read parameters

2013-04-17 Thread Danny Yoo
Forwarding message to Python-tutor mailing list.  I did not realize
that your reply didn't show up for the rest of Python tutor.  Please
use Reply to All in the future.



-- Forwarded message --
From: Danilo Chilene 
Date: Wed, Apr 17, 2013 at 2:17 PM
Subject: Re: [Tutor] Sys.argv read parameters
To: Danny Yoo 


Hello Danny,

The part that is changing is just the sys.argv[1]
The vars will have always the same content.

What I don't want is to have like 255 'ifs' if I have 255 vars.


On Wed, Apr 17, 2013 at 5:03 PM, Danny Yoo  wrote:
>
> What's the part that's "changing"?  What's the part that stays the same?
>
> I would recommend thinking of this in terms of a function.
>
> Can you write a function that consumes a letter l and returns the
> string "This is ..." where "..." is the uppercased l?
>
> As an example of a simple function on strings:
>
> ##
> ## double: string -> string
> def double(x):
> return x + x
>
> ## For example, a blast from the past:
> print "The Noid says: " + double("pizza")
> ##
>
>
> There are functions in Python standard library that know how to do
> things to strings.  See:
>
> http://docs.python.org/2/library/stdtypes.html#string-methods
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Mark Lawrence

On 17/04/2013 20:27, Danilo Chilene wrote:

Dear Python Tutor,

I have the code below(file.py):

import sys

a = 'This is A'
b = 'This is B'
c = 'This is C'

for i in sys.argv[1]:
 if sys.argv[1] == 'a':
 print a
 if sys.argv[1] == 'b':
 print b
 if sys.argv[1] == 'c':
 print c

I run python file.py a and returns the var a, so far so good.

The problem is that i have a bunch of vars(like a to z), how I can
handle this in a pythonic way?

Best Regards,

Danilo



Further to earlier answers you might like to look at these modules for 
parsing items from sys.argv.


http://docs.python.org/3/library/argparse.html#module-argparse
http://docs.python.org/3/library/getopt.html#module-getopt

http://docs.python.org/3/library/optparse.html#module-optparse is 
available in Python 2.x but is deprecated as of Python 3.2.


There is also an awesome third party module here 
https://github.com/docopt/docopt


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: [Tutor] Fwd: Sys.argv read parameters

2013-04-17 Thread Dave Angel



-- Forwarded message --
From: Danilo Chilene 
Date: Wed, Apr 17, 2013 at 2:17 PM
Subject: Re: [Tutor] Sys.argv read parameters
To: Danny Yoo 


Hello Danny,

The part that is changing is just the sys.argv[1]
The vars will have always the same content.

What I don't want is to have like 255 'ifs' if I have 255 vars.



Please don't top post. Put your response AFTER the part you're quoting.



On Wed, Apr 17, 2013 at 5:03 PM, Danny Yoo  wrote:


What's the part that's "changing"?  What's the part that stays the same?



Are all the strings going to be identical except for the one letter?  If 
so, then read the better responses here, like those from Danny Yoo


So far, you haven't provided any useful input as to what you're really 
looking for.





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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Alan Gauld

On 17/04/13 20:27, Danilo Chilene wrote:


import sys

a = 'This is A'
b = 'This is B'
c = 'This is C'

for i in sys.argv[1]:
 if sys.argv[1] == 'a':
 print a
 if sys.argv[1] == 'b':
 print b
 if sys.argv[1] == 'c':
 print c

I run python file.py a and returns the var a, so far so good.

The problem is that i have a bunch of vars(like a to z), how I can
handle this in a pythonic way?


Use a dictionary.

myVars = {
'a':'This is A',
'b':'This is B',
'c':'This is C',
# as many more as needed
}

print myVars.get(sys.argv[1], 'No variable found')

Is that what you mean?

--
Alan G
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] new to threading, Queues

2013-04-17 Thread Chuck Mayers
Hi!  I was having a bit of nostalgia today, and thought I'd try to write a
simple, old school BBS.  I found the 'paramiko' library, and I've got
something I can SSH into that would have impressed my 1990's self.

I found some example code of the "threading" library, and I've managed to
use it to take multiple incoming connections at once.

I've never done any multithreaded programming, and everything I've ever
read is essentially "don't do it! It's really hard to get right!"

Everything I've read today says to use the Queue library
I can't seem to wrap my head around it, though. I've not seen any example
code that looks like what I'm trying to do.

Some examples of things I'd like to do:
Have one thread open a file (that all threads will want to read/write to)
and update it
Have one thread broadcast to all other threads a "chat message"


The only way I can think of, with the Queue library, would be the following
(I know this has to be the "wrong way" to do this):

1. create an object MultiTask which will have methods to handle thread
sensitive things like "update a file", etc
2. create one instance of it (we'll call it mtask) and add it to the queue
3. each thread, when it needs to, does queue.get() to get that instance
4. the thread then calls whatever it needs to, say mtask.update_users_file()
5. then, call queue.put(mtask) to put it back on the queue

so, there is only ever 1 object in the queue. I assume when another thread
tries to get() it, they block until the other threads are done with it.

Will this work? Is there a better way to do this?

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


Re: [Tutor] Sys.argv read parameters

2013-04-17 Thread Dave Angel

On 04/17/2013 08:12 PM, Alan Gauld wrote:

On 17/04/13 20:27, Danilo Chilene wrote:


import sys

a = 'This is A'
b = 'This is B'
c = 'This is C'

for i in sys.argv[1]:
 if sys.argv[1] == 'a':
 print a
 if sys.argv[1] == 'b':
 print b
 if sys.argv[1] == 'c':
 print c

I run python file.py a and returns the var a, so far so good.

The problem is that i have a bunch of vars(like a to z), how I can
handle this in a pythonic way?


Use a dictionary.

myVars = {
'a':'This is A',
'b':'This is B',
'c':'This is C',
# as many more as needed
}

print myVars.get(sys.argv[1], 'No variable found')

Is that what you mean?



What's the point?  The OP is not clarifying the "spec."  I posited a 
dict and supplied code several messages ago (about 4.5 hours), and he 
said "worked like a charm."  But since then he has given cryptic 
responses to others, and still not answered a few of the questions I 
asked then.



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