Re: download x bytes at a time over network

2009-03-17 Thread n . s . buttar
2009/3/16 Saurabh :
> I want to download content from the net - in chunks of x bytes or characters
> at a time - so that it doesnt pull the entire content in one shot.
>
> import urllib2
> url = "http://python.org/";
> handler = urllib2.urlopen(url)
>
> data = handler.read(100)
> print """Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100)
>
> # Disconnect the internet
>
> data = handler.read(100) # I want it to throw an exception because it cant
> fetch from the net
> print """Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100) # But
> still works !
>
> Apparently, handler = urllib2.urlopen(url) takes the entire data in buffer
> and handler.read(100) is just reading from the buffer ?
>
> Is it possible to get content from the net in chunks ? Or do I need to use
> HTTPClient/sockets directly ?
>
> Thanks
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

yes you can do it with urllib(2). Please take a look at the following
HTTP headers which facilitiate this kind of transfers

Content-Range http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.16
Range http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35

You can set the values for these and send the request to get partial contents.
However let me warn you that, not all servers allow this.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lists aggregation

2009-03-17 Thread Peter Otten
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.

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: download x bytes at a time over network

2009-03-17 Thread Steven D'Aprano
Please excuse my replying to a reply instead of the original, but the 
original doesn't show up on my news feed.


On 2009/3/16 Saurabh :
> I want to download content from the net - in chunks of x bytes or
> characters at a time - so that it doesnt pull the entire content in one
> shot.
>
> import urllib2
> url = "http://python.org/";
> handler = urllib2.urlopen(url)
>
> data = handler.read(100)
> print """Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100)
>
> # Disconnect the internet
>
> data = handler.read(100) # I want it to throw an exception because it
> # cant fetch from the net
> print """Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100) 
> # But still works !


Perhaps you have a local caching web proxy server, which has downloaded 
the entire document, and even though you have disconnected the Internet, 
you haven't disconnected the proxy which is feeding you the rest of the 
file from the server's cache.


>> Apparently, handler = urllib2.urlopen(url) takes the entire data in
>> buffer and handler.read(100) is just reading from the buffer ?

I could be wrong, but I don't think so.

I think you have the right approach. By default, urllib2 uses a 
ProxyHandler which will try to auto-detect any proxies (according to 
environment variables, registry entries or similar).




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


lib to auto-click mouse

2009-03-17 Thread oyster
is there any this kind of lib for python?
thanx
--
http://mail.python.org/mailman/listinfo/python-list


Re: finally successful in ods with python, just one help needed.

2009-03-17 Thread Krishnakant
Hi David and John.
Thanks a lot, the problem is solved.

David, your idea was the key to solve the problem.
Actually John in his code and the explanation made it clear that the
wrong attributes were being used on wrong elements.

david's code confirmed the fact.  The center style which david had in
his code solved the problem for me because we don't need paragraph
alignment styles but the merged cells to be having center alignment for
the text contained in those cells.

This implied that the centering aught to happen in the cells when they
are created. So that style was applied to the cells in david's code.

The other mistake was that after merging 4 cells I had to actually add 3
cells to be used by the merged (spanned ) cells.  after those cells I
could start my next set of merged cells.
Initially the mistake I was doing was adding a cell which could span 4
cells and try to add another cell just after the code that created that
merged set of cells.

But later on I realised that once cells are merged we have to physically
add the blank cells to fill up the merged space else the next few cells
won't show up with the other text.

Now the code works fine and if any one would ever need this, please do
write to me.

Thanks to all again, specially David and John.

happy hacking.
Krishnakant.



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


Re: download x bytes at a time over network

2009-03-17 Thread Saurabh
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


Re: A request (was: how to repeat function definitions less

2009-03-17 Thread Marc 'BlackJack' Rintsch
On Mon, 16 Mar 2009 02:59:57 -0700, Michele Simionato wrote:

> On Mar 16, 8:08 am, Dennis Lee Bieber  wrote:
>> On Sun, 15 Mar 2009 23:18:54 -0500, alex goretoy
>>         Many of your posts are actually exceeding the 500-line
>>         limit I've
>> set in my client for down-load -- yet have nothing worthy of 500 lines!
> 
> Could this be the reason why I cannot see his messages on Google Groups?

I don't see them either.  My news server provider filters spam too.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem: Terminal (OS X) window exits immediately on opening. & Solution.

2009-03-17 Thread Python


On 16 mrt 2009, at 22:15, Lou Pecora wrote:


In article ,
Python  wrote:


--

why don't you just execute the script directly form the terminal?
then you will be able to read all error messages...
and you can delete all the files you want

just my 2c

Arno


Because the shell process in the Terminal window would exit right  
after

it started even when I was just trying to open a new window (not even
running a script), i.e. command-N in Terminal.  So I could not run
anything from the Terminal.

More info:  There is an Executestring in the Terminal preferences that
is executed (I would guess as a shell command or script) when the  
shell

starts up (which happens whenever a new window is opened).  If that is
bad in any way, the shell exists with an error message.  There is no  
way
to run any shell in the Terminal at that point.  The only option  
appears
to be to clean up the preferences.  I don't know why the preferences  
got

a bad command while I was running Python scripts.  Maybe nothing to do
with Python, maybe it does.  Not sure.

--
-- Lou Pecora
--
oh i think it understand i now... it has nothing to do with python,  
has it?

just with some garbage left by a python script that should have been
deleted at startup of OSX

501 is the default user ID in OSX
maybe it's a bash thing?


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


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

2009-03-17 Thread mattia
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?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Lists aggregation

2009-03-17 Thread mattia
Il Tue, 17 Mar 2009 08:18:08 +0100, Peter Otten ha scritto:

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

Correct, and thanks for all the help.
--
http://mail.python.org/mailman/listinfo/python-list


Re: String to sequence

2009-03-17 Thread Jeremiah Dodds
On Sat, Mar 14, 2009 at 9:09 AM, mattia  wrote:

> How can I convert the following string:
>
> 'AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'
>
> into this sequence:
>
> ['AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
> EGC','SXF','BZR','BIQ','BLL','BHX','BLQ']
>
> Thanks a lot,
> Mattia
> --
> http://mail.python.org/mailman/listinfo/python-list
>


I don't know if I'm missing something or what in other peoples replies (they
seem unnecessarily complicated) or if what popped into my head would be a
bad idea, but I would do:

>>> s = "'AAR','ABZ','AGA','AHO','ALC','LEI','AOC',
'EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'"
>>> s.replace("'","").split(',')
['AAR',
 'ABZ',
 'AGA',
 'AHO',
 'ALC',
 'LEI',
 'AOC',
 'EGC',
 'SXF',
 'BZR',
 'BIQ',
 'BLL',
 'BHX',
 'BLQ']
--
http://mail.python.org/mailman/listinfo/python-list


Re: python book for a C programmer

2009-03-17 Thread Jeremiah Dodds
On Sat, Mar 14, 2009 at 5:10 AM, Saurabh  wrote:

> For introduction I am thinking about 'Learning Python' and for
> reference I am thinking about 'Python Bible'.
>
> I need your suggestions on this.
>
> Thanks in advance
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Here's another vote for "Python in a Nutshell" after the tutorial, it's an
excellent read, and perfect for someone who has a bit of experience with
other languages.
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-03-17 Thread R. David Murray
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.

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

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


array next pointer

2009-03-17 Thread Anjanesh Lekshminarayanan
>>> a = ['cat','dog','elephant']
>>> a.next()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'list' object has no attribute 'next'
>>>
Is there something that imtates PHP's next() ? (http://php.net/next)
--
http://mail.python.org/mailman/listinfo/python-list


Re: download x bytes at a time over network

2009-03-17 Thread R. David Murray
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 ?
> 
> >>> import urllib.request
> >>> url = "http://feeds2.feedburner.com/jquery/";
> >>> handler = urllib.request.urlopen(url)
> >>> data = handler.read(1000)
> >>> print("""Content :\n%s \n%s \n%s""" % ('=' * 100, data, '=' * 100))
> Content :
> 
> b'b495\r\n\r\nhttp://www.bitdance.com

--
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 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...
--
http://mail.python.org/mailman/listinfo/python-list


Re: array next pointer

2009-03-17 Thread Andre Engels
On Tue, Mar 17, 2009 at 12:12 PM, Anjanesh Lekshminarayanan
 wrote:
 a = ['cat','dog','elephant']
 a.next()
> Traceback (most recent call last):
>  File "", line 1, in 
> AttributeError: 'list' object has no attribute 'next'

> Is there something that imtates PHP's next() ? (http://php.net/next)

>>> b = a.__iter__()
>>> b.next()
1
>>> b.next()
2

However, in many cases the more Pythonic way of working will be to use
a for-loop (which is like a foreach-loop in PHP and other languages).


-- 
André Engels, andreeng...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: array next pointer

2009-03-17 Thread Andre Engels
On Tue, Mar 17, 2009 at 12:24 PM, Andre Engels  wrote:
> On Tue, Mar 17, 2009 at 12:12 PM, Anjanesh Lekshminarayanan
>  wrote:
> a = ['cat','dog','elephant']
> a.next()
>> Traceback (most recent call last):
>>  File "", line 1, in 
>> AttributeError: 'list' object has no attribute 'next'
>
>> Is there something that imtates PHP's next() ? (http://php.net/next)
>
 b = a.__iter__()
 b.next()
> 1
 b.next()
> 2

That should of course be:

>>> b = a.__iter__()
>>> b.next()
'cat'
>>> b.next()
'dog'

I had copied a bit too literal from my own attempt...

-- 
André Engels, andreeng...@gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: array next pointer

2009-03-17 Thread andrew cooke
Andre Engels wrote:
[...]
 b = a.__iter__()
 b.next()
> 'cat'
 b.next()
> 'dog'

not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:

>>> b = iter(a)
>>> b.next()

andrew


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


Re: array next pointer

2009-03-17 Thread andrew cooke
Andre Engels wrote:
[...]
 b = a.__iter__()
 b.next()
> 'cat'
 b.next()
> 'dog'

NOTE CORRECTION BELOW!  IT'S next(b), not b.next() which doesn't exist in
Python 3 (if you need it, it's now b.__next__()).  sorry.

not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:

>>> b = iter(a)
>>> next(b)

andrew




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


Re: How to interface with C# without IronPython

2009-03-17 Thread Kay Schluehr
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.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Problem: Terminal (OS X) window exits immediately on opening. & Solution.

2009-03-17 Thread Lou Pecora

Python wrote:


On 16 mrt 2009, at 22:15, Lou Pecora wrote:


Because the shell process in the Terminal window would exit right after
it started even when I was just trying to open a new window (not even
running a script), i.e. command-N in Terminal.  So I could not run
anything from the Terminal.

More info:  There is an Executestring in the Terminal preferences that
is executed (I would guess as a shell command or script) when the shell
starts up (which happens whenever a new window is opened).  If that is
bad in any way, the shell exists with an error message.  There is no way
to run any shell in the Terminal at that point.  The only option appears
to be to clean up the preferences.  I don't know why the preferences got
a bad command while I was running Python scripts.  Maybe nothing to do
with Python, maybe it does.  Not sure.

--
-- Lou Pecora
--
oh i think it understand i now... it has nothing to do with python, 
has it?

Well, I can't figure out which app/system is to "blame."

just with some garbage left by a python script that should have been
deleted at startup of OSX
The garbage was left in the Terminal's preferences (the Property List). 
That's pretty specific.  Why?  I don't know.  It just shouldn't have 
been there, I think.  It had nothing to do with the startup of OS X.


501 is the default user ID in OSX
maybe it's a bash thing?
That's interesting.  Could be.  At least I found the solution and since 
it seemed to involve Python, I thought the OS X people on the list might 
be interested.  Thanks for the info.


--
Cheers,

Lou Pecora

Code 6362
Naval Research Lab
Washington, DC  20375, USA
Ph:  +202-767-6002
email:  louis.pec...@nrl.navy.mil

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


Re: Problem while copying a file from a remote filer

2009-03-17 Thread Jorgen Grahn
On Sun, 15 Mar 2009 22:47:54 -0700, Chris Rebert  wrote:
> On Sun, Mar 15, 2009 at 10:24 PM, venutaurus...@gmail.com
>  wrote:
>> Hi all,
>>      I have to write an application which does a move and copy of a
>> file from a remote machine to the local machine. I tried something
>> like:
>>
>> file = ur"venuwin2008\\C\\4Folders\\Folder02\\Folder002\
>> \TextFile_06.txt"
>
> The 'r' prefix on the string makes it a raw string, meaning you don't
> have do double-up the backslashes, but you did so anyway, so your path
> has many extra backslashes, making it invalid. Dropping the 'r' prefix
> should fix the problem.

Also, the file isn't really remote if you can use the normal local
file system calls to read it.

/Jorgen

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


Re: array next pointer

2009-03-17 Thread Tim Chase

not sure from what version, but certainly in 2.6 and on, you can improve
the syntax slightly:


b = iter(a)
b.next()


This syntax (also my preferred version) has been available since 
at least 2.3


-tkc


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


Re: Problem while copying a file from a remote filer

2009-03-17 Thread Tim Golden

Jorgen Grahn wrote:

On Sun, 15 Mar 2009 22:47:54 -0700, Chris Rebert  wrote:

On Sun, Mar 15, 2009 at 10:24 PM, venutaurus...@gmail.com
 wrote:

Hi all,
 I have to write an application which does a move and copy of a
file from a remote machine to the local machine. I tried something
like:

file = ur"venuwin2008\\C\\4Folders\\Folder02\\Folder002\
\TextFile_06.txt"

The 'r' prefix on the string makes it a raw string, meaning you don't
have do double-up the backslashes, but you did so anyway, so your path
has many extra backslashes, making it invalid. Dropping the 'r' prefix
should fix the problem.


Also, the file isn't really remote if you can use the normal local
file system calls to read it.


That's a slightly strange position to take. 
When *is* it remote, then?


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


Re: download x bytes at a time over network

2009-03-17 Thread Jorgen Grahn
On Tue, 17 Mar 2009 13:38:31 +0530, 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 ?

I don't know much about RSS, but one approach is "If they are too lazy
to provide the information which protects their bandwidth, they
deserve being bombarded. But they also deserve a polite mail telling
them that they have that problem."

/Jorgen

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


Re: lib to auto-click mouse

2009-03-17 Thread Marcin Stępnicki
Dnia Tue, 17 Mar 2009 15:43:27 +0800, oyster napisał(a):

> is there any this kind of lib for python? thanx

If you plan to use it for some sort of automatic testing, look here:

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy#GUITestingTools
--
http://mail.python.org/mailman/listinfo/python-list


Re: error writing str to binary stream - fails in Python 3.0.1, works in 2.x

2009-03-17 Thread wallenpb
On Mar 16, 5:42 pm, John Machin  wrote:
> On Mar 17, 9:29 am, "R. David Murray"  wrote:
>
>
>
> > walle...@gmail.com wrote:
> > > On Mar 16, 4:10 pm, Benjamin Peterson  wrote:
> > > >   gmail.com> writes:
>
> > > > > self.out.write(b'BM') worked beautifully.  Now I also have a similar
> > > > > issue, for instance:
> > > > > self.out.write("%c" % y) is also giving me the same error as the other
> > > > > statement did.
> > > > > I tried self.out.write(bytes("%c" %y),encoding=utf-8) in an attempt to
> > > > > convert to bytes, which it did, but not binary.  How do I affect
> > > > > self.out.write("%c" % y) to write out as a binary byte steam?  I also
> > > > > tried self.out.write(b"%c" % y), but b was an illegal operator in when
> > > > > used that way.
> > > > > It is also supposed to be data being written to the .bmp file. --Bill
>
> > > > Are you writing to sys.stdout? If so, use sys.stdout.buffer.write(b'some
> > > > bytes'). If you're writing to a file, you have to open it in binary 
> > > > mode like
> > > > this: open("someimage.bmp", "wb")
>
> > > Yes, I am writing to a file.  That portion is correct and goes like
> > > this:
>
> > > self.out=open(filename,"wb")
> > >     self.out.write(b"BM")          # This line works thanks to advice 
> > > given
> > >                                    # in previous reply
>
> > > However, here is some more code that is not working and the error it
> > > gives:
>
> > > def write_int(self,n):
> > >     str_out='%c%c%c%c' % ((n&255),(n>>8)&255,(n>>16)&255,(n>>24)&255)
> > >     self.out.write(str_out)
>
> > > this is line 29, does not work - not
> > > sure how to get this complex str converted over to binary bytes to
> > > write to bmp file.
>
> > (I reformatted your message slightly to make the code block stand out more.)
>
> > A byte array is an array of bytes, and it understands integers as input.
> > Check out the PEP (the official docs leave some things out):
>
> >    http://www.python.org/dev/peps/pep-0358/
>
> > Here is some example code that works:
>
> No it doesn't. There is an extra "(" in the assignment to bytesout.
>
>
>
> >     out=open('temp', "wb")
> >     out.write(b"BM")
>
> >     def write_int(out, n):
> >         bytesout=bytes(([n&255), (n>>8)&255, (n>>16)&255, (n>>24)&255])
> >         out.write(bytesout)  
>
> >     write_int(out, 125)
>
> Consider using the struct module; it's expressly designed for that
> sort of thing.
>
> import struct
> out.write(struct.pack("<2sI", "BM", 125))
>
> HTH,
> John

To everyone who has responded, thanks very much.  This was an effort
to port some 2.x code to 3.0.1.  It is obvious now that just a couple
of tweaks will not do it.  I need to step back and give the code a
good reevaluation.  Thanks for all the great pointers!  I learned a
lot.   :-)

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


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

2009-03-17 Thread Vincent Davis
Thought you might find his interesting.

"A standalone Eye-Fi server has now been presented to the general public,
and while it's written in a language that few understand these days (it's
Python, and no, we're not joking), the functionality here is second to
none."
http://www.engadget.com/2009/03/17/sandalone-eye-fi-server-hack-one-ups-eye-fi-manager/

Thanks
Vincent Davis
720-301-3003
--
http://mail.python.org/mailman/listinfo/python-list


Re: array next pointer

2009-03-17 Thread Luis Zarrabeitia

Quoting andrew cooke :

> Andre Engels wrote:
> [...]
>  b = a.__iter__()
>
> not sure from what version, but certainly in 2.6 and on, you can improve
> the syntax slightly:
> >>> b = iter(a)
> >>> b.next()

Indeed. Directly calling __special_methods__ should be avoided. That one is a
better idiom.

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

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie
 Participe en Universidad 2010, del 8 al 12 de febrero de 2010
 La Habana, Cuba 
 http://www.universidad2010.cu
 
--
http://mail.python.org/mailman/listinfo/python-list


ValueError: filedescriptor out of range in select()

2009-03-17 Thread Laszlo Nagy
This is a long running process, written in Python. Only standard lib is 
used. This process accepts connections on TCP sockets, read/write data.


After about one day, it starts throwing this when I try to connect:

2009-03-17 09:49:50,096 INFO .accesspoint0 ('127.0.0.1', 55510) connecting
2009-03-17 09:49:50,097 ERROR .accesspoint0 Traceback (most recent call 
last):
 File 
"/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/accesspoints/srvtcp.py", 
line 34, in handle_request

   t = sorb.endpoint.SocketEndpoint(conn,self.router)
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 304, in __init__

   StreamEndpoint.__init__(self,router)
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 133, in __init__

   self.format = self.read_str() # determine remote format
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 236, in read_str

   size = self.read_long()
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 222, in read_long

   return struct.unpack(">q",self.read_data(8))[0]
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", 
line 344, in read_data

   ready = select.select([fd], [], [], 0.2)
ValueError: filedescriptor out of range in select()


The code for read_data:

   def read_data(self,size):
   res = ""
   fd = self.socket.fileno()
   while not self.stop_requested.isSet():
   remaining = size - len(res)
   if remaining<=0:
   break
   # Give one second for an incoming connection so we can stop the
   # server in seconds when needed
   ready = select.select([fd], [], [], 0.2)
   if fd in ready[0]:
   data = self.socket.recv(min(remaining,8192)) # 8192 is 
recommended by socket.socket manual.

   if not data:
   # select returns the fd but there is no data to read 
-> connection closed!

   self.shutdown()
   raise TransportClosedError("Connection closed.")
   else:
   res += data
   else:
   pass
   if self.stop_requested.isSet():
   self.shutdown()
   raise TransportClosedError()
   return res


If I telnet this server this is what I see:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.


E.g. the connection is closed instantly after the connection has been 
made. For some reason, the client socket on the server side has an 
invalid fileno, and it cannot be passed to select.select(). I found some 
articles on the internet about this error, but nothing useful about how 
to solve it.


Please help.

  Laszlo

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


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

2009-03-17 Thread Philip Semanchuk


On Mar 17, 2009, at 10:04 AM, Laszlo Nagy wrote:

This is a long running process, written in Python. Only standard lib  
is used. This process accepts connections on TCP sockets, read/write  
data.


After about one day, it starts throwing this when I try to connect:

2009-03-17 09:49:50,096 INFO .accesspoint0 ('127.0.0.1', 55510)  
connecting
2009-03-17 09:49:50,097 ERROR .accesspoint0 Traceback (most recent  
call last):
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/accesspoints/ 
srvtcp.py", line 34, in handle_request

  t = sorb.endpoint.SocketEndpoint(conn,self.router)
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py",  
line 304, in __init__

  StreamEndpoint.__init__(self,router)
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py",  
line 133, in __init__

  self.format = self.read_str() # determine remote format
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py",  
line 236, in read_str

  size = self.read_long()
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py",  
line 222, in read_long

  return struct.unpack(">q",self.read_data(8))[0]
File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py",  
line 344, in read_data

  ready = select.select([fd], [], [], 0.2)
ValueError: filedescriptor out of range in select()



Hi Laszlo,
Just a hunch -- are you leaking file handles and eventually running out?


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


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

2009-03-17 Thread Laszlo Nagy




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?


L

--
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 15:04:22 +0100, Laszlo Nagy  wrote:
This is a long running process, written in Python. Only standard lib is 
used. This process accepts connections on TCP sockets, read/write data.


After about one day, it starts throwing this when I try to connect:

[snip]
 File "/usr/local/www/vhosts/shopzeus.com/fantasy/sorb/endpoint.py", line 
344, in read_data

   ready = select.select([fd], [], [], 0.2)
ValueError: filedescriptor out of range in select()



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. ;)

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?  If that's the case, there may be other approaches
to solving the problem (like fixing the bug in CPython which causes this).

Jean-Paul
--
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 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.

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


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

2009-03-17 Thread Philip Semanchuk


On Mar 17, 2009, at 10:31 AM, 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?


Well, anything is possible...it's also possible that file handles are  
being leaked elsewhere, by your code or another application. I don't  
know socket code very well so my hunch was not so much based on your  
code as much as on your general description of the problem. To have  
something break after a day of working OK is a common way for a  
leaking-resource bug to express itself. Since you're having trouble  
with a file handle, it seems logical to conclude that file handles  
might be the resource that's being used up.


I had a similar bug in on of my apps and the only reason I found it  
was because my Apache logs had "no open file handle" errors. It turned  
out that I had a process (unrelated to Apache) that was leaking file  
handles at a fixed rate. When the process ended all of the file  
handles were returned to the OS. It was only if the process ran for  
several days that it would consume all of the available file handles  
in the system. And then at some point it would exit and the problem  
would magically clear itself up. It was easy to find the broken code  
once I realized what was going on.


Like I said, it's just a hunch.

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

In order to get the correct encoding I've come up with this:
>>> response = urllib.request.urlopen("http://www.google.com";)
>>> print(response.read().decode(response.headers.get_charsets()[0]))
--
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 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!)

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

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


Re: Roundup Issue Tracker release 1.4.7

2009-03-17 Thread J Kenneth King
Richard Jones  writes:

> I'm proud to release version 1.4.7 of Roundup.

> - Allow CGI frontend to serve XMLRPC requests.
> - Added XMLRPC actions, as well as bridging CGI actions to XMLRPC actions.

Sweet.

I'm working on a small project called TracShell which is a command-line
front-end to the Trac issue tracker and uses XML-RPC to communicate with
the server.

However, as I work with it/hack on it I'm slowly building it into a
generic front-end to work with any XML-RPC enabled issue tracker.

I'll definitely look into extending it to work with Roundup in the
future now that it has XML-RPC support. :)

Keep up the great work!
--
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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


  1   2   >