Re: SDL doesn't cope well with FreeSans

2006-07-27 Thread greg
Carl Banks wrote:
 Greg Ewing wrote:
  The characters come out slightly
  higglety-pigglety -- randomly displaced up or down
  a pixel or so from the baseline.

 It would depend on how you're displaying them, I would think.

I've seen the same thing happen two different ways:
* Rendering with PyGame's font functions
* With Soya, which is using OpenGL textures

Both of these are using FreeType to do the rastering,
I believe.

 2. Convert it to a type 1 font and see if the problem remains.

Can FreeType deal with Type 1 fonts? Also, what utility
would I use to do that (preferably on MacOSX).

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray
Hey Steve,

Yes, I agree with you. The lack of checking can get confusing fast.
It's not about typing without errors. Regardless of how you train as
long as you're human you WILL make typos.

Also having to check whether a name has already existed can be a major
pain in the butt with Python. With Java you always know when a name has
already existed, and what type is bound to the name. I consider this to
be a Good Thing (tm).

Regarding the lack of privacy, I actually like the C++ way of doing it.
Privacy is there, but you have the choice to break it (using friend)
if you want/need to. C++ doesn't make it easy for you to do it
inadvertently, like you described in the example.

The argument against this is that since development with Python is so
rapid, you're supposed to always equip your code with extensive unit
tests. I like Python but I've never really bought that argument--I
guess I've been doing Java too long :)

Cheers
Ray


Steve Jobless wrote:
 Sybren Stuvel wrote:
 
  Steve Jobless enlightened us with:
   The first case can be just a typo, like:
  
 x.valeu = 5
  
   I make typos all the time. Without a spell checker, this message
   would be unreadable :).
 
  Then learn to read what you type, as you type it. Typing without
  errors can be trained.

 I'd rather let a machine to do that. Wasn't computer created for tasks
 like this? (No, not really. But...)

 
   The second case can be like:
  
 x.next = y
 y.next = None
  
   to create a linked list by piggybacking next to the class. It will
   overwrite the iterater for the class if defined.
 
  You shouldn't simply pick a name and assign something to it without
  checking the current use of that name. It's pretty much true for
  everything in life.

 Well, the choice of next was not a good example. Sure, no one with
 decent Python knowledge would do that.
 But what about adding a method to the class? Am I supposed to ask Is
 anyone using name xxx? The class may be used by  developers I don't
 even know and some of them may be on the other side of the planet...

 
   If I was working on a large project with many engineers, I'd assume
   someone will do things like this sooner or later. I've seen many
   horrendous code in my life and I have no control over who I work
   with.
 
  Long live a versioning system. With that, you can find the person
  writing the horrible code, and slap them on the back of the head.
  People, like all animals, can be trained into doing the right thing.

 I'd like to. But often those people are long gone for one reason or
 another. Sometimes, it happens to be my boss...


 Maybe I should ask the question in a different way:

 What are the benefits from this? There must be something really good
 (and hopefully safe) you can do with it only from the outside. I don't
 have much problem over-riding functions from the inside of the class.
 But from the outside, I fail to see why anyone needs to add attributes
 or over-ride functions.
 
 SJ

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


Re: binding more than one attribute in a facntion

2006-07-27 Thread [EMAIL PROTECTED]

Peter Otten wrote:
 [EMAIL PROTECTED] wrote:

 Bound methods are limited to one implicit parameter. What you need is
 partial function application:

  def f(a, b, c):
 ... return a + b + c
 ...
  def partial(f, *args):
 ... def g(*more):
 ... return f(*args+more)
 ... return g
 ...
  partial(f, 1, 2)(3)
 6
  partial(f, 1)(2, 3)
 6
  partial(f)(1, 2, 3)
 6

 See http://www.python.org/dev/peps/pep-0309/ for more.

Thanks, this explanation is great. Nice to see also that python 2.5
will have it,

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


Re: Scope, type and UnboundLocalError

2006-07-27 Thread Paddy

Paddy wrote:
 Hi,
 I am trying to work out why I get UnboundLocalError when accessing an
 int from a function where the int is at the global scope, without
 explicitly declaring it as global but not when accessing a list in
 similar circumstances.

 The documentation: http://docs.python.org/ref/naming.html does not give
 me enough info to determine why the difference exists as it does not
 seem to mention types at all..

 The code:

 = scope_and_type.py ===
 m = 0
 n = [0]

 def int_access0():
 m = m + 1
 return m
 def int_access1():
 m += 1
 return m
 def list_access0():
 n[0] = n[0] + 1
 return n
 def list_access1():
 n[0] += 1
 return n

 try:
 print \nint_access0:, int_access0()
 except UnboundLocalError, inst:
 print  ERROR:\n, inst
 try:
 print \nint_access1:, int_access1()
 except UnboundLocalError, inst:
 print  ERROR:\n, inst
 try:
 print \nlist_access0:, list_access0()
 except UnboundLocalError, inst:
 print  ERROR:\n, inst
 try:
 print \nlist_access1:, list_access1()
 except UnboundLocalError, inst:
 print  ERROR:\n, inst


 print \n (m,n) = , (m,n)


 p = (0,)
 def tuple_access():
 return p[0]
 try:
 print \ntuple_acces:, tuple_access()
 except UnboundLocalError, inst:
 print  ERROR:\n, inst
 print \n p = , p

 = END scope_and_type.py ===

 The output:
 
 int_access0:  ERROR:
 local variable 'm' referenced before assignment

 int_access1:  ERROR:
 local variable 'm' referenced before assignment

 list_access0: [1]

 list_access1: [2]

  (m,n) =  (0, [2])

 tuple_acces: 0

  p =  (0,)
 

I blogged the following as a summary:
(From:
http://paddy3118.blogspot.com/2006/07/python-functions-assignments-and-scope.html)

 Python Functions: Assignments And Scope

Explaining why this works:

n = [0]
def list_access():
   n[0] = n[0] + 1
   return n

try:
   print \nlist_access:, list_access()
except UnboundLocalError, inst:
   print  ERROR:\n, inst

And this throws the exception:

m = 0
def int_access():
   m = m + 1
   return m

try:
   print \nint_access:, int_access()
except UnboundLocalError, inst:
   print  ERROR:\n, inst

To execute a source program, the Python compiler compiles your
original source into 'byte codes' - a form of your program that
is easier for the Python interpreter to later run. In generating this
byte code, the byte code compiler will determine which variable names
in a function are local to that function, (so alowing it to optimise
accesses to such local names).

The rule for determining if a variable is local to a function is:

* If there is a global statement for the name in the function
  then the name is accessed from the global scope.
* If there is no global statement for the name, and if there
  are assignments to the 'bare' name within the function then the
name
  is of local scope.
  ( A bare name assignment means assignment to a
  name, where the name occurs without attribute references,
  subscripts, or slicing s, just the bare name).
* Otherwise the name will be looked up in reverse order of all
  enclosing scopes, until it is found.

In the second example, function int_access; name m is flagged as
local by the byte code compiler as the bare name is being assigned
to. The interpreter therefore looks for a value of m to increment
only in the local scope, cannot find a value, then raises the
UnboundLocalError exception.
In function list_access, the bare
name n is not assigned to, so n is found when looking back
through enclosing scopes.
References

   1.


http://groups.google.com/group/comp.lang.python/browse_frm/thread/db9955da70c4e0ca
   2.

  http://pyref.infogami.com/assignments
   3.

  http://pyref.infogami.com/naming-and-binding
   4.

  http://www.python.org/doc/2.4/ref/global.html

END.

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


write()

2006-07-27 Thread manuhack
I copied the lines

f=open('/tmp/workfile', 'w')
print f
f.close()

from Python 2.4 Documentation 7.2.  But it said IOerror No such file or
directory '/tmp/workfile'

Is it something about the os?  I'm using Python 2.4 under WinXP.
Thanks.  Without / I can open it.

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


Re: How to force a thread to stop

2006-07-27 Thread H J van Rooyen
Carl J. Van Arsdall [EMAIL PROTECTED] wrote:

8


| point).  Its not only important that the threads die, but that they die
| with grace.  There's lots of cleanup work that has to be done when
| things exit or things end up in an indeterminable state.
|
| So, I feel like I have a couple options,
|
|  1) try moving everything to a process oriented configuration - we think
| this would be bad, from a resource standpoint as well as it would make
| things more difficult to move to a fully distributed system later, when
| I get my army of code monkeys.
|
| 2) Suck it up and go straight for the distributed system now - managers
| don't like this, but maybe its easier than I think its going to be, I dunno
|
| 3) See if we can find some other way of getting the threads to terminate.
|
| 4) Kill it and clean it up by hand or helper scripts - we don't want to
| do this either, its one of the major things we're trying to get away from.

8-

This may be a stupid suggestion - If I understand what you are doing, its
essentially running a bunch of compilers with different options on various
machines around the place - so there is a fifth option - namely to do nothing -
let them finish and just throw the output away - i.e. just automate the
cleanup...

- Hendrik


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


MySql

2006-07-27 Thread miker2
HI,

I'm having trouble writing to a MySql db using python and the MySQLdb
module. Here is the code:

import MySQLdb
base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
db=test_py)
cursor = base.cursor()
cursor.execute(INSERT INTO table (field) VALUES (int))

this does not work but the interesting thing is, there is an
AUTO_INCREMENT
field. Now say i had a couple of entries in there already:
   auto  table
1|90
2|32

and then i run my py script 3 times, the data is not entered but if i
add
another entry from mysql the auto increment field will have counted the

python entries:
   autotable
 1|90
 2|32
 6|47

please tell me what i am doing wrong. thanks.

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


Re: write()

2006-07-27 Thread miker2

manuhack wrote:
 I copied the lines

 f=open('/tmp/workfile', 'w')
 print f
 f.close()

 from Python 2.4 Documentation 7.2.  But it said IOerror No such file or
 directory '/tmp/workfile'

 Is it something about the os?  I'm using Python 2.4 under WinXP.
 Thanks.  Without / I can open it.


f=open(r'c:\blah\blah').read()

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


Re: write()

2006-07-27 Thread manuhack
How about write mode?  Changing r to w doesn't work...

[EMAIL PROTECTED] wrote:
 manuhack wrote:
  I copied the lines
 
  f=open('/tmp/workfile', 'w')
  print f
  f.close()
 
  from Python 2.4 Documentation 7.2.  But it said IOerror No such file or
  directory '/tmp/workfile'
 
  Is it something about the os?  I'm using Python 2.4 under WinXP.
  Thanks.  Without / I can open it.
 
 
 f=open(r'c:\blah\blah').read()

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


Re: Intermittent permission denied errors when using os.rename and a recently deleted path??

2006-07-27 Thread Neil Hodgson
Russell Warren:

 I'm actually running both... but I would think that once os.remove
 returns that the file is actually gone from the hdd.  Why would either
 application be blocking access to a non-existent file?

Does it actually tell you the target is the problem? I see an 
OSError: [Errno 17] File exists for that case, not a permission error. 
A permission error could occur, for example, if GDS has the source open 
or locked when you call os.rename.

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


Re: write()

2006-07-27 Thread miker2

manuhack wrote:
 How about write mode?  Changing r to w doesn't work...

 [EMAIL PROTECTED] wrote:
  manuhack wrote:
   I copied the lines
  
   f=open('/tmp/workfile', 'w')
   print f
   f.close()
  
   from Python 2.4 Documentation 7.2.  But it said IOerror No such file or
   directory '/tmp/workfile'
  
   Is it something about the os?  I'm using Python 2.4 under WinXP.
   Thanks.  Without / I can open it.
 
 
  f=open(r'c:\blah\blah').read()

what is it that you want to do??

in XP if i have or want to create a file in c:\ called test.txt i would
do this:

f = open(r'c:\test.txt', 'w')
f.write(rock the house party)
f.close()

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


Re: builtin function compile exceptions thrown?

2006-07-27 Thread Martin v. Löwis
James Thiele wrote:
 What exceptions (if any) can the python builtin compile() function
 throw besides SyntaxError?

- TypeError, if the parameters are wrong/too many/too few
- Any errors that a codec may raise, if there is an encoding
  declaration, plus (pre 2.5) MemoryError if the encoding is
  unknown.
- MemoryError, if you run out of memory
- SystemError, for various internal-error conditions

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


Re: MySql

2006-07-27 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], miker2 wrote:

 import MySQLdb
 base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
 db=test_py)
 cursor = base.cursor()
 cursor.execute(INSERT INTO table (field) VALUES (int))
 
 this does not work but the interesting thing is, there is an
 AUTO_INCREMENT
 field. Now say i had a couple of entries in there already:
auto  table
 1|90
 2|32
 
 and then i run my py script 3 times, the data is not entered but if i
 add
 another entry from mysql the auto increment field will have counted the
 
 python entries:
autotable
  1|90
  2|32
  6|47
 
 please tell me what i am doing wrong. thanks.

Where's the problem?  Do you mind that the third entry has a 6 as unique
`auto` value?  Doesn't `AUTO_INCREMENT` just guarantee unique values?

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


Re: How to find difference in years between two dates?

2006-07-27 Thread Bruno Desthuilliers
thebjorn wrote:
 Bruno Desthuilliers wrote:
 [...]
 
Possible solution:

import mx.DateTime as dt
def age(date):
return dt.Age(dt.today(), date).years
born = dt.Date(1967, 5, 1)
assert age(born) == 39
 
 
 dealbreaker:

age(datetime.date(1970,5,2))

(snip traceback)

What about:
 age(dt.Date(1970,5,2))

 I'm getting data from a database, and conversions
 are out of the
 question for something like this.

Which conversion ? How do you get the data ? as a datetime object ? as a
(y,m,d) tuple ? as a y-m-d string ? Else ?



-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads vs Processes

2006-07-27 Thread [EMAIL PROTECTED]
John Henry wrote:
 
  Carl,
   OS writers provide much more tools for debugging, tracing, changing
  the priority of, sand-boxing processes than threads (in general) It
  *should* be easier to get a process based solution up and running
  andhave it be more robust, when compared to a threaded solution.
 
  - Paddy (who shies away from threads in C and C++ too ;-)

 That mythical process is more robust then thread application
 paradigm again.

 No wonder there are so many boring software applications around.

 Granted.  Threaded program forces you to think and design your
 application much more carefully (to avoid race conditions, dead-locks,
 ...) but there is nothing inherently *non-robust* about threaded
 applications.

Indeed.  Let's just get rid of all preemptive multitasking while we're
at it; MacOS9's cooperative, non-memory-protected system wasn't
inherently worse as long as every application was written properly.
There was nothing inherently non-robust about it!

The key difference between threads and processes is that threads share
all their memory, while processes have memory protection except with
particular segments of memory they choose to share.

The next most important difference is that certain languages have
different support for threads/procs.  If you're writing a Python
application, you need to be aware of the GIL and its implications on
multithreaded performance.  If you're writing a Java app, you're
handicapped by the lack of support for multiprocess solutions.

The third most important difference--and it's a very distant
difference--is the performance difference.  In practice, most
well-designed systems will be pooling threads/procs and so startup time
is not that critical.  For some apps, it may be.  Context switching
time may differ, and likewise that is not usually a sticking point but
for particular programs it can be.  On some OSes, launching a
copy-on-write process is difficult--that used to be a reason to choose
threads over procs on Windows, but nowadays all modern Windows OSes
offer a CreateProcessEx call that allows full-on COW processes.

In general, though, if you want to share _all_ memory or if you have
measured and context switching sucks on your OS and is a big factor in
your application, use threads.  In general, if you don't know exactly
why you're choosing one or the other, or if you want memory protection,
robustness in the face of programming errors, access to more 3rd-party
libraries, etc, then you should choose a multiprocess solution.

(OS designers spent years of hard work writing OSes with protected
memory--why voluntarily throw that out?)

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


Re: How to force a thread to stop

2006-07-27 Thread Paul Rubin
Dennis Lee Bieber [EMAIL PROTECTED] writes:
   Ugh... Seems to me it would be better to find some Python library
 for SSH, something similar to telnetlib, rather than doing an
 os.system() per command line. EACH of those os.system() calls probably
 causes a full fork() operation on Linux/UNIX, and the equivalent on
 Windows (along with loading a command shell interpreter to handle the
 actual statement).

I think Carl is using Linux, so the awful overhead of process creation
in Windows doesn't apply.  Forking in Linux isn't that big a deal.
os.system() usually forks a shell, and the shell forks the actual
command, but even two forks per ssh is no big deal.  The Apache web
server usually runs with a few hundred processes, etc.  Carl, just how
many of these ssh's do you need active at once?  If it's a few hundred
or less, I just wouldn't worry about these optimizations you're asking
about.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads vs Processes

2006-07-27 Thread [EMAIL PROTECTED]
Russell Warren wrote:
 This is something I have a streak of paranoia about (after discovering
 that the current xmlrpclib has some thread safety issues).  Is there a
 list maintained anywhere of the modules that are aren't thread safe?


It's much safer to work the other way: assume that libraries are _not_
thread safe unless they're listed as such.  Even things like the
standard C library on mainstream Linux distributions are only about 7
years into being thread-safe by default, anything at all esoteric you
should assume is not until you investigate and find documentation to
the contrary.

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


Re: How to force a thread to stop

2006-07-27 Thread bryanjugglercryptographer

Carl J. Van Arsdall wrote:
 [EMAIL PROTECTED] wrote:
  Carl J. Van Arsdall wrote:
 
  [EMAIL PROTECTED] wrote:
 
  Carl J. Van Arsdall wrote:
 
  I don't get what threading and Twisted would to do for
  you. The problem you actually have is that you sometimes
  need terminate these other process running other programs.
  Use spawn, fork/exec* or maybe one of the popens.
 
 
  I have a strong need for shared memory space in a large distributed
  environment.
 
 
  Distributed shared memory is a tough trick; only a few systems simulate
  it.
 
 Yea, this I understand, maybe I chose some poor words to describe what I
 wanted.

Ya' think?  Looks like you have no particular need for shared
memory, in your small distributed system.

 I think this conversation is getting hairy and confusing so  I'm
 going to try and paint a better picture of what's going on.  Maybe this
 will help you understand exactly what's going on or at least what I'm
 trying to do, because I feel like we're just running in circles.
[...]

So step out of the circles already. You don't have a Python thread
problem. You don't have a process overhead problem.

[...]
 So, I have a distributed build system. [...]

Not a trivial problem, but let's not pretend we're pushing the
state of the art here.

Looks like the system you inherited already does some things
smartly: you have ssh set up so that a controller machine can
launch various build steps on a few dozen worker machines.

[...]
 The threads invoke a series
 of calls that look like

 os.system(ssh host command)

 or for more complex operations they would just spawn a process that ran
 another python script)

 os.system(ssh host script)
[...]
 Alright, so this scheme that was first put in place kind of worked.
 There were some problems, for example when someone did something like
 os.system(ssh host script)  we had no good way of knowing what the
 hell happened in the script.

Yeah, that's one thing we've been telling you. The os.system()
function doesn't give you enough information nor enough control.
Use one of the alternatives we've suggested -- probably the
subprocess.Popen class.

[...]
 So, I feel like I have a couple options,

  1) try moving everything to a process oriented configuration - we think
 this would be bad, from a resource standpoint as well as it would make
 things more difficult to move to a fully distributed system later, when
 I get my army of code monkeys.

 2) Suck it up and go straight for the distributed system now - managers
 don't like this, but maybe its easier than I think its going to be, I dunno

 3) See if we can find some other way of getting the threads to terminate.

 4) Kill it and clean it up by hand or helper scripts - we don't want to
 do this either, its one of the major things we're trying to get away from.

The more you explain, the sillier that feeling looks -- that those
are your options. Focus on the problems you actually have. Track
what build steps worked as expected; log what useful information
you have about the ones that did not.

That resource standpoint thing doesn't really make sense. Those
os.system() calls launch *at least* one more process. Some
implementations will launch a process to run a shell, and the
shell will launch another process to run the named command. Even
so, efficiency on the controller machine is not a problem given
the scale you have described.


-- 
--Bryan

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


Re: function to convert degree (hour), minute, seconds string to integer

2006-07-27 Thread Marc 'BlackJack' Rintsch
In [EMAIL PROTECTED], John Machin
wrote:

 You may wish to put more restrictions on the separators ... I would be
 suspicious of cases where dms[2] != dms[5]. What plausible separators
 are there besides :? Why allow alphabetics? If there's a use case for
 23h59m59s, that would have to be handled separately.

Looking at the subject I would expect to be able to give 76°04'54 as
argument.  Hm, but degrees don't map directly to hours!?

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

Re: MySql

2006-07-27 Thread miker2

Marc 'BlackJack' Rintsch wrote:
 In [EMAIL PROTECTED], miker2 wrote:

  import MySQLdb
  base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
  db=test_py)
  cursor = base.cursor()
  cursor.execute(INSERT INTO table (field) VALUES (int))
 
  this does not work but the interesting thing is, there is an
  AUTO_INCREMENT
  field. Now say i had a couple of entries in there already:
 auto  table
  1|90
  2|32
 
  and then i run my py script 3 times, the data is not entered but if i
  add
  another entry from mysql the auto increment field will have counted the
 
  python entries:
 autotable
   1|90
   2|32
   6|47
 
  please tell me what i am doing wrong. thanks.

 Where's the problem?  Do you mind that the third entry has a 6 as unique
 `auto` value?  Doesn't `AUTO_INCREMENT` just guarantee unique values?

 Ciao,
   Marc 'BlackJack' Rintsch

the problem is that the entry from python: cursor.execute(INSERT INTO
table (field) VALUES (3)) is not there.

the auto increment counts the entries 1,2,3,4,5, ect.   the 3,4,5 in
the example above is where i've run the py script and as you can see
there are not there.  the 6 is an entry from mysql.

so basically the data from python is not being entered but the auto
increment is being counted. thanks.

sorry about the dodgie description.

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


Re: Threads vs Processes

2006-07-27 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:
 John Henry wrote:
  Granted.  Threaded program forces you to think and design your
  application much more carefully (to avoid race conditions, dead-locks,
  ...) but there is nothing inherently *non-robust* about threaded
  applications.

 Indeed.  Let's just get rid of all preemptive multitasking while we're
 at it

Also, race conditions and deadlocks are equally bad in multiprocess
solutions as in multithreaded ones.  Any time you're doing parallel
processing you need to consider them.

I'd actually submit that initially writing multiprocess programs
requires more design and forethought, since you need to determine
exactly what you want to share instead of just saying what the heck,
everything's shared!  The payoff in terms of getting _correct_
behavior more easily, having much easier maintenance down the line, and
being more robust in the face of program failures (or unforseen
environment issues) is usually well worth it, though there are
certainly some applications where threads are a better choice.

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


Re: How to force a thread to stop

2006-07-27 Thread Nick Craig-Wood
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  Hans wrote:
  Is there a way that the program that created and started a thread also stops
  it.
  (My usage is a time-out).
 
  E.g.
 
  thread = threading.Thread(target=Loop.testLoop)
  thread.start() # This thread is expected to finish within a second
  thread.join(2)# Or time.sleep(2) ?
 
  No, Python has no threadicide method

Actually it does in the C API, but it isn't exported to python.
ctypes can fix that though.

 and its absence is not an oversight. Threads often have important
 business left to do, such as releasing locks on shared data; killing
 them at arbitrary times tends to leave the system in an inconsistent
 state.

Here is a demo of how to kill threads in python in a cross platform
way.  It requires ctypes.  Not sure I'd use the code in production but
it does work...


How to kill a thread demo


import threading
import time
import ctypes

class ThreadKilledError(Exception): pass
_PyThreadState_SetAsyncExc = ctypes.pythonapi.PyThreadState_SetAsyncExc
_c_ThreadKilledError = ctypes.py_object(ThreadKilledError)

def _do_stuff(t):
Busyish wait for t seconds.  Just sleeping delays the exeptions in the 
example
start = time.time()
while time.time() - start  t:
time.sleep(0.01)

class KillableThread(threading.Thread):

Show how to kill a thread

def __init__(self, name=thread, *args, **kwargs):
threading.Thread.__init__(self, *args, **kwargs)
self.name = name
print Starting %s % self.name
def kill(self):
Kill this thread
print Killing %s % self.name
_PyThreadState_SetAsyncExc(self.id, _c_ThreadKilledError)
def run(self):
self.id = threading._get_ident()
while 1:
print Thread %s running % self.name
_do_stuff(1.0)

if __name__ == __main__:
thread1 = KillableThread(name=thread1)
thread1.start()
_do_stuff(0.5)
thread2 = KillableThread(name=thread2)
thread2.start()
_do_stuff(2.0)
thread1.kill()
thread1.join()
_do_stuff(2.0)
thread2.kill()
thread2.join()
print Done

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


Re: MySql

2006-07-27 Thread John Machin
[EMAIL PROTECTED] wrote:
 HI,

 I'm having trouble writing to a MySql db using python and the MySQLdb
 module. Here is the code:

 import MySQLdb
 base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
 db=test_py)
 cursor = base.cursor()
 cursor.execute(INSERT INTO table (field) VALUES (int))

I've never used MySQL but they're all much the same --
table is a reserved word. What is int supposed to be? That's not
valid SQL AFAIK. Is that exactly what you typed? Or are you coyly
obfuscating?

Try this:
cursor.execute(INSERT INTO tablename (fieldname) VALUES (42))
or better,
somevar = 42
cursor.execute(INSERT INTO tablename (fieldname) VALUES (?),
(somevar,))
even better, read the docs and look at the examples :-)



 this does not work

... and the error message was ... what? If it's not a state secret, how
about divulging it?


 but the interesting thing is, there is an
 AUTO_INCREMENT
 field. Now say i had a couple of entries in there already:
auto  table
 1|90
 2|32

 and then i run my py script 3 times, the data is not entered but if i
 add
 another entry from mysql the auto increment field will have counted the

 python entries:
autotable
  1|90
  2|32
  6|47

Evidently it's committed the auto increment before it decides that it
doesn't like your SQL or whatever. Read the warranty card that came
with the autoincrementer gizmoid; you won't find continuous or no
gaps mentioned anywhere.

 please tell me what i am doing wrong.

Inter alia, not giving enough clear unambiguous info about what your
problem really is.

Cheers,
John

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray

Sybren Stuvel wrote:
 Ray enlightened us with:
  Also having to check whether a name has already existed can be a
  major pain in the butt with Python. With Java you always know when a
  name has already existed, and what type is bound to the name. I
  consider this to be a Good Thing (tm).

 I don't buy that. With long functions and variable declarations all
 over the place, you can easily miss a few. If you then also forget to
 declare a variable (people make mistakes) you can easily re-use a name
 without knowing.

Huh? No. The compiler will always tell you. Have you ever tried Java
before?


 Sybren
 --
 The problem with the world is stupidity. Not saying there should be a
 capital punishment for stupidity, but why don't we just take the
 safety labels off of everything and let the problem solve itself?
  Frank Zappa

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Bruno Desthuilliers
Ray wrote:
otRay, please, don't top-post/ot

(snip)
 Also having to check whether a name has already existed can be a major
 pain in the butt with Python.

assert 'somename' not in dir(someObject)

(snip)

 Regarding the lack of privacy, 

s/privacy/language-inforced access restriction/

 --I guess I've been doing Java too long :)

indeed !-)

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySql

2006-07-27 Thread miker2

John Machin wrote:
 [EMAIL PROTECTED] wrote:
  HI,
 
  I'm having trouble writing to a MySql db using python and the MySQLdb
  module. Here is the code:
 
  import MySQLdb
  base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
  db=test_py)
  cursor = base.cursor()
  cursor.execute(INSERT INTO table (field) VALUES (int))

 I've never used MySQL but they're all much the same --
 table is a reserved word. What is int supposed to be? That's not
 valid SQL AFAIK. Is that exactly what you typed? Or are you coyly
 obfuscating?

 Try this:
 cursor.execute(INSERT INTO tablename (fieldname) VALUES (42))
 or better,
 somevar = 42
 cursor.execute(INSERT INTO tablename (fieldname) VALUES (?),
 (somevar,))
 even better, read the docs and look at the examples :-)


 
  this does not work

 ... and the error message was ... what? If it's not a state secret, how
 about divulging it?


  but the interesting thing is, there is an
  AUTO_INCREMENT
  field. Now say i had a couple of entries in there already:
 auto  table
  1|90
  2|32
 
  and then i run my py script 3 times, the data is not entered but if i
  add
  another entry from mysql the auto increment field will have counted the
 
  python entries:
 autotable
   1|90
   2|32
   6|47

 Evidently it's committed the auto increment before it decides that it
 doesn't like your SQL or whatever. Read the warranty card that came
 with the autoincrementer gizmoid; you won't find continuous or no
 gaps mentioned anywhere.

  please tell me what i am doing wrong.

 Inter alia, not giving enough clear unambiguous info about what your
 problem really is.

 Cheers,
 John


sorry guys...

forget about the auto incrementer for a second.

the entry is not being recorded. that is my problem. the script does
not work. thanks.

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread John Machin

Ray wrote:

 The argument against this is that since development with Python is so
 rapid, you're supposed to always equip your code with extensive unit
 tests. I like Python but I've never really bought that argument--I
 guess I've been doing Java too long :)


In Java, if you don't always equip your code with extensive unit tests,
what sort of testing do you do?

When do you pick up typo errors that the compiler may not detect, like
blahblah*12.365 instead of blahblah*12/365 ?

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


locked file

2006-07-27 Thread Kirt

i have a code that backsup file from src to dest.
 Now if some of the files are locked , i need to skip those files..
I was trying to use fctl module but it can be used only in unix i
suppose.

is there anyother way? i am using windows os.

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Bruno Desthuilliers
Steve Jobless wrote:
 Sybren Stuvel wrote:
 
Steve Jobless enlightened us with:

The first case can be just a typo, like:

  x.valeu = 5

I make typos all the time. Without a spell checker, this message
would be unreadable :).

Then learn to read what you type, as you type it. Typing without
errors can be trained.
 
 
 I'd rather let a machine to do that. Wasn't computer created for tasks
 like this? (No, not really. But...)

There's obviously a trade-off between 'security' and flexibility. As I
said, I do make lots of typo too, but OTOH the edit/test cycle in Python
is usually so short that such errors are not a problem for me - they're
caught almost immediatly.

 
The second case can be like:

  x.next = y
  y.next = None

to create a linked list by piggybacking next to the class. It will
overwrite the iterater for the class if defined.

You shouldn't simply pick a name and assign something to it without
checking the current use of that name. It's pretty much true for
everything in life.
 
 
 Well, the choice of next was not a good example. Sure, no one with
 decent Python knowledge would do that.
 But what about adding a method to the class? Am I supposed to ask Is
 anyone using name xxx?

assert 'xxx' not in dir(SomeClassOrObject)

 The class may be used by  developers I don't
 even know and some of them may be on the other side of the planet...

How could this be a problem ? What's added/changed at runtime in a given
app doesn't impact the source code.

 
If I was working on a large project with many engineers, I'd assume
someone will do things like this sooner or later. I've seen many
horrendous code in my life and I have no control over who I work
with.

Long live a versioning system. With that, you can find the person
writing the horrible code, and slap them on the back of the head.
People, like all animals, can be trained into doing the right thing.
 
 
 I'd like to. But often those people are long gone for one reason or
 another. Sometimes, it happens to be my boss...

If your boss is a bad programmer and doesn't know it, then you're in for
trouble whatever the language.

 
 Maybe I should ask the question in a different way:
 
 What are the benefits from this? There must be something really good
 (and hopefully safe) you can do with it only from the outside. I don't
 have much problem over-riding functions from the inside of the class.
 But from the outside, I fail to see why anyone needs to add attributes
 or over-ride functions.

Just one example : suppose you're working with a framework. Suppose
you'd need to customize some objects (including classes - Python classes
are objects too) of the framework, but the framework has no hook for
this and you definitively don't wan't to bother patching the code and
maintaining your own branch.


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySql

2006-07-27 Thread John Machin
[EMAIL PROTECTED] wrote:
 John Machin wrote:
  [EMAIL PROTECTED] wrote:
   HI,
  
   I'm having trouble writing to a MySql db using python and the MySQLdb
   module. Here is the code:
  
   import MySQLdb
   base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
   db=test_py)
   cursor = base.cursor()
   cursor.execute(INSERT INTO table (field) VALUES (int))
 
  I've never used MySQL but they're all much the same --
  table is a reserved word. What is int supposed to be? That's not
  valid SQL AFAIK. Is that exactly what you typed? Or are you coyly
  obfuscating?
 
  Try this:
  cursor.execute(INSERT INTO tablename (fieldname) VALUES (42))
  or better,
  somevar = 42
  cursor.execute(INSERT INTO tablename (fieldname) VALUES (?),
  (somevar,))
  even better, read the docs and look at the examples :-)
 
 
  
   this does not work
 
  ... and the error message was ... what? If it's not a state secret, how
  about divulging it?
 
 
   but the interesting thing is, there is an
   AUTO_INCREMENT
   field. Now say i had a couple of entries in there already:
  auto  table
   1|90
   2|32
  
   and then i run my py script 3 times, the data is not entered but if i
   add
   another entry from mysql the auto increment field will have counted the
  
   python entries:
  autotable
1|90
2|32
6|47
 
  Evidently it's committed the auto increment before it decides that it
  doesn't like your SQL or whatever. Read the warranty card that came
  with the autoincrementer gizmoid; you won't find continuous or no
  gaps mentioned anywhere.
 
   please tell me what i am doing wrong.
 
  Inter alia, not giving enough clear unambiguous info about what your
  problem really is.
 
  Cheers,
  John


 sorry guys...

 forget about the auto incrementer for a second.

 the entry is not being recorded. that is my problem. the script does
 not work. thanks.

OK we've forgotten about the auto incrementer.

Now tell us what does not work means.
Show us an actual suitably-cut down script that does not work.
If you get an error message, tell us what the error message was.
If you didn't get an error message, bloody well tell us that you
didn't.

BTW, if the script doesn't contain

base.commit()

somewhere, take yourself out to the back lane and give yourself a good
thumping :-)
Then come back in and read the docs about transactions and commit and
autocommit etc.

HTH,
John

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


Re: MySql

2006-07-27 Thread Atanas Banov

[EMAIL PROTECTED] wrote:

 sorry guys...

 forget about the auto incrementer for a second.

 the entry is not being recorded. that is my problem. the script does
 not work. thanks.

after Dijkstra: the use of mySql cripples the mind; its teaching
should, therefore, be regarded as a criminal offence. 

thus said, which mysql engine are you using for your DB? is it
transactional, should you commit?

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


Re: How to find difference in years between two dates?

2006-07-27 Thread thebjorn
John Machin wrote:
 thebjorn wrote:
  John Machin wrote:
   thebjorn wrote:
[...]
   Holy code bloat, Batman! Try this:
  
   return now.year - born.year - (birthday  now)
 
  yuck :-)

 But this:
 return now.year - born.year - (birthday  now and 1 or 0) is not yuck???

Correct.

  [...]
   It's the irregular-size months that cause the problems. If you can work
   out the months difference, then just floor_div by 12 to get the years
   difference.
 
  I don't agree that the irregular sized months cause a problem in this
  case. They do cause a problem if you're asking when is today + one
  month?, i.e. there isn't an unambiguous answer to that question in
  general (e.g. if today was January 31). We're asking a different kind
  of question though: has it been at least one month since January 31?,
  the answer would be no on Feb 29 and yes on Mar 1.

 If a bank were paying you interest on a monthly basis, and you
 deposited money on Jan 31 and pulled it out on the last day of
 February, that would count as one month. This is what I call the today
 - yesterday == 1 rule. For computing things like duration of employee
 service, you need the today - yesterday == 2 rule -- on the
 assumption that service counts from start of business yesterday to
 close of business today. So hire date of 1 Feb to fire date of (last
 day of Feb) would count as one month.

You give a good argument that the concept of a month is fuzzy, I still
don't believe that it makes the concept of a year fuzzy (or that the
fuzziness of month needs to be accounted for when computing years).

 Sorry, I don't understand. Why are you speechless? What would I want to
 use the calendar module for? Apart from the leap() function and the
 table of days in a month, the calendar module doesn't have any of the
 functionality that one would expect in a general-purpose date class.

Well, I thought replacing a 4 line function with 31 lines, 13 of which
duplicated functionality in the standard library was overkill... I came
up with this yesterday which seems sufficient?

def yeardiff(a, b):
  y = a.year - b.year
  if (a.month, a.day)  (b.month, b.day): # tuple comparison
  y -= 1
  return y 

-- bjorn

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


Re: a print bug?

2006-07-27 Thread Duncan Booth
[EMAIL PROTECTED] wrote:

 
 Sybren Stuvel wrote:
 It has nothing to do with the print command, and everything with
 floating point precision. See http://docs.python.org/tut/node16.html
 
 
 how about the discrepancy between
 
 print 1.2345
 
 1.2345
 
 print %10.3f % 1.2345# seems like a bug
 
  1.234
 
 the first one, print knows enough to recognize and print it as 1.2345.
 however, in the second line, when it is round off, it doesn't know it
 is 1.2345 any more.
 

But you wouldn't complain about this would you?

 print %10.4f % 1.23445
1.2345
 print %10.3f % 1.23445
 1.234

A value which is slightly than 1.2345 prints to 4 decimal places as 1.2345 
and to 3 decimal places as 1.234.

That's all that happens with your value as well: 1.2345 is not exactly 
representable as a floating point number, and the nearest representable 
number is less than 1.2345.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: splitting words with brackets

2006-07-27 Thread Paul McGuire

Tim Chase [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
   r = re.compile(r'(?:\([^\)]*\)|\[[^\]]*\]|\S)+')
   r.findall(s)
 ['(a c)b(c d)', 'e']
 
  Ah, it's exactly what I want!  I thought the left and right
  sides of | are equal, but it is not true.

 In theory, they *should* be equal. I was baffled by the nonparity
 of the situation.  You *should be able to swap the two sides of
 the | and have it treated the same.  Yet, when I tried it with
 the above regexp, putting the \S first, it seemed to choke and
 give different results.  I'd love to know why.

Does the re do left-to-right matching?  If so, then the \S will eat the
opening parens/brackets, and never get into the other alternative patterns.
\S is the most matchable pattern, so if it comes ahead of the other
alternatives, then it will always be the one matched.  My guess is that if
you put \S first, you will only get the contiguous character groups,
regardless of ()'s and []'s.  The expression might as well just be \S+.

Or I could be completely wrong...

-- Paul


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


Re: Tkinter pack Problem

2006-07-27 Thread H J van Rooyen

Simon Forman [EMAIL PROTECTED] wrote:

| I find the Tkinter reference: a GUI for Python under Local links on
| this page http://infohost.nmt.edu/tcc/help/lang/python/tkinter.html to
| be very helpful.  It has a decent discussion of the grid layout
| manager.
| 
| HTH,
| ~Simon

Thanks am checking it out - downloading pdf... - Hendrik

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


Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread Bruno Desthuilliers
Jaroslaw Zabiello wrote:
 On Wed, 26 Jul 2006 18:20:44 +0200, Bruno Desthuilliers wrote:
 
 
May I suggest that you learn some Lisp dialect ?
 
 
 Nope. I hate Lisp syntax. 

This should not prevent you from learning it - at least, you'd then
avoid making dumb statements...

 
Of course, I you like, you can freeze every object you want and nobody can
be able to open and change it. You can also trace all activity for changing
something to objects because Ruby has nice system hooks implemented.

__getattribute__ / __setattr__ / __delattr__
 
 
 This is not the same. Ruby can also lock access to attributes.

Please re-read the doc for these methods.

 Freezing is
 different. It can freeze any object so you will not be able to add or
 delete any method. Once freezed object cannot be unfrozen. So you can be
 sure that nobody will change you classes if you do not like.
  
 
And of course import hooks.
 
 
 Python?? Where?

RTFM:
http://www.python.org/doc/2.3.5/lib/built-in-funcs.html

What about learning Python instead of repeating arguments from clueless
people ?

 
Ruby
has nice security system (private, protected, public scopes for methods and
attributes,

This is not security, this is data-hiding. 
 
 
 No. Data hiding are in Python.

Oh, yes ? Where, exactly ?

 Ruby uses security similiar to Java. If the
 class has method marked as private it cannot be used in children classes. 

Python methods are attributes, so from a Python POV, this is still
data-hiding. But granted, I should have used language-inforced access
restriction instead. So let me rephrase:

This is not security, this is language-inforced access restriction.

And IIRC, Ruby's attributes are always private
 
 
 Yes and no. Yes, because you cannot read them directly without accessors.
 And no, because you can read them if you set proper accessor.

Adding accessors doesn't make the attributes public.

 
In past Python had Bastion module 
(http://www.python.org/doc/lib/module-Bastion.html) 
but it was rejected and now Python has nothing.

Seems like Zope is not doing so bad...
  
 Zope is only a great application. 

Zope is a framework and an application server.

 Invision Power Board or ezPublish are
 also great application, but it does not mean that PHP is great language. :)

You can't say that Python is dumb when it comes to security and reject
an example of a Python framework that address the problem.

 Python has no security at all. I has only convention and mangling.

Please define security. I fail to see how language-inforced access
restriction (and mandatory declarative static typing etc) relates to
'security'. As far as I'm concerned, security is about protecting a
system from piracy, not about inflicting useless pain to programmers.

 Of
 course somebode can say, it is enough, and maybe it is. But I think, that
 this might be another reason why Java guys prefer Ruby to Python.

Agreed - but this all boil down to Ruby having a more canonical object
model than Python.

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: self question

2006-07-27 Thread Duncan Booth
Mike wrote:

 I think the answer is that 'def' is an executable statement in python
 rather than a definition that the compiler interprets at compile time.
 
 As a result the compiler can evaluate 'foo()' when it defines 'bar', so
 it does.
 
 The following works as expected:
 def bar():
   print foo()
 
 Hopefully somebody more knowledgable will also respond
 

The def statement is, as you say, an executable statement. It creates a new 
function object, so it is quite useful to look directly at the function 
type to see what arguments its constructor takes:

 import types
 help(types.FunctionType)
Help on class function in module __builtin__:

class function(object)
 |  function(code, globals[, name[, argdefs[, closure]]])
 |  
 |  Create a function object from a code object and a dictionary.
 |  The optional name string overrides the name from the code object.
 |  The optional argdefs tuple specifies the default argument values.
 |  The optional closure tuple supplies the bindings for free variables.
...

The arguments passed to the constructor when the def statement is executed 
are:

a code object. This object is created once when the module containing the 
function is compiled. The def doesn't have to do anything with the actual 
code, so it will take the same time whether you def a function of 1 line or 
1000 lines.

globals is the same value as you get by calling the builtin globals().

name is the function name. It's a string constant.

argdefs is the interesting one here. It is a tuple of the default argument 
values and is created every time the def statement is executed.

closure is another tuple which is created every time the def statement is 
executed. This allows a def nested inside another function to reference the 
current local variables which are in scope when the def executes. This 
tuple can only contain cell objects which are an internal object type used 
for scoped variables.

So, argdefs and closure are the two things which are different each time 
you def a function, everything else is constant and shared between 
definitions of the same function.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread Bruno Desthuilliers
Jaroslaw Zabiello wrote:
 On Wed, 26 Jul 2006 18:23:22 +0200, Bruno Desthuilliers wrote:
 
 
Care to write an external DSL in Ruby ?

?

I mean : write a parser and interpreter for a DSL. In Ruby.
 
 
 I see. Nope. I do not like code generators. 

I'm not talking about code generators, I'm talking about implementing
a language with another language.

 In this sense every template
 system is DSL.

Indeed.

 E.g. Smarty Templates for PHP. You can create external DSL
 even in C or assembler.

Indeed.

 This is not the point. The point is Ruby is much
 better suited to create internal DSL than Python

Care to back your claim ? Or are you just parroting what you read from
some clueless guy more concerned with hype and buzz than with reality ?

 or PHP.

Please compare what is comparable.

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Worarrounding hardcoded Option class in optparse in Python 2.3

2006-07-27 Thread Pupeno
For the record, the copy and paste fix seems to have worked, so far.

Pupeno wrote:

 Hello,
 I am doing some extreme use of optparse, that is, extending it as
 explained on
 http://docs.python.org/lib/optparse-other-reasons-to-extend-optparse.html
 I have subclassed OptionParser and Option. MyOptionParser uses MyOption as
 option_class and in Python 2.4 it works. But I have to target Python 2.3.
 In Python 2.3 the help and version options seem to be created before even
 a parser is created and they are created using a hardcoded call to Option.
 So, they are not using MyOption. I am creating MyOption specifically for
 the help and version Option so I need the to be MyOption.
 I check out the documentation of this module for Python 2.3 and it
 recommends the same procedure:
 http://www.python.org/doc/2.3/lib/optparse-extending-other-reasons.html
 Is this a bug in Python 2.3 that was solved in 2.4 and nobody cared to
 backport ?
 At any rate, what are my options (no pun intended) ?
 I could copy and paste the fix[1] from Python 2.4 into MyOptionParser; I
 checked it carefull and it seems it would work, but I am not sure, does
 anybody know ?
 Should I override the hardcoded module variables of optparse in 2.3 ?
 (that seems like a bad, bad idea).
 I am open to suggestions.
 Thanks.

-- 
Pupeno [EMAIL PROTECTED] (http://pupeno.com)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray

Sybren Stuvel wrote:
 Ray enlightened us with:
  Huh? No. The compiler will always tell you. Have you ever tried Java
  before?

 I know what I'm talking about, I've got a degree in Computer Science
 from the University of Amsterdam.

Then how come you didn't know that the Java compiler will always tell
you? :)


 Sybren
 --
 The problem with the world is stupidity. Not saying there should be a
 capital punishment for stupidity, but why don't we just take the
 safety labels off of everything and let the problem solve itself?
  Frank Zappa

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray

John Machin wrote:
 Ray wrote:

  The argument against this is that since development with Python is so
  rapid, you're supposed to always equip your code with extensive unit
  tests. I like Python but I've never really bought that argument--I
  guess I've been doing Java too long :)
 

 In Java, if you don't always equip your code with extensive unit tests,
 what sort of testing do you do?

In Java of course we do unit testing as well. It's just that things
like what the OP was asking about has already been caught by the
compiler at this time.

 When do you pick up typo errors that the compiler may not detect, like
 blahblah*12.365 instead of blahblah*12/365 ?

Yeah, I know what you mean. This is not what the OP was referring to,
though.

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray

Bruno Desthuilliers wrote:
 Ray wrote:
 otRay, please, don't top-post/ot

Um, top-post? I'm using Google News and it looks like it is placed
correctly in the thread... or you're referring to a different thing?

snip

Ray

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


Re: How to find difference in years between two dates?

2006-07-27 Thread John Machin

thebjorn wrote:
 John Machin wrote:
  thebjorn wrote:
   John Machin wrote:
thebjorn wrote:
 [...]
Holy code bloat, Batman! Try this:
   
return now.year - born.year - (birthday  now)
  
   yuck :-)
 
  But this:
  return now.year - born.year - (birthday  now and 1 or 0) is not yuck???

 Correct.

   [...]
It's the irregular-size months that cause the problems. If you can work
out the months difference, then just floor_div by 12 to get the years
difference.
  
   I don't agree that the irregular sized months cause a problem in this
   case. They do cause a problem if you're asking when is today + one
   month?, i.e. there isn't an unambiguous answer to that question in
   general (e.g. if today was January 31). We're asking a different kind
   of question though: has it been at least one month since January 31?,
   the answer would be no on Feb 29 and yes on Mar 1.
 
  If a bank were paying you interest on a monthly basis, and you
  deposited money on Jan 31 and pulled it out on the last day of
  February, that would count as one month. This is what I call the today
  - yesterday == 1 rule. For computing things like duration of employee
  service, you need the today - yesterday == 2 rule -- on the
  assumption that service counts from start of business yesterday to
  close of business today. So hire date of 1 Feb to fire date of (last
  day of Feb) would count as one month.

 You give a good argument that the concept of a month is fuzzy

Sorry,  I can't imagine where you got fuzzy from. Perhaps you mean
some other word. The concept is capable of being expressed precisely.

 I still
 don't believe that it makes the concept of a year fuzzy (or that the
 fuzziness of month needs to be accounted for when computing years).

The point is to ensure that (say) 24 months of employment and 2 years
of employment are determined in a consistent fashion.


  Sorry, I don't understand. Why are you speechless? What would I want to
  use the calendar module for? Apart from the leap() function and the
  table of days in a month, the calendar module doesn't have any of the
  functionality that one would expect in a general-purpose date class.

 Well, I thought replacing a 4 line function with 31 lines, 13 of which
 duplicated functionality in the standard library was overkill.

I think you missed the point that the lines I quoted were straight out
of a self-contained library that existed (in C as well as Python) way
before the datetime module was a gleam in Fred  the timbot's eyes.
Even if I had noticed a leap year function in the calendar module, I
would probably not have used it. The Python version of the module was
just a stopgap while I fiddled with getting a C extension going. The C
leap year function doesn't have any of that modulo stuff in it.

 I came
 up with this yesterday which seems sufficient?

 def yeardiff(a, b):
   y = a.year - b.year
   if (a.month, a.day)  (b.month, b.day): # tuple comparison
   y -= 1
   return y

At least it doesn't blow up when b is leapyear-02-29. It just gives the
wrong answer when a is nonleapyear-02-28. E.g. it gives 0 years
difference  from 1992-02-29 to 1993-02-28 instead of 1.

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


Re:[OT] Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Bruno Desthuilliers
Ray wrote:
 Bruno Desthuilliers wrote:
 
Ray wrote:
otRay, please, don't top-post/ot
 
 
 Um, top-post? I'm using Google News and it looks like it is placed
 correctly in the thread... or you're referring to a different thing?

http://en.wikipedia.org/wiki/Top-posting

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Re:[OT] Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ray

Bruno Desthuilliers wrote:
 Ray wrote:
  Bruno Desthuilliers wrote:
 
 Ray wrote:
 otRay, please, don't top-post/ot
 
 
  Um, top-post? I'm using Google News and it looks like it is placed
  correctly in the thread... or you're referring to a different thing?

 http://en.wikipedia.org/wiki/Top-posting

Gotcha ;-)


 --
 bruno desthuilliers
 python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
 p in '[EMAIL PROTECTED]'.split('@')])

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


Re: httplib, threading, wx app freezing after 4 hours

2006-07-27 Thread Dermot Doran
According to the wxPython in Action book using the wx.CallAfter function in a non-gui thread is a safe wayfor threads to call functions that will then update the gui in the gui thread.

Cheers!!

Dermot.
On 23/07/06, Mark rainess [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] wrote: Mark rainess wrote:
 [...] It runs perfectly for about 4 hours, then freezes. I'm stuck. How do I debug this? [...] Can anyone suggest techniques to help me learn what is going on.
 By inspection: errcode is undefined; I expect you stripped the example a bit too far. If it is set to something other 200, it looks like you loop out. You are calling 
wx.CallAfter() from a different thread than runs the GUI. Is that documented to be safe? I've read that wxPostEvent() is is the call to use for this. Next thing to try is adding enough logging to tell exactly what
 statement hangs.Thanks guys, I found the problem.I had screen-saver set to None and power set to blank monitor after 30minutes. The problem occurred after the monitor blanked. I remembered I
re-flashed my bios a few weeks ago. I didn't check the biospower-management settings. I'm not going to reboot now to check becauseI have too much stuff open.I set power to never blank monitor. Now there is no problem.
I added code to monitor for activity and to kill and restart the threadif activity stops. Now if power-management kills it, it wakes-up whenthe screen returns.I think using wx.CallAfter() the way I have is correct. I will check
that. It does work properly though.Mark--http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread Kay Schluehr
Jaroslaw Zabiello wrote:

 Of course, I you like, you can freeze every object you want and nobody can
 be able to open and change it. You can also trace all activity for changing
 something to objects because Ruby has nice system hooks implemented. Ruby
 has nice security system (private, protected, public scopes for methods and
 attributes, objects freezing, system hooks).

Sounds good. I guess unlike Java you can't inspect and modify all those
data using reflection in Ruby?

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


Re: How to display name of elements in list? PROBLEM SOLVED.

2006-07-27 Thread cz

[EMAIL PROTECTED] wrote:
 It looks like the PyTensor object *should* have .xx, .xy, etc
 properties, but they may be accessible through a matrix, i.e. .t(i,j)


Thanks to all of you for your help!
The solution is easy: The tensor components have labels t11, t12,...
Good guess ruibalp!

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


Re: Threads vs Processes

2006-07-27 Thread Nick Craig-Wood
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  Yes, someone can, and that someone might as well be you.
  How long does it take to create and clean up 100 trivial
  processes on your system? How about 100 threads? What
  portion of your user waiting time is that?

Here is test prog...

The results are on my 2.6GHz P4 linux system

  Forking
  1000 loops, best of 3: 546 usec per loop
  Threading
  1 loops, best of 3: 199 usec per loop

Indicating that starting up and tearing down new threads is 2.5 times
quicker than starting new processes under python.

This is probably irrelevant in the real world though!



Time threads vs fork


import os
import timeit
import threading

def do_child_stuff():
Trivial function for children to run
# print hello from child
pass

def fork_test():
Test forking
pid = os.fork()
if pid == 0:
# child
do_child_stuff()
os._exit(0)
# parent - wait for child to finish
os.waitpid(pid, os.P_WAIT)

def thread_test():
Test threading
t = threading.Thread(target=do_child_stuff)
t.start()
# wait for child to finish
t.join()

def main():
print Forking
timeit.main([-s, from __main__ import fork_test, fork_test()])
print Threading
timeit.main([-s, from __main__ import thread_test, thread_test()])

if __name__ == __main__:
main()

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


Re: subprocess module

2006-07-27 Thread Nick Craig-Wood
placid [EMAIL PROTECTED] wrote:
  import subprocess
  p = subprocess.Popen([ffmpeg.exe -i video.mpg, -f mjpeg  -ss 5 
  -vframes 1 -s 160x120 -an video.gif], shell=True, stdout=subprocess.PIPE)
 
  but the ffmpeg complains about the input file being corrupter, whereas
  when i run the same command via the command shell (cmd.exe) it works.
  Does anyone know what the problem is?

Here is an idea to try: I would say you've mixed up the two styles of
passing arguments, either pass

  args = ffmpeg.exe -i video.mpg -f mjpeg  -ss 5 -vframes 1 -s 160x120 -an 
video.gif
  p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)

or

  args = ['ffmpeg.exe', '-i', 'video.mpg', '-f', 'mjpeg', '', '-ss', '5', 
'-vframes', '1', '-s', '160x120', '-an', 'video.gif']
  p = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE)

If you mix the two styles then you are probably heading for trouble
with argument quoting.

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


Re: PySNMP Thread unsafe?

2006-07-27 Thread Nick Craig-Wood
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  I'm trying to monitor about 250 devices with SNMP, using PySNMP version
  4. I use the threading.Thread to create a threadpool of 10 threads, so
  devices not responding won't slow down the monitoring process too
  much.

This is surely a job for twisted not threads?  You'd be able to poll
all 250 devices at once with twisted...

This might be helpful (haven't tried it myself though)

  http://twistedsnmp.sourceforge.net/

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


Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread Kay Schluehr

Bruno Desthuilliers wrote:

 Care to write an external DSL in Ruby ?

 I mean : write a parser and interpreter for a DSL. In Ruby.

It is this kind of stuff Rubys talk about when they mention DSLs in
Ruby:

http://www.artima.com/rubycs/articles/ruby_as_dsl.html

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


Re: Nested function scope problem

2006-07-27 Thread Antoon Pardon
On 2006-07-25, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
 Gerhard Fiedler wrote:
 On 2006-07-25 04:06:24, Steve Holden wrote:
 
 Since Python has no local variable declaration, there must be a rule
 to distinguish local names from names living in the enclosing
 namespaces. The rule is: unless previously declared 'global' (ie
 module-level) with the appropriate statement, any name bound in the
 local namespace is local. If declared 'global', it has to exist in the
 global namespace.

 This was much more simple to understand when we didn't have nested
 functions - we mostly had global and local scope.

But having only a global an local scope, didn't prevent nested
functions. The nested functions just didn't have nested scopes
and that had it's own problems.

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


coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread Jon Bowlas
Hi listers,

I wrote this script in Zope some time ago and it worked for a while, but now 
I'm getting the following error:
TypeError: coercing to Unicode: need string or buffer, NoneType found

Here's my script:

results = context.module_retriever().tuples() # call to ZSQLMethod
converted = []
for result in results:
   result = list(result) # make a list from the tuple
   for i in range(len(result)):
# for each element in the listified tuple, make decode to a
# unicode from the latin-1 string
result[i] = unicode(result[i], 'latin-1')
   converted.append(tuple(result)) # tuplify again
return converted

Where module_retriever is a simple Z SQL method that returns a module title 
and a module code.
So results = context.module_retriever().tuples() returns:
[('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy', 
'ANAT2009'),('etc...', 'etc..'),..]So I was hoping someone might be able to 
identify what I've done wrong here. It is a little mistifying as to why it's 
suddenly stopped working though.CheersJon



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


Automatic email checking - best procedures/suggestions

2006-07-27 Thread Jon Clements
Hi All,

I'm hoping someone has some experience in this field and could give me
a pointer in the right direction - it's not purely python related
though. Any modules/links someone has tried and found useful would be
greatly appreciated...

I want to have an automated process which basically has its own email
account on the LAN. The basic idea is that upon receipt of an email, it
logs this in a database and then forwards the message on to 'suitable'
recipients. (Well it will do more, but this is perfect to be going on
with and buildable on...)

The database and email account are set up and working fine. Using
smtplib, imaplib or poplib I can send and receive mail - this is not a
problem. What I'm unsure of is the best way to design this. Bear in
mind that network/email server configuration changes can be made. For
instance, do I connect to the email server and keep polling it every
'n' whatever for new messages, or should I be looking to the smtpd
module and get mail via that? (or any other way?)

I think I'm basically after the best way to implement:
Email in -- Python process -- Email out

Cheers,

Jon.

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


Re: coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread blue99

Jon Bowlas wrote:

 Here's my script:

 results = context.module_retriever().tuples() # call to ZSQLMethod
 converted = []
 for result in results:
result = list(result) # make a list from the tuple
for i in range(len(result)):
 # for each element in the listified tuple, make decode to a
 # unicode from the latin-1 string

try this:
   if result[i] is None: continue

 result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
 return converted

regards,
Rob

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


Re: coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread Jon Bowlas
Ahh, that did it, thanks very much.

Jon
- Original Message - 
From: [EMAIL PROTECTED]
Newsgroups: comp.lang.python
To: python-list@python.org
Sent: Thursday, July 27, 2006 11:19 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found


 
 Jon Bowlas wrote:
 
 Here's my script:

 results = context.module_retriever().tuples() # call to ZSQLMethod
 converted = []
 for result in results:
result = list(result) # make a list from the tuple
for i in range(len(result)):
 # for each element in the listified tuple, make decode to a
 # unicode from the latin-1 string
 
 try this:
   if result[i] is None: continue
 
 result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
 return converted
 
 regards,
 Rob
 
 -- 
 http://mail.python.org/mailman/listinfo/python-list

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


Re: coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread Peter Otten
Jon Bowlas wrote:

 I wrote this script in Zope some time ago and it worked for a while, but
 now I'm getting the following error:
 TypeError: coercing to Unicode: need string or buffer, NoneType found
 
 Here's my script:
 
 results = context.module_retriever().tuples() # call to ZSQLMethod
 converted = []
 for result in results:
result = list(result) # make a list from the tuple
for i in range(len(result)):
 # for each element in the listified tuple, make decode to a
 # unicode from the latin-1 string
 result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
 return converted
 
 Where module_retriever is a simple Z SQL method that returns a module
 title and a module code.
 So results = context.module_retriever().tuples() returns:
 [('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy',
 'ANAT2009'),('etc...', 'etc..'),..]So I was hoping someone might be able
 to identify what I've done wrong here. It is a little mistifying as to why
 it's suddenly stopped working though.CheersJon

This may be an indication that in your database you have a record with a
None value, e. g.

[('Developmental Neurobiology', 'ANAT2008'),
 ('Neuroanatomy', 'ANAT2009'),
 ('Man in black', None)])

and most likely the right fix is to correct the database entry and make sure
that no new such records can be entered into it.

If that is not possible, a quick fix would be to replace your unicode() call
with something that special-cases None:

def from_latin(s):
if s is None:
return None # or maybe 'return u', if your app expects a unicode
# string
return unicode(s, latin-1)

By the way, in recent Python your snippet might become

converted = []
for record in results:
# prior to 2.4: tuple([...])
converted.append(tuple(from_latin(field) for field in record))
return converted

Peter

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


Re: coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread Jon Bowlas
Ahh yes there are a couple of dodgy records that seem to have been added.

many thanks.

Jon
- Original Message - 
From: Peter Otten [EMAIL PROTECTED]
Newsgroups: comp.lang.python
To: python-list@python.org
Sent: Thursday, July 27, 2006 11:22 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found


 Jon Bowlas wrote:

 I wrote this script in Zope some time ago and it worked for a while, but
 now I'm getting the following error:
 TypeError: coercing to Unicode: need string or buffer, NoneType found

 Here's my script:

 results = context.module_retriever().tuples() # call to ZSQLMethod
 converted = []
 for result in results:
result = list(result) # make a list from the tuple
for i in range(len(result)):
 # for each element in the listified tuple, make decode to a
 # unicode from the latin-1 string
 result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
 return converted

 Where module_retriever is a simple Z SQL method that returns a module
 title and a module code.
 So results = context.module_retriever().tuples() returns:
 [('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy',
 'ANAT2009'),('etc...', 'etc..'),..]So I was hoping someone might be able
 to identify what I've done wrong here. It is a little mistifying as to 
 why
 it's suddenly stopped working though.CheersJon

 This may be an indication that in your database you have a record with a
 None value, e. g.

[('Developmental Neurobiology', 'ANAT2008'),
 ('Neuroanatomy', 'ANAT2009'),
 ('Man in black', None)])

 and most likely the right fix is to correct the database entry and make 
 sure
 that no new such records can be entered into it.

 If that is not possible, a quick fix would be to replace your unicode() 
 call
 with something that special-cases None:

 def from_latin(s):
if s is None:
return None # or maybe 'return u', if your app expects a unicode
# string
return unicode(s, latin-1)

 By the way, in recent Python your snippet might become

converted = []
for record in results:
# prior to 2.4: tuple([...])
converted.append(tuple(from_latin(field) for field in record))
return converted

 Peter

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

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


Re: coercing to Unicode: need string or buffer, NoneType found

2006-07-27 Thread Jon Bowlas
It says line 8 in the traceback so I guess its 

result[i] = unicode(result[i], 'latin-1')

Jon
- Original Message - 
From: Sybren Stuvel [EMAIL PROTECTED]
Newsgroups: comp.lang.python
To: python-list@python.org
Sent: Thursday, July 27, 2006 11:06 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found


 Jon Bowlas enlightened us with:
 I wrote this script in Zope some time ago and it worked for a while,
 but now I'm getting the following error:
 TypeError: coercing to Unicode: need string or buffer, NoneType
 found
 
 What line is causing the error?
 
 Sybren
 -- 
 The problem with the world is stupidity. Not saying there should be a
 capital punishment for stupidity, but why don't we just take the
 safety labels off of everything and let the problem solve itself? 
 Frank Zappa
 -- 
 http://mail.python.org/mailman/listinfo/python-list

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


Re: MySql

2006-07-27 Thread Sibylle Koczian
John Machin schrieb:
 
 BTW, if the script doesn't contain
 
 base.commit()
 
 somewhere, take yourself out to the back lane and give yourself a good
 thumping :-)

That's not really fair, because transactions were added to MySQL only a
short time ago (at least to the default table type). There simply hasn't
yet been time for every experienced MySQL user to get hit by the need to
commit things.

-- 
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg
e-mail : [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function to convert degree (hour), minute, seconds string to integer

2006-07-27 Thread google0
John Machin wrote:
 [EMAIL PROTECTED] wrote:
  I know this is a trivial function, and I've now spent more time
  searching for a surely-already-reinvented wheel than it would take to
  reinvent it again, but just in case... is there a published,
  open-source, function out there that takes a string in the form of
  hh:mm:ss (where hh is 00-23, mm is 00-59, and ss is 00-59) and
  converts it to an integer (ss + 60 * (mm + 60 * hh))?  I'd like
  something that throws an exception if hh, mm, or ss is out of range, or
  perhaps does something reasonable (like convert 01:99 to 159).
  Thanks,
  --dang

 Have you considered time.strptime()?

 BTW, your function, given 00:00:00 will return 0 -- you may well have
 trouble distinguishing that from False (note that False == 0), without
 resorting to ugliness like:

 if result is False ...

 Instead of returning False for some errors and letting int() raise an
 exception for others, I would suggest raising ValueError yourself for
 *all* invalid input.

 You may wish to put more restrictions on the separators ... I would be
 suspicious of cases where dms[2] != dms[5]. What plausible separators
 are there besides :? Why allow alphabetics? If there's a use case for
 23h59m59s, that would have to be handled separately. Note that
 06-12-31 could be a date, 12,34,56 could be CSV data.

 Cheers,
 John

Good point about 0/False.  I don't think it would have bitten me in my
current program, given my expected (and filtered) inputs, but I might
have reused it in the future, and been bitten later.

I had looked at the time module, but apparently not long enough.
This does the trick:

def dms2int(dms):
int(time.mktime(time.strptime(2000-01-01 %s % dms, %Y-%m-%d
%H:%M:%S)))

I only need the minutes, but can work with seconds.  The only downside
is that I'm hardcoding an arbitrary date, but I can deal with that.

Thanks for your help, John!
--dang

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


Re: MySql

2006-07-27 Thread ftc
[EMAIL PROTECTED] a écrit :
 import MySQLdb
 base = MySQLdb.connect(host=localhost, user=blah, passwd=blah,
 db=test_py)
 cursor = base.cursor()
 cursor.execute(INSERT INTO table (field) VALUES (int))
 
 this does not work but the interesting thing is, there is an
 AUTO_INCREMENT
 field. Now say i had a couple of entries in there already:
auto  table
 1|90
 2|32
 
 and then i run my py script 3 times, the data is not entered but if i
 add
 another entry from mysql the auto increment field will have counted the
 
 python entries:
autotable
  1|90
  2|32
  6|47
 
 please tell me what i am doing wrong. thanks.


The dbapi2 specification says:
if the database supports an auto-commit feature, this must be initially 
off

So you have to do a commit ( base.commit() ) or to enable auto-commit at 
the beginning ( base.autocommit( True ) )
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested function scope problem

2006-07-27 Thread Jeremy Sanders
Gerhard Fiedler wrote:

 Going back to the original question... What would be the most
 common/useful way to access variables from the outer function for writing
 from within the inner function?

I've done something like this (which doesn't look very nice)

def myfunc():

   tok = ['']

   def inner():
   tok[0] += 'hi'
   ...
   tok[0] = 'foo'

   inner()
   print tok[0]

-- 
Jeremy Sanders
http://www.jeremysanders.net/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySql

2006-07-27 Thread John Machin

Sibylle Koczian wrote:
 John Machin schrieb:
 
  BTW, if the script doesn't contain
 
  base.commit()
 
  somewhere, take yourself out to the back lane and give yourself a good
  thumping :-)

 That's not really fair, because transactions were added to MySQL only a
 short time ago (at least to the default table type). There simply hasn't
 yet been time for every experienced MySQL user to get hit by the need to
 commit things.


As I said earlier, I don't use MySQL. I wasn't aware it didn't have
transactions -- what did people use it for, then?  So is the upshot is
that he should thump himself for using a DBMS w/o transactions,
perhaps?

Cheers,
John

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


Thread Question

2006-07-27 Thread Ritesh Raj Sarraf
Hi,

I have some basic doubts about thread.

I have a list which has items in it which need to be downloaded from
the internet.
Let's say list is:

list_items[] which has 100 items in it.

I have a function download_from_web() which does the work of
downloading the items from the web. It does error handling in case it
gets errors like 404 et cetera.

Currently, this is how I'm using it.

for item in list_items:
download_from_web(item)

This way, one items is downloaded at a time.

I'm planning to implement threads in my application so that multiple
items can be downloaded concurrently. I want the thread option to be
user-defined.

Looking at the documentation of threads (Core Python Programming), I've
noticed that all threads are executed a once. Depending upon what they
are doing, some finish early and some later.

But I want to implement something like:

for item in list_items:
for num in thread_args:
   thread[num].start()
   thread[num].start()

Is this the correct way of threading applications ?
This is the first time I'd be doing threading. So was looking for
comments and suggestions.

Thanks,
Ritesh

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


Re: function to convert degree (hour), minute, seconds string to integer

2006-07-27 Thread John Machin

[EMAIL PROTECTED] wrote:
 John Machin wrote:
  [EMAIL PROTECTED] wrote:
   I know this is a trivial function, and I've now spent more time
   searching for a surely-already-reinvented wheel than it would take to
   reinvent it again, but just in case... is there a published,
   open-source, function out there that takes a string in the form of
   hh:mm:ss (where hh is 00-23, mm is 00-59, and ss is 00-59) and
   converts it to an integer (ss + 60 * (mm + 60 * hh))?  I'd like
   something that throws an exception if hh, mm, or ss is out of range, or
   perhaps does something reasonable (like convert 01:99 to 159).
   Thanks,
   --dang
 
  Have you considered time.strptime()?
 
  BTW, your function, given 00:00:00 will return 0 -- you may well have
  trouble distinguishing that from False (note that False == 0), without
  resorting to ugliness like:
 
  if result is False ...
 
  Instead of returning False for some errors and letting int() raise an
  exception for others, I would suggest raising ValueError yourself for
  *all* invalid input.
 
  You may wish to put more restrictions on the separators ... I would be
  suspicious of cases where dms[2] != dms[5]. What plausible separators
  are there besides :? Why allow alphabetics? If there's a use case for
  23h59m59s, that would have to be handled separately. Note that
  06-12-31 could be a date, 12,34,56 could be CSV data.
 
  Cheers,
  John

 Good point about 0/False.  I don't think it would have bitten me in my
 current program, given my expected (and filtered) inputs, but I might
 have reused it in the future, and been bitten later.

The bigger pain would have been two types of error handling
(try/except) *AND* if result is False

 I had looked at the time module, but apparently not long enough.
 This does the trick:

 def dms2int(dms):
 int(time.mktime(time.strptime(2000-01-01 %s % dms, %Y-%m-%d
 %H:%M:%S)))

 I only need the minutes, but can work with seconds.  The only downside
 is that I'm hardcoding an arbitrary date, but I can deal with that.


That's a bit too elaborate. Python gives you the hard-coded date for
free -- then (you ingrate!) you ignore it, like this:

| import time
| dms = 23:48:59
| t = time.strptime(dms, %H:%M:%S)
| t
(1900, 1, 1, 23, 48, 59, 0, 1, -1)
| seconds = (t[3] * 60 + t[4]) * 60.0 + t[5]
| seconds
85739.0 # assuming you do want it as a float, not an int

Cheers,
John

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


Python with DyBase

2006-07-27 Thread Asem Eltaher
Dear users,
I use Python version 2.4.3 and DyBase Object Oriented Database.  These 
are the first three lines in my program:

import os
import sys
import dybase

I usually get the error message:  ImportError: DLL load failed. Kindly 
be informed that I have in the folder: C:\Python24\Lib the following DLL 
files:
python23.dll
python24.dll
dybasedll.dll

Does soembody know what is wrong?
Thanks
Asem
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySql

2006-07-27 Thread Paul Boddie
John Machin wrote:
 Sibylle Koczian wrote:
  John Machin schrieb:
  
   base.commit()

[...]

  That's not really fair, because transactions were added to MySQL only a
  short time ago (at least to the default table type). There simply hasn't
  yet been time for every experienced MySQL user to get hit by the need to
  commit things.

This is mentioned in the MySQLdb FAQ:

http://sourceforge.net/docman/display_doc.php?docid=32070group_id=22307

The default behaviour has been changed to match the DB-API standard,
which probably matches the normal behaviour on most mainstream
relational database systems.

 As I said earlier, I don't use MySQL. I wasn't aware it didn't have
 transactions -- what did people use it for, then?  So is the upshot is
 that he should thump himself for using a DBMS w/o transactions,
 perhaps?

Some awareness of common practice would certainly be beneficial.
Attempting to insert data into PostgreSQL, Oracle, sqlite and so on
would produce similar results to those described. The principal
difference is that with MySQL (and presumably with things like
Microsoft Access' storage engine that no-one takes seriously anyway),
everyone has been able to get away with ignoring transactions and
considering such behaviour as normal (or not even considering that
anyone really uses anything which does anything else), and this
obviously affects software governed by such assumptions.

I suppose it's unfortunate for anyone who was using MySQLdb prior to
release 1.2.0, especially if the software didn't give any obvious
run-time warnings (not that I can say whether it did or not), but the
MySQL-centric culture of ignoring/ridiculing stuff they don't support
(and then eventually supporting it, in this case) is probably most to
blame if we have to point the finger.

Paul

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


Re: Automatic email checking - best procedures/suggestions

2006-07-27 Thread Jorge Godoy
Jon Clements [EMAIL PROTECTED] writes:

 problem. What I'm unsure of is the best way to design this. Bear in
 mind that network/email server configuration changes can be made. For
 instance, do I connect to the email server and keep polling it every
 'n' whatever for new messages, or should I be looking to the smtpd
 module and get mail via that? (or any other way?)

 I think I'm basically after the best way to implement:
 Email in -- Python process -- Email out

It depends on your mail server, on what you're willing to do and also on how
locked to your mail server you want to be.

Certainly fetching messages from your mailserver through POP3 and reinjecting
them to the correct mailaccounts will be a more portable solution than
writing, e.g., a filter to your mailserver.

On the other hand, if you need the message processed as soon as it arrives you
can make your program a filter -- or have a filter to call it somehow -- to
process the message.

Also consider the startup time of your code for each processed message,
concurrency issues, etc. 

-- 
Jorge Godoy  [EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a print bug?

2006-07-27 Thread Steve Holden
[EMAIL PROTECTED] wrote:
 it seems that the behavior of print is that it will round off
 properly for any floating point imperfection, such as shown in the
 first two samples.  The third sample seems to be a bug?  It doesn't
 know how to handle the floating imperfection in this case.
 
 
1.2345
 
 1.2344
 
 
print 1.2345
 
 1.2345
 
 
print %10.3f % 1.2345# seems like a bug 
--
 
  1.234
 
 
print %10.3f % 1.23450001
 
  1.235
 
 
print %10.3f % 1.2344
 
  1.234
 
 
print %10.3f % 1.2346
 
  1.235
 
   print %25.22f % 1.2345
  1.234400

You obviously haven't yet passed your floating-point number proficiency 
test yet. Please restrict yourself to integers until you understand the 
difficulties that inaccuracies in floating-point can create ;-)

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: Thread Question

2006-07-27 Thread Duncan Booth
Ritesh Raj Sarraf wrote:

 I'm planning to implement threads in my application so that multiple
 items can be downloaded concurrently. I want the thread option to be
 user-defined.
 
 Looking at the documentation of threads (Core Python Programming), I've
 noticed that all threads are executed a once. Depending upon what they
 are doing, some finish early and some later.
 
 But I want to implement something like:
 
 for item in list_items:
 for num in thread_args:
thread[num].start()
thread[num].start()
 
 Is this the correct way of threading applications ?
 This is the first time I'd be doing threading. So was looking for
 comments and suggestions.
 

What you want is to use a pool of threads so that you can configure how 
many requests are issued at a time (you don't want to try to issue 100 
requests all in parallel). You can communicate with the threads through a 
Queue.

So if the code for a thread looks like:

   def run(request, response):
   while 1:
   item = request.get()
   if item is None:
break
   response.put(download_from_web(item))

# your main loop can be something like:

requestQueue = Queue()
responseQueue = Queue()
thread_pool = [
Thread(target=run, args=(requestQueue, responseQueue)
for i in range(numthreads)]
for t in thread_pool: t.start()

for item in list_items:
 requestQueue.put(item)

for i in range(len(list_items)):
response = responseQueue.get()
handle_response(response)

# and then to shut down the threads when you've finished:
for t in thread_pool:
requestQueue.put(None)
for t in thread_pool:
 t.join()

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


Re: Threads vs Processes

2006-07-27 Thread Steve Holden
Carl J. Van Arsdall wrote:
 Paul Rubin wrote:
 
Carl J. Van Arsdall [EMAIL PROTECTED] writes:
  

Processes seem fairly expensive from my research so far.  Each fork
copies the entire contents of memory into the new process.


No, you get two processes whose address spaces get the data.  It's
done with the virtual memory hardware.  The data isn't copied.  The
page tables of both processes are just set up to point to the same
physical pages.  Copying only happens if a process writes to one of
the pages.  The OS detects this using a hardware trap from the VM
system.
  
 
 Ah, alright.  So if that's the case, why would you use python threads 
 versus spawning processes?  If they both point to the same address space 
 and python threads can't run concurrently due to the GIL what are they 
 good for?
 

Well, of course they can interleave essentially independent 
computations, which is why threads (formerly lightweight processes) 
were traditionally defined.

Further, some thread-safe extension (compiled) libraries will release 
the GIL during their work, allowing other threads to execute 
simultaneously - and even in parallel on multi-processor hardware.

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd  http://www.holdenweb.com
Skype: holdenweb   http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

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


Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread Bruno Desthuilliers
Kay Schluehr wrote:
 Bruno Desthuilliers wrote:
 
 
Care to write an external DSL in Ruby ?

I mean : write a parser and interpreter for a DSL. In Ruby.
 
 
 It is this kind of stuff Rubys talk about when they mention DSLs in
 Ruby:
 
 http://www.artima.com/rubycs/articles/ruby_as_dsl.html
 
yes, I know - embedded DSLs.

FWIW, after a *quick* glance at this article, I haven't seen anything
that seemed undoable nor even difficult to do in Python - it all boils
down to reading an ini-like file and creating a stub source-tree from it.

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Need a compelling argument to use Django instead of Rails

2006-07-27 Thread gregarican

Bruno Desthuilliers wrote:
 Please define security. I fail to see how language-inforced access
 restriction (and mandatory declarative static typing etc) relates to
 'security'. As far as I'm concerned, security is about protecting a
 system from piracy, not about inflicting useless pain to programmers.

I must agree here. When I am coding I appreciate ease of referencing
things above and beyond a language tying my hands behind my back
supposedly in the name of security. If I am savvy enough and know what
I am doing I can create classes, methods, etc. that implement an
effective security model in terms of encapsulation and hiding. But
there are times that I am creating something that I don't want boxed in
by the language enforcing all of this for me. It's like when people
dismiss PHP as a supposedly insecure language. It's more a problem of
too many newly initiated PHP developers not using techniques they
should be to create secure applications.

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


pysqlite2 fetching from select different than pysqlite?

2006-07-27 Thread schwehr
Hi All,

I have some old pysqlite 1.x code that uses a pattern like this:

cu.execute('SELECT weight FROM weights WHERE samplename=foo)
row = cu.fetchone()
weight=row['weight']

It seems like lookups by name are no longer supported in pysqlite2.  Is
that true?  And if not, and I want to do a SELECT * FROM table and go
through name by name, how can I do this?

Hopefully this is not a FAQ somewhere that I've overlooked!

Thanks!
-kurt

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


Functions and code objects

2006-07-27 Thread Fuzzyman
Hello all,

I'm trying to extract the code object from a function, and exec it
without explicitly passing parameters.

The code object 'knows' it expects to receive paramaters. It's
'arg_count' attribute is readonly.

How can I set the arg_count to 0, or pass parameters to the code object
when I exec it ?

 def f(x):
... print x
...
 c = f.func_code
 type(c)
type 'code'
 exec c
Traceback (most recent call last):
  File input, line 1, in ?
TypeError: f() takes exactly 1 argument (0 given)
 c.co_argcount
1
 c.co_argcount = 0
Traceback (most recent call last):
  File input, line 1, in ?
TypeError: readonly attribute
 

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

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


RE: pysqlite2 fetching from select different than pysqlite?

2006-07-27 Thread Tim Golden
[EMAIL PROTECTED]

| I have some old pysqlite 1.x code that uses a pattern like this:
| 
| cu.execute('SELECT weight FROM weights WHERE samplename=foo)
| row = cu.fetchone()
| weight=row['weight']
| 
| It seems like lookups by name are no longer supported in 
| pysqlite2. 

According to this:

http://initd.org/pub/software/pysqlite/doc/usage-guide.html#accessing-co
lumns-by-name-instead-of-by-index

you have to specify a .row_factory before running the select

TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

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


Re: Python with DyBase

2006-07-27 Thread John Machin

Asem Eltaher wrote:
 Dear users,
 I use Python version 2.4.3 and DyBase Object Oriented Database.  These
 are the first three lines in my program:

 import os
 import sys
 import dybase

 I usually get the error message:  ImportError: DLL load failed. Kindly
 be informed that I have in the folder: C:\Python24\Lib the following DLL
 files:
 python23.dll
 python24.dll
 dybasedll.dll

Kindly be informed that some person or persons unknown has/have
committed a nonsense. You should have no DLLs in that directory.
1. Delete those DLLs.
2. Install the dybase package according to the instructions (if any)
3. If those DLLs reappear in that directory, delete them, stop
attempting to use the package, and ask for a refund :-)

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


Re: Nested function scope problem

2006-07-27 Thread Bruno Desthuilliers
danielx wrote:
 Bruno Desthuilliers wrote:
 
(snip)

Surprising for me are actually two things: 1- the fact itself, and 2- that
term binding, and that whatever it means  (I'll have to read more on that,
now that I know the term)

a binding is the association of a name and a reference to an object in
a given namespace. It's different from the common notion of variable,
which is usually a symbolic name for a memory address storing a value
(like a pointer to an object's address).
 
 
 Wait, I'm not sure I see the difference. Isn't reference ~ C
 pointer. 

For a very large definition of '~' !-)

 Are you saying Python variables don't hold references to
 actual Python objects? 

Exactly.

 That idea has been working well for me so far.

It can only take you so far. Now it's time you know the truth: there are
*no* 'variables' in Python (hence the term 'binding').

What you really have is (somewhat simplified, of course) a dict with
names as keys and objects references (think of 'smart' pointers) as
values. So the name doesn't 'hold' anything - it's really nothing more
than a name. And the object doesn't know nothing about which names it's
bound to.


-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to force a thread to stop

2006-07-27 Thread Jean-Paul Calderone
On Thu, 27 Jul 2006 07:07:05 GMT, Dennis Lee Bieber [EMAIL PROTECTED] wrote:
On Wed, 26 Jul 2006 17:38:06 -0700, Carl J. Van Arsdall
[EMAIL PROTECTED] declaimed the following in comp.lang.python:

 Well, I guess I'm thinking of an event driven mechanism, kinda like
 setting up signal handlers.  I don't necessarily know how it works under
 the hood, but I don't poll for a signal.  I setup a handler, when the
 signal comes, if it comes, the handler gets thrown into action.  That's
 what I'd be interesting in doing with threads.

   Well, first off, something in the run-time IS doing the equivalent
of polling. When IT detects the condition of a registered event has
taken place, it calls the registered handler as a normal subroutine.
Now, that polling may seem transparent if it is tied to, say the OS I/O
operations. This means that, if the process never performs any I/O, the
polling of the events never happens. Basically, as part of the I/O
operation, the OS checks for whatever data signals an event (some bit
set in the process table, etc.) has happened and actively calls the
registered handler. But that handler runs as a subroutine and returns.

   How would this handler terminate a thread? It doesn't run /as/ the
thread, it runs in the context of whatever invoked the I/O, and a return
is made to that context after the handler finishes.

   You are back to the original problem -- such an event handler, when
called by the OS, could do nothing more than set some data item that is
part of the thread, and return... The thread is STILL responsible for
periodically testing the data item and exiting if it is set.

   If your thread is waiting for one of those many os.system() calls to
return, you need to find some way to kill the child process (which may
be something running on the remote end, since you emphasize using SSH).


While you are correct that signals are not the solution to this problem,
the details of this post are mostly incorrect.

If a thread never performs any I/O operations, signal handlers will still
get invokes on the arrival of a signal.

Signal handlers have to run in some context, and that ends up being the
context of some thread.  A signal handler is perfectly capable of exiting
the thread in which it is running.  It is also perfectly capable of
terminating any subprocess that thread may be responsible for.

However, since sending signals to specific threads is difficult at best
and impossible at worst, combined with the fact that in Python you
/still/ cannot handle a signal until the interpreter is ready to let you
do so (a fact that seems to have been ignored in this thread repeatedly),
signals end up not being a solution to this problem.

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


Re: Functions and code objects

2006-07-27 Thread Fuzzyman

Fuzzyman wrote:
 Hello all,

 I'm trying to extract the code object from a function, and exec it
 without explicitly passing parameters.

 The code object 'knows' it expects to receive paramaters. It's
 'arg_count' attribute is readonly.

 How can I set the arg_count to 0, or pass parameters to the code object
 when I exec it ?


Ok, so now I'm getting somewhere, without really knowing what I'm
doing. Using the CodeType I can create a new code object with identical
attributes, except an 'argcount' of 0.

It doesn't quite work, so I probably need to set some of the attributes
*differently*.

The code I use is :

 def f(x):
 ... print x
 ...
 c = f.func_code
 CodeType = type(c)
 a = CodeType(0, c.co_nlocals, c.co_stacksize, c.co_flags, c.co_code,
 ... c.co_consts, c.co_names, c.co_varnames, c.co_filename,
c.co_name,
 ... c.co_firstlineno, c.co_lnotab, c.co_freevars, c.co_cellvars)
 a
code object f at 01C707A0, file input, line 1
 exec a
Traceback (most recent call last):
  File input, line 1, in ?
  File input, line 2, in f
UnboundLocalError: local variable 'x' referenced before assignment

(the first argument, 0, becomes the 'arg_count' attribute)

So the code object is still creating a local scope, and knows that x is
a local variable. ('co_nlocals' and 'co_varnames' ?).

I'd like to construct the code object so that it takes the parameters
from the enclosing scope (the context I pass into exec I guess),
without clobbering any local variables that may be defined in the code
object.

Anyone got any clues ?


The attributes mean (from http://pyref.infogami.com/type-code ) :

* co_name gives the function name
* co_argcount is the number of positional arguments (including
arguments with default values)
* co_nlocals is the number of local variables used by the function
(including arguments)
* co_varnames is a tuple containing the names of the local
variables (starting with the argument names)
* co_cellvars is a tuple containing the names of local variables
that are referenced by nested functions
* co_freevars is a tuple containing the names of free variables
* co_code is a string representing the sequence of bytecode
instructions
* co_consts is a tuple containing the literals used by the bytecode
* co_names is a tuple containing the names used by the bytecode
* co_filename is the filename from which the code was compiled
* co_firstlineno is the first line number of the function
* co_lnotab is a string encoding the mapping from byte code offsets
to line numbers (for details see the source code of the interpreter)
* co_stacksize is the required stack size (including local
variables)
* co_flags is an integer encoding a number of flags for the
interpreter.

In case anyone wonders, this is purely an experiment in creating
'annoymous code blocks'. :-)

What is a 'free variable' ?

'cell_vars' also looks interesting, but probably better not to mess
with it :-) (I guess it is only relevant for functions defined in the
code block)

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

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


Re: How to force a thread to stop

2006-07-27 Thread Jean-Paul Calderone
On Thu, 27 Jul 2006 02:30:03 -0500, Nick Craig-Wood [EMAIL PROTECTED] wrote:
[EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
  Hans wrote:
  Is there a way that the program that created and started a thread also 
  stops
  it.
  (My usage is a time-out).
 
  E.g.
 
  thread = threading.Thread(target=Loop.testLoop)
  thread.start() # This thread is expected to finish within a second
  thread.join(2)# Or time.sleep(2) ?

  No, Python has no threadicide method

Actually it does in the C API, but it isn't exported to python.
ctypes can fix that though.

No, it has a method for raising an exception.  This isn't quite the
same as a method for killing a thread.  Also, this has been mentioned
in this thread before.  Unfortunately:

  import os, threading, time, ctypes

  class ThreadKilledError(Exception):
  pass

  _PyThreadState_SetAsyncExc = ctypes.pythonapi.PyThreadState_SetAsyncExc
  _c_ThreadKilledError = ctypes.py_object(ThreadKilledError)

  def timeSleep():
  time.sleep(100)

  def childSleep():
  os.system(sleep 100) # time.sleep(100)

  def catchException():
  while 1:
  try:
  while 1:
  time.sleep(0.0)
  except Exception, e:
  print 'Not exiting because of', e


  class KillableThread(threading.Thread):
  
  Show how to kill a thread -- almost
  
  def __init__(self, name=thread, *args, **kwargs):
  threading.Thread.__init__(self, *args, **kwargs)
  self.name = name
  print Starting %s % self.name

  def kill(self):
  Kill this thread
  print Killing %s % self.name
  _PyThreadState_SetAsyncExc(self.id, _c_ThreadKilledError)

  def run(self):
  self.id = threading._get_ident()
  try:
  return threading.Thread.run(self)
  except Exception, e:
  print 'Exiting', e

  def main():
  threads = []
  for f in timeSleep, childSleep, catchException:
  t = KillableThread(target=f)
  t.start()
  threads.append(t)
  time.sleep(1)
  for t in threads:
  print 'Killing', t
  t.kill()
  for t in threads:
  print 'Joining', t
  t.join()

  if __name__ == '__main__':
  main()


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


Re: Changing a value for each folder while traversing a file system

2006-07-27 Thread PipedreamerGrey
No, that doesn't work.  Though, leaving the random snippet about the
for file in DirectoryWalker(.): line, it does leave all files the
same value, rather than switching the value for every single file.  The
problem is, the value is shared accross every folder.

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


Re: How to force a thread to stop

2006-07-27 Thread Paul Boddie
Paul Rubin wrote:

 Instead of using os.system, maybe you want to use one of the popens or
 the subprocess module.  For each ssh, you'd spawn off a process that
 does the ssh and communicates back to the control process through a
 set of file descriptors (Unix pipe endpoints or whatever).  The
 control process could use either threads or polling/select to talk to
 the pipes and keep track of what the subprocesses were doing.

For some insight into what you might need to do to monitor asynchronous
communications, take a look at the parallel/pprocess module, which I
wrote as a convenience for spawning processes using a thread
module-style API whilst providing explicit channels for interprocess
communication:

http://www.python.org/pypi/parallel

Better examples can presumably be found in any asynchronous
communications framework, I'm sure.

 I don't think you need anything as complex as shared memory for this.
 You're just writing a special purpose chat server.

Indeed. The questioner might want to look at the py.execnet software
that has been presented now at two consecutive EuroPython conferences
(at the very least):

http://indico.cern.ch/contributionDisplay.py?contribId=91sessionId=41confId=44

Whether this solves the questioner's problems remains to be seen, but
issues of handling SSH-based communications streams do seem to be
addressed.

Paul

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


Re: xmlrpc: hostname nor servname provided ?

2006-07-27 Thread Laszlo Nagy
 I'm running a service on a machine. The service is written in Python 
 (of course) and it connects to an XMLRPC server periodically.
 It recreates the ServerProxy instance each time it needs to connect to 
 the RPC server.

 The server is created with this code:

 server = xmlrpclib.ServerProxy(local.SERVER_URL,allow_none=True)   # 
 local.SERVER_URL is something like 'https://myserver.com:3421'


 After running for a few hours, I always get this:

 2006-07-25 16:33:26,431 ERROR .main Traceback (most recent call last):
   File /home/gandalf/AmazonOfferDownloader/AmazonOfferDownloader.py, 
 line 27, in run
 logger.info(Connected to %s,server.serversignature())
   File /usr/local/lib/python2.4/xmlrpclib.py, line 1096, in __call__
 return self.__send(self.__name, args)
   File /usr/local/lib/python2.4/xmlrpclib.py, line 1383, in __request
 verbose=self.__verbose
   File /usr/local/lib/python2.4/xmlrpclib.py, line 1129, in request
 self.send_content(h, request_body)
   File /usr/local/lib/python2.4/xmlrpclib.py, line 1243, in 
 send_content
 connection.endheaders()
   File /usr/local/lib/python2.4/httplib.py, line 798, in endheaders
 self._send_output()
   File /usr/local/lib/python2.4/httplib.py, line 679, in _send_output
 self.send(msg)
   File /usr/local/lib/python2.4/httplib.py, line 646, in send
 self.connect()
   File /usr/local/lib/python2.4/httplib.py, line 1072, in connect
 sock.connect((self.host, self.port))
   File string, line 1, in connect
 gaierror: (8, 'hostname nor servname provided, or not known')

 This exception does not stop my server program and it keeps trying to 
 connect to the xmlrpc server, but it raises the same exception again 
 and again.
 However, if I restart the program then everything works. The XML RPC 
 server has a static IP address and it is never restarted/reconfigured.
 I cannot find the problem. Please help me.
   
Do you have ANY ideas about what I should check? We have 5 clients for 
the RPC server and they need to be run continously. This is a big 
problem because after some hours they stop working.

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


Re: Changing a value for each folder while traversing a file system

2006-07-27 Thread Peter Otten
PipedreamerGrey wrote:

 I'm using the script below (originally from http://effbot.org, given to
 me here) to open all of the text files in a directory and its
 subdirectories and combine them into one Rich text
 file (index.rtf).  Now I'm adapting the script to convert all the text
 files into individual html files.  What I can't figure out is how to
 trigger a change in the background value for each folder change (not
 each file), so that text is color-coded by folder.

 for file in DirectoryWalker(.):
 # divide files names into path and extention
 path, ext = os.path.splitext(file)
 # choose the extention you would like to see in the list
 if ext == .txt:
 print file
 file = open(file)
 fileContent = file.readlines()
# just for example, let's say I want to print the color here as
 if in an html tag...
 index.write(color)
 for line in fileContent:
 if not line.startswith(\n):
 index.write(line)
 index.write(\n)

You have to remember which directory you are in:

#untested
last_directory = None
for file in DirectoryWalker(.):
directory = os.dirname(file)
if directory != last_directory:
color = random.choice([red, green, blue])
last_directory = directory

# do whatever you want with file and color
print color, file 

Peter

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


Re: write()

2006-07-27 Thread Rick Zantow
manuhack [EMAIL PROTECTED] wrote in news:1153981114.837884.232610
@p79g2000cwp.googlegroups.com:

 I copied the lines
 
 f=open('/tmp/workfile', 'w')
 print f
 f.close()
 
 from Python 2.4 Documentation 7.2.  But it said IOerror No such file or
 directory '/tmp/workfile'
 
 Is it something about the os?  I'm using Python 2.4 under WinXP.
 Thanks.  Without / I can open it.
 

The problem is probably that you don't have a '/tmp' directory. WinXP 
doesn't create a directory structure to match a file path specification. If 
you first create the '/tmp' directory (which is a directory off the root of 
your hard drive on WinXP, so that if your current directory is on a D: 
drive it would be D:\tmp), then your code would work.

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


Re: using names before they're defined

2006-07-27 Thread Bruno Desthuilliers
[EMAIL PROTECTED] wrote:
do you mean 'configparser'?

Yes.


Does it generate objects from the config file automatically?

It generates a representation of the config file as a Python object
composed of sections and options. The documentation should get you started.
 
 
 Hiya, you might be interested in this alternative config parsing
 program:
 http://www.voidspace.org.uk/python/configobj.html

Yes, I know it. But I don't like it. Either a simple ini file do the
trick, or I need a full blown app-specific DSL - which can be as simple
as a Python file with dicts, lists, etc !-)

-- 
bruno desthuilliers
python -c print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in '[EMAIL PROTECTED]'.split('@')])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using iterators to write in the structure being iterated through?

2006-07-27 Thread Pierre Thibault
On Wed, 26 Jul 2006 16:11:48 -0700, Paddy wrote:

 
 Paddy wrote:
 Pierre Thibault wrote:
  Hello!
 
  I am currently trying to port a C++ code to python, and I think I am stuck
  because of the very different behavior of STL iterators vs python
  iterators. What I need to do is a simple arithmetic operations on objects
  I don't know. In C++, the method doing that was a template, and all that
  was required is that the template class has an iterator conforming to the
  STL forward iterator definition. Then, the class would look like:
 
 SNIP
  Then I discovered python and wanted to use all its goodies. I thought it
  would be easy to do the same thing but I can't: the iterator mechanism is
  read-only, right? So it does no make sense to write:
 
  io1 = iter(object1)
  io2 = iter(object2)
 
  try:
while 1:
  io1.next() += io2.next()
  except StopIteration:
pass
 
  That won't work:
  SyntaxError: can't assign to function call
 
  Here is my question: how could I do that and retain enough generallity?
 
  Thanks!
 
  Pierre

 Pierre,
 You should be able to write
   io1.next().param += io2.next().param
 If iter(object1) and iter(object2) both return classes or instances
 with the appropriate parameter.
 Here is what I was thinking of:


 class ParamHolder(object):
 def __init__(self, n):
 self.param = n

 class C1(object):
 def __init__(self,m):
 self.value = [ParamHolder(n) for n in range(m)]
 def __getitem__(self, p):
 return self.value[p]

 obj1 = C1(5)
 obj2 = C1(5)

 io1 = iter(obj1)
 io2 = iter(obj2)

 print obj1 pre loop,[r.param for r in obj1.value]

 try:
 while 1:
 io1.next().param += io2.next().param
 except StopIteration:
 pass

 print obj1 post loop,[r.param for r in obj1.value]

 - Paddy.
 
 I don't like the try/except code and would write something like the
 following:
 
 obj1 = C1(5)
 obj2 = C1(5)
 from itertools import izip
 for x,y in izip(obj1, obj2):
 ...   x.param += y.param
 ...
 print obj1 post for loop,[r.param for r in obj1.value]
 obj1 post for loop [0, 2, 4, 6, 8]
 
 
 - Paddy.

Thanks Paddy,

This looks like the closest thing to what I wanted, though the need for
this param makes it not very general to my taste. Besides, this method
would not work with ndarrays anyways.

Thanks again for taking the time to answer,

Pierre

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


Re: MySql

2006-07-27 Thread miker2

Paul Boddie wrote:
 John Machin wrote:
  Sibylle Koczian wrote:
   John Machin schrieb:
   
base.commit()

 [...]

   That's not really fair, because transactions were added to MySQL only a
   short time ago (at least to the default table type). There simply hasn't
   yet been time for every experienced MySQL user to get hit by the need to
   commit things.

 This is mentioned in the MySQLdb FAQ:

 http://sourceforge.net/docman/display_doc.php?docid=32070group_id=22307

 The default behaviour has been changed to match the DB-API standard,
 which probably matches the normal behaviour on most mainstream
 relational database systems.

  As I said earlier, I don't use MySQL. I wasn't aware it didn't have
  transactions -- what did people use it for, then?  So is the upshot is
  that he should thump himself for using a DBMS w/o transactions,
  perhaps?

 Some awareness of common practice would certainly be beneficial.
 Attempting to insert data into PostgreSQL, Oracle, sqlite and so on
 would produce similar results to those described. The principal
 difference is that with MySQL (and presumably with things like
 Microsoft Access' storage engine that no-one takes seriously anyway),
 everyone has been able to get away with ignoring transactions and
 considering such behaviour as normal (or not even considering that
 anyone really uses anything which does anything else), and this
 obviously affects software governed by such assumptions.

 I suppose it's unfortunate for anyone who was using MySQLdb prior to
 release 1.2.0, especially if the software didn't give any obvious
 run-time warnings (not that I can say whether it did or not), but the
 MySQL-centric culture of ignoring/ridiculing stuff they don't support
 (and then eventually supporting it, in this case) is probably most to
 blame if we have to point the finger.

 Paul

Thanks for the thumping, will try harder next time.
_

 Thanks for commit comment i think that whats i need.
_

I think you should support people rather than pay them out despite
thier Ignorance.

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


Re: Newbie Q: Class Privacy (or lack of)

2006-07-27 Thread Ben Sizer
Steve Jobless wrote:
 Sybren Stuvel wrote:
 
  Steve Jobless enlightened us with:
   The first case can be just a typo, like:
  
 x.valeu = 5
  
   I make typos all the time. Without a spell checker, this message
   would be unreadable :).
 
  Then learn to read what you type, as you type it. Typing without
  errors can be trained.

 I'd rather let a machine to do that. Wasn't computer created for tasks
 like this? (No, not really. But...)

One of the benefits of Python is being able to dynamically add
attributes like that, or indeed to be able to access attributes that
haven't explicitly been created. If it raised an error each time you
tried to add something outside of an __init__ function, there are many
Python tricks you could no longer achieve. Unfortunately, when you come
from a language like C++ or Java where you learned to live without
those benefits, often not even knowing such things existed, all you see
are the negatives (ie. lack of checking).

--
Ben Sizer





The



 
   The second case can be like:
  
 x.next = y
 y.next = None
  
   to create a linked list by piggybacking next to the class. It will
   overwrite the iterater for the class if defined.
 
  You shouldn't simply pick a name and assign something to it without
  checking the current use of that name. It's pretty much true for
  everything in life.

 Well, the choice of next was not a good example. Sure, no one with
 decent Python knowledge would do that.
 But what about adding a method to the class? Am I supposed to ask Is
 anyone using name xxx? The class may be used by  developers I don't
 even know and some of them may be on the other side of the planet...

 
   If I was working on a large project with many engineers, I'd assume
   someone will do things like this sooner or later. I've seen many
   horrendous code in my life and I have no control over who I work
   with.
 
  Long live a versioning system. With that, you can find the person
  writing the horrible code, and slap them on the back of the head.
  People, like all animals, can be trained into doing the right thing.

 I'd like to. But often those people are long gone for one reason or
 another. Sometimes, it happens to be my boss...


 Maybe I should ask the question in a different way:

 What are the benefits from this? There must be something really good
 (and hopefully safe) you can do with it only from the outside. I don't
 have much problem over-riding functions from the inside of the class.
 But from the outside, I fail to see why anyone needs to add attributes
 or over-ride functions.
 
 SJ

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


Re: why is this not working? (nested scope question)

2006-07-27 Thread biner . sebastien

 Actually, the code in the book is:

 def f1():
  x = 88
  f2(x)

 def f2(x):
  print x

 f1()

 which makes all the difference in the world. Not to mention that this
 particular section of the book is giving an example of how to write the
 code *without* using nested functions.

Ouch! You got me there, I did not copy the code properly. Now I feel
stupid. Thanks for the enlightment.

I think I am starting to get it.

Sébastien.

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


Re: pysqlite2 fetching from select different than pysqlite?

2006-07-27 Thread schwehr
Thanks Tim!  That works well.  As a followup, is there a standard
compliant way to ask what fields are in the table?

-kurt

Tim Golden wrote:

 | I have some old pysqlite 1.x code that uses a pattern like this:
 |
 | cu.execute('SELECT weight FROM weights WHERE samplename=foo)
 | row = cu.fetchone()
 | weight=row['weight']
 |


 http://initd.org/pub/software/pysqlite/doc/usage-guide.html#accessing-co
 lumns-by-name-instead-of-by-index

 you have to specify a .row_factory before running the select
 
 TJG

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


Re: Using iterators to write in the structure being iterated through?

2006-07-27 Thread Pierre Thibault
On Wed, 26 Jul 2006 20:59:12 +0200, Peter Otten wrote:

 Pierre Thibault wrote:
 
 Hum, this example seems like a special case not really appropriate for my
 needs. Let me make my problem a little more precise. The objects I'll want
 to iterate through will always contain some floats. Very often, I guess
 they will be numpy.ndarray instances, but they can be something else as
 well. For instance, I will have to deal with arrays having internal
 symmetries (crystallographic datasets). The most efficient thing to do
 with those is save only the unique values and keep track of the symmetry
 operations needed to extract other values.
 
 A 1d examples: I have a class which represents a unit cell
 which has the additional mirror symmetry in the middle of the cell. Say
 C is an instance of this class representing data on a 50-long array. Only
 25 datum will be stored in C, and the class takes care of giving the value
 of C[0] if C[49] is asked, C[1] for C[48], and so on. Since crystals are
 periodic, the translational symmetry can also be managed (C[-1] == C[49]
 == C[0], C[-2] == C[48] == C[1]...). In any case, the user of this object
 need not know how the data is stored: for him, C is just a funny
 array-like object defined from -infinity to +infinity on a 1-D lattice.
 
 Now, I want to do simple math operations on the data in C. Doing a loop
 from 0 to 49 would loop twice through the actual data. In this
 context, an iterator is perfect since it can take care internally of going
 through the data only once, so it would be great to access _AND MODIFY_
 data over which I iterate.
 
 That would not only be nice but crucial to the integrity of your data. You'd
 have to explicitly forbid operations on individual cells as applying an
 operation to a single cell would change two or more cells, depending of the
 kind of symmetry.
 
 I now realize I don't know how to do that with numpy.ndarray either. It
 looks like the only way I can modify the content of an array is by using
 the [] notation (that is, in random access). For instance, the
 following will not change the state of a:
 
 import numpy
 a = numpy.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
 
 for c in a.flat
 c += 2.
 
 Of course, I know that a += .2 would be alright, but this is not general
 enough for more complicated classes.
 
 So I guess my problem is worst than I thought.
 
 Indeed, but I don't think it would be easier in C++...
 
 Peter

Well, no, C++ is better in this case because the thing you iterate over is
a generalized pointer, which can be dereferenced to get access to the
memory. So in other word, iterator.next() (in python) is a copy, while
*iterator (in C++) is a reference. 

Thanks anyway for your comments!

Pierre

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


RE: pysqlite2 fetching from select different than pysqlite?

2006-07-27 Thread Tim Golden
[EMAIL PROTECTED]
| Sent: 27 July 2006 15:01
| To: python-list@python.org
| Subject: Re: pysqlite2 fetching from select different than pysqlite?
| 
| Thanks Tim!  That works well.  As a followup, is there a standard
| compliant way to ask what fields are in the table?
| 
| -kurt

Assuming this is what you meant, look at:

http://sqlite.org/pragma.html

specifically at PRAGMA table_info

So, in python:

python
from pysqlite2 import dbapi2 as sqlite

db = sqlite.connect (:memory:)
db.row_factory = sqlite.Row

db.execute (CREATE TABLE x (i INTEGER, code VARCHAR (10), name VARCHAR
(60)))

for row in db.execute (PRAGMA TABLE_INFO ('x')):
  print row['name'], row['type']

/python

TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

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


Re: Nested function scope problem

2006-07-27 Thread Antoon Pardon
On 2006-07-27, Bruno Desthuilliers [EMAIL PROTECTED] wrote:
 danielx wrote:
 Bruno Desthuilliers wrote:
 
 (snip)

Surprising for me are actually two things: 1- the fact itself, and 2- that
term binding, and that whatever it means  (I'll have to read more on 
that,
now that I know the term)

a binding is the association of a name and a reference to an object in
a given namespace. It's different from the common notion of variable,
which is usually a symbolic name for a memory address storing a value
(like a pointer to an object's address).
 
 
 Wait, I'm not sure I see the difference. Isn't reference ~ C
 pointer. 

 For a very large definition of '~' !-)

 Are you saying Python variables don't hold references to
 actual Python objects? 

 Exactly.

 That idea has been working well for me so far.

 It can only take you so far. Now it's time you know the truth: there are
 *no* 'variables' in Python (hence the term 'binding').

That depends on what you want to mean with the term. IMO 'variables'
in python behave sufficiently similar as in other languages to use
the term.

 What you really have is (somewhat simplified, of course) a dict with
 names as keys and objects references (think of 'smart' pointers) as
 values.

That is just an implementation issue. Nothing prevents variables to
be associated with an index in a list.

 So the name doesn't 'hold' anything - it's really nothing more
 than a name.

In a language like C the name doesn't hold anything either. 
The name is just a way for refering to a memory space which
will hold something.

 And the object doesn't know nothing about which names it's
 bound to.

In a language like C, the value '3' doesn't know which variables
will hold it either.

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


Re: Functions and code objects

2006-07-27 Thread Fuzzyman

Fuzzyman wrote:
 Fuzzyman wrote:
  Hello all,
 
  I'm trying to extract the code object from a function, and exec it
  without explicitly passing parameters.
 
  The code object 'knows' it expects to receive paramaters. It's
  'arg_count' attribute is readonly.
 
  How can I set the arg_count to 0, or pass parameters to the code object
  when I exec it ?
 

 Ok, so now I'm getting somewhere, without really knowing what I'm
 doing. Using the CodeType I can create a new code object with identical
 attributes, except an 'argcount' of 0.

 It doesn't quite work, so I probably need to set some of the attributes
 *differently*.

 The code I use is :

  def f(x):
  ... print x
  ...
  c = f.func_code
  CodeType = type(c)
  a = CodeType(0, c.co_nlocals, c.co_stacksize, c.co_flags, c.co_code,
  ... c.co_consts, c.co_names, c.co_varnames, c.co_filename,
 c.co_name,
  ... c.co_firstlineno, c.co_lnotab, c.co_freevars, c.co_cellvars)
  a
 code object f at 01C707A0, file input, line 1
  exec a
 Traceback (most recent call last):
   File input, line 1, in ?
   File input, line 2, in f
 UnboundLocalError: local variable 'x' referenced before assignment

 (the first argument, 0, becomes the 'arg_count' attribute)

 So the code object is still creating a local scope, and knows that x is
 a local variable. ('co_nlocals' and 'co_varnames' ?).

 I'd like to construct the code object so that it takes the parameters
 from the enclosing scope (the context I pass into exec I guess),
 without clobbering any local variables that may be defined in the code
 object.

 Anyone got any clues ?


*Damn* I've extracted the code object and told it that it has no
arguments. Executing the code object results in the function object !

The code object is obviously the code object for the function
definition. *sigh*

I was hoping I could get to the code object for the *body* of the
function. Looks like that won't be possible without dis-assembling the
bytecode or other tricks even more hackish than what I've already done.

For the record, the code  I was using was :

x = 3
def f(x):
print x

CodeType = type(f.func_code)

def convert_function(f):
code = f.func_code
nlocals = max(code.co_nlocals - code.co_argcount, 0)
newCode = CodeType(0, nlocals, code.co_stacksize, code.co_flags,
   code.co_code, code.co_consts, code.co_names,
   code.co_varnames, code.co_filename,
code.co_name,
   code.co_firstlineno, code.co_lnotab,
code.co_freevars,
   code.co_cellvars)
return newCode

print convert_function(f)
exec convert_function(f)

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

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


Re: why is this not working? (nested scope question)

2006-07-27 Thread John Salerno
[EMAIL PROTECTED] wrote:

 Ouch! You got me there, I did not copy the code properly. Now I feel
 stupid. Thanks for the enlightment.
 
 I think I am starting to get it.

P.S. The point of the example was to show how nesting isn't necessary 
much of the time. The authors wanted to show that it is okay to write a 
call to f2 before f2 is even defined, as long as f2 is defined before 
that call is actually executed, i.e. when f1() is called. Keeping the 
two functions separate is cleaner than nesting, and passing parameters 
is how you get around the local scope issue.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threads vs Processes

2006-07-27 Thread Gerhard Fiedler
On 2006-07-26 19:10:14, Carl J. Van Arsdall wrote:

 Ah, alright.  So if that's the case, why would you use python threads 
 versus spawning processes?  If they both point to the same address space 
 and python threads can't run concurrently due to the GIL what are they 
 good for?

Nothing runs concurrently on a single core processor (pipelining aside).
Processes don't run any more concurrently than threads. The scheduling is
different, but they still run sequentially.

Gerhard

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


  1   2   3   >