Re: [Tutor] feeding data to subprocess exes and getting results without writing files

2007-01-11 Thread Barton David
Thanks Chris,

I figured it out after a while..

import subprocess
inputtext="my input string"
process=subprocess.Popen("myprog.exe -i stdin -o
stdout",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.S
TDOUT)
outputtext,errortext=process.communicate(inputtext)
 
..so it is fairly simple after all.

Thanks for the help,
Dave

(ps I inadvertantly replied directly to you Chris, rather than the
mailing list- sorry-, so this is just for posterity in case anybody else
wants to know the solution)


-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Christopher Arndt
Sent: 09 January 2007 14:26
To: Tutor@python.org
Subject: Re: [Tutor] feeding data to subprocess exes and getting results
without writing files

Barton David schrieb:
> I just can't wrap my head around stdin, stdout and the whole pipes 
> thing, but there's got to be a relatively simple way to do this,
surely?

You have to distinguish between three different concepts:

1) file names
2) file objects
3) file contents

1) Is just a string with some identifier (the file path). To use a file
with that identifier in Python, you have to create a file object from it
by using the builtin 'open' function.

2) File objects are builtin Python objects that are usually created by
the 'open' function or returned by some other function. There are a few
file objects that are already opened and accessible to your Python
program. These are sys.stdin, sys.stderr and sys.stdout. They are file
objects, *not* strings representing the (non-existant) file name or file
content!

3) File contents are just represented by binary strings in Python. You
read/write them with the appropriate methods of file objects.

==> The subprocess.Popen constructor expects *file objects* not strings
for its 'stdin' and 'stdout' arguments. Read the documentation of
subprocess.Popen very carefully again (ignore references to file
*descriptors*).

BTW: a pipe is just an object or function that reads from one file
object and writes to another.

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

This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.

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


Re: [Tutor] string.uppercase: too many for locale

2007-01-11 Thread Barnaby Scott
Kent Johnson wrote:
> Barnaby Scott wrote:
>> Can anyone explain the following: I was getting string.uppercase 
>> returning an unexpected number of characters, given that the Python 
>> Help says that it should normally be A-Z. Being locale-dependent, I 
>> checked that my locale was not set to something exotic, and sure 
>> enough it is only what I expected - see below:
>>
>>
>> IDLE 1.1   No Subprocess 
>>  >>> import locale, string
>>  >>> locale.getlocale()
>> ['English_United Kingdom', '1252']
>>  >>> print string.uppercase
>> ABCDEFGHIJKLMNOPQRSTUVWXYZŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ
>>  >>> print string.lowercase
>> abcdefghijklmnopqrstuvwxyzƒšœžßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ
>>  >>>
>>
>> What am I missing here? Surely for UK English, I really should just be 
>> getting A-Z and a-z. In case it is relevant, the platform is Windows 
>> 2000.
> 
> Interesting. Here is what I get:
>  >>> import locale, string
>  >>> locale.getlocale()
> (None, None)
>  >>> string.uppercase
> 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
> 
> Somehow the locale for your system has changed from the 'C' locale. If I 
> set the default locale I get similar results to yours:
>  >>> locale.setlocale(locale.LC_ALL, '')
> 'English_United States.1252'
>  >>> locale.getlocale()
> ('English_United States', '1252')
>  >>> print string.uppercase
> ABCDEFGHIJKLMNOPQRSTUVWXYZèîă└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╪┘┌█▄▌▐
> 
> which doesn't print correctly because my console encoding is actually 
> cp437 not cp1252.
> 
> It looks like string.uppercase is giving you all the characters which 
> are uppercase in the current encoding, which seems reasonable. You can 
> use string.ascii_uppercase if you want just A-Z.
> 
> Kent
> 
Thanks, but this raises various questions:

Why would my locale have 'changed' - and from what?
What *would* be the appropriate locale given that I am in the UK and use
English, and how would I set it?
Why on earth does the ['English_United Kingdom', '1252'] locale setting
consider ŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ to be appropriate?
Is this less to do with Python than the operating system?
Where can I read more on the subject?

Sorry for all the open-ended questions, but I am baffled by this and can
find no information. Sadly, just using string.ascii_uppercase is not a
solution because I am trying to develop something for different locales,
but only want the actual letters that a particular language uses to be
returned - e.g. English should be A-Z only, Swedish should be A-Z + ÅÄÖ
(only) etc. The thing I really want to avoid is having to hard-code for
every language on the planet - surely this is the whole point of locale
settings, and locale-dependent functions and constants?

Thanks

Barnaby Scott

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


[Tutor] Image Question, List of Image Colors

2007-01-11 Thread Carlos
Hello,

Could someone who is familiar with image manipulation tell me what is 
the best way to obtain the list of colors inside an image?

By best I'm referring to something that gets the job done in the fastest 
way, because this operation will be repeated a number of times.

Thanks for your help,
Carlos

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


[Tutor] Finding the key for a value in a dictionary.

2007-01-11 Thread Wesley Brooks
Dear Users,

I'm trying to find the key of a unique value within a dictionary. Is
the code bellow a safe way of doing so, or is there a better way of
doing it?

a = {'de':'df', 'gf':'hg'}
key = a.keys()[a.values().index('hg')]

Thanks for your help,

Yours Faithfully,

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


[Tutor] Question on joining out of order dictionary elements

2007-01-11 Thread raghu raghu

Hi everyone,

I have a quick quick question joining out of order dictionary values.

For example:

I created an empty


config={}


Added some key/value pairs


config["test1"]="elem1"
config["test2"]="elem2"
config["test3"]="elem3"


etc




Dumped the values and joined them at the same time.


print "\\".join(config.values())

elem1\elem3\elem2

This is fine but it doesn't entirely solve the issue.

Only some of the key/value pairs in the dictionary are needed so a
dump of all values does not work.

Also, the order in which the values are joined is important so walking
through and joining all values does not work either.


The simplest way would be to do something like:


print "\\".join((config["val2"],config["val1"],config["val3"]))

elem2\elem1\elem3

or


print "%s\\%s\\%s" % (config["val2"],config["val1"],config["val3"])

elem2\elem1\elem3

but this seems somehow uneligent.

Are there a more efficient/compact ways of doing this kind of
operation or is this it?

The key/value pairs in these examples are contrived for purposes of
this discussion but the end goal is to piece together server and
directory path information for use with pysvn.

I have a Perl programmer who is learning Python and he is griping that
this kind of operation is far simpler in Perl.

To print or to retain individual values from a list,it has to be written in
the form of
config={'test1':'elem1','test2':'elem2','test3':'elem3'}
config['test4'] = 'elem4'
print config.values()
print config['test1']- To get individual values from a list
If we want to retrieve all the values or say 2 from 4 its not possible to
put in a while loop and get that as a list only takes one argument at a time
better way is to extract individually and respective keys could not be
obtained if corresponding values are given.



--

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


Re: [Tutor] Finding the key for a value in a dictionary.

2007-01-11 Thread Kent Johnson
Wesley Brooks wrote:
> Dear Users,
> 
> I'm trying to find the key of a unique value within a dictionary. Is
> the code bellow a safe way of doing so, or is there a better way of
> doing it?
> 
> a = {'de':'df', 'gf':'hg'}
> key = a.keys()[a.values().index('hg')]

This is safe, as long as the dict is not being modified (by another 
thread, presumably) while this is going on - the docs for dict 
explicitly guarantee that the order of items in a.keys() and a.values() 
will correspond as long as a doesn't change.

Whether it is a good solution or not, I don't know. It sounds like your 
dictionary is backwards, maybe, if you are looking up by value.

Kent

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


Re: [Tutor] Finding the key for a value in a dictionary.

2007-01-11 Thread Wesley Brooks
Cheers for the reply.

I'm creating a custom dictionary that I can use to store list of
unique objects used in a GUI. Items are added then a unique string is
returned. I have used this approach so if an item is deleted from the
storage dictionary I can still find it using the key, where as if I
had used a list I would have to update all references to an object if
an object before it in the list was deleted as it's index would
change.

Wesley.

On 11/01/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Wesley Brooks wrote:
> > Dear Users,
> >
> > I'm trying to find the key of a unique value within a dictionary. Is
> > the code bellow a safe way of doing so, or is there a better way of
> > doing it?
> >
> > a = {'de':'df', 'gf':'hg'}
> > key = a.keys()[a.values().index('hg')]
>
> This is safe, as long as the dict is not being modified (by another
> thread, presumably) while this is going on - the docs for dict
> explicitly guarantee that the order of items in a.keys() and a.values()
> will correspond as long as a doesn't change.
>
> Whether it is a good solution or not, I don't know. It sounds like your
> dictionary is backwards, maybe, if you are looking up by value.
>
> Kent
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] string.uppercase: too many for locale

2007-01-11 Thread Kent Johnson
Barnaby Scott wrote:
> Thanks, but this raises various questions:

I am in no way an expert on this, I am guessing...if anyone else knows 
for sure what is going on, please let me know!
> 
> Why would my locale have 'changed' - and from what?

The docs for the locale module say "According to POSIX, a program which 
has not called setlocale(LC_ALL, '') runs using the portable 'C' 
locale. Calling setlocale(LC_ALL, '') lets it use the default locale as 
defined by the LANG variable." So "from what" is the so-called 'C' 
locale; this is the setting on my machine. Why it would change I don't 
know - due to an environment variable setting or perhaps a setlocale() 
call in sitecustomize.py?

> What *would* be the appropriate locale given that I am in the UK and use
> English, and how would I set it?

I think your locale is appropriate...

> Why on earth does the ['English_United Kingdom', '1252'] locale setting
> consider ŠŒŽŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ to be appropriate?

The locale specifies 1252 as the encoding. Presumably this is Windows 
code page 1252. All of the characters you list are valid uppercase 
characters in that encoding.

> Is this less to do with Python than the operating system?

Python's locale capabilities are built on those of the underlying C 
language so that is probably where this is coming from.

> Where can I read more on the subject?

comp.lang.python is where I would start, ask your question there. State 
you confusion and ask your real question - how do I find out the actual 
uppercase letters for the language in use? If you come at it from 
"Python is broken, why does it do such a stupid thing" you will get the 
explanation of why it is not broken rather than the solution to your 
problem.

Kent

> 
> Sorry for all the open-ended questions, but I am baffled by this and can
> find no information. Sadly, just using string.ascii_uppercase is not a
> solution because I am trying to develop something for different locales,
> but only want the actual letters that a particular language uses to be
> returned - e.g. English should be A-Z only, Swedish should be A-Z + ÅÄÖ
> (only) etc. The thing I really want to avoid is having to hard-code for
> every language on the planet - surely this is the whole point of locale
> settings, and locale-dependent functions and constants?
> 
> Thanks
> 
> Barnaby Scott
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


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


[Tutor] Psyco Puzzle

2007-01-11 Thread Dick Moores
Sometimes psyco speeds up a script by a factor of 10, and sometimes 
it makes no difference at all. Here's a case where I fully expected 
it to make a difference: 
. Whether using psyco 
or not, this takes about 13 seconds on my computer. Why no difference?

Here's a script where psyco makes a big difference: 
. 1.8 seconds with 
psyco, vs. 12.7. without.seconds.

Thanks,

Dick Moores
Win XP
Python 2.5
psyco for Python 2.5

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


Re: [Tutor] Image Question, List of Image Colors

2007-01-11 Thread Kent Johnson
Carlos wrote:
> Hello,
> 
> Could someone who is familiar with image manipulation tell me what is 
> the best way to obtain the list of colors inside an image?
> 
> By best I'm referring to something that gets the job done in the fastest 
> way, because this operation will be repeated a number of times.

The Image object in the Python Imaging Library has a getcolors() method 
that seems to do what you want.
http://www.pythonware.com/library/pil/handbook/image.htm

Kent

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


Re: [Tutor] Question on joining out of order dictionary elements

2007-01-11 Thread Andre Engels

2007/1/11, raghu raghu <[EMAIL PROTECTED]>:


>>> print "\\".join((config["val2"],config["val1"],config["val3"]))
elem2\elem1\elem3

or

>>> print "%s\\%s\\%s" % (config["val2"],config["val1"],config["val3"])
elem2\elem1\elem3

but this seems somehow uneligent.

Are there a more efficient/compact ways of doing this kind of
operation or is this it?



Maybe you like:
print "\\".join([config[val] for val in ["val2","val1","val3"]])

--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Reminder: Early Bird Registration for PyCon Ending Soon

2007-01-11 Thread Jeff Rush
Greetings.  As co-chair for the upcoming volunteer-run conference in the 
Dallas (Addison) area I would like to extend a *special* invitation to those 
just getting started with Python.  PyCon is not just for the experts and this 
year we are making a special effort to reach out to those new to or curious 
about Python.

The event is the fifth international Python Conference, being held Feb 23-25, 
2007 at the Marriott-Quorum in Addison, with early-bird registration ending 
*Jan 15*.

The conference draws approximately 400-500 attendees from diverse backgrounds 
such as scientists from national and medical labs, college/K-12 educators, web 
engineers and the myriad of IT developers and programming hobbyists.

Those new to the Python language are welcome, and we're offering a half-day 
"Python 101" tutorial in the morning as well as a "Code Like a Pythonista: 
Idiomatic Python" tutorial in the afternoon, on the day before the conference, 
Thursday Feb 22.  This pair of tutorials is designed to help you get up to 
speed and better enjoy the rest of the conference.

Other talks that may be of special interest to new Python programmers are:

  - Iterators in Action
  - Good-bye Hello World, Rethinking Teaching with Python
  - Easy Creation of Interactive Tutorials
  - pyweek: make games in 7 days
  - Developing Python Applications in Komodo 4.0
  - Tutorial: How to Document a Python Open Source Project

Being run by the Python community as a non-profit event, the conference 
strives to be inexpensive, with registration being only $260 (or $195 if you 
register prior to Jan 15th), with a further discount for students.  On the day 
before the conference we are running the full day of classroom tutorials 
(extra charge per class) and then after the conference is a free four-days of 
sprints, which are informal gatherings of programmers to work together in 
coding on various projects.  Sprints are excellent opportunities to do agile 
pair-programming side-by-side with experienced programmers and make new friends.

Other activities are lightning talks, which are 5-minute presentations to show 
off a cool technology or spread the word about a project, open space talks, 
which are spontaneous gatherings around a topic and, new this year, a Python 
Lab where experienced and novice programmers will work together to solve 
challenging problems and then present their solutions.

The conference is also running four keynote talks by leaders in the 
programming field, with a special focus on education this year:

   "The Power of Dangerous Ideas: Python and One Laptop per Child"
  by Ivan Krstic, senior member of the One Laptop per Child project

   "Premise: eLearning does not Belong in Public Schools"
  by Adele Goldberg, of SmallTalk fame

   "Python 3000"
  by Guido van Rossum, creator of Python

   "The Importance of Programming Literacy"
  by Robert M. "r0ml" Lefkowitz, a frequent speaker at O'Reilly conferences

I believe you will find the conference educational and enjoyable. More 
information about the conference along with the full schedule of presentations 
with abstracts, is available online:

   http://us.pycon.org/

Thanks for any help you can give in spreading the word,

Jeff Rush
Co-Chair PyCon 2007
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Question on joining out of order dictionary elements

2007-01-11 Thread Andrew Robert
I like this solution.

Thanks everyone for all of the suggestions.

On 1/11/07, Andre Engels <[EMAIL PROTECTED]> wrote:
> 2007/1/11, raghu raghu <[EMAIL PROTECTED]>:
>
> > >>> print "\\".join((config["val2"]
> > ,config["val1"],config["val3"]))
> > elem2\elem1\elem3
> >
> > or
> >
> > >>> print "%s\\%s\\%s" %
> (config["val2"],config["val1"],config["val3"])
> > elem2\elem1\elem3
> >
> > but this seems somehow uneligent.
> >
> > Are there a more efficient/compact ways of doing this kind of
> > operation or is this it?
> >
>
> Maybe you like:
> print "\\".join([config[val] for val in ["val2","val1","val3"]])
>
> --
> Andre Engels, [EMAIL PROTECTED]
> ICQ: 6260644  --  Skype: a_engels
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


-- 
Thank you,
Andrew Robert

Senior MQ Engineer
Information Technologies
Massachusetts Financial Services
Phone: 617-954-5882
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Removing duplicates in a list with a fixed length of items in the list.

2007-01-11 Thread Adam Cripps
I have a list which comprises of simple random arithmetic problems for
teachers to give to their pupils. This list must be a set length (if
the teacher asks for 10 questions, they should get 10 questions), but
should not have any duplicates.

I've seen the use of sets, but this reduces the size of the list, when
I have to have a fixed length.

Is there a quick way of checking if a string is already within a list
without iterating over the items of the list (which will slow the
application down slightly)?

TIA
Adam
-- 
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.

2007-01-11 Thread Kent Johnson
Adam Cripps wrote:
> I have a list which comprises of simple random arithmetic problems for
> teachers to give to their pupils. This list must be a set length (if
> the teacher asks for 10 questions, they should get 10 questions), but
> should not have any duplicates.
> 
> I've seen the use of sets, but this reduces the size of the list, when
> I have to have a fixed length.
> 
> Is there a quick way of checking if a string is already within a list
> without iterating over the items of the list (which will slow the
> application down slightly)?

Hmm. If you are trying to pick 10 elements at random from a larger list, 
use random.sample(problems, 10)

If for some reason that doesn't work for you (maybe you are creating 
problems on the fly?) you could build a set in a loop, adding items 
until it is the size you want:
problemSet = set()
while len(problemSet) < 10:
   # do something to add a (possibly) new problem to the set

Finally, unless your lists are huge (thousands of items, at a guess) or 
you are doing this very many times (thousands of times), you will not 
notice the time it takes to search the list for duplicates.

Kent

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


Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.

2007-01-11 Thread Adam Cripps
On 1/11/07, Adam Cripps <[EMAIL PROTECTED]> wrote:
> On 1/11/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> > Adam Cripps wrote:
> > > I have a list which comprises of simple random arithmetic problems for
> > > teachers to give to their pupils. This list must be a set length (if
> > > the teacher asks for 10 questions, they should get 10 questions), but
> > > should not have any duplicates.
> > >
> > > I've seen the use of sets, but this reduces the size of the list, when
> > > I have to have a fixed length.
> > >
> > > Is there a quick way of checking if a string is already within a list
> > > without iterating over the items of the list (which will slow the
> > > application down slightly)?
> >
> > Hmm. If you are trying to pick 10 elements at random from a larger list,
> > use random.sample(problems, 10)
> >
> > If for some reason that doesn't work for you (maybe you are creating
> > problems on the fly?) you could build a set in a loop, adding items
> > until it is the size you want:
> > problemSet = set()
> > while len(problemSet) < 10:
> ># do something to add a (possibly) new problem to the set
> >
>

I'm creating them on the fly - so this suggestion seems to be the
sensible way forward.

I've not worked with Sets before but will give it a go. Is there a way
of turning the Set back to a list?

TIA
Adam
-- 
http://www.monkeez.org
PGP key: 0x7111B833
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.

2007-01-11 Thread Kent Johnson
Adam Cripps wrote:
> I've not worked with Sets before but will give it a go. Is there a way
> of turning the Set back to a list?

list(mySet)

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


Re: [Tutor] Image Question, List of Image Colors

2007-01-11 Thread Carlos
Hello,

I'm trying to do what Kent suggested, but I have a little problem, no 
matter were I put my image or if I use sys.path.append to append the 
folder were my Image is located it is impossible to open the image. I 
know that this is a silly question but what is going on?

I like to solve problems but this is... well. I searched the web for 
help, but nothing.

This is how I am doing it

from PIL import Image
import sys

sys.path.append('D:\MASTER THESIS\Branch_01\Thesis B_01\images\tmp')
im = Image.open("Wind_Test_01")

Thanks and sorry for bothering you with such a basic thing
Carlos
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Converting a string to a list with each character as an item

2007-01-11 Thread Amadeo Bellotti

does anyone know how to do this? I need it because i want to make a binary
coverter
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting a string to a list with each character as an item

2007-01-11 Thread Terry Carroll
On Thu, 11 Jan 2007, Amadeo Bellotti wrote:

> does anyone know how to do this? I need it because i want to make a binary
> coverter

>>> s = "101001100100101"
>>> l = list(s)
>>> l
['1', '0', '1', '0', '0', '1', '1', '0', '0', '1', '0', '0', '1', '0', '1']

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


Re: [Tutor] Psyco Puzzle

2007-01-11 Thread Danny Yoo


> Sometimes psyco speeds up a script by a factor of 10, and sometimes
> it makes no difference at all. Here's a case where I fully expected
> it to make a difference:
> . Whether using psyco
> or not, this takes about 13 seconds on my computer. Why no difference?

Psyco implements a Just-In-Time optimization approach, so I suspect that 
it does its analysis of a function only after that function has been run 
at least once --- otherwise, it has no run-time information on which it 
can use to analyze.

In the code above, the code we want to optimize is fact().  However, 
fact() is only called one time in the whole program.  To test this 
hypothesis, it might be interesting to see if "priming" fact up will help.

#
if __name__ == '__main__':
 printFact(5)## just to prime the function up
 timeStart = time.time()
 printFact(2)
 timeEnd = time.time()
 print "Time was %.4g seconds" % (timeEnd - timeStart)
#



Furthermore, the magnitude of the numbers in the fact() code quickly get 
into bignum range, where psyco's optimizations probably won't be so 
effective.  In contrast, the primes code you have all deal with integers 
in the range of 32 bits.

Unfortunately, I can't test psyco, so there might be another factor that 
I'm missing.  If you want an authoritative answer, I'd recommend asking 
your question on the psyco mailing list: they have more experience with 
what psyco's good at.


One other thing to note: something looks weird:

 if __name__ == '__main__'== '__main__':
 ^^^

This looks off.  Can you correct it?


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


Re: [Tutor] python web dev

2007-01-11 Thread Hugo González Monteverde
OkaMthembo wrote:

> this is my first post. please could you tell me which is the best 
> lightweight python web framework? also, which is the best templating 
> language for python? (which can handle other data formats in addition to 
> text). so far im lured by Cheetah, although i havent done any web dev 
> with python yet.

If you really want to go light, you may want to try Albatross, as others 
like Django are pretty heavy.

Hugo

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


Re: [Tutor] problems pickling functions

2007-01-11 Thread Hugo González Monteverde
Arild B. Næss wrote:
> I haven't found out how to change the working directory in IDLE,  
> though – and for some reason it seems to be a different one this  
> session from the last one. Does anyone know?
>   (I use a mac by the way.)


take a look at os.chdir()

This changes the interpreter's working dir (and thus whatever you're 
writing into the prompt in IDLE)

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


Re: [Tutor] XML-RPC data transfers.

2007-01-11 Thread Hugo González Monteverde
Luke Paireepinart wrote:
  > But the main strategy is to get the data out of the ImageGrab object.
> one way is stated above - use the save method to write to a file.
> another possible way is to create a filelike class, implementing 'tell' 
> 'seek' and 'write' methods, that just collects all the data written to 
> it and keeps it in memory
> rather than writing it out to a file.
> This is hinted at in the documentation for im.save():
> "You can use a file object instead of a filename. In this case, you must 
> always specify the format. The file object must implement the *seek*, 
> *tell*, and *write* methods, and be opened in binary mode."
> (I assume it means you can use a file-like object, also, but it's 
> possible that you can't, and I leave it to you to test that :) )


hmmm StringIO module maybe?

Hugo

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


Re: [Tutor] Python and rpy

2007-01-11 Thread Hugo González Monteverde
Geoframer wrote:
> 
> However i switched to Ubuntu 6.10 today (from WinXP) and to my suprise 
> it does work under linux! :-)

Probably Numeric is included in Ubuntu's  Python distro.

Hugo

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


Re: [Tutor] Question on joining out of order dictionary elements

2007-01-11 Thread Luke Paireepinart
raghu raghu wrote:
>
> [snip original message]
>
>
> To print or to retain individual values from a list,it has to be 
> written in the form of
> config={'test1':'elem1','test2':'elem2','test3':'elem3'}
> config['test4'] = 'elem4'
> print config.values()
> print config['test1']- To get individual values from a list
> If we want to retrieve all the values or say 2 from 4 its not possible 
> to put in a while loop and get that as a list only takes one argument 
> at a time better way is to extract individually and respective keys 
> could not be obtained if corresponding values are given.
Hello, raghu -
Please don't reply like this.
It breaks threading.
You should have a Re: in the title bar,
and the quoted text should have > before it.
If you reply like you did, it creates a new thread in Thunderbird.
I believe the 'reply all' button has some kind of magic that tells mail 
clients it's a reply to another message.

In addition, please don't use bolding.
If you reply normally, it will be clear what text is yours and what is 
from the original author.
Bolding text just makes it harder to read.
Thanks,
-Luke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Image Question, List of Image Colors

2007-01-11 Thread Danny Yoo
> I'm trying to do what Kent suggested, but I have a little problem, no 
> matter were I put my image or if I use sys.path.append to append the 
> folder were my Image is located it is impossible to open the image. I 
> know that this is a silly question but what is going on?

sys.path contains a list of paths that Python will use to search for 
modules.  It does not affect anything else.  In particular, adding it 
won't affect the paths that PIL uses to open files.



> sys.path.append('D:\MASTER THESIS\Branch_01\Thesis B_01\images\tmp')
> im = Image.open("Wind_Test_01")

You can build up the fulle path name, using os.path.join:

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


By the way, beware backslashes in your path there.  You can use forward 
slashes with fewer problems.  The reason backslashes are more problematic 
is because it almost always collides with the way we make string literal 
escapes in Python.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Image Question, List of Image Colors

2007-01-11 Thread Luke Paireepinart
Carlos wrote:
> Hello,
>
> I'm trying to do what Kent suggested, but I have a little problem, no 
> matter were I put my image or if I use sys.path.append to append the 
> folder were my Image is located it is impossible to open the image. I 
> know that this is a silly question but what is going on?
>
> I like to solve problems but this is... well. I searched the web for 
> help, but nothing.
>
> This is how I am doing it
>
> from PIL import Image
> import sys
>
> sys.path.append('D:\MASTER THESIS\Branch_01\Thesis B_01\images\tmp')
> im = Image.open("Wind_Test_01")
>   
You should include your traceback whenever you have a problem.
For example, if it said "File Wind_Test_01 not found" I would say
'You probably need to specify the file extension, like you would with 
any other program.'
but I don't know, because I can't see the error message.

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


Re: [Tutor] Converting a string to a list with each character as an item

2007-01-11 Thread Danny Yoo


On Thu, 11 Jan 2007, Terry Carroll wrote:

> On Thu, 11 Jan 2007, Amadeo Bellotti wrote:
>
>> does anyone know how to do this? I need it because i want to make a binary
>> coverter
>
 s = "101001100100101"
 l = list(s)
 l
> ['1', '0', '1', '0', '0', '1', '1', '0', '0', '1', '0', '0', '1', '0', '1']

By the way Amadeo, is it really necessary to do the conversion here? 
Strings follow the same indexing as lists, and they can also be used in 
loops.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting a string to a list with each character as an item

2007-01-11 Thread Bob Gailer
Amadeo Bellotti wrote:
> does anyone know how to do this? I need it because i want to make a 
> binary coverter
 >>> list('abc')
['a', 'b', 'c']

-- 
Bob Gailer
510-978-4454

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


Re: [Tutor] Image Question, List of Image Colors

2007-01-11 Thread Carlos
Hello,

Well seems like Danny was right, the backslashes were the problem! This 
really got an 11 in my frustration meter.

Thanks for your help
Carlos

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


[Tutor] more fun and games with padded numbers

2007-01-11 Thread Christopher Spears
Let's say I have a series of files that are named like
so:

0001.ext
0230.ext
0041.ext
0050.ext

How would I convert these from a padding of 4 to a
padding of 1?  In other words, I want the files to be
renamed as:

1.ext
230.ext
41.ext
50.ext

At first I used strip(), which was a mistake because
it removed all the zeroes.  

The only other solution I can think of is to use
slicing to slice off the zeroes.  Perhaps I could loop
through the name and remove zeroes until I hit a
number that is not zero and then exit the loop.

Any hints or suggestions?
 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] more fun and games with padded numbers

2007-01-11 Thread John Fouhy
On 12/01/07, Christopher Spears <[EMAIL PROTECTED]> wrote:
> Let's say I have a series of files that are named like
> so:
>
> 0001.ext
> 0230.ext
> 0041.ext
> 0050.ext
>
> How would I convert these from a padding of 4 to a
> padding of 1?  In other words, I want the files to be
> renamed as:
>
> 1.ext
> 230.ext
> 41.ext
> 50.ext
>
> At first I used strip(), which was a mistake because
> it removed all the zeroes.

Are you sure?  If you used .replace('0', '') it would remove all the
zeros.  But this is what strip is for.

>>> lst = ['0001.ext', '0230.ext', '0041.ext', '0050.ext']
>>> [s.lstrip('0') for s in lst]
['1.ext', '230.ext', '41.ext', '50.ext']

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


Re: [Tutor] Removing duplicates in a list with a fixed length of items in the list.

2007-01-11 Thread Klaus Ramelow
Why not sorting the items and throw out all multiples until you reach 
the fixed length ?

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


Re: [Tutor] more fun and games with padded numbers

2007-01-11 Thread Terry Carroll
On Thu, 11 Jan 2007, Christopher Spears wrote:

> Let's say I have a series of files that are named like
> so:
> 
> 0001.ext
> 0230.ext
> 0041.ext
> 0050.ext
> 
> How would I convert these from a padding of 4 to a
> padding of 1?  In other words, I want the files to be
> renamed as:
> 
> 1.ext
> 230.ext
> 41.ext
> 50.ext

That doesn't look to me like a padding of 1; that looks to me like no 
padding.

But if that's what you want, I'm sure there's a better way, but my initial 
approach would be:

>>> oldnames = ["0001.ext", "0230.ext", "0041.ext", "0050.ext"]
>>> for name in oldnames:
...  newfname, fext = name.split('.')
...  newname = "%s.%s" % (int(newfname), fext)
...  print newname
...
1.ext
230.ext
41.ext
50.ext
>>>

Note that this assumes that the filename portion is entirely numeric.  
This would break on, for example, a file named 00abc.ext.

Actually, what I don't like here is the conversion of string to int to 
string for no real purpose.

> At first I used strip(), which was a mistake because
> it removed all the zeroes.

That should work, with an argument:

>>> oldnames = ["0001.ext", "0230.ext", "0041.ext", "0050.ext"]
>>> newnames = [name.strip('0') for name in oldnames]
>>> newnames
['1.ext', '230.ext', '41.ext', '50.ext']



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