unittest, order of test execution

2009-01-26 Thread mk

Hello everyone,

I've got 2 functions to test, extrfromfile which returns a list of 
dictionaries, and extrvalues that extracts values from that list.


Now I can test them both in one test case, like this:

def test_extrfromfile(self):
valist = ma.extrfromfile('loadavg_unittest.txt')
valist_ut = [ {'day': '08-11-19', 'time': '12:41', 'val': 0.11},
  {'day': '08-11-19', 'time': '12:42', 'val': 0.08},
  {'day': '08-11-19', 'time': '12:43', 'val': 0.57},
  {'day': '08-11-19', 'time': '12:44', 'val': 0.21},
  {'day': '08-11-19', 'time': '12:45', 'val': 0.08},
  {'day': '08-11-19', 'time': '12:46', 'val': 0.66},
  {'day': '08-11-19', 'time': '12:47', 'val': 0.32},
  {'day': '08-11-19', 'time': '12:48', 'val': 0.12},
  {'day': '08-11-19', 'time': '12:49', 'val': 0.47},
  {'day': '08-11-19', 'time': '12:50', 'val': 0.17}]
self.assertEqual(valist, valist_ut)

vlextr_ut = [0.11, 0.08, 0.57, 0.21, 0.08, 0.66, 0.32, 0.12, 
0.47, 0.17]

vlextr = ma.extrvalues(valist)
self.assertEqual(len(vlextr_ut), len(vlextr))
for (idx, elem) in enumerate(vlextr_ut):
self.assertAlmostEqual(elem, vlextr[idx])


But I was wondering, *should* this test be separated into two unit 
tests, one for each function? On the face of it, it looks that's how it 
should be done.


This, however, raises the question: what's the order of test execution 
in the unittest? And how to pass values between unit tests? Should I 
modify 'self' in unit test?


Regards,
mk

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


Re: v = json.loads("{'test':'test'}")

2009-01-26 Thread Ivan Illarionov
Diez wrote:
> gert schrieb:
> > Single quotes works in every browser that support json so i
> > recommended python should support it too, besides it looks much
> > cleaner
> > {'test': 'test'}
> > {"test": "test"}
>
> > It can not be that hard to support both notation can it ?
>
> It's not hard, but it's not standard-conform.
>
> Most browsers even accept something like this:
>
> {foo : "bar"}
>
> But all of this is not JSON.

By the way, all of this *is* YAML:

>>> yaml.load("{'test':'test'}")
{'test': 'test'}
>>> yaml.load("{test: test}")
{'test': 'test'}

If someone needs more forgiving and user-editable format,
YAML might be a good choice.

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


Re: USB in python

2009-01-26 Thread Lie Ryan
On Fri, 23 Jan 2009 18:56:38 +1100, Astan Chee wrote:

> Diez B. Roggisch wrote:
>>> 
>>> 
>> If all you need is on-off - why can't you just use a switch?
>>
>>
>>
> Because I want to control the on-off the device using a computer and
> write software for it (which I am confident I can do if I had references
> to how the wrappers to said interface). Cheers
> Astan.
> 

How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little 
Hardware Experience). High pitched sound (or anything in sound-ology that 
means high voltage) means the device is on and low pitched sound off. The 
device will need an additional transistor to separate low voltage from 
the high voltage. I don't know how much power can be pulled from jack 
out, but for a home brewn device it is still feasible to draw power from 
USB and signal from jack out.

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


Re: USB in python

2009-01-26 Thread Banibrata Dutta
high pitch is == high frequency, no higher amplitude... but the difference
can be easily made out and the electronics for this is very well understood
and used.point is, the gentleman asking the question might already have a
USB controller built into his device, and while most modern computers have
anywhere between 2-6 USB ports, you have only 1 audio-out (Mic), so chances
of finding a free "controller" port may be a bit of a challenge.

On Mon, Jan 26, 2009 at 1:51 PM, Lie Ryan  wrote:

> On Fri, 23 Jan 2009 18:56:38 +1100, Astan Chee wrote:
>
> > Diez B. Roggisch wrote:
> >>>
> >>>
> >> If all you need is on-off - why can't you just use a switch?
> >>
> >>
> >>
> > Because I want to control the on-off the device using a computer and
> > write software for it (which I am confident I can do if I had references
> > to how the wrappers to said interface). Cheers
> > Astan.
> >
>
> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little
> Hardware Experience). High pitched sound (or anything in sound-ology that
> means high voltage) means the device is on and low pitched sound off. The
> device will need an additional transistor to separate low voltage from
> the high voltage. I don't know how much power can be pulled from jack
> out, but for a home brewn device it is still feasible to draw power from
> USB and signal from jack out.
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
regards,
Banibrata
http://www.linkedin.com/in/bdutta
--
http://mail.python.org/mailman/listinfo/python-list


Function arguments

2009-01-26 Thread brasse
Hello!

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

I'm mainly curious since I have stumbled on to some cases where it
might have been nice to be able to do that.

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


Re: v = json.loads("{'test':'test'}")

2009-01-26 Thread Diez B. Roggisch

gert schrieb:

On Jan 26, 12:40 am, "Diez B. Roggisch"  wrote:

But all of this is not JSON.

Yes it is, you just make it more python dictionary compatible :)

No, what you do is to make it more incompatible with other
json-implementations. Which defies the meaning of a standard.

Besides, {foo : "bar"} is *not* python dictionary compatible, at least
not unless you defined foo beforehand, and then there is no guarantee
that foo is actually as string containing 'foo'.


What is this json person email address so I can ask that he makes a
very small update on his site.

Go try your luck -http://www.json.org/


Besides if you can make lightweight versions of standards
http://docs.python.org/library/xml.dom.minidom.html

minidom is a lightweight version of the DOM-API. But it reads and writes
standard-conform XML documents.

The same applies for element-tree and lxml.

So it does not serve as a counter-example.


yes it does because adding ' does not mean replacing " so it will
still load standard json. Like every browser does and is exactly the
same philosofie as


No. If minidom would accept XML-documents that contain newlines in 
attributes (which otherwise are forbidden), e.g.




*that* would be like adding single-quote stringliterals to JSON.

This is about the *format*, not the API.

There are people who say something along the lines of "be strict when 
writing, and tolerant when reading" (the exact quote is different, but 
neither google:~site:mybrain nor any other have helped me here), so one 
could argue that reading JSON that is not standard-conform would be ok.


But IMHO that would increase the amount of interoperability-problems - 
because some people would *write* json that isn't standard-conform anymore.


Diez

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


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

2009-01-26 Thread Bruno Desthuilliers

Russ P. a écrit :

On Jan 23, 4:57 am, Bruno Desthuilliers  wrote:

Russ P. a écrit :



As I said before, if you have the source code you can always change
private attributes to public in a pinch if the language enforces
encapsulation.

And then have to maintain a fork. No, thanks.


For crying out loud, how many private attributes do you need to
access?



May I remind you that this is an hypothetical use case ?


If it's a dozen, then you and your library developer are
obviously not on the same page. If it's one or two, then it's hardly a
"fork." Just take note of the one or two places where you needed to
remove the access restriction and you're done.


Yeah, fine. And doing it each and any release. A fork is a fork is a fork...



But if you are working on a team project, you can't
change the code that another member of a team checks in.

Why on earth couldn't I change the code of another member of my team if
that code needs changes ? The code is the whole team's ownership.


OK, fine, you can change the code of another member of the team.


No. I can change the *team's* code. Please *read*. "team's ownership", 
ok ? Or do I have to spell it out loud ? TEAM'S OWNERSHIP. Uh. You get 
the message, now ?



Are
you going to check with him first, or just do it?


I give up.

(snip)


My my my. If you don't trust your programmers, then indeed, don't use
Python. What can I say (and what do I care ?). But once again, relying
on the language's access restriction to manage *security* is, well, kind
of funny, you know ?


Are you seriously saying that if you were managing the production of a
major financial software package with hundreds of developers, you
would just "trust" them all to have free access to the most sensitive
and critical parts of the program?   Now *that's*, well, kind of funny,
you know?


A remote web service - for example - is a far better blackbox when it 
comes to this kind of "sensitive and critical parts". If I can't trust 
someone wrt/ "this" part of the code, then he won't even have it as a 
binary package. Period.



Would you give all those developers your password to get into the
system? No? Wait a minute ... you mean you wouldn't "trust" them with
your password? But what about "openness"? Are you some sort of fascist
or what?


Goodwin point. You loose. Good bye again, Mr P.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Function arguments

2009-01-26 Thread Erik Max Francis

brasse wrote:


Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

I'm mainly curious since I have stumbled on to some cases where it
might have been nice to be able to do that.


Look up the function call syntaxes with * and **:

>>> def foo(*args): print args
...
>>> def bar(**keywords): print keywords
...
>>> foo(1, 2, 3)
(1, 2, 3)
>>> bar(a=1, b=2, c=3)
{'a': 1, 'c': 3, 'b': 2}

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
 San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
  Custom reconciles us to everything.
   -- Edmund Burke
--
http://mail.python.org/mailman/listinfo/python-list


Re: Function arguments

2009-01-26 Thread Chris Rebert
On Mon, Jan 26, 2009 at 1:03 AM, brasse  wrote:
> Hello!
>
> Is there any way that I can get at all the arguments passed to a
> function as a map without using keyword arguments?
>
> def foo(a, b, c):
># Can I access all the arguments in a collection somewhere?

You can use positional arguments:

def foo(*args):
print args

foo("a", "b", "c") #==> ["a", "b", "c"]

Though if you explained your situation more, the newsgroup could
probably be of greater help.

Cheers,
Chris

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


Re: Function arguments

2009-01-26 Thread Diez B. Roggisch

brasse schrieb:

Hello!

Is there any way that I can get at all the arguments passed to a
function as a map without using keyword arguments?

def foo(a, b, c):
# Can I access all the arguments in a collection somewhere?

I'm mainly curious since I have stumbled on to some cases where it
might have been nice to be able to do that.



There is the locals()-call that returns the local variables as dictionary.

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


Re: Function arguments

2009-01-26 Thread brasse
On Jan 26, 10:11 am, Chris Rebert  wrote:
> On Mon, Jan 26, 2009 at 1:03 AM, brasse  wrote:
> > Hello!
>
> > Is there any way that I can get at all the arguments passed to a
> > function as a map without using keyword arguments?
>
> > def foo(a, b, c):
> >    # Can I access all the arguments in a collection somewhere?
>
> You can use positional arguments:
>
> def foo(*args):
>     print args
>
> foo("a", "b", "c") #==> ["a", "b", "c"]
>
> Though if you explained your situation more, the newsgroup could
> probably be of greater help.
>

This is an abbreviated version of what I am doing now:

def make_data(**kw):
'''
make_data(foo='123', bar=42, time=time.time())
'''
template = '%(foo)s - %(bar)d - %(time)s'
kw['time'] = time.strftime('%c', kw['time']
return template % kw

This works, but the function signature doesn't say much about
arguments I should pass to it. What I would like to do is something
like this:

def make_data(foo, bar time):
template = '%(foo)s - %(bar)d - %(time)s'
args = magic_get_args_function()
args['time'] = time.strftime('%c', args['time']
return template % args

I hope this should clear things up a bit. :-)

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


Re: Function arguments

2009-01-26 Thread Chris Rebert
On Mon, Jan 26, 2009 at 1:34 AM, brasse  wrote:
> On Jan 26, 10:11 am, Chris Rebert  wrote:
>> On Mon, Jan 26, 2009 at 1:03 AM, brasse  wrote:
>> > Hello!
>>
>> > Is there any way that I can get at all the arguments passed to a
>> > function as a map without using keyword arguments?
>>
>> > def foo(a, b, c):
>> ># Can I access all the arguments in a collection somewhere?
>>
>> You can use positional arguments:
>>
>> def foo(*args):
>> print args
>>
>> foo("a", "b", "c") #==> ["a", "b", "c"]
>>
>> Though if you explained your situation more, the newsgroup could
>> probably be of greater help.
>>
>
> This is an abbreviated version of what I am doing now:
>
> def make_data(**kw):
>'''
>make_data(foo='123', bar=42, time=time.time())
>'''
>template = '%(foo)s - %(bar)d - %(time)s'
>kw['time'] = time.strftime('%c', kw['time']
>return template % kw
>
> This works, but the function signature doesn't say much about
> arguments I should pass to it. What I would like to do is something
> like this:
>
> def make_data(foo, bar time):
>template = '%(foo)s - %(bar)d - %(time)s'
>args = magic_get_args_function()
>args['time'] = time.strftime('%c', args['time']
>return template % args

Just use locals() as was pointed out by Diez:

def make_data(foo, bar, time):
template = '%(foo)s - %(bar)d - %(time)s'
time = time.strftime('%c', time)
return template % locals()

Cheers,
Chris

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


Re: ob_type in shared memory

2009-01-26 Thread Aaron Brady
Hi Mark, nice to have your comment.

On Jan 25, 9:47 pm, Mark Wooding  wrote:
> Aaron Brady  writes:
snip
> > Object 'A' is of type 'Ta'.  When process 'P' is looking at it, it
> > needs to have an 'ob_type' that is 'Ta' as process 'P' sees it.  When
> > process 'Q' is looking at it, it needs to have an 'ob_type' that is
> > 'Ta' as process 'Q' sees it.  If it referred to 'Ta' in process 'P'
> > when 'Q' was calling it, 'Q' would have to access memory that is in
> > another process.
>
> I see.  My immediate reaction was to suggest that you just put the
> PyTypeObject in shared memory -- but then I realised that the shared
> memory region will probably be in different addresses in the two
> processes.

Yes, exactly.

> I'll assume that you've got enough synchronization between
> the processes involved to stop everything from turning to mush.

Have at thee, synchronization!  Yes, it is possible theoretically.
(By the way, did you know you can detect deadlock before it strikes
with a simple breadth-first search?)  The idea is still not out of the
proof-of-concept stage-- the concept not having been proved.

> > Therefore, I need a field and an array.  The field indicates which
> > type should be loaded, and the array contains the types.  Quick
> > example:
>
> > PyTypeObject* array_of_types[]= { &SharedList, &SharedTuple };
>
> > Then, when a list is being accessed, it can set its own 'ob_type'
> > field to 'array_of_types[ 0 ]', and similarly for a tuple.
>
> > However, I'm having trouble getting 'array_of_types' in the right
> > module during compilation.  My question is: Where do 'array_of_types'
> > and the forward declarations for the types go?
>
> I'm not sure I understand the difficulty.  They'll want to go in your C
> module somewhere, but as long as SharedList and SharedTuple are either
> in the same source file or not declared `static' you just write the
> definition you've got somewhere after declaring or defining the actual
> types in question.

The problem I ran into was that different modules (C files) were
seeing different versions of the structure.  '&SharedList' showed up
as different addresses in the debugger!  I might be missing something,
but I think the forward declaration got interpreted as an actual
declaration in the different files that included the header.
Mysteriously, it disappeared when I tried it again now.  It's still a
possibility.

The solution I looked at today was declare the array like this:

PyTypeObject* keep_types[];

It's even ok in a shared header (multiply included).  Then the actual
definition just gives it NULLs and a size, which are filled in as the
needed modules are loaded.  That solves the (additional) problem of
not having called the owner module's initialization code.

The catch is that if you have an object in a SharedList, you need to
have imported its module before you load it.  However 'pickle' has
that problem too, so I don't feel bad.

> There's a comment in Extending and Embedding (2.1) about initializing
> PyTypeObjects:
>
> :          PyObject_HEAD_INIT(NULL)
> :
> : This line is a bit of a wart; what we'd like to write is:
> :
> :          PyObject_HEAD_INIT(&PyType_Type)
> :
> : as the type of a type object is "type", but this isn't strictly
> : conforming C and some compilers complain.
>
> The comment here is wrong: &PyType_Type is a perfectly good constant
> expression as far as C is concerned, but alas Microsoft's dynamic
> linking system isn't clever enough to cope with it even so (because it's
> in a separate link unit).  But this isn't a problem in our case:
> presumably SharedList and SharedTuple are in the same module, so this
> should all just work.

Here is another quote from the docs:

"Portability therefore requires not to make any assumptions about
symbol visibility. This means that all symbols in extension modules
should be declared static, except for the module’s initialization
function, in order to avoid name clashes with other extension modules
(as discussed in section The Module’s Method Table and Initialization
Function)."

All declared static?  Dear.  So I don't know what to make of it,
especially given the need to call 'PyType_Ready'.
--
http://mail.python.org/mailman/listinfo/python-list


Ldap Extended Operation python

2009-01-26 Thread Benny Fallica
Hello there,

what would be the python implementation for this line in Java:


java.util.Hashtable environment = LdapHelper.getEnvironment(url, true);
LdapContext ldapContext = new InitialLdapContext(environment, null);
Response resp = (Response) ldapContext.extendedOperation(new Request())
how to deal with extended operations in python?

thanks for your help!


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


Re: Dynamic methods and lambda functions

2009-01-26 Thread Mark Wooding
Michael Torrie  writes:

> Basically, don't use a lambda.  Create a real, local closure with a
> nested def block.  That way the closure is created every time the
> parent function is called.

Nope.  I explained the real problem quite clearly, and it's to do with
the difference between binding and assignment.

What's the difference between these two pieces of code?

## First
def __init__(self):
  for n, v in props:
setattr(Person, '_' + n, v)
setattr(Person, n, lambda self: getattr(self, '_' + n))

## Second
def __init__(self):
  for n, v in props:
setattr(Person, '_' + n, v)
def hack(self): return getattr(self, '_' + n)
setattr(Person, n, hack)

> Lambda expressions are only ever compiled once during execution.

The same is true of `def' bodies.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Counting number of objects

2009-01-26 Thread Andreas Waldenburger
On Mon, 26 Jan 2009 02:37:37 + Mark Wooding 
wrote:

> > This looks OK, although I'd suggest using "cls.counter += 1" instead
> > of "a.counter += 1" in the __new__() method. Just seems clearer to
> > me, esp. when you think about subclassing. 
> 
> I'm not sure about clarity, but that would be semantically different.
> The code as written counts all instances of a and its subclasses.
> Your suggestion would count instances of subclasses independently.  I
> don't know which behaviour the OP would prefer, but I don't think
> choosing between them is a matter of clarity.
> 
Oh shoot. I should have known that. I hope I will from now on, at
least. :/

/W


-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


Re: *.python.org broken?

2009-01-26 Thread Carlos Ribeiro
I experienced the same problem here in Brazil. Tweeks ago I could not access
any *.python.org site. Today I tried and it worked. The problem seemed to be
limited to Python's domain because I could access every other site that I
tried the same day.
More info:

- At least for me I didn't seem to be IPv6 related.
- It failed in the same day in more than one PC, using different providers.
- The domain name was resolving ok.

My guess is that it was some kind of BGP failure. Perhaps the AS that hosts
Python's servers injected a wrong route or someone else did, effectively
stealing all the traffic for that prefix. That's more common than people
realize (specially nowadays when countries such as Pakistan try to block
domains by messing with BGP).

Carlos Ribeiro

On Sun, Jan 25, 2009 at 11:44, Cousin Stanley wrote:

>
> > Is anybody else having trouble accessing sites (including www, docs,
> > wiki) in the python.org tree, or is it just me? (Or just .au?)
>
>   Yes, connecting to python.org sites has been problematic
>  for me as well 
>
>  I don't remember when the trouble started, but it's been
>  problematic for at least a week or longer here 
>
>
> --
> Stanley C. Kitching
> Human Being
> Phoenix, Arizona
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribe...@gmail.com
mail: carribe...@yahoo.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python C API (PyObject_CallMethod clear object)

2009-01-26 Thread googler . 1 . webmaster
On 26 Jan., 03:25, "Gabriel Genellina"  wrote:
> En Sun, 25 Jan 2009 23:46:01 -0200,   
> escribió:
>
>
>
> > I have a problm with deallocating stuff. I call a function with this
> > command:
>
> > PyObject *rvalue = PyObject_CallMethod(obj, "execute","",NULL);
>
> > if(rvalue==NULL)
> >     PyErr_Print();
> > else
> >     Py_DECREF(rvalue);
>
> > Can it be, that something is missing here? Imagine I allocate an
> > object of a type:
>
> > t = MyImage()
> > ,;- syntax_error 129=)/%  #is a syntax error
>
> > How you see I would get a syntaxerror, but an object of the type
> > MyImage() is created. But its deallocated when Py_Finalize() is
> > called. What can I do to deallocate this object after
> > PyObject_CallMethod returns NULL, too? I debug it, so if rvalue!=NULL
> > the object is deallocated, but what can I do if rvalue==NULL=
>
> A syntax error is detected *before* the code runs -- so the object isn't  
> created at all.
>
> --
> Gabriel Genellina

Hi!

Yes, you are right. I got this issue not in syntax error, I get it on
a runtime error like this:

t = MyImage()
self.test("hallo")   #test takes an integer so this would throw an
exception.


Here you see, an object of the type MyImage() was created but its not
deleted. 't' is a local reference so how can I do that?


Thanks!

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Tim Rowe
2009/1/25 Tim Chase :

> (again, a malformed text-file with no terminal '\n' may cause it
> to be absent from the last line)

Ahem. That may be "malformed" for some specific file specification,
but it is only "malformed" in general if you are using an operating
system that treats '\n' as a terminator (eg, Linux) rather than as a
separator (eg, MS DOS/Windows).

Perhaps what you don't /really/ want to be reminded of is the
existence of operating systems other than your preffered one?

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


Re: A java hobbyist programmer learning python

2009-01-26 Thread Tim Rowe
> I like the latter two styles, particularly the last one. That way you
> can see at a glance that those member variables are defined in the
> super class.

I like the second style because it makes it leaves the 2-d
implementation hidden, which is the whole point of encapsulation.

> But then I am a fan of Hungarian notation, which many
> programmers can't stand.

Is it that programmers can't stand it, or is it that they can't stand
it when it's imposed when not needed? As a pseudo type system for
languages with no typing it's pretty useful. To the extent that a
language provides typing it's useless verging on dangerous because it
can get out of synch with the actual type. I believe that any case of
Hungarian notation being useful is evidence of a flaw in the language
being used -- but arguably all languages are flawed in some way or
other, so Hungarian /can/ be useful. At this level I don't recognise a
difference between System and Applications Hungarian, by the way --
the difference is eliminated if you declare types corresponding to the
"meanings", which is commonplace in, for example, Ada.

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


Re: Python C API (PyObject_CallMethod clear object)

2009-01-26 Thread googler . 1 . webmaster
the hook is, how to delete the locals of this function, maybe thats a
workaround?

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


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

2009-01-26 Thread Bruno Desthuilliers

Russ P. a écrit :

On Jan 23, 6:36 pm, Luis Zarrabeitia  wrote:


Makes *no* sense? There's *no* good reason *at all* for the original
author to hide or protect internals?

My bad, sorry.
It makes sense... if the original author is an egotist who believes he must
control how I use that library.


If the original author provides you with the source code and the right
to modify it, he cannot possibly control how you use the library. You
can trivially disable any access controls. But for some reason that's
not enough for you.

Has it occurred to you that some users might actually *want* access
controls? 


Then they'll have to choose a language which provides it.


Maybe some users want to actually use the library as the
author intended it to be used.


And ? Strange enough, that's usually what happens - using the official, 
documented API. Strange enough, it seems that Python programmers are 
mostly wise enough to not break encapsulation (nor abuse any of the 
highly dynamic features of Python) without pretty good reasons, lots of 
thought and attention, clear documentation of the fact, and possibly 
exchanges with the library author (or maintainer) to discuss the problem.

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Sion Arrowsmith
Diez B. Roggisch  wrote:
> [ ... ] Your approach of reading the full contents can be 
>used like this:
>
>content = a.read()
>for line in content.split("\n"):
> print line
>

Or if you want the full content in memory but only ever access it on a
line-by-line basis:

content = a.readlines()

(Just because we can now write "for line in file" doesn't mean that
readlines() is *totally* redundant.)

-- 
\S -- si...@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange error whilst porting to 2.6

2009-01-26 Thread Robin Becker

I found that this error

Exception RuntimeError: 'maximum recursion depth exceeded in __subclasscheck__' in  ignored 


occurs when attempting to copy (copy.copy(inst)) an instance of a class that 
looks like this


class LazyParagraph(_LazyMixin,TTParagraph):
SUPER=TTParagraph
_CLEAN_SPACE=1


however, whilst debugging I found the error disappeared when I tried to 
determine what accesses were being made on this instance during the copy. If I 
use this code


class _LazyParagraph(_LazyMixin,TTParagraph):
SUPER=TTParagraph
_CLEAN_SPACE=1

class LazyParagraph(_LazyParagraph):
def __getattr__(self,a):
return getattr(_LazyParagraph,a)


then instances of the new lazy paragraph don't cause a problem. This looks 
awfully fragile to me, and I haven't really got any closer to understanding 
what's going on. Anyone got any ideas?


I have attempted to abstract the problem, but so far I haven't found the vital 
bits.
--
Robin Becker

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


Re: Ldap Extended Operation python

2009-01-26 Thread Michael Ströder
Benny Fallica wrote:
> Hello there,
> 
> what would be the python implementation for this line in Java:
> 
> 
> java.util.Hashtable environment = LdapHelper.getEnvironment(url, true);
> LdapContext ldapContext = new InitialLdapContext(environment, null);
> Response resp = (Response) ldapContext.extendedOperation(new Request())
> how to deal with extended operations in python?

Which LDAP extended operation do you want to use?

In python-ldap the following ext. ops are already implemented:
http://python-ldap.sourceforge.net/doc/html/ldap.html#ldap.LDAPObject.whoami_s
http://python-ldap.sourceforge.net/doc/html/ldap.html#ldap.LDAPObject.passwd

There's no generic interface for ext. ops. yet. Feel free to contribute
something like this.

Ciao, Michael.
--
http://mail.python.org/mailman/listinfo/python-list


Re: is None vs. == None

2009-01-26 Thread Steve Holden
Terry Reedy wrote:
> Roger wrote:
>>> And, just for completeness, the "is" test is canonical precisely because
>>> the interpreter guarantees there is only ever one object of type None,
>>> so an identity test is always appropriate. Even the copy module doesn't
>>> create copies ...
>>>
>>
>> Does the interpreter guarantee the same for False and True bools?
> 
> Yes.  Bool(x) should return one of the two existing instances.
> 
> In 2.x, the *names* 'True' and 'False' can be rebound because bool is
> new and people write
> try:
>   False,True
> except NameError:
>   False,True = 0,1
> 
> to make code back compatible.
> 
I would claim that the ability to rebind True and False is a simple bug,
though one not likely to be fixed in an 2.x release. The code above
doesn't rebind True and False in interpreters that have them ...

> In 3.0, the names are keywords, just like 'None' and cannot be rebound,
> so x is True is guaranteed to answer whether x *is* the true object.
> 
> Back before rebinding 'None' was prohibited, 'is None' was not
> completely guaranteed either (absent reading the rest of a file to be
> sure no rebinding would be done).
> 
And that was a bug too, in this case one that *was* removed in 2.4, I
believe. Don't have 2.3 lying around just now.

Python 2.4.3 (#1, May 24 2008, 13:47:28)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> None = 3
SyntaxError: assignment to None

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Counting number of objects

2009-01-26 Thread Steve Holden
Mark Wooding wrote:
> Andreas Waldenburger  writes:
> 
>> On Sun, 25 Jan 2009 09:23:35 -0800 (PST) Kottiyath
>>  wrote:
>>
>>> class a(object):
>>> counter = 0
>>> def __new__(cls, *args, **kwargs):
>>> a.counter += 1
>>> return object.__new__(cls, *args, **kwargs)
> 
> Hmm.  Exceptions raised during object creation make this rather
> precarious.  In your code, if object.__new__ raises an exception, the
> counter will end up too high (the __del__ method won't get called in
> this case).
> 
If object.__new__ raises an exception you have bigger problems to worry
about than an object count being wrong.

> One might try to rewrite it:
> 
> def __new__(cls, *args, **kw):
>   thing = object.__new__(cls, *args, **kw)
>   a.counter += 1
>   return thing
> 
> Now this won't work in subclasses if they also have a __new__ method:
> again, if the subclass's __new__ raises an exception then __del__ won't
> be called and the counter will be too high.
> 
> To make this technique work, I think you need to do the counter
> increment in __init__ rather than __new__, and to set an attribute so
> that __del__ knows whether to do the decrement.  (If a subclass's
> __init__ raises an exception before yours gets called, you don't want to
> do the decrement because that'll leave the counter too low.)
> 
Yes, rather better to do it that way and decouple it from __new__(). Of
course super() might help here, though not everyone approves of it.

>> This looks OK, although I'd suggest using "cls.counter += 1" instead
>> of "a.counter += 1" in the __new__() method. Just seems clearer to me,
>> esp. when you think about subclassing. 
> 
> I'm not sure about clarity, but that would be semantically different.
> The code as written counts all instances of a and its subclasses.  Your
> suggestion would count instances of subclasses independently.  I don't
> know which behaviour the OP would prefer, but I don't think choosing
> between them is a matter of clarity.
> 
Correct, but pointing out the differences has highlighted that is a real
design decision to be made in the event that subclassing is used.

>> Another way to go would be to use the weakref module and create a
>> weakref-set (or list) as the counter. That way you would only need to
>> add the objects in the __new__() method and not worry about removing
>> them. I will admit that this is overengineering the problem a bit, but
>> might be a good exercise.
> 
> This is a better approach, because it avoids the problems with
> exceptions during object construction that I described above.
> 
>>> Another question - unrelated to the major topic:
>>> How much time does it take to be proficient in Python?
>> Don't concern yourself with that question at all, is my advice.
> 
> Indeed.  Besides, it varies an awful lot.
> 
[...]
>>> - or am I just dumb?
>> You're writing programs and you're communicating with like-minded
>> people about your problems (in a socially appropriate way). Not what
>> dumb people do, in my book.
> 
> Absolutely!  Besides, you're asking sensible questions about subtle
> parts of the language -- I wouldn't say that __new__ was beginner
> territory, for example.  So, no, you certainly don't seem dumb to me.
> 
Hear, hear!

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


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

2009-01-26 Thread Steve Holden
Bruno Desthuilliers wrote:
> Russ P. a écrit :
>> On Jan 23, 6:36 pm, Luis Zarrabeitia  wrote:
>>
 Makes *no* sense? There's *no* good reason *at all* for the original
 author to hide or protect internals?
>>> My bad, sorry.
>>> It makes sense... if the original author is an egotist who believes
>>> he must
>>> control how I use that library.
>>
>> If the original author provides you with the source code and the right
>> to modify it, he cannot possibly control how you use the library. You
>> can trivially disable any access controls. But for some reason that's
>> not enough for you.
>>
>> Has it occurred to you that some users might actually *want* access
>> controls? 
> 
> Then they'll have to choose a language which provides it.
> 
>> Maybe some users want to actually use the library as the
>> author intended it to be used.
> 
> And ? Strange enough, that's usually what happens - using the official,
> documented API. Strange enough, it seems that Python programmers are
> mostly wise enough to not break encapsulation (nor abuse any of the
> highly dynamic features of Python) without pretty good reasons, lots of
> thought and attention, clear documentation of the fact, and possibly
> exchanges with the library author (or maintainer) to discuss the problem.

Quite. Python is a language "for consenting adults". It has perceived
deficiencies for certain software engineering environments. Can we drop
the subject now? This horse was flogged to death long ago, and it's
pointless and cruel to keep on beating the remains.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Dynamic methods and lambda functions

2009-01-26 Thread Steve Holden
Mark Wooding wrote:
> unine...@gmail.com writes:
[...]
>   * Assignment stores a new (reference to a) value in the variable.
> 
>   * Binding modifies the mapping between names and variables.
> 
I realise I have omitted what was doubtless intended to be explanatory
detail, but I am having trouble reconciling those sentences. Would you
mind explaining "in vacuuo" what you see as the difference between
assignment and binding?

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Building matplotlib from source on windows

2009-01-26 Thread mk

Hello everyone,

I'm trying to get 0.98.5.2 installed on Windows to use Python 2.6 
(dependency packages I need to use on that version, long story, etc).


When I was trying to build it (python setup.py build), it was finding 
the VC 9.0 C++ compiler on my comp. However, after adding necessary 
packages (zlib, png, etc), it was reporting missing 'unistd.h'. Clearly, 
this means it was meant to be built with GCC for Windows like MinGW ?


I have uninstalled the VC compiler, installed GnuWin32 packages and 
tried using MinGW (passing --compiler=mingw32 to python setup.py build ) 
but now compilation process fails like this:


c:\MinGW\bin\g++.exe -mno-cygwin -shared -s 
build\temp.win32-2.6\Release\src\ft2font.o build\temp.wi
n32-2.6\Release\src\mplutils.o 
build\temp.win32-2.6\Release\cxx\cxxsupport.o build\temp.win32-2.6\Re
lease\cxx\cxx_extensions.o 
build\temp.win32-2.6\Release\cxx\indirectpythoninterface.o build\temp.win
32-2.6\Release\cxx\cxxextensions.o 
build\temp.win32-2.6\Release\src\ft2font.def -LC:\Python26\libs -
LC:\Python26\PCbuild -lfreetype -lz -lgw32c -lstdc++ -lm -lpython26 
-lmsvcr90 -o build\lib.win32-2.6

\matplotlib\ft2font.pyd
c:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: 
cannot find -lgw32c

collect2: ld returned 1 exit status
error: command 'g++' failed with exit status 1

What the heck is lgw32c??

Regards,
mk

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


Re: USB in python

2009-01-26 Thread Steve Holden
Lie Ryan wrote:
> On Fri, 23 Jan 2009 18:56:38 +1100, Astan Chee wrote:
> 
>> Diez B. Roggisch wrote:
 
 
>>> If all you need is on-off - why can't you just use a switch?
>>>
>>>
>>>
>> Because I want to control the on-off the device using a computer and
>> write software for it (which I am confident I can do if I had references
>> to how the wrappers to said interface). Cheers
>> Astan.
>>
> 
> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little 
> Hardware Experience). High pitched sound (or anything in sound-ology that 
> means high voltage) means the device is on and low pitched sound off. The 
> device will need an additional transistor to separate low voltage from 
> the high voltage. I don't know how much power can be pulled from jack 
> out, but for a home brewn device it is still feasible to draw power from 
> USB and signal from jack out.
> 
Congratulations. You just invented the modem.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: USB in python

2009-01-26 Thread Steve Holden
Astan Chee wrote:
> Tim Roberts wrote:
>> Sorry, but you have NOT created a USB device, and I sincerely hope you do
>> not try to plug it in to a real USB port.
>>   
> Sorry, by USB device, I meant a device that is powered/activated by a
> bunch of wires that I want to control using a computer and since I had a
> spare USB jack lying around, I used that instead. But so far I haven't
> tried it, nor will try it if it wont work properly. Yes, it is not a
> proper USB device, because I didnt build it to specifically interface
> with the USB port; but I had to start somewhere. Also, the device
> requires more power than the standard parallel port can give.
> Anyway, it looks like the easiest solution for my case is a microcontroller

In which case the Arduino may be a good place to start. The recent
duemilanove boards use USB to communicate with the host, and so the USB
port should be available to the microcontroller. We areg etting some way
away from Python now, of course ...

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: I'm a python addict !

2009-01-26 Thread J Kenneth King
Linuxguy123  writes:

> I just started using python last week and I'm addicted.
>
> I hate Perl.  I never did learn to use it with any competence.  I has to
> be the most obfuscated, cryptic language I've ever seen.  Making it
> "object oriented" only makes it worse !
> ..  ..

I program full-time in Python, so I share your excitement and
enthusiasm. But bashing Perl like that doesn't make you sound very
smart. I'm probably one of the very few Python programmers who came from
(and still occassionally) use Perl. I've written non-trivial programs in
it and from my experience I can tell you that it's actually a great
language. The Moose object system is well beyond Python's class
system. But I guess you wouldn't know that.

So yay for Python, but don't get in the habit of criticising that which
you do not know.
--
http://mail.python.org/mailman/listinfo/python-list


Re: I'm a python addict !

2009-01-26 Thread J Kenneth King
J Kenneth King  writes:

> Linuxguy123  writes:
>
>> I just started using python last week and I'm addicted.
>>
>> I hate Perl.  I never did learn to use it with any competence.  I has to
>> be the most obfuscated, cryptic language I've ever seen.  Making it
>> "object oriented" only makes it worse !
>> ..  ..
>
> I program full-time in Python, so I share your excitement and
> enthusiasm. But bashing Perl like that doesn't make you sound very
> smart. I'm probably one of the very few Python programmers who came from
> (and still occassionally) use Perl. I've written non-trivial programs in
> it and from my experience I can tell you that it's actually a great
> language. The Moose object system is well beyond Python's class
> system. But I guess you wouldn't know that.
>
> So yay for Python, but don't get in the habit of criticising that which
> you do not know.

I just realized this might become flame-bait. Please disregard the
flamey bits. :(
--
http://mail.python.org/mailman/listinfo/python-list


Re: Function arguments

2009-01-26 Thread brasse
On Jan 26, 10:39 am, Chris Rebert  wrote:
> On Mon, Jan 26, 2009 at 1:34 AM, brasse  wrote:
> > On Jan 26, 10:11 am, Chris Rebert  wrote:
> >> On Mon, Jan 26, 2009 at 1:03 AM, brasse  wrote:
> >> > Hello!
>
> >> > Is there any way that I can get at all the arguments passed to a
> >> > function as a map without using keyword arguments?
>
> >> > def foo(a, b, c):
> >> >    # Can I access all the arguments in a collection somewhere?
>
> >> You can use positional arguments:
>
> >> def foo(*args):
> >>     print args
>
> >> foo("a", "b", "c") #==> ["a", "b", "c"]
>
> >> Though if you explained your situation more, the newsgroup could
> >> probably be of greater help.
>
> > This is an abbreviated version of what I am doing now:
>
> > def make_data(**kw):
> >    '''
> >    make_data(foo='123', bar=42, time=time.time())
> >    '''
> >    template = '%(foo)s - %(bar)d - %(time)s'
> >    kw['time'] = time.strftime('%c', kw['time']
> >    return template % kw
>
> > This works, but the function signature doesn't say much about
> > arguments I should pass to it. What I would like to do is something
> > like this:
>
> > def make_data(foo, bar time):
> >    template = '%(foo)s - %(bar)d - %(time)s'
> >    args = magic_get_args_function()
> >    args['time'] = time.strftime('%c', args['time']
> >    return template % args
>
> Just use locals() as was pointed out by Diez:
>
> def make_data(foo, bar, time):
>     template = '%(foo)s - %(bar)d - %(time)s'
>     time = time.strftime('%c', time)
>     return template % locals()
>

Nice, thank you!

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Marc 'BlackJack' Rintsch
On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote:

> content = a.readlines()
> 
> (Just because we can now write "for line in file" doesn't mean that
> readlines() is *totally* redundant.)

But ``content = list(a)`` is shorter.  :-)

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


Re: USB in python

2009-01-26 Thread Brian Allen Vanderburg II

astan.c...@al.com.au wrote:

Tim Roberts wrote:
Sorry, but you have NOT created a USB device, and I sincerely hope 
you do

not try to plug it in to a real USB port.
  
Sorry, by USB device, I meant a device that is powered/activated by a 
bunch of wires that I want to control using a computer and since I had 
a spare USB jack lying around, I used that instead. But so far I 
haven't tried it, nor will try it if it wont work properly. Yes, it is 
not a proper USB device, because I didnt build it to specifically 
interface with the USB port; but I had to start somewhere. Also, the 
device requires more power than the standard parallel port can give.
Anyway, it looks like the easiest solution for my case is a 
microcontroller

--
http://mail.python.org/mailman/listinfo/python-list
I've played around in this area a little bit.  Microcontrollers still 
require hardware programming and for simple circuits I think it is 
overkill.  If you want to use USB then you may be able to use the FTDI 
chips.  They have both serial (FT232) and parallel (FT245) chips and are 
quite cheap.  They are surface mount devices though, but you can get a 
kit that includes USB port, the chip already connected to a board with a 
DIP plug and some essential circuits.  libftdi, which runs on top of 
libusb, can control both of these and they require no programming 
(unless you want to change the USB configuration settings such as vendor 
ID, etc, from the default value)


This is the FT245 chip which is basically USB-to-Parallel.

Chips: http://www.ftdichip.com/Products/FT245R.htm
Kit/Board: http://www.ftdichip.com/Products/EvaluationKits/UM245R.htm

The spec sheet for the board seems quite simple.  It's pin out is 
similar to that of a parallel port in that you have your data lines 
DB0-DB7, etc.  It can also be connected in bus-powered configuration 
(~100mA) or self-powered configuration.  The kit is more expensive than 
the chip itself, but probably easier especially if you don't have any 
experience with surface mount.


You could build it into your device. You could also create a simple 
switch box out of it to control external devices, maybe connecting each 
of the data lines to relays to turn on/off eight devices, etc.


Brian Vanderburg II

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Andreas Waldenburger
On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch 
wrote:

> On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote:
> 
> > content = a.readlines()
> > 
> > (Just because we can now write "for line in file" doesn't mean that
> > readlines() is *totally* redundant.)
> 
> But ``content = list(a)`` is shorter.  :-)
> 
But much less clear, wouldn't you say?

content is now what? A list of lines? Characters? Bytes? I-Nodes?
Dates? Granted, it can be inferred from the fact that a file is its
own iterator over its lines, but that is a mental step that readlines()
frees you from doing.

My ~0.0154 €.

/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).

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


really slow gzip decompress, why?

2009-01-26 Thread redbaron
I've one big (6.9 Gb) .gz file with text inside it.
zcat bigfile.gz > /dev/null does the job in 4 minutes 50 seconds

python code have been doing the same job for 25 minutes and still
doesn't finish =( the code is simpliest I could ever imagine:

def main():
  fh = gzip.open(sys.argv[1])
  all(fh)

As far as I understand most of the time it executes C code, so pythons
no overhead should be noticible. Why is it so slow?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newby: how to transform text into lines of text

2009-01-26 Thread J. Cliff Dyer

On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote:
> On Jan 26, 1:03 pm, "Gabriel Genellina" 
> wrote:
> > En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase  
> >  escribió:
> >
> >
> >
> > > Unfortunately, a raw rstrip() eats other whitespace that may be  
> > > important.  I frequently get tab-delimited files, using the following  
> > > pseudo-code:
> >
> > >def clean_line(line):
> > >  return line.rstrip('\r\n').split('\t')
> >
> > >f = file('customer_x.txt')
> > >headers = clean_line(f.next())
> > >for line in f:
> > >  field1, field2, field3 = clean_line(line)
> > >  do_stuff()
> >
> > > if field3 is empty in the source-file, using rstrip(None) as you suggest  
> > > triggers errors on the tuple assignment because it eats the tab that  
> > > defined it.
> >
> > > I suppose if I were really smart, I'd dig a little deeper in the CSV  
> > > module to sniff out the "right" way to parse tab-delimited files.
> >
> > It's so easy that don't doing that is just inexcusable lazyness :)
> > Your own example, written using the csv module:
> >
> > import csv
> >
> > f = csv.reader(open('customer_x.txt','rb'), delimiter='\t')
> > headers = f.next()
> > for line in f:
> >  field1, field2, field3 = line
> >  do_stuff()
> >
> 
> And where in all of that do you recommend that .decode(some_encoding)
> be inserted?
> 

If encoding is an issue for your application, then I'd recommend you use
codecs.open('customer_x.txt', 'rb', encoding='ebcdic') instead of open()

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

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


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

2009-01-26 Thread Paul Rubin
Steve Holden  writes:
> Quite. Python is a language "for consenting adults". 

Shouldn't such a language allow consenting adults to enter a BDSM
scene without being moralized at, if that's what they want to do? ;-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: strange error whilst porting to 2.6

2009-01-26 Thread Robin Becker

Robin Becker wrote:

I found that this error

Exception RuntimeError: 'maximum recursion depth exceeded in 
__subclasscheck__' in  ignored 


occurs when attempting to copy (copy.copy(inst)) an instance of a class 
that looks like this


class LazyParagraph(_LazyMixin,TTParagraph):
SUPER=TTParagraph
_CLEAN_SPACE=1




I have attempted to abstract the problem, but so far I haven't found the 
vital bits.


OK this turns out to be one of those useful exercises after all. After 
instrumenting the copy instance copy func my colleague and I found the problem 
occurs in this innocuous looking example


###
import copy
class _LazyMixin:
"""don't do any initialization until later"""
def __init__(self,*args):
self._args = args
self._initialized = 0

def __getattr__(self,a):
if not self._initialized:
self._Initialize()
return getattr(self,a)
raise AttributeError("No attribute '%s'" % a)

def _Initialize(self):
if not self._initialized:
self._initialized = 1
del self._args

l=_LazyMixin()
print l._initialized
copy.debug=1
copy.copy(l)
###


it turns out that in the absence of other info _copy_inst creates a dummy 
instance and changes its class to the incoming class. It then asks the new 
unpopulated  instance if it has an attribute __setstate__, that triggers the 
_LazyMixin __getattr__ which fails because there's no _initialize member so 
__getattr__ gets recalled etc etc. Presumably in earlier pythons this bad 
behaviour is just hidden. The fix for us is to provide a __setstate__ which does 
what the no __setstate__ code does ie update __dict__.


However, since the _copy_inst code knows there can be no members on the new 
instance should it not be asking the __class__ for __setstate__?

--
Robin Becker

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


Re: really slow gzip decompress, why?

2009-01-26 Thread Diez B. Roggisch
redbaron wrote:

> I've one big (6.9 Gb) .gz file with text inside it.
> zcat bigfile.gz > /dev/null does the job in 4 minutes 50 seconds
> 
> python code have been doing the same job for 25 minutes and still
> doesn't finish =( the code is simpliest I could ever imagine:
> 
> def main():
>   fh = gzip.open(sys.argv[1])
>   all(fh)
> 
> As far as I understand most of the time it executes C code, so pythons
> no overhead should be noticible. Why is it so slow?

I'm guessing here - but if gzip streams (and AFAIK it does), the commandline
will simply stream to /dev/null.

OTOH, python is not streaming, it will instead allocate buffers for the
whole file. Which for a *zipped* 6.9Gb file might take a while.

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


AttributeError: 'module' object has no attribute 'open_workbook'

2009-01-26 Thread Jay Jesus Amorin
Hi,

Kindly help, I've got this error when running my script:

AttributeError: 'module' object has no attribute 'open_workbook'


Here's my code:

#!/usr/bin/python

import xlrd
import sys

mySpreadsheet = xlrd.open_workbook(open(sys.argv[1]))
firstSheet = wb.sheet_by_index(0)

for myRows in range(sh.nrows):
print firstSheet.row_values(myRows)


Here's the error message:

r...@nebuchadnezzar:/home/test/project# ./xlrd.py test.xls
Traceback (most recent call last):
  File "./xlrd.py", line 3, in 
import xlrd
  File "/home/jayam/project/xlrd.py", line 6, in 
mySpreadsheet = xlrd.open_workbook(open(sys.argv[1]))
AttributeError: 'module' object has no attribute 'open_workbook'


Thanks,


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


Re: really slow gzip decompress, why?

2009-01-26 Thread Jeff McNeil
On Jan 26, 10:22 am, redbaron  wrote:
> I've one big (6.9 Gb) .gz file with text inside it.
> zcat bigfile.gz > /dev/null does the job in 4 minutes 50 seconds
>
> python code have been doing the same job for 25 minutes and still
> doesn't finish =( the code is simpliest I could ever imagine:
>
> def main():
>   fh = gzip.open(sys.argv[1])
>   all(fh)
>
> As far as I understand most of the time it executes C code, so pythons
> no overhead should be noticible. Why is it so slow?

Look what's happening in both operations. The zcat operation is simply
uncompressing your data and dumping directly to /dev/null. Nothing is
done with the data as it's uncompressed.

On the other hand, when you call 'all(fh)', you're iterating through
every element in in bigfile.gz.  In other words, you're reading the
file and scanning it for newlines versus simply running the
decompression operation.


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


Re: really slow gzip decompress, why?

2009-01-26 Thread Jeff McNeil
On Jan 26, 10:51 am, Jeff McNeil  wrote:
> On Jan 26, 10:22 am, redbaron  wrote:
>
> > I've one big (6.9 Gb) .gz file with text inside it.
> > zcat bigfile.gz > /dev/null does the job in 4 minutes 50 seconds
>
> > python code have been doing the same job for 25 minutes and still
> > doesn't finish =( the code is simpliest I could ever imagine:
>
> > def main():
> >   fh = gzip.open(sys.argv[1])
> >   all(fh)
>
> > As far as I understand most of the time it executes C code, so pythons
> > no overhead should be noticible. Why is it so slow?
>
> Look what's happening in both operations. The zcat operation is simply
> uncompressing your data and dumping directly to /dev/null. Nothing is
> done with the data as it's uncompressed.
>
> On the other hand, when you call 'all(fh)', you're iterating through
> every element in in bigfile.gz.  In other words, you're reading the
> file and scanning it for newlines versus simply running the
> decompression operation.

The File:

[j...@marvin ~]$ ls -alh junk.gz
-rw-rw-r-- 1 jeff jeff 113M 2009-01-26 10:42 junk.gz
[j...@marvin ~]$

The 'zcat' time:

[j...@marvin ~]$ time zcat junk.gz > /dev/null

real0m2.390s
user0m2.296s
sys 0m0.093s
[j...@marvin ~]$


Test Script #1:

import sys
import gzip

fs = gzip.open('junk.gz')
data = fs.read(8192)
while data:
sys.stdout.write(data)
data = fs.read(8192)


Test Script #1 Time:

[j...@marvin ~]$ time python test9.py >/dev/null

real0m3.681s
user0m3.201s
sys 0m0.478s
[j...@marvin ~]$


Test Script #2:

import sys
import gzip

fs = gzip.open('junk.gz')
all(fs)


Test Script #2 Time:

[j...@marvin ~]$ time python test10.py

real1m51.764s
user1m51.475s
sys 0m0.245s
[j...@marvin ~]$

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


Re: AttributeError: 'module' object has no attribute 'open_workbook'

2009-01-26 Thread MRAB

Jay Jesus Amorin wrote:
> Hi,
>
> Kindly help, I've got this error when running my script:
>
> AttributeError: 'module' object has no attribute 'open_workbook'
>
>
> Here's my code:
>
> #!/usr/bin/python
>
> import xlrd
> import sys
>
> mySpreadsheet = xlrd.open_workbook(open(sys.argv[1]))
> firstSheet = wb.sheet_by_index(0)
>
> for myRows in range(sh.nrows):
> print firstSheet.row_values(myRows)
>
>
> Here's the error message:
>
> r...@nebuchadnezzar:/home/test/project# ./xlrd.py test.xls
> Traceback (most recent call last):
>   File "./xlrd.py", line 3, in 
> import xlrd
>   File "/home/jayam/project/xlrd.py", line 6, in 
> mySpreadsheet = xlrd.open_workbook(open(sys.argv[1]))
> AttributeError: 'module' object has no attribute 'open_workbook'
>
From the traceback it looks like your script is called "xlrd.py", which
is the same name as the module "xlrd" that it's importing. If that's the
case, rename your script.
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-26 Thread Tim Rowe
2009/1/26 Paul Rubin <"http://phr.cx"@nospam.invalid>:
> Steve Holden  writes:
>> Quite. Python is a language "for consenting adults".
>
> Shouldn't such a language allow consenting adults to enter a BDSM
> scene without being moralized at, if that's what they want to do? ;-)

The language doesn't stop you. You can shift all of your code over to
Ada if you want to, and although /some/ Pythonistas might shake their
heads in bewilderment, I don't think there would be any moralising.
The question is whether Python has to /provide/ the BDSM scene for
you. I don't think it's realistic for a language to provide every
possible degree of BDSM from BCPL to Ada, Eiffel and beyond. A
language has to be positioned somewhere on the scale, and deciding
whether that's the right point on the scale for you and your project
is part of what being a grown-up programmer is about. One size does
not fit all, one language is not ideal for all applications.


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


Problem with Nose testing until forking process

2009-01-26 Thread Jan Koprowski
Hi !

  I write application witch sometimes need fork to shell based process
(some kind of shell command). I snatch stdin, stdout, stderr and two
additional streams and fork process to run command and get results.

# -*- encoding: utf-8 -*-

import os
import sys
import subprocess

def pipes_function():

global login_r, login_w, password_r, password_w

try: os.fdopen(3).close()
except OSError: pass
try: os.fdopen(4).close()
except OSError: pass

os.close(login_w)
os.close(password_w)
os.dup2(login_r, 3)
os.dup2(password_r, 4)

class BrokerProcess:

__slots__ = ['username', 'password', 'command', 'stdout',
'stderr', 'returned']

def run(self):

global login_r, login_w, password_r, password_w

login_r, login_w = os.pipe()
password_r, password_w = os.pipe()

wrapper = subprocess.Popen(args=('/usr/bin/special/wrapper'),
preexec_fn=pipes_function, bufsize=0, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

os.close(login_r)
os.close(password_r)

login_w = os.fdopen(login_w, 'w')
password_w = os.fdopen(password_w, 'w')

login_w.write(self.username)
login_w.close()

password_w.write(self.password)
password_w.close()

self.command = self.command.encode()
wrapper.stdin.write(self.command)
wrapper.stdin.close()
self.stdout = wrapper.stdout.read()
self.stderr = wrapper.stderr.read()

self.returned = wrapper.wait()
return self.returned


  My problem is that when I run nose test for function witch use this
broker i get followin errors (I test Pylons app):

 File "/home/users/matrix/johny/Pylons/zhradmin/zhradmin2/zhradmin2/
controllers/account.py",
line 51, in login
   account.login()
 File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/model/
account.py",
line 29, in login
 File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/lib/
brokerprocess.py",
line 41, in run
 File "/usr/local/lib/python2.5/subprocess.py", line 594, in __init__
   errread, errwrite)
 File "/usr/local/lib/python2.5/subprocess.py", line 1091, in
_execute_child
   raise child_exception
OSError: [Errno 9] Bad file descriptor

Is anyone know why I get error ?
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-26 Thread Steve Holden
Paul Rubin wrote:
> Steve Holden  writes:
>> Quite. Python is a language "for consenting adults". 
> 
> Shouldn't such a language allow consenting adults to enter a BDSM
> scene without being moralized at, if that's what they want to do? ;-)

Yes, but you know what moralizers are like ...

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 14:43 -0800, J Kenneth King wrote:
> Linuxguy123  writes:
> 
> > I just started using python last week and I'm addicted.
> >
> > I hate Perl.  I never did learn to use it with any competence.  I has to
> > be the most obfuscated, cryptic language I've ever seen.  Making it
> > "object oriented" only makes it worse !
> > ..  ..
> 
> I program full-time in Python, so I share your excitement and
> enthusiasm. But bashing Perl like that doesn't make you sound very
> smart. I'm probably one of the very few Python programmers who came from
> (and still occassionally) use Perl. 

Really?

I think many many python programmers cut their teeth on Perl.  I for
one.  I loved programming in it when I did, but I hate having to try to
understand OPP.  Now, when I deal with Perl, it's mostly legacy code,
and it's a miserable experience.

> I've written non-trivial programs in
> it and from my experience I can tell you that it's actually a great
> language. The Moose object system is well beyond Python's class
> system. But I guess you wouldn't know that.
> 
> So yay for Python, but don't get in the habit of criticising that which
> you do not know.

There are legitimate reasons to criticize things even when they are
powerful.

Cheers,
Cliff


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


Web services

2009-01-26 Thread loial
I am trying to learn about web services and how to interface with a
3rd party web service from python.

Can anyone point me at an idiots guide/tutorial for someone who is new
to web services?

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


Method returning an Iterable Object

2009-01-26 Thread Anjanesh Lekshminarayanan
Is there a way to return an iterable object ?

class twoTimes:
def __init__(self, n):
self.__n = n

def getNext():
self.__n *= 2
return self.__n


t = twoTimes(5)
while (n in t.getNext()): # while (n in t):
print (n)

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


Re: Method returning an Iterable Object

2009-01-26 Thread Steve Holden
Anjanesh Lekshminarayanan wrote:
> Is there a way to return an iterable object ?
> 
> class twoTimes:
> def __init__(self, n):
> self.__n = n
> 
> def getNext():
> self.__n *= 2
> return self.__n
> 
> 
> t = twoTimes(5)
> while (n in t.getNext()): # while (n in t):
> print (n)
> 
Sure:

class EveryOther:
def __init__(self, seq):
self.seq = seq self.idx = 0
def next(self):
self.idx += 1
if self.idx >= len(self.seq):
raise StopIteration
value = self.seq[self.idx]
self.idx += 1
return value
def __iter__(self):
return self


print [x for x in EveryOther(range(10))
[1, 3, 5, 7, 9]

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
> Want to change the type/behavior of an object from class A to class
> B?  How about this:
> 
> aobj = A()
> aobj.__class__ = B
> 
> Try *that* in as simple-looking C++ or Java!

Wow.  That looks very powerful and fun.  But scary.  Any thoughts on how
you would use that in a way that wouldn't unleash sulphurous code
smells?

Cheers,
Cliff


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


Re: Method returning an Iterable Object

2009-01-26 Thread Andreas Waldenburger
On Mon, 26 Jan 2009 22:01:21 +0530 Anjanesh Lekshminarayanan
 wrote:

> Is there a way to return an iterable object ?
> 
> class twoTimes:
> def __init__(self, n):
> self.__n = n
> 
> def getNext():
> self.__n *= 2
> return self.__n
> 
> 
Rename getNext() to next() and create another method named __iter__()
that just returns self. TwoTimes is now an iterator.

You can also replace the whole class with a function thusly:

def two_times(n):
for k in itertools.count(1):
yield n * (2**k)

This function is then called a generator (because it generates an
iterator). You can now say

infinitely_doubling_numbers = two_times(2)
for number in in infinitely_doubling_numbers:
print number

to the same effect as the iterator version above.

python.org seems uncooperative at the moment, but look up iterators or
the iterator protocol and generators if it works for you.


> t = twoTimes(5)
> while (n in t.getNext()): # while (n in t):
> print (n)
> 
You are aware that this is an infinite loop, as is my example above BTW?
(Probably just an example, but I ask just in case.)

Also, could it be that you mean "for n in t"?

regards
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


Re: USB in python

2009-01-26 Thread Grant Edwards
On 2009-01-26, Lie Ryan  wrote:

> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little 
> Hardware Experience). High pitched sound (or anything in sound-ology that 
> means high voltage) means the device is on and low pitched sound off.

 1) Pitch has nothing to do with voltage.  A high-pitch sound
and a low pitch sound can have the exact same voltage.

 2) The OP's device requires quite a bit of power.  There is
almost no power available from the line-out jack, and the
voltage is limited to about 1V.  If his sound card has a
power-amp (none do these days), he might be able to get a
usable amount of power.

> The device will need an additional transistor to separate low
> voltage from the high voltage.

He'll need more than a transistor.  He needs a power supply,
some sort of rectifier/detector, and a comparitor. It would be
more interesting to use notch filters to detect different
frequencies so that you could have multiple output "bits".

> I don't know how much power can be pulled from jack out,

Almost none, and what's there is very low voltage.

> but for a home brewn device it is still feasible to draw power
> from USB and signal from jack out.

It would probably be easier to buy a USB-parallel port chip.
Then he's got power from the USB bus and something like 14
parallel I/O pins he can control.  Alternatively A USB-serial
chip will provide 2 outputs and 4 inputs.

-- 
Grant Edwards   grante Yow! Not SENSUOUS ... only
  at   "FROLICSOME" ... and in
   visi.comneed of DENTAL WORK ... in
   PAIN!!!
--
http://mail.python.org/mailman/listinfo/python-list


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

2009-01-26 Thread Bruno Desthuilliers

Russ P. a écrit :
(snip)

You are trying to dictate that the library implementer not be allowed
to use enforced access restriction. And, in the larger sense, you are
trying to dictate that access restrictions not be enforced in Python.


FWIW, it's actually *you* who are trying to dictate that access 
restrictions should be enforced in Python.


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


Re: What is intvar? [Python Docs]

2009-01-26 Thread W. eWatson

r wrote:

W. eWatson,

I contacted the author of New Mexico Techs "Introduction to Tkinter" a
couple of weeks ago. He is going to update the reference material with
a few missing widgets and some info on Photo and Bitmap classes. I
really love the NMT layout and use it quite often. Fredricks
Tkinterbook is more detail but lacking in navigation. I swing back and
forth between both sites.





Good. Thanks. I think Lundh might have taken his material to book form.

I might be repeating myself here, but If anyone has pdf experience, and 
could provide page numbers and maybe a TOC for some of Lundh's 
contributions, that would be helpful.


--
   W. eWatson

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: 

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


Re: Python C API (PyObject_CallMethod clear object)

2009-01-26 Thread Gabriel Genellina
En Mon, 26 Jan 2009 08:47:31 -0200,   
escribió:

On 26 Jan., 03:25, "Gabriel Genellina"  wrote:
En Sun, 25 Jan 2009 23:46:01 -0200,  
 escribió:


> I have a problm with deallocating stuff. I call a function with this
> command:

> PyObject *rvalue = PyObject_CallMethod(obj, "execute","",NULL);

> if(rvalue==NULL)
>     PyErr_Print();
> else
>     Py_DECREF(rvalue);


Yes, you are right. I got this issue not in syntax error, I get it on
a runtime error like this:

t = MyImage()
self.test("hallo")   #test takes an integer so this would throw an
exception.

Here you see, an object of the type MyImage() was created but its not
deleted. 't' is a local reference so how can I do that?


It will be deleted after the last reference to it is released, as every  
object in Python. If the above two lines are the whole function code, this  
will happen when exiting the function. If some other object holds a  
reference to this MyImage instance (by example, by doing xxx.attribute =  
t, or inserting it in a list) then that won't happen until all those  
references are released.
Note that, in Python 2.x, the traceback object holds a reference to all  
local variables -- if you store the traceback somewhere, those objects  
won't be deleted.


--
Gabriel Genellina

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Gabriel Genellina
En Mon, 26 Jan 2009 13:35:39 -0200, J. Cliff Dyer   
escribió:

On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote:

On Jan 26, 1:03 pm, "Gabriel Genellina" 
wrote:
> En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase
>  escribió:



> > I suppose if I were really smart, I'd dig a little deeper in the CSV
> > module to sniff out the "right" way to parse tab-delimited files.
>
> It's so easy that don't doing that is just inexcusable lazyness :)
> Your own example, written using the csv module:
>
> import csv
>
> f = csv.reader(open('customer_x.txt','rb'), delimiter='\t')
> headers = f.next()
> for line in f:
>  field1, field2, field3 = line
>  do_stuff()
>

And where in all of that do you recommend that .decode(some_encoding)
be inserted?


If encoding is an issue for your application, then I'd recommend you use
codecs.open('customer_x.txt', 'rb', encoding='ebcdic') instead of open()


This would be the best way *if* the csv module could handle Unicode input,  
but unfortunately this is not the case. See my other reply.


--
Gabriel Genellina

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Gabriel Genellina
En Mon, 26 Jan 2009 13:35:39 -0200, J. Cliff Dyer   
escribió:

On Sun, 2009-01-25 at 18:23 -0800, John Machin wrote:

On Jan 26, 1:03 pm, "Gabriel Genellina" 
wrote:
> En Sun, 25 Jan 2009 23:30:33 -0200, Tim Chase
>  escribió:



> > I suppose if I were really smart, I'd dig a little deeper in the CSV
> > module to sniff out the "right" way to parse tab-delimited files.
>
> It's so easy that don't doing that is just inexcusable lazyness :)
> Your own example, written using the csv module:
>
> import csv
>
> f = csv.reader(open('customer_x.txt','rb'), delimiter='\t')
> headers = f.next()
> for line in f:
>  field1, field2, field3 = line
>  do_stuff()
>

And where in all of that do you recommend that .decode(some_encoding)
be inserted?


If encoding is an issue for your application, then I'd recommend you use
codecs.open('customer_x.txt', 'rb', encoding='ebcdic') instead of open()


This would be the best way *if* the csv module could handle Unicode input,  
but unfortunately this is not the case. See my other reply.


--
Gabriel Genellina

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


Re: Web services

2009-01-26 Thread Chris Rebert
On Mon, Jan 26, 2009 at 8:11 AM, loial  wrote:
> I am trying to learn about web services and how to interface with a
> 3rd party web service from python.
>
> Can anyone point me at an idiots guide/tutorial for someone who is new
> to web services?

The XML-RPC client module in the std lib (xmlrpclib) includes an
example: http://docs.python.org/library/xmlrpclib.html#example-of-client-usage
And the XML-RPC server server in the stdlib also includes a full
client-server example:
http://docs.python.org/library/simplexmlrpcserver.html#simplexmlrpcserver-example

Cheers,
Chris

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


Re: I'm a python addict !

2009-01-26 Thread Paul McGuire
On Jan 26, 10:54 am, "J. Cliff Dyer"  wrote:
> On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
> > Want to change the type/behavior of an object from class A to class
> > B?  How about this:
>
> >     aobj = A()
> >     aobj.__class__ = B
>
> > Try *that* in as simple-looking C++ or Java!
>
> Wow.  That looks very powerful and fun.  But scary.  Any thoughts on how
> you would use that in a way that wouldn't unleash sulphurous code
> smells?
>

This technique is perfect for implementing the GoF State pattern.

In the State pattern, you implement behavior for an object's various
states using one of several interchangeable classes.  The classes are
"interchangeable" in that they all implement a common interface.  Here
is my favorite State pattern example, a traffic light:


import time

class TrafficLight(object):
pass

class RedLight(TrafficLight):
cars_can_go = False
pedestrians_can_cross = True
color = (255,0,0)
duration = 20

class YellowLight(TrafficLight):
cars_can_go = True
pedestrians_can_cross = False
color = (255,255,0)
duration = 5

class GreenLight(TrafficLight):
cars_can_go = True
pedestrians_can_cross = False
color = (0,255,0)
duration = 15

# now add in next_state class vars for state transitions
RedLight.next_state = GreenLight
YellowLight.next_state = RedLight
GreenLight.next_state = YellowLight
TrafficLight.initial_state = RedLight

# run a traffic light for a while...
can_they = lambda cond : ("can't","can")[cond]
light = TrafficLight.initial_state()
while 1:
print light.__class__.__name__
print "waiting for", light.duration, "seconds"
print "Cars", can_they(light.cars_can_go), "go"
print "People", can_they(light.pedestrians_can_cross), "cross"
print
time.sleep(light.duration)

# how you have to do it in C++ and Java
# light = light.next_state()

# using Python
light.__class__ = light.next_state


Gives this output:

RedLight
waiting for 20 seconds
Cars can't go
People can cross

GreenLight
waiting for 15 seconds
Cars can go
People can't cross

YellowLight
waiting for 5 seconds
Cars can't go
People can't cross

RedLight
waiting for 20 seconds
Cars can't go
People can cross

... and so on ...



In Python, the base TrafficLight class isn't even necessary ("don't
need no stinking interfaces!"), although it is a good place to define
default behavior, and it helps tie together the other classes from a
self-documentation standpoint.  But any class that has the necessary
attributes would suffice, whether it inherits from TrafficLight or
not.

class HoldForEmergencyVehiclesLight(object):
cars_can_go = False
pedestrians_can_cross = False
color = (255,0,0)


-- Paul

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


Process crash with no reason

2009-01-26 Thread gil . shinar
Hi All,

I'm running a program that is acting as a nice interface to sybase'
replication server. The program is using the cherrypy web service for
the GUI. The process is crashing every few days with no reason. In the
log I can see INFO and DEBUG (No ERROR) log lines and I do not get any
TraceBack python's message. This program is running on solaris 9
machine.
Where can I see or what can I do in order to find out what causes the
process to crash?
I have tried simulating a "traceBack" message and I could see this
traceback message in one of the log files I'm using. When the process
crashes without my help, I don't have a clue.
Let me know if you need any other info

Thanks

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


Re: Dynamic methods and lambda functions

2009-01-26 Thread Kay Schluehr
On 26 Jan., 15:13, Steve Holden  wrote:
> Mark Wooding wrote:
> > unine...@gmail.com writes:
> [...]
> >   * Assignment stores a new (reference to a) value in the variable.
>
> >   * Binding modifies the mapping between names and variables.
>
> I realise I have omitted what was doubtless intended to be explanatory
> detail, but I am having trouble reconciling those sentences. Would you
> mind explaining "in vacuuo" what you see as the difference between
> assignment and binding?
>
> regards
>  Steve
> --
> Steve Holden+1 571 484 6266   +1 800 494 3119
> Holden Web LLC  http://www.holdenweb.com/

"Assignment" is binding values to names whereas "binding" is binding
names to scopes. Marks terminology is a bit more standard than Pythons
in this respect. As you know, Python avoids the idea of variables as
if those were storage cells having a fixed logical address.
--
http://mail.python.org/mailman/listinfo/python-list


unable to print Unicode characters in Python 3

2009-01-26 Thread jefm
Hi,
while checking out Python 3, I read that all text strings are now
natively Unicode.
In the Python language reference (http://docs.python.org/3.0/reference/
lexical_analysis.html) I read that I can show Unicode character in
several ways.
"\u" supposedly allows me to specify the Unicode character by hex
number and the format  "\N{name}" allows me to specify by Unicode
name.
Neither seem to work for me.
What am I doing wrong ?

Please see error output below where I am trying to show the EURO sign
(http://www.fileformat.info/info/unicode/char/20ac/index.htm):

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('\u20ac')
Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python30\lib\io.py", line 1491, in write
b = encoder.encode(s)
  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
position 0: character maps to 
>>>
>>> print ("\N{EURO SIGN}")
Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python30\lib\io.py", line 1491, in write
b = encoder.encode(s)
  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
position 0: character maps to 
--
http://mail.python.org/mailman/listinfo/python-list


Re: Method returning an Iterable Object

2009-01-26 Thread Anjanesh Lekshminarayanan
> You can also replace the whole class with a function thusly:
>
>def two_times(n):
>for k in itertools.count(1):
>yield n * (2**k)
>
> This function is then called a generator (because it generates an
> iterator). You can now say
>
>infinitely_doubling_numbers = two_times(2)
>for number in in infinitely_doubling_numbers:
>print number

Oh..this is new. Will checkup itertools. Thanks.

>> t = twoTimes(5)
>> while (n in t.getNext()): # while (n in t):
>> print (n)
>>
> You are aware that this is an infinite loop, as is my example above BTW?
> (Probably just an example, but I ask just in case.)

I was aware this was an infinite loop - just didnt want to put more
code for an example.
--
http://mail.python.org/mailman/listinfo/python-list


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread Martin
Hmm this works for me,

it's a self compiled version:

~ $ python3
Python 3.0 (r30:67503, Dec 29 2008, 21:35:15)
[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("\u20ac")
€
>>> print ("\N{EURO SIGN}")
€
>>>


2009/1/26 jefm :
> What am I doing wrong ?

"\N{EURO SIGN}".encode("ISO-8859-15") ## could be something but I'm
pretty sure I'm totally wrong on this


-- 
http://soup.alt.delete.co.at
http://www.xing.com/profile/Martin_Marcher
http://www.linkedin.com/in/martinmarcher

You are not free to read this message,
by doing so, you have violated my licence
and are required to urinate publicly. Thank you.

Please avoid sending me Word or PowerPoint attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html
--
http://mail.python.org/mailman/listinfo/python-list


Re: Process crash with no reason

2009-01-26 Thread Philip Semanchuk


On Jan 26, 2009, at 1:13 PM, gil.shi...@gmail.com wrote:


Hi All,

I'm running a program that is acting as a nice interface to sybase'
replication server. The program is using the cherrypy web service for
the GUI. The process is crashing every few days with no reason. In the
log I can see INFO and DEBUG (No ERROR) log lines and I do not get any
TraceBack python's message. This program is running on solaris 9
machine.
Where can I see or what can I do in order to find out what causes the
process to crash?
I have tried simulating a "traceBack" message and I could see this
traceback message in one of the log files I'm using. When the process
crashes without my help, I don't have a clue.
Let me know if you need any other info



Although Python isn't immune to fatal errors like you describe, I'd  
immediately suspect a 3rd-party module instead, esp. one written in C  
or C++. Are you using anything like that?



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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread Benjamin Kaplan
On Mon, Jan 26, 2009 at 1:16 PM, jefm  wrote:

> Hi,
> while checking out Python 3, I read that all text strings are now
> natively Unicode.
> In the Python language reference (http://docs.python.org/3.0/reference/
> lexical_analysis.html)
> I read that I can show Unicode character in
> several ways.
> "\u" supposedly allows me to specify the Unicode character by hex
> number and the format  "\N{name}" allows me to specify by Unicode
> name.
> Neither seem to work for me.
> What am I doing wrong ?
>
> Please see error output below where I am trying to show the EURO sign
> (http://www.fileformat.info/info/unicode/char/20ac/index.htm):
>
> Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> print('\u20ac')
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "c:\python30\lib\io.py", line 1491, in write
>b = encoder.encode(s)
>  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
>return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
> position 0: character maps to 
> >>>
> >>> print ("\N{EURO SIGN}")
> Traceback (most recent call last):
>  File "", line 1, in 
>  File "c:\python30\lib\io.py", line 1491, in write
>b = encoder.encode(s)
>  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
>return codecs.charmap_encode(input,self.errors,encoding_map)[0]
> UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
> position 0: character maps to 
> --
>  



The strings are represented internally as Unicode, but you can't print them
that way. When you call print(), python needs to turn your string into a
sequence of bytes that are then interpreted by the terminal (in your case
cmd.exe). On modern Unix-based systems (like Mac and Linux), the console
uses UTF-8. Since it uses a unicode-based encoding, everything prints fine.
Your Windows machine seems determined to use the old (c. 1980s) CP 437. Code
Page 437 doesn't have the Euro symbol, so python throws an error. Try using
"\N{EURO SIGN}".encode("cp1252"). If your console still can't handle it,
you'll need to change its encoding.
--
http://mail.python.org/mailman/listinfo/python-list


Python and CUDO

2009-01-26 Thread fredrik kant
Hi!
Sorry about the misspelling, it should of course been "NIVIDAS CUDA".
I also noticed that there wrappers around such as: pycuda which answers my
question.

-- 
Fredrik Kant

Kant Consulting AB
Mobile: +46 70 787 06 01
www.kantconsulting.se
--
http://mail.python.org/mailman/listinfo/python-list


Re: v = json.loads("{'test':'test'}")

2009-01-26 Thread Miles
On Mon, Jan 26, 2009 at 4:06 AM, Diez B. Roggisch wrote:
> There are people who say something along the lines of "be strict when
> writing, and tolerant when reading" (the exact quote is different, but
> neither google:~site:mybrain nor any other have helped me here)

That's Postel's Law:
http://en.wikipedia.org/wiki/Robustness_Principle

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


Re: Plugin system, RuntimeWarning: Parent module 'ext_abc' not found while handling absolute import

2009-01-26 Thread Torsten Mohr
Hello,

>> Basically, this here works but gives a warning:
>> RuntimeWarning: Parent module 'ext_abc' not found while handling
>> absolute import
> 
> 
>> here = os.path.abspath('.')
> 
> (Unrelated to the main question, but you probably want to use
> os.path.dirname(os.path.abspath(__file__)) instead - the above just
> returns the current directory, which might not be the directory containing
> the module)

Thanks, i'll try that.

>> mpath = os.path.abspath('mymodule')
>> epath = os.path.abspath('extension')
>>
>> sys.path.append(here)
>> sys.path.append(mpath)
>>
>> FILE mymodule/__init__.py:
> 
> So mymodule is actually a package. Packages should *not* appear in
> sys.path.

Oh, how does it find modules then?  I thought that would be PYTHONPATH or
sys.path ?


Best regards,
Torsten.

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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread jefm
>Hmm this works for me,
>it's a self compiled version:
>~ $ python3
>Python 3.0 (r30:67503, Dec 29 2008, 21:35:15)
>[GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2

You are running on Linux. Mine is on Windows.
Anyone else have this issue on Windows ?

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


Re: A java hobbyist programmer learning python

2009-01-26 Thread Scott David Daniels

Tim Rowe wrote:

... I like the second style because it makes it leaves the 2-d
implementation hidden, which is the whole point of encapsulation.

I like the second as well, in that it it allows the parent to update any
related data structures (for example, updating a display).  However, I
am a bit nervous about changing the signature of the move message, and
would be more tempted to add a move3d, for multiple inheritance reasons.


But then I am a fan of Hungarian notation, which many
programmers can't stand.


Is it that programmers can't stand it, or is it that they can't stand
it when it's imposed when not needed? 

Well, I tend to be one who hates Hungarian.  I spent a long time being
a fan of strong typing.  Then, I felt the compiler would enforce the
typing, and the Hungarian muddied the readability of the expressions.
I felt that you should define a type for distance_in_yards, and not have
that confusable with distance_in_meters (though you might provide an
automatic conversion.

Now, I feel strong-typing systems cannot express the type relationships
that I want as a programmer in the face of sub-typing, and I hold
no hope that (A) such a language will appear, or (B) that the strong
majority of the bugs I produce in Python will be caught by a statically
typed system.  I do however, still find the neddling little text
annotations in the variable naming to be irritating noise that distracts
from the readability of the expressions.

The kinds of type systems that I want(ed) is most closely expressed by
F-Bounded polymorphism, and that doesn't manage to be both expressive
enough and computationally feasible to use as a production type system.
For example, I'd like to type a sort function or a project function,
and all I've seen looks like its fit will feel more like a suit of armor
than a shirt.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Method returning an Iterable Object

2009-01-26 Thread Andreas Waldenburger
On Tue, 27 Jan 2009 00:05:53 +0530 Anjanesh Lekshminarayanan
 wrote:

> > You can also replace the whole class with a function thusly:
> >
> >def two_times(n):
> >for k in itertools.count(1):
> >yield n * (2**k)
> >
> > This function is then called a generator (because it generates an
> > iterator). You can now say
> >
> >infinitely_doubling_numbers = two_times(2)
> >for number in in infinitely_doubling_numbers:
> >print number
> 
> Oh..this is new. Will checkup itertools. Thanks.
> 
OK, happy I could help. But my point was the yield statement (in case
you didn't know about yield).

regards
/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


Re: I'm a python addict !

2009-01-26 Thread Scott David Daniels

J. Cliff Dyer wrote:

On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:

... How about this:
aobj = A()
aobj.__class__ = B

... Wow.  That looks very powerful and fun.  But scary.  Any thoughts
on how you would use that in a way that wouldn't unleash sulphurous
code smells?


Seems like a nice way to transition between "ActiveAccount": and
"InActiveAccount" or some such.  The requirement is that the two
classes share memory layout (essentially, class type and all __slots__
involved (including __dict__).  It is a bit prickly, so I'd personally
only start to use it once my methods were littered with lots of:
...
if self.active_flag:
self.account.update(...
else:
raise ValueError('Purchase prohibitted')

Could also be useful as the stub for a kept-on-disk structure,
where all methods on the inactive one rolled in data switched class,
and invoked their corresponding method.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 09:52 -0800, Paul McGuire wrote:
> On Jan 26, 10:54 am, "J. Cliff Dyer"  wrote:
> > On Fri, 2009-01-23 at 20:25 -0800, Paul McGuire wrote:
> > > Want to change the type/behavior of an object from class A to class
> > > B?  How about this:
> >
> > > aobj = A()
> > > aobj.__class__ = B
> >
> > > Try *that* in as simple-looking C++ or Java!
> >
> > Wow.  That looks very powerful and fun.  But scary.  Any thoughts on how
> > you would use that in a way that wouldn't unleash sulphurous code
> > smells?
> >
> 
> This technique is perfect for implementing the GoF State pattern.
> 
> In the State pattern, you implement behavior for an object's various
> states using one of several interchangeable classes.  The classes are
> "interchangeable" in that they all implement a common interface.  Here
> is my favorite State pattern example, a traffic light:
> 
> 
> import time
> 
> class TrafficLight(object):
> pass
> 
> class RedLight(TrafficLight):
> cars_can_go = False
> pedestrians_can_cross = True
> color = (255,0,0)
> duration = 20
> 
> class YellowLight(TrafficLight):
> cars_can_go = True
> pedestrians_can_cross = False
> color = (255,255,0)
> duration = 5
> 
> class GreenLight(TrafficLight):
> cars_can_go = True
> pedestrians_can_cross = False
> color = (0,255,0)
> duration = 15
> 
> # now add in next_state class vars for state transitions
> RedLight.next_state = GreenLight
> YellowLight.next_state = RedLight
> GreenLight.next_state = YellowLight
> TrafficLight.initial_state = RedLight
> 
> # run a traffic light for a while...
> can_they = lambda cond : ("can't","can")[cond]
> light = TrafficLight.initial_state()
> while 1:
> print light.__class__.__name__
> print "waiting for", light.duration, "seconds"
> print "Cars", can_they(light.cars_can_go), "go"
> print "People", can_they(light.pedestrians_can_cross), "cross"
> print
> time.sleep(light.duration)
> 
> # how you have to do it in C++ and Java
> # light = light.next_state()
> 
> # using Python
> light.__class__ = light.next_state
> 
> 
> Gives this output:
> 
> RedLight
> waiting for 20 seconds
> Cars can't go
> People can cross
> 
> GreenLight
> waiting for 15 seconds
> Cars can go
> People can't cross
> 
> YellowLight
> waiting for 5 seconds
> Cars can't go
> People can't cross
> 
> RedLight
> waiting for 20 seconds
> Cars can't go
> People can cross
> 
> ... and so on ...
> 
> 
> 
> In Python, the base TrafficLight class isn't even necessary ("don't
> need no stinking interfaces!"), although it is a good place to define
> default behavior, and it helps tie together the other classes from a
> self-documentation standpoint.  But any class that has the necessary
> attributes would suffice, whether it inherits from TrafficLight or
> not.
> 
> class HoldForEmergencyVehiclesLight(object):
> cars_can_go = False
> pedestrians_can_cross = False
> color = (255,0,0)
> 
> 
> -- Paul
> 

Thanks.  That makes sense.  But your example creates a new instance of
the new class each time, rather than changing the class of a persistent
instance, as the original example, to which I was responding, did.

But perhaps something like:

class TrafficLight(object):
def change_light(self):
self.__class__ = next_state

and then you can persist information about the light (bulb_type in
set(['led', 'incandescent']), last_maintenance_date, location, etc.) on
the instance level, unaffected by the changing color.

Interesting stuff.

Cheers,
Cliff


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


Anoying unicode / str conversion problem

2009-01-26 Thread Hans Müller
Hi python experts,

in the moment I'm struggling with an annoying problem in conjunction with mysql.

I'm fetching rows from a database, which the mysql drive returns as a list of 
tuples.

The default coding of the database is utf-8.

Unfortunately in the database there are rows with different codings and there 
is a blob
column.

In the app. I search for double entries in the database with this code.

hash = {}
cursor.execute("select * from table")
rows = cursor.fetchall()
for row in rows:
key = "|".join([str(x) for x in row])   <- here the problem 
arises
if key in hash:
print "found double entry"

This code works as expected with python 2.5.2
With 2.5.1 it shows this error:


key = "|".join(str(x) for x in row)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u017e' in position 
3: ordinal
not in range(128)

When I replace the str() call by unicode(), I get this error when a blob column 
is being
processed:

key = "|".join(unicode(x) for x in row)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 119: 
ordinal not in
range(128)


Please help, how can I convert ANY column data to a string which is usable as a 
key to a
dictionary. The purpose of using a dictionary is to find equal rows in some 
database
tables. Perhaps using a md5 hash from the column data is also an idea ?

Thanks a lot in advance,

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


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

2009-01-26 Thread Russ P.
On Jan 26, 1:07 am, Bruno Desthuilliers  wrote:

> No. I can change the *team's* code. Please *read*. "team's ownership",
> ok ? Or do I have to spell it out loud ? TEAM'S OWNERSHIP. Uh. You get
> the message, now ?

Team ownership doesn't necessarily mean that you can just change code
at will. In industry, teams usually have a leader that you need to
check with before you can change an interface. A language with
enforced access restriction merely provides language support for such
coordination. That was my only point.

> > Would you give all those developers your password to get into the
> > system? No? Wait a minute ... you mean you wouldn't "trust" them with
> > your password? But what about "openness"? Are you some sort of fascist
> > or what?
>
> Goodwin point. You loose. Good bye again, Mr P.

You missed the point once again. In asking if you are a "fascist," I
was *parodying* your attitude that languages with enforced access
restrictions are for "fascists" who don't trust their co-workers or
employees. [I don't recall if you actually used that word or if it was
someone else, but you did use "B&D", which carries the same general
impression.]

So I parodied your hyperbole, and you dismiss me for it. Without
realizing it, you just dismissed yourself, sir. Thanks for saving me
the trouble.
--
http://mail.python.org/mailman/listinfo/python-list


Re: is None vs. == None

2009-01-26 Thread Terry Reedy

Steve Holden wrote:

Terry Reedy wrote:



In 2.x, the *names* 'True' and 'False' can be rebound because bool is
new and people write
try:
  False,True
except NameError:
  False,True = 0,1

to make code back compatible.


I would claim that the ability to rebind True and False is a simple bug,
though one not likely to be fixed in an 2.x release. The code above
doesn't rebind True and False in interpreters that have them ...


In pre-bool 2.x, people never wrote the above but sometime wrote
  False,True = 0,1

To me it is hardly a bug to not gratuitously break substantial amounts 
of proper code.



Back before rebinding 'None' was prohibited, 'is None' was not
completely guaranteed either (absent reading the rest of a file to be
sure no rebinding would be done).


And that was a bug too, in this case one that *was* removed in 2.4, I
believe. Don't have 2.3 lying around just now.


Unlike with True and False, the devs could not think of or find any 
proper use case for rebinding None and judged that the new prohibition 
would break very little if any code.  As far as I know, they were correct.


tjr

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


Re: I'm a python addict !

2009-01-26 Thread Paul McGuire
On Jan 26, 2:06 pm, "J. Cliff Dyer"  wrote:
>
> Thanks.  That makes sense.  But your example creates a new instance of
> the new class each time, rather than changing the class of a persistent
> instance, as the original example, to which I was responding, did.
>

Look closer.  The line that creates a new instance is commented out,
with the notation "how you have to do it in C++ and Java".  The actual
Python code is just below, and just assigns a new class to
self.__class__, as in the original example.

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


Re: Anoying unicode / str conversion problem

2009-01-26 Thread Benjamin Kaplan
On Mon, Jan 26, 2009 at 3:21 PM, Hans Müller  wrote:

> Hi python experts,
>
> in the moment I'm struggling with an annoying problem in conjunction with
> mysql.
>
> I'm fetching rows from a database, which the mysql drive returns as a list
> of tuples.
>
> The default coding of the database is utf-8.
>
> Unfortunately in the database there are rows with different codings and
> there is a blob
> column.
>
> In the app. I search for double entries in the database with this code.
>
> hash = {}
> cursor.execute("select * from table")
> rows = cursor.fetchall()
> for row in rows:
>key = "|".join([str(x) for x in row])   <- here the problem
> arises
>if key in hash:
>print "found double entry"
>
> This code works as expected with python 2.5.2
> With 2.5.1 it shows this error:
>
>
> key = "|".join(str(x) for x in row)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u017e' in
> position 3: ordinal
> not in range(128)
>
> When I replace the str() call by unicode(), I get this error when a blob
> column is being
> processed:
>
> key = "|".join(unicode(x) for x in row)
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 119:
> ordinal not in
> range(128)
>
>
> Please help, how can I convert ANY column data to a string which is usable
> as a key to a
> dictionary. The purpose of using a dictionary is to find equal rows in some
> database
> tables. Perhaps using a md5 hash from the column data is also an idea ?


unicode takes an optional encoding argument. If you don't specify, it uses
ascii. Try using (untested):

key = u"|".join(unicode(x, encoding="utf-8") for x in row)


> Thanks a lot in advance,
>
> Hans.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread Michael Torrie
jefm wrote:
>> Hmm this works for me,
>> it's a self compiled version:
>> ~ $ python3
>> Python 3.0 (r30:67503, Dec 29 2008, 21:35:15)
>> [GCC 4.2.4 (Ubuntu 4.2.4-1ubuntu3)] on linux2
> 
> You are running on Linux. Mine is on Windows.
> Anyone else have this issue on Windows ?


As Benjamin Kaplin said, Windows terminals use the old cp1252 character
set, which cannot display the euro sign. You'll either have to run it in
 something more modern like the cygwin rxvt terminal, or output some
other way, such as through a GUI.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Monitor a FTP site for arrival of new/updated files

2009-01-26 Thread Giampaolo Rodola'
On 25 Gen, 21:11, Steve Holden  wrote:
> pyt...@bdurham.com wrote:
> >  Any suggestions on a best practice way to monitor a remote FTP site for
> > the arrival of new/updated files? I don't need specific code, just some
> > coaching on technique based on your real-world experience including
> > suggestions for a utility vs. code based solution.
>
> > My goal is to maintain a local collection of files synced with a remote
> > FTP site and when I download a new/updated file locally, run a script to
> > process it. The arrival and format of the files that I need to sync with
> > are beyond my control (eliminating a rsync solution) ... all I have is a
> > generic FTP connection to a specific FTP address. Note: The remote site
> > I'm monitoring may have multiple uploads occuring at the same time.
>
> > My basic strategy is to poll the remote directory on a regular basis and
> > compare the new directory listing to a previous snapshot of the
> > directory listing. If a file timestamp or size has changed (or a new
> > file has appeared), then track this file as a changed file. Once a file
> > has been marked as changed, wait  polling cycles for the file
> > timestamp and size to remain stable, then download it, and trigger a
> > local script to process the file. In addition to detecting new or
> > changed files, I would compare remote directory listings to my local
> > sync folder and delete local files that no longer exist on the remote site.
>
> > My concern about using a utility is the utility's ability to detect when
> > a remote file has finished being updated. I don't want to download files
> > that are still in the process of being updated - I only want to download
> > new/updated files after they've been closed on the remote site.
>
> > Any ideas appreciated!
>
> Well, the ftpmirror will cope with most of what you want to do as it is,
> but I am unsure how you can determine whether a file is in the process
> of being written on the server.
>
> regards
>  Steve
> --
> Steve Holden        +1 571 484 6266   +1 800 494 3119
> Holden Web LLC              http://www.holdenweb.com/- Nascondi testo citato
>
> - Mostra testo citato -

If you're going to do that manually through a script, and the server
supports it, use MLSD instead of LIST command for listing files and
determine their last modification time.

My 2 cents

--- Giampaolo
http://code.google.com/p/pyftpdlib
--
http://mail.python.org/mailman/listinfo/python-list


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread Terry Reedy

jefm wrote:

Hi,
while checking out Python 3, I read that all text strings are now
natively Unicode.


True


In the Python language reference (http://docs.python.org/3.0/reference/
lexical_analysis.html) I read that I can show Unicode character in
several ways.
"\u" supposedly allows me to specify the Unicode character by hex
number and the format  "\N{name}" allows me to specify by Unicode
name.


These are ways to *specify* unicode chars on input.


Neither seem to work for me.


If you separate text creation from text printing, you would see that 
they do.  Try

s='\u20ac'
print(s)


What am I doing wrong ?


Using the interactive interpreter running in a Windows console.


Please see error output below where I am trying to show the EURO sign
(http://www.fileformat.info/info/unicode/char/20ac/index.htm):

Python 3.0 (r30:67507, Dec  3 2008, 20:14:27) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

print('\u20ac')

Traceback (most recent call last):
  File "", line 1, in 
  File "c:\python30\lib\io.py", line 1491, in write
b = encoder.encode(s)
  File "c:\python30\lib\encodings\cp437.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in
position 0: character maps to 


With the standard console, I get the same.  But with IDLE, using the 
same Python build but through a different interface


>>> s='\u20ac'
>>> len(s)
1
>>> str(s)
'€' # euro sign

I have fiddled with the shortcut to supposed make it work better as 
claimed by posts found on the web, but to no avail.  Very frustrating 
since I have fonts on the system for at least all of the first 64K 
chars.  Scream at Microsoft or try to find or encourage a console 
replacement that Python could use.  In the meanwhile, use IDLE.  Not 
perfect for Unicode, but better.


Terry Jan Reedy

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


Re: Pipe stdout && stderr to a TkLabel widget

2009-01-26 Thread rantingrick
OK, here is a simple example that will show you what i want to do.
Right now if you type print 'hello' in the entry and press  you
will see braces in the label "{}". But if you type sys.stdou.write
("hello") you will see "{hello}" in the label. So i got the stdout
piping to the widget now, but it will not work with the print
statement. AND there are those braces around the string??? Any ideas?

from Tkinter import *
root = Tk()

class NewOut():
def write(self, *arg):
v.set(arg)

saveout = sys.stdout
newout = NewOut()
sys.stdout = newout

e = StringVar(root)
entry = Entry(root, textvariable=e, font=('Courier New', 12))
entry.pack(fill=X, expand=1, padx=5, pady=5)

v = StringVar(root)
Label(textvariable=v).pack(fill=X, expand=1)

def onReturn(event):
try:
exec(e.get())
e.set('')
except:
print 'Command Invalid'

root.bind('', onReturn)
entry.focus_set()
root.mainloop()
--
http://mail.python.org/mailman/listinfo/python-list


Re: is None vs. == None

2009-01-26 Thread Steve Holden
Terry Reedy wrote:
> Steve Holden wrote:
>> Terry Reedy wrote:
> 
>>> In 2.x, the *names* 'True' and 'False' can be rebound because bool is
>>> new and people write
>>> try:
>>>   False,True
>>> except NameError:
>>>   False,True = 0,1
>>>
>>> to make code back compatible.
>>>
>> I would claim that the ability to rebind True and False is a simple bug,
>> though one not likely to be fixed in an 2.x release. The code above
>> doesn't rebind True and False in interpreters that have them ...
> 
> In pre-bool 2.x, people never wrote the above but sometime wrote
>   False,True = 0,1
> 
Right. This is the use case I overlooked.

> To me it is hardly a bug to not gratuitously break substantial amounts
> of proper code.
> 
I quite agree. I take it all back!

>>> Back before rebinding 'None' was prohibited, 'is None' was not
>>> completely guaranteed either (absent reading the rest of a file to be
>>> sure no rebinding would be done).
>>>
>> And that was a bug too, in this case one that *was* removed in 2.4, I
>> believe. Don't have 2.3 lying around just now.
> 
> Unlike with True and False, the devs could not think of or find any
> proper use case for rebinding None and judged that the new prohibition
> would break very little if any code.  As far as I know, they were correct.
> 
Indeed they were.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: Anoying unicode / str conversion problem

2009-01-26 Thread Peter Otten
Hans Müller wrote:

> Hi python experts,
> 
> in the moment I'm struggling with an annoying problem in conjunction with
> mysql.
> 
> I'm fetching rows from a database, which the mysql drive returns as a list
> of tuples.
> 
> The default coding of the database is utf-8.
> 
> Unfortunately in the database there are rows with different codings and
> there is a blob column.
> 
> In the app. I search for double entries in the database with this code.
> 
> hash = {}
> cursor.execute("select * from table")
> rows = cursor.fetchall()
> for row in rows:
> key = "|".join([str(x) for x in row]) <- here the problem arises
> if key in hash:
> print "found double entry"
> 
> This code works as expected with python 2.5.2
> With 2.5.1 it shows this error:
> 
> 
> key = "|".join(str(x) for x in row)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u017e' in
> position 3: ordinal not in range(128)
> 
> When I replace the str() call by unicode(), I get this error when a blob
> column is being processed:
> 
> key = "|".join(unicode(x) for x in row)
> 
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xfc in position 119:
> ordinal not in range(128)
> 
> 
> Please help, how can I convert ANY column data to a string which is usable
> as a key to a dictionary. The purpose of using a dictionary is to find
> equal rows in some database tables. Perhaps using a md5 hash from the
> column data is also an idea ?
> 
> Thanks a lot in advance,

No direct answer, but can't you put the rows into the dict (or a set)
without converting them to a string?

seen = set()
for row in rows:
if row in seen:
print "dupe"
else:
seen.add(row)


Or, even better, solve the problem within the db:

select  from  group by  having count(*) > 1

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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread jefm
>As Benjamin Kaplin said, Windows terminals use the old cp1252 character
>set, which cannot display the euro sign. You'll either have to run it in
> something more modern like the cygwin rxvt terminal, or output some
>other way, such as through a GUI.

>With the standard console, I get the same.  But with IDLE, using the
>same Python build but through a different interface

>Scream at Microsoft or try to find or encourage a console
>replacement that Python could use.  In the meanwhile, use IDLE.  Not
>perfect for Unicode, but better.


So, if I understand it correctly, it should work as long as you run
your Python code on something that can actually print the Unicode
character.
Apparently, the Windows command line can not.

I mainly program command line tools to be used by Windows users. So I
guess I am screwed.

Other than converting my tools to have a graphic interface, is there
any other solution, other than give Bill Gates a call and bring his
command line up to the 21st century ?

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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread Jean-Paul Calderone

On Mon, 26 Jan 2009 13:26:56 -0800 (PST), jefm  
wrote:

As Benjamin Kaplin said, Windows terminals use the old cp1252 character
set, which cannot display the euro sign. You'll either have to run it in
something more modern like the cygwin rxvt terminal, or output some
other way, such as through a GUI.



With the standard console, I get the same.  But with IDLE, using the
same Python build but through a different interface



Scream at Microsoft or try to find or encourage a console
replacement that Python could use.  In the meanwhile, use IDLE.  Not
perfect for Unicode, but better.



So, if I understand it correctly, it should work as long as you run
your Python code on something that can actually print the Unicode
character.
Apparently, the Windows command line can not.

I mainly program command line tools to be used by Windows users. So I
guess I am screwed.

Other than converting my tools to have a graphic interface, is there
any other solution, other than give Bill Gates a call and bring his
command line up to the 21st century ?


cp1252 can represent the euro sign 
().  Apparently the chcp command can 
be used to change the code page
active in the console 
().  I've never tried 
this myself, though.

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


Re: I'm a python addict !

2009-01-26 Thread J. Cliff Dyer

On Mon, 2009-01-26 at 12:37 -0800, Paul McGuire wrote:
> On Jan 26, 2:06 pm, "J. Cliff Dyer"  wrote:
> >
> > Thanks.  That makes sense.  But your example creates a new instance of
> > the new class each time, rather than changing the class of a persistent
> > instance, as the original example, to which I was responding, did.
> >
> 
> Look closer.  The line that creates a new instance is commented out,
> with the notation "how you have to do it in C++ and Java".  The actual
> Python code is just below, and just assigns a new class to
> self.__class__, as in the original example.
> 

Right.  Sorry about that.


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

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


Re: AttributeError: 'module' object has no attribute 'open_workbook'

2009-01-26 Thread John Machin
On Jan 27, 3:07 am, MRAB  wrote:
> Jay Jesus Amorin wrote:
[snip]
>  > Here's the error message:
>  >
>  > r...@nebuchadnezzar:/home/test/project# ./xlrd.py test.xls
>  > Traceback (most recent call last):
>  >   File "./xlrd.py", line 3, in 
>  >     import xlrd
>  >   File "/home/jayam/project/xlrd.py", line 6, in 
>  >     mySpreadsheet = xlrd.open_workbook(open(sys.argv[1]))

I concur with MRAB's diagnosis.

To answer the OP's possible next question: the first arg of
xlrd.open_workbook() is the path to the file to be opened. It won't
work with a string of data. That's what the file_contents arg is for.
Manual says:
"""
filename
The path to the spreadsheet file to be opened.
[snip]
file_contents
... as a string or an mmap.mmap object or some other behave-alike
object. If file_contents is supplied, filename will not be used,
except (possibly) in messages.
"""
So the above line of code should be:
mySpreadsheet = xlrd.open_workbook(sys.argv[1])

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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread jefm
Now that I know the problem, I found the following on Google.

Windows uses codepages to display different character sets. (http://
en.wikipedia.org/wiki/Code_page)

The Windows chcp command allows you to change the character set from
the original 437 set.

When you type on the command line:  chcp 65001
it sets your console in UTF-8 mode.
(http://en.wikipedia.org/wiki/Code_page_65001)

Unfortunately, it still doesn't do what I want. Instead of printing
the error message above, it prints nothing.

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


Re: Anoying unicode / str conversion problem

2009-01-26 Thread Hans Müller
Thanks Peter,

your answer did the trick.
I programed a lot with awk (also a very cool scripting language).
So I was focused on the concept a dictionary key has to be string
(as in awk). Since it's impossible to use a list as a dictionary
key I thought it's also impossible to use a tuple as a key.
I was wrong!
So my code will become more pythonic, much simpler and even faster!
Your suggestion to use the database is also an idea, but the actual
task is to see if some rows in (identical) tables across many
servers are missing and if so to add the missing rows.
In my post I showed a simplified code.

Again, thanks for the hint!

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


Re: unable to print Unicode characters in Python 3

2009-01-26 Thread jefm
chcp 1252 does allow me to print the EURO sign. Thanks for pointing
that out.
However, it does not show me some ALL Unicode characters. Very
frustrating.
I was hoping to find something that allows me to print any Unicode
character on the console.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Marc 'BlackJack' Rintsch
On Mon, 26 Jan 2009 16:10:11 +0100, Andreas Waldenburger wrote:

> On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch 
> wrote:
> 
>> On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote:
>> 
>> > content = a.readlines()
>> > 
>> > (Just because we can now write "for line in file" doesn't mean that
>> > readlines() is *totally* redundant.)
>> 
>> But ``content = list(a)`` is shorter.  :-)
>> 
> But much less clear, wouldn't you say?

Okay, so let's make it clearer and even shorter: ``lines = list(a)``.  :-)

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


scatterhist and resizing figures

2009-01-26 Thread perfreem
i am using scatterhist to plot some data. i find that when i plot the
data
and matlab shows it in the figure window, stretching the figure window
(with the mouse) to enlarge it actually changes the properties of the
figure.
for example, making it bigger sometimes reveals more tick marks
- like the y limit of the y axis, which i have set but was not shown
until i enlarged the window. also, more crucially enlarging can make
bars that appear at 0 or not at all to show... when i save the figure
window
as pdf, depending on which of these is shown, i get different pdfs.

here's an example:

x=rand(1, 100);
y=x+5;
scatterhist(x,y);
set(gca, 'Box' , 'off' , ...
 'LineWidth'   , 1);
set(gca , 'FontSize' , 12);
set(gca, 'FontName'   , 'Helvetica');
set(gca, 'TickDir', 'out');

first question: how can i programtically save the figure as pdf in a
way that shows maximal
info? i don't want to lose tick marks on my axis or bars in my
histogram.

second: how can i plot with scatterhist but make the scatter plot
points filled? with ordinary scatter, i can simply do: scatter(x, y,
'filled') but the 'filled' argument doesn't appear to work for
scatterhist.

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


Re: Newby: how to transform text into lines of text

2009-01-26 Thread Andreas Waldenburger
On 26 Jan 2009 22:12:43 GMT Marc 'BlackJack' Rintsch 
wrote:

> On Mon, 26 Jan 2009 16:10:11 +0100, Andreas Waldenburger wrote:
> 
> > On 26 Jan 2009 14:51:33 GMT Marc 'BlackJack' Rintsch
> >  wrote:
> > 
> >> On Mon, 26 Jan 2009 12:22:18 +, Sion Arrowsmith wrote:
> >> 
> >> > content = a.readlines()
> >> > 
> >> > (Just because we can now write "for line in file" doesn't mean
> >> > that readlines() is *totally* redundant.)
> >> 
> >> But ``content = list(a)`` is shorter.  :-)
> >> 
> > But much less clear, wouldn't you say?
> 
> Okay, so let's make it clearer and even shorter: ``lines =
> list(a)``.  :-)
> 
OK, you win. :)

/W

-- 
My real email address is constructed by swapping the domain with the
recipient (local part).
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >