Re: [Tutor] how to permanently add a module path

2006-12-23 Thread Alan Gauld

"Christopher Arndt" <[EMAIL PROTECTED]> wrote 
> Linux:
> 
> Depends on your distribution:

It depends even more on the shell you use.
In csh or tcsh the file to modify is .cshrc

> - Standard way would be to edit ~/.bash_profile and add e.g.

I prefer to modify environment variables in .profile.
That way they are visible in bash, sh, ksh, ash 
which all read .profile, whereas only bash reads .bash_profile.

> # entries are separated by colons ':'
> PYTHONPATH=$HOME/lib/python:/where/my/other/python/modules/live
> export PYTHONPATH

I usually put $PYTHONPATH in front too in case there is a site 
wide version defined already.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

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


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Alan Gauld
"Carlos" <[EMAIL PROTECTED]> wrote
> I am wondering if it is possible to create lists on the fly.

In general you can use a list comprehension to do that.

> This is only so you can have an idea:
>
> for i in range(5):
>'list_%i' % (i) = []

>>> mylists = [ [] for n in range(5)]

creats a list of 5 empty lists. You can then poulate the sublists
using normal indexing:

>>> mylists[0].append(5)
>>> print mylists[0][0]
5

You can find more on List Comprehensions in the Functional Programming
topic of my web tutor.

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


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


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Alan Gauld

"Luke Paireepinart" <[EMAIL PROTECTED]> wrote

>>> 'list_%i' % (i) = []
>>>
> Note, though this is pretty arbitrary  :) and only saves you 2 
> characters...
> You don't need to create a tuple if you're only packing in one 
> value.
> >>> i = 1
> >>> 'list_%i' % i

And that works for multiple values provided they are unambiguous.

"%d,%d,%d" % 1,2,3

You need the parens if the substitution values are more complex
and include operators:

"%d,%d,%d" % 1, 2, 1+2

Will give an error (adding int to string) you need the parens here:

"%d,%d,%d" % (1, 2, 1+2)

HTH,

Alan G 


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


[Tutor] [tutor] sort a list

2006-12-23 Thread emilia12
Hi List,

I have a list like this x=[7,4,2,6]
and print x.sort()
gives to me None ! :

>> x=[7,4,2,6]
>> print x.sort()
None

... but

>> x=[7,4,2,6]
>> x.sort()
>> print x
[2, 4, 6, 7]

so, why list.sort() returns None? is this normal ?

(the python is "Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
[MSC v.1310 32 bit (Intel)] on win32")

E.


-

Започва новото голямо приключение!
Ерагон: от 15 декември в кината.
http://reklama.mail.bg/eragon/

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


Re: [Tutor] [tutor] sort a list

2006-12-23 Thread Luke Paireepinart
[EMAIL PROTECTED] wrote:
> Hi List,
>
> I have a list like this x=[7,4,2,6]
> and print x.sort()
> gives to me None ! :
>
>   
>>> x=[7,4,2,6]
>>> print x.sort()
>>>   
> None
>
> ... but
>
>   
>>> x=[7,4,2,6]
>>> x.sort()
>>> print x
>>>   
> [2, 4, 6, 7]
>
> so, why list.sort() returns None? is this normal ?
>   
No, this is not normal.
When the planets are aligned perfectly (which is very rare, thankfully), 
it creates magnetic interference that messes with Python interpreters in 
RAM and causes the list.sort method to return None.
It's still being investigated.
It just so happens that we had a momentary planet alignment earlier this 
week, which should explain the problems you've been having.

Seriously, though, see 
http://www.python.org/infogami-faq/general/why-doesn-t-list-sort-return-the-sorted-list/
 
for an explanation.
Basically, as we can see here
> (the python is "Python 2.4.3 (#69, Mar 29 2006, 17:35:34)
> [MSC v.1310 32 bit (Intel)] on win32")
>   
you're using Python 2.4, so you can just do sorted(x) and it will return 
the sorted list.

It also might be a good idea to read the rest of that FAQ.
HTH,
-Luke

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


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Carlos
Hi,

First of all, thanks for your help.

This is how this ended up:

Elements =  [
{'Name': 'Access', 'Parent': 'Plot', 
'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Circulation_01', 'Parent': 
'Access', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Circulation_02', 'Parent': 
'Access', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Circulation_03', 'Parent': 
'Access', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_01', 'Parent': 
'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_02', 'Parent': 
'Circulation_01', 'Level': 1.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_03', 'Parent': 
'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_04', 'Parent': 
'Circulation_02', 'Level': 2.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_05', 'Parent': 
'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0},
{'Name': 'Int_Circ_06', 'Parent': 
'Circulation_03', 'Level': 3.0, 'Height': 3.0, 'Width': 3.0, 'Depth': 3.0}
   ]

# Get Levels List 
for Element in Elements:
Lev_List['Level_%i' % (Element['Level'])] = []

# Append Element to corresponding Level
for Element in Elements:
Lev_List['Level_%i' % (Element['Level'])].append(Element['Name'])

print Lev_List

And this is the result:

Lev_List = {
 'Level_1': ['Access', 'Circulation_01', 
'Int_Circ_01', 'Int_Circ_02'],
 'Level_2': ['Circulation_02', 'Int_Circ_03', 
'Int_Circ_04'],
 'Level_3': ['Circulation_03', 'Int_Circ_05', 
'Int_Circ_06']
}

It works fine, as far as I can tell. I mean, no matter how many 'Levels' 
it will allways get them right (levels so far are only integers). 
Probably the first loop is a little redundant, but performance is not 
one of my concerns now, but in the future the project will not be 
composed by a handful of spaces, probably by hundreds or more.

Regards,
Carlos






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


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Luke Paireepinart
[snip]
> # Get Levels List 
> for Element in Elements:
> Lev_List['Level_%i' % (Element['Level'])] = []
>
> # Append Element to corresponding Level
> for Element in Elements:
> Lev_List['Level_%i' % (Element['Level'])].append(Element['Name'])
[snip snip]
> Probably the first loop is a little redundant, but performance is not 
> one of my concerns now, but in the future the project will not be 
> composed by a handful of spaces, probably by hundreds or more.
>   
I think a better way to do this is to check if 'Level_%i' is in your 
dictionary already.
so the loop becomes

Lev_List = {}
for Element in Elements:
keystr = 'Level_%i' % Element['Level']
if not Lev_List.has_key(keystr):
   Lev_List[keystr] = []
Lev_List[keystr].append(Element['Name'])

And of course you can go the EAFP (easier to ask forgiveness than 
permission)
route and use a try/accept block

Lev_List = {}
for Element in Elements:
keystr = 'Level_%i' % Element['Level']
try:
   Lev_List[keystr].append(Element['Name'])
except KeyError:
   Lev_List[keystr] = [Element['Name']]

I suspect the try/accept would be the fastest,
but you'd have to timeit to be sure, of course.
  
HTH,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Kent Johnson
Luke Paireepinart wrote:
> I think a better way to do this is to check if 'Level_%i' is in your 
> dictionary already.
> so the loop becomes
> 
> Lev_List = {}
> for Element in Elements:
> keystr = 'Level_%i' % Element['Level']
> if not Lev_List.has_key(keystr):
>Lev_List[keystr] = []
> Lev_List[keystr].append(Element['Name'])

I am a fan of dict.setdefault() which has this logic built in:
Lev_List = {}
for Element in Elements:
 keystr = 'Level_%i' % Element['Level']
 Lev_List.setdefault(keystr, []).append(Element['Name'])

Also there is no need to create a string for the key, Element['Level'] 
is a fine key:
Lev_List = {}
for Element in Elements:
 Lev_List.setdefault(Element['Level'], []).append(Element['Name'])

Kent

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


Re: [Tutor] Lists on the fly?

2006-12-23 Thread Luke Paireepinart
Kent Johnson wrote:
> Luke Paireepinart wrote:
>   
>> I think a better way to do this is to check if 'Level_%i' is in your 
>> dictionary already.
> I am a fan of dict.setdefault() which has this logic built in:
> Lev_List = {}
> for Element in Elements:
>  keystr = 'Level_%i' % Element['Level']
>  Lev_List.setdefault(keystr, []).append(Element['Name'])
>
> Also there is no need to create a string for the key, Element['Level'] 
> is a fine key:
>   
You're absolutely right, Kent.
I just created a variable for it because I referred to it 3 times, and 
didn't want to write out the string over and over.
The only reason I used 'Level_%i' instead of just Element['Level'] was 
because he had that in the original program.
> Lev_List = {}
> for Element in Elements:
>  Lev_List.setdefault(Element['Level'], []).append(Element['Name'])
>   
Really cool.
I'm a fan now, too :)
Thanks for this.

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


Re: [Tutor] MVC/MVP examples of how to implement it

2006-12-23 Thread Basil Shubin
Don Taylor пишет:
> Basil Shubin wrote:
>> Hi friends!
>>
>> I have read articles about MVC/MVP, but still can't get a clue to how 
>> implement it in really working application :-( Because I better 
>> understand with ready to use examples, can you provide link to free 
>> python+GUI application which implements MVC/MVP design?
>>
> Here is another example of MVP in Python/wxPython.
> 
> http://wiki.wxpython.org/index.cgi/ModelViewPresenter

Thanks, now I almost implement what I want :-) BTW current design of my 
app is much better with MVP than with my own solution.

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