Re: Python + PostgreSQL

2009-03-17 Thread Jeroen Ruigrok van der Werven
-On [20090318 04:01], Lobo (carlosgali...@gmail.com) wrote:
>I am wondering whether I can jump directly to Python 3.x (instead of
>using Python 2.6), depending of course on psycopg2 compatibility?.

Might I suggest sticking to 2.6 for now?

The 2.x series is what is now going around as 'stable' in the Python world.
Almost all third-party modules are written for 2.x (typically 2.3 or 2.4 and
higher). 3.0 is very new and the documentation and modules are not just up
to the standard of the 2.x series.

Using 3.0 right now will get you little added benefit at this point in time.

Also, chalk up my preference for SQLAlchemy.

-- 
Jeroen Ruigrok van der Werven  / asmodai
イェルーン ラウフロック ヴァン デル ウェルヴェン
http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
The thought of a moment is as fleeting as the memory of it intense...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question: How do I add elements to **kwargs in a function?

2009-03-17 Thread Gabriel Genellina
En Tue, 17 Mar 2009 01:24:18 -0200, Aaron Garrett  
 escribió:

On Mar 16, 9:59 pm, Chris Rebert  wrote:

On Mon, Mar 16, 2009 at 7:48 PM, Aaron Garrett
 wrote:
> I have spent quite a bit of time trying to find the answer on this
> group, but I've been unsuccessful. Here is what I'd like to be able to
> do:

> def A(**kwargs):
>    kwargs['eggs'] = 1

> def B(**kwargs):
>    print(kwargs)

> def C(**kwargs):
>    A(**kwargs)
>    B(**kwargs)

> I'd like to be able to make a call like:

> C(spam=0)

> and have it print
> {'spam': 0, 'eggs': 1}

> But it doesn't do that. Instead, it gives
> {'spam': 0}

> I was under the impression that kwargs is passed by reference and,

It's not. Semantically, the dictionary broken up into individual
keyword arguments on the calling side, and then on the callee side, a
fresh dictionary is created from the individual arguments. So while
Python uses call-by-object, extra packing/unpacking takes place in
this case, causing copying, and thus your problem.


Thank you for the explanation.


There is still a way of getting what you want -- you must pass the actual  
dictionary, and not unpack/repack the arguments:


def A(kwargs):# no **
kwargs['eggs'] = 1

def B(**kwargs):
print(kwargs)

def C(**kwargs):
A(kwargs) # no **
B(**kwargs)

C(spam=0)

--
Gabriel Genellina

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


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-17 Thread John Machin
On Mar 18, 4:19 pm, Matt Nordhoff  wrote:
> bdb112 wrote:
> > Thanks for all the replies:
> > I think I see now - % is a binary operator whose precedence rules are
> > shared with the modulo operator regardless of the nature of its
> > arguments, for language consistency.
> > I understand the arguments behind the format method, but hope that the
> > slightly idiosyncratic print( ..% ..) remains, as the vaguely
> > pictorial "printf" format string is clearer for a long line with
> > several arguments.
> > I will use the "implicit string concatenation" to solve my problem but
> > it is a little odd that an "invisible" operator is stronger than a
> > visible one. (+).
>
> The implicit string concatenation is actually done by the compiler; it
> isn't an operator at all. Look:
>
> >>> import dis
> >>> def f():
>
> ...     return "foo" "bar"
> ...>>> dis.dis(f)
>
>   2           0 LOAD_CONST               1 ('foobar')
>               3 RETURN_VALUE
> --

I think you need better evidence than that obtained by proctologising
about with dis.dis:

Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)] onwin32
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> def f():
...return ('foo') + ('bar')
...
>>> dis.dis(f)
  2   0 LOAD_CONST   3 ('foobar')
  3 RETURN_VALUE
>>>

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


Strange crash issue on Windows w/ PyGTK, Cairo...

2009-03-17 Thread CJ Kucera
Hello list!

I'm having a strange issue, and I'm not entirely certain yet where
the actual problem is (ie, Python, PyGTK, or gtk+), but I figure I'll
start here.  Bear with me, this'll probably be a long explanation...

I've been building an app which is meant to be run on both Linux and
Windows.  It uses PyGTK for its GUI, and the main area of the app is
a gtk.DrawingArea which I draw on using PyCairo.  I've been developing
on Linux, and it works great on that platform, with no issues that
I'm aware of.  When running on Windows, though, the app exhibits the
following behavior:

  1) When the .py of the main file which runs the application GUI first
  gets compiled to a .pyc (ie: the first time it's run, or the first
  time after .py modification), the application runs totally fine, with
  no apparent problems.

  2) Any attempt AFTER that, the application will start up, *start* to
  do its data-loading, but then almost immediately crash with an
  enigmatic "python.exe has generated errors and will be closed by
  Windows."  When it does so, there is no output whatsoever to the
  console that the application was launched from, and the crash doesn't
  always happen in exactly the same place.

The pattern remains the same, though - if the .pyc needs to be compiled,
the application works fine, but if not: boom.

I've been steadily stripping the program down to what I hoped would be a
small, reproducible app that I could post here, and I do intend to do so
still, but it's rather slow going.  For now, I was hoping to see if
anyone's ever heard of behavior like this before, and might know what
to do about it, or at least a possible avenue of attack.

As I've been reducing the program down, I've encountered even stranger
(IMO) behavior...  In one instance, changing a function name seemed to
make the program work.  I took out the handler which draws my app's
"About" box, and suddenly my problem went away.  Occasionally I would
remove a function and the app would suddenly *always* fail with that
Windows crash error, and I'd have to put the function back in.  Keep
in mind, these are functions which *aren't being called anywhere.*

Sometimes I could replace a function's entire contents with just "pass"
and the app would suddenly behave properly, or not behave at all.

It's almost as if whatever's doing the byte-compilation is getting
screwed up somehow, and really small changes to parts of the file which
aren't even being touched are having a huge impact on the application as
a whole.  It's seriously vexing, and certainly the oddest problems I've
seen in Python.

Windows versions I can reproduce this on: XP and win2k
Python versions I've reproduced this on:
  Python 2.5.4 with:
PyGTK 2.12.1-2-win32-py2.5
PyGObject 2.14.1-1.win32-py2.5
PyCairo 1.4.12-1.win32-py2.5
  Python 2.6.1 with:
PyGTK 2.12.1-3-win32-py2.6 
PyGObject 2.14.2-2.win32-py2.6
PyCairo 1.4.12-2.win32-py2.6
gtk+ 2.12.9-win32-2 (from http://sf.net/projects/gladewin32 , which is
the version linked to from pygtk.org)

The 2.6 Python stuff I've actually only tried on win2k so far, not XP,
though given my history with this, I suspect that that wouldn't make a
difference.

Since gtk+ is the one bit of software that hasn't been swapped out for
another version, I suppose that perhaps that's where the issue is, but
it seems like Python should be able to at least throw an Exception or
something instead of just having a Windows crash.  And having it work
the FIRST time, when the .pyc's getting compiled, is rather suspicious.

Anyway, I'll continue trying to pare this app down to one manageable
script which I can post here, but until then I'd be happy to hear ideas
from anyone else about this.

Thanks!

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


Re: Python to Perl transalators

2009-03-17 Thread Armin
On Wednesday 18 March 2009 02:22:57 Chris Rebert wrote:
> 2009/3/17  :
> > Could anyone suggest whether there is any Python to Perl code convertors?
> > I found one on the net viz. Perthon. But it wasn’t really helping out.
>
> 
> Why on earth would you want to? That'd be like translating Shakespeare
> into a bad rap song!
> 
>
> Cheers,
> Chris

lol, actually I would prefer a rap song over Shakespeare, so your analogy 
doesn't work there ;)

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


RE: Python to Perl transalators

2009-03-17 Thread Abhinayaraj . Raju

-Original Message-
From: ch...@rebertia.com [mailto:ch...@rebertia.com] On Behalf Of Chris Rebert
Sent: Wednesday, March 18, 2009 10:53 AM
To: Raju, Abhinayaraj
Cc: python-list@python.org
Subject: Re: Python to Perl transalators

2009/3/17  :
> Could anyone suggest whether there is any Python to Perl code convertors?
> I found one on the net viz. Perthon. But it wasn’t really helping out.


Why on earth would you want to? That'd be like translating Shakespeare
into a bad rap song!


Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com

:-)

I am just a beginner learning both the languages. Wondered if I can have some 
comparative understanding of both.

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


Re: download x bytes at a time over network

2009-03-17 Thread Matt Nordhoff
Saurabh wrote:
> Heres the reason behind wanting to get chunks at a time.
> Im actually retrieving data from a list of RSS Feeds and need to
> continuously check for latest posts.
> But I dont want to depend on Last-Modified header or the pubDate tag
> in . Because a lot of feeds just output date('now')  instead
> of the actual last-updated timestamp.
> But when continuously checking for latest posts, I dont want to
> bombard other people's bandwidth - so I just want to get chunks of
> bytes at a time and internally check for ... with my
> database against timestamp values.
> Is there a better way to achieve this ?

For the feeds that *do* set Last-Modified properly, won't you be using
*more* bandwidth by downloading part of the feed instead of just using
If-Modified-Since?
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python to Perl transalators

2009-03-17 Thread Chris Rebert
2009/3/17  :
> Could anyone suggest whether there is any Python to Perl code convertors?
> I found one on the net viz. Perthon. But it wasn’t really helping out.


Why on earth would you want to? That'd be like translating Shakespeare
into a bad rap song!


Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-17 Thread Matt Nordhoff
bdb112 wrote:
> Thanks for all the replies:
> I think I see now - % is a binary operator whose precedence rules are
> shared with the modulo operator regardless of the nature of its
> arguments, for language consistency.
> I understand the arguments behind the format method, but hope that the
> slightly idiosyncratic print( ..% ..) remains, as the vaguely
> pictorial "printf" format string is clearer for a long line with
> several arguments.
> I will use the "implicit string concatenation" to solve my problem but
> it is a little odd that an "invisible" operator is stronger than a
> visible one. (+).

The implicit string concatenation is actually done by the compiler; it
isn't an operator at all. Look:

>>> import dis
>>> def f():
... return "foo" "bar"
...
>>> dis.dis(f)
  2   0 LOAD_CONST   1 ('foobar')
  3 RETURN_VALUE
-- 
--
http://mail.python.org/mailman/listinfo/python-list


Python to Perl transalators

2009-03-17 Thread Abhinayaraj . Raju
Could anyone suggest whether there is any Python to Perl code convertors?
I found one on the net viz. Perthon. But it wasn't really helping out.

Thanks
Agni





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


Re: Mangle function name with decorator?

2009-03-17 Thread Michele Simionato
I forgot; people interested in metaclasses in Python 3.0
will want to read this paper:
http://www.artima.com/weblogs/viewpost.jsp?thread=236234
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mangle function name with decorator?

2009-03-17 Thread Michele Simionato
On Mar 17, 7:45 pm, Aaron Brady  wrote:

> (Perhaps someday, we will be able to write:
> def dec( namespace ):
>   def outer( fun ):
>     if fun.__name__ in namespace:
>       namespace[ dup_var ]= namespace[ fun.__name__ ]
>     return fun
>   return outer
> It allows us to see if there's a prior entry in the current namespace,
> and save it to a different name.)

Not in the future, but right now, with Python 3.0:

class noclashdict(dict):
"""
A dictionary where overriding a name actually generates a new
entry
with suffix '_new':

>>> d = noclashdict()
>>> d['a'] = 1
>>> d['a'] = 2
>>> sorted(d)
['a', 'a_new']
"""
def __setitem__(self, name, value):
setitem = super().__setitem__
if name in self:
setitem(name + "_new", value)
else:
setitem(name, value)

class Meta(type):
"A metaclass replacing the class dictionary with a noclashdict in
its instances"
@classmethod
def __prepare__(cls, name, bases):
return noclashdict()

class C(metaclass=Meta):
def foo(self):
return 1
def foo(self):
return 2

print(sorted(n for n in vars(C) if not n.startswith('__')))
# prints ['foo', 'foo_new']

if __name__ == '__main__':
import doctest; doctest.testmod()

It seems Guido's time machine is ticking again ;)

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


Re: download x bytes at a time over network

2009-03-17 Thread Gabriel Genellina
En Tue, 17 Mar 2009 17:09:35 -0200, R. David Murray  
 escribió:

Jean-Paul Calderone  wrote:
On Tue, 17 Mar 2009 15:17:56 + (UTC), "R. David Murray"  
 wrote:

>Jean-Paul Calderone  wrote:
>> On Tue, 17 Mar 2009 12:15:23 +0530, Saurabh   
wrote:

>> >
>> >It seems to have some header like the one below : b'b495 - binary  
mode

>> >with 46229 bytes ? Or is it something else ?
>>
>> That's just a bug in urllib in Python 3.0.
>
>What makes you say that's a bug?  Did I miss something?  (Which is  
entirely

>possible!)

I saw it in the Python issue tracker. :)  Python 3.0 broke handling of
chunked HTTP responses.  Instead of interpreting the chunk length  
prefixes,

it delivered them as part of the response.


Ah, got you.  Thanks for the info.


Just for completeness, here is the tracker issue:
http://bugs.python.org/issue4631

--
Gabriel Genellina

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


Re: Creating a python extension that works with multiprocessing.Queue

2009-03-17 Thread Gabriel Genellina
En Sun, 15 Mar 2009 01:51:35 -0200, Travis Miller   
escribió:



I am very new to the python C API, and have written a simple type
called SU2 that has 4 members that are all doubles.  Everything seems
to work fine (can import my module and instantiate the new type and
act on it with various methods I have defined), except for when I
attempt to use my new type with multiprocessing.Queue (by the way I am
working with python 2.6).

So when I declare my object instance and call the put() method on the
Queue instance, the object I get out with the get() method of the same
Queue instance is not the same object.   The four members of the
object are all coming through as all zeros.  Below is the snippet of
code.


Are you on Windows? multiprocessing uses pickles to transfer objects  
between processes. See if you can dump and load those kind of objects. If  
not, you may need to implement __getstate__/__setstate__ or __reduce__

See http://docs.python.org/library/pickle.html


--
Gabriel Genellina

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


RE: Python + PostgreSQL

2009-03-17 Thread Jeff Peck
Just thought I'd add that I've been using SQLAlchemy + Postgresql w/
psycopg2 driver with great success for a long time now. This is just a
preference, but I like using SQLAlchemy without the ORM. It has really good
support for basic low level stuff like defining tables, inserts and updates.
The big win for me has been the ease of moving apps between different
databases. I have had to do this several times, and the process is mostly
painless.


Jeff


-Original Message-
From: python-list-bounces+jpeck=fedex@python.org
[mailto:python-list-bounces+jpeck=fedex@python.org] On Behalf Of Philip
Semanchuk
Sent: Tuesday, March 17, 2009 10:24 PM
To: python-list (General)
Subject: Re: Python + PostgreSQL


On Mar 17, 2009, at 10:57 PM, Lobo wrote:

> Many thanks to all for your valuable input.
>
> I've done some research and I believe I will use (at least for now, to
> make it simple) psycopg2 module to connect Python to PostgreSQL.
>
> I am wondering whether I can jump directly to Python 3.x (instead of
> using Python 2.6), depending of course on psycopg2 compatibility?.


You can, but as you observed you'll be running a patched version of  
psycopg2. If this is the only extension module library you think  
you'll need in your project, then running Python 3.x is fine.  
Otherwise you might run into surprises when you find that there's a  
lot fewer extensions available for 3.x than for 2.x. That's changing,  
but it seems to be the state of the Python world today.

Case in point -- you said your project is a Web project, yes? Then  
whatever Web framework you use will need to have been ported to 3.x.  
At this point, I don't know if any of the major ones have been.


I know that as a new user you'd like to start using the latest &  
greatest version of Python so that you don't put your project in a  
position where you know you'll have to upgrade at some point in the  
future, but that's probably your best course of action at the moment.

Good luck
Philip




> I saw in a different post that psycopg2 does work on Python 3.x as
> long as a patch is applied (by Martin v. Löwis):
>
>
http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/56b7f
ca444a5aa5d/4064a307dca37686?rnum=1&q=python3+postgresql&_done=%2Fgroup%2Fco
mp.lang.python%2Fbrowse_frm%2Fthread%2F56b7fca444a5aa5d%2Fc4f74719f6694dce%3
Flnk%3Dgst%26q%3Dpython3%2Bpostgresql%26#doc_29389da8b2b83188
>
> Do you know where can I find this patch, and if it does fully solve
> any incompatibility issues to be able to use Python 3.x without
> problems?.
>
> Or should I just use Python 2.6?.
>
> What would you recommend?.
>
> Many thanks again,
>
>   Carlos
>
> On Mar 17, 12:20 pm, Philip Semanchuk  wrote:
>> On Mar 17, 2009, at 12:46 PM, Lobo wrote:
>>
>>> Hi,
>>
>>> I am new to this newsgroup (and new to Python and PostgreSQL). My
>>> experience (17+ years) has been with Smalltalk (e.g. VAST) and  
>>> Object
>>> databases (e.g. Versant, OmniBase).
>>
>>> I now have a new project to develop web applications using the  
>>> latest/
>>> best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
>>> pgAdmin 1.10?).
>>
>>> I hope to get some hints as of what frameworks/modules to use for  
>>> this
>>> specific combination (Python + PostgreSQL)?, should I use django,
>>> zope, web2py, psycopg module, others?, what are their pros/cons?.
>>
>> Hi Carlos,
>> You'll find a lot of libraries and projects aren't supporting Python
>> 3.x yet. Consider (as others have suggested) working in Python 2.6 to
>> ease the transition to 3.x when you & your libs are ready.
>>
>> I've used Psycopg2 to talk to Postgres from Python and had great
>> success with it.
>>
>> As far as Django versus Zope versus web2py versus Pylons versus
>> TurboGears versus...  Well, there's enough flamewar material in there
>> to power New York for centuries. They've all got their strengths and
>> weaknesses. I know which I prefer but my needs and preferences are my
>> own and only you know yours.
>>
>> One thing I will note is that Zope's database is an object hierarchy
>> which sounds like a familiar tool for you, so that might ease your
>> transition into the Python world.
>>
>> Good luck
>> Philip
>
> --
> http://mail.python.org/mailman/listinfo/python-list

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

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


Re: How to do this in Python?

2009-03-17 Thread Grant Edwards
On 2009-03-18, Grant Edwards  wrote:
> On 2009-03-17, Jim Garrison  wrote:
>> I'm an experienced C/Java/Perl developer learning Python.
>>
>> What's the canonical Python way of implementing this pseudocode?
>>
>>  String buf
>>  File   f
>>  while ((buf=f.read(1)).length() > 0)
>>  {
>>  do something
>>  }
>>
>> In other words, I want to read a potentially large file in 1 byte
>> chunks (or some other suitably large chunk size).  Since the Python 
>> 'file' object implements __next__() only in terms of lines (even,
>> it seems, for files opened in binary mode) I can't see how to use
>> the Python for statement in this context.
>>
>> Am I missing something basic, or is this the canonical way:
>>
>>  with open(filename,"rb") as f:
>>  buf = f.read(1)
>>  while len(buf) > 0
>>  # do something
>>  buf = f.read(1)
>
> with open(filename,"rb") as f:
>   buf = f.read(1)
>   if not f: break
>   # do something

Ow!  Botched that in a couple ways

  with open(filename,"rb") as f:
while True:
  buf = f.read(1)
  if not buf: break
  # do something

-- 
Grant

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


Re: How to do this in Python?

2009-03-17 Thread Grant Edwards
On 2009-03-18, Jim Garrison  wrote:
> Tim Chase wrote:
>>> Am I missing something basic, or is this the canonical way:
>>>
>>>  with open(filename,"rb") as f:
>>>  buf = f.read(1)
>>>  while len(buf) > 0
>>>  # do something
>>>  buf = f.read(1)
>> 
>> That will certainly do.  Since read() should simply return a 0-length
>> string when you're sucking air, you can just use the test "while buf"
>> instead of "while len(buf) > 0".
>> 
>> However, if you use it multiple places, you might consider writing an
>> iterator/generator you can reuse:
>> 
>>   def chunk_file(fp, chunksize=1):
>> s = fp.read(chunksize)
>> while s:
>>   yield s
>>   s = fp.read(chunksize)
>> 
>>   with open(filename1, 'rb') as f:
>> for portion in chunk_file(f):
>>   do_something_with(portion)
>> 
>>   with open(filename2, 'rb') as f:
>> for portion in chunk_file(f, 1024):
>>   do_something_with(portion)
>> 
>> -tkc
>
> Ah.  That's the Pythonesque way I was looking for.

That's not pythonic unless you really do need to use
chumk_file() in a lot of places (IMO, more than 3 or 4).  If it
only going to be used once, then just do the usual thing:

f = open(...)
while True:
   buf = f.read()
   if not buf: break
   # whatever.
f.close()

Or, you can substitute a with if you want.

-- 
Grant

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


Re: How to do this in Python?

2009-03-17 Thread Grant Edwards
On 2009-03-17, Jim Garrison  wrote:
> I'm an experienced C/Java/Perl developer learning Python.
>
> What's the canonical Python way of implementing this pseudocode?
>
>  String buf
>  File   f
>  while ((buf=f.read(1)).length() > 0)
>  {
>  do something
>  }
>
> In other words, I want to read a potentially large file in 1 byte
> chunks (or some other suitably large chunk size).  Since the Python 
> 'file' object implements __next__() only in terms of lines (even,
> it seems, for files opened in binary mode) I can't see how to use
> the Python for statement in this context.
>
> Am I missing something basic, or is this the canonical way:
>
>  with open(filename,"rb") as f:
>  buf = f.read(1)
>  while len(buf) > 0
>  # do something
>  buf = f.read(1)

with open(filename,"rb") as f:
  buf = f.read(1)
  if not f: break
  # do something

-- 
Grant

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


Roundup Issue Tracker release 1.4.8

2009-03-17 Thread Richard Jones
I'm proud to release version 1.4.8 of Roundup.

This release fixes some regressions:

- bug introduced into hyperdb filter (issue 2550505)
- bug introduced into CVS export and view (issue 2550529)
- bugs introduced in the migration to the email package (issue 2550531)

And adds a couple of other fixes:

- handle bogus pagination values (issue 2550530)
- fix TLS handling with some SMTP servers (issues 2484879 and 1912923)


Though some new features made it in also:

- Provide a "no selection" option in web interface selection widgets
- Debug logging now uses the logging module rather than print
- Allow CGI frontend to serve XMLRPC requests.
- Added XMLRPC actions, as well as bridging CGI actions to XMLRPC actions.
- Optimized large file serving via mod_python / sendfile().
- Support resuming downloads for (large) files.

If you're upgrading from an older version of Roundup you *must* follow
the "Software Upgrade" guidelines given in the maintenance documentation.

Roundup requires python 2.3 or later for correct operation.

To give Roundup a try, just download (see below), unpack and run::

roundup-demo

Release info and download page:
 http://cheeseshop.python.org/pypi/roundup
Source and documentation is available at the website:
 http://roundup.sourceforge.net/
Mailing lists - the place to ask questions:
 http://sourceforge.net/mail/?group_id=31577


About Roundup
=

Roundup is a simple-to-use and -install issue-tracking system with
command-line, web and e-mail interfaces. It is based on the winning design
from Ka-Ping Yee in the Software Carpentry "Track" design competition.

Note: Ping is not responsible for this project. The contact for this
project is rich...@users.sourceforge.net.

Roundup manages a number of issues (with flexible properties such as
"description", "priority", and so on) and provides the ability to:

(a) submit new issues,
(b) find and edit existing issues, and
(c) discuss issues with other participants.

The system will facilitate communication among the participants by managing
discussions and notifying interested parties when issues are edited. One of
the major design goals for Roundup that it be simple to get going. Roundup
is therefore usable "out of the box" with any python 2.3+ installation. It
doesn't even need to be "installed" to be operational, though a
disutils-based install script is provided.

It comes with two issue tracker templates (a classic bug/feature tracker and
a minimal skeleton) and four database back-ends (anydbm, sqlite, mysql
and postgresql).


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


Re: Python + PostgreSQL

2009-03-17 Thread Philip Semanchuk


On Mar 17, 2009, at 10:57 PM, Lobo wrote:


Many thanks to all for your valuable input.

I've done some research and I believe I will use (at least for now, to
make it simple) psycopg2 module to connect Python to PostgreSQL.

I am wondering whether I can jump directly to Python 3.x (instead of
using Python 2.6), depending of course on psycopg2 compatibility?.



You can, but as you observed you'll be running a patched version of  
psycopg2. If this is the only extension module library you think  
you'll need in your project, then running Python 3.x is fine.  
Otherwise you might run into surprises when you find that there's a  
lot fewer extensions available for 3.x than for 2.x. That's changing,  
but it seems to be the state of the Python world today.


Case in point -- you said your project is a Web project, yes? Then  
whatever Web framework you use will need to have been ported to 3.x.  
At this point, I don't know if any of the major ones have been.



I know that as a new user you'd like to start using the latest &  
greatest version of Python so that you don't put your project in a  
position where you know you'll have to upgrade at some point in the  
future, but that's probably your best course of action at the moment.


Good luck
Philip





I saw in a different post that psycopg2 does work on Python 3.x as
long as a patch is applied (by Martin v. Löwis):

http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/56b7fca444a5aa5d/4064a307dca37686?rnum=1&q=python3+postgresql&_done=%2Fgroup%2Fcomp.lang.python%2Fbrowse_frm%2Fthread%2F56b7fca444a5aa5d%2Fc4f74719f6694dce%3Flnk%3Dgst%26q%3Dpython3%2Bpostgresql%26#doc_29389da8b2b83188

Do you know where can I find this patch, and if it does fully solve
any incompatibility issues to be able to use Python 3.x without
problems?.

Or should I just use Python 2.6?.

What would you recommend?.

Many thanks again,

  Carlos

On Mar 17, 12:20 pm, Philip Semanchuk  wrote:

On Mar 17, 2009, at 12:46 PM, Lobo wrote:


Hi,



I am new to this newsgroup (and new to Python and PostgreSQL). My
experience (17+ years) has been with Smalltalk (e.g. VAST) and  
Object

databases (e.g. Versant, OmniBase).


I now have a new project to develop web applications using the  
latest/

best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
pgAdmin 1.10?).


I hope to get some hints as of what frameworks/modules to use for  
this

specific combination (Python + PostgreSQL)?, should I use django,
zope, web2py, psycopg module, others?, what are their pros/cons?.


Hi Carlos,
You'll find a lot of libraries and projects aren't supporting Python
3.x yet. Consider (as others have suggested) working in Python 2.6 to
ease the transition to 3.x when you & your libs are ready.

I've used Psycopg2 to talk to Postgres from Python and had great
success with it.

As far as Django versus Zope versus web2py versus Pylons versus
TurboGears versus...  Well, there's enough flamewar material in there
to power New York for centuries. They've all got their strengths and
weaknesses. I know which I prefer but my needs and preferences are my
own and only you know yours.

One thing I will note is that Zope's database is an object hierarchy
which sounds like a familiar tool for you, so that might ease your
transition into the Python world.

Good luck
Philip


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


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


Re: print - bug or feature - concatenated format strings in a print statement

2009-03-17 Thread bdb112
Thanks for all the replies:
I think I see now - % is a binary operator whose precedence rules are
shared with the modulo operator regardless of the nature of its
arguments, for language consistency.
I understand the arguments behind the format method, but hope that the
slightly idiosyncratic print( ..% ..) remains, as the vaguely
pictorial "printf" format string is clearer for a long line with
several arguments.
I will use the "implicit string concatenation" to solve my problem but
it is a little odd that an "invisible" operator is stronger than a
visible one. (+).

On Mar 16, 5:00 pm, bdb112  wrote:
> #   is the difference between
> print(" %d,  %d, buckle my shoe" % (1,2))
> #   and
> print(" %d, " + " %d, buckle my shoe" % (1,2))
> # a bug or a feature?
>
> First output
> ... print(" %d " + " %d, buckle my shoe" % (1,2))
>
> Second output
> TypeError: not all arguments converted during string formatting
>
> Version Info:
> Python 2.5.1 (r251:54863, Oct 30 2007, 13:54:11)
> [GCC 4.1.2 20070925 (Red Hat 4.1.2-33)] on linux2
>
> also
>
> Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit
> (Intel)] on win32

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


Re: Python + PostgreSQL

2009-03-17 Thread Lobo
Many thanks to all for your valuable input.

I've done some research and I believe I will use (at least for now, to
make it simple) psycopg2 module to connect Python to PostgreSQL.

I am wondering whether I can jump directly to Python 3.x (instead of
using Python 2.6), depending of course on psycopg2 compatibility?.

I saw in a different post that psycopg2 does work on Python 3.x as
long as a patch is applied (by Martin v. Löwis):

http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/56b7fca444a5aa5d/4064a307dca37686?rnum=1&q=python3+postgresql&_done=%2Fgroup%2Fcomp.lang.python%2Fbrowse_frm%2Fthread%2F56b7fca444a5aa5d%2Fc4f74719f6694dce%3Flnk%3Dgst%26q%3Dpython3%2Bpostgresql%26#doc_29389da8b2b83188

Do you know where can I find this patch, and if it does fully solve
any incompatibility issues to be able to use Python 3.x without
problems?.

Or should I just use Python 2.6?.

What would you recommend?.

Many thanks again,

   Carlos

On Mar 17, 12:20 pm, Philip Semanchuk  wrote:
> On Mar 17, 2009, at 12:46 PM, Lobo wrote:
>
> > Hi,
>
> > I am new to this newsgroup (and new to Python and PostgreSQL). My
> > experience (17+ years) has been with Smalltalk (e.g. VAST) and Object
> > databases (e.g. Versant, OmniBase).
>
> > I now have a new project to develop web applications using the latest/
> > best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
> > pgAdmin 1.10?).
>
> > I hope to get some hints as of what frameworks/modules to use for this
> > specific combination (Python + PostgreSQL)?, should I use django,
> > zope, web2py, psycopg module, others?, what are their pros/cons?.
>
> Hi Carlos,
> You'll find a lot of libraries and projects aren't supporting Python  
> 3.x yet. Consider (as others have suggested) working in Python 2.6 to  
> ease the transition to 3.x when you & your libs are ready.
>
> I've used Psycopg2 to talk to Postgres from Python and had great  
> success with it.
>
> As far as Django versus Zope versus web2py versus Pylons versus  
> TurboGears versus...  Well, there's enough flamewar material in there  
> to power New York for centuries. They've all got their strengths and  
> weaknesses. I know which I prefer but my needs and preferences are my  
> own and only you know yours.
>
> One thing I will note is that Zope's database is an object hierarchy  
> which sounds like a familiar tool for you, so that might ease your  
> transition into the Python world.
>
> Good luck
> Philip

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


Re: How to do this in Python?

2009-03-17 Thread Terry Reedy

Jim Garrison wrote:


Ah.  That's the Pythonesque way I was looking for.  I knew
it would be a generator/iterator but haven't got the Python
mindset down yet and haven't played with writing my own
generator.  I'm still trying to think in purely object-
oriented terms where I would override __next__() to
return a chunk of the appropriate size.

Give a man some code and you solve his immediate problem.
Show him a pattern and you've empowered him to solve
his own problems.  Thanks!


Python's iterator-fed for-loops are its primary motor for calculation. 
Anytime one thinks of processing successive items with a while-loop, one 
could consider factoring out the production of the successive items with 
an iterator.  While loops are really only needed for repeated processing 
of a single object.


tjr

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


Re: How to do this in Python?

2009-03-17 Thread MRAB

Jim Garrison wrote:
[snip]

Ah.  That's the Pythonesque way I was looking for.

>
FYI, the correct word is "Pythonic". "Pythonesque" refers to Monty
Python.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python?

2009-03-17 Thread Jim Garrison
andrew cooke wrote:
> Jim Garrison wrote:
>> I'm an experienced C/Java/Perl developer learning Python.
>>
>> What's the canonical Python way of implementing this pseudocode?
[snip]

> 
> embarrassed by the other reply i have read, 

There's always some "trollish" behavior in any comp.lang.*
group.  Too many people treat languages as religions instead
of tools.  They all have strengths and weaknesses :-)

> but not doing much binary i/o
> myself, i suggest:
> 
> with open(...) as f:
>   while (True):
> buf = f.read(1)
> if not buf: break
> ...
> 
> but are you sure you don't want:
> 
> with open(...) as f:
>   for line in f:
> ...
> 
> andrew

For a one-off,,your first example would work fine.  See the
other reply from Tim Chase for a much more Pythonesque
pattern.  I don't want "for line in f:" because binary
files don't necessarily have lines and I'm bulk processing
files potentially 100MB and larger.  Reading them one line
at a time would be highly inefficient.

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


Re: How to do this in Python?

2009-03-17 Thread Jim Garrison
Tim Chase wrote:
>> Am I missing something basic, or is this the canonical way:
>>
>>  with open(filename,"rb") as f:
>>  buf = f.read(1)
>>  while len(buf) > 0
>>  # do something
>>  buf = f.read(1)
> 
> That will certainly do.  Since read() should simply return a 0-length
> string when you're sucking air, you can just use the test "while buf"
> instead of "while len(buf) > 0".
> 
> However, if you use it multiple places, you might consider writing an
> iterator/generator you can reuse:
> 
>   def chunk_file(fp, chunksize=1):
> s = fp.read(chunksize)
> while s:
>   yield s
>   s = fp.read(chunksize)
> 
>   with open(filename1, 'rb') as f:
> for portion in chunk_file(f):
>   do_something_with(portion)
> 
>   with open(filename2, 'rb') as f:
> for portion in chunk_file(f, 1024):
>   do_something_with(portion)
> 
> -tkc

Ah.  That's the Pythonesque way I was looking for.  I knew
it would be a generator/iterator but haven't got the Python
mindset down yet and haven't played with writing my own
generator.  I'm still trying to think in purely object-
oriented terms where I would override __next__() to
return a chunk of the appropriate size.

Give a man some code and you solve his immediate problem.
Show him a pattern and you've empowered him to solve
his own problems.  Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


MySQL DB Error

2009-03-17 Thread manny
Hi,

When I buil the MySQLDB  mod I get the following error:

[r...@box MySQL-python-1.2.2]# â_mysql.c:2420: error:
â_mysql_ResultObjectâ has no member named âconverter
-bash: â_mysql.c:2420:: command not found
[r...@box MySQL-python-1.2.2]# â_mysql.c:2420: error: initializer
element is not constant
-bash: â_mysql.c:2420:: command not found
[r...@box MySQL-python-1.2.2]# _mysql.c:2420: error: (near
initialization for â_mysql_ResultObject_memberlist[0].offsetâ
-bash: syntax error near unexpected token `('
[r...@box MySQL-python-1.2.2]# )_mysql.c: In function
â_mysql_ConnectionObject_getattrâ
-bash: syntax error near unexpected token `)'
[r...@box MySQL-python-1.2.2]# :_mysql.c:2442: error:
â_mysql_ConnectionObjectâ has no member named âopen
-bash: :_mysql.c:2442:: command not found
[r...@box MySQL-python-1.2.2]# âerror: command 'gcc' failed with exit
status 1

I am running CentOS and Python 2.5.4

Any well would be appreciated.

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


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Aahz
In article <163b0c86-adf7-434c-9270-c819c5a07...@k29g2000prf.googlegroups.com>,
grocery_stocker   wrote:
>
>[cdal...@localhost ~]$ python
>Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
>[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
>Type "help", "copyright", "credits" or "license" for more information.
 list = [7,8,9]

Don't use a variable named "list" -- you're hiding the list() type.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"Programming language design is not a rational science. Most reasoning
about it is at best rationalization of gut feelings, and at worst plain
wrong."  --GvR, python-ideas, 2009-3-1
--
http://mail.python.org/mailman/listinfo/python-list


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Emile van Sebille

grocery_stocker wrote:

On Mar 17, 3:22 pm, Emile van Sebille  wrote:

grocery_stocker wrote:

It seems like id(list[]) == id().

It might seem that way, but test with other than single-character
strings, eg lists like [7],[8],[9] and try again.

I still get the same thing...


Well, yes -- because you're still testing the same things.  I meant of 
course to test other things, like lists as in:


>>> for ii in [[7],[7],[8],[8],[9],[9]]: id(ii)
...
16607992
16608952
16609032
16608992
16608832
16608872
>>>

What you're testing is the content of the list elements, more like:

>>> for ii in [[7],[7],[8],[8],[9],[9]]: id(ii[0])
...
15424016
15424016
15424004
15424004
15423992
15423992
>>>

And as you've heard, interning is at the root of the apparent sameness..

Emile

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


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread andrew cooke
grocery_stocker wrote:
> It seems like id(list[]) == id(). However, I
> can't find anything in the python documentation that talks about it.
> Did I perhaps overlook something?

most of your examples stated the obvious (that if x is in a list l at
index i then id(list[i]) == id(x) - this is because list[i] is x).  but
that is not what you state above in words (and which is wrong).

the only non-trivial thing in your post is that two different instances of
(an object representing) the same value have the same id, and that is due
to implementation specific caching.

but i am worried you think you have seen something that you have not,
especially given your incorrect text above.

andrew


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


Re: How to do this in Python?

2009-03-17 Thread Armin
On Tuesday 17 March 2009 19:10:20 Josh Holland wrote:
> On Tue, Mar 17, 2009 at 05:04:36PM -0500, Jim Garrison wrote:
> > What's the canonical Python way of implementing this pseudocode?
> >
> > String buf
> > File   f
> > while ((buf=f.read(1)).length() > 0)
> > {
> > do something
> > }
>
> That looks more like C than pseudocode to me...
> Someone's been spending far too much time on C-like languages, if that's
> what your idea of simply readable code looks like. Thank heavens you
> found Python before it was too late!

I should agree, that looks too much like C. (except there are no ; at the end 
of first two lines).  And I'm sure you will much enjoy your adventure as a 
pythonista (pythanista?) just as I have after migration from C++.

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


Re: array next pointer

2009-03-17 Thread Armin
On Tuesday 17 March 2009 19:04:25 Luis Zarrabeitia wrote:
> On Tuesday 17 March 2009 03:17:02 pm R. David Murray wrote:
> > > > (btw, I love the new sentinel argument for the next function in
> > > > python3!)
> > >
> > > next() doesn't have a sentinel argument. It's iter() which does, and
> > > that's in 2.x also.
> >
> > But it does have a 'default' argument, and you can pass that
> > a sentinel, so it amounts to the same thing ;)
>
> Yep, that's what I meant, I forgot the parameter name.

Could you give an example of next() with a sentinel and describe its use case 
please?  I have a little trouble understanding what you guys mean!

thanks,
-- 
Armin Moradi
--
http://mail.python.org/mailman/listinfo/python-list


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Martin v. Löwis
> It seems like id(list[]) == id(). However, I
> can't find anything in the python documentation that talks about it.

It's deliberately undocumented (outside of the source code, that is).

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


Re: Can python quickly display results like bash?

2009-03-17 Thread Chris Jones
On Tue, Mar 17, 2009 at 02:10:36PM EDT, Chris Rebert wrote:
> On Mon, Mar 16, 2009 at 6:05 PM, robert song  
> wrote:
> > Hello, everyone.
> > python can be debugged with pdb, but if there anyway to get a quick
> > view of the python execution.
> > Just like sh -x of bash command.
> > I didn't find that there is an option of python that can do it.
> 
> I've read the manpage for bash and can find no such -x option listed.
> And this term is nigh impossible to google for, so that didn't turn
> anything up.

Google "advanced" search -> "bash -x" - with the quotes! 

The first link displayed "bash debugging tips" has a useful discussion.

> What exactly does the -x option to bash do? Where is it documented?

$ bash -c "help set"

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


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Gary Herron

grocery_stocker wrote:

Given the following

[cdal...@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
  

list = [7,8,9]
id(list)


-1209401076
  

id(list[0])


154303848
  

id(list[1])


154303836
  

id(list[2])


154303824
  

for x in list:


...print id(x),
...
154303848 154303836 154303824
  

id(7)


154303848
  

id(8)


154303836
  

id(9)


154303824


It seems like id(list[]) == id(). However, I
can't find anything in the python documentation that talks about it.
Did I perhaps overlook something?
  


No you didn't overlook anything.  Here's the (full and complete) 
documentation for id.Anything else you may notice about the values 
of id() are implementation details, and must not be depended upon.
You may, however, be able to use what you've noticed to discern some 
things about the inner workings of Python, in particular how it stores 
multiple instances of immutable objects.But be warned Python makes 
some fairly complex decisions here in the interest of efficiency.


*id*(   object)

   Return the ``identity'' of an object. This is an integer (or long
   integer) which is guaranteed to be unique and constant for this
   object during its lifetime. Two objects with non-overlapping
   lifetimes may have the same id() value. (Implementation note: this
   is the address of the object.) 


Gary Herron



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


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


Re: ldap package question

2009-03-17 Thread Michael Ströder
John Gordon wrote:
> I'm using the ldap package to connect to an ldap server and run a query.
> Very simple code, along these lines:
> 
>   con = ldap.initialize(uri)
>   con.simple_bind_s(user, password)
>   results = con.search_s(group, ldap.SCOPE_SUBTREE, filter, attrs)
>   for r in results:
> # inspect the results
> 
> I'm experiencing some intermittent failures

What effects do you see? How do you detect a failure?

> and I think it's because the
> URI I'm connecting to is actually an alias (proxy?  load-balancer?  I'm not
> sure of the right word here) for a pool of servers, one of which is flaky.

There are LDAP proxies with load-balancing and network-level
load-balancers. What is deployed at your site? What are the LDAP server?

> Is there a way to ask the connection object exactly which server I'm
> connected to?

Since load-balancers typically share a virtual IP you cannot really
determine to which server your client is connected. It's up to this
system's configuration. You could ask some monitoring entry in the LDAP
server itself but this might also be flaky depending how your LDAP
session is dispatched to the LDAP servers. You better should talk to the
admin of the load-balancer to make sure session stickyness is guaranteed
within one LDAP session.

Also load-balancers typically works ok for read access. But if your LDAP
client applications writes to a pool of dispatched LDAP servers probably
configured with multi-master replication you should read and understand
the implications in draft-zeilenga-ldup-harmful first.

http://tools.ietf.org/draft/draft-zeilenga-ldup-harmful/

If you're implementing a really heavy application you'd better do
load-balancing / fail-over within your application.

Ciao, Michael.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread grocery_stocker
On Mar 17, 3:22 pm, Emile van Sebille  wrote:
> grocery_stocker wrote:
>
> 
>
>
>
> > It seems like id(list[]) == id().
>
> It might seem that way, but test with other than single-character
> strings, eg lists like [7],[8],[9] and try again.
>

I still get the same thing...

[cdal...@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[7]
>>> print a[0]
7
>>> id(a[0])
165911912
>>> id(7)
165911912
>>> b=[8]
>>> id(b[0])
165911900
>>> id(8)
165911900
>>> c=[9]
>>> id(c[0])
165911888
>>> id(9)
165911888
>>>


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


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Benjamin Kaplan
On Tue, Mar 17, 2009 at 6:14 PM, grocery_stocker  wrote:

> Given the following
>
> [example of cached numbers]
>
>
> It seems like id(list[]) == id(). However, I
> can't find anything in the python documentation that talks about it.
> Did I perhaps overlook something?


You didn't find anything because there's nothing to find. The fact that "7
is 7" is an implementation detail- CPython caches small numbers (I believe
<256) and some strings to boost performance. This isn't necessarily true of
Python in general.

Python 2.6.1 (r261:67515, Dec  9 2008, 14:03:29)
[GCC 4.0.1 (Apple Inc. build 5484)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 7
>>> b = 7
>>> id(a)
8405312
>>> id(b)
8405312
>>> a is b
True


IronPython 1.1 (1.1) on .NET 2.0.50727.1433
Copyright (c) Microsoft Corporation. All rights reserved.
>>> a = 7
>>> b = 7
>>> id(a)
43L
>>> id(b)
44L
>>>a is b
False


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


Re: How to do this in Python?

2009-03-17 Thread Luis Zarrabeitia
On Tuesday 17 March 2009 06:04:36 pm Jim Garrison wrote:
>
> Am I missing something basic, or is this the canonical way:
>
>  with open(filename,"rb") as f:
>  buf = f.read(1)
>  while len(buf) > 0
>  # do something
>  buf = f.read(1)

well, a bit more canonical would be:
   ...
   while buf:
# do something
   ...
instead of comparing len(buf) with 0. But that's a minor detail.

One could use this: 

with open(filename, "rb") as f:
for buf in iter(lambda: f.read(1000),''):
do_something(buff)

but I don't really like a lambda in there. I guess one could use 
functools.partial instead, but it still looks ugly to me. Oh, well, I guess I 
also want to see the canonical way of doing it.

-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to do this in Python?

2009-03-17 Thread Matthew Woodcraft
Jim Garrison  writes:

> buf = f.read(1)
> while len(buf) > 0
> # do something
> buf = f.read(1)

I think it's more usual to use a 'break' rather than duplicate the read.

That is, something more like

while True:
buf = f.read(1)
if len(buf) == 0:
break
# do something

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


Re: How to do this in Python?

2009-03-17 Thread Tim Chase

Am I missing something basic, or is this the canonical way:

 with open(filename,"rb") as f:
 buf = f.read(1)
 while len(buf) > 0
 # do something
 buf = f.read(1)


That will certainly do.  Since read() should simply return a 
0-length string when you're sucking air, you can just use the 
test "while buf" instead of "while len(buf) > 0".


However, if you use it multiple places, you might consider 
writing an iterator/generator you can reuse:


  def chunk_file(fp, chunksize=1):
s = fp.read(chunksize)
while s:
  yield s
  s = fp.read(chunksize)

  with open(filename1, 'rb') as f:
for portion in chunk_file(f):
  do_something_with(portion)

  with open(filename2, 'rb') as f:
for portion in chunk_file(f, 1024):
  do_something_with(portion)

-tkc





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


Re: How to do this in Python?

2009-03-17 Thread andrew cooke
Jim Garrison wrote:
> I'm an experienced C/Java/Perl developer learning Python.
>
> What's the canonical Python way of implementing this pseudocode?
>
>  String buf
>  File   f
>  while ((buf=f.read(1)).length() > 0)
>  {
>  do something
>  }
>
> In other words, I want to read a potentially large file in 1 byte
> chunks (or some other suitably large chunk size).  Since the Python
> 'file' object implements __next__() only in terms of lines (even,
> it seems, for files opened in binary mode) I can't see how to use
> the Python for statement in this context.
>
> Am I missing something basic, or is this the canonical way:
>
>  with open(filename,"rb") as f:
>  buf = f.read(1)
>  while len(buf) > 0
>  # do something
>  buf = f.read(1)

embarrassed by the other reply i have read, but not doing much binary i/o
myself, i suggest:

with open(...) as f:
  while (True):
buf = f.read(1)
if not buf: break
...

but are you sure you don't want:

with open(...) as f:
  for line in f:
...

andrew



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


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Josh Holland
On Tue, Mar 17, 2009 at 03:14:39PM -0700, grocery_stocker wrote:
> It seems like id(list[]) == id().

Only when certain immutable objects are involved. It is the
implementation's option to allow different immutable values to be the
same object (same id()). In CPython, this is used to cache strings that
look like identifiers and small integers. This has been discussed here 
a lot; have a look at the archives.

-- 
Josh Holland 
http://joshh.co.uk
madmartian on irc.freenode.net
--
http://mail.python.org/mailman/listinfo/python-list


Re: Where's the documentation to support the following behavior...

2009-03-17 Thread Emile van Sebille

grocery_stocker wrote:



It seems like id(list[]) == id(). 


It might seem that way, but test with other than single-character 
strings, eg lists like [7],[8],[9] and try again.


Emile


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


Re: How to do this in Python?

2009-03-17 Thread Josh Holland
On Tue, Mar 17, 2009 at 05:04:36PM -0500, Jim Garrison wrote:
> What's the canonical Python way of implementing this pseudocode?
>
> String buf
> File   f
> while ((buf=f.read(1)).length() > 0)
> {
> do something
> }

That looks more like C than pseudocode to me...
Someone's been spending far too much time on C-like languages, if that's
what your idea of simply readable code looks like. Thank heavens you
found Python before it was too late!

-- 
Josh Holland 
http://joshh.co.uk
madmartian on irc.freenode.net
--
http://mail.python.org/mailman/listinfo/python-list


Where's the documentation to support the following behavior...

2009-03-17 Thread grocery_stocker
Given the following

[cdal...@localhost ~]$ python
Python 2.4.3 (#1, Oct  1 2006, 18:00:19)
[GCC 4.1.1 20060928 (Red Hat 4.1.1-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> list = [7,8,9]
>>> id(list)
-1209401076
>>> id(list[0])
154303848
>>> id(list[1])
154303836
>>> id(list[2])
154303824
>>> for x in list:
...print id(x),
...
154303848 154303836 154303824
>>> id(7)
154303848
>>> id(8)
154303836
>>> id(9)
154303824


It seems like id(list[]) == id(). However, I
can't find anything in the python documentation that talks about it.
Did I perhaps overlook something?
--
http://mail.python.org/mailman/listinfo/python-list


How to do this in Python?

2009-03-17 Thread Jim Garrison

I'm an experienced C/Java/Perl developer learning Python.

What's the canonical Python way of implementing this pseudocode?

String buf
File   f
while ((buf=f.read(1)).length() > 0)
{
do something
}

In other words, I want to read a potentially large file in 1 byte
chunks (or some other suitably large chunk size).  Since the Python 
'file' object implements __next__() only in terms of lines (even,

it seems, for files opened in binary mode) I can't see how to use
the Python for statement in this context.

Am I missing something basic, or is this the canonical way:

with open(filename,"rb") as f:
buf = f.read(1)
while len(buf) > 0
# do something
buf = f.read(1)
--
http://mail.python.org/mailman/listinfo/python-list


Re: array next pointer

2009-03-17 Thread Luis Zarrabeitia
On Tuesday 17 March 2009 03:17:02 pm R. David Murray wrote:
> > > (btw, I love the new sentinel argument for the next function in
> > > python3!)
> >
> > next() doesn't have a sentinel argument. It's iter() which does, and
> > that's in 2.x also.
>
> But it does have a 'default' argument, and you can pass that
> a sentinel, so it amounts to the same thing ;)

Yep, that's what I meant, I forgot the parameter name.

-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
--
http://mail.python.org/mailman/listinfo/python-list


Re: packaging

2009-03-17 Thread Terry Reedy

Craig Allen wrote:

we have software we are putting into package form. So far, all the
code was in local py files and we imported between the modules as
you'd think.  Now with the package ("ourpackage") we are addressing
how import affects the importing module.

if "ourpackage" __init__.py itself does regular imports of the key
modules, like "ourmodule" (containing class OurClass)... it's a bit of
a pain for the user.

one imports ourpackage, and then need to get OurClass from ourmodule
still, i.e.:

  import ourpackage
  inst = ourpackage.ourmodule.OurClass()

Instead, I think we want "import package" to preserve the sort of
namespace our loose python files provided, so:

  import ourpackage
  inst = ourpackage.OurClass()

I think the way to do this, and it seems a legit use of a somewhat
dangerous form of import, to in ourpackage's __init__.py do this:

  from ourmodule import *


There is also
from ourmodule import OurClass



So that the second form works.  If anyone has a comment on that I'm
interested, either that it won't work, or to contradict my idea that a
wildcarded import is appropriate in this place as we are trying to
fill a flattened namespace.

But the reason I'm asking is that it's also been suggested that we
should include everything in a single module, so, ourpython1.py and
ourpython2.py, etc, all get moved to ourpython.py.  I very much
dislike that idea for various (probably obvious) reasons.

On the other hand I do want to adopt whatever are de facto standards
for this sort of thing (especially from the user pov but also from the
developer's)... so any comment are appreciated.  I've been using
python for a few years now but this is the first time we are forming
it in the shape of a proper package.


One extreme is to have a flat 'tree' with hundreds of leaves under the 
top node.  The other is to have one leaf per module.  Either can be a 
pain for the user.  If your package provides a few main objects and lots 
of minor objects (less important, emphasized, or used) then it makes 
sense to import the main  objects for direct access and sensibly group 
the others.  (All this regardless of file organization.)


I suggest you looks through the docs of existing packages, especially 
those in some way comparable to yours, and consider the user-view 
organization presented "Is this a view I sold like to use or would I 
prefer otherwise?"


Good luck with whatever you have written.

Terry Jan Reedy

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


Re: How print binary data on screen

2009-03-17 Thread andrew cooke
Mensanator wrote:
> Maybe he's looking for the face of Jesus?

or aphex twin

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


Re: packaging

2009-03-17 Thread andrew cooke
Craig Allen wrote:
[...]
> Instead, I think we want "import package" to preserve the sort of
> namespace our loose python files provided, so:
>
>   import ourpackage
>   inst = ourpackage.OurClass()
>
> I think the way to do this, and it seems a legit use of a somewhat
> dangerous form of import, to in ourpackage's __init__.py do this:
>
>   from ourmodule import *
[...]

you can make that cleaner by using __all__ (so that you only expose what
you want to, and not everything that the "*" pulled in).

that is what i do, and i think it's normal behaviour.  see
http://docs.python.org/3.0/tutorial/modules.html#importing-from-a-package

for example, in my lepl parser -
http://www.acooke.org/lepl/api/lepl-pysrc.html

andrew

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


Re: Is their an expression to create a class?

2009-03-17 Thread Donald 'Paddy' McCarthy

Chris Rebert wrote:

On Tue, Mar 17, 2009 at 2:24 PM, Robert Kern  wrote:

On 2009-03-17 16:13, Paddy wrote:

We the def statement and the lambda expression. We have the class
statement, but is their an expression to create a class?

Or:


def F(): pass
type(F)



# Is to:
F2 = lambda : none
type(F2)



# As
class O(object): pass
type(O)



# is to:
# 

type('O', (object,), {})


Further detail from the docs (http://docs.python.org/library/functions.html):

type(name, bases, dict)

Return a new type object. This is essentially a dynamic form of
the class statement. The name string is the class name and becomes the
__name__ attribute; the bases tuple itemizes the base classes and
becomes the __bases__ attribute; and the dict dictionary is the
namespace containing definitions for class body and becomes the
__dict__ attribute. For example, the following two statements create
identical type objects:

>>> class X(object):
... a = 1
...
>>> X = type('X', (object,), dict(a=1))

New in version 2.2.

Cheers,
Chris



Thanks guys. Youve put my mind at rest!

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


packaging

2009-03-17 Thread Craig Allen
we have software we are putting into package form. So far, all the
code was in local py files and we imported between the modules as
you'd think.  Now with the package ("ourpackage") we are addressing
how import affects the importing module.

if "ourpackage" __init__.py itself does regular imports of the key
modules, like "ourmodule" (containing class OurClass)... it's a bit of
a pain for the user.

one imports ourpackage, and then need to get OurClass from ourmodule
still, i.e.:

  import ourpackage
  inst = ourpackage.ourmodule.OurClass()

Instead, I think we want "import package" to preserve the sort of
namespace our loose python files provided, so:

  import ourpackage
  inst = ourpackage.OurClass()

I think the way to do this, and it seems a legit use of a somewhat
dangerous form of import, to in ourpackage's __init__.py do this:

  from ourmodule import *

So that the second form works.  If anyone has a comment on that I'm
interested, either that it won't work, or to contradict my idea that a
wildcarded import is appropriate in this place as we are trying to
fill a flattened namespace.

But the reason I'm asking is that it's also been suggested that we
should include everything in a single module, so, ourpython1.py and
ourpython2.py, etc, all get moved to ourpython.py.  I very much
dislike that idea for various (probably obvious) reasons.

On the other hand I do want to adopt whatever are de facto standards
for this sort of thing (especially from the user pov but also from the
developer's)... so any comment are appreciated.  I've been using
python for a few years now but this is the first time we are forming
it in the shape of a proper package.

cheers and thanks.
-craig
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is their an expression to create a class?

2009-03-17 Thread Chris Rebert
On Tue, Mar 17, 2009 at 2:24 PM, Robert Kern  wrote:
> On 2009-03-17 16:13, Paddy wrote:
>>
>> We the def statement and the lambda expression. We have the class
>> statement, but is their an expression to create a class?
>>
>> Or:
>>
> def F(): pass
>>
> type(F)
>>
>> 
>
> # Is to:
> F2 = lambda : none
> type(F2)
>>
>> 
>
> # As
> class O(object): pass
>>
> type(O)
>>
>> 
>
> # is to:
> # 
>
> type('O', (object,), {})

Further detail from the docs (http://docs.python.org/library/functions.html):

type(name, bases, dict)

Return a new type object. This is essentially a dynamic form of
the class statement. The name string is the class name and becomes the
__name__ attribute; the bases tuple itemizes the base classes and
becomes the __bases__ attribute; and the dict dictionary is the
namespace containing definitions for class body and becomes the
__dict__ attribute. For example, the following two statements create
identical type objects:

>>> class X(object):
... a = 1
...
>>> X = type('X', (object,), dict(a=1))

New in version 2.2.

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


Re: Is their an expression to create a class?

2009-03-17 Thread Robert Kern

On 2009-03-17 16:13, Paddy wrote:

We the def statement and the lambda expression. We have the class
statement, but is their an expression to create a class?

Or:


def F(): pass



type(F)



# Is to:
F2 = lambda : none
type(F2)



# As
class O(object): pass



type(O)



# is to:
# 


type('O', (object,), {})

--
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


Is their an expression to create a class?

2009-03-17 Thread Paddy
We the def statement and the lambda expression. We have the class
statement, but is their an expression to create a class?

Or:

>>> def F(): pass

>>> type(F)

>>> # Is to:
>>> F2 = lambda : none
>>> type(F2)

>>>
>>> # As
>>> class O(object): pass

>>> type(O)

>>> # is to:
>>> # 

Thanks.

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


ldap package question

2009-03-17 Thread John Gordon
I'm using the ldap package to connect to an ldap server and run a query.
Very simple code, along these lines:

  con = ldap.initialize(uri)
  con.simple_bind_s(user, password)
  results = con.search_s(group, ldap.SCOPE_SUBTREE, filter, attrs)
  for r in results:
# inspect the results

I'm experiencing some intermittent failures and I think it's because the
URI I'm connecting to is actually an alias (proxy?  load-balancer?  I'm not
sure of the right word here) for a pool of servers, one of which is flaky.

Is there a way to ask the connection object exactly which server I'm
connected to?

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Can python quickly display results like bash?

2009-03-17 Thread Chris Rebert
On Tue, Mar 17, 2009 at 12:15 PM, Hrvoje Niksic  wrote:
> Chris Rebert  writes:
>
>> Ah, I should've thought to google for the sh manpage. Locally, man
>> sh just gives me the bash manpage again which doesn't list -x :-(
>
> Are you sure?  On my system the OPTIONS section of bash(1) begins with:
>
>    In addition to the single-character shell options documented in
>    the description of the set builtin command, bash interprets the
>    following options when it is invoked:
> [...]
> set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
> [...]
>    -x   After expanding each simple command, for command, case
>         command, select command, or arithmetic for command, display
>         the expanded value of PS4, followed by the command and its
>         expanded arguments or associated word list.

Ah. This is what I get for using Preview to view manpages. It used
actual unicode hyphens, thus screwing up my search.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can python quickly display results like bash?

2009-03-17 Thread Hrvoje Niksic
Chris Rebert  writes:

> Ah, I should've thought to google for the sh manpage. Locally, man
> sh just gives me the bash manpage again which doesn't list -x :-(

Are you sure?  On my system the OPTIONS section of bash(1) begins with:

In addition to the single-character shell options documented in
the description of the set builtin command, bash interprets the
following options when it is invoked:
[...]
set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
[...]
-x   After expanding each simple command, for command, case
 command, select command, or arithmetic for command, display
 the expanded value of PS4, followed by the command and its
 expanded arguments or associated word list.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How print binary data on screen

2009-03-17 Thread Mensanator
On Mar 17, 1:45 pm, Irmen de Jong  wrote:
> Ehsen Siraj wrote:
> > I am trying to print binary data on screen but I got the following error.
>
> > f = open('/home/ehsen/1.mp3','rb')
> > g = f.read()
> > print g
> [...]
> > UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0:
> > unexpected code byte
>
> > please help me how i fix this thing.
>
> One way of printing it would be:
>
> print g.encode("hex")
>
> but you don't say what you wanted to accomplish.
> "printing" a mp3 file to the screen doesn't make much sense.

Maybe he's looking for the face of Jesus?

import gmpy

f = open('0010.wav','rb')
g = f.read()

line = []
count = 0
for i in g:
  c = gmpy.digits(ord(i),2).zfill(8)
  line.append(c)
  count += 1
  if count==10:
print ''.join(line)
line = []
count = 0

##010100100100100101000110010001100110010011001001010101110101
##0101011001000101011001100110110101110111
##0001000100100010010101100010001001010110
##0001111001000111011101000111
##010011001001011100110111001101110011011100100111000101110001
##01110001011100100111001001110010011100010111000101110001011100110111001101110011
##01110011011101000111010001110100011100110111010001110100011101100111011001110110
##00010010001101000100010101100001
##1001101010111100110011011110100010001000100110001011
##10001100100011001000110110001001100110010001100100101001010010010101
##10010101100101011001011010011000100110001001100110011001100110011001100110011010
##100110111001101010011010100111001001110110001000100010001000
##1001100110011101100111011000100010011101100111011001110010011100
##1001110110001001110110011101100111011001110110011101100111011001110010011011
##1001101110011010100110011001100110010001011010010101100101011001001110010010
##1001000110011000100011101000110010001001110111001010
##11100011000101110111011101010111001101110001011001101110
##0110101101101110011101100100011000110111011001010101110101011100
##0101101101011010010110010101100101011101011101010111010101010101010101010100
##010101000101001001010001010100100101001001010010010100010101000101010101
##01010101010001001101010011010100110001001100010011000100101001001010
##0100101101001011010010100100110010010100101001001010010010110100101001001010
##01001001010010100100101001001010010010100100101101001011010011010100111001001110
##010001010010010100110101010001010101010101100101011101011101101001011011
##010111010101011101100011011001000110010101100111011010010110101101101100
##0110111001100111011100010111001101110011011101010111011100100011
## ...continues for thousands of lines

>
> --irmen

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


Re: array next pointer

2009-03-17 Thread R. David Murray
Benjamin Peterson  wrote:
> Luis Zarrabeitia  uh.cu> schrieb:
> > 
> > Works for python2.4 and 2.5 also.
> > 
> > In python3, this should be used instead:
> > 
> > >>> b = iter(a)
> > >>> c = next(b)
> > 
> > (btw, I love the new sentinel argument for the next function in python3!)
> 
> next() doesn't have a sentinel argument. It's iter() which does, and that's in
> 2.x also.

But it does have a 'default' argument, and you can pass that
a sentinel, so it amounts to the same thing ;)

--
R. David Murray   http://www.bitdance.com

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


Re: Lists aggregation

2009-03-17 Thread Mensanator
On Mar 17, 2:18 am, Peter Otten <__pete...@web.de> wrote:
> Mensanator wrote:
> > On Mar 16, 1:40 pm, Peter Otten <__pete...@web.de> wrote:
> >> mattia wrote:
> >> > I have 2 lists, like:
> >> > l1 = [1,2,3]
> >> > l2 = [4,5]
> >> > now I want to obtain a this new list:
> >> > l = [(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)]
> >> > Then I'll have to transform the values found in the new list.
> >> > Now, some ideas (apart from the double loop to aggregate each element
> >> > of l1 with each element of l2):
> >> > - I wanted to use the zip function, but the new list will not aggregate
> >> > (3,4) and (3,5)
> >> > - Once I've the new list, I'll apply a map function (e.g. the exp of
> >> > the values) to speed up the process
> >> > Some help?
>
> >> Why would you keep the intermediate list?
>
> >> With a list comprehension:
>
> >> >>> a = [1,2,3]
> >> >>> b = [4,5]
> >> >>> [x**y for x in a for y in b]
>
> >> [1, 1, 16, 32, 81, 243]
>
> >> With itertools:
>
> >> >>> from itertools import product, starmap
> >> >>> from operator import pow
> >> >>> list(starmap(pow, product(a, b)))
>
> >> [1, 1, 16, 32, 81, 243]
>
> > That looks nothing like [(1,4),(1,5),(2,4),(2,5),(3,4),(3,5)].
>
> The point of my post was that you don't have to calculate that list of
> tuples explicitly.

Ok, nevermind.

>
> If you read the original post again you'll find that Mattia wanted that list
> only as an intermediate step to something else. He gave "the exp of values"
> as an example. As math.exp() only takes one argument I took this to
> mean "exponentiation", or **/pow() in Python.
>
> Peter- Hide quoted text -
>
> - Show quoted text -

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


Re: download x bytes at a time over network

2009-03-17 Thread R. David Murray
Jean-Paul Calderone  wrote:
> On Tue, 17 Mar 2009 15:17:56 + (UTC), "R. David Murray" 
>  wrote:
> >Jean-Paul Calderone  wrote:
> >> On Tue, 17 Mar 2009 12:15:23 +0530, Saurabh  wrote:
> >> >> This isn't exactly how things work.  The server *sends* you bytes.  It 
> >> >> can
> >> >> send you a lot at once.  To some extent you can control how much it 
> >> >> sends
> >> >> before it waits for you to catch up, but you don't have anywhere near
> >> >> byte-level control (you might have something like 32kb or 64kb level
> >> >> control).
> >> >
> >> >What abt in Python3 ?
> >> >It seems to have some header like the one below : b'b495 - binary mode
> >> >with 46229 bytes ? Or is it something else ?
> >>
> >> That's just a bug in urllib in Python 3.0.
> >
> >What makes you say that's a bug?  Did I miss something?  (Which is entirely
> >possible!)
> 
> I saw it in the Python issue tracker. :)  Python 3.0 broke handling of
> chunked HTTP responses.  Instead of interpreting the chunk length prefixes,
> it delivered them as part of the response.

Ah, got you.  Thanks for the info.

--
R. David Murray   http://www.bitdance.com

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


Re: How print binary data on screen

2009-03-17 Thread Irmen de Jong

Ehsen Siraj wrote:

I am trying to print binary data on screen but I got the following error.

f = open('/home/ehsen/1.mp3','rb')
g = f.read()
print g

[...]
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: 
unexpected code byte


please help me how i fix this thing.


One way of printing it would be:

print g.encode("hex")

but you don't say what you wanted to accomplish.
"printing" a mp3 file to the screen doesn't make much sense.

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


Re: Mangle function name with decorator?

2009-03-17 Thread Aaron Brady
at bottom
On Mar 17, 12:54 pm, "andrew cooke"  wrote:
> ah, ok.  then yes, you can do that with decorators.  you'd need hash
> tables or something similar in a metaclass.  then the decorator would take
> the given function, stick it in the appropriate hash table, and return a
> function that does the dispatch (ie at run time does the lookup from the
> hash table) (you'd only want to set the function once once, so it would
> need to check the function wasn't already defined).  also, the decorator
> would have one name and get/post etc would be an argument.
>
> this is all documented in the docs and peps, although it's very spread
> around.  you might look at the code for the property decorator - it's not
> doing what you want, but it's an interesting non-trivial example that's
> public.
>
> http://docs.python.org/library/functions.html#property
>
> andrew
>
> Adam wrote:
> > Thanks, Andrew.  I'm trying to accomplish something with a
> > metaprogramming flavor, where, for the convenience of the programmer
> > and the clarity of code, I'd like to have a decorator or some other
> > mechanism do twiddling behind the scenes to make a class do something
> > it wouldn't normally do.
>
> > Here's a less-obfuscated exmaple:  I want to build a framework for
> > creating MVC web applications in Python (I know, I know, there's
> > already 2^1bazillion of them) where a controller class can have
> > methods that respond to the same action but for different HTTP verbs:
snip

I didn't study the OP post very well.

It almost sounds like you want two separate classes, one with methods
for one 'state' the other with methods for another state.  You can
assign to 'self.__class__' to transition between them.

Otherwise, if you're using a hash table, the keys could be
( method_name, flag_value ) pairs, so that looking up a method with a
flag is 'meth= self._dispatch[ ( method_name, flag_value ) ]'.
However, without metaclasses or a global variable, there's no way to
remember a prior definition when you redefine a name.

class X:
  @stateA
  def M.
  @stateB
  def M.

When 'stateB( M )' is called, the result is assigned to 'M'.  There's
no way to access previous definitions of M, since there is no way to
reference the namespace that is under construction.

However, if you're willing to tolerate a little redundancy, you can
chain prior definitions of a variable onto later ones.

>>> def dec( var ):
... def inner( fun ):
... fun.prior= var
... return fun
... return inner
...
>>> class X:
... def M( self ): pass
... @dec( M )
... def M( self ): pass
...
>>> X.M.prior

>>> X.M

>>>

So, you still have the earlier definition of M.  You just had to
mention it when you redefined the variable.

(Perhaps someday, we will be able to write:
def dec( namespace ):
  def outer( fun ):
if fun.__name__ in namespace:
  namespace[ dup_var ]= namespace[ fun.__name__ ]
return fun
  return outer
It allows us to see if there's a prior entry in the current namespace,
and save it to a different name.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: download x bytes at a time over network

2009-03-17 Thread Jean-Paul Calderone

On Tue, 17 Mar 2009 15:17:56 + (UTC), "R. David Murray" 
 wrote:

Jean-Paul Calderone  wrote:

On Tue, 17 Mar 2009 12:15:23 +0530, Saurabh  wrote:
>> This isn't exactly how things work.  The server *sends* you bytes.  It can
>> send you a lot at once.  To some extent you can control how much it sends
>> before it waits for you to catch up, but you don't have anywhere near
>> byte-level control (you might have something like 32kb or 64kb level
>> control).
>
>What abt in Python3 ?
>It seems to have some header like the one below : b'b495 - binary mode
>with 46229 bytes ? Or is it something else ?

That's just a bug in urllib in Python 3.0.


What makes you say that's a bug?  Did I miss something?  (Which is entirely
possible!)


I saw it in the Python issue tracker. :)  Python 3.0 broke handling of
chunked HTTP responses.  Instead of interpreting the chunk length prefixes,
it delivered them as part of the response.

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


Re: Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
Arg, my apologies, I posted my replies to the wrong group :(

--
R. David Murray   http://www.bitdance.com

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


How print binary data on screen

2009-03-17 Thread Ehsen Siraj

I am trying to print binary data on screen but I got the following error.

f = open('/home/ehsen/1.mp3','rb')
g = f.read()
print g
Traceback (most recent call last):
 File "", line 1, in 
 File 
"/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/py/shell.py", 
line 1160, in writeOut

   self.write(text)
 File 
"/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/py/shell.py", 
line 950, in write

   self.AddText(text)
 File "/usr/lib/python2.5/site-packages/wx-2.8-gtk2-unicode/wx/stc.py", 
line 1425, in AddText

   return _stc.StyledTextCtrl_AddText(*args, **kwargs)
 File "/usr/lib/python2.5/encodings/utf_8.py", line 16, in decode
   return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xff in position 0: 
unexpected code byte


please help me how i fix this thing.


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


Re: array next pointer

2009-03-17 Thread Benjamin Peterson
Luis Zarrabeitia  uh.cu> schrieb:
> 
> Works for python2.4 and 2.5 also.
> 
> In python3, this should be used instead:
> 
> >>> b = iter(a)
> >>> c = next(b)
> 
> (btw, I love the new sentinel argument for the next function in python3!)

next() doesn't have a sentinel argument. It's iter() which does, and that's in
2.x also.




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


Re: Can python quickly display results like bash?

2009-03-17 Thread Chris Rebert
On Tue, Mar 17, 2009 at 11:13 AM, D'Arcy J.M. Cain  wrote:
> On Tue, 17 Mar 2009 11:10:36 -0700
> Chris Rebert  wrote:
>> I've read the manpage for bash and can find no such -x option listed.
>
> It's an option from sh(1) that bash copies.  Check the man page for sh
> (1) for a description.

Ah, I should've thought to google for the sh manpage. Locally, man sh
just gives me the bash manpage again which doesn't list -x :-(

In answer to the OP's question, you can use the `trace` module
(http://docs.python.org/library/trace.html):

python -m trace -t somefile.py

will display the lines of source as they are executed.

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python + PostgreSQL

2009-03-17 Thread Philip Semanchuk


On Mar 17, 2009, at 12:46 PM, Lobo wrote:


Hi,

I am new to this newsgroup (and new to Python and PostgreSQL). My
experience (17+ years) has been with Smalltalk (e.g. VAST) and Object
databases (e.g. Versant, OmniBase).

I now have a new project to develop web applications using the latest/
best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
pgAdmin 1.10?).

I hope to get some hints as of what frameworks/modules to use for this
specific combination (Python + PostgreSQL)?, should I use django,
zope, web2py, psycopg module, others?, what are their pros/cons?.


Hi Carlos,
You'll find a lot of libraries and projects aren't supporting Python  
3.x yet. Consider (as others have suggested) working in Python 2.6 to  
ease the transition to 3.x when you & your libs are ready.


I've used Psycopg2 to talk to Postgres from Python and had great  
success with it.


As far as Django versus Zope versus web2py versus Pylons versus  
TurboGears versus...  Well, there's enough flamewar material in there  
to power New York for centuries. They've all got their strengths and  
weaknesses. I know which I prefer but my needs and preferences are my  
own and only you know yours.


One thing I will note is that Zope's database is an object hierarchy  
which sounds like a familiar tool for you, so that might ease your  
transition into the Python world.



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


Re: urllib2 (py2.6) vs urllib.request (py3)

2009-03-17 Thread mattia
Il Tue, 17 Mar 2009 15:40:02 +, R. David Murray ha scritto:

> mattia  wrote:
>> Il Tue, 17 Mar 2009 10:55:21 +, R. David Murray ha scritto:
>> 
>> > mattia  wrote:
>> >> Hi all, can you tell me why the module urllib.request (py3) add
>> >> extra characters (b'fef\r\n and \r\n0\r\n\r\n') in a simple example
>> >> like the following and urllib2 (py2.6) correctly not?
>> >> 
>> >> py2.6
>> >> >>> import urllib2
>> >> >>> f = urllib2.urlopen("http://www.google.com";).read() fd =
>> >> >>> open("google26.html", "w")
>> >> >>> fd.write(f)
>> >> >>> fd.close()
>> >> 
>> >> py3
>> >> >>> import urllib.request
>> >> >>> f = urllib.request.urlopen("http://www.google.com";).read() with
>> >> >>> open("google30.html", "w") as fd:
>> >> ... print(f, file=fd)
>> >> ...
>> >> >>>
>> >> >>>
>> >> Opening the two html pages with ff I've got different results (the
>> >> extra characters mentioned earlier), why?
>> > 
>> > The problem isn't a difference between urllib2 and urllib.request, it
>> > is between fd.write and print.  This produces the same result as your
>> > first example:
>> > 
>> > 
>>  import urllib.request
>>  f = urllib.request.urlopen("http://www.google.com";).read() with
>>  open("temp3.html", "wb") as fd:
>> > ... fd.write(f)
>> > 
>> > 
>> > The "b''" is the stringified representation of a bytes object,
>> > which is what urllib.request returns in python3.  Note the 'wb',
>> > which is a critical difference from the python2.6 case.  If you omit
>> > the 'b' in python3, it will complain that you can't write bytes to
>> > the file object.
>> > 
>> > The thing to keep in mind is that print converts its argument to
>> > string before writing it anywhere (that's the point of using it), and
>> > that bytes (or buffer) and string are very different types in
>> > python3.
>> 
>> Well... now in the saved file I've got extra characters "fef" at the
>> begin and "0" at the end...
> 
> The 'fef' is reminiscent of a BOM.  I don't see any such thing in the
> data file produced by my code snippet above.  Did you try running that,
> or did you modify your code?  If the latter, maybe if you post your
> exact code I can try to run it and see if I can figure out what is going
> on.
> 
> I'm far from an expert in unicode issues, by the way :)  Oh, and I'm
> running 3.1a1+ from svn, by the way, so it is also possible there's been
> a bug fix of some sort.

The extra code were produced using python version 3.0. This afternoon 
I've downloaded the 3.0.1 version and everything works fine for the 
previous example using the "wb" params. And now knowing that urlopen 
returns bytes I've also figured out how to decode the result (I deal with 
just html pages, no jpg, pdf, etc.) so I just have to know the charset of 
the page (if available).
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can python quickly display results like bash?

2009-03-17 Thread D'Arcy J.M. Cain
On Tue, 17 Mar 2009 11:10:36 -0700
Chris Rebert  wrote:
> I've read the manpage for bash and can find no such -x option listed.

It's an option from sh(1) that bash copies.  Check the man page for sh
(1) for a description.

-- 
D'Arcy J.M. Cain  |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can python quickly display results like bash?

2009-03-17 Thread Chris Rebert
On Mon, Mar 16, 2009 at 6:05 PM, robert song  wrote:
> Hello, everyone.
> python can be debugged with pdb, but if there anyway to get a quick
> view of the python execution.
> Just like sh -x of bash command.
> I didn't find that there is an option of python that can do it.

I've read the manpage for bash and can find no such -x option listed.
And this term is nigh impossible to google for, so that didn't turn
anything up.
What exactly does the -x option to bash do? Where is it documented?

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3D CAD -- need collaborators, or just brave souls :)

2009-03-17 Thread Josh Dukes
The real problem jelle is the license of OpenCASCADE. My understanding
is that it's not recognized as "free" by debian because of it's
description.

The phrase "You are also obliged to send your modifications of the
original source code (if you have made any) to the Initial Developer
(i.e. Open CASCADE S.A.S.)." describes a nonfree license, even though
the actual license, according to debian legal, is actually free and
does not specify this requirement.

If you are wrapping OpenCASCADE code, then you are also bound by this
license. Since this license, if it were as described (which it doesn't
appear to be), would not be compatable with GPL, it wouldn't be possible
to write something in PythonOCC and GPL it. If this phrase could be
removed then OCC could be included in debian free and this would
actually solve a lot of other problems for free cad software. 

Debian's legal department still wanted to avoid classifying this as
free because of the qualifying description from upstream. It seems
that it's debian's general policy to avoid legal conflicts with
upstream developers, even if those developers don't appear to have any
legal standing.

On Thu, 5 Mar 2009 08:15:44 + (UTC)
jelle feringa  wrote:

> 
> Hi Josh,
> 
> > http://www.pythonocc.org/ However, I'm
> > not entirely clear on the license for this so that might be an
> > issue.
>  
> We're using a French license for the moment, but will move to
> something more standard soon. PythonOCC ( the current SVN version )
> wraps 85% of the OpenCASCADE kernel. Consider that complete, since
> there are a bunch of modules are obsolete ( WOK, drawing ).
> 
> (Binaries are supplied for win32, linux & osx.)
> 
> We're starting to work on a high level API, so this is a wonderful
> moment to jump on.
> 
> 
> -jelle
> 
> --
> http://mail.python.org/mailman/listinfo/python-list


-- 

Josh Dukes
MicroVu IT Department
--
http://mail.python.org/mailman/listinfo/python-list


Library for generating indicators and graphs for weather stations

2009-03-17 Thread Joel Koltner
Hello,

Could someone suggest a Python library for generating the indicators and 
graphs that "weather station software" typically produces, e.g., similar to 
those seen here: http://www.weather-display.com/wdfull.html ... and here: 
http://www.weather-display.com/index.php ?  I did stumble across the IaGraph 
package (http://www.johnny-lin.com/py_pkgs/IaGraph/Doc/index.html) -- which 
was even developed by a guy doing climate analysis -- but that package was 
designed for graphing results and hence doesn't solve the "current [wind 
speed/direction/etc.] indicator" problem.

My goal here is to have my weather station upload its raw data to a commercial 
web server and then have a CGI Python script generate the actual web page to 
display that data.  (Actually, my short-term goal is to just write a few 
Python scripts that feed the data to Weather Underground as this is much 
easier to do and WU alround makes nice graphs, but long term generating my own 
custom web pages would be fun...)  But in any case, the idea is that I could 
feed some lists of data to a graphing widget or somesuch and be able to spit 
out PNGs or GIFs of the results for ready reference from an HTML file.

Thank you,
---Joel


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


Re: Mangle function name with decorator?

2009-03-17 Thread andrew cooke

ah, ok.  then yes, you can do that with decorators.  you'd need hash
tables or something similar in a metaclass.  then the decorator would take
the given function, stick it in the appropriate hash table, and return a
function that does the dispatch (ie at run time does the lookup from the
hash table) (you'd only want to set the function once once, so it would
need to check the function wasn't already defined).  also, the decorator
would have one name and get/post etc would be an argument.

this is all documented in the docs and peps, although it's very spread
around.  you might look at the code for the property decorator - it's not
doing what you want, but it's an interesting non-trivial example that's
public.

http://docs.python.org/library/functions.html#property

andrew


Adam wrote:
> Thanks, Andrew.  I'm trying to accomplish something with a
> metaprogramming flavor, where, for the convenience of the programmer
> and the clarity of code, I'd like to have a decorator or some other
> mechanism do twiddling behind the scenes to make a class do something
> it wouldn't normally do.
>
> Here's a less-obfuscated exmaple:  I want to build a framework for
> creating MVC web applications in Python (I know, I know, there's
> already 2^1bazillion of them) where a controller class can have
> methods that respond to the same action but for different HTTP verbs:
>
> class foo_controller(Controller):
> @GET
> def new(self):
> # Display the form to create a new foo
>
> @POST
> def new(self):
> # Receive a form post with new foo data in it
>
> The Controller class will do all of the work behind the scenes to
> makes sure that the correct method is called at run-time, but for the
> sake of the programmer, I'd like to supply a clear, friendly syntax
> like this.  Without a little metaprogramming magic, last-in-wins, and
> only the second version of foo will end up in the class, so I'd like
> to mangle the names to something that will result in a unique
> attribute name and that the Controller class can work with behind the
> scenes to do the right thing at run time.
>
> So, Python experts, am I completely barking up the wrong tree here?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>


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


Re: Mangle function name with decorator?

2009-03-17 Thread Aaron Brady
On Mar 17, 12:20 pm, Adam  wrote:
> Thanks, Andrew.  I'm trying to accomplish something with a
> metaprogramming flavor, where, for the convenience of the programmer
> and the clarity of code, I'd like to have a decorator or some other
> mechanism do twiddling behind the scenes to make a class do something
> it wouldn't normally do.
>
> Here's a less-obfuscated exmaple:  I want to build a framework for
> creating MVC web applications in Python (I know, I know, there's
> already 2^1bazillion of them) where a controller class can have
> methods that respond to the same action but for different HTTP verbs:
>
> class foo_controller(Controller):
>     @GET
>     def new(self):
>         # Display the form to create a new foo
>
>     @POST
>     def new(self):
>         # Receive a form post with new foo data in it
>
> The Controller class will do all of the work behind the scenes to
> makes sure that the correct method is called at run-time, but for the
> sake of the programmer, I'd like to supply a clear, friendly syntax
> like this.  Without a little metaprogramming magic, last-in-wins, and
> only the second version of foo will end up in the class, so I'd like
> to mangle the names to something that will result in a unique
> attribute name and that the Controller class can work with behind the
> scenes to do the right thing at run time.
>
> So, Python experts, am I completely barking up the wrong tree here?

Unfortunately, your target is out of range for decorators.  Decorators
can only do:

a= foo( a )

You want something like:

a_jam= foo( a )

However, metaclasses, possibly -combined- with decorators, may fit the
bill.  When the class is being constructed, and is just a string, a
tuple of bases and a dictionary, you can intercept the call to 'type',
and modify the dictionary.

You would need a unique attribute to look for on values in the
dictionary, which means you'd need to detect what functions you are
renaming; possibly by using a decorator to mark them.  (Untested:)

class X( metaclass= M ):
  @mark( 'A' )
  def foo( ... ).

class M( type ):
  def __new__( cls, name, bases, namespace ):
for k, v in namespace.items():
  if v.tag== 'mark-A':
namespace[ k ]= modification( v )
or in your case:
del namespace[ k ]
namespace[ k_mod ]= v.original

possibly also setting func_name as well.
return type( name, bases, namespace )
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python + PostgreSQL

2009-03-17 Thread Marco Mariani

Lobo wrote:


I now have a new project to develop web applications using the latest/
best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
pgAdmin 1.10?).


You want to use Python 2.5.x (or 2.6 if your framework of choice already 
supports it), Postgres 8.3 and have a look at SQLAlchemy (please do).


As for the framework of choice, "it depends" :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python + PostgreSQL

2009-03-17 Thread Krishnakant
hello,

On Tue, 2009-03-17 at 09:46 -0700, Lobo wrote:
> Hi,
> 
> I am new to this newsgroup (and new to Python and PostgreSQL). My
> experience (17+ years) has been with Smalltalk (e.g. VAST) and Object
> databases (e.g. Versant, OmniBase).
> 
Welcome to the world of monty pythons,
/\/\/\:

> I now have a new project to develop web applications using the latest/
> best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
> pgAdmin 1.10?).
> 
It is still a better option to go with python 2.x latest releases IMHO.

> I hope to get some hints as of what frameworks/modules to use for this
> specific combination (Python + PostgreSQL)?, should I use django,
> zope, web2py, psycopg module, others?, what are their pros/cons?.
> 
With regards the web apps, zope is the best server I ever saw in my 10
years of II.T curier.

Python-psycopg2 is the most commonly used DBAPI implementations for
postgresql.

happy hacking.
Krishnakant.


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


Re: Mangle function name with decorator?

2009-03-17 Thread Adam
Thanks, Andrew.  I'm trying to accomplish something with a
metaprogramming flavor, where, for the convenience of the programmer
and the clarity of code, I'd like to have a decorator or some other
mechanism do twiddling behind the scenes to make a class do something
it wouldn't normally do.

Here's a less-obfuscated exmaple:  I want to build a framework for
creating MVC web applications in Python (I know, I know, there's
already 2^1bazillion of them) where a controller class can have
methods that respond to the same action but for different HTTP verbs:

class foo_controller(Controller):
@GET
def new(self):
# Display the form to create a new foo

@POST
def new(self):
# Receive a form post with new foo data in it

The Controller class will do all of the work behind the scenes to
makes sure that the correct method is called at run-time, but for the
sake of the programmer, I'd like to supply a clear, friendly syntax
like this.  Without a little metaprogramming magic, last-in-wins, and
only the second version of foo will end up in the class, so I'd like
to mangle the names to something that will result in a unique
attribute name and that the Controller class can work with behind the
scenes to do the right thing at run time.

So, Python experts, am I completely barking up the wrong tree here?
--
http://mail.python.org/mailman/listinfo/python-list


Re: ValueError: filedescriptor out of range in select()

2009-03-17 Thread Jean-Paul Calderone

On Tue, 17 Mar 2009 18:03:59 +0100, Laszlo Nagy  wrote:


For whatever reason, you're ending up with a lot of open files and/or 
sockets

(and/or any other resource based on file descriptors).  That results in new
file descriptors having large values (>=1024).

You cannot use select() with such file descriptors.  Try poll() instead,
or Twisted. ;)


Poll is not good, because it does not wait.


This is not true.  You can accomplish just what you're doing with select()
using poll() instead.  Oops, but I forgot, Windows doesn't have poll(), so
that doesn't really help you.  There's WaitForSingleObject if you can depend
on pywin32, though.  It supports timeouts, just as select() does (in fact,
select() is a wrapper around WaitForMultipleObjects on Windows - the multi-
handle version of WaitForSingleObject).


[snip]

You are talking about twisted - can someone tell me how twisted does this?


Twisted does it by taking the call to low-level I/O routines out of your
hands and just doing them right on its own. ;)  It also doesn't use blocking
I/O calls, and it doesn't make any I/O calls unless it thinks they're going
to succeed immediately anyway.

It deals with the problem of allowing timely exits on Windows by waiting
up select frequently, which is just a hack to work around the bug I
mentioned in CPython on Windows.


[snip]

I would really like to know what other options I have to implement this 
efficiently. Is my approach bad?


I prefer the approach Twisted (and other libraries too) take.  Don't use
threads to multiplex I/O.  Use non-blocking I/O calls and select() or the
best platform-specific equivalent.



Or am I using the wrong language? I started to use threads heavily in the 
past months. Haskell might be a better choice for me, except that Python is 
much more elegant. :-)


I'm sure people will tell you that this is a good use of threads and that
Python is up to the task.  I think it's a bad use of threads, but with a
different approach, I still think Python is up to the task.

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


Re: SWIG, c++ to Python: array of pointers (double pointer) not working

2009-03-17 Thread bobicanprogram
On Mar 14, 5:22 am, Matteo  wrote:
> Re-posting in more simple and precise terms from a previous 
> threadhttp://groups.google.it/group/comp.lang.python/browse_thread/thread/6...
>
> Problem:
> SWIG doesn't properly wrap c++ arrays of pointers, therefore when you
> try to call a c++ function which requires them, a TypeError exception
> is raised.
>
> Similar story here:http://osdir.com/ml/programming.swig/2003-02/msg00064.html
>
> Already tried:
> - some ctypes functions
> - tuple or string instead of list
>
> Possibile solutions:
> something 
> likehttp://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapF...
> that didn't work either, but I think I was not able to adapt the code
> to my case, since the example is poorly explained.
>
> Code to reproduce error:
> I made a dptest.cpp function that calculates the sum of an array of
> pointers to ints.
>
> #include "dptest.h"
> //the header file is just
> //int sum(int**, int);
>
> int sum(int** dp, int len){
> int sum = 0;
> for (int i = 0; i < len; i++){
> sum += *(dp[i]);
> }
> return sum;
>
> }
>
> swig -c++ -python, then setup.py build_ext --inplace gets it nicely
> compiled and wrapped for python use. It also is imported without
> problems, but then...
>
> mat...@matteo:~/lab/sandbox$ python
> Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49)
> [GCC 4.3.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> 
> import dptest as dp
> >>> l = [1, 2, 3, 4]
> >>> size = len(l)
> >>> dp.sum(l,size)
>
> Traceback (most recent call last):
>   File "", line 1, in 
> TypeError: in method 'sum', argument 1 of type 'int **'
>
> NOTE: A pure c++ program works as expected:
>
> #include 
>
> int sum(int**, int);
>
> int main(){
> int **array_of_ptr = new int*[4];
> for (int i = 0; i < 4; i++){
> array_of_ptr[i] = new int;
> *array_of_ptr[i] = i+1; //fill it with 1,2,3,4: 1+2+3+4 = 10
> }
> std::cout << sum(array_of_ptr, 4) << std::endl;
>
> }
>
> int sum(int** dp, int len){
> int sum = 0;
> for (int i = 0; i < len; i++){
> sum += *(dp[i]);
> }
> return sum;
>
> }
>
> compiling and running prints the correct result:
> mat...@matteo:~/lab/sandbox$ ./purecpp
> 10


Depending on what you are wanting to accomplish with your SWIG library
you may be able to more easily glue your Python code to the C++ piece
using SIMPL (http://www.icanprogram.com/simpl).SIMPL is an ultra
lightweight toolkit useful for gluing modules written in different
languages (C, C++, Python, Tcl/Tk, JAVA) using Send/Receive/Reply
messaging first pioneered by QNX.

If you think SIMPL might help,  don't hesitate to contact me offlist
at this email address.

bob
SIMPL project coordinator
--
http://mail.python.org/mailman/listinfo/python-list


Re: ValueError: filedescriptor out of range in select()

2009-03-17 Thread Laszlo Nagy


For whatever reason, you're ending up with a lot of open files and/or 
sockets
(and/or any other resource based on file descriptors).  That results 
in new

file descriptors having large values (>=1024).

You cannot use select() with such file descriptors.  Try poll() instead,
or Twisted. ;)
Poll is not good, because it does not wait. I cannot call poll() in a 
loop and have 100% CPU used for nothing. Well of course I could call 
time.sleep() if there is nothing to receive/send. But then it would 
increase response times dramatically. The only thing I can use is 
select.select(). Or do you have another idea?


You are talking about twisted - can someone tell me how twisted does this?



However, the use of select() you demonstrated is an unusual one, and not
very good.  It looks like the only purpose is to work around a bug in
CPython on Windows where a process in a blocking read cannot be 
interrupted

by C-c in the console?
Not just Ctrl+C. All of my threads are monitoring this event, and will 
terminate within some seconds, if stop_requested.isSet(). This is a 
multi-threaded program, and stop_requested.set() can be called from 
several places:


- when a thread catches an exception that indicates an internal error in 
the service

- when the program is killed, a TERM signal handler sets this event
- when Ctrl+C is pressed, in the main thread of the program
- when the program is requested to stop itself, over the network
- etc.

So for example, when I kill my program using the 'kill' command, it 
cleans up everything before exiting.


I would really like to know what other options I have to implement this 
efficiently. Is my approach bad?


Or am I using the wrong language? I started to use threads heavily in 
the past months. Haskell might be a better choice for me, except that 
Python is much more elegant. :-)


Thanks,

  Laszlo

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


Re: Mangle function name with decorator?

2009-03-17 Thread andrew cooke
Adam wrote:
> class A(object):
> def __init__(self, method, usebar = False):
> self.method = method
> self.usebar = usebar
>
> def __call__(self):
> if self.usebar == True:
> mangled_name = "_bar_" + self.method
> if hasattr(self, mangled_name):
> return getattr(self, mangled_name)()
> else:
> return getattr(self, self.method)()
> else:
> if hasattr(self, self.method):
> return getattr(self, self.method)()
> else:
> raise NotImplementedError
>
> @bar
> def foo(self):
> print "in _bar_foo"
>
> def foo(self):
> print "in foo"


this isn't exactly what you ask for, but it gives a similar result.  it's
kind of obvious, but it's possible you didn't know methods were first
class entities.

class Foo:

  def __init__(self, useBar=False):
if useBar:
  self.foo = self.__bar
else:
  self.foo = self.__foo

  def __foo(self):
print('foo')

  def __bar(self):
print('bar')

andrew

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


Python + PostgreSQL

2009-03-17 Thread Lobo
Hi,

I am new to this newsgroup (and new to Python and PostgreSQL). My
experience (17+ years) has been with Smalltalk (e.g. VAST) and Object
databases (e.g. Versant, OmniBase).

I now have a new project to develop web applications using the latest/
best possible versions of Python (3.x?) with PostgreSQL (8.x?, with
pgAdmin 1.10?).

I hope to get some hints as of what frameworks/modules to use for this
specific combination (Python + PostgreSQL)?, should I use django,
zope, web2py, psycopg module, others?, what are their pros/cons?.

Your help is greatly appreciated - thanks !

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


Re: ValueError: filedescriptor out of range in select()

2009-03-17 Thread Laszlo Nagy





Here's an interesting post:

http://mail.python.org/pipermail/python-list/2005-April/317442.html


Thank you. I'll try socket.close() instead of socket.shutdown(). Or 
both. :-)

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


Mangle function name with decorator?

2009-03-17 Thread Adam
I am using Python 2.5, and I would like to write a decorator (or using
some other elegant, declarative approach) to mangle the name of
function in a class.  I would like to be able to have two methods
declared with the same name, but one of them will have a decorator (or
whatever) that will change the name of one of them.

Example:

class A(object):
def __init__(self, method, usebar = False):
self.method = method
self.usebar = usebar

def __call__(self):
if self.usebar == True:
mangled_name = "_bar_" + self.method
if hasattr(self, mangled_name):
return getattr(self, mangled_name)()
else:
return getattr(self, self.method)()
else:
if hasattr(self, self.method):
return getattr(self, self.method)()
else:
raise NotImplementedError

@bar
def foo(self):
print "in _bar_foo"

def foo(self):
print "in foo"

Desired output:
>>>y = A("foo", True)
>>>y()
in _bar_foo
>>>z = A("foo")
>>>z()
in foo


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


Re: Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
Jacob Holm  wrote:
> I believe that as soon as the left-hand side stops being a simple 
> variable and it is used in non-trivial expressions on the right-hand 
> side, using the keyword would help clarify the intent.  What I mean is 
> that the examples you should be looking at are more like:
> 
> A[n+1] = same*same + 1
> B[2*j].foo = frobnicate(same, same+1)
> ...
> 
> If you try expanding these into current python with minimal change in 
> semantics you will end up with something like
> 
> _1 = n+1
> _2 = A[_1]
> A[_1] = _2*_2 + 1
> del _1
> del _2

Um, I'm not following you.  Why would this not translate to:

A[n+1] = A[n+1]*A[n+1] + 1

which I find more readable than the 'same' example. 

> _1 = B[2*j]
> _2 = _1.foo
> _1.foo = frobnicate(_2, _2+1)
> del _1
> del _2

I'd be looking for ways to refactor that code, no matter how you
write it.  But really,

B[2*j].foo = frobnicate(B[2*j].foo, B[2*j].foo + 1)

is to my eyes much clearer than the 'same' version.  I had to scan
the 'same' version twice to figure out what it was doing, and then
a third time to make sure I had it right.  This version has more
characters and thus more visual clutter, but all of the information
needed to understand what it is doing is right under my eyes as
I scan it.  I don't have to look back to the beginning of the
statement to remember what the function arguments are.

> I still think that the cost of a new keyword is probably too high a 
> price to pay, but I like the idea.

Yeah, you have to make a really, _really_ compelling case to get
a new keyword added.  Something you can't do any other way, or at
least not without an awful lot of hassle.

--
R. David Murray   http://www.bitdance.com

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


Re: download x bytes at a time over network

2009-03-17 Thread n . s . buttar
http://code.activestate.com/recipes/114217/

On Tue, Mar 17, 2009 at 1:38 PM, Saurabh  wrote:
> Heres the reason behind wanting to get chunks at a time.
> Im actually retrieving data from a list of RSS Feeds and need to
> continuously check for latest posts.
> But I dont want to depend on Last-Modified header or the pubDate tag
> in . Because a lot of feeds just output date('now')  instead
> of the actual last-updated timestamp.
> But when continuously checking for latest posts, I dont want to
> bombard other people's bandwidth - so I just want to get chunks of
> bytes at a time and internally check for ... with my
> database against timestamp values.
> Is there a better way to achieve this ?
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: ValueError: filedescriptor out of range in select()

2009-03-17 Thread MRAB

Laszlo Nagy wrote:




Hi Laszlo,
Just a hunch -- are you leaking file handles and eventually running out?
These file handles are for TCP sockets. They are accept()-ed, used and 
then thrown out. I guess after the connection was closed, the file 
handle is destroyed automatically. BTW here is the shutdown() method for 
socket based endpoints:



def shutdown(self):
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
StreamEndpoint.shutdown(self)

This method is called after the connection has been closed. Is is 
possible that somehow the file handles are leaking?



Here's an interesting post:

http://mail.python.org/pipermail/python-list/2005-April/317442.html
--
http://mail.python.org/mailman/listinfo/python-list


Keyword same in right hand side of assignments (rev)

2009-03-17 Thread R. David Murray
hwpus...@yahoo.de wrote:
> What I would like is to extend the augmented assignment
> and make it easy to understand for naive readers.

Good luck. :)

> I hope the following literary definition 
> is consistent enough to convey the correct meaning:
>   "whenever it is possible, modify the target IN PLACE 
>   according to the right hand side expression.
>   If it is not possible to do such a thing,
>   substitute the target object with 
>   an object that is build according to the right hand side expression
>   and subsequently deleted"

I don't think that last part is expressing your intent.  If you delete
the object you've constructed you've got nothing left for the target to
point to.  From what you say later I think you are confusing identifiers
with objects in this text.

> The following examples should be correct:
>   "xx = same + 5"  synonymous with  "xx += 5" 
>   "value =  2*same + 5"  synonymous with  "value =*2; value +=5" 
>   "switch = 1 - same"  synonymous with  "switch *-1; switch +=1" 
>   "lst = same + [5,6]"  synonymous with  "lst += [5,6]"
>   "lst[2] = 1/same" synonymous with  "lst[2] **=-1"

Your revised intent breaks my expectations of how python's assignment
operator works.  At least with += and kin I'm alerted to the fact that
something weird is going on by the fact that the assignment operator
is different.

> The following examples would be extensions:
>   "lst = [5,6] + same" synonymous with
>       "lst.reverse(); lst.extend([6,5]); lst.reverse()"
>   "inmutable = same*(same+1)"  synonymous with
>       "unused=inmutable+1; inmutable*=unused; del unused"
>
> There seems to be no really simple expression for the above extensions,
> and I take that as an indication
> that the proposed feature could be quite useful.

For the first one, we have:

lst[:0] = [5, 6]

And unless I'm misunderstanding you, the second one is trivially
expressed as:

immutable = immutable*(immutable+1)

I'm afraid I'm -1 on this proposal even without the issue of the keyword.

--
R. David Murray   http://www.bitdance.com

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


Re: python book for a C programmer

2009-03-17 Thread Jorgen Grahn
On Fri, 13 Mar 2009 22:10:37 -0700 (PDT), Saurabh  wrote:
> Hi all,
> I am an experienced C programmer, I have done some perl code as well.
> But while thinking about large programs,I find perl syntax a
> hinderance.
> I read Eric Raymonds article reagrding python,(http://
> www.linuxjournal.com/article/3882).

I have a similar background, and I was pleased with this one:

%A Alex Martelli
%T Python in a nutshell
%I O'Reilly
%D 2003

Plus the excellent online docs.

/Jorgen

-- 
  // Jorgen Grahn   R'lyeh wgah'nagl fhtagn!
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to interface with C# without IronPython

2009-03-17 Thread Kay Schluehr
On 17 Mrz., 16:22, Mudcat  wrote:
> On Mar 17, 6:39 am, Kay Schluehr  wrote:
>
>
>
> > On 16 Mrz., 23:06, Mudcat  wrote:
>
> > > On Mar 13, 8:37 pm, Christian Heimes  wrote:
>
> > > > Chris Rebert wrote:
> > > > > Haven't used it, butPythonfor .NET sounds like it might be what you
> > > > > want:http://pythonnet.sourceforge.net/
>
> > > > I've done some development for and with PythonDotNET. It's definitely
> > > > the right thing. It works with .NET, Mono andPython2.4 to 2.6.
>
> > > > Christian
>
> > > That looks exactly like what I'm searching for. I'll give it a shot.
>
> > > One question, the last update for that is back in '07. Do you know if
> > > it's still in active development?
>
> > > Thanks
>
> > Don't think it's maintained right now. But be aware that it runs well
> > for the current CLR 2.0.
>
> > For using .NET 3.5 features you just have to add
>
> > c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\
>
> > to your Python path and add assemblies with clr.AddReference as
> > convenient.
>
> Based on the archived emails I know that it can work on Python 2.6,
> but it's not clear what modifications are necessary. I downloaded the
> source files to run independently, and that was capable of running
> with installs of Python 2.4 and 2.5.

> If I download the installer will
> it automatically recognize 2.6, or will I need to change paths and
> possibly re-compile?
>
> Thanks

You'll have to change the BUILD option in the project settings of the
Python.Runtime assembly. There is already a conditional compilation
switch for Python 2.6 available in the source. So after this has been
done it will build pythonnet for Python 2.6.

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


Re: engadget; "a language that few understand these days (it's Python, and no, we're not joking)"

2009-03-17 Thread Ian Mallett
I like some of the comments: "oh come on, you can practically just read it
like it's english" and "And there's me thinking Python was getting more
popular...", both true.
--
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 (py2.6) vs urllib.request (py3)

2009-03-17 Thread R. David Murray
mattia  wrote:
> Il Tue, 17 Mar 2009 10:55:21 +, R. David Murray ha scritto:
> 
> > mattia  wrote:
> >> Hi all, can you tell me why the module urllib.request (py3) add extra
> >> characters (b'fef\r\n and \r\n0\r\n\r\n') in a simple example like the
> >> following and urllib2 (py2.6) correctly not?
> >> 
> >> py2.6
> >> >>> import urllib2
> >> >>> f = urllib2.urlopen("http://www.google.com";).read() fd =
> >> >>> open("google26.html", "w")
> >> >>> fd.write(f)
> >> >>> fd.close()
> >> 
> >> py3
> >> >>> import urllib.request
> >> >>> f = urllib.request.urlopen("http://www.google.com";).read() with
> >> >>> open("google30.html", "w") as fd:
> >> ... print(f, file=fd)
> >> ...
> >> >>>
> >> >>>
> >> Opening the two html pages with ff I've got different results (the
> >> extra characters mentioned earlier), why?
> > 
> > The problem isn't a difference between urllib2 and urllib.request, it is
> > between fd.write and print.  This produces the same result as your first
> > example:
> > 
> > 
>  import urllib.request
>  f = urllib.request.urlopen("http://www.google.com";).read() with
>  open("temp3.html", "wb") as fd:
> > ... fd.write(f)
> > 
> > 
> > The "b''" is the stringified representation of a bytes object, which
> > is what urllib.request returns in python3.  Note the 'wb', which is a
> > critical difference from the python2.6 case.  If you omit the 'b' in
> > python3, it will complain that you can't write bytes to the file object.
> > 
> > The thing to keep in mind is that print converts its argument to string
> > before writing it anywhere (that's the point of using it), and that
> > bytes (or buffer) and string are very different types in python3.
> 
> Well... now in the saved file I've got extra characters "fef" at the 
> begin and "0" at the end...

The 'fef' is reminiscent of a BOM.  I don't see any such thing in the
data file produced by my code snippet above.  Did you try running that,
or did you modify your code?  If the latter, maybe if you post your
exact code I can try to run it and see if I can figure out what is going on.

I'm far from an expert in unicode issues, by the way :)  Oh, and I'm running
3.1a1+ from svn, by the way, so it is also possible there's been a bug
fix of some sort.

--
R. David Murray   http://www.bitdance.com

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


Cannot allocate memory when using os.spawn for moving files

2009-03-17 Thread Andreas

Hello there,

I have a problem moving files from my local harddrive to a NFS share 
using a Python script.
The script is used to run a model which produces large (~500MB) binary 
output files. The model itself is a Fortran program, and I call it from 
my script using the line


os.spawnlp(os.P_WAIT, 'time', 'time', OPT.binary)

where OPT.binary is the filename to be run. This works flawlessly.

However, in order not to clutter up my harddrive, I want to move the 
generated files to a network location after each model run. To save 
time, I use os.P_NOWAIT here:


os.spawnlp(os.P_NOWAIT, 'mv', 'mv', LOCALFILENAME, REMOTEFILENAME)

where LOCALFILENAME is some string like '/home/andreas/model.bin' and 
REMOTEFILENAME is some string like '/home/nfs/model-output/'.


Here I have the problem that after about every 10th model run, I get an 
error saying


Traceback (most recent call last):
  File "../../../../pyiup/b3dctm/run_b3dctm.py", line 281, in 
main()
  File "../../../../pyiup/b3dctm/run_b3dctm.py", line 71, in main
copy_model_output()
  File "../../../../pyiup/b3dctm/run_b3dctm.py", line 162, in 
copy_model_output
os.spawnlp(os.P_NOWAIT, 'mv', 'mv', get_base_filename(RUNDATE) + 
'.pdg', 
os.path.join(OPT.datastoragepath,OPT.modelname,OPT.runname,RUNDATE.strftime('%Y/

%m')))
  File "/usr/lib64/python2.5/os.py", line 635, in spawnlp
return spawnvp(mode, file, args)
  File "/usr/lib64/python2.5/os.py", line 584, in spawnvp
return _spawnvef(mode, file, args, None, execvp)
  File "/usr/lib64/python2.5/os.py", line 530, in _spawnvef
pid = fork()
OSError: [Errno 12] Cannot allocate memory

And my Python script dies.

Is there anyone who can help me with this problem? I'm on Ubuntu 8.04 
64bit with Python 2.5. I am willing to accept that the moving of the 
files does not always work, but then it would be really helpful if I 
could somehow prevent my script from dying and just try the moving again.


Any help is greatly appreciated!

Cheers,

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


supervisor 3.0a6 and Python2.6

2009-03-17 Thread George Trojan
Supervisor does not work with Python2.6. While running with the test 
configuration, supervisord prints traceback:

2009-03-17 15:12:31,927 CRIT Traceback (most recent call last):
  File 
"/usr/local/Python-2.6/lib/python2.6/site-packages/supervisor-3.0a6-py2.6.egg/supervisor/xmlrpc.py", 
line 367, in continue_request

pushproducer(DeferredXMLRPCResponse(request, value))
  File "/usr/local/Python-2.6/lib/python2.6/asynchat.py", line 190, in 
push_with_producer

self.initiate_send()
  File "/usr/local/Python-2.6/lib/python2.6/asynchat.py", line 227, in 
initiate_send

data = first.more()
AttributeError: class NOT_DONE_YET has no attribute 'more'

The last entry on http://supervisord.org/news/ is exactly one year ago, 
obviously it predates Python2.6. Two questions:

1. Is supervisor still developed?
2. Is there a quick fix to the above behaviour? If not, what would be a 
reasonable alternative to supervisor? I know of upstart and daemontools.


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


Re: ValueError: filedescriptor out of range in select()

2009-03-17 Thread Richard Brodie

"Laszlo Nagy"  wrote in message 
news:mailman.2032.1237300298.11746.python-l...@python.org...

> This method is called after the connection has been closed. Is is possible 
> that somehow 
> the file handles are leaking?

If I understand correctly, you call shutdown() but not close() in
response to a remote disconnect. That is likely to leak handles.
Check with lsof (or one of the Sysinternals tools on Windows). 


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


Re: How to interface with C# without IronPython

2009-03-17 Thread Mudcat
On Mar 17, 6:39 am, Kay Schluehr  wrote:
> On 16 Mrz., 23:06, Mudcat  wrote:
>
>
>
> > On Mar 13, 8:37 pm, Christian Heimes  wrote:
>
> > > Chris Rebert wrote:
> > > > Haven't used it, butPythonfor .NET sounds like it might be what you
> > > > want:http://pythonnet.sourceforge.net/
>
> > > I've done some development for and with PythonDotNET. It's definitely
> > > the right thing. It works with .NET, Mono andPython2.4 to 2.6.
>
> > > Christian
>
> > That looks exactly like what I'm searching for. I'll give it a shot.
>
> > One question, the last update for that is back in '07. Do you know if
> > it's still in active development?
>
> > Thanks
>
> Don't think it's maintained right now. But be aware that it runs well
> for the current CLR 2.0.
>
> For using .NET 3.5 features you just have to add
>
> c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\
>
> to your Python path and add assemblies with clr.AddReference as
> convenient.

Based on the archived emails I know that it can work on Python 2.6,
but it's not clear what modifications are necessary. I downloaded the
source files to run independently, and that was capable of running
with installs of Python 2.4 and 2.5. If I download the installer will
it automatically recognize 2.6, or will I need to change paths and
possibly re-compile?

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


  1   2   >