Re: Event Handling and Signal-Slot Mechanism

2009-01-18 Thread Steven Woody
On Mon, Jan 19, 2009 at 11:29 AM, James Mills
 wrote:
> On Mon, Jan 19, 2009 at 1:10 PM, Steven Woody  wrote:
>> Python has Signal-Slot mechanism, why he still need another mechanism
>> Event Handling?  And, in some cases, it seems only Event Handling
>> mechanism is available, for example closeEvent().  For what case and
>> for what reason, the python think Signal Slot is not enough and will
>> not work?
>
> In what context ? What application ?
>

I am reading mark summerfield's book Rapid GUI Programming with Python
and Qt, chapter 6. In the example code, it inserted customized
behavior when user selects file->exit by overriding closeEvent() event
handler, but in other context, when a behavior is needed to handle
user interaction, the single-slot mechanism is always used.
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem in implementing multiprocessing

2009-01-18 Thread James Mills
On Mon, Jan 19, 2009 at 3:50 PM, gopal mishra  wrote:
> i know this is not an io - bound problem, i am creating heavy objects in the
> process and add these objects in to queue and get that object in my main
> program using queue.
> you can test the this sample code
> import time
> from multiprocessing import Process, Queue
>
> class Data(object):
>def __init__(self):
>self.y = range(1, 100)
>
> def getdata(queue):
>data = Data()
>queue.put(data)
>
> if __name__=='__main__':
>t1 = time.time()
>d1 = Data()
>d2 = Data()
>t2 = time.time()
>print "without multiProcessing total time:", t2-t1
>#multiProcessing
>queue = Queue()
>Process(target= getdata, args=(queue, )).start()
>Process(target= getdata, args=(queue, )).start()
>s1 = queue.get()
>s2 = queue.get()
>t2 = time.time()
>print "multiProcessing total time::", t2-t1

The reason your code above doesn't work as you
expect and the multiprocessing part takes longer
is because your Data objects are creating a list
(a rather large list) of ints. Use xrange instead of range.

Here's what I get (using xrange):

$ python test.py
without multiProcessing total time: 1.50203704834e-05
multiProcessing total time:: 0.116630077362

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


RE: problem in implementing multiprocessing

2009-01-18 Thread gopal mishra

i know this is not an io - bound problem, i am creating heavy objects in the
process and add these objects in to queue and get that object in my main
program using queue.
you can test the this sample code
import time
from multiprocessing import Process, Queue

class Data(object):
def __init__(self):
self.y = range(1, 100)

def getdata(queue):
data = Data()
queue.put(data)

if __name__=='__main__':
t1 = time.time()
d1 = Data()
d2 = Data()
t2 = time.time()
print "without multiProcessing total time:", t2-t1
#multiProcessing
queue = Queue()
Process(target= getdata, args=(queue, )).start()
Process(target= getdata, args=(queue, )).start()
s1 = queue.get()
s2 = queue.get()
t2 = time.time()
print "multiProcessing total time::", t2-t1



-Original Message-
From: James Mills [mailto:prolo...@shortcircuit.net.au] 
Sent: Saturday, January 17, 2009 10:37 AM
To: gopal mishra
Cc: python-list@python.org
Subject: Re: problem in implementing multiprocessing

On Fri, Jan 16, 2009 at 7:16 PM, gopal mishra  wrote:
> I create two heavy objects sequentially without using multipleProcessing
> then creation of the objects takes 2.5 sec.if i create these two objects
in
> separate process then total time is 6.4 sec.
>
> i am thinking it is happening due to the pickling and unpickling of the
> objects.if i am right then what could be the sollution.
>
> my system configuration:
> dual-core processor
> winXP
> python2.6.1

System specs in this case are irrelevant.

What you are experiencing is most likely an I/O
bound problem - using multiprocessing may likely
not help you solve the problem any faster because of
your I/O constraint.

cheers
James

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


Re: Python 3: exec arg 1

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 11:06:10 -0600, Rob Williscroft wrote:

> You must have missed the subject line: "Re: Python 3: exec arg 1"

Doh!



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


Re: Relax Syntax for Augmented Arithmetic?

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 15:11:46 -0500, Terry Reedy wrote:

> andrew cooke wrote:
>> Context -
>> http://docs.python.org/3.0/reference/datamodel.html?highlight=data
>> model#object.__iadd__
>> 
>> Just a suggestion I thought I'd throw out...  There's a restriction in
>> the language implementation on exactly what can go the left of an
>> augmented arithmetic expression.
>> 
>> For example:
> a = 3
> a **= 2
>> 
>> is ok, but:
> class Foo():
>> ...   def __init__():
>> ... self.a = 3
>> ...   def __ipow__(self, x):
>> ... self.a **= x
>> ...
> Foo() **= 2
> 
> Calls return objects and therefore cannot be the target of an
> assignment, augmented or otherwise.  The target of an assignment is a
> name or collection slot, both of which are grammatical constructs, not
> objects.

There's a practical reason too. You create a new Foo instance, mutate it 
with the augmented assignment operator, and then a tenth of a millisecond 
later the garbage collector throws it away because it has a reference 
count of zero.



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


Re: function argument dependent on another function argument?

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 22:28:04 +, Steven D'Aprano wrote:

> Built-ins rarely accept None as a sentinel, slice() being a conspicuous
> exception. This is sometimes a nuisance when writing wrappers:
> 
> def my_find(S, sub, start=None, end=None):
> """Like string.find() only with pre-processing.""" pre_process()  #
> stub for something complicated if end is None and start is None:
> return S.find(sub)
> elif end if None:
> return S.find(sub, start)
> else:
> return S.find(sub, start, end)

Typical.

As of Python 2.6, string.find accepts None as sentinels. All my 
beautiful, beautiful code made obsolete!!! *wink*


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


Re: Event Handling and Signal-Slot Mechanism

2009-01-18 Thread James Mills
On Mon, Jan 19, 2009 at 1:10 PM, Steven Woody  wrote:
> Python has Signal-Slot mechanism, why he still need another mechanism
> Event Handling?  And, in some cases, it seems only Event Handling
> mechanism is available, for example closeEvent().  For what case and
> for what reason, the python think Signal Slot is not enough and will
> not work?

In what context ? What application ?

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


Event Handling and Signal-Slot Mechanism

2009-01-18 Thread Steven Woody
Hi,

Python has Signal-Slot mechanism, why he still need another mechanism
Event Handling?  And, in some cases, it seems only Event Handling
mechanism is available, for example closeEvent().  For what case and
for what reason, the python think Signal Slot is not enough and will
not work?

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


Re: function argument dependent on another function argument?

2009-01-18 Thread Aaron Brady
On Jan 18, 12:02 pm, Rob Williscroft  wrote:
> Aaron Brady wrote 
> innews:582ef883-0176-4984-9521-6c1894636...@a26g2000prf.googlegroups.com
> in comp.lang.python:
>
>
>
> > On Jan 18, 10:44 am, Rob Williscroft  wrote:
> >> Aaron Brady wrote
> >> innews:6a10378f-addb-4d56-bc1b-0c382b3cb...@t26g2000prh
> > .googlegroups.com
> >> in comp.lang.python:
>
> >> > It is too bad that it is so much work to detect whether 'y' was
> >> > passed in the function call directly.  However, sentinel is just as
> >> > good (or nearly); at worst, you need one sentinel per argument per
> >> > function,
>
> >> One per Module should be good enough. The only reason None doesen't
> >> suffice is that it has other legitimate uses.  Though to be honest
> >> I would always use None as the sentinel if it wasn't a legitimate
> >> argument.
>
> >> > which is possible to create, which has a specific meaning.  If you
> >> > are
> >> > making systematic function calls, e.g. with a dictionary or list,
> >> > you can just use the sentinel in the dictionary.
>
> >> IIUYC then, one sentinel is still only needed as the missing argument
> >> is indicated by *both* position and value or by name and value (in
> >> the case of a keyword-dictionary), so seperate distinct sentinel
> >> objects aren't required, for example:
>
> >> SENTINEL = object()
>
> >> def f( a, b, c = SENTINEL, d = SENTINEL ):
> >>   print( "values: %r" % ( ( a, b, c, d ), ) )
> >>   if c is SENTINEL:
> >>     print( "c is missing" )
> >>   if d is SENTINEL:
> >>     print( "d is missing" )
>
> >> f( *( 1, 2, SENTINEL, SENTINEL ) )
>
> >> f( **dict( a = 1 , b = 2, d = 4 ) )
>
> >> f( **dict( a = 1 , b = 2, d = 4, c = SENTINEL ) )
> > I don't have a concrete example, so you may prove to be right, but I'm
> > not convinced.
>
> I'm afraid I can't think of a use case for passing default values around
> eiither, and I suspect if we were to come up with one, a better solution
> that didn't involve passing default values around could be found.
>
> > If you have one function with an argument that defaults to an empty
> > list, and calls another with an argument that defaults to an empty
> > dict, then what is the meaning of passing sentinel to the first one?
> > Whereas, if each had their own, then passing the first one's default
> > would mean the empty list, and passing the second one's default would
> > mean the dict.
>
> If you *mean* to pass an "empty list" or "empty dict"'s you should do
> it like:
>
>   function_taking_list( [] )
>   function_taking_dict( {} )
>
> Its when you don't (have reason to) care that you need default arguments.

'None' isn't a valid value for many standard library functions.  So,
if you try to pass it meaning, "Whatever the default value you usually
use is," you'll get an error.  Sometimes, the functions don't even use
a public sentinel, so if you want the default value, you either have
to know what it is, or you can't pass anything to that parameter.
This is usually possible, it just prevents making a uniform call to a
function.  If you need to do some calculations to determine what
parameters you're going to pass, you're stuck testing their presence
with 'if-else' combinations, then calling individually.
--
http://mail.python.org/mailman/listinfo/python-list


Re: function argument dependent on another function argument?

2009-01-18 Thread Aaron Brady
On Jan 18, 12:42 pm, andrew cooke  wrote:
> >     sentinel = object()
> >     ...
>
> >     def foo(x, y=sentinel):
> >       if y is sentinel:
> >           y = self.a
>
> it just struck me you could also do:
>
>      def foo(self, x, *y_args)
>        y = y_args[0] if y_args self.a
>
> which more directly checks whether an argument was passed, but has the
> downside of making the method signature less clear in the declaration.
>
> andrew

Also, if you need to change your calling signature down the line, this
alternative really ties your hands with regard to it.  You also lose
the ability to pass 'y' by keyword.

George Sakkis, who I only know from the NG, has a recipe that tests a
call against a function signature to determine what arguments are
being passed.  The 'inspect' module also has the 'getargvalues'
function, which does something similar.
--
http://mail.python.org/mailman/listinfo/python-list


Re: reading file to list

2009-01-18 Thread André Thieme

William James schrieb:

André Thieme wrote:

You make a very strong case that Lisp is very feeble at
processing data.  I'm almost convinced.


I somehow don’t believe you :-)



Ruby isn't feeble, so data like this is fine:


shall we begin?
or lotus135? 1984 times!
The 3 stooges: COBOL,LISP,FORTRAN.
3.14, si11y L00KING


Extracting the unsigned integers:

IO.readlines('data').map{|s| s.scan(/\d+/).map{|s| s.to_i}}
==>[[], [135, 1984], [3], [3, 14, 11, 0]]


Just take the program I posted before and replace
(.split % "\\s")
with
(re-seq #"\d+" %)


Now that I was so kind to give you what you asked for, you will
probably do the same for me and show me your Ruby code, in which you
define a variable that contains the Fibonacci sequence.

(def fibs (lazy-cat [0 1] (map + fibs (drop 1 fibs


(nth fibs 70) ==> 190392490709135

One requirement is that when you look at the n'th fib the results up
to n must be memoized, so that nothing needs to be calculated again
when you ask for a fib between 0 and n. And when you ask for the n+1'th
it will start the calculation right from point n.

Example:
user> (time (nth fibs 900))
"Elapsed time: 16.898726 msecs"

user> (time (nth fibs 901))
"Elapsed time: 0.268603 msecs"


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


Re: what's the point of rpython?

2009-01-18 Thread Paul Rubin
s...@pobox.com writes:
> http://letmegooglethatforyou.com/?q=lock+free+reference+counting

I found a paper by Detlefs et al describing a method which is

a) patented
b) can potentially lock out some threads from ever running, and
c) relies on a hardware instruction (double compare and swap)
   that's not available on most processors.

There are well known concurrent and parallel GC techniques that
don't have that problem, see for example Cheng's dissertation:
  http://reports-archive.adm.cs.cmu.edu/anon/2001/CMU-CS-01-174.pdf

GHC 6.10 uses a parallel stop-the-world gc that is simpler, I think.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to use *.py modules instead of *.pyc?

2009-01-18 Thread Chris Rebert
On Sun, Jan 18, 2009 at 5:13 PM,   wrote:
> How to use *.py modules instead of *.pyc or automatically recompile
> all modules each time I change *.py files?
> Thank you in advance.

Also, just for the sake of completeness (since John and I have shown
that your real problem lies elsewhere), I happened to come across the
`compileall` module via a Planet Python blog, which mass-compiles
.py-s into .pyc-s:

http://blog.doughellmann.com/2009/01/pymotw-compileall.html

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to use *.py modules instead of *.pyc?

2009-01-18 Thread John Machin
On Jan 19, 12:13 pm, dsblizz...@gmail.com wrote:
> How to use *.py modules instead of *.pyc or automatically recompile
> all modules each time I change *.py files?

You don't need to do anything special. If, when you import foo,
foo.pyc is outdated by changes to the foo.py that is in the same
directory, then foo.py will be compiled to a new foo.pyc -- otherwise
compilation is unnecessary and the existing foo.pyc will be used.

If you think you have a problem, it could be caused by foo.pyc being
in a directory that is in sys.path but you are editing a foo.py that
is not in sys.path.

HTH,
John

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


Re: How to use *.py modules instead of *.pyc?

2009-01-18 Thread Chris Rebert
On Sun, Jan 18, 2009 at 5:13 PM,   wrote:
> How to use *.py modules instead of *.pyc or automatically recompile
> all modules each time I change *.py files?

IIRC, you shouldn't need to worry about this. Python checks the
modification times on the .py and .pyc files and if the .pyc is older,
it regenerates the .pyc from the .py; thus ensuring that the latest
version of the source file is always used.
What led you to conclude otherwise?

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


How to use *.py modules instead of *.pyc?

2009-01-18 Thread dsblizzard
How to use *.py modules instead of *.pyc or automatically recompile
all modules each time I change *.py files?
Thank you in advance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: tp_base, ob_type, and tp_bases

2009-01-18 Thread Jeff McNeil
On Jan 18, 5:40 am, Carl Banks  wrote:
> On Jan 17, 8:12 am, Jeff McNeil  wrote:
>
>
>
> > On Jan 17, 11:09 am, Jeff McNeil  wrote:
>
> > > On Jan 17, 10:50 am, "Martin v. Löwis"  wrote:
>
> > > > > So, the documentation states that ob_type is a pointer to the type's
> > > > > type, or metatype. Rather, this is a pointer to the new type's
> > > > > metaclass?
>
> > > > That's actually the same. *Every* ob_type field points to the object's
> > > > type, e.g. for strings, integers, tuples, etc. That includes type
> > > > objects, where ob_type points to the type's type, i.e. it's meta-type,
> > > > also called metaclass (as "class" and "type" are really synonyms).
>
> > > > > Next, we have tp_base.  That's defined as "an optional pointer to a
> > > > > base type from which type properties are inherited."  The value of
> > > > > tp_base is then added to the tp_bases tuple.  This is confusing me. On
> > > > > the surface, it sound as though they're one in the same?
>
> > > > (I don't understand the English "one in the same" - interpreting it
> > > > as "as though they should be the same")
>
> > > > No: tp_bases is a tuple of all base types (remember, there is multiple
> > > > inheritance); tp_base (if set) provides the first base type.
>
> > > > > I *think* (and dir() of a subclass of type seems to back it up), that
> > > > > tp_base is only at play when the object in question inherits from
> > > > > type?
>
> > > > No - it is the normal case for single inheritance. You can leave it
> > > > NULL, which means you inherit from object.
>
> > > > Regards,
> > > > Martin
>
> > > Thank you! It was tp_base that was confusing me.  The tp_bases member
> > > makes sense as Python supports multiple inheritance.  It wasn't
> > > immediately clear that tp_base is there for single inheritance
> > > reasons. It's all quite clear now.
>
> > > Is that an optimization of sorts?
>
> > Well, maybe not specifically for single inheritance reasons, I just
> > didn't see an immediate reason to keep a separate pointer to the first
> > base type.
>
> The reason you need a separate tp_base is because it doesn't
> necessarily point to the first base type; rather, it points to the
> first base type that has added any fields or slots to its internal
> layout (in other words, the first type with a tp_basicsize > 8, on 32-
> bit versions).  I believe this is mainly for the benefit of Python
> subclasses that define their own slots.  The type constructor will
> begin adding slots at an offset of tp_base->tp_basicsize.
>
> To see an example, int objects have a tp_basicsize of 12 (there are 4
> extra bytes for the interger).  So if you multiply-inherit from int
> and a Python class, int will always be tp_base.
>
> class A(object): pass
>
> class B(int,A): pass
> print B.__base__ # will print 
>
> class C(A,int): pass
> print C.__base__ # will print 
>
> A related issue is that you can't multiply inherit from two types that
> have tp_basicsize > 8 unless one of them inherits from the other.
> There can be only one tp_base.  For instance:
>
> class D(int,tuple): pass # will raise TypeError
>
> class E(object):
>     __slots__ = ['a','b']
>
> class F(object):
>     __slots__ = ['c','d']
>
> class G(E,G): pass # will raise TypeError
>
> class H(E,int): pass  # will raise TypeError
>
> 
>
> Here's a bit more background (and by "a bit" I mean "a lot"):
>
> In 32-bit Python, objects of types defined in Python are usually only
> 16 bytes long.  The layout looks like this.
>
> instance dict
> weak reference list
> reference count
> ob_type
>
> The reference count, which is always the thing that the actual
> PyObject* points at, isn't actually the first item in the object's
> layout.  The dict and weakref list are stored at a lower address.
> (There's a reason for it.)
>
> If a Python class defines any __slots__, the type constructor will add
> the slots to the object's layout.
>
> instance dict (if there is one)
> weak reference list (if there is one)
> reference count
> ob_type
> slot
> slot
> slot
>
> Note that, because you defined __slots__, the object might not have an
> instance dict or weak reference list associated with it.  It might,
> though, if one of the base classes had defined an instance dict or
> weakref list.  Or, it might not, but then a subclass might have its
> onw instance dict or weakref list.  That's why these guys are placed
> at a lower address: so that they don't interfere with the layout of
> subclasses.  A subclass can add either more slots or a dict.
>
> Object of types defined in C can have arbitrary layout.  For instance,
> it could have a layout that looks like this:
>
> reference count
> ob_type
> PyObject* a
> PyObject* b
> long c
> float d
> instance dict
>
> A problem with Python slots and C fields is that, if you want to
> inherit from a type with a non-trivial object layout, aside from the
> dict and weakrefs, all of the subtypes have to maintain the same
> layout (see Liskov substitutability for rationale).
>
> 

Re: what's the point of rpython?

2009-01-18 Thread skip
Paul> That's interesting, got a reference?  (no pun intended)

http://letmegooglethatforyou.com/?q=lock+free+reference+counting



-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: changing URLs in webpages, python solutions?

2009-01-18 Thread Simon Forman
On Jan 18, 8:40 am, Stefan Behnel  wrote:
> Simon Forman wrote:
> > I want to take a webpage, find all URLs (links, img src, etc.) and
> > rewrite them in-place, and I'd like to do it in python (pure python
> > preferred.)
>
> lxml.html has functions specifically for this problem.
>
> http://codespeak.net/lxml/lxmlhtml.html#working-with-links
>
> Code would be something like
>
>         html_doc = lxml.html.parse(b"http://.../xyz.html";)
>         html_doc.rewrite_links( ... )
>         print( lxml.html.tostring(html_doc) )
>
> It also handles links in CSS or JavaScript, as well as broken HTML documents.
>
> Stefan

Thank you so much! This is exactly what I needed.

(for what it's worth, parse() seems to return a "plain" ElementTree
object but document_fromstring() gave me a special html object with
the rewrite_links() method.  I think, but I didn't try it, that

html_doc = lxml.html.parse(b"http://.../xyz.html";)
lxml.html.rewrite_links(html_doc, ... )

would work too.)

Thanks again!
Regards,
~Simon
--
http://mail.python.org/mailman/listinfo/python-list


Re: uninstall before upgrade?

2009-01-18 Thread The Music Guy
On Sun, 2009-01-18 at 10:06 -0800, waltbrad wrote:
> I want to upgrade from 2.5 to 2.6.  Do I need to uninstall 2.5 before
> I do that? If so, what's the best way to uninstall it?  Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list

I've heard of people having problems trying to replace 2.5 with 2.6, so
you probably don't want to uninstall 2.5. However, 2.5 and 2.6 can both
be installed simultaneously.

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


uninstall before upgrade?

2009-01-18 Thread waltbrad
I want to upgrade from 2.5 to 2.6.  Do I need to uninstall 2.5 before
I do that? If so, what's the best way to uninstall it?  Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: dynamic module import?

2009-01-18 Thread alex23
On Jan 19, 2:24 am, Duncan Booth  wrote:
[a nice concise explanation on __import__ & fromlist]

Cheers, Duncan, that explained it perfectly.

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


Overriding base class methods in the C API

2009-01-18 Thread Floris Bruynooghe
Hello

I've been trying to figure out how to override methods of a class in
the C API.  For Python code you can just redefine the method in your
subclass, but setting tp_methods on the type object does not seem to
have any influcence.  Anyone know of a trick I am missing?

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


Re: braces fixed '#{' and '#}'

2009-01-18 Thread Brendan Miller
Yes, I also recently noticed the bug in python's parser that doesn't
let it handle squigly braces and the bug in the lexer that makes white
space significant. I'm surprised the dev's haven't noticed this yet.

On Sat, Jan 17, 2009 at 2:09 AM, v4vijayakumar
 wrote:
> I saw some code where someone is really managed to import braces from
> __future__. ;)
>
> def test():
> #{
>print "hello"
> #}

This seems like the best workaround.  Hopefully python curly brace
support will be fixed soon. I think technically a language can't be
turing complete without curly braces right? That's definitely how I
read this:

http://www.thocp.net/biographies/papers/turing_oncomputablenumbers_1936.pdf

"If the negation of what Gödel has shown had been proved, i.e. if, for each U,
either U or –U is provable, then we should have an immediate solution of the
Entscheidungsproblem. As a corollary we also have that real
programmers use squigly braces and everyone else is nubs"
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling an external program and capturing the output

2009-01-18 Thread Marek Kubica
On Sun, 18 Jan 2009 09:36:59 -0800 (PST)
Xah Lee  wrote:

> See:
> 
> • Making System Calls in Perl and Python
>   http://xahlee.org/perl-python/system_calls.html

You can safely drop the Raw-Strings as they are only needed on Windows
when constuction paths and programs with hardcoded paths are rare.

regards,
Marek

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


Re: what's the point of rpython?

2009-01-18 Thread Paul Rubin
"Brendan Miller"  writes:
> That's interesting, I hadn't heard the reference counting mechanism
> was related to the GIL. Is it just that you need to lock the reference
> count before mutating it if there's no GIL? 

Yes.  Someone tried inserting such a lock but it slowed down the
single-cpu case unacceptably.

> Really, that shouldn't be
> the case. Reference counting can be done with a lock free algorithm.

That's interesting, got a reference?  (no pun intended)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Can Python manipulate PE structure or bytes?

2009-01-18 Thread Gabriel Genellina

En Sun, 18 Jan 2009 20:18:13 -0200,  escribió:


I'm interested in Python and wanted to know if Python can manipulate
PE structure and bytes. Also what are its limits?


If you're talking about the "Portable Executable" file format, yes. Take a  
look at the struct module, or ctypes.Struct, but try Google first...



--
Gabriel Genellina

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


Re: Can Python manipulate PE structure or bytes?

2009-01-18 Thread Gabriel Genellina

En Sun, 18 Jan 2009 20:18:13 -0200,  escribió:


I'm interested in Python and wanted to know if Python can manipulate
PE structure and bytes. Also what are its limits?


If you're talking about the "Portable Executable" file format, yes. Take a  
look at the struct module, or ctypes.Struct, but try Google first...



--
Gabriel Genellina

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


Re: *Advanced* Python book?

2009-01-18 Thread Simon Brunning
2009/1/17 Michele Simionato :
> "Expert Python Programming" by Tarek Ziadé is quite good and I wrote
> a review for it:
>
> http://www.artima.com/weblogs/viewpost.jsp?thread=240415

+1 for this. I'm 3/4 of the way through it, it's pretty good. Covers
many on the important areas that the more introductory books rightly
leave out,

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


Re: reading file to list

2009-01-18 Thread William James
André Thieme wrote:

> Xah Lee schrieb:
> > comp.lang.lisp,comp.lang.scheme,comp.lang.functional,comp.lang.pytho
> > n,comp.lang.ruby
> > 
> > Here's a interesting toy problem posted by Drew Krause to
> > comp.lang.lisp:
> > 
> > 
> > On Jan 16, 2:29 pm, Drew Krause wrote [paraphrased a bit]:
> > 
> > OK, I want to create a nested list in Lisp (always of only integers)
> > from a text file, such that each line in the text file would be
> > represented as a sublist in the 'imported' list.
> > 
> > example of a file's content
> > 
> > 3 10 2
> > 4 1
> > 11 18
> > 
> > example of programing behavior
> >(make-list-from-text "blob.txt") => ((3 10 2) (4 1) (11 18))
> > 
> > w_a_x_...@yahoo.com gave a ruby solution:
> > 
> >  IO.readlines("blob.txt").map{|line| line.split }
> > 
> > augumented by Jilliam James for result to be numbers:
> > 
> >  IO.readlines("blob.txt").map{|line| line.split.map{|s| s.to_i }}
> > 
> > Very beautiful.
> > 
> > ---
> > 
> > That's really the beauty of Ruby.
> > 
> > This problem and ruby code illustrates 2 fundamental problems of
> > lisp, namely, the cons problem, and the nested syntax pain. Both of
> > which are practically unfixable.
> 
> *Of course* Xah is wrong, as always.
> Who would expect anything else?
> In the Lisp style Clojure for example one does exactly the same as
> Jillian James (JJ) did in Ruby:
> (map #(map (fn [s] (Integer/parseInt s)) (.split % "\\s")) (line-seq
> (reader "blob.txt")))

That fails when numbers are separated by more than one space.
And what about leading or trailing space?

> 
> This is slightly longer, simply because the functions have longer
> names.  Integer/parseInt  vs  to_i
> 
> Also the function split takes a regexp, so I have to add the "\\s"
> here.  I don’t know though if Jillians version also handles any
> kind of whitespac per line.


irb(main):003:0> puts "  foo   \t   bar  "
  foo  bar
=> nil
irb(main):004:0> "  foo   \t   bar  ".split
=> ["foo", "bar"]
irb(main):005:0> "foo...bar?-!hoo".split /\W+/
=> ["foo", "bar", "hoo"]


> And the last thing is, that the function reader returns a
> BufferedReader.  So in general this is more valuable in real programs
> as opposed to one- line scripts. If we combine line-seq and reader
> into readline and apply the two other changes we would say:
> (map #(map (fn [s] (to_i s)) (.split %)) (readlines "blob.txt"))

That is not a complete program.

> vs
> IO.readlines("blob.txt").map{|line| line.split.map{|s| s.to_i }}

That is the complete program.

> 
> So, obviously Xah is far away from any reality.
> It took him hours of his life to write up all his ideas about Lisp.
> Sad to see that all that time was wasted. All of it was wrong.
> Poor Xah :(
> 
> 
> And btw, Drew Krause is just a Lisp newbie. No Lisper would ever store
> data for his programs in the format
> 3 10 2
> 4 1
> 11 18
> 
> but instead as
> (3 10 2)
> (4 1)
> (11 18)

Perhaps the data was stored by someone else.  Understand?

> 
> And then read it back into the program with:
> (map read-string (line-seq (reader "blob.txt")))
>

You make a very strong case that Lisp is very feeble at
processing data.  I'm almost convinced.

Ruby isn't feeble, so data like this is fine:


shall we begin?
or lotus135? 1984 times!
The 3 stooges: COBOL,LISP,FORTRAN.
3.14, si11y L00KING


Extracting the unsigned integers:

IO.readlines('data').map{|s| s.scan(/\d+/).map{|s| s.to_i}}
==>[[], [135, 1984], [3], [3, 14, 11, 0]]
--
http://mail.python.org/mailman/listinfo/python-list


Re: what's the point of rpython?

2009-01-18 Thread Brendan Miller
On Sat, Jan 17, 2009 at 7:57 PM, Paul Rubin
<"http://phr.cx"@nospam.invalid> wrote:
> alex23  writes:
>> Here's an article by Guido talking about the last attempt to remove
>> the GIL and the performance issues that arose:
>>
>> "I'd welcome a set of patches into Py3k *only if* the performance for
>> a single-threaded program (and for a multi-threaded but I/O-bound
>> program) *does not decrease*."
>
> The performance decrease is an artifact of CPython's rather primitive
> storage management (reference counts in every object).  This is
> pervasive and can't really be removed.  But a new implementation
> (e.g. PyPy) can and should have a real garbage collector that doesn't
> suffer from such effects.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

That's interesting, I hadn't heard the reference counting mechanism
was related to the GIL. Is it just that you need to lock the reference
count before mutating it if there's no GIL? Really, that shouldn't be
the case. Reference counting can be done with a lock free algorithm.

Garbage collection is definitely in vogue right now. However, people
tend to treat it more like a religion than an algorithm. Garbage
collection vs reference counting  actually has some trade offs both
ways. GC gets you some amortized performance gains, and some space
gains because you don't need to hang on to a bunch of counts. However,
GC also has the problem of having a very loose memory profile and poor
interactive performance during compaction if the heap is large. In
some cases this discussion becomes complicated with python because
python has both reference counting and GC.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
Michele Simionato:

> I don't like that. Scala was designed with the idea of putting
> together the two worlds, by I think the result was to get the
> complications of both worlds.

But some other people may like it, and it's a design experiment worth
doing. Every programming paradigm has advantages, so it's normal for
people to try to design language that have the best of different
worlds. Even if Scala is a failure (and I don't think it is), it's
good to keep trying to design a mixed language (Like Python, that
mixes procedural, some OOP and a bit of functional stiles. It has not
pattern matching stile (Mathematica, OcaML), logic-inferential style
(Prolog), constraint style (Oz, Mozart), data flow style, generic
programming (C++, D), etc).

Today lot of people understand that functional languages have
advantages, so they are trying to created hybrids (Scala, F#, D V.2,
etc), and they may succeed only trying. The purpose is of course to
create a language that isn't too much complex, but has those
advantages anyway. I presume Scala is quite less complex than C++
anyway.


>Programming languages should be designed not by piling feature on top of 
>feature, but by removing the weaknesses and restrictions that make additional 
>features appear necessary. -- William Clinger<

I think it's false, but it requires me lot of space to explain why. In
few words: Perl is more useful than Scheme if you have to solve a lot
of practical computational problems, despite Scheme looks much more
nicer.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Diez B. Roggisch

Ron Garret schrieb:

On Jan 18, 12:40 pm, "Diez B. Roggisch"  wrote:

Ron Garret schrieb:




On Jan 18, 11:29 am, "Diez B. Roggisch"  wrote:

Ron Garret schrieb:

I'm writing a WSGI application and I would like to check the content-
length header before reading the content to make sure that the content
is not too big in order to prevent denial-of-service attacks.  So I do
something like this:
def application(environ, start_response):
status = "200 OK"
headers = [('Content-Type', 'text/html'), ]
start_response(status, headers)
if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
But this doesn't seem to work.  If I upload a huge file it still waits
until the entire file has been uploaded before complaining that it's
too big.
Is it possible to read the HTTP headers in WSGI before the request
body has been read?

AFAIK that is nothing that WSGI defines - it's an implementation-detail
of your server. Which one do you use?

Apache at the moment, with lighttpd as a contender to replace it.

Together with mod_wsgi?

Diez


Yes.  (Is there any other way to run WSGI apps under Apache?)


Well, not so easy, but of course you can work with mod_python or even 
CGI/fastcgi to eventually invoke a WSGI-application.


However, the original question - that's a tough one.

According to this, it seems one can use an apache-directive to prevent 
mod_wsgi to even pass a request to the application if it exceeds a 
certain size.


http://code.google.com/p/modwsgi/wiki/ConfigurationGuidelines

Search for "Limiting Request Content"

However, I'm not sure how early that happens. I can only suggest you try 
& contact Graham Dumpleton directly, he is very responsive.



Diez

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


Re: function argument dependent on another function argument?

2009-01-18 Thread Paul Rubin
Steven D'Aprano  writes:
> Having said that, there are times where you need to pass None as a 
> legitimate argument and not as a sentinel. 

I don't think it's worth trying to figure out which those times are.
The conclusion can be wrong, or can become wrong later because of
some faraway change in the code.  I prefer using the bulletproof
method from the beginning, whether it is needed or not.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
alex23:
> Paul, have you looked into Cython at all?

I can also suggest ShedSkin and the D language.

I have often used D for data munging, producing quick & short
programs, when Python isn't fast enough for a certain purpose.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: function argument dependent on another function argument?

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 07:36:53 -0800, Paul Rubin wrote:

> Steven D'Aprano  writes:
>> def foo(self, x, y=None):
>> if y is None:
>> y = self.a
>> 
>> I don't find that clumsy in the least. I find it perfectly readable and
>> a standard idiom.
> 
> That has the same problem as the earlier version.

No it doesn't. The earlier version had the problem that *any* false 
object is replaced by self.a. I'm just following the standard Python 
idiom of using None as a sentinel. Have a look through the standard 
library and see how many times it is used.

Built-ins rarely accept None as a sentinel, slice() being a conspicuous 
exception. This is sometimes a nuisance when writing wrappers:

def my_find(S, sub, start=None, end=None):
"""Like string.find() only with pre-processing."""
pre_process()  # stub for something complicated
if end is None and start is None:
return S.find(sub)
elif end if None:
return S.find(sub, start)
else:
return S.find(sub, start, end)

or if you prefer:

def my_find(S, sub, start=None, end=None):
"""Like string.find()"""
pre_process()
args = [sub]
if start is not None:
args.append(start)
if end is not None:
args.append(end)
return S.find(*args)


Having said that, there are times where you need to pass None as a 
legitimate argument and not as a sentinel. In that case, your solution:

> If the person passes
> None, they get self.a.  I prefer:
> 
> sentinel = object()
> ...
> 
> def foo(x, y=sentinel):
>   if y is sentinel:
>   y = self.a

is an excellent one.


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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread bearophileHUGS
r:
>Of course it would not run in C or Python but the point here is readability.<

With languages like Genie you can go close:
http://live.gnome.org/Genie

Or better Delight (based on the D language), plus some my "Python
compatibility" libs (plus Pyd, if you want) you can run that code:
http://delight.sourceforge.net/

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Can Python manipulate PE structure or bytes?

2009-01-18 Thread seaworthyjeremy
I'm interested in Python and wanted to know if Python can manipulate
PE structure and bytes. Also what are its limits?
--
http://mail.python.org/mailman/listinfo/python-list


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Ron Garret
On Jan 18, 1:21 pm, Graham Dumpleton 
wrote:
> On Jan 19, 6:01 am, Ron Garret  wrote:
>
> > I'm writing a WSGI application and I would like to check the content-
> > length header before reading the content to make sure that the content
> > is not too big in order to prevent denial-of-service attacks.  So I do
> > something like this:
>
> > def application(environ, start_response):
> >     status = "200 OK"
> >     headers = [('Content-Type', 'text/html'), ]
> >     start_response(status, headers)
> >     if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
>
> You should be returning 413 (Request Entity Too Large) error status
> for that specific case, not a 200 response.
>
> You should not be returning a string as response content as it is very
> inefficient, wrap it in an array.
>
> > But this doesn't seem to work.  If I upload a huge file it still waits
> > until the entire file has been uploaded before complaining that it's
> > too big.
>
> > Is it possible to read the HTTP headers in WSGI before the request
> > body has been read?
>
> Yes.
>
> The issue is that in order to avoid the client sending the data the
> client needs to actually make use of HTTP/1.1 headers to indicate it
> is expecting a 100-continue response before sending data. You don't
> need to handle that as Apache/mod_wsgi does it for you, but the only
> web browser I know of that supports 100-continue is Opera browser.
> Clients like curl do also support it as well though. In other words,
> if people use IE, Firefox or Safari, the request content will be sent
> regardless anyway.
>
> There is though still more to this though. First off is that if you
> are going to handle 413 errors in your own WSGI application and you
> are using mod_wsgi daemon mode, then request content is still sent by
> browser regardless, even if using Opera. This is because the act of
> transferring content across to mod_wsgi daemon process triggers return
> of 100-continue to client and so it sends data. There is a ticket for
> mod_wsgi to implement proper 100-continue support for daemon mode, but
> will be a while before that happens.
>
> Rather than have WSGI application handle 413 error cases, you are
> better off letting Apache/mod_wsgi handle it for you. To do that all
> you need to do is use the Apache 'LimitRequestBody' directive. This
> will check the content length for you and send 413 response without
> the WSGI application even being called. When using daemon mode, this
> is done in Apache child worker processes and for 100-continue case
> data will not be read at all and can avoid client sending it if using
> Opera.
>
> Only caveat on that is the currently available mod_wsgi has a bug in
> it such that 100-continue requests not always working for daemon mode.
> You need to apply fix in:
>
>  http://code.google.com/p/modwsgi/issues/detail?id=121
>
> For details on LimitRequestBody directive see:
>
>  http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody
>
> Graham

Thanks for the detailed response!

rg

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Graham Dumpleton
On Jan 19, 6:43 am, Petite Abeille  wrote:
> On Jan 18, 2009, at 8:01 PM, Ron Garret wrote:
>
> > def application(environ, start_response):
> >    status = "200 OK"
> >    headers = [('Content-Type', 'text/html'), ]
> >    start_response(status, headers)
> >    if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
>
> How would that work for chunked transfer-encoding?

Chunked transfer encoding on request content is not supported by WSGI
specification as WSGI requires CONTENT_LENGTH be set and disallows
reading more than defined content length, where CONTENT_LENGTH is
supposed to be taken as 0 if not provided.

If using Apache/mod_wsgi 3.0 (currently in development, so need to use
subversion copy), you can step outside what WSGI strictly allows and
still handle chunked transfer encoding on request content, but you
still don't have a CONTENT_LENGTH so as to check in advance if more
data than expected is going to be sent.

If wanting to know how to handle chunked transfer encoding in
mod_wsgi, better off asking on mod_wsgi list.

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Tim Rowe
2009/1/18 Paul Rubin <"http://phr.cx"@nospam.invalid>:

> I.e. the cast was wrong because of the failure of an unstated
> assumption that a certain sensor reading was in a certain range.
> Spark may still have allowed the cast only if the assumption was
> stated explicitly in the specification.

Unless it's changed since I used it, technically, SPADE doesn't allow
or disallow anything. It produces a predicate (a proof obligation)
that you have to prove is always true (or is it always false? It's
been 18 years since I worked on that stuff, and SPADE and MALPAS
produced their proof obligations with opposite values). So it's still
up to you to show that it won't overflow, it just gives you the
predicate calculus expression that you need to do that.

Since the value appears to come from a sensor, the only way one could
prove that there would be no overflow would be to state it as a part
of the specification of what is read in. If that specification doesn't
match the specification of the actual sensor, that's nothing to do
with the programming language or, for that matter, the program itself.
It's a specification mismatch.

I was actually at the European Space Agency's Toulouse site the week
after the Ariane 5 incident. I've been at jollier funerals. I can't
help thinking that thinking that the team would have benefited from
reading David Parnas's work on the specification of the A-7E avionics.

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Graham Dumpleton
On Jan 19, 6:01 am, Ron Garret  wrote:
> I'm writing a WSGI application and I would like to check the content-
> length header before reading the content to make sure that the content
> is not too big in order to prevent denial-of-service attacks.  So I do
> something like this:
>
> def application(environ, start_response):
>     status = "200 OK"
>     headers = [('Content-Type', 'text/html'), ]
>     start_response(status, headers)
>     if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'

You should be returning 413 (Request Entity Too Large) error status
for that specific case, not a 200 response.

You should not be returning a string as response content as it is very
inefficient, wrap it in an array.

> But this doesn't seem to work.  If I upload a huge file it still waits
> until the entire file has been uploaded before complaining that it's
> too big.
>
> Is it possible to read the HTTP headers in WSGI before the request
> body has been read?

Yes.

The issue is that in order to avoid the client sending the data the
client needs to actually make use of HTTP/1.1 headers to indicate it
is expecting a 100-continue response before sending data. You don't
need to handle that as Apache/mod_wsgi does it for you, but the only
web browser I know of that supports 100-continue is Opera browser.
Clients like curl do also support it as well though. In other words,
if people use IE, Firefox or Safari, the request content will be sent
regardless anyway.

There is though still more to this though. First off is that if you
are going to handle 413 errors in your own WSGI application and you
are using mod_wsgi daemon mode, then request content is still sent by
browser regardless, even if using Opera. This is because the act of
transferring content across to mod_wsgi daemon process triggers return
of 100-continue to client and so it sends data. There is a ticket for
mod_wsgi to implement proper 100-continue support for daemon mode, but
will be a while before that happens.

Rather than have WSGI application handle 413 error cases, you are
better off letting Apache/mod_wsgi handle it for you. To do that all
you need to do is use the Apache 'LimitRequestBody' directive. This
will check the content length for you and send 413 response without
the WSGI application even being called. When using daemon mode, this
is done in Apache child worker processes and for 100-continue case
data will not be read at all and can avoid client sending it if using
Opera.

Only caveat on that is the currently available mod_wsgi has a bug in
it such that 100-continue requests not always working for daemon mode.
You need to apply fix in:

  http://code.google.com/p/modwsgi/issues/detail?id=121

For details on LimitRequestBody directive see:

  http://httpd.apache.org/docs/2.2/mod/core.html#limitrequestbody

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Ron Garret
On Jan 18, 12:40 pm, "Diez B. Roggisch"  wrote:
> Ron Garret schrieb:
>
>
>
> > On Jan 18, 11:29 am, "Diez B. Roggisch"  wrote:
> >> Ron Garret schrieb:
>
> >>> I'm writing a WSGI application and I would like to check the content-
> >>> length header before reading the content to make sure that the content
> >>> is not too big in order to prevent denial-of-service attacks.  So I do
> >>> something like this:
> >>> def application(environ, start_response):
> >>>     status = "200 OK"
> >>>     headers = [('Content-Type', 'text/html'), ]
> >>>     start_response(status, headers)
> >>>     if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
> >>> But this doesn't seem to work.  If I upload a huge file it still waits
> >>> until the entire file has been uploaded before complaining that it's
> >>> too big.
> >>> Is it possible to read the HTTP headers in WSGI before the request
> >>> body has been read?
> >> AFAIK that is nothing that WSGI defines - it's an implementation-detail
> >> of your server. Which one do you use?
>
> > Apache at the moment, with lighttpd as a contender to replace it.
>
> Together with mod_wsgi?
>
> Diez

Yes.  (Is there any other way to run WSGI apps under Apache?)

rg

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


libmsi.a import library from wine, and header files available (entirely free software), available for python-win32 builds under msys+wine

2009-01-18 Thread Luke Kenneth Casson Leighton
as part of building python2.5.2 under msys under wine on linux using
mingw, i thought i'd try building _msi.pyd just for kicks.  of course,
that required having an msi.lib import library, and associated header
files.  so, purely as an experiment, i've documented the process by
which it is possible to take wine 1.1.13 (current development release)
source code, modify the header files for use with mingw, create a
typelibrary (using dlltool) and then, surprise-surprise, _msi.pyd
successfully builds.

i say successfully builds: but then, running from _outside_ of a
wineconsole cmd, doing this:

/usr/local/bin/wine ./python.exe -c 'import _msi'

succeeds.

but if you do this:

  /usr/local/bin/wineconsole cmd
  c:/python2.5/bin/python.exde -c 'import _msi'

you get an "access violation - no access to memory" blah blah

to be honest, i don't care about that: this message is to let people
know that it _is_ possible, and, if anyone is interested in e.g.
adding -lmsi -lcabinet support to MinGW, here's where you can get the
necessary crud:

http://lkcl.net/msi.tgz

if the wine team have any objections, if they believe this is a bad
idea, please do say so :)

l.

p.s. if anyone would like to add a regression test to python called
"test_msi.py" - or if they know of one that exists, i'd love to hear
from you and try it out.  outside of a wine cmd.exe of course :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Diez B. Roggisch

Ron Garret schrieb:

On Jan 18, 11:29 am, "Diez B. Roggisch"  wrote:

Ron Garret schrieb:




I'm writing a WSGI application and I would like to check the content-
length header before reading the content to make sure that the content
is not too big in order to prevent denial-of-service attacks.  So I do
something like this:
def application(environ, start_response):
status = "200 OK"
headers = [('Content-Type', 'text/html'), ]
start_response(status, headers)
if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
But this doesn't seem to work.  If I upload a huge file it still waits
until the entire file has been uploaded before complaining that it's
too big.
Is it possible to read the HTTP headers in WSGI before the request
body has been read?

AFAIK that is nothing that WSGI defines - it's an implementation-detail
of your server. Which one do you use?


Apache at the moment, with lighttpd as a contender to replace it.



Together with mod_wsgi?

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Ron Garret
On Jan 18, 11:43 am, Petite Abeille  wrote:
> On Jan 18, 2009, at 8:01 PM, Ron Garret wrote:
>
> > def application(environ, start_response):
> >    status = "200 OK"
> >    headers = [('Content-Type', 'text/html'), ]
> >    start_response(status, headers)
> >    if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
>
> How would that work for chunked transfer-encoding?

It wouldn't.  But many clients don't use chunked-transfer-encoding
when uploading files whose size is known.  In that case it would be
nice to let users know that their upload is going to fail BEFORE they
waste hours waiting for 10GB of data to go down the wire.

rg

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


Re: Python and threads

2009-01-18 Thread Stefan Behnel
vedrandeko...@yahoo.com wrote:
> and thanks for all previous help.I want to measure memory usage of
> executed python script.I'am working on windows XP.

Could you qualify "measure"? Do you mean:

a) "debug" (permanently high accuracy, potentially high runtime overhead)
b) "monitor" (high accuracy, low/medium-resolution surveillance, no runtime
  overhead)
c) "find out" (low accuracy or just max usage, no permanent observation)

?

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Ron Garret
On Jan 18, 11:29 am, "Diez B. Roggisch"  wrote:
> Ron Garret schrieb:
>
>
>
> > I'm writing a WSGI application and I would like to check the content-
> > length header before reading the content to make sure that the content
> > is not too big in order to prevent denial-of-service attacks.  So I do
> > something like this:
>
> > def application(environ, start_response):
> >     status = "200 OK"
> >     headers = [('Content-Type', 'text/html'), ]
> >     start_response(status, headers)
> >     if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'
>
> > But this doesn't seem to work.  If I upload a huge file it still waits
> > until the entire file has been uploaded before complaining that it's
> > too big.
>
> > Is it possible to read the HTTP headers in WSGI before the request
> > body has been read?
>
> AFAIK that is nothing that WSGI defines - it's an implementation-detail
> of your server. Which one do you use?

Apache at the moment, with lighttpd as a contender to replace it.

rg

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


Re: Relax Syntax for Augmented Arithmetic?

2009-01-18 Thread Terry Reedy

andrew cooke wrote:

Context - http://docs.python.org/3.0/reference/datamodel.html?highlight=data
model#object.__iadd__

Just a suggestion I thought I'd throw out...  There's a restriction in
the language implementation on exactly what can go the left of an
augmented arithmetic expression.

For example:

a = 3
a **= 2


is ok, but:

class Foo():

...   def __init__():
... self.a = 3
...   def __ipow__(self, x):
... self.a **= x
...

Foo() **= 2


Calls return objects and therefore cannot be the target of an 
assignment, augmented or otherwise.  The target of an assignment is a 
name or collection slot, both of which are grammatical constructs, not 
objects.


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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Russ P.
On Jan 18, 9:22 am, Bruno Desthuilliers
 wrote:

> Properties by themselves are not the problem, quite on the contrary - as
> you say, they actually help wrt/ encapsulation. What breaks
> encapsulation is *automatic generation* for properties for *each and
> any* implementation attribute. Might as well just makes them all public
> attribute then.

Let me correct my statement about the automatic generation of
properties in Scala: it is only for public attributes, not all
attributes.

Getting back to the bigger point, I will gladly agree with you that
data hiding is not a magic bullet that will eliminate all bugs. The
idea that I or anyone else said that, however, is a red herring. Data
hiding is just one safeguard in a portfolio of safeguards that can
*help* to prevent certain kinds of bugs as well as deliberate acts of
sabotage or fraud. When you have a tough problem to solve, you need
all the help you can get.

You keep saying that if you hire competent, trustworthy developers,
you don't need data hiding. Well, maybe, but when you have a team of
dozens or hundreds of developers, your chances of avoiding any bad
ones is zero for all practical purposes.

And even if all your developers were excellent, data hiding would
still be a convenient mechanism to simplify their jobs so they can
focus on higher level problems -- and not have to rely on an ugly
naming convention.

Now, if developers become careless because they think data hiding will
save them, then that would be a problem. That much I will concede. But
I doubt that happens much.
--
http://mail.python.org/mailman/listinfo/python-list


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Petite Abeille


On Jan 18, 2009, at 8:01 PM, Ron Garret wrote:


def application(environ, start_response):
   status = "200 OK"
   headers = [('Content-Type', 'text/html'), ]
   start_response(status, headers)
   if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'


How would that work for chunked transfer-encoding?

Cheers,

--
PA.
http://alt.textdrive.com/nanoki/

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


Re: WSGI question: reading headers before message body has been read

2009-01-18 Thread Diez B. Roggisch

Ron Garret schrieb:

I'm writing a WSGI application and I would like to check the content-
length header before reading the content to make sure that the content
is not too big in order to prevent denial-of-service attacks.  So I do
something like this:

def application(environ, start_response):
status = "200 OK"
headers = [('Content-Type', 'text/html'), ]
start_response(status, headers)
if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'

But this doesn't seem to work.  If I upload a huge file it still waits
until the entire file has been uploaded before complaining that it's
too big.

Is it possible to read the HTTP headers in WSGI before the request
body has been read?


AFAIK that is nothing that WSGI defines - it's an implementation-detail 
of your server. Which one do you use?


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


Re: Python and threads

2009-01-18 Thread vedrandekovic
On 18 sij, 17:48, "Diez B. Roggisch"  wrote:
> vedrandeko...@yahoo.com schrieb:
>
>
>
> > Hello again,
>
> > Thanks for previous help on "Start two threads in same time" it was
> > useful,but when I run this
> > two threads, I think they don't start at the same time, here is my
> > code snippet:
>
> > import threading
>
> > class ThreadedClass1(threading.Thread):
> >     def __init__(self):
> >         threading.Thread.__init__(self)
>
> >     def run(self):
> >         a=True
> >         while a==True:
> >             bm=my_module.MyClass()
> >             a=bm.get_Python_Process_Usage_Function()   #Returns True
> > or False
>
> > class ThreadedClass2(threading.Thread):
> >     def __init__(self):
> >         threading.Thread.__init__(self)
>
> >     def run(self):
> >         os.popen("my_python_script.py")
>
> > threaded_obj = ThreadedClass1()
> > threaded_obj.start()
> > threaded_obj2 = ThreadedClass2()
> > threaded_obj2.start()
>
> If you want to synchronize two or more threads, you need to do so
> manually using objects like Locks, Events, Conditions and Semaphores.
> See the threading module.
>
> Even if you managed to get two threads started simultaneously (which the
>   OS doesn't even offer IINM), the would soon run out of sync.
>
> How about you tell us what you *want*, and we tell you if and how it's
> possible to do that.
>
> Diez

Hello,

and thanks for all previous help.I want to measure memory usage of
executed python script.I'am working on windows XP.

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


Re: pep 8 constants

2009-01-18 Thread MRAB

Francesco Bochicchio wrote:

On Wed, 14 Jan 2009 08:13:30 +, Steven D'Aprano wrote:



Absolutely. It's rather sad that I can do this:

import math
math.pi = 3.0

I like the ability to shoot myself in the foot, thank you very much, but 
I should at least get a warning when I'm about to do so:


math.PI = 3.0  # use God-like powers to change a constant




Constants would be a nice addition in python, sure enough.
But I'm not sure that this can be done without a run-time check every time
the constant is used, and python is already slow enough. Maybe a check
that is disabled when running with optimizing flags ?

But I'm sure this discussion has been already made and the FINAL WORD has
been already spoken.


>>> class Constants(object):
def __setattr__(self, key, value):
if key in self.__dict__:
raise ValueError("Can't change constant")
self.__dict__[key] = value


>>> c = Constants()
>>> c.PI = 3.0
>>> c.PI
3.0
>>> c.PI = 4.0

Traceback (most recent call last):
  File "", line 1, in 
c.PI = 4.0
  File "", line 4, in __setattr__
raise ValueError("Can't change constant")
ValueError: Can't change constant
--
http://mail.python.org/mailman/listinfo/python-list


WSGI question: reading headers before message body has been read

2009-01-18 Thread Ron Garret
I'm writing a WSGI application and I would like to check the content-
length header before reading the content to make sure that the content
is not too big in order to prevent denial-of-service attacks.  So I do
something like this:

def application(environ, start_response):
status = "200 OK"
headers = [('Content-Type', 'text/html'), ]
start_response(status, headers)
if int(environ['CONTENT_LENGTH'])>1000: return 'File too big'

But this doesn't seem to work.  If I upload a huge file it still waits
until the entire file has been uploaded before complaining that it's
too big.

Is it possible to read the HTTP headers in WSGI before the request
body has been read?

Thanks,
rg

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


Re: function argument dependent on another function argument?

2009-01-18 Thread andrew cooke
>     sentinel = object()
>     ...
>
>     def foo(x, y=sentinel):
>       if y is sentinel:
>           y = self.a

it just struck me you could also do:

 def foo(self, x, *y_args)
   y = y_args[0] if y_args self.a

which more directly checks whether an argument was passed, but has the
downside of making the method signature less clear in the declaration.

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


Re: calling an external program and capturing the output

2009-01-18 Thread Eric

Thanks guys. That helped point me int he right direction.

with your advice on the subprocess module I stumbled upon this
posting:
http://www.velocityreviews.com/forums/t359866-subprocess-module.html

for anyone else that might be interested here is the solution. It
simply calls a perl script called add.pl that reads 2 numbers from
stdin and adds them together.

Thanks again for the help.

-Eric


#!/usr/bin/env python

import subprocess

prog = "./add.pl"
args = "3 4"

app = subprocess.Popen
(prog ,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)

print "opened " + prog
#print app.stdout.read()

print "writing \'" + args + "\' to " + prog + " subprocess"
app.stdin.write(args)
app.stdin.write("\n")

print "wrote \'" + args + "\' to " + prog + " subprocess"
result = app.stdout.read()
result = result.rstrip('\n')

print "received: " + result



and here is the output:

./call_add.py
opened ./add.pl
writing '3 4' to ./add.pl subprocess
wrote '3 4' to ./add.pl subprocess
received: 7



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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Bruno Desthuilliers

Steven D'Aprano a écrit :

On Sat, 17 Jan 2009 20:49:38 +0100, Bruno Desthuilliers wrote:


Russ P. a écrit :


(snip)
 

Why leave to
coding standards and company policy what can be encoded right into the
language?

Because human are smarter than computers.


That's an awfully naive statement. It's a sound-byte instead of a 
reasoned argument. We're smarter than computers?


Obviously and definitively, yes. Not that we are that smart - it's just 
that computers are totally stupids. The differences between a human and 
a computer are that

1/ a human can eventually DWIM.
2/ a computer can do what is has been told way faster than any human

The first point is being smart. The second is being fast. Not quite the 
same thing !-)


Then why are we 
programming in languages like Python instead of directly in machine code? 
Why can optimizing C compilers make more efficient code than the best 
human assembly language programmers?


Because :
1/ this is something that can be solved by heavy computations
2/ these computations can be programmed
3/ these compuations would just take too long for a human being to do 
manually



Humans and computers are smart at different things.


s/smart/good/g


Why leave to humans (who are known to err) what can be automated
relatively easily? Isn't that what computers are for?

Error is human. For a real catastrophic failure, it requires a computer.


Oh rubbish. That's a sound-byte invented by nervous technophobes scared 
of computers.


Nope, that's a computer-user joke.




All those "setters" and
"getters" are a kludge. I think Python "properties" are a major step
forward here. Just for fun, I checked to see if Scala has properties.
Guess what? Not only does it have them, but they are generated
automatically for all member data. That's even better than Python
properties!

Oh yes ? "Better", really ? So it's better to have a language that
automagically breaks encapsulation (requiring an additionnal level of
indirection) than a language that do the right thing by default ? I'm
afraid I missed the point ???


You certainly do. How do properties "break" encapsulation rather than 
enforcing it?


Properties by themselves are not the problem, quite on the contrary - as 
you say, they actually help wrt/ encapsulation. What breaks 
encapsulation is *automatic generation* for properties for *each and 
any* implementation attribute. Might as well just makes them all public 
attribute then.






As I said before, enforced encapsulation may not be appropriate for
every application, but it is definitely appropriate for some.

No. It is appropriate for dummy managers hiring dummy programmers. The
project's size and domain have nothing to do with it.

Let me try to be very clear here. We are dealing with two separate but
related issues. The first is whether data hiding should be added to
Python.

No need to add it, it's already there : every name that starts with an
underscore is hidden !-)


That's not hidden. It's there in plain sight. 


Once again, I'm afraid you missed the joke - despite the smiley.


Whether it can be added without screwing up the language, I don't know.
If not, then so be it. That just limits the range of domains where
Python is suitable.

That's just plain stupid.


No it's not. It's *practical*. There are domains where *by law* code 
needs to meet all sorts of strict standards to prove safety and security, 
and Python *simply cannot meet those standards*.


Oh, sorry. I was talking about *technical* issues, not about legal ones. 
IANAL...


(snip)


I like to use the example of the flight software for a large commercial
transport aircraft, but many other examples could be given. How about
medical systems that control radiation therapy or chemotherapy? How
about financial systems that could take away your retirement account in
1.5 milliseconds. Or how about the control software for the strategic
nuclear arsenals of the US or Russia? When you consider the sea, air,
and land-based components, I'm sure that's one hell of a lot of code!

And ? Such systems have been written (and quite a lot are still running)
with languages way more permissive than Python. You know, languages like
C or assembly. 


Yes, and it is *hard* because the programmer has to worry about data 
hiding *on his own*.


Nope, it's hard because C and assembly are very low-level languages 
where you have to micro-manage each and everything. This has nothing to 
do with data hiding (unless you count memory management as data hiding ?)



One of my friends has worked for many years programming some pretty high-
powered banking software. Their approach is to move data-hiding into the 
database, or the operating system. Cobol doesn't enforce encapsulation, 
but the database and OS certainly do, with a permissions-based approach.


Guess what ? Whatever the language (Cobol, Java, Python, or even VB), 
chances are such an application would be written using a RDBMS (not even 
addressing the point about OS).

Re: pep 8 constants

2009-01-18 Thread Francesco Bochicchio
On Wed, 14 Jan 2009 08:13:30 +, Steven D'Aprano wrote:


> 
> Absolutely. It's rather sad that I can do this:
> 
> import math
> math.pi = 3.0
> 
> I like the ability to shoot myself in the foot, thank you very much, but 
> I should at least get a warning when I'm about to do so:
> 
> math.PI = 3.0  # use God-like powers to change a constant
> 
> 

Constants would be a nice addition in python, sure enough.
But I'm not sure that this can be done without a run-time check every time
the constant is used, and python is already slow enough. Maybe a check
that is disabled when running with optimizing flags ?

But I'm sure this discussion has been already made and the FINAL WORD has
been already spoken.

Ciao

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


Re: uninstall before upgrade?

2009-01-18 Thread Diez B. Roggisch

waltbrad schrieb:

I want to upgrade from 2.5 to 2.6.  Do I need to uninstall 2.5 before
I do that? If so, what's the best way to uninstall it?  Thanks.


No, several versions of python can live happily together.

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


Re: import urllib2 fails with Python 2.6.1 on Vista

2009-01-18 Thread Scott MacDonald
Ah yes, with your help I seem to have solved my own problem.  I had
PYTHONPATH defined to point to the 2.5 directory.

Thanks!
Scott

On Sun, Jan 18, 2009 at 11:01 AM, Scott MacDonald <
scott.p.macdon...@gmail.com> wrote:

> Yes, I see your point.  Not sure how that would happen.  It is possible to
> have multiple versions of python on the same machine I assume?
>
> During the installation I have specified the directory to install python
> in, otherwise I have not changed anything.  Could it be an environment
> variable or something like that?
>
> Thanks,
> Scott
>
>
>
>
> On Sun, Jan 18, 2009 at 12:44 AM, Gabriel Genellina <
> gagsl-...@yahoo.com.ar> wrote:
>
>> En Sat, 17 Jan 2009 17:13:00 -0200, Scott MacDonald
>>  escribió:
>>
>>  I googled a bit this morning search for an answer to this problem but
>>> have
>>> come up empty so far.  Can anyone help?
>>>
>>> Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
>>>
>>  ^
>>
>>> (Intel)]
>>> on win32
>>> Type "help", "copyright", "credits" or "license" for more information.
>>>
  import urllib2
>>
> Traceback (most recent call last):
>>>  File "", line 1, in 
>>>  File "C:\dev\opt\Python25\Lib\urllib2.py", line 92, in 
>>>
>>  
>>
>> It seems your have a confusing setup. Why is Python 2.6 using
>> C:\dev\opt\Python25?
>>
>> --
>> Gabriel Genellina
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: function argument dependent on another function argument?

2009-01-18 Thread Rob Williscroft
Aaron Brady wrote in
news:582ef883-0176-4984-9521-6c1894636...@a26g2000prf.googlegroups.com
in comp.lang.python: 

> On Jan 18, 10:44 am, Rob Williscroft  wrote:
>> Aaron Brady wrote
>> innews:6a10378f-addb-4d56-bc1b-0c382b3cb...@t26g2000prh 
> .googlegroups.com
>> in comp.lang.python:
>>

>> > It is too bad that it is so much work to detect whether 'y' was
>> > passed in the function call directly.  However, sentinel is just as
>> > good (or nearly); at worst, you need one sentinel per argument per
>> > function, 
>>
>> One per Module should be good enough. The only reason None doesen't
>> suffice is that it has other legitimate uses.  Though to be honest
>> I would always use None as the sentinel if it wasn't a legitimate
>> argument.
>>
>> > which is possible to create, which has a specific meaning.  If you
>> > are
>> > making systematic function calls, e.g. with a dictionary or list,
>> > you can just use the sentinel in the dictionary.
>>
>> IIUYC then, one sentinel is still only needed as the missing argument
>> is indicated by *both* position and value or by name and value (in
>> the case of a keyword-dictionary), so seperate distinct sentinel
>> objects aren't required, for example:
>>
>> SENTINEL = object()
>>
>> def f( a, b, c = SENTINEL, d = SENTINEL ):
>>   print( "values: %r" % ( ( a, b, c, d ), ) )
>>   if c is SENTINEL:
>>     print( "c is missing" )
>>   if d is SENTINEL:
>>     print( "d is missing" )
>>
>> f( *( 1, 2, SENTINEL, SENTINEL ) )
>>
>> f( **dict( a = 1 , b = 2, d = 4 ) )
>>
>> f( **dict( a = 1 , b = 2, d = 4, c = SENTINEL ) )

 
> I don't have a concrete example, so you may prove to be right, but I'm
> not convinced.

I'm afraid I can't think of a use case for passing default values around
eiither, and I suspect if we were to come up with one, a better solution
that didn't involve passing default values around could be found.
 
> If you have one function with an argument that defaults to an empty
> list, and calls another with an argument that defaults to an empty
> dict, then what is the meaning of passing sentinel to the first one?
> Whereas, if each had their own, then passing the first one's default
> would mean the empty list, and passing the second one's default would
> mean the dict.

If you *mean* to pass an "empty list" or "empty dict"'s you should do 
it like:

  function_taking_list( [] )
  function_taking_dict( {} )

Its when you don't (have reason to) care that you need default arguments.

> (Or, even if that evaluates correctly, perhaps there is no such useful
> program.)

I agree, though I think some confusion arises here as there are two
(at least) distinct meanings for default arguments in python:

1) provide a value for when the caller doesn't, eg: 

def f( a = 1 ): ...

2) provide a cache for the functions /private/ use, eg:

def f( cache = {} ): ...

If the two are mixed up, then I can imagine a situation where somebody
might want to start passing default caches around.  It could only end
in tears.

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: import urllib2 fails with Python 2.6.1 on Vista

2009-01-18 Thread Scott MacDonald
Yes, I see your point.  Not sure how that would happen.  It is possible to
have multiple versions of python on the same machine I assume?

During the installation I have specified the directory to install python in,
otherwise I have not changed anything.  Could it be an environment variable
or something like that?

Thanks,
Scott



On Sun, Jan 18, 2009 at 12:44 AM, Gabriel Genellina
wrote:

> En Sat, 17 Jan 2009 17:13:00 -0200, Scott MacDonald
>  escribió:
>
>  I googled a bit this morning search for an answer to this problem but have
>> come up empty so far.  Can anyone help?
>>
>> Python 2.6.1 (r261:67517, Dec  4 2008, 16:51:00) [MSC v.1500 32 bit
>>
>  ^
>
>> (Intel)]
>> on win32
>> Type "help", "copyright", "credits" or "license" for more information.
>>
>>> import urllib2
>
 Traceback (most recent call last):
>>  File "", line 1, in 
>>  File "C:\dev\opt\Python25\Lib\urllib2.py", line 92, in 
>>
>  
>
> It seems your have a confusing setup. Why is Python 2.6 using
> C:\dev\opt\Python25?
>
> --
> Gabriel Genellina
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: calling an external program and capturing the output

2009-01-18 Thread Xah Lee
On Jan 18, 8:41 am, Eric  wrote:
> Coming from a perl background I'm new to the Python world.  I need to
> read a list of values, send each value to an external program and
> capture and act on the output of that program. Reading and parsing the
> initial values is not a problem but I can't seem to find anything on
> the sending to and capturing from an external program.
>
> Any suggestions would be greatly appreciated.

See:

• Making System Calls in Perl and Python
  http://xahlee.org/perl-python/system_calls.html

  Xah
∑ http://xahlee.org/

☄

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


Re: Python 3: exec arg 1

2009-01-18 Thread Rob Williscroft
Steven D'Aprano wrote in news:018342f9$0$8693$c3e8...@news.astraweb.com
in comp.lang.python: 

> I'm not sure if this is a stupid question or not, but what's a 
> TextIOWrapper? In the example you give:
> 
> exec(open(fname))
> 
> the argument to exec -- open(fname) -- is a file object:
> 
 type(open('hello.py'))
> 
> 
> 
> BTW, exec is a statement. The brackets there are totally superfluous.
> You can, and should, write: 
> 
> exec open(fname)
> 

You must have missed the subject line: "Re: Python 3: exec arg 1"

Python 3.0 (r30:67507, Dec  3 2008, 19:44:23) [MSC v.1500 64 bit (AMD64)] 
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> open( "hello.py" )

>>> exec "a = 1"
  File "", line 1
exec "a = 1"
   ^
SyntaxError: invalid syntax
>>> exec( "a = 1" )
>>> a
1
>>>

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: function argument dependent on another function argument?

2009-01-18 Thread Aaron Brady
On Jan 18, 10:44 am, Rob Williscroft  wrote:
> Aaron Brady wrote 
> innews:6a10378f-addb-4d56-bc1b-0c382b3cb...@t26g2000prh.googlegroups.com
> in comp.lang.python:
>
>
>
> > On Jan 18, 9:36 am, Paul Rubin  wrote:
> >> Steven D'Aprano  writes:
> >> > def foo(self, x, y=None):
> >> >     if y is None:
> >> >         y = self.a
>
> >> > I don't find that clumsy in the least. I find it perfectly readable
> >> > and
> >  a
> >> > standard idiom.
>
> >> That has the same problem as the earlier version.  If the person
> >> passes None, they get self.a.  I prefer:
>
> >>     sentinel = object()
> >>     ...
>
> >>     def foo(x, y=sentinel):
> >>       if y is sentinel:
> >>           y = self.a
>
> > It is too bad that it is so much work to detect whether 'y' was passed
> > in the function call directly.  However, sentinel is just as good (or
> > nearly); at worst, you need one sentinel per argument per function,
>
> One per Module should be good enough. The only reason None doesen't
> suffice is that it has other legitimate uses.  Though to be honest
> I would always use None as the sentinel if it wasn't a legitimate
> argument.
>
> > which is possible to create, which has a specific meaning.  If you are
> > making systematic function calls, e.g. with a dictionary or list, you
> > can just use the sentinel in the dictionary.
>
> IIUYC then, one sentinel is still only needed as the missing argument
> is indicated by *both* position and value or by name and value (in the
> case of a keyword-dictionary), so seperate distinct sentinel objects
> aren't required, for example:
>
> SENTINEL = object()
>
> def f( a, b, c = SENTINEL, d = SENTINEL ):
>   print( "values: %r" % ( ( a, b, c, d ), ) )
>   if c is SENTINEL:
>     print( "c is missing" )
>   if d is SENTINEL:
>     print( "d is missing" )
>
> f( *( 1, 2, SENTINEL, SENTINEL ) )
>
> f( **dict( a = 1 , b = 2, d = 4 ) )
>
> f( **dict( a = 1 , b = 2, d = 4, c = SENTINEL ) )
>
> Rob.
> --http://www.victim-prime.dsl.pipex.com/

I don't have a concrete example, so you may prove to be right, but I'm
not convinced.

If you have one function with an argument that defaults to an empty
list, and calls another with an argument that defaults to an empty
dict, then what is the meaning of passing sentinel to the first one?
Whereas, if each had their own, then passing the first one's default
would mean the empty list, and passing the second one's default would
mean the dict.

(Or, even if that evaluates correctly, perhaps there is no such useful
program.)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Am I interacting with the database correctly?

2009-01-18 Thread Gabriel Genellina
En Sun, 18 Jan 2009 13:54:06 -0200, John Fabiani   
escribió:



I have never worked with MySQL.  I do work with others.  The first part
looks fine.  If you insert, update or delete then you need a 'commit' or
a 'rollback'.  Preparing data for a report it is unlikely that you need  
to

commit or rollback anything.  After all you are only using 'select'.


Note that you have to commit/rollback a transaction *even* if you only  
execute select statements - at least when using isolation levels higher  
than "read uncommited".
By example, using "repeatable reads", a "select" on table A blocks any  
attempt to modify the involved rows until the transaction ends. And using  
"read commited", it won't see rows modified or added after the transaction  
began.


--
Gabriel Genellina

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


Re: Am I interacting with the database correctly?

2009-01-18 Thread Gabriel Genellina
En Sun, 18 Jan 2009 13:54:06 -0200, John Fabiani   
escribió:



I have never worked with MySQL.  I do work with others.  The first part
looks fine.  If you insert, update or delete then you need a 'commit' or
a 'rollback'.  Preparing data for a report it is unlikely that you need  
to

commit or rollback anything.  After all you are only using 'select'.


Note that you have to commit/rollback a transaction *even* if you only  
execute select statements - at least when using isolation levels higher  
than "read uncommited".
By example, using "repeatable reads", a "select" on table A blocks any  
attempt to modify the involved rows until the transaction ends. And using  
"read commited", it won't see rows modified or added after the transaction  
began.


--
Gabriel Genellina

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


Re: function argument dependent on another function argument?

2009-01-18 Thread Rob Williscroft
Aaron Brady wrote in
news:6a10378f-addb-4d56-bc1b-0c382b3cb...@t26g2000prh.googlegroups.com
in comp.lang.python: 

> On Jan 18, 9:36 am, Paul Rubin  wrote:
>> Steven D'Aprano  writes:
>> > def foo(self, x, y=None):
>> >     if y is None:
>> >         y = self.a
>>
>> > I don't find that clumsy in the least. I find it perfectly readable
>> > and 
>  a
>> > standard idiom.
>>
>> That has the same problem as the earlier version.  If the person
>> passes None, they get self.a.  I prefer:
>>
>>     sentinel = object()
>>     ...
>>
>>     def foo(x, y=sentinel):
>>       if y is sentinel:
>>           y = self.a
> 
> It is too bad that it is so much work to detect whether 'y' was passed
> in the function call directly.  However, sentinel is just as good (or
> nearly); at worst, you need one sentinel per argument per function,

One per Module should be good enough. The only reason None doesen't
suffice is that it has other legitimate uses.  Though to be honest
I would always use None as the sentinel if it wasn't a legitimate
argument.

> which is possible to create, which has a specific meaning.  If you are
> making systematic function calls, e.g. with a dictionary or list, you
> can just use the sentinel in the dictionary.

IIUYC then, one sentinel is still only needed as the missing argument
is indicated by *both* position and value or by name and value (in the
case of a keyword-dictionary), so seperate distinct sentinel objects
aren't required, for example:

SENTINEL = object()

def f( a, b, c = SENTINEL, d = SENTINEL ):
  print( "values: %r" % ( ( a, b, c, d ), ) )
  if c is SENTINEL:
print( "c is missing" )
  if d is SENTINEL:
print( "d is missing" )

f( *( 1, 2, SENTINEL, SENTINEL ) )

f( **dict( a = 1 , b = 2, d = 4 ) )

f( **dict( a = 1 , b = 2, d = 4, c = SENTINEL ) )

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Bruno Desthuilliers

Russ P. a écrit :

On Jan 17, 11:49 am, Bruno Desthuilliers
 wrote:


Please educate yourself and learn about why Ariane 5 crashed on it's
first flight, due to an error in a module written in ADA (which is such
a psychorigid language that C++ and Java are even looser than Javascript
in comparison). Perhaps will it light a bulb for you.


The claim here regarding the Ariane 5 failure is one of those urban
myths that refuses to die.


You failed the test. Sorry.


It has been refuted over and over


What has been refuted exactly ?


(including on this thread already), but I would just like to add
something in defense of Ada.

Studies have found that Ada reduces both bugs and long-term
development costs by something like a factor of two compared to C.


This _might_ (or not - there are too many variables to get any 
scientific certitudes here) be true. But that was not the point. See my 
other posts here for more about it.


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


Re: calling an external program and capturing the output

2009-01-18 Thread Diez B. Roggisch

Eric schrieb:

Coming from a perl background I'm new to the Python world.  I need to
read a list of values, send each value to an external program and
capture and act on the output of that program. Reading and parsing the
initial values is not a problem but I can't seem to find anything on
the sending to and capturing from an external program.

Any suggestions would be greatly appreciated.


See the module subprocess.

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


Re: Python and threads

2009-01-18 Thread Diez B. Roggisch

vedrandeko...@yahoo.com schrieb:

Hello again,

Thanks for previous help on "Start two threads in same time" it was
useful,but when I run this
two threads, I think they don't start at the same time, here is my
code snippet:


import threading

class ThreadedClass1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
a=True
while a==True:
bm=my_module.MyClass()
a=bm.get_Python_Process_Usage_Function()   #Returns True
or False

class ThreadedClass2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
os.popen("my_python_script.py")



threaded_obj = ThreadedClass1()
threaded_obj.start()
threaded_obj2 = ThreadedClass2()
threaded_obj2.start()


If you want to synchronize two or more threads, you need to do so 
manually using objects like Locks, Events, Conditions and Semaphores. 
See the threading module.


Even if you managed to get two threads started simultaneously (which the 
 OS doesn't even offer IINM), the would soon run out of sync.


How about you tell us what you *want*, and we tell you if and how it's 
possible to do that.


Diez

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Bruno Desthuilliers

Russ P. a écrit :

No one ever claimed that a programming language, no matter how
rigorous, can eliminate all bugs. All a language can do is to reduce
their rate of occurrence.

The Ariane fiasco was not a failure of Ada per se but rather a failure
of people using Ada. 


Almost right.


They attempted to re-use software written for one
rocket for another without proper testing. No language can prevent
that sort of error.


Now this is plain right.



We can argue forever about the usefulness of language-enforced
restriction of access to private data and methods. I have no doubt
whatsoever that it is very useful at least for the extreme cases of
very large, safety-critical systems.


And my POV is that it's just plain useless.


If you don't think access to
private data needs to be restricted for control of strategic nuclear
arsenals, for example, I think you're crazy, but that's just my
opinion.


If you think that this kind of access restriction makes softwares 
controling strategic nuclear arsenal any safer, then *you* are totally 
crazy. As *you* stated above, "no language can prevent this kind of error".



The only reasonable question in my mind is where the line should be
drawn between systems that should have enforced restrictions and those
that can rely on coding standards and voluntary cooperation among
programmers.


The only reasonable question in *my* mind is whether you think it's 
better, specially for safety-critical stuff, to hire people you can 
trust or to rely on technology.



A while back, I read something about the final integration of the
flight software on the Boeing 777, which was written mainly in Ada.
The claim was made that this integration took only three days, whereas
normally it would be expected to take more like three months with a
less rigorous language such as C++.  The reason for the simplified
integration is that Ada enforces interfaces and prevents access to
private data and methods.


C++ does it too. Or at least, that's a very common "claim" about C++.


Apparently such enforcement can improve the
efficiency of software production -- and that's not just in "theory."


My own experience is that too much rigidity in a language only leads to 
more boilerplate code and more workarounds (design patterns anyone ?), 
IOW more accidental complexity, hence more space for bugs to creep in.


My very humble (no don't say it) opinion about this is that what's 
important is how you handle and manage the whole project (including both 
technical and non-technical aspects), not the technology used (which 
might be relevant - I of course wouldn't use Python for anything 
real-time - or not - most of the monstruogigantic "entreprise" software 
written in Java would work just as well in Python or any other decent 
language).


No technology will prevent human errors - I think this point is clear, 
and that we both agree on it. OTHO, some technologies can at least help 
reducing the opportunities for human errors - and I think we also agree 
on this. Now the point is : *how* can a given techno helps wrt/ this 
second goal. Here you have two philosophies. One is that you'll reduce 
errors by improving simplicity and readability, and by promoting trust 
(whithin the team), sense of responsabily, and communication. The other 
one is that you'll reduce errors by not allowing those stupid 
code-monkeys to do anything that *might* (according mostly to unproved 
assertions) be "dangerous" - hence promoting distrust and 
irresponsability, and adding quite a lot of accidental complexity. BTW, 
do you know the by far most common Java idiom ? It's named the 
"do-nothing catch-all exception handler". As an excercise, explain the 
reasons behind this idiom, and it's practical results.


As a last point : I certainly don't think Python is perfect in any way, 
*nor* that it's the appropriate tool for each and any project. I just 
disagree with you on your assertion that Python is not ok for large or 
critical projects because of it's "lack" of language-enforced access 
restriction.

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


Re: Ordering attributes for dynamically generated class

2009-01-18 Thread Duncan Booth
Aaron Brady  wrote:

> On Jan 18, 9:52 am, David Pratt  wrote:
>> Hi list. I use 'type' to generate classes but have a need to order  
>> the attributes for the generated class. Of course a dict is not going  
>> to maintain ordering. Is there any way to dynamically generate a  
>> class with attributes in specific order?
>>
>> my_new_class = type( 'MyNewClass', tuple_of_bases, dict_of_attributes)
>>
>> Many thanks,
>> David
> 
> Just a thought, you can subclass 'dict' and assign an instance of it
> to the __dict__ member of your new instance.
> 
You can certainly pass a subclass of 'dict' to type(), in fact in Python 
3.0 you can use a metaclass with a __prepare__ method to substitute your 
own value for __dict__ in any class definition, but I don't think your own 
type will preserved when the instance is actually constructed.

A simple solution which works in any version of Python is just to maintain 
an attribute with the desired ordering and override __dir__ to return the 
attributes in that order.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and threads

2009-01-18 Thread Stefan Behnel
vedrandeko...@yahoo.com wrote:
> Thanks for previous help on "Start two threads in same time" it was
> useful,but when I run this
> two threads, I think they don't start at the same time

That's normal. Threading is an unpredictable concurrency pattern. Things
often don't happen the way one would want them to.

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


calling an external program and capturing the output

2009-01-18 Thread Eric

Coming from a perl background I'm new to the Python world.  I need to
read a list of values, send each value to an external program and
capture and act on the output of that program. Reading and parsing the
initial values is not a problem but I can't seem to find anything on
the sending to and capturing from an external program.

Any suggestions would be greatly appreciated.

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


Re: changing URLs in webpages, python solutions?

2009-01-18 Thread Stefan Behnel
Simon Forman wrote:
> I want to take a webpage, find all URLs (links, img src, etc.) and
> rewrite them in-place, and I'd like to do it in python (pure python
> preferred.)

lxml.html has functions specifically for this problem.

http://codespeak.net/lxml/lxmlhtml.html#working-with-links

Code would be something like

html_doc = lxml.html.parse(b"http://.../xyz.html";)
html_doc.rewrite_links( ... )
print( lxml.html.tostring(html_doc) )

It also handles links in CSS or JavaScript, as well as broken HTML documents.

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Paul Rubin
Bruno Desthuilliers  writes:
> As I already stated, no technology can protect us from this kind of
> error. Ask yourself why this module was reused as-is, instead of going
> thru the whole specs / tests / QA process again, and *perhaps* you'll
> start to understand why I say that language-enforced access
> restrictions are the wrong solution to a real problem.

I have not seen anywhere that the specs stated that the sensor reading
that overflowed that variable was supposed to be in range.  It looks
to me (but I don't know for sure) that the development process allowed
an assumption to creep into the code that wasn't stated in the specs.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Paul Rubin
"Russ P."  writes:
> I don't know which variant of Ada was used here, but something called
> the "Ravenscar Profile" is a reduced subset of Ada that might have
> prevented this error (though I haven't verified this). Then there is
> Spark Ada, which supposed to be much safer than even Ada.

I'm not sure, but I think Ravenscar and Spark would both have
permitted the cast.  However, Spark is intended to be used with an
external tool called Spade (sort of a Pylint on steroids) that would
not have allowed the cast unless it was provable (starting from the
specification) that the number was in range before the cast.

I.e. the cast was wrong because of the failure of an unstated
assumption that a certain sensor reading was in a certain range.
Spark may still have allowed the cast only if the assumption was
stated explicitly in the specification.  Requiring the assumption to
be stated may have made the error more likely to be spotted as part of
the surrounding systems engineering.  Of course, the specification can
still be wrong, but that's hardly a problem with Ada or even with the
software.  It's not too much different than if the specification says
the rocket is 10 feet wide and they build the launch pad for that
size, but then the rocket turns out to actually be 12 feet wide.

I have Barnes's book about Spark at the office and will try to check
next week what it says about these casts.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ordering attributes for dynamically generated class

2009-01-18 Thread David Pratt
Hi Aaron. Yeah, definitely sounds like a possibility. I was able to  
locate an ordered dict implementation that subclasses dict. This  
might work fine.  Might be able to pass into type method directly  
since I think that dict passed into type is setting __dict__ I  
believe.  Let you know if that works out. Many thanks.


Regards,
David


On Jan 18, 2009, at 11:57 AM, Aaron Brady wrote:


On Jan 18, 9:52 am, David Pratt  wrote:

Hi list. I use 'type' to generate classes but have a need to order
the attributes for the generated class. Of course a dict is not going
to maintain ordering. Is there any way to dynamically generate a
class with attributes in specific order?

my_new_class = type( 'MyNewClass', tuple_of_bases,  
dict_of_attributes)


Many thanks,
David


Just a thought, you can subclass 'dict' and assign an instance of it
to the __dict__ member of your new instance.
--
http://mail.python.org/mailman/listinfo/python-list


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


Re: dynamic module import?

2009-01-18 Thread Duncan Booth
alex23  wrote:

> I must confess I've rarely had a need to use __import__ and don't
> think I've ever used the fromlist arg. I'm confused, though, because
> the docstring states:
> 
>  The fromlist should be a list of names to emulate ``from name
> import ...''
> 
> But it also states that __import__ always returns a module, so I'm
> utterly confused as to the purpose of fromlist, or how to inject the
> specified entries into the calling namespace. If anyone could explain
> this for me, I'd really appreciate it.

Compare these:

>>> dom =  __import__('xml').dom
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'dom'
>>> dom =  __import__('xml', fromlist=['dom']).dom
>>> dom


Then in a new session:
>>> import xml
>>> xml.dom
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'dom'
>>> from xml import dom
>>> dom

>>>

When you import a package such as xml it only imports the top level. 
Modules and subpackages within the imported package aren't available until 
they are explicitly imported. The fromlist argument to __import__ allows 
you to force the lower modules to also import.

>>> xml = __import__('xml', fromlist=['dom'])

is effectively the same as doing:

>>> import xml.dom

After either of these you have a name 'xml' which has an attribute 'dom':

>>> xml.dom


To access the actual sub-objects using __import__ use getattr on the 
returned module. So far as injecting names into the calling namespace is 
concerned just assign to variables; you don't want to be injecting unknown 
names into your namespace.

For the same effect if you only have one name and you know it is a module 
you could do:

>>> xml = __import__('xml.dom')

but you need the fromlist parameter if you don't know which of the names 
are modules.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Bruno Desthuilliers

Russ P. a écrit :

On Jan 17, 1:43 pm, Paul Rubin  wrote:

Bruno Desthuilliers  writes:

Once again, there's quite a lot to learn from
the story of Ariane 5.

Do you know what actually happened with Ariane 5?  The failure was
because "smart" humans overrode the language enforced protection by
casting a floating point number down to a 16-bit integer, which worked
ok in Ariane 4, but failed with an overflow on Ariane 5 where bigger


So this turns out to be an example of a failure due, not to the
*rigidity* of Ada, but to its *permissiveness* in allowing such a
cast.


Nope. This is an example of failure due to the *human* part of the 
process - this happened because of a lack of testing / QA, not because 
of a language feature or misfeature.


(snip)


This is one thing that Python gets right, automatically using bignums
rather than allowing int overflow.  In that sense, Python has more
enforced protection than Ada.


True, but Ada does not have the luxury of just using doubles and
"bignums" everywhere, because it needs to work on cheap processors
too.   But perhaps it could somehow be configured to do so by the user
if sufficiently powerful computers are being used.


Here the error was *not* to disable the overflow error checking (in the 
context of Ariane 4), but to reuse the module as-is in another context.


As I already stated, no technology can protect us from this kind of 
error. Ask yourself why this module was reused as-is, instead of going 
thru the whole specs / tests / QA process again, and *perhaps* you'll 
start to understand why I say that language-enforced access restrictions 
are the wrong solution to a real problem.


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


Re: process command line parameter

2009-01-18 Thread Thorsten Kampe
* asit (Sat, 17 Jan 2009 13:28:12 -0800 (PST))
> Recently I was coding a link extractor. It's a command line stuff and
> takes parameter as argument.
> I found that the in operator is not always helpful.
> eg. if "--all" in sys.argv:
>print "all links will be printed"
> 
> its not helpful when some attribute value is sent in command line
> parameter.
> hence I want to process some data like find=".co.uk"
> 
> How can i do this ???

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


Python and threads

2009-01-18 Thread vedrandekovic
Hello again,

Thanks for previous help on "Start two threads in same time" it was
useful,but when I run this
two threads, I think they don't start at the same time, here is my
code snippet:


import threading

class ThreadedClass1(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
a=True
while a==True:
bm=my_module.MyClass()
a=bm.get_Python_Process_Usage_Function()   #Returns True
or False

class ThreadedClass2(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)

def run(self):
os.popen("my_python_script.py")



threaded_obj = ThreadedClass1()
threaded_obj.start()
threaded_obj2 = ThreadedClass2()
threaded_obj2.start()

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-18 Thread Bruno Desthuilliers

Paul Rubin a écrit :

Bruno Desthuilliers  writes:

Once again, there's quite a lot to learn from
the story of Ariane 5.


Do you know what actually happened with Ariane 5?


*yes I do* - else I wouldn't mention it. Thanks.


 The failure was
because "smart" humans overrode the language enforced protection by
casting a floating point number down to a 16-bit integer, which worked
ok in Ariane 4, but failed with an overflow on Ariane 5 where bigger
numbers were involved.


The failure was because a module tested, QA'd and certified within a 
given context (in which it was ok to drop the builtin error handling) 
was reused in a context where it was not ok. And the point is exactly 
that : no *technology* can solve this kind of problem, because it is a 
*human* problem (in that case, not taking time to repass the whole specs 
/ tests / QA process given context change).

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


Re: Ordering attributes for dynamically generated class

2009-01-18 Thread Aaron Brady
On Jan 18, 9:52 am, David Pratt  wrote:
> Hi list. I use 'type' to generate classes but have a need to order  
> the attributes for the generated class. Of course a dict is not going  
> to maintain ordering. Is there any way to dynamically generate a  
> class with attributes in specific order?
>
> my_new_class = type( 'MyNewClass', tuple_of_bases, dict_of_attributes)
>
> Many thanks,
> David

Just a thought, you can subclass 'dict' and assign an instance of it
to the __dict__ member of your new instance.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Am I interacting with the database correctly?

2009-01-18 Thread John Fabiani
Hussein B wrote:

> Hey,
> I'm new with database interactions in Python and I'm not sure if I'm
> handling the cursor and transactions correctly:
> 
> cursor = db.cursor(MySQLdb.cursors.DictCursor)
> cursor.execute(flate_rate_pkgs_sql)
> rows = cursor.fetchall()
> #I have for loop here to iterate over rows
>cursor.execute() 
>rows = cursor.fetchall()
># some more cursor.execute() calls but only SQL select statements
># here is another for loop that contains try block
>   # here are cursor.execute() calls, both insert and update
>   db.commit()
>   # in the except code block, I use db.rollback()
> 
> As you see, my script contains only one db object and one cursor
> object and both the db and cursor objects are used multiple times, it
> is ok?
> As you might figured, this is a script for reports :)
> Thanks.

I have never worked with MySQL.  I do work with others.  The first part
looks fine.  If you insert, update or delete then you need a 'commit' or
a 'rollback'.  Preparing data for a report it is unlikely that you need to
commit or rollback anything.  After all you are only using 'select'.

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


Ordering attributes for dynamically generated class

2009-01-18 Thread David Pratt
Hi list. I use 'type' to generate classes but have a need to order  
the attributes for the generated class. Of course a dict is not going  
to maintain ordering. Is there any way to dynamically generate a  
class with attributes in specific order?


my_new_class = type( 'MyNewClass', tuple_of_bases, dict_of_attributes)

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


Re: function argument dependent on another function argument?

2009-01-18 Thread Aaron Brady
On Jan 18, 9:36 am, Paul Rubin  wrote:
> Steven D'Aprano  writes:
> > def foo(self, x, y=None):
> >     if y is None:
> >         y = self.a
>
> > I don't find that clumsy in the least. I find it perfectly readable and a
> > standard idiom.
>
> That has the same problem as the earlier version.  If the person
> passes None, they get self.a.  I prefer:
>
>     sentinel = object()
>     ...
>
>     def foo(x, y=sentinel):
>       if y is sentinel:
>           y = self.a

It is too bad that it is so much work to detect whether 'y' was passed
in the function call directly.  However, sentinel is just as good (or
nearly); at worst, you need one sentinel per argument per function,
which is possible to create, which has a specific meaning.  If you are
making systematic function calls, e.g. with a dictionary or list, you
can just use the sentinel in the dictionary.
--
http://mail.python.org/mailman/listinfo/python-list


Re: what's the point of rpython?

2009-01-18 Thread Luis M . González
On Jan 18, 8:56 am, andrew cooke  wrote:
> Since this is a PyPy bashing thread, maybe it's an appropriate place
> to suggest that the project has got a little bit waylaid by exploring
> cool things instead of releasing a useful final result?
>
> I am not questioning rpython directly - the case for something like
> that is obvious.  But there's a question of balance.  It's possible to
> go on building ever more complex systems which are theoretically
> justified, but which postpone ever finishing the job.  At some point
> there has to be a "good enough".
>
> To some extent I am playing devil's advocate here, but as an outside
> who looked at PyPy a while back, my uninformed and naive impression
> was that the project was suffering from the kid of issues I have
> caricatured above
>
> Andrew
>
> PS I guess you are aware of worse is better etc?  I think this may
> also be a US/Euro culture issue...

It's worth adding that, regarding the goal of having a faster python,
we have had for a long time a solid and useful proof-of-concept in
psyco. Pypy rolls up on this concept and adds many more useful
features, not all of them related to speed but to flexibility.
I have no doubt the project will be succesful. The question is how
long we will have to wait...

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


Re: Difference between Python 2.2.2 and Python 2.5

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 07:30:52 -0800, Ravi wrote:

> I am developing for PyS60 1.4.4 which supports Python 2.2.2 while what I
> know is Python 2.5  .
> 
> Can you please tell me differences between the two so that I can save
> myself from incompatible code.

Everything new mentioned here:

http://www.python.org/doc/2.5/whatsnew/whatsnew25.html
http://www.python.org/doc/2.4/whatsnew/whatsnew24.html
http://www.python.org/doc/2.3/whatsnew/whatsnew23.html

won't exist in Python 2.2.


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


Re: output problem

2009-01-18 Thread 7stud
On Jan 16, 8:24 am, "Jean-Paul VALENTIN"  wrote:
> Feature? the output of below Hello program he0.py started from command
> line looks as follows:
> F:\prompt>he0.py
> Hello
> F:\prompt>
>
> 'Hello' was sent with sys.stdout.write, so "without newline".
> But why cannot be output (more logically):
> F:\prompt>he0.py
> HelloF:\prompt>
>
> Is it normal? Is there any means or workaround to obtain that output if
> I wish??
> 
> __
> #!/usr/bin/python
> #-*- coding: iso-8859-15 -*-
> import sys
> sys.stdout.write('Hello')
>

Not on a Mac:

import sys
sys.stdout.write("hello")

output:
$ python 6test.py
hello$

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


Re: function argument dependent on another function argument?

2009-01-18 Thread Paul Rubin
Steven D'Aprano  writes:
> def foo(self, x, y=None):
> if y is None:
> y = self.a
> 
> I don't find that clumsy in the least. I find it perfectly readable and a 
> standard idiom.

That has the same problem as the earlier version.  If the person
passes None, they get self.a.  I prefer:

sentinel = object()
...

def foo(x, y=sentinel):
  if y is sentinel:
  y = self.a
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3: exec arg 1

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 14:36:15 +, Alan G Isaac wrote:

> Well, that does not really answer my question, imo. I do not much care
> about the disappearance of ``execfile``. I was asking, why is it a
> **good thing** that ``exec`` does not accept a TextIOWrapper? 


I'm not sure if this is a stupid question or not, but what's a 
TextIOWrapper? In the example you give:

exec(open(fname))

the argument to exec -- open(fname) -- is a file object:

>>> type(open('hello.py'))



BTW, exec is a statement. The brackets there are totally superfluous. You 
can, and should, write: 

exec open(fname)


> Or is it just not implemented yet?
> What is the gain from this particular backwards incompatibility (in the
> sense that ``exec(open(fname))`` no longer works)?

Simplicity of implementation?


> Context: I used to be able to tell students they could run their scripts
> from the interpreter prompt with ``execfile(fname)``.  I expected to be
> able to tell them to ``exec( open(fname) )``, which worked in Python 2. 
> Now instead it is ``exec( open(filename).read() )`` which is a bit more
> mysterious to a newbie.  (Note: I teach economics, not computer
> science.) 

Why don't you give your students a site.py module containing

def execfile(fname):
exec open(fname).read()

and tell them to do "from site import execfile"?

If you control the environment they are running their programs on (school 
computers?) then you can put a startup file in their path and have 
execfile imported automatically.


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


Difference between Python 2.2.2 and Python 2.5

2009-01-18 Thread Ravi
I am developing for PyS60 1.4.4 which supports Python 2.2.2 while what
I know is Python 2.5  .

Can you please tell me differences between the two so that I can save
myself from incompatible code.
--
http://mail.python.org/mailman/listinfo/python-list


Re: function argument dependent on another function argument?

2009-01-18 Thread Steven D'Aprano
On Sun, 18 Jan 2009 06:19:03 -0800, Reckoner wrote:

> I  would like to do:
> 
> def foo(self,x,y=self.a)
> 
> where the default value for y=self.a. Since this is not possible, I wind
> up doing
> 
> 
> def foo(self,x,y=None)
>   if not y:
> y=self.a
> 
> but that seems kind of clumsy.

It's also incorrect, because if you pass y=0 as an argument, or any other 
false value, it will be replaced by self.a.


> Is there a better way to do this?

def foo(self, x, y=None):
if y is None:
y = self.a


I don't find that clumsy in the least. I find it perfectly readable and a 
standard idiom.


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


Re: function argument dependent on another function argument?

2009-01-18 Thread Aaron Brady
On Jan 18, 8:19 am, Reckoner  wrote:
> I  would like to do:
>
> def foo(self,x,y=self.a)
>
> where the default value for y=self.a. Since this is not possible, I
> wind up doing
>
> def foo(self,x,y=None)
>   if not y:
>     y=self.a
>
> but that seems kind of clumsy.
>
> Is there a better way to do this?
>
> Thanks in advance

No.  The only alternative is really niche and probably not what you
want.  You'd use a decorator (which I don't have, btw):

@defval( y= selfattrget( 'a' ) )
def foo( self, x, y ).



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


Re: Python 3: exec arg 1

2009-01-18 Thread Alan G Isaac

Alan G Isaac wrote:

Is it intentional that ``exec`` cannot handle a TextIOWrapper?
Bottom line: has ``execfile(filename)`` really become
``exec(open(filename).read())``? Is this a good thing?




On 1/17/2009 4:20 PM Terry Reedy apparently wrote:

Yes. Yes.




Alan G Isaac wrote:

OK.  Why?



On 1/17/2009 10:08 PM Terry Reedy apparently wrote:

This: execfile(filename)
is a trivial (9 keystroke) abbreviation of
this: exec(open(filename).read())
which is similar in effect to
this: from filename import *
so it is really not needed.



Well, that does not really answer my question, imo.
I do not much care about the disappearance of ``execfile``.
I was asking, why is it a **good thing** that
``exec`` does not accept a TextIOWrapper?
Or is it just not implemented yet?
What is the gain from this particular backwards
incompatibility (in the sense that ``exec(open(fname))``
no longer works)?

Context: I used to be able to tell students they
could run their scripts from the interpreter
prompt with ``execfile(fname)``.  I expected to
be able to tell them to ``exec( open(fname) )``,
which worked in Python 2.  Now instead it is
``exec( open(filename).read() )`` which is a bit
more mysterious to a newbie.  (Note: I teach
economics, not computer science.) And despite your
claim above, you know that ``import`` is not
really equivalent, and even more so now that
``reload`` must be imported.

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


Re: output problem

2009-01-18 Thread Pierre Bourdon
IIRC, Windows automatically add a newline after the program output.

On Fri, Jan 16, 2009 at 4:24 PM, Jean-Paul VALENTIN
 wrote:
> Feature? the output of below Hello program he0.py started from command
> line looks as follows:
> F:\prompt>he0.py
> Hello
> F:\prompt>
>
> 'Hello' was sent with sys.stdout.write, so "without newline".
> But why cannot be output (more logically):
> F:\prompt>he0.py
> HelloF:\prompt>
>
> Is it normal? Is there any means or workaround to obtain that output if
> I wish??
> 
> __
> #!/usr/bin/python
> #-*- coding: iso-8859-15 -*-
> import sys
> sys.stdout.write('Hello')
>
>
> About Ingenico: Ingenico is the world's leading provider of payment 
> solutions, with over 15 million terminals deployed across the globe. 
> Delivering the very latest secure electronic payment technologies, 
> transaction management and the widest range of value added services, Ingenico 
> is shaping the future direction of the payment solutions market. Leveraging 
> on its global presence and local expertise, Ingenico is reinforcing its 
> leadership by taking banks and businesses beyond payment through offering 
> comprehensive solutions, a true source of differentiation and new revenues 
> streams.
>  This message may contain confidential and/or privileged information. If you 
> are not the addressee or authorized to receive this for the addressee, you 
> must not use, copy, disclose or take any action based on this message or any 
> information herein. If you have received this message in error, please advise 
> the sender immediately by reply e-mail and delete this message. Thank you for 
> your cooperation.
>  P Please consider the environment before printing this e-mail
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


function argument dependent on another function argument?

2009-01-18 Thread Reckoner

I  would like to do:

def foo(self,x,y=self.a)

where the default value for y=self.a. Since this is not possible, I
wind up doing


def foo(self,x,y=None)
  if not y:
y=self.a

but that seems kind of clumsy.

Is there a better way to do this?

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


  1   2   >