HOWTO: Parsing email using Python part1

2011-07-03 Thread aspineux
Hi
I have written an article about parsing email using Python.
The article is at http://blog.magiksys.net/Parsing-email-using-python-header
and the full content is here.

Hope this help someone.

Regards.


A lot of programs and libraries commonly used to send emails
don't comply with RFC. Ignore such kind of email is not an option
because all
mails are important. It is important to do it best when parsing
emails, like
does most popular MUA.

Python's has one of the best library to parse emails: the
email package.
First part, how to decode mails header

Regarding RFC 2047
non ascii text in the header must be encoded.
RFC 2822 make the difference
between different kind of header. *text field like
Subject: or address fields like
To:, each with different encoding rules.
This is because RFC 822
forbids the use of some ascii characters at some place because
they have some meaning, but these ascii characters can be used when
they are encoded
because the encoded version don't disturb the parsing of string.

Python provides email.Header.decode_header() for decoding header.
The function decode each atom and return a list of tuples
( text, encoding ) that you still have to decode and join to get
the full text. This is done in my getmailheader() function.

For addresses, Python provides email.utils.getaddresses()
that split addresses in a list of tuple ( display-name, address ).
display-name need to be decoded too and addresses must match
the RFC2822 syntax. The function getmailaddresses() does
all the job.

Here are the functions in actions.

import re
import email
from email.Utils import parseaddr
from email.Header import decode_header

# email address REGEX matching the RFC 2822 spec
# from perlfaq9
#my $atom   = qr{[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+};
#my $dot_atom   = qr{$atom(?:\.$atom)*};
#my $quoted = qr{"(?:\\[^\r\n]|[^\\"])*"};
#my $local  = qr{(?:$dot_atom|$quoted)};
#my $domain_lit = qr{\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\]};
#my $domain = qr{(?:$dot_atom|$domain_lit)};
#my $addr_spec  = qr{$local\@$domain};
#
# Python translation

atom_rfc2822=r"[a-zA-Z0-9_!#\$\%&'*+/=?\^`{}~|\-]+"
atom_posfix_restricted=r"[a-zA-Z0-9_#\$&'*+/=?\^`{}~|\-]+" # without
'!' and '%'
atom=atom_rfc2822
dot_atom=atom  +  r"(?:\."  +  atom  +  ")*"
quoted=r'"(?:\\[^\r\n]|[^\\"])*"'
local="(?:"  +  dot_atom  +  "|"  +  quoted  +  ")"
domain_lit=r"\[(?:\\\S|[\x21-\x5a\x5e-\x7e])*\]"
domain="(?:"  +  dot_atom  +  "|"  +  domain_lit  +  ")"
addr_spec=local  +  "\@"  +  domain

email_address_re=re.compile('^'+addr_spec+'$')

raw="""MIME-Version: 1.0
Received: by 10.229.233.76 with HTTP; Sat, 2 Jul 2011 04:30:31 -0700
(PDT)
Date: Sat, 2 Jul 2011 13:30:31 +0200
Delivered-To: [email protected]
Message-ID: 
Subject: =?ISO-8859-1?Q?Dr.=20Pointcarr=E9?=
From: Alain Spineux 
To: =?ISO-8859-1?Q?Dr=2E_Pointcarr=E9?= 
Content-Type: multipart/alternative;
boundary=000e0cd68f223dea3904a714768b

--000e0cd68f223dea3904a714768b
Content-Type: text/plain; charset=ISO-8859-1

--
Alain Spineux

--000e0cd68f223dea3904a714768b
Content-Type: text/html; charset=ISO-8859-1



--
Alain Spineux


--000e0cd68f223dea3904a714768b--
"""

def getmailheader(header_text, default="ascii"):
"""Decode header_text if needed"""
try:
headers=decode_header(header_text)
except email.Errors.HeaderParseError:
# This already append in email.base64mime.decode()
# instead return a sanitized ascii string
return header_text.encode('ascii', 'replace').decode('ascii')
else:
for i, (text, charset) in enumerate(headers):
try:
headers[i]=unicode(text, charset or default,
errors='replace')
except LookupError:
# if the charset is unknown, force default
headers[i]=unicode(text, default, errors='replace')
return u"".join(headers)

def getmailaddresses(msg, name):
"""retrieve From:, To: and Cc: addresses"""
addrs=email.utils.getaddresses(msg.get_all(name, []))
for i, (name, addr) in enumerate(addrs):
if not name and addr:
# only one string! Is it the address or is it the name ?
# use the same for both and see later
name=addr

try:
# address must be ascii only
addr=addr.encode('ascii')
except UnicodeError:
addr=''
else:
# address must match adress regex
if not email_address_re.match(addr):
addr=''
addrs[i]=(getmailheader(name), addr)
return addrs

msg=email.message_from_string(raw)
subject=getmailheader(msg.get('Subject', ''))
from_=getmailaddresses(msg, 'from')
from_=('', '') if not from_ else from_[0]
tos=getmailaddresses(msg, 'to')

print 'Subject: %r' % subject
print 'From: %r' % (from_, )
print 'To: %r' % (tos, )

And the ouput:

Subject: u'Dr. Pointcarr\xe9'
From: (u'Alain Spineux', '[email protected]')
To: [(u'Dr. Pointcarr\xe9', 'ala

generate and send mail with python: tutorial

2011-08-11 Thread aspineux
Hi I have written a tutorial about how to generate and send emails
with python.

You can find it here 
http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial

And here is the content. Enjoy.

This article follows two other articles (1, 2) about how to parse
emails in Python.
These articles describe very well mail usages and rules and can be
helpful for non Python developer too.

The goal here is to generate a valid email including internationalized
content, attachments and even inline images, and send it to a SMTP
server.

The procedure can be achieved in 3 steps :

compose the body of the email.
compose the header of the email.
send the email to a SMTP server

At the end you will find the sources.

The first rule to keep in mind all the time, is that emails are 7bits
only, they can contains us-ascii characters only ! A lot of RFCs
define rules to encode non us-ascii characters when they are required:
RFC2047 to encode headers, RFC2045 for encoding body using the MIME
format or RFC2231 for MIME parameters like the attachment filename.
The mail header

The header is composed of lines having a name and a value. RFC2047 let
you use quoted or binary encoding to encode non us-ascii characters in
it.
For example:

Subject: Courrier électronique en Français

become using the quoted format

Subject: =?iso-8859-1?q?Courrier_=E8lectronique_en_Fran=E7ais?=

or using the binary one.

Subject: =?iso-8859-1?b?Q291cnJpZXIg6GxlY3Ryb25pcXVlIGVuIEZyYW7nYWlz?=

Here the quoted format is readable and shorter. Python
email.header.Header object generates user friendly header by choosing
the quoted format when the encoding shares characters with us-ascii,
and the binary one for encoding like the Chinese big5 that requires
the encoding of all characters.

>>> str(Header(u'Courrier \xe8lectronique en Fran\xe7ais', 'iso-8859-1'))
'=?iso-8859-1?q?Courrier_=E8lectronique_en_Fran=E7ais?='

Header values have different types (text, date, address, ...) that all
require different encoding rules. For example, in an address like :

Sender: Alain Spineux 

The email part  has to be in us-ascii and
cannot be encoded. The name part cannot contains some special
characters without quoting : []\()<>@,:;".
We can easily understand why "<>" are in the list, others have all
their own story.

For example, the use of the "Dr." prefix requires to quote the name
because of the '.':

Sender: "Dr. Alain Spineux" 

For a name with non us-ascii characters like (look at the "ï" in
Alaïn), the name must be encoded.

Sender: Dr. Alïan Spineux 

must be written :

Sender: =?iso-8859-1?q?Dr=2E_Ala=EFn_Spineux?=


Notice that the '.' in the prefix is replaced by "=2E", because Header
preventively encode all non alpha or digit characters to match the
most restrictive header rules.

The Python function email.utils.formataddr() quotes the special
characters but don't encode non us-ascii characters. On the other
hand, email.header.Header can encode non us-ascii characters but
ignore all specials rule about address encoding.
Let see how both work:

>>> email.Utils.formataddr(('Alain Spineux', '[email protected]'))
'Alain Spineux '

This is a valid header value for a To: field

>>> str(Header('Dr. Alain Spineux '))
'Dr. Alain Spineux '

Here the '.' should be escaped like these 13 characters: []\()<>@,:;".

>>> email.Utils.formataddr(('"Alain" Spineux', '[email protected]'))
'"\\"Alain\\" Spineux" '

Here '"' is escaped using '\', this is fine.

>>> email.Utils.formataddr((u'Ala\xefn Spineux', '[email protected]'))
u'Ala\xefn Spineux '

formataddr() don't handle non us-ascii string, this must be done by
Header object

>>> str(Header(email.Utils.formataddr((u'Ala\xefn Spineux', 
>>> '[email protected]'
'=?utf-8?q?Ala=C3=AFn_Spineux_=3Calain=2Espineux=40gmail=2Ecom=3E?='

This is not valid because the address is also encoded and an old or
some recent MUA will't handle this. The good form here is :

=?utf-8?q?Ala=C3=AFn_Spineux?= '

Function format_addresses(addresses, header_name=None, charset=None)
handle carefully the encoding of the addresses.

>>> str(format_addresses([ (u'Ala\xefn Spineux', '[email protected]'), 
>>> ('John', '[email protected]'), ], 'to', 'iso-8859-1'))
'=?iso-8859-1?q?Ala=EFn_Spineux?=  ,\n John
'

Bytes and unicode string can be mixed. Addresses must always be us-
ascii. Byte string must be encoded using charset or be us-ascii.
Unicode strings that cannot be encoded using charset will be encoded
using utf-8 automatically by Header.

For dates, use the email.utils.formatdate() this way.

>>> email.utils.formatdate(time.time(), localtime=True)
'Wed, 10 Aug 2011 16:46:30 +0200'

The mail body

Depending of your need :

text and/or html version of the message
related inline images
attached files

the structure of the MIME email may vary, but the general one is as
follow:

multipart/mixed
 |
 +-- multipart/related
 ||
 |+-- multipart/alternative
 |||
 ||+-- t

Re: generate and send mail with python: tutorial

2011-08-12 Thread aspineux
On Aug 12, 8:51 am, Dennis Lee Bieber  wrote:
> On Thu, 11 Aug 2011 08:07:09 -0700 (PDT), aspineux 
> declaimed the following in gmane.comp.python.general:
>
> > Hi I have written a tutorial about how to generate and send emails
> > with python.
>
>         Is that really such a difficult task?

Yes it's difficult to make an _universal_ mail parser, able to handle
mails from different MUA, using different charsets.
Some emails that break RFC's requires at least some background
to know which rules can be missed by some MUA !

Handle mail from a unique source or similar sources is easy.
Using try and error method is fine for this kind of job, you don't
even need to care if the email you're parsing match the RFC's or not.

My "parser" functions have been tested over 20.000 mails from all
around the world using some human and automated verifications !

Generating emails is not easy, when mixing internationalized
addresses,
subject and content. Integrating text, html, attachment and embedded
images in one single email looks to me difficult enough to require
some explanations.

At least read carefully all my articles to get an idea about the
difficulty,
this is what I try to explain.

Regards.


>
>         Okay, I didn't really use Python for the entire creation of the
> message... But my first real Python program (using 1.4 or 1.5, whatever
> was included in the first "Programming Python" disk, for AmigaOS) was a
> rudimentary outgoing SMTPd which had to parse headers from  message
> files "queued" by an ARexx script from an Amiga version of ELM, then
> handshake with the ISP SMTPd to relay the message onward. It took less
> than a week after buying the book that I had this ARexx/Python hybrid
> working -- and it worked better than either C-language programs I'd
> downloaded (at that period of time, Amiga email programs ONLY read from
> local spool and queued to local spool; separate programs had to be used
> to read POP3 and send SMTP... My first SMTP utility presumed 1) direct
> connection to destination address, 2) created an email file for each
> address -- problem: if a destination did not have a receiving SMTPd [ie,
> one needed to do an MX lookup instead] it would hang trying to send that
> message, and never process others... The second program used ISP relay
> -- but it only parsed the "TO:" header, and thereby failed to handshake
> CC and BCC destinations)
> --
>         Wulfraed                 Dennis Lee Bieber         AF6VN
>         [email protected]    HTTP://wlfraed.home.netcom.com/

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


Re: fork problem

2011-08-12 Thread aspineux
On Aug 12, 4:18 pm, "守株待兔" <[email protected]> wrote:
> in the book ,A call to wait() suspends execution (i.e., waits) until a child 
> process (any child process) has completed, terminating either normally or via 
> a signal. wait() will then reap the child, releasing any resources. If the 
> child has already completed, then wait() just performs the reaping procedure.
> here  is my code
> import os
> print "i am parent ",os.getpid()
> ret  =  os.fork()
> print  "i am here",os.getpid()
> if  ret  ==  0:
>  os.system('ls')
> else:
> os.wait()
> print "i am runing,who am i? ",os.getpid()
>
> according to the word,the output may be:
> i am parent  8014
> i am here 8014
> i am here 8015
> "omitted my file"
> i am runing,who am i?  8014
> because 8015  is terminated by os.wait().
>
> in fact the output is:
>
> i am parent  8014
> i am here 8014
> i am here 8015
> "omitted my file"
> i am runing,who am i?  8015
> i am runing,who am i?  8014
>
> i want to know why ??

To get what you want, use os.exec() instead of os.system()
or add a os.exit() just after os.system()

Regards

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


Re: generate and send mail with python: tutorial

2011-08-12 Thread aspineux
On Aug 12, 3:38 pm, Steven D'Aprano  wrote:
> aspineux wrote:
> > Hi I have written a tutorial about how to generate and send emails
> > with python.
>
> [...]
>
> Thank you, that is an extremely detailed tutorial.

Thanks,  It was my intention

Alain

>
> --
> Steven

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


Re: thread and process

2011-08-13 Thread aspineux
On Aug 13, 11:09 am, "守株待兔" <[email protected]> wrote:
> please see my code:
> import os
> import  threading
> print  threading.currentThread()  
> print "i am parent ",os.getpid()
> ret  =  os.fork()
> print  "i am here",os.getpid()
> print  threading.currentThread()
> if  ret  ==  0:
>  print  threading.currentThread()
> else:
> os.wait()
> print  threading.currentThread()
>
> print "i am runing,who am i? ",os.getpid(),threading.currentThread()
>
> the output is:
> <_MainThread(MainThread, started -1216477504)>
> i am parent  13495
> i am here 13495
> <_MainThread(MainThread, started -1216477504)>
> i am here 13496
> <_MainThread(MainThread, started -1216477504)>
> <_MainThread(MainThread, started -1216477504)>
> i am runing,who am i?  13496 <_MainThread(MainThread, started -1216477504)>
> <_MainThread(MainThread, started -1216477504)>
> i am runing,who am i?  13495 <_MainThread(MainThread, started -1216477504)>
> it is so strange that  two  different  processes  use one  mainthread!!

You should not mix thread and fork.

Some hint :

You put your "import threading" before your fork(), then data
initialized by the import
are the same in the two process then it display the same, this is like

a=-1216477504
os.fork()
print a

second I thing -1216477504 has no meaning, this is not a system thread
ID but just an ID generated by python
I think they must be unique inside a process but not cross process.
Then 2 process can have the same python thread ID.

If you have to mix thread and fork try to find some hints from
Internet.
Something like don't fork a process that already has tread(),
or try to keep all your threads inside the same process ...

Regards





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


Re: generate and send mail with python: tutorial

2011-08-13 Thread aspineux
On Aug 13, 3:00 am, Ben Finney  wrote:
> Ben Finney  writes:
> > What is the process if the OP, or someone to whom the OP delegates
> > authority, wants to [contribute their work to the Python
> > documentation]?
>
> The answer is partly at http://docs.python.org/documenting/>:
>
>     If you’re interested in contributing to Python’s documentation […]
>     Send an e-mail to [email protected] or open an issue on the tracker.
>
> One should, before doing so, follow the above document on the
> documentation style conventions for Python.

I'm using python for long now, and just discovered these HowTo today :-
(
I don't thing rewriting these articles  into another format
will improve the "message". I will not rewrite them.

You are free to do it, just keep my name as the original author.
I have no other original source than the HTML one you can have on my
blog.

I appreciate your interest for my work.

I think to put all the sources together to create a library.
I thing about the name of pyezmail for "python easy mail".
Any comment ?

Regards

>
> --
>  \      “Contentment is a pearl of great price, and whosoever procures |
>   `\        it at the expense of ten thousand desires makes a wise and |
> _o__)                                      happy purchase.” —J. Balguy |
> Ben Finney

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


Re: Messed up Mac installation

2011-08-16 Thread aspineux
On Aug 16, 12:03 pm, Christopher Brewster  wrote:
> Are there instructions somewhere online as to how to sort out a messed
> up installation of Python on a Mac?
> I have a 2 month old Macbook running Snow Leopard, I installed Python
> 2.7.1 some time ago. Now when I run 'python' this is what it shows.
> But easy_install and pip are installing modules to the python2.6
> (which shipped with the OS). If I run 'python2.6' then I have access
> to the modules installed with pip.

pip and easy_install work on directory hierarchy of the running
python.

then  the idea (just the idea, not read sample)

python2.7 pip.py  will install into python2.7 directories
python2.7 easy_install.py  will install into python2.7 directories

python2.6 pip.py  will install into python2.6 directories
python2.6 easy_install.py  will install into python2.6 directories


For example (real this time) to install easy_install for your 2.7

wget http://peak.telecommunity.com/dist/ez_setup.py
python2.7 ez_setup.py


>
> Oh and weirdly if I tell pip to uninstall modules it says the module
> does not exist. For example: "/Library/Frameworks/Python.framework/
> Versions/2.7/bin/pip run on Tue Aug 16 11:46:28 2011
> Cannot uninstall requirement reportlab, not installed"

Probably because you run installed pip from python2.7 directory, that
certainly use python2.7 binary,
and then pip uninstall from python2.7 directories

>
> So why is it installing to one place (the 2.6 library) and trying to
> uninstall elsewhere (the 2.7 library)?

Because you are no using the same pip/easy_install to install or
uninstall (or at least the python binary is not the same)

>
> I know this has been discussed many times but I do not know how to
> track this down.
> Thanks for any pointers.
>
> Christopher

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


Re: Idea for pure-python templates using AST.

2011-08-16 Thread aspineux
On Aug 16, 1:33 pm, "Paul Wray"  wrote:
> Hello all
>
> Ive had what I think is a great idea for pure-python templates (I can almost
> hear the groans, bear with me...)
>
> For the impatient, proof of concept is athttp://pastie.org/2379978
> demonstrating simple substitution, balanced tags using context manager,
> subtemplates,  and template inheritance.

You code fail, see below for other comment

Traceback (most recent call last):
  File "Download/pastie-2379978.rb", line 108, in 
make_template(template1)
  File "Download/pastie-2379978.rb", line 60, in make_template
ast.fix_missing_locations(astFromSrc)
  File "/usr/lib/python2.6/ast.py", line 133, in fix_missing_locations
_fix(node, 1, 0)
  File "/usr/lib/python2.6/ast.py", line 132, in _fix
_fix(child, lineno, col_offset)
  File "/usr/lib/python2.6/ast.py", line 132, in _fix
_fix(child, lineno, col_offset)
  File "/usr/lib/python2.6/ast.py", line 121, in _fix
if 'lineno' in node._attributes:
AttributeError: 'arguments' object has no attribute '_attributes'




>
> I'm posting here to get opinions on:
> * the merits of the idea, (or otherwise!)
> * whether there are any established/mature templating systems that use this
> approach, or whether its come up before,
> * ideas for improvements and advice on other aspects such as sandboxing
> * of course, to share the idea in case others want to use it

This is very original ! First time I see it. I like it.
But how to debug large template ?
How to find/detect a missing  ?
This is very important. This is one big advantage of Genshi over Kid
How to report the correct error at the correct line ?
How to find/get nice editor to edit large template ?


>
> Background: Ive been working on an application that recursively renders
> html/xml documents from heterogenoeus trees, with separate classes for each
> document component. First I built my own renderer but was dissatisfied with
> the repetitive code. Then looked at Mako and Jinja, and used Jinja but was
> still disatisfied, because I still needed a render method in each class to
> do preparation, and also the template which was centrally managed by the
> Jinja loader and environment. I found a way to call templates recursively
> via Jinja filters, but was not sure that it wouldnt blow up in my face, so I
> also had separate calls to render the children of each node, and then feed
> the value to the parent template. I kept thinking that there must be a way
> to get the brevity and clarity of declarative templates, plus the simplicity
> of pure python loops, tests and function calls.
>
> The idea:
> Python syntax allows a statement to be a bare literal or identifier. These
> have no effect on the program.
>
> So the function below is legal python:
>
> def myFunc():
>     'a'
>     x = 45
>     'b'; 'c'; x
>
> So is this (within the appropriate class context of course):
>
> def body(self, r):
>         ''; self.heading; ''
>         ''
>         for itm in self.items:
>             ''; itm; ''
>         ''
>
> The idea is simply to use python ASTs to transform this code so that it
> accumulates the values of the bare expressions.

You could call it PHP :-)

>
> I think this give the best of both worlds - declarative syntax for the
> template literals, but in a pure python context, giving you all the power of
> python control statements, classes etc.
>
> For application contexts outside pure python code (ie user-created
> templates) , it would be simple to extend the technique to creating template
> functions from strings, and insert the resulting methods into a namespace
> for execution.)
>
> I think, given the technique is already working with the AST, that
> sandboxing should not be too hard either - advice on this aspect would be
> appreciated.


Maybe a good idea.
But still a lot of work to get the level of existing libraries.

Maybe better if mixed with other tools like Genshi to create widget.
Maybe next toscawidget could use such a technique.


Thanks for sharing

>
> Paul Wray

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


Re: How to install easy_install on windows ?

2011-08-16 Thread aspineux
On Aug 16, 2:17 pm, smith jack  wrote:
> it needs read registry, but the python i used is extracted from .zip,
> so there is no record in the registry,

Noone need it, Python dont use any registry key

> what should i do in order to install easy_install for my python environment?

get ez_setup.py from http://peak.telecommunity.com/dist/ez_setup.py

in a command prompt run
C:\Your Python Directory\python.exe  C:\Your Download directory
\ez_setup.py

Then use

C:\Your Python Directory\python.exe C:\Your Python Directory\I dont
know where the easy_install script will be installed\easy_install.py
install module_name

to install a modul_name

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


Re: Windows service in production?

2011-08-16 Thread aspineux
On Aug 16, 6:32 am, snorble  wrote:
> Anyone know of a Python application running as a Windows service in
> production? I'm planning a network monitoring application that runs as
> a service and reports back to the central server. Sort of a heartbeat
> type agent to assist with "this server is down, go check on it" type
> situations.

I don't use service for my own python applications, even standalone
py2exe applications.
I use the Windows taskscheduler that start it once and try every
minute to start it again
but is is running all the time, and the the scheduler does nothing and
wait to see for the next minute.
When the python program crash then the scheduler restart it in the
minute.

look here how I do with tcpproxyreflector:
http://blog.magiksys.net/software/tcp-proxy-reflector#tcpr_service

In My MKSBackup  http://blog.magiksys.net/mksbackup-quick-overview
I have a install() function that create the task in the scheduler for
XP and Windows2008

And this code, use a 2 min interval


print 'create task %s in scheduler' % (task_name, )
interval=raw_input_with_default('Enter interval in minutes',
'>', str(interval_in_minutes))
cmd='"%s" -c "%s"' % (os.path.join(install_target,
os.path.basename(sys.argv[0])), os.path.join(target, ini_file))
schtasks_cmd=[ 'SCHTASKS', '/Create', '/SC', 'MINUTE', '/MO',
interval, '/TN', task_name, '/RU', os.getenv('USERNAME'), '/TR', cmd ]
if sys.getwindowsversion()[0]>5:
# under 2008 ? require HIGHEST privilege
# schtasks_cmd.insert(2, 'HIGHEST')
# schtasks_cmd.insert(2, '/RL')
# under 2008, to force the system to ask for the password,
set empty password
i=schtasks_cmd.index('/RU')+2
schtasks_cmd.insert(i, '')
schtasks_cmd.insert(i, '/RP')
else:
pass


Mix all 3 source to get what you want.

Services are probably fine too, but this is another story.


>
> If using Visual Studio and C# is the more reliable way, then I'll go
> that route. I love Python, but everything I read about Python services
> seems to have workarounds ahoy for various situations (or maybe that's
> just Windows services in general?). And there seem to be multiple
> layers of workarounds, since it takes py2exe (or similar) and there
> are numerous workarounds required there, depending on which libraries
> and functionality are being used. Overall, reading about Windows
> services in Python is not exactly a confidence inspiring experience.
> If I knew of a reference example of something reliably running in
> production, I'd feel better than copying and pasting some code from a
> guy's blog.

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


extend class: include factories functions inside constructor

2011-08-18 Thread aspineux
Hi
I have a closed class and 2 factories function

class Data:
  def __init__(self):
self.payload=None

def data_from_string(payload):
  data=Data()
  data.payload=payload
  return data

def data_from_file(f):
  data=Data()
  data.payload=f.read()
  return data

And I want to extend the class, by first including the factories
function inside the constructor,
second add some method to the class.

class MyData(Data):

  def __init__(self, arg):
# I know this coke is not working, this is to show you
# what I expect
if isinstance(arg, file):
self=data_from_file(arg)
else:
self=data_from_string(arg)
return self

  def len(self):
return len(self.payload)

And how I want to use it

>>> m=MyData('hello')
>>> print m.len()
5

any idea ?

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


Re: extend class: include factories functions inside constructor

2011-08-18 Thread aspineux
On Aug 18, 4:45 pm, Peter Otten <[email protected]> wrote:
> aspineux wrote:
> > Hi
> > I have a closed class and 2 factories function
>
> > class Data:
> >   def __init__(self):
> >     self.payload=None
>
> > def data_from_string(payload):
> >   data=Data()
> >   data.payload=payload
> >   return data
>
> > def data_from_file(f):
> >   data=Data()
> >   data.payload=f.read()
> >   return data
>
> > And I want to extend the class, by first including the factories
> > function inside the constructor,
> > second add some method to the class.
>
> > class MyData(Data):
>
> >   def __init__(self, arg):
> >     # I know this coke is not working, this is to show you
> >     # what I expect
> >     if isinstance(arg, file):
> >         self=data_from_file(arg)
> >     else:
> >         self=data_from_string(arg)
> >     return self
>
> >   def len(self):
> >     return len(self.payload)
>
> > And how I want to use it
>
> >>>> m=MyData('hello')
> >>>> print m.len()
> > 5
>
> > any idea ?
>
> Assuming that Data is an oldstyle class you can either copy the Data
> instance's state
>
> class MyData(Data):
>     def __init__(self, arg):
>         if hasattr(arg, "read"):
>             factory = data_from_file
>         else:
>             factory = data_from_string
>         obj = factory(arg)
>         self.__dict__.update(obj.__dict__)
>
>     def __len__(self):
>         return len(self.payload)
>
> or use another factory function and manually set the __class__:

Exactly what I was looking for

The magic is in  self.__dict__.update(obj.__dict__) :-)


>
> class MyData(Data):
>     def __len__(self):
>         return len(self.payload)
>
> def make_mydata(arg):
>     if hasattr(arg, "read"):
>         factory = data_from_file
>     else:
>         factory = data_from_string
>     obj = factory(arg)
>     obj.__class__ = MyData
>     return obj
>

I don't want to replace 2 factories by one !

> If Data were a newstyle class you could move the hand-made polymorphism into
> the __new__() method:
>
> class MyData(Data):
>     def __new__(class_, arg):
>         if hasattr(arg, "read"):
>             factory = data_from_file
>         else:
>             factory = data_from_string
>         obj = factory(arg)
>         obj.__class__ = class_
>         return obj
>     def __init__(self, arg):
>         pass
>     def __len__(self):
>         return len(self.payload)

Very nice, I wahad no solution for newer style class. Thanks
>
> if __name__ == "__main__":
>     m = MyData("hello")
>     print len(m)
>



> Nothing of the above strikes me as pretty. Perhaps we could come up with a
> neater solution if you tell us more about your use-case...

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


Re: extend class: include factories functions inside constructor

2011-08-18 Thread aspineux
On Aug 18, 4:45 pm, Peter Otten <[email protected]> wrote:
> aspineux wrote:
> > Hi
> > I have a closed class and 2 factories function
>
> > class Data:
> >   def __init__(self):
> >     self.payload=None
>
> > def data_from_string(payload):
> >   data=Data()
> >   data.payload=payload
> >   return data
>
> > def data_from_file(f):
> >   data=Data()
> >   data.payload=f.read()
> >   return data
>
> > And I want to extend the class, by first including the factories
> > function inside the constructor,
> > second add some method to the class.
>
> > class MyData(Data):
>
> >   def __init__(self, arg):
> >     # I know this coke is not working, this is to show you
> >     # what I expect
> >     if isinstance(arg, file):
> >         self=data_from_file(arg)
> >     else:
> >         self=data_from_string(arg)
> >     return self
>
> >   def len(self):
> >     return len(self.payload)
>
> > And how I want to use it
>
> >>>> m=MyData('hello')
> >>>> print m.len()
> > 5
>
> > any idea ?
>
> Assuming that Data is an oldstyle class you can either copy the Data
> instance's state
>
> class MyData(Data):
>     def __init__(self, arg):
>         if hasattr(arg, "read"):
>             factory = data_from_file
>         else:
>             factory = data_from_string
>         obj = factory(arg)
>         self.__dict__.update(obj.__dict__)
>
>     def __len__(self):
>         return len(self.payload)
>
> or use another factory function and manually set the __class__:
>
> class MyData(Data):
>     def __len__(self):
>         return len(self.payload)
>
> def make_mydata(arg):
>     if hasattr(arg, "read"):
>         factory = data_from_file
>     else:
>         factory = data_from_string
>     obj = factory(arg)
>     obj.__class__ = MyData
>     return obj
>
> If Data were a newstyle class you could move the hand-made polymorphism into
> the __new__() method:
>
> class MyData(Data):
>     def __new__(class_, arg):
>         if hasattr(arg, "read"):
>             factory = data_from_file
>         else:
>             factory = data_from_string
>         obj = factory(arg)
>         obj.__class__ = class_
>         return obj
>     def __init__(self, arg):
>         pass
>     def __len__(self):
>         return len(self.payload)
>
> if __name__ == "__main__":
>     m = MyData("hello")
>     print len(m)
>
> Nothing of the above strikes me as pretty. Perhaps we could come up with a
> neater solution if you tell us more about your use-case...

Ops, I clicked send before to finish my post.

Thanks for your quick and very valuable answer.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Word Perfect integration

2011-08-19 Thread aspineux
On Aug 18, 7:00 pm, Ethan Furman  wrote:
> I have WordPerfect v13 which we are currently using for letter merges.
> I would like to automate this with Python instead of learning the WP
> Macro language.
>
> Does anyone have any pointers?
>

paper letter or eletronic mail merger ?

What you need is :

- 1. write a "template" in WP with tag like   
- 2. make a database with the corresponding data
- 3. replace tag by data from a database, and generate a new WP
document
- 4. print all these ducument.

1 & 2 are not programming related
3. Should not be impossible, look at the wp binary file if you can
find and replace the tag
4. More difficult: can you start a print job from a command line ? or
put all file in
a directory, then start WP and ask him to print all file in this
directory or create print job and
put them in a queue and hope WP will process the queue.

Regards

> ~Ethan~

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


Re: How to make statements running in strictly sequential fashion like in an interactive shell

2011-08-19 Thread aspineux
On Aug 19, 5:00 pm, lzlu123  wrote:
> I have an instrument that has a RS232 type serial comm port and I need
> to connect to and control. I use Python 3.2 in Windows XP, plus
> pySerial module. I have a problem when I execute a script consisting
> of statements that open the comm port, configure it, write strings to
> and receive strings from it. Thoese strings aer either commands
> pertinent to the instrument (control) or responses from the instrument
> (response).
>
> When those statements are executed in a python interpreter
> interactively (at >>>), I get what I expect and the results are good
> and correct. However, when I execute the script, either being invoked
> within the interpreter or run file, I don’t get what I want. The
> statements in the script is the same as what I use in the interactive
> interpreter.
>
> Why do I get the strange behavior and how can I change the script to
> make it to behave like in interactive interpreter?
>
> --script---
> def read(comport):
>
>     wrt_str=b'movt 3000'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     wrt_str=b'scan'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     rsp_str=comport.readlines() #1


You use readlines() with a s at the end !

* Note that when the serial port was opened with no timeout, that
readline()
* blocks until it sees a newline (or the specified size is reached)
* and that readlines() would never return and therefore refuses to
work
* (it raises an exception in this case)!

>
>     wrt_str=b'hllo'+b'\r\n'
>     ret_str=comport.write(wrt_str)
>
>     rsp_str=comport.readlines()#2
> --
>
> The problem is with the lines above with ###. In interactive mode,
> there is about 1 second delay at #1, and 9 seonds delay at #2. I get
> correct responses there. However, if I execute the script above, there
> is no delay at all and I get incorrect results (garbage). I set the
> read timeout to 0 in comm port set up, as
>
> comport.timeout=0
> So the comport should be in blocking mode if it waits for the end of
> line or end of file.

Wrong :

timeout = None: wait forever
timeout = 0: non-blocking mode (return immediately on read)
timeout = x: set timeout to x seconds (float allowed)

>
> I tried many things, like exec (execfile in 2.7), but at no avail.
>
> I have an update to the original post I made a few days ago. I think I
> know what the problem is and want to know if anyone has a solution:
>
> After putting "print" and "time.sleep(delay)" after every statement, I
> found when the script is running, it appears going around the pyserial
> statement, such as "comport.write(..)" or "comport.readlines(...)"
> while the pyserial command is executing (appearing as waiting and
> busying doing some thing, you know serial port is slow). So for
> example, when I exec all statements in a python interactive shell, I
> am not able to type and run a new statement if the previous one is not
> returned. Let's ay, if comport.readlines() is not returning, I can't
> type and run the next comport.write(...) statemtn. However, in a
> script that is running, if the comport.readlines() is busy reading,
> the next statement is running, if the next statement happens to be a
> comport.write() which will abort the reading.
>
> Is there any way to force the python script to behave like running
> exactly sequentially?


You have some new things to try

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


pyzmail-0.9.0: high level mail library to read, write and send emails easily

2011-08-28 Thread aspineux
Python easy mail library

pyzmail is a high level mail library for Python. It provides functions
and classes that help to read, compose and send emails. pyzmail exists
because their is no reasons that handling mails with Python would be
more difficult than with popular mail clients like Outlook or
Thunderbird. pyzmail hide the difficulties of the MIME structure and
MIME encoding/decoding. It also hide the problem of the
internationalized header encoding/decoding.

More here http://www.magiksys.net/pyzmail/

You can get a lot of usage samples from the inside API documentation :
http://www.magiksys.net/pyzmail/api/index.html

This library is the result of 3 articles I wrote about sending mail
using python

- Parsing email using Python part 1 of 2 : The Header
http://blog.magiksys.net/parsing-email-using-python-header
- Parsing email using Python part 2 of 2 : The content
http://blog.magiksys.net/parsing-email-using-python-content
- Generate and send mail with python: tutorial
http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial

pyzmail also include a very nice "pyzsendmail" command line utility
to send the most complex email from the command line.

"pyzsendmail -h" for more


I'm waiting for your feedback
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fun with nested loops

2011-08-31 Thread aspineux
On Aug 31, 5:51 pm, Daniel  wrote:
> Dear All,
>
> I have some complicated loops of the following form
>
> for c in configurations: # loop 1
>     while nothing_bad_happened: # loop 2
>         while step1_did_not_work: # loop 3
>             for substeps in step1 # loop 4a
>                 # at this point, we may have to
>                 -leave loop 1
>                 -restart loop 4
>                 -skip a step in loop 4
>                 -continue on to loop 4b
>
>         while step2_did_not_work: # loop 4b
>             for substeps in step2:
>                 # at this point, we may have to
>                 -leave loop 1
>                 -restart loop 2
>                 -restart loop 4b
>                 ...
>         ...many more loops...
>
> I don't see any way to reduce these nested loops logically, they
> describe pretty well what the software has to do.
> This is a data acquisition application, so on ever line there is
> a lot of IO that might fail or make subsequent steps useless or
> require a
> retry.
>
> Now every step could need to break out of any of the enclosing loops.
> So basically I have to transform every loop to be of the following
> horror:
>
> # general transformation of
> # "for c in configurations..."
> # to provide restart, break and continue
> # callable from any nesting level inside of the loop
>
> class loop1_restart(Exception): pass
> class loop1_break(Exception): pass
> class loop1_continue(Exception): pass
>
> while True:
>     try:
>         for c in configurations:
>             while True:
>                 try:
>                     # inner loops go here, of course, they would have
> to get
>                     # all the boilerplate added, too
>                     while nothing_bad_happened:
>                         while step1_did_not_work:
>                            if cond1:
>                                raise loop1_restart()
>                            elif cond3:
>                                raise loop1_break()
>                            elif cond3:
>                                raise loop1_continue()
>
>                     break
>
>                 except loop1_continue:
>                     pass
>         break
>     except loop1_restart:
>         pass
>     except loop1_break:
>         break
>
> Of course this is extremely tedious and error prone.
> If Python had named loops (PEP 3136, unfortunately rejected), this
> would be trivial:
> In Fortran I can continue (cycle), break (exit) and redo (goto label)
> arbitrary
> loops, which results in neat code:
>
> 10 loop1: do I=1,3
>     loop2: do J=1,4
>         print *,I,J
>         goto 10
>         cycle loop1
>         exit loop1
>     enddo loop2
> enddo loop1
>
> My question is, how do I obtain something that implements the
> following logic:
>
> @named_loop(fred) # I wish this would be possible
> for i in range(10):
>     for j in range(10):
>         break fred # breaks out of outer loop
>         continue fred # continues outer loop
>         redo fred # resets outer loop and restarts with i=0
>
> The best solution would be something along the Proposal D - Explicit
> iterators
> in PEP 3136, in this case it would even be possible to peek at the
> next i or
> advance/reverse the iterator a few steps.
>
> Has anyone an idea on a nice way to write breaks/continues/redos for
> deeply
> nested loops?

Hi Dan, it looks like you have already answered all your questions.

one more idea, a kind of named loop:

ic=0
op='what to do'
while ichttp://en.wikipedia.org/wiki/Finite-state_machine


Regards


>
> Dan

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


Re: Processing a file using multithreads

2011-09-08 Thread aspineux
On Sep 9, 12:49 am, Abhishek Pratap  wrote:
> Hi Guys
>
> My experience with python is 2 days and I am looking for a slick way
> to use multi-threading to process a file. Here is what I would like to
> do which is somewhat similar to MapReduce in concept.
>
> # test case
>
> 1. My input file is 10 GB.
> 2. I want to open 10 file handles each handling 1 GB of the file
> 3. Each file handle is processed in by an individual thread using the
> same function ( so total 10 cores are assumed to be available on the
> machine)
> 4. There will be 10 different output files
> 5. once the 10 jobs are complete a reduce kind of function will
> combine the output.
>
> Could you give some ideas ?

You can use "multiprocessing" module instead of thread to bypass the
GIL limitation.

First cut your file in 10 "equal" parts. If it is line based search
for the first line
close to the cut. Be sure to have "start" and "end" for each parts,
start is the address of the
first character of the first line and end is one line too much (==
start of the next block)

Then use this function to handle each part .

def handle(filename, start, end)
  f=open(filename)
  f.seek(start)
  for l in f:
start+=len(l)
if start>=end:
  break
# handle line l here
print l

Do it first in a single process/thread to be sure this is ok (easier
to debug) then split in multi processes


>
> So given a file I would like to read it in #N chunks through #N file
> handles and process each of them separately.
>
> Best,
> -Abhi

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


pyzmail-0.9.9 mail library to read, compose and send emails easily, support for py3k

2011-09-16 Thread aspineux
Now pyzmail include support for python 3.2 and above

pyzmail is a high level mail library for Python. It provides functions
and classes that help to read, compose and send emails. pyzmail exists
because their is no reasons that handling mails with Python would be
more difficult than with popular mail clients like Outlook or
Thunderbird. pyzmail hide the difficulties of the MIME structure and
MIME encoding/decoding. It also hide the problem of the
internationalized header encoding/decoding.

The library contains lot of sample and is very well documented.

http://www.magiksys.net/pyzmail/

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


fromutc: dt.tzinfo is not self: pytz.timezone('UTC').fromutc(datetime.utcnow())

2011-10-19 Thread aspineux
hi

>>> import pytz
>>> from datetime import datetime
>>> pytz.timezone('GMT0').fromutc(datetime.utcnow())
datetime.datetime(2011, 10, 19, 7, 54, 45, 579125,
tzinfo=)
>>>  pytz.timezone('UTC').fromutc(datetime.utcnow())
Traceback (most recent call last):
  File "", line 1, in 
ValueError: fromutc: dt.tzinfo is not self
>>> pytz.timezone('Europe/Brussels').fromutc(datetime.utcnow())
datetime.datetime(2011, 10, 19, 9, 55, 47, 787937, tzinfo=)

Why does UTC fail ?
Bug or feature ?

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


Re: fromutc: dt.tzinfo is not self: pytz.timezone('UTC').fromutc(datetime.utcnow())

2011-10-21 Thread aspineux
On Oct 19, 11:03 pm, Steven D'Aprano  wrote:
> On Wed, 19 Oct 2011 01:06:53 -0700, aspineux wrote:
> > hi
>
> >>>> import pytz
> >>>> from datetime import datetime
> >>>> pytz.timezone('GMT0').fromutc(datetime.utcnow())
> > datetime.datetime(2011, 10, 19, 7, 54, 45, 579125, tzinfo= > 'GMT0'>)
> >>>>  pytz.timezone('UTC').fromutc(datetime.utcnow())
> > Traceback (most recent call last):
> >   File "", line 1, in 
> > ValueError: fromutc: dt.tzinfo is not self
> >>>> pytz.timezone('Europe/Brussels').fromutc(datetime.utcnow())
> > datetime.datetime(2011, 10, 19, 9, 55, 47, 787937, tzinfo= > 'Europe/Brussels' CEST+2:00:00 DST>)
>
> > Why does UTC fail ?
> > Bug or feature ?
>
> Looks like a bug to me. But I'm not an expert on pytz. Perhaps you should
> report it back to the package author.

Done

https://bugs.launchpad.net/pytz/+bug/879480

>
> --
> Steven

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


modifying values of List or Dictionary when iterating on them

2007-05-24 Thread aspineux
Hello

I just want to update the data inside List or Dictionary without
adding or deleting object.

is this correct ?

l=[1, 2, 3]
for i, v in enumerate(l):
l[i]=v+1

d=dict(a=1, b=2, c=3)
for k, v in d.iteritems():
d[k]=d[k]+1

Both works, but :

are they correct ?
are they optimum for big structure ?

Thanks

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


Re: No file from stdin

2007-05-24 Thread aspineux
On 24 mai, 18:48, Tartifola <[EMAIL PROTECTED]> wrote:
> Hi,
> suppose a script of python is waiting for a file from the stdin and none
> is given. How can I make the script to stop and, for example, print an
> error message?
>
> Sorry for the n00b question and thanks

import sys
import os.path

if len(sys.argv)<2:
print >>sys.stderr, 'Usage: %s filename' %
(os.path.basename(sys.argv[0]),)
sys.exit(1)

print 'Your filename is %s' % (sys.argv[1],)


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


Re: modifying values of List or Dictionary when iterating on them

2007-05-24 Thread aspineux
On 24 mai, 19:21, "Christopher Anderson" <[EMAIL PROTECTED]>
wrote:
> > d=dict(a=1, b=2, c=3)
> > for k, v in d.iteritems():
> > d[k]=d[k]+1
>
> You might as well do: d[k] = v + 1, like for the list.


ops, yes it was a typo

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


Re: need advice on building core code for python and PHP

2007-05-24 Thread aspineux
On 24 mai, 19:33, Szabolcs Nagy <[EMAIL PROTECTED]> wrote:
> > Is there a way I could code the base (core) code in Python and have
> > PHP call it?  I've really liked using SQLAlchemy and there are other
>
> * quick and dirty solution:
> in a shell:
>   $ python yourscript.py pipe_out
> in the php script:
>   fwrite(pipe_in, input_data);
>   results = fread(pipe_out, sizeof_results);
>
> * simple and nice solution:
>   do not ever use php

Write a CGI wrapper around your python script, and publish it using
mod_python.
And make the appropriate http requests from PHP.





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


replace the base class

2007-05-30 Thread aspineux
Hi

I would like a kind of function able to replace the base class like
that:

class Graph:
pass

class Circle(Graph):
pass

class Square(Graph):
pass

class ColorGraph:
pass

def Adopt(new_base_class, child_class, old_base_class):

return newclass

ColorCircle=Adopt(ColorGraph, Circle, Graph)
ColorSquare=Adopt(ColorGraph, Square, Graph)


I have a lot of classes (Circle, Square, ...) that inherit all from
base class Graph
I have a more powerful class ColorGraph that do the same as Graph and
more.
I want to have new classes ColorCircle, ColorSquare that share exactly
the same code has
Circle or Square but inherit from base class ColorGraph to take
benefit the new features ?

How can I do that ?

I can get what I want by duplicating the source of all my child
classes,
and replace any occurrence of Graph by ColorGraph.
But when the code of Circle or Square is changed, I don't want to redo
the job.
I could also have a lot of new base class : 3DGraph, ..

Thanks

Alain

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


Re: file / module / package - import problem

2007-05-30 Thread aspineux

The filename and its path is in global variable __file__ (that is
different in any source file)

try

import os.path

file=open(os.path.join(os.path.dirname(__file__), 'hauteur.yaml'))




On 30 mai, 22:22, EuGeNe Van den Bulke <[EMAIL PROTECTED]>
wrote:
> Hi there,
>
> I have a "problem" which could be a bad design on my behalf but I am not
> sure so ...
>
> I have a package WMI which contains a module hauteur.py which, when
> imported, load data from a file located in WMI/data/. In hauteur.py I
> call open('data/hauteur.yaml').
>
> test.py
> WMI/
>  hauteur.py
>  data/
>  hauteur.yaml
>  lot.py
>
> It works well when hauteur is imported in lot.py but if I try import
> WMI.hauteur in test.py it doesn't work because it looks for the
> hauteur.yaml file in the "wrong" place.
>
> Is there a way to tell a module in a package to look for a file in a
> specific place i.e. a within package location?
>
> Thanks,
>
> EuGeNe --http://www.3kwa.com


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


Re: replace the base class

2007-05-31 Thread aspineux

Larry: Using "as", I cannot use both Circle and ColorCircle or more
like 3DCircle ... at the same time, only one of them.
But this is a easy solution in some cases.


On 31 mai, 00:25, Matimus <[EMAIL PROTECTED]> wrote:

Hi Martimus, your idea was a good one, I used it to meet my
_particular_ needs.
I was expecting something more complicate with metaclass, object
introspection 

I used an interface (like in Java, a class with just method that must
work in //
with anoter class).
Finally the "replacement" of the base class in done with a simple :

class ColorCircle(ColorGraphInterface, Circle):
graph_base_class=Circle


Here is my full working test code

class Graph:
def plot(self):
print 'Graph.plot'

class Circle(Graph):
def draw(self):
print 'Circle.draw'
self.plot()

class Square(Graph):
def draw(self):
print 'Square.draw'
self.plot()

class ColorGraphInterface:
graph_base_class=None
def plot(self):
print 'ColorGraphInterface.plot'
self.graph_base_class.plot(self)
def draw(self):
print 'ColorGraphInterface.draw'
self.graph_base_class.draw(self)

class ColorCircle(ColorGraphInterface, Circle):
graph_base_class=Circle

class ColorCircle(ColorGraphInterface, Square):
graph_base_class=Square


cc=ColorCircle()
cc.draw()




> This is a rather simplistic example, but you may be able to use a
> mixin class to achieve what you need. The idea is that you write a
> class that overrides only what is needed to add the new functionality.
> For you it would be Color.
>
> [code]
> class Graph(object):
> def _foo(self):
> print "Graph._foo"
>
> class Color(object):
> def _foo(self):
> print "Color._foo"
>
> class Circle(Graph):
> def bar(self):
> self._foo()
>
> class ColorCircle(Color,Circle): # the order of parent classes is
> important
> pass
>
> if __name__ == "__main__":
> c1 = Circle()
> c2 = ColorCircle()
>
> c1.bar() #prints: Graph._foo
> c2.bar() #pritns: Color._foo
> [/code]
>
> You might not have to do anything already. Try this and see if it
> works:
>
> [code]
>
> ColorCircle(ColorGraph,Circle):
> pass
>
> [/code]
>
> Note that you will probably have to create an __init__ method, and
> explicitly call the __init__ methods for the base classes. If the
> __init__ for Circle ends up calling the __init__ method from Graph,
> you may have issues. That is why the Color mixin that I created
> inherited from object not Graph. It helps to avoid clashing __init__
> methods.

Yes I know, super() do a great job for that :-)

>
> Matt


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


Re: non standard path characters

2007-05-31 Thread aspineux

I thing you should change the code page before to run the test, doing
something like :

c:\> chcp 850
c:\> \python.exe  ..\test.py

look for the good code page for you, maybe 850, 437 or 1230 or 1250
should work

Regards

On 31 mai, 12:17, Robin Becker <[EMAIL PROTECTED]> wrote:
> A kind user reports having problems running the reportlab tests because his 
> path
> has non-ascii characters in it eg
>
> .\Mes documents\Mes Téléchargements\Firefox\...
>
> somewhere in the tests we look at the path and then try and convert to utf8 
> for
> display in pdf.
>
> Is there a standard way to do these path string conversions?
>
> Paths appear to come from all sorts of places and given the increasing use of
> zip file packaging it doesn't seem appropriate to rely on the current platform
> as a single choice for the default encoding.
> --
> Robin Becker


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


Re: file reading by record separator (not line by line)

2007-05-31 Thread aspineux

something like

name=None
lines=[]
for line in open('yourfilename.txt'):
if line.startwith('>'):
if name!=None:
print 'Here is the record', name
print lines
print
name=line.stripr('\r')
lines=[]
else:
lines.append(line.stripr('\n'))



On 31 mai, 14:39, Lee Sander <[EMAIL PROTECTED]> wrote:
> I wanted to also say that this file is really huge, so I cannot
> just do a read() and then split on ">" to get a record
> thanks
> lee
>
> On May 31, 1:26 pm, Lee Sander <[EMAIL PROTECTED]> wrote:
>
> > Dear all,
> > I would like toreada really hugefilethat looks like this:
>
> > > name1
>
> > line_11
> > line_12
> > line_13
> > ...>name2 ...
>
> > line_21
> > line_22
> > ...
> > etc
>
> > where line_ij is just a free form text on that line.
>
> > how can ireadfileso that every time i do a "read()" i get exactly
> > onerecord
> > up to the next ">"
>
> > many thanks
> > Lee


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


Re: Automagically log changes in table

2007-03-17 Thread aspineux
Hi

You can get this using "triggers" and "stored procedures".
These are SQL engine dependent! This is available for long time with
postgress and only from version 5 with mysql.
This let you write SQL code (Procedure) that will be called when
"trigged" by an event like inserting new row, updating rows,
deleting 

BR


On 17 mar, 21:43, "George Sakkis" <[EMAIL PROTECTED]> wrote:
> This is more in the context of Turbogears/SQLAlchemy, but if anyone
> has implemented something similar with other packages it might be
> useful to know.
>
> I'd like to have a way to make a table "loggable", meaning it would
> get, say, two fields "last_modified" and "modified_by", and every
> write operation on it would automatically record the time and the id
> of the user who did the addition or change (I'm not sure how to deal
> with deletions let's leave this for now). Has anyone done something
> like that or knows where to start from ?
>
> George


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


Re: tkinter grid vs pack

2007-03-17 Thread aspineux
On 18 mar, 16:49, Gigs_ <[EMAIL PROTECTED]> wrote:
> For what is grid better than pack, and otherwise?
>
> thanks in advance

Grid are useful to align items vertically _AND_ horizontally !
For example to make adialog box with label of different length

Firstname: John__
Lastname: Smith__
Contry: Uk__

to align vertically Firstname and John , Lastname and Smith 
But ALSO horizontaly John, Smith and Uk !

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


Re: MIME Magic

2007-03-17 Thread aspineux
On 18 mar, 00:54, "Samuel" <[EMAIL PROTECTED]> wrote:
> On Mar 18, 12:30 am, Jorge Godoy <[EMAIL PROTECTED]> wrote:
>
> > I'd start by taking a look at "file"'s code. :-)
>
> > The thing is being able to identify the signatures of several different 
> > types
> > of files.  Here's some help for 
> > you:http://www.garykessler.net/library/file_sigs.html
>
> > And here's an example of an authoritative source for that 
> > table:http://www.libpng.org/pub/png/spec/1.2/PNG-Structure.html
>
> Actually, I am looking for an existing solution because I do not want
> to write it from scratch... I hope I am not out of options.
>
> -Samuel


python-magic : Python binding for the magic library

This Python library can be use to query /etc/magic information for a
given file in Python.


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


Re: Automagically log changes in table

2007-03-18 Thread aspineux
On 18 mar, 04:20, "George Sakkis" <[EMAIL PROTECTED]> wrote:
> On Mar 17, 7:59 pm, "aspineux" <[EMAIL PROTECTED]> wrote:
>
> > Hi
>
> > You can get this using "triggers" and "stored procedures".
> > These are SQL engine dependent! This is available for long time with
> > postgress and only from version 5 with mysql.
> > This let you write SQL code (Procedure) that will be called when
> > "trigged" by an event like inserting new row, updating rows,
> > deleting 
>
> I'd rather avoid triggers since I may have to deal with Mysql 4. Apart
> from that, how can a trigger know the current user ?

Because each user opening an web session will login to the SQL
database using it's own
login. That way you will be able to manage security at DB too. And the
trigger
will use the userid of the SQL session.

> This information
> is part of the context (e.g. an http request), not stored persistently
> somewhere. It should be doable at the framework/orm level but I'm
> rather green on Turbogears/SQLAlchemy.
>
> George

Maybe it will be easier to manage this in your web application.

it's not too dificulte to replace any

UPDATE persone
SET name = %{new_name}s
WHERE persone_id=%{personeid}

by something like

UPDATE persone
SET name = %{new_name}s, modified_by=%{user_id}s, modified_time=%
{now}s
WHERE persone_id=%{personeid}

BR

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


Re: Make variable global

2007-03-21 Thread aspineux
try

>>> import a
>>> a.blah
>>> a.go()
>>> a.blah

or

rewrite a.py like this

blah = [ None ]
def go():
global blah
blah[0] = 5 # and not blah=[5]

then
>>> from a import *
>>> blah
>>> go()
>>> blah

will work as expected

in case 1 ( import a )

blah in a.py and a.blah in python interpreter are the same
_REFERENCE_, they are the same

in case 2 (blah[0]=5)

blah in a.py and a.blah in python interpreter are just 2 differents
variables
that reference the _SAME_ object, but here (the difference with your
original case)
the object ( a list ) is a mutable object (you can change its value
without changing
the object. Here blah=[5] should simply make blah reference a new
object, but will
let the old one, [None] untouched, but unreachable (no more reference
to it)

BR





On 21 mar, 20:55, "abcd" <[EMAIL PROTECTED]> wrote:
> I have a file, "a.py"
>
> blah = None
> def go():
> global blah
> blah = 5
>
> >From the python interpreter I try
> >>> from a import *
> >>> blah
> >>> go()
> >>> blah
>
> ...i was hoping to see "5" get printed out the second time I displayed
> blah, but it doesn't.  Now, if I type this same code directly into the
> python interpreter it works as i was hoping.  what i am missing?
>
> thanks


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


make RE more cleaver to avoid inappropriate : sre_constants.error: redefinition of group name

2007-03-29 Thread aspineux

I want to parse

'[EMAIL PROTECTED]' or '<[EMAIL PROTECTED]>' and get the email address [EMAIL 
PROTECTED]

the regex is

r'<[EMAIL PROTECTED]>|[EMAIL PROTECTED]'

now, I want to give it a name

r'<(?P[EMAIL PROTECTED])>|(?P[EMAIL PROTECTED])'

sre_constants.error: redefinition of group name 'email' as group 2;
was group 1

BUT because I use a | , I will get only one group named 'email' !

Any comment ?

PS: I know the solution for this case is to use  r'(?P<)?(?P
[EMAIL PROTECTED])(?(lt)>)'

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


A nice way to use regex for complicate parsing

2007-03-29 Thread aspineux

My goal is to write a parser for these imaginary string from the SMTP
protocol, regarding RFC 821 and 1869.
I'm a little flexible with the BNF from these RFC :-)
Any comment ?

tests=[ 'MAIL FROM:<[EMAIL PROTECTED]>',
'MAIL FROM:[EMAIL PROTECTED]',
'MAIL FROM:<[EMAIL PROTECTED]> SIZE=1234
[EMAIL PROTECTED]',
'MAIL FROM:[EMAIL PROTECTED] SIZE=1234
[EMAIL PROTECTED]',
'MAIL FROM:<"[EMAIL PROTECTED]> legal=email"@address.com>',
'MAIL FROM:"[EMAIL PROTECTED]> legal=email"@address.com',
'MAIL FROM:<"[EMAIL PROTECTED]> legal=email"@address.com> SIZE=1234
[EMAIL PROTECTED]',
'MAIL FROM:"[EMAIL PROTECTED]> legal=email"@address.com SIZE=1234
[EMAIL PROTECTED]',
]

def RN(name, regex):
"""protect using () and give an optional name to a regex"""
if name:
return r'(?P<%s>%s)' % (name, regex)
else:
return r'(?:%s)' % regex


regex={}

#  ::=  "."  "."  "." 
regex['dotnum']=RN(None, r'[012]?\d?\d\.[012]?\d?\d\.[012]?\d?\d\.
[012]?\d?\d' % regex)
#  ::=  |  "." 
regex['dot_string']=RN(None, r'[a-zA-Z0-9]+(?:\.[a-zA-Z0-9]+)*' %
regex)
#  ::=   |  "." 
regex['domain']=RN('domain', r'%(dotnum)s|%(dot_string)s' % regex)
#  ::= any one of the 128 ASCII characters except , , quote
("), or backslash (\)
regex['q']=RN(None, r'[^\n\r"\\]' % regex)
#  ::= any one of the 128 ASCII characters (no exceptions)
regex['x']=RN(None, r'.' % regex)
#  ::=  "\"  | "\"   |  |  
regex['qtext']=RN(None, r'(?:\\%(x)s|%(q)s)+' % regex)
#  ::=  """  """
regex['quoted_string']=RN('quoted_string', r'"%(qtext)s"' % regex)
#  ::=  | 
regex['local_part']=RN('local_part', r'%(quoted_string)s|%
(dot_string)s' % regex)
#  ::=  "@" 
regex['mailbox']=RN('mailbox', r'%(local_part)[EMAIL PROTECTED](domain)s' % 
regex)
#  ::= "<" [  ":" ]  ">"
# also accept address without <>
regex['path']=RN('path', r'(?P<)?%(mailbox)s(?(path_lt)>)' %
regex)
# esmtp-keyword::= (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
regex['esmtp_keyword']=RN(None, r'[a-zA-Z0-9][-a-zA-Z0-9]*' % regex)
# esmtp-value  ::= 1*http://mail.python.org/mailman/listinfo/python-list


Re: make RE more cleaver to avoid inappropriate : sre_constants.error: redefinition of group name

2007-03-29 Thread aspineux
On 29 mar, 16:22, "aspineux" <[EMAIL PROTECTED]> wrote:
> I want to parse
>
> '[EMAIL PROTECTED]' or '<[EMAIL PROTECTED]>' and get the email address [EMAIL 
> PROTECTED]
>
> the regex is
>
> r'<[EMAIL PROTECTED]>|[EMAIL PROTECTED]'
>
> now, if I want to give it a name
>
> r'<(?P[EMAIL PROTECTED])>|(?P[EMAIL PROTECTED])'
>
> sre_constants.error: redefinition of group name 'email' as group 2;
> was group 1
>
> BUT because I use a | , I will get only one group named 'email' !

THEN my regex is meaningful, and the error is meaningless and
somrthing
should be change into 're'

But maybe I'm wrong ?

>
> Any comment ?

I'm trying to start a discussion about something that can be improved
in 're',
not looking for a solution about email parsing :-)


>
> PS: I know the solution for this case is to use  r'(?P<)?(?P
> [EMAIL PROTECTED])(?(lt)>)'


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


Re: make RE more cleaver to avoid inappropriate : sre_constants.error: redefinition of group name

2007-03-30 Thread aspineux
On 30 mar, 00:13, "Paddy" <[EMAIL PROTECTED]> wrote:
> On Mar 29, 3:22 pm, "aspineux" <[EMAIL PROTECTED]> wrote:
>
>
>
> > I want to parse
>
> > '[EMAIL PROTECTED]' or '<[EMAIL PROTECTED]>' and get the email address 
> > [EMAIL PROTECTED]
>
> > the regex is
>
> > r'<[EMAIL PROTECTED]>|[EMAIL PROTECTED]'
>
> > now, I want to give it a name
>
> > r'<(?P[EMAIL PROTECTED])>|(?P[EMAIL PROTECTED])'
>
> > sre_constants.error: redefinition of group name 'email' as group 2;
> > was group 1
>
> > BUT because I use a | , I will get only one group named 'email' !
>
> > Any comment ?
>
> > PS: I know the solution for this case is to use  r'(?P<)?(?P
> > [EMAIL PROTECTED])(?(lt)>)'
>
> use two group names, one for each alternate form and if you are not
> concerned with whichever matched do something like the following:
>
The problem is the way I create this regex :-)

regex={}
regex['email']=r'(?P[EMAIL PROTECTED])'

path=r'<%(email)s>|%(email)s' % regex

Once more, the original question is :
Is it normal to get an error when the same id used on both side of a
|

>
>
> >>> s1 = '[EMAIL PROTECTED]'
> >>> s2 = '<[EMAIL PROTECTED]>'
> >>> matchobj = re.search(r'<(?P[EMAIL PROTECTED])>|(?P[EMAIL 
> >>> PROTECTED])', s1)
> >>> matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
> '[EMAIL PROTECTED]'
> >>> matchobj = re.search(r'<(?P[EMAIL PROTECTED])>|(?P[EMAIL 
> >>> PROTECTED])', s2)
> >>> matchobj.groupdict()['email1'] or matchobj.groupdict()['email2']
> '[EMAIL PROTECTED]'
>
> - Paddy.


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


Re: A nice way to use regex for complicate parsing

2007-03-30 Thread aspineux
On 29 mar, 17:33, "Paul McGuire" <[EMAIL PROTECTED]> wrote:
> On Mar 29, 9:42 am, Shane Geiger <[EMAIL PROTECTED]> wrote:
>
> > It would be worth learning pyparsing to do this.
>
> Thanks to Shane and Steven for the ref to pyparsing.  I also was
> struck by this post, thinking "this is pyparsing written in re's and
> dicts".

My first idea was : why learn a parsing library if I can do it using
're'
and dicts :-)

>
> The approach you are taking is *very* much like the thought process I
> went through when first implementing pyparsing.  I wanted to easily
> compose expressions from other expressions.  In your case, you are
> string interpolating using a cumulative dict of prior expressions.
> Pyparsing uses various subclasses of the ParserElement class, with
> operator definitions for alternation ("|" or "^" depending on non-
> greedy vs. greedy), composition ("+"), and negation ("~").  Pyparsing
> also uses its own extended results construct, ParseResults, which
> supports named results fields, accessible using list indicies, dict
> names, or instance names.
>
> Here is the pyparsing treatment of your example (I may not have gotten
> every part correct, but my point is more the similarity of our
> approaches).  Note the access to the smtp parameters via the Dict
> transformer.
>
> -- Paul

Thanks !

Any parsing library I used before were heavy to start with.
The benefit was inversely proportional to the size of the project.
Your look to be lighter, and the results are more easily usable.

Thanks for showing me your lib.

Anyway today I will keep my idea for small parsing.


Alain


>
> from pyparsing import *
>
> #  ::=  "."  "."  "." 
> intgr = Word(nums)
> dotnum = Combine(intgr + "." + intgr + "." + intgr + "." + intgr)
>
> #  ::=  |  "." 
> string_ = Word(alphanums)
> dotstring = Combine(delimitedList(string_,"."))
>
> #  ::=   |  "." 
> domain = dotnum | dotstring
>
> #  ::= any one of the 128 ASCII characters except , , quote
> ("), or backslash (\)
> #  ::= any one of the 128 ASCII characters (no exceptions)
> #  ::=  "\"  | "\"   |  |  
> #  ::=  """  """
> quotedString = dblQuotedString  # <- just use pre-defined expr from
> pyparsing
>
> #  ::=  | 
> localpart = (dotstring | quotedString).setResultsName("localpart")
>
> #  ::=  "@" 
> mailbox = Combine(localpart + "@" + domain).setResultsName("mailbox")
>
> #  ::= "<" [  ":" ]  ">"
> # also accept address without <>
> path = "<" + mailbox + ">" | mailbox
>
> # esmtp-keyword::= (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
> esmtpkeyword = Word(alphanums,alphanums+"-")
>
> # esmtp-value  ::= 1* # esmtp-parameter  ::= esmtp-keyword ["=" esmtp-value]
> esmtpparameters = Dict(
> ZeroOrMore( Group(esmtpkeyword + Suppress("=") + esmtpvalue) ) )
>
> # esmtp-cmd::= inner-esmtp-cmd [SP esmtp-parameters] CR LF
> esmtp_addr = path + \
> Optional(esmtpparameters,default=[])\
> .setResultsName("parameters")
>
> for t in tests:
> for keyword in [ 'MAIL FROM:', 'RCPT TO:' ]:
> keylen=len(keyword)
> if t[:keylen].upper()==keyword:
> t=t[keylen:]
> break
>
> try:
> match = esmtp_addr.parseString(t)
> print 'MATCH'
> print match.dump()
> # some sample code to access elements of the parameters
> "dict"
> if "SIZE" in match.parameters:
> print "SIZE is", match.parameters.SIZE
> print
> except ParseException,pe:
> print 'DONT match', t
>
> prints:
> MATCH
> ['<', ['[EMAIL PROTECTED]'], '>']
> - mailbox: ['[EMAIL PROTECTED]']
>   - localpart: johnsmith
> - parameters: []
>
> MATCH
> [['[EMAIL PROTECTED]']]
> - mailbox: ['[EMAIL PROTECTED]']
>   - localpart: johnsmith
> - parameters: []
>
> MATCH
> ['<', ['[EMAIL PROTECTED]'], '>', ['SIZE', '1234'], ['OTHER',
> '[EMAIL PROTECTED]']]
> - OTHER: [EMAIL PROTECTED]
> - SIZE: 1234
> - mailbox: ['[EMAIL PROTECTED]']
>   - localpart: johnsmith
> - parameters: [['SIZE', '1234'], ['OTHER', '[EMAIL PROTECTED]']]
>   - OTHER: [EMAIL PROTECTED]
>   - SIZE: 1234
> SIZE is 1234
>
> MATCH
> [['[EMAIL PROTECTED]'], ['SIZE', '1234'], ['OTHER', '[EMAIL PROTECTED]']]
> - OTHER: [EMAIL PROTECTED]
> - SIZE: 1234
> - mailbox: ['[EMAIL PROTECTED]']
>   - localpart: johnsmith
> - parameters: [['SIZE', '1234'], ['OTHER', '[EMAIL PROTECTED]']]
>   - OTHER: [EMAIL PROTECTED]
>   - SIZE: 1234
> SIZE is 1234
>
> MATCH
> ['<', ['"[EMAIL PROTECTED]> legal=email"@addresscom'], '>']
> - mailbox: ['"[EMAIL PROTECTED]> legal=email"@addresscom']
>   - localpart: "[EMAIL PROTECTED]> legal=email"
> - parameters: []
>
> MATCH
> [['"[EMAIL PROTECTED]> legal=email"@addresscom']]
> - mailbox: ['"[EMAIL PROTECTED]> legal=email"@addresscom']
>   - localpart: "[EMAIL PROTECTED]> legal=email"
> - parameters: []
>
> MATCH
> ['<', ['"[EMAIL PROTECTED]> legal=email"@addresscom'], '>', ['SIZE', '1234'],
> ['OTHER', '[EMAIL PROTECTE

Re: saving Python process state for later debugging

2007-03-31 Thread aspineux
On 31 mar, 16:48, [EMAIL PROTECTED] wrote:
> Hi!
>
> Is there a way to save the state of a Python process for later
> inspection with a debugger? One way to do this is to dump core, but is
> the result usable for debugging with pdb (it can be debugged by gdb,
> which can inspect PyObjects in a CPython core, for example, but it's
> not much fun)?
>
> If there is no way to do this today, are there essential difficulties
> in implementing this on top of an OS support for "raw" core dumps?

Pylon has something like that.
http://pylonshq.com/docs/0.9.4.1/interactive_debugger.html

Turbogears has the same with option tg.fancy_exception

You can navigate into the stack trace from the web interface :-)



>
> TIA,
> Yossi


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


Re: saving Python process state for later debugging

2007-04-01 Thread aspineux
On 1 avr, 09:39, [EMAIL PROTECTED] wrote:
> On Apr 1, 2:07 am, "aspineux" <[EMAIL PROTECTED]> wrote:
>
>
>
> > Pylon has something like 
> > that.http://pylonshq.com/docs/0.9.4.1/interactive_debugger.html
>
> > Turbogears has the same with option tg.fancy_exception
>
> I could get it wrong, but these things seem to be about debugging
> crashed processes "online", not saving snapshots to files for later
> inspection. Can you e-mail a process snapshot to a different machine
> with them, for example? I understood that you are supposed to debug
> the original process, which is kept alive, via the web. I'm talking
> about a situation where you have a Python program deployed to a user
> who is not running a web server, and have the user send you a snapshot
> as a bug report.
>
> -- Yossi

A context in python is no more than 2 dictionaries ( globals() and
locals()).
You can easily serialize both to store them.
You can navigate into the python stack using module inspect and
generate the context for all
the functions in the stack trace.

This is probably no more than 50 lines of code, maybe 20 :-)

You can find sample of how to get these info and use them in the
sample I was reffering before.

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


Re: BaseHTTPRequestHandler reading .html with python code

2007-04-06 Thread aspineux
On 6 avr, 11:52, [EMAIL PROTECTED] wrote:
> H!
>
> I was wondering how I can do something like this.

Use a template engine like :

Genshi
Django/Jinja
Cheetah
Kid template

For more engine look at

http://www.turbogears.org/cogbin/

And maybe what you are looking fore is a complete framework like :

turbogears
pylon
django

Hope this help

>
> file.html
> -
> hello world
> 
> print 'hello world'
> #or display str(time.localtime()[7])
> 
>
> webserver.py
> -
> def do_GET(self):
> try:
> if self.path.endswith(".html"):
> f = open(curdir + sep + self.path) #self.path has /
> test.html
> self.send_response(200)
> self.send_header('Content-type','text/html')
> self.end_headers()
> # now display the hello world and run the python code.
> self.wfile.write(f.read())
> f.close()
> return
>
> I saw the mod python for apache and some PSP but I really want to know
> how they do this.
> Is there are special module for doing this ?
>
> Or is there something like this:
> ---
> 1. a running python program
> 2. include with write(f.read()) a extra python code inside the already
> running python program.
>
> How they handle this ?


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


How to set program name in Python : success

2007-04-08 Thread aspineux
here is the trick I found

#  ( exec  -a "pgm.py" python < pgm.py ) &
# ps ax | grep pgm.py
22334 pts/2S+ 0:00 pgm.py

this solution works on linux and probably all BSD too,
but don't let specify any arguments.

If someone know a better solution 

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


Re: Nice "bug" to loose a contest

2007-04-08 Thread aspineux
This code works like the python one,
I dont use buffered stdio f... functions,
but the more 'system call' read and write

int main()
{
char buf[120];
int len;

while (len=read(1, buf, sizeof(buf))) {
write(1, buf, len);
}
return 0;
}

I dont understand what is appening,
but you have to know their is a link between
stdin and stdout in 'terminal mode', when you make a read on stdin,
stdout is automatically flushed, this link disappears when onr is not
liked to a terminal.
This is to facilitate user input, to avoid to put a flush after each
write,
in fact before any user input.

Hope someone else will give the trick



On 8 avr, 12:52, "stdazi" <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Yesterday, I was at a programming competition. We programmed on Linux
> liveCD's and Python was one of the allowed languages (among C and
> Java). I cared just about the algorithmic approach so I used Python.
> One of the main rules is, that the code reads its standard input and
> dumps the result on the standard output. Here happened my bigger
> programming mistake ever - I used the following approach :
>
> 
> import sys
> print sys.stdout.readlines() # TYPO ! stdin != stdout
> 
>
> So the WEIRD issue is, that it worked and, executing the following
> code I get :
>
> 
> [EMAIL PROTECTED] ~ $ python la.py
> test
> test #2
> ['test \n', 'test #2\n']
> [EMAIL PROTECTED] ~ $
> 
>
> Ok, as the algorithm worked perfectly, and all the test cases we were
> given were positive, I've continued with the next problem, copy/
> pasting the above idiom... When they tested the code, they used file
> redirection like :
>
> ==
> python la.py < /somefile
> ==
>
> And, my code resulted in no output/input (=> 0 points), which can be
> proved here too :
>
> 
> [EMAIL PROTECTED] ~ $ python la.py < /etc/passwd
>
> ===
>
> Some friend of mine told me that's the Unix way, (stdout can act like
> stdin) so I tried some C code :
>
> ===
>   1 #include 
>   2
>   3 int main() {
>   4 char buf[120];
>   5 while (fgets(buf, sizeof(buf), stdout) != NULL) {
>   6 puts(buf);
>   7 }
>   8return 0;
>   9}
> ===
>
> The code returns with no delay, so I'm really wondering where is that
> nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
> anything. I'd spot the mistake before submitting the problem solutions
> if it was written in C :)
>
> Thanks!


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


Re: Nice "bug" to loose a contest

2007-04-12 Thread aspineux
On 8 avr, 13:40, "aspineux" <[EMAIL PROTECTED]> wrote:
> This code works like the python one,
> I dont use buffered stdio f... functions,
> but the more 'system call' read and write
>
> int main()
> {
> char buf[120];
> int len;
>
> while (len=read(1, buf, sizeof(buf))) {
> write(1, buf, len);
> }
> return 0;
>
> }
>
> I dont understand what is appening,

But I have an idea !

It is possible to open a file in RW mode, nothing exceptional with
that.
And when connected in a terminal, you are connected through a file
called a PTY !
This "file" is open in RW !

File descriptors 0, 1 and 2 are certainly a "view" of this PTY and
then in fact the same thing !





> but you have to know their is a link between
> stdin and stdout in 'terminal mode', when you make a read on stdin,
> stdout is automatically flushed, this link disappears when onr is not
> liked to a terminal.
> This is to facilitate user input, to avoid to put a flush after each
> write,
> in fact before any user input.
>
> Hope someone else will give the trick
>
> On 8 avr, 12:52, "stdazi" <[EMAIL PROTECTED]> wrote:
>
> > Hello,
>
> > Yesterday, I was at a programming competition. We programmed on Linux
> > liveCD's and Python was one of the allowed languages (among C and
> > Java). I cared just about the algorithmic approach so I used Python.
> > One of the main rules is, that the code reads its standard input and
> > dumps the result on the standard output. Here happened my bigger
> > programming mistake ever - I used the following approach :
>
> > 
> > import sys
> > print sys.stdout.readlines() # TYPO ! stdin != stdout
> > 
>
> > So the WEIRD issue is, that it worked and, executing the following
> > code I get :
>
> > 
> > [EMAIL PROTECTED] ~ $ python la.py
> > test
> > test #2
> > ['test \n', 'test #2\n']
> > [EMAIL PROTECTED] ~ $
> > 
>
> > Ok, as the algorithm worked perfectly, and all the test cases we were
> > given were positive, I've continued with the next problem, copy/
> > pasting the above idiom... When they tested the code, they used file
> > redirection like :
>
> > ==
> > python la.py < /somefile
> > ==
>
> > And, my code resulted in no output/input (=> 0 points), which can be
> > proved here too :
>
> > 
> > [EMAIL PROTECTED] ~ $ python la.py < /etc/passwd
>
> > ===
>
> > Some friend of mine told me that's the Unix way, (stdout can act like
> > stdin) so I tried some C code :
>
> > ===
> >   1 #include 
> >   2
> >   3 int main() {
> >   4 char buf[120];
> >   5 while (fgets(buf, sizeof(buf), stdout) != NULL) {
> >   6 puts(buf);
> >   7 }
> >   8return 0;
> >   9}
> > ===
>
> > The code returns with no delay, so I'm really wondering where is that
> > nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
> > anything. I'd spot the mistake before submitting the problem solutions
> > if it was written in C :)
>
> > Thanks!


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


imaplib : error reporting use 'randomly' exception or return value

2007-02-01 Thread aspineux
imaplib use exception to report errors,  but some problems must be
detected by checking the return value !
For example, when trying to append into a mailbox with wrong ACL,
imaplib return 'NO', but dont raise any exception (I give a sample at
the end).
This make error handling more complicate, because any imap statement
is supposed to be followed by a test of the returned value!

Why not report all problems using exceptions ?

It easy to modify imaplib.py to manage this because most of the imap
call are made through function
_simple_command this way :

def _simple_command(self, name, *args):
return self._command_complete(name, self._command(name,
*args))

I propose to replace it by something like :

def _simple_command(self, name, *args):
typ, dat=self._command_complete(name, self._command(name,
*args))
if typ!='OK':
raise self.error(dat[-1])
return typ, dat

Any comment ?

Here is an example, append  on a mailbox with the wrong ACL fail by
returning a 'NO'

import imaplib

server='localhost'
login='[EMAIL PROTECTED]'
passwd='password'

c=imaplib.IMAP4(server)
c.login(login, passwd)
c.setacl('INBOX', login, '') # set wrong ACL, removing 'i'
typ, dat=c.append('INBOX', None, None, "From: [EMAIL PROTECTED]: %s
\nSubject: test append\n\nHello\n" % (login))
print typ, dat

output:

NO ['Permission denied']

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


imaplib: support for more cyrus extension : expire

2007-02-01 Thread aspineux
setacl and getacl look to be already "Cyrus" specific (according the
doc),
why not to extend imaplib a little bit more ?

Here are some code I wrote and tested to support cyrus "expire" that
manage how long a message
can stay into a mailbox.
This is usefull for NEWS server !

The code is preceded by lot of sample.
I wrote the 2 generic functions getannotation and setannotation that
coul be used with any IMAP server.
And use them to access the Cyrus extensions !
I tried to use http://www.potaroo.net/ietf/all-ids/draft-daboo-imap-
annotatemore-00.txt
but used a lot TCPDUMP to compare with cyradm :-)

Any chances to see this small part of me into the official python
release ?

"""IMAP4 Client

Extend default python imaplib to include ANNOTATION as described in
http://www.potaroo.net/ietf/all-ids/draft-daboo-imap-annotatemore-00.txt

This extention is used by cyrus imap, for exemple to manage
automatique
removale of expired mail based on '/vendor/cmu/cyrus-imapd/expir'
annotation
"""

"""
Some usage

>>> i.getannotation('user/[EMAIL PROTECTED]', '"*"', '"value.shared"')
>>> i.getannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>> '"value.shared"')
('OK', ['"user/[EMAIL PROTECTED]" "/vendor/cmu/cyrus-imapd/
expire" ("value.shared" "44")'])
>>> i.getannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>> '("value.shared")')
('OK', ['"user/[EMAIL PROTECTED]" "/vendor/cmu/cyrus-imapd/
expire" ("value.shared" "44")'])
>>> i.getannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>> '("*")')
('OK', ['"user/[EMAIL PROTECTED]" "/vendor/cmu/cyrus-imapd/
expire" ("value.shared" "44" "content-type.shared" "text/plain"
"size.shared" "2" "modifiedsince.shared" "1156264470")'])
>>> i.getannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>> '("value.shared" "content-type.shared")')
('OK', ['"user/[EMAIL PROTECTED]" "/vendor/cmu/cyrus-imapd/
expire" ("value.shared" "44" "content-type.shared" "text/plain")'])
>>> i.setannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>>  '("value.shared" "44")')
('OK', [None])
>>> i.setannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>>  '("value.shared" NIL)')
('OK', [None])
>>> i.getannotation('user/[EMAIL PROTECTED]', '/vendor/cmu/cyrus-imapd/expire', 
>>> '("value.shared")')
('OK', [None])

some more

>>> i=cyrusimap.CYRUSIMAP4('localhost')
>>> i.login('manager', 'password')
('OK', ['User logged in'])
>>> i.getcyrusexpire("user/[EMAIL PROTECTED]")
('OK', 0)
>>> i.setcyrusexpire("user/[EMAIL PROTECTED]", 88)
('OK', [None])
>>> i.getcyrusexpire("user/[EMAIL PROTECTED]")
('OK', 88)
>>> i.setcyrusexpire("user/[EMAIL PROTECTED]", None)
('OK', [None])
>>> i.getcyrusexpire("user/[EMAIL PROTECTED]")
('OK', 0)


"""


import imaplib

imaplib.Commands.update(
   {
'GETANNOTATION': ('AUTH', 'SELECTED'),
'SETANNOTATION': ('AUTH', 'SELECTED'),
})

class CYRUSIMAP4(imaplib.IMAP4):

def getannotation(self, root, entry, attrib):
"""Get annotation

(typ, [data]) = .getannotation(self, root, entry,
attrib)
"""
typ, dat = self._simple_command('GETANNOTATION', root, entry,
attrib)
return self._untagged_response(typ, dat, 'ANNOTATION')

def setannotation(self, root, entry, value):
"""Set annotation value.

(typ, [data]) = .setannotation(root, limits)
"""
typ, dat = self._simple_command('SETANNOTATION', root, entry,
value)
return self._untagged_response(typ, dat, 'ANNOTATION')

def getcyrusexpire(self, root):
"""Get cyrus 'expire' annotation value.

(typ, [data]) = .getcyrusexpire(root)
"""

typ, dat=self.getannotation(root, '/vendor/cmu/cyrus-imapd/
expire', '("value.shared")')
if typ!='OK':
return typ, dat

if dat[0]==None:
return typ, 0

# ['"user/[EMAIL PROTECTED]" "/vendor/cmu/cyrus-imapd/
expire" ("value.shared" "44")'])
v=int(dat[0].split(None,2)[2].strip('()').split(None,1)
[1].strip('"'))
return typ, v

def setcyrusexpire(self, root, value):
"""Get cyrus 'expire' annotation value.

(typ, [data]) = .setcyrusexpire(root, value)
"""
if value==None or value==0:
v='NIL'
else:
v='"%d"' % (value,)

return self.setannotation(root, '/vendor/cmu/cyrus-imapd/
expire', '("value.shared" %s)'%(v,))

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


Re: LDAP/LDIF Parsing

2007-02-01 Thread aspineux

The tree hierarchy is defined by the DN of each object, the types of
the object is specified by its objectClass.
Just collect all items (or do it dynamically by tunning the scope and
the base of your search request)


On 1 fév, 18:22, "Cruelemort" <[EMAIL PROTECTED]> wrote:
> All,
>
> I am hoping someone would be able to help me with a problem. I have an
> LDAP server running on a linux box, this LDAP server contains a
> telephone list in various groupings, the ldif file of which is -
>
> dn: dc=example,dc=com
> objectClass: top
> objectClass: dcObject
> objectClass: organization
> dc: example
> o: Example Organisation
>
> dn: ou=groupa,dc=example,dc=com
> ou: groupa
> objectClass: top
> objectClass: organizationalUnit
> description: Group A
>
> dn: cn=johnsmith,ou=groupa,dc=example,dc=com
> cn: johnsmith
> objectClass: top
> objectClass: person
> sn: Smith
> telephoneNumber: 112
>
> dn: cn=davesteel,ou=groupa,dc=example,dc=com
> cn: davesteel
> objectClass: top
> objectClass: person
> sn: Steel
> telephoneNumber: 113
>
> dn: ou=groupb,dc=example,dc=com
> ou: groupb
> objectClass: top
> objectClass: organizationalUnit
> description: Group B
>
> dn: cn=williamdavis,ou=groupb,dc=example,dc=com
> cn: williamdavis
> objectClass: top
> objectClass: person
> sn: Davis
> telephoneNumber: 122
>
> dn: cn=jamesjarvis,ou=groupb,dc=example,dc=com
> cn: jamesjarvis
> objectClass: top
> objectClass: person
> sn: Jarvis
> telephoneNumber: 123
>
> I am creating a python client program that will display the telephone
> list in the same directory structure as is on the LDAP server (i.e. it
> starts with buttons of all the groups, when you click on a group it
> comes up with buttons of all the numbers or groups available, and you
> can continually drill down).
>
> I was wondering the best way to do this? I have installed and used the
> python-ldap libraries and these allow me to access and search the
> server, but the searches always return a horrible nesting of lists,
> tuples and dictionaries, below is an example of returning just one
> record -
>
> ('dc=example,dc=com', {'objectClass': ['top', 'dcObject',
> 'organization'], 'dc': ['example'], 'o': ['Example Organisation']})
>
> Basically i think i need to parse the search results to create objects
> and build the python buttons around this, but i was hoping someone
> would be able to point me in the correct direction of how to do this?
> Is there a parser available? (there is an ldif library available but
> it is not obvious how this works, i cannot see much documentation, and
> it seems to be deprecated...).
>
> Many thanks.
>
> Ian


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


Re: python executing windows exe

2007-02-01 Thread aspineux
First look if your .exe will accept to work that way !
try in a DOS prompt

> echo yourfilename | nec2d.exe

or if the program expect more input, write them all in a file
( myinputs.txt ) and try :

> nec2d.exe < myinputs.txt

If it works looks for module subprocess and more precisely object
subprocess.popen and method comunicate


On 1 fév, 23:57, "Kiran" <[EMAIL PROTECTED]> wrote:
> Hi everybody,
>   I am making python run an executable using the os module.  Here is
> my question.  The executable, once it is running, asks the user to
> input a filename that it will process.  Now, my question is how do i
> automate this.  let me make this clear, it is not an argument you pass
> in when you type in the exe.  An example below illustrates:
>
> THIS IS NOT WHAT YOU DO:
> nec2d.exe couple1.nec # couple1.nec is the
> file you want to prcess
>
> rather, what you do is in windows cmd:
>
> nec2d.exe
>  PROGRAM PRINTS OUT STUFF*
>  PROGRAM PRINTS OUT STUFF*
>  PROGRAM PRINTS OUT STUFF*
>  PROGRAM PRINTS OUT STUFF*
> Please enter input file:  <- THIS IS WHERE THE USER IS ASKED
> TO TYPE IN THE FILENAME
>
> everybody thanks for your help
> -- Kiran


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


Re: how to add class attributes in __new__

2007-02-01 Thread aspineux
On 1 fév, 17:50, "jeremito" <[EMAIL PROTECTED]> wrote:
> I am subclassing the array class and have __new__ to initialize and
> create my class.  In that class I create not only do I create an array
> object, but I also create some other data in __new__ I want to have
> access to outside of __new__.  I tried
>
> self.mydata = mydata
>
> but that didn't work.
>
> Can someone point me in the right direction?
> Thanks,
> Jeremy

self ? in __new__ ?

What about with this ?

def __new__(cls, *args, **kw):
newobj=object.__new__(cls)
newobj.foo='bar'
return newobj

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


Re: asyncore DoS vulnerability

2007-02-02 Thread aspineux
Did you take a look for "too many file descriptors in select()" on
google.

On 1 fév, 20:18, "billie" <[EMAIL PROTECTED]> wrote:
> Hi all. I've just terminated a server application using asyncore /
> asynchat frameworks.
> I wrote a test script that performs a lot of connections to the server
> app and I discovered that asyncore (or better, select()) can manage
> only a limited number of file descriptors (aka simultaneous
> connections).

I thing this is the system that limit the number of open sockets or
select() list

> When this number is reached select() raises an error but asyncore
> doesn't handle this exception (a crash occurs).

This is not a CRASH, It looks an exception with a "Traceback", this is
the normal way python report problems, nothing wrong with that.
You can handle it with a try: except:

> If you want to try this I pasted two scripts below: a server and a
> client.
> On my Windows XP system server.py the crash occurs when 512
> simultaneous connections are reached.

512 is probably a fixed limit into XP, win2k3 or win2k server will
accept more.
Maybe It's possible to increase this value somewhere in the registry.
If not this is how microsoft justify the difference between server and
workstation products :-)

> Here's the traceback:
>
> Traceback (most recent call last):
>   File "C:\Documents and Settings\root\Desktop\test.py", line 31, in ?
> asyncore.loop(timeout=1)
>   File "C:\Python24\lib\asyncore.py", line 192, in loop
> poll_fun(timeout, map)
>   File "C:\Python24\lib\asyncore.py", line 122, in poll
> r, w, e = select.select(r, w, e, timeout)
> ValueError: too many file descriptors in select()
>
> Why does this exception isn't handled inside asyncore.py?

To do what ? To raise a custom asyncore error ?
You can can probably run over this limit by starting multiple of your
server process (not thread, process) .

>
> 
> # server.py
> import asyncore, socket
>
> class server(asyncore.dispatcher):
> """The base class for the backend."""
>
> def __init__(self):
> asyncore.dispatcher.__init__(self)
> self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
> self.bind(('', 8080))
> self.listen(5)
>
> def handle_accept(self):
> sock_obj, addr = self.accept()
> handler(sock_obj)
>
> class handler(asyncore.dispatcher):
>
> def __init__(self, sock_obj):
> asyncore.dispatcher.__init__(self, sock=sock_obj)
>
> def handle_write(self):
> pass
>
> def handle_read(self):
> pass
>
> server()
> asyncore.loop(timeout=1)
>
> -
>
> # client.py
> import socket, threading, os
>
> def client():
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> try:
> s.connect(("127.0.0.1", 8080))
> except:
> print x
> os._exit(0)
> while 1:
> s.recv(1024)
>
> x = 0
> while 1:
> x +=1
> threading.Thread(target=client).start()
> print x


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


Re: imaplib : error reporting use 'randomly' exception or return value

2007-02-02 Thread aspineux

I looked carefully imaplib.py and wrote this version of
_simple_command that take care of the way the function is used by
other to keep the same functionality

class bye(imaplib.IMAP4.abort): pass
class bad(imaplib.IMAP4.abort): pass

def _simple_command(self, name, *args):

typ, dat=self._command_complete(name, self._command(name,
*args))
if typ!='OK':
if name in ('LOGOUT',):
return typ, dat

if name in ('EXAMINE', 'SELECT'):
self.state = 'AUTH'

if typ=='BYE': raise self.bye(dat[-1])
elif typ=='BAD': raise self.bad(dat[-1])
else: raise self.error(dat[-1])

return typ, dat


On 1 fév, 23:28, "aspineux" <[EMAIL PROTECTED]> wrote:
> imaplib use exception to report errors,  but some problems must be
> detected by checking the return value !
> For example, when trying to append into a mailbox with wrong ACL,
> imaplib return 'NO', but dont raise any exception (I give a sample at
> the end).
> This make error handling more complicate, because any imap statement
> is supposed to be followed by a test of the returned value!
>
> Why not report all problems using exceptions ?
>
> It easy to modify imaplib.py to manage this because most of the imap
> call are made through function
> _simple_command this way :
>
> def _simple_command(self, name, *args):
> return self._command_complete(name, self._command(name,
> *args))
>
> I propose to replace it by something like :
>
> def _simple_command(self, name, *args):
> typ, dat=self._command_complete(name, self._command(name,
> *args))
> if typ!='OK':
> raise self.error(dat[-1])
> return typ, dat
>
> Any comment ?
>
> Here is an example, append  on a mailbox with the wrong ACL fail by
> returning a 'NO'
>
> import imaplib
>
> server='localhost'
> login='[EMAIL PROTECTED]'
> passwd='password'
>
> c=imaplib.IMAP4(server)
> c.login(login, passwd)
> c.setacl('INBOX', login, '') # set wrong ACL, removing 'i'
> typ, dat=c.append('INBOX', None, None, "From: [EMAIL PROTECTED]: %s
> \nSubject: test append\n\nHello\n" % (login))
> print typ, dat
>
> output:
>
> NO ['Permission denied']


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


Re: result of os.times() is different with 'time' command

2007-02-02 Thread aspineux

I dont see anything wrong !
Did you try to measure time with your watch ?
Did you try a simple python test.py without the time command ?
Maybe python is 'disturbed' by the intel core

Here my result on a linux dual AMD, python 2.4.3

# time python test.py
n=35, v=14930352
utime=22.54, stime=0.02

real0m22.755s
user0m22.564s
sys 0m0.022s

can you try this ?

# strace python test.py  2>&1 | grep time
times({tms_utime=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
43021
times({tms_utime=2238, tms_stime=2, tms_cutime=0, tms_cstime=0}) =
430220049
write(1, "n=35, v=14930352\nutime=22.37, st"..., 41n=35, v=14930352
utime=22.37, stime=0.01

now you can compare what your system replied and what python
returned !


On 2 fév, 19:30, [EMAIL PROTECTED] wrote:
> Hi,
>
> I have a question about os.times().
> os.times() returns a tuple containing user time and system time,
> but it is not matched to the result of 'time' command.
> For example, os.times() reports that user time is 39.85 sec,
> but 'time' command reports that user time is 28.55sec.
> (machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
> duo)
>
>   file: ostimetest.py
>   
>   import os
>
>   ## benchmark function
>   def f(x):
>   if x <= 1:
>   return 1
>   else:
>   return f(x-1) + f(x-2)
>
>   ## do benchmark
>   n = 35
>   t1 = os.times() # start time
>   v = f(n)# end time
>   print "n=%d, v=%d" % (n, v)
>   t2 = os.times()
>
>   ## print result
>   utime = t2[0] - t1[0]   # user time
>   stime = t2[1] - t1[1]   # system time
>   print "utime=%s, stime=%s" % (utime, stime)
>   
>
>   Result:
>   
>   $ python -V
>   Python 2.5
>   $ time python ostimetest.py
>   n=35, v=14930352
>   utime=39.85, stime=0.2167
>   real0m28.554suser0m23.938ssys 0m0.177s
>   
>
> This shows that os.times() reports that user time is 39.85sec,
> but time command shows that user time is 23.938sec.
> Why os.times() reports wrong result? Do I have any mistake?
>
> --
> kwatch


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


Re: asyncore DoS vulnerability

2007-02-03 Thread aspineux
On 2 fév, 16:32, "billie" <[EMAIL PROTECTED]> wrote:
> >> Why does this exception isn't handled inside asyncore.py?
> > To do what ? To raise a custom asyncore error ?
>
> asyncore aims to be a framework, right?
> I think that when select() limit is reached asyncore should just drop
> other connections. That's all.

Nice idea.
It shoul be nice to be able to limit the number of connections
asyncore can manage, to say,
limit the load of the server, or in your case tell asyncore you are
working on a poor platform :-)

Then asyncore could call a user defined function (a policy) when this
limit is reached.
This function could interact with asyncore to take an action: reject
the connection, close idle connections .

You could try to persuade asyncore developers to include this feature.


> > You can can probably run over this limit by starting multiple of your
> > server process (not thread, process).
>
> Hope you're joking...
> Why should I have to run multiple processes / threads to avoid such a
> problem?
> And what if my system / inteprepter does not support multiple
> processes / threads?

I was just giving a possible workaround.

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


Re: result of os.times() is different with 'time' command

2007-02-03 Thread aspineux

Your analysis looks great, maybe the good arguments to report a bug ?


On 3 fév, 04:27, [EMAIL PROTECTED] (Douglas Wells) wrote:
> [various posting problems corrected and response interspersed in
> previous post for purposes of coherent response]
>
> In article <[EMAIL PROTECTED]>,
>
>
>
> "aspineux" <[EMAIL PROTECTED]> writes:
> > On 2 Feb, 19:30, [EMAIL PROTECTED] wrote:
> > > Hi,
>
> > > I have a question about os.times().
> > > os.times() returns a tuple containing user time and system time,
> > > but it is not matched to the result of 'time' command.
> > > For example, os.times() reports that user time is 39.85 sec,
> > > but 'time' command reports that user time is 28.55sec.
> > > (machine: Python2.5, MacOS X 10.4 Tiger, MacBook 1.83GHz intel core
> > > duo)
>
> > >   [ source elided ]
>
> > I dont see anything wrong !
> > Did you try to measure time with your watch ?
> > Did you try a simple python test.py without the time command ?
> > Maybe python is 'disturbed' by the intel core
>
> > can you try this ?
>
> > # strace python test.py  2>&1 | grep time
> > times({tms_utime=1, tms_stime=1, tms_cutime=0, tms_cstime=0}) =
> > 43021
> > times({tms_utime=2238, tms_stime=2, tms_cutime=0, tms_cstime=0})  = 
> > 430220049
> > write(1, "n=35, v=14930352\nutime=22.37, st"..., 41n=35, v=14930 52
> > utime=22.37, stime=0.01
>
> Note that this likely won't work.  First, strace is not native to
> OS X; ktrace is the analogous native command.  Second, OS X almost
> certainly implements the times system call in terms of getrusage.
>
>
>
> > >   Result:
> > >   
> > >   $ python -V
> > >   Python 2.5
> > >   $ time python ostimetest.py
> > >   n=35, v=14930352
> > >   utime=39.85, stime=0.2167
> > >   real0m28.554suser0m23.938ssys 0m0.177s
> > >   
>
> > > This shows that os.times() reports that user time is 39.85sec,
> > > but time command shows that user time is 23.938sec.
> > > Why os.times() reports wrong result? Do I have any mistake?
>
> > > --
> > > kwatch
>
> Yes, I can reproduce this on my FreeBSD system.  No, I do not believe
> that you have made a mistake.  Yes, I believe that you have uncovered
> a bug in the Python os/posix modules.
>
> Here's my analysis (although I should note that I've not looked
> at the source of Python previously).  I'm looking at Python 2.4.3,
> but this looks like a long existing bug:
>
> The following code exists in the source code module
> Modules/posixmodule.c @ posix_times:
> struct tms t;
> clock_t c;
> [ ... ]
> c = times(&t);
> [ ... ]
> return Py_BuildValue("d",
>  (double)t.tms_utime / HZ,
>  (double)t.tms_stime / HZ,
>  (double)t.tms_cutime / HZ,
>  (double)t.tms_cstime / HZ,
>  (double)c / HZ);
> This is incorrect.  It should not be dividing by HZ, but by the
> result of the dynamic value 'sysconf (_SC_CLK_TCK)'.  Even if
> it were to use a compile time value, the proper value would be
> CLK_TCK, not HZ.
>
> So here's what's happening.  Neither my FreeBSD nor the OP's Mac
> defines HZ as a compile time variable, but the same source module
> also contains the following code:
> #ifndef HZ
> #define HZ 60 /* Universal constant :-) */
> #endif /* HZ */
> So, the Python posix module is using 60 instead of the proper
> value, which happens to be 128 on my FreeBSD, and 100 on the OP's
> OS X(*).  (BTW, this sort of historic code is exactly why POSIX
> no longer defines HZ.)
>
> In support of this, I note that the following ratios exist:
>   user time from os.times / user time from time command
> 39.85 / 23.938 => 1.665
>   CLK_TCK / HZ
>   100 / 60 => 1.667
> which are in reasonably close agreement!
>
>  - dmw
>
> [*] I've actually only looked at OS X for the PPC platform, not
> for the User's Intel platform, but I'm fairly certain that the
> CLK_TCK value is the same on both.
>
> --
> .   Douglas Wells .  Connection Technologies  .
> .   Internet:  -sp9804- -at - contek.com- .


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


Re: favourite editor

2007-02-13 Thread aspineux

I'm very happy with eclipse and its python module called pydev.

It's a little slow because it parse continuously the code (P4 >2.0Ghz
required), but give
nice realtime info by underlining syntax error or unknown variable
(very very useful when
miss spelling a variable or a function :-)


On 11 fév, 05:41, "azrael" <[EMAIL PROTECTED]> wrote:
> Since i'm new on this forum, and first time meeting a python comunity,
> i wanted to ask you for your python editors.
>
> Im looking for some good python editor, with integrated run function,
> without having to set it up manualy like komodo.
> I found the pyscripter, and it has all i need, but it's unsatble.
> bloated. it crashes when i close an yplication window run by python
> from pyscripter. please. tell me a good one with buil in run (<-very
> important) and nice gui. if possible to suport projects, code
> highlighting, code completition, class browser, python comand line
> (>>>), traceback.
>
> I didn't take a look on vista (and i dont want to), but i hope they
> improved the notepad.


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


How a smart editor could make "Postfix type declarations PEP3117" in Python3000 more readable

2008-01-04 Thread aspineux
Hi

I read the PEP 3117 about the new "Postfix type declarations"  in
Python3000.
THIS PEP as been REJECTED ! But ...

The notation in the PEP is very ugly !  This make python code more
difficult to read!

Anyway when I switched to python (from C, C++, ..), I suffered a lot
of the
untyped python variables. And I think this is a good idea to include
typing in python.

Then I get this idea: The editor could hide the typing notation, just
displaying hint !
It could also auto-complete the type of any variable having already a
type in
the current function, and underline untyped variable or variable
having multiple type inside the function.

Just an idea !


Alain Spineux

Happy new year.


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


Re: How a smart editor could make "Postfix type declarations PEP3117" in Python3000 more readable

2008-01-05 Thread aspineux
On Jan 5, 4:39 am, aspineux <[EMAIL PROTECTED]> wrote:
> Hi
>
> I read the PEP 3117 about the new "Postfix type declarations"  in
> Python3000.
> THIS PEP as been REJECTED ! But ...
>
> The notation in the PEP is very ugly !  This make python code more
> difficult to read!
>
> Anyway when I switched to python (from C, C++, ..), I suffered a lot
> of the
> untyped python variables. And I think this is a good idea to include
> typing in python.
>
> Then I get this idea: The editor could hide the typing notation, just
> displaying hint !
> It could also auto-complete the type of any variable having already a
> type in
> the current function, and underline untyped variable or variable
> having multiple type inside the function.
>
> Just an idea !

And to go further the editor could do all the job of type checking,
using formatted comment to specify type, like in some existing
embedded documentation.


But then we are losing the brevity provided by the PEP.


Pydev (and certainly other) already does some interesting work to find
mistyped
(typed like in "I made a typo") variable name.

TO ALL "NEW IDEA" RESISTANT :

Hopefully, in 1990 nobody said to someone that inventing a language
where bloc definition
is based on indentation was a s


Regards

>
> Alain Spineux
>
> Happy new year.

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


short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread aspineux

This append in both case

dict(a=1).get('a', f())
dict(a=1).setdefault('a', f())

This should be nice if f() was called only if required.

Regards.

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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-14 Thread aspineux
On Jan 14, 7:49 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
> On Jan 14, 2008 12:39 PM, aspineux <[EMAIL PROTECTED]> wrote:
>
>
>
> > This append in both case
>
> > dict(a=1).get('a', f())
> > dict(a=1).setdefault('a', f())
>
> > This should be nice if f() was called only if required.
>
> Think about the change to Python semantics that would be required for
> this to be true, and then use collections.defaultdict instead.

Yes, I missed 'get' and 'setdefault' are functions :-)
Then why not some new semantic

d.get('a', f()) --> d['a', f()]
d.setdefault('a', f()) --> d['a'=f()]

Is is a good idea enough to change the python semantic ?
Or simply is it a good idea ?


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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-15 Thread aspineux
On Jan 15, 12:15 am, Paul Rubin  wrote:
> Steven D'Aprano <[EMAIL PROTECTED]> writes:
> > map = {'a': Aclass, 'b': Bclass, 'c': Cclass}
> > class_ = map.get(astring, default=Zclass)
>
> > The result I want is the class, not the result of calling the class
> > (which would be an instance). If I wanted the other semantics, I'd be
> > using defaultdict instead.
>
> I used default as a keyward arg name indicating the presence of
> a callable.  I probably should have called it defaultfunc or something.
>
> x = d.get('a', f)  # --> default value is f
> x = d.get('a', defaultfunc=f)  # --> default value is result of f() .

Nice idea, but if I want args I need to write it like that:

x=d.get('a', defaultfunc=f, funcargs=(1,2,3))

instead of d['a', f(1,2,3)]

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


Re: short path evaluation, why is f() called here: dict(a=1).get('a', f())

2008-01-15 Thread aspineux
On Jan 14, 8:07 pm, aspineux <[EMAIL PROTECTED]> wrote:
> On Jan 14, 7:49 pm, "Chris Mellon" <[EMAIL PROTECTED]> wrote:
>
> > On Jan 14, 2008 12:39 PM, aspineux <[EMAIL PROTECTED]> wrote:
>
> > > This append in both case
>
> > > dict(a=1).get('a', f())
> > > dict(a=1).setdefault('a', f())
>
> > > This should be nice if f() was called only if required.
>
> > Think about the change to Python semantics that would be required for
> > this to be true, and then use collections.defaultdict instead.
>
> Yes, I missed 'get' and 'setdefault' are functions :-)
> Then why not some new semantic
>
> d.get('a', f()) --> d['a', f()]
> d.setdefault('a', f()) --> d['a'=f()]
>
> Is is a good idea enough to change the python semantic ?
> Or simply is it a good idea ?

Thanks for all your answers.

Anyway these notations are very compact,
don't require the definition of a specific function,
and work with old style/or already existing dictionary,
dictionary you didn't created yourself.

While the use of defaultdict require the definition of such a function
and to control the creation of the dictionary.

For me the best alternative that match the requirement above is
the one provided by Paul Rubin.


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


/kolab/bin/python: double free or corruption (fasttop)

2008-12-28 Thread aspineux
I got this.
This is a test script, to help me to understand why I have unexpected
result in application.
But I got a more unexpected result, and probably wrong error message
about the read-only cursor.
The full script is at the end.


db cleanup, 326 records deleted, 9990 remains
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/kolab/lib/python/threading.py", line 486, in
__bootstrap_inner
self.run()
  File "/kolab/lib/python/threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
  File "./test/test_bdb.py", line 85, in 
update_thread=threading.Thread(target=lambda : cleanup(db))
  File "./test/test_bdb.py", line 72, in cleanup
cursor.delete()
DBPermissionsError: (1, 'Operation not permitted -- Write attempted on
read-
only
cursor')

Traceback (most recent call last):
  File "./test/test_bdb.py", line 130, in 
server()
  File "./test/test_bdb.py", line 92, in server
db[str(last)]=str(last)
KeyboardInterrupt
*** glibc detected *** /kolab/bin/python: double free or corruption
(fasttop):
0
x09d521a8 ***
=== Backtrace: =
/lib/i686/nosegneg/libc.so.6[0xccfd]
/lib/i686/nosegneg/libc.so.6(cfree+0x90)[0x444503b0]
/kolab/bin/python(__db_c_destroy+0x60)[0x81fa6f0]
/kolab/bin/python(__db_refresh+0x165)[0x81f3515]
/kolab/bin/python(__db_close+0x64)[0x81f3cb4]
/kolab/bin/python(__db_close_pp+0xa1)[0x8204b61]
/kolab/bin/python[0x812123f]
/kolab/bin/python[0x8159534]
/kolab/bin/python[0x815fb0a]
/kolab/bin/python[0x811abbc]
/kolab/bin/python[0x811abc9]
/kolab/bin/python[0x80b0229]
/kolab/bin/python(PyDict_SetItem+0x6e)[0x80b1c0e]
/kolab/bin/python(PyDict_SetItemString+0x42)[0x80b1ce2]
/kolab/bin/python(PyImport_Cleanup+0xd4)[0x8108294]
/kolab/bin/python(Py_Finalize+0xbf)[0x8113f1f]
/kolab/bin/python(Py_Main+0x468)[0x80845c8]
/kolab/bin/python(main+0x22)[0x8084052]
/lib/i686/nosegneg/libc.so.6(__libc_start_main+0xdc)[0x443fbdec]
/kolab/bin/python[0x8083fa1]
=== Memory map: 
0011-001d7000 rwxp 0011 00:00 0
001d7000-001da000 r-xp  ca:03 973941 /kolab/lib/python/lib-
dynload/
t
ime.so
001da000-001dc000 rwxp 2000 ca:03 973941 /kolab/lib/python/lib-
dynload/
t
ime.so
001dc000-001e r-xp  ca:03 973938 /kolab/lib/python/lib-
dynload/
s
trop.so
001e-001e2000 rwxp 3000 ca:03 973938 /kolab/lib/python/lib-
dynload/
s
trop.so
001e2000-001e3000 r-xp  ca:03 973911 /kolab/lib/python/lib-
dynload/
_
weakref.so
001e3000-001e4000 rwxp  ca:03 973911 /kolab/lib/python/lib-
dynload/
_
weakref.so
001e4000-001ea000 rwxs  ca:03 1747650/tmp/db/__db.001
001ea000-001fc000 rwxs  ca:03 1747651/tmp/db/__db.002
001fc000-0023e000 rwxs  ca:03 1747652/tmp/db/__db.003
00388000-003e rwxs  ca:03 1747653/tmp/db/__db.004
0045-00452000 r-xp  ca:03 1132355/lib/libutil-2.5.so
00452000-00453000 r-xp 1000 ca:03 1132355/lib/libutil-2.5.so
00453000-00454000 rwxp 2000 ca:03 1132355/lib/libutil-2.5.so
00459000-0047e000 r-xp  ca:03 1132349/lib/i686/nosegneg/
libm-2.5.so
0047e000-0047f000 r-xp 00024000 ca:03 1132349/lib/i686/nosegneg/
libm-2.5.so
0047f000-0048 rwxp 00025000 ca:03 1132349/lib/i686/nosegneg/
libm-2.5.so
0063e000-0063f000 r-xp 0063e000 00:00 0  [vdso]
0097a000-0098d000 r-xp  ca:03 1134447/lib/i686/nosegneg/
libpthread-2
  .
5.so
0098d000-0098e000 r-xp 00012000 ca:03 1134447/lib/i686/nosegneg/
libpthread-2
  .
5.so
0098e000-0098f000 rwxp 00013000 ca:03 1134447/lib/i686/nosegneg/
libpthread-2
  .
5.so
0098f000-00991000 rwxp 0098f000 00:00 0
009a8000-009e9000 rwxp 009a8000 00:00 0
00b8-00d8 r-xp  ca:03 845325 /usr/lib/locale/
locale-archive
00ec9000-00ecc000 r-xp  ca:03 973916 /kolab/lib/python/lib-
dynload/
c
StringIO.so
00ecc000-00ecd000 rwxp 3000 ca:03 973916 /kolab/lib/python/lib-
dynload/
c
StringIO.so
00fd2000-00fd6000 r-xp  ca:03 973918 /kolab/lib/python/lib-
dynload/
c
ollections.so
00fd6000-00fd7000 rwxp 4000 ca:03 973918 /kolab/lib/python/lib-
dynload/
c
ollections.so
00fd7000-00fd8000 --xp 00fd7000 00:00 0
00fd8000-019d8000 rwxp 00fd8000 00:00 0
08048000-08433000 r-xp  ca:03 430731 /kolab/bin/python
08433000-0846e000 rwxp 003eb000 ca:03 430731 /kolab/bin/python
0846e000-08478000 rwxp 0846e000 00:00 0
09ce5000-09da5000 rwxp 09ce5000 00:00 0
443c4000-443dd000 r-xp  ca:03 1132323/lib/ld-2.5.so
443dd000-443de000 r-xp 00018000 ca:03 1132323/lib/ld-2.5.so
443de000-443df000 rwxp 00

Re: call to pypcap in separate thread blocks other threads

2009-12-30 Thread aspineux
On Dec 30, 1:34 pm, Dmitry Teslenko  wrote:
> Hello!
> I'm making gui gtk application. I'm using pypcap
> (http://code.google.com/p/pypcap/) to sniff some network packets.
> To avoid gui freezing I put pcap call to another thread.
> Pypcap call looks like:
>
> pc = pcap.pcap()
> pc.setfilter('tcp')
> for ts, pkt in pc:
>         spkt = str(pkt)
>         ...
>
> Sadly, but this call in another thread blocks gtk gui thread anyway.
> If I substitute pcap call with something else separate thread don't block
> gui thread.
>
> Using another process instead of thead isn't appropriate.
>
> Thread initialization looks like and takes place before gtk.main():
>
> self.__pcap_thread = threading.Thread(target = self.get_city_from_pcap)
> self.__pcap_thread.start()

Did you try using build-in gtk thread ?

Regards

Alain Spineux |  aspineux gmail com
Your email 100% available |  http://www.emailgency.com
NTBackup frontend sending mail report |  http://www.magikmon.com/mkbackup

>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing in e-mail?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: call to pypcap in separate thread blocks other threads

2009-12-30 Thread aspineux
On Dec 30, 3:07 pm, Dmitry Teslenko  wrote:
> On Wed, Dec 30, 2009 at 16:18, aspineux  wrote:
> > On Dec 30, 1:34 pm, Dmitry Teslenko  wrote:
> >> Hello!
> >> I'm making gui gtk application. I'm using pypcap
> >> (http://code.google.com/p/pypcap/) to sniff some network packets.
> >> To avoid gui freezing I put pcap call to another thread.
> >> Pypcap call looks like:
>
> >> pc = pcap.pcap()
> >> pc.setfilter('tcp')
> >> for ts, pkt in pc:
> >>         spkt = str(pkt)
> >>         ...
>
> >> Sadly, but this call in another thread blocks gtk gui thread anyway.
> >> If I substitute pcap call with something else separate thread don't block
> >> gui thread.
>
> >> Using another process instead of thead isn't appropriate.
>
> >> Thread initialization looks like and takes place before gtk.main():
>
> >> self.__pcap_thread = threading.Thread(target = self.get_city_from_pcap)
> >> self.__pcap_thread.start()
>
> > Did you try using build-in gtk thread ?
>
> > Regards
>
> > Alain Spineux                         |  aspineux gmail com
> > Your email 100% available             |  http://www.emailgency.com
> > NTBackup frontend sending mail report |  http://www.magikmon.com/mkbackup
>
> >> --
> >> A: Because it messes up the order in which people normally read text.
> >> Q: Why is top-posting such a bad thing?
> >> A: Top-posting.
> >> Q: What is the most annoying thing in e-mail?
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> No. What makes it different?

It allow you to access to you GTK widgets from your threads (you have
rules to follow to do that).

What are you doing from inside your "pcap" thread ?



>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is top-posting such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing in e-mail?

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


generating .zip including __main__.py file in top root using distutils

2011-02-12 Thread aspineux
Python 2.6 can run a zip file, searching for __main__.py in the root
of the zip archive and running it.
How can I create such an archive using distutils (and not
setuptools) ?

If I use
# python setup.py bdist --format=zip
I get a "dumb" zip file with a deep tree structure from "/" and I
cannot put the __main__.py in the root archive

If I use
# python setup.py sdist --format=zip
I get a more compact tree. But the tree start bellow directory named
"mypackage-version",

my-package-1.0/
  setup.py
  __main__.py
  

If I use setuptool, generating .EGG I get exacly what I want !
But I read it is bad to use setuptool and EGGs since pip is
available :-)

I was hopping distutils have been updated when adding the zip trick,
but I didn't find the trick.

Help




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


Re: Question on Creating exe file with py2exe

2011-02-12 Thread aspineux
Hi

I'm releasing a .exe made with py2exe myself an got this problem too.
99% of the time the required DLL is already installed by another
application and you don't need to care about it.
The 1% is about empty or fresh windows install (server most of the
time)
For them, I provide a link to the M$ vcredist and a warning in my
download page asking them to install it them self.

Take a look at my site for the vcredist link :
http://www.magikmon.com/mksbackup/download.en.html

Regards

Alain

On Feb 12, 9:06 pm, joy99  wrote:
> Dear Room,
>
> I am using Windows XP (SP2) and a Python Version "Python 2.6.5
> (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
> win32".
>
> I was looking to create exe files. Reviewing various posts here, I
> felt py2exe may be the best thing I can opt for.
>
> But for Python2.6 I found the following note in the py2exe tutorial:
> "For Python 2.6, the DLL you need is called MSVCR90.dll. Py2exe is not
> able to automatically include this DLL in your dist directory, so you
> must provide it yourself.
>
> To complicate things, there is more than one version of this DLL in
> existance, each with the same filename. You need the same version that
> the Python interpreter was compiled with, which is version
> 9.0.21022.8. Through the remainder of these instructions, hover your
> mouse over the dll file (or the vcredist_x86.exe installer executable)
> to confirm which version you've got. "
>
> My questions are:
> (i) From where I can download "MSVCR90.dll" ? Is there any trusted
> site?
> (ii) How to install the same?
> (iii) Would py2exe work fine if I install it?
> (iv) Is there any other exe creating program which does not have all
> these problems?
>
> As it is a room for expert python developers, I felt to ask you, if
> any one can kindly find some time to resolve my query, I would be
> honored.
>
> Thanks in Advance,
> Best Regards,
> Subhabrata.

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


Re: files,folders,paths

2011-02-13 Thread aspineux
Without trying to understand your code, I see one difference

   if not os.path.isfile(destination+leftover+fname):

become

   if not os.path.isfile(destination+leftover):


Regards


On Feb 13, 11:06 am, ecu_jon  wrote:
> i have a samba server at home (acting as my test environment) with one
> of the 'shares' mounted as v: on my windows box. inside of that are 4
> folders week[1-4]. i have created c:\users\name\backup , and put a few
> files/folders in there to test with. please ignore the wxpython parts
> of the script, the last 1/3, that part is functional. initial test was
> looking good. (in the code, ver011 source1, destination1) coping files/
> folders and not clobbering files. going to version 12a, it seems like
> it is trying to copy the files under the folder, before making the
> folder. or doing the copy2 part before the copytree part. i dont know
> why it works on ver011, and not in ver12a, the only difference being
> the destination.
> http://thanksforallthefish.endofinternet.net/
> facbac-011.py">facbac 011
> http://thanksforallthefish.endofinternet.net/
> facbac-012a.py">facbac 012a
> Traceback (most recent call last):
>   File "I:\college\spring11\capstone-project\facbac-012a.py", line
> 161, in button1Click
>     backupall()
>   File "I:\college\spring11\capstone-project\facbac-012a.py", line 81,
> in backupall
>     shutil.copy2(filesource, filedest1)
>   File "C:\Python27\lib\shutil.py", line 127, in copy2
>     copyfile(src, dst)
>   File "C:\Python27\lib\shutil.py", line 82, in copyfile
>     with open(dst, 'wb') as fdst:
> IOError: [Errno 2] No such file or directory: 'V:\\week2\\configs\
> \apache2.conf'
>
> here, apache2.conf = c:\users\name\backup\config\apache2.conf
> trying to copy to v:\week2\config\apache2.conf
> but there is no v:\week2\config
> as it has not made the config folder yet
> i think the problem might be the \\ in the destination name.

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


Re: How to create a dict based on such a file?

2011-02-13 Thread aspineux
On 14 fév, 06:47, Wang Coeus  wrote:
> Hi all,
> I am new to python. Currently I encountered a problem, please help me to
> solve this. Thanks in advance!
> I have a file like below:

ConfigParser Library does exacly what you want but with .ini file
format
[block1]
key1=value1
key2=value2
...

Can you change the format of your file ? If so

import ConfigParser
config=ConfigParser.RawConfigParser(config_default)
try:
config.readfp(open(filename, 'r'))
except Exception, e:
logging.error('error reading configuration file %s: %s', filename,
e)
sys.exit(1)

def func(config, key1):
result={}
for section in config.sections():
if config.has_option(section, key1):
result[section]=config.get(section, key1)
return result


If not, you need to parse youre file, and the some question :
How or what generate this file, is it always the same format ? Could
it chnage, for exemple for

block1 { key1=value1 key2=value2 }

or at least

block1 {

key1=value1
key2=value2


}

Is-it big, too big to keep in memory ?



> ++
> block1
> {
>   key1=value1
>   key2=value2
>   key3=value3}
>
> block2
> {
>   key1=value4
>   key2=value5
>   key4=value6}
>
> ...
> blockn
> {
>   key1=value7
>   key2=value8
>   keyn=valuen}
>
> +++
> Different block may have different keys and even same key in different
> blocks may have different values.
>
> Now I want to get a function, which like this:
> func(key)
> and it will return a dictionary as below:
> func(key1) = [block1:value1,block2:value4,...,blockn:value7]
> and if one block has no "key1" parameter, it will not include in this
> dict.
>
> Thanks a lot!
> --
> Coeus
> In the middle of every difficulty lies opportunity.
>                 -- Albert Einstein

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


Re: Question on Creating exe file with py2exe

2011-02-15 Thread aspineux
On 13 fév, 06:20, joy99  wrote:
> On Feb 13, 1:29 am, aspineux  wrote:
>
>
>
>
>
> > Hi
>
> > I'm releasing a .exe made with py2exe myself an got this problem too.
> > 99% of the time the required DLL is already installed by another
> > application and you don't need to care about it.
> > The 1% is about empty or fresh windows install (server most of the
> > time)
> > For them, I provide a link to the M$ vcredist and a warning in my
> > download page asking them to install it them self.
>
> > Take a look at my site for the vcredist link 
> > :http://www.magikmon.com/mksbackup/download.en.html
>
> > Regards
>
> > Alain
>
> > On Feb 12, 9:06 pm, joy99  wrote:
>
> > > Dear Room,
>
> > > I am using Windows XP (SP2) and a Python Version "Python 2.6.5
> > > (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
> > > win32".
>
> > > I was looking to create exe files. Reviewing various posts here, I
> > > felt py2exe may be the best thing I can opt for.
>
> > > But for Python2.6 I found the following note in the py2exe tutorial:
> > > "For Python 2.6, the DLL you need is called MSVCR90.dll. Py2exe is not
> > > able to automatically include this DLL in your dist directory, so you
> > > must provide it yourself.
>
> > > To complicate things, there is more than one version of this DLL in
> > > existance, each with the same filename. You need the same version that
> > > the Python interpreter was compiled with, which is version
> > > 9.0.21022.8. Through the remainder of these instructions, hover your
> > > mouse over the dll file (or the vcredist_x86.exe installer executable)
> > > to confirm which version you've got. "
>
> > > My questions are:
> > > (i) From where I can download "MSVCR90.dll" ? Is there any trusted
> > > site?
> > > (ii) How to install the same?
> > > (iii) Would py2exe work fine if I install it?
> > > (iv) Is there any other exe creating program which does not have all
> > > these problems?
>
> > > As it is a room for expert python developers, I felt to ask you, if
> > > any one can kindly find some time to resolve my query, I would be
> > > honored.
>
> > > Thanks in Advance,
> > > Best Regards,
> > > Subhabrata.
>
> Hi Alain,
> Thank you for your product information. But, with my problem
> installation is okay.
> The first two steps:
> "from distutils.core import setup
>  import py2exe"
> but as I am giving
> "setup(console=['file.py'])"
>
> the setup file is not being generated.

What do you mean ? Do you expect the 3 lines above will generate a
setup.py file ?
You are wrong you must create your setup.py file yourself, and this
file must contains
the 3 lines above, and more a lot more ..
Then you will run

 python setup.py py2exe

to generate your .exe file


> It is giving some error
> messages.
> Any suggestions?
> Best Regards,
> Subhabrata.- Masquer le texte des messages précédents -
>
> - Afficher le texte des messages précédents -

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


Re: email library, get_payload() bug?

2011-02-18 Thread aspineux
On 18 fév, 14:41, peterob  wrote:
> Helllo,
>
> I want to get the raw format of email body. the code Im using is
>
>         for part in email.walk():
>                 if part.get_content_maintype() == 'multipart':
>                         continue
>                 if part.get_content_type() == 'text/plain':
> #                       print part.get_content_type()
>                         print part.get_payload(None,True)
>
> The problem is, that code adds one more '\n' at the and of the output
> printed by get_payload(). Is that bug?
>
> Thanks.
>
> Best,
> Peter

No sure to undeerstand, but print add one '\n' by itself
maybe using

print part.get_payload(None,True),

with a "," at the end to tell print not to add a "\n"

hope this help
-- 
http://mail.python.org/mailman/listinfo/python-list