Re: exec statement with/without prior compile...

2009-05-26 Thread Gabriel Genellina

En Mon, 25 May 2009 11:07:13 -0300, Jaime Fernandez del Rio
 escribió:


These weekend I've been tearing down to pieces Michele Simionato's
decorator module, that builds signature preserving decorators. At the
heart of it all there is a dynamically generated function, which works
something similar to this...

...
src = """def function(a,b,c) :\nreturn _caller_(a,b,c)\n"""
evaldict = {'_caller_' : _caller_}
code = compile(src, '', 'single')
exec code in evaldict
new_func = evaldict[function]
...

I have found fooling around with this code, that the compile step can
be completely avoided and go for a single:

exec src in evaldict

Now, I'm sure there is a good reason for that additional step, but I
haven't been able to find what the difference between both approaches
is.


The only reason I can think of, is restricting src to be a single
statement only (due to the compile flag used).


And since I'm asking, could something similar, i.e. defining a new
function and get a handle to it, be achieved with eval?


No, because def is a statement, and eval only accepts an expression.

--
Gabriel Genellina

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Lawrence D'Oliveiro
In message , Steven 
D'Aprano wrote:

> On Tue, 26 May 2009 18:31:56 +1200, Lawrence D'Oliveiro wrote:
> 
>> In message > b201-4b2445732...@v35g2000pro.googlegroups.com>, LittleGrasshopper
>> wrote:
>> 
>>> ... I am looking for suitable syntax files for [editor of choice] ...
>> 
>> I don't understand why people need "syntax files" to use a text editor.
> 
> Do you want syntax highlighting?

Why?

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


Re: how to get rid of pyc files ?

2009-05-26 Thread Gabriel Genellina

En Mon, 25 May 2009 17:00:00 -0300,  escribió:

On May 25, 12:08 am, Dave Angel  wrote:


Somebody else has already pointed out that you can tell Python not to
create thosefiles(during your development stages).


Yes, that's probably the best way to sort out the problems.
It would of course be nice if python could detect that these files
aren't usable and just regenerate them. I realise it might not be a
trivial feature to implement though.


.pyc files are platform independent; you *can* use the same .pyc files
both on Windows and Linux.
A .pyc/.pyo file contains two bits of info:
- a magic number that depends on the Python version that created it
- the last-modification-time of the corresponding .py source
When you import a module, both numbers are read from the .pyc file. If the
magic number matches the version of the running Python, and the
modification time matches the one of the .py file (or no .py file exists),
then the .pyc file is used. In any other case, the .py file is read,
compiled, and a .pyc file is written -- if possible.

So, if you ensure that both platforms "see" the same
last-modification-time, and you use the same Python version on both
platforms, then .pyc files may be shared. If not, each time you switch
platforms all .pyc files will be re-created (option -B avoids that, as
already noted).

I any case, I don't think leaving the wrong .pyc files should create any
problem (except some delay when the application starts, and unless you've
found a bug, of course)

--
Gabriel Genellina

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


Re: unicode confusing

2009-05-26 Thread Pet
On May 25, 6:07 pm, Paul Boddie  wrote:
> On 25 Mai, 17:39, someone  wrote:
>
> > Hi,
>
> > reading content of webpage (encoded in utf-8) with urllib2, I can't
> > get parsed data into DB
>
> > Exception:
>
> >   File "/usr/lib/python2.5/site-packages/pyPgSQL/PgSQL.py", line 3111,
> > in execute
> >     raise OperationalError, msg
> > libpq.OperationalError: ERROR:  invalid UTF-8 byte sequence detected
> > near byte 0xe4
>
> > I've already checked several python unicode tutorials, but I have no
> > idea how to solve my problem.
>
> With pyPgSQL, there are a few tricks that you have to take into
> account:
>
> 1. With PostgreSQL, it would appear advantageous to create databases
> using the "-E unicode" option.

Hi,

DB is in UTF8


>
> 2. When connecting, use the client_encoding and unicode_results
> arguments for the connect function call:
>
>   connection = PgSQL.connect(client_encoding="utf-8",
> unicode_results=1)

If I do unicode_results=1, then there are exceptions in other places,
e.g. urllib.urlencode(values)
cant encode values

>
> 3. After connecting, it appears necessary to set the client encoding
> explicitly:
>
>   connection.cursor().execute("set client_encoding to unicode")

I've tried this as well, but still have exceptions

>
> I'd appreciate any suggestions which improve on the above, but what
> this should allow you to do is to present Unicode objects to the
> database and to receive such objects from queries. Whether you can
> relax this and pass UTF-8-encoded strings instead of Unicode objects
> is not something I can guarantee, but it's usually recommended that
> you manipulate Unicode objects in your program where possible, and
> here you should be able to let pyPgSQL deal with the encodings
> preferred by the database.
>

Thanks for your suggestions! Sadly, I can't solve my problem...

Pet

> Paul

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


Re: What text editor is everyone using for Python

2009-05-26 Thread CTO
On May 26, 3:23 am, Lawrence D'Oliveiro  wrote:
> In message , Steven
>
> D'Aprano wrote:
> > On Tue, 26 May 2009 18:31:56 +1200, Lawrence D'Oliveiro wrote:
>
> >> In message  >> b201-4b2445732...@v35g2000pro.googlegroups.com>, LittleGrasshopper
> >> wrote:
>
> >>> ... I am looking for suitable syntax files for [editor of choice] ...
>
> >> I don't understand why people need "syntax files" to use a text editor.
>
> > Do you want syntax highlighting?
>
> Why?

Provides a clear visual indicator of when you've screwed up, which is
pretty important for me ;)

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Hendrik van Rooyen
"Steven D'Aprano"  wrote:

>I use kwrite when on a GUI. When I can't avoid editing files remotely
>over ssh, I use nano.
>
>Why? I dislike Gnome's user-interface, and I find gedit slightly too
>underpowered and dumbed down for my taste. (Although it has a couple of
>nice features.) Of the KDE editors, kedit is too basic and I've never got
>into kate, although perhaps I should. kwrite has a nice clean, consistent
>UI that matches other KDE apps, instead of being hideously ugly like some
>apps I won't mention.
>
Interesting - Kwrite is essentially a bit of a dumbed down Kate - and I use
both - for python stuff I use Kwrite, set up to indent with tabs, and for SDCL
and C code I use Kate, set up to indent with spaces.  I have not noticed a
difference
in the UI, except that Kate keeps a list of active files, and seems to remember
what you were doing last time you used it.  (on SuSe)

When ssh- ing I have been using vim, painfully. Must look at nano - sounds good.
I really miss Brief.

- Hendrik


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


Re: Network programming ?

2009-05-26 Thread Dave Angel

thushiantha...@gmail.com wrote:

Hi everyone,

I am planning to develop a chatting software in Python, for my college
project. I am using Windows Vista. Is it possible to do sockets
programming in Python ? Any books or websites ?  Also, i want to
develop a gui for that program. What are the gui tool kits available
for windows? I already knew about PyGtk and PyQT, but will they work
properly in Windows platform? Any suggestions?

Thank you. Excuse my English.

  
You're asking two very distinct questions, and I hope others will chime 
in, especially with the sockets one.


1)  There is a module called socket, for low level BSD socket access, 
and it does support Windows (plus Mac OSX, BeOS, OS/2, and "all modern 
Unix"   No experience with it, however.


2) There is tkInterf supplied with Python to do GUI development.  
However, when I studied the choices I knew about, I picked wxPython.  I 
don't even remember the other choices, because it's hard to learn more 
than one.


see:  http://www.python.org/
and http://wiki.wxpython.org/

Both totally free, and with very easy (msi) installers for Windows.

Watch out for versions, though.  If you're goin to run wxPython, you 
want to get Python 2.5x or 2.6x, nothing later.  And many say that  
wxPython for  2.5x is more stable,

Then when you download wxPython, you need to get the matching version.


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Ant
On May 25, 6:35 pm, LittleGrasshopper  wrote:
> With so many choices, I was wondering what editor is the one you
> prefer when coding Python, and why. I normally use vi, and just got

I use GVim or vim if on a terminal. There are quite a few plugins and
scripts I find invaluable, but most are not python specific, and many
are home grown (if compiled against python, you can script vim using
Python).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode confusing

2009-05-26 Thread Pet
On May 26, 9:29 am, Pet  wrote:
> On May 25, 6:07 pm, Paul Boddie  wrote:
>
>
>
>
>
> > On 25 Mai, 17:39, someone  wrote:
>
> > > Hi,
>
> > > reading content of webpage (encoded in utf-8) with urllib2, I can't
> > > get parsed data into DB
>
> > > Exception:
>
> > >   File "/usr/lib/python2.5/site-packages/pyPgSQL/PgSQL.py", line 3111,
> > > in execute
> > >     raise OperationalError, msg
> > > libpq.OperationalError: ERROR:  invalid UTF-8 byte sequence detected
> > > near byte 0xe4
>
> > > I've already checked several python unicode tutorials, but I have no
> > > idea how to solve my problem.
>
> > With pyPgSQL, there are a few tricks that you have to take into
> > account:
>
> > 1. With PostgreSQL, it would appear advantageous to create databases
> > using the "-E unicode" option.
>
> Hi,
>
> DB is in UTF8
>
>
>
> > 2. When connecting, use the client_encoding and unicode_results
> > arguments for the connect function call:
>
> >   connection = PgSQL.connect(client_encoding="utf-8",
> > unicode_results=1)
>
> If I do unicode_results=1, then there are exceptions in other places,
> e.g. urllib.urlencode(values)
> cant encode values
>
>
>
> > 3. After connecting, it appears necessary to set the client encoding
> > explicitly:
>
> >   connection.cursor().execute("set client_encoding to unicode")
>
> I've tried this as well, but still have exceptions
>
>
>
> > I'd appreciate any suggestions which improve on the above, but what
> > this should allow you to do is to present Unicode objects to the
> > database and to receive such objects from queries. Whether you can
> > relax this and pass UTF-8-encoded strings instead of Unicode objects
> > is not something I can guarantee, but it's usually recommended that
> > you manipulate Unicode objects in your program where possible, and
> > here you should be able to let pyPgSQL deal with the encodings
> > preferred by the database.
>
> Thanks for your suggestions! Sadly, I can't solve my problem...
>
> Pet
>
>
>
> > Paul

After some time, I've tried, to convert result with unicode(result,
'ISO-8859-15') and that was it :)
I've thought it was already utf-8, because of charset defining in
 of webpage I'm fetching
Pet
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: A fast way to read last line of gzip archive ?

2009-05-26 Thread Barak, Ron
Hi David, 
Thanks for the below solutions: most illuminating.
I implemented your previous message suggestions, and already the processing 
time (on my datasets) is within acceptable human times.
I'll try your suggestion below.
Thanks again.
Ron.

> -Original Message-
> From: David Bolen [mailto:db3l@gmail.com] 
> Sent: Tuesday, May 26, 2009 03:56
> To: python-list@python.org
> Subject: Re: A fast way to read last line of gzip archive ?
> 
> "Barak, Ron"  writes:
> 
> > I couldn't really go with the shell utilities approach, as 
> I have no 
> > say in my user environment, and thus cannot assume which 
> binaries are 
> > install on the user's machine.
> 
> I suppose if you knew your target you could just supply the 
> external binaries to go with your application, but I agree 
> that would probably be more of a pain than its worth for the 
> performance gain in real world time.
> 
> > I'll try and implement your last suggestion, and see if the 
> > performance is acceptable to (human) users.
> 
> In terms of tuning the third option a bit, I'd play with the 
> tracking of the final two chunk (as mentioned in my first 
> response), perhaps shrinking the chunk size or only 
> processing a smaller chunk of it for lines (assuming a 
> reasonable line size) to minimize the final loop.
> You could also try using splitlines() on the final buffer 
> rather than a StringIO wrapper, although that'll have a 
> memory hit for the constructed list but doing a small portion 
> of the buffer would minimize that.
> 
> I was curious what I could actually achieve, so here are 
> three variants that I came up with.
> 
> First, this just fine tunes slightly tracking the chunks and 
> then only processes enough final data based on anticipated 
> maximum length length (so if the final line is longer than 
> that you'll only get the final MAX_LINE bytes of that line).  
> I also found I got better performance using a smaller 1024 
> chunk size with GZipFile.read() than a MB - not entirely sure 
> why although it perhaps matches the internal buffer size
> better:
> 
> # last-chunk-2.py
> 
> import gzip
> import sys
> 
> CHUNK_SIZE = 1024
> MAX_LINE = 255
> 
> in_file = gzip.open(sys.argv[1],'r')
> 
> chunk = prior_chunk = ''
> while 1:
> prior_chunk = chunk
> # Note that CHUNK_SIZE here is in terms of decompressed data
> chunk = in_file.read(CHUNK_SIZE)
> if len(chunk) < CHUNK_SIZE:
> break
> 
> if len(chunk) < MAX_LINE:
> chunk = prior_chunk + chunk
> 
> line = chunk.splitlines(True)[-1]
> print 'Last:', line
> 
> 
> On the same test set as my last post, this reduced the 
> last-chunk timing from about 2.7s to about 2.3s.
> 
> Now, if you're willing to play a little looser with the gzip 
> module, you can gain quite a bit more.  If you directly call 
> the internal _read() method you can bypass some of the 
> unnecessary processing read() does, and go back to larger I/O chunks:
> 
> # last-gzip.py
> 
> import gzip
> import sys
> 
> CHUNK_SIZE = 1024*1024
> MAX_LINE = 255
> 
> in_file = gzip.open(sys.argv[1],'r')
> 
> chunk = prior_chunk = ''
> while 1:
> try:
> # Note that CHUNK_SIZE here is raw data size, not 
> decompressed
> in_file._read(CHUNK_SIZE)
> except EOFError:
> if in_file.extrasize < MAX_LINE:
> chunk = chunk + in_file.extrabuf
> else:
> chunk = in_file.extrabuf
> break
> 
> chunk = in_file.extrabuf
> in_file.extrabuf = ''
> in_file.extrasize = 0
> 
> line = chunk[-MAX_LINE:].splitlines(True)[-1]
> print 'Last:', line
> 
> Note that in this case since I was able to bump up 
> CHUNK_SIZE, I take a slice to limit the work splitlines() has 
> to do and the size of the resulting list.  Using the larger 
> CHUNK_SIZE (and it being raw size) will use more memory, so 
> could be tuned down if necessary.
> 
> Of course, the risk here is that you are dependent on the 
> _read() method, and the internal use of the 
> extrabuf/extrasize attributes, which is where _read() places 
> the decompressed data.  In looking back I'm pretty sure this 
> code is safe at least for Python 2.4 through 3.0, but you'd 
> have to accept some risk in the future.
> 
> This approach got me down to 1.48s.
> 
> Then, just for the fun of it, once you're playing a little 
> looser with the gzip module, it's also doing work to compute 
> the crc of the original data for comparison with the 
> decompressed data.  If you don't mind so much about that 
> (depends on what you're using the line for) you can just do 
> your own raw decompression with the zlib module, as in the 
> following code, although I still start with a GzipFile() 
> object to avoid having to rewrite the header processing:
> 
> # last-decompress.py
> 
> import gzip
> import sys
> import zlib
> 
>

Re: What text editor is everyone using for Python

2009-05-26 Thread Hendrik van Rooyen
"Lawrence D'Oliveiro"  wrote:

> In message , Steven 
> D'Aprano wrote:
> 
> > On Tue, 26 May 2009 18:31:56 +1200, Lawrence D'Oliveiro wrote:
> > 
> >> In message  >> b201-4b2445732...@v35g2000pro.googlegroups.com>, LittleGrasshopper
> >> wrote:
> >> 
> >>> ... I am looking for suitable syntax files for [editor of choice] ...
> >> 
> >> I don't understand why people need "syntax files" to use a text editor.
> > 
> > Do you want syntax highlighting?
> 
> Why?

It makes your screen look more busy as you type - for instance, if you
type a " or a ' then it treats the rest of the file from that point on as 
belonging to the same string you are about to start typing, and colours it
all using the colour you have selected for displaying string literals.
This is basically to make you seasick and to force you to type the closing
quote immediately and then to back arrow to inside the string again to finish
typing the string, thereby making sure that firstly you type more keystrokes, 
which will look good on your productivity summary, and secondly to save the
Python parser the hassle of dealing with a string that has no end quote.

Now with a recommendation like that - how can you possibly refuse to use
this excellent technology - are you some kind of Luddite, or what?

:-) - Hendrik


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Piet van Oostrum
> Esmail  (E) wrote:

>E> LittleGrasshopper wrote:
>>> 
>>> So what do you guys use, and why? Hopefully we can keep this civil.

>E> I use Emacs, just because I have been using this editor for
>E> all sorts of things in the last 20+ years.

Me too. I like my tools to be programmable.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode confusing

2009-05-26 Thread Paul Boddie
On 26 Mai, 10:09, Pet  wrote:
>
> After some time, I've tried, to convert result with unicode(result,
> 'ISO-8859-15') and that was it :)

I haven't really investigated having unicode_results set to false (or
the default) with a database containing UTF-8 (or any non-ASCII
encoded) text, since it's always desirable to manipulate Unicode
internally in one's programs: I don't want plain strings containing
various encoded sequences of bytes when I'm dealing with characters.
That said, if one were consuming XML/HTML and then putting it in raw
form into a database (including the tags), I could understand that
Unicode objects might then seem like a distraction.

> I've thought it was already utf-8, because of charset defining in
>  of webpage I'm fetching

There are lots of caveats about Web page encodings - which metadata
actually indicates the encoding - but I still regard the best approach
to involve converting text to Unicode as soon as possible, then
presenting Unicode objects to the database. This way, you can separate
the decisions about which encodings the Web pages are using and which
encoding the database is using.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Jeremiah Dodds
On Mon, May 25, 2009 at 6:35 PM, LittleGrasshopper
wrote:

> With so many choices, I was wondering what editor is the one you
> prefer when coding Python, and why. I normally use vi, and just got
> into Python, so I am looking for suitable syntax files for it, and
> extra utilities. I dabbled with emacs at some point, but couldn't get
> through the key bindings for commands. I've never tried emacs with vi
> keybindings (I forgot the name of it) but I've been tempted.
>
> So what do you guys use, and why? Hopefully we can keep this civil.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I, like some others on this list, use emacs.

I've heard that vi (well, vim) makes a pretty nice python editor though. If
you're comfortable with it, keep using it.
-- 
http://mail.python.org/mailman/listinfo/python-list


which database is suitable for small applications

2009-05-26 Thread Kalyan Chakravarthy
Hi All,
  can any one suggest me which database I can use for my small
application(to store user names ,passwords, very few other data.. )
I am using Python, Google Apps and guide me how to connect to database, I am
very new to these technologies

Please help me

Thanks in advance

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


Re: exec statement with/without prior compile...

2009-05-26 Thread Arnaud Delobelle
"Gabriel Genellina"  writes:

> En Mon, 25 May 2009 11:07:13 -0300, Jaime Fernandez del Rio
>  escribió:
>> And since I'm asking, could something similar, i.e. defining a new
>> function and get a handle to it, be achieved with eval?
>
> No, because def is a statement, and eval only accepts an expression.

Well there is the lambda expression, which would work in the example
given in the OP.

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


How to create a list of functions depending on a parameter?

2009-05-26 Thread enzo michelangeli
Let's suppose I want to create a list of n functions of a single
argument, returning the sum between argument and index in the list, so
that e.g.:

f[0](10) will return 10
f[3](12) will return 15

...and so on. I had naively though of coding:

 f = [lambda x: x+j for j in range(n)]

Unfortunately, each function in the list f[]() behaves as a closure,
and f[k](p) does NOT return p+k but p+j (for whatever value j has at
the moment: typically n, the last value assumed by j in the list
comprehension loop).

Is there a way of achieving my goal? (Of course, n is not a constant
known in advance, so I can't manually unroll the loop.)

Thanks for any suggestion.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-26 Thread Steven D'Aprano
On Tue, 26 May 2009 09:58:40 +0200, Hendrik van Rooyen wrote:

> "Steven D'Aprano"  wrote:
> 
>>I use kwrite when on a GUI. When I can't avoid editing files remotely
>>over ssh, I use nano.
>>
>>Why? I dislike Gnome's user-interface, and I find gedit slightly too
>>underpowered and dumbed down for my taste. (Although it has a couple of
>>nice features.) Of the KDE editors, kedit is too basic and I've never
>>got into kate, although perhaps I should. kwrite has a nice clean,
>>consistent UI that matches other KDE apps, instead of being hideously
>>ugly like some apps I won't mention.
>>
> Interesting - Kwrite is essentially a bit of a dumbed down Kate - and I
> use both - for python stuff I use Kwrite, set up to indent with tabs,
> and for SDCL and C code I use Kate, set up to indent with spaces.  I
> have not noticed a difference
> in the UI, except that Kate keeps a list of active files, and seems to
> remember what you were doing last time you used it.  (on SuSe)


By memory, I started using kwrite before kate even existed, or at least 
before it was included in KDE.

I think the most obvious difference is that kwrite uses one window per 
open file, and kate uses a single IDE-style window for all open files.



> When ssh- ing I have been using vim, painfully. Must look at nano -
> sounds good. I really miss Brief.

nano is basically an updated (forked?) version of pico.



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


Re: 4 hundred quadrillonth?

2009-05-26 Thread Steven D'Aprano
On Mon, 25 May 2009 23:10:02 -0700, Scott David Daniels wrote:

> Steven D'Aprano wrote:
>> On Mon, 25 May 2009 16:21:19 +1200, Lawrence D'Oliveiro wrote:
>>... (0) "Opposite" is not well-defined unless you have a dichotomy. In
>>the ... (1/3) Why do you jump to the conclusion that "pi=3" implies that
>>only ... (1/2) If you "get rid of real numbers", then obviously you must
>>have a ... (2/3) There is *no* point (2/3).
>>... (1) I thought about numbering my points as consecutive increasing
>> integers, but decided that was an awfully boring convention. A shiny
>> banananana for the first person to recognise the sequence.
> 
> I'd call it F_3, but using a Germanic F (Farey sequence limit 3).

That's the one.


> Do I get a banana or one with a few more ans?

I know how to spell bananananana, I just don't know when to stop.





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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Paul Rudin
enzo michelangeli  writes:

> Let's suppose I want to create a list of n functions of a single
> argument, returning the sum between argument and index in the list, so
> that e.g.:
>
> f[0](10) will return 10
> f[3](12) will return 15
>
> ...and so on. I had naively though of coding:
>
>  f = [lambda x: x+j for j in range(n)]
>
> Unfortunately, each function in the list f[]() behaves as a closure,
> and f[k](p) does NOT return p+k but p+j (for whatever value j has at
> the moment: typically n, the last value assumed by j in the list
> comprehension loop).
>
> Is there a way of achieving my goal? (Of course, n is not a constant
> known in advance, so I can't manually unroll the loop.)
>

class Foo(object):

def __init__(self, pos):
self.pos = pos

def __call__(self, arg):
return self.pos + arg

f = [Foo(x) for x in range(10)]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Diez B. Roggisch

enzo michelangeli schrieb:

Let's suppose I want to create a list of n functions of a single
argument, returning the sum between argument and index in the list, so
that e.g.:

f[0](10) will return 10
f[3](12) will return 15

...and so on. I had naively though of coding:

 f = [lambda x: x+j for j in range(n)]

Unfortunately, each function in the list f[]() behaves as a closure,
and f[k](p) does NOT return p+k but p+j (for whatever value j has at
the moment: typically n, the last value assumed by j in the list
comprehension loop).

Is there a way of achieving my goal? (Of course, n is not a constant
known in advance, so I can't manually unroll the loop.)


You need to capture n into the closure of the lambda:

f = [lambda x, n=n: x+j for j in xrange(n)]

Also note the use of xrange instead of range - the latter creates a 
list-object while the former only returns a generator. Most of the 
times, xrange is thus faster, and also most of the times less memory 
consuming (exception to this rule occur for small n, and are neglible)


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Steven D'Aprano
On Tue, 26 May 2009 19:23:19 +1200, Lawrence D'Oliveiro wrote:

> In message ,
> Steven D'Aprano wrote:
> 
>> On Tue, 26 May 2009 18:31:56 +1200, Lawrence D'Oliveiro wrote:
>> 
>>> In message >> b201-4b2445732...@v35g2000pro.googlegroups.com>, LittleGrasshopper
>>> wrote:
>>> 
 ... I am looking for suitable syntax files for [editor of choice] ...
>>> 
>>> I don't understand why people need "syntax files" to use a text
>>> editor.
>> 
>> Do you want syntax highlighting?
> 
> Why?

The human visual system is evolved to detect, and respond to, differences 
in the frequency of light entering the eye. This is called "colour", and 
colour can be used to make certain things stand out more than others, or 
fade more into the background. *wink*

The use of colour not only increases the bandwidth of information 
entering the brain, but it can be used to discriminate between what's 
important and what isn't. Personally, I find that it is worth having 
syntax highlighting just to get comments displayed in grey instead of 
black. Everything else is a bonus: e.g. I can instantly tell if I 
neglected to close a string, because my code displays in red.



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


Re: What text editor is everyone using for Python

2009-05-26 Thread Paul Rudin
Steven D'Aprano  writes:

>  e.g. I can instantly tell if I neglected to close a string, because
> my code displays in red.

I like syntax hightlighting for whitespace related things in python. e.g
to highlight tabs, or whitespace at the end of a line.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Paul Rudin
"Diez B. Roggisch"  writes:

> enzo michelangeli schrieb:
>> Let's suppose I want to create a list of n functions of a single
>> argument, returning the sum between argument and index in the list, so
>> that e.g.:
>>
>> f[0](10) will return 10
>> f[3](12) will return 15
>>
>> ...and so on. I had naively though of coding:
>>
>>  f = [lambda x: x+j for j in range(n)]
>>
>> Unfortunately, each function in the list f[]() behaves as a closure,
>> and f[k](p) does NOT return p+k but p+j (for whatever value j has at
>> the moment: typically n, the last value assumed by j in the list
>> comprehension loop).
>>
>> Is there a way of achieving my goal? (Of course, n is not a constant
>> known in advance, so I can't manually unroll the loop.)
>
> You need to capture n into the closure of the lambda:
>
> f = [lambda x, n=n: x+j for j in xrange(n)]
>

But be careful using such a technique with mutable arguments...

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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Arnaud Delobelle
"Diez B. Roggisch"  writes:

> You need to capture n into the closure of the lambda:
>
> f = [lambda x, n=n: x+j for j in xrange(n)]

You mean  [lambda x, j=j: x+j for j in xrange(n)]

Another way would be [(lambda j:lambda x: x+j)(j) for j in xrange(n)]

Or more readably:

def adder(n):
return lambda x: x+n

[adder(j) for j in xrange(n)]

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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread enzo michelangeli
Thanks guys. So far I had only come out with

f = [eval('lambda x: x+'+str(j)) for j in range(3)]

...which I hated because, as everybody knows, eval is evil :-)

Enzo

On May 26, 5:20 pm, Arnaud Delobelle  wrote:
> "Diez B. Roggisch"  writes:
>
> > You need to capture n into the closure of the lambda:
>
> > f = [lambda x, n=n: x+j for j in xrange(n)]
>
> You mean  [lambda x, j=j: x+j for j in xrange(n)]
>
> Another way would be [(lambda j:lambda x: x+j)(j) for j in xrange(n)]
>
> Or more readably:
>
> def adder(n):
>     return lambda x: x+n
>
> [adder(j) for j in xrange(n)]
>
> --
> Arnaud

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


Re: A list with periodic boundary conditions

2009-05-26 Thread youh...@googlemail.com

Rodhri,

thank you very much for your reply. I've tried to extend what you
suggested; I think it can be made to work, but you're right -- it gets
horrid rapidly. I'm at the start of what promises to be a large
project, and I want to make sure that I get my data structures right,
but my impression right now is that the memory efficiency of views is
negated by the computational complexity of their implementation. So I
think I will stick to copies and be done with it.

For the record, part of the reason I wanted a view was that my class
will be inherited from numpy.ndarray, where, by default, slices return
views.

Thanks again,

-AM

On 22 May, 00:20, "Rhodri James"  wrote:
> On Thu, 21 May 2009 13:08:39 +0100,  wrote:
> > Hi,
>
> > I'm trying to create a new class of list that has periodic boundary
> > conditions.
>
> > Here's what I have so far:
>
> > class wrappedList(list):
> >     def __getitem__(self, p):
> >         return list.__getitem__(self, p%len(self))
> >     def __setitem__(self, p, v):
> >         list.__setitem__(self, p%len(self), v)
>
> > a=wrappedList(range(10))
> > a[1]
> > 1
> > a[11]
> > 1
>
> > But I would also like to make slices. For instance I would like
> > a[8:11]
> > [8, 9, 0]
>
> > I can do it by copying, but I want to return a view to the original
> > data, and I have no idea where to start. It seems that the slice needs
> > to retain some knowledge of the list from which it derived. i.e. it
> > needs to know that it is a slice. Any ideas on how I can extend this
> > to allow views?
>
> Reading the docs, this looks like a very messy area of Python 2.x in
> that for a builtin like `list` you have to provide a __getslice__ method
> despite it being deprecated and only dealing with simple slices.  Good
> luck with that.  In Python 3, you just have to deal with the fact that
> your __getitem__ and __setitem__ `p` arguments can be slice objects
> instead of integers.
>
> If you're set on making your slices views on the original (which means
> that changes to the slice will change the original, unlike normal lists!)
> then you need to extend your class to remember what it's viewing, and
> what the start, stop and step of the slice were.  When it's a view, it
> passes (massaged) requests up to its parent.  There's a cute (but likely
> inefficient) way of doing this that uses the fact that your view is still
> a list under the hood, and stashes the parental indices in it.  This will
> also make len() work correctly, for a bonus :-)  It gets quite horrid
> quite fast, but here's a very incomplete untested skeleton:
>
> class wrappedList(list):
>    def __init__(self, parent=None, *args):
>      list.__init__(self, *args)
>      self.parent = parent
>
>    def __getitem__(self, p):
>      if self.parent is None:
>        self.primary_getitem(p)
>      else:
>        self.view_getitem(p)
>
>    def view_getitem(self, p):
>      return self.parent[list.__getitem__(self, p%len(self))]
>
> ...and similarly for __setitem__, where primary_getitem() is your
> previous __getitem__ method.  All four need to be modified to cope
> with slices, of course, and to create new wrappedList objects.
> Something like this:
>
>    if isinstance(p, slice):
>      if p.start is None:
>        start = 0
>      else:
>        start = p.start
>      if p.step is None:
>        step = 1
>      else:
>        step = p.step
>      indices = range(start, p.stop, step)
>      return wrappedList(indices, parent=self)
>
> This will go horribly, horribly wrong if you delete anything from
> your original list, but I can't off-hand think of a view method
> that won't.
>
> --
> Rhodri James *-* Wildebeeste Herder to the Masses

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


Re: Network programming ?

2009-05-26 Thread CTO
On May 25, 11:05 pm, thushiantha...@gmail.com wrote:
> Hi everyone,
>
> I am planning to develop a chatting software in Python, for my college
> project. I am using Windows Vista. Is it possible to do sockets
> programming in Python ? Any books or websites ?  Also, i want to
> develop a gui for that program. What are the gui tool kits available
> for windows? I already knew about PyGtk and PyQT, but will they work
> properly in Windows platform? Any suggestions?
>
> Thank you. Excuse my English.

There's a book called Foundations of Python Network Programming that
is
pretty much as good a book as you could ever ask for on the subject. I
strongly recommend it, and I think you'll find many of the examples
relevant.

As far as PyGTK and PyQT, my understanding is that they will work, but
require some effort. You may want to look at PyGUI, which I haven't
used
but have consistently heard good things about- they seem to have good
windows support.

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


Re: Are Python-based web frameworks reliable enough?

2009-05-26 Thread Bruno Desthuilliers

Gilles Ganault a écrit :

Hello

Until now, the modest web apps I wrote were all in PHP because it's
available on just about any hosted server.

I now have a couple of ideas for applications where I would deploy my
own servers, so that I'd rather write them in Python because I find
the language more pleasant to write with.

I read that the traction for the mod_python module isn't as strong as
mod_php, so I guess I should consider the alternative, which is
writing a long-running process with some framework like TurboGears or
Django.

To make an informed choice, I'd like your feedback on this:

1. Is mod_python a bad choice today? If yes, are there other PHP-like
modules available, ie. the Python code is hosted in pages that are
loaded every time a user calls them?


I indeed wouldn't use mod_python for web app development, unless I need 
really strong integration with apache and don't care for portability (=> 
 deploying on other web servers).


And while we're at it, the server page model (PHP etc) is just braindead 
for anything more complicated than a simple personal home page. FWIW, 
even PHP coders tend to go away from it and turn to more "structured" 
frameworks like Symfony or CakePHP - which look much more like Django, 
TG, Pylons or (for CakePHP) Ruby On Rails.



2. If you think I should consider long-running processes, are the
frameworks reliable enough so that I shouldn't fear crashes every so
often?


My experience is that they are usually much more reliable than most PHP 
code around.



3. What about performance as compared to equivalent PHP/MySQL apps?


The right question here would be "what about performances as compared to 
equivalent PHP apps of equivalent quality written using similar 
frameworks, deployed on equivalent web servers and getting equivalent 
loads".


Ok, to make a long story short: I never had any performance issues so 
far with app written in Django, and never heard of anyone reporting such 
issues. I whish I could say the same about Zope/Plone...

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


Re: which database is suitable for small applications

2009-05-26 Thread Jean-Michel Pichavant

Kalyan Chakravarthy wrote:

Hi All,
  can any one suggest me which database I can use for my small 
application(to store user names ,passwords, very few other data.. )
I am using Python, Google Apps and guide me how to connect to 
database, I am very new to these technologies


Please help me

Thanks in advance

--
Regards
Kalyan

If you are really new to these technologies and don't want to spend some 
time on it, just serialize on the disk your data structure containing 
your names, password and so on (http://docs.python.org/library/pickle.html).
Depending on your application you may not be that concerned with 
security/reliability issues.


Of course if you want to learn more about it, go on. I personally use 
postgreSQL with the pgdb python module. But this is the only one I ever 
used so I'll let someone else more familiar with all the database types 
respond to you.


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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Diez B. Roggisch
Paul Rudin wrote:

> "Diez B. Roggisch"  writes:
> 
>> enzo michelangeli schrieb:
>>> Let's suppose I want to create a list of n functions of a single
>>> argument, returning the sum between argument and index in the list, so
>>> that e.g.:
>>>
>>> f[0](10) will return 10
>>> f[3](12) will return 15
>>>
>>> ...and so on. I had naively though of coding:
>>>
>>>  f = [lambda x: x+j for j in range(n)]
>>>
>>> Unfortunately, each function in the list f[]() behaves as a closure,
>>> and f[k](p) does NOT return p+k but p+j (for whatever value j has at
>>> the moment: typically n, the last value assumed by j in the list
>>> comprehension loop).
>>>
>>> Is there a way of achieving my goal? (Of course, n is not a constant
>>> known in advance, so I can't manually unroll the loop.)
>>
>> You need to capture n into the closure of the lambda:
>>
>> f = [lambda x, n=n: x+j for j in xrange(n)]
>>
> 
> But be careful using such a technique with mutable arguments...

What makes "this technique" more susceptible to mutable argument issues than
any other function or method-definition?

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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Diez B. Roggisch
Arnaud Delobelle wrote:

> "Diez B. Roggisch"  writes:
> 
>> You need to capture n into the closure of the lambda:
>>
>> f = [lambda x, n=n: x+j for j in xrange(n)]
> 
> You mean  [lambda x, j=j: x+j for j in xrange(n)]


Ah, sorry, parentheses-problem.

Diez


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Lawrence D'Oliveiro
In message , Hendrik van 
Rooyen wrote:

> "Lawrence D'Oliveiro"  wrote:
>> 
>> Why [do you want syntax highlighting]?
> 
> It makes your screen look more busy as you type - for instance, if you
> type a " or a ' then it treats the rest of the file from that point on as
> belonging to the same string you are about to start typing, and colours it
> all using the colour you have selected for displaying string literals.
> This is basically to make you seasick and to force you to type the closing
> quote immediately and then to back arrow to inside the string again to
> finish typing the string, thereby making sure that firstly you type more
> keystrokes, which will look good on your productivity summary, and
> secondly to save the Python parser the hassle of dealing with a string
> that has no end quote.

Yeah, that's about what I figured. I think I first came across it 15 years 
ago in Metrowerks CodeWarrior. I tried it for a while, but it didn't really 
help with any of my program bugs, so I turned it off.


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


What is the difference between init and enter?

2009-05-26 Thread John
I'm okay with init, but it seems to me that enter is redundant since it 
appears that anything you want to execute in enter can be done in init.

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


DB-API execute params, am I missing something?

2009-05-26 Thread Gabriel Rossetti

Hello everyone, I am trying to use dbapi with mysql and I get this error:

Traceback (most recent call last):
 File "", line 1, in 
 File "", line 2, in getUnitParams
 File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 151, 
in execute

   query = query % db.literal(args)
TypeError: int argument required

with this code :

import MySQLdb

def getParams(curs):
   curs.execute("select * from param where id=%d", 1001)
   return curs.fetchall()

cp = MySQLdb.connect(host="localhost",
port=3306,
user="root",
passwd="123",
db="test")

curs = cp.cursor()
result = getParams(curs)

I checked MySQLdb.paramstyle and it uses "format". I also tried passing 
(1001,) instead of just 1001 as the param but this changes nothing. What 
am I doing wrong?


Thank you,
Gabriel
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between init and enter?

2009-05-26 Thread Diez B. Roggisch
John wrote:

> I'm okay with init, but it seems to me that enter is redundant since it
> appears that anything you want to execute in enter can be done in init.

About what are you talking? 

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Lacrima
I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: email scanning for X-Spam-Score

2009-05-26 Thread skip
Helmut>mailmsg = email.message_from_string(msg)
Helmut>SPAM_CORE = mailmsg['X-Spam-Score']

Maybe lower case?

SPAM_CORE = mailmsg['x-spam-score']

-- 
Skip Montanaro - s...@pobox.com - http://www.smontanaro.net/
America's vaunted "free press" notwithstanding, story ideas that expose
the unseemly side of actual or potential advertisers tend to fall by the
wayside.  Not quite sure why.  -- Jim Thornton
-- 
http://mail.python.org/mailman/listinfo/python-list


Building Python with icc on 64-bit Linux

2009-05-26 Thread Konrad Hinsen
I am trying to install Python 2.6.2 on a 64-bit Linux machine (RedHat  
Enterprise) using the Intel compiler (version 11). Even without  
optimizations (-O0), I get a compilation error in the ctypes module


icc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -O0 -I. -I/home/shr/ 
khinsen/tm\
p/Python-2.6.2/./Include -Ibuild/temp.linux-x86_64-2.6/libffi/include  
-Ibuild/t\
emp.linux-x86_64-2.6/libffi -I/home/shr/khinsen/tmp/Python-2.6.2/ 
Modules/_ctype\
s/libffi/src -I. -IInclude -I./Include -I/usr/local/include -I/home/ 
shr/khinsen\
/tmp/Python-2.6.2/Include -I/home/shr/khinsen/tmp/Python-2.6.2 -c / 
home/shr/khi\
nsen/tmp/Python-2.6.2/Modules/_ctypes/libffi/src/x86/ffi64.c -o build/ 
temp.linu\
x-x86_64-2.6/home/shr/khinsen/tmp/Python-2.6.2/Modules/_ctypes/libffi/ 
src/x86/f\

fi64.o
/home/shr/khinsen/tmp/Python-2.6.2/Modules/_ctypes/libffi/src/x86/ 
ffi64.c(43): \

error: identifier "__int128_t" is undefined
__int128_t sse[MAX_SSE_REGS];
^
compilation aborted for /home/shr/khinsen/tmp/Python-2.6.2/Modules/ 
_ctypes/libf\

fi/src/x86/ffi64.c (code 2)


and failed test:

test test_cmath failed -- Traceback (most recent call last):
  File "/home/shr/khinsen/tmp/Python-2.6.2/Lib/test/test_cmath.py",  
line 366, i\

n test_specific_values
self.fail(error_message)
AssertionError: acos: acos(complex(0.0, 0.0))
Expected: complex(1.5707963267948966, -0.0)
Received: complex(1.5707963267948966, 0.0)
Received value insufficiently close to expected value.


Adding optimization yields even more failed tests. My configuration  
options are:


configure --prefix=$HOME CC=icc CXX=icc OPT=-O0

Did anyone encounter these problems before? Any solutions?

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


Re: DB-API execute params, am I missing something?

2009-05-26 Thread Diez B. Roggisch
Gabriel Rossetti wrote:

> Hello everyone, I am trying to use dbapi with mysql and I get this error:
> 
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 2, in getUnitParams
>   File "/var/lib/python-support/python2.5/MySQLdb/cursors.py", line 151,
> in execute
> query = query % db.literal(args)
> TypeError: int argument required
> 
> with this code :
> 
> import MySQLdb
> 
> def getParams(curs):
> curs.execute("select * from param where id=%d", 1001)
> return curs.fetchall()
> 
> cp = MySQLdb.connect(host="localhost",
>  port=3306,
>  user="root",
>  passwd="123",
>  db="test")
> 
> curs = cp.cursor()
> result = getParams(curs)
> 
> I checked MySQLdb.paramstyle and it uses "format". I also tried passing
> (1001,) instead of just 1001 as the param but this changes nothing. What
> am I doing wrong?

AFAIK you need to use %s, regardless of what type you pass.

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


Re: which database is suitable for small applications

2009-05-26 Thread Tim Chase

  can any one suggest me which database I can use for my small
application(to store user names ,passwords, very few other data.. )
I am using Python, Google Apps and guide me how to connect to database, I am
very new to these technologies


Well, depending on the complexity you need, you can use either 
the standard  "anydbm" module for simple, persistent key-value 
string-to-string mappings; or you can use the "sqlite3" module 
for a more full-fledged SQL experience with a local data-store. 
If you need to step up to a shared database, you can investigate 
the various MySQL, PostgreSQL, etc. bindings that can be added to 
Python.


You can read about both the "anydbm" and "sqlite3" modules (along 
with example code) in the standard library reference docs.


-tkc



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


Re: What text editor is everyone using for Python

2009-05-26 Thread J Kenneth King
Lacrima  writes:

> I am new to python.
> And now I am using trial version of Wing IDE.
> But nobody mentioned it as a favourite editor.
> So should I buy it when trial is expired or there are better choices?

That is a slightly better question.

Try some of the free alternatives.  I do happen to use emacs.  It took
me quite a lot of adjusting to get used to it after being a vim user
for almost ten years.  Doesn't cost anything to give one a shot and
see if it works for you.  The choice of editor is a personal one
(hence the reluctance to answer your original question).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between init and enter?

2009-05-26 Thread Ken Seehart

Diez B. Roggisch wrote:

John wrote:

  

I'm okay with init, but it seems to me that enter is redundant since it
appears that anything you want to execute in enter can be done in init.



About what are you talking? 


Diez
  

Presumably, the 'with' statement.  http://www.python.org/dev/peps/pep-0343/

   *__enter__*(self)

   Enter the runtime context related to this object. The *with*
    statement will bind this method's
   return value to the target(s) specified in the *as*
    clause of the statement, if any.


Unlike __init__,  __enter__ can return a value, which is assigned to the 
variable (or tuple) following the 'as' keyword:


   with EXPR as VAR:
   BLOCK

Also, the object used in a with statement can be constructed prior to 
the with statement.  The __init__ method is called when the object is 
initialized, but the __enter__ method is called when the context is 
entered (i.e. when the 'with' statement is invoked).


Ken

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


Re: which database is suitable for small applications

2009-05-26 Thread J Kenneth King
Jean-Michel Pichavant  writes:

> Kalyan Chakravarthy wrote:
>> Hi All,
>>   can any one suggest me which database I can use for my
>> small application(to store user names ,passwords, very few other
>> data.. )
>> I am using Python, Google Apps and guide me how to connect to
>> database, I am very new to these technologies
>>
>> Please help me
>>
>> Thanks in advance
>>
>> -- 
>> Regards
>> Kalyan
>>
> If you are really new to these technologies and don't want to spend
> some time on it, just serialize on the disk your data structure
> containing your names, password and so on
> (http://docs.python.org/library/pickle.html).
> Depending on your application you may not be that concerned with
> security/reliability issues.
>
> Of course if you want to learn more about it, go on. I personally use
> postgreSQL with the pgdb python module. But this is the only one I
> ever used so I'll let someone else more familiar with all the database
> types respond to you.
>
> Jean-Michel

sqlite is also a pretty lite database. Sits in a single file and the
libraries ship with Python (> 2.6 I think? or maybe 2.5?).

Serialization is always a neat option. With a hash table DB like
berkeley or some such you can get a pretty speedy (albeit simple)
persistence layer.

Really depends on what your app needs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between init and enter?

2009-05-26 Thread Lie Ryan

Diez B. Roggisch wrote:

John wrote:


I'm okay with init, but it seems to me that enter is redundant since it
appears that anything you want to execute in enter can be done in init.


About what are you talking? 


Diez


Do you mean __init__ and __enter__?

They are used for two completely different purpose. __init__ is used to 
initialize an object when they've just been created. __enter__ is called 
when an object enters a Context Manager (i.e. with statement)


Example, when opening a file:

# this calls file.__init__
f = open('foo.bar')

# some codes...

# this calls file.__enter__
with f:
pass

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Jean-Michel Pichavant

Why buy an IDE when you just need a text editor ? I don't get it.
Anyway gvim (aka vim aka vi) and emacs are the most powerful editors for 
years. Both have Windows and Linux version and most important, they both 
are very effective at editing any file type (python, C, latex, love 
letters...)
Emacs is more accessible to the newby but requires time to master its 
complex features.
Gvim may be complex for the newby due to its command/insertion mode 
constant switch, but is as powerful as emacs can be. Being a gvim adept, 
I should stat that gvim is far better but it would only feed the 
neverending war.


Jean-Michel


Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
  


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Roy Smith
In article ,
 "Hendrik van Rooyen"  wrote:

> [syntax coloring] makes your screen look more busy as you type - for 
> instance, if you
> type a " or a ' then it treats the rest of the file from that point on as 
> belonging to the same string you are about to start typing, and colours it
> all using the colour you have selected for displaying string literals.
> This is basically to make you seasick and to force you to type the closing
> quote immediately 

Good syntax coloring implementations have some hysteresis built in, to 
prevent transient things (like strings you haven't closed yet) from 
changing the entire file.  If you stop typing for a while, it then does a 
full update.  This avoids the problem above.

That being said, syntax coloring is like any other tool.  It's worth 
trying, but if you find it hinders you more than it helps, turn it off.  
Different strokes for different folks.

My pet peeve is syntax-aware editors which get things wrong.  For example, 
the version of emacs I'm using now doesn't parse this properly:

'''A triple-quoted string.  Some editors won't get this right'''

The solution is to change the outer quotes to double-quotes, but it annoys 
me when I have to change my code to appease a tool.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-26 Thread Ken Seehart

Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
  

It's my favorite.  Buy it.  I'm not aware of any better choices.

If you can afford the Pro version buy that, especially if you are 
planning any large projects.  But take a peek at the feature list before 
making that decision: http://www.wingware.com/wingide/features .  Some 
of my favorite Pro features are test suite support, advanced debugging, 
and code folding.  The debugger is way more powerful in the Pro version.


Of course, favorites are ultimately determined by religious preference.

Ken

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


Re: Regarding sort()

2009-05-26 Thread Scott David Daniels

Jaime Fernandez del Rio wrote:

... the reasons are starting to pile to fare 2.6 goodbye and move on to 3.0...

If you wait just a bit (TM)*, you'd be better served to move on to
3.1.  I think 3.0 is for learning, both for Python developers and
users, and has less "fit and polish" than previous Python releases.
Between 2.x and 3.0, there were huge changes, and some substantial
restructuring.  The move to Unicode strings was not without its
bumps and bruises, and efficiency was (quite properly) by no means
the top priority.  As a result, "normal" file I/O on 3.0 is slowed,
and getting that fixed was not a priority (as correctness was plenty
of work).

I'd view 3.0 as the first real-world view of Python 3000, and 3.1 as
getting the surprise burs and sharp edges of 3.0 cleaned up a bit.


* 3.1 is already in beta, according to PEP 375, the first release
  candidate is coming 30-May, with final release on 27-June:
  http://python.org/dev/peps/pep-0375

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: which database is suitable for small applications

2009-05-26 Thread Mike Driscoll
On May 26, 8:16 am, J Kenneth King  wrote:
> Jean-Michel Pichavant  writes:
> > Kalyan Chakravarthy wrote:
> >> Hi All,
> >>           can any one suggest me which database I can use for my
> >> small application(to store user names ,passwords, very few other
> >> data.. )
> >> I am using Python, Google Apps and guide me how to connect to
> >> database, I am very new to these technologies
>
> >> Please help me
>
> >> Thanks in advance
>
> >> --
> >> Regards
> >> Kalyan
>
> > If you are really new to these technologies and don't want to spend
> > some time on it, just serialize on the disk your data structure
> > containing your names, password and so on
> > (http://docs.python.org/library/pickle.html).
> > Depending on your application you may not be that concerned with
> > security/reliability issues.
>
> > Of course if you want to learn more about it, go on. I personally use
> > postgreSQL with the pgdb python module. But this is the only one I
> > ever used so I'll let someone else more familiar with all the database
> > types respond to you.
>
> > Jean-Michel
>
> sqlite is also a pretty lite database. Sits in a single file and the
> libraries ship with Python (> 2.6 I think? or maybe 2.5?).
>

Sqlite starting shipping with Python 2.5.

I use SqlAlchemy to connect to Microsoft SQL 2000 right now, but I
used to use the mssql and adodb modules. The latter two worked for
what I needed, although I was always running to escaping issues with
them. SqlAlchemy is nice because it does all that for you and you can
switch database back-ends with little to no change in your code.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Mel
LittleGrasshopper wrote:

> With so many choices, I was wondering what editor is the one you
> prefer when coding Python, and why. I normally use vi, and just got
> into Python, so I am looking for suitable syntax files for it, and
> extra utilities. I dabbled with emacs at some point, but couldn't get
> through the key bindings for commands. I've never tried emacs with vi
> keybindings (I forgot the name of it) but I've been tempted.
> 
> So what do you guys use, and why? Hopefully we can keep this civil.
 SciTE
I like one big uncomplicated window, tabbed file panes, syntax coloring and 
help with indentation.  There's nothing to it I hate.  It would be nice if 
customization were easier.

Mel.


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Arnaud Delobelle
Lawrence D'Oliveiro  writes:

> In message , Hendrik van 
> Rooyen wrote:
>
>> "Lawrence D'Oliveiro"  wrote:
>>> 
>>> Why [do you want syntax highlighting]?
>> 
>> It makes your screen look more busy as you type - for instance, if you
>> type a " or a ' then it treats the rest of the file from that point on as
>> belonging to the same string you are about to start typing, and colours it
>> all using the colour you have selected for displaying string literals.
>> This is basically to make you seasick and to force you to type the closing
>> quote immediately and then to back arrow to inside the string again to
>> finish typing the string, thereby making sure that firstly you type more
>> keystrokes, which will look good on your productivity summary, and
>> secondly to save the Python parser the hassle of dealing with a string
>> that has no end quote.
>
> Yeah, that's about what I figured. I think I first came across it 15 years 
> ago in Metrowerks CodeWarrior. I tried it for a while, but it didn't really 
> help with any of my program bugs, so I turned it off.

It's a bit like the colour-coding of electrical cables - distracting
more than anything else!  And don't start me on traffic lights...  They
nearly give me an epileptic fit each time.

That's why I use ed.  Ed is the standard text editor [1].  Ed is open
source [2].

[1] http://www.gnu.org/fun/jokes/ed.msg
[2] http://www.gnu.org/fun/jokes/ed

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


Re: How to create a list of functions depending on a parameter?

2009-05-26 Thread Scott David Daniels

enzo michelangeli wrote:

Let's suppose I want to create a list of n functions of a single
argument, returning the sum between argument and index in the list, so
that e.g.:

f[0](10) will return 10
f[3](12) will return 15

...and so on. I had naively though of coding:

 f = [lambda x: x+j for j in range(n)]


OK, first, almost a joke (unless you really need what you say):
f = [j.__add__ for j in range(n)]

And now the one various others are missing:

def add(x, y):
'''replace with the function you actually want'''
return x + y

f = [functools.partial(add, n) for n in range(0)]

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


large array in a single line

2009-05-26 Thread karthik167
I would like to have a txt file of single line with
[1 2 3 .100]

I try something like
q=arange(100)
fl=file('tmp.ext','w')
fl.writelines(str(q))
fl.close()

Unfortunately my output is

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99]

ie there is automatic split in line after 76 characters. How do I
avoid it? Thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: large array in a single line

2009-05-26 Thread Ken Seehart

Arnaud Delobelle wrote:

karthik...@gmail.com writes:

  

I would like to have a txt file of single line with
[1 2 3 .100]

I try something like
q=arange(100)
fl=file('tmp.ext','w')
fl.writelines(str(q))
fl.close()

Unfortunately my output is

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99]

ie there is automatic split in line after 76 characters. How do I
avoid it? Thanks.



You need to tell us more about the arange() function you use and what
object it returns.
  


That would be *numpy.arange

*Since numpy has it's own idea about formatting, you would have to roll 
your own.


How about:

   q=arange(100)
   *s = '[' + ' '.join([`x` for x in q]) + ']'*
   fl=file('tmp.ext','w')
   fl.*write*(s)
   fl.close()
 


See: http://docs.python.org/tutorial/datastructures.html#list-comprehensions

Incidentally, it's a funny thing that* writelines *coincidentally does 
what you expect it to do when given a string.  I suspect that you need 
to read the documentation on* writelines*:


file.writelines(/sequence/)
   Write a sequence of strings to the file. The sequence can be any
   iterable object producing strings, typically a list of strings.
   There is no return value. (The name is intended to match
   readlines(); writelines() does not add line separators.)

So you are asking it to effectively concatenate the characters in your 
string, (which gives you back your original string), and write the 
result.  Simply using* write *instead would be more efficient in this case.


Ken

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


How to test python snippets in my documents?

2009-05-26 Thread Matthew Wilson
I'm using a homemade script to verify some code samples in my
documentation.  Here it is:

#! /usr/bin/env python2.6

# vim: set expandtab ts=4 sw=4 filetype=python:

import doctest, os, sys

def main(s):
"Run doctest.testfile(s, None)"

return doctest.testfile(s, None)

if __name__ == "__main__":
for x in sys.argv[1:]:
print "testing code excerpts in %s..." % x
print main(x)


The script checks all the files listed as arguments.  This is OK, but is
there anything better?

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


Re: [Python-Dev] PEP 384: Defining a Stable ABI

2009-05-26 Thread Martin v. Löwis
> Now, with the PEP, I have a feeling that the Python C-API
> will in effect be limited to what's in the PEP's idea of
> a usable ABI and open up the non-inluded public C-APIs
> to the same rate of change as the private APIs.

That's certainly not the plan. Instead, the plan is to have
a stable ABI. The policy on the API isn't affected, except
for restricting changes to the API that would break the ABI.

>> During the compilation of applications, the preprocessor macro
>> Py_LIMITED_API must be defined. Doing so will hide all definitions
>> that are not part of the ABI.
> 
> So extensions wanting to use the full Python C-API as documented
> in the C-API docs will still be able to do this, right ?

Correct. They would link to the version-specific DLL on Windows.

>> The structure of type objects is not available to applications;
>> declaration of "static" type objects is not possible anymore
>> (for applications using this ABI).
> 
> Hmm, that's going to create big problems for extensions that
> want to expose a C-API for their types: Type checks are normally
> done by pointer comparison using those static type objects.

I don't see the problem. During module initialization, you
create the type object and store it in a global variable, and
then both clients and the module compare against the stored
pointer.

>> Function-like macros (in particular, field access macros) remain
>> available to applications, but get replaced by function calls
>> (unless their definition only refers to features of the ABI, such
>> as the various _Check macros)
> 
> Including Py_INCREF()/Py_DECREF() ?

Yes, although some people are requesting that these become functions.

>> Excluded Functions
>> --
>>
>> Functions declared in the following header files are not part
>> of the ABI:
>> - cellobject.h
>> - classobject.h
>> - code.h
>> - frameobject.h
>> - funcobject.h
>> - genobject.h
>> - pyarena.h
>> - pydebug.h
>> - symtable.h
>> - token.h
>> - traceback.h
> 
> I don't think that's feasable: you basically remove all introspection
> functions that way.
> 
> This will need a more fine-grained approach.

What specifically is it that you want to do in a module that you
couldn't do anymore?

>> On Windows, applications shall link with python3.dll;
> 
> You mean: extensions that were compiled with Py_LIMITED_API, right ?

Correct, see "Terminology" in the PEP.

> 
>> an import
>> library python3.lib will be available. This DLL will redirect all of
>> its API functions through /export linker options to the full
>> interpreter DLL, i.e. python3y.dll.
> 
> What if you mix extensions that use the full C-API with ones
> that restrict themselves to the limited version ?

Some link against python3.dll, others against python32.dll (say).

> Would creating a Python object in a full-API extension and
> free'ing it in a limited-API extension cause problems ?

No problem that I can see.

>> This PEP will be implemented in a branch, allowing users to check
>> whether their modules conform to the ABI. To simplify this testing, an
>> additional macro Py_LIMITED_API_WITH_TYPES will expose the existing
>> type object layout, to let users postpone rewriting all types. When
>> the this branch is merged into the 3.2 code base, this macro will
>> be removed.
> 
> Now I'm confused again: this sounds a lot like you do want all extension
> writers to only use the limited API.

I certainly want to support as many modules as reasonable with the PEP.
Whether or not developers then chose to build version-independent
binaries is certainly outside the scope of the PEP - it only specifies
action items for Python, not for application authors.

>>> Something I haven't seen explicitly mentioned as yet (in the PEP or the
 python-dev list discussion) are the memory management APIs and the FILE*
 APIs which can cause the MSVCRT versioning issues on Windows.

 Those would either need to be excluded from the stable ABI or else
 changed to use opaque pointers.
>> Good point. As a separate issue, I would actually like to deprecate,
>> then remove these APIs. I had originally hoped that this would happen
>> for 3.0 already, alas, nobody worked on it.
>>
>> In any case, I have removed them from the ABI now.
> 
> How do you expect Python extensions to allocate memory and objects
> in a platform independent way without those APIs ?

I have only removed functions from the ABI that have FILE* in their
signatures.

> And as an aside: Which API families are you referring to ? PyMem_Malloc,
> PyObject_Malloc, or PyObject_New ?

Neither. PyRun_AnyFileFlags and friends.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


scripting and uploading in Python

2009-05-26 Thread Mark Tarver
I've recently purchased a site on a Linux server which runs Python.

Generally I'd like to bring myself up to speed on scripting in
Python.   Any good reads - dead tree or otherwise?

One thing I need to learn is to be able to give folks the power to
upload files onto their own disk space using passwords.

thanks

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Ken Seehart


Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?
  


Jean-Michel Pichavant wrote:

Why buy an IDE when you just need a text editor ? I don't get it.
Anyway gvim (aka vim aka vi) and emacs are the most powerful editors 
for years. Both have Windows and Linux version and most important, 
they both are very effective at editing any file type (python, C, 
latex, love letters...)
Emacs is more accessible to the newby but requires time to master its 
complex features.
Gvim may be complex for the newby due to its command/insertion mode 
constant switch, but is as powerful as emacs can be. Being a gvim 
adept, I should stat that gvim is far better but it would only feed 
the neverending war.


Jean-Michel


I've heard notepad is pretty good.  http://www.notepad.org/

:-) Ken


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


Re: What text editor is everyone using for Python

2009-05-26 Thread Zamnedix
On May 25, 10:35 am, LittleGrasshopper  wrote:
> With so many choices, I was wondering what editor is the one you
> prefer when coding Python, and why. I normally use vi, and just got
> into Python, so I am looking for suitable syntax files for it, and
> extra utilities. I dabbled with emacs at some point, but couldn't get
> through the key bindings for commands. I've never tried emacs with vi
> keybindings (I forgot the name of it) but I've been tempted.
>
> So what do you guys use, and why? Hopefully we can keep this civil.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Benjamin Kaplan
On Tue, May 26, 2009 at 8:07 AM, Lacrima  wrote:

> I am new to python.
> And now I am using trial version of Wing IDE.
> But nobody mentioned it as a favourite editor.
> So should I buy it when trial is expired or there are better choices?


We're mostly talking about text editors, not full-fledged IDEs. Those are
actually 3 different pieces of flame bait right there: favorite text editor,
favorite IDE, and text editors vs. IDEs.

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


Re: large array in a single line

2009-05-26 Thread Dave Angel

karthik...@gmail.com wrote:

I would like to have a txt file of single line with
[1 2 3 .100]

I try something like
q=arange(100)
fl=file('tmp.ext','w')
fl.writelines(str(q))
fl.close()

Unfortunately my output is

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49
 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74
 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99]

ie there is automatic split in line after 76 characters. How do I
avoid it? Thanks.

  


I never heard of arange(), but perhaps you meant range() or xrange().  
If so, the first problem is that range(100) starts with 0 and ends just 
before 100, so you only get the values 0 to 99.  So if you need the 
final value to be 100, you need to pass it a 101.


Next problem is that you're using str() on a list.  All that does is try 
to give you some printable representation of the list.  You've observed 
it divides it into lines, but that's arbitrary.  If you want the output 
to look a certain way, build it that way.   Use write instead of 
writelines, and put your own \n at the end.


Create a for loop, iterate over the list q, writing each value with a 
trailing space, and follow the loop with one extra write, containing the 
closing brace, and the newline.


Don't forget the leading brace as well.


Sometimes debugging these things can be easier if you temporarily use 
sys.stdout instead of a file, while you're working on it.



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


Re: What text editor is everyone using for Python

2009-05-26 Thread Bar Shirtcliff
Ken Seehart writes:
| 
| Lacrima wrote:
| > I am new to python.
| > And now I am using trial version of Wing IDE.
| > But nobody mentioned it as a favourite editor.
| > So should I buy it when trial is expired or there are better choices?
| >   
| 

| I've heard notepad is pretty good.  http://www.notepad.org/
| 
| :-) Ken
| 

Seriously.  I actually know a programmer who does all of his work in
notepad: no macros, syntax-highlighting, or any other robust features,
plus that horrible font.

I use emacs.  No alternative presentation rivals, for me, the ability
to program your editor while you use it.  Never having to reach for
the mouse in emacs is a terrific (and optional) bonus.

I guess it depends on how you work.  If you're willing to invest a
fair amount of time in your editor and you like the idea of being able
to drastically modify your editor while you use it, you should
consider emacs.  Emacs has a good on-line help system, including a
friendly emacs lisp introduction.  If you're using a Mac, check out
Aquamacs Emacs.

I can't say a thing about other editors, except that when some shell
script perversely dumped me into vi a month ago, I felt as horrified
as if some actually living bugs had crawled out of my own reflection
on the computer screen and fallen, clicking and scraping, onto the
keyboard.  That's a personal reaction - totally irrelevant, of course.

Bar 

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


Re: Network programming ?

2009-05-26 Thread Benjamin Kaplan
On Tue, May 26, 2009 at 4:00 AM, Dave Angel  wrote:

> thushiantha...@gmail.com wrote:
>
>> Hi everyone,
>>
>> I am planning to develop a chatting software in Python, for my college
>> project. I am using Windows Vista. Is it possible to do sockets
>> programming in Python ? Any books or websites ?  Also, i want to
>> develop a gui for that program. What are the gui tool kits available
>> for windows? I already knew about PyGtk and PyQT, but will they work
>> properly in Windows platform? Any suggestions?
>>
>> Thank you. Excuse my English.
>>
>>
>>
> You're asking two very distinct questions, and I hope others will chime in,
> especially with the sockets one.
>
> 1)  There is a module called socket, for low level BSD socket access, and
> it does support Windows (plus Mac OSX, BeOS, OS/2, and "all modern Unix"
> No experience with it, however.
>
> 2) There is tkInterf supplied with Python to do GUI development.  However,
> when I studied the choices I knew about, I picked wxPython.  I don't even
> remember the other choices, because it's hard to learn more than one.
>
> see:  http://www.python.org/
> and http://wiki.wxpython.org/
>
> Both totally free, and with very easy (msi) installers for Windows.
>
> Watch out for versions, though.  If you're goin to run wxPython, you want
> to get Python 2.5x or 2.6x, nothing later.  And many say that  wxPython for
>  2.5x is more stable,
> Then when you download wxPython, you need to get the matching version.
>

There was a problem with manifest files in the original Python 2.6 with
wxPython (Python switched to using Visual Studio 2008, wxPython was still
using an older version). I think they have most of the kinks worked out now.


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


Re: large array in a single line

2009-05-26 Thread Robert Kern

On 2009-05-26 09:18, karthik...@gmail.com wrote:

I would like to have a txt file of single line with
[1 2 3 .100]

I try something like
q=arange(100)
fl=file('tmp.ext','w')
fl.writelines(str(q))
fl.close()

Unfortunately my output is

[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
  25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49
  50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
73 74
  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
98 99]

ie there is automatic split in line after 76 characters. How do I
avoid it? Thanks.


You will want to ask numpy questions on the numpy mailing list:

  http://www.scipy.org/Mailing_Lists

You can control the options for printing numpy arrays via the 
numpy.set_printoptions() function.



http://docs.scipy.org/doc/numpy/reference/generated/numpy.set_printoptions.html#numpy.set_printoptions

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: large array in a single line

2009-05-26 Thread karthik167
On May 26, 7:26 pm, Arnaud Delobelle  wrote:
> karthik...@gmail.com writes:
> > I would like to have a txt file of single line with
> > [1 2 3 .100]
>
> > I try something like
> > q=arange(100)
> > fl=file('tmp.ext','w')
> > fl.writelines(str(q))
> > fl.close()
>
> > Unfortunately my output is
>
> > [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
> > 23 24
> >  25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
> > 48 49
> >  50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
> > 73 74
> >  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
> > 98 99]
>
> > ie there is automatic split in line after 76 characters. How do I
> > avoid it? Thanks.
>
> You need to tell us more about the arange() function you use and what
> object it returns.
>
> --
> Arnaud

arange(start, stop=None, step=1, typecode=None)

 Just like range() except it returns an array whose type can be
specified by the keyword argument typecode.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What text editor is everyone using for Python

2009-05-26 Thread Tim Chase

Zamnedix wrote:

So what do you guys use, and why? Hopefully we can keep this civil.


Nano! Nano! Nano Revolution!!!


Thank you, Mork[1] :)

-tkc (who uses Vim for Python coding, and on rare occasions, 
Notepad or ed)


[1]
http://en.wikipedia.org/wiki/Mork_%26_Mindy





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


Re: large array in a single line

2009-05-26 Thread Arnaud Delobelle
karthik...@gmail.com writes:

> On May 26, 7:26 pm, Arnaud Delobelle  wrote:
>> karthik...@gmail.com writes:
>> > I would like to have a txt file of single line with
>> > [1 2 3 .100]
>>
>> > I try something like
>> > q=arange(100)
>> > fl=file('tmp.ext','w')
>> > fl.writelines(str(q))
>> > fl.close()
>>
>> > Unfortunately my output is
>>
>> > [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
>> > 23 24
>> >  25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
>> > 48 49
>> >  50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
>> > 73 74
>> >  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
>> > 98 99]
>>
>> > ie there is automatic split in line after 76 characters. How do I
>> > avoid it? Thanks.
>>
>> You need to tell us more about the arange() function you use and what
>> object it returns.
>>
>> --
>> Arnaud
>
> arange(start, stop=None, step=1, typecode=None)
>
>  Just like range() except it returns an array whose type can be
> specified by the keyword argument typecode.

You didn't mention that it is a numpy function!  It returns a
numpy.ndarray object whose __str__() method automatically inserts
newlines to make the output easier to read I guess.

Don't use this, then. You can do for instance:

fl = open('tmp.ext', 'w')
fl.writelines(['[', ' '.join(range(1, 101)), ']'])
fl.close()

(untested)

-- 
Arnaud

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


Re: Optimizing math functions

2009-05-26 Thread Robert Kern

On 2009-05-24 07:42, Esmail wrote:

Robert Kern wrote:



We have several bounded optimization routines in scipy.

http://docs.scipy.org/doc/scipy/reference/optimize.html


Hi Robert,

Thanks for the lead .. I briefly looked at the documentation, but
before I dig into this more deeply 2 quick questions:

1. Will is also allow me to maximize a function (I only saw minimum)?


Already answered.


2. Will it give me the global minima/maxima?


No, they are local optimizers.


I only ask because I got all excited to see that R has an optimize
function but unfortunately it can not deal with multi-modal functions.
I am trying to come up with some complex test cases for a genetic
algorithm I'm working with, both for functions using f(x) and f(x,y) over a
variety of intervals so I would like to have a way to verify results for
more challenging functions.


You may want to consider using artificial functions with known optima. Here is a 
paper from the ACM Transactions on Mathematical Software doing such:


  http://portal.acm.org/citation.cfm?id=962444
  http://wwwinfo.deis.unical.it/~yaro/GKLS.html
  http://www.netlib.org/toms/829

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: which database is suitable for small applications

2009-05-26 Thread Kushal Kumaran
On Tue, May 26, 2009 at 2:03 PM, Kalyan Chakravarthy
 wrote:
> Hi All,
>   can any one suggest me which database I can use for my small
> application(to store user names ,passwords, very few other data.. )
> I am using Python, Google Apps and guide me how to connect to database, I am
> very new to these technologies
>

Going by your previous mail, I assume you meant Google App Engine when
you said Google Apps.  Given that, searching for google app engine
database gives 
http://code.google.com/appengine/docs/python/gettingstarted/usingdatastore.html
as the first result.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Benjamin Kaplan
On Tue, May 26, 2009 at 11:17 AM, Ken Seehart  wrote:

>
> Lacrima wrote:
>
>> I am new to python.
>> And now I am using trial version of Wing IDE.
>> But nobody mentioned it as a favourite editor.
>> So should I buy it when trial is expired or there are better choices?
>>
>>
>
> Jean-Michel Pichavant wrote:
>
>> Why buy an IDE when you just need a text editor ? I don't get it.
>> Anyway gvim (aka vim aka vi) and emacs are the most powerful editors for
>> years. Both have Windows and Linux version and most important, they both are
>> very effective at editing any file type (python, C, latex, love letters...)
>> Emacs is more accessible to the newby but requires time to master its
>> complex features.
>> Gvim may be complex for the newby due to its command/insertion mode
>> constant switch, but is as powerful as emacs can be. Being a gvim adept, I
>> should stat that gvim is far better but it would only feed the neverending
>> war.
>>
>> Jean-Michel
>>
>>  I've heard notepad is pretty good.  http://www.notepad.org/
>

But it's not available on any *real* OS. XP

>
> :-) Ken
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to test python snippets in my documents?

2009-05-26 Thread Lie Ryan

Matthew Wilson wrote:

I'm using a homemade script to verify some code samples in my
documentation.  Here it is:

#! /usr/bin/env python2.6

# vim: set expandtab ts=4 sw=4 filetype=python:

import doctest, os, sys

def main(s):
"Run doctest.testfile(s, None)"

return doctest.testfile(s, None)

if __name__ == "__main__":
for x in sys.argv[1:]:
print "testing code excerpts in %s..." % x
print main(x)


The script checks all the files listed as arguments.  This is OK, but is
there anything better?



maybe glob.glob('*.py')?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Network programming ?

2009-05-26 Thread thushianthan15
Thank you, for all your support.

I will try wxPython with the sockets module.

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


Re: replacement for execfile

2009-05-26 Thread Scott David Daniels

Alex Popescu wrote:

"Steven D'Aprano"  wrote in
news:pan.2007.07.29.03.39.34.703...@remove.this.cybersource.com.au: 

... you're expecting the users to write working Python code, but
you think "import module" is too hard for them?


That was just a cherry on the cake. The most important part is that 
through this mechanism the user will be able to configure the framework 
through normal Python code (very simple API: 3 functions), and not 
through .ini, .xml or other variants.


I think Steven's point is that you can tell the to define a module with,
for example a setup function.  You then import their module and run
their setup function.  They then have a straightforward environment,
and can, for example do straightforward unit tests on their code if
anything even slightly tricky might be wanted as support code.  I agree
with Steven; much nicer to ask them to define a particular module and go
names inside that module.


--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: What is the difference between init and enter?

2009-05-26 Thread Gary Herron

John wrote:
I'm okay with init, but it seems to me that enter is redundant since 
it appears that anything you want to execute in enter can be done in 
init.


The proper response to a question like this has to be
   http://www.catb.org/~esr/faqs/smart-questions.html
as anything else is complete guesswork.



Gary Herron

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


Re: large array in a single line

2009-05-26 Thread Arnaud Delobelle
karthik...@gmail.com writes:

> I would like to have a txt file of single line with
> [1 2 3 .100]
>
> I try something like
> q=arange(100)
> fl=file('tmp.ext','w')
> fl.writelines(str(q))
> fl.close()
>
> Unfortunately my output is
>
> [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22
> 23 24
>  25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
> 48 49
>  50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
> 73 74
>  75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
> 98 99]
>
> ie there is automatic split in line after 76 characters. How do I
> avoid it? Thanks.

You need to tell us more about the arange() function you use and what
object it returns.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread David Smith
Lacrima wrote:
> I am new to python.
> And now I am using trial version of Wing IDE.
> But nobody mentioned it as a favourite editor.
> So should I buy it when trial is expired or there are better choices?


I use Wing IDE and like it.  It very nicely enforces consistent space
indentations and other Python basics that might fall through the cracks
when writing.  I'm not too hot on the auto-suggest, but I haven't seen
any other IDE do better.

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


Re: How to test python snippets in my documents?

2009-05-26 Thread Ned Deily
In article ,
 Matthew Wilson  wrote:
> I'm using a homemade script to verify some code samples in my
> documentation.  [...]
> The script checks all the files listed as arguments.  This is OK, but is
> there anything better?

Perhaps a test-discovery framework, like nose:

http://ivory.idyll.org/articles/nose-intro.html#running-doctests-in-nose

-- 
 Ned Deily,
 n...@acm.org

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


Re: [Python-Dev] PEP 384: Defining a Stable ABI

2009-05-26 Thread M.-A. Lemburg
Martin v. Löwis wrote:
>> Now, with the PEP, I have a feeling that the Python C-API
>> will in effect be limited to what's in the PEP's idea of
>> a usable ABI and open up the non-inluded public C-APIs
>> to the same rate of change as the private APIs.
> 
> That's certainly not the plan. Instead, the plan is to have
> a stable ABI. The policy on the API isn't affected, except
> for restricting changes to the API that would break the ABI.

Thanks for clarifying this.

>>> During the compilation of applications, the preprocessor macro
>>> Py_LIMITED_API must be defined. Doing so will hide all definitions
>>> that are not part of the ABI.
>> So extensions wanting to use the full Python C-API as documented
>> in the C-API docs will still be able to do this, right ?
> 
> Correct. They would link to the version-specific DLL on Windows.

Good.

>>> The structure of type objects is not available to applications;
>>> declaration of "static" type objects is not possible anymore
>>> (for applications using this ABI).
>> Hmm, that's going to create big problems for extensions that
>> want to expose a C-API for their types: Type checks are normally
>> done by pointer comparison using those static type objects.
> 
> I don't see the problem. During module initialization, you
> create the type object and store it in a global variable, and
> then both clients and the module compare against the stored
> pointer.

Ah, good point !

>>> Function-like macros (in particular, field access macros) remain
>>> available to applications, but get replaced by function calls
>>> (unless their definition only refers to features of the ABI, such
>>> as the various _Check macros)
>> Including Py_INCREF()/Py_DECREF() ?
> 
> Yes, although some people are requesting that these become functions.

I'd opt against that, simply because it creates a lot of overhead
due to the function call and issues with cache locality.

>>> Excluded Functions
>>> --
>>>
>>> Functions declared in the following header files are not part
>>> of the ABI:
>>> - cellobject.h
>>> - classobject.h
>>> - code.h
>>> - frameobject.h
>>> - funcobject.h
>>> - genobject.h
>>> - pyarena.h
>>> - pydebug.h
>>> - symtable.h
>>> - token.h
>>> - traceback.h
>> I don't think that's feasable: you basically remove all introspection
>> functions that way.
>>
>> This will need a more fine-grained approach.
> 
> What specifically is it that you want to do in a module that you
> couldn't do anymore?

See my reply to Nick: some of the functions are needed even
if you don't want to do introspection, such as Py_FatalError()
or PyTraceBack_Print().

BTW: Given the headline, I take it that the various type checking
macros in these header will still be available, right ?

>>> On Windows, applications shall link with python3.dll;
>> You mean: extensions that were compiled with Py_LIMITED_API, right ?
> 
> Correct, see "Terminology" in the PEP.

Good, thanks.

>>> an import
>>> library python3.lib will be available. This DLL will redirect all of
>>> its API functions through /export linker options to the full
>>> interpreter DLL, i.e. python3y.dll.
>> What if you mix extensions that use the full C-API with ones
>> that restrict themselves to the limited version ?
> 
> Some link against python3.dll, others against python32.dll (say).
> 
>> Would creating a Python object in a full-API extension and
>> free'ing it in a limited-API extension cause problems ?
> 
> No problem that I can see.

Can we be sure that the MSVCRT used by python35.dll stays compatible
to the one used by say python32.dll ? What if the CRT memory
management changes between MSVCRT versions ?

Another aspect to consider:

How will this work in the light of having multiple copies of
Python installed on a Windows machine ?

They implementation section suggests that python3.dll would always
redirect to the python3x.dll for which it was installed, ie. if
I have Python 3.5 installed, but then need to run some app with
Python 3.2, the installed python3.dll would then point back to the
python32.dll.

Now, if I start a Python 3.5 application which uses a limited
API extension, this would try to load python32.dll into the
Python 3.5 process. AFAIK, that's not possible due to the
naming conflicts.

>>> This PEP will be implemented in a branch, allowing users to check
>>> whether their modules conform to the ABI. To simplify this testing, an
>>> additional macro Py_LIMITED_API_WITH_TYPES will expose the existing
>>> type object layout, to let users postpone rewriting all types. When
>>> the this branch is merged into the 3.2 code base, this macro will
>>> be removed.
>> Now I'm confused again: this sounds a lot like you do want all extension
>> writers to only use the limited API.
> 
> I certainly want to support as many modules as reasonable with the PEP.
> Whether or not developers then chose to build version-independent
> binaries is certainly outside the scope of the PEP - it only specifies
> action items for Python, not 

test message

2009-05-26 Thread Joel Goldstick
I'm wondering why I couldn't post here.  I read the email list, not from 
 the newsgroup

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


Re: Ted Dziuba

2009-05-26 Thread John DeRosa
On 2009-05-25 00:22:04 -0700, Lawrence D'Oliveiro 
 said:



:

If you've ever had to build C extensions to Python on Windows, you can
join me in a feeling of satisfaction that someone at Microsoft is going
to have to figure this out. Let's call it retribution for Internet
Explorer 6.


Brilliant. OT, I miss uncov.com. His wit is razor-sharp.

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


multiprocessing / forking memory usage

2009-05-26 Thread Randall Smith
I'm trying to get a grasp on how memory usage is affected when forking 
as the multiprocessing module does.  I've got a program with a parent 
process using wx and other memory intensive modules. It spawns child 
processes (by forking) that should be very lean (no wx required, etc). 
Based on inspection using "ps v" and psutil, the memory usage (rss) is 
much higher than I would expect for the subprocess.


My understanding is that COW is used when forking (on Linux).  So maybe 
"ps v pid" is reflecting that.  If that's the case, is there a way to 
better determine the child's memory usage?  If it's not the case and I'm 
using modules I don't need, how can I reduce the memory usage to what 
the child actually uses instead of including everything the parent is using?


Randall

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


Passing string from python programs to external programs

2009-05-26 Thread lone_eagle
Hi all,

On Linux, I do something like this

$ program_to_execute < input_file
... get some output ...

I have the content of the input_file as a string inside a python
program and would like to pass this string to the external program
from inside the python program and get back the programs output in a
string/file. Can someone tell me how to achieve this. I have been
through the documentation for Popen, but this one beats me.

Cheers,
Chaitanya
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Sumitava Mukherjee
Hi all,
I need to randomly sample from a list where all choices have weights
attached to them. The probability of them being choosen is dependent
on the weights.
If say Sample list of choices are [A,B,C,D,E] and weights of the same
are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
want the likeliness of them being chosen be in the order : D>A>C>E>B

In short I mean if prob of a H is .9 and probability of T be 0.1 then
if I draw 10 samples, 9 should be H and 1 should be T.

I coudn't find a function in the module random that does so.
Please can someone guide me how the above could be implemented [either
through some function which exists and I don't know or pointers to
some code snippets which does so]?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Sumitava Mukherjee
On May 26, 11:39 pm, Sumitava Mukherjee  wrote:
> Hi all,
> I need to randomly sample from a list where all choices have weights
> attached to them. The probability of them being choosen is dependent
> on the weights.
> If say Sample list of choices are [A,B,C,D,E] and weights of the same
> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
> want the likeliness of them being chosen be in the order : D>A>C>E>B
>
> In short I mean if prob of a H is .9 and probability of T be 0.1 then
> if I draw 10 samples, 9 should be H and 1 should be T.
>
> I coudn't find a function in the module random that does so.
> Please can someone guide me how the above could be implemented [either
> through some function which exists and I don't know or pointers to
> some code snippets which does so]?

 [Oh, I forgot to mention. I am looking for sampling without replacement.]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Emile van Sebille

On 5/26/2009 11:39 AM Sumitava Mukherjee said...

Hi all,
I need to randomly sample from a list where all choices have weights
attached to them. The probability of them being choosen is dependent
on the weights.
If say Sample list of choices are [A,B,C,D,E] and weights of the same
are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
want the likeliness of them being chosen be in the order : D>A>C>E>B

In short I mean if prob of a H is .9 and probability of T be 0.1 then
if I draw 10 samples, 9 should be H and 1 should be T.


I don't know if there's a function for this somewhere, but you can 
easily roll your own...


import random

choices = list('ABCDE')
weights = [0.895,0.567,0.765,0.890,0.60]

selections = list("".join([ choice*int(weight*1000) for choice,weight in 
zip(choices,weights) ]))


random.shuffle(selections)

for randomchoice in selections:
   dosomething(randomchoice)

Emile





I coudn't find a function in the module random that does so.
Please can someone guide me how the above could be implemented [either
through some function which exists and I don't know or pointers to
some code snippets which does so]?


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


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Arnaud Delobelle
Sumitava Mukherjee  writes:

> On May 26, 11:39 pm, Sumitava Mukherjee  wrote:
>> Hi all,
>> I need to randomly sample from a list where all choices have weights
>> attached to them. The probability of them being choosen is dependent
>> on the weights.
>> If say Sample list of choices are [A,B,C,D,E] and weights of the same
>> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
>> want the likeliness of them being chosen be in the order : D>A>C>E>B

You mean A > D > C > E > B

>> In short I mean if prob of a H is .9 and probability of T be 0.1 then
>> if I draw 10 samples, 9 should be H and 1 should be T.
>>
>> I coudn't find a function in the module random that does so.
>> Please can someone guide me how the above could be implemented [either
>> through some function which exists and I don't know or pointers to
>> some code snippets which does so]?
>
> [Oh, I forgot to mention. I am looking for sampling without replacement.]

If you do sampling without replacement, you need to know the exact
number of each of A, B, C, D, E in the sample, not just their relative
frequency.

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


Re: [Python-Dev] PEP 384: Defining a Stable ABI

2009-05-26 Thread Martin v. Löwis
 Functions declared in the following header files are not part
 of the ABI:
 - cellobject.h
 - classobject.h
 - code.h
 - frameobject.h
 - funcobject.h
 - genobject.h
 - pyarena.h
 - pydebug.h
 - symtable.h
 - token.h
 - traceback.h
>>> I don't think that's feasable: you basically remove all introspection
>>> functions that way.
>>>
>>> This will need a more fine-grained approach.
>> What specifically is it that you want to do in a module that you
>> couldn't do anymore?
> 
> See my reply to Nick: some of the functions are needed even
> if you don't want to do introspection, such as Py_FatalError()

Ok. I don't know what Py_FatalError is doing in pydebug.h, so I
now propose to move it to pyerrors.h.

> or PyTraceBack_Print().

Ok; I have removed traceback.h from the list. By the other rules
of the PEP, the only function that becomes available then is
PyTraceBack_Print.

> BTW: Given the headline, I take it that the various type checking
> macros in these header will still be available, right ?

Which headers? The one on the list above? No; my idea would
be to completely hide them as-is.

All other type-checking macros will remain available, and
will remain being macros.

>>> Would creating a Python object in a full-API extension and
>>> free'ing it in a limited-API extension cause problems ?
>> No problem that I can see.
> 
> Can we be sure that the MSVCRT used by python35.dll stays compatible
> to the one used by say python32.dll ? What if the CRT memory
> management changes between MSVCRT versions ?

It doesn't matter. For Python "things", the extension module will
use the pymem.h functions, which get routed through pythonxy.dll
to the CRT that Python was build with.

If the extension uses regular malloc(), it should also invoke
regular free() on the pointer. There is no API where Python
calls malloc directly and the extension calls free, or vice
versa.

> How will this work in the light of having multiple copies of
> Python installed on a Windows machine ?

Interesting question. One solution could be to use SxS, which
would allow multiple concurrent installations of python3.dll,
although we would need to make sure it always binds to the
"right" one in each context.

Another solution could be to keep the various copies of python3.dll
in their respective PYTHONHOMEs, and leave it to python.exe or the
app to load the right one; any subsequent extension modules should
then pick up the one that was already loaded.

> They implementation section suggests that python3.dll would always
> redirect to the python3x.dll for which it was installed, ie. if
> I have Python 3.5 installed, but then need to run some app with
> Python 3.2, the installed python3.dll would then point back to the
> python32.dll.

That depends on where they get installed. If they all go into system32,
only the most recent one would be available, which is probably not
desirable.

> Now, if I start a Python 3.5 application which uses a limited
> API extension, this would try to load python32.dll into the
> Python 3.5 process. AFAIK, that's not possible due to the
> naming conflicts.

I don't see this problem. As long as we manage to install multiple
versions of python3.dll on the system somehow, different processes
could certainly load different such DLLs, and the same extension
module would always use the right one.

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Passing string from python programs to external programs

2009-05-26 Thread CTO
On May 26, 2:12 pm, lone_eagle  wrote:
> Hi all,
>
> On Linux, I do something like this
>
> $ program_to_execute < input_file
> ... get some output ...
>
> I have the content of the input_file as a string inside a python
> program and would like to pass this string to the external program
> from inside the python program and get back the programs output in a
> string/file. Can someone tell me how to achieve this. I have been
> through the documentation for Popen, but this one beats me.
>
> Cheers,
> Chaitanya

from subprocess import getstatusoutput

cmd = 'echo '
str = 'Hello World!'
status, output = getstatusoutput(cmd + repr(str))

Obviously, this is 3.x. I believe that in 2.x it was in
the commands module.

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


Re: What text editor is everyone using for Python

2009-05-26 Thread Brian Blais

On May 26, 2009, at 11:17 , Ken Seehart wrote:



Lacrima wrote:

I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?



Jean-Michel Pichavant wrote:

Why buy an IDE when you just need a text editor ? I don't get it.
Anyway gvim (aka vim aka vi) and emacs are the most powerful  
editors for years. Both have Windows and Linux version and most  
important, they both are very effective at editing any file type  
(python, C, latex, love letters...)
Emacs is more accessible to the newby but requires time to master  
its complex features.
Gvim may be complex for the newby due to its command/insertion  
mode constant switch, but is as powerful as emacs can be. Being a  
gvim adept, I should stat that gvim is far better but it would  
only feed the neverending war.


Jean-Michel


I've heard notepad is pretty good.  http://www.notepad.org/




I'm sorry, but ed is the standard editor[1].   :)


bb



[1] http://en.wikipedia.org/wiki/Ed_(text_editor)




--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



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


Re: What text editor is everyone using for Python

2009-05-26 Thread Brian Blais

On May 26, 2009, at 8:07 , Lacrima wrote:


I am new to python.
And now I am using trial version of Wing IDE.
But nobody mentioned it as a favourite editor.
So should I buy it when trial is expired or there are better choices?


I have found that the appreciation of a text editor varies greatly  
across different people.  What one person swears by, feels clunky to  
another.  On the Mac, I like TextMate because I find it pretty clean  
and fast.  On Windows I've used Notepad++, Emacs, Crimson, and  
possibly a few others.  What I recommend is to try out some of the  
editors in:


http://wiki.python.org/moin/PythonEditors

and see how it works for you.  Depending on what you need, and how  
they "feel", you may find what you want with a free editor.




bb

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais



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


Re: Compile python extensions under windows/cygwin

2009-05-26 Thread Joshua Kugler
Joana wrote:

> I mantain Python on Windows, all installed packages are under c:
> \Python25\Lib\site-packages. Now I have to build C libraries used by
> python extensions and I am using cygwin, but I don't know how to
> install the module in Windows directory.

I have used MingW to do this.  Might this help:

http://joshuakugler.com/archives/13-Building-Python-Eggs-with-C-Extensions-on-Windows.html

j

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


Re: Passing string from python programs to external programs

2009-05-26 Thread Jeff McNeil
On May 26, 2:12 pm, lone_eagle  wrote:
> Hi all,
>
> On Linux, I do something like this
>
> $ program_to_execute < input_file
> ... get some output ...
>
> I have the content of the input_file as a string inside a python
> program and would like to pass this string to the external program
> from inside the python program and get back the programs output in a
> string/file. Can someone tell me how to achieve this. I have been
> through the documentation for Popen, but this one beats me.
>
> Cheers,
> Chaitanya

Sounds like subprocess is what you want.  Here's a quick ad-hoc
example. You can find more information on the module at
http://docs.python.org/library/subprocess.html.

[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> sub = subprocess.Popen('/bin/gzip', stdin=subprocess.PIPE, 
>>> stdout=subprocess.PIPE)
>>> g = sub.communicate('Please, sir, gzip me?')
>>> import gzip
>>> import StringIO
>>> gzip.GzipFile(fileobj=StringIO.StringIO(g[0])).read()
'Please, sir, gzip me?'
>>>

Thanks,

Jeff
mcjeff.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Pickle of the instance of new style class fails when execute with exec statement

2009-05-26 Thread Борис Казаков
Hi, I'm trying to execute the following code:

d = {}
exec('import cPickle')  in d
desc = """
class A(object):
pass
"""
exec(desc) in d

exec('a = A()') in d
exec('cPickle.dump(a, open("tmp.txt","wb"))')in d

for some reason it fails with the following traceback:

Traceback (most recent call last):
  File "pickleTest.py", line 10, in 
exec('cPickle.dump(a, open("tmp.txt","wb"))')in d
  File "", line 1, in 
cPickle.PicklingError: Can't pickle : attribute lookup
__builtin__.A failed

pickle also dosen't work, changing protocol dosen't seem to help.

it works when i do:

exec('import __builtin__') in d
exec('__builtin__.A = A') in d

but it pollutes builtin namespace, which I don't like.

May be there is a workaround, how to do this correctly?
Thanks in advance,
Boris.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread George Sakkis
On May 26, 2:39 pm, Sumitava Mukherjee  wrote:
> Hi all,
> I need to randomly sample from a list where all choices have weights
> attached to them. The probability of them being choosen is dependent
> on the weights.
> If say Sample list of choices are [A,B,C,D,E] and weights of the same
> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
> want the likeliness of them being chosen be in the order : D>A>C>E>B
>
> In short I mean if prob of a H is .9 and probability of T be 0.1 then
> if I draw 10 samples, 9 should be H and 1 should be T.
>
> I coudn't find a function in the module random that does so.
> Please can someone guide me how the above could be implemented [either
> through some function which exists and I don't know or pointers to
> some code snippets which does so]?

1. Normalize the weights so that they sum up to 1.
2. Form the cumulative sequence S = [0, w0, w0+w1, w0+w1+w2, ..., sum
(w)==1.0]
3. Call random.random() -> x
4. Find the bucket in S that x belongs to, i.e. the i so that s[i] <=
x < s[i+1] (you can do this fast using the bisect module).
5. Return choice[i]
6. Test.
7. Submit homework ;)

HTH,
George
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Mel
Sumitava Mukherjee wrote:

> Hi all,
> I need to randomly sample from a list where all choices have weights
> attached to them. The probability of them being choosen is dependent
> on the weights.
> If say Sample list of choices are [A,B,C,D,E] and weights of the same
> are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
> want the likeliness of them being chosen be in the order : D>A>C>E>B
> 
> In short I mean if prob of a H is .9 and probability of T be 0.1 then
> if I draw 10 samples, 9 should be H and 1 should be T.
> 
> I coudn't find a function in the module random that does so.
> Please can someone guide me how the above could be implemented [either
> through some function which exists and I don't know or pointers to
> some code snippets which does so]?

 I've usually used something like (untested):

def chooser (objects, weights):
total = 0.0
for obj, weight in zip (objects, weights):
total += weight
if weight < total * random.random():
chosen = obj
return chosen

Works fine if runtime is not a great concern.

Mel.

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


unpickling a stream

2009-05-26 Thread msolem
Hello,

I want to send a stream of pickled objects over a socket.  Is there a
standard way of ensuring that only complete objects are unpickled on
the receiving side.

client pseudo code:
  loop forever:
receive some bytes on the socket
if we have received a complete pickled object:  <== How is this
done?
  unpickle the object

This isn't for a project, just trying to learn some more about the
pickle module.

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


Re: How do I sample randomly based on some probability(wightage)?

2009-05-26 Thread Dave Angel

Sumitava Mukherjee wrote:

Hi all,
I need to randomly sample from a list where all choices have weights
attached to them. The probability of them being choosen is dependent
on the weights.
If say Sample list of choices are [A,B,C,D,E] and weights of the same
are [0.895,0.567,0.765,0.890,0.60] when I draw (say 2) samples then I
want the likeliness of them being chosen be in the order : D>A>C>E>B

In short I mean if prob of a H is .9 and probability of T be 0.1 then
if I draw 10 samples, 9 should be H and 1 should be T.

I coudn't find a function in the module random that does so.
Please can someone guide me how the above could be implemented [either
through some function which exists and I don't know or pointers to
some code snippets which does so]?

  
Emile gave you a pretty elegant solution, assuming two things:  The 
choices can be represented by a single character each, and a roundoff 
error of one part in a thousand is acceptable.


Of course, you can represent 256 choices in a 2.x character, and you 
could switch to Unicode if that's not enough.  And if 1000 isn't enough, 
you could make it bigger, at the cost of needing a bigger table.


Here's my more-straightforward algorithm, not reduced to code.

Start with a list of probabilities.  Replace each item with the sum of 
all the items up to and including itself.  (If you do it in-place, you'd 
need to do it in reverse order, but if you use a copy, you could simply 
replace each item with a sum() of the appropriate slice of the copy.



Anyway, now you've got a list of cumulative probabilites, with the last 
item equalling 1.0 (pretty close).  You might need to fudge that to 
exactly 1.0, just in case.


I think I'd zip that list with the original list of items, so that you 
have a single list of tuples.


Now to generate a random sample, call  random.random() to get a value 
between 0 and 1. Search the list for the first item greater than or 
equal to your value, and return the other half of the tuple.



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


distutils extension configuration problem

2009-05-26 Thread Ron Garret
I'm trying to build PyObjC on an Intel Mac running OS X 10.5.7.  The 
build is breaking because distutils seems to want to build extension 
modules as universal binaries, but some of the libraries it depends on 
are built for intel-only, i.e.:

[...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ python2.6 
setup.py build
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils
/dist.py:266: UserWarning: Unknown distribution option: 'options'
  warnings.warn(msg)
running build
running build_py
running build_ext
building 'ScreenSaver._inlines' extension
gcc -arch ppc -arch i386 -isysroot /Developer/SDKs/MacOSX10.4u.sdk -g 
-bundle -undefined dynamic_lookup 
build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o -o 
build/lib.macosx-10.3-i386-2.6/ScreenSaver/_inlines.so -framework 
ScreenSaver
ld: in /Developer/SDKs/MacOSX10.4u.sdk/usr/local/lib/libTIFF.dylib, file 
is not of required architecture for architecture ppc
collect2: ld returned 1 exit status
lipo: can't open input file: 
/var/folders/nT/nTiypn-v2RatkU+BYncrKU+++TI/-Tmp-//ccMFYRkt.out (No such 
file or directory)
error: command 'gcc' failed with exit status 1

[...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ file 
build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o 
build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o: Mach-O 
universal binary with 2 architectures
build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o (for 
architecture ppc): Mach-O object ppc
build/temp.macosx-10.3-i386-2.6/Modules/_ScreenSaver_inlines.o (for 
architecture i386):   Mach-O object i386

[...@mickey:~/Desktop/pyobjc-framework-ScreenSaver-2.2b2]$ file 
/usr/local/lib/libtiff.dylib 
/usr/local/lib/libtiff.dylib: Mach-O dynamically linked shared library 
i386

How do I get distutils to stop trying to build extensions as universal 
binaries?

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


Re: multiprocessing / forking memory usage

2009-05-26 Thread Piet van Oostrum
> Randall Smith  (RS) wrote:

>RS> I'm trying to get a grasp on how memory usage is affected when forking as
>RS> the multiprocessing module does.  I've got a program with a parent process
>RS> using wx and other memory intensive modules. It spawns child processes (by
>RS> forking) that should be very lean (no wx required, etc). Based on
>RS> inspection using "ps v" and psutil, the memory usage (rss) is much higher
>RS> than I would expect for the subprocess.

The child is a clone of the parent. So both its virtual memory usage and
its resident memory usage will be equal to the parent's ones immediately
after the fork(). But the actual physical memory has only one copy
resident, although ps will show it on both processes (at least I think
that's how ps works). Of course later they will diverge.

>RS> My understanding is that COW is used when forking (on Linux).  

I think this is true of all modern Unix systems.

>RS> So maybe "ps v pid" is reflecting that. If that's the case, is
>RS> there a way to better determine the child's memory usage?
 
Define `memory usage' in the light of the above.

As long as the parent is still around and you don't run out of virtual
memory in the child, not much harm is done.

If the parent stops and you don't run out of virtual memory in the
child, the excessive pages will eventually be paged out, and then no
longer occupy physical memory. As long as you have enough swap space it
shouldn't be a big problem. The extra paging activity is a bit of a
loss, however.

If you run out of virtual memory in the child you have a problem, however.

>RS> If it's not the case and I'm using
>RS> modules I don't need, how can I reduce the memory usage to what the child
>RS> actually uses instead of including everything the parent is using?

The best would be to fork the child before you import the excess modules
in the parent. If that is not possible you could try to delete as much
in the child as you can, for example by
del wx; del sys.modules['wx'] etc, delete all variables that you don't
need, and hope the garbage collector will clean up enough. But it will
make you application quite complicated. From the python level you can't
get rid of loaded shared libraries, however. And trying to do that from
the C level is probably close to committing suicide.

My advise: don't worry until you really experience memory problems.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >