Re: How to use a timer in Python?

2005-09-22 Thread Wolfram Kraus
Nico Grubert wrote:
> Hi there,
> 
> on a Linux machine running Python 2.3.5. I want to create a file 
> 'newfile' in a directory '/tmp' only if there is no file 'transfer.lock' 
> in '/temp'.
> A cronjob creates a file 'transfer.lock' in '/temp' directory every 15 
> minutes while the cronjob is doing something. This job takes around 30 
> seconds. During these 30 seconds the 'transfer.lock' file is present in 
> the '/temp' directory and I must not create 'newfile'. After the cronjob 
> has been finished, the 'transfer.lock' file is deleted from '/temp' and 
> I can create 'newfile'.
> How can I use a timer that waits e.g. 10 seconds if 'transfer.lock' is 
> present and then checks again if 'transfer.lock' is still there?
> 
> I want to do something like this:
> 
> import os
> if 'transfer.lock' in os.listdir('/temp'):
>   # ...wait 10 seconds and then check again if
>   # 'transfer.lock' is in os.listdir('/temp')
> else:
>   # create 'newfile'
> 
> 
> Nico

import time
time.sleep(10)

For more information: help(time.sleep) ;-)

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


Re: How to creat a file?

2005-12-02 Thread Wolfram Kraus
sandorf wrote:
> I'm new to python. Have a simple question.
> 
> "open" function can only open an existing file and raise a IOerror when
> the given file does not exist. How can I creat a new file then?
> 
open the new file in write mode: open('foo', 'w')
See: help(open)

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


Re: SuSE 9.1: updating to python-2.4

2005-01-10 Thread Wolfram Kraus
Heyho!
Torsten Mohr wrote:
Hi,
along with my distribution SuSE 9.1 came python 2.3.3.
I'd like to update to 2.4 now, is this an easy thing to do or will
lots of installed modules refuse to work then?
I installed Python 2.4 under SuSE 9.1 and had no problems so far. If you
install it via "./configure;make;make install", python 2.4 will be
installed in /usr/local/lib/python2.4 parallel to your existing 2.3
installation under /usr/lib/python2.3.
Is there an easy way to find out what i need to update?
Just check out, which version comes up when you call "python" from the 
shell. If this is version 2.3 you can start 2.4 with "python2.4"
All the installed packages for python2.3 (e.g. PIL, MySQLdb, wxPython,
...) need to be installed for the new version, too.
Thanks for any hints, Torsten.
HTH,
Wolfram
--
http://mail.python.org/mailman/listinfo/python-list


Associate objects with Tix.Tree

2005-01-15 Thread Wolfram Kraus
Heyho!
I want to associate objects with the nodes of a Tix.Tree. Is this 
possible, because the following example won't work:

--8<-- Cut here -
import Tix
class C:
pass
root = Tix.Tk()
top = Tix.Frame(root, relief=Tix.RAISED, bd=1)
tixtree = Tix.Tree(top)
tixtree.pack(expand=1, fill=Tix.BOTH, padx=10, pady=10, side=Tix.LEFT)
c=C()
print c
tixtree.hlist.add(c,
  itemtype=Tix.IMAGETEXT,
  text='foo',
  image=tixtree.tk.call('tix', 'getimage', 'folder'))
tixtree.setmode(c, 'open')
root.mainloop()
--8<-- Cut here -
Can this somehow be done?
TIA,
Wolfram
--
http://mail.python.org/mailman/listinfo/python-list


Re: how to pass attribute name via sys.argv

2005-01-27 Thread Wolfram Kraus
Felix Hebeler wrote:
Hi all, I am doing some Python scripting for a while, but I'm not too
deep into it yet. So I have a problem I can't solve.
I need to call an object attribute:
value = object.attrName[0]
the problem is, that the attribute name can only be specified at
runtime.
So what I have is something like
attrName = sys.argv[1] attrName
'cellsize'
and I need to pass it on so I can call
value = object.cellsize[0]
Use getattr:
value = getattr(object, attrName)[0]
Can this be done using Python?
Thanks for any hints
Cheers Felix
HTH,
Wolfram
--
http://mail.python.org/mailman/listinfo/python-list


Re: python-2.4.msi installation issue

2005-02-03 Thread Wolfram Kraus
[EMAIL PROTECTED] wrote:
O/S: Windows XP Home (with Service Pack 2)
Downloaded python-2.4.msi from python.org (10,632KB).  When I double
click on the file from Windows Explorer, the installation process
presents the window in which I am prompted to either install for all
users (the default) or install for just me.  I selected the defaulted
value.  Installation process presents a window in which the user may
select the directory for python 2.4 files.  I selected the default
value of the Python24 folder.  After clicking Next, the process
presents a window with the message, "Python 2.4 installer ended
prematurely.  Python 2.4 ended prematurely because of an error.  Your
system has not been modified.  To install this program at a later time,
please run the installation again."
My question is this: how do I determine what the error was that caused
the installation process to end prematurely?
I have the same problem and don't know what's happening, but 
ActivePython worked for me. Get it here:
http://activestate.com/Products/ActivePython/

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


Re: catch argc-argv

2005-06-19 Thread Wolfram Kraus
mg wrote:
> Hello,
> 
> I am writting bindings for a FEM application. In  one of my function 
> 'initModulename', called when the module is imported, I would like to 
> get the argc and argv arguments used in the main function of Python.
> So, my question is: does the Python API containe fonctions like 
> 'get_argc()' and 'get_argv()' ?
> 
> Thanks,
> 
> 
> 
Use sys.argv:
http://python.org/doc/2.4.1/lib/module-sys.html

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


Re: need help with MySQLdb

2005-06-29 Thread Wolfram Kraus
[EMAIL PROTECTED] wrote:
> Hey there all,
> i have a question about how to point my python install to my sql
> database.
> 
> when i enter this: db = MySQLdb.connect(user="user", passwd="pass",
> db="myDB")
> 
> i get this:
> Traceback (most recent call last):
>   File "", line 1, in -toplevel-
> db = MySQLdb.connect(user="user", passwd="pass", db="MyDB")
>   File "/usr/lib/python2.4/site-packages/MySQLdb/__init__.py", line 66,
> in Connect
> return Connection(*args, **kwargs)
>   File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line
> 134, in __init__
> super(Connection, self).__init__(*args, **kwargs2)
> OperationalError: (1049, "Unknown database 'MyDB'")
> 
> i am using the all in one package from lampp (now xampp) and i have
> tested a couple of python scripts from the cgi, but nothing that
> connects to the database.
> 
> any ideas?
> 
> thanks
> 
Try the following from the shell (NOT the python shell):
mysql -u user -p
[Enter passwd]
mysql> show databases;

If MyDB isn't in the list either something went wrong with the xampp 
installation or the database for xampp got a different name. (I am no 
xampp expert, so I can't help you any further)

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


Re: need help with MySQLdb

2005-06-30 Thread Wolfram Kraus
nephish wrote:
[...]

>> Try the following from the shell (NOT the python shell):
>> mysql -u user -p
>> [Enter passwd]
>> mysql> show databases;
>>
>> If MyDB isn't in the list either something went wrong with the xampp 
>> installation or the database for xampp got a different name. (I am no 
>> xampp expert, so I can't help you any further)
>>
>> HTH,
>> Wolfram
>>
>>
> after i entered the password it told me it cannot connect to mysql through
> socket /tmp/mysql.sock
> 
> h.
> hope this helps

Please keep the discussion on the list

Try
mysql -u user -p -h 127.0.0.1

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


Re: Replacing last comma in 'C1, C2, C3' with 'and' so that it reads 'C1, C2 and C3'

2005-07-11 Thread Wolfram Kraus
Ric Da Force wrote:
> Hi,
> 
> I have a string such as 'C1, C2, C3'.   Without assuming that each bit of 
> text is of fixed size, what is the easiest way to change this list so that 
> it reads:
> 'C1, C2 and C3' regardless of the length of the string.
> 
> Regards and sorry for the newbie question,
> 
> Ric 
> 
> 
Use rfind and slicing:

 >>> x = "C1, C2, C3"
 >>> x[:x.rfind(',')]+' and'+x[x.rfind(',')+1:]
'C1, C2 and C3'

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


Re: Retrieving and saving images from internet address

2005-07-25 Thread Wolfram Kraus
rock69 wrote:
> Hi all :)
> 
> I got this address:
> 
> http://www.infomedia.it/immagini/riviste/covers/cp/cp137.jpg
> 
> and I would like to download that image and save it to a local file.
> How do you do that in Python?
> 
Use urllib2:

http://docs.python.org/lib/module-urllib2.html
http://docs.python.org/lib/urllib2-examples.html

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


Re: display VARCHAR(mysql) and special chars in html

2005-02-23 Thread Wolfram Kraus
Jonas Meurer wrote:
hello,
my script selects a comment saved as VARCHAR in MySQL and displays it
inside an html page.
the problem is, that the comment contains several special characters, as
mysterious utf-8 hyphens, german umlauts, etc.
i could write a function to parse the comment and substitute special
chars with the relevant html code, but maybe this already exists in some
module?
if not, it'll be hard work, as i've to consider many special chars, and
at least iso-8859-1* and utf-8 as charmaps.
bye
 jonas
If I understand you correctly, just put

somewhere in the -section of you HTML-Page.
HTH,
Wolfram
--
http://mail.python.org/mailman/listinfo/python-list


Re: Writing to stdout and a log file

2005-04-20 Thread Wolfram Kraus
Mike wrote:
I would like my 'print' statements to send its output to the user's
screen and a log file.
This is my initial attempt:
class StdoutLog(file):
def __init__(self, stdout, name='/tmp/stdout.log',
mode='w',bufsize=-1):
super(StdoutLog, self).__init__(name,mode,bufsize)
self.stdout = stdout
def write(self, data):
self.stdout.write(data)
What happens when you do a self.stdout.flush() here?
self.write(data)
import sys
sys.stdout = StdoutLog(sys.stdout)
print 'STDOUT', sys.stdout
When the program is run the string is written to the log file but
nothing appears on my screen. Where's the screen output?
It looks like the superclass's write() method is getting called instead
of the StdoutLog instance's write() method.
The python documentation says 'print' should write to
sys.stdout.write() but that doesn't seem to be happening.
Any idea what's going one? 
Or ideas on how to debug this?

Thanks, Mike
I had the same problem (writing to file and stdout with print) and my 
solution was *not* to subclass file and instead add a 
self.outfile=file(...) to the constructor.

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


Re: Sorted list - how to change it

2006-11-09 Thread Wolfram Kraus
On 09.11.2006 10:52, Lad wrote:
> I have a sorted list for example [1,2,3,4,5] and I would like to change
> it in a random way
> e.g [2,5,3,1,4] or [3,4,1,5,2]  or in any other way except being
> ordered.
> What is the best/easiest
>  way how to do it?
> 
> Thank you for help
> L.
> 
use random.shuffel:

>>> import random
>>> x = [1,2,3,4,5]
>>> random.shuffle(x)
>>> x
[1, 4, 2, 3, 5]
>>> random.shuffle(x)
>>> x
[4, 2, 1, 3, 5]

see: http://docs.python.org/lib/module-random.html

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


Re: How to measure execution time of a program

2006-06-28 Thread Wolfram Kraus
On 28.06.2006 10:01, Girish Sahani wrote:
> Sorry for spamming again, but please also enlighten me with some way to
> time a function i.e. to find out how much time each function takes for
> execution in a big program.
>> Hi all,
>>
>>   Can anyone tell me the simplest way to do it (some code snippet that
>> could be included in the program's main function) ??
>>
>> Thanks,
>> girish
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
> 

Use the Python profiler:

http://docs.python.org/lib/profile.html

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


Re: looping question 4 NEWB

2006-07-06 Thread Wolfram Kraus
On 06.07.2006 12:43, manstey wrote:
> Hi,
> 
> I often have code like this:
> 
> data='asdfbasdf'
> find = (('a','f')('s','g'),('x','y'))
> for i in find:
>if i[0] in data:
>data = data.replace(i[0],i[1])
> 
> is there a faster way of implementing this? Also, does the if clause
> increase the speed?
> 
> Thanks,
> Matthew
> 

>>> import string
>>> data='asdfbasdf'
>>> data.translate(string.maketrans('asx', 'fgy'))
'fgdfbfgdf'

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


Re: MySQL

2006-02-16 Thread Wolfram Kraus
rodmc wrote:
> I need to write a Python application which connects to a MySQL 5.0
> database. Can anyone point me in the right direction or a compatible
> library?
> 
> Best,
> 
> rod
> 

See http://sourceforge.net/projects/mysql-python


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


Re: What's this error then?

2005-05-02 Thread Wolfram Kraus
Sara Khalatbari wrote:
> You remember me asking about running a program in my
> code...I got a problem here
> 
[...]

> & my code is:
> #! /usr/bin/env python
 ^
Remove this space, the line should be:
#!/usr/bin/env python


> 
> import os
> from os import execl
> 
> os.execl ('/home/sara/PYTHON/HEADER/msgfmt.py',
> 'file-roller.HEAD.fa.po')
> 
> 
> But when I run it, this Error raises:
> [EMAIL PROTECTED] HEADER]$ python run.py
> /usr/bin/env: python2.2: No such file or directory
> 
> Do you have any solutions?


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


Re: doc tags?

2005-05-13 Thread Wolfram Kraus
Wolfram Kriesing wrote:
> i was already searching and remember i had seen something like "its not 
> needed".
> 
> Anyway, are there any doc tags, like in Java/PHPDoc etc, where you can
> describe parameters (@param[eter]), return values (@ret[urn]),
> attributes (@var), references (@see), etc?
> 
> I guess I have just not found the link to the last flameware about it :-))
> 

I'm not sure if there is something like that builtin, but you can use 
epydoc: http://epydoc.sourceforge.net/

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


Re: Strings for a newbie

2005-05-27 Thread Wolfram Kraus
Malcolm Wooden wrote:
> I'm trying to get my head around Python but seem to be failing miserably. I 
> use RealBasic on a Mac and find it an absolute dream! But PythonUGH!
> 
> I want to put a sentence of words into an array, eg "This is a sentence of 
> words"
> 
> In RB it would be simple:
> 
> Dim s as string
> Dim a(-1) as string
> Dim i as integer
> 
> s = "This is a sentence of words"
> For i = 1 to CountFields(s," ")
>   a.append NthField(s," ",i)
> next
> 
> That's it an array a() containing the words of the sentence.
> 
> Now can I see how this is done in Python? - nope!
> 
> UGH!
> 
> Malcolm
> (a disillusioned Python newbie) 
> 
> 
Use split:

 >>> s = "This is a sentence of words"
 >>> s.split()
['This', 'is', 'a', 'sentence', 'of', 'words']

See:
http://www.python.org/doc/2.4.1/lib/string-methods.html#l2h-202

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


Re: Incrementing letters

2005-05-27 Thread Wolfram Kraus
Heiko Wundram wrote:
> Am Freitag, 27. Mai 2005 13:31 schrieb Michael:
> 
>>if(C == 'z') C='a';
>>else C++;
> 
> 
> if C == "z":
>   C = "a"
> else:
>   C = chr(ord(C)+1)
> 
According to the OP's problem (with the assumption that only characters 
  from a-z are given) he might even try a lil LC:
 >>> s = "shiftthis"
 >>> ''.join([chr(((ord(x)-ord('a')+1)%26)+ord('a')) for x in s])
'tijguuijt'


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


Re: Strings for a newbie

2005-05-27 Thread Wolfram Kraus
Malcolm Wooden wrote:
> my actual code is:
> 
>   for x in range(len(l)):
> h = string.split(l[x])
> 
> where the sentence string is in an array of one element 'l'
> Error is:
> 
> Traceback (most recent call last):
>   File "", line 34, in ?
>   File "", line 27, in SentenceText
>   File "C:\PYTHON22\lib\string.py", line 122, in split
> return s.split(sep, maxsplit)
> AttributeError: 'list' object has no attribute 'split'
> 

What is l[x]? Are you sure that l[x] is a string? Do a print before the 
split to see what l[x] is. Oh, and no need for range here, if l is a list:

for x in l:
  print x
  h = x.split()

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


Re: Incrementing letters

2005-05-27 Thread Wolfram Kraus
Duncan Booth wrote:
> Michael wrote:
> 
> 
>>Hi,
>>I've got a string s, and i want to shift all the letters up by one, eg
>>a->b, b->c  z->a
>>In c++ i can do this quite simply with
>>
>>if(C == 'z') C='a';
>>else C++;
>>
>>but i can't work out how to do this this in python??
> 
> 
import string
upone = string.maketrans(
> 
> 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
> 'bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA')
> 
string.translate("I've got a string s", upone)
> 
> "J'wf hpu b tusjoh t"
> 
> 
> Note the difference though: the Python code does what you said you wanted, 
> whereas your sample code corrupts punctuation.

Wow, that's quite nice. You really learn something new every day :-)
A minor improvement: Use string.ascii_letters as the first parameter for 
string.maketrans

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


Re: What's wrong with Zope 3 ?

2005-05-31 Thread Wolfram Kraus
Kay Schluehr wrote:
> The last downloadable release is from november 2004. The Windows
> installer is configured for Python 2.3(!). The Zope.org main page
> announces Zope 2.8 beta 2. Is it stillborn? 
> 
> Kay
> 
What you see is not Zope 3, it is Zope X 3. To quote from the X3 
information page: "Zope X3 3.0 is for developers. If you are expecting 
an end-user application, this is not for you."
The current stable brance is Zope2.X If you want to incorporate some 
functionalty from X3 in Zope 2.X, do a search for "Five"

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


Re: What's wrong with Zope 3 ?

2005-06-02 Thread Wolfram Kraus
Kay Schluehr wrote:
> 
> Wolfram Kraus wrote:
> 
>> Kay Schluehr wrote:
>> 
>>> The last downloadable release is from november 2004. The Windows 
>>> installer is configured for Python 2.3(!). The Zope.org main page
>>>  announces Zope 2.8 beta 2. Is it stillborn?
>>> 
>>> Kay
>>> 
>> 
>> What you see is not Zope 3, it is Zope X 3. To quote from the X3 
>> information page: "Zope X3 3.0 is for developers. If you are
>> expecting an end-user application, this is not for you."
> 
> 
> Yes I noticed this almost 8 months ago, read a bit of the
> documentation and articles published that time, regarded it as
> interesting and considered it for future development. But since then
> absolutely nothing happened. No project plan, no time-schedule, no
> subsequent releases.
You can always scan the zope3-mailinglist at zope.org (or via
news.gmane.org) to see whats happening. Migth be a better place for 
questions, too.

>> The current stable brance is Zope2.X If you want to incorporate
>> some functionalty from X3 in Zope 2.X, do a search for "Five"
> 
> 
> No, I do not want to migrate components from a new major release 
> backwards, as well as I do not want to migrate applications from
> WinXP to Win98. This seems to be the wrong development process
> direction.
Well, the problem is that it is not a "new major release" but a "new 
mayor experimental release". And X3 is not backward compatible, so a lot 
of Z2 products, e.g. Plone, don't work with X3.

> Regards, Kay
> 

HTH,
Wolfram (still using Z2 ;-))
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: substitution of list elements

2008-07-18 Thread Wolfram Kraus

Am 18.07.2008 14:33, antar2 schrieb:

I want to replace each first element in list 5 that is equal to the
first element of the list of lists4 by the fourth element. I wrote
following code that does not work:

list4 = [['1', 'a', 'b', 'c'], ['2', 'd', 't', 'e'], ['8', 'g', 'q',
'f']]
list5 = ['1', '2', '3']

for j in list4:
  for k in list5:

  for i,k in enumerate(list5):


if j[0] == k:
k = j[3]

  list5[i] = j[3]


print list5
Wanted result: ['c', 'e', '3']

thanks!


You didn't replace the list element itself.

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