Re: Very simple WSGI question

2008-11-22 Thread Manlio Perillo
Il Mon, 17 Nov 2008 01:35:19 +, Matthew Wilson ha scritto:

 I want to write some middleware to notice when the inner app returns a
 500 status code.  I'm sure there are already sophisticated loggers that
 do this sort of thing, but I'm using this as a learning exercise.
 
 Right now, I wrapped the start_response callable.  So when the WSGI
 application calls the start response callable, I look at the first arg
 passed in and do my test.
 
 What's the right way to do this?
 

Your method is correct.

As a more elaborated example:
http://hg.mperillo.ath.cx/wsgix/file/tip/wsgix/contrib/error_page.py

This middleware enables the rendering of custom pages for HTTP error 
responses.

 Matt



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


problem with internationalized headers in email package

2008-06-29 Thread Manlio Perillo
Hi.

From RFC 2047:
+ An 'encoded-word' MUST NOT appear in any portion of an 'addr-spec'.
+ An 'encoded-word' MUST NOT appear within a 'quoted-string'.
+ An 'encoded-word' MUST NOT be used in a Received header field.
+ An 'encoded-word' MUST NOT be used in parameter of a MIME
  Content-Type or Content-Disposition field, or in any structured
  field body except within a 'comment' or 'phrase'.


However (Python 2.5.2):

 h = Header(u'Andrè [EMAIL PROTECTED]')
 h.encode()
'=?utf-8?b?QW5kcsOoIDxhbmRyZUBsb2NhbGhvc3Q+?='


I'm not sure if this can be considered a bug, but surely this is an 
invalid header.



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

Re: Beta testers needed for a high performance Python application server

2008-03-26 Thread Manlio Perillo
Il Tue, 25 Mar 2008 20:31:39 +, Minor Gordon ha scritto:

 Hello all,
 
 I'm looking for beta testers for a high performance, event-driven Python
 application server I've developed.
 
 About the server: the front end and other speed-critical parts of the
 server are written in portable, multithreaded C++. The back end is an
 embedded CPython interpreter. The server is much faster than anything in
 pure Python, and it can compete with C servers (including e.g. lighttpd
 for file workloads) or outdo them (e.g. anything behind Apache) until
 CPython consumes a single processor. 


Have you tried my WSGI implementation for Nginx?
http://hg.mperillo.ath.cx/nginx/mod_wsgi/

Its not a general purpose solution, but it can be of interest.

 On the Python side it supports WSGI
 (the server can handle the static and dynamic requests of MoinMoin with
 a handful of lines), the DB API with blocking calls offloaded to a
 connection in a separate thread (MySQL, SQLite supported), Google's
 ctemplate, gzipping responses, file caching, reading and writing to URIs
 as a client, AJAX integration, debugging as a Python extension, and a
 lot of other features. The core Python API is event-driven, using
 continuations like Twisted but much cleaner (continuations are any
 callables, there are no special objects anywhere). The Python back end
 also supports Stackless Python so all of the continuation machinery can
 be hidden behind tasklet switching.
 


I have recently added support for asynchronous application.
There are two examples: an application that execute a query to PostgreSQL 
and an application that execute an HTTP request with pycurl:

http://hg.mperillo.ath.cx/nginx/mod_wsgi/file/tip/examples/nginx-postgres-
async.py
http://hg.mperillo.ath.cx/nginx/mod_wsgi/file/tip/examples/nginx-curl.py


Note that ngx.poll extension is still experimental.



 [...]


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


Re: Beta testers needed for a high performance Python application server

2008-03-26 Thread Manlio Perillo
Il Wed, 26 Mar 2008 00:22:38 -0700, John Nagle ha scritto:

 Graham Dumpleton wrote:
 Yes that is a viable option, as still are existing fastcgi solutions
 for Apache, lighttpd and nginx.
 
 Fast cgi is a good technology, 

Well, not really so good:
http://twistedmatrix.com/pipermail/twisted-web/2006-April/002598.html

 but it's not well documented or
 well supported.  For some reason, the Apache people don't like it. It
 used to be part of the Apache distribution, but that ended years ago.
 
 It's more reliable than using things like mod_python, where you have
 application code running in the web server's address space.  That
 creates both security problems and robustness problems.  If an fcgi
 process crashes, it is automatically replaced by a fresh copy of the
 program at the next request.  Other activity in progress is not
 affected. Also, fcgi processes are reloaded after some number of
 requests, so minor memory leaks won't choke the system over time.
 

The problem is simple: why put an extra server layer between an HTTP 
client and an HTTP server?


Moreover, you should not use mod_python as an example.
The WSGI module for Apache has a lot of feature for reducing these 
problems; and as an alternative you can use the WSGI implementation for 
Nginx.


 [...]


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


Re: High speed web services

2007-12-14 Thread Manlio Perillo
Il Fri, 14 Dec 2007 11:07:49 -0800, herbasher ha scritto:

 Hello!
 
 I'm wondering: I'm really not so much into heavy frameworks like Django,
 because I need to build a fast, simple python based webservice.
 
 That is, a request comes in at a certain URL, and I want to utilize
 Python to respond to that request.
 
 Ideally, I want the script to be cached so it doesn't have to be re-
 parsed everytime.
 
 
 Phrasing this into a question:
 How do I built highly available and lighting fast Python webservice?
 
 I'm open to using any kind of server software, and all that under a *nix
 based system.
 

I'm developing a mod_wsgi module for Nginx (http://www.nginx.net).
Nginx is a very fast asynchronous and multiprocess web server.

The development is still in alpha status:
http://hg.mperillo.ath.cx/nginx/mod_wsgi/


If your application is not I/O bound mod_wsgi for nginx can be a good 
solution, otherwise you should look for mod_wsgi for Apache.



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


PyErr_NoMemory and multiple sub-interpreters

2007-12-11 Thread Manlio Perillo
Hi.

Is it safe to use PyErr_NoMemory in a multi sub-interpreters environment?

I have some doubts since PyErr_NoMemory uses a global variable: 
PyExc_MemoryErrorInst



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


Re: __unicode__ method for exception object

2007-07-08 Thread Manlio Perillo
Il Sun, 08 Jul 2007 10:02:01 +1000, Ben Finney ha scritto:

 Manlio Perillo [EMAIL PROTECTED] writes:
 
 I have just noticed that exception objects does not handle well Unicode
 arguments.
 
 This error is unrelated to the fact that you created an exception
 object.
 

No, it is related.

  e = RuntimeError(u'àèìòù')
  str(e)
 Traceback (most recent call last):
   File stdin, line 1, in ?
 UnicodeEncodeError: 'ascii' codec can't encode characters in position
 
 Your terminal has been detected as using the 'ascii' encoding, so while
 that's true no attempt to output non-ASCII characters will work.
 
 You'll need to change whatever settings are on your terminal emulator so
 that it is using an encoding (such as 'utf-8') which can display the
 characters you want.


This is not a problem with the terminal.
And the problem is not with str(e) but with unicode(e).

unicode(e) converts the exception argument to an Unicode object, but 
since no __unicode__ object is defined, it firsts calls the __str__ 
method (and this, of course, fails, since the default encoding in CPython 
is us-ascii) and then converts the result to an Unicode object using, 
again, the default encoding.



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

__unicode__ method for exception object

2007-07-07 Thread Manlio Perillo
Hi all.

I have just noticed that exception objects does not handle well Unicode 
arguments.

 e = RuntimeError(u'àèìòù')
 str(e)
Traceback (most recent call last):
  File stdin, line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 
0-4: ordinal not in range(128)
 unicode(e)
Traceback (most recent call last):
  File stdin, line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode characters in position 
0-4: ordinal not in range(128)


C object struct does not have a tp_unicode slot (maybe it will be added 
in Python 3000?), however I think that the Base Exception class should 
implement the __unicode__ method.

Any problems in doing so?
I have not yet required this feature in the Python tracker.


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

Re: Problems with email.Generator.Generator

2006-09-11 Thread Manlio Perillo
Chris Withers ha scritto:
 Hi All,
 
 The following piece of code is giving me issues:
 
 from email.Charset import Charset,QP
 from email.MIMEText import MIMEText
 charset = Charset('utf-8')
 charset.body_encoding = QP
 msg = MIMEText(
 u'Some text with chars that need encoding: \xa3',
 'plain',
 )
 msg.set_charset(charset)
 print msg.as_string()
 
 Under Python 2.4.2, this produces the following output, as I'd expect:
 

 [...]
 However, under Python 2.4.3, I now get:
 

Try with:

msg = MIMEText(
 u'Some text with chars that need encoding: \xa3',
 _charset='utf-8',
 )


and you will obtain the error:


Traceback (most recent call last):
  File pyshell#4, line 3, in -toplevel-
_charset='utf-8',
  File C:\Python2.4\lib\email\MIMEText.py, line 28, in __init__
self.set_payload(_text, _charset)
  File C:\Python2.4\lib\email\Message.py, line 218, in set_payload
self.set_charset(charset)
  File C:\Python2.4\lib\email\Message.py, line 260, in set_charset
self._payload = charset.body_encode(self._payload)
  File C:\Python2.4\lib\email\Charset.py, line 366, in body_encode
return email.base64MIME.body_encode(s)
  File C:\Python2.4\lib\email\base64MIME.py, line 136, in encode
enc = b2a_base64(s[i:i + max_unencoded])
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in
position 41: ordinal not in range(128)




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


Re: Problems with email.Generator.Generator

2006-09-11 Thread Manlio Perillo
Chris Withers ha scritto:
 [...]
 
 OK, but I fail to see how replacing one unicode error with another is
 any help... :-S
 


The problem is simple: email package does not support well Unicode strings.

For now I'm using this:

charset = utf-8 # the charset to be used for email


class HeadersMixin(object):
A custom mixin, for automatic internationalized headers
support.


def __setitem__(self, name, val, **_params):
if isinstance(val, str):
try:
# only 7 bit ascii
val.decode(us-ascii)
except UnicodeDecodeError:
raise ValueError(8 bit strings not accepted)

return self.add_header(name, val)
else:
try:
# to avoid unnecessary trash
val = val.encode('us-ascii')
except:
val = Header.Header(val, charset).encode()

return self.add_header(name, val)


class MIMEText(HeadersMixin, _MIMEText.MIMEText):
A MIME Text message that allows only Unicode strings, or plain
ascii (7 bit) ones.


def __init__(self, _text, _subtype=plain):
_charset = charset

if isinstance(_text, str):
try:
# only 7 bit ascii
_text.decode(us-ascii)
_charset = us-ascii
except UnicodeDecodeError:
raise ValueError(8 bit strings not accepted)
else:
_text = _text.encode(charset)

return _MIMEText.MIMEText.__init__(self, _text, _subtype, _charset)


class MIMEMultipart(HeadersMixin, _MIMEMultipart.MIMEMultipart):
def __init__(self):
_MIMEMultipart.MIMEMultipart.__init__(self)



This only accepts Unicode strings or plain ascii strings.




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


Re: Making HTTP requests using Twisted

2006-07-11 Thread Manlio Perillo
rzimerman ha scritto:
 I'm hoping to write a program that will read any number of urls from
 stdin (1 per line), download them, and process them. So far my script
 (below) works well for small numbers of urls. However, it does not
 scale to more than 200 urls or so, because it issues HTTP requests for
 all of the urls simultaneously, and terminates after 25 seconds.
 Ideally, I'd like this script to download at most 50 pages in parallel,
 and to time out if and only if any HTTP request is not answered in 3
 seconds. What changes do I need to make?
 

Take a look at
http://svn.twistedmatrix.com/cvs/trunk/doc/core/examples/stdiodemo.py?view=markuprev=15456

And read
http://twistedmatrix.com/documents/current/api/twisted.web.client.HTTPClientFactory.html

You can pass a timeout to the constructor.

To download at most 50 pages in parallel you can use a download queue.

Here is a quick example, ABSOLUTELY NOT TESTED:

class DownloadQueue(object):
SIZE = 50

def init(self):
self.requests = [] # queued requests
self.deferreds = [] # waiting requests

def addRequest(self, url, timeout):
if len(self.deferreds) = sels.SIZE:
# wait for completion of all previous requests
DeferredList(self.deferreds
 ).addCallback(self._callback)
self.deferreds = []

# queue the request
deferred = Deferred()
self.requests.append((url, timeout, deferred))

return deferred
else:
# execute the request now
deferred = getPage(url, timeout=timeout)
self.deferreds.append(deferred)

return deferred

def _callback(self):
if len(self.requests)  self.SIZE:
queue = self.requests[:self.SIZE]
self.requests = self.requests[self.SIZE:]
else:
queue = self.requests[:]
self.requests = []

# execute the requests
for (url, timeout, deferredHelper) in queue:
deferred = getPage(url, timeout=timeout)
self.deferreds.append(deferred)

deferred.chainDeferred(deferredHelper)




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


Re: Making HTTP requests using Twisted

2006-07-11 Thread Manlio Perillo
Manlio Perillo ha scritto:
 [...]

 Here is a quick example, ABSOLUTELY NOT TESTED:
 
 class DownloadQueue(object):
 SIZE = 50
 
 def init(self):
 self.requests = [] # queued requests
 self.deferreds = [] # waiting requests
 
 def addRequest(self, url, timeout):
 if len(self.deferreds) = sels.SIZE:
 # wait for completion of all previous requests
 DeferredList(self.deferreds
  ).addCallback(self._callback)
 self.deferreds = []

The deferreds list should be cleared in the _callback method, not here.
Please note that probably there are other bugs.


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


[ANN] pglib 0.1

2006-05-22 Thread Manlio Perillo
I'm pleasant to announce the first public release of pglib.

pglib is an implementation of the PostgreSQL protocol, version 3.0.
It is written in Twisted, and aims to be a complete replacement for libpq.

In the current version the extended query sub protocol is not supported.

The project can be found at:
http://developer.berlios.de/projects/pglib/

Unfortunately, I still have to write some documentation.
However there is a (hopefully) detailed README and a test case.



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


[ANN] pykill32 0.2

2006-05-15 Thread Manlio Perillo
pykill32 is a small library that enable developers and users on Windows
2000/XP to send signals to a Python process.

It emulates the POSIX kill.
There is also a script, kill.py, with a minimal emulation of POSIX kill(1).

And a simple dumpbin.py script that list all modules (DLL) loaded by a
process.

pykill32 works by creating a thread on the *remote* process
(CreateRemoteThread) that calls the raise procedure from the MSVCR71 module.

It just works fine for Twisted processes.


The library can be found on:
http://developer.berlios.de/projects/pykill32/

and via SVN:
http://svn.berlios.de/svnroot/repos/pykill32/trunk


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


SIGTERM handling

2006-05-05 Thread Manlio Perillo
Hi.

I have tried this script:

from ctypes import cdll, c_int
from signal import SIGTERM, SIGINT
from time import sleep


msvcrt = cdll.LoadLibrary(MSVCR71) # is this the lib to use?
sig = c_int(SIGTERM)
raise_ = getattr(msvcrt, raise)
raise_(sig)
sleep(10)


The problem is that SIGTERM causes the program to exit (without calling
atexit registered functions).

Why?
In the MSDN documentations it is written that SIGTERM is by default
ignored and in the Python documentation for signal module it is written
that there should be no handler for SIGTERM.



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


Re: python-dev Summary for 2006-02-16 through 2006-02-28

2006-04-29 Thread Manlio Perillo
Steven Bethard ha scritto:
 python-dev Summary for 2006-02-16 through 2006-02-28
 
 
 .. contents::
 
 [...]
 
 Translating the Python documentation
 
 
 Facundo Batista had proposed translating the Library Reference and
 asked about how to get notifications when the documentation was
 updated (so that the translations could also be updated). Georg Brandl
 suggested a post-commit hook in SVN, though this would only give
 notifications at the module level. Fredrik Lundh suggested something
 based on his `more dynamic library reference platform`_ so that the
 notifications could indicate particular methods and functions instead.
 

As the new maintainer of python.it (where an huge amount of
documentation has been already translated), I have written some
scripts/modules to help to keep this translation updated.

The scripts are in
http://svn.python.it/admin/trunk/scripts/

with some examples in
http://svn.python.it/admin/branches/updates/examples/

The process is simple.
First with the mark script one can split a new document in N subparts
(using visual marks), for helping translators to focus on small parts.
see:
http://svn.python.it/admin/branches/updates/examples/libdecimal.tex

The main issue is updating the documentation to a new version.
The update-version script will merge changes from old and new versions
of the original documentation to the translated one.
see: http://svn.python.it/admin/branches/updates/examples/emailmessage.tex

I hope this scripts can be useful, I'm thinking of releasing then as a
separate package.




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


pgasync

2006-04-28 Thread Manlio Perillo
Hi.

Someone knows if this project is still alive?


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


Re: Socket Programming HOWTO example

2006-01-17 Thread Manlio Perillo
Steve Holden ha scritto:
 [...]
 I can see you have changed the example a little (because I know that
 Gordon's original didn't have comments in Italian). 

The example cames from italian translation of the howto:
http://python.it/doc/howto/Socket/sockets-it/sockets-it.html




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


Re: different versions for 2.3.4 documentation

2006-01-13 Thread Manlio Perillo
Martin v. Löwis ha scritto:
 Manlio Perillo wrote:
 
I'm only a bit curious, but why documentation from
http://www.python.org/ftp/python/2.3.4/Python-2.3.4.tar.bz2
and
http://www.python.org/ftp/python/doc/2.3.4/latex-2.3.4.tar.bz2
differ?
 
 
 It appears that the latex-* set really comes from the 2.4 branch,
 somehow. That must be a mistake.
 

Well, the same happens for 2.3.5...
And I still don't have checked 2.4.x releases.



Thanks and regards  Manlio Perillo
-- 
http://mail.python.org/mailman/listinfo/python-list


different versions for 2.3.4 documentation

2006-01-12 Thread Manlio Perillo
Regards.

I'm only a bit curious, but why documentation from
http://www.python.org/ftp/python/2.3.4/Python-2.3.4.tar.bz2
and
http://www.python.org/ftp/python/doc/2.3.4/latex-2.3.4.tar.bz2
differ?



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


Re: Placing graphics text on printed page - jan06call.jpg (0/1)

2006-01-12 Thread Manlio Perillo
Michael Galvin ha scritto:
 I am trying to use Python to send to the printer a calender filled
 with a mix of text and simple graphics.  I want to draw on the printed
 page something like a table with 6 rows and 7 columns to represent a
 calendar.  I want to place text precisely within those boxes on the
 printed page. I am using Python 2.4 on Windows XP
 
 I was  in the past able to do this within Visual Basic using its
 printer object.  Visual Basic's printer object uses a coordinate
 system to allow you to draw lines and to place text on the printed
 page precisely. I have attached a file jan06call.jpg to this message
 to illustrate what I am trying to do.
 
 Does Python have a module which would help me do this?
 

You can try to use pycairo for graphics, using postscript as a backend.
Then print the postscript file (with Ghostscript).

Or you can use a more high level tool.



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


email package and line ending

2005-12-23 Thread Manlio Perillo
Regards.

The problem is this code:

  import email.Message
  msg = email.Message.Message()
  msg[subject] = email bug
  msg[from] = Manlio Perillo
  print repr(msg.as_string())
'subject: email bug\nfrom: Manlio Perillo\n\n'


Why line ending is '\n' and not '\r\n' ?
RFC 2822 says that the delimiter must(?) be '\r\n'.

The same problem is presente in email.Generator and email.Header (for 
multiline headers).


P.S.
email.Header has a bug:

this code causes an infinite recursion:

  from email.Header import Header
  h = Header('multiline header', 'iso-8859-1', maxlinelen=4)
  e.encode()




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


questions about locale and Windows

2005-12-23 Thread Manlio Perillo
Regards.

I have a few questions about locale handling on Windows.

The first: why getlocale() returns (None, None) when the locale is 'C'?

The second is: why this code fails?:

  loc, enc = locale.getdefaultlocale()
  print loc, enc
it_IT cp1252
  locale.setlocale(locale.LC_ALL, (loc, enc))

Traceback (most recent call last):
   File pyshell#15, line 1, in -toplevel-
 locale.setlocale(locale.LC_ALL, (loc, enc))
   File C:\Python2.4\lib\locale.py, line 381, in setlocale
 return _setlocale(category, locale)
Error: unsupported locale setting


Fortunately
  locale.setlocale(locale.LC_ALL, '')
'Italian_Italy.1252'

and
locale.setlocale(locale.LC_ALL)
'C'

works...



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


Re: writing a Mail Server

2005-12-14 Thread Manlio Perillo
[EMAIL PROTECTED] ha scritto:
 Hello,
 I have to implement a mail server type program for a project using
 python. I have already implemented smtp and pop3 protocol to send and
 retrieve mail at the client side. I used an existing mail server
 account to test the code. Now, I need to write the functionality of the
 mail server end. My goal is to receive emails from a few accounts, look
 at the to addresses and forward it to different mailboxes. And,
 different users should be able to pick up mail from their mailboxes.
 I am new to python and from what I have learned, I need to use
 SimpleSocketServer. I am not clear about how it will get an email type
 file and then put it in the right mailbox.
 I don't need to do too much fancy address checking, spam filtering etc.
 
 Is there a simple way to approach this? Will really appreciate the
 input.
 Thanks
 

Twisted.


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


Re: strange behaviour when writing a large amount of data on stdout

2005-11-24 Thread Manlio Perillo
On Thu, 24 Nov 2005 05:22:18 GMT, Dennis Lee Bieber
[EMAIL PROTECTED] wrote:

On Wed, 23 Nov 2005 16:51:15 GMT, Manlio Perillo
[EMAIL PROTECTED] declaimed the following in
comp.lang.python:

 So, it's seem to be a specific problem of Windows XP(?).

   Pardon? I think the prior respondent said it /did/ work on WinXP.

   Sure works on my WinXP Pro; 3.4GHz, 2GB RAM -- Even setting 


I have added a question mark...

However: did you have installed SP1 or SP2?




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


Re: strange behaviour when writing a large amount of data on stdout

2005-11-24 Thread Manlio Perillo
On Thu, 24 Nov 2005 10:24:02 +0100, Fredrik Lundh
[EMAIL PROTECTED] wrote:

Manlio Perillo wrote:

 I have added a question mark...

 However: did you have installed SP1 or SP2?

running XP without service packs is a really bad idea.  you're not
telling me that you haven't updated, are you?


I have only installed security patches...

The problem, here, is that when running the bug script with Twisted,
the Python interpreter crashes (and this should never happen).





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


strange behaviour when writing a large amount of data on stdout

2005-11-23 Thread Manlio Perillo
Regards.

On my system:
Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
on win32, Windows XP

I have this problem:

 n = 61409 + 1
 data = 'x' * n

 print data

Traceback (most recent call last):
  File xxx, line xxx, in ?
print data
IOError: [Errno 12] Not enough space


Can someone reproduce this bug?
I begin to think that my system is messed up...



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


Re: strange behaviour when writing a large amount of data on stdout

2005-11-23 Thread Manlio Perillo
On Wed, 23 Nov 2005 14:59:45 +0100, Fredrik Lundh
[EMAIL PROTECTED] wrote:

Manlio Perillo wrote:

 Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]
 on win32, Windows XP

 I have this problem:

 n = 61409 + 1
 data = 'x' * n

 print data

 Traceback (most recent call last):
  File xxx, line xxx, in ?
print data
 IOError: [Errno 12] Not enough space

errno 12 is ENOMEM (that is, the system did not have enough memory
to finish an operation).


...

However I think the error message is not a good one.
Better a Not enough memory

is the above a complete interpreter session?  if it is, why did you xxx out
the filename (stdin) and line number?


I put these instructions in a script and executed it (from the
console).

The same problem with a complete interpreter session.



Regards  Manlio Perillo

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


Re: strange behaviour when writing a large amount of data on stdout

2005-11-23 Thread Manlio Perillo
On Wed, 23 Nov 2005 07:48:30 -0600, [EMAIL PROTECTED] wrote:

That code works here.

Python2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32

It's Windows XP, Pentium 4, unknown amount of RAM.  I'm running python.exe in a
console window.  It also worked in IDLE.

Jeff


Thanks.
So, it's seem to be a specific problem of Windows XP(?).

Nobody can test it on a Windows 98/2000 machine?



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


Re: strange behaviour when writing a large amount of data on stdout

2005-11-23 Thread Manlio Perillo
On Wed, 23 Nov 2005 18:21:45 +0100, Fredrik Lundh
[EMAIL PROTECTED] wrote:

Manlio Perillo wrote:

 So, it's seem to be a specific problem of Windows XP(?).

 Nobody can test it on a Windows 98/2000 machine?

works fine on assorted XP and 2000 boxes for me.


That's very strange.

is the n = 61409 + 1 stuff necessary, or do you get the same
problem for all outputs over a given size ?  even if you just do,
say,

print '*'*61410

?  does print '*'*61409 work?  

It works for n  61410, fails for n = 61410

is it always the same number, or
does it vary depending on, say, what other programs you're using,
how much you've already printed to the console, etc.

Well, if I try with n = 61409 (that works), then it works even with n
= 61410.
But, as an example, with n = 614100, it fails again...

 are you
using a standard cmd.exe in a standard console?  no cygwin or
other oddities?

No.
Only cmd.exe, started with /k chcp.com 1252 
(but is is the same without it).


What a mess!


P.S.
Do you have installed SP1 or SP2 on Windows XP?



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


Re: sqlite utf8 encoding error

2005-11-19 Thread Manlio Perillo
On 18 Nov 2005 09:09:24 -0800, Greg Miller [EMAIL PROTECTED]
wrote:

Thank you for all your suggestions.  I ended up casting the string to
unicode prior to inserting into the database.  


Don't do it by hand if it can be done by an automated system.

Try with:

from pysqlite2 import dbapi2 as sqlite

def adapt_str(s):
# if you have declared this encoding at begin of the module
return s.decode(iso-8859-1) 

sqlite.register_adapter(str, adapt_str)


Read pysqlite documentation for more informations:
http://initd.org/pub/software/pysqlite/doc/usage-guide.html



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


Re: Accessing Python parse trees

2005-03-05 Thread Manlio Perillo
On 3 Mar 2005 11:15:28 -0800, Lonnie Princehouse
[EMAIL PROTECTED] wrote:

No.  I don't think it's possible to read the parse tree used by the
interpreter, especially as it is being created. Here are a couple of
kludgy ideas that might come close, though:

Is this a 'limitation' of the current version or it is impossible for
the architecture of CPython?
What about pypy?


On a side note, check out the compiler module.  You might find it to be
friendlier and more useful than parser.

Thanks for the hint. It is what I want.
Unfortunately is seem to be not well documented.


Anyway, here is an example of what I would like to do:

#begin
def foo(**kwargs): print kwargs

foo(a = 1, b = 2, c = 3)
#end


In the current implementation kwargs is a dict, but I need to have the
keyword argument sorted.
Unfortunately subclassing fron dict and installing the class in the
__builtin__ module (with the name 'dict') does not work, CPython uses
only builtin types.

With the compiler module I can obtain the keyword arguments in the
order the were specified.
The problem is how to do this for every call to foo!




Thanks and regards   Manlio Perillo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Accessing Python parse trees

2005-03-05 Thread Manlio Perillo
On Sat, 05 Mar 2005 08:52:38 -0500, Kent Johnson [EMAIL PROTECTED]
wrote:

Manlio Perillo wrote:
 Anyway, here is an example of what I would like to do:
 
 #begin
 def foo(**kwargs): print kwargs
 
 foo(a = 1, b = 2, c = 3)
 #end
 
 
 In the current implementation kwargs is a dict, but I need to have the
 keyword argument sorted.
 Unfortunately subclassing fron dict and installing the class in the
 __builtin__ module (with the name 'dict') does not work, CPython uses
 only builtin types.
 
 With the compiler module I can obtain the keyword arguments in the
 order the were specified.
 The problem is how to do this for every call to foo!

Why not just pass the kind of argument you want? What is it you really need to 
do?

def foo(kwds): print kwds

foo(MyDict(a = 1, b = 2, c = 3))

Kent

I don't understand your code.
Here an example using OrderedDict from twisted:

 import twisted.python.util as util

 foo(util.OrderedDict(a = 1, b = 2, c = 3))
{'a': 1, 'c': 3, 'b': 2}


Simply I can't use a dict.

I have to do, as an example example:
foo('a', 1, 'b', 2, 'c', 3)

or

foo(['a', 'b', 'c'], [1, 2, 3])


Thanks and regards   Manlio Perillo


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


Re: Regular Expressions: large amount of or's

2005-03-03 Thread Manlio Perillo
On Tue, 1 Mar 2005 15:03:50 -0500, Tim Peters [EMAIL PROTECTED]
wrote:

[André Søreng]
 Given a string, I want to find all ocurrences of
 certain predefined words in that string. Problem is, the list of
 words that should be detected can be in the order of thousands.

 With the re module, this can be solved something like this:

 import re

 r = re.compile(word1|word2|word3|...|wordN)
 r.findall(some_string)

 Unfortunately, when having more than about 10 000 words in
 the regexp, I get a regular expression runtime error when
 trying to execute the findall function (compile works fine, but slow).

 I don't know if using the re module is the right solution here, any
 suggestions on alternative solutions or data structures which could
 be used to solve the problem?

Put the words you're looking for into a set (or as the keys of a dict
in older Pythons; the values in the dict are irrelevant).

I don't know what you mean by word, so write something that breaks
your string into what you mean by words.  Then:

for word in something_that_produces_words(the_string):
if word in set_of_words:
# found one



I have the same problem.
Unfortunately the meaning of a word depends on the word.
As an example I would like to count the number of occurrences of
movies titles in some text.

Maybe lex is more optimized?
Unfortunately is seems that there are no lex versions that generate
python (or PyRex) code.



Thanks and regards   Manlio Perillo

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


Accessing Python parse trees

2005-03-03 Thread Manlio Perillo
Hi.
With module parser it is possible to access Python parse trees.
But this only works for 'external' source.
I would like to known if, at least in theory, it can be possible to
access Python parse trees from 'inside' a script.


As a simple example:


def on_parsing(ast):
 ...


@parsing - on_parsing
some statement



This will call on_parsing function with the AST object generated from
the next statement:

on_parsing(parser.suit(somestatement))



Thanks and regards  Manlio Perillo

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


Re: Regular Expressions: large amount of or's

2005-03-03 Thread Manlio Perillo
Hi.

Python allows to subclass builtin classes but the Python Interpreter
uses builtin types.
As an example keyword arguments are inserted in a dict but I would
like to use an user defined SortedDict.

There are plans to add such a feature in a future version?


Thanks and regards  Manlio Perillo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Regular Expressions: large amount of or's

2005-03-03 Thread Manlio Perillo
Hi.

Python allows to subclass builtin classes but the Python Interpreter
uses builtin types.
As an example keyword arguments are inserted in a dict but I would
like to use an user defined SortedDict.

There are plans to add such a feature in a future version?


Thanks and regards  Manlio Perillo
-- 
http://mail.python.org/mailman/listinfo/python-list