Re: Cross platform Python app deployment

2007-07-30 Thread Benjamin Niemann
Hi,

Will McGugan wrote:

 Is there some reference regarding how to package a Python application
 for the various platforms? I'm familiar with Windows deployment - I use
 Py2Exe  InnoSetup - but I would like more information on deploying on
 Mac and Linux.

The standard way to package portable Python apps is a tarball with a
setup.py script. That should work on all systems where Python is already
installed, including Windows. Read http://docs.python.org/dist/dist.html,
if you have not already done so.

If you want more 'native' packages (as you already have for Windows, where
users are used to have a graphical installer), you'll have to
create .deb, .rpm, .whatever packages. You usually won't have to bother
about the Python interpreter, because it is either already present (it's a
standard package on most systems) or it will be pulled in via a package
dependency.
distutils already allows you to create rpm packages.


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread Benjamin Niemann
Hello,

Sakagami Hiroki wrote:

 What is the easiest way to create a daemon process in Python?  Google
 says I should call fork() and other system calls manually, but is
 there no os.daemon() and the like?

You could try
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 On Feb 21, 9:33 am, Eirikur Hallgrimsson [EMAIL PROTECTED]
 wrote:
 Sakagami Hiroki wrote:
  What is the easiest way to create a daemon process in Python?
 
 I've found it even easier to use the built in threading modules:
 
 import time
 
 t1 = time.time()
 print t_poc.py called at, t1
 
 import threading
 
 def im_a_thread():
 time.sleep(10)
 print This is your thread speaking at, time.time()
 
 thread = threading.Thread(target=im_a_thread)
 thread.setDaemon(True)
 thread.start()
 t2 = time.time()
 print Time elapsed in main thread:, t2 - t1
 
 
 Of course, your mileage may vary.

That's not a daemon process (which are used to execute 'background services'
in UNIX environments).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: calling php function from python

2007-02-14 Thread Benjamin Niemann
Rob Wolfe wrote:

 
 mark wrote:
 is it possible to call a php function from python and use a class from
 php in python? i want to use mmslib which creates mms messages and the
 only implementation is a php mmslib implementation.
 
 You can consider to use some kind of RPC (remote procedure call)
 for example XML-RPC. This is a platform and language independent
 solution.
 Here you have some information:
 http://www.faqs.org/docs/Linux-HOWTO/XML-RPC-HOWTO.html
 http://groups.google.pl/group/comp.lang.python/msg/5a6ae6290593fc97

For a quickstart it's probably easier to run the PHP code using the CGI
version of the PHP interpreter. E.g.
os.system('/usr/bin/php mms.php')
or using the subprocess module, if you want to pass some data to the script
(using stdin/out).

As long as no remote PHP server is involved this is easier and perhaps even
faster than the RPC approach...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: distutils: renaming setup.py ok?

2007-02-07 Thread Benjamin Niemann
Hello,

Anastasios Hatzis wrote:

 I want to distribute Python site-packages. Is it okay to use other setup
 file names than setup.py, which is mentioned in any place I read in the
 doc?
 
 E.g., setupMySDK.py, setupMyLib.py
 
 It seems that it works with distutils at least - but probably doing so
 has side-effects with other tools which may expect exactly setup.py as
 file name.

I'd say, it's mostly the user who is expecting setup.py as the filename.

Perhaps 'setup.py --install-sdk' and 'setup.py --install-lib' would be more
approriate (which can be done with distutils).

HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Inheriting str object

2007-02-05 Thread Benjamin Niemann
Marc 'BlackJack' Rintsch wrote:

 In [EMAIL PROTECTED],
 [EMAIL PROTECTED] wrote:
 
 I want to have a str with custom methods, but I have this problem:
 
 class myStr(str):
 def hello(self):
 return 'hello '+self
 
 s=myStr('world')
 print s.hello() # prints 'hello world'
 s=s.upper()
 print s.hello() # expected to print 'hello WORLD', but s is no longer
 myStr, it's a regular str!
 
 What can I do?
 
 Return a `myStr` instance instead of a regular `str`:
 
 def hello(self):
 return myStr('hello ' + self)

yes, but the 'upper' method is the problem here.
So you'd have to override all string methods, like

class myStr(str):
...

def upper(self):
return myStr(str.upper(self))


And I'm not sure, if it then works in the intended way...
What you are probably looking for, is to extend the 'str' class itself, so
every str instance has your added functionality.
Don't know, if this is possible at all, but usually it's not a good idea to
mess with the bowels of Python unless you have some greater surgical
skills.


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: marshal.loads ValueError

2007-01-31 Thread Benjamin Niemann
Hello,

abcd wrote:

 I have the following code which is sent over the wire as a string...
 
 from time import time
 class Foo:
 def go(self):
 print Time:, time()
 
 
 I get this code and store it as, data
 
 data = receivePythonSource()
 
 Then I try...
 
 exec marshal.loads(data) in my_module.__dict__
 
 However I get an error saying:
 ValueError: invalid literal for __float__: om time import time
 class Foo:
 def go(self):
 
 almost like when I try to marshal and exec it misses the first two
 characters, fr ...any ideas?

marshal is used to (de)serialize python objects from/to strings.
marshal.loads() tries to deserialize an encoded string back into a python
object - which does not make sense here.
What you probably want is:

exec data in my_module.__dict__


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is there a better way to implement this:

2007-01-22 Thread Benjamin Niemann
Michael Yanowitz wrote:

 Hello:
 
I wrote the code below (much irrelevant code removed).
 This doesn't quite work. What I wanted it to do was
  a) Execute function ftimed, which takes a function and a timeout
 in seconds.
  b) This will also execute function abort() as a thread.
 This function just runs for the specified
 number of seconds and returns.
 However, before it returns, throws an exception.
 c)  If test() is still running when abort() is
 finished, ftimed() should catch the exception and
 return.
 
 It is catching the exception, however it continues running the function.
 Why does it continue and not return?

The exception is raised in the thread that executes the abort() function.
The exception does not get caught and terminates this thread. The other
(main) thread is unaffected - exceptions are local to a thread and there is
currently no (portable) way to raise an exception in another thread.

 What am I missing, or is there a better way to
 implement this (having ftimed() return when the
 abort-timer time is exceeded?

You may use the signal.alarm() function, if you are on a UNIXoid system and
you have only a signle time-out at a time (e.g. not nested).

 import time, thread, sys
 
 thread_finished = MAX RUN TIME EXCEEDED!
 
 def abort (seconds):
  start_time = time.time()
  while ((time.time() - start_time)  seconds):
 time.sleep(0.01)

any reason for not using time.sleep(seconds) here?

  print script run time exceeded max_run_time of, seconds, seconds.
  raise thread_finished
  return
 
 
 def test():
 i = 0
 while (True):
time.sleep(1)
print HELLO, i
i+=1
 
 
 def ftimed (func, seconds):
 thread.start_new_thread (abort, (seconds,))
 
 try:
 func()
 except thread_finished:
 print  Timeout
 return
 
 ftimed (test, 30)
 print Script finished

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: html + javascript automations = [mechanize + ?? ] or something else?

2007-01-16 Thread Benjamin Niemann
Hello,

John wrote:

 John wrote:
 I have to write a spyder for a webpage that uses html + javascript. I
 had it written using mechanize
 but the authors of the webpage now use a lot of javascript. Mechanize
 can no longer do the job.
 Does anyone know how I could automate my spyder to understand
 javascript? Is there a way
 to control a browser like firefox from python itself? How about IE?
 That way, we do not have
 to go thru something like mechanize?
 
 I am curious about the webbrowser module. I can open up firefox
 using webbrowser.open(), but can one control it? Say enter a
 login / passwd on a webpage? Send keystrokes to firefox?
 mouse clicks?

Not with the webbrowser module - it can only launch a browser.

On the website of mechanize you will also find DOMForm
http://wwwsearch.sourceforge.net/DOMForm/, which is a webscraper with
basic JS support (using the SpiderMonkey engine from the Mozilla project).
But note that DOMForm is in a early state and not developed anymore
(according to the site, never used it myself).

You could try to script IE (perhaps also FF, dunno..) using COM. This can be
done using the pywin32 module https://sourceforge.net/projects/pywin32/.
How this is done in detail is a windows issue. You may find help and
documentation in win specific group/mailing list, msdn, ... You can usually
translate the COM calls from VB, C#, ... quite directly to Python.


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: UnboundLocalError

2006-11-09 Thread Benjamin Niemann
Hello,

Camellia wrote:

 why it generates an UnboundLocalError when I do the following:
 
 code
 ...
 def main():
 number = number()
 number_user = user_guess()
 while number_user != number:
 check_number(number = number, number_user = number_user)
 number_user = user_guess()
 
 UnboundLocalError: local variable 'number' referenced before assignment
 /code
 
 I found when I changed the number() to num() or whatever the issue
 solved
 but doesn't every function has its own namespace?
 Can anyone please explain it to me?

When Python compiles your code, it sees that you are using a local
variable 'number' in this function ('number = ...') and probably allocates
some memory for it or whatever - ask the core Python hackers for details.
When this statement is executed, the right hand side is evaluated first and
Python finds a reference to 'number'. It knows that number is a local
variable in main(), but has not been assigned yet (which would happend
after the right hand side is evaluated) - thus the error.

Even if you could get this to work, you have two 'number's in the function
with different meaning, which just makes understanding the code too
difficult.

Is number() (the global one) a function or class. I guess it's a function,
so I would suggest to name it 'get_number' (and same
for 'get_user_guess') - this makes the code more descriptive. Should it
really be a class, a common convention is to capitalize it 'Number()'.


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Deprecation in String.joinfields()

2006-09-25 Thread Benjamin Niemann
Hello,

Anoop wrote:

 I am getting the following error while trying to use deprecation
 
 Please help
 
 li = [a, b, mpilgrim, z, example]
 newname = string.joinfields (li[:-1], .)
 newname
 'a.b.mpilgrim.z'
 newname = li[:-1].joinfields(.)
 Traceback (most recent call last):
   File interactive input, line 1, in ?
 AttributeError: 'list' object has no attribute 'joinfields'
 
 newname1 = string.join (li[:-1], .)
 newname1
 'a.b.mpilgrim.z'
 newname = li[:-1].joinfields(.)
 Traceback (most recent call last):
   File interactive input, line 1, in ?
 AttributeError: 'list' object has no attribute 'join'
 
 Thank you for your help

joinfields is just an alias for join and only the latter is a method of the
string class (which should be used instead of the deprecated string.join
function), which means that the string . has a method join which takes
the list as an argument.

So what you want should be written as:

newname = ..join(li[:-1])


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I inherit member variables?

2006-09-21 Thread Benjamin Niemann
Hello,

[EMAIL PROTECTED] wrote:

 I'm trying to work with the following idea:
 
 class animal:
   def __init__(self, weight, colour):
 self.weight = weight
 self.colour = colour
 
 
 class bird(animal):
   def __init__(self, wingspan):
 self.wingspan = wingspan
 print self.weight, self.colour, self.wingspan
 
 class fish(animal):
   def __init__(self, length):
 self.length = length
 print self.weight, self.colour, self.length
 
 
 So basically I have a base class (animal) that has certain attributes.
 When animal is inherited by a specific instance, further attributes are
 added, but I need to have access to the original attributes (weight,
 colour). When I run my code from within the derived class, self.weight
 and self.colour are not  inherited (although methods are inherited as I
 would have expected).

You'll have to invoke the __init__ method of the superclass, this is not
done implicitly. And you probably want to add the weight and colour
attributes to your subclass in order to pass these to the animal
constructor.

class fish(animal):
  def __init__(self, length, weight, colour):
animal.__init__(self, weight, colour)
self.length = length
print self.weight, self.colour, self.length


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Can I inherit member variables?

2006-09-21 Thread Benjamin Niemann
LorcanM wrote:

 Benjamin Niemann wrote:
 
 You'll have to invoke the __init__ method of the superclass, this is not
 done implicitly. And you probably want to add the weight and colour
 attributes to your subclass in order to pass these to the animal
 constructor.

 class fish(animal):
   def __init__(self, length, weight, colour):
 animal.__init__(self, weight, colour)
 self.length = length
 print self.weight, self.colour, self.length

 Thanks for the reply.
 
 I think there's a basic misunderstanding about the nature of
 inheritance on my side.
 
 What I want to do is instantiate the sub class (derived class) from
 within the animal class. I then expect the sub class to have inherited
 some basic properties that it knows it has (weight, colour). If I can
 expand the example I gave previously to try to make this a little
 clearer:
 
 class animal:
   def __init__(self, weight, colour):
 self.weight = weight
 self.colour = colour
 
   def describeMyself(self, type, measurement):
 if type == 'bird':
   myanimal = bird(measurement)
 elif type == 'fish':
   myanimal = fish(measurement)
 
 class bird(animal):
   def __init__(self, wingspan):
 self.wingspan = wingspan
 print I'm a bird, weight %s, colour %s, wingspan %s %
 (self.weight, self.colour, self.wingspan)
 
 class fish(animal):
   def __init__(self, length):
 self.length = length
 print I'm a fish, weight %s, colour %s, length %s % (self.weight,
 self.colour, self.length)

Do you really want one animal instance to act both as bird and fish?
Wouldn't it be more sensible to instanciate either bird or fish (animal
being what is called an abstract class in other OOP languages)? bird and
fish would then have their own implementation of describeMyself() with
the 'print I'm a...' statement.

I must admit that I don't really understand what you are trying to
achieve...


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why a main() function?

2006-09-18 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 I think I read a suggestion somewhere to wrap the code where a Python
 script starts in a main() function, so one has
 
 def main():
 print hi
 
 main()
 
 instead of
 
 print hi
 
 What are the advantages of doing this?

Refine this to:

def main():
print hi

if __name__ == __main__:
main()

The advantage of the 'if __name__ ..' statement is that you can import the
script without running the 'main' code, e.g. from your unittest module.

Wrapping the main code in a function allows you to call this function from
your unittests and test it like any other function.

Additionally I do usually add an 'argv' argument to main() which I use
instead of sys.argv, so I can easily test it with different arguments.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.clock()

2006-07-14 Thread Benjamin Niemann
Tobiah wrote:

 The manual says:
 
 On Unix, return the current processor time as a
 floating point number expressed in seconds.
 
 So I ran this program:
 
 #!/usr/bin/python
 
 import time
 
 while 1:
 print time.clock()
 
 
 
 This gave me a stream of floats, the integer part of which
 only updated about every three seconds.  Now, the manual
 also states:
 
 The precision, and in fact the very definition of the meaning
 of ``processor time'', depends on that of the C function of the same name
 
 So I man 3 clock and notice:
 
 The value returned is the CPU time used so far as a clock_t; to get  the 
 number
of  seconds  used,  divide by CLOCKS_PER_SEC.
 
 So, I'm wondering how to get that value from python.  All I
 really want to do is know current time relative to a given
 point so that I can capture MIDI events, and store the time
 at which they arrive.  Am I barking up the wrong tree?

What you want sound like the 'wall clock' time. The CPU time is the time
that the CPU spent on executing your process. And unless the process uses
100% of the CPU, CPU time will appear to be 'slower' than the wall clock.
In your little program above the CPU spent about one third of the time on
this process and the rest is used for other processes (e.g. updating the
display).

What you need is time.time(), if its precision is sufficient.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb not updating rows

2006-06-28 Thread Benjamin Niemann
Bowen wrote:

 import md5
 import string
 import MySQLdb
 
 tc = raw_input(Teacher Code: )
 p = raw_input(New Password: )
 
 print tc
 hash = md5.new()
 hash.update(p)
 print p
 print hash.hexdigest()
 h = hash.hexdigest()
 
 boo = raw_input(Sure you want to update password with above details? Y
 or N: )
 
 if boo == 'y':
 db = MySQLdb.connect(copweb2, **, **, ***)
 cursor = db.cursor()
 if cursor.execute(UPDATE teachers SET password = '%s' WHERE
 teacher_code = '%s' % (h, tc)):
 print Done
 else:
 print Error
 else:
 print cancelled
 
 cursor.close()
 db.close()
 
 
 This code doesn't seem to update my database, anyone any idea why? Is
 it me being stupid? It doesn't kick out an error at all.

Another side note: don't build your queries using (dumb) string formatting,
let the MySQLdb module do it for you. More specifically use:

cursor.execute(
  UPDATE teachers SET password = %s WHERE teacher_code = %s,
  (h, tc)
  )

instead of

cursor.execute(
  UPDATE teachers SET password = '%s' WHERE teacher_code = '%s'
  % (h, tc)
  )

The former form takes care of quoting and escaping, your version did not
escape potentially harmful characters in tc, resulting in a possibly opened
door for SQL injection attacks. 

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib behaves strangely

2006-06-12 Thread Benjamin Niemann
Gabriel Zachmann wrote:

 Here is a very simple Python script utilizing urllib:
 
  import urllib
  url =
 http://commons.wikimedia.org/wiki/Commons:Featured_pictures/chronological;
  print url
  print
  file = urllib.urlopen( url )
  mime = file.info()
  print mime
  print file.read()
  print file.geturl()
 
 
 However, when i ecexute it, i get an html error (access denied).
 
 On the one hand, the funny thing though is that i can view the page fine
 in my browser, and i can download it fine using curl.
 
 On the other hand, it must have something to do with the URL because
 urllib works fine with any other URL i have tried ...
 
 Any ideas?
 I would appreciate very much any hints or suggestions.

The ':' in '..Commons:Feat..' is not a legal character in this part of the
URI and has to be %-quoted as '%3a'.
Try the URI
'http://commons.wikimedia.org/wiki/Commons%3aFeatured_pictures/chronological',
perhaps urllib is stricter than your browsers (which are known to accept
every b**t you feed into them, sometimes with very confusing results)
and gets confused when it tries to parse the malformed URI.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib behaves strangely

2006-06-12 Thread Benjamin Niemann
Benjamin Niemann wrote:

 Gabriel Zachmann wrote:
 
 Here is a very simple Python script utilizing urllib:
 
  import urllib
  url =
 http://commons.wikimedia.org/wiki/Commons:Featured_pictures/chronological;
  print url
  print
  file = urllib.urlopen( url )
  mime = file.info()
  print mime
  print file.read()
  print file.geturl()
 
 
 However, when i ecexute it, i get an html error (access denied).
 
 On the one hand, the funny thing though is that i can view the page fine
 in my browser, and i can download it fine using curl.
 
 On the other hand, it must have something to do with the URL because
 urllib works fine with any other URL i have tried ...
 
 Any ideas?
 I would appreciate very much any hints or suggestions.
 
 The ':' in '..Commons:Feat..' is not a legal character in this part of the
 URI and has to be %-quoted as '%3a'.

Oops, I was wrong... ':' *is* allowed in path segments. I should eat
something, my vision starts to get blurry...

 Try the URI
 'http://commons.wikimedia.org/wiki/Commons%3aFeatured_pictures/chronological',

You may try this anyway...


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: GUI viewer for profiler output?

2006-05-23 Thread Benjamin Niemann
Kent Johnson wrote:

 Can anyone point me to a GUI program that allows viewing and browsing
 the output of the profiler? I know I have used one in the past but I
 can't seem to find it...

If you use KDE, then you can convert the output of the hotshot profiler to
into a format that KCachegrind can read (using a script called
hotshot2calltree, which is part of KCachegrind).

If anyone has a more direct solution, I'd be interested, too :)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3.0 or Python 3000?

2006-04-10 Thread Benjamin Niemann
Dennis Lee Bieber wrote:

 On Sun, 9 Apr 2006 22:15:15 -0400, Tim Peters [EMAIL PROTECTED]
 declaimed the following in comp.lang.python:
 
 [John Salerno]
  Is 'Python 3000' just a code name for version 3.0, or will it really be
  called that when it's released?
 
 The smart money is on changing the name to Ecstasy, to leverage
 marketing publicity from the hallucinogenic club drug of the same
 name.  class will be renamed to rave, and the license will be
 changed to prohibit use by people with bipolar disorder.  Either that,
 or the name will be Python 3.0.
 
 Or... just to save 3000 as a time way down the road... The next
 major version of Python will be: Python PI (and each build will add
 another digit... 3.1, 3.14, 3.141, ...)

That's actually the versioning scheme of TeX, currently being at 3.141592

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why did someone write this?

2006-04-07 Thread Benjamin Niemann
Sandra-24 wrote:

 try:
exc_type, exc_value, exc_traceback = sys.exc_info()
# Do something
 finally:
exc_traceback = None
 
 Why the try/finally with setting exc_traceback to None? The python docs
 didn't give me any clue, and I'm wondering what this person knows that
 I don't.

You just have not found the right part of the doc:
http://docs.python.org/lib/module-sys.html#l2h-337

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: import random module

2006-03-22 Thread Benjamin Niemann
DataSmash wrote:

 Hi,
 When I import the random module at the python interpreter, it works
 fine:
 import random
 x = random.randint(1,55)
 print x
 14

 
 BUT, when I put the same code in a python script:
 * random.py:
 
 import random
 
 x = random.randint(1,55)
 print x
 
 and run it at the command line, I get:
 Traceback (most recent call last):
File p:\temp\random.py, line 7, in ?
   import random
File p:\temp\random.py, line 8, in ?
   x = random.randint(1,55)
 AttributeError: 'module object has no attribut 'randint'
 
 I run scripts at the command line everyday so there must be something
 specifically
 wrong with the random module, unless I'm totally missing something
 here.

This is not specific to the random module, try:

time.py:

import time
print time.time()


Don't name your script 'random.py' (or any other name from the stdlib).
'import random' will import the script itself (not the random module from
the stdlib), which is not what you want.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python version of XMLUnit?

2006-03-06 Thread Benjamin Niemann
Kent Johnson wrote:

 I have found XMLUnit to be very helpful for testing Java and Jython code
 that generates XML. At its heart XMLUnit is an XML-aware diff - it
 parses expected and actual XML and pinpoints any differences. It is
 smart enough to ignore things like attribute order, different quoting
 and escaping styles, and insignificant whitespace.
 
 Now I am working on a CPython project and have a similar need. Is there
 any comparable tool for Python? Basically I'm looking for a tool to
 compare XML and show diffs in an intelligible fashion that is usable
 from Python unit tests (using py.test, if it matters).

http://www.logilab.org/projects/xmldiff

You'd still have to integrate this into your test framework though...

And I'll have a look at XMLUnit - seem's like something I could use for my
current project ;)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Another stupid newbie question

2006-02-17 Thread Benjamin Niemann
Byte wrote:

 How can I make the following code:
 
 from random import random
 
 
 
 def stuff(a, mx):
 x = 2
 while x == 2:
 x = random()
 if x == mx: print x
 else: print 'No luck,', x
 x = 2
 
 Stop when x == mx?

What's the intention behind setting x = 2 at all?

def stuff(a, mx):
while True:
x = random()
if x == mx: print x
else: print 'No luck,', x

Should do the same as you're code above.

If stuff will never be called with mx=None, I would suggest using

def stuff(a, mx):
x = None
while x != mx:
x = random()
if x == mx: print x
else: print 'No luck,', x

Also note that random() returns a float and it is *very* unlikely that the
condition x == mx will ever come true

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Apparent eval() leak for python 2.3.5

2006-02-17 Thread Benjamin Niemann
John Marshall wrote:

 Hi,
 
 I am reposting this message from python-dev.
 
 Could someone please test the code below
 to verify that there is indeed a problem
 with eval() under python 2.3.5. I have
 rebuilt python 2.3.5 under the latest debian
 and under RH 7.3 (in case the problem is
 in system libraries).
 
 The following code causes the virtual memory
 allocation to constantly increase until there
 is no more memory left (the VIRT column of the
 'top' utility display).

I have this leak, too in python2.3 under debian/sid:
Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
[GCC 4.0.3 2005 (prerelease) (Debian 4.0.2-4)] on linux2

python2.4 seems to be unaffected.
Python 2.4.2 (#2, Nov 20 2005, 17:04:48)
[GCC 4.0.3 2005 (prerelease) (Debian 4.0.2-4)] on linux2

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python encoding bug?

2005-12-31 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 
 I was playing with python encodings and noticed this:
 
 [EMAIL PROTECTED]:~$ python2.4
 Python 2.4 (#2, Dec  3 2004, 17:59:05)
 [GCC 3.3.5 (Debian 1:3.3.5-2)] on linux2
 Type help, copyright, credits or license for more information.
 unicode('\x9d', 'iso8859_1')
 u'\x9d'

 
 U+009D is NOT a valid unicode character (it is not even a iso8859_1
 valid character)

It *IS* a valid unicode and iso8859-1 character, so the behaviour of the
python decoder is correct. The range U+0080 - U+009F is used for various
control characters. There's rarely a valid use for these characters in
documents, so you can be pretty sure that a document using these characters
is windows-1252 - it is valid iso-8859-1, but for a heuristic guess it's
probably saver to assume windows-1252.

If you want an exception to be thrown, you'll need to implement your own
codec, something like 'iso8859_1_nocc' - mmm.. I could try this myself,
because I do such a test in one of my projects, too ;)

 The same happens if I use 'latin-1' instead of 'iso8859_1'.
 
 This caught me by surprise, since I was doing some heuristics guessing
 string encodings, and 'iso8859_1' gave no errors even if the input
 encoding was different.
 
 Is this a known behaviour, or I discovered a terrible unknown bug in
 python encoding implementation that should be immediately reported and
 fixed? :-)
 
 
 happy new year,
 

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can modules be dynamically reloaded

2005-11-16 Thread Benjamin Niemann
Steve wrote:

 Can python modules be reloaded.

Yes:

import mymod
...
reload(mymod)

But this will not always work as expected (what someone would expect who
does not really understand how it works...). Read the documentation on
http://docs.python.org/lib/built-in-funcs.html#l2h-59

 For example you import a module, programatically edit it, then have the
 file reload the module?
 
 Does anyone have any ideas about this?
 Steve

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making a persistent HTTP connection

2005-11-14 Thread Benjamin Niemann
Diez B. Roggisch wrote:

 David Rasmussen wrote:
 I use urllib2 to do some simple HTTP communication with a web server. In
 one session, I do maybe 10-15 requests. It seems that urllib2 opens op
 a connection every time I do a request. Can I somehow make it use _one_
 persistent connection where I can do multiple GET-receive data passes
 before the connection is closed?
 
 Are you sure HTTP supports that? This would be news to me - which
 doesn't mean much :)

It does (HTTP/1.1 at least) and it's called 'keep-alive'.

 And even if it works - what is the problem with connections being created?

Performance, network load...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python's website does a great disservice to the language

2005-11-01 Thread Benjamin Niemann
Steven D'Aprano wrote:

 http://www.python.com/ perhaps?

Yep, let's make this the new official python site ;)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to translate python into C

2005-10-28 Thread Benjamin Niemann
Johnny Lee wrote:

 Hi,
First, I want to know whether the python interpreter translate the
 code directly into machine code, or translate it into C then into
 machine code?

Neither this nor that. The interpreter first compiles the code into python
'byte code' - something similar to machine code, but not it is not targeted
at the CPU of your system, but for a portable virtual machine. This virtual
machine will then execute the byte code, just like a CPU would execute
machine code.

Second, if the codes are translated directly into machine codes, how
 can I translate the codes into C COMPLETELY the same? if the codes are
 translated first into C, where can I get the C source?

You may have a look at PyPy. I do not know what it exactly can do, but this
might be interesting for you:
http://codespeak.net/pypy/dist/pypy/doc/faq.html#how-do-i-compile-my-own-programs

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pickle to source code

2005-10-26 Thread Benjamin Niemann
Gabriel Genellina wrote:

 I want to convert from pickle format to python source code. That is,
 given an existing pickle, I want to produce a textual representation
 which, when evaluated, yields the original object (as if I had
 unpickled the pickle).
 I know of some transformations pickle/xml (Zope comes with one such
 tool, gnosis xml is another) so I believe I could build something based
 on them.
 But I dont want to reinvent the wheel, I wonder if anyone knows of a
 library which could do what I want?

If all objects correctly implement the __repr__ method (true for built-in
stuff like list, dict, set...):
Just unpickle it and call repr() on the resulting object.


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question about StringIO

2005-10-10 Thread Benjamin Niemann
Frank Millman wrote:

 I will try to explain my experience with popen() briefly.
 
 I have some sql scripts to create tables, indexes, procedures, etc. At
 present there are about 50 scripts, but this number will grow. I have
 been running them manually so far. Now I want to automate the process.
 
 I am supporting PostgreSQL and MS SQL Server, and the syntax is
 slightly different in some cases. Rather than maintain two sets of
 scripts, I prefix some lines with -pg- or -ms- to indicate the
 platform, and then use Python to parse the scripts and generate a
 correct output for each platform, passing it to 'psql' and 'osql'
 respectively, using popen().
 
 I have had a few problems, but it would take too long to describe them
 all, and I just want a working solution, so I will focus on my latest
 attempt.
 
 I run through all the scripts and create a StringIO object with the
 string I want to pass. It is about 250 000 bytes long. If I run psql
 using popen(), and pass it the string via stdin, it works fine, but I
 get all the messages on the screen. If I do the same, but end the
 command with '  fjm 21' it works correctly and the messages end up
 in the file fjm, which is about 40 000 bytes long. If I run it with
 popen4(), it starts ok, but then hangs about 1/4 of the way through.
 Exactly the same happens on MSW. It seems to be hitting a limit on the
 size of the stdout file - is that possible?
 
 For my purposes, I will be happy to use popen() and a choice of no
 redirection, redirect to a file, or redirect to /dev/null. The question
 about popen4() is therefore academic, though I would be interested to
 know the answer.

That's probably a deadlock as described in
http://docs.python.org/lib/popen2-flow-control.html

 BTW, is there an equivalent of /dev/null on MSW?

Dunno - but as a last resort, you could create a tempfile with a unique name
(to be sure, not to override any existing data), dump your output there and
later os.unlink() it...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to get the return value of a thread?

2005-09-09 Thread Benjamin Niemann
Leo Jay wrote:

 Dear all,
 
 i would like to get the return value of all threads
 
 e.g.
 def foo(num):
 if num10:
 return 1
 elif num50:
 return 2
 else
 return 0
 
 
 after i invoked
 t = thread.start_new_thread(foo,(12,))
 how to get the return value of `foo'?

Take a look at the Queue module. Create a queue instance at let the 'foo
thread' put() its result into it:

fooResult = Queue.Queue()

def foo(num):
result = 0

if num10:
result = 1
elif num50:
result = 2

fooResult.put(result)

t = thread.start_new_thread(foo,(12,))

# do other stuff, foo is running in background

r = fooResult.get() # guaranteed to block until result is available
print r

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ~ after script filename?

2005-09-07 Thread Benjamin Niemann
presentt wrote:

 Hello all,
 
 I just wrote a really simple script and named it helloworld.py.  Inside
 was only:
 
 #!/usr/bin/env
 print Hello, world
 
 I used chmod to set the permissions, and ran it to see what happened (I
 just started learning Python, if you couldn't guess)
 
 Then, I typed ls in the directory to see what was there, and I noticed
 a new file, namely helloworld.py~ .  What is that file (its contents
 are identicle to helloworld.py)?  Why the ~?
 
 Thanks a lot.  I'm using Ubuntu Linux 5.04 (Hoary), and wrote the
 script with gedit.
 
 ~~Ted Present

As others have already said: this is not a python issue.

A ~ suffix is commonly used by editors for backup file. If you save a file
from the editor, and the file already exists (so it doesn't happen the
first time you save a new file), the existing version is renamed with the ~
suffix.

After some time, you will learn to simply ignore these files ;)
Many file managers have already learnt this lesson and have options to hide
such backup files.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is my thread safe from premature garbage collection?

2005-09-01 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 Hello all,
 
 I'm aware that in Python an object is cleared for garbage collection as
 soon as the last reference to it disappears. Normally this is fine.
 However, in my current project I'm creating a bunch of threads which
 are supposed to run until they've completed their run() method, and I'm
 worried that if I do not keep references to these thread objects
 around, the GC might happily delete them (and thereby kill my thread
 routines maybe?) while they're not done yet. Is this fear justified? Or
 is the Python GC smart enough to leave thread objects alone until their
 run() methods have finished?
 
 If not, I do have a workaround, but it is a bit clumsy IMO. Basically I
 would just keep a list into which each thread object enters a reference
 to itself on creation. This way I'd ensure that I have a reference to
 the thread to prevent the GC from killing it. Then, when a thread is
 about to finish its run() method, the thread finds and removes that
 reference to itself from my list of thread references.
 
 Anyway, if anyone could make a definite statement on whether threads
 are safe from unwanted garbage collection, that'd be really great.
 Thanks in advance for any helpful replies!

The threading module does already take care of keeping references to all
running threads, so there's no need to do it yourself and your threads are
safe from being garbage collected.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Decrypting GPG/PGP email messages

2005-09-01 Thread Benjamin Niemann
posted  mailed

Alessandro Bottoni wrote:

 I know you will shake you head sadly but... I really have to perform such
 a suicidal task (even if for a short time and just for internal use).
 
 I have to send by email (over the open internet) a XML file containing
 _system commands_ (yes: the kind of stuff like rm -dfr /) to a server
 and have a Python program sitting on this server, fetching and parsing the
 e-mail message and executing the commands (maybe with _root privileges_).
 
 Of course, I want to be sure that only the allowed people is able to send
 such dangerous messages to my server so I will ask my users to encrypt and
 digitally sign their messages using Thunderbird, Enigmail and GPG as
 described in this very fine tutorial:
 
 http://goldenspud.com/webrog/archives/2005/03/10/encrypt-encrypt/
 
 So far, so good, but I still have a couple of doubts about the server
 side:
 
 1) What would you use to decrypt the messages? The GPG module created by
 Andrew Kuchling is declared incomplete and no more maintained on his
 web pages (http://www.amk.ca/python/code/gpg) so I think it is out of the
 game. Would you use OpenPGP (http://www.aonalu.net/openpgp/python)? Any
 other module?

What about using the command line program via os.pipeX(gpg...)?
I've done it this way when I needed to _create_ encrypted mail attachments
using python (you'll need different gpg options for decrypting):

pipe_in, pipe_out = os.popen2(/usr/bin/gpg -q -r KEYID -s
   --passphrase-fd 0 --batch --no-tty -a -o - -e '%s'
   % path_to_temporary_file)
pipe_in.write(passphrase)
pipe_in.close()

# read encrypted file from pipe_out
pipe_out.close()


 2) I did not find any mention of _encrypted attachments_ on the Net. Does
 anybody know of a tutorial or a guide that explains how to encrypt (with
 Thunderbird/Enigmail) and decrypt (with Python) the (ANSI text) files
 attached to a email message?

I can't help you with Thunderbird. In the worst case, you'll have to encrypt
your command file manually and attach the encrypted version to your mail.
KMail does have checkboxes for encrypt/sign every attachment separately...


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is my thread safe from premature garbage collection?

2005-09-01 Thread Benjamin Niemann
Sion Arrowsmith wrote:

 In article [EMAIL PROTECTED], Benjamin Niemann  [EMAIL PROTECTED]
 wrote:
[EMAIL PROTECTED] wrote:
 However, in my current project I'm creating a bunch of threads which
 are supposed to run until they've completed their run() method, and I'm
 worried that if I do not keep references to these thread objects
 around, the GC might happily delete them (and thereby kill my thread
 routines maybe?) while they're not done yet. Is this fear justified?
The threading module does already take care of keeping references to all
running threads,
 
 The implementation of threading.enumerate() would be entertaining if it
 didn't.
 
 Quite apart from which, I presume the OP's run() method looks something
 like:
 class MyThread(threading.Thread):
 def run(self):
 ...
 So what is self if not a reference to the Thread object which is kept
 around until run() has completed?

This was just too obvious;) Looking at the sourcecode of the threading
module and discovering the 'limbo' dict, where every thread stores a
reference to itself, was certainly more entertaining.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: is there a better way to check an array?

2005-09-01 Thread Benjamin Niemann
jdonnell wrote:

 I want to check if a value is in an array. I'm currently doing it as
 follows, but I just don't like this way of doing it. It seems
 unpythonic.
 
 fieldIsRequired = true
 try:
 notRequiredAry.index(k)
 fieldIsRequired = false
 except ValueError:
 pass
 
 # throw expception if field is required and is empty
 if(product[k] == '' and fieldIsRequired):
 raise GMError(k + ' is required')

if product[k] == '' and k in fieldIsRequired:
raise GMError(k + ' is required')

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: time.strptime() for different languages

2005-08-31 Thread Benjamin Niemann
Adam Monsen wrote:

 Anyone know of something that works like time.strptime(), but for
 other languages? Specifically, Dutch (ex: 31 augustus 2005, 17:26)
 and German?
 
 Thinking out loud... since 31 augustus 2005, 17:26 is only different
 by month name, I suppose I could just substitute the month name using
 a translation table for English to Dutch month names.

Have you tested it with the proper locale setting and strptime(dateString,
%c)? I have not ;)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic image creation for the web...

2005-08-28 Thread Benjamin Niemann
Tompa wrote:

 Hi,
 
 I would like to create images on the fly as a response to an http request.
 I can do this with PIL like this (file create_gif.py):
 from PIL import Image, ImageDraw
 
 print 'Status: 200 OK'
 print 'Content-type: text/html'
 print
 print 'HTMLHEADTITLEPython Dynamic Image Creation
 Test/TITLE/HEAD' print 'BODY'
 im = Image.new(P, (600, 400))
 draw = ImageDraw.Draw(im)
 draw.rectangle((0, 0) + im.size, fill=blue)
 im.save(images/tmp.gif);
 print 'img src=/scripts/images/tmp.gif'
 print '/BODY'
 
 
 However, I would like to 1) avoid saving the image in a file on disk and
 2) separate the HTLM code from the python image creation code.
 
 Something like this is what I have in mind:
 (file index.html):
 html
 headmeta HTTP-EQUIV=content-type CONTENT=text/html; charset=UTF-8
  titlePython Dynamic Image Creation/title
 /head
 IMG SRC=/scripts/create_image.py ALT=Image created on the fly...
 /html
 
 and in file create_image.py:
 from PIL import Image, ImageDraw, ImageFont
 im = Image.new(P, (600, 400))
 draw = ImageDraw.Draw(im)
 draw.rectangle((0, 0) + im.size, fill=blue)
 
 
 Unfortunately this does not work :-(
 What is missing?

You are almost there. Your create_image.py does not return anything to the
browser yet.

First return proper HTTP headers, e.g.

sys.stdout.write('Status: 200 OK\r\n')
sys.stdout.write('Content-type: image/gif\r\n')
sys.stdout.write('\r\n')

(Your prints above are mostly equivalent, but do not output the correct \r\n
as line terminator - at least on UNIX style systems. Most webservers
tolarate this, if it's coming from a CGI - but doing it right and not
relying on a certain server behaviour is not bad anyway ;)

Then check the PIL docs to find out, how to output the image to sys.stdout
(instead of writing to a file).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dynamic image creation for the web...

2005-08-28 Thread Benjamin Niemann
Tompa wrote:

 Benjamin Niemann pink at odahoda.de writes:
 You are almost there.
 I don't feel so...
 
 Your create_image.py does not return anything to the
 browser yet.
 Yes, I am aware of that but I do not what to return.
 
 First return proper HTTP headers, e.g.
 
 sys.stdout.write('Status: 200 OK\r\n')
 sys.stdout.write('Content-type: image/gif\r\n')
 sys.stdout.write('\r\n')
 
 Ok, but if possible I'd rather not return anything HTTP/HTML-related from
 my create_image.py file.

When the browser fetches the images for displaying, it performs just another
HTTP request, and you must reply with a valid HTTP response. The
Content-type header is the absolute minimum that must always be returned.
(IIRC the 'Status' can be omitted, if it's 200).

 Then check the PIL docs to find out, how to output the image to
 sys.stdout (instead of writing to a file).
 
 Ok, then I get this:
 
 from PIL import Image, ImageDraw
 import sys
 
 im = Image.new(P, (600, 400))
 draw = ImageDraw.Draw(im)
 draw.rectangle((0, 0) + im.size, fill=blue)
 
 sys.stdout.write('Status: 200 OK\r\n')
 sys.stdout.write('Content-type: image/gif\r\n')
 sys.stdout.write('\r\n')
 
 im.save(sys.stdout, GIF)
 
 But this does not work.
 I also tested to skip the HTTP-header stuff and just write the gif to
 sys.stdout, believing that that would work. But not so...

Works perfectly here...
What does the error.log of the webserver say?

 Hmm, I'm a newbie to Python (as you already probably have noticed ;-) so I
 don't know what else I should try. Any hints are welcome!
 
 /Tompa

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Speed quirk: redundant line gives six-fold speedup

2005-08-26 Thread Benjamin Niemann
Raymond Hettinger wrote:

 
 Mark Dickinson wrote:
 I have a simple 192-line Python script that begins with the line:

 dummy0 = 47

 The script runs in less than 2.5 seconds.  The variable dummy0 is never
 referenced again, directly or indirectly, by the rest of the script.

 Here's the surprise: if I remove or comment out this first line, the
 script takes more than 15 seconds to run.  So it appears that adding a
 redundant line produces a spectacular six-fold increase in speed!
 
 Thanks for your post.  It is cute, brilliant, and interesting.
 
 I haven't had time to investigate but can point at the likely cause.
 All of the global names are stored in a single hash table.  Search time
 is dictated by two factors, the sparseness of the hash table and the
 distribution of hash values.  With respect to sparseness, whenever that
 table becomes 2/3 full, it grows by a factor of four and becomes only
 1/6 full (leading to many fewer collisions).  With respect to
 distribution, it should be noted that string hash values are decidely
 non-random and your variable names likely congested consecutive spaces
 in a nearly full table (resulting in seven times as many search probes
 to find a global value).
 
 When the extra value was added, it likely resized the table four-fold
 and redistributed the hash values into fewer consecutive positions.

If that's the cause, then here's another reason to use long, descriptive
names instead of C64-BASIC style a, b, c, i, j... - with long names the
chances of hash collisions are pretty low.
Or everyone will start optimizing their programs by using long, *random*
names ;)


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Warning when doubly linked list is defined gloablly

2005-08-24 Thread Benjamin Niemann
chand wrote:

 Hi.,
 
 In my api.py file 'g_opt_list' is defined globally
 g_opt_list =[[],[],[],[],[],[],[]]
 
 I am using this global list in the fucntion
 
 def function ():
 gloabl g_opt_list

global?

 when I run the py file, I am getting the Following Error
 
 SyntaxWarning: name 'g_opt_list' is used prior to global declaration
 
 Please let me know how to remove this error

This happens in cases like
 a = 1
 def c():
...   a
...   global a

Does your source only consist of the three lines above? (These would trigger
a SyntaxError because of the typo...)
You should provide a more complete example, so we can tell you where the
problem is. Try to build a minimal file that reproduces the problem. Often
you will find the line causing the error yourself - and the fix might be
obvious to you -, if your strip stuff away.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: DeprecationWarning: Non-ASCII character '\xf3'

2005-08-23 Thread Benjamin Niemann
jau wrote:

 Hi co-listers!
 
 I have been off Python for 2 years and now, that i'm used to Eclipse and
 Java, I decided to start a project with Python to refresh skills this
 time using Eclipse and TrueStudio. But now, two things can be occured
 since the last time i used it.
 
 the first one, something concerning to the encoding has changed and i
 haven't noticed it.
 
 the other one, when using Python from Eclipse i have to add any special
 config lines at the begining of my Python files.
 
 if i have this hello world python program (i have to call it by
 someway, hahaha)
 
 print hello world
 
 i get this output
 
 hello world
 sys:1: DeprecationWarning: Non-ASCII character '\xf3' in file
 C:\Workspace\JJ\src\es\jau\main.py on line 2, but no encoding declared;
 see http://www.python.org/peps/pep-0263.html for details

 the article mentioned above didn't explain so much for me.
 
 i doesn't look to be an error, but curiosity is bitting me... what's
 really happening here? Do I need to do any special thing to avoid this?

The single print statement above should not trigger this warning. Are there
any non-ASCII character in the file (perhaps comments?).
If you use non-ASCII characters in python source files, you have to tell the
interpreter which encoding you are using - otherwise funny things will
happen. You have to find out which encoding Eclipse uses to save file
(looks like iso-8859-1) and add the line

# -*- coding: iso-8859-1 -*-

at the top of the file.

Python does not make any assumptions about the encoding beyond US-ASCII
(perhaps it does, but it does not encourage taking advantage of this) - if
you use anything else, then you'll have to declare it.

This will be important, if you use unicode strings:

ustränge characters

Python must know the encoding of the file in order to decode the string
literal into an unicode string.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop in python

2005-08-22 Thread Benjamin Niemann
km wrote:

 Hi all,
 
 thing.  If *all* your loops are going to do is print stuff, then you're
 doing the right thing with the version that emits values.
 
 ya most of the loops print values.

In that case, you are interested in IO performance. The time spent handling
the loop is not significant compared to the time spent executing the
'print' statement - which is a very complex operation despite its simple
usage.

An implementation where a simple control flow structure has an measurable
impact on the overall timing of an IO bound program would really suprising.
Python suprises me often, but in different ways ;)

Perhaps you could try to use sys.stdout.write() directly, avoiding the
overhead of 'print'. Perhaps there are differences between the (default)
output buffering settings of perl and python.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adobe COM with Python

2005-08-19 Thread Benjamin Niemann
Andy W wrote:

 I'm wanting to automate Adove Acrobat Reader using Com thru Python and
 win32com, but i can't find any documentation for the Adobe stuff? Has
 anyone done anything with Acrobat COM ?
 
 I've searched Google groups and the web but am unable to find anything.

I have not hacked Acrobat yet, but done a bit with InDesign - Adobe's
documentation is less than perfect, but still pretty good. You might look
at
http://partners.adobe.com/public/developer/acrobat/sdk/index_doc.html#js
- in InDesign I could easily map the methods and attributes from the JS
documentation to COM calls (the JS members start with lowercase - e.g.
'properties' -, while COM need uppercase - 'Properties').


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Save Binary data.

2005-08-19 Thread Benjamin Niemann
GMane Python wrote:

 Hello All.
   I have a program that downloads 'gigabytes' of Axis NetCam photos per
   day.
 Right now, I set up the process to put the images into a queue, and every
 30
 or so seconds, 'pop' them from the queue and save them to disc.   I save
 them as individual files.
 
   I think that I'd like to modify it to save into one file 100-200 images,
 so that I don't have directories with 50,000-90,000 frames before handing
 that off to a DivX Encoder.
 
   I don't know if I need to use something like cPickle, or maybe just save
 them as a binary data file (which would be a temp file until later in the
 day when I open it to begin the encoding process.)
 
 Can someone please help me with some direction?

You could use the tarfile module to create a single file holding a arbitrary
number of files - including compression, if you want, but if your images
are JPEGs then further compression is mostly a waste of CPU cycles. You can
extract the images later on with either a python script and tarfile or
using the standard commandline tool 'tar'.

If the images are uncompressed anyway, you could have a look at the netpbm
suite and its fileformat (which is pretty simple, but uncompressed and
would bloat JPEGs to a multiple of the original filesize) which supports
'image sequences'. Perhaps a DivX encoder could even support this
fileformat directly as input.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: stopping a python windows service

2005-08-16 Thread Benjamin Niemann
DK wrote:

 i was able to successfully create a windows service using py2exe. it
 polls a website periodically and logs it to a file. this is done using
 a function that does an infinite loop with periodic sleeps.
 
 my question is...
 
 what's the best way to stop this service gracefully?
 
 when try to stop it from the services applet from control panel, it
 takes forever and then gives me an error.
 
 currently, the only way i am able to stop it is using the task manager
 and killing the process.

Windows services generally use two threads: one to do the work and one to
listen for messages from the
whatever-the-component-is-called-to-control-services.
When the message thread received a 'stop' message, it should inform the
worker thread to shut down, e.g. using threading.Event. So your worker
should regularily check for the shutdown event, e.g.:

while not shutdownEvent.isset():
pollWebsite()

for i in xrange(1800):
if shutdownEvent.isset():
break
time.sleep(1)

But if you get the 'stop' message while the worker thread is in
pollWebsite() and the webserver is slw, you'll still have a significant
delay... To avoid this, you would need a http client based on select() that
allows you to check shutdownEvent.isset() at certain intervals - instead of
urlopen which just blocks.


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compile time checking?

2005-08-13 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 On Fri, 12 Aug 2005 22:25:07 -0700
 Steve Jorgensen wrote:
 
 Since Python does not use manifest typing, there's not much you can do
 about this, but typeless languages like this are great if you're using a
 process
 that finds the errors the compiler would otherwise find.  I'm referring,
 of course, to Test Driven Development (TDD).
 
 If you do TDD, you won't miss compile-time checking much.  In fact, the
 extra kruft that manifest typing requires is an annoying burden when
 doing TDD, so Python is a breath of fresh air in this regard.
 
 What test should one implement to catch that kind of errors like in OP
 example?

A 'good' testsuite should run each line of the code at least once. There's
more to do for a good code coverage, but that's the most basic requirement.
So the faulty line will be executed by the testsuite and the runtime
checking will raise an exception.

Static typing is a very rudimentary type of code checking that is performed
by the compiler with 100% code coverage. Such test won't tell you, if your
code is actually doing what it is supposed to do (or anything sensible at
all) and are mostly useless. If you think that code quality is important,
you'll have to perform much more and more sophisticated tests. Such a
testsuite is usually (I can't think of cases where it is not) a superset of
static code checking - all type bugs are found be the testsuite even in the
absence if static type checking.

So you won't loose anything, if you drop static typing.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help with Regular Expressions

2005-08-10 Thread Benjamin Niemann
Harlin Seritt wrote:

 Forgive another question here, but what is the 'r' for when used with
 expression: r'\w+...' ?

r'..' or r.. are raw strings where backslashes do not introduce an
escape sequence - so you don't have to write '\\', if you need a backslash
in the string, e.g. r'\w+' == '\\w+'.
Useful for regular expression (because the re module parses the '\X'
sequences itself) or Windows pathes (e.g. r'C:\newfile.txt').

And you should append a '$' to the regular expression, because
r[EMAIL PROTECTED] would match '[EMAIL PROTECTED]', too.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Compile time checking?

2005-08-10 Thread Benjamin Niemann
Qopit wrote:

 [snip]

 My questions are:
 - Am I missing something with my tester example?
 - Are there other code-checking options other than PyChecker?

Try pylint

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: about coding

2005-08-06 Thread Benjamin Niemann
cantabile wrote:

 Hi, being a newbie in Python, I'm a bit lost with the '-*- coding : -*-'
 directive.
 
 I'm using an accented characters language. Some of them are correctly
 displayed while one doesn't. I've written :
 -*- coding: utf-8 -*-
 
 Is this wrong ?
 
 Where can I find a pratical explanation about these encodings ?

The coding line tell the interpreter to assume that the file is encoded as
utf-8. It's the job of the editor that you are using, to actually encode
the file as utf-8.
The coding directive uses a format that is automatically recognized by the
emacs editor. If you are using another editor, you'll have to search its
menus for the proper way to save files as utf-8.


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Putting function references in a Queue

2005-08-06 Thread Benjamin Niemann
Richard Townsend wrote:

 I've been experimenting putting a reference to a function into a Queue
 object and was wondering what actually gets put in the Queue - is it the
 function's code object?

No, it's justa referenceto the function object.
 
 If I read from the Queue in a different module, it appears that I don't
 need to import the module that defines the function - or any module that
 it uses - is this generally true, or are there some conditions to be aware
 of?

There function reference is sufficient to call the function, there's not
need to import the module containing the function. It has already been
imported (otherwise the function wouldn't be there) and the function has a
reference to this module in order to resolve its global (module-level)
references.

 The scenario I'm working on has child threads doing some tasks and then
 sending back tuples (done, function, args, kwargs) via the Queue, to be
 called in the main thread. The Python code is ultimately embedded in a
 C/Motif app.

As long as you take care of the usual threading issues (concurrent access to
shared objects guarded by semaphores etc.), there should not be any greater
problems.
The function will be executed in the thread that is called it of course (and
not in the thread that sent the reference to it).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Py: a very dangerous language

2005-08-05 Thread Benjamin Niemann
Luis M. Gonzalez wrote:

 This is great!
 It's absolutely useless, like a real therapist, but it's free!

Never heard of Eliza? Even Emacs has it built in (Menu Help - Emacs
Psychiatrist).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: minidom xml non ascii / unicode files

2005-08-05 Thread Benjamin Niemann
webdev wrote:

 lo all,
 
 some of the questions i'll ask below have most certainly been discussed
 already, i just hope someone's kind enough to answer them again to help
 me out..
 
 so i started a python 2.3 script that grabs some web pages from the web,
 regex parse the data and stores it localy to xml file for further use..
 
 at first i had no problem using python minidom and everything concerning
 my regex/xml processing works fine, until i tested my tool on some
 french page with non ascii chars and my script started to throw errors
 all over the place..
 
 I've looked into the matter and discovered the unicode / string encoding
 processes implied when dealing with non ascii texts and i must say i
 almost lost my mind.. I'm loosing it actually..

The general idea is:
- convert everything that's coming in (from the net, database, files) into
unicode
- do all your processing with unicode strings
- encode the strings to your preferred/the required encoding when you write
it to the net/database/file

 so here are the few questions i'd like to have answers for :
 
 1. when fetching a web page from the net, how am i supposed to know how
 it's encoded.. And can i decode it to unicode and encode it back to a
 byte string so i can use it in my code, with the charsets i want, like
 utf-8.. ?

First look at the HTTP 'Content-Type' header. If it has a parameter
'charset', that the encoding to use, e.g.
Content-Type: text/html; charset=iso-8859-1

If there's not encoding specified in the header, look at the ?xml .. ?
prolog, if you have a XHTML document at hand (and it's present). Look below
for the syntax.

The last fallback is the meta http-equiv=Content-Type content=... tag.
The content attribute has the same format as the HTTP header.

But you can still run into UnicodeDecodeErrors, because many website just
don't get their encoding issues right. Browser do some (more or less)
educated guesses and often manage to display the document as intended.
You should probably use htmlData.encode(encoding, ignore) or
htmlData.encode(encoding, replace) to work around these problems (but
loose some characters).

And, as said above: don't encode the unicode string into bytestrings and
process the bytestrings in your program - that's a bad idea. Defer the
encoding until you absolutely necessary (usually file.write()).

 2. in the same idea could anyone try to post the few lines that would
 actually parse an xml file, with non ascii chars, with minidom
 (parseString i guess).

The parser determines the encoding of the file from the ?xml..? line. E.g.
if your file is encoded in utf-8, add the line
?xml version=1.0 encoding=utf-8?
at the top of it, if it's not already present.
The parser will then decode everything into unicode strings - all TextNodes,
attributes etc. should be unicode strings.

When writing the manipulated DOM back to disk, use toxml() which has an
encoding argument.

 Then convert a string grabbed from the net so parts of it can be
 inserted in that dom object into new nodes or existing nodes.
 And finally write that dom object back to a file in a way it can be used
 again later with the same script..

Just insert the unicode strings.

 I've been trying to do that for a few days with no luck..
 I can do each separate part of the job, not that i'm quite sure how i
 decode/encode stuff in there, but as soon as i try to do everything at
 the same time i get encoding errors thrown all the time..
 
 3. in order to help me understand what's going on when doing
 encodes/decodes could you please tell me if in the following example, s
 and backToBytes are actually the same thing ??
 
 s = hello normal string
 u = unicode( s, utf-8 )
 backToBytes = u.encode( utf-8 )
 
 i knwo they both are bytestrings but i doubt they have actually the same
 content..

Why not try it yourself?
hello normal string is just US-ASCII. The utf-8 encoded version of the
unicode string uhello normal string will be identical to the ASCII byte
string hello normal string.


 
 4. I've also tried to set the default encoding of python for my script
 using the sys.setdefaultencoding('utf-8') but it keeps telling me that
 this module does not have that method.. i'm left no choice but to edit
 the site.py file manually to change ascii to utf-8, but i won't be
 able to do that on the client computers so..
 Anyways i don't know if it would help my script at all..

There was just recently a discussing on setdefaultencoding() on various
pythonistic blogs, e.g.
http://blog.ianbicking.org/python-unicode-doesnt-really-suck.html


 
 any help will be greatly appreciated
 thx
 
 Marc

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Making a timebomb

2005-08-05 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 I have a server that right now runs infinitely.  I'd like to make it
 die after some amount of time.  I was thinking of having a timebomb
 thread that starts when the server starts.  The timebomb sits, and
 sleeps for the specified timeout period (e.g., 5 hours), then does
 something to make the main thread terminate.  But I'm too inexperienced
 to figure out what that thing is.
 
 Any suggestions?
 
 
 
 class TimeBomb( threading.Thread ):
def run(self):
timeout = 5 * 60 * 60 #-- 3 hours
time.sleep( timeout )
MakeTheRestOfTheStuffDie()
 
 class MyServer:
def __init__(self):
TimeBomb().run()
serve()

Unfortunately you can raise an exception in another thread. You could tell
tell main thread to terminate by setting a flag that is polled by the main
thread.
You could also try to send a signal to yourself, but I'm not sure what will
happen then...


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reliable destruction

2005-08-04 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 Hello,
 
 I have a class measurement representing a physical measurement.
 Different objects in this class represent laboratory equipment, which
 might raise an exception (e.g. overtemperature).
 
 In any case the equipment has to be switched off after the experiment,
 since if a
 power supply stays in the on state for a prolonged time equipment may
 be
 destroyed. Switching off is done by invoking the destructors of the
 instruments.
 
 My measurement looks like this:
 
 class measurement:
 def __init__(self):
 self.setup()
 self.run()
 
 def setup(self):
 self.powerSupply=apparate.PowerSupply()
 self.magnet=apparate.magnet() # Exception(Communication Error)
 self.thermo=apparate.thermometer()
 # some 5 more instruments
 
 def run():
 for i in range(100)
 self.powerSupply.setCurrent(i) # Exception(Overcurrent)
 self.magnet.setField(0.5*i)
 
 
 Different measurements are executed in a script which might run
 overnight:
 If one measurement raises an exception the next one might still work
 and I don't
 want to loose the results from the following experiments.
 
 try:
 measurement()
 except:
 pass
 try:
 measurement2()
 except:
 pass
 
 
 An exception might be thrown anywhere in init or run if e.g. the
 PowerSupply
 overheats. Maybe an asynchronous event might happen, too (user
 interrupt with ^C but I might live without that if it is impossible to
 handle)
 
 My questions are:
 1) under normal conditions (no exceptions) is there a guarantee, that
 __del__ of
 all instruments is called at the end of measurement()?
 
 2) if an exception is thrown, will all instruments be deleted if the
 error
 occurs in run() ?
 (only the instruments already initialized if the error occurs
 in setup() )?
 
 I am using CPython (on WinXP) and there are no reference cycles between
 the instruments.
 
 I have to stress again that a reliable finalization is important and
 cannot wait
 until the interpreter shuts down.
 
 I have tried this and it seems to work but this is of course no
 guarantee.

I would suggest an explicit tearDown() method

try:
m = measurement()
try:
m.setup()
m.run()
finally:
m.tearDown()
except KeyboardInterrupt:
# user pressed ctrl-C
print ***BREAK
sys.exit(1)
except:
# you should at least log the exception for later debugging
traceback.print_exc()

and remove the calls to setup() and run() from the constructor.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reliable destruction

2005-08-04 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 Hello Benjamin,
 
 What would happen if an exception was thrown in the middle of setup()?
 tearDown could not handle this case without having a list of the
 objects already constructed (Or I would have to rely on the automatic
 call to __del__, if it is reliable).

class measurement:
def __init__(self):
self.powerSupply = None
...

def setup(self):
self.powerSupply=apparate.PowerSupply()
...

def tearDown(self):
if self.powerSupply is not None:
try:
self.powerSupply.tearDown()
except:
# Exception in powerSupply.tearDown() should not stop
# the following tearDown()s from being executed
traceback.print_exc()

...


 There is still some problem:
 Imagine a communication error in run() which would cause del to fail on
 the instrument.

Not really sure, if I understand what you mean? Does my tearDown() above
covers this?

 Anyway, I think this case is still more difficult to handle.

Reliable, fail-safe software *is* hard to design and implement, that's for
sure..
Be happy that it's just a power supply that could overheat and not the core
of a nuclear power plant.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Art of Unit Testing

2005-08-03 Thread Benjamin Niemann
Christoph Zwerschke wrote:

 Benjamin Niemann wrote:
 Some (many?) people don't like the unittest module, because it is not
 very pythonic - nothing to wonder as it has its root in the Java world.
 That's probably one of the reasons why there are other (more pythonic)
 unittesting frameworks for Python out there.
 
 So I think it would have been better that unittest had been named
 PUnit to make clear that it is a JUnit port and to allow a more
 pythonic testing framework to be added to the Python's standard lib.

It was called PyUnit before it was integrated into the stdlib. Dunno why it
was renamed...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Art of Unit Testing

2005-08-03 Thread Benjamin Niemann
Michael Hoffman wrote:

 Benjamin Niemann wrote:
 Christoph Zwerschke wrote:
 
 
Benjamin Niemann wrote:

Some (many?) people don't like the unittest module, because it is not
very pythonic - nothing to wonder as it has its root in the Java world.
That's probably one of the reasons why there are other (more pythonic)
unittesting frameworks for Python out there.

So I think it would have been better that unittest had been named
PUnit to make clear that it is a JUnit port and to allow a more
pythonic testing framework to be added to the Python's standard lib.
 
 
 It was called PyUnit before it was integrated into the stdlib. Dunno why
 it was renamed...
 
 unittest describes exactly what it does.
 
 pyunit says that it is in Python (duh), and that it has something to do
 with units, which could be a whole number of things.

XUnit (with X being the preferred prefix for the programming language) is a
common and wellknown name for a certain kind of unittesting framework. Of
course there are some people around who know what unittesting is but never
heard of JUnit and it decendents. But a quick textsearch on the TOC of the
library reference would reveal it.
Anyway, it too late now.

 I'm thankful that logging is called logging as well, rather than log4py.

If someone is heavily using log4j and thinks about moving to Python, she
will be happy to see that her preferred logging API is available for Python
without even leaving the TOC of the library reference. log4X is a similar
case as XUnit.
(Is logging an implementation of the log4X API? Never used log4X...)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trying to parse non valid html documents with HTMLParser

2005-08-03 Thread Benjamin Niemann
Steve M wrote:

You were right, the HTMLParser of htmllib is more permissive. He just
 ignores the bad tags !
 
 The HTMLParser on my distribution is a she. But then again, I am using
 ActivePython on Windows...

Although building parsers is for some strange reason one of my favourite
programming adventures, I do not have such a personal relationship with my
classes ;)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: trying to parse non valid html documents with HTMLParser

2005-08-02 Thread Benjamin Niemann
florent wrote:

 I'm trying to parse html documents from the web, using the HTMLParser
 class of the HTMLParser module (python 2.3), but some web documents are
 not fully valids.

Some?? Most of them :(

 When the parser finds an invalid tag, he raises an 
 exception. Then it seems impossible to resume the parsing just after
 where the exception was raised. I'd like to continue parsing an html
 document even if an invalid tag was found. Is it possible to do this ?

AFAIK not with HTMLParser or htmllib. You might try (if you haven't done
yet) htmllib and see, which parser is more forgiving.

You might pipe the document through an external tool like HTML Tidy
http://www.w3.org/People/Raggett/tidy/ before you feed it into
HTMLParser.


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Art of Unit Testing

2005-08-02 Thread Benjamin Niemann
Christoph Zwerschke wrote:

 Thanks for the link, Grig. I wasn't aware of the py lib so far. The
 possibility to create fixtures at the three different scopes is exactly
 what I was looking for.
 
 Anyway, I think it would be nice to have that feature in the standard
 lib unittest as well. It should not be too hard to add setUpOnce and
 tearDownOnce methods in addition to setUp and tearDown. Actually, I am
 wondering that there doesn't seem to be any development progress since
 unittest was included in the standard lib of Python 2.1 in August 2001.
 I had expected that such an important module would be continually
 improved and maintained. How come? So few people using unit tests? Or do
 most people write their own testing code or use py.test?

Or because it is already close to perfection (at least in what it is
intended to do).

The unittest module is a 'port' of the JUnit framework for Java which has a
certain wellknown API and semantics. The same API is available for many
other languages, so it is probably a good idea to stick with it in order to
make people coming from other language feel more comfortable with Python.

Some (many?) people don't like the unittest module, because it is not very
pythonic - nothing to wonder as it has its root in the Java world. That's
probably one of the reasons why there are other (more pythonic) unittesting
frameworks for Python out there.

I prefer to use unittest (because this was the API the textbook was using
that introduced me to this topic) and I also had the problem of heavy setup
costs. I deal with it by using a script around my testsuite (instead of
calling just unittest.main()) that does the setup/teardown of the
environment.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python SMTP server

2005-08-01 Thread Benjamin Niemann
Cliff Wells wrote:

 On Sun, 2005-07-31 at 13:14 +0200, Benjamin Niemann wrote:
 
 But you should be aware of the fact that (if you send mail from a dialup
 machine without going through a relay server) your mails will quickly be
 marked as spam - I hope you do not intend to send spam...
 
 Yah, Postfix on my servers uses several dnsbl's which automatically
 reject home users (unless they authenticate first).  Even if this isn't
 the case for the majority of SMTP servers today I expect it won't be
 long before it is.
 
 As an aside, I will say that many SMTP servers that service home users
 (i.e. Comcast, et al) limit the amount of mail that you can send within
 a defined period.

Or completely block outgoing traffic on port 25 except to their own relay...

 By using a local SMTP server to proxy, your app can 
 queue up a large amount of mail in a much shorter period.  It won't
 necessarily go out any faster, but at least your app won't be tied up
 waiting for the mail to be accepted.  So there is perhaps one useful
 (beyond learning and fun) application for using a local SMTP server.

It would be interesting what the intention of the OP is. I just stumpled
upon a similar problem. The prog I'm currently working on has a function to
report crashes back to me. Originally these reports where sent by mail - no
problem on UNIX/Linux hosts where you can assume to have a working MDA on
localhost. But what to do on Windows systems?!? Ask for a SMTP server
during installation? Confusing as the program itself is totally unrelated
to email. In this case you _could_ deliver the mail directly to my MX
host... But instead of this I installed a small CGI on my website that
sends the mails to me and gets the data via HTTP POST from my app.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python SMTP server

2005-08-01 Thread Benjamin Niemann
Cliff Wells wrote:

 On Mon, 2005-08-01 at 12:28 +0200, Benjamin Niemann wrote:
 Cliff Wells wrote:
 
[snip] 

  By using a local SMTP server to proxy, your app can
  queue up a large amount of mail in a much shorter period.  It won't
  necessarily go out any faster, but at least your app won't be tied up
  waiting for the mail to be accepted.  So there is perhaps one useful
  (beyond learning and fun) application for using a local SMTP server.
 
 It would be interesting what the intention of the OP is. I just stumpled
 upon a similar problem. The prog I'm currently working on has a function
 to report crashes back to me. Originally these reports where sent by mail
 - no problem on UNIX/Linux hosts where you can assume to have a working
 MDA on localhost. But what to do on Windows systems?!? Ask for a SMTP
 server during installation? Confusing as the program itself is totally
 unrelated to email. In this case you _could_ deliver the mail directly to
 my MX host... But instead of this I installed a small CGI on my website
 that sends the mails to me and gets the data via HTTP POST from my app.
 
 You can also use port redirection to bypass this sort of thing.  Send on
 port 10025 instead and direct your MTA to listen on both ports.
This would still be problematic with company firewall that block everything
but a handful of ports. Port 80 is still one of the safest bets.


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python SMTP server

2005-07-31 Thread Benjamin Niemann
Fernando M. wrote:

 Hi,
 i made a test with smtplib module a few days ago, for sending mails,
 and i was wondering if there's another module for running an SMTP
 server, so i could make a standalone script for sending mails without
 using an external SMTP server.
 I've been searching but i'm not sure if there are modules for it, or
 anything.
 Which ones are my options?

An SMTP server is (simplified) a program that listens on port 25 for
incoming mails.

What you probably want to do is:
Do a DNS query for the recipent's domain and use smtplib to connect to the
server specified in the MX record. There was a module for DNS querie (not
in Python's stdlib), but I forgot how it was called or where you could find
it (try the Vaults of Parnassus or PyPI).

Or you may use an external tool like 'dig' ('dig mx DOMAIN').

But you should be aware of the fact that (if you send mail from a dialup
machine without going through a relay server) your mails will quickly be
marked as spam - I hope you do not intend to send spam...

-- 

Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting TypeError in Changing file permissions

2005-07-22 Thread Benjamin Niemann
Pranav Bagora wrote:

 Hello ,
 i am trying to change mode of files using the
 os.chmod()function. But i am getting an error
 
  os.chmod(outfile,0700)
 TypeError: coercing to Unicode: need string or buffer,
 file found

Looks as if your are using a file object (that you got from an open() call)
as the first parameter. What you need is a string with the path to the
file.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Difference between and '

2005-07-21 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 Hi,
 
 Can someone tell me the difference between single quote and double
 quote?
There is none. Except that in a double quoted string, single quotes don't
have to be escaped and vice versa, sometimes one of the two forms saves you
some backslashes:

That's my house == 'That\'s my house'
You say: \Hello\ == 'You say: Hello'


-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dictionary as property

2005-07-20 Thread Benjamin Niemann
Thanos Tsouanas wrote:

 Hello.
 
 (How) can I have a class property d, such that d['foo'] = 'bar' will run
 a certain function of the class with 'foo' and 'bar' as it's arguments?

I think you mean:

class A:
  def __init__(self):
self.d = {}

  def dict_change(self, key, value):
print key, value

a = A()
a.d['foo'] = 'bar'
-- foo bar

'a' only has a reference to 'd', it won't know, who has a copy of this
reference and what done to it.
What you could create, is a wrapper around 'd', that passes __getitem__,
__setitem__ and every other required method to the underlying dict and call
the appropriate hook method of A

class WrappedDict:
  def __init__(self, owner, d):
self.owner = owner
self.d = d

  def __setitem__(self, key, value):
self.owner.dict_changed(key, value)
self.d[key] = value

  def __getitem(self, key):
return self.d[key]

  

And in A.__init__
  self.d = WrappedDict(self, {})

You may also subclass WrappedDict from dict...

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Regular Expressions: re.sub(regex, replacement, subject)

2005-07-05 Thread Benjamin Niemann
Vibha Tripathi wrote:

 Hi Folks,
 
 I put a Regular Expression question on this list a
 couple days ago. I would like to rephrase my question
 as below:
 
 In the Python re.sub(regex, replacement, subject)
 method/function, I need the second argument
 'replacement' to be another regular expression ( not a
 string) . So when I find a 'certain kind of string' in
 the subject, I can replace it with 'another kind of
 string' ( not a predefined string ). Note that the
 'replacement' may depend on what exact string is found
 as a result of match with the first argument 'regex'.

Do mean 'backreferences'?

 re.sub(rthis(\d+)that, rthat\1this, this12that foo13bar)
'that12this foo13bar'

Note that the replacement string rthat\1this is not a regular expression,
it has completely different semantics as described in the docs. (Just
guessing: are you coming from perl? rxxx is not a regular expression in
Python, like /xxx/ in perl. It's is just an ordinary string where
backslashes are not interpreted by the parser, e.g. r\x == \\x. Using
r when working with the re module is not required but pretty useful,
because re has it's own rules for backslash handling).

For more details see the docs for re.sub():
http://docs.python.org/lib/node114.html

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Lisp development with macros faster than Python development?..

2005-07-05 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 I've been reading the beloved Paul Graham's Hackers and Painters.
 He claims he developed a web app at light speed using Lisp and lots
 of macros.
 
 It got me curious if Lisp
 is inherently faster to develop complex apps in.  It would seem if you
 could create your own language in Lisp using macros that that would be
 quite an advantage
 
 I realize that Python has operator overloading and OOP so I'm not sure.
 
 Any ideas?  Any *evidence* one way or another?
If that means that I have to learn a new programming language for every
program I'd like to apply a minor fix/customization to: not a good idea.
Probably not just for the casual hacker like me, but also for the
maintainance phase of a project, when the former lead gurus lost interest
and/or employment.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Existance of of variable

2005-07-04 Thread Benjamin Niemann
George Sakkis wrote:

 Hi Josiah,
 
 Hello. I am very new to Python, and have been unable to figure out how
 to check if a variable exists or not. In the following code I have made
 a kludge that works, but I think that it would be clearer to check if
 closest exists and not have to initialize it in the first place. How is
 that check done?

 The following code finds the closest place to a position and rejects
 places that are too far away.

 dist = 1e9
 closest = -1

 for n,p in galaxy.places.iteritems():
 dif = p.pos - pos
 len = dif.len()
 if len  dist and len  10.0/self.zoom:
 dist = len
 closest = p

 if closest != -1:
 self.sel = [closest.name]
 
 I would write it like this:
 
 def setClosest(self, pos, galaxy):
 minDist, closestPlace = min([((place.pos-pos).len(), place)
for place in galaxy.places.itervalues()])
 if minDist  10.0/self.zoom:
 self.sel = [closestPlace.name]
 else:
 raise RuntimeError(No place close enough)

A galaxy can be pretty big and contain a lot of places ;) You'll easily run
out of memory, if you try to build a list of (dist, place) tuples. Better
use the generator syntax (requires python 2.4):

 minDist, closestPlace = min(((place.pos-pos).len(), place)
for place in galaxy.places.itervalues())



-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: nested lists - utter newbie

2005-07-01 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 Hi,
 why is this possible -
b = [1,2,3]
b[2] = b
b
 [1,2,[...]]
b[2]
 [1,2,[...]]
b[2][2][2][2][2]
 [1,2,[...]]
 
 but this is not -
x = [1,2,x]
 Traceback (most recent call last):
   File stdin, line 1, in ?
 NameError: name 'x' is not defined

Because the right hand side ('[1,2,x]') is evaluated *before* the value is
bound to the name 'x' - and at this point there is obviously no name 'x'
defined.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Request for help on naming conventions

2005-06-13 Thread Benjamin Niemann
Steven D'Aprano wrote:

 Are there any useful naming conventions for modules, classes and
 functions?
 
 For instance, should I name functions as verbs and classes as nouns?
 
 eg
 class Transformer():
 pass
 
 def transform():
 do_stuff
 
 What about the module name? transformations.py or transform.py?
You probably want to read the PEP 8, Style Guide for Python Code:
http://www.python.org/peps/pep-0008.html


 What do people do with their own code? Do folks find that being
 consistent helps them remember what is what, or do you name objects
 whatever feels right at the time?
Naming convention are mostly a matter of personal taste (unless you are
working in a larger team, where there are some official conventions that
must be followed). So I would say the 'feels right' is the most important
factor.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Walking through a mysql db

2005-06-04 Thread Benjamin Niemann
Jeff Elkins wrote:

 I'm writing a small wxpython app to display and update a dataset. So far,
 I get the first record for display:
 
  try:
   cursor = conn.cursor ()
   cursor.execute (SELECT * FROM dataset)
   item = cursor.fetchone ()
 
 Now, how do I step through the dataset one row at a time?  My form has 
 'next' and 'back' buttons, and I'd like them to step forward or back,
 fetching the appropriate row in the table. I've tried setting
 cursor.rownumber by incrementing it prior to the fetchone() w/o effect.
You could either execute N-1 fetchone()s before you fetch the Nth dataset
(with N starting at 1)
or use the 'LIMIT' feature of MySQL:
cursor.execute (SELECT * FROM dataset LIMIT %s,1, n)
where n is the index of the requested dataset (starting at 0)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write html-headers (utf-8)

2005-05-30 Thread Benjamin Niemann
db wrote:

 Hello all,
 
 I hope this is the correct newsgroup for this question.
 
 Does anybody know how I can write a html-header with python(cgi)?
 The problem is, I have a few html templates in which I have a header e.g:
 
 !DOCTYPE  HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
 http://www.w3.org/TR/html4/strict.dtd; html
 head
 meta http-equiv=Content-Type content=text/html; charset=UTF-8
 
 In this template I write a few Mysql variables.
 Those variable often have german characters. This characters (Gösing in
 stead of Gösing). The german characters in the html template are shown
 correctly.
 
 If I change the character encoding with the browser to utf-8, all the
 characters are shown correctly. As you can see, I put in the header of the
 html template that the encoding is UTF-8, the browser still shows windows
 ISO-8859-15. Can I write the header with python so the browser uses the
 utf-8 encoding?
 
 My hosting providor uses fedora core 2, Python 2.2.3, MySQLdb. Mysql
 3.23.58
 
 I googled for hours, but I can't find the answer. I hope ypu can help me.
Does your browser send a Content-Type HTTP header (don't confuse this with
the HTML head part)? If it does and it specifies a charset, this will
override your meta http-equiv. Often iso-8859-1 is the default charset
for the Content-Type header.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Suggesting methods with similar names

2005-03-30 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 I have a class Surface with many methods. Working in the interactive
 window I receive an error like this when I write the wrong method name:
 
 table.addGlas()
 Traceback (most recent call last):
   File stdin, line 1, in ?
 AttributeError: 'Surface' object has no attribute 'addGlas'
 
 Is it possibile to make the object give a better answer: a short list
 of few method names similar to the one I've misspelled?
 
 I've found some modules with phonetic algorithms like soundex,
 metaphone, etc, for example here:
 http://sourceforge.net/projects/advas/
 
 I can produce the list of method names with this:
 toRemove = __delattr__ __dict__ __getattribute__ __module__ __new__
__reduce__ __copy__ __reduce_ex__ __setattr__ __slot__
__weakref__ __str__ __class__ __doc__.split()
 methods = sorted( set(dir(Surface)).difference(toRemove) )
 
 The problem is calling the phonetic algorithm to show a ranked list of
 the 2-4 method names most similar to the wrong one called. I don't know
 if this problem requires a change in the python shell, or in the
 metaclass of that Surface class, etc.
 And in the end maybe this functionality (inspired by a similar
 Mathematica one) is already inside IPython :-]
You could achieve this by overriding __getattribute__ (untested):

def __getattribute__(self, name):
  try:
object.__getattribute__(self, name) # or whatever is your superclass
# or use super(), but I would have to lookup the syntax and
# breakfast is waiting ;)
  except AttributeError:
# find similar attributes
suggestions = 
raise AttributeError('Surface' object has no attribute '%s'. Did you
mean %s? % (name, suggestions))

I leave it to the experts to wrap this into a generic metaclass, decorator
etc. ;)

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IronPython 0.7 released!

2005-03-24 Thread Benjamin Niemann
Ville Vainio wrote:

 Robin == Robin Becker [EMAIL PROTECTED]
 writes:
 
 Robin well that's nice, but I don't do blogs and certainly don't
 
 You don't need to do much - just go to planetpython.org
 
Or check out the Daily Python URL (http://www.pythonware.com/daily/) which
has a pretty high signal to noise ratio.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IronPython 0.7 released!

2005-03-23 Thread Benjamin Niemann
Robin Becker wrote:

 Do Re Mi chel La Si Do wrote:
 Hi !
 
 
 I am curious :
 
 1)I can't download the file (error in the GotDotNet system).
 2)   On http://ironpython.com, there are no new version
 
 
 
 I'm also a bit puzzled that www.ironpython.com has no mention of this
 release.
 Curious that J Hugunin didn't announce it himself.
Jim Hugunin announced it himself in a keynote at PyCon. You can read a lot
about it on Python centric blogs - just one example:
http://www.postneo.com/2005/03/23/keynote-ironpython

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string join() method

2005-03-23 Thread Benjamin Niemann
Derek Basch wrote:

 Can anyone tell me why this CGI code outputs a blank page?
 
 self.output = []
 self.setContentType(text/plain)
 ascii_temp.seek(0)
 self.output.extend(ascii_temp.read())
 print ''.join(self.output)
 
 def setContentType(self, type=text/xml):
 self.output.extend([Content-type: , type, \n\r])
 -
 
 but this code works?:
 -
 self.output = []
 self.setContentType(text/plain)
 print ''.join(self.output)
 ascii_temp.seek(0)
 print ascii_temp.read()
 
 def setContentType(self, type=text/xml):
 self.output.extend([Content-type: , type, \n\r])
 -
 [snip]

First thing: HTTP header lines must be terminated by \r\n not \n\r.
The headers are terminated by another \r\n. I'm not sure (but I would bet
an Euro or two that it does), but perhaps the webserver sanitizes the
output of CGI script and converts plain \n into \r\n - if not then you
shouldn't use print to output the headers, because it only outputs
non-HTTPish \n.

The output of your first script is

---
Content-type: text/plain\r\n
Allele...
---

whereas the second script outputs

---
Content-type: text/plain\r\n
\n
Allele...
---

The (required) \n - that should also be an \r\n - seperating the headers
from the content is added by the first print.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list