Re: Syntactic structure for 'until :' loop

2011-01-12 Thread Peter Otten
Ian Kelly wrote:

> reader_iter = iter(self.reader)
> headers = reader_iter.next()
> # intermediate code
> for line in reader_iter:
>  data.append(line)
> return data

If data is a list the for loop can be replaced with

data.extend(reader_iter)

or, if data is an empty list created within the function

data = list(reader_iter)
-- 
http://mail.python.org/mailman/listinfo/python-list


read text color from image

2011-01-12 Thread prakash jp
Hi All,

During automation of a test case the web interface throws failure and sucess
text in RED and GREEN colors respectively. Is there a method to read the
color of the Success(green) and Failure(red) from the screenshots of the
webinterfaces collect for Failure and Success

say :

import Image
import ImageChops
im2 = Image.open("Failure.JPG")
im1 = Image.open("Sucess.JPG")
#print list(im1.getdata())
diff = ImageChops.difference(im2, im1)
#print diff.getbbox()
#diff.show()
#print im1.tostring()

Thanks for all in advance

Prakash
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Colin J. Williams

On 10-Jan-11 16:02 PM, MRAB wrote:

On 10/01/2011 20:29, Dan Stromberg wrote:

I invite folks to check out Tiobe's Language Popularity Rankings:

http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html

The gist is: Python grew faster than any other programming language
over the last year, according to this (slightly arbitrary, but better
than no indicator) ranking.

...despite our wikipedia page whose first paragraph almost seems like
it was written with the intention of scaring off new converts, with
its "unusual" comment:

http://en.wikipedia.org/wiki/Python_%28programming_language%29

(Like it or not, people do frequently confuse the descriptive for the
normative)


It shows an example of Python code, which happens to have 2 syntax
errors!


Why not correct the Wikipedia entry?

Colin W.
--
http://mail.python.org/mailman/listinfo/python-list


Career path - where next?

2011-01-12 Thread Alan Harris-Reid


Hi there, I wonder if any Python folk out there can help me.

For many years I was a contractor developing desktop and web 
applications using Visual Foxpro as my main language, with Foxpro, 
SQL-server and Oracle as back-end databases.  Unfortunately Foxpro was 
killed-off by Microsoft, hence my work dried-up and my last 'big' 
contract ended about a year ago.  Since then I have taken time off 
programming doing house-renovation, and in the last 6 months I have been 
updating my programming skills by learning Python (3) with SQLite, 
JavaScript, HTML and CSS to a level where I can create and deploy 
data-based web-sites.


My situation now is that I am reasonably comfortable with the above 
languages and am now in a position where I wish to return to employment 
using my new and/or existing skills (contract/permanent, full/part-time 
or teleworking).   However, I have yet to find any UK vacancy which will 
accept a relative 'beginner' - they all require at least 2-3 years 
Python in a commercial environment.  It's a catch-22 situation - it's 
hard to get a job without experience, but you need a job to get 
experience in the 1st place!


I would even consider doing small projects for nothing so that I can 
'get my foot in the door' (although I hope to be wise-enough to know 
when I am being taken advantage of!).  I am also mailing CVs to agencies 
I think may be interested.


If anyone out has ideas as to how to proceed towards achieving my goal, 
I would be grateful for any advice.


Regards,
Alan Harris-Reid

--
http://mail.python.org/mailman/listinfo/python-list


Re: Career path - where next?

2011-01-12 Thread Terry Reedy

On 1/12/2011 11:37 AM, Alan Harris-Reid wrote:

...

updating my programming skills by learning Python (3) with SQLite,
JavaScript, HTML and CSS to a level where I can create and deploy
data-based web-sites.

...

I would even consider doing small projects for nothing so that I can
'get my foot in the door' (although I hope to be wise-enough to know


I believe both Roundup/Python tracker and PyPI (Python package index) 
are based on sqlite and have small projects available/needed. I cannot 
help you otherwise. Good luck.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Terry Reedy

On 1/12/2011 9:51 AM, Colin J. Williams wrote:


It shows an example of Python code, which happens to have 2 syntax
errors!


Why not correct the Wikipedia entry?


As I reported early, the errors, if any, are in .png and .svg images of 
text, which would have to be replaced, not corrected. Would be good 
since the imaged snippet is a haphazard except from a much larger file 
and inane out of context.


--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for a module to process command line arguments

2011-01-12 Thread Alice Bevan–McGregor

On 2011-01-11 21:41:24 -0800, Michele Simionato said:

Originally plac too was able to recognize flags automatically by 
looking at the default value (if the default value is a boolean then 
the option is a flag); however I removed that functionality because I 
wanted to be able to differentiate between flag and (smart) options 
(see 
http://micheles.googlecode.com/hg/plac/doc/plac.html#scripts-with-options-and-smart-options).


Not 


entirely sure what you mean by 'smart' options.  If your'e referring to 
using a single hyphen and a list of characters to represent a long 
option (which, to the rest of the world, use two leading hyphens) then 
that's pretty weird.  ;)


Consider most of the GNU tools:

ls -lvh
tar -xzvf file.tgz (goes so far as to make the leading hyphen optional!)
less -ceF logfile
bc -qw
ps -aux (same as tar)

And even third-party tools:

mysql -fH
pg_dump -abO ...

One major system in the world that doesn't really differentiate between 
long and short options is... DOS, and by extension, Windows.  But they 
also use / as a switch character.


Anyway; I'm happy with what I have wrought (and am continuing to update 
with support for class-based sub-command dispatch) and will be 
utilizing it for all scripts in the Marrow suite.  To each their own, 
but reinvention itself can be for motivations other than NIH.  I wanted 
something pure-Python, portable across the 3k barrier without code 
modification (no 2to3), that didn't use optparse, getopt, or argparse 
and basically be a translation layer.  It can be simpler than that, as 
marrow.script demonstrates.


- Alice.


--
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Krzysztof Bieniasz
> As I reported early, the errors, if any, are in .png and .svg images of
> text, which would have to be replaced, not corrected. Would be good
> since the imaged snippet is a haphazard except from a much larger file
> and inane out of context.

I don't think it really is a big deal. I mean, this is merely an 
illustration for the syntax-highlighted python code. So the message 
isn't: "Go ahead and try it with your Python". It's rather "Look, you can 
have colorful highlighting of python code, isn't that cool?!" It actually 
presents the specific indentation of Python code and therefore it is 
mostly useful to someone who never used Python. And actually I wouldn't 
expect any Python programmer to look for feedback on Wikipedia. It's not 
that I have anything against Wikipedia -- on the contrary, I use it all 
the time. But remember that it's an encyclopedia not a Python manual.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Career path - where next?

2011-01-12 Thread Jon Clements
On Jan 12, 4:37 pm, Alan Harris-Reid 
wrote:
> Hi there, I wonder if any Python folk out there can help me.
>
> For many years I was a contractor developing desktop and web
> applications using Visual Foxpro as my main language, with Foxpro,
> SQL-server and Oracle as back-end databases.  Unfortunately Foxpro was
> killed-off by Microsoft, hence my work dried-up and my last 'big'
> contract ended about a year ago.  Since then I have taken time off
> programming doing house-renovation, and in the last 6 months I have been
> updating my programming skills by learning Python (3) with SQLite,
> JavaScript, HTML and CSS to a level where I can create and deploy
> data-based web-sites.
>
> My situation now is that I am reasonably comfortable with the above
> languages and am now in a position where I wish to return to employment
> using my new and/or existing skills (contract/permanent, full/part-time
> or teleworking).   However, I have yet to find any UK vacancy which will
> accept a relative 'beginner' - they all require at least 2-3 years
> Python in a commercial environment.  It's a catch-22 situation - it's
> hard to get a job without experience, but you need a job to get
> experience in the 1st place!
>
> I would even consider doing small projects for nothing so that I can
> 'get my foot in the door' (although I hope to be wise-enough to know
> when I am being taken advantage of!).  I am also mailing CVs to agencies
> I think may be interested.
>
> If anyone out has ideas as to how to proceed towards achieving my goal,
> I would be grateful for any advice.
>
> Regards,
> Alan Harris-Reid

Hi Alan,

Just some ideas (not in any order, just as they're thought of):-

- Emphasise your experience with Oracle & SQL Server, and use Python
as a "I also have". It may be someone will accept that as viable
(saves them a DBA or something), and maybe you'll get into an
experienced group and get on the job training. (I assume you have good
SQL skills).

- Look at cwjobs.co.uk / monster / etc..., and search for Python. Get
a list of agencies there. Phone them *first*, explain what is it
you've done, and what you can do. If the person seems to know what
they're talking about send your CV - but chase often.

- Look at web-frameworks. Django seems to be the most listed for
"required"/"nice to have". Also check out javascript-frameworks -
jquery & extjs are the biggest 2, so at least you can say you've had
some experience.

- Perhaps phone your local job centre, and ask for a contact for their
local volunteer centre. They might have something like work for a
small charity that just needs a couple of pages done. The idea being:
1) If it's a cause you believe in, it makes up for not getting paid;
2) You can use it as an example and reference; 3) You might be able to
use it as networking - might get a free lunch from an event and meet
someone with money, that's impressed with your good will and work, and
just happens to have a job going spare...

- Build a copy of your CV designed for the web. Make sure it looks
good, is HTML/CSS compliant, and even add some nice interactive stuff
to it, and include it as a link in your CV. [The other thing you can
do, is only display the CV on entry of a short PIN (different for each
one you send - '2431' or something'), then you can log who's bothered
looking at it, and when, enabling timing of a follow-up better)].

- Look in local papers for local companies that offer not necessarily
web design, but possibly just print design. See if you can't have a
chat with them and get some work your way. Other options might be new-
starts up, non-chain pubs, community/sports clubs, a local church for
fund-raising, your local chinese/indian takeaway - wouldn't hurt to
put their menu online with an online order form would it!? [What you
might find about this, is that as they're not likely to be technical,
you can take your own time, charge a reasonable amount, experiment a
little and learn, and not have too tight deadlines or someone looking
over your shoulder].

Brain (or somewhere else) dump finished.

hth

Jon.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Career path - where next?

2011-01-12 Thread Philip Semanchuk

On Jan 12, 2011, at 11:37 AM, Alan Harris-Reid wrote:

> 
> Hi there, I wonder if any Python folk out there can help me.
> 
> For many years I was a contractor developing desktop and web applications 
> using Visual Foxpro as my main language, with Foxpro, SQL-server and Oracle 
> as back-end databases.  Unfortunately Foxpro was killed-off by Microsoft, 
> hence my work dried-up and my last 'big' contract ended about a year ago.  
> Since then I have taken time off programming doing house-renovation, and in 
> the last 6 months I have been updating my programming skills by learning 
> Python (3) with SQLite, JavaScript, HTML and CSS to a level where I can 
> create and deploy data-based web-sites.
> 
> My situation now is that I am reasonably comfortable with the above languages 
> and am now in a position where I wish to return to employment using my new 
> and/or existing skills (contract/permanent, full/part-time or teleworking).   
> However, I have yet to find any UK vacancy which will accept a relative 
> 'beginner' - they all require at least 2-3 years Python in a commercial 
> environment.  It's a catch-22 situation - it's hard to get a job without 
> experience, but you need a job to get experience in the 1st place!
> 
> I would even consider doing small projects for nothing so that I can 'get my 
> foot in the door' (although I hope to be wise-enough to know when I am being 
> taken advantage of!).  I am also mailing CVs to agencies I think may be 
> interested.
> 
> If anyone out has ideas as to how to proceed towards achieving my goal, I 
> would be grateful for any advice.

Contributing to open source projects (your own or someone else's) will help to 
convince some employers that you're worth taking a look at. If nothing else it 
gives you a public example of the work that you can point them to.

Good luck
Philip
-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to automatically copy out attachments from an email

2011-01-12 Thread Matty Sarro
As of now here is my situation:
I am working on a system to aggregate IT data and logs. A number of
important data are gathered by a third party system. The only
immediate way I have to access the data is to have their system
automatically email me updates in CSV format every hour. If I set up a
mail client on the server, this shouldn't be a huge issue.

However, is there a way to automatically open the emails, and copy the
attachments to a directory based on the filename? Kind of a weird
project, I know. Just looking for some ideas hence posting this on two
lists.

Thanks all, and happy hump day!
-Matty
-- 
http://mail.python.org/mailman/listinfo/python-list


How to populate all possible hierarchical clusterings from a set of elements?

2011-01-12 Thread justin
The title sounds too complex, but my question is actually simple.

Suppose I have [1,2,3,4,5], then there are many ways of making
clustering.
Among them, I want to pair up terminals until there is only one left
at the end.
For example, 1,2),3),4),5), (1,(2,(3,(4,5, or (((1,2),(3,4)),
5) would be legitimate ones.

How do you think can I, using the modules of Python such as itertools
as much as possible, make all possible such clusterings?

Thanks in advance,
Justin.
-- 
http://mail.python.org/mailman/listinfo/python-list


Nested structures question

2011-01-12 Thread Physics Python
Hello,

I am teaching myself python using the book: Python Programming for Absolute 
Beginners, 2nd edition by Michael Dawson. I am using python 2.7.1.

In chapter 3 we are learning to use structures (while, if, elif) to write a 
program that has the user guess a number between 1 and 100.

Here is the code for the baseline program:

- start --

# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low
# or right on the money

import random  

print "\tWelcome to 'Guess My Number'!"
print "\nI'm thinking of a number between 1 and 100." 
print "Try to guess it in as few attempts as possible.\n"

# set the initial values
the_number = random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1

# guessing loop
while (guess != the_number):
if (guess > the_number):
print "Lower..."
else:
print "Higher..."

guess = int(raw_input("Take a guess: "))
tries += 1

print "You guessed it!  The number was", the_number
print "And it only took you", tries, "tries!\n"
  
raw_input("\n\nPress the enter key to exit.")

--- end -

The book asks to write a version of this program that limits the number of 
guess the user can take. I have tried to write this program, and I am getting 
some run time errors. Can anybody take a look at my code and give me some 
advice or hints?

Thanks!

 start ---

# Number Guessing Game Version 2
#
# The computer picks a random number between 1 and 100
# The player tries to guess and the computer tells
# the player if the guess is high or low or correct
# The player has to guess the number in less than 7 tries.
#
# 1/12/2011



import random

# welcome the player to the game

print "\tWelcome to 'Guess My Number'!"
print "\nI am thinking of a number between 1 and 100."
print "Try and guess it in as few attempts as possible.\n"

# Set the initial values

the_number= random.randrange(100) + 1
guess = int(raw_input("Take a guess: "))
tries = 1
   
# Guessing loop


while guess != the_number:
while tries > 7:
if guess > the_number:
print "Lower..."
else:
print "Higher..."
guess = int(raw_input("Take a guess: "))
tries += 1
  
print "You guessed it! The number was: ", the_number
print "And it only took you", tries, "tries!\n"

print "Wow, you suck at this, you should be able to solve this in 7 attempts or 
less"

raw_input("Press Enter to exit the program.")

--- end -
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Tim Harig
On 2011-01-12, Physics Python  wrote:
> while guess != the_number:

=
> while tries > 7:
> if guess > the_number:
> print "Lower..."
> else:
> print "Higher..."
> guess = int(raw_input("Take a guess: "))
> tries += 1
=

Think about what happens when this nested loop exits because tries > 7?  It
returns to the outer loop whether or not the actual number was guessed
correctly.  There is no real need for this loop.
 
> print "You guessed it! The number was: ", the_number
> print "And it only took you", tries, "tries!\n"

Note that the outer loop ends here without any test to see whether or not
the number was actually guested and there is *nothing* that stops this
outer loop, so it will spin forever.

> print "Wow, you suck at this, you should be able to solve this in 7 attempts 
> or less"
>
> raw_input("Press Enter to exit the program.")

This is never reached.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Grant Edwards
On 2011-01-12, Terry Reedy  wrote:
> On 1/12/2011 9:51 AM, Colin J. Williams wrote:
>
>>> It shows an example of Python code, which happens to have 2 syntax
>>> errors!
>>
>> Why not correct the Wikipedia entry?
>
> As I reported early, the errors, if any, are in .png and .svg images of 
> text, which would have to be replaced, not corrected. Would be good 
> since the imaged snippet is a haphazard except from a much larger file 
> and inane out of context.

OK, but that answerws the question "what's wrong with it?", not "why
not fix it?"

-- 
Grant Edwards   grant.b.edwardsYow! Thousands of days of
  at   civilians ... have produced
  gmail.coma ... feeling for the
   aesthetic modules --
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Physics Python
Thanks,

Is this an indentation problem then?
How do I update the sentinel within the secondary while loop. I am trying to 
avoid using breaks by the way, as I can program this example using breaks:

--- start---
import random
print "\tWelcome to 'Guess my number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"

the_number = random.randrange(1,101)

tries = 0

while True:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if  tries == 7:
print "Wow you suck! It should only take at most 7 tries!"
break

raw_input ("\n\nPress the enter key to exit.")

--- end ---

But the book states that this can be done without needing to use breaks.

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: order of importing modules

2011-01-12 Thread Catherine Moroney

I've looked at my sys.path variable and I see that it has
a whole bunch of site-package directories, followed by the
contents of my $PYTHONPATH variable, followed by a list of
misc site-package variables (see below).

I've verified that if I manually reverse the order of sys.path
I can then import the proper version of the module that I want.
But this is not a permanent solution for me as this will mess up
other people are who working with the same code.

But, I'm curious as to where the first bunch of 'site-package'
entries come from.  The 
/usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg

is not present in any of my environmental variables yet it shows up
as one of the first entries in sys.path.

A colleague of mine is running on the same system as I am, and he
does not have the problem of the
/usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
variable showing up as one of the first entries in sys.path.

Thanks for the education,

Catherine

Dan Stromberg wrote:

On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney
 wrote:

In what order does python import modules on a Linux system?  I have a
package that is both installed in /usr/lib64/python2.5/site-packages,
and a newer version of the same module in a working directory.

I want to import the version from the working directory, but when I
print module.__file__ in the interpreter after importing the module,
I get the version that's in site-packages.

I've played with the PYTHONPATH environmental variable by setting it
to just the path of the working directory, but when I import the module
I still pick up the version in site-packages.

/usr/lib64 is in my PATH variable, but doesn't appear anywhere else.  I
don't want to remove /usr/lib64 from my PATH because that will break
a lot of stuff.

Can I force python to import from my PYTHONPATH first, before looking
in the system directory?

Catherine
--
http://mail.python.org/mailman/listinfo/python-list


Please import sys and inspect sys.path; this defines the search path
for imports.

By looking at sys.path, you can see where in the search order your
$PYTHONPATH is going.

It might actually be better to give your script a command line option
that says "Throw the following directory at the beginning of
sys.path".

--
http://mail.python.org/mailman/listinfo/python-list


Re: order of importing modules

2011-01-12 Thread Catherine Moroney

I've looked at my sys.path variable and I see that it has
a whole bunch of site-package directories, followed by the
contents of my $PYTHONPATH variable, followed by a list of
misc site-package variables (see below).

I've verified that if I manually reverse the order of sys.path
I can then import the proper version of the module that I want.
But this is not a permanent solution for me as this will mess up
other people are who working with the same code.

But, I'm curious as to where the first bunch of 'site-package'
entries come from.  The 
/usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg

is not present in any of my environmental variables yet it shows up
as one of the first entries in sys.path.

A colleague of mine is running on the same system as I am, and he
does not have the problem of the
/usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
variable showing up as one of the first entries in sys.path.

Thanks for the education,

Catherine

Dan Stromberg wrote:

On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney
 wrote:

In what order does python import modules on a Linux system?  I have a
package that is both installed in /usr/lib64/python2.5/site-packages,
and a newer version of the same module in a working directory.

I want to import the version from the working directory, but when I
print module.__file__ in the interpreter after importing the module,
I get the version that's in site-packages.

I've played with the PYTHONPATH environmental variable by setting it
to just the path of the working directory, but when I import the module
I still pick up the version in site-packages.

/usr/lib64 is in my PATH variable, but doesn't appear anywhere else.  I
don't want to remove /usr/lib64 from my PATH because that will break
a lot of stuff.

Can I force python to import from my PYTHONPATH first, before looking
in the system directory?

Catherine
--
http://mail.python.org/mailman/listinfo/python-list


Please import sys and inspect sys.path; this defines the search path
for imports.

By looking at sys.path, you can see where in the search order your
$PYTHONPATH is going.

It might actually be better to give your script a command line option
that says "Throw the following directory at the beginning of
sys.path".

--
http://mail.python.org/mailman/listinfo/python-list


RE: Nested structures question

2011-01-12 Thread Jason Staudenmayer
Return False instead of break should work

else:
 print "You guessed it! The number was", the_number
 print "And it only took you", tries, "tries!\n"
 return False

Jason
 
 
 
..·><º>

> -Original Message-
> From: 
> python-list-bounces+jasons=adventureaquarium@python.org 
> [mailto:python-list-bounces+jasons=adventureaquarium@pytho
> n.org] On Behalf Of Physics Python
> Sent: Wednesday, January 12, 2011 2:53 PM
> To: python-list@python.org
> Subject: Re: Nested structures question
> 
> 
> Thanks,
> 
> Is this an indentation problem then?
> How do I update the sentinel within the secondary while loop. 
> I am trying to avoid using breaks by the way, as I can 
> program this example using breaks:
> 
> --- start---
> import random
> print "\tWelcome to 'Guess my number'!:"
> print "\nI'm thinking of a number between 1 and 100."
> print "Try to guess it in as few attempts as possible.\n"
> 
> the_number = random.randrange(1,101)
> 
> tries = 0
> 
> while True:
> guess = int(raw_input("Take a guess: "))
> tries += 1
> if guess > the_number:
> print "Lower..."
> elif guess < the_number:
> print "Higher..."
> else:
> print "You guessed it! The number was", the_number
> print "And it only took you", tries, "tries!\n"
> break
> if  tries == 7:
> print "Wow you suck! It should only take at most 7 tries!"
> break
> 
> raw_input ("\n\nPress the enter key to exit.")
> 
> --- end ---
> 
> But the book states that this can be done without needing to 
> use breaks.
> 
> Thanks!
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Tim Harig
[wrapped lines to <80 characters per RFC 1855]
On 2011-01-12, Physics Python  wrote:
> Is this an indentation problem then?

That depends how you look at it.  I was not clear from your code exactly
where you wanted to handle things.

> How do I update the sentinel within the secondary while loop. I am
> trying to avoid using breaks by the way, as I can program this example
> using breaks:

You don't need breaks.

> import random
> print "\tWelcome to 'Guess my number'!:"
> print "\nI'm thinking of a number between 1 and 100."
> print "Try to guess it in as few attempts as possible.\n"
>
> the_number = random.randrange(1,101)
>
> tries = 0
>
> while True:

while can be used to test for more then a single condition at a time using
and/or chains.

> else:
> print "You guessed it! The number was", the_number
> print "And it only took you", tries, "tries!\n"
> break
> if  tries == 7:
> print "Wow you suck! It should only take at most 7 tries!"
> break

Both of these tests can be performed as part of the loop itself.  The end
results can therefore be tested and handled outside of the loop without
using breaks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Tim Harig
On 2011-01-12, Jason Staudenmayer  wrote:
> Return False instead of break should work
>
> else:
>  print "You guessed it! The number was", the_number
>  print "And it only took you", tries, "tries!\n"
>  return False

Since he isn't in a function, that isn't any good.  He would import sys and
use sys.exit() but that rather defeats the purpose of having a single entry
and exit point to the loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to populate all possible hierarchical clusterings from a set of elements?

2011-01-12 Thread Peter Otten
justin wrote:

> The title sounds too complex, but my question is actually simple.
> 
> Suppose I have [1,2,3,4,5], then there are many ways of making
> clustering.
> Among them, I want to pair up terminals until there is only one left
> at the end.
> For example, 1,2),3),4),5), (1,(2,(3,(4,5, or (((1,2),(3,4)),
> 5) would be legitimate ones.
> 
> How do you think can I, using the modules of Python such as itertools
> as much as possible, make all possible such clusterings?

Here's my first attempt:

def cluster(items):
if len(items) == 2:
yield items
return

for i in range(len(items)-1):
for c in cluster(items[:i] + (items[i:i+2],) + items[i+2:]):
yield c

def unique(items):
seen = set()
for item in items:
if item not in seen:
seen.add(item)
yield item

if __name__ == "__main__":
for item in unique(cluster(tuple("abcd"))):
print item

Unfortunately I get a lot of duplicates :(

You could define a kind of operator precedence using 
itertools.combinations(), but I think it suffers from the same problem as

a 3 b 1 c 2 d

and

a 2 b 1 c 3 d

would both result in ((a, b), (c, d)).



-- 
http://mail.python.org/mailman/listinfo/python-list


How to Buffer Serialized Objects to Disk

2011-01-12 Thread Scott McCarty
Sorry to ask this question. I have search the list archives and googled, but
I don't even know what words to find what I am looking for, I am just
looking for a little kick in the right direction.

I have a Python based log analysis program called petit (
http://crunchtools.com/petit). I am trying to modify it to manage the main
object types to and from disk.

Essentially, I have one object which is a list of a bunch of "Entry"
objects. The Entry objects have date, time, date, etc fields which I use for
analysis techniques. At the very beginning I build up the list of objects
then would like to start pickling it while building to save memory. I want
to be able to process more entries than I have memory. With a strait list it
looks like I could build from xreadlines(), but once you turn it into a more
complex object, I don't quick know where to go.

I understand how to pickle the entire data structure, but I need something
that will manage the memory/disk allocation?  Any thoughts?

Gracias
Scott M
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Alice Bevan–McGregor

On 2011-01-10 19:49:47 -0800, Roy Smith said:

One of the surprising (to me, anyway) uses of JavaScript is as the 
scripting language for MongoDB (http://www.mongodb.org/).


I just wish they'd drop spidermonkey and go with V8 or another, faster 
and more modern engine.  :(


- Alice.


--
http://mail.python.org/mailman/listinfo/python-list


Re: order of importing modules

2011-01-12 Thread Chris Rebert
> Dan Stromberg wrote:
>> On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney
>>  wrote:
>>>
>>> In what order does python import modules on a Linux system?  I have a
>>> package that is both installed in /usr/lib64/python2.5/site-packages,
>>> and a newer version of the same module in a working directory.
>>>
>>> I want to import the version from the working directory, but when I
>>> print module.__file__ in the interpreter after importing the module,
>>> I get the version that's in site-packages.
>>>
>>> I've played with the PYTHONPATH environmental variable by setting it
>>> to just the path of the working directory, but when I import the module
>>> I still pick up the version in site-packages.
>>>
>>> /usr/lib64 is in my PATH variable, but doesn't appear anywhere else.  I
>>> don't want to remove /usr/lib64 from my PATH because that will break
>>> a lot of stuff.
>>>
>>> Can I force python to import from my PYTHONPATH first, before looking
>>> in the system directory?
>>>
>> Please import sys and inspect sys.path; this defines the search path
>> for imports.
>>
>> By looking at sys.path, you can see where in the search order your
>> $PYTHONPATH is going.
>>
On Wed, Jan 12, 2011 at 11:07 AM, Catherine Moroney
 wrote:
> I've looked at my sys.path variable and I see that it has
> a whole bunch of site-package directories, followed by the
> contents of my $PYTHONPATH variable, followed by a list of
> misc site-package variables (see below).

> But, I'm curious as to where the first bunch of 'site-package'
> entries come from.  The
> /usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
> is not present in any of my environmental variables yet it shows up
> as one of the first entries in sys.path.

You probably have a .pth file somewhere that adds it (since it's an
egg, probably site-packages/easy-install.pth).
See http://docs.python.org/install/index.html#modifying-python-s-search-path

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread MRAB

On 12/01/2011 21:05, Scott McCarty wrote:

Sorry to ask this question. I have search the list archives and googled,
but I don't even know what words to find what I am looking for, I am
just looking for a little kick in the right direction.

I have a Python based log analysis program called petit
(http://crunchtools.com/petit). I am trying to modify it to manage the
main object types to and from disk.

Essentially, I have one object which is a list of a bunch of "Entry"
objects. The Entry objects have date, time, date, etc fields which I use
for analysis techniques. At the very beginning I build up the list of
objects then would like to start pickling it while building to save
memory. I want to be able to process more entries than I have memory.
With a strait list it looks like I could build from xreadlines(), but
once you turn it into a more complex object, I don't quick know where to go.

I understand how to pickle the entire data structure, but I need
something that will manage the memory/disk allocation?  Any thoughts?


To me it sounds like you need to use a database.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread Chris Rebert
On Wed, Jan 12, 2011 at 1:05 PM, Scott McCarty  wrote:
> Sorry to ask this question. I have search the list archives and googled, but
> I don't even know what words to find what I am looking for, I am just
> looking for a little kick in the right direction.
> I have a Python based log analysis program called petit
> (http://crunchtools.com/petit). I am trying to modify it to manage the main
> object types to and from disk.
> Essentially, I have one object which is a list of a bunch of "Entry"
> objects. The Entry objects have date, time, date, etc fields which I use for
> analysis techniques. At the very beginning I build up the list of objects
> then would like to start pickling it while building to save memory. I want
> to be able to process more entries than I have memory. With a strait list it
> looks like I could build from xreadlines(), but once you turn it into a more
> complex object, I don't quick know where to go.
> I understand how to pickle the entire data structure, but I need something
> that will manage the memory/disk allocation?  Any thoughts?

You could subclass `list` and use sys.getsizeof()
[http://docs.python.org/library/sys.html#sys.getsizeof ] to keep track
of the size of the elements, and then start pickling them to disk once
the total size reaches some preset limit.
But like MRAB said, using a proper database, e.g. SQLite
(http://docs.python.org/library/sqlite3.html ), wouldn't be a bad idea
either.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread Peter Otten
Scott McCarty wrote:

> Sorry to ask this question. I have search the list archives and googled,
> but I don't even know what words to find what I am looking for, I am just
> looking for a little kick in the right direction.
> 
> I have a Python based log analysis program called petit (
> http://crunchtools.com/petit). I am trying to modify it to manage the main
> object types to and from disk.
> 
> Essentially, I have one object which is a list of a bunch of "Entry"
> objects. The Entry objects have date, time, date, etc fields which I use
> for analysis techniques. At the very beginning I build up the list of
> objects then would like to start pickling it while building to save
> memory. I want to be able to process more entries than I have memory. With
> a strait list it looks like I could build from xreadlines(), but once you
> turn it into a more complex object, I don't quick know where to go.
> 
> I understand how to pickle the entire data structure, but I need something
> that will manage the memory/disk allocation?  Any thoughts?

You can write multiple pickled objects into a single file:

import cPickle as pickle

def dump(filename, items):
with open(filename, "wb") as out:
dump = pickle.Pickler(out).dump
for item in items:
dump(item)

def load(filename):
with open(filename, "rb") as instream:
load = pickle.Unpickler(instream).load
while True:
try:
item = load()
except EOFError:
break
yield item

if __name__ == "__main__":
filename = "tmp.pickle"
from collections import namedtuple
T = namedtuple("T", "alpha beta")
dump(filename, (T(a, b) for a, b in zip("abc", [1,2,3])))
for item in load(filename):
print item

To get random access you'd have to maintain a list containing the offsets of 
the entries in the file.
However, a simple database like SQLite is probably sufficient for the kind 
of entries you have in mind, and it allows operations like aggregation, 
sorting and grouping out of the box.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to automatically copy out attachments from an email

2011-01-12 Thread Chris Rebert
On Wed, Jan 12, 2011 at 10:59 AM, Matty Sarro  wrote:
> As of now here is my situation:
> I am working on a system to aggregate IT data and logs. A number of
> important data are gathered by a third party system. The only
> immediate way I have to access the data is to have their system
> automatically email me updates in CSV format every hour. If I set up a
> mail client on the server, this shouldn't be a huge issue.
>
> However, is there a way to automatically open the emails, and copy the
> attachments to a directory based on the filename? Kind of a weird
> project, I know. Just looking for some ideas hence posting this on two
> lists.

Parsing out email attachments:
http://docs.python.org/library/email.parser.html
http://docs.python.org/library/email.message.html#module-email.message

Parsing the extension from a filename:
http://docs.python.org/library/os.path.html#os.path.splitext

Retrieving email from a mail server:
http://docs.python.org/library/poplib.html
http://docs.python.org/library/imaplib.html

You could poll for new messages via a cron job or the `sched` module
(http://docs.python.org/library/sched.html ). Or if the messages are
being delivered locally, you could use inotify bindings or similar to
watch the appropriate directory for incoming mail. Integration with a
mail server itself is also a possibility, but I don't know much about
that.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing string for " "

2011-01-12 Thread Aahz
In article <0d7143ca-45cf-44c3-9e8d-acb867c52...@f30g2000yqa.googlegroups.com>,
Daniel da Silva   wrote:
>
>I have come across a task where I would like to scan a short 20-80
>character line of text for instances of " ". Ideally
> could be of any tense.

In Soviet Russia,   you!
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Think of it as evolution in action."  --Tony Rand
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread Tim Harig
In case you still need help:

- # Set the initial values
- the_number= random.randrange(100) + 1
- tries = 0
- guess = None
-
- # Guessing loop
- while guess != the_number and tries < 7:
- guess = int(raw_input("Take a guess: "))
- if guess > the_number:
- print "Lower..."
- elif guess < the_number:
- print "Higher..."
- tries += 1
- 
- # did the user guess correctly to make too many guesses?
- if guess == the_number:
- print "You guessed it! The number was", the_number
- print "And it only took you", tries, "tries!\n"
- else:
- print "Wow you suck! It should only take at most 7 tries!"
- 
- raw_input("Press Enter to exit the program.")
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread Scott McCarty
Been digging ever since I posted this. I suspected that the response might
be use a database. I am worried I am trying to reinvent the wheel. The
problem is I don't want any dependencies and I also don't need persistence
program runs. I kind of wanted to keep the use of petit very similar to cat,
head, awk, etc. But, that said, I have realized that if I provide the
analysis features as an API, you very well, might want persistence between
runs.

What about using an array inside a shelve?

Just got done messing with this in python shell:

import shelve

d = shelve.open(filename="/root/test.shelf", protocol=-1)

d["log"] = ()
d["log"].append("test1")
d["log"].append("test2")
d["log"].append("test3")

Then, always interacting with d["log"], for example:

for i in d["log"]:
print i

Thoughts?


I know this won't manage memory, but it will keep the footprint down right?
On Wed, Jan 12, 2011 at 5:04 PM, Peter Otten <__pete...@web.de> wrote:

> Scott McCarty wrote:
>
> > Sorry to ask this question. I have search the list archives and googled,
> > but I don't even know what words to find what I am looking for, I am just
> > looking for a little kick in the right direction.
> >
> > I have a Python based log analysis program called petit (
> > http://crunchtools.com/petit). I am trying to modify it to manage the
> main
> > object types to and from disk.
> >
> > Essentially, I have one object which is a list of a bunch of "Entry"
> > objects. The Entry objects have date, time, date, etc fields which I use
> > for analysis techniques. At the very beginning I build up the list of
> > objects then would like to start pickling it while building to save
> > memory. I want to be able to process more entries than I have memory.
> With
> > a strait list it looks like I could build from xreadlines(), but once you
> > turn it into a more complex object, I don't quick know where to go.
> >
> > I understand how to pickle the entire data structure, but I need
> something
> > that will manage the memory/disk allocation?  Any thoughts?
>
> You can write multiple pickled objects into a single file:
>
> import cPickle as pickle
>
> def dump(filename, items):
>with open(filename, "wb") as out:
>dump = pickle.Pickler(out).dump
>for item in items:
>dump(item)
>
> def load(filename):
>with open(filename, "rb") as instream:
>load = pickle.Unpickler(instream).load
>while True:
>try:
>item = load()
>except EOFError:
>break
>yield item
>
> if __name__ == "__main__":
>filename = "tmp.pickle"
>from collections import namedtuple
>T = namedtuple("T", "alpha beta")
>dump(filename, (T(a, b) for a, b in zip("abc", [1,2,3])))
>for item in load(filename):
>print item
>
> To get random access you'd have to maintain a list containing the offsets
> of
> the entries in the file.
> However, a simple database like SQLite is probably sufficient for the kind
> of entries you have in mind, and it allows operations like aggregation,
> sorting and grouping out of the box.
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I find the remainder when dividing 2 integers

2011-01-12 Thread Rohan Pai
Try using dividend % divisor, this will return the remainder

 

Rohan Pai

 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread Peter Otten
Scott McCarty wrote:

> Been digging ever since I posted this. I suspected that the response might
> be use a database. I am worried I am trying to reinvent the wheel. The
> problem is I don't want any dependencies and I also don't need persistence
> program runs. 

I don't think sqlite3 counts as a dependency these days.

> I kind of wanted to keep the use of petit very similar to
> cat, head, awk, etc. But, that said, I have realized that if I provide the
> analysis features as an API, you very well, might want persistence between
> runs.
> 
> What about using an array inside a shelve?
> 
> Just got done messing with this in python shell:
> 
> import shelve
> 
> d = shelve.open(filename="/root/test.shelf", protocol=-1)
> 
> d["log"] = ()
> d["log"].append("test1")
> d["log"].append("test2")
> d["log"].append("test3")
> 
> Then, always interacting with d["log"], for example:
> 
> for i in d["log"]:
> print i
> 
> Thoughts?

That won't save you any memory as the whole value (the complete list) is 
unpickled once you say d["log"].

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to Buffer Serialized Objects to Disk

2011-01-12 Thread Adam Tauno Williams
On Wed, 2011-01-12 at 17:29 -0500, Scott McCarty wrote:
> Been digging ever since I posted this. I suspected that the response
> might be use a database.

I use shelve extensively; there are many use-cases where it makes sense.
And there are many where a database makes sense.

Basically, if I just want key based lookup and the data fits in memory I
use a shelve.

> The problem is I don't want any dependencies and I also don't need
> persistence program runs. I kind of wanted to keep the use of petit
> very similar to cat, head, awk, etc. But, that said, I have realized
> that if I provide the analysis features as an API, you very well,
> might want persistence between runs.
> What about using an array inside a shelve?
> Just got done messing with this in python shell:
> import shelve
> d = shelve.open(filename="/root/test.shelf", protocol=-1)
> d["log"] = ()
> d["log"].append("test1")
> d["log"].append("test2")
> d["log"].append("test3")
> Then, always interacting with d["log"], for example:
> for i in d["log"]:
> print i
> Thoughts?

That is fine so long as all your data fits comfortable in memory.

> I know this won't manage memory, but it will keep the footprint down
> right?

No.  All of "log" will always be in memory. 

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python use growing fast

2011-01-12 Thread Roy Smith
In article ,
 Alice Bevan­McGregor  wrote:

> On 2011-01-10 19:49:47 -0800, Roy Smith said:
> 
> > One of the surprising (to me, anyway) uses of JavaScript is as the 
> > scripting language for MongoDB (http://www.mongodb.org/).
> 
> I just wish they'd drop spidermonkey and go with V8 or another, faster 
> and more modern engine.  :(

Could be.  I've opened a few bugs against Mongo which were explained 
away as "it's really a bug in SM".
-- 
http://mail.python.org/mailman/listinfo/python-list


cipher encoding

2011-01-12 Thread Cathy James
Dear all,

I hope someone out there can help me.

 The output string of my code is close to what i need, but i need it
1)printed on one line and
2) reversed

#mycode:
s= input("Enter message: ")
key=1
for letter in s:
num=(chr(ord(letter)+1))
print(num)
#or is there a better way to rewrite it with elementary level Python, which
happens 2b my current ranking.
#Your insight is always appreciated:)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cipher encoding

2011-01-12 Thread Corey Richardson
On 01/12/2011 07:35 PM, Cathy James wrote:
> Dear all,
>  
> I hope someone out there can help me.
>  
>  The output string of my code is close to what i need, but i need it
> 1)printed on one line and
> 2) reversed
>  
> #mycode:
> s= input("Enter message: ")
> key=1
> for letter in s:
> num=(chr(ord(letter)+1))
> print(num)
> #or is there a better way to rewrite it with elementary level Python,
> which happens 2b my current ranking.
> #Your insight is always appreciated:)
> 

s = input("Enter message: ")
key = int(input("Enter offset: ")) # I think that's what you wanted
for letter in s:
print(chr(ord(letter) + key), end = "")

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cipher encoding

2011-01-12 Thread Corey Richardson
On 01/12/2011 07:39 PM, Corey Richardson wrote:
> On 01/12/2011 07:35 PM, Cathy James wrote:
>> Dear all,
>>  
>> I hope someone out there can help me.
>>  
>>  The output string of my code is close to what i need, but i need it
>> 1)printed on one line and
>> 2) reversed
>>  
>> #mycode:
>> s= input("Enter message: ")
>> key=1
>> for letter in s:
>> num=(chr(ord(letter)+1))
>> print(num)
>> #or is there a better way to rewrite it with elementary level Python,
>> which happens 2b my current ranking.
>> #Your insight is always appreciated:)
>>
> 
> s = input("Enter message: ")
> key = int(input("Enter offset: ")) # I think that's what you wanted
> for letter in s:
> print(chr(ord(letter) + key), end = "")
> 
Oh, and you wanted it reversed. That's not hard.

message = input("Enter message: ")
key = int(input("Enter offset: "))
new_message = ""
for char in message:
new_message += chr(ord(letter) + key)
print(new_message[::-1])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cipher encoding

2011-01-12 Thread Nick Stinemates
Try

print s[::-1]

Nick

On Wednesday, January 12, 2011, Cathy James  wrote:
> Dear all,
>
> I hope someone out there can help me.
>
>  The output string of my code is close to what i need, but i need it
> 1)printed on one line and
>
> 2) reversed
>
>
> #mycode:
> s= input("Enter message: ")
> key=1
> for letter in s:
>     num=(chr(ord(letter)+1))
>     print(num)
> #or is there a better way to rewrite it with elementary level Python, which 
> happens 2b my current ranking.
> #Your insight is always appreciated:)
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cipher encoding

2011-01-12 Thread MRAB

On 13/01/2011 00:49, Corey Richardson wrote:

On 01/12/2011 07:39 PM, Corey Richardson wrote:

On 01/12/2011 07:35 PM, Cathy James wrote:

Dear all,

I hope someone out there can help me.

  The output string of my code is close to what i need, but i need it
1)printed on one line and
2) reversed

#mycode:
s= input("Enter message: ")
key=1
for letter in s:
 num=(chr(ord(letter)+1))
 print(num)
#or is there a better way to rewrite it with elementary level Python,
which happens 2b my current ranking.
#Your insight is always appreciated:)



s = input("Enter message: ")
key = int(input("Enter offset: ")) # I think that's what you wanted
for letter in s:
 print(chr(ord(letter) + key), end = "")


Oh, and you wanted it reversed. That's not hard.

message = input("Enter message: ")
key = int(input("Enter offset: "))
new_message = ""
for char in message:
 new_message += chr(ord(letter) + key)
print(new_message[::-1])


It's neater with:

for char in reversed(message):

then you don't need 'new_message'.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Which coding style is better? public API or private method inside class definition

2011-01-12 Thread DevPlayer
On Jan 4, 11:46 pm, Inyeol  wrote:
> Which coding style do you prefer? I'm more toward public API way,
> since it requires less code change if I refactor private data
> structure later.
> Plz give pros and cons of these.

Great question. It gets at the heart of Python style.
It's a tricky question and can be answered so many ways. I'm not a pro-
programmer anymore so here's my hobbyst's take on it.

First, API is application programming interface. Follow me for a sec.
It's the interface that your programmers use to use your "package".
But WHICH programmers? For large apps like game developers or large
all encompassing apps for companys there are many different types of
programmers. That helps determine how heavy to lean towards private
verse public interfaces.

Funny thing is I end up using the private interfaces as much as the
public interaces for certain things. For example when coding in
wxPython I rarely use any private apis (ie. __dict__ or other "special
or magic named" attributes). But when coding outside of wxPython I'm
all over the place defining new classes with special named attributes
like __new__ or __metaclass__, and lots and lots of other magic named
methods and private attributes.

So one way to decide whether to be coding towards more public verse
private is where in your project you are. Building infrastructure and
framework verse building "application" and end-user interfaces.

Or similarly the interface TO the API interface is private (as far as
Python considers things private that is). The interface to all others
to use your "package" is public.

And if your code is not a package for other coders, then what is the
sense in having private anythings?

One thing that mungles one's mind into thinking it matters is your IDE
tools. When you run your scripts as an end user, who cares that
there's tons of objects cluttering your namespace - I'm implying that
using private API in that case is somewhat a waste. When in your IDE
coding like PyCrust and you type in the interface something like wx.
you'll get a popup of "attributes" and you think "ah, these attributes
and methods I can use". But in use of that program, there's no sense
in hidding stuff with underscored names.

On the otherhand. I like to think my namespaces are simple, clean,
uncluttered. There's less "What the heck is that thing" while looking
at dir() and debugging.

Personally I do not like the underscore. My hands don't like the
keystroke pattern. And the underscore is butt ugly to me. Ugly does
not equal "less readable". I find underscore easier to read then camel
case/Hungarian Notation (wx and .NET has a lot of that). Although name
mangling and camel case usage are different things, from a visual (of
source code), to me they are the same. Also when there is name
mangling, there is often an unmangled version of it in some usage
somewhere in your code.

Another thought. Although private interfaces are not intended to be
SEEN, you code your private interfaces (i.e. attributes and methods,
functions, etc) to denote themselves as private when you LOOK at them
by using underscores. I'd rather have my source code just color
private attributes differently. However editors are not at that level
yet (meaning they'd have to interpret the code to know what's
private).

Lastly. Shouldn't it be better to use APIs based on the docs instead
of analyzing the code for intended public interfaces verse private
ones?

Although not explicidly listing pros and cons to public verse private
APIs I hope I spark some useful insight.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: order of importing modules

2011-01-12 Thread Dan Stromberg
I don't know where the site-packages directories are coming from -
maybe a site.py or sitecustomize.py?  Sometimes you can strace with a
very large -s to see where something like this is coming from.  -o is
your friend for saving the output to a file in, EG, /tmp.

What I usually do is to put specific versions of modules I need, that
are different from what the OS is providing, in the same CWD as my
script during testing.  Then in my experience, CPython imports them
instead.  When it comes time for a production run, you can drop the
special modules into the OS directories somewhere.

However, another option is to add a --test command line argument to
your script, and make the option just do something like:

if '--test' in sys.argv:
   sys.path.insert(0, os.path.expanduser('~/magic-modules'))

...for example (and that in sys.argv test could probably be improved
to fit your program's specifics).

On Wed, Jan 12, 2011 at 11:07 AM, Catherine Moroney
 wrote:
> I've looked at my sys.path variable and I see that it has
> a whole bunch of site-package directories, followed by the
> contents of my $PYTHONPATH variable, followed by a list of
> misc site-package variables (see below).
>
> I've verified that if I manually reverse the order of sys.path
> I can then import the proper version of the module that I want.
> But this is not a permanent solution for me as this will mess up
> other people are who working with the same code.
>
> But, I'm curious as to where the first bunch of 'site-package'
> entries come from.  The
> /usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
> is not present in any of my environmental variables yet it shows up
> as one of the first entries in sys.path.
>
> A colleague of mine is running on the same system as I am, and he
> does not have the problem of the
> /usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
> variable showing up as one of the first entries in sys.path.
>
> Thanks for the education,
>
> Catherine
>
> Dan Stromberg wrote:
>>
>> On Tue, Jan 11, 2011 at 4:30 PM, Catherine Moroney
>>  wrote:
>>>
>>> In what order does python import modules on a Linux system?  I have a
>>> package that is both installed in /usr/lib64/python2.5/site-packages,
>>> and a newer version of the same module in a working directory.
>>>
>>> I want to import the version from the working directory, but when I
>>> print module.__file__ in the interpreter after importing the module,
>>> I get the version that's in site-packages.
>>>
>>> I've played with the PYTHONPATH environmental variable by setting it
>>> to just the path of the working directory, but when I import the module
>>> I still pick up the version in site-packages.
>>>
>>> /usr/lib64 is in my PATH variable, but doesn't appear anywhere else.  I
>>> don't want to remove /usr/lib64 from my PATH because that will break
>>> a lot of stuff.
>>>
>>> Can I force python to import from my PYTHONPATH first, before looking
>>> in the system directory?
>>>
>>> Catherine
>>> --
>>> http://mail.python.org/mailman/listinfo/python-list
>>
>> Please import sys and inspect sys.path; this defines the search path
>> for imports.
>>
>> By looking at sys.path, you can see where in the search order your
>> $PYTHONPATH is going.
>>
>> It might actually be better to give your script a command line option
>> that says "Throw the following directory at the beginning of
>> sys.path".
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: order of importing modules

2011-01-12 Thread Ben Finney
(Please don't top-post replies. Instead, reply in-line with the quoted
material, removing irrelevant material. An example is this message.)

Catherine Moroney  writes:

> I've looked at my sys.path variable and I see that it has a whole
> bunch of site-package directories, followed by the contents of my
> $PYTHONPATH variable, followed by a list of misc site-package
> variables (see below).

Can you show the full list here? What does ‘sys.path’ actually contain
when your Python interpreter is running?

> I've verified that if I manually reverse the order of sys.path I can
> then import the proper version of the module that I want.

I think the PEP 328 relative import feature is likely to be what you
want: you can specify that a specific import should search for the file
relative to the current module's file, instead of from ‘sys.path’.

As described earlier, by working through the Python tutorial
http://docs.python.org/tutorial/> you will learn about the
different styles of import statement, including relative versus absolute
http://docs.python.org/tutorial/modules.html>.

> But, I'm curious as to where the first bunch of 'site-package' entries
> come from. The
> /usr/lib64/python2.5/site-packages/pyhdfeos-1.0_r57_58-py2.5-linux-x86_64.egg
> is not present in any of my environmental variables yet it shows up as
> one of the first entries in sys.path.

That's probably there because you have used the Setuptools system (or
something related) to install a third-party package. That is achieved by
meddling with the start-up module search path, to make the third-party
package available for import. The result is often a messy search path.

Python's system for installing third-party components is far from ideal;
it has seen a lot of improvement recently, but on the other hand is
carrying a lot of baggage from its earlier design. This is one of the
symptoms.

> Thanks for the education,

I hope that helps!

-- 
 \“To me, boxing is like a ballet, except there's no music, no |
  `\   choreography, and the dancers hit each other.” —Jack Handey |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


The problem of 324 (net::ERR_EMPTY_RESPONSE)

2011-01-12 Thread shengli zhang
This is something I've been fighting for a couple of days. Let me try to
describe it as I can.

I use "python2.5 + django1.0 + mod_python + apache2" to develop a website.
the website has run for two years. But the problem happend these days. when
I log in my system, the first browser's sessin will be a error page: 324
(net::ERR_EMPTY_RESPONSE) in Chrom, empty page in Firefox, error in IE, and
"exit signal Segmentation fault (11)" in apache error_log. However, when I
close this error page and open a new page to log in the system, everything
is ok. I have tried many ways to test or solve this problem, but I failed.
following is my tries:

1 using "python manage.py runserver" to start project, everything is ok, so
django is not a factor of the problem.
2 update the OS's expat to expat-2.0.0 which is accordant with pyexpat of
python, the problem remains.
3 replace mod_python to mod_wsgi, the problem remains too.
4 modify apache's httpd.conf to remove some modules of apache, including
mod_expires, mod_deflate and libphp5, but I failed again.


Any idea about these? Has anyone experienced something similar? Or how shall
I approach this further?

Thanks a lot!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Parsing string for " "

2011-01-12 Thread Daniel da Silva
MRAB,

I will check it out. Thanks!

Daniel

On Tue, Jan 11, 2011 at 10:23 PM, MRAB  wrote:

> On 12/01/2011 01:50, Daniel da Silva wrote:
>
>> Hi,
>>
>> I have come across a task where I would like to scan a short 20-80
>> character line of text for instances of "  ". Ideally
>>   could be of any tense.
>>
>> I know quite a bit of programming and computer science, but
>> computational linguistics is relatively new to me. If anyone can point
>> me in the right direction, I would be very thankful!
>>
>>  Have a look at the Natural Language Toolkit:
>
>http://www.nltk.org/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested structures question

2011-01-12 Thread DevPlayer
looping = True
while looping:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break

if  tries >= 7:
print "Wow you suck! It should only take at most 7 tries!"
looping = False


# Alternatively while learing while looping use the continue statement

looping = True
while looping:
guess = int(raw_input("Take a guess: "))
tries += 1

if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break

if  tries < 7:
continue

print "Wow you suck! It should only take at most 7 tries!"
looping = False




# In a while loop I recommend to NOT end loops using the
# conditional test of == but instead use >, <, >= or <= or !=.
# In a complex while loop the exit condition may be skipped
# by mistake and you'll loop forever.

# In while loops I get less bugs by putting the incrementor as
# the last statement in the while block;
# this helps follow precedence like range(7) is - zero to 6
# as well as index 0 in a list is the first item. However
# while index: where index == 0 will exit the loop before
# it even starts as 0 == False (0 is not False but equals False)

# Use the while loop for looping an undetermined number of
# iterations or conditional iterations.
# Use for loops for an explicid number of iterations.

for tries in range(7):
guess = int(raw_input("Take a guess: "))
if guess > the_number:
print "Lower..."
elif guess < the_number:
print "Higher..."
else:
print "You guessed it! The number was", the_number
print "And it only took you", tries, "tries!\n"
break
if  tries >= 7:
print "Wow you suck! It should only take at most 7 tries!"

# I'm guessing the chapter's test is to see if you remember the for
loop.

# start using print() to get into a good habit for Python 3.0+


# I haven't seen the book but often one part of while that is
# left off in tutorials is the "else" statement.
while condition:
"block"
else:
"block"

# you can use else for when the condition never happens.
-- 
http://mail.python.org/mailman/listinfo/python-list