Re: [Tutor] pychecker: x is None or x == None

2005-08-13 Thread Alan G
>if x == None:
>do_something()
> 
> but then someone thought that we should really change these to
> 
>if x is None:
>do_something()
> 
> However. if you run pychecker on these two snippets of code, it
> complains about the second, and not the first:

Personally I'd use

if not x:
  do_something()

No idea what pyChecker thinks of that though! :-)
And of course it's slightly different to a specific check for None, 
it will catch 0, [], '',"", etc...

Of the two forms you suggest I'd stick with equality since the 
identity test (is) assumes that only one instance of None ever 
exists which could potentially change in some future exotic 
version of Python... You really are interested in the value 
of x not its identity.

Alan G.

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


[Tutor] python and xml

2005-08-13 Thread David Holland
What is a good way of using xml and python ?
I tried to install PyXML but I get this as an error
message :-
python setup.py
Traceback (most recent call last):
  File "setup.py", line 127, in ?
config_h_vars = parse_config_h(open(config_h))
IOError: [Errno 2] No such file or directory:
'/usr/include/python2.3/pyconfig.h

BTW there is a competition to make a sudoku solve £500
for the best entry see here if you are interested :-

http://www.linuxformat.co.uk/index.php?name=PNphpBB2&file=viewtopic&t=559&sid=2e3eec3b842c470b497aabc853ace21c



___ 
How much free photo storage do you get? Store your holiday 
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and xml

2005-08-13 Thread David Holland
I forgot to say that I am using knoppix 3.9



___ 
How much free photo storage do you get? Store your holiday 
snaps for FREE with Yahoo! Photos http://uk.photos.yahoo.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Invoking bash from within a python program

2005-08-13 Thread joe_schmoe
Hi Pythoneers

I'm sure that somewhere in the dim and distant past this was asked, but 
I'll be b***ered if I can find reference to it, and google searches 
haven't been too illuminating yet. Anyway, how do I call bash to run a 
program - e.g. slocate - from within a python program. I believe that I 
have to import the OS first (or do I?) and I was thinking something like:

...
sh slocate file_name > python_defined_list
...

This is to populate a list which would then become the filename list for 
the remove file command to iterate through sequentially and delete, as in:

for i in python_defined_list:
rm -fr i

How the heck do I set that kind of interaction between bash and python 
and the user home directory content up?

Any thoughts?

/j

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


Re: [Tutor] Invoking bash from within a python program

2005-08-13 Thread Vinay Reddy
> I'm sure that somewhere in the dim and distant past this was asked, but
> I'll be b***ered if I can find reference to it, and google searches
> haven't been too illuminating yet. Anyway, how do I call bash to run a
> program - e.g. slocate - from within a python program. I believe that I
> have to import the OS first (or do I?) and I was thinking something like:
> 
> ...
> sh slocate file_name > python_defined_list

You can directly run slocate (or any other program) using the
os.popen* command. Refer to:
http://docs.python.org/lib/os-newstreams.html.
And you do need to import the os module.

The function call will be something like:
inStream, outStream = os.popen2("slocate file_name")

You can then read the output from outStream.

HTH,
Vinay Reddy
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] iterating in the same line

2005-08-13 Thread Jonas Melian
I would check 3 words at the starting of a line

s=['foo','bar','qwe']

if ln.startswith(s):   (this is bad)

what is the best way for making it?

if max(map(ln.startswith,s)):
or
reduce(lambda m,n:m or n, map(ln.startswith, s))

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


Re: [Tutor] Invoking bash from within a python program

2005-08-13 Thread Danny Yoo


On Sat, 13 Aug 2005, Vinay Reddy wrote:

> > Anyway, how do I call bash to run a program - e.g. slocate - from
> > within a python program. I believe that I have to import the OS first
> > (or do I?) and I was thinking something like:
> >
> > ...
> > sh slocate file_name > python_defined_list
>
> You can directly run slocate (or any other program) using the
> os.popen* command. Refer to:
> http://docs.python.org/lib/os-newstreams.html.
> And you do need to import the os module.


Hi Joe,

Also, if you have a recent version of Python (Python 2.4), the
'subprocess' module might be worth a look:

http://www.python.org/doc/lib/module-subprocess.html



> > This is to populate a list which would then become the filename list
> > for the remove file command to iterate through sequentially and
> > delete, as in:
> >
> > for i in python_defined_list:
> >rm -fr i


'shutil' and its rmtree() function may be helpful for you:

http://www.python.org/doc/lib/module-shutil.html

Good luck!

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


[Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Jim Roush
I have a python script that creates a list of lists like so:

Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] )

After Quant is created, I want to sort it by MTD.  If I use a simple 
Quant.sort(), I assume its going to sort by 'db_ticker' which is not 
what I want.

I've been trying to figure out how to use the cmp() in a list sort 
method and I have failed to understand.

Please help.




-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.338 / Virus Database: 267.10.8/71 - Release Date: 8/12/2005

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


Re: [Tutor] iterating in the same line

2005-08-13 Thread Danny Yoo


On Sat, 13 Aug 2005, Jonas Melian wrote:

> I would check 3 words at the starting of a line
>
> s=['foo','bar','qwe']
>
> if ln.startswith(s):   (this is bad)


Hi Jonas,

Just checking: is this similar to a question brought up a few days ago?

http://mail.python.org/pipermail/tutor/2005-August/040592.html


Do you really need to do this in one line, or would it be sufficient to
write a function definition that you can reuse?  You mentioned earlier
that:

> if ln.startswith(s):   (this is bad)

But the general idea is a good one!  You can write your own function
called startswithany(), which will then read as:

if startswithany(ln, s): ...

and writing a simple version of startswithany() should be straightforward.



> what is the best way for making it?
>
> if max(map(ln.startswith,s)):
> or
> reduce(lambda m,n:m or n, map(ln.startswith, s))

I'm not so sure about this.  It does more work than it needs to, and it
doesn't scan well for human beings.  *grin*

If you really want to do it this way, at leeast wrap the it in a
well-named function.  But if you really insist on getting it done in one
line, take a look at the Regular Expression HOWTO:

http://www.amk.ca/python/howto/regex/


Good luck to you!

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


Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Danny Yoo


On Sat, 13 Aug 2005, Jim Roush wrote:

> I have a python script that creates a list of lists like so:
>
> Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] )
>
> After Quant is created, I want to sort it by MTD.  If I use a simple
> Quant.sort(), I assume its going to sort by 'db_ticker' which is not
> what I want.
>
> I've been trying to figure out how to use the cmp() in a list sort
> method and I have failed to understand.


Hi Jim,

AMK has written a nice mini-tutorial on how to use a list's sort()
effectively:

http://www.amk.ca/python/howto/sorting/sorting.html

Does his tutorial make sense, or are there parts in there that are
baffling?  Please feel free to ask questions on it, and we'll try to help.

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


Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Jim Roush
Danny Yoo wrote:

>On Sat, 13 Aug 2005, Jim Roush wrote:
>
>  
>
>>I have a python script that creates a list of lists like so:
>>
>>Quant.append( [ db_ticker, stock_close, MTD, 0, QTD, 0, YTD, 0, 0, 0 ] )
>>
>>After Quant is created, I want to sort it by MTD.  If I use a simple
>>Quant.sort(), I assume its going to sort by 'db_ticker' which is not
>>what I want.
>>
>>I've been trying to figure out how to use the cmp() in a list sort
>>method and I have failed to understand.
>>
>>
> 
>
>Hi Jim,
>
>AMK has written a nice mini-tutorial on how to use a list's sort()
>effectively:
>
>http://www.amk.ca/python/howto/sorting/sorting.html
>
>Does his tutorial make sense, or are there parts in there that are
>baffling?  Please feel free to ask questions on it, and we'll try to help.
>
>
>
>
>  
>
I actually saw this page this morning, which leads me to believe what I 
want is something like:

Quant.sort(lambda x, y: x-y)

except how does x and y relate to the list of lists I created?


-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.338 / Virus Database: 267.10.8/71 - Release Date: 8/12/2005

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


Re: [Tutor] iterating in the same line

2005-08-13 Thread Bob Gailer
At 12:20 PM 8/13/2005, Jonas Melian wrote:
>I would check 3 words at the starting of a line
>
>s=['foo','bar','qwe']
>
>if ln.startswith(s):   (this is bad)
>
>what is the best way for making it?

Iterations over a list of this nature are most succinctly done using list 
comprehensions.

if [l for l in s if ln.startswith(l)]:

Caveat: This will test every word in s. If s is a large list it would be 
more efficient to break as soon as a match is found:

match = False
for l in s:
 if ln.startswith(l):
 match = True
 break

The 3rd alternative is to use regular expression matching. That is beyond 
the scope of this thread.
[snip]

Bob Gailer
mailto:[EMAIL PROTECTED]
303 442 2625 home
720 938 2625 cell 

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


[Tutor] re

2005-08-13 Thread Jesse Lands
I am trying to create a simple GUI that will track a network connection using 
ping.  It's a starter project for me so I am sure there are other easier ways 
of doing it, but I am learning, so bare with me.  

I figured out the first part 

import os
ping = os.popen('ping -c 4 10.0.8.200')
ping_result = ping.read()


I assume I have to use Regular Expression to read the results, but I don't 
quite understand it.  Can someone explain it or point me in a direction to 
where it's explained better then what I could find with google.

TIA
Jesse
-- 
JLands
Arch Current(Laptop)
Slackware 9.1(Server)
Slackware Current(Desktop)
Registered Linux User #290053
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] PyGTK, classes and return values?

2005-08-13 Thread Simon Gerber
Greetings, all.

I've been stumbling my way through the PyGTK tutorial, with mixed 
success. So far I have been able to achieve everything I have set out to 
achieve, with one rather major exception.

I have a class called 'AddAccountWindow' that creates a window asking 
for hostname, username, password, account-type. That sorta thing.

When the user fills out the data and hits 'okay', it creates a class 
(self.account) with the required parameters. So far, so good. But now, I 
have no idea how to get that class *out* of AddAccountWindow.

I can't seem to find any way to return it. Nor can I successfully pass 
anything in by reference, change it into the required class, and pass it 
out again.

Any help would be much appreciated.

Regards,

-- 
"Come back to the workshop and dance cosmological models with me?"
 - Peer, "Permutation City", by Greg Egan. 

Simon Gerber
[EMAIL PROTECTED]

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


Re: [Tutor] Sorting a list of lists aka nested lists

2005-08-13 Thread Danny Yoo


> >AMK has written a nice mini-tutorial on how to use a list's sort()
> >effectively:
> >
> >http://www.amk.ca/python/howto/sorting/sorting.html
> >
> >Does his tutorial make sense, or are there parts in there that are
> >baffling?  Please feel free to ask questions on it, and we'll try to
> >help.
> >
> I actually saw this page this morning, which leads me to believe what I
> want is something like:
>
> Quant.sort(lambda x, y: x-y)
>
> except how does x and y relate to the list of lists I created?


Hi Jim,

Whenever we're sorting lists of objects, we have to tell Python how to
detect whenever two things are misordered.

Let's make this concrete: say that we have a list of names, like this:

###
>>> names = ['hamlet', 'rosencrantz', 'guildenstern']
###

We'd like to order things "alphabetically".

(... and pretend that we don't know about list.sort() for the moment.
*grin*)


How could we go about doing this?  One way is to scan across the list,
pairwise.  As we look at adjacent pairs of words, we can check to see if
those words are misordered, and if so, swap!  I'll step through this just
so you get the general idea of what's so useful about a comparison
function.

Let's start looking at the first two names in the list.

###
>>> names[0], names[1]
('hamlet', 'rosencrantz')
###

Are they in the right order?  We know they are, but again, let's pretend
that we don't.  *grin*  So the question really is: how does Python know if
they're in the right order?

We can ask Python this "are they in order?" question by using the built-in
cmp() function:

###
>>> cmp(names[0], names[1])
-1
###

If cmp gives a negative number, that means that names[0] is "less than"
names[1], and so, yes, they are.  So we'll leave the first two list
elements alone for the moment.


Now let's look at the next pair:

##
>>> cmp(names[1], names[2])
1
##

Ok, this is different.  cmp() gives a positive value if the first argument
and the second argument are misordered.  They should be swapped around!
So let's swap them:

###
>>> names[1], names[2] = names[2], names[1]
###


Intuitively, we've increased the "order" of our list by doing the swap.
Just to make sure things are fine, let's cmp() both pairs again, and make
sure everything comes out as negative.

###
>>> cmp(names[0], names[1])
1
###

Oh, now the first two elements of our list are misordered.  Let's swap
again.

##
>>> names[0], names[1] = names[1], names[0]
##


Do we have to swap anymore?  Let's check again:

##
>>> cmp(names[0], names[1])
-1
>>> cmp(names[1], names[2])
-1
##

Ok, good.  No more need to swap things around anymore.  Now that we
swapped them, let's look at our list:

###
>>> names
['guildenstern', 'hamlet', 'rosencrantz']
###

Everything's in the right "alphabetical" order now.  Hurrah!

What Python actually does in sorting a list is a little different that
what we described above.  But conceptually, it uses a comparison function
to detect disorder, and increases the order of a list by swapping and
moving things around, until everything's truly sorted.  These techniques
are called "comparison-based" sorts, because they compare between two
elements at a time.


Let's change the problem in a slightly wacky way: what if we'd like to
order the names by their third letter?

###
def my_custom_cmp(first_name, second_name):
return cmp(first_name[2], second_name[2])
###

Given any two names, we check to see if their respective third letters are
misordered.  Notice that we've actually reused the built-in cmp()
operator, but got it to concentrate on the third letter of the given
names.  Let's try it out:

###
>>> my_custom_cmp("danny", "jim")
1
>>> my_custom_cmp("jim", "danny")
-1
>>> my_custom_cmp("jim", "jim")
0
###


Once we have this customized comparison function, we can pass it to
sort(), and it'll know to use our comparison operator when it's checking
for disorder:

###
>>> names.sort(my_custom_cmp)
>>> names
['guildenstern', 'hamlet', 'rosencrantz']
###

H.  No change.  But that's ok; that's perfectly correct.


Ok, let's see if we order by the lengths of words:

###
>>> def another_custom_cmp(w1, w2):
... return cmp(len(w1), len(w2))
...
>>> names.sort(another_custom_cmp)
>>> names
['hamlet', 'rosencrantz', 'guildenstern']
###

We've come full-circle.  *grin* But I hope that this makes sense.


Please feel free to ask questions on any part of this.  Good luck!

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


Re: [Tutor] python and xml

2005-08-13 Thread Luis N
On 8/13/05, David Holland <[EMAIL PROTECTED]> wrote:
> What is a good way of using xml and python ?

ElementTree? http://effbot.org/zone/element-index.htm

or, lxml http://codespeak.net/lxml/

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