[Tutor] [tutor] list order change

2010-11-26 Thread Michael Sprayberry
You can also sort a list so that is ordered in reverse or alphabetically
 
 
Cheers,
Michael


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


Re: [Tutor] List Changing Order

2010-11-26 Thread Corey Richardson

Steven D'Aprano wrote:

Corey Richardson wrote:

Tutors,

I recall that the keys of dictionaries have arbitrary order, and may 
change over time. Is this true of lists? I can't find the answer from 
a simple Google search. Thank you!


Only if you re-arrange it yourself.

list.sort(), list.reverse() and random.shuffle(list) explicitly change 
the list's order. You can also manually move items around, e.g.:


list[3], list[5] = list[5], list[3]  # swap items 3 and 5

but otherwise lists keep their order.



Many thanks, everyone who replied.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If os.path.lexists() isn't working properly

2010-11-26 Thread Susana Iraiis Delgado Rodriguez
Hello! After all the sugestions I received throught this list; I tried them
all and worked ok. At he end I noticed that a single variable change will
bring me the results I was looking for. In my code I replaced the line

n = os.path.splitext(filename)

for:
n = os.path.splitext(filepath)
 p = n[0]+'.prj'
 if os.path.exists(p):
... code

I just wanted to pointed out, thank you all of you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] normalize an array

2010-11-26 Thread John
I know this is a simple problem, but I want to do it the most
efficient way (that is vectorized...)

import numpy as np

a = np.array(([1,2,3,4],[1,.2,3,4],[1,22,3,4]))
b = np.sum(a,axis=1)

for i,elem in enumerate(a):
a[i,:] = elem/b[i]



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


Re: [Tutor] Random Number Question

2010-11-26 Thread Piotr Kamiński

Dnia 25-11-2010 o 13:20:22 Mac Ryan  napisał(a):


On Thu, 25 Nov 2010 11:09:10 +0100
Timo  wrote:


> I was wondering... apart from checking each name individually, is
> there any easy-peasy way to get a list of names used in the
> standard library (I am thinking to something like "dir()"?
This is the webpage I always use for searching an appropriate module:
http://docs.python.org/modindex.html


I was more thinking to some introspective capacity of python itself
rather than a web page... yet, thank you for the link that I did not
know beforehand! :)

Mac.



You can get the list of Python's standard modules by typing help() and  
then you will see something similar to:




help()


Welcome to Python 2.6!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help> modules
..
..

===
The last paragraph of the message shows the answer to you question.

HTH,

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


Re: [Tutor] Random Number Question

2010-11-26 Thread Mac Ryan
On Fri, 26 Nov 2010 18:33:07 +0100
Piotr Kamiński  wrote:

> You can get the list of Python's standard modules by typing help()
> and then you will see something similar to:

Thanks a lot! This is what I was after!

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


[Tutor] Consequences of opening subprocesses?

2010-11-26 Thread Cory Teshera-Sterne
Hi all,

I'm practicing python by rewriting some bash scripts, and I've found a few
things that I can't directly translate, like certain calls to command-line
tools. I've figured out how to start them with the subprocess module, but
I'm wondering if I'm going to get myself in hot water memory- or
performance-wise since these scripts are scheduled to run on thousands of
files fairly often. Do I need to explicitly close the subprocesses, or clean
them up, or something (I have some programming experience, but not a lot of
experience with taking into account these kinds of issues - ie, no C ... )?

An example is a call to the command-line image manipulation program,
ImageMagick:
s = subprocess.Popen(['identify', '-format', ''%w %h\n'', 'testimg.jpg'],
stdout=subprocess.PIPE)
output = s.communicate()[0]
print output
# output should be the dimensions of testimg.jpg in pixels, ie, 100 50

What I'm thinking is that if, when typed into the terminal, the command
appears to give me some output and then exit like the one above (or bash
commands like "ls -l"), then I'm ok ... but like I said, I just don't know
much about this kind of issue. Can anyone tell me if my intuition's way off
on this?

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


Re: [Tutor] normalize an array

2010-11-26 Thread Peter Otten
John wrote:

> I know this is a simple problem, but I want to do it the most
> efficient way (that is vectorized...)
> 
> import numpy as np
> 
> a = np.array(([1,2,3,4],[1,.2,3,4],[1,22,3,4]))
> b = np.sum(a,axis=1)
> 
> for i,elem in enumerate(a):
> a[i,:] = elem/b[i]
 
> suggestions?

I'm not a numpy expert, but:

(a.transpose()/np.sum(a, axis=1)).transpose()

Peter

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


Re: [Tutor] Consequences of opening subprocesses?

2010-11-26 Thread Alan Gauld


"Cory Teshera-Sterne"  wrote


I'm wondering if I'm going to get myself in hot water memory- or
performance-wise since these scripts are scheduled to run on 
thousands of

files fairly often.


It shouldn't be worse than your bash scripts since they implicitly
start subprocesses anyway. And if you mare moving some of the
functionality into Python it should be better overall.


Do I need to explicitly close the subprocesses, or clean
them up, or something


Mostly they should just close down themselves.
Keep a check with a tool like top when testing to ensure you
don't get something stuck spinning in a loop, but it should be
OK most of the time.


An example is a call to the command-line image manipulation program,
ImageMagick:
s = subprocess.Popen(['identify', '-format', ''%w %h\n'', 
'testimg.jpg'],

stdout=subprocess.PIPE)


If its image manipulation have you checked out the PIL package?
It can do most of what imagemagick can do from Python. Its not
in the standard library but Google will find it for you. The only snag 
is

that I'm not sure if its on Python 3 yet if you are using v3...

# output should be the dimensions of testimg.jpg in pixels, ie, 100 
50


PIL should definitely be able to manage that kind of thing.

There are also Python bindings to drive ImageMagick from within
Python too although I don;t know if they are regularly maintained.

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] lambda in python

2010-11-26 Thread john tsolox
since in Java i can pass an anonymous class to a function, and this anon class 
has member functions that can contain a
body of implementation codes having the full expression of permissible syntax 
(if,for,while...), my question is, after
seeing various examples of lambda in python being ALL one-liners. These 
one-liners inside a lambda seems not to have the
full permissible use of the full power of python language (if,for,while). Is 
this correct?

Is it correct to say, that within python lambda, you are not allowed to use 
'if' ? pls enlighten...

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


Re: [Tutor] lambda in python

2010-11-26 Thread Steven D'Aprano

john tsolox wrote:

since in Java i can pass an anonymous class to a function, and this anon class 
has member functions that can contain a
body of implementation codes having the full expression of permissible syntax 
(if,for,while...), my question is, after
seeing various examples of lambda in python being ALL one-liners. These 
one-liners inside a lambda seems not to have the
full permissible use of the full power of python language (if,for,while). Is 
this correct?

Is it correct to say, that within python lambda, you are not allowed to use 
'if' ? pls enlighten...


Python lambda is syntactic sugar for an anonymous function, not a class.

Lambda is also limited to only a single expression. However, you can use 
the ternary if:


lambda x: x+1 if x < 0 else x-3

is almost the same as:

def func(x):
if x < 0: return x+1
else: return x-3



--
Steven

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


[Tutor] %T as a strftime format identifier

2010-11-26 Thread Terry Carroll

Was %T ever a valid format specifier for time.strftime in Python?

I just installed a Python streaming MP3 server called Edna 
(http://edna.sourceforge.net/).  It was an easy install except that I got 
a ValueError on one line, essentially for:


  time.strftime("%a, %d %b %Y %T GMT")

After a few seconds experimentation, I found that "%T" is not recognized, 
but that in ANSI C strftime, it's a shorthand for %H:%M:%S.  I changed it 
to:


  time.strftime("%a, %d %b %Y %H:%M:%S PST")

and it worked fine.

My question: was %T ever a valid format specifier in Python?  My best 
guess is that it was when Edna was written (the most current release is 
from 2006, and the docs say it needs at least Python 1.5.2, which gives 
you an example of its age).  It seems odd that the format identifier would 
be dropped if it had existed, though; that seems like a needless 
upward compatibility issue.


I got this working, so this is mere curiosity; anyone know?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor