Re: having problems with a multi-conditional while statement

2009-01-07 Thread Hendrik van Rooyen
 "Philip Semanchuk"  wrote:

8< nice explanation 

> Change the "and" to an "or" and you'll get the result you expected.

Also google for "De Morgan", or "De Morgan's laws"

Almost everybody stumbles over this or one of it's
corollaries at least once in their careers.

- Hendrik

-- 
With the disappearance of the gas mantle and the advent
of the short circuit, man's tranquillity began to be threatened
by everything he put his hand on.
(James Thurber.  First sentence of Sex ex Machina)


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


Re: why cannot assign to function call

2009-01-07 Thread Hendrik van Rooyen
"Mark Wooding"  wrote:

> A better analogy.  The objects are scattered across the floor.  No
> object is contained in another.  However, we have a plentiful supply of
> bits of string, each of which is tied to a Teflon-covered paperweight at
> one end and has a blob of Blu-Tack on the other.  Instead of putting
> something in a box directly, what we do is grab a piece of string, stick
> the Blu-Tack to the thing, and put the paperweight in the box.  This
> way, we can stick several bits of string to the same object and put the
> paperweights in different boxes.  Indeed, nothing stops us sticking two
> bits of string to a box, and putting both paperweights inside that same
> box.  But fitting a box into itself twice requires origami skills that
> frighten me.
> 
> The Teflon stops the Blu-Tack from sticking to the paperweights, 'cos
> you're not allowed to do that.  
> 
> There's a daemon who comes around periodically and cleans up the mess of
> paperweights which aren't in boxes, and tidies away things which don't
> have any string stuck to them any more, but really he's just an
> implementation detail and you wouldn't need him if your floor was big
> enough and you had enough bits of sufficiently long string.
> 

Lovely!

This is the nicest analogy I have seen,
and it seems to completely account
for all the observed effects.

The only other one that comes close
is Dennis L Bieber's "Wandering Names".

I propose that we name the garbage
collection demon "Steven" because
he has IMO lost this argument, and
therefore deserves to spend eternity
tied up in string.

;-)

- Hendrik

-- 
With the disappearance of the gas mantle and the advent
of the short circuit, man's tranquillity began to be threatened
by everything he put his hand on.
(James Thurber.  First sentence of Sex ex Machina)




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


Using couchdb to build a EMAIL messaging solution with threaded inline replies

2009-01-07 Thread mobiledreamers
Using couchdb to build a EMAIL messaging solution with threaded inline
replies
http://pylab.blogspot.com/search/label/couchdb
--
http://mail.python.org/mailman/listinfo/python-list


Any news on when some libraries will be ported to Python 3.0?

2009-01-07 Thread Just Another Victim of the Ambient Morality
I'm excited to use Python 3.0 (foolishly, it's the only Python 
interpreter I have on my system) but there are no libraries for it beyond 
the kitchen sink.  Personally, a good start would be Beautiful Soup and 
Mechanize.  I could also use DB.
Has there been any word on Beautiful Soup?
Has there been any word on Mechanize?
What about DB?
PIL would be nice, too, now that I think about it.
Anyway, I'd love to hear some news about any of these things in 
particular or even anything in general.  Am I the only one who's psyched for 
this version of Python?
Thank you... 


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


Re: why cannot assign to function call

2009-01-07 Thread Steven D'Aprano
On Wed, 07 Jan 2009 03:49:02 +, Mark Wooding wrote:

> Steven D'Aprano  wrote:
> 
>> The only tricky thing is that items 1, 2 and 3 can be inside two
>> different boxes at the same time. There's no obvious real world analogy
>> to that without the boxes being nested. This ability for objects to be
>> in two places at once (or even to be inside themselves!) is one of the
>> few reasons why Python's use of references in the implementation needs
>> to be mentioned.
> 
> Ahh.  So it /does/ need to be mentioned after all.  

Only in the sense that the behaviour of *real world* objects don't 
entirely match the behaviour of Python objects. If you just accept that 
Python objects can be in two places at once, an unintuitive concept I 
accept but hardly difficult to grasp, then you don't need to mention 
references.

(Of course, as a purely practical matter, the English language makes it 
difficult to avoid the word "reference" entirely. That's not my 
intention.)



> But you're wrong:
> it's not an implementation detail: it's an essential part of the
> language semantics.

Er, pardon me, but you yourself suggested that one could implement Python 
without using references to objects, "like keeping lists of clones, and 
magically updating all the clones whenever one us mutated.  That may even 
be a valid implementation for a distributed Python".

Like you suggested, it would be a seriously rotten model for standard 
Python, but it is possible. The language semantics specifies the 
*behaviour*, not the *mechanism* required to implement that behaviour.



> A better analogy.  The objects are scattered across the floor.  No
> object is contained in another.  However, we have a plentiful supply of
> bits of string, each of which is tied to a Teflon-covered paperweight at
> one end and has a blob of Blu-Tack on the other.  Instead of putting
> something in a box directly, what we do is grab a piece of string, stick
> the Blu-Tack to the thing, and put the paperweight in the box.

Not a bad analogy. I like it. But it still fails.

Why can't I stick the paperweight in the box *before* attaching the Blu-
Tack to something else, and then forget about attaching the Blu-Tack? 
There's nothing in your model to prevent dangling pointers, except hand-
waving "it doesn't work like that".

I assume the string is made of Teflon, otherwise I could stick the Blu-
Tack to another piece of string. By the same token, the floor needs to be 
Teflon too.

Why can't I grab the string by the end with the Blu-Tack and follow it 
backwards to find out where the paperweight is? I obviously know how to 
handle the end, because according to your model I'm sticking it to other 
objects. I suppose maybe there's a robot that does that Blu-Tack sticking 
for me, I just point to the object and say "That one!" and it happens. 
The string itself is invisible except to radar, which the robot has, and 
I don't. That way I can't follow the string backwards to find out where 
the paperweight is. Hmmm. This model is getting awfully complicated: 
Teflon string, Teflon paperweights, Blu-Tack, *and* a robot (presumably 
Teflon as well), none of which are visible to Python code, as well as 
objects which are.



> This
> way, we can stick several bits of string to the same object and put the
> paperweights in different boxes.  Indeed, nothing stops us sticking two
> bits of string to a box, and putting both paperweights inside that same
> box.  But fitting a box into itself twice requires origami skills that
> frighten me.

Ah, you've obviously never studied origami under Sensei Ping of the Clan 
of the Pointed Stick!


> The Teflon stops the Blu-Tack from sticking to the paperweights, 'cos
> you're not allowed to do that.
> 
> There's a daemon who comes around periodically and cleans up the mess of
> paperweights which aren't in boxes, and tidies away things which don't
> have any string stuck to them any more,

So there's a brief moment between creating the object and sticking the 
Blu-Tack on it when the daemon might take the object and the string away? 
No, I guess not, that's another thing I just have to take on faith... the 
daemon knows not to touch anything until the robot has finished with it. 

Can I tie two pieces of string together, stop the daemon from disposing 
of them? Apparently not.

Objects in two places at one time isn't sounding that weird any more.

I actually do like your model, despite poking holes in it. As a model for 
what CPython is doing under the hood, it pretty good.

But it's not a model for what a Python programmer does when writing 
Python code. There's no action equivalent to "attach blob of Blu-Tack to 
object", no action equivalent to "put the paperweight in the box". The 
robot does that when you point to an object and say "put that inside the 
box". The programmer doesn't do these things, he says "put that object in 
the box" and the robot attaches the Blu-Tack and moves the paperweight. 
The strings and paperweights a

Re: Any news on when some libraries will be ported to Python 3.0?

2009-01-07 Thread Roger Binns
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Just Another Victim of the Ambient Morality wrote:
> Anyway, I'd love to hear some news about any of these things in 
> particular or even anything in general.  Am I the only one who's psyched for 
> this version of Python?

I ported my APSW SQLite access module many months before Python 3.0 was
released (around the time of the first beta release).  The same codebase
supports both Python 2 and 3 with very few conditionals and a few macros
to make some Python 2 C api look like the Python 3 C api (eg pretending
that the bytes type exists).  Python 2.3 and up are supported on all
platforms.

It took longest to port my test suite over as I have 99.6% code coverage
which is achieved by the test suite abusing every corner of the language
and implementation.  Fortunately a whole host of issues go away because
of no 8 bit strings in Python 3.

Other tools like Sphinx made my documentation so much better.  That
actually psyches me the most since Python is all about ease of reading
and writing.

Roger
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAklkchwACgkQmOOfHg372QTgewCfXBrz0UGuUNDqxjVGEJOwfF4p
N6QAoJvZCZ1Vm9f6sN0P4bXb8o3I2pVj
=Lba+
-END PGP SIGNATURE-

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


multiprocessing and SIGINT

2009-01-07 Thread akineko
Hello everyone,

I'm trying to use multiprocessing module, which is now available with
Python 2.6.
It is a nice enhancement and it worked great.

However, I have a situation and I couldn't figure out how to deal
with.

My Python program spawns another process to take care of GUI house-
keeping.
When I type Ctrl-C to my terminal, instead of my main process, the
spawned process always gets the SIGINT.

I don't want the spawned process to handle SIGINT event. An exception
handler (KeyboardInterrupt) is placed in the main program (but it
didn't catch the event).

I never encountered this problem before as spawned threads won't take
SIGINT event.

Is there any way I can force SIGINT event routing so that my main
process will get it?
Because of another requirement, I cannot swap main and spawned
process.

Any suggestions will be greatly appreciated.

Best regards,
Aki Niimura
--
http://mail.python.org/mailman/listinfo/python-list


Re: Rich Comparisons Gotcha

2009-01-07 Thread Steven D'Aprano
On Wed, 07 Jan 2009 01:23:19 +, Mark Wooding wrote:

> A case-sensitive string is /not the same/ as a case-insensitive string.
> One's a duck, the other's a goose.  I'd claim here that i"abc" =~ "ABC"
> must be False, because i"abc" =~ "abc" must be false also!  To define it
> otherwise leads to the incoherence you describe.

It's only incoherent if you need equality to be an equivalence relation. 
If you don't, it is perfectly reasonable to declare that i"abc" equals 
"abc".


-- 
Steven


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


Multiprocessing takes higher execution time

2009-01-07 Thread Sibtey Mehdi
Hi,

 

I use multiprocessing to compare more then one set of files.

For comparison each set of files (i.e. Old file1 Vs New file1) I create a
process,

Process(target=compare, args=(oldFile, newFile)).start()

It takes 61 seconds execution time.

 

When I do the same comparison without implementing multiprocessing, it takes
52 seconds execution time.

 

The parallel processing time should be lesser. 

 

I am not able to get advantage of multiprocessing here.

 

Any suggestions can be very helpful.

 

Thanks,

Gopal

 

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


Re: Extending Python with C or C++

2009-01-07 Thread Thomas Heller
Nick Craig-Wood schrieb:
> Ralf Schoenian  wrote:
>>  Ryan wrote:
>> > I've been using Python for many years now. It's a wonderful language
>> > that I enjoy using everyday. I'm now interested in getting to know
>> > more about the guts (C/C++) and extending it. But, extending python
>> > still seems like a black art to me. Is there anymore docs or info on
>> > extending it besides the standard sparse ones (http://www.python.org/
>> > doc/2.5.2/ext/intro.html) that may give me more insight? Is there a
>> > class available? How can I learn more about the guts of python? How
>> > would one go about following an interest in contributing to the
>> > development of python.
>> 
>>  It is not exactly what you are looking for but nevertheless I am 
>>  thinking the  article "Automatic C Library Wrapping -- Ctypes from the 
>>  Trenches" may be interesting for you. You can find it in the latest 
>>  Python Papers issue or simply following the link: 
>>  http://ojs.pythonpapers.org/index.php/tpp/article/view/71
> 
> Interesting - I didn't know about h2xml and xml2py before and I've
> done lots of ctypes wrapping!  Something to help with the initial
> drudge work of converting the structures would be very helpful.
> 
> ( http://pypi.python.org/pypi/ctypeslib/ )
> 
> I gave it a quick go and it worked fine.  I had to edit the XML in one
> place to make it acceptable (removing a u from a hex number).  The
> output of xml2py was an excellent place to start for the conversion,
> though I don't think I'd want to use an automated process like in the
> paper above as its output needed tweaking.
> 
> ...

If you are using a recent version of gccxml, then you should use the
ctypeslib-gccxml-0.9 branch of ctypeslib instead of the trunk. (I should
really merge the gccxml-0.9 branch into the trunk;-)

If you are already using the branch and the XML file is not accepted,
then could you please provide a short C code snippet that reproduces
the problem so that I can fix it?


> Here are my thoughts on the conversion :-
> 
> It converted an interface which looked like this (the inotify interface)
> 
> struct inotify_event {
> int  wd;   /* Watch descriptor */
> uint32_t mask; /* Mask of events */
> uint32_t cookie;   /* Unique cookie associating related
>   events (for rename(2)) */
> uint32_t len;  /* Size of name field */
> char name[];   /* Optional null-terminated name */
> };
> 
> Into this
> 
> class inotify_event(Structure):
> pass
> inotify_event._fields_ = [
> ('wd', c_int),
> ('mask', uint32_t),
> ('cookie', uint32_t),
> ('len', uint32_t),
> ('name', c_char * 0),
> ]
> 
> Which is a very good start.  However it defined these which clearly
> aren't portable
> 
> int32_t = c_int
> uint32_t = c_uint
> 
> Whereas it should have been using the ctypes inbuilt types
> 
> c_int32
> c_uint32

IMO this would be difficult to achive automatically.  There are cases
wher c_int is the correct type, in other cases c_int32 is correct.

> Also I don't think c_char * 0 does anything sensible in ctypes,
> c_byte * 0 is what is required plus a bit of casting.  This is a
> non-standard GNU extension to C though.
> 
> All that said though, it looks like a great time saver.
> 

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


Re: Oh! The people of all over the world!

2009-01-07 Thread James Stroud

Obaid R. wrote:

So an illegal occupation is NOT a provocation but fighting the
occupation IS? By which scales do you judge?

I would argue that a person following such backwards logic and who
sees these pictures[1] and STILL claims that showing sympathy is
difficult has a serious problem. After all the logic is simple: for
every action (read occupation) there is a reaction (read resistance)
equal to it in force and opposite to it in direction.

You can of course argue that the reaction is the cause of the action,
but that is clearly illogical and proves your iniquity in this matter
and your bias against the victim. It might take a humanitarian
catastrophe of sizable proportions and a biased person would still not
feel any sympathy. Not that there is no catastrophe, but rather
because bias is by nature practiced in spite of good reason and
available evidence, not because of it.



I'm much more apt to listen now that you aren't invoking references to 
religion at every sentence. If Israel is guilty of inhumanity, then it 
is more appropriate to appeal to one's humanity.


By the way, its obvious to most rational people that the Israeli 
reaction is overwrought and likely morally unjust--but I can't stand 
antisemitism just like I can't stand hatred of Islam just like I can't 
stand hatred of Christians.


James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA  90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-07 Thread Mark Wooding
Steven D'Aprano  wrote:

> Only in the sense that the behaviour of *real world* objects don't
> entirely match the behaviour of Python objects. If you just accept
> that Python objects can be in two places at once, an unintuitive
> concept I accept but hardly difficult to grasp, then you don't need to
> mention references.

To my mind, explaining that objects can actually be in two (or three, or
any unbounded number) of places at once, stashed inside each other to an
arbitrary degree -- that looks like the excuses of someone who's been
caught in a lie.  Maybe it was a useful lie-to-children, meant to avoid
explaining the full details in advance, but the time comes to put away
childish things, and learn the truth.  (I can't believe I just quoted
Paul.  I feel all dirty.)

> Er, pardon me, but you yourself suggested that one could implement
> Python without using references to objects, "like keeping lists of
> clones, and magically updating all the clones whenever one us mutated.
> That may even be a valid implementation for a distributed Python".
>
> Like you suggested, it would be a seriously rotten model for standard
> Python, but it is possible. The language semantics specifies the
> *behaviour*, not the *mechanism* required to implement that behaviour.

Yes, there's an isomorphism between the two, so technically either could
be used as a model to explain the behaviour of Python.  But, to my mind
at least, the idea of references (or sticky bits of string) is the
simpler -- it doesn't involve doing anything`magically' -- so Occam
suggests that it's superior.  I'm sure, if I tried, I could come with an
even more absurd model, or express it in more complicated ways, but the
power of a model lies in its combination of simplicity and explanatory
power.

The `they're just objects' model is very simple, but gets tied up in
knots explaining things.  The `it's all references' model is only a
little more complicated, but explains everything.

It might be -- and I don't know, because I'm not an educator -- that the
`they're just objects' model hits a local maximum in the tradeoff
between simplicity and explanation that's sufficiently high that it's
useful for teaching beginners.  It's hard to expose this simple model's
weaknesses without assignment (or messing with `is' or `id');
unfortunately, it's very easy to explose them once you do have
assignment (or any kind of mutable state).  And Python's purely
functional subset is rarely used exclusively in nontrivial programs.  So
I'm sceptical that this particular lie-to-children doesn't cause more
harm than good.

> Why can't I stick the paperweight in the box *before* attaching the Blu-
> Tack to something else, and then forget about attaching the Blu-Tack? 
> There's nothing in your model to prevent dangling pointers, except hand-
> waving "it doesn't work like that".

Perils of posting at half past three in the morning, I'm afraid.  I
seemed to have hit a creative streak, but my clarity of thinking was
definitely impaired.

You are only allowed to handle paperweights.  A robot does the rest.
Some of the objects might give you splinters or paper cuts.  The robot
is concerned about your safety and won't let you go near them.  There's
a wall between you and the playpen, with a catflap in it.  The robot
will fit through the catflap but you're too big.

(For advanced users only: there might be a secret hatch which lets you
into the main arena where the robot works, via a storeroom containing a
surprising quantity of handy power tools, heavy weaponry, and
explosives, but oddly lacking in safety goggles.  You can build some
truly marvellous things by sneaking through here, and persuade the robot
to do things it wouldn't usually by threatening it at gunpoint, but you
can also blow yourself up.)

You tell the robot what you want him to do by handing him paperweights.
He doesn't take the paperweights away with him, but instead hooks onto
the string so that he can follow it to the other end.  He's quite good
at following even tangled strings, and at finding paperweights in boxes
and hooking onto their strings too.  The robot will stick new strings
onto objects at your request and hand you the paperweights.

> I assume the string is made of Teflon, otherwise I could stick the Blu-
> Tack to another piece of string. By the same token, the floor needs to be 
> Teflon too.

You could do, if you were actually given a loose piece of string.  But
you aren't, so that's OK.  I was wrong that the paperweights needed to
be Teflon.

> Why can't I grab the string by the end with the Blu-Tack and follow it 
> backwards to find out where the paperweight is? 

Because you're never at that end.  And the robot just won't do that.

(The daemon in the MIT Scheme environment /can/ do this for you.  It
uses a feature called `wabbit hunting'.  You set up a `Fudd thunk'
describing which objects you want it to find the paperweights of, and
set the thing to `wabbit season'.  The daemon will track down

Re: Rich Comparisons Gotcha

2009-01-07 Thread Mark Wooding
Steven D'Aprano  wrote:

> It's only incoherent if you need equality to be an equivalence relation. 
> If you don't, it is perfectly reasonable to declare that i"abc" equals 
> "abc".

Right!  And if you didn't want an equivalence relation, then `==' will
suit you fine.  The problem is that some applications seem to /want/ an
equivalence relation, and one that's more useful (i.e., less
discriminating) than `is'.

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


Re: Traceback in Logging

2009-01-07 Thread Vinay Sajip
On Jan 6, 10:19 pm, Thorsten Kampe  wrote:
> I don't think he's interested in the "line number where the logging call
> was issued" but where the exception occurred.

Well, he does say "all logging" and "every logging". And, as I
mentioned earlier, there is already support for tracebacks by using
the Logger.exception method in an exception handler.

Regards,

Vinay Sajip

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


string length problem?

2009-01-07 Thread Qian Xu
Hi All,

why the code
  print len(u"»test«") 
returns 8 instead of 6?

Best regards
Qian
--
http://mail.python.org/mailman/listinfo/python-list


linked list with cycle structure

2009-01-07 Thread David Hláčik
dictionary with cycle structure

Hello guys,

I have a linked list where *number of elements is unlimited* and
**last element points on random (can be first, last, in middle ,
anywhere) element within linked list** - this is important . My goals
is to create an architecture /scheme for **algoritmus which will count
total number of elements** of such linked list.
Yes , maybe it sounds strange, but we need to implement this and i
would be very gladfull for your toughts.

Thanks in advance and wishing you a sucessfull year!

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


Re: Problem building Python 2.5.2 curses module on HP/UX 11.11

2009-01-07 Thread Martin v. Löwis
> So, given that there are no actual errors during the compile, and the
> warnings are probably benign, does anyone know what would cause the
> resulting compiled module to NOT have the init function?

Can you do nm(1) on the module? It's now called _curses_failed.sl

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


[urllib2] 302 -> can't get cookie

2009-01-07 Thread Gilles Ganault
Hello

I'm using urllib2 to connect to a web server with POST, and then the
server sends a cookie to hold the session ID, but also redirects the
user to another page:

===
HTTP/1.1 302 Found
Date: Wed, 07 Jan 2009 11:20:51 GMT
Server: Apache
Set-Cookie: PHPSESSID=4015f14eb04dc81159253a9533a7c590; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0,
pre-check=0
Pragma: no-cache
Location: second_page.php
Content-Length: 0
Connection: close
Content-Type: text/html; charset=ISO-8859-1
===

As a result, urllib2 follows this new page, and the HTTP header that I
get is the one from the second page.

Is there a way to ignore this second page, and stick to the first page
through which I connect?

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


Re: string length problem?

2009-01-07 Thread Peter Otten
Qian Xu wrote:

> why the code
>   print len(u"»test«")
> returns 8 instead of 6?

You may have declared an encoding that differs from the one your editor
actually uses, e. g.

# -*- coding: iso-8859-1 -*-
print len(u"»test«")

whereas your editor writes UTF-8.

Peter

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


Re: string length problem?

2009-01-07 Thread Qian Xu
Qian Xu wrote:

> Hi All,
> 
> why the code
>   print len(u"»test«")
> returns 8 instead of 6?
> 
> Best regards
> Qian

I have solved the problem myself.

# -*- coding: utf-8 -*-
print len(u"»test«")

--- or ---
s = "»test«"
print len(s.decode("utf-8"))

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


Re: why cannot assign to function call

2009-01-07 Thread sturlamolden
On Jan 7, 2:02 am, Steven D'Aprano
 wrote:

> In Python code, there are no references and no dereferencing.

The why does CPython keep track of reference counts?


> You can't, because Python doesn't have references. In a language with
> references, that's easy.

Python does not 'pass-by-reference' as Fortran or Pascal do.


> It's harder (impossible?) to write a version that will operate on
> arbitrary types, but that's statically typed languages for you.

In Python an assignment (re)binds the name to another value. You can
certainly write a swap method for mutable types. But you cannot use
the assignment operator to swap the values.


> No no no, lists and tuples store *objects*.


>>> a = 123456789
>>> b = [a]
>>> c = [a]
>>> d = [a, a]

>>> b[0] is a
True
>>> c[0] is a
True
>>> d[0] is a
True
>>> d[1] is a
True

Where is the object 'a' stored?





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


Re: pg_result_status() alternative?

2009-01-07 Thread Steve Holden
Qian Xu wrote:
> Hi All,
> 
> I am using the pg module (http://www.pygresql.org/pg.html) for database
> testing.
> 
> I have got a problem now:
> I want to check the result status of postgresql database, which can be done
> in php by using pg_result_status()
> (http://www.phpbuilder.com/manual/en/function.pg-result-status.php)
> 
> How can I do the same thing in python (2.5)?
> 
Without knowing the full details of that particular module I would
hazard a guess that any database errors will raise exceptions in Python.
No exceptions means your database operation worked fine.

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: Multiprocessing takes higher execution time

2009-01-07 Thread Steve Holden
Sibtey Mehdi wrote:
> Hi,
> 
>  
> 
> I use multiprocessing to compare more then one set of files.
> 
> For comparison each set of files (i.e. Old file1 Vs New file1) I create
> a process,
> 
> Process(target=compare, args=(oldFile, newFile)).start()
> 
> It takes 61 seconds execution time.
> 
>  
> 
> When I do the same comparison without implementing multiprocessing, it
> takes 52 seconds execution time.
> 
>  
> 
> The parallel processing time should be lesser.
> 
>  
> 
> I am not able to get advantage of multiprocessing here.
> 
>  
> 
> Any suggestions can be very helpful.
> 
My first suggestion would be: show us some code. We aren't psychic, you
know.

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


mac osx how to use a specific python environment

2009-01-07 Thread marco kuhn
hi,

I would like to use a specific python environment in a script .
The script is load as a plugin by a program which offer a python api .
The python environment is build in.

How can I use the standard python environment.
Can i explicit load the env in my python script.

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


Re: pg_result_status() alternative?

2009-01-07 Thread Qian Xu
Steve Holden wrote:
> Without knowing the full details of that particular module I would
> hazard a guess that any database errors will raise exceptions in Python.
> No exceptions means your database operation worked fine.

result status is not an exception.
It means the information of frontend/backend protocal, after a SQL-statement
is execute.

The following is a list of backend IPC commands (v3)

  Z - Zero / Ready for Query
  E - Error Code
  A - Notification
  P - Cursor Response
  B - Binary Row
  D - Data, Ascii
  C - Complete
  G - Copy IN
  H - Copy OUT
  I - Idle
  T - Row Description
  V - Function result

For instance:

  SELECT * FROM my_table;
The backend protocal should return T (Row Description) and the frontend
protocal should return Q (Query)

  DROP TABLE my_table;
The backend protocal should return C (Complete) and the frontend protocal
should return Q (Query)



--Qian




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


Re: Extending Python with C or C++

2009-01-07 Thread Nick Craig-Wood
Thomas Heller  wrote:
>  Nick Craig-Wood schrieb:
> > Interesting - I didn't know about h2xml and xml2py before and I've
> > done lots of ctypes wrapping!  Something to help with the initial
> > drudge work of converting the structures would be very helpful.
> > 
> > ( http://pypi.python.org/pypi/ctypeslib/ )
> > 
> > I gave it a quick go and it worked fine.  I had to edit the XML in one
> > place to make it acceptable (removing a u from a hex number).
> 
>  If you are using a recent version of gccxml, then you should use the
>  ctypeslib-gccxml-0.9 branch of ctypeslib instead of the trunk. (I should
>  really merge the gccxml-0.9 branch into the trunk;-)
> 
>  If you are already using the branch and the XML file is not accepted,
>  then could you please provide a short C code snippet that reproduces
>  the problem so that I can fix it?

I'm using gccxml from debian testing 0.9.0+cvs20080525-1 which I guess
doesn't have the code from the branch in.

> > Here are my thoughts on the conversion :-
> > 
> > It converted an interface which looked like this (the inotify interface)
> > 
> > struct inotify_event {
> > int  wd;   /* Watch descriptor */
> > uint32_t mask; /* Mask of events */
> > uint32_t cookie;   /* Unique cookie associating related
> >   events (for rename(2)) */
> > uint32_t len;  /* Size of name field */
> > char name[];   /* Optional null-terminated name */
> > };
> > 
> > Into this
> > 
> > class inotify_event(Structure):
> > pass
> > inotify_event._fields_ = [
> > ('wd', c_int),
> > ('mask', uint32_t),
> > ('cookie', uint32_t),
> > ('len', uint32_t),
> > ('name', c_char * 0),
> > ]
> > 
> > Which is a very good start.  However it defined these which clearly
> > aren't portable
> > 
> > int32_t = c_int
> > uint32_t = c_uint
> > 
> > Whereas it should have been using the ctypes inbuilt types
> > 
> > c_int32
> > c_uint32
> 
>  IMO this would be difficult to achive automatically.  There are cases
>  wher c_int is the correct type, in other cases c_int32 is correct.

Yes it is almost impossible difficult to achieve automatically -
exactly how far do you want to unravel the twisty turny mess of
typedefs that make up uint32_t?  It is easy to change the generated
output since it defines the type in one place.

uint32_t and friends (stdint.h) are standardised in C99 so might it be
reasonable to put some special cases in for them, expecially since the
corresponding types already exist in ctypes?

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Defer problem

2009-01-07 Thread wladimir

Hi,

I have an problem. I want to fire one callback after n callbacks fired, but
when i fire one callback all the chain will be executed. Example:

def handle(x):
print 'callback ' + str(x)


def handle2(x):
print 'after callbacks'


d1 = defer.Deffered()
d2 = defer.Deffered()
d3 = defer.Deffered()

d = defer.DeferredList([d1, d2, d3], fireOnOneCallback =1 ,
fireOnOneErrback=1)

d1.addCallback(handle)
d2.addCallback(handle)
d3.addCallback(handle)
#d.addCallback(handle2)

d1.callback(1)
d2.callback(2)
d3.callback(3)

I want fire handle2 after all handle



-- 
View this message in context: 
http://www.nabble.com/Defer-problem-tp21330603p21330603.html
Sent from the Python - python-list mailing list archive at Nabble.com.

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


Re: Defer problem

2009-01-07 Thread Jean-Paul Calderone

On Wed, 7 Jan 2009 04:44:43 -0800 (PST), wladimir  wrote:


Hi,

I have an problem. I want to fire one callback after n callbacks fired, but
when i fire one callback all the chain will be executed. Example:

def handle(x):
   print 'callback ' + str(x)


def handle2(x):
   print 'after callbacks'


d1 = defer.Deffered()
d2 = defer.Deffered()
d3 = defer.Deffered()

d = defer.DeferredList([d1, d2, d3], fireOnOneCallback =1 ,
fireOnOneErrback=1)


`fireOnOneCallback´ is the flag which makes the `DeferredList´ fire when
the first wrapped `Deferred´ has fired.  If you want the `DeferredList´
to only fire after all of the wrapped deferreds have fired, don't pass
this flag.

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


Re: string length problem?

2009-01-07 Thread John Machin
On Jan 7, 9:40 pm, Qian Xu  wrote:
> Hi All,
>
> why the code
>   print len(u"»test«")
> returns 8 instead of 6?

I suggest that you do
   print repr(u"»test«")
and inspect the result.
HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list


Re: ssl module - how can I accept SSLv3 and TLSv1 protocols only?

2009-01-07 Thread Jean-Paul Calderone

On Tue, 6 Jan 2009 19:01:48 -0800 (PST), Giampaolo Rodola'  
wrote:

Hi,
I'm trying to add TLS/SSL support to pyftpdlib.
Since various defects have been found in the SSLv2 protocol many FTPS
servers (i.e. proftpd and vsftpd) decided to support SSLv3 and TLSv1
only and sistematically reject any client attempting to use SSLv2.
Is there a way to tell ssl.wrap_socket() to accept SSLv3 and TLSv1
connections only?
If that's not possible can I determine the encryption protocol being
used *after* that the SSL/TLS handshake took place?


I tried to use wrap_socket as follows:

self.socket = ssl.wrap_socket(self.socket, ,
   certfile=CERTFILE,
   server_side=True,

ssl_version=ssl.PROTOCOL_SSLv3 | ssl.PROTOCOL_TLSv1)

...it works if on the client side I use TLSv1 but not if I use SSLv3
("SSLError: [Errno 1] _ssl.c:480: error:14094410:SSL
routines:SSL3_READ_BYTES:sslv 3 alert handshake failure" exception is
raised)



At the OpenSSL level, you do this by specifying SSLv23_METHOD and then
setting the SSL_OP_NO_SSLv2 flag.  With pyOpenSSL, you do this by
creating a context with SSLv23_METHOD and then setting SSL_OP_NO_SSLv2 on
it, like so:

   from OpenSSL.SSL import Context, SSLv23_METHOD, OP_NO_SSLv2
   context = Context(SSLv23_METHOD)
   context.set_options(OP_NO_SSLv2)

It seems the ssl module does expose SSLv23_METHOD as PROTOCOL_SSLv23,
but I don't see SSL_OP_NO_SSLv2 anywhere, nor any way to specify any
extra flags.

Oring PROTOCOL_SSLv3 together with PROTOCOL_TLSv1 is almost certainly
not the right approach, anyway (as you saw with your tests).

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


Re: linked list with cycle structure

2009-01-07 Thread Diez B. Roggisch
David Hláčik wrote:

> dictionary with cycle structure
> 
> Hello guys,
> 
> I have a linked list where *number of elements is unlimited* and
> **last element points on random (can be first, last, in middle ,
> anywhere) element within linked list** - this is important . My goals
> is to create an architecture /scheme for **algoritmus which will count
> total number of elements** of such linked list.
> Yes , maybe it sounds strange, but we need to implement this and i
> would be very gladfull for your toughts.

Time for homework again? Last time sorting in O(n), now this. How about you
try something yourself and show us the results - then we might comment on
enhancements or problems.

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


Re: image recogniton?

2009-01-07 Thread Li Han
On 1月7日, 上午4时14分, J Kenneth King  wrote:
> I'm curious as to what application the solution to this problem is
> practical for all of its difficulty?
Sorry, I oversimplified the question because of my poor english. It is
an analog compass whose value we need to read  into the computer every
second. We use a video camera keep shooting it, and the compass and
camera are  fixed.
--
Li Han

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


Interpreter & Thread state & Frame structures.

2009-01-07 Thread alessiogiovanni . baroni
Hi to all,
there are some fields in the PyInterpreterState and PyThreadState
obscures.
PyInterpreterState:
  1) Why there are the fields *next and *tstate_head?

PyThreadState:
  1) Why there is the field *next?

An object PyFrameObject is a portion of code, with a state. But
exactly? Contents of a method/function, or more simply any block
indented of code? For example, this code:
def foo(s):
if s > 0:
return True
else:
return False

how much frames are?

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


Re: How to get millisec/fractional seconds out of a time object ?

2009-01-07 Thread David
On Tue, 6 Jan 2009 13:46:46 -0800, "Chris Rebert" 
wrote:


>The attribute you're looking for is 'microseconds', not 'milliseconds'.
>
>Cheers,
>Chris

Thanks   Most of the docs are useful but those on timedelta seemed
too concise. Dave
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-07 Thread Marc 'BlackJack' Rintsch
On Wed, 07 Jan 2009 03:45:00 -0800, sturlamolden wrote:

> On Jan 7, 2:02 am, Steven D'Aprano
>  wrote:
> 
>> In Python code, there are no references and no dereferencing.
> 
> The why does CPython keep track of reference counts?

Show me the *Python* code that does that.  Using reference counting for 
memory management is an implementation detail.  It's possible to use 
other garbage collectors without the need of reference counting.

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


Re: An idea of how to identify Israeli owned software companies

2009-01-07 Thread Terje
Is there a web service/API out there identifying Israel owned 
software/software companies/web sites/web services? If I am about to buy 
a piece of software, but don't want to support the Israeli economy, it 
would have been handy if I could just poll a web service to get the 
answer. This information should be kept in a database, and be public to 
the world through a very simple xml API, something along these lines:


Request:
Some Company Name

Response:
true

Here's one source for this kind of information (I am sure there are 
plenty others):

http://www.science.co.il/SoftwareCo.asp

Of course, a web service like this would be equally useful to those who 
want to support Israeli companies.

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


Re: image recogniton?

2009-01-07 Thread Almar Klein
2009/1/7 Li Han 

> On 1月7日, 上午4时14分, J Kenneth King  wrote:
> > I'm curious as to what application the solution to this problem is
> > practical for all of its difficulty?
> Sorry, I oversimplified the question because of my poor english. It is
> an analog compass whose value we need to read  into the computer every
> second. We use a video camera keep shooting it, and the compass and
> camera are  fixed.
> --
> Li Han
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

The compass of the needle looks like a line. Lines can be best detected
using second order (Gaussian) derivatives.
scipy.ndimage.gaussian_filter provides a filter you can use for that.

Then maybe PCA to find the direction of the needle, and you're done!
There's probably more to it (there always is), but this might get you
started.

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


Re: mac osx how to use a specific python environment

2009-01-07 Thread Philip Semanchuk


On Jan 7, 2009, at 7:27 AM, marco kuhn wrote:


hi,

I would like to use a specific python environment in a script .
The script is load as a plugin by a program which offer a python api .
The python environment is build in.

How can I use the standard python environment.
Can i explicit load the env in my python script.


Hi Marco,
I'm not exactly sure what you want to do. But if you execute `python`  
at the command line, OS X will launch whatever version of Python it  
finds first in your PATH.


Hope this helps
Philip
--
http://mail.python.org/mailman/listinfo/python-list


Re: image recogniton?

2009-01-07 Thread Joe Strout

Li Han wrote:


Sorry, I oversimplified the question because of my poor english. It is
an analog compass whose value we need to read  into the computer every
second. We use a video camera keep shooting it, and the compass and
camera are  fixed.


If you have any choice about it, it would be greatly simpler, cheaper, 
and more effective to throw out the camera and analog compass, and use 
an electronic compass instead.  These are affordable and work quite well 
provided you keep them level (the same constraint you have with an 
old-fashioned compass too).  You can make some simple interface 
hardware, or spend a bit more for something with a USB or serial 
interface built in, like this: 



Best,
- Joe



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


Re: Any news on when some libraries will be ported to Python 3.0?

2009-01-07 Thread pruebauno
On Jan 7, 3:48 am, "Just Another Victim of the Ambient Morality"
 wrote:
>     Anyway, I'd love to hear some news about any of these things in
> particular or even anything in general.  Am I the only one who's psyched for
> this version of Python?
>     Thank you...

There are many people psyched about 3.0, but also many older farts
that have been using Python since 1.0 and have tons of code that
already works fine and feel a little bit like the mechanic that has to
switch his tools from imperial to metric; it is overall probably for
the better but still a pain to adapt everything to the new system.
This is open source so you either have to find a way to motivate the
authors of the libraries without pissing them off, or you will have to
be patient.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Tkinter - problem closing window

2009-01-07 Thread Djames Suhanko
Wow, nice!
But, with join i can't padding with 0.
'-'.join(str(random.randint(0, 60)) for dummy in xrange(6))

Then, i has been used:
a[i] = "%02d" % int(random.randint(0,60))
 I will change int(random.randint(0,60)) for random.randint(0,60), only.
Thank you, guys !! ;-)

The problem was solved when removed the second "mainloop".

I will use the Toplevel instead Tk() again. Thank you for the nice lesson !!


On Tue, Jan 6, 2009 at 6:47 AM, Marc 'BlackJack' Rintsch  wrote:
> On Mon, 05 Jan 2009 12:25:53 -0200, Djames Suhanko wrote:
>
>> I has a litle program that open another window. When I close de root
>> window in quit button, I need clicking 2 times to close. is where the
>> problem?
>>
>> […]
>>
>>  17  def gera_seis(self):
>>  18a = {}
>>  19for i in range(6):
>>  20   a[i] = "%02d" %  int (random.randint(0,60))
>>  21resultadoA = "%s-%s-%s-%s-%s-%s" %
>> (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
>>  22return resultadoA
>
> Not the problem but unnecessary complex.  `random.randint()` already
> returns an int, no need to call `int()` on it.  The string formatting
> with ``%`` returns strings, so there is no need to call `str()` on the
> values.  Even if the values where not strings:  The '%s' place holder
> implies a call to `str()` while formatting.  If you put something into a
> dictionary with consecutive `int` keys, you might use a list instead.
>
> All this can be written as a simple one liner::
>
>'-'.join(str(random.randint(0, 60)) for dummy in xrange(6))
>
>>  24  def say_hi(self):
>>  25resultado = self.gera_seis()
>>  26raiz = Tk()
>
> The problem is here…
>
>>  27F = Frame(raiz)
>>  28F.pack()
>>  29hello = Label(F, text=resultado) 30hello.pack()
>>  31F.mainloop()
>
> …and here.
>
> There is only one `Tk` instance and mainloop allowed per `Tkinter`
> application.  Otherwise really strange things can happen.  Additional
> windows have to be created as `Toplevel` instances.
>
> Ciao,
>Marc 'BlackJack' Rintsch
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Djames Suhanko
LinuxUser 158.760
--
http://mail.python.org/mailman/listinfo/python-list


Re: Multiprocessing takes higher execution time

2009-01-07 Thread Grant Edwards
On 2009-01-07, Steve Holden  wrote:

>> I use multiprocessing to compare more then one set of files.
>> 
>> For comparison each set of files (i.e. Old file1 Vs New file1)
>> I create a process,
>> 
>> Process(target=compare, args=(oldFile, newFile)).start()
>> 
>> It takes 61 seconds execution time.
>> 
>> When I do the same comparison without implementing
>> multiprocessing, it takes 52 seconds execution time.

> My first suggestion would be: show us some code. We aren't
> psychic, you know.

I am!

He's only got one processor, and he's just been bit by Amdahl's
law when P<1 and S<1.

There you have a perfectly "psychic" answer: an educated guess
camoflaged in plausible-sounding but mostly-bullshit buzzwords.
A better psychic would have avoided making that one falsifiable
statement (he's only got one processor).

-- 
Grant Edwards   grante Yow! Hello.  Just walk
  at   along and try NOT to think
   visi.comabout your INTESTINES being
   almost FORTY YARDS LONG!!
--
http://mail.python.org/mailman/listinfo/python-list


How to set a cookie using Cookie Module

2009-01-07 Thread tryg . olson
Hello -

This is my first attempt at python cookies.  I'm using the Cookie
module and trying to set a cookie.  Below is my code.  The cookie does
not get set.  What am I doing wrong?


print "Cache-Control: max-age=0, must-revalidate, no-store"
print "Content-type: text/html"
print
print """
 My Page
 """

c = Cookie.Cookie()
c['MyCookie'] = "Tryg"
print c
print

If I put javascript code to create a cookie in place of my python
cookie code, the javascript cookie gets set.

Any tips appreciated!
Tryg
--
http://mail.python.org/mailman/listinfo/python-list


Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-07 Thread J Kenneth King
Jonathan Gardner  writes:

> On Jan 6, 12:24 pm, J Kenneth King  wrote:
>> Jonathan Gardner  writes:
>> > On Jan 6, 8:18 am, sturlamolden  wrote:
>> >> On Jan 6, 4:32 pm, mark  wrote:
>>
>> >> > I want to implement a internal DSL in Python. I would like the syntax
>> >> > as human readable as possible.
>>
>> >> Also beware that Python is not Lisp. You cannot define new syntax (yes
>> >> I've seen the goto joke).
>>
>> > This isn't really true. You can, for instance, write a program (in
>> > Python) that takes your pseudo-Python and converts it into Python.
>> > This is what a number of templating libraries such as Mako do.
>>
>> Which is not even close to being the same.
>>
>> Lisp - the program source is also the data format
>>
>> Python - the program source is a string
>>
>> I could go on a really long rant about how the two are worlds apart, but
>> I'll let Google tell you if you're really interested.
>
> I get that Lisp is special because you can hack on the reader as it is
> reading the file in. This is strongly discouraged behavior, as far as
> I know, despite the number of cute hacks you can accomplish with it.

It is generally discouraged unless there's a reason for it.

> But consider that this really isn't different than having a program
> read in the lisp-with-modification source and spitting out pure lisp,
> to be read by an honest-to-gosh lisp program later.
>
> If that's the case, then Lisp and Python really aren't that different
> in this regard, except that you don't have the option of modifying the
> reader as it reads in the file.

I think you are missing the distinction.

Lisp expressions are also data structures. A Lisp expression can be
passed to functions and macros to be operated on before being
executed. When you're writing Lisp source, you're basically looking at
the AST on one level and when you start writing macros for your program,
you're creating a "DSL" or interface to that AST. Lisp source is
eventually expanded to a giant list that is consed by the evaluator (as
far as I understand it. I'm just getting into the compiler stuff
myself).

Consider:

(my-func 1 2 3)

This is just a list, the "primitive" data-type in Lisp! This piece of
"data" can be operated on by other bits of Lisp code because it is just
a list as far as Lisp is concerned.

In contrast, Python source is a string that needs to be parsed into
bytecode which is then run through the interpreter. The AST is
completely hidden from the source author. Python expressions are not
data types either and hence no macros -- I can't write a python function
that generates python code at compile time. I can only write a python
program that parses some other string and generates code that can be run
by another interpreter.

Consider:

for i in range(0, 100):
do_something_interesting(i)

That's a pretty straight forward Python expression, but I can't do
anything with it -- it's not a unit of data, it's a string.

The distinction is not subtle by any means.
--
http://mail.python.org/mailman/listinfo/python-list


Re: What would you like to see in a book about Matplotlib?

2009-01-07 Thread Thomas Guettler
Sandro Tosi schrieb:

> Some basic question I'd like to ask are:
> 
> - what are you using matplotlib for?

I use the API to create PNGs from data stored in postgres. Webframework: Django.

> - what are the (basic) things that, when you were beginning to use
> matplotlib, you wanted to see grouped up but couldn't find?

The API is not good documented. It was very hard for me to understand
what was going on the source.

> - what would you like to see in a book about matplotlib?

How to use the API.

  Thomas


-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
--
http://mail.python.org/mailman/listinfo/python-list


listdir reports [Error 1006] The volume for a file has been externally altered so that the opened file is no longer valid

2009-01-07 Thread PerOK
os.listdir() keep giving this error:
[Error 1006] The volume for a file has been externally altered so that the
opened file is no longer valid:
'V:\\ETOPOK_Example_MyTestView\\LD_FuncBlocks_013\\cnh1603087_ui_toolkit_mid
p\\test/*.*'

The V:\\ETOPOK_Example_MyTestView refer to a ClearCase (CC) dynamic view.

I have found that MS Visual Studio.Net 2002 and 2003 seems to have this
problem with CC.  But I am not using any of MS's Visual Studios. (see
http://www-01.ibm.com/support/docview.wss?ratlid=cctocbody&rs=984&uid=swg21229572)

The seen problem only to appear when some/all caches have forgot about the
directory.  If I query the directory again (multiple times) listdir() will
finally return the content of the directory.

The "DOS" command DIR has no problem listing the same directory.

Configuration:
>>> sys.version
'2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]'
>>> sys.getwindowsversion()
(5, 0, 2195, 2, 'Service Pack 4')
>>> sys.platform
'win32'


Per OK
PS: Why does the listdir() function add '*.*' to the path?
PS2: Why does the listdir() function add '/*.*' to the path on windows and
not '\\*.*' ?


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


Re: Multiprocessing takes higher execution time

2009-01-07 Thread Nick Craig-Wood
Grant Edwards  wrote:
>  On 2009-01-07, Steve Holden  wrote:
> 
> >> I use multiprocessing to compare more then one set of files.
> >> 
> >> For comparison each set of files (i.e. Old file1 Vs New file1)
> >> I create a process,
> >> 
> >> Process(target=compare, args=(oldFile, newFile)).start()
> >> 
> >> It takes 61 seconds execution time.
> >> 
> >> When I do the same comparison without implementing
> >> multiprocessing, it takes 52 seconds execution time.
> 
> > My first suggestion would be: show us some code. We aren't
> > psychic, you know.
> 
>  I am!
> 
>  He's only got one processor, and he's just been bit by Amdahl's
>  law when P<1 and S<1.
> 
>  There you have a perfectly "psychic" answer: an educated guess
>  camoflaged in plausible-sounding but mostly-bullshit buzzwords.
>  A better psychic would have avoided making that one falsifiable
>  statement (he's only got one processor).

;-)

My guess would be that the job is IO bound rather than CPU bound, but
that is covered by Amdahl's Law too where P is approx 0, N
irrelevant...

Being IO bound explains why it takes longer with multiprocessing - it
causes more disk seeks to run an IO bound algorithm in parallel than
running it sequentially.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
--
http://mail.python.org/mailman/listinfo/python-list


Looking for a generic Python script to turn xml into a dataset/array

2009-01-07 Thread davidgshi
I am looking for a generic Python script to turn xml into a dataset/
array, to be ready to be saved in a .dbf file.   The xml contains
CDATA section.

Regards.

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


Re: An idea of how to identify Israeli owned software companies

2009-01-07 Thread Steve Holden
Terje wrote:
> Is there a web service/API out there identifying Israel owned
> software/software companies/web sites/web services? If I am about to buy
> a piece of software, but don't want to support the Israeli economy, it
> would have been handy if I could just poll a web service to get the
> answer. This information should be kept in a database, and be public to
> the world through a very simple xml API, something along these lines:
> 
> Request:
> Some Company Name
> 
> Response:
> true
> 
> Here's one source for this kind of information (I am sure there are
> plenty others):
> http://www.science.co.il/SoftwareCo.asp
> 
> Of course, a web service like this would be equally useful to those who
> want to support Israeli companies.

Words fail me.

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: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-07 Thread Kay Schluehr
On 7 Jan., 16:50, J Kenneth King  wrote:

> Python expressions are not
> data types either and hence no macros -- I can't write a python function
> that generates python code at compile time.

Have you ever considered there are languages providing macros other
than Lisp? Macros have nothing to do with homoiconcity.

> I can only write a python
> program that parses some other string and generates code that can be run
> by another interpreter.

No, it is the same interpreter and it is also possible to modify
python parsers on the fly. This is just not possible with Pythons
builtin parser.


>
> Consider:
>
> for i in range(0, 100):
>     do_something_interesting(i)
>
> That's a pretty straight forward Python expression, but I can't do
> anything with it -- it's not a unit of data, it's a string.
>
> The distinction is not subtle by any means.

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


Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-07 Thread Chris Mellon
On Wed, Jan 7, 2009 at 9:50 AM, J Kenneth King  wrote:
> Jonathan Gardner  writes:
>
>> On Jan 6, 12:24 pm, J Kenneth King  wrote:
>>> Jonathan Gardner  writes:
>>> > On Jan 6, 8:18 am, sturlamolden  wrote:
>>> >> On Jan 6, 4:32 pm, mark  wrote:
>>>
>>> >> > I want to implement a internal DSL in Python. I would like the syntax
>>> >> > as human readable as possible.
>>>
>>> >> Also beware that Python is not Lisp. You cannot define new syntax (yes
>>> >> I've seen the goto joke).
>>>
>>> > This isn't really true. You can, for instance, write a program (in
>>> > Python) that takes your pseudo-Python and converts it into Python.
>>> > This is what a number of templating libraries such as Mako do.
>>>
>>> Which is not even close to being the same.
>>>
>>> Lisp - the program source is also the data format
>>>
>>> Python - the program source is a string
>>>
>>> I could go on a really long rant about how the two are worlds apart, but
>>> I'll let Google tell you if you're really interested.
>>
>> I get that Lisp is special because you can hack on the reader as it is
>> reading the file in. This is strongly discouraged behavior, as far as
>> I know, despite the number of cute hacks you can accomplish with it.
>
> It is generally discouraged unless there's a reason for it.
>
>> But consider that this really isn't different than having a program
>> read in the lisp-with-modification source and spitting out pure lisp,
>> to be read by an honest-to-gosh lisp program later.
>>
>> If that's the case, then Lisp and Python really aren't that different
>> in this regard, except that you don't have the option of modifying the
>> reader as it reads in the file.
>
> I think you are missing the distinction.
>
> Lisp expressions are also data structures. A Lisp expression can be
> passed to functions and macros to be operated on before being
> executed. When you're writing Lisp source, you're basically looking at
> the AST on one level and when you start writing macros for your program,
> you're creating a "DSL" or interface to that AST. Lisp source is
> eventually expanded to a giant list that is consed by the evaluator (as
> far as I understand it. I'm just getting into the compiler stuff
> myself).
>
> Consider:
>
> (my-func 1 2 3)
>
> This is just a list, the "primitive" data-type in Lisp! This piece of
> "data" can be operated on by other bits of Lisp code because it is just
> a list as far as Lisp is concerned.
>
> In contrast, Python source is a string that needs to be parsed into
> bytecode which is then run through the interpreter. The AST is
> completely hidden from the source author. Python expressions are not
> data types either and hence no macros -- I can't write a python function
> that generates python code at compile time. I can only write a python
> program that parses some other string and generates code that can be run
> by another interpreter.
>
> Consider:
>
> for i in range(0, 100):
>do_something_interesting(i)
>
> That's a pretty straight forward Python expression, but I can't do
> anything with it -- it's not a unit of data, it's a string.
>
> The distinction is not subtle by any means.


Ignoring reader macros for a moment, there is no way in either lisp,
ruby, or python to change the syntax that the compiler understands,
and the ability to work with your code directly as a data structure
(which is what makes lisp macros powerful) isn't directly relevant to
the idea of an "internal' DSL.

The OP wants a Ruby-style DSL by which he means "something that lets
me write words instead of expressions". The ruby syntax is amenable to
this, python (and lisp, for that matter) syntax is not and you can't
implement that style of internal DSL in those languages.

The answer to the OP is "you can't - use Ruby or modify your requirements".
--
http://mail.python.org/mailman/listinfo/python-list


Creating new instances of subclasses.

2009-01-07 Thread J. Cliff Dyer
I want to be able to create an object of a certain subclass, depending
on the argument given to the class constructor.

I have three fields, and one might need to be a StringField, one an
IntegerField, and the last a ListField.  But I'd like my class to
delegate to the proper subclass automatically, so I can just do:

>>> f1 = Field('abc')
>>> f2 = Field('123')
>>> f3 = Field('D,E,F')
>>> f1.data
'abc'
>>> f2.data
123
>>> f3.data
['D','E','F']
>>> type(f1)

>>> type(f2)

>>> type(f3)


I've come up with a solution, but I suspect there's something cleaner I
can do with the inheritance structure of __new__.  I don't like
explicitly leapfrogging over Field.__new__ to object.__new__.

My attempt is below:

def is_list(arg):
if ',' in arg:  return True
else:  return False

def is_integer(arg):
try:  int(arg)
except ValueError:  return False
else:  return True

class Field(object):
def __new__(cls, a):
if is_list(a):
return ListField(a)
elif is_integer(a):
return IntegerField(a)
else:
return StringField(a)

def __init__(self, input):
super(Field, self).__init__(input)
self.data = input

class IntegerField(Field):
def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(IntegerField, self).__init__(s)
self.s = int(self.s)

class ListField(Field):
def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(ListField, self).__init__(s)
self.s = s.split(',')

class StringField(Field):
def __new__(cls, a):
return object.__new__(cls, a)

Is there a cleaner way to do this?  The main problem is that
Field.__new__ gets in the way of properly constructing the subclasses
once I've used it to select the proper subclass in the first place.

Cheers,
Cliff

-- 
Oook,
J. Cliff Dyer
Carolina Digital Library and Archives
UNC Chapel Hill

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


Is it ok to type check a boolean argument?

2009-01-07 Thread Adal Chiriliuc
Hello,

Me and my colleagues are having an discussion about the best way to
code a function (more Pythonic).

Here is the offending function:

def find(field, order):
if not isinstance(order, bool):
raise ValueError("order must be a bool")
order_by = "asc" if order else "desc"
return _find(field + "+" + order_by)

We are not sure what's the best practice here. Should we or should we
not check the type of the "order" variable, which should be a bool?

In one of our unit-tests we passed the "invalid_order" string as the
order argument value. No exception was raised, since the string was
evaluated as being True.

We know about "Don't look before we jump", but we are not sure how it
applies in this case, since we don't get any exception when passing an
invalid type argument.

This function is not visible to our clients, only internally in our
project. It's part of the public interface of a sub-system, so we are
not sure either if the fact that it returns an invalid result for a
badly type argument it's an API specification break or not.

The pro argument was that if a new programmer comes and passes a
wrongly typed argument, he will get a silent failure.
The cons argument was that there are many functions which could
silently fail in this mode, especially those having bool arguments.

Should we in general check when doing unit-testing that the methods
behave correctly when passed arguments of the wrong type? What do you
do in your projects?
--
http://mail.python.org/mailman/listinfo/python-list


Re: ssl module - how can I accept SSLv3 and TLSv1 protocols only?

2009-01-07 Thread Giampaolo Rodola'
On 7 Gen, 14:21, Jean-Paul Calderone  wrote:
> On Tue, 6 Jan 2009 19:01:48 -0800 (PST), Giampaolo Rodola'  
> wrote:
> >Hi,
> >I'm trying to add TLS/SSL support to pyftpdlib.
> >Since various defects have been found in the SSLv2 protocol many FTPS
> >servers (i.e. proftpd and vsftpd) decided to support SSLv3 and TLSv1
> >only and sistematically reject any client attempting to use SSLv2.
> >Is there a way to tell ssl.wrap_socket() to accept SSLv3 and TLSv1
> >connections only?
> >If that's not possible can I determine the encryption protocol being
> >used *after* that the SSL/TLS handshake took place?
>
> >I tried to use wrap_socket as follows:
>
> >self.socket = ssl.wrap_socket(self.socket, ,
> >                                            certfile=CERTFILE,
> >                                            server_side=True,
>
> >ssl_version=ssl.PROTOCOL_SSLv3 | ssl.PROTOCOL_TLSv1)
>
> >...it works if on the client side I use TLSv1 but not if I use SSLv3
> >("SSLError: [Errno 1] _ssl.c:480: error:14094410:SSL
> >routines:SSL3_READ_BYTES:sslv 3 alert handshake failure" exception is
> >raised)
>
> At the OpenSSL level, you do this by specifying SSLv23_METHOD and then
> setting the SSL_OP_NO_SSLv2 flag.  With pyOpenSSL, you do this by
> creating a context with SSLv23_METHOD and then setting SSL_OP_NO_SSLv2 on
> it, like so:
>
>     from OpenSSL.SSL import Context, SSLv23_METHOD, OP_NO_SSLv2
>     context = Context(SSLv23_METHOD)
>     context.set_options(OP_NO_SSLv2)
>
> It seems the ssl module does expose SSLv23_METHOD as PROTOCOL_SSLv23,
> but I don't see SSL_OP_NO_SSLv2 anywhere, nor any way to specify any
> extra flags.
>
> Oring PROTOCOL_SSLv3 together with PROTOCOL_TLSv1 is almost certainly
> not the right approach, anyway (as you saw with your tests).
>
> Jean-Paul- Nascondi testo citato
>
> - Mostra testo citato -

These are pretty bad news.
I'm going to open a request on the bug tracker hoping that this
situation could be solved soon.
Thanks for your reply anyway.


Sincerely

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


Importing modules

2009-01-07 Thread e4me4m
Coming from a scripting background where we used to write everything
into one script, I'm now going modular with Python. I place related
functions in one module, and other functions in other modules.

This all works OK, but I'm a bit confused about importing modules from
the standard library. For example, say 3 of my 12 modules use os and
os.path. Right now, I have import statements in all 3 modules that
import both os and os.path. This seems wrong to me.

I read the docs and understand that before loading a module that
Python is smart enough to look and see if it's already loaded, so
there is no performance issue (it would seem), but I was thinking
there is probably a way to load all the modules I need in some orderly
fashion without having these multiple import statements in my separate
modules.

Could someone point me to some docs that explain the Python way of
loading modules when breaking old, big (everything in one script) into
more manageable modular scripts?

Thanks,

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


formatted 'time' data in calculations

2009-01-07 Thread Ross
There seems to be no shortage of information around on how to use the 
time module, for example to use time.ctime() and push it into strftime 
and get something nice out the other side, but I haven't found anything 
helpful in going the other way.


That is, given some formatted text describing times - is there something 
that makes it easy to calculate time differences, or do I have to index 
my way through the string pulling out characters, converting to integers 
etc...


Data is formatted:

   t1 = 09:12:10
   t2 = 11:22:14

I want to calculate tdiff = t2-t1

Any suggestions? (Thanks for anything you can offer)

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


Re: Importing modules

2009-01-07 Thread Diez B. Roggisch
e4m...@gmail.com wrote:

> Coming from a scripting background where we used to write everything
> into one script, I'm now going modular with Python. I place related
> functions in one module, and other functions in other modules.
> 
> This all works OK, but I'm a bit confused about importing modules from
> the standard library. For example, say 3 of my 12 modules use os and
> os.path. Right now, I have import statements in all 3 modules that
> import both os and os.path. This seems wrong to me.
> 
> I read the docs and understand that before loading a module that
> Python is smart enough to look and see if it's already loaded, so
> there is no performance issue (it would seem), but I was thinking
> there is probably a way to load all the modules I need in some orderly
> fashion without having these multiple import statements in my separate
> modules.
> 
> Could someone point me to some docs that explain the Python way of
> loading modules when breaking old, big (everything in one script) into
> more manageable modular scripts?

There is no way around the multiple imports. Or, at least not a clean way -
you *could* stuff things into __builtin__ in one module, effectively making
it available globally.

But this is a hack at best, and certainly not the way to go.

The thing about modularization is that you want single pieces of code that
can be read and understood and extended by themselves (even if embedded and
useful in - or only in - a larger context).

So the seemingly unnecessary imports serve documentation purposes - what is
actually needed by the current module. 

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


Re: listdir reports [Error 1006] The volume for a file has been externally altered so that the opened file is no longer valid

2009-01-07 Thread Chris Rebert
> PS: Why does the listdir() function add '*.*' to the path?

Don't know what you're talking about. It doesn't do any globbing or
add "*.*" to the path. Its exclusive purpose is to list the contents
of a directory, so /in a sense/ it does add "*.*", but then not adding
"*.*" would make the function completely useless given its purpose.

> PS2: Why does the listdir() function add '/*.*' to the path on windows and
> not '\\*.*' ?

You can use either directory separator (\ or /) with the Python APIs
on Windows. r"c:\WINDOWS\" works just as well as "c:/WINDOWS/".

Cheers,
Chris

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


Re: Importing modules

2009-01-07 Thread Steve Holden
e4m...@gmail.com wrote:
> Coming from a scripting background where we used to write everything
> into one script, I'm now going modular with Python. I place related
> functions in one module, and other functions in other modules.
> 
> This all works OK, but I'm a bit confused about importing modules from
> the standard library. For example, say 3 of my 12 modules use os and
> os.path. Right now, I have import statements in all 3 modules that
> import both os and os.path. This seems wrong to me.
> 
It isn't.

> I read the docs and understand that before loading a module that
> Python is smart enough to look and see if it's already loaded, so
> there is no performance issue (it would seem), but I was thinking
> there is probably a way to load all the modules I need in some orderly
> fashion without having these multiple import statements in my separate
> modules.
> 
Think of the import statement as giving access to a particular
namespace, with the side-effect of loading the code if it's the first
import.

> Could someone point me to some docs that explain the Python way of
> loading modules when breaking old, big (everything in one script) into
> more manageable modular scripts?
> 
Import each module into every other module that requires its functionality.

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


#python IRC help - my internet provider is banned!

2009-01-07 Thread simonh
Hi. Not sure if anyone can help here, but I'm trying to access #python
and have found out that my ISP has a blanket ban (for some reason).
Does anyone know how I can contact an operator to join? Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it ok to type check a boolean argument?

2009-01-07 Thread Paul McGuire
On Jan 7, 11:24 am, Adal Chiriliuc  wrote:
> Hello,
>
> Me and my colleagues are having an discussion about the best way to
> code a function (more Pythonic).
>
> Here is the offending function:
>
> def find(field, order):
> if not isinstance(order, bool):
> raise ValueError("order must be a bool")
> order_by = "asc" if order else "desc"
> return _find(field + "+" + order_by)
>


First of all, please get used to saying "My colleagues and I..."
instead of "Me and my colleagues". My kids still haven't learned this
(both are in college!), and it annoys me daily.  Sorry for the rant,
you were just the final straw, so I'm taking it out on you.


The offending part of this function is the argument name "order".  If
this is a boolean argument, I would assume that True should be ordered
data, and False would be unordered or randomly ordered data.  The name
"order" really does not clue me in that the two choices are
"ascending" and "descending", nor is it clear what the mapping is to
True and False.

You are right, Python's inference of boolean-ness makes it easy for
coders to guess wrong as to the argument type and still get a program
that emits no exceptions, although possibly doing the opposite of the
intended function.  I would suggest that in your project, you
establish a naming convention for these boolean arguments.  The name
should be descriptive and assertive, so that the value of True is
reasonably intuitive.  Instead of "order", perhaps
"return_ascending_results" with a default value of True.  To further
cue your developers that a boolean argument is desired, you could try
adding "_flag" to all of your boolean arguments, sort of a Reverse
Hungarian Notation.

Or another option altogether would be to define some module level
constants, like this:

ASCENDING_RESULTS = object()
DESCENDING_RESULTS = object()
def find(field, results_order=ASCENDING_RESULTS):
order_by = { ASCENDING_RESULTS : "asc", DESCENDING_RESULTS: "desc"}
[results_order]
return _find(field + "+" + order_by)

If anything other than those two constants is passed for the
results_order argument, then a KeyError exception will be raised.  (I
used a similar technique to this in pyparsing's operatorPrecedence
method, in which an argument indicates whether an operator is right-
or left-associative.)

-- Paul

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


Re: formatted 'time' data in calculations

2009-01-07 Thread Diez B. Roggisch
Ross wrote:

> There seems to be no shortage of information around on how to use the
> time module, for example to use time.ctime() and push it into strftime
> and get something nice out the other side, but I haven't found anything
> helpful in going the other way.
> 
> That is, given some formatted text describing times - is there something
> that makes it easy to calculate time differences, or do I have to index
> my way through the string pulling out characters, converting to integers
> etc...
> 
> Data is formatted:
> 
> t1 = 09:12:10
> t2 = 11:22:14
> 
> I want to calculate tdiff = t2-t1
> 
> Any suggestions? (Thanks for anything you can offer)

The datetime module contains everything you need. Look at the
strptime-function that will allow you to parse the above string to an
actual datetime.time-object, and the you can subtract these to yield a
datetime.timedelta-object.

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


Re: formatted 'time' data in calculations

2009-01-07 Thread Chris Rebert
On Wed, Jan 7, 2009 at 9:35 AM, Ross  wrote:
> There seems to be no shortage of information around on how to use the time
> module, for example to use time.ctime() and push it into strftime and get
> something nice out the other side, but I haven't found anything helpful in
> going the other way.
>
> That is, given some formatted text describing times - is there something
> that makes it easy to calculate time differences, or do I have to index my
> way through the string pulling out characters, converting to integers etc...
>
> Data is formatted:
>
>   t1 = 09:12:10
>   t2 = 11:22:14
>
> I want to calculate tdiff = t2-t1
>
> Any suggestions? (Thanks for anything you can offer)

Use the `datetime` class in the `datetime` module
(http://docs.python.org/library/datetime.html). It has a class method
.strptime() to parse a string into a `datetime` object. You can then
subtract one `datetime` from another to produce a `timedelta` object
representing the difference between them.

Cheers,
Chris

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


Re: formatted 'time' data in calculations

2009-01-07 Thread Stephen Chapman
Here is how I have done adjustments to time in the past.  This is mostly 
Date related but it may help


today = datetime.date.today()
wkdiff = datetime.timedelta(weeks=1)
daydiff = datetime.timedelta(days=1)
startdate=(today-wkdiff)-daydiff

this will subtract 1 week and 1 day from today.
Stephen


Ross wrote:
There seems to be no shortage of information around on how to use the 
time module, for example to use time.ctime() and push it into strftime 
and get something nice out the other side, but I haven't found 
anything helpful in going the other way.


That is, given some formatted text describing times - is there 
something that makes it easy to calculate time differences, or do I 
have to index my way through the string pulling out characters, 
converting to integers etc...


Data is formatted:

   t1 = 09:12:10
   t2 = 11:22:14

I want to calculate tdiff = t2-t1

Any suggestions? (Thanks for anything you can offer)

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




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


Re: Importing modules

2009-01-07 Thread Paul McGuire
...and don't worry about a possible performance issue of importing os
(or any other module) multiple times - the Python import manager is
smart enough to recognize previously imported modules, and wont import
them again.

If a module uses the os module, then it should import it - that's just
it.

Another consideration might be that you are breaking up your own
program modules too much.  For instance, if I had a program in which I
were importing urllib in lots of modules, it might indicate that I
still have some regrouping to do, and that I could probably gather all
of my urllib dependent code into a single place.

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


Re: formatted 'time' data in calculations

2009-01-07 Thread Ross

Thanks Chris and Diez for the quick pointers... Very helpful

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


Re: Is it ok to type check a boolean argument?

2009-01-07 Thread Philip Semanchuk


On Jan 7, 2009, at 12:24 PM, Adal Chiriliuc wrote:


Hello,

Me and my colleagues are having an discussion about the best way to
code a function (more Pythonic).

Here is the offending function:

def find(field, order):
if not isinstance(order, bool):
raise ValueError("order must be a bool")
order_by = "asc" if order else "desc"
return _find(field + "+" + order_by)

We are not sure what's the best practice here. Should we or should we
not check the type of the "order" variable, which should be a bool?


IMHO you should not.


The pro argument was that if a new programmer comes and passes a
wrongly typed argument, he will get a silent failure.


"Wrongly typed" is a matter of opinion. The following values also  
evaluate to False when converted to bool:

None
[]
()
""
{}

To me, they're just as false as False is, if you catch my meaning. I  
would not like to have to convert them to bool to be able to use them  
when calling your function.


Imagine this scenario --

   order = read_order_from_preferences_xml_file(default_value = None)

   # order is now "ascending" or None

   find(the_field_name, order)


It seems logical to pass a string or None in this case without  
converting them to bool.


You might feel more comfortable if the parameter was called  
"is_ordered", which would at least imply that it is a boolean so that  
someone unfamiliar with the interface would be less tempted to pass  
something like the last part of an ORDER BY statement, like  
"last_name, first_name, age DESC".


I can understand your temptation to enforce bool-ness, but you have a  
very good point about this one function then being different from all  
of the others that aren't as picky.


HTH
Philip





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


Re: Creating new instances of subclasses.

2009-01-07 Thread Paul McGuire
On Jan 7, 10:38 am, "J. Cliff Dyer"  wrote:
> I want to be able to create an object of a certain subclass, depending
> on the argument given to the class constructor.
>
> I have three fields, and one might need to be a StringField, one an
> IntegerField, and the last a ListField.  But I'd like my class to
> delegate to the proper subclass automatically, so I can just do:
>
> >>> f1 = Field('abc')
> >>> f2 = Field('123')
> >>> f3 = Field('D,E,F')

O-O is not always the solution to every problem.  Since inheritance is
getting in your way, try using a class-level factory method.  Instead
of using the Field constructor, use a staticmethod of Field, something
like:

@staticmethod
def make_Field(a)
if is_list(a):
return ListField(a)
elif is_integer(a):
return IntegerField(a)
else:
return StringField(a)

and then get rid of all those __new__ methods, too.

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


Re: Importing modules

2009-01-07 Thread Mel
Steve Holden wrote:

> e4m...@gmail.com wrote:
[ ... ]
>> Could someone point me to some docs that explain the Python way of
>> loading modules when breaking old, big (everything in one script) into
>> more manageable modular scripts?
>> 
> Import each module into every other module that requires its
> functionality.

True.  The O.P. can note one of the miracles of Python: (for an example,
I'll pick a library module at random .. image)  One can write a module  --
myimage.py, say, that imports image, and has some functions that return
objects based on calls to image functions.

A program that imports myimage can use these objects in all possible ways
and not have to import image.  It doesn't need to know anything about just
where myimage gets its stuff.  Importing only strictly needs to be one
level deep.  (Of course, the programmer needs to know what all those
possible ways are.  The programmer may have to have read the docs for the
image module ... )

Mel.

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


Re: Creating new instances of subclasses.

2009-01-07 Thread Terry Reedy

J. Cliff Dyer wrote:

I want to be able to create an object of a certain subclass, depending
on the argument given to the class constructor.

I have three fields, and one might need to be a StringField, one an
IntegerField, and the last a ListField.  But I'd like my class to
delegate to the proper subclass automatically, so I can just do:


f1 = Field('abc')
f2 = Field('123')
f3 = Field('D,E,F')
f1.data

'abc'

f2.data

123

f3.data

['D','E','F']

type(f1)



type(f2)



type(f3)



I've come up with a solution, but I suspect there's something cleaner


Make your master class _Field and make Field a factory function that 
returns the proper subclass instance.  The body of Field could be the 
body of __new__ below.  Then dump the __new__ methods.


tjr


 I

can do with the inheritance structure of __new__.  I don't like
explicitly leapfrogging over Field.__new__ to object.__new__.

My attempt is below:

def is_list(arg):
if ',' in arg:  return True
else:  return False

def is_integer(arg):
try:  int(arg)
except ValueError:  return False
else:  return True

class Field(object):
def __new__(cls, a):
if is_list(a):
return ListField(a)
elif is_integer(a):
return IntegerField(a)
else:
return StringField(a)

def __init__(self, input):

super(Field, self).__init__(input)
self.data = input

class IntegerField(Field):
def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(IntegerField, self).__init__(s)
self.s = int(self.s)

class ListField(Field):

def __new__(cls, a):
return object.__new__(cls, a)
def __init__(self, s):
super(ListField, self).__init__(s)
self.s = s.split(',')

class StringField(Field):
def __new__(cls, a):
return object.__new__(cls, a)

Is there a cleaner way to do this?  The main problem is that
Field.__new__ gets in the way of properly constructing the subclasses
once I've used it to select the proper subclass in the first place.

Cheers,
Cliff



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


Re: An idea of how to identify Israeli owned software companies

2009-01-07 Thread Mensanator
On Jan 7, 8:45 am, Terje  wrote:
> Is there a web service/API out there identifying Israel owned
> software/software companies/web sites/web services? If I am about to buy
> a piece of software, but don't want to support the Israeli economy, it
> would have been handy if I could just poll a web service to get the
> answer. This information should be kept in a database, and be public to
> the world through a very simple xml API, something along these lines:
>
> Request:
> Some Company Name
>
> Response:
> true
>
> Here's one source for this kind of information (I am sure there are
> plenty others):http://www.science.co.il/SoftwareCo.asp
>
> Of course, a web service like this would be equally useful to those who
> want to support Israeli companies.

Something like that web-service that publishes the names
and addresses of doctors who perform abortions so that
they can be assassinated?
--
http://mail.python.org/mailman/listinfo/python-list


socket.error 24: too many open files

2009-01-07 Thread TheDavidFactor
I'm new to python, but have been writing programs in other languages
for about 15 years now. As part of my job I develop applications that
interface with Asterisk in various ways. As a way of getting my feet
wet I decided to try to rewrite an outbound call script that I have in
another language into python. It's a deamon that runs on a linux box
and every 15 seconds it checks a MySQL table for new records, if there
are any it creates a .call file on the Asterisk server using ssh, it
also checks the Asterisk server, again via ssh, for any finished calls
and if there are any it reads the .call file and writes the result in
to the MySQL table.

I did a google search for ssh and a python script that someone had
written that wraps up some paramiko calls to make the ssh interaction
easier. I'm also using MySQLdb for the MySQL access. It sleeps for 15
seconds then creates an ssh connection the asterisk server, checks the
MySQL table, checks the asterisk server, closes the connection and
goes back to sleep. After it has run for an hour or so I get the error
in the subject when the ssh class tries to create a new socket. I have
double checked that it is closing the socket. I don't know what else
to check, any suggestions would be much appreciated.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Is it ok to type check a boolean argument?

2009-01-07 Thread Terry Reedy

Adal Chiriliuc wrote:


Me and my colleagues are having an discussion about the best way to
code a function (more Pythonic).

Here is the offending function:

def find(field, order):
if not isinstance(order, bool):
raise ValueError("order must be a bool")
order_by = "asc" if order else "desc"
return _find(field + "+" + order_by)


My opinions:
1. 'order' should be 'a' (the default) or 'd'. True and False by 
themselves are meaningless.  Then the check, if needed, is "if order not 
in 'ad':".

'up' and 'down' are possible too.
2. Consider renaming _find as find, with two params and do the parameter 
check there.  Except when one is calling a function recursively 
(repeatedly) with known-good params, wrapping a function with a simple 
arg check seems like noise to me.



We are not sure what's the best practice here. Should we or should we
not check the type of the "order" variable, which should be a bool?


I say it should not be a bool.  The below illustrates another reason why 
this is the wrong type.



In one of our unit-tests we passed the "invalid_order" string as the
order argument value. No exception was raised, since the string was
evaluated as being True.


If you make 'order' a string, then a bad string input should raise an 
exception somewhere.  I suspect _find could be written to do this if it 
does not already.



We know about "Don't look before we jump", but we are not sure how it
applies in this case, since we don't get any exception when passing an
invalid type argument.



This function is not visible to our clients, only internally in our
project. It's part of the public interface of a sub-system, so we are
not sure either if the fact that it returns an invalid result for a
badly type argument it's an API specification break or not.

The pro argument was that if a new programmer comes and passes a
wrongly typed argument, he will get a silent failure.


That is bad, but in this case, I see the problem as a slight mis-design.


The cons argument was that there are many functions which could
silently fail in this mode, especially those having bool arguments.


Which are rather rare, I think.  In the 3.0 builtin functions, sorted's 
'reverse' param is the only one.  For back compatibility, it actually 
accepts ints.



Should we in general check when doing unit-testing that the methods
behave correctly when passed arguments of the wrong type?


As in 'raise an exception', I think at least one test is good.

Terry Jan Reedy



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


[Mechanize.ClientForm] double reading from urllib2.urlopen

2009-01-07 Thread tiktak . hodiki
Hello, folks!
I use mechanize.clientform to parse HTML-forms. I preliminary check
response and call response.read().find("..."). But when it's taken to
ClientForm.ParseResponse, it can't parse because of response.read() is
zero-length text. The problem is that ClientForm.ParseResponse is not
taken text of response, only object.

Example:

import urllib
from ClientForm import ParseResponse
response = urllib.urlopen("http://yandex.ru";)
if -1 != response.read().find("foobar"):
pass
form = ParseResponse(response)[1] <-- there is exception IndexError
--
http://mail.python.org/mailman/listinfo/python-list


Re: #python IRC help - my internet provider is banned!

2009-01-07 Thread simonh
On Jan 7, 6:30 pm, "Mildew Spores"  wrote:
> What? Sounds a bit unlikely unless its Virgin..
> I'd imagine it might be that your isp needs to get itself off a black list.
> Brian
>
> --
> My Hotmail Account
> mildew_spo...@hotmail.com
>
> "simonh"  wrote in message
>
> news:fd0bd7d3-ad1d-43c2-b8e5-642a95c21...@t26g2000prh.googlegroups.com...
>
> > Hi. Not sure if anyone can help here, but I'm trying to access #python
> > and have found out that my ISP has a blanket ban (for some reason).
> > Does anyone know how I can contact an operator to join? Thanks.
>
>


ISP is bethere and client is Chatzilla.
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to get millisec/fractional seconds out of a time object ?

2009-01-07 Thread M.-A. Lemburg


On 2009-01-06 22:34, da...@bag.python.org wrote:
> Thanks for help to a beginner.
> 
> script23
> import time
> import datetime
> start_time = datetime.datetime.now()
> time.sleep(0.14)
> end_time = datetime.datetime.now()
> datetime.timedelta = end_time - start_time
> print(datetime.timedelta)#  works, prints 0:00:0.141000
> print(datetime.timedelta.seconds)#  prints 0
> print(datetime.timedelta.milliseconds)  # fails 
>   < object has no attribute milliseconds >
> 
>How do I get the 0.141000 out of that or any time object ?
>On line docs are arcane to a novice.

If you're only interested in the fractional seconds part, you're
much better off with doing:

import time
start_time = time.time()
...
end_time = time.time()
delta_seconds = end_time - start_time
delta_milliseconds = delta_seconds * 1000

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 07 2009)
>>> Python/Zope Consulting and Support ...http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list


Re: [Mechanize.ClientForm] double reading from urllib2.urlopen

2009-01-07 Thread MRAB

tiktak.hod...@gmail.com wrote:

Hello, folks!
I use mechanize.clientform to parse HTML-forms. I preliminary check
response and call response.read().find("..."). But when it's taken to
ClientForm.ParseResponse, it can't parse because of response.read() is
zero-length text. The problem is that ClientForm.ParseResponse is not
taken text of response, only object.

Example:

import urllib
from ClientForm import ParseResponse
response = urllib.urlopen("http://yandex.ru";)
if -1 != response.read().find("foobar"):
pass
form = ParseResponse(response)[1] <-- there is exception IndexError

It might be that read() is consuming the data, so there's none remaining 
for the second read(). Try:


response = urllib.urlopen("http://yandex.ru";)
text = response.read()
if "foobar" in text: # preferred to find()
pass
form = ParseResponse(text)[1]
--
http://mail.python.org/mailman/listinfo/python-list


Re: FTP example going through a FTP Proxy

2009-01-07 Thread jakecjacobson
On Jan 7, 12:32 pm, jakecjacobson  wrote:
> Hi,
>
> I need to write a simple Python script that I can connect to a FTP
> server and download files from the server to my local box.  I am
> required to go through a FTP Proxy and I don't see any examples on how
> to do this.  The FTP proxy doesn't require username or password to
> connect but the FTP server that I am connecting to does.
>
> Any examples on how to do this would be greatly appreciated.  I am
> limited to using Python version 2.4.3 on a Linux box.

This is what I have tried so far,

import urllib

proxies = {'ftp':'ftp://proxy_server:21'}
ftp_server = 'ftp.somecompany.com'
ftp_port='21'
username = ''
password = 'secretPW'

ftp_string='ftp://' + username + '@' + password + ftp_server + ':' +
ftp_port


data = urllib.urlopen(ftp_string, proxies=proxies)

data=urllib.urlopen(req).read()

print data

I get the following error:

Traceback (most recent call last):
  File "./ftptest.py", line 22, in ?
data = urllib.urlopen(ftp_server, proxies=proxies)
  File "/usr/lib/python2.4/urllib.py", line 82, in urlopen
return opener.open(url)
  File "/usr/lib/python2.4/urllib.py", line 190, in open
return getattr(self, name)(url)
  File "/usr/lib/python2.4/urllib.py", line 470, in open_ftp
host, path = splithost(url)
  File "/usr/lib/python2.4/urllib.py", line 949, in splithost
match = _hostprog.match(url)
TypeError: expected string or buffer
--
http://mail.python.org/mailman/listinfo/python-list


Re: formatted 'time' data in calculations

2009-01-07 Thread Scott David Daniels

Ross wrote:
There seems to be no shortage of information around on how to use the 
time module, for example to use time.ctime() and push it into strftime 
and get something nice out the other side, but I haven't found anything 
helpful in going the other way.


As to a paucity of conversion formatting, there is no magic way to take
everyone's way of putting date and time information in text and convert
it to unambiguous format, in part because there are too many different
and contradictory formats.  When I write dates, I know what I intended;
when I read dates, I guess what the author intended.

Have you read the entire time module document?  If so, which functions
in that module take strings as arguments?

That is, given some formatted text describing times - is there something 
that makes it easy to calculate time differences, or do I have to index 
my way through the string pulling out characters, converting to integers 
etc...


Data is formatted:
   t1 = 09:12:10
   t2 = 11:22:14
I want to calculate tdiff = t2-t1


Do you do any work yourself?  Show us your attempts.  This looks like
a trivial exercise.  It seems that for less than four times the effort
of asking your question you might have found the answer.

Perhaps I am being too cranky this morning.
--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Any news on when some libraries will be ported to Python 3.0?

2009-01-07 Thread Stefan Behnel
Just Another Victim of the Ambient Morality wrote:
> I'm excited to use Python 3.0 (foolishly, it's the only Python 
> interpreter I have on my system) but there are no libraries for it beyond 
> the kitchen sink.  Personally, a good start would be Beautiful Soup and 
> Mechanize.  I could also use DB.
> Has there been any word on Beautiful Soup?

At least for new code, you can use lxml instead. Being written (mostly) in
Cython, it compiles and works in Py3.

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


State of the art: Tkinter, Tk 8.5, Tix?

2009-01-07 Thread excord80
Does Python work with Tk 8.5? I'm manually installing my own Python
2.6.1 (separate from my system's Python 2.5.2), and am about to
install my own Tcl/Tk 8.5 but am unsure how to make them talk to
eachother. Should I install Tk first? If I put Tk into my home
directory (under "~/opt" most likely), is there some configure option
I need to pass Python to tell it where to find my Tk?

Also, I see that Python comes with Tix. Was Tix supposed to be
something to make up for what was lacking in Tk prior to its 8.5
release? Is Tix here to stay, or has it been eclipsed by what comes
with Tk 8.5 OOTB?
--
http://mail.python.org/mailman/listinfo/python-list


del behavior

2009-01-07 Thread Eric Snow
I was reading in the documentation about __del__ and have a couple of
questions.  Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

What is globals referring to in the following text from that reference
page?

"Starting with version 1.5, Python guarantees that globals whose name
begins with a single underscore are deleted from their module before
other globals are deleted; if no other references to such globals
exist, this may help in assuring that imported modules are still
available at the time when the __del__() method is called."

Thus those with an _ get deleted before everything else.  This is not
referring to members of my objects is it, such that those members
starting with _ get deleted first?  I suppose that would delete
__del__ before it would get called so I assume that is not the case.
But I want to be sure about that behavior and exactly what globals
is.  Is globals meaning the contents of "globals" or something else.
I ask because sometimes some words get used for varied meanings.
--
http://mail.python.org/mailman/listinfo/python-list


Re: formatted 'time' data in calculations

2009-01-07 Thread Aaron Hill
I personally use epoch time since its absolute. I have a simple time clock
app that uses this method, from that it is easy to convert into human date:


[code]

def OnButtonIn(self,evt):

'create time stamp with ID/action'

'print to the rightFrame text'

if self.punchedIn:

print "Already punched in cannot punch again"

else:

seconds = time.time()

current = time.localtime(seconds)

day = time.localtime()

# time string can have characters 0..9, -, period, or space

timeday = time.strftime('%H:%M-%m.%d.%y', day)

formatday = time.strftime('%c', day)

self.ClockIntext = seconds

#self.text =  "Clock in: " + self.text

self.punchIn.SetValue(formatday)

self.punchedIn = True

self.punchedOut = False



def OnButtonOut(self,evt):

if self.punchedOut:

print "Already punched out!"

else:

'create time stamp with ID/action'

'print to the rightFrame text'

'write to time sheet'

seconds = time.time()

current = time.localtime(seconds)

day = time.localtime()

# time string can have characters 0..9, -, period, or space

timeday = time.strftime('%H:%M-%m.%d.%y', day)

formatday = time.strftime('%c', day)

self.ClockOuttext = seconds

self.punchOut.SetValue(formatday)

self.punchedIn = False

self.punchedOut = True

'create a file and write the table to it'

file = open('timesheet.txt', 'a')

file.write(str(self.ClockIntext))

file.write('\t')

file.write(str(self.ClockOuttext))

file.write('\n')

file.close()

return None



def OnButtonCalc(self,event):

'open the time sheet and calculate the total time'

file = open('timesheet.txt','r')

lines = file.readlines()

time1 = ''

time2 = ''

self.hours = 0.000

for punch in lines:

for x in punch:

if(len(x) <= 0):

self.hours = self.hours

else:

if x != '\t' and x!= '\n':

time1 = time1 + x

elif x == '\t':

time2 = time1

time1 = ''

elif x == '\n':

self.hours = self.hours +
abs(((float(time2)-float(time1))/60)/60)

time2 = ''

time1 = ''



self.total.SetValue('%2f' % self.hours)
[/code]

Oops wrong person, sorry about that. This time it should go to the mailing
list



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


del behavior 2

2009-01-07 Thread Eric Snow
I was reading in the documentation about __del__ and have a couple of
questions.  Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

My second question is about the following:

"It is not guaranteed that __del__() methods are called for objects
that still exist when the interpreter exits."

I understand that and have seen it too.  That's fine.  But how do any
of you deal with things that are left open because you did not get a
chance to close them?  How do you clean up after the fact?  Do you
simply keep track externally the things that need to be cleaned up if
__del__ doesn't get a chance?  Any ideas?  Thanks

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


Re: del behavior

2009-01-07 Thread Chris Rebert
On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:
> I was reading in the documentation about __del__ and have a couple of
> questions.  Here is what I was looking at:
>
> http://docs.python.org/reference/datamodel.html#object.__del__
>
> What is globals referring to in the following text from that reference
> page?

Globals are variables that have toplevel module scope. Basically, any
assignments, function definitions, or class definitions with no
indentation from the left margin will create a global variable. If you
can get at the variable by appending something of the form
"\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
function, then it's a global.

Cheers,
Chris

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


Re: del behavior 2

2009-01-07 Thread MRAB

Eric Snow wrote:

I was reading in the documentation about __del__ and have a couple of
questions.  Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

My second question is about the following:

"It is not guaranteed that __del__() methods are called for objects
that still exist when the interpreter exits."

I understand that and have seen it too.  That's fine.  But how do any
of you deal with things that are left open because you did not get a
chance to close them?  How do you clean up after the fact?  Do you
simply keep track externally the things that need to be cleaned up if
__del__ doesn't get a chance?  Any ideas?  Thanks


There's the 'with' statement and try...finally.
--
http://mail.python.org/mailman/listinfo/python-list


Re: del behavior 2

2009-01-07 Thread Chris Rebert
On Wed, Jan 7, 2009 at 11:42 AM, Eric Snow  wrote:
> I was reading in the documentation about __del__ and have a couple of
> questions.  Here is what I was looking at:
>
> http://docs.python.org/reference/datamodel.html#object.__del__
>
> My second question is about the following:
>
> "It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits."
>
> I understand that and have seen it too.  That's fine.  But how do any
> of you deal with things that are left open because you did not get a
> chance to close them?  How do you clean up after the fact?  Do you
> simply keep track externally the things that need to be cleaned up if
> __del__ doesn't get a chance?  Any ideas?  Thanks

As you point out, __del__ is not a reliable way to free limited
resources. Instead, one generally includes logic to explicitly free
the resources. This is generally done using try-finally or the `with`
statement.

Example:

def mess_with_file(f):
try:
#fiddle with the file
finally:
f.close() #guarantee that the file gets closed

def mess_with_other_file(filename):
with open(filename) as f:
#do stuff with file
x = None #the file has now been closed, and it'll be closed even
if an exception gets raised
#the "context handler" (see PEP 343) for the `file` type
guarantees this for us

Cheers,
Chris

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


Re: del behavior

2009-01-07 Thread Eric Snow
On Jan 7, 12:48 pm, "Chris Rebert"  wrote:
> On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:
> > I was reading in the documentation about __del__ and have a couple of
> > questions.  Here is what I was looking at:
>
> >http://docs.python.org/reference/datamodel.html#object.__del__
>
> > What is globals referring to in the following text from that reference
> > page?
>
> Globals are variables that have toplevel module scope. Basically, any
> assignments, function definitions, or class definitions with no
> indentation from the left margin will create a global variable. If you
> can get at the variable by appending something of the form
> "\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
> function, then it's a global.
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

Perfect!  that is kind of what I thought.  Thanks.

So any such in any module every variable in memory that starts with an
underscore will be deleted before the rest.  Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

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


Re: del behavior

2009-01-07 Thread Eric Snow
On Jan 7, 12:55 pm, Eric Snow  wrote:
> On Jan 7, 12:48 pm, "Chris Rebert"  wrote:
>
>
>
> > On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:
> > > I was reading in the documentation about __del__ and have a couple of
> > > questions.  Here is what I was looking at:
>
> > >http://docs.python.org/reference/datamodel.html#object.__del__
>
> > > What is globals referring to in the following text from that reference
> > > page?
>
> > Globals are variables that have toplevel module scope. Basically, any
> > assignments, function definitions, or class definitions with no
> > indentation from the left margin will create a global variable. If you
> > can get at the variable by appending something of the form
> > "\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
> > function, then it's a global.
>
> > Cheers,
> > Chris
>
> > --
> > Follow the path of the Iguana...http://rebertia.com
>
> Perfect!  that is kind of what I thought.  Thanks.
>
> So any such in any module every variable in memory that starts with an
> underscore will be deleted before the rest.  Then this does not affect
> the order in which variables are deleted in instances of my classes,
> and thus all my class and instance variables (including methods) are
> available when the __del__ of the class instance is called?
>
> -eric

Typo.

Perfect!  that is kind of what I thought.  Thanks.

So in any module every such variable in memory that starts with an
underscore will be deleted before the rest.  Then this does not affect
the order in which variables are deleted in instances of my classes,
and thus all my class and instance variables (including methods) are
available when the __del__ of the class instance is called?

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


Re: del behavior 2

2009-01-07 Thread Martin v. Löwis
> I understand that and have seen it too.  That's fine.  But how do any
> of you deal with things that are left open because you did not get a
> chance to close them?  How do you clean up after the fact?  Do you
> simply keep track externally the things that need to be cleaned up if
> __del__ doesn't get a chance?  Any ideas?  Thanks

You should try to write you program so that any kind of process exit
will not need any cleanup. For many kinds of things, this will work
automatically on most file systems. For example, file handles and
network connections get automatically closed - so you don't absolutely
have to close them if your program exits abnormally. Likewise, database
connections will shut down properly, and windows will close just fine.

What kind of thing do you have that remains open even after the
process terminates?

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


Re: del behavior 2

2009-01-07 Thread Eric Snow
On Jan 7, 12:57 pm, "Chris Rebert"  wrote:
> On Wed, Jan 7, 2009 at 11:42 AM, Eric Snow  wrote:
> > I was reading in the documentation about __del__ and have a couple of
> > questions.  Here is what I was looking at:
>
> >http://docs.python.org/reference/datamodel.html#object.__del__
>
> > My second question is about the following:
>
> > "It is not guaranteed that __del__() methods are called for objects
> > that still exist when the interpreter exits."
>
> > I understand that and have seen it too.  That's fine.  But how do any
> > of you deal with things that are left open because you did not get a
> > chance to close them?  How do you clean up after the fact?  Do you
> > simply keep track externally the things that need to be cleaned up if
> > __del__ doesn't get a chance?  Any ideas?  Thanks
>
> As you point out, __del__ is not a reliable way to free limited
> resources. Instead, one generally includes logic to explicitly free
> the resources. This is generally done using try-finally or the `with`
> statement.
>
> Example:
>
> def mess_with_file(f):
>     try:
>         #fiddle with the file
>     finally:
>         f.close() #guarantee that the file gets closed
>
> def mess_with_other_file(filename):
>     with open(filename) as f:
>         #do stuff with file
>     x = None #the file has now been closed, and it'll be closed even
> if an exception gets raised
>     #the "context handler" (see PEP 343) for the `file` type
> guarantees this for us
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

Thanks for the responses.  What I mean is when a python process is
interrupted and does not get a chance to clean everything up then what
is a good way to do so?  For instance, I have a script that uses child
ptys to facilitate ssh connections (I'm using pxssh).  When I ^C the
python process I am left with the child processes running and the ssh
connections open.  Naturally I run out of ttys if this happens too
much, which I have had happen.  So if python does not get a chance to
take care of those, what is a good way to do so?  Does a try/finally
or a with statement address that?  Thanks!

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


Re: del behavior 2

2009-01-07 Thread Eric Snow
On Jan 7, 1:03 pm, Eric Snow  wrote:
> On Jan 7, 12:57 pm, "Chris Rebert"  wrote:
>
>
>
> > On Wed, Jan 7, 2009 at 11:42 AM, Eric Snow  wrote:
> > > I was reading in the documentation about __del__ and have a couple of
> > > questions.  Here is what I was looking at:
>
> > >http://docs.python.org/reference/datamodel.html#object.__del__
>
> > > My second question is about the following:
>
> > > "It is not guaranteed that __del__() methods are called for objects
> > > that still exist when the interpreter exits."
>
> > > I understand that and have seen it too.  That's fine.  But how do any
> > > of you deal with things that are left open because you did not get a
> > > chance to close them?  How do you clean up after the fact?  Do you
> > > simply keep track externally the things that need to be cleaned up if
> > > __del__ doesn't get a chance?  Any ideas?  Thanks
>
> > As you point out, __del__ is not a reliable way to free limited
> > resources. Instead, one generally includes logic to explicitly free
> > the resources. This is generally done using try-finally or the `with`
> > statement.
>
> > Example:
>
> > def mess_with_file(f):
> >     try:
> >         #fiddle with the file
> >     finally:
> >         f.close() #guarantee that the file gets closed
>
> > def mess_with_other_file(filename):
> >     with open(filename) as f:
> >         #do stuff with file
> >     x = None #the file has now been closed, and it'll be closed even
> > if an exception gets raised
> >     #the "context handler" (see PEP 343) for the `file` type
> > guarantees this for us
>
> > Cheers,
> > Chris
>
> > --
> > Follow the path of the Iguana...http://rebertia.com
>
> Thanks for the responses.  What I mean is when a python process is
> interrupted and does not get a chance to clean everything up then what
> is a good way to do so?  For instance, I have a script that uses child
> ptys to facilitate ssh connections (I'm using pxssh).  When I ^C the
> python process I am left with the child processes running and the ssh
> connections open.  Naturally I run out of ttys if this happens too
> much, which I have had happen.  So if python does not get a chance to
> take care of those, what is a good way to do so?  Does a try/finally
> or a with statement address that?  Thanks!
>
> -eric

pxssh uses pexpect which uses pty.fork
--
http://mail.python.org/mailman/listinfo/python-list


Re: del behavior

2009-01-07 Thread MRAB

Chris Rebert wrote:

On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:

I was reading in the documentation about __del__ and have a couple of
questions.  Here is what I was looking at:

http://docs.python.org/reference/datamodel.html#object.__del__

What is globals referring to in the following text from that reference
page?


Globals are variables that have toplevel module scope. Basically, any
assignments, function definitions, or class definitions with no
indentation from the left margin will create a global variable. If you
can get at the variable by appending something of the form
"\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
function, then it's a global.

Actually, the amount of indentation doesn't matter. What matters is 
whether it's within a 'def' or 'class' statement or not.

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


Re: del behavior

2009-01-07 Thread Chris Rebert
On Wed, Jan 7, 2009 at 11:55 AM, Eric Snow  wrote:
> On Jan 7, 12:48 pm, "Chris Rebert"  wrote:
>> On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:
>> > I was reading in the documentation about __del__ and have a couple of
>> > questions.  Here is what I was looking at:
>>
>> >http://docs.python.org/reference/datamodel.html#object.__del__
>>
>> > What is globals referring to in the following text from that reference
>> > page?
>>
>> Globals are variables that have toplevel module scope. Basically, any
>> assignments, function definitions, or class definitions with no
>> indentation from the left margin will create a global variable. If you
>> can get at the variable by appending something of the form
>> "\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
>> function, then it's a global.
>>
>> Cheers,
>> Chris
>>
>> --
>> Follow the path of the Iguana...http://rebertia.com
>
> Perfect!  that is kind of what I thought.  Thanks.
>
> So any such in any module every variable in memory that starts with an
> underscore will be deleted before the rest.  Then this does not affect
> the order in which variables are deleted in instances of my classes,
> and thus all my class and instance variables (including methods) are
> available when the __del__ of the class instance is called?

Indeed. The underscore special-casing only applies to modules.

Cheers,
Chris

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


Re: del behavior

2009-01-07 Thread Chris Rebert
On Wed, Jan 7, 2009 at 12:05 PM, MRAB  wrote:
> Chris Rebert wrote:
>>
>> On Wed, Jan 7, 2009 at 11:39 AM, Eric Snow  wrote:
>>>
>>> I was reading in the documentation about __del__ and have a couple of
>>> questions.  Here is what I was looking at:
>>>
>>> http://docs.python.org/reference/datamodel.html#object.__del__
>>>
>>> What is globals referring to in the following text from that reference
>>> page?
>>
>> Globals are variables that have toplevel module scope. Basically, any
>> assignments, function definitions, or class definitions with no
>> indentation from the left margin will create a global variable. If you
>> can get at the variable by appending something of the form
>> "\nSomeIdentifierHere\n" to the module's file, and it's not a built-in
>> function, then it's a global.
>>
> Actually, the amount of indentation doesn't matter. What matters is whether
> it's within a 'def' or 'class' statement or not.

Yes, but those do require you *to indent* (though so do while & if for
that matter); I just couldn't seem to come up with a better
description of the rule at the time.
But you are correct and yours is a much better description of the rule.

Cheers,
Chris

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


Re: State of the art: Tkinter, Tk 8.5, Tix?

2009-01-07 Thread Roger
On Jan 7, 2:31 pm, excord80  wrote:
> Does Python work with Tk 8.5? I'm manually installing my own Python
> 2.6.1 (separate from my system's Python 2.5.2), and am about to
> install my own Tcl/Tk 8.5 but am unsure how to make them talk to
> eachother. Should I install Tk first? If I put Tk into my home
> directory (under "~/opt" most likely), is there some configure option
> I need to pass Python to tell it where to find my Tk?
>
> Also, I see that Python comes with Tix. Was Tix supposed to be
> something to make up for what was lacking in Tk prior to its 8.5
> release? Is Tix here to stay, or has it been eclipsed by what comes
> with Tk 8.5 OOTB?

I'm curious about this too.  If no one responds please let us know how
it goes.
--
http://mail.python.org/mailman/listinfo/python-list


Re: compiling python2.5 on linux under wine

2009-01-07 Thread Luke Kenneth Casson Leighton
On Sat, Jan 3, 2009 at 9:22 PM, Luke Kenneth Casson Leighton
 wrote:

> hey, has anyone investigated compiling python2.5 using winegcc, under wine?

some people might find this kind of thing amusing.  it's considered in
very obtuse circles to be "progress"... :)


l...@gonzalez:/mnt/src/python2.5-2.5.2/Lib$ ../build/python -v
Could not find platform independent libraries 
Could not find platform dependent libraries 
Consider setting $PYTHONHOME to [:]
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
'import site' failed; traceback:
ImportError: No module named site
Python 2.5.2 (r252:60911, Jan  7 2009, 20:33:53) [gcc] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
[]
[]
[]
import sre_compile # from Z:\mnt\src\python2.5-2.5.2\Lib\sre_compile.py
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
fixme:msvcrt:MSVCRT__sopen : pmode 0x01b6 ignored
# wrote Z:\mnt\src\python2.5-2.5.2\Lib\sre_compile.pyc
import _sre # builtin
import sre_constants # from Z:\mnt\src\python2.5-2.5.2\Lib\sre_constants.py
# wrote Z:\mnt\src\python2.5-2.5.2\Lib\sre_constants.pyc
import sre_parse # from Z:\mnt\src\python2.5-2.5.2\Lib\sre_parse.py
# wrote Z:\mnt\src\python2.5-2.5.2\Lib\sre_parse.pyc
Traceback (most recent call last):
  File "", line 1, in 
  File "site.py", line 415, in 
main()
  File "site.py", line 406, in main
aliasmbcs()
  File "site.py", line 356, in aliasmbcs
import locale, codecs
  File "Z:\mnt\src\python2.5-2.5.2\Lib\locale.py", line 167, in 
import re, operator
  File "Z:\mnt\src\python2.5-2.5.2\Lib\re.py", line 223, in 
_pattern_type = type(sre_compile.compile("", 0))
  File "Z:\mnt\src\python2.5-2.5.2\Lib\sre_compile.py", line 530, in compile
groupindex, indexgroup
OverflowError: signed integer is less than minimum
>>>
--
http://mail.python.org/mailman/listinfo/python-list


How to store passwords?

2009-01-07 Thread Oltmans
I'm writing a program in which I will ask users to enter user name and
password once only. It's a console based program that will run on
Windows XP. Actually, I'm trying to provide the similar functionality
as "Remember me" thing in browsers. For that, I will need to store
user name and passwords on the disk. I don't have a background in
Crypto so how do you suggest I do that? What algorithms shall I be
using? Moreover, I cannot use a whole library to do that due to
certain issues. However, I can use like 1--2 files that will be
shipped along with the main script. Any ideas? Any help will be really
appreciated. Thanks.
--
http://mail.python.org/mailman/listinfo/python-list


Re: looking for tips on how to implement "ruby-style" Domain Specific Language in Python

2009-01-07 Thread J Kenneth King
Kay Schluehr  writes:

> On 7 Jan., 16:50, J Kenneth King  wrote:
>
>> Python expressions are not
>> data types either and hence no macros -- I can't write a python function
>> that generates python code at compile time.
>
> Have you ever considered there are languages providing macros other
> than Lisp?

Of course.

> Macros have nothing to do with homoiconcity.

Not directly, no.

>> I can only write a python
>> program that parses some other string and generates code that can be run
>> by another interpreter.
>
> No, it is the same interpreter and it is also possible to modify
> python parsers on the fly. This is just not possible with Pythons
> builtin parser.

PyPy is probably the best bet when/if it gets finished.

>
>
>>
>> Consider:
>>
>> for i in range(0, 100):
>>     do_something_interesting(i)
>>
>> That's a pretty straight forward Python expression, but I can't do
>> anything with it -- it's not a unit of data, it's a string.
>>
>> The distinction is not subtle by any means.
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-07 Thread Dan Esch
Wait a sec...

I think I get this...

In essence, the implication of immutability for Python is that there is only
one "parrot", one "spam,"in fact one anything. (This seems like it must hold
for data primitives - does it hold for complex objects as well? It seems it
must...) In addition there is only one 1, and one 2 etc.  We may or may not
have realized that string in a memory address to which variable names can be
bound, but should we do so, there is only one "parrot"

Python, is in fact, a Platonic programming language.  Weird.  If I've got
this right, worth chewing on


On 1/2/09, Derek Martin  wrote:
>
> On Tue, Dec 30, 2008 at 02:21:29PM +, John O'Hagan wrote:
> > Fortunately, unlike the murky world of philosophy, Python (AIUI)
> > simplifies this question by simply declaring that yes, in the case
> > of mutable objects, we may say that we are still referring to the
> > same object although we've changed it, and no, in the case of
> > immutable objects, we may not, and must exchange it if we want a
> > different "value" (a word too fraught with ambiguity in this context
> > to use unquoted!).
>
> That's sort of true; it would seem to be more accurate to say that
> whenever a name is assigned to an object and subsequently reassigned,
> the name no longer is associated with the original object.  In the
> case of mutable objects, the object can be changed by performing an
> assignment of *part* of the object through its original name, i.e.
> strings may be mutable, but the following code still produces two
> different objects:
>
> a = 'hello'
> a = 'goodbye'
>
> The first object so created is orphaned; it's been given the Russian
> non-person treatment.  It still exists, but the authorities (i.e. the
> python interpreter) don't acknowledge it and provide the rest of the
> world no way to communicate with it, and eventually it is reaped by
> the garbage collector. :)
>
> What the Python community often overlooks, when this discussion again
> rears its ugly head (as it seems to every other hour or so), is that
> its assignment model is BIZARRE, as in it's conceptually different
> from virtually all other languages substantially taught in
> undergraduate computer science programs.  And for that matter, it's
> pretty unintuitive generally.
>
> That is, in what I'll call "normal" computer languages, a variable
> name is thought of as the address of a bin where some data is stored,
> and the name is inexorably tied to that bin.  You can change what's in
> the bin, but the name you gave the bin always points to the same bin.
> This tends to be conceptually true even if it might technically not be
> true in a given implementation of a language.
>
> Python is very different from this.  Names are not addresses of bins;
> they are instead simply ephemeral labels which are given to bins,
> where the bin is a Python object which contains specific data at the
> time of assignment.  A second assignment of that name doesn't change
> what's in the original bin; it actually (probably) first creates a new
> bin, then removes the name from the original bin and assigns it to
> the new one.  Intuitively, it's a bit like saying your kitchen table
> is no longer a kitchen table, and now the thing where you wash your
> dishes is a kitchen table.  It doesn't really make a lot of sense
> (whether or not it's so for good reason), and it makes describing the
> assignment model necessarily convoluted, whereas the "named bins"
> model from the majority of other languages people are likely to have
> been exposed to is simple and sensible.
>
> It's small wonder that neophytes try to cram Python behaviors into
> terms and computing concepts they already understand from learning
> other languages, and that they fail to do so.  What's mystifying is
> that when Pythonistas reply to their messages, they universally seem
> confused at how this could possibly happen, and often enough actually
> seem offended (or at least offensive) when it inevitably does happen...
>
> --
> Derek D. Martin
> http://www.pizzashack.org/
> GPG Key ID: 0x81CFE75D
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
>
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >