schedule at specific hours

2006-08-11 Thread Yves Glodt
Hi there,

I have a daemon running 24/7, and I want that it executes a certain function 
several times a day, as specified in an configfile (e.g. 
actiontimes=10:00,12:00,19:00)

Do I have to fiddle with sched.scheduler and calc. time differences to 
schedule my events, or is there another (nicer...) way?


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


Activate a daemon several times a day

2006-07-06 Thread Yves Glodt
Hi,

I have a daemon which runs permanently, and I want it to do a special 
operation at some specifiy times every day, consider this configfile 
extract:

[general]
runat=10:00,12:00


What would be the easiest and most pythonic way to do this?
Something like this pseudocode:

while True:
if now(hours) in runat:
act()
sleep(60)
sleep(10)


Please enlighten me!

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


pyqt show wizard

2006-06-07 Thread Yves Glodt
Hi,

I have a mainwindow in my pyqt application, and on click of a button I 
want to start an assistant (wizard).

I have create the wizard with the Qt Designer, generated the python code 
with pyuic, imported it from assistant import *, and subclassed it as 
usual.

To show it, the onclick method of the button does:

w = Wizard()
w.show()

bot nothing happens...


How must I do to start the wizard...?


Best regards,
Yves

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


Re: encoding problem

2006-03-08 Thread Yves Glodt
Sebastjan Trepca wrote:
 I think you are trying to concatenate a unicode string with regular
 one so when it tries to convert the regular string to unicode with
 ASCII(default one) encoding it fails. First find out which of these
 strings is regular and how it was encoded, then you can decode it like
 this(if regular string is diff):
 
 mailbody +=diff.decode('correct encoding')

Thanks I'll look into that...

It seems in general I have trouble with special characters...
What is the python way to deal with éàè öäü etc...

print 'é' fails here,
print u'é' as well :-(

How am I supposed to print non-ascii characters the correct way?


best regards,
Yves

 Sebastjan
 
 On 3/3/06, Yves Glodt [EMAIL PROTECTED] wrote:
 Hi list,


 Playing with the great pysvn I get this problem:


 Traceback (most recent call last):
File D:\avn\mail.py, line 80, in ?
  mailbody += diff
 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position
 10710: ordinal not in range(128)



 It seems the pysvn.client.diff function returns bytes (as I read in
 the changelog of pysvn: http://svn.haxx.se/dev/archive-2005-10/0466.shtml)

 How can I convert this string so that I can contatenate it to my
 regular string?


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


encoding problem

2006-03-03 Thread Yves Glodt
Hi list,


Playing with the great pysvn I get this problem:


Traceback (most recent call last):
   File D:\avn\mail.py, line 80, in ?
 mailbody += diff
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 
10710: ordinal not in range(128)



It seems the pysvn.client.diff function returns bytes (as I read in 
the changelog of pysvn: http://svn.haxx.se/dev/archive-2005-10/0466.shtml)

How can I convert this string so that I can contatenate it to my 
regular string?


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


Re: Python threading, and processes

2006-02-08 Thread Yves Glodt
Robin Haswell wrote:
 Hey there
 
 I'm doing some threading in python with Python 2.3 and 2.4 on Ubuntu and
 Debian machines, and I've noticed that if I open a lot of threads (say,
 50), I get lots of python processes with individual PIDs, which consume a
 disproportionate amount of CPU. Does this mean that Python is using the
 dummy_thread module by accident? And is there a realistic limitation to
 the number of threads I can do?

Though I can not answer your question, I have however a similar 
situation here, on debian sarge.

I have a simple daemon that runs one thread, and I noticed that on our 
sarges with kernel 2.4 my daemon creates 4 processes, on the ones with 
kernel 2.6, only one process.

btw, I use the thread module.

best regards,
Yves

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


test whether 2 objects are equal

2006-01-31 Thread Yves Glodt
Hello,


I need to compare 2 instances of objects to see whether they are equal 
or not, but with the code down it does not work (it outputs not equal)


#!/usr/bin/python

class Test:
var1 = ''
var2 = ''

test1 = Test()
test1.var1 = 'a'
test1.var2 = 'b'

test2 = Test()
test2.var1 = 'a'
test2.var2 = 'b'

if test1 == test2:
print equal
else:
print not equal




What am I doing wrong...?


best regards,
Yves


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


Re: test whether 2 objects are equal

2006-01-31 Thread Yves Glodt
Rene Pijlman wrote:
 Yves Glodt:
 I need to compare 2 instances of objects to see whether they are equal 
 or not, 
 
 This prints equal:

thank you!

Have a nice day,
Yves

 class Test(object):
 def __init__(self):
 self.var1 = ''
   self.var2 = ''
 def __eq__(self,other):
   return self.var1 == other.var1 and self.var2 == other.var2
 
 test1 = Test()
 test1.var1 = 'a'
 test1.var2 = 'b'
 
 test2 = Test()
 test2.var1 = 'a'
 test2.var2 = 'b'
 
 if test1 == test2:
   print equal
 else:
   print not equal
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: test whether 2 objects are equal

2006-01-31 Thread Yves Glodt
bruno at modulix wrote:
 Yves Glodt wrote:
 Hello,


 I need to compare 2 instances of objects to see whether they are equal
 or not, but with the code down it does not work (it outputs not equal)


 #!/usr/bin/python

 class Test:
 var1 = ''
 var2 = ''
 
 Take care, this creates two *class* variables var1 and var2. For
 *instance* variables, you want:

Thanks for making me aware. I'll have to read more about classes in 
python... ( As you can see I'm still new to it ;-)

btw, this is the best list I've ever joined, very helpful and nice ppl.

Have a nice day!
Yves

 class Test:
   def __init__(self, var1='', var2=''):
 self.var1 = var1
 self.var2 = var2
 
 
 test1 = Test()
 test1.var1 = 'a'
 test1.var2 = 'b'
 
 This creates instances variables var1 and var2 for test1 (shadowing
 class variables).
 
 (snip the rest, see other posts in this thread)
 
-- 
http://mail.python.org/mailman/listinfo/python-list


append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Hi there,

I seem to be unable to find a way to appends more keys/values to the end 
of a dictionary... how can I do that?

E.g:

mydict = {'a':'1'}

I need to append 'b':'2' to it to have:

mydict = {'a':'1','b':'2'}



How to do?

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


Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Rene Pijlman wrote:
 Yves Glodt:
 I seem to be unable to find a way to appends more keys/values to the end 
 of a dictionary
 
 A dictionary has no order, and therefore no end.

that means I can neither have a dictionary with 2 identical keys but 
different values...?


I would need e.g. this:
(a list of ports and protocols, to be treated later in a loop)



ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 'tcp'}
#then:
for port,protocol in ports.iteritems():
print port,protocol
#do more stuff


What would be the appropriate pythonic way of doing this?



 mydict = {'a':'1'}

 I need to append 'b':'2' to it to have:

 mydict = {'a':'1','b':'2'}

 How to do?
 
 Like this:
 
 mydict = {'a':'1'}
 mydict['b'] = '2'
 print mydict
 {'a': '1', 'b': '2'}
 {'a': '1', 'b': '2'} == {'b': '2', 'a': '1'}
 True
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Yves Glodt wrote:
 Hi there,
 
 I seem to be unable to find a way to appends more keys/values to the end 
 of a dictionary... how can I do that?
 
 E.g:
 
 mydict = {'a':'1'}
 
 I need to append 'b':'2' to it to have:
 
 mydict = {'a':'1','b':'2'}
 
 
 
 How to do?

Sorry for the noise...

mydict['b'] = '2'

 Best regards,
 Yves


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


Re: append to the end of a dictionary

2006-01-24 Thread Yves Glodt
Paul Rubin wrote:
 Yves Glodt [EMAIL PROTECTED] writes:
 that means I can neither have a dictionary with 2 identical keys but
 different values...?
 
 No.
 
 I would need e.g. this:
 (a list of ports and protocols, to be treated later in a loop)

 ports = {'5631': 'udp', '5632': 'tcp', '3389': 'tcp', '5900': 'tcp'}
 #then:
 for port,protocol in ports.iteritems():
 print port,protocol
 #do more stuff

 What would be the appropriate pythonic way of doing this?
 
 ports = [('5631', 'udp'),
  ('5632': 'tcp'),
  ('3389': 'tcp'),
  ('5900': 'tcp')]
 
 for port,protocol in ports:
 print port, protocol  # ...
 
 You'd append with
 
ports.append(('2345', 'tcp'))
 
 note the double set of parentheses since you're appending a tuple.

Tim, Paul, I love you guys !

Thanks a lot
-- 
http://mail.python.org/mailman/listinfo/python-list


python-soappy

2006-01-11 Thread Yves Glodt
Hi list,

can anybody point me to a tutorial, howto or example code of 
python-soappy...? google did not have really useful results about...


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


heartbeats

2005-12-09 Thread Yves Glodt
Hi,

I need to write a heartbeat solution to monitor some external clients, 
and what is different as in the examples that I have seen so far is that 
I want my central server to poll the clients, and not the clients 
pinging the central server.

In detail I need a daemon on my central server which e.g. which in a 
loop pings (not really ping but you know what I mean) each 20 seconds 
one of the clients.

The only thing the client has to do is to accept the connection. 
(optionally sending back some bytes). If it refuses it is assumed to be 
offline.

My central server, and this is important, should have a short timeout. 
If one client does not respond because it's offline, after max. 10 
seconds the central server should continue with the next client.


Which python functions would be the most convenient for this application?

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


spawnle umask

2005-12-08 Thread Yves Glodt
Hi,

I tried something like this but the umask part does not work clearly...:

newpid = 
os.spawnle(os.P_NOWAIT,'/usr/bin/touch','/usr/bin/touch','xyz','umask 0113')

What would be the correct syntax for setting the umask for the created 
process...?


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


Re: spawnle umask

2005-12-08 Thread Yves Glodt
Fredrik Lundh wrote:
 Yves Glodt wrote:
 
 I tried something like this but the umask part does not work clearly...:

 newpid =
 os.spawnle(os.P_NOWAIT,'/usr/bin/touch','/usr/bin/touch','xyz','umask 0113')

 What would be the correct syntax for setting the umask for the created
 process...?
 
 not sure, but something like
 
 try:
 old_mask = os.umask(0113)
 newpid = os.spawnle(...)
 finally:
 os.umask(old_mask) # restore
 
 might work.

It does, I did like this:

os.umask(0113)
newpid = 
os.spawnl(os.P_NOWAIT,'/usr/local/bin/wine','/usr/local/bin/wine',executable)

But I wanted to use spawnle and it's env argument, to avoid setting 
umask manually...

regards,
Yves


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


Re: spawnle umask

2005-12-08 Thread Yves Glodt
David Wahler wrote:
 Yves Glodt wrote:
 It does, I did like this:

 os.umask(0113)
 newpid =
 os.spawnl(os.P_NOWAIT,'/usr/local/bin/wine','/usr/local/bin/wine',executable)

 But I wanted to use spawnle and it's env argument, to avoid setting
 umask manually...
 
 The umask is not part of the environment, so there's no way to set it
 directly through spawnle.

ok

  Why don't you want to use os.umask?

Only because I thought spawnle could set it through env...
But as it can't I will now go with os.umask.

thanks,
Yves

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


[Twisted-Python] ssh tunnel

2005-11-16 Thread Yves Glodt
(I asked about this several times in the twisted list but never got an 
answer, maybe here I'll more happy...)

Hi,

I'm new to conch and I wonder if somebody could point me to an example
of how to create an ssh tunnel with conch to forward a connection (e.g.
database or vnc) through that tunnel (if that's possible at all...)


Thanks in advance and best regards,
Yves

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


Re: Python, Linux, Desktop Environment

2005-11-16 Thread Yves Glodt

 [EMAIL PROTECTED] wrote:
   
 So, I've written my first GUI app in python.  I've turned it into a
 binary .exe and .app that runs on Windows and Mac respectively, but on
 my Linux box, where I wrote the thing, I still have to drop to the
 command line and ./myscript.py.  What can I do to make it a click and
 run sort of application in KDE or Gnome on Linux?
 
I don't really understand what you mean... Have you tried simply 
creating a shortcut
that points to your script.py?
That should make it run with a click...


HTH,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Clone an object

2005-11-14 Thread Yves Glodt
Hello,

how can I clone a class instance?
I have trouble finding that in the documentation...

thanks and best regards,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pregunta sobre python

2005-11-14 Thread Yves Glodt
Andres de la Cuadra wrote:
 Hola, me llamo Andres de la cuadra, soy un usuario de python en chile y me
 gustaría saber como puedo cerrer un programa a través de python. Yo se que
 con la librería os puedo ejecutar programas, pero no e encontrado una
 librería para poder cerrarlos

Hola Andres,

puedes cerrer un programa con os.kill, pero depende de tu plataforma, 
por ejemplo en linux (no se para windows):

os.kill(pid_del_proceso, 9)


p.s.
Vas a tener mas excito si escribes en ingles, esta es une lista en 
ingles ;-)


 Gracias 
 

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


how to start a process and get it's pid?

2005-11-11 Thread Yves Glodt
Hello,

another question rose for me today...

Is there a way to start an external process, in it's own context (not as 
the exec-() functions do), and get it's pid...?

e.g.:

pid = wonderfulstartprocfunction('/usr/bin/wine bla.exe')
#... later

if (...):
os.kill(pid,9)


best regards,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: how to start a process and get it's pid?

2005-11-11 Thread Yves Glodt
Gerhard Häring wrote:
 Yves Glodt wrote:
 Hello,

 another question rose for me today...

 Is there a way to start an external process, in it's own context (not as 
 the exec-() functions do), and get it's pid...? [...]
 
 Check out the subprocess module if you're using Python 2.4.
 
 Otherwise, you can always use os.spawn*, for example:
 
   os.spawnl(os.P_NOWAIT, c:/windows/notepad.exe)
 1944

Thanks, in Linux it seems to work.

 HTH,
 
 -- Gerhard
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-10 Thread Yves Glodt
bruno at modulix wrote:
 Yves Glodt wrote:
 (snip)
 ok I see your point, and python's...

 (just FYI, and not to start a flamewar ;-):
 In php, the [] means append to an array object.
 
 yes, I know this.
 
 If the array does not exist yet, it's created.
 
 Which is what I don't like. It should crash.
 
 [] *is* explicit for
 arrays, 
 
 IIRC, you can also use it to sbscript strings, but I wouldn't bet my
 life on this.
 
 thus for php it's clear what you want.)
 
 Nope. You may be in the case where you think the array already exists,
 and then you (well, I at least...) would prefer that PHP don't try to
 second-guess you... If and when I want to create an object, I tell it.
 If I dont tell create me an array, I don't want one to come in existence.

not adding fuel to language flamewar

 (snip)
 an undefined notice, yes, not a warning... ;-)

 (snip)
 Ok... I thank you for all the explanations.

 It helps me to see more far. I (and will continue to) use php for web,
 and wanna standardize on python for all non-web stuff we are doing, 
 
 You might discover that Python is just great for web programming too !-)

Which raises another question... :-)

Is there a possibility to bring together apache and python in a way that 
I can embed python into html?

Or even, write a smallish webserver in python (using twisted maybe) 
whose only purpose is to serve pages and execute the embedded code...?


 so I
 might be a frequent guest on this list...
 
 You're welcome !-)
 And if you're a french speaker, there's also french-speaking mailing
 list and newsgroups.

Merci pour l'info, I am, but for now the volume of this list is enough 
for me ... :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-10 Thread Yves Glodt
[EMAIL PROTECTED] wrote:
 Yves Glodt wrote:
 Which raises another question... :-)

 Is there a possibility to bring together apache and python in a way that
 I can embed python into html?
 What do you mean ?

I need this (invalid example-html follows):

html
h1title of page/h1

?py

import time

print pHello, today is: %s/p % (time.ctime())

?

/html


Should that not be fairly easy to to, even from scratch, with the 
httplib module...?

The advantage would be that I could write a webinterface for my database 
and reuse the classes I wrote for the command line app.

 Or even, write a smallish webserver in python (using twisted maybe)
 whose only purpose is to serve pages and execute the embedded code...?
 My favorite is TurboGears, a collection of modules and glue that brings
 together HTTP server(cherrypy), template(Kid) and SQL object
 store(SQLObject) that can do serious web developement yet don't need to
 read a book before starting.
 
 If you just need the server, cherrypy is pretty good and simple to
 start.

Ok gonna look into that,

regards,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


iterate over class variables

2005-11-10 Thread Yves Glodt
Hello list,

I need to iterate over a class and get all her variable names and 
values, e.g. considering this example:


class testclass:
var1 = 'ab'
var2 = 'cd'
var3 = 'ef'

test = testclass()



Then I wanna do sonmething like this:

for name,value in test:
print name
print value

fails with of course with:
TypeError: iteration over non-sequence


How can I do that?

regards,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterate over class variables

2005-11-10 Thread Yves Glodt
Yves Glodt wrote:
 Hello list,
 
 I need to iterate over a class and get all her variable names and 
 values, e.g. considering this example:
 
 
 class testclass:
   var1 = 'ab'
   var2 = 'cd'
   var3 = 'ef'
 
 test = testclass()
 
 
 
 Then I wanna do sonmething like this:
 
 for name,value in test:
   print name
   print value
 
 fails with of course with:
 TypeError: iteration over non-sequence
 
 
 How can I do that?

sorry for selfreplying, but I found a solution:

for key in dir(test):
if '__' not in key:
value = getattr(test,key)
print key, value

Does anything speak about this?
Is there a better-performing way to do this?


 regards,
 Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterate over class variables

2005-11-10 Thread Yves Glodt
Yves Glodt wrote:
 Yves Glodt wrote:
 Hello list,

 I need to iterate over a class and get all her variable names and 
 values, e.g. considering this example:


 class testclass:
  var1 = 'ab'
  var2 = 'cd'
  var3 = 'ef'

 test = testclass()



 Then I wanna do sonmething like this:

 for name,value in test:
  print name
  print value

 fails with of course with:
 TypeError: iteration over non-sequence


 How can I do that?
 
 sorry for selfreplying, but I found a solution:
 
 for key in dir(test):
   if '__' not in key:
   value = getattr(test,key)
   print key, value
 
 Does anything speak about this?

s/about/against

/me: s/more sleep/less party

 Is there a better-performing way to do this?
 
 
 regards,
 Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: iterate over class variables

2005-11-10 Thread Yves Glodt
bruno at modulix wrote:
 Yves Glodt wrote:
 Yves Glodt wrote:

 Hello list,

 I need to iterate over a class and get all her variable names and
 values, e.g. considering this example:


 class testclass:
 var1 = 'ab'
 var2 = 'cd'
 var3 = 'ef'
 
 Take care, these are *class* variables, not instance variables.
 
 test = testclass()

 Then I wanna do sonmething like this:

 for name,value in test:
 print name
 print value

 (snip)
 sorry for selfreplying, but I found a solution:

 for key in dir(test):
 if '__' not in key:
 value = getattr(test,key)
 print key, value

 Does anything speak about this?
 
 1/ dir() doesn't necessary returns all the attributes of an object:
 
 dir(...)
 dir([object]) - list of strings
 
 Return an alphabetized list of names comprising (some of) the attributes
 of the given object, and of attributes reachable from it:
 
 
 But I don't think this is a problem here.
 
 2/ everything being an object, dir() also returns methods (a method
 being a - callable - attribute of the object's class).
 
 If you're only interested in data attributes, you may want to try this:
 
 for key in dir(test):
 if not key.startswith('__'):
 value = getattr(test,key)
 if not callable(value):
 print key, value

This serves me well so far, thanks to you, Peter and Daniel for the 
suggestions!

Yves (still amazed of the responsiveness of this list :-)


 You can also check inspect.getmember()
-- 
http://mail.python.org/mailman/listinfo/python-list


append to non-existing list

2005-11-09 Thread Yves Glodt
Hello,

if I do this:

for row in sqlsth:
pkcolumns.append(row[0].strip())
etc


without a prior:

pkcolumns = [];


I get this error on first iteration:
UnboundLocalError: local variable 'pkcolums' referenced before assignment



I guess that's normal as it's the way python works...?!?

My question is: Is there no way to append to a non existing list?

I am lazy for declaring it first, IMHO it bloats the code, and (don't 
know if it's good to say that here) where I come from (php) I was used 
to not-needing it...


regards,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
[EMAIL PROTECTED] wrote:
 Yves My question is: Is there no way to append to a non existing list?
 
 My question in return is: How is Python supposed to know that pkcolumns is
 supposed to be a list instead of some other type of object that happens to
 define an append() method?

I am fairly new to python (and I like it more and more), but I can not 
answer this question... As I said, where I come from it is possible, and 
how they do it is explained a little here:
http://lu.php.net/manual/en/language.types.type-juggling.php

As I want to learn, I will work with python the pythonic way, but 
sometimes I just fall back to what I know from php... :-)



Thanks for your answer and the example code,
Yves

p.s.
thanks for the import this hint

 For example, my code might contain this class
 definition before the suspect pkcolumns.append() call:
 
 class MaxLengthList:
  def __init__(self, maxsize=0):
  self.data = []
  self.maxsize = maxsize
 
  def append(self, item):
  if self.maxsize and len(self.data) == self.maxsize:
  del self.data[0]
  self.data.append(item)
 
 def __getattr__(self, attr):
 return getattr(self.data, attr)
 
 I think it would be perfectly reasonable for Python to choose to instantiate
 my MaxLengthList instead of a plain old list.
 
 Yves I am lazy for declaring it first, IMHO it bloats the code, and
 Yves (don't know if it's good to say that here) where I come from (php)
 Yves I was used to not-needing it...
 
 I don't know php, but would also have to wonder how it knows to create a
 list instead of an integer (for example).
 
 Finally, from the Zen of Python (try import this at an interpreter
 prompt): 
 
 In the face of ambiguity, refuse the temptation to guess.
 
 Skip
 
 .
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
[EMAIL PROTECTED] wrote:
 I am afraid you have to either go back to php or whatever programming
 language that fits your style or change your style to fit python.

sorry for offending... I just asked a question, and now I know one more 
thing about python...

And btw I really am surprised by the amount of answers that my question 
rose, in so little time!

thanks all!

 There is a lot I don't like about python but if you have to use it, you
 have to cope with it.
 
 Yves Glodt wrote:
 My question is: Is there no way to append to a non existing list?

 I am lazy for declaring it first, IMHO it bloats the code, and (don't
 know if it's good to say that here) where I come from (php) I was used
 to not-needing it...


 regards,
 Yves
 

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


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
Juho Schultz wrote:
 Yves Glodt wrote:
 Hello,

 if I do this:

 for row in sqlsth:
 pkcolumns.append(row[0].strip())
 etc


 without a prior:

 pkcolumns = [];


 I get this error on first iteration:
 UnboundLocalError: local variable 'pkcolums' referenced before assignment

 I guess that's normal as it's the way python works...?!?

 My question is: Is there no way to append to a non existing list?

 I am lazy for declaring it first, IMHO it bloats the code, and (don't 
 know if it's good to say that here) where I come from (php) I was used 
 to not-needing it...


 regards,
 Yves
 
 You mean you want to type pkcolumns only once to keep your code short?
 Would something like this be useful?
 
 pkcolumns = [row.strip() for row in sqlsth]

I will look into this, maybe it's what I need, thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
bruno at modulix wrote:
 Yves Glodt wrote:
 Hello,

 if I do this:

 for row in sqlsth:
 pkcolumns.append(row[0].strip())
 etc


 without a prior:

 pkcolumns = [];


 I get this error on first iteration:
 UnboundLocalError: local variable 'pkcolums' referenced before assignment


 I guess that's normal as it's the way python works...?!?
 
 yes sir.
 
 My question is: Is there no way to append to a non existing list?
 
 No. Definitively. And that's a Good Thing(tm).
 
 How would you use something that doesn't exist ???
 
 I am lazy for declaring it first,
 
 s/declaring/instantiating/
 
 If you were to use your own class Toto, would you ever hope that the
 following code would work :
 
 for v in some_seq:
   toto.dothis(v)
 
 without instantiating Toto and binding it to the name 'toto' before ?
 Well, in Python, a list is an instance of class list. There are
 syntactic sugar to instanciate a list (or a tuple or a string or a dict
 etc), but this:
 
 my_list = []
 
 is strictly equivalent to this:
 
 my_list = list()
 
 Now let's try something else:
 
 class Machin(object):
   def append(self, value): pass
 
 class Bidule(object):
   def append(self, value): pass
 
 for i in range(10):
   m.append(i)
 
 
 How should Python interpret this ? Should it create a list, or a Machin,
  or a Bidule, or an instance of whatever imported class having a
 append() method ?

ok I see your point, and python's...

(just FYI, and not to start a flamewar ;-):
In php, the [] means append to an array object.

If the array does not exist yet, it's created. [] *is* explicit for 
arrays, thus for php it's clear what you want.)

 IMHO it bloats the code,
 
 run your python interpreter and type:
 import this
 
 Then read carefully.
 
 Now if being explicit still hurts your personal convictions, there's
 this other language with a name starting with p... !-)

no thanks, this is the 21st century ;-)

 and (don't
 know if it's good to say that here) where I come from (php) I was used
 to not-needing it...
 
 Not creating an Array before using it is Bad Style in PHP (and generate
 a Warning BTW).

an undefined notice, yes, not a warning... ;-)

 There are warts in Python (as in any other languages), and there are
 things that sometimes bore me but are not really warts. But having to
 explicitely instanciate objects can't be seen as a wart in any language
 IMHO !-)

Ok... I thank you for all the explanations.

It helps me to see more far. I (and will continue to) use php for web, 
and wanna standardize on python for all non-web stuff we are doing, so I 
might be a frequent guest on this list...

have a nice day,
Yves
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
[EMAIL PROTECTED] wrote:
 Thomas Bellman wrote:
 The next time you go shopping at your local super-market, do
 *not* get a shopping-cart (or shopping-basket, or any similar
 container).  As you pick up the things you want to buy, try
 to put them into the non-existing cart.  Perhaps you will then
 become enlightened.
 
 But in PHP(and I believe Perl), it is more like :
 
 oops, I need a cart. shoutHello, I need a cart here, please and
 magically, someone wheel you a cart which you can put your stuff in.
 The @ is the magic calls for service. So as a customer, this sounds
 like better service but that will create problems to the super market
 as it need extra staff for this service and the environment is noiser,
 that is another story.
 

:-)

I will never mention any p-language except python in this list anymore...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: append to non-existing list

2005-11-09 Thread Yves Glodt
Max M wrote:
 Yves Glodt wrote:
 bruno at modulix wrote:

 Yves Glodt wrote:

 Hello,

 if I do this:

 for row in sqlsth:
 pkcolumns.append(row[0].strip())
 etc


 without a prior:

 pkcolumns = [];


 I get this error on first iteration:
 UnboundLocalError: local variable 'pkcolums' referenced before 
 assignment


 I guess that's normal as it's the way python works...?!?
 
 
 Well you could do something like this. (Untested and unrecommended)
 
  self.__dict__.setdefault('pkcolumns', []).append(row[0].strip())
 
 Personally I find
 
  pkcolumns = []
  pkcolumns .append(row[0].strip())
 
 to be nicer ;-)

Yes me too, I'm gonna stick to that... :-)
-- 
http://mail.python.org/mailman/listinfo/python-list