Re: [Tutor] Bad time to get into Python?

2008-02-04 Thread Eric Brunson

Dotan Cohen wrote:

On 03/02/2008, Kent Johnson <[EMAIL PROTECTED]> wrote:
  

I did a little research on the question of running the same script
 unmodified in Python 2.6 and 3.0. It seems that there is no consensus
 opinion and it may depend on your personal tolerance for compatibility
 cruft. Here is a c.l.py thread of interest:
 
http://groups.google.com/group/comp.lang.python/browse_thread/thread/4e0a71d86e80d0f9/8e7fb527e32d3064?hl=en&;


 Kent




Like I mentioned earlier, I'm more interested in my learning being 3.x
compatible, not my scripts. If all I need to do is learn to print("")
instead of print"" then that's fine.
  


Basically, if you follow a few simple rules you'll avoid 99% of 3.0 
incompatibilities:


1) Always use:

   print( "like it was a function" )
   rather than:
   print "like it was a statement"

2) Always use:

   class NewStyle( object ):
   rather than:
   class OldStyle():

3) Never try to be clever with side effects of internal implementations 
of language



Pretty much everything else you learn in 2.5 will be applicable in 3.0.

(Others on the list, please feel free to extend my rules with things 
that you feel will be important)



Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie code review please!

2008-02-04 Thread Kent Johnson
Tyler Smith wrote:

> That cleaned up a lot. However, I couldn't figure out a way to do
> random.choice(word_hash[(w1, w2)]) on a dict with set-type values.

Ah, sorry; random.choice() needs an indexable sequence. Try
   w1, w2 = w2, random.sample(word_hash[(w1, w2)], 1)[0]

random.sample() works with any iterable, not just sequences. It returns 
a list, hence the indexing [0].

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] os.system() problem

2008-02-04 Thread Eric Brunson
dave selby wrote:
> Hi all,
>
> I am not sure if this is a Python or bash issue :).
>
> In bash if I execute 'motion' with the following ...
>
> [EMAIL PROTECTED]:~/.kde/share/apps/kmotion$ motion &> /dev/null &
> [1] 10734
> [EMAIL PROTECTED]:~/.kde/share/apps/kmotion$
>
> I get what I expect, a background job, however if I execute it from
> Python with an os.system ...
>
> os.system('motion &> /dev/null &')
>   

This happens because &> and & are shell constructs, they are bash 
specific shell syntax for "redirect stderr and stdout" and "put this job 
in the background".  But os.system simply calls the OS's "system(3)" 
call, which under linux calls "/bin/sh".  If you read the docs for bash, 
calling it as "sh" results in POSIX compliance mode and falls back to 
Bourne shell's less rich syntax, so it doesn't understand the "&>" 
construct.  If I had to guess at the parsing, I imagine it runs the 
"motion &" as one process in the background, then "> /dev/null &" as a 
second.

Long story short, look at this page:
http://docs.python.org/lib/node537.html

> I get tons of output to the BASH shell ...
>
> [0] Processing thread 0 - config file /etc/motion/motion.conf
> [0] Processing config file /etc/motion/motion.1.conf
> [0] Processing config file /etc/motion/motion.2.conf
> [1] Thread is from /etc/motion/motion.1.conf
> [2] Thread is from /etc/motion/motion.2.conf
> [1] Thread started
> [2] Thread started
> [1] File of type 2 saved to: /var/lib/motion/20080203/01/tmp/175253.jpg
> ...etc ...
>
> I just can't work out why this is happening & how to stop it ?. Any ideas ?
>
> Cheers
>
> Dave
>
>
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie code review please!

2008-02-04 Thread Tyler Smith
On Sun, Feb 03, 2008 at 04:35:08PM -0500, Kent Johnson made several
helpful suggestions:

Thanks!

That cleaned up a lot. However, I couldn't figure out a way to do
random.choice(word_hash[(w1, w2)]) on a dict with set-type values. The
closest I could get was word_hash[(w1, w2)].pop(), but then I need to
add a few lines to put the value back into the set afterwards. Is
there a way to randomly lookup a value in a set without removing it
from the set? I had better success with collections.defaultdict(list),
as pasted below.

Cheers,

Tyler

#! /usr/bin/python2.5

import sys, random, collections

word_max = 1000

# Read in file
infile = open(sys.argv[1], 'r')
ifstring = infile.read()

# break file into words
iflist = ifstring.split()

# build hash of form (prefix1, prefix2): [word]
word_hash = collections.defaultdict(list)
for i in range(len(iflist) - 2):
tk = tuple(iflist[i:i+2])
tv = iflist[i+2]
if tv not in word_hash[tk]:
word_hash[tk].append(tv)

word_hash[(iflist[-2], iflist[-1])].append('\n')

# output first two words
w1, w2 = iflist[0:2]
print w1, w2,

# generate new words from hash
for word_num in range(word_max):
w1, w2 = w2, random.choice(word_hash[(w1, w2)])
print w2,
if w2 == '\n':
break

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie code review please!

2008-02-04 Thread tyler
On Sun, Feb 03, 2008 at 04:35:08PM -0500, Kent Johnson made several
helpful suggestions:

Thanks!

That cleaned up a lot. However, I couldn't figure out a way to do
random.choice(word_hash[(w1, w2)]) on a dict with set-type values. The
closest I could get was word_hash[(w1, w2)].pop(), but then I need to
add a few lines to put the value back into the set afterwards. Is
there a way to randomly lookup a value in a set without removing it
from the set? I had better success with collections.defaultdict(list),
as pasted below.

Cheers,

Tyler

#! /usr/bin/python2.5

import sys, random, collections

word_max = 1000

# Read in file
infile = open(sys.argv[1], 'r')
ifstring = infile.read()

# break file into words
iflist = ifstring.split()

# build hash of form (prefix1, prefix2): [word]
word_hash = collections.defaultdict(list)
for i in range(len(iflist) - 2):
tk = tuple(iflist[i:i+2])
tv = iflist[i+2]
if tv not in word_hash[tk]:
word_hash[tk].append(tv)

word_hash[(iflist[-2], iflist[-1])].append('\n')

# output first two words
w1, w2 = iflist[0:2]
print w1, w2,

# generate new words from hash
for word_num in range(word_max):
w1, w2 = w2, random.choice(word_hash[(w1, w2)])
print w2,
if w2 == '\n':
break

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-04 Thread Andre Roberge
On Feb 4, 2008 1:26 PM, Eric Brunson <[EMAIL PROTECTED]> wrote:
>
>  Dotan Cohen wrote:
>
>  Like I mentioned earlier, I'm more interested in my learning being 3.x
> compatible, not my scripts. If all I need to do is learn to print("")
> instead of print"" then that's fine.
>
>
>  Basically, if you follow a few simple rules you'll avoid 99% of 3.0
> incompatibilities:
>
>  1) Always use:
>
> print( "like it was a function" )
>  rather than:
>  print "like it was a statement"
>  2) Always use:
>
> class NewStyle( object ):
>  rather than:
>  class OldStyle():
>  3) Never try to be clever with side effects of internal implementations of
> language
>
>
>  Pretty much everything else you learn in 2.5 will be applicable in 3.0.
>
>  (Others on the list, please feel free to extend my rules with things that
> you feel will be important)
>

No, this seems about right.  For the record, I have attempted an
experiment to see if I could make my program (Crunchy) run under 2.4,
2.5, 3.0a1 and 3.0a2 simulatenously. This is a program with about 40
different modules, using a number of other modules from the standard
library.   I managed to get everything working almost perfectly using
3.0a1 and about 95% under 3.0a2 - I am convinced that, with a bit more
effort, I could have gotten everything working under all 4 Python
versions.

I have complete unit tests for about 20 of the modules I wrote and it
was very easy to make them work (with no errors) under all 4 Python
versions.   There are only a small number of places where the
transition from 2.x to 3.0 is going to be tricky - and most of these
are related to dealings with strings and unicode characters, something
not everyone has to deal with.

So, based on actual experience, I am confident in telling anyone
interested that they should not fear learning Python 2.x (thinking it
might become obsolete) as 99% of your code (excluding print
statements) is probably going to work unchanged.

André
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-04 Thread Dotan Cohen
On 03/02/2008, Kent Johnson <[EMAIL PROTECTED]> wrote:
> I did a little research on the question of running the same script
>  unmodified in Python 2.6 and 3.0. It seems that there is no consensus
>  opinion and it may depend on your personal tolerance for compatibility
>  cruft. Here is a c.l.py thread of interest:
>  
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/4e0a71d86e80d0f9/8e7fb527e32d3064?hl=en&;
>
>
>  Kent
>

Like I mentioned earlier, I'm more interested in my learning being 3.x
compatible, not my scripts. If all I need to do is learn to print("")
instead of print"" then that's fine.

Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bad time to get into Python?

2008-02-04 Thread Dotan Cohen
On 04/02/2008, Andre Roberge <[EMAIL PROTECTED]> wrote:
>  I have complete unit tests for about 20 of the modules I wrote and it
>  was very easy to make them work (with no errors) under all 4 Python
>  versions.   There are only a small number of places where the
>  transition from 2.x to 3.0 is going to be tricky - and most of these
>  are related to dealings with strings and unicode characters, something
>  not everyone has to deal with.
>

Since my language is Hebrew, I will be dealing with unicode characters a lot.

Dotan Cohen

http://what-is-what.com
http://gibberish.co.il
א-ב-ג-ד-ה-ו-ז-ח-ט-י-ך-כ-ל-ם-מ-ן-נ-ס-ע-ף-פ-ץ-צ-ק-ר-ש-ת

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] newbie code review please!

2008-02-04 Thread tyler
On Mon, Feb 04, 2008 at 03:27:35PM -0400, tyler wrote:
> On Mon, Feb 04, 2008 at 11:56:17AM -0500, Kent Johnson wrote:
> > Tyler Smith wrote:
> >
> >> That cleaned up a lot. However, I couldn't figure out a way to do
> >> random.choice(word_hash[(w1, w2)]) on a dict with set-type values.
> >
> > Ah, sorry; random.choice() needs an indexable sequence. Try
> >   w1, w2 = w2, random.sample(word_hash[(w1, w2)], 1)[0]
> >
> > random.sample() works with any iterable, not just sequences. It returns  
> > a list, hence the indexing [0].
> >

Excellent, thanks!
 
Tyler
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] good reference book recommendations

2008-02-04 Thread tyler
Hi,

At the risk of beating a well-dead horse, I'm looking for book
suggestions. I've already got Core Python Programming, but I find it
doesn't quite suit my needs. I'd like a comprehensive and *concise*
reference to the core language and standard libraries. It looks like
Beazely's Essential Reference and the Martelli's Nutshell book are
both aimed to fill this role - any reason to choose one over the
other? The free library reference would almost do for me, except that
I want a hardcopy and it's a big document to print out.

Thanks!

Tyler

-- 
I never loan my books, for people never return them. The only books remaining in
my library are those I’ve borrowed from others.
   --unknown
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PHP & Python suggestions....

2008-02-04 Thread Chris Fuller
On Sunday 03 February 2008 17:35, GTXY20 wrote:
> Hi all,
>
> First off let me say how helpful and informative this mailing list is. It
> is very much appreciated.
>
> Anyway, I am looking for some suggestions for reading up on how to call
> Python from PHP scripts, specifically calling from a PHP web application -
> PHP will call python script with some arguments and python will run on the
> server and return the results within another PHP page.
>
> Once again thanks.
>
> GTXY20.

You can also use python in server-side scripting, at least with apache and 
mod_python.  You can use it the same way as PHP.  This might not be suitable 
for what you need to do, but if you could do it this way, it would probably 
be faster.

Cheers
Chris Fuller

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] good reference book recommendations

2008-02-04 Thread bhaaluu
Being a book snob, I'd go for the O'Reilly Nutshell book
over the SAMS Essential Reference. I've always had good
luck with books published by O'Reilly. I have neither of
the books you asked about, because I use online docs.
I don't need no steenkin' dead tree Python reference. 8^P
Actually, I've heard several recommendations for the Nutshell
book, but never heard of the Beaszely book. Sprinkle with
salt. Go to Borders or B&N and check them out (if they're
on the shelf). It shouldn't take more than a few minutes
for you to see which one fits you!
-- 
b h a a l u u at g m a i l dot c o m
"You assist an evil system most effectively by obeying its
orders and decrees. An evil system never deserves such
allegiance.  Allegiance to it means partaking of the evil.
A good person will resist an evil system with his or her
whole soul." [Mahatma Gandhi]

On Feb 4, 2008 6:54 PM, tyler <[EMAIL PROTECTED]> wrote:
> Hi,
>
> At the risk of beating a well-dead horse, I'm looking for book
> suggestions. I've already got Core Python Programming, but I find it
> doesn't quite suit my needs. I'd like a comprehensive and *concise*
> reference to the core language and standard libraries. It looks like
> Beazely's Essential Reference and the Martelli's Nutshell book are
> both aimed to fill this role - any reason to choose one over the
> other? The free library reference would almost do for me, except that
> I want a hardcopy and it's a big document to print out.
>
> Thanks!
>
> Tyler
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PHP & Python suggestions....

2008-02-04 Thread Michael Langford
OT Aside:
Anyone ever used PSP pages? Seems like a good substitute for PHPish
tasks, but I've not seen many users.

As to the actual question, PHP can call python through a variety of
means. One such one I'm familiar with is XMLRPC.

PHP:
http://phpxmlrpc.sourceforge.net/

Python:
http://www.ibm.com/developerworks/library/ws-pyth10.html (servers at
the bottom).

XMLRPC can be slow. Make sure its fast enough for what
you're doing by running a benchmark. But if it is fast enough, the
python libraries at least are quite magically simple as far as cross
language programming can be.

--Michael

On Feb 4, 2008 10:43 AM, Chris Fuller <[EMAIL PROTECTED]> wrote:
>
> On Sunday 03 February 2008 17:35, GTXY20 wrote:
> > Hi all,
> >
> > First off let me say how helpful and informative this mailing list is. It
> > is very much appreciated.
> >
> > Anyway, I am looking for some suggestions for reading up on how to call
> > Python from PHP scripts, specifically calling from a PHP web application -
> > PHP will call python script with some arguments and python will run on the
> > server and return the results within another PHP page.
> >
> > Once again thanks.
> >
> > GTXY20.
>
> You can also use python in server-side scripting, at least with apache and
> mod_python.  You can use it the same way as PHP.  This might not be suitable
> for what you need to do, but if you could do it this way, it would probably
> be faster.
>
> Cheers
> Chris Fuller
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.RowdyLabs.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] PHP & Python suggestions....

2008-02-04 Thread Eric Brunson
Michael Langford wrote:
> OT Aside:
> Anyone ever used PSP pages? Seems like a good substitute for PHPish
> tasks, but I've not seen many users.
>   

I have, but I find generating my HTML with classes I wrote to be more to 
my tastes.  I did PHP for years and I'm just not a fan of the way it 
mixes code and HTML.  I think that model works better when  you have 
someone doing the layout and someone else doing the server side code.

> As to the actual question, PHP can call python through a variety of
> means. One such one I'm familiar with is XMLRPC.
>
> PHP:
> http://phpxmlrpc.sourceforge.net/
>
> Python:
> http://www.ibm.com/developerworks/library/ws-pyth10.html (servers at
> the bottom).
>
> XMLRPC can be slow. Make sure its fast enough for what
> you're doing by running a benchmark. But if it is fast enough, the
> python libraries at least are quite magically simple as far as cross
> language programming can be.
>
> --Michael
>
> On Feb 4, 2008 10:43 AM, Chris Fuller <[EMAIL PROTECTED]> wrote:
>   
>> On Sunday 03 February 2008 17:35, GTXY20 wrote:
>> 
>>> Hi all,
>>>
>>> First off let me say how helpful and informative this mailing list is. It
>>> is very much appreciated.
>>>
>>> Anyway, I am looking for some suggestions for reading up on how to call
>>> Python from PHP scripts, specifically calling from a PHP web application -
>>> PHP will call python script with some arguments and python will run on the
>>> server and return the results within another PHP page.
>>>
>>> Once again thanks.
>>>
>>> GTXY20.
>>>   
>> You can also use python in server-side scripting, at least with apache and
>> mod_python.  You can use it the same way as PHP.  This might not be suitable
>> for what you need to do, but if you could do it this way, it would probably
>> be faster.
>>
>> Cheers
>> Chris Fuller
>>
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>> 
>
>
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor