Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Antoon Pardon
On 2008-06-03, Lie <[EMAIL PROTECTED]> wrote:
>
> Python has an extremely good design because the BDFL doesn't just
> listen to everyone and create a product that tries to please
> everybody, no, he listens to those that have good ideas and tells the
> stupid ideas to go away and he applies a subjective decision which
> more often than not leads to a better python.

I agree that Guido van Rossum has done an excellent job. That doesn't
mean he has to be painted as unfailable in which the ideais he accepts
are good ideas and those he rejects are bad ideas almost by definition.

Guido has been known to change his mind, which is an admirabele quality,
but it does show that at some point he rejected a good idea or accepted
a bad idea.

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


Re: Help need with subprocess communicate

2008-06-03 Thread rdabane
On Jun 3, 11:23 pm, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Tue, 3 Jun 2008 18:04:40 -0700 (PDT), [EMAIL PROTECTED] declaimed the
> following in comp.lang.python:
>
>
>
> > Hi Daniel,
> > Thanks for your reply..
> > I've done exactly as you suggested...but I'm still having problem with
> > the read...it just gets stuck in
> > the read ( I think because its a blocking read...)
>
> And it is likely blocking because the subprocess is doing buffered
> output -- ie, nothing is available to be read because the output has not
> been flushed.
>
> This is a problem with most programs when run as a subprocess -- it
> is common for stdout, when routed to a pipe or file, to behave as a
> buffered stream that only flushes when some x-bytes have been written;
> unlike stdout to a console which gets flushed on each new-line.
> --
> WulfraedDennis Lee Bieber   KD6MOG
> [EMAIL PROTECTED]  [EMAIL PROTECTED]
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff:   [EMAIL PROTECTED])
> HTTP://www.bestiaria.com/


Is there way to configure the stdout buffer size so that it flushes
earlier..
Is there a way to make above mentioned piece code working?
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to perform a nonblocking read from a process

2008-06-03 Thread rdabane
On Jun 3, 7:53 pm, sturlamolden <[EMAIL PROTECTED]> wrote:
> On Jun 4, 3:20 am, [EMAIL PROTECTED] wrote:
>
> > It seems that stdout.readline() is a blocking read and it just gets
> > stuck their..
> > How to fix this ..
>
> Threads are the simplest remedy for blocking i/o.



> Threads are the simplest remedy for blocking i/o.
I've to admit I'm a newbie to this kind of programming...
what if I have to run thousands of these commands...it doesn't make
sense to create
thousands of threads..
Is there a way that above mentioned piece of code be made to worked...


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


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Ben Finney
Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> writes:

> On Wed, 04 Jun 2008 13:50:42 +1000, Ben Finney wrote:
> 
> > It seems you [alex23] have a different idea of what unit testing
> > is for from me.
> 
> For me it's about finding bugs where documentation and
> implementation disagree.

Where "documentation" is "specified externally-visible behaviour of
the unit", I agree with this.

> And if you document private functions

By definition, "private" functions are not part of the publicly
documented behaviour of the unit. Any behaviour exhibited by some
private component is seen externally as a behaviour of some public
component.

> it makes sense to me to also test if they work as documented.

If they affect the behaviour of some public component, that's where
the documentation should be.

If they *don't* affect the external behaviour, then they shouldn't be
there :-) Or, at least, their behaviour shouldn't be asserted as part
of the tests of external behaviour.

> Because the official API relies on the correct implementation of the
> private parts it uses under the hood.

Only to the extent that the documented behaviour of the API is
affected. The main benefit of marking something as "not public" is
that one *is* free to change its behaviour, so long as the public API
is preserved.

> One part of writing unit tests is invoking functions with arguments
> that you think are "corner cases". For example test if a function
> that takes a list doesn't bomb out when you feed the empty list into
> it. Or if it handles all errors correctly.

This sounds like part of the externally-visible behaviour of the code
unit; i.e. something that belongs in the public API. I agree that this
is the domain of a unit test.

> If a function `f()` calls internally `_g()` and that function might
> even call other private functions, then you have to know how `f()`
> works internally to create input that checks if error handling in
> `_g()` works correctly.

No, you don't need to know how it works internally; you need only know
what guarantees it must keep for its external behaviour.

If someone wants to alter the `_g()` function, or remove it entirely
while preserving the correct behaviour of `f()`, that should have no
effect on the external behaviour of `f()`.

That is to say, the knowledge of the "internals" of `f()` in your
example is actually knowledge of something that should be documented
as part of the external behaviour of `f()` — that, or it's not
relevant to the behaviour of `f()` and shouldn't be unit tested in
order that encapsulation is preserved.

> What do you do in such a situation? Build something from untested
> private parts and just test the assembled piece?

Assert the corner-case behaviour of `f()`, through unit tests that
operate on `f()` without caring about its internals.

> I prefer to test the private functions too. After all the private
> functions are not private to the everybody, there *are* functions
> that rely on them working correctly.

Then for *those* interfaces, unit tests can be devised that make
assertions about those interfaces.

-- 
 \   "[T]he speed of response of the internet will re-introduce us |
  `\to that from which our political systems have separated us for |
_o__) so long, the consequences of our own actions."  -- Douglas Adams |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Marc 'BlackJack' Rintsch
On Wed, 04 Jun 2008 13:50:42 +1000, Ben Finney wrote:

> alex23 <[EMAIL PROTECTED]> writes:
> 
>> So the basic answers I'm seeing that "do just fine" are:
>> 
>> 1. Don't test private functions.
>> 2. Add functionality _to_ the private functions for testing.
>> 3. Change the interface for the purpose of testing.
>> 
>> All of which seem exceptionally inefficient and run counter to the
>> whole purpose of unit testing.
> 
> It seems you have a different idea of what unit testing is for from
> me.

For me it's about finding bugs where documentation and implementation
disagree.  And if you document private functions it makes sense to me to
also test if they work as documented.  Because the official API relies on
the correct implementation of the private parts it uses under the hood.

> Isn't the entire point of encapsulation to separate internal
> components from the external interface?
> 
> Why would a unit test, the whole purpose of which is to assert some
> aspect of the external behaviour of the unit of code, care about how
> that code unit is implemented internally?

One part of writing unit tests is invoking functions with arguments that
you think are "corner cases".  For example test if a function that takes a
list doesn't bomb out when you feed the empty list into it.  Or if it
handles all errors correctly.

If a function `f()` calls internally `_g()` and that function might even
call other private functions, then you have to know how `f()` works
internally to create input that checks if error handling in `_g()` works
correctly.  So it goes against your understanding of unit tests.

What do you do in such a situation?  Build something from untested private
parts and just test the assembled piece?  I prefer to test the private
functions too.  After all the private functions are not private to the
everybody, there *are* functions that rely on them working correctly.

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


Re: Books for programmers

2008-06-03 Thread Paul Rubin
V <[EMAIL PROTECTED]> writes:
> I think that I'm interested in a more advance book, ideally one that
> talk of the Python gotchas, traps, pitfall, idioms, performance,
> stile, and so on. 

I may have missed it but I haven't seen Python in a Nutshell mentioned
in this thread.  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q about object identity

2008-06-03 Thread Marc 'BlackJack' Rintsch
On Tue, 03 Jun 2008 23:08:46 +0200, Christian Heimes wrote:

> [EMAIL PROTECTED] schrieb:
>> Hello,
>> 
>> I am testing object identity.
>> 
>> If I do it from the interpreter, I get strange results.
>> 
> print [] is []
>> False
>> 
> print id([]), id([])
>> 3083942700 3083942700
>> 
>> 
>> 
>> Why is that? Isn't this an error?
> 
> No, it's not an error. You are getting this result because the list
> implementation keeps a bunch of unused list objects in a free list. It's
> an optimization trick. Python has to create two different list objects
> for "[] is []" while it can reuse the same list object for id([]) == id([]).

I don't think you need optimization tricks for the explanation.  In ``[]
is []`` there need to be two lists that exist at the same time to be
compared by the ``is`` operator.  With ``id([]) == id([])`` just the
id numbers have to exist at the same time, so the execution may look like
this:

• create empty list
• call `id()` with it
• remember first id number
• when `id()` returns, the list is not referenced anymore and can be
  garbage collected
• create empty list, and here the list object might get allocated at the
  same memory location as the first empty list → same id.
• call `id()` with it
• remember second id number
• garbage collect second empty list
• compare both numbers

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

Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Russ P.
On Jun 3, 8:50 pm, Ben Finney <[EMAIL PROTECTED]>
wrote:
> alex23 <[EMAIL PROTECTED]> writes:
> > So the basic answers I'm seeing that "do just fine" are:
>
> > 1. Don't test private functions.
> > 2. Add functionality _to_ the private functions for testing.
> > 3. Change the interface for the purpose of testing.
>
> > All of which seem exceptionally inefficient and run counter to the
> > whole purpose of unit testing.
>
> It seems you have a different idea of what unit testing is for from
> me.
>
> Isn't the entire point of encapsulation to separate internal
> components from the external interface?
>
> Why would a unit test, the whole purpose of which is to assert some
> aspect of the external behaviour of the unit of code, care about how
> that code unit is implemented internally?
>
> If changing the internal, encapsulated components of a unit causes its
> external behaviour to change, that's a bug; either in the change made
> (it shouldn't have altered the external behaviour), or in the unit
> test asserting the wrong thing (it shouldn't be asserting anything
> about internal state of the code).
>
> --
>  \   “Try to become not a man of success, but try rather to become |
>   `\ a man of value.” —Albert Einstein |
> _o__)  |
> Ben Finney

Thank you. Let me just add that, as I said before, I think "private"
data (if it were added to Python) should be accessible through some
sort of "indirect" mechanism akin to the double-leading-underscore
rule. Then, even if it *is* needed for unit testing, it can be
accessed.

As for unit testing in C++, Java, and Ada, I confess I know nothing
about it, but I assume it gets done. Considering that Ada is used to
manage and control fighter jets, cruise missiles, and nuclear
arsenals, let's hope it gets done right.
--
http://mail.python.org/mailman/listinfo/python-list


Re: python blogs

2008-06-03 Thread pythonblogs
On Jun 2, 7:43 pm, Benjamin <[EMAIL PROTECTED]> wrote:
> On Jun 2, 1:49 pm, [EMAIL PROTECTED] wrote:
>
> > Hello!
>
> > It seems likePythonblogsare gaining popularity. It seems to me that
> > they play a crucial role in promotingPythonas a language.
> > Do you agree with that?
>
> > Just a few days ago I've finished setting up a dedicatedPython
> > blogging environment at:http://www.pythonblogs.com
> > Do you think it will be useful forPythoncommunity?
> > By the way, everyone is welcome to join.
>
> Thanks very much, but the extension says it's written in PHP!

Sure, python-coded Python blogging platform would be great. But I do
not aware of such system which allows multi user/ multi blog with a
separate domains for each blog. Besides I've have experience in
tweaking this particular system.

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


Supply, Cheap Dior Burberry Chanel Fendi Sunglasses & Sandals

2008-06-03 Thread cheapforwholesale555
Discount Coach Sandals, Dior Sandals, Prada  Sandals, Chanel Sandals,
Versace Sandals, Crocs Sandals, LV Sandals, ( G U C C I ) Sandals,
Women's Sandals Men's Slippers From
China

Brand Sunglasses Wholesale:

 Discount, Prada Sunglasses
 Discount, D&G Sunglasses
 Discount, Fendi Sunglasses
 Discount, Burberry Sunglasses
 Discount, Chanel Sunglasses
 Discount, LV Sunglasses
 Discount, Dior Sunglasses
 Discount, (G U C C I ) Sunglasses
 Discount, Armani Sunglasses
 Discount, Versace Sunglasses
 Discount, A&F Sunglasses
 Discount, LV Sunglasses

Wholesale, Prada Sunglasses
Wholesale,  D&G Sunglasses
Wholesale,  Fendi Sunglasses
Wholesale,  Burberry Sunglasses
Wholesale,  Chanel Sunglasses
Wholesale,  LV Sunglasses
Wholesale,  Dior Sunglasses
Wholesale,  ( G U C C I ) Sunglasses
Wholesale,  Armani Sunglasses
Wholesale,  Versace Sunglasses
Wholesale,  A&F Sunglasses
Wholesale,  LV Sunglasses

( www.c heapforwholesale.com )
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Ben Finney
alex23 <[EMAIL PROTECTED]> writes:

> So the basic answers I'm seeing that "do just fine" are:
> 
> 1. Don't test private functions.
> 2. Add functionality _to_ the private functions for testing.
> 3. Change the interface for the purpose of testing.
> 
> All of which seem exceptionally inefficient and run counter to the
> whole purpose of unit testing.

It seems you have a different idea of what unit testing is for from
me.

Isn't the entire point of encapsulation to separate internal
components from the external interface?

Why would a unit test, the whole purpose of which is to assert some
aspect of the external behaviour of the unit of code, care about how
that code unit is implemented internally?

If changing the internal, encapsulated components of a unit causes its
external behaviour to change, that's a bug; either in the change made
(it shouldn't have altered the external behaviour), or in the unit
test asserting the wrong thing (it shouldn't be asserting anything
about internal state of the code).

-- 
 \   “Try to become not a man of success, but try rather to become |
  `\ a man of value.” —Albert Einstein |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list

Re: can python do some kernel stuff?

2008-06-03 Thread sturlamolden
On Jun 4, 12:41 am, Ethan Furman <[EMAIL PROTECTED]> wrote:

> the kernel itself, *is* kernel coding.  And as wonderful as Python is,
> it is *not* for kernel coding.

Not in its present form, no, it would take some porting. But aside
from that, is there any reason one could not embed a python
interpreter in the kernel?

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


Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-03 Thread Gabriel Genellina
En Tue, 03 Jun 2008 17:11:06 -0300, Matthew Wilson <[EMAIL PROTECTED]>  
escribió:



I used defaultdict.fromkeys to make a new defaultdict instance, but I
was surprised by behavior:

>>> b = defaultdict.fromkeys(['x', 'y'], list)
   >>> b
defaultdict(None, {'y': , 'x': })
   >>> b['x']

   >>> b['z']

Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'z'

I think that what is really going on is that fromdict makes a regular
dictionary, and then hands it off to the defaultdict class.

I find this confusing, because now I have a defaultdict that raises a
KeyError.

Do other people find this intuitive?

Would it be better if defaultdict.fromkeys raised a
NotImplementedException?

Or would it be better to redefine how defaultdict.fromkeys works, so
that it first creates the defaultdict, and then goes through the keys?


That looks reasonable. It appears there is currently no way to do what you  
want (apart from using a for loop to set each key)


--
Gabriel Genellina

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


Re: can python do some kernel stuff?

2008-06-03 Thread Ethan Furman

Andrew Lee wrote:


Diez B. Roggisch wrote:


Andrew Lee schrieb:


Diez B. Roggisch wrote:

What has that todo with kernel programming? You can use e.g. pygame 
to get keystrokes. Or under linux, read (if you are root) the 
keyboard input file - I've done that to support several keyboards 
attached to a machine.


And the original question: no, python can't be used as kernel 
programming language. Amongst other reasons, performance & the GIL 
prevent that.


Diez



http://www.kernel-panic.it/programming/py-pf/

Of course you can code kernel routines in Python -- you are just 
calling the underlying C interface.  The GIL means you have to 
manage threadsafety on your own -- it doesn't imply kernel 
programming can not be done.



I understood the OP's question as "can one program kernelspace 
routines in python". Which I don't think is possible. And I don't see 
how py-pf does that either.


Diez




OP: "I am wondering if python can do some kernel coding that
used to be the private garden of C/C++."

The answer is yes.  IPC and py-pf are examples.  If you don't think of 
packet filtering as kernel coding, I can understand.  But clearly the 
Python interfaces to fork(), waitpid(), signal(), alarm() and so forth 
are forays into the once private garden of C.


Being able to call routines in the kernel is *not* the same as kernel 
coding.  Calling C routines is *not* the same as kernel coding.  
Actually writing the routines that are to be called, and that constitute 
the kernel itself, *is* kernel coding.  And as wonderful as Python is, 
it is *not* for kernel coding.


Having just looked at Py-PF, it is *managing* the firewall, not 
implementing it.  Again, not kernel coding.

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


Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-03 Thread Gabriel Genellina

En Tue, 03 Jun 2008 17:18:59 -0300, Chris <[EMAIL PROTECTED]> escribió:

On Jun 3, 10:11 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote:



I used defaultdict.fromkeys to make a new defaultdict instance, but I
was surprised by behavior:

    >>> b = defaultdict.fromkeys(['x', 'y'], list)

    >>> b
    defaultdict(None, {'y': , 'x': })

    >>> b['x']
    

    >>> b['z']
    
    Traceback (most recent call last):
      File "", line 1, in 
    KeyError: 'z'

I find this confusing, because now I have a defaultdict that raises a
KeyError.


To me it's intuitive for it to raise a KeyError, afterall the Key
isn't in the dictionary.


But the idea behind a defaultdict is to *not* raise a KeyError but use the  
default_factory to create missing values. (Unfortunately there is no way  
to provide a default_factory when using fromkeys).


--
Gabriel Genellina

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


Re: How to perform a nonblocking read from a process

2008-06-03 Thread sturlamolden
On Jun 4, 3:20 am, [EMAIL PROTECTED] wrote:

> It seems that stdout.readline() is a blocking read and it just gets
> stuck their..
> How to fix this ..

Threads are the simplest remedy for blocking i/o.

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


Re: Different execution time in python code between embedded or standalone

2008-06-03 Thread Gabriel Genellina
En Tue, 03 Jun 2008 16:58:12 -0300, Pau Freixes <[EMAIL PROTECTED]>  
escribió:



Hi list,

First Hello to all, this is my and hope not end message to the list :P

This last months I have been writting a  program in c like to mod_python  
for
embedding python language, it's a middleware for dispatch and execute  
python
batch programs into several nodes. Now I'm writing some python program  
for

test how scale this into several nodes and comparing with "standalone"
performance.

I found a very strange problem with one application named md5challenge,  
this

aplication try to calculate the max number md5 digest in several seconds,
md5challenge use a simple signal alarm for stop program when time has
passed. This is the code of python script

def handler_alrm(signum, frame):
global _signal
global _nrdigest
global _f


_signal = True

def try_me():
global _nrdigest
global _f
global _signal

_f = open("/dev/urandom","r")
while _signal is not True:
buff = _f.read(_const_b)
md5.md5(buff).hexdigest()
_nrdigest = _nrdigest + 1

if _f is not None :
_f.close()

def main( req ):
global _nrdigest


signal.signal(signal.SIGALRM, handler_alrm)
signal.alarm(req.input['time'])


try_me()

req.output['count'] = _nrdigest

return req.OK


if __name__ == "__main__":

# test code
class test_req:
pass

req = test_req()
req.input = { 'time' : 10 }
req.output = { 'ret' : 0, 'count' : 0 }
req.OK = 1

main(req)

print "Reached %d digests" % req.output['count']


When I try to run this program in standalone into my Pentium Dual Core
md4challenge reached 1.000.000 milion keys in 10 seconds but when i try  
to
run this in embedded mode md5challenge reached about 200.000 more keys  
!!! I

repeat this test many times and  always  wins  embedded mode  !!!  What's
happen ?

Also I tested to erase read dependencies from /dev/random, and calculate  
all
keys from same buffer. In this case embedded mode win always also, and  
the

difference are more bigger !!!

Thks to all, can anybody help to me ?


So the above code corresponds to the standalone version - what about the  
embedded version? Are you sure it is exactly the *same* code? All those  
global statements are suspicious, and you don't even need most of them.  
Note that looking up a name in the global namespace is much slower than  
using a local name.
Also, you're including the time it takes the OS to *generate* several  
megabytes of random data from /dev/urandom (how big is _const_b?).
Usually it's easier (and more accurate) to measure the time it takes to  
compute a long task (let's say, how much time it takes to compute 100  
md5 values). You're doing it backwards instead.

I'd rewrite the test as:

def try_me():
  from md5 import md5
  buff = os.urandom(_const_b)
  for i in xrange(100):
md5(buff).hexdigest()

def main(req):
  t0 = time.clock()
  try_me()
  t1 = time.clock()
  # elapsed time = t1-t0


PS: Recuerdo que respondí esto en la lista de Python en castellano, pero  
ahora veo que mi mensaje nunca llegó :(


--
Gabriel Genellina

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


Re: getting an HTTP Error 500 on a form post

2008-06-03 Thread iBlaine
problem solved...changes to the python code were not necessary
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread alex23
On Jun 4, 4:29 am, "Russ P." <[EMAIL PROTECTED]> wrote:
> If you think that private data and methods should not be allowed
> because they complicate unit testing, then I suggest you take a look
> at how unit testing is done is C++, Java, and Ada. They seem to do
> just fine.

Nice to put the burden of evidence back onto everyone else, but doing
a bit of searching I found the following "answers" to the question of
unit-testing private functions & methods:

> I suggest that tests should be written only for the public methods.

> You can use a debugger, probably Carbide. That way you can see
> all the variables. Otherwise, write the values to a log or EMCT.
> You can make the logging only happen for debug builds if you don't
> want the logging in the production code. If you really need to
> see the private variables from your code, declare them public in
> debug builds.

> Problem is testing private functions. Some can be fixed by
> promoting private to protected, inheriting the class adding
> testing in the class. Others get refactored out the classes
> they reside in and get put into their own functor classes[...]

So the basic answers I'm seeing that "do just fine" are:

1. Don't test private functions.
2. Add functionality _to_ the private functions for testing.
3. Change the interface for the purpose of testing.

All of which seem exceptionally inefficient and run counter to the
whole purpose of unit testing.

> But I think there is a more fundamental issue here. You complain about
> problems with software that uses data encapsulation. So two
> possibilities exist here: either the designers of the code were not
> smart enough to understand what data or methods the client would need,
> or the client is not smart enough to understand what they need. Maybe
> the solution is smarter programmers and clients rather than a dumber
> language.

This is the most ludicrous argument I've ever heard. Of _course_ we
can't predict every possible usage of our code that others might want
it for. If someone can easily extend code that I've written to improve
or increase its functionality, why would I want to prevent them from
doing so?

Then again, I tend to think of other programmers as "peers" rather
than clients. YMMV.
--
http://mail.python.org/mailman/listinfo/python-list


How to make py2.5 distutil to use VC2005?

2008-06-03 Thread 甜瓜
Howdy,
This problem have puzzled me for a long time. I usually use
python2.5 in Windows, while VC2005 is installed.
However python25.lib is compiled by VC2003. When I use disutil to
build some C extensions, it complaints that
there is no VC2003.
Well, IMO, the format of binary files generated by VC2003 and
VC2005 is compatible in most cases. What
should I do to workaround this error? I mean, disable distutil
complaints and use VC2005 to build C extensions.
I have google-ed some discussion related on this topic. It seems that
it's real possible!
Thank you in advance.
--
ShenLei
--
http://mail.python.org/mailman/listinfo/python-list


Re: a python phpmyadmin like program

2008-06-03 Thread alex23
On Jun 4, 12:12 pm, Gandalf <[EMAIL PROTECTED]> wrote:
> mmm, for windows... ?

Do we have to do this dance every single time you ask a question?

Your time (and ours) would be better invested in your learning how to
search for things via Google.
--
http://mail.python.org/mailman/listinfo/python-list


Re: a python phpmyadmin like program

2008-06-03 Thread Gandalf
On Jun 3, 11:53 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Gandalf schrieb:
>
> > is their any graphic program for handling sqlite like phpmyadmin or
> > access in python?
>
> rekall?
>
> Diez

mmm, for windows... ?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Handling some isolated iso-8859-1 characters

2008-06-03 Thread Justin Ezequiel
On Jun 4, 2:38 am, Daniel Mahoney <[EMAIL PROTECTED]> wrote:
> I'm working on an app that's processing Usenet messages. I'm making a
> connection to my NNTP feed and grabbing the headers for the groups I'm
> interested in, saving the info to disk, and doing some post-processing.
> I'm finding a few bizarre characters and I'm not sure how to handle them
> pythonically.
>
> One of the lines I'm finding this problem with contains:
> 137050  Cleo and I have an anouncement!   "Mlle. =?iso-8859-1?Q?Ana=EFs?="
> <[EMAIL PROTECTED]>  Sun, 21 Nov 2004 16:21:50 -0500
> <[EMAIL PROTECTED]>  447869 Xref:
> sn-us rec.pets.cats.community:137050
>
> The interesting patch is the string that reads "=?iso-8859-1?Q?Ana=EFs?=".
> An HTML rendering of what this string should look would be "Anaïs".
>
> What I'm doing now is a brute-force substitution from the version in the
> file to the HTML version. That's ugly. What's a better way to translate
> that string? Or is my problem that I'm grabbing the headers from the NNTP
> server incorrectly?

>>> from email.Header import decode_header
>>> decode_header("=?iso-8859-1?Q?Ana=EFs?=")
[('Ana\xefs', 'iso-8859-1')]
>>> (s, e), = decode_header("=?iso-8859-1?Q?Ana=EFs?=")
>>> s
'Ana\xefs'
>>> e
'iso-8859-1'
>>> s.decode(e)
u'Ana\xefs'
>>> import unicodedata
>>> import htmlentitydefs
>>> for c in s.decode(e):
... print ord(c), unicodedata.name(c)
...
65 LATIN CAPITAL LETTER A
110 LATIN SMALL LETTER N
97 LATIN SMALL LETTER A
239 LATIN SMALL LETTER I WITH DIAERESIS
115 LATIN SMALL LETTER S
>>> htmlentitydefs.codepoint2name[239]
'iuml'
>>>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Handling some isolated iso-8859-1 characters

2008-06-03 Thread Gabriel Genellina
En Tue, 03 Jun 2008 15:38:09 -0300, Daniel Mahoney <[EMAIL PROTECTED]>  
escribió:



I'm working on an app that's processing Usenet messages. I'm making a
connection to my NNTP feed and grabbing the headers for the groups I'm
interested in, saving the info to disk, and doing some post-processing.
I'm finding a few bizarre characters and I'm not sure how to handle them
pythonically.

One of the lines I'm finding this problem with contains:
137050  Cleo and I have an anouncement!   "Mlle.  
=?iso-8859-1?Q?Ana=EFs?="

<[EMAIL PROTECTED]>  Sun, 21 Nov 2004 16:21:50 -0500
<[EMAIL PROTECTED]>  447869 Xref:
sn-us rec.pets.cats.community:137050

The interesting patch is the string that reads  
"=?iso-8859-1?Q?Ana=EFs?=".

An HTML rendering of what this string should look would be "Anaïs".

What I'm doing now is a brute-force substitution from the version in the
file to the HTML version. That's ugly. What's a better way to translate
that string? Or is my problem that I'm grabbing the headers from the NNTP
server incorrectly?


No, it's not you, those headers are formatted following RFC 2047  

Python already has support for that format, use the email.header class,  
see 


--
Gabriel Genellina

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


ANN: equivalence 0.2

2008-06-03 Thread George Sakkis
Equivalence v0.2 has been released. Also the project is now hosted at
http://code.google.com/p/pyquivalence/ (the name 'equivalence' was
already taken but the module is still called equivalence).

Changes
===
- The internals were largely rewritten, but the API remained
effectively intact.
- A new `equivalence(**kwds)` factory function is now the preferred
way to create an equivalence. Two kwds supported for now, `key` and
`bidirectional`.
- Now uses the union-find algorithm on disjoint-set forests; improves
the linear worst-case time to practically constant amortized time for
the basic operations.
- Modular redesign; the original Equivalence class has been broken
down into subclasses, each trading off more features with extra
overhead:
  - The base Equivalence class is a vanilla disjoint-sets forest; it
doesn't support keys and partition() is slow, but the rest operations
are faster.
  - KeyEquivalence adds implicit (key-based) equivalence.
  - BidirectionalEquivalence provides fast partition() at the cost of
higher memory overhead and slowing down slightly the rest operations.
  - KeyBidirectionalEquivalence combines the two previous.
- Added more tests and updated docs.

About
=
*equivalence* is a Python module for building equivalence relations:
partitionings of objects into sets that maintain the equivalence
relation properties (reflexivity, symmetry, transitivity). Two objects
are considered equivalent either explicitly, after being merged, or
implicitly, through a key function.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Import, site packages, my modules, Windows vs. Linux

2008-06-03 Thread Ben Finney
John Ladasky <[EMAIL PROTECTED]> writes:

> I want to know what is the *recommended* way to integrate my own
> personal modules with Python.  Thanks!

You want the 'distutils' documentation
http://www.python.org/doc/lib/module-distutils> and the documents
that it references, which will lead you to write a 'setup.py' module
for your package.

Then, it's a matter of running 'python ./setup.py install' to install
your package on a particular system.

-- 
 \  "I hope if dogs ever take over the world, and they chose a |
  `\king, they don't just go by size, because I bet there are some |
_o__)Chihuahuas with some good ideas."  -- Jack Handey |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: printf in python

2008-06-03 Thread Gabriel Genellina
En Tue, 03 Jun 2008 10:28:57 -0300, gianluca <[EMAIL PROTECTED]>  
escribió:
> > On Mon, 2 Jun 2008 00:32:33 -0700 (PDT), gianluca  
<[EMAIL PROTECTED]>

> > declaimed the following in comp.lang.python:

> > > Hy, I've a problem with may python library generated with swig  
from C
> > > code. I works and I can access all function but a  simèple  
function

> > > that print a string don't work's.
> > > The function is like this:
> > > int PrintTEST()
> > > {
> > >  printf("TEST ");
> > >  return 1;
> > > }


I know!! I'm bore!! But I need help indeed!!
I'm looking inside lib_wrap.c generated by swig. In the wrap function
there is no printf function. If is this the problem how can I do to
resolve it?


Generated swig code is... uhm, ugly at least, and rather undebuggable. I'd  
try to avoid it.
Another alternative is Cython: I've not actually used it myself, but I've  
seen good reports from it.
If your library is written in C (not C++), you may use directly ctypes  
from inside Python.


--
Gabriel Genellina

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


How to perform a nonblocking read from a process

2008-06-03 Thread rdabane
Hi,
I'm trying to perform following operation from inside the python
script
1. Open a shell ( start a process )
2. Send command1 to the process
3. Get output from the process
4. Send command2 to the process
5. Get output from the process
..


Following is sample code :

from subprocess import *
p2 = Popen('python',stdin=PIPE,stdout=PIPE,universal_newlines=True)
for i in range(10):
p2.stdin.write('print 10'+'\n')
o,e = p2.stdout.readline()
print o,e


It seems that stdout.readline() is a blocking read and it just gets
stuck their..
How to fix this ..

All the help is appreciated ..

Thanks,
-Rahul.



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


Re: Help need with subprocess communicate

2008-06-03 Thread rdabane
On Jun 3, 5:42 pm, Daniel Klein <[EMAIL PROTECTED]> wrote:
> On Tue, 3 Jun 2008 14:04:10 -0700 (PDT), [EMAIL PROTECTED] wrote:
> >I'm trying to perform following type of operation from inside a python
> >script.
> >1. Open an application shell (basically a tcl )
> >2. Run some commands on that shell and get outputs from each command
> >3. Close the shell
>
> >I could do it using communicate if I concatenate all my commands
> >( separated by newline ) and read all the output in the end. So
> >basically I could do following sequence:
> >1. command1 \n command2 \n command 3 \n
> >2. Read all the output
>
> >But I want to perform it interactively.
> >1. command1
> >2. read output
> >3. command2
> >4. read output ..
>
> >Following is my code:
>
> >from subprocess import *
> >p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
> >o,e = p2.communicate(input='qdl_help \n qdl_read  \n
> >qdl_reg_group_list ')
>
> >Please suggest a way to perform it interactively with killing the
> >process each time I want to communicate with it.
>
> Use
> stdin.write(command + '\n')
> to 'send' data to the sub-process.
>
> Use
> stdout.readline()
> to 'receive' data from the sub-process.
>
> But to use this requires you open the subprocess with:
>
> universal_newlines = True
>
> It assumes that 'command' will be sent with '\n' and received data will come
> in a line at a time. Your Python program needs to know what to expect; you
> are in control.
>
> Alternatively, you can use std.write() and stdout.read() (without
> universal_newlines) but this means you need to create your own IPC protocol
> (like netstrings).
>
> Hope this helps,
>
> Daniel Klein

Hi Daniel,
Thanks for your reply..
I've done exactly as you suggested...but I'm still having problem with
the read...it just gets stuck in
the read ( I think because its a blocking read...)

following is a simple example of problem..please try running it ...

from subprocess import *
p2 =
Popen('python',shell=True,stdin=PIPE,stdout=PIPE,universal_newlines=True)
for i in range(10):
p2.stdin.write('print 10'+'\n')   # Write Command
o = p2.stdout.readline()  # Read Command
print o


I appreciate all your help...

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


Import, site packages, my modules, Windows vs. Linux

2008-06-03 Thread John Ladasky
Hi folks,

Running Python 2.5 on both a Windows XP laptop, and an Ubuntu Linux
7.04 desktop.

I've gotten tired of maintaining multiple copies of my personal
modules that I use over and over.  I have copies of these files in the
same directory as the main program I happen to be working on at the
time.  I've also downloaded FANN, and want to use its Python
bindings.  FANN does not seem to build automatically, like wxWidgets
did.

These two issues have led me to examine exactly how the import
statement works, how the PYTHONPATH environment variable is
constructed, and how to change it.

On Windows I found a solution that works, but which may be a kludge.
In the Python "site-packages" folder, I added a sub-folder called "my-
packages".  Then I created a text file, "my-packages.pth", containing
the single line, "my-packages."  Finally, I moved my common personal
modules into the my-packages folder and deleted all of my clumsy
duplicates.  Import statements now work for all of my files on the
Windows box, great!

I then tried to use this same strategy in Linux, and saw that I don't
automatically have the privileges needed to alter the site-packages
folder.  On my Windows box, my default account has Administrator
privileges.  On Linux I can, of course, use sudo to modify the site-
packages folder.  But the fact that I would have to use sudo has me
asking -- is there something inappropriate, or unsafe in my approach?

I want to know what is the *recommended* way to integrate my own
personal modules with Python.  Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Help need with subprocess communicate

2008-06-03 Thread Daniel Klein
On Tue, 3 Jun 2008 14:04:10 -0700 (PDT), [EMAIL PROTECTED] wrote:

>I'm trying to perform following type of operation from inside a python
>script.
>1. Open an application shell (basically a tcl )
>2. Run some commands on that shell and get outputs from each command
>3. Close the shell
>
>I could do it using communicate if I concatenate all my commands
>( separated by newline ) and read all the output in the end. So
>basically I could do following sequence:
>1. command1 \n command2 \n command 3 \n
>2. Read all the output
>
>But I want to perform it interactively.
>1. command1
>2. read output
>3. command2
>4. read output ..
>
>Following is my code:
>
>from subprocess import *
>p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
>o,e = p2.communicate(input='qdl_help \n qdl_read  \n
>qdl_reg_group_list ')
>
>Please suggest a way to perform it interactively with killing the
>process each time I want to communicate with it.

Use
stdin.write(command + '\n')
to 'send' data to the sub-process.

Use
stdout.readline()
to 'receive' data from the sub-process.

But to use this requires you open the subprocess with:

universal_newlines = True

It assumes that 'command' will be sent with '\n' and received data will come
in a line at a time. Your Python program needs to know what to expect; you
are in control.

Alternatively, you can use std.write() and stdout.read() (without
universal_newlines) but this means you need to create your own IPC protocol
(like netstrings).

Hope this helps,

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


Re: "Faster" I/O in a script

2008-06-03 Thread Michael Torrie
kalakouentin wrote:
> I use python in order to analyze my data which are in a text form. The
> script is fairly simple. It reads a line form the input file, computes
> what it must compute and then write it it to a buffer/list. When the
> whole reading file is processed (essential all lines) then the
> algorithms goes ahead and writes them one by one on the output file.
> It works fine. But because of the continuous I/O it takes a lot of
> time to execute.

Sounds like perhaps generators would help.  They let you process your
data a chunk at a time, rather than reading them all in at once.  For
some powerful tips and examples, see:

http://www.dabeaz.com/generators/Generators.pdf

To me this principle was very enlightening.  Especially where you can
then apply optimizations to your data processing flow by altering when
the generators take place.  Much as how pushing selects down the tree in
relational algebra reduces runtime dramatically, using generators to
filter and process data can be made very fast.
--
http://mail.python.org/mailman/listinfo/python-list


getting an HTTP Error 500 on a form post

2008-06-03 Thread iBlaine
I'm hoping someone here can answer my problem - I'm getting a 500
error when I run this code.  What it should do is setup cookies, log
in, then post a file to a form.  The problem is it throws an exception
at ClientCookie.urlopen(form.click()).  The webserver hosting the form
is fine, the python looks like it should work, I cant figure out how
to solve or debug this.

# python code
# cookie handler
cookieJar = ClientCookie.CookieJar()
opener =
ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(cookieJar))
opener.addheaders = [("User-agent","Mozilla/5.0 (compatible)")]
ClientCookie.install_opener(opener)
fp = ClientCookie.urlopen("http://example.com/login.user";)
forms = ClientForm.ParseResponse(fp)
fp.close()

# login
form = forms[0]
form["j_username"] = "bob" # use your userid
form["j_password"] = "1234" # use your password
fp = ClientCookie.urlopen(form.click())
fp.close()

# post new file
fp = ClientCookie.urlopen("http://example.com/form_page";)
forms = ClientForm.ParseResponse(fp)
form = forms[0]
form["reportId"] = "4239"
form.add_file(open("/home/filename.xml"), "text/plain",
"filename.xml")
request = form.click()
fp = ClientCookie.urlopen(request)
fp.close()

### error
Traceback (most recent call last):
  File "./old-import-xml.py", line 62, in upload_xml
fp = ClientCookie.urlopen(request)
  File "/usr/lib/python2.5/site-packages/ClientCookie/
_urllib2_support.py", line 717, in urlopen
return _opener.open(url, data)
  File "/usr/lib/python2.5/urllib2.py", line 387, in open
response = meth(req, response)
  File "/usr/lib/python2.5/site-packages/ClientCookie/
_urllib2_support.py", line 391, in http_response
"http", request, response, code, msg, hdrs)
  File "/usr/lib/python2.5/urllib2.py", line 425, in error
return self._call_chain(*args)
  File "/usr/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
  File "/usr/lib/python2.5/urllib2.py", line 506, in
http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for master's thesis

2008-06-03 Thread MRAB
On Jun 3, 10:22 pm, Larry Bugbee <[EMAIL PROTECTED]> wrote:
> > I would like to do something with this language, yet
> > I don't know if there are any needs/science fields, that could be used
> > as a basis for a thesis.
>
> Personally, I'd like to see *optional* data typing added to Python
> perhaps along the lines of what was done in Pyrex.  You declare the
> data type when you know it, or when it matters, and skip it
> otherwise.  Your paper could analyze its pros and cons, analyze any
> potential performance gains, and recommend how to implement it.  Your
> professor will suggest some additional questions.
>
> I suspect, if the type be known and declared, the interpreter could be
> streamlined and quicker, you might get asserts for free, and perhaps,
> Python becomes even more self-documenting.  Perhaps I've missed it,
> but I haven't seen a strong analytical case made for or against
> optional data typing.  Your paper?
>
You might want to have a look at Boo at http://boo.codehaus.org/.
--
http://mail.python.org/mailman/listinfo/python-list


Re: parser recommendation

2008-06-03 Thread rurpy
On Jun 3, 2:55 pm, "Filipe Fernandes" <[EMAIL PROTECTED]> wrote:
> I haven't given up on pyparsing, although I'm now heavily leaning
> towards PLY as an end solution since lex and yacc parsing is available
> on other platforms as well.

Keep in mind that PLY's "compatibility" with YACC is functional,
not syntactical.  That is, you can not take a YACC file, replace
the actions with Python actions and feed it to PLY.

It's a shame that the Python world has no truly YACC compatible
parser like YAPP in the Perl world.
--
http://mail.python.org/mailman/listinfo/python-list


Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-03 Thread MRAB
On Jun 3, 9:11 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote:
> I used defaultdict.fromkeys to make a new defaultdict instance, but I
> was surprised by behavior:
>
> >>> b = defaultdict.fromkeys(['x', 'y'], list)
>
> >>> b
> defaultdict(None, {'y': , 'x': })
>
> >>> b['x']
> 
>
> >>> b['z']
> 
> Traceback (most recent call last):
>   File "", line 1, in 
> KeyError: 'z'
>
> I think that what is really going on is that fromdict makes a regular
> dictionary, and then hands it off to the defaultdict class.
>
> I find this confusing, because now I have a defaultdict that raises a
> KeyError.
>
> Do other people find this intuitive?
>
> Would it be better if defaultdict.fromkeys raised a
> NotImplementedException?
>
> Or would it be better to redefine how defaultdict.fromkeys works, so
> that it first creates the defaultdict, and then goes through the keys?
>
> All comments welcome.  If I get some positive feedback, I'm going to try
> to submit a patch.
>
The statement:

b = defaultdict.fromkeys(['x', 'y'], list)

is equivalent to:

b = defaultdict()
for i in ['x', 'y']:
b[i] = list

so there's no default_factory and therefore the defaultdict will
behave like a dict. Perhaps there could be an optional third argument
to provide a default_factory.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for master's thesis

2008-06-03 Thread Edward Bugbee


On Jun 3, 2008, at 2:35 PM, Andrii V. Mishkovskyi wrote:


2008/6/4 Larry Bugbee <[EMAIL PROTECTED]>:

I would like to do something with this language, yet
I don't know if there are any needs/science fields, that could be  
used

as a basis for a thesis.


Personally, I'd like to see *optional* data typing added to Python
perhaps along the lines of what was done in Pyrex.  You declare the
data type when you know it, or when it matters, and skip it
otherwise.  Your paper could analyze its pros and cons, analyze any
potential performance gains, and recommend how to implement it.  Your
professor will suggest some additional questions.

I suspect, if the type be known and declared, the interpreter could  
be

streamlined and quicker, you might get asserts for free, and perhaps,
Python becomes even more self-documenting.  Perhaps I've missed it,
but I haven't seen a strong analytical case made for or against
optional data typing.  Your paper?


I think what you are talking about is already implemented in Python
3.0 as annotations. Forgive me if I missed your point.


Close.  I haven't followed Python 3 features that closely so had to go  
back and read about annotations.  If my read is correct, annotations  
address only arguments and return values and do not affect runtime  
code.  They are there, principally, for documentation and library  
argument checking purposes.  That's a start.


In addition to arguments, I'd like the ability to optionally declare  
the types for local and global variables, and going beyond doc and  
external lib checking, I'd like to see the declarations affect the  
compilation, potentially sidestepping runtime type checking.  I  
suspect performance could be improved if the intrepreter could make  
some assumptions and not have to check type and every time.  But, that  
is a guess on my part and a paper doing a deeper analysis might prove  
or disprove the hypothesis.  (A good analysis would be non-trivial  
which is why I'm thinking it could be a good Master's Project/Thesis.)


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


Re: Keep a script running in the background

2008-06-03 Thread Guillermo

These are the basic requirements:

Script A must keep a dictionary in memory constantly and script B must
be able to access and update this dictionary at any time. Script B
will start and end several times, but script A would ideally keep
running until it's explicitly shut down.

I have the feeling the way to do this is Python is by pickling the
dict, but I need the method that gives the best performance. That's
why I'd rather want to keep it in memory, since I understand pickling
involves reading from and writing to disk.

I'm using SQLite as a database. But this dict is an especial index
that must be accessed at the highest speed possible.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Constructor re-initialization issue

2008-06-03 Thread pythonreptile
On Jun 3, 12:59 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Jun 3, 6:11 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > Hello all,
>
> > I have come across this issue in Python and I cannot quite understand
> > what is going on.
>
> > class Param():
> > def __init__(self, data={}, condition=False):
> > if condition:
> > data['class']="Advanced"
> > print data
>
> > In the previous example, I expect the variable data to be re-
> > initialized every time I construct an object type Param. However, when
> > I do the following:
>
> > Param(condition=True)
> > Param(condition=False)
>
> > The second call still prints {'class': 'Advanced'}
>
> > Shouldn't data be initialized to {} since it is the default in
> > __init__? Why would the state of data be preserved between two
> > independent instantiations?
>
> > Any help would be greatly appreciated.
>
> > M.
>
> This must be by far the most FAQ.. unfortunately it seems it will
> remain for 3.x as 
> well:http://www.python.org/doc/faq/general/#why-are-default-values-shared-...
Thanks for clearing this up for me.

M.

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


Re: Picking apart strings

2008-06-03 Thread Russ P.
On Jun 3, 11:44 am, tmallen <[EMAIL PROTECTED]> wrote:
> Is there a way to pick apart this text without resorting to regular
> expressions?
>
> p {
> color: black;
>
> }
>
> p -> element
> color -> property
> black -> value

Sure.

data = txt.strip("}").split("{")

element = data[0].strip()

items = data[1].split(";")

for item in items:

data = item.split(":")
property = data[0].strip() # avoid this keyword
value = data[1].strip()

I didn't test this. Also, the module suggested in the other reply
might make more sense, depending on how much of this sort of thing you
need to do.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Constructor re-initialization issue

2008-06-03 Thread Michael Mabin
Changing the default for data to None and creating a new dict inside your
function might handle this.  But I don't know what it is you want.  It never
even occurred to me that this behavior might be desired in the first place.

class Param(object):
   def __init__(self,data=None,condition=False):
   if data is None:
  data = {}
   if condition:
  data['class'] = 'Advanced'
   print data

Param(condition=True)
Param(condition=False)

I always thought you didn't want to have empty sequences as default values.

On Tue, Jun 3, 2008 at 5:11 PM, <[EMAIL PROTECTED]> wrote:

> Hello all,
>
> I have come across this issue in Python and I cannot quite understand
> what is going on.
>
> class Param():
>def __init__(self, data={}, condition=False):
>if condition:
>data['class']="Advanced"
>print data
>
> In the previous example, I expect the variable data to be re-
> initialized every time I construct an object type Param. However, when
> I do the following:
>
> Param(condition=True)
> Param(condition=False)
>
> The second call still prints {'class': 'Advanced'}
>
> Shouldn't data be initialized to {} since it is the default in
> __init__? Why would the state of data be preserved between two
> independent instantiations?
>
> Any help would be greatly appreciated.
>
> M.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
| _ | * | _ |
| _ | _ | * |
| * | * | * |
--
http://mail.python.org/mailman/listinfo/python-list

Re: Constructor re-initialization issue

2008-06-03 Thread George Sakkis
On Jun 3, 6:11 pm, [EMAIL PROTECTED] wrote:

> Hello all,
>
> I have come across this issue in Python and I cannot quite understand
> what is going on.
>
> class Param():
> def __init__(self, data={}, condition=False):
> if condition:
> data['class']="Advanced"
> print data
>
> In the previous example, I expect the variable data to be re-
> initialized every time I construct an object type Param. However, when
> I do the following:
>
> Param(condition=True)
> Param(condition=False)
>
> The second call still prints {'class': 'Advanced'}
>
> Shouldn't data be initialized to {} since it is the default in
> __init__? Why would the state of data be preserved between two
> independent instantiations?
>
> Any help would be greatly appreciated.
>
> M.

This must be by far the most FAQ.. unfortunately it seems it will
remain for 3.x as well: 
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects
--
http://mail.python.org/mailman/listinfo/python-list


Re: Constructor re-initialization issue

2008-06-03 Thread Mike Kent
On Jun 3, 6:11 pm, [EMAIL PROTECTED] wrote:
> Hello all,
>
> I have come across this issue in Python and I cannot quite understand
> what is going on.
>
> class Param():
> def __init__(self, data={}, condition=False):
> if condition:
> data['class']="Advanced"
> print data
>
> In the previous example, I expect the variable data to be re-
> initialized every time I construct an object type Param. However, when
> I do the following:
>
> Param(condition=True)
> Param(condition=False)
>
> The second call still prints {'class': 'Advanced'}
>
> Shouldn't data be initialized to {} since it is the default in
> __init__? Why would the state of data be preserved between two
> independent instantiations?
>
> Any help would be greatly appreciated.
>
> M.

This is a Frequently Asked Question (FAQ):
http://www.python.org/doc/faq/general/#why-are-default-values-shared-between-objects
--
http://mail.python.org/mailman/listinfo/python-list


Re: Continuous Timer

2008-06-03 Thread Robert Dailey
I just need a repeating timer, I could care less about microsecond
accuracies.

On Tue, Jun 3, 2008 at 11:19 AM, John Nagle <[EMAIL PROTECTED]> wrote:

> Gabriel Genellina wrote:
>
>> En Fri, 30 May 2008 22:50:13 -0300, Robert Dailey <[EMAIL PROTECTED]>
>> escribió:
>>
>>  Reading through the Python 2.5 docs, I'm seeing a Timer class in the
>>> threading module, however I cannot find a timer object that will
>>> continuously call a function of my choice every  amount of
>>> milliseconds.
>>> For example, every 1000 milliseconds I want a function named Foo to be
>>> called. This would continue to happen until I terminate the timer in my
>>> main
>>> thread. Thanks for the help.
>>>
>>
>> Use an Event object; its wait() will provide the sleep time, and when it
>> is set() the thread knows it has to exit.
>>
>> import threading
>> import time
>>
>> def repeat(event, every, action):
>>while True:
>>event.wait(every)
>>if event.isSet():
>>break
>>action()
>>
>
>Actually, to do this right, it's necessary to account for the time used
> by
> "action".  The code above will run no sooner than the time "every" after
> the COMPLETION of action.
>
>I've done this sort of thing under QNX, the real-time operating system,
> which has better timing primitives, and seen the action executed within
> a few microseconds of the correct time, every time.  But that was in C++.
>
>If you're trying to do hard real time in Python on Linux or Windows,
> don't expect reliable timing.  Remember, Python isn't really preemptive,
> because of the global interpreter lock and the lack of thread priorities.
>
>John Nagle
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Constructor re-initialization issue

2008-06-03 Thread pythonreptile
Hello all,

I have come across this issue in Python and I cannot quite understand
what is going on.

class Param():
def __init__(self, data={}, condition=False):
if condition:
data['class']="Advanced"
print data

In the previous example, I expect the variable data to be re-
initialized every time I construct an object type Param. However, when
I do the following:

Param(condition=True)
Param(condition=False)

The second call still prints {'class': 'Advanced'}

Shouldn't data be initialized to {} since it is the default in
__init__? Why would the state of data be preserved between two
independent instantiations?

Any help would be greatly appreciated.

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


Re: a python phpmyadmin like program

2008-06-03 Thread Diez B. Roggisch

Gandalf schrieb:

is their any graphic program for handling sqlite like phpmyadmin or
access in python?


rekall?

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


Re: Keep a script running in the background

2008-06-03 Thread Tobiah

> I need a script to keep running in the background after it's loaded
> some data. It will make this data available to the main program in the
> form of a dictionary, but I don't want to reload the calculated data
> every time the user needs it via the main program.

If it were me, I'd go with a database server like mysql.
** Posted from http://www.teranews.com **
--
http://mail.python.org/mailman/listinfo/python-list


Re: Ideas for master's thesis

2008-06-03 Thread Andrii V. Mishkovskyi
2008/6/4 Larry Bugbee <[EMAIL PROTECTED]>:
>> I would like to do something with this language, yet
>> I don't know if there are any needs/science fields, that could be used
>> as a basis for a thesis.
>
> Personally, I'd like to see *optional* data typing added to Python
> perhaps along the lines of what was done in Pyrex.  You declare the
> data type when you know it, or when it matters, and skip it
> otherwise.  Your paper could analyze its pros and cons, analyze any
> potential performance gains, and recommend how to implement it.  Your
> professor will suggest some additional questions.
>
> I suspect, if the type be known and declared, the interpreter could be
> streamlined and quicker, you might get asserts for free, and perhaps,
> Python becomes even more self-documenting.  Perhaps I've missed it,
> but I haven't seen a strong analytical case made for or against
> optional data typing.  Your paper?

I think what you are talking about is already implemented in Python
3.0 as annotations. Forgive me if I missed your point.

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



-- 
Wbr, Andrii Mishkovskyi.

He's got a heart of a little child, and he keeps it in a jar on his desk.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q about object identity

2008-06-03 Thread vronskij
On 3. Jún, 23:08 h., Christian Heimes <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] schrieb:
>
> > Hello,
>
> > I am testing object identity.
>
> > If I do it from the interpreter, I get strange results.
>
>  print [] is []
> > False
>
>  print id([]), id([])
> > 3083942700 3083942700
>
> > Why is that? Isn't this an error?
>
> No, it's not an error. You are getting this result because the list
> implementation keeps a bunch of unused list objects in a free list. It's
> an optimization trick. Python has to create two different list objects
> for "[] is []" while it can reuse the same list object for id([]) == id([]).
>
> Christian

Aha.

Thanks.

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


Re: How to increase the depth of the python traceback?

2008-06-03 Thread Peter Otten
Milton wrote:

> How to increase the depth of the python traceback?
> 
> I have some code that gets an exception deep in the python logging
> module and the traceback produced does not go back far enough to show
> my own code. How can the traceback limit be controlled in Python 2.5.
> 
> The docs indicate that there is an attribute in the sys module
> “tracebacklimit” that defaults to 1000 (affectively no limit). I
> cannot find this attribute in the sys module of Python2.5

Just set it yourself:

>>> import sys
>>> sys.tracebacklimit
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'tracebacklimit'
>>> sys.tracebacklimit = 3
>>> def bomb(n):
... if n: bomb(n-1)
... else: 1/0
...
>>> bomb(100)
Traceback (most recent call last):
  File "", line 2, in bomb
  File "", line 2, in bomb
  File "", line 3, in bomb
ZeroDivisionError: integer division or modulo by zero

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

Re: Ideas for master's thesis

2008-06-03 Thread Larry Bugbee
> I would like to do something with this language, yet
> I don't know if there are any needs/science fields, that could be used
> as a basis for a thesis.

Personally, I'd like to see *optional* data typing added to Python
perhaps along the lines of what was done in Pyrex.  You declare the
data type when you know it, or when it matters, and skip it
otherwise.  Your paper could analyze its pros and cons, analyze any
potential performance gains, and recommend how to implement it.  Your
professor will suggest some additional questions.

I suspect, if the type be known and declared, the interpreter could be
streamlined and quicker, you might get asserts for free, and perhaps,
Python becomes even more self-documenting.  Perhaps I've missed it,
but I haven't seen a strong analytical case made for or against
optional data typing.  Your paper?

Larry

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


How to increase the depth of the python traceback?

2008-06-03 Thread Milton
How to increase the depth of the python traceback?

I have some code that gets an exception deep in the python logging
module and the traceback produced does not go back far enough to show
my own code. How can the traceback limit be controlled in Python 2.5.

The docs indicate that there is an attribute in the sys module
“tracebacklimit” that defaults to 1000 (affectively no limit). I
cannot find this attribute in the sys module of Python2.5

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


Re: Webpy vs Django?

2008-06-03 Thread cirfu
wow now i have django running. now i see...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Books for programmers

2008-06-03 Thread vasudevram
On Jun 3, 6:42 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote:
> On Jun 3, 5:45 am, V <[EMAIL PROTECTED]> wrote:
>
> > Hi Matt,
>
> > and thank you very much for your answer.
>
> > > Hm, depends of course, how good your programming skills are in the
> > > languages you knwo already, but I rely on the book "Beginning Python -
> > > From Novice to Professional" by Magnus Lie Hetland, published by Apress.
>
> > I think that I'm interested in a more advance book, ideally one that
> > talk of the Python gotchas, traps, pitfall, idioms, performance,
> > stile, and so on. I really like the style used from Scott Meyers in
> > his Effective C++ series, or from Herb Sutter's Exceptional C++, but
> > after a quick look I did not find anything similar for Python...
>
> > Best regards.
>
> I agree with Rick. "Core Python Programming" by Chun is pretty good.
> However, Lutz's "Programming Python" is also very good and has a few
> big example programs to walk through. You might also find the Python
> Cookbooks handy.
>
> There's also "Python Power!" by Matt Telles, which is more of a
> reference book although not quite as dry as "Python Essential
> Reference" was.
>
> Mike


The Python Cookbook - printed version - I've read it - is a very good
book on the lines of what you're looking for, IMO. If you go by its
title, it might not sound like a book in the Effective Series (I've
read Effective C++ too and agree that its excellent), but it actually
is something quite like Effective C++, since its contributors include
many very good Python developers, including Alex Martelli, David
Ascher, Tim Peters, Raymond Hettinger, to name just a few. Though the
explicit goal of the book is not to be a book about idiomatic Python,
the point is that it ends up being a lot like that, since most of the
contributors write idiomatic Python. For example, one idiom that's
mentioned a lot in the book, is about one of Python's greatest
strengths - "smooth signature-based polymorphism" - with good examples
to substantiate it.

Though not a book, you may also find the Python articles by David
Mertz on IBM developerWorks very useful. Go to http://www.ibm.com/developerworks
and search for either "Charming Python" - the name of his Python
column there - or his name - to get the articles.

HTH
Vasudev
---
Vasudev Ram
Biz site: http://www.dancingbison.com
Quick PDF creation toolkit (in Python, open source):
http://www.dancingbison.com/products.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Q about object identity

2008-06-03 Thread Christian Heimes
[EMAIL PROTECTED] schrieb:
> Hello,
> 
> I am testing object identity.
> 
> If I do it from the interpreter, I get strange results.
> 
 print [] is []
> False
> 
 print id([]), id([])
> 3083942700 3083942700
> 
> 
> 
> Why is that? Isn't this an error?

No, it's not an error. You are getting this result because the list
implementation keeps a bunch of unused list objects in a free list. It's
an optimization trick. Python has to create two different list objects
for "[] is []" while it can reuse the same list object for id([]) == id([]).

Christian

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


Help need with subprocess communicate

2008-06-03 Thread rdabane
I'm trying to perform following type of operation from inside a python
script.
1. Open an application shell (basically a tcl )
2. Run some commands on that shell and get outputs from each command
3. Close the shell

I could do it using communicate if I concatenate all my commands
( separated by newline ) and read all the output in the end. So
basically I could do following sequence:
1. command1 \n command2 \n command 3 \n
2. Read all the output

But I want to perform it interactively.
1. command1
2. read output
3. command2
4. read output ..

Following is my code:

from subprocess import *
p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
o,e = p2.communicate(input='qdl_help \n qdl_read  \n
qdl_reg_group_list ')

Please suggest a way to perform it interactively with killing the
process each time I want to communicate with it.

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


help needed in subprocess communicate

2008-06-03 Thread rdabane
I'm trying to perform following type of operation from inside a python
script.
1. Open an application shell (basically a tcl )
2. Run some commands on that shell and get outputs from each command
3. Close the shell


I could do it using communicate if I concatenate all my commands
( separated by newline ) and read all the output in the end. So
basically I could do following sequence:
1. command1 \n command2 \n command 3 \n
2. Read all the output

But I want to perform it interactively.
1. command1
2. read output
3. command2
4. read output ..

Following is my code:

from subprocess import *
p2 = Popen('qdl_tcl',stdin=PIPE,stdout=PIPE)
o,e = p2.communicate(input='qdl_help \n qdl_read /home/rokit/
svnproject/falcon/chip/blocks/intf/bitsif/rtl/m6_bitsif.qdl_cpp \n
qdl_reg_group_list m6_bitsif')


Please suggest a way to perform it interactively with killing the
process each time I want to communicate with it.

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


Q about object identity

2008-06-03 Thread vronskij
Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.

>>> print [] is []
False

>>> print id([]), id([])
3083942700 3083942700



Why is that? Isn't this an error?


If I test it in a script, all is OK.

#!/usr/bin/python

a = []
b = []

print a == b
print a is b

print id(a), id(b)

print id(None), id(None)
print id(True), id(True)


On my computer, I got these results:

True
False
3083769036 3083793516
135553336 135553336
135526444 135526444

All as expected.


jan bodnar


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


Re: parser recommendation

2008-06-03 Thread Filipe Fernandes
> On Jun 3, 12:34 pm, "Filipe Fernandes" <[EMAIL PROTECTED]> wrote:
>> On Tue, Jun 3, 2008 at 10:41 AM, Paul McGuire <[EMAIL PROTECTED]> wrote:
>> But I do have more questions... when reading the ply.py header (in
>> 2.5) I found the following paragraph...
>>
>> # The current implementation is only somewhat object-oriented. The
>> # LR parser itself is defined in terms of an object (which allows multiple
>> # parsers to co-exist).  However, most of the variables used during table
>> # construction are defined in terms of global variables.  Users shouldn't
>> # notice unless they are trying to define multiple parsers at the same
>> # time using threads (in which case they should have their head examined).
>>
>> Now, I'm invariably going to have to use threads...  I'm not exactly
>> sure what the author is alluding to, but my guess is that to overcome
>> this limitation I need to acquire a thread lock first before
>> "defining/creating" a parser object before I can use it?
>>
>> Has anyone ran into this issue?  This would definitely be a
>> showstopper (for PLY anyway), if I couldn't create multiple parsers
>> because of threads.  I'm not saying I need more than one, I'm just not
>> comfortable with that limitation.
>>

On Tue, Jun 3, 2008 at 1:53 PM, Kay Schluehr <[EMAIL PROTECTED]> wrote:
> Nope. It just says that the parser-table construction itself relies on
> global state. But you will most likely build your parser offline  in a
> separate run.

Thanks Kay for the context.., I misunderstood completely, but your
last sentence coupled with a few running examples, cleared things
right up...

On Tue, Jun 3, 2008 at 4:36 PM, Paul McGuire <[EMAIL PROTECTED]> wrote:
> You can use pyparsing from any thread, and you can create multiple
> parsers each running in a separate thread, but you cannot concurrently
> use one parser from two different threads.  Some users work around
> this by instantiating a separate parser per thread using pickle to
> quickly construct the parser at thread start time.

I didn't know that pyparsing wasn't thread safe.  I kind of just
assumed because of it's OO approach.  Thanks for the work around.  I
haven't given up on pyparsing, although I'm now heavily leaning
towards PLY as an end solution since lex and yacc parsing is available
on other platforms as well.

Thanks Kay and Paul for the advice...  I'm still using the first two I
started looking at, but I'm much for confident in the choices made.

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


Re: parser recommendation

2008-06-03 Thread Paul McGuire
On Jun 3, 12:34 pm, "Filipe Fernandes" <[EMAIL PROTECTED]> wrote:
> On Tue, Jun 3, 2008 at 10:41 AM, Paul McGuire <[EMAIL PROTECTED]> wrote:
> But I do have more questions... when reading the ply.py header (in
> 2.5) I found the following paragraph...
>
> # The current implementation is only somewhat object-oriented. The
> # LR parser itself is defined in terms of an object (which allows multiple
> # parsers to co-exist).  However, most of the variables used during table
> # construction are defined in terms of global variables.  Users shouldn't
> # notice unless they are trying to define multiple parsers at the same
> # time using threads (in which case they should have their head examined).
>
> Now, I'm invariably going to have to use threads...  I'm not exactly
> sure what the author is alluding to, but my guess is that to overcome
> this limitation I need to acquire a thread lock first before
> "defining/creating" a parser object before I can use it?
>
> Has anyone ran into this issue?  This would definitely be a
> showstopper (for PLY anyway), if I couldn't create multiple parsers
> because of threads.  I'm not saying I need more than one, I'm just not
> comfortable with that limitation.
>
> I have a feeling I'm just misunderstanding since it doesn't seem to
> hold you back from creating multiple parsers under a single process.
>
> filipe

You can use pyparsing from any thread, and you can create multiple
parsers each running in a separate thread, but you cannot concurrently
use one parser from two different threads.  Some users work around
this by instantiating a separate parser per thread using pickle to
quickly construct the parser at thread start time.

-- Paul


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


Re: New variable?

2008-06-03 Thread Chris
On Jun 3, 9:34 pm, "Dan Upton" <[EMAIL PROTECTED]> wrote:
> On Tue, Jun 3, 2008 at 3:16 PM, tmallen <[EMAIL PROTECTED]> wrote:
> > On Jun 3, 3:03 pm, Chris <[EMAIL PROTECTED]> wrote:
> >> On Jun 3, 8:40 pm, tmallen <[EMAIL PROTECTED]> wrote:
>
> >> > What's the proper way to instantiate a new variable? x = ""?
>
> >> You don't need to pre-declare your variables.  Just assign them as you
> >> need them and they will take the correct type.
>
> > unless I'm using the += or a similar operator, right? That doesn't
> > seem to instantiate a variable.
>
> Right... because you don't have anything to increment or append to.  I
> guess this also comes up in the case of something like lists or
> dictionaries you want to uniformly create in a loop.  I guess you
> could call it instantiating, but really it's more like going ahead and
> assigning to them as Chris mentioned and you're just starting them
> with a default value.  Assuming you're working with strings, x=""
> should work just fine in that case.  Lists, x=[], dictionaries, x={},
> integers, probably x=1 or x=0...

You can always use the conversion functions if you want to be explicit
str(), int(), list(), dict(), set() etc
--
http://mail.python.org/mailman/listinfo/python-list


Re: Creating object in function doesn't seem to create a new object.

2008-06-03 Thread Matimus
> I thought that when I wrote fc1 = FlightCondition() in the function it
> would create a new FlightCondition object which would be passed back
> every time.
> Instead it seems to re-reference the old version and continue to add
> to it.

That is exactly what is happening. You have created a class with two
_class_ attributes that are lists. These attributes are attached to
the _class_ not the instance that was created. You are simply mutating
the list that is attached to the class.

You really want to do something like this:

class FlightCondition(object):
def __init__(self):
self.lsf = [0,'Low Speed Flare']
self.vto = [0,'Vertical Take-Off']

Other than that, its hard to tell what you are actually trying to do,
but likely get_flight_condition could become a method of the
FlightCondition class.

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


RE: New variable?

2008-06-03 Thread Reedick, Andrew
> -Original Message-
> From: [EMAIL PROTECTED] [mailto:python-
> [EMAIL PROTECTED] On Behalf Of tmallen
> Sent: Tuesday, June 03, 2008 2:41 PM
> To: python-list@python.org
> Subject: New variable?
> 
> What's the proper way to instantiate a new variable? x = ""?

I've always used
X = None
in those cases where I need to pre-declare a variable to set scope.


*

The information transmitted is intended only for the person or entity to which 
it is addressed and may contain confidential, proprietary, and/or privileged 
material. Any review, retransmission, dissemination or other use of, or taking 
of any action in reliance upon this information by persons or entities other 
than the intended recipient is prohibited. If you received this in error, 
please contact the sender and delete the material from all computers. GA623


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


Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-03 Thread Chris
On Jun 3, 10:11 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote:
> I used defaultdict.fromkeys to make a new defaultdict instance, but I
> was surprised by behavior:
>
>     >>> b = defaultdict.fromkeys(['x', 'y'], list)
>
>     >>> b
>     defaultdict(None, {'y': , 'x': })
>
>     >>> b['x']
>     
>
>     >>> b['z']
>     
>     Traceback (most recent call last):
>       File "", line 1, in 
>     KeyError: 'z'
>
> I think that what is really going on is that fromdict makes a regular
> dictionary, and then hands it off to the defaultdict class.
>
> I find this confusing, because now I have a defaultdict that raises a
> KeyError.
>
> Do other people find this intuitive?
>
> Would it be better if defaultdict.fromkeys raised a
> NotImplementedException?
>
> Or would it be better to redefine how defaultdict.fromkeys works, so
> that it first creates the defaultdict, and then goes through the keys?
>
> All comments welcome.  If I get some positive feedback, I'm going to try
> to submit a patch.
>
> Matt

To me it's intuitive for it to raise a KeyError, afterall the Key
isn't in the dictionary.
--
http://mail.python.org/mailman/listinfo/python-list


defaultdict.fromkeys returns a surprising defaultdict

2008-06-03 Thread Matthew Wilson
I used defaultdict.fromkeys to make a new defaultdict instance, but I
was surprised by behavior:

>>> b = defaultdict.fromkeys(['x', 'y'], list)
 
>>> b
defaultdict(None, {'y': , 'x': })
 
>>> b['x']

 
>>> b['z']

Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'z'

I think that what is really going on is that fromdict makes a regular
dictionary, and then hands it off to the defaultdict class.

I find this confusing, because now I have a defaultdict that raises a
KeyError.

Do other people find this intuitive?

Would it be better if defaultdict.fromkeys raised a
NotImplementedException?

Or would it be better to redefine how defaultdict.fromkeys works, so
that it first creates the defaultdict, and then goes through the keys?

All comments welcome.  If I get some positive feedback, I'm going to try
to submit a patch.

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


Re: Picking apart strings

2008-06-03 Thread jay graves
On Jun 3, 1:44 pm, tmallen <[EMAIL PROTECTED]> wrote:
> Is there a way to pick apart this text without resorting to regular
> expressions?
>
> p {
> color: black;
>
> }
>
> p -> element
> color -> property
> black -> value

http://code.google.com/p/cssutils/

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


Re: Merging ordered lists

2008-06-03 Thread etal
On Jun 3, 1:22 am, Peter Otten <[EMAIL PROTECTED]> wrote:
>
> Yes :)
>
> Seriously, you are using O(n) containers and O(n) lookup where mine uses
> O(1). For short lists it doesn't matter, but as the list length grows the
> difference gets huge:
>
> $ cat unique.py
> def unique(items):
>     u = set(items)
>     if len(u) == len(items):
>         return items
>     result = []
>     for item in items:
>         if item in u:
>             result.append(item)
>             u.remove(item)
>     return result
>

Yikes... and the list comprehension looked so innocent. I had resigned
myself to O(n) lookup, but you and Raymond appear to agree on the
basic concept for unique() -- check set membership, then generate the
final sequence and update the set based on that.
--
http://mail.python.org/mailman/listinfo/python-list


Different execution time in python code between embedded or standalone

2008-06-03 Thread Pau Freixes
Hi list,

First Hello to all, this is my and hope not end message to the list :P

This last months I have been writting a  program in c like to mod_python for
embedding python language, it's a middleware for dispatch and execute python
batch programs into several nodes. Now I'm writing some python program for
test how scale this into several nodes and comparing with "standalone"
performance.

I found a very strange problem with one application named md5challenge, this
aplication try to calculate the max number md5 digest in several seconds,
md5challenge use a simple signal alarm for stop program when time has
passed. This is the code of python script

def handler_alrm(signum, frame):
global _signal
global _nrdigest
global _f


_signal = True

def try_me():
global _nrdigest
global _f
global _signal

_f = open("/dev/urandom","r")
while _signal is not True:
buff = _f.read(_const_b)
md5.md5(buff).hexdigest()
_nrdigest = _nrdigest + 1

if _f is not None :
_f.close()

def main( req ):
global _nrdigest


signal.signal(signal.SIGALRM, handler_alrm)
signal.alarm(req.input['time'])


try_me()

req.output['count'] = _nrdigest

return req.OK


if __name__ == "__main__":

# test code
class test_req:
pass

req = test_req()
req.input = { 'time' : 10 }
req.output = { 'ret' : 0, 'count' : 0 }
req.OK = 1

main(req)

print "Reached %d digests" % req.output['count']


When I try to run this program in standalone into my Pentium Dual Core
md4challenge reached 1.000.000 milion keys in 10 seconds but when i try to
run this in embedded mode md5challenge reached about 200.000 more keys !!! I
repeat this test many times and  always  wins  embedded mode  !!!  What's
happen ?

Also I tested to erase read dependencies from /dev/random, and calculate all
keys from same buffer. In this case embedded mode win always also, and the
difference are more bigger !!!

Thks to all, can anybody help to me ?
-- 
Pau Freixes
Linux GNU/User
--
http://mail.python.org/mailman/listinfo/python-list

Re: New variable?

2008-06-03 Thread Dan Upton
On Tue, Jun 3, 2008 at 3:16 PM, tmallen <[EMAIL PROTECTED]> wrote:
> On Jun 3, 3:03 pm, Chris <[EMAIL PROTECTED]> wrote:
>> On Jun 3, 8:40 pm, tmallen <[EMAIL PROTECTED]> wrote:
>>
>> > What's the proper way to instantiate a new variable? x = ""?
>>
>> You don't need to pre-declare your variables.  Just assign them as you
>> need them and they will take the correct type.
>
> unless I'm using the += or a similar operator, right? That doesn't
> seem to instantiate a variable.

Right... because you don't have anything to increment or append to.  I
guess this also comes up in the case of something like lists or
dictionaries you want to uniformly create in a loop.  I guess you
could call it instantiating, but really it's more like going ahead and
assigning to them as Chris mentioned and you're just starting them
with a default value.  Assuming you're working with strings, x=""
should work just fine in that case.  Lists, x=[], dictionaries, x={},
integers, probably x=1 or x=0...
--
http://mail.python.org/mailman/listinfo/python-list


Creating object in function doesn't seem to create a new object.

2008-06-03 Thread Paul Childs
Hi folks,

I'll start off with the code I wrote...
(ActivePython 2.4 on Windows XP SP2)

---
class FlightCondition(object):

lsf = [0,'Low Speed Flare']
vto = [0,'Vertical Take-Off']

def get_flight_condition(flight_data):

fc1 = FlightCondition()

for row in flight_data:

fc1.lsf[0] += 1
fc1.vto[0] += 1


print 'in function get_flight_condition'
print fc1.lsf
print fc1.vto

return fc1

for count in range(3):

fc = get_flight_condition([1,2,3])

print 'returned fc'
print fc.lsf
print fc.vto
-

When I run it I get...

in function get_flight_condition
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
returned fc
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
in function get_flight_condition
[6, 'Low Speed Flare']
[6, 'Vertical Take-Off']
returned fc
[6, 'Low Speed Flare']
[6, 'Vertical Take-Off']
in function get_flight_condition
[9, 'Low Speed Flare']
[9, 'Vertical Take-Off']
returned fc
[9, 'Low Speed Flare']
[9, 'Vertical Take-Off']

-
I thought that when I wrote fc1 = FlightCondition() in the function it
would create a new FlightCondition object which would be passed back
every time.
Instead it seems to re-reference the old version and continue to add
to it.

-
What I expected was...

in function get_flight_condition
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
returned fc
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
in function get_flight_condition
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
returned fc
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
in function get_flight_condition
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']
returned fc
[3, 'Low Speed Flare']
[3, 'Vertical Take-Off']

-

Could someone please explain to me why I get the output I did instead
of what I expected.

How do I code my function so I get a new fc1 every time and my desired
output?

Thanks in advance.

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


Re: New variable?

2008-06-03 Thread tmallen
On Jun 3, 3:03 pm, Chris <[EMAIL PROTECTED]> wrote:
> On Jun 3, 8:40 pm, tmallen <[EMAIL PROTECTED]> wrote:
>
> > What's the proper way to instantiate a new variable? x = ""?
>
> You don't need to pre-declare your variables.  Just assign them as you
> need them and they will take the correct type.

unless I'm using the += or a similar operator, right? That doesn't
seem to instantiate a variable.
--
http://mail.python.org/mailman/listinfo/python-list


Re: New variable?

2008-06-03 Thread Chris
On Jun 3, 8:40 pm, tmallen <[EMAIL PROTECTED]> wrote:
> What's the proper way to instantiate a new variable? x = ""?

You don't need to pre-declare your variables.  Just assign them as you
need them and they will take the correct type.
--
http://mail.python.org/mailman/listinfo/python-list


Shed Skin (restricted) Python-to-C++ Compiler 0.0.28

2008-06-03 Thread srepmub
Hi all,

I have just released Shed Skin 0.0.28, with the following changes.
Thanks to those mentioned for helping out!

- basic 'socket' support (Michael Elkins)
- support for os.{popen3, popen4} under UNIX (Jaroslaw Tworek)
- support for time.strptime under Windows (David Marek)
- options for changing output dir, disabling annotation (Dave Tweed)
- support for 'cmp' and 'reverse' arguments of 'sorted' and
'list.sort'
- fixes for cross-module default arguments
- important fixes for type inference and inheritance
- restore compatibility with Python 2.3
- many minor bugfixes

I would really like to receive more bug reports. Please try out the
new version, and let me know about any problems.

With the socket support, 15 common modules are now largely supported.
For a 0.1 release, I'd really like to have support for one more
module: datetime. Thanks to the GHOP, there is a type model already
(lib/datetime.py), so we only still need a C++ implementation..

http://shedskin.googlecode.com

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


a python phpmyadmin like program

2008-06-03 Thread Gandalf
is their any graphic program for handling sqlite like phpmyadmin or
access in python?


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


Picking apart strings

2008-06-03 Thread tmallen
Is there a way to pick apart this text without resorting to regular
expressions?

p {
color: black;
}

p -> element
color -> property
black -> value
--
http://mail.python.org/mailman/listinfo/python-list


New variable?

2008-06-03 Thread tmallen
What's the proper way to instantiate a new variable? x = ""?
--
http://mail.python.org/mailman/listinfo/python-list


Handling some isolated iso-8859-1 characters

2008-06-03 Thread Daniel Mahoney
I'm working on an app that's processing Usenet messages. I'm making a
connection to my NNTP feed and grabbing the headers for the groups I'm
interested in, saving the info to disk, and doing some post-processing.
I'm finding a few bizarre characters and I'm not sure how to handle them
pythonically.

One of the lines I'm finding this problem with contains:
137050  Cleo and I have an anouncement!   "Mlle. =?iso-8859-1?Q?Ana=EFs?="
<[EMAIL PROTECTED]>  Sun, 21 Nov 2004 16:21:50 -0500
<[EMAIL PROTECTED]>  447869 Xref:
sn-us rec.pets.cats.community:137050

The interesting patch is the string that reads "=?iso-8859-1?Q?Ana=EFs?=".
An HTML rendering of what this string should look would be "Anaïs".

What I'm doing now is a brute-force substitution from the version in the
file to the HTML version. That's ugly. What's a better way to translate
that string? Or is my problem that I'm grabbing the headers from the NNTP
server incorrectly?



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


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Russ P.
On Jun 3, 11:02 am, Richard Levasseur <[EMAIL PROTECTED]> wrote:
> On Jun 3, 3:07 am, "BJörn Lindqvist" <[EMAIL PROTECTED]> wrote:
>
>
>
> > On Mon, Jun 2, 2008 at 10:50 PM, Russ P. <[EMAIL PROTECTED]> wrote:
> > > On Jun 2, 6:41 am, Carl Banks <[EMAIL PROTECTED]> wrote:
>
> > >> You are not realizing that only useful(**) thing about data hiding is
> > >> that some code has access to the data, other code does not.  If you
> > >> "hide" data equally from everyone it's just a useless spelling change.
>
> > > I think you're missing the point.
>
> > > As I see it, the primary value of data hiding is that it provides
> > > useful information on which data and methods are intended for the
> > > client and which are intended for internal use. It's like putting a
> > > front panel on a TV set with the main controls intended for the
> > > viewer.
>
> > Here's my two cents. First of all, a TV is a bad analogy compared to
> > reusable software libraries. Really bad analogy. A TV is a horribly
> > complicated device which has to be dumbed down because otherwise it
> > would be to hard to use for ordinary people.
>
> > A software developers relation to a third party library is more
> > similar to a TV repair man trying to repair a TV than to a random
> > person watching TV. For a repair man, the front panel is just useless
> > and in the way.
>
> > Oh, and to continue on the TV analogy, one of the reason why a TV is
> > complicated is because its interface is totally different from its
> > implementation. Channels are just a bad abstraction for tuning the
> > receiver to different frequencies and for switching inputs. Merely
> > using a TV doesn't teach you anything about how it actually works.
>
> > KISS: Keep It Simple Stupid. And it is always simpler to not implement
> > the gunk needed for data hiding than to do it. By keeping things
> > simple you keep your code easy to implement, easy to understand and
> > easy to reuse.
>
> > Data hiding sacrifices implementation simplicity supposedly to make
> > the interface simpler and to keep backwards compatibility. It allows
> > you to change implementation details without affecting the
> > interface. But do you really want to do that? Consider this silly Java
> > example:
>
> > class Foo {
> > private int bar;
> > public int getBar() {
> > return bar;
> > }
> > };
>
> > Then for some reason you decide that hm, "bar" is not a good attribute
> > name so you change it to "babar". And you can do that without changing
> > the public interface! Woho! So now you have a public getter named
> > "getBar" that returns an attribute named "babar". That's in reality
> > just bad and whoever is maintaining the implementation is going to be
> > annoyed that the getters name doesn't match the attribute name.
>
> > What would have happened without data hiding? Renaming the public
> > attribute "bar" to "babar" probably cause some grief for someone
> > reusing your library, but you would keep your implementation pure.
>
> > What about semantic changes? Data hiding doesn't protect you against
> > that, so you'll have to change your interface anyway. The interface
> > for a car hasn't changed much in the last 100 years, but the
> > implementation has. How easy is it to repair a car nowadays compared
> > to 30 years ago?
>
> > And data hiding as a documentation aid is just a sham. "These methods
> > are public so you can call them, these aren't so hands off!" A reuser
> > of your library *will* want to know what happens on the inside, by
> > trying to make stuff impossible to reach you are just making that kind
> > of information much harder to come by.
>
> > The better method is to just write proper docstrings that tell the
> > user what the methods do and when they can be called.
>
> > Another good way to see how useless data hiding is, is to try and unit
> > test a very encapsulated library. You'll see that it is almost
> > impossible to write good unit tests unless you publicly export
> > almost everything in the code. At which point you come to realize that
> > all the data hiding was for naught.
>
> > --
> > mvh Björn
>
> I really like this message and find it very true.  Writing unit tests
> for private data is nigh impossible.  You end up either creating
> accessors, or passing in parameters via the constructor (resulting in
> a huge constructor).  Personally, I'd rather have better test coverage
> than data hiding.
>
> Second, private vars with third party libs suck, and are nothing but
> an infuriating frustration.  I'm currently dealing with about 3 or 4
> different libs, one of them uses private variables and its a huge
> headache.  I have to access some of those private vars occasionally to
> make my thing work.  The other libs i'm using don't have any private
> vars (__) (only a couple protected ones, _), and its a breeze.  The
> docs say "this does x" or there's a comment that says "don't use this
> unless you really know what you're d

Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Russ P.
On Jun 3, 4:21 am, George Sakkis <[EMAIL PROTECTED]> wrote:
> On Jun 3, 1:42 am, "Russ P." <[EMAIL PROTECTED]> wrote:
>
> > On Jun 2, 10:23 pm, alex23 <[EMAIL PROTECTED]> wrote:
>
> > > Then again, I have no issue with the current convention and personally
> > > find the idea of adding a "private" keyword makes as much sense as
> > > being able to syntactically define "model", "view" and "controller"
> > > methods.
>
> > Well, the designers of C++, Java, and Ada, to name just three very
> > popular languages (well, two) seem to think it makes sense. But maybe
> > you know more than they know.
>
> And even more (well, almost all) languages use explicit delimiters for
> defining blocks instead of indentation, so what's your point ?

You are comparing a syntactic convention with a more fundmaental
aspect of the language. But beyond that, I dislike braces as
delimiters for the same reason I dislike leading underscores: both are
unnecessary syntactic noise. And the whole idea of encoding properties
of an object in its name just seems tacky to me.

What is it about leading underscores that bothers me? To me, they are
like a small pebble in your shoe while you are on a hike. Yes, you can
live with it, and it does no harm, but you still want to get rid of it.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread sturlamolden
On Jun 2, 12:40 pm, Antoon Pardon <[EMAIL PROTECTED]> wrote:

> I think you completed missed the point.
>
> This is just a proof of concept thing. In a real example there would
> of course no Set en Get methods but just methods that in the course
> of their execution would access or update the hidden attributes

I have to agree with Banks here, you have not provided an example of
data hiding. It does not discriminate between attribute access from
within and from outside the class. You just assume that the attribute
named 'hidden' will be left alone. Also naming it hidden is stupid as
it is visible.

What you need is a mechanism that will thrown an exception whenever an
attribue is accessed from outside the class, but not from inside.

The mechanism must also be impossible to override with additional
code.

If Ada is what you want, Ada is what you should use.







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


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Richard Levasseur
On Jun 3, 3:07 am, "BJörn Lindqvist" <[EMAIL PROTECTED]> wrote:
> On Mon, Jun 2, 2008 at 10:50 PM, Russ P. <[EMAIL PROTECTED]> wrote:
> > On Jun 2, 6:41 am, Carl Banks <[EMAIL PROTECTED]> wrote:
>
> >> You are not realizing that only useful(**) thing about data hiding is
> >> that some code has access to the data, other code does not.  If you
> >> "hide" data equally from everyone it's just a useless spelling change.
>
> > I think you're missing the point.
>
> > As I see it, the primary value of data hiding is that it provides
> > useful information on which data and methods are intended for the
> > client and which are intended for internal use. It's like putting a
> > front panel on a TV set with the main controls intended for the
> > viewer.
>
> Here's my two cents. First of all, a TV is a bad analogy compared to
> reusable software libraries. Really bad analogy. A TV is a horribly
> complicated device which has to be dumbed down because otherwise it
> would be to hard to use for ordinary people.
>
> A software developers relation to a third party library is more
> similar to a TV repair man trying to repair a TV than to a random
> person watching TV. For a repair man, the front panel is just useless
> and in the way.
>
> Oh, and to continue on the TV analogy, one of the reason why a TV is
> complicated is because its interface is totally different from its
> implementation. Channels are just a bad abstraction for tuning the
> receiver to different frequencies and for switching inputs. Merely
> using a TV doesn't teach you anything about how it actually works.
>
> KISS: Keep It Simple Stupid. And it is always simpler to not implement
> the gunk needed for data hiding than to do it. By keeping things
> simple you keep your code easy to implement, easy to understand and
> easy to reuse.
>
> Data hiding sacrifices implementation simplicity supposedly to make
> the interface simpler and to keep backwards compatibility. It allows
> you to change implementation details without affecting the
> interface. But do you really want to do that? Consider this silly Java
> example:
>
> class Foo {
> private int bar;
> public int getBar() {
> return bar;
> }
> };
>
> Then for some reason you decide that hm, "bar" is not a good attribute
> name so you change it to "babar". And you can do that without changing
> the public interface! Woho! So now you have a public getter named
> "getBar" that returns an attribute named "babar". That's in reality
> just bad and whoever is maintaining the implementation is going to be
> annoyed that the getters name doesn't match the attribute name.
>
> What would have happened without data hiding? Renaming the public
> attribute "bar" to "babar" probably cause some grief for someone
> reusing your library, but you would keep your implementation pure.
>
> What about semantic changes? Data hiding doesn't protect you against
> that, so you'll have to change your interface anyway. The interface
> for a car hasn't changed much in the last 100 years, but the
> implementation has. How easy is it to repair a car nowadays compared
> to 30 years ago?
>
> And data hiding as a documentation aid is just a sham. "These methods
> are public so you can call them, these aren't so hands off!" A reuser
> of your library *will* want to know what happens on the inside, by
> trying to make stuff impossible to reach you are just making that kind
> of information much harder to come by.
>
> The better method is to just write proper docstrings that tell the
> user what the methods do and when they can be called.
>
> Another good way to see how useless data hiding is, is to try and unit
> test a very encapsulated library. You'll see that it is almost
> impossible to write good unit tests unless you publicly export
> almost everything in the code. At which point you come to realize that
> all the data hiding was for naught.
>
> --
> mvh Björn

I really like this message and find it very true.  Writing unit tests
for private data is nigh impossible.  You end up either creating
accessors, or passing in parameters via the constructor (resulting in
a huge constructor).  Personally, I'd rather have better test coverage
than data hiding.

Second, private vars with third party libs suck, and are nothing but
an infuriating frustration.  I'm currently dealing with about 3 or 4
different libs, one of them uses private variables and its a huge
headache.  I have to access some of those private vars occasionally to
make my thing work.  The other libs i'm using don't have any private
vars (__) (only a couple protected ones, _), and its a breeze.  The
docs say "this does x" or there's a comment that says "don't use this
unless you really know what you're doing," and I respect their
warnings.

When I was fooling around with sqlalchemy, it made heavy use of
protected vars but had a straight forward public api.  Unfortunately,
writing plugins for it required access to some of those protect

Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread sturlamolden
On May 24, 3:41 pm, Sh4wn <[EMAIL PROTECTED]> wrote:

> first, python is one of my fav languages, and i'll definitely keep
> developing with it. But, there's 1 one thing what I -really- miss:
> data hiding. I know member vars are private when you prefix them with
> 2 underscores, but I hate prefixing my vars, I'd rather add a keyword
> before it.

Python has no data hiding because C++ has (void *).

Python underscores does some name mangling, but does not attempt any
data hiding.

Python and C has about the same approach to data hiding. It is well
tried, and works equally well in both languages:

   # this is mine, keep your filthy paws off!!!

Irresponsible programmers should not be allowed near a computer
anyway. If you use data hiding to protect your code from yourself,
what you really need is some time off to reconsider your career.



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


Re: parser recommendation

2008-06-03 Thread Kay Schluehr
On 3 Jun., 19:34, "Filipe Fernandes" <[EMAIL PROTECTED]> wrote:

> # The current implementation is only somewhat object-oriented. The
> # LR parser itself is defined in terms of an object (which allows multiple
> # parsers to co-exist).  However, most of the variables used during table
> # construction are defined in terms of global variables.  Users shouldn't
> # notice unless they are trying to define multiple parsers at the same
> # time using threads (in which case they should have their head examined).
>
> Now, I'm invariably going to have to use threads...  I'm not exactly
> sure what the author is alluding to, but my guess is that to overcome
> this limitation I need to acquire a thread lock first before
> "defining/creating" a parser object before I can use it?

Nope. It just says that the parser-table construction itself relies on
global state. But you will most likely build your parser offline  in a
separate run.

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


Re: need help with timezone conversion (unexpected side effect of time.mktime ??)

2008-06-03 Thread Ivan Velev
> I've tried this with Python 2.3 and 2.4 on Red Hat Enterprise Linux 4
> and can't reproduce the problem, even with other TZ values such as

Thanks for the quick reply.

Can you please let me know what value do you receive during your
tests ?


As far as I can see, Python timezone API is just a wrapper around
(shared ) native C libraries, but I really do not have any idea on how
to reproduce this issue in a non-Python environment :(

btw. problem exists for this particular datetime only, an hour or more
before / after that time givse identical results (this particular
datetime is near the "daylight saving time change" moment)


On Jun 3, 6:01 pm, Paul Boddie <[EMAIL PROTECTED]> wrote:
> On 3 Jun, 16:12, Ivan Velev <[EMAIL PROTECTED]> wrote:
>
>
>
> > Minimal example below - it gives me different output if I comment /
> > uncomment the extra time.mktime call - note that this call is not
> > related in any way to main logic flow.
>
> > When "problematicStamp = ..." is commented I get
> > gmtStamp: 1130634600.0
>
> > when I uncomment that line I get
> > gmtStamp: 1130631000.0
>
> I've tried this with Python 2.3 and 2.4 on Red Hat Enterprise Linux 4
> and can't reproduce the problem, even with other TZ values such as
> "EEST3" (which appears to be a legal name and does change the
> timestamp produced). I don't think much has changed in the time module
> since 2.4, which means that there might be some kind of library or
> configuration issue involved which causes the observed behaviour.
>
> Paul

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


Re: Merging ordered lists

2008-06-03 Thread etal
On Jun 2, 11:08 pm, Raymond Hettinger <[EMAIL PROTECTED]> wrote:
>
> If the inputs were not sorted, then I don't think you have a precise
> idea of what it means to merge them while preserving order.   For
> example if the inputs are XYZPDQ and bYlmPz, then what does a merged
> sequence look like once the Y and P duplicates are removed? Is it
> XZPDQblmz or some intermix of the two like XbZlPmDzQ?  If it is the
> first of those, then the answer is simple:
>

I was looking at Bram Cohen's description of his diff algorithm,
implemented in Bazaar:
http://bramcohen.livejournal.com/37690.html
"Instead of doing a longest common subsequence on everything, it does
a longest common subsequence on lines which occur exactly once on both
sides, then recurses between lines which got matched on that pass."

So, if sources looks like:
[list("XYZPDQ"), list("bYlmPz")]

Then the proper merge would use Y and P as the delimiters, putting X
and b before Y; Z, l and m before P; and D, Q and z after P -- like
your second example (but keeping an instance of Y). Right? Leaving us
with the context-dependent issue of ordering between the delimiters,
probably depending on which list is used as the reference initially.
So, if the reference list's elements go first, the result would be
XbYZlmPDQz. If the elements from later sources go after the last
common element (my first approach), it's bXYlmZPzDQ.

However, in the cold light of morning it looks like that was the wrong
approach -- it makes sense to treat any data that doesn't occur in the
reference list as semi-obsolete and append it instead -- so I think
I'll use your much simpler algorithm. Thanks!
--
http://mail.python.org/mailman/listinfo/python-list


Re: parser recommendation

2008-06-03 Thread Filipe Fernandes
On Tue, Jun 3, 2008 at 10:41 AM, Paul McGuire <[EMAIL PROTECTED]> wrote:
> If you learn both, you may find that pyparsing is a good way to
> quickly prototype a particular parsing problem, which you can then
> convert to PLY for performance if necessary.  The pyparsing prototype
> will be an efficient way to work out what the grammar "kinks" are, so
> that when you get around to PLY-ifying it, you already have a clear
> picture of what the parser needs to do.
>

Thanks (both Paul and Kay) for responding.  I'm still looking at Trail
in EasyExtend and pyparsing is very nicely objected oriented but PLY
does seems to have the speed advantage, so I'm leaning towards PLY

But I do have more questions... when reading the ply.py header (in
2.5) I found the following paragraph...

# The current implementation is only somewhat object-oriented. The
# LR parser itself is defined in terms of an object (which allows multiple
# parsers to co-exist).  However, most of the variables used during table
# construction are defined in terms of global variables.  Users shouldn't
# notice unless they are trying to define multiple parsers at the same
# time using threads (in which case they should have their head examined).

Now, I'm invariably going to have to use threads...  I'm not exactly
sure what the author is alluding to, but my guess is that to overcome
this limitation I need to acquire a thread lock first before
"defining/creating" a parser object before I can use it?

Has anyone ran into this issue?  This would definitely be a
showstopper (for PLY anyway), if I couldn't create multiple parsers
because of threads.  I'm not saying I need more than one, I'm just not
comfortable with that limitation.

I have a feeling I'm just misunderstanding since it doesn't seem to
hold you back from creating multiple parsers under a single process.

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


Data Structures - Getting started..

2008-06-03 Thread rh0dium
Hi all,

I have a primitive data structure which looks like this.

cells = [{'name': 'AND2X1',
  'pins': [{'direction': 'input', 'name': 'A', 'type':
'signal'},
   {'direction': 'input', 'name': 'B', 'type':
'signal'},
   {'direction': 'output', 'name': 'Y', 'type':
'signal'},
   {'direction': 'inout', 'name': 'VDD', 'type':
'power'},
   {'direction': 'inout', 'name': 'VSS', 'type':
'ground'}],
  'type': None},
 {'name': 'AND2X2',
  'pins': [{'direction': 'input', 'name': 'A', 'type':
'signal'},
   {'direction': 'input', 'name': 'B', 'type':
'signal'},
   {'direction': 'output', 'name': 'Y', 'type':
'signal'},
   {'direction': 'inout', 'name': 'VDD', 'type':
'power'},
   {'direction': 'inout', 'name': 'VSS', 'type':
'ground'}],
  'type': None},]

I want to make me a 'more better'  data structure where I could do the
following:

lef = IP()  # My new fangled data structure
lef.add("AND2X1")
lef.AND2X.type("stdcell")
lef.AND2X.pin.add("A")
lef.AND2X.pin.A.direction=("Input")
lef.AND2X.pin.add("B")
lef.AND2X.pin.A.direction=("Input")
lef.AND2X.pin.add("Y")
lef.AND2X.pin.A.direction=("Output")
lef.AND2X.pin.A.type=("signal")

lef.add("AND3X1")
lef.AND2X.type("stdcell")
lef.AND2X.pin.add("A")
lef.AND2X.pin.A.direction=("Input")
lef.AND2X.pin.add("B")
lef.AND2X.pin.A.direction=("Input")
lef.AND2X.pin.add("Y")
lef.AND2X.pin.A.direction=("Output")
lef.AND2X.pin.A.type=("signal")

print lef.asList()  # Would return ["AND2X1",
"AND3X1"]
print lef.AND2X1.pins.asList()  # Would return ["A", "B", "Y"]

for x in lef:
print x.name# Would return AND2X1
print x.type# Would return stdcell (if used)
print x.pins# Would return pins (if used)

Can anyone get me started?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Keep a script running in the background

2008-06-03 Thread subeen
On Jun 3, 10:07 pm, Guillermo <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I need a script to keep running in the background after it's loaded
> some data. It will make this data available to the main program in the
> form of a dictionary, but I don't want to reload the calculated data
> every time the user needs it via the main program.
>
> I won't be working with an UI, hope that can be made easily in Python
> somehow.
>
> Cheers,
>
> Guillermo

You can try this command: nohup python script.py &

regards,
Subeen.
http://love-python.blogspot.com/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Webpy vs Django?

2008-06-03 Thread [EMAIL PROTECTED]
> i played with webpy a bit and it is easy to get going with. but django
> seems like once you have it all up and running it will be easier.
> just that the barrier of entry is much higher.

I can't comment on webpy, but yes, Django has a bit more of a learning
curve in some areas, less in others.

> is django worth it? seems so ridicoulusly hard to get it running. i
> run into trouble every time i advance a little, firstin the
> installationa nd now in the tutorial(creating polls).

For a big project, I'd say it's very worth it. The code tends to read
easy, the people are friendly, the documentation is good for an open
source project, and it's on an upward adoption trend.

> what do you think of webpy for big projects that need performance?

No clue. Django seems to be tried-and-true in terms of scalability and
performance, I haven't had to look further.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Keep a script running in the background

2008-06-03 Thread Bjoern Schliessmann
Guillermo wrote:

> I need a script to keep running in the background after it's
> loaded some data. It will make this data available to the main
> program in the form of a dictionary, but I don't want to reload
> the calculated data every time the user needs it via the main
> program.
> 
> I won't be working with an UI, hope that can be made easily in
> Python somehow.

Sure. Try the subprocess module. 

Regards,


Björn

-- 
BOFH excuse #205:

Quantum dynamics are affecting the transistors

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


Re: Keep a script running in the background

2008-06-03 Thread Daniel Fetchinson
> I need a script to keep running in the background after it's loaded
> some data. It will make this data available to the main program in the
> form of a dictionary, but I don't want to reload the calculated data
> every time the user needs it via the main program.
>
> I won't be working with an UI, hope that can be made easily in Python
> somehow.

I'm not sure I understand exactly what you want but you might find
these daemonize examples useful from the cookbook:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown
--
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes, function pointers and a lot of trouble

2008-06-03 Thread Matt



The docs say CFUNCTYPE(restype, *argtypes), so:

cstreamopen = CFUNCTYPE(c_uint, c_ushort, c_uint)

is saying that the result type is c_uint, not void. I think you need:

cstreamopen = CFUNCTYPE(None, c_uint, c_ushort, c_uint)

instead.


Hm, thanks, now I can access my data in the functions and also write 
them but the program keeps terminating right at the point when the 
"open" function finishes. Unfortunately everything closes and I get no 
error messages.


I did some additional work in the meantime and changed my code so it has 
the correct datatypes now:


--CODE

def pystreamopen (contextH, mode, pErr):
print "opening..."
print contextH.contents.dwBufferSize #just to check the structure
print mode  #tells about what the DLL wants to do with this stream
contextH.contents.mode = c_byte(5) #5=Permission to read and write
contextH.contents.lPos = c_uint(0) #start position

print pErr.contents
pErr.contents = c_uint(0)


cstreamopen = CFUNCTYPE(None, POINTER(MemStreamData), c_ushort, 
POINTER(c_uint))


def pystreamclose (contextH, pErr):
print "closing..."
pass

cstreamclose = CFUNCTYPE(None, POINTER(MemStreamData), POINTER(c_uint))

def pystreamread (contextH, pBuf, pBufsize, pErr):
print "reading..."
pass

cstreamread = CFUNCTYPE(None, POINTER(MemStreamData), c_uint, c_uint, 
POINTER(c_uint))


def pystreamtell (contextH, pErr):
print "telling...

.
.
.etc...

--/CODE

and the function call:

--CODE

errorcode = cdsdk.CDGetReleasedData(devicehandle, byref(cbfunct), 
c_uint(0), c_uint(0), byref(datainfo), POINTER(cdStgMedium)(data))


--/CODE


while it's running my program prints the following and then 
exits/terminates:


>>> opening...990001
>>> 2
>>> c_ulong(0L)
>>> Script terminated.  ##This is a message by my editor (SPE)

I also tried different editors, but get the same result...



Anyway, meanwhile  decided to try a different approach. Maybe I have 
more luck by having the function write the data directly into a file on 
the HDD.

Doe anyone know how to translate the following into Python/ctypes?

I googled quite a lot before but all topic-related I found was my own 
posting here in this NG :S


--CODE

pFilStrm->hFile = CreateFile(	pFilStrm->szFileName,			 
dwDesiredAccess,dwShareMode,	NULL,	 
dwCreationDisposition,FILE_ATTRIBUTE_NORMAL,NULL );



--/CODE

This function is obviously a default C-function that generates a file 
and returns a c-handle to that file. In my case I'd need exactly such a 
handle to get things working...


Furthermore there's a writefile function I'd need to translate too:

--CODE

WriteFile( pFilStrm->hFile, pBuf, dwNumberOfBytesToWrite, pBufsize, NULL );

--/CODE


Ideas?

Thanks and best wishes,
Matt
--
http://mail.python.org/mailman/listinfo/python-list


Re: Importing xlrd

2008-06-03 Thread Chanman
On Jun 2, 5:48 pm, John Machin <[EMAIL PROTECTED]> wrote:
> On Jun 3, 8:23 am, Chanman <[EMAIL PROTECTED]> wrote:
>
> > This is probably a simple question to most of you, but here goes.
> > I've downloaded the xlrd (version 0.6.1) module and placed in in the
> > site-packages folder.  Now, when I write a script, I type:
>
> > import sys
> > import xlrd
>
> > When I run it, there is an import error saying there is no module
> > named xlrd.  However when I type sys.path, the site-packages folder is
> > definitely in the path.  Do I somehow need to run the xlrd setup.py
> > first before importing?
>
> > Any help would be appreciated.
>
> From the xlrd home-page (http://www.lexicon.net/sjmachin/xlrd.htm):
> """
> Installation:
>
> * Windows only: download and run this installer
> xlrd-0.6.1.win32.exe. Any platform: download this ZIP file
> xlrd-0.6.1.zip which you unzip into a suitable directory, then cd to
> that directory, and do "python setup.py install".
> """
>
> From the xlrd README file:
> """
> Installation:
>
> * On Windows: use the installer.
> * Any OS: Unzip the .zip file into a suitable directory, chdir to
> that directory, then do "python setup.py install".
> * If PYDIR is your Python installation directory: the main files
> are in PYDIR/Lib/site-packages/xlrd (except for Python 2.1 where they
> will be in PYDIR/xlrd), the docs are in the doc subdirectory, and
> there's a sample script: PYDIR/Scripts/runxlrd.py
> * If os.sep != "/": make the appropriate adjustments.
> """

Thanks, turns out I just had to type "setup.py install" in the command
line, no "python" required.

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


Re: trinity school defender

2008-06-03 Thread Paul McGuire
On Jun 3, 10:26 am, Dino Dragovic <[EMAIL PROTECTED]> wrote:
> u gorenavedenom flajeru u 8. redu:
>
> "postoji više od 60.000 virusa i drugih štetnih programa "
>
> samo virusa ima nekoliko stotina tisuca, zajedno sa potencijalno stetim
> aplikacijama i ostalim malicioznim kodom brojka ide preko milion

castironpi, call your office...
--
http://mail.python.org/mailman/listinfo/python-list


Re: Easy install / setuptools

2008-06-03 Thread John Nagle

Gabriel Genellina wrote:
En Thu, 29 May 2008 06:29:00 -0300, <[EMAIL PROTECTED]> 
escribió:


I'm trying to figure out the "best" way to distribute my own python 
packages. 


Well... don't use an egg in the first place :)


   "easy install" usually isn't.  It tends to do the wrong thing,
then leave things in a confused state.  Too many things have to
be set up right before "easy install" works easily.

   "python setup.py" is the de-facto standard.

   Yes, there's no really good, standard solution for Python
installation.

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


Keep a script running in the background

2008-06-03 Thread Guillermo

Hi,

I need a script to keep running in the background after it's loaded
some data. It will make this data available to the main program in the
form of a dictionary, but I don't want to reload the calculated data
every time the user needs it via the main program.

I won't be working with an UI, hope that can be made easily in Python
somehow.

Cheers,

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


Re: Continuous Timer

2008-06-03 Thread John Nagle

Gabriel Genellina wrote:
En Fri, 30 May 2008 22:50:13 -0300, Robert Dailey <[EMAIL PROTECTED]> 
escribió:



Reading through the Python 2.5 docs, I'm seeing a Timer class in the
threading module, however I cannot find a timer object that will
continuously call a function of my choice every  amount of 
milliseconds.

For example, every 1000 milliseconds I want a function named Foo to be
called. This would continue to happen until I terminate the timer in 
my main

thread. Thanks for the help.


Use an Event object; its wait() will provide the sleep time, and when it 
is set() the thread knows it has to exit.


import threading
import time

def repeat(event, every, action):
while True:
event.wait(every)
if event.isSet():
break
action()


Actually, to do this right, it's necessary to account for the time used by
"action".  The code above will run no sooner than the time "every" after
the COMPLETION of action.

I've done this sort of thing under QNX, the real-time operating system,
which has better timing primitives, and seen the action executed within
a few microseconds of the correct time, every time.  But that was in C++.

If you're trying to do hard real time in Python on Linux or Windows,
don't expect reliable timing.  Remember, Python isn't really preemptive,
because of the global interpreter lock and the lack of thread priorities.

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


Re: ThreadPoolingMixIn

2008-06-03 Thread pavel . uvarov
On Jun 3, 1:19 am, [EMAIL PROTECTED] wrote:
> On Jun 2, 12:41 pm, [EMAIL PROTECTED] wrote:
>
>
>
> > On Jun 2, 7:15 pm, Michael Ströder <[EMAIL PROTECTED]> wrote:
>
> > Here are benchmarks for FreeBSD 6.2, amd64
>
> > packet_size x y
> >   0499.57   1114.54
> >1024499.29   1130.02
> >3072500.09   1119.14
> >7168498.20   .76
> >   15360499.29   1086.73
> >   31744500.04   1036.46
> >   64512499.43939.60
> >  130048499.28737.44
> >  261120498.04499.03
> >  523264307.54312.04
> > 1047552173.57185.32
> > 2096128 93.61 94.39
>
> > x = ThreadingMixIn replies/s
> > y = ThreadPoolingMixIn replies/s
>
> Well, I'd say you've got yourself a winner.  Performance (at least on
> FreeBSD) seems as good or better for your ThreadPoolingMixin than
> ThreadingMixin.  Is this with the default values of min=5 and max=5
> worker threads?

No, I initialized thread pool with min_threads=2, max_threads=200 and
min_spare_threads=20.

For Linux (2.6.22, amd64) I got even more dramatic improvement:

packet_sizex y
  0   249.97   2014.63
   1024   249.98   1782.83
   3072   240.09   1859.00
   7168   249.98   1900.61
  15360   249.98   1787.30
  31744   238.96   1808.17
  64512   249.85   1561.47
 130048   237.26   1213.26
 261120   249.98841.96
 523264   249.97595.40
1047552   236.40351.96
2096128   216.26218.15

x = ThreadingMixIn replies/s
y = ThreadPoolingMixIn replies/s
--
http://mail.python.org/mailman/listinfo/python-list


trinity school defender

2008-06-03 Thread Dino Dragovic

u gorenavedenom flajeru u 8. redu:

"postoji više od 60.000 virusa i drugih štetnih programa "

samo virusa ima nekoliko stotina tisuca, zajedno sa potencijalno stetim 
aplikacijama i ostalim malicioznim kodom brojka ide preko milion

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


Re: Why does python not have a mechanism for data hiding?

2008-06-03 Thread Bruno Desthuilliers

Russ P. a écrit :

On Jun 2, 6:41 am, Carl Banks <[EMAIL PROTECTED]> wrote:


You are not realizing that only useful(**) thing about data hiding is
that some code has access to the data, other code does not.  If you
"hide" data equally from everyone it's just a useless spelling change.


I think you're missing the point.

As I see it, the primary value of data hiding is that it provides
useful information on which data and methods are intended for the
client and which are intended for internal use. It's like putting a
front panel on a TV set with the main controls intended for the
viewer.

People seem to be preoccupied with whether or not the back panel of
the TV is locked, but that is not the main issue. Sure, you probably
want to make the back panel removable, but you don't want the viewer
opening it up to change the channel, and you certainly don't want to
put all the internal adjustments for factory technicians together with
the controls for the end user.

As far as I am concerned, the current Python method of using
underscores to distinguish between internal and external methods and
data is an ugly hack that goes completely against the elegance of the
language in other areas. 


As far as I'm concerned, it's JustFine(tm). I don't have to ask myself 
if an attribute is part of the API or not, I know it immediatly.



It is like a TV set with no back cover and
the volume and channel controls intermingled with the factory
controls. The underscores are just an afterthought like a red dot or
something used to tell the TV viewer  what to fiddle with.


Your opinion. But beware of leaky TV-Set-metaphor abstractions


Python is a very nice language overall, but as far as I am concerned
the underscore convention is a blemish. I just wish people wouldn't
get so infatuated with the language that they cannot see the obvious
staring them in the face.


I definitively don't have problem with this naming convention, which I'd 
find useful ever with a language having enforced access restrictions. If 
that's the only - or worse - wart you find in Python, then it must 
surely be a pretty good language !-)

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


  1   2   >