Re: here document
harold fellermann wrote: f = open("/bin/exe.x","w") print >>f , """CategoryY = GRIB etc. """ This would overwrite the existing /bin/exe.x ... HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating text on images
morphex wrote: Hi all, I'm trying to create a script that will superimpose text on an image. I didn't find any great examples out there on how this can be done (I presume using PIL is necessary), do you know of any examples? Thanks, Morten Hi, something like this? ### from PIL import Image, ImageFont, ImageDraw import sys im = Image.open(YOUR_IMAGE_HERE) idraw = ImageDraw.Draw(im) idraw.text((1,1),"Hello", fill=128) im.save(YOUR_NEW_IMAGE_HERE, IMAGE_TYPE) ### HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Best python postgres module?
Hi, i recently migrated from mysql to postgresql and did use severel python postgres-modules. All do what they are designed for, so which one would you use? psycopg, pygresql, pypgsql? psycopg seems to be the best solution for heavy traffic/multiple connections i have no real testing environment, so any advice which one to use for different usecases would be nice. Thanks, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating .pyc/.pyo from a make file
Tim Daneliuk wrote: I use a makefile to create distribution tarballs of freestanding Python programs and their documentation. I cannot seem to find the right command line option to just generate a pyc/pyo file from the program and then exit. If I use 'python - -c"import myprog"' it creates the pyo file, but myprog starts up and keeps running. IOW, I need a batch method for generating compiled python. I know it exists, but I can't find it for some reason ... TIA, Hi, take a look at http://docs.python.org/lib/module-compileall.html HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating .pyc/.pyo from a make file
Tim Daneliuk wrote: It does - thanks. One more question: Are pyc and pyo file portable across operating systems? I suspect not since I generated a pyo on a FreeBSD machine that will not run on a Win32 machine. I was under the impression that "compiled" meant optimized byte code that was portable across implementations, but it looks to not be the case... Hi, .pyc's should be, cause it's standard python-bytecode, if you use massive optimizations it depends not on the os but on the underlying cpu/architecture ... So long, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Generating .pyc/.pyo from a make file
Roland Heiber wrote: Tim Daneliuk wrote: under the impression that "compiled" meant optimized byte code that You where right, i was totally mislead by "optimized" ... ;) Greetings, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: python nested class
Vedanta Barooah wrote: > o = mother() > o.show() > y=mother.child() > y.increase(20) > # this should print 20 > o.show() > > .. is it possible somehow ??? Hi, this should do what you want: --- test.py class mother: x=0 def __init__(self): mother.x=1 def show(self): print mother.x class child: def increase(self,num): mother.x=num o = mother() o.show() y=mother.child() y.increase(20) # this should print 20 o.show() --- >pythonw -u "test.py" 1 20 >Exit code: 0 HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Frankenstring
Thomas Lotze wrote: > It's definitely no help that file-like objects are iterable; I do want > to get a character, not a complete line, at a time. Hi, if i did understand what you mean, what about using mmap? Iterating over characters in a file like this: # -*- coding: iso-8859-1 -*- import os import mmap f = open("file.txt", "r+") size = os.path.getsize("file.txt") m = mmap.mmap(f.fileno(), size) for x in m: print x m.close() f.close() -- http://mail.python.org/mailman/listinfo/python-list
Re: Frankenstring
Thomas Lotze wrote: > AIUI (and as a little experimenting seems to confirm), you can't > reposition an iterator over an mmap'ed file by seeking. True, you have > both iterating by characters and seeking/telling, but the two > functionalities don't play together. A quick and dirty hack!? Maybe i'm missing what it is that you need ... class MmapWithSeekAndTell(object): def __init__(self, m, size): self._m = m self._pos = 0 self._size = size-1 def __iter__(self): return self def next(self): if self._pos < self._size-1: self._pos += 1 return self._m[self._pos] raise StopIteration def seek(self, offset, whence=0): if whence == 0 and 0 <= offset < self._size: self._pos = offset elif whence == 1 and 0 <= (self._pos + offset) < self._size: self._pos += offset elif whence == 2 and 0<= (self._size - offset) < self._size: self._pos = self._size - offset def tell(self): return self._pos HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Frankenstring
Roland Heiber wrote: > class MmapWithSeekAndTell(object): > def __init__(self, m, size): .. where m is a mmap-object and size the filesize ... sorry. -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call?
Francois De Serres wrote: > Hiho, > > Having a string: "dothat" > and a tuple: (x, y) > 1. What's the best way to build a function call like: dothat(x,y)? Not the best (not at all) but one way: def dothat(x,y): print "Called with:", x, y c = (1,2) locals().get("dothat")(*c) Called with: 1 2 HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: **kwargs?
Francois De Serres wrote: > All your **kwargs are belong to us. > > *args is documented in the Tutorial. I reckon **kwargs represents a > dictionary of arguments. But I don't quite get the semantics of **x. > Undefined length tuple of undefined length tuples? Are there other > practical use cases for ** (common enough please, I wish I was, but I'm > not a scientist). > > TIA, > Francois Assume d = { 'arg1':'value1','arg2':'value2' }. Then func(**d) is the same as: func(arg1='value1', arg2='value2') HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Building a function call?
Peter Hansen wrote: >> locals().get("dothat")(*c) This was just meant as a quick example, not as production-level code ;) Even with globals(), I think its a bit odd ..., but safer than using eval() ... HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: how to control a USB DISK?
Hi, when you're under linux and a 2.6er kernel, take a look at hotplug, you can configure it the way that your script gets executed whenever a specific device is plugged in/out ... HtH, Roland [EMAIL PROTECTED] wrote: when a new usb disk is plugged into the usb socket, the program will copy a file to the usb disk, then disabled the usb disk to be pulled out safely. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linux Multimedia System
Marek Franke wrote: too. The whole project is just for fun. Afaik Freevo and/or MythTV are written in C/C++ and don't have any support for joysticks (afaik!). And the Freevo is pure python already ;) Greetings, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: KeyError
[EMAIL PROTECTED] wrote: Hi "R", The only explanation I can give is that the environment varialbe REMOTE_ADDR does not exist! Wrap your high-level code with try and except. Example: try: tablesDirectory = tablesDirectoryPrefix + os.environ['REMOTE_ADDR'] except KeyError: # Code to handle the fact tht REMOT_ADDR does not exist. ... or just replace os.environ['REMOTE_ADDR'] with os.environ.get('REMOTE_ADDR', 'enter_default_here') to use a default in case of missing REMOTE_ADDR ... HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Python IDE
limodou wrote: http://wiki.wookpecker.org.cn/moin.cgi/NewEdit Try this instead: http://wiki.woodpecker.org.cn/moin.cgi/NewEdit ^ SCNR, Roland ;) -- http://mail.python.org/mailman/listinfo/python-list
Re: ANN: Snakelets 1.38 (simple-to-use web app server with dynamic pages)
Irmen de Jong wrote: I'm happy to say that Snakelets 1.38 is available. Fine thing again! Maybe someone is interested in this: I just tried tlslite and did a dirty hack so you could use snakelets via SSL. See the patch below. Snip and save it, use it against snakeserver/server.py. You've to place a key-pair in your server-dir (localhost.crt/localhost.private.key in the path). You can generate a self-signed certificate easily with openssl. HtH, Roland - SNIP AND SAVE BELOW - 961d 927a def handshake(self, tlsConnection): try: tlsConnection.handshakeServer(certChain=certChain, privateKey=privateKey, sessionCache=sessionCache) tlsConnection.ignoreAbruptClose = True return True except (SyntaxError, TLSError), error: print "Handshake failure:", str(error) return False . 908c if IS_SSL: tlsConnection = TLSConnection(request) if self.handshake(tlsConnection) == True: self.RequestHandlerClass(tlsConnection, client_address, self) tlsConnection.close() else: self.RequestHandlerClass(request, client_address, self) . 889c if IS_SSL: tlsConnection = TLSConnection(request) if self.handshake(tlsConnection) == True: self.RequestHandlerClass(tlsConnection, client_address, self) tlsConnection.close() else: self.RequestHandlerClass(request, client_address, self) . 21a IS_SSL=True try: from tlslite.api import * except ImportError: IS_SSL=False if IS_SSL: s = open("./localhost.crt").read() x509 = X509() x509.parse(s) certChain = X509CertChain([x509]) s = open("./localhost.private.key").read() privateKey = parsePEMKey(s, private=True) sessionCache = SessionCache() . -- http://mail.python.org/mailman/listinfo/python-list
Snakelets via SSL
Hi, after Irmen de Jong did another fine release with Snakelets-1.38 i just did a dirty hack for using snakelets with SSL-support. SSL-support is added through the use of tlslite from Trevor Perrin. You'll have to download and install it from http://trevp.net/tlslite/. Furthermore you need a key-pair in your serverdir, easily generetad with openssl. Just snip the patch below and use it against snakeserver/server.py (with standard *nix patch). HtH, Roland SNIP BELOW, SAVE AS ssl.patch 961d 927a def handshake(self, tlsConnection): try: tlsConnection.handshakeServer(certChain=certChain, privateKey=privateKey, sessionCache=sessionCache) tlsConnection.ignoreAbruptClose = True return True except (SyntaxError, TLSError), error: print "Handshake failure:", str(error) return False . 908c if IS_SSL: tlsConnection = TLSConnection(request) if self.handshake(tlsConnection) == True: self.RequestHandlerClass(tlsConnection, client_address, self) tlsConnection.close() else: self.RequestHandlerClass(request, client_address, self) . 889c if IS_SSL: tlsConnection = TLSConnection(request) if self.handshake(tlsConnection) == True: self.RequestHandlerClass(tlsConnection, client_address, self) tlsConnection.close() else: self.RequestHandlerClass(request, client_address, self) . 21a IS_SSL=True try: from tlslite.api import * except ImportError: IS_SSL=False if IS_SSL: s = open("./localhost.crt").read() x509 = X509() x509.parse(s) certChain = X509CertChain([x509]) s = open("./localhost.private.key").read() privateKey = parsePEMKey(s, private=True) sessionCache = SessionCache() . -- http://mail.python.org/mailman/listinfo/python-list
Re: Snakelets via SSL
Irmen de Jong wrote: However, may I ask you to re-submit the patch but this time in the patch tracker on SF; http://sourceforge.net/tracker/?group_id=41175 because news/mail clients often mangle source code. Hi, I re-submited it as requested. It's just a quick hack, but maybe it's worth trying. HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Python instances
Hi, class MyClass: list = [] you have "list" defined as a classmember, not an instancemember. So "list" ist defined ONCE for all instances. Try this instead: class MyClass: def __init__(self): self.list = [] [...] and use self.list ... HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Testing for the presence of input from stdin.
Will McDonald wrote: > Hi all. > > I'm writing a little script that operates on either stdin or a file > specified on the command line when run. I'm trying to handle the > situation where the script's run without any input gracefully but > can't think how to test for stdin. > Hi, maybe http://docs.python.org/lib/module-fileinput.html is useful for you: "This iterates over the lines of all files listed in sys.argv[1:], defaulting to sys.stdin if the list is empty. If a filename is '-', it is also replaced by sys.stdin. To specify an alternative list of filenames, pass it as the first argument to input(). A single file name is also allowed." HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: A __getattr__ for class methods?
Dylan Moreland wrote: > I have a metaclass generating basic properties such as .name and .city, > but I don't want to generate a class method for every permutation of > the attributes. I'd like to have something much like __getattr__ for > instance attributes, so that if a method like > Person.find_by_city_and_email cannot be found, I can construct a call > to the basic find method that hides the SQL. Is there any way of doing > this, or am I trying to mirror a functionality that Python simply does > not have? > I don't get it? __getattr__ is exactly what you are looking for: http://docs.python.org/ref/attribute-access.html#l2h-206 __getattr__( self, name) Called when an attribute lookup has not found the attribute in the usual places (i.e. it is not an instance attribute nor is it found in the class tree for self). name is the attribute name. This method should return the (computed) attribute value or raise an AttributeError exception. HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Serving binary data from a cgi-script
Thomas W wrote: > print d Hi, use sys.stdout.write instead, print is adding linebreaks ... HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list
Re: Introspection Class/Instance Name
*binarystar* wrote: > Hello there, > > what method would you use to return the name of the class and/or > instance introspectively eg. > > class Bollocks: > > def __init__( self ): > > print self.__method_that_returns_class_name__() > print self.__method_that_returns_instance_name__() > > > instance_of_bollocks= Bollocks() > > # Which outputs > > 'Bollocks' > 'instance_of_bollocks' > > > > I have been scouring the 2.4 docs ... I am sure it is frustratingly simple > > thx in advance > > ** Hi, take a look at self.__class__.__name__ for the Class-name. HtH, Roland -- http://mail.python.org/mailman/listinfo/python-list