Re: Regular expression question -- exclude substring
Ya, for some reason your non-greedy "?" doesn't seem to be taking.
This works:
re.sub('(.*)(00.*?01) target_mark', r'\2', your_string)
--
http://mail.python.org/mailman/listinfo/python-list
SocketServer and a Java applet listener
Dear newsgroup, I give up, I must be overseeing something terribly trivial, but I can't get a simple (Java) applet to react to incoming (python) SocketServer messages. Without boring you with the details of my code (on request available, though), here is what I do : I have a TCPServer and BaseRequestHandler . Connecting via telnet : everything goes OK. Connecting from Applet : problem 1 (worked around it) : java has some 'propietary' UTF-8 format, python's unicode doesn't seem to handle it correctly and I have to strip the first two bytes/chars , then all goes OK . problem 2: I have tried IMHO everything. In the BaseRequestHandler.handle() method, I want to update a list of clients in the server, i.e.: self.server.players[username] = self self := instance of the BaseRequestHandler, I only do this after succesfull connect , i.e. first time socket. I assume (wrongfully?) that I can now use the self.request socket for future transmissions to the client. In the applet, I start a thread that listens to the socket by eternally looping over: String line = self.din.readUTF() if (line == null) break; handle(line); self := instance of Thread self.din := DataInputStream(socket.getInputStream()); (It's a bit simplistic, but I am quite sure (well...) I got those things right, the issue seems to me to lie in some weird java-python-socket anomaly having to do with close()/flush() etc. ...) However, the handle(line) method only seems to get called when I destroy (close ?) the socket on the server side. I tried making it a wfile (socket.makefile) and calling the flush() method. Moreover, I searched and googled and couldn't find reference to a working implementation of a python SocketServer sending to a java Applet (socket listener). Would be much appreciated if anyone knows such a reference ? Any pointers to correct handling of the 'java propietary UTF-8 format' in python (xml.sax) would also be appreciated. Skipping the first two bytes really is a smelly workaround, I know, *deep sigh*... -- Thijs Cobben www.phaedro.com -- http://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and a Java applet listener
Steve Horsley schreef: > [EMAIL PROTECTED] wrote: > > Dear newsgroup, > > > > I give up, I must be overseeing something terribly trivial, but I can't > > get a simple (Java) applet to react to incoming (python) SocketServer > > messages. > > > > Without boring you with the details of my code (on request available, > > though), here is what I do : > > > > I have a TCPServer and BaseRequestHandler . > > Connecting via telnet : everything goes OK. > > > > Connecting from Applet : > > problem 1 (worked around it) : java has some 'propietary' UTF-8 format, > > python's unicode doesn't seem to handle it correctly and I have to > > strip the first two bytes/chars , then all goes OK . > > > > Those 2 bytes are important! They are a string length indicator. > Here are the docs that tell you that it puts a 2-byte length on > the front: > http://java.sun.com/j2se/1.5.0/docs/api/java/io/DataOutputStream.html#writeUTF(java.lang.String) > If you are ignoring these bytes, then how can you be sure you > have received the whole string? Please don't tell me you "just > hope" that the whole string will always arrive in a single read() > call. There is no guarantee of that. TCP does NOT have message > boundaries, and TCP packets can be both fragmented and coalesced. Ah, I see... I worked around this (see below), the workaround is consistent with what you assume. My question "How to send/receive between python SocketServer and java Applet (SocketListener)" then seems to be boiling down to: "How to interface between python unicode and java read/writeUTF?" > > Probably the same problem. If you didn't send a 2 byte length > indicator first, then java's readUTF() will have tried to > interpret the first 2 bytes that you did actually send as the > string length, and may well simply be waiting patiently for the > rest to arrive. > I just couldn't get read/writeUTF and python unicode to interface, so I refactored the applet's socketlistener to convert the socket.getInputStream to a BufferedInputReader on which I call the readline() method. I still skip the first two bytes in the python receiver which seems harmless since python doesn't use the byte length. On both sides I have other way to know when the end-of-message has been reached. A timeout mechanism provides some safety for partial transmission handling. Thanks ! Thijs -- http://mail.python.org/mailman/listinfo/python-list
Re: SocketServer and a Java applet listener
Steve Horsley schreef: > [EMAIL PROTECTED] wrote: > > Steve Horsley schreef: > > > > > >>Probably the same problem. If you didn't send a 2 byte length > >>indicator first, then java's readUTF() will have tried to > >>interpret the first 2 bytes that you did actually send as the > >>string length, and may well simply be waiting patiently for the > >>rest to arrive. > >> > > > > I just couldn't get read/writeUTF and python unicode to interface, so I > > refactored the applet's socketlistener to convert the > > socket.getInputStream to a BufferedInputReader on which I call the > > readline() method. > There are two normal ways to delineate messages in the > byte-stream: An explicit length indication up front (which java > read/writeUTF chooses), or a unique end-of-message indication at > the end such as your readline() for strings that end in linefeed. > > > If you choose to go for the java read/writeUTF approach, the > 2-byte length indicator goes Most Significant Byte first, so a > 100 char string would be preceded by 00 64 ... Also, the > indicator gives the number of bytes after encoding, not the > number of characters before encoding. > You are right, Steven. I invested some time and isolated the problem of transferring UTF-8 strings between a python SocketServer and a Java applet. In a very, very draft version, hereby surrounded by every disclaimer imagineable (as in "Don't try this at home!") I have put the result on a webpage: http://www.phaedro.com/javapythonutf8/ Not only I tried to give a more or less 'generic' solution to the UTF-8 interface (reversing the leading bytes using python's struct module, and based on SocketServer.StreamingRequestHandler), maybe the draft-craft helps others looking for (rather scarce) examples of SocketServer implementations - this one is very trivial so maybe others can learn more efficiently than I had to do. -- Thijs Cobben Explicit typing sux. -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing Multithreaded Client-Server in Python.
Paul Rubin schreef: > "Sidd" <[EMAIL PROTECTED]> writes: > >I tried finding and example of multithreaded client-serve program in > > python. Can any one please tell me how to write a multithreaded > > client-server programn in python such that > > 1.It can handle multiple connections > > 2.It uses actual threads and not select() or some other function > > If you mean multiple threads on the server side, see the SocketServer > module and its ThreadingMixin class. You may have to look at the > source code since up until recently the docs were incomplete. There > is a big helpful comment at the top of SocketServer.py which recently > got merged into the library reference manual. Yes, however the term 'ThreadingMixIn' is a bit confusing (at least it was to me). What it does do, is handle each request (from the same client too) in a new separate thread. Convenient if your processing intensive handle may otherwise slow down the main server process becoming less responsive to other requests. What it doesn't do (and what Sidd seems to search as is suggested by his 'select()' remark) is handle each client in a separate thread. This is in fact listed in the (scarce) doc as a 'ToDo' of the SocketServer if my memory serves me right. If you want to apply SocketServer such that each client corresponds to one thread that handles its' requests (and maintains its state), don't use ThreadingMixIn - only the thread-selection will be executed in a separate thread. You could maintain a dictionary of Threads running RequestHandlers for each client in the server class. I use a new Handler for each incoming request, parse a username parameter from the message and select the thread-handler from the server dict. Each handler in the dict contains an open outgoing socket, so I can also e.g. broadcast and notify clients. Alternatively, you can (probably) keep the client2server socket open as well (on both client and server). Thread selection should then (hopefully :-) happen magically by calling the handle() method in each thread directly. HTH Thijs -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing Multithreaded Client-Server in Python.
Steve Holden schreef: > Paul Rubin wrote: > > [EMAIL PROTECTED] writes: > > > >>What it doesn't do (and what Sidd seems to search as is suggested by > >>his 'select()' remark) is handle each client in a separate thread. > > > > > > I don't know what you mean by that. It launches a new thread for each > > client connection. The connection is two-way and can last as long as > > desired. If you're imagining something like a web server handling > > http requests, using http 1.1 keepalive, you could handle any number > > of requests in that connection. > > > I suspect he was trying to say that BaseHTTPServer has no mechanism for > handling state. As you know, of course, this is most relevant across > multiple successive connections to a server from the same client, and > has little to do with threads. > see below: I must apologize for not being able to use the standard CS vernacular but indeed I meant session (persistent state over multiple requests) - I still think that the thread-starter looks for a mechanism that handles each *session* in a thread where the 'mother' server-thread selects the thread belonging to a session. > > > >>If you want to apply SocketServer such that each client corresponds to > >>one thread that handles its' requests (and maintains its state), don't > >>use ThreadingMixIn - only the thread-selection will be executed in a > >>separate thread. > > > > > > What do you mean by "each client"? If a client connects, does some > > stuff, disconnects, and later reconnects, how do you know that it's > > the same client that's come back? > > The assertion that ThreadingMixIn doesn't handle *sessions* might be > more appropriate, but then there's no reason why it really should (since > if they were handled at all they would be better handled in the base > server classes). By "each client" I suspect the OP really meant "each > session", and was ignoring the fact that the same client can have > multiple sessions to the same server. > Correct. My own 'brew' is multi-session-clients enabled (in fact I test with two applets from the same PC) but indeed I used confusing language saying 'client' for 'session'. Again: ThreadingMixIn doesn't give you 'session threads' in which you store persistent information - a candidate for some generic extension of SocketServer ? When doing research for my own hobby project, I stumbled upon "Twisted" , it seems to give a lot in terms client-server features/functionality compared to SocketServer ? Is it indeed a 'generic network programming framework'? Anyone has experience with it ? Yours, -- Thijs - phaedro.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Writing Multithreaded Client-Server in Python.
a bit of a late reply, sorry... Paul Rubin schreef: > [EMAIL PROTECTED] writes: > > > I suspect he was trying to say that BaseHTTPServer has no mechanism for > > > handling state. As you know, of course, this is most relevant across > > > multiple successive connections to a server from the same client, and > > > has little to do with threads. > > Usually you would do that with browser cookies. > Mwah... Not if you want the client to be responsive to server-initiated messages, i.e. to implement some sort of listener ? (Or you should use Jason-RPC or some other relatively exotic tech?) And, and my guess is this is more applicable in the context of OP's question, not if you want to prevent constructing some client-representing object from a cookie-id for each incoming request (istd. of forwarding the request to a thread holding the client-representing object in the correct current state) > > Correct. My own 'brew' is multi-session-clients enabled (in fact I test > > with two applets from the same PC) but indeed I used confusing language > > saying 'client' for 'session'. > > > > Again: ThreadingMixIn doesn't give you 'session threads' in which you > > store persistent information - a candidate for some generic extension > > of SocketServer ? > > What exactly do you mean by session? I mean (in a C/S context) : "A persistent state initiated by a client (e.g. a login- or first-message event) and maintained by the server." > > How many sessions took place here? Though trivial: 2 'TCP sessions' but I am (sorry for the confusion) not talking about TCP sessions but (and I guess OP too) application-level sessions. > It sounds like you want the answer > to be one session, that operations 2 and 3 are part of the same session. > But that makes no sense unless you associate them somehow, say using > a client cookie and a global session table indexed by the cookie. > In my world, message 3 would be 'session initiating'. Message 2 could be handled on the basis of a possibly changed 'client state' due to processing of message 1 (or other intermediate message exchange). > Why don't you take a look at any of the web frameworks that get > discussed here (Spyce, CherryPy, Zope, Django, whatever). They all > have mechanisms like that. BaseHTTPServer doesn't try to operate > at that level. The OP had a question about SocketServer not BaseHTTPServer? That certainly doesn't give you 'persistent state mechanism' for free. SocketServer doesn't either - all I wanted to say is I got myself confused a bit by the name "ThreadingMixIn" suggesting to me that it would give me a thread to store state info in - again it doesn't . I know Zope very well, and CherryPy a bit. At least for my project they wouldn't suffice because, as far as I know, they don't offer a mechanism for the server to 'raise an event' or 'send a non-response message' to the client. I don't know about Djange and Spyce but thanks: I'll certainly study them ! I suggested Twisted as an 'out-of-the-box' framework for state-persistent, real-time C/S framework but I perceived the overhead/learning-curve intimidating and decided to pass. -- T. www.phaedro.com Compile-time type-checking is a drag. http://www.bushclintonkatrinafund.com/ - help now but never ever vote republican again please -- http://mail.python.org/mailman/listinfo/python-list
Re: new-style classes multiplication error message isn't very informative
gmail.com> writes: > This is a bug in Python. See this thread: > http://mail.python.org/pipermail/python-dev/2005-December/059046.html OK, thanks. This doesn't strike me as the same issue (but maybe it is). We're not getting NotImplemented returned, we're getting a TypeError; just not a good TypeError. > and this patch: > http://sourceforge.net/tracker/?group_id=5470&atid=305470&func=detail&aid=1390657 > > for more details. The patch certainly appears to be on topic, though. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: python module for finite element program
This isn't a python module, but python is embedded in the environment http://salome-platform.org/ Also check out http://www.caelinux.com/CMS/ for a live distro that contains several FE applications. Andy -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie question: how to get started?
On Jun 17, 12:48 pm, ed <[EMAIL PROTECTED]> wrote: > Hi, > > I'm interested in starting to learn python. I'm looking for any > reccomendations or advice that I can use to get started. Looking > forward to any help you can give! > > Thanks! > > -e There are some great tutorials online. Try this one if you're new to programming: http://docs.python.org/tut/tut.html Otherwise, this is more for experienced programmers: http://www.diveintopython.org/ -- Gerald Kaszuba http://geraldkaszuba.com -- http://mail.python.org/mailman/listinfo/python-list
Re: static python classes ?
On Jun 19, 10:00 pm, Tom Gur <[EMAIL PROTECTED]> wrote: > Hi, > > I'm new to python, and I can't seem to find in the docs how to create > the python equivalent of what's called in most OOP languages "static > classes", can you give me a hint ? Look for @staticmethod in http://docs.python.org/lib/built-in-funcs.html Example: class C: @staticmethod def f(arg1, arg2, ...): ... -- Gerald Kaszuba http://geraldkaszuba.com -- http://mail.python.org/mailman/listinfo/python-list
Re: static python classes ?
> > the python equivalent of what's called in most OOP languages "static > > classes", can you give me a hint ? > > Look for @staticmethod inhttp://docs.python.org/lib/built-in-funcs.html Woops... I misread... -- Gerald Kaszuba http://geraldkaszuba.com -- http://mail.python.org/mailman/listinfo/python-list
How to use os.putenv() ?
>>>
>>> import os
>>>
>>> os.environ['PATH']
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>>
>>> os.putenv('PATH', 'C:\\WINNT\\system32')
>>>
>>> os.environ['PATH']
'C:\\WINNT\\system32;C:\\WINNT;C:\\WINNT\\System32\\Wbem;%C:\\WINNT%\
\system32;%C:\\WINNT%;%C:\\WINNT%\\System32\\Wbem'
>>>
What am I doing wrong? How do I change the value of an environment
variable?
--
http://mail.python.org/mailman/listinfo/python-list
Output to a text window
Hi, I'm going around in circles so I'm asking for help. I want to read a simple text file and output the contents to a GUI window when I click on a button. I have written a small python program to read the contents of a file when a button is clicked but can only output this to a console window. I'm using the pygtk binding with glade for the gui. I know it must be quiet simple but a mental block has rapidly descended. Any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Output to a text window
Hi, I'm going around in circles so I'm asking for help. I want to read a simple text file and output the contents to a GUI window when I click on a button. I have written a small python program to read the contents of a file when a button is clicked but can only output this to a console window. I'm using the pygtk binding with glade for the gui. I know it must be quiet simple but a mental block has rapidly descended. Any help would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Output to a text window
On Feb 17, 1:25 pm, "Joshua J. Kugler" <[EMAIL PROTECTED]> wrote:
> [EMAIL PROTECTED] wrote:
> > Hi,
>
> > I'm going around in circles so I'm asking for help. I want to read a
> > simple text file and output the contents to a GUI window when I click
> > on a button. I have written a small python program to read the
> > contents of a file when a button is clicked but can only output this
> > to a console window. I'm using the pygtk binding with glade for the
> > gui.
>
> > I know it must be quiet simple but a mental block has rapidly
> > descended.
>
> > Any help would be appreciated.
>
> What does your code look like? What are you using to print? Are you
> writing to the GUI or using the 'print statement?'
>
> j
>
> --
> Joshua Kugler
> Lead System Admin -- Senior Programmerhttp://www.eeinternet.com
> PGP Key:http://pgp.mit.edu/ ID 0xDB26D7CE
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com
This is the code, it was adapted from code I found on the net. Code as
follows,
#!/usr/bin/python
import pygtk
import gtk
import gtk.glade
import string
import os
import gobject
class gui:
def __init__(self):
self.wTree=gtk.glade.XML('dansgui.glade')
dic = { "on_Read_File": self.on_Read_File,
"on_cancel": (gtk.main_quit)}
self.wTree.signal_autoconnect(dic)
self.count = 0
self.win = self.wTree.get_widget("window1")
self.win.connect("delete_event", self.on_delete_event)
self.win.show()
def on_Read_File(self, widget):
print "Opening Dansguardian bannedsitelist file for reading..."
print; print
lineoftext = open('/etc/dansguardian/lists/bannedsitelist', 'r')
myarray = []
lnum = 0
for line in lineoftext:
line = line.rstrip('\n')
line = line.rstrip( )
lnum = lnum + 1
print lnum , line
myarray.append(line);
lineoftext.close( )
def on_delete_event(self, widget, event):
self.win.set_sensitive(False)
dialog = gtk.MessageDialog(self.win,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
gtk.MESSAGE_INFO, gtk.BUTTONS_YES_NO, None)
dialog.set_markup('Are you sure you want to quit?')
dialog.connect("destroy", lambda w:
self.win.set_sensitive(True))
answer = dialog.run()
if answer == -8:
dialog.destroy()
return False
if answer == -9:
dialog.destroy()
return True
app = gui()
gtk.main()
--
http://mail.python.org/mailman/listinfo/python-list
How do I save the contents of a text buffer
Hi,
I'm using Python with pygtk and have this problem - I have read the
contents of a file into the text buffer with this code,
infile = open("mytextfile", "r")
if infile:
string = infile.read()
infile.close()
textbuffer.set_text(string)
As a test, I tried to write the buffer back to a file with this code
but did not work,
outfile = open("newbannedsitelist", "w")
outfile.write(textbuffer.get_text(0, 1000,
include_hidden_chars=True))
outfile.close()
What I want to know is how do I write the contents of the text buffer
back to a file?
Thanks
--
http://mail.python.org/mailman/listinfo/python-list
Re: How do I save the contents of a text buffer
I just included file opening code just to show how i read the file
into the text buffer - I have no issues with this as such. Problem is
only with the writing of the text buffer back to a file. When I try to
write the buffer to a file it gave the following,
Traceback (most recent call last):
File "./configbox.py", line 78, in ?
TextViewExample()
File "./configbox.py", line 53, in __init__
outfile.write(textbuffer.get_text(0,1000,
include_hidden_chars=True))
TypeError: start should be a GtkTextIter
How can I use outfile.write() to wite the contents of the text buffer
correctly?
Thanks
On Feb 18, 1:38 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> En Sat, 17 Feb 2007 20:47:20 -0300, <[EMAIL PROTECTED]> escribió:
>
> > I'm using Python with pygtk and have this problem - I have read the
> > contents of a file into the text buffer with this code,
>
> > infile = open("mytextfile", "r")
> > if infile:
> > string = infile.read()
> > infile.close()
> > textbuffer.set_text(string)
>
> Note that "if infile" does nothing: either the open succeeds and you get a
> file object which is *always* True, or the open fails and raises an
> exception and the code below never executes.
>
> > As a test, I tried to write the buffer back to a file with this code
> > but did not work,
>
> "did not work" means...
>
> --
> Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list
Re: How do I save the contents of a text buffer
On Feb 18, 1:14 pm, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Sat, 17 Feb 2007 15:47:20 -0800, google wrote: > > As a test, I tried to write the buffer back to a file with this code > > but did not work, > > Oooh, guessing games! I love guessing games! Good > Let me see... did it reboot your PC? No > Did Python crash? No - it runs on nix > Did you get an exception? Maybe something about not being able to open > the file for reading? Or perhaps disk full? File read ok for input, its the file write thats the problem > Did you get something unexpected in the file? Maybe an empty file? Yes, a rabbit popped out of the floppy slot - amazing! > I'm guessing... it erased your hard disk. Do I win? Sorry, you loseare you a Windows user? > -- > Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: How do I save the contents of a text buffer
Solved!
# Output file
outfile = open("newbannedsitelist", "w")
outfile.write(textbuffer.get_text(textbuffer.get_start_iter(),
textbuffer.get_end_iter(), include_hidden_chars=True))
outfile.close()
I'm new to Python and GTK and may not be asking the right type of
questions initially. I programmed with C many many years ago and my
programming instincts are very rusty so still feeling my way again
with this stuff. Apologies for the sarcastic reply earlier but i felt
your initial reply to be sarcastic. I also did research this problem
but seemed to be getting nowhere - thats why I asked for the groups
help.
Anyway, thanks to all that replied via emailed and in this group.
On Feb 18, 7:34 pm, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Sat, 17 Feb 2007 17:10:50 -0800, google wrote:
> > I just included file opening code just to show how i read the file
> > into the text buffer - I have no issues with this as such. Problem is
> > only with the writing of the text buffer back to a file. When I try to
> > write the buffer to a file it gave the following,
>
> >
> > Traceback (most recent call last):
> > File "./configbox.py", line 78, in ?
> > TextViewExample()
> > File "./configbox.py", line 53, in __init__
> > outfile.write(textbuffer.get_text(0,1000,
> > include_hidden_chars=True))
> > TypeError: start should be a GtkTextIter
>
> Ah, well there's your problem. start should be a GtkTextIter, just like
> the exception says.
>
> Question for you: in the line of code in the traceback, which function
> takes an argument called "start"?
>
> > How can I use outfile.write() to wite the contents of the text buffer
> > correctly?
>
> Your problem isn't with outfile.write().
>
> --
> Steven.
--
http://mail.python.org/mailman/listinfo/python-list
Re: SimpleXMLRPCServer and creating a new object on for each new client request.
On May 8, 1:17 am, Piet van Oostrum wrote:
> > Jelle Smet (JS) wrote:
>
> One more thing:
>
> >JS> I start python interactively:
> > import xmlrpclib
> > session1=xmlrpclib.ServerProxy('http://localhost:8000')
> > session2=xmlrpclib.ServerProxy('http://localhost:8000')
> > print session1.show_random()
> >JS> 13930
> > print session2.show_random()
> >JS> 13930
>
> I get the impression, also from your use of the variable names
> 'session1' and 'session2' that xmlrpclib.ServerProxy() gives you some
> kind of connection to the XMLRPC server. This is not the case. It gives
> just an administration object *in the client* that will communicate with
> the server when you call a method on it. The two session's in your code
> are therefore functionally equivalent and there is no advantage in
> having two of them instead of one. And the name session is misleading.
>
> Please note also that XMLRPC isn't object-oriented. There is just the
> server; in the protocol there are no objects other than the server.
> --
> Piet van Oostrum
> URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
> Private email: [email protected]
Hi Piet,
Yes, I'm aware of this.
>You create a single instance of your class and then register that. There
>is nothing in your code that makes the server believe it should create a
>new instance for each request.
>On the other hand, If SimpleXMLRPCServer had this capability, my example
>wouldn't be correct anyway, because only one instance of class Randomizer is
>>created when the SimpleXMLRPCServer starts.
Exactly, I understand.
>Otherwise you would have to register a function that creates a new
>instance on every call.
>But as Martin P. Hellwig has noted, it wouldn't give you sessions anyway.
Well, I think Martin's example will suit my needs.
Thanks for the explanation!
Jelle
Thanks for the feedback, ...
--
http://mail.python.org/mailman/listinfo/python-list
haif riends how do you
hi friends COMMON DO YOU SEE THE DIFFERNT PICTURE AND INFORMATION COMMON LETS GOO http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com http://www.airnet5.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: XML and namespaces
Uche <[EMAIL PROTECTED]> wrote: > Of course. Minidom implements level 2 (thus the "NS" at the end of the > method name), which means that its APIs should all be namespace aware. > The bug is that writexml() and thus toxml() are not so. Not exactly a bug - DOM Level 2 Core 1.1.8p2 explicitly leaves namespace fixup at the mercy of the application. It's only standardised as a DOM feature in Level 3, which minidom does not yet claim to support. It would be a nice feature to add, but it's not entirely trivial to implement, especially when you can serialize a partial DOM tree. Additionally, it might have some compatibility problems with apps that don't expect namespace declarations to automagically appear. For example, perhaps, an app dealing with HTML that doesn't want spare xmlns="http://www.w3.org/1999/xhtml"; declarations appearing in every snippet of serialized output. So it should probably be optional. In DOM Level 3 (and pxdom) there's a DOMConfiguration parameter 'namespaces' to control it; perhaps for minidom an argument to toxml() might be best? -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Octal notation: severe deprecation
John Machin wrote: > I regard continued usage of octal as a pox and a pestilence. Quite agree. I was disappointed that it ever made it into Python. Octal's only use is: a) umasks b) confusing the hell out of normal non-programmers for whom a leading zero is in no way magic (a) does not outweigh (b). In Mythical Future Python I would like to be able to use any base in integer literals, which would be better. Example random syntax: flags= 2x00011010101001 umask= 8x664 answer= 10x42 addr= 16x0E84 # 16x == 0x gunk= 36x8H6Z9A0X But either way, I want rid of 0->octal. > Is it not regretted? Maybe the problem just doesn't occur to people who have used C too long. OT: Also, if Google doesn't stop lstrip()ing my posts I may have to get a proper news feed. What use is that on a Python newsgroup? Grr. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI POST problem was: How to read POSTed data
Dan Perl wrote: > how is a multipart POST request parsed by CGIHTTPServer? It isn't; the input stream containing the multipart/form-data content is passed to the CGI script, which can choose to parse it or not using any code it has to hand - which could be the 'cgi' module, but not necessarily. > Where is the parsing done for the POST data following the header? If you are using the 'cgi' module, then cgi.parse_multipart. > As a side note, I found other old reports of problems with cgi > handling POST requests, reports that don't seem to have had a > resolution. (in particular?) FWIW, for interface-style and multipart-POST-file-upload-speed reasons I wrote an alternative to cgi, form.py (http://www.doxdesk.com/software/py/form.html). But I haven't found cgi's POST reading to be buggy in general. > There is even a bug reported just a few days ago (1112856) that is > exactly about multipart post requests. If I understand the bug > report correctly though, it is only on the latest version in CVS > and it states that what is in the 2.4 release works. That's correct. > All this tells me that it could be a "fragile" part in the standard > library. I don't really think so; it's really an old stable part of the library that is a bit crufty in places due to age. The patch that caused 1112856 was an attempt to rip out and replace the parser stuff, which as a big change to old code is bound to cause trouble. But that's what the dev cycle is for. CGIHTTPServer, on the other hand, I have never really trusted. I would suspect that fella. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with sha.new
Florian Lindner wrote: > sha = sha.new(f.read()) > this generates a traceback when sha.new() is called for the second time You have reassigned the variable 'sha'. First time around, sha is the sha module object as obtained by 'import sha'. Second time around, sha is the SHA hashing object you used the first time around. This does not have a 'new' method. Python does not have separate namespaces for packages and variables. Modules are stored in variables just like any other object. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: why UnboundLocalError?
Alex Gittens wrote: > I'm getting an UnboundLocalError > def fieldprint(widths,align,fields): [...] > def cutbits(): [...] > fields = fields[widths[i]:] There's your problem. You are assigning 'fields' a completely new value. Python doesn't allow you to rebind a variable from an outer scope in an inner scope (except for the special case where you explicitly use the 'global' directive, which is no use for the nested scopes you are using here). So when you assign an identifier in a function Python assumes that you want that identifier to be a completely new local variable, *not* a reference to the variable in the outer scope. By writing 'fields= ...' in cutbits you are telling Python that fields is now a local variable to cutbits. So when the function is entered, fields is a new variable with no value yet, and when you first try to read it without writing to it first you'll get an error. What you probably want to do is keep 'fields' pointing to the same list, but just change the contents of the list. So replace the assign operation with a slicing one: del fields[:widths[i]] -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Yet Another Python Web Programming Question
Daniel Bickett wrote: > Python using CGI, for example, was enough for him until he started > getting 500 errors that he wasn't sure how to fix. Every time you mention web applications on this list, there will necessarily be a flood of My Favourite Framework Is X posts. But you* sound like you don't want a framework to take over the architecture of your app and tell you what to do. And, indeed, you don't need to do that. There are plenty of standalone modules you can use - even ones that are masquerading as part of a framework. I personally use my own input-stage and templating modules, along with many others, over standard CGI, and only bother moving to a faster server interface which can support DB connection pooling (such as mod_python) if it's actually necessary - which is, surprisingly, not that often. Hopefully if WSGI catches on we will have a better interface available as standard in the future. Not quite sure what 500 Errors you're getting, but usually 500s are caused by unhandled exceptions, which Apache doesn't display the traceback from (for security reasons). Bang the cgitb module in there and you should be able to diagnose problems more easily. > He is also interested in some opinions on the best/most carefree way > of interfacing with MySQL databases. MySQLdb works fine for me: http://sourceforge.net/projects/mysql-python/ (* - er, I mean, Hypothetical. But Hypothetical is a girl's name!) -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Importing User-defined Modules
Walter Brunswick <[EMAIL PROTECTED]> wrote: > I need to import modules with user-defined file extensions > that differ from '.py', and also (if possible) redirect the > bytecode output of the file to a file of a user-defined > extension. You shouldn't really need a PEP for that; you can take control of the compile and import processes manually. See the py_compile and imp modules. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL: retreive image resolution (dpi)
[EMAIL PROTECTED] wrote: > I looked at the PIL Image class but cannot see a posibility to retreive > the image resolution dots per inch (or pixels per inch) Not all formats provide a DPI value; since PIL doesn't do anything with DPI it's not part of the main interface. For PNG and JPEG at least the value may be retrievable from the extra info dictionary (image.info['dpi']) when loaded from a file that sets it. Expect an (x, y) tuple (not necessarily square-pixel). -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with minidom and special chars in HTML
Horst Gutmann wrote:
> I currently have quite a big problem with minidom and special chars
> (for example ü) in HTML.
Yes. Ignoring the issue of the wrong doctype, minidom is a pure XML
parser and knows nothing of XHTML and its doctype's entities 'uuml' and
the like. Only the built-in entities (& etc.) will work.
Unfortunately the parser minidom uses won't read external entities -
including the external subset of the DTD (which is where all the stuff
about what "ü" means is stored). And because minidom does not
support EntityReference nodes, the information that there was an entity
reference there at all gets thrown away as it is replaced with the
empty string. Which is kind of bad.
Possible workarounds:
1. pass minidom a different parser to use, one which supports external
entities and which will parse all the DTD stuff. I don't know if there
is anything suitable available, though...
2. use a DOM implementation with the option to support external
entities. For example, with pxdom, one can use DOM Level 3 LS methods,
or pxdom.parse(f, {'pxdom-external-entities': True}).
However note that reading and parsing an external entity will introduce
significant slowdown, especially in the case of the rather complex
multi-file XHTML DTD. Other possibilities:
3. hack the content on the way into the parser to replace the DOCTYPE
declaration with one including entity definitions in the internal
subset:
...
]>
...
4. hack the content on the way into the parser to replace entity
references with character references, eg. ü -> ü. This is
'safe' for simple documents without an internal subset; charrefs and
entrefs can be used in the same places with the same meaning, except
for some issues in the internal subset.
5. use a DOM implementation that supports EntityReference nodes, such
as pxdom. Entity references with no replacement text (or all entity
references if the DOM Level 3 LS parameter 'entities' is set) will
exist as EntityReference DOM objects instead of being flattened to
text. They can safely be reserialized as ü without the
implementation having to know what text they represent.
Entities are a big source of complication and confusion, which I wish
had not made it into XML!
--
Andrew Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: get textual content of a Xml element using 4DOM
Frank Abel Cancio Bello <[EMAIL PROTECTED]> wrote: > PrettyPrint or Print return the value to the console, and i need > keep this value in a string variable to work with it, how can i > do this? The second parameter to either of these functions can be a stream object, so you can use a StringIO to get string output: from StringIO import StringIO from xml.dom.ext import Print buf= StringIO() Print(doc, buf) xml= buf.getvalue() -- Andrew Clover http://www.doxdesk.com/ mailto:[EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: convert gb18030 to utf16
Xah Lee <[EMAIL PROTECTED]> wrotE:
> i have a bunch of files encoded in GB18030. Is there a way to convert
> them to utf16 with python?
You will need CJKCodecs (http://cjkpython.i18n.org/), or Python 2.4,
which has them built in. Then just use them like any other codec. eg.
f= open(path, 'rb')
content= unicode(f.read(), 'gb18030')
f.close()
f= open(path, 'wb')
f.write(content.encode('utf-16'))
f.close()
--
Andrew Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: function with a state
Xah Lee <[EMAIL PROTECTED]> wrote: > is it possible in Python to create a function that maintains a > variable value? Yes. There's no concept of a 'static' function variable as such, but there are many other ways to achieve the same thing. > globe=0; > def myFun(): > globe=globe+1 > return globe This would work except that you have to tell it explicitly that you're working with a global, otherwise Python sees the "globe=" and decides you want 'globe' be a local variable. globe= 0 def myFun(): global globe globe= globe+1 return globe Alternatively, wrap the value in a mutable type so you don't have to do an assignment (and can use it in nested scopes): globe= [ 0 ] def myFun(): globe[0]+= 1 return globe[0] A hack you can use to hide statics from code outside the function is to abuse the fact that default parameters are calcuated at define-time: def myFun(globe= [ 0 ]): globe[0]+= 1 return globe[0] For more complicated cases, it might be better to be explicit and use objects: class Counter: def __init__(self): self.globe= 0 def count(self): self.globe+= 1 return self.globe myFun= Counter().count -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: injecting "set" into 2.3's builtins?
Skip Montanaro wrote: > I use sets a lot in my Python 2.3 code at work and have been using > this hideous import to make the future move to 2.4's set type > transparent: > try: > x = set (Surely just 'set' on its own is sufficient? This avoids the ugly else clause.) > __builtin__.set = sets.Set > I'm wondering if others have tried it. If so, did it cause any > problems? I don't know of any specific case where it would cause problems but I'd be very wary of this; certainly doing the same with True and False has caused problems in the past. A module might sniff for 'set' and assume it is running on 2.4 if it sees it, with unpredictable results if it relies on any other 2.4 behaviour. I'd personally put this at the top of local scripts: from siteglobals import * Then put compatibility hacks like set and bool in siteglobals.py. Then any modules or other non-site scripts could continue without the polluted builtin scope. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: regex syntax
Andreas Volz <[EMAIL PROTECTED]> schrieb:
> Ich hab mir schon überlegt einfach die letzten viel Stellen des
> strings "per Hand" auf die Zeichenfolge zu vergleichen und so
> regex zu umgehen. Aber ich muss es irgendwann ja doch mal nutzen
"Muss"? stimme nicht zu! Regexps sind ja fuer begrenzte Zwecke eine
gute Loesung, aber kein Basisteil der Programmierung. Bei diesem
Beispiel waere:
>>> filename.endswith('.jpg')
viel besser als das vergleichbare Regexp:
>>> re.match('.*\.jpg$', filename)
--
Andrew Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Web forum (made by python)
Choe, Cheng-Dae wrote: > example site is http://bbs.pythonworld.net:9080/pybbs.py Since this seems quite happy to accept posted
Re: Looking for source preservation features in XML libs
Grzegorz Adam Hankiewicz <[EMAIL PROTECTED]> wrote: > I have looked at xml.minidom, elementtree and gnosis and haven't > found any such features. Are there libs providing these? pxdom (http://www.doxdesk.com/software/py/pxdom.html) has some of this, but I think it's still way off what you're envisaging. > One is to be able to tell which source file line a tag starts > and ends. You can get the file and line/column where a node begins in pxdom using the non-standard property Node.pxdomLocation, which returns a DOM Level 3 DOMLocator object, eg.: uri= node.pxdomLocation.uri line= node.pxdomLocation.lineNumber col= node.pxdomLocation.columnNumber There is no way to get the location of an Element's end-tag, however. Except guessing by looking at the positions of adjacent nodes, which is kind of cheating and probably not reliable. SAX processors can in theory use Locator information too, but AFAIK (?) this isn't currently implemented. > Another feature is to be able to save the processed XML code in a way > that unmodified tags preserve the original identation. Do you mean whitespace *inside* the start-tag? I don't know of any XML processor that will do anything but ignore whitespace here; in XML terms it is utterly insignificant and there is no place to store the information in the infoset or DOM properties. pxdom will preserve the *order* of the attributes, but even that is not required by any XML standard. > Or in the worst case, all identation is lost, but I can control to > some degree the outlook of the final XML output. The DOM Level 3 LS feature format-pretty-print (and PyXML's PrettyPrint) influence whitespace in content. However if you do want control of whitespace inside the tags themselves I don't know of any XML tools that will do it. You might have to write your own serializer, or hack it into a DOM implementation of your choice. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: File Uploads
Doug Helm wrote:
> form = cgi.FieldStorage()
> if lobjUp.Save('filename', 'SomeFile.jpg'):
> class BLOB(staticobject.StaticObject):
> def Save(self, pstrFormFieldName, pstrFilePathAndName):
> form = cgi.FieldStorage()
You are instantiating cgi.FieldStorage twice. This won't work for POST
requests, because instantiating a FieldStorage reads the form data from
the standard input stream (the HTTP request).
Try to create a second one and cgi will try to read all the form data
again; this will hang, waiting for the socket to send it a load more
data which will not be forthcoming.
When using CGI, parse the input only once, then pass the results (a
FieldStorage object if you are using the cgi module) in to any other
functions that need to read it.
--
Andrew Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Python 2.4.1 install broke RedHat 9 printconf-backend
BrianS wrote: > File "/usr/share/printconf/util/printconf_conf.py", line 83, in ? > from xml.utils import qp_xml > ImportError: No module named utils > It seems that the xml package have been changed. Not exactly. xml.utils is part of the XML processing package PyXML - you don't get it in the cut-down XML stuff available in the standard library. You could try downloading and installing from http://pyxml.sf.net/. Though I can't guarantee there won't be other problems as RedHat can be very annoying like this. You might have to keep Python 2.2 around in addition to 2.4 for RH's benefit; in any case trying to remove 2.2 will probably lead you into an RPM dependency nightmare. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: XML and namespaces
Uche Ogbuji <[EMAIL PROTECTED]> wrote:
> Andrew Clover also suggested an overly-legalistic argument that current
> minidom behavior is not a bug.
I stick by my language-law interpretation of spec. DOM 2 Core
specifically disclaims any responsibility for namespace fixup and
advises the application writer to do it themselves if they want to be
sure of the right output. W3C knew they weren't going to get all that
standardised by Level 2 so they left it open for future work - if
minidom claimed to support DOM 3 LS it would be a different matter.
> '\n'
> (i.e. "ferh" rather than "href"), would you not consider that a minidom
> bug?
It's not a *spec* bug, as no spec that minidom claims to conform to
says anything about serialisation. It's a *minidom* bug in that it
fails to conform to the minimal documentation of the method toxml()
which claims to "Return the XML that the DOM represents as a string" -
the DOM does not represent that XML.
However that doc for toxml() says nothing about being namespace-aware.
XML and XML-with-namespaces both still exist, and for the former class
of document the minidom behaviour is correct.
> The reality is that once the poor user has done:
> element = document.createElementNS("DAV:", "href")
> They are following DOM specification that they have created an element
> in a namespace
It's possible that a namespaced node could also be imported/parsed into
a non-namespace document and then serialised; it's particularly likely
this could happen for scripts processing XHTML.
We shouldn't change the existing behaviour for toxml/writexml because
people may be relying on it. One of the reasons I ended up writing a
replacement was that the behaviour of minidom was not only wrong, but
kept changing under my feet with each version.
However, adding the ability to do fixup on serialisation would indeed
be very welcome - toxmlns() maybe, or toxml(namespaces= True)?
> I'll be sure to emphasize heavily to users that minidom is broken
> with respect to Namespaces and serialization, and that they
> abandon it in favor of third-party tools.
Well yes... there are in any case more fundamental bugs than just
serialisation problems.
Frederik wrote:
> can anyone perhaps dig up a DOM L2 implementation that's not written
> by anyone involved in this thread
--
And Clover
mailto:[EMAIL PROTECTED]
http://doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Retrieve a GIF's palette entries using Python Imaging Library (PIL)
Stuart wrote: > I see that the 'Image' class has a 'palette' attribute which returns an > object of type 'ImagePalette'. However, the documentation is a bit > lacking regarding how to maniuplate the ImagePalette class to retrieve > the palette entries' RGB values. ImagePalette.getdata() should do it. There seems to be some kind of bug, however, where Images lose their ImagePalettes after being convert()ed to paletted images (eg. using Image.ADAPTIVE). For this reason I personally use the getpalette() method from the wrapped image object, which seems to contain the proper raw palette data. For example to get a list of [r,g,b] colour lists: def chunk(seq, size): return [seq[i:i+size] for i in range(0, len(seq), size)] palette= image.im.getpalette() colours= [map(ord, bytes) for bytes in chunk(palette, 3)] -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI Tutorial
Clodoaldo Pinto Neto wrote:
> print 'The submited name was "' + name + '"'
Bzzt! Script injection security hole. See cgi.escape and use it (or a
similar function) for *all* text -> HTML output.
> open('files/' + fileitem.filename, 'w')
BZZT. filesystem overwriting security hole, possibly escalatable to
code execution. clue: fileitem.filename= '../../something.py'
> sid = cookie['sid'].value
> session = shelve.open('/tmp/.session/sess_' + sid
Bad filename use allows choice of non-session files, opening with
shelve allows all sorts of pickle weirdnesses. Just use strings.
> p = sub.Popen(str_command,
o.O
Sure this stuff may not matter for Hello World on a test server, but if
you're writing a tutorial you should ensure newbies know the Right Way
to do it from the start. The proliferation of security-oblivious PHP
tutorials is directly responsible for the disasterous amount of
script-injection- and SQL-injection-vulnerable webapps out there -
let's not have the same for Python.
--
And Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Dr. Dobb's Python-URL! - weekly Python news and links (Jun 12)
John Salerno wrote: > I love the new 'folder' icon, but how can I access it as an icon? I've just given these are proper home, so here: http://doxdesk.com/software/py/pyicons.html cheers! -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: new python icons for windows
Istvan Albert wrote: > But these new icons are too large, too blocky and too pastel. Hooray! Glad to see *someone* doesn't like 'em, I'll expect a few more when b1 hits. :-) Although I can't really see 'large', 'blocky' or 'pastel'... they're the same size and shape as other Windows document icons, and I personally find the Python logo colours quite striking. If it's the new-fangled shadey gradienty kind of nonsense you don't like, you could also try the low-colour versions. eg. ICOs compiled with only 16-colour and 16/32 sizes: http://doxdesk.com/file/software/py/pyicons-tiny.zip > For example it resembles the icon for text files. This is intentional: to make it obvious that .py files are the readable, editable scripts, contrasting with .pyc's binary gunk - something that wasn't 100% clear before. With the obviousness of the Python-plus and the strong difference between the white and black base document icons, squinting shouldn't really be necessary IMO. > can someone point me to a page/link that contains the old icons? Sure, http://svn.python.org/view/python/branches/release24-maint/PC/py.ico http://svn.python.org/view/python/branches/release24-maint/PC/pyc.ico http://svn.python.org/view/python/branches/release24-maint/PC/pycon.ico -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Having problems with strings in HTML
Sion Arrowsmith wrote: > I've never encountred a browser getting tripped up by it. I suppose you > might need it if you've got parameters called quot or nbsp There are many more entities than you can comfortably remember, and browsers can interpret anything starting with one as being an entity reference, hence all the problems with parameters like 'section' (-> §). Plus of course there's nothing stopping future browsers supporting more entities, breaking your apps. Just write &. There's no reason not to (except ignorance). The fact that so much of the web is written with broken HTML is not an argument for doing the same. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: getting POST vars from BaseHTTPRequestHandler
Christopher J. Bottaro wrote: > When I make a post, it just hangs (in self.rfile.read()). I don't know about BaseHTTPRequestHandler in particular, but in general you don't want to call an unlimited read() on an HTTP request - it will try to read the entire incoming stream, up until the stream is ended by the client dropping the connection (by which point it's too late to send a response). Instead you'll normally want to read the request's Content-Length header (int(os.environ['CONTENT_LENGTH']) under CGI) and read(that many) bytes. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: A critique of cgi.escape
Jon Ribbens wrote:
> I'm sorry, that's not good enough. How, precisely, would it break
> "existing code"?
('owdo Mr. Ribbens!)
It's possible there could be software that relies on ' not being
escaped, for example:
# Auto-markup links to O'Reilly, everyone's favourite
# example name with an apostrophe in it
#
URI= 'http://www.oreilly.com/'
html= cgi.escape(text)
html= html.replace('O\'Reilly', 'O\'Reilly' % URI)
Sure this may be rare, but it's what the documentation says, and
changing it may not only fix things but also subtly break things in
ways that are hard to detect.
A similar change to str.encode('unicode-escape') in Python 2.5 caused a
number of similar subtle problems. (In this case the old documentation
was a bit woolly so didn't prescribe the exact older behaviour.)
I'm not saying that the cgi.escape interface is *good*, just that it's
too late to change it.
I personally think the entire function should be deprecated, firstly
because it's insufficient in some corner cases (apostrophes as you
pointed out, and XHTML CDATA), and secondly because it's in the wrong
place: HTML-escaping is nothing to do with the CGI interface. A good
template library should deal with escaping more smoothly and correctly
than cgi.escape. (It may be able to deal with escape-or-not-bother and
character encoding issues automatically, for example.)
--
And Clover
mailto:[EMAIL PROTECTED]
http://www.doxdesk.com/
--
http://mail.python.org/mailman/listinfo/python-list
Re: Pure python implementation of string-like class
Akihiro KAYAMA wrote: > As the character set is wider than UTF-16(U+10), I can't use > Python's native unicode string class. Have you tried using Python compiled in Wide Unicode mode (--enable-unicode=ucs4)? You get native UTF-32/UCS-4 strings then, which should be enough for most purposes. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Elementtree and CDATA handling
Alain <[EMAIL PROTECTED]> wrote:
> I would expect a piece of XML to be read, parsed and written back
> without corruption [...]. It isn't however the case when it comes
> to CDATA handling.
This is not corruption, exactly. For most intents and purposes, CDATA
sections should behave identically to normal character data. In a real
XML-based browser (such as Mozilla in application/xhtml+xml mode), this
line of script would actually work fine:
> if (a < b && a > 0) {
The problem is you're (presumably) producing output that you want to be
understood by things that are not XML parsers, namely legacy-HTML web
browsers, which have special exceptions-to-the-rule like "
Re: Python as CGI on IIS and Windows 2003 Server
Lothat <[EMAIL PROTECTED]> wrote: > No test with or without any " let the IIS execute python scrits as cgi. > Http Error code is 404 (but i'm sure that the file exists in the > requested path). Have you checked the security restrictions? IIS6 has a new feature whereby script mappings are disabled by default even if they are listed in the configuration list. To turn CGI on, go to the IIS Manager snap-in and select the 'Web Service Extensions' folder. Select 'All Unknown CGI Extensions' and click 'Allow'. Incidentally, the string I am using is: "C:\Program Files\Python\2.4\python.exe" -u "%s" "%s" -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Pattern for error checking easiest-first?
Heres the situation: class AbstractThing(): def changeMe(self,blah): if blah < 1: raise MyException self.blah = blah class NetworkedThing(AbstractThing): def changeMe(self,blah): if blah > self.getUpperLimitOverTheNetworkSlowly: raise MyOtherException AbstractThing.changeMe(self,blah) The problem is that code like this does error checking backwards. A call to NetworkedThing.changeMe will first do a slow error check and then a fast one. Obviously there are various ways to get around this - either have the subclass explicitly ask the superclass to error check first, or vice totally versa. Is there some accepted pattern/idiom for handling this issue? -- http://mail.python.org/mailman/listinfo/python-list
Re: Psycho question
On Aug 8, 7:18 pm, "David C. Ullrich" <[EMAIL PROTECTED]> wrote: > The one thing that puzzles me about > all the results is why // is so much slower than / inside > that Psyco loop. Just an oversight. The optimization about '/' between integers was not copied for the case of '//' between integers. Fixed in the svn head :-) Armin -- http://mail.python.org/mailman/listinfo/python-list
New-style Python icons
Personally, I *like* the new website look, and I'm glad to see Python having a proper logo at last! I've taken the opportunity to knock up some icons using it, finally banishing the poor old standard-VGA-palette snake from my desktop. If you like, you can grab them from: http://www.doxdesk.com/img/software/py/icons.zip in .ICO format for Windows - containing all resolutions/depths up to and including Windows Vista's crazy new enormo-icons. Also contains the vector graphics source file in Xara format. You can also see a preview here: http://www.doxdesk.com/img/software/py/icons.png -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: New-style Python icons
Scott David Daniels wrote: > Maybe you could change the ink color to better distinguish > the pycon and pyc icons. Yeah, might do that... I'm thinking I might flip the pycon icon so that the Windows shortcut badge doesn't obscure the Python logo, too. Maybe. I'll let them stew on my desktop for a bit first though... -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: why isn't Unicode the default encoding?
John Salerno wrote: > So as it turns out, Unicode and UTF-8 are not the same thing? Well yes. UTF-8 is one scheme in which the whole Unicode character repertoire can be represented as bytes. Confusion arises because Windows uses the name 'Unicode' in character encoding lists, to mean UTF-16_LE, which is another encoding that can store the whole Unicode character repertoire as bytes. However UTF-16_LE is not any more definitively 'Unicode' than UTF-8 is. Further confusion arises because the encoding 'UTF-16' can actually mean two things that are deceptively different: - Unicode characters stored natively in 16-bit units (using two UTF-16 characters to represent characters outside of the Basic Multilingual Plane) - Either of the 8-bit encodings UTF-16_LE and UTF-16_BE, detected automatically using a Byte Order Mark when loaded, or chosen arbitrarily when saving Yet more confusion arises because UTF-32 (which can reference any Unicode character directly) has the same problem. And though wide-unicode builds of Python understand the first meaning (unicode() strings are stored natively as UTF-32), they don't support the 8-bit encodings UTF-32_LE and UTF-32_BE. Phew! To summarise: confusion. > Am I right to say that UTF-8 stores the first 128 Unicode code points > in a single byte, and then stores higher code points in however many > bytes they may need? That is correct. To answer the original question, we're always going to need byte strings. They're a fundamental part of computing and the need to process them isn't going to go away. However as Unicode text manipulation becomes a more common event than byte string processing, it makes sense to change the default kind of string you get when you type a literal. Personally I would like to see byte strings available under an easy syntax like b'...' and UTF-32 strings available as w'...', or something like that - currently having u'...' mean either UTF-16 or UTF-32 depending on compile-time options is very very annoying to the few kinds of programs that really do need to know the difference. But whatever is chosen, it's all tasty Python 3000 future-soup and not worth worrying about for the moment. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: New-style Python icons
Michael Tobis wrote: > Besides the pleasant colors what do you like about it? I like that whilst being a solid and easily-recognisable, it isn't clever-clever. I had personally been idly doodling some kind of swooshy thing before, with a snake's head forming a P and its forked tongue a Y coming out of it, but in retrospect it was just trying too hard. The plus-tadpoles' simplicity appeals to me. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: New-style Python icons
Luis M. González wrote: > This is strange... I've been trying to access this site since > yesterday, but I couldn't Might it be possible you have malware installed? Since I do a bunch of anti-spyware work, there are a few different bits of malware that try to block doxdesk.com, usually using a Hosts file hijack. Try it with the IP address 64.251.25.168 instead - if that works you should probably investigate your Hosts file and/or look at spyware removers. -- Andrew Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: New-style Python icons
Fredrik Lundh wrote: > could you perhaps add an SVG version ? Yes. I'll look at converting when I've used them a bit and am happy with them. I think some of the higher-level Xara effects may not convert easily to SVG but I'm sure there'll be workarounds of some sort. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Uploading files from IE
AB wrote: > I tried the following with the same result: > myName = ulImage.filename > newFile = file (os.path.join(upload_dir, os.path.basename(myName)), 'wb') os.path is different on your system to the uploader's system. You are using Unix pathnames, with a '/' separator - they are using Windows ones, with '\', so os.path.basename won't recognise them as separators. Old-school-Macintosh and RISC OS machines have different path separators again. The Content-Disposition filename parameter can be set by the user-agent to *anything at all*. Using it without some serious sanitising beforehand is a recipe for security holes. In your original code an attacker could have arbitrarily written to any file the web user had access to. The code with os.path.basename is better but could still be confused by things like an empty string, '.', '..' or invalid characters. It's best not to use any user-submitted data as the basis for filenames. If you absolutely *must* use Content-Disposition as a local filename you must send it through some strict checking first, whether the browser sends full paths to you or not. -- And Clover mailto:[EMAIL PROTECTED] http://www.doxdesk.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI redirection: let us discuss it further
Sullivan WxPyQtKinter wrote: > 1. Are there any method (in python of course) to redirect to a web page > without causing a "Back" button trap... rather than the redirection page > with a "Location: url" head What's wrong with the redirection page? If there's really a necessary reason for not using an HTTP redirect (for example, needing to set a cookie, which doesn't work cross-browser on redirects), the best bet is a page containing a plain link and
Buy Genuine Google Adsense Account only for Rs.200/- for indian people. For more details visit http://www.buygoogleadsense.tk/ We also provide procedure for creating unlimited google adsense account
Buy Genuine Google Adsense Account only for Rs.200/- for indian people. For more details visit http://www.buygoogleadsense.tk/ We also provide procedure for creating unlimited google adsense account trick . -- http://mail.python.org/mailman/listinfo/python-list
What to do if anything bites.
What to do if anything bites. Check our bites treatment at http://108ambulance.blogspot.com/2010/03/108-ambulance-home-page.html -- http://mail.python.org/mailman/listinfo/python-list
Trying to decide between PHP and Python
About once a year, I have to learn yet another programming language. Given all the recommendations (an outstanding accolade from Bruce Eckel, author of "Thinking in Java") I have set my aim to Python. Sounds kinda cool. The indentation-as-block is unique, but that is how I always indent, anyway. Can any of you nice folks post a snippet of how to perform a listing of the current directory and save it in a string? Something like this: $ setenv FILES = `ls` Bonus: Let's say that I want to convert the names of the files to lowercase? As 'tolower()' TIA, -Ramon -- http://mail.python.org/mailman/listinfo/python-list
Re: opinion: comp lang docs style
On Jan 4, 12:24 pm, Xah Lee wrote: > a opinion piece. > > 〈The Idiocy of Computer Language > Docs〉http://xahlee.org/comp/idiocy_of_comp_lang.html > > -- > The Idiocy of Computer Language Docs > > Xah Lee, 2011-01-03 > > Worked with Mathematica for a whole day yesterday, after about 10 > years hiatus. Very nice. Mathematica lang and doc, is quite unique. > Most other langs drivel with jargons, pettiness, comp-sci > pretentiousness, while their content is mathematically garbage. > (unixism mumble jumple (perl, unix), or “proper”-engineering OOP > fantasy (java), or impractical and ivory-tower adacemician idiocy as > in Scheme & Haskell ( currying, tail recursion, closure, call-cc, > lisp1 lisp2, and monad monad monad!)) (See: What are OOP's Jargons and > Complexities ◇ Language, Purity, Cult, and Deception.) > > Mathematica, in its doc, is plain and simple. None of the jargon and > pretention shit. Very easy to understand. Yet, some of its function's > technical aspects are far more scholarly abstruse than any other lang > (dealing with advanced math special functions that typically only a > few thousand people in the world understand.). > > -- > A Gander into the Idiocies > > Here's a gander into the doc drivel in common langs. > > -- > unix > > In unix man pages, it starts with this type of garbage: > > SYNOPSIS > gzip [ -acdfhlLnNrtvV19 ] [-S suffix] [ name ... ] > gunzip [ -acfhlLnNrtvV ] [-S suffix] [ name ... ] > zcat [ -fhLV ] [ name ... ] > > SYNOPSIS > zip [-aabcddeeffghjkllmoqrrstuvvwx...@$] [-- > longoption ...] [-b path] [-n suf > fixes] [-t date] [-tt date] [zipfile [file ...]] [-xi > list] > > Here, the mindset of unix idiots, is that somehow this “synopsis” form > is technically precise and superior. They are thinking that it > captures the full range of syntax in the most concise way. In > practice, it's seldomly read. It's actually not accurate as one'd > thought; no program can parse it and agree with the actual behavior. > It's filled with errors, incomprehensible to human. Worse of all, the > semantic of unix software's options are the worst rape to any possible > science in computer science. See: The Nature of the Unix Philosophy ◇ > Unix Pipe As Functional Language ◇ Unix zip Utility Path Problem. > > -- > Python > > In Python, you see this kinda garbage: > > 7.1. The if statement > > The if statement is used for conditional execution: > if_stmt ::= "if" expression ":" suite > ( "elif" expression ":" suite )* > ["else" ":" suite] > > (Source docs.python.org) > > Here, the mindset of the python idiots is similar to the unix tech > geekers. They think that using the BNF notation makes their doc more > clear and precise. The fact is, there are so many variations of BNF > each trying to fix other's problem. BNF is actually not used as a > computer language for syntax description. It's mostly used to > communicate syntax to humans. Like regex, there are so many > variations. But worse than regex in the sense that there are actually > not many actual implementations of BNF. Real word syntax description > language are usually nothing close to BNF. See: Pattern Matching vs > Lexical Grammar Specification. > > This incomprehensible BNF notation is the only thing you get if you > want to know the basic syntax of “if”, “for”, “while”, “lambda”, or > other basic constructs of python. > > -- > Perl > > In perl, you see this type of drivel: > > A Perl program consists of a sequence of declarations and > statements which run from the top to the bottom. Loops, subroutines > and other control structures allow you to jump around within the code. > > Perl is a free-form language, you can format and indent it however > you like. Whitespace mostly serves to separate tokens, unlike > languages like Python where it is an important part of the syntax. > > Many of Perl's syntactic elements are optional. Rather than > requiring you to put parentheses around every function call and > declare every variable, you can often leave such explicit elements off > and Perl will figure out what you meant. This is known as Do What I > Mean, abbreviated DWIM. It allows programmers to be lazy and to code > in a style with which they are comfortable. > > Perl borrows syntax and concepts from many languages: awk, sed, C, > Bourne Shell, Smalltalk, Lisp and even English. Other languages have > borrowed syntax from Perl, particularly its regular expression > extensions. So if you have programmed in another language you will see > familiar pieces in Perl. They often work the same, but see perltrap > for information about how they differ. > > (Source perldoc.perl.org) > > Notice they introduced you to their lingo “DWIM”. Juvenile humor is a
Re: Troll Alert (do not click)
On Jan 4, 8:19 am, SHILPA wrote: > UNSEEN HOT SEXY PHOTOS > http://karomasti9.blogspot.com/2011/01/never.html > SEXY DIYA MIRZA > http://karomasti9.blogspot.com/2010/12/diya-mirza.html > HOT AISHWARIYA > RAIhttp://karomasti9.blogspot.com/2010/12/aish.html > priyamani hot&sexy > photoshttp://karomasti9.blogspot.com/2010/12/priyamani.html > KATRINA SEXY > PHOTOShttp://karomasti9.blogspot.com/2010/12/katrina.html > ANUSHKA HOT > PHOTOShttp://karomasti9.blogspot.com/2010/12/anuska.html > BEAUTIFUL AISHWARIYA > RAIhttp://karomasti9.blogspot.com/2010/12/aiesh.html > TRISHA HOT > PHOTOShttp://karomasti9.blogspot.com/2010/11/trisha-hot.html > AMISHAPATEL HOT > VIDEOShttp://karomasti9.blogspot.com/search/label/amisha > HANSIKHA HOT SEXY > PHOTOShttp://karomasti9.blogspot.com/search/label/HANSIKA > HOT SEXY COLLEGE > GIRLShttp://karomasti9.blogspot.com/search/label/hot > BEAUTIFUL > LARADATTAhttp://karomasti9.blogspot.com/search/label/laradatta > NIKISHA HOT > BOOBShttp://karomasti9.blogspot.com/search/label/nikisha > PRIYANKA SPICY LATEST > PICShttp://karomasti9.blogspot.com/search/label/priyanka > ONLY FOR YOUTHhttp://karomasti9.blogspot.com/search/label/spicy > SRILEKHA UNSEENED > PHOTOShttp://karomasti9.blogspot.com/search/label/Srilekha > CHOPRA UNBELIVABLE > PHOTOShttp://karomasti9.blogspot.com/search/label/chopra > HOT BIPASA BASU > PHOTOShttp://karomasti9.blogspot.com/search/label/bipasa > TRISHA IN A SEXY > FEELhttp://karomasti9.blogspot.com/search/label/thrisha > SRISHA HOT BOOBS > SHOWhttp://karomasti9.blogspot.com/search/label/srisha > BEAUTIFUL POONAM > PHOTOShttp://karomasti9.blogspot.com/search/label/poonam -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to decide between PHP and Python
On Jan 4, 2:29 pm, Dan M wrote:
> On Tue, 04 Jan 2011 12:20:42 -0800, Google Poster wrote:
> > About once a year, I have to learn yet another programming language.
> > Given all the recommendations (an outstanding accolade from Bruce Eckel,
> > author of "Thinking in Java") I have set my aim to Python. Sounds kinda
> > cool.
>
> > The indentation-as-block is unique, but that is how I always indent,
> > anyway.
>
> > Can any of you nice folks post a snippet of how to perform a listing of
> > the current directory and save it in a string?
>
> > Something like this:
>
> > $ setenv FILES = `ls`
>
> > Bonus: Let's say that I want to convert the names of the files to
> > lowercase? As 'tolower()'
>
> > TIA,
>
> > -Ramon
>
> 1)
> import os
> files = ' '.join(os.listdir('/home/dan'))
>
> 2)
> import os
> import string
> files = string.lower(' '.join(os.listdir('/home/dan')))
>
> As to choice between Python and PHP, I would say learn anything but PHP.
> Even Perl has fewer tentacles than PHP.
Not to mention that it took me 9 minutes to get a reply from you...
Quite speedy community support.
That is a very important parameter in my technology decisions these
days.
Thanks!
-Ramon
--
http://mail.python.org/mailman/listinfo/python-list
Re: Trying to decide between PHP and Python
On Jan 4, 2:34 pm, Dan M wrote: > On Tue, 04 Jan 2011 12:32:28 -0800, Google Poster wrote: > > Not to mention that it took me 9 minutes to get a reply from you... > > Quite speedy community support. > > > That is a very important parameter in my technology decisions these > > days. > > > Thanks! > > > -Ramon > > This Usenet group is a truly awesome resource. My kinda place. :-) Now, I will embark on my learning journey the proper way: reading some Internet tutorials, instead of cheating like I just did. -Ramon -- http://mail.python.org/mailman/listinfo/python-list
Re: Trying to decide between PHP and Python
On Jan 4, 3:09 pm, Alex Willmer wrote:
> On Jan 4, 8:20 pm, Google Poster wrote:
>
> > Can any of you nice folks post a snippet of how to perform a listing
> > of the current directory and save it in a string?
>
> > Something like this:
>
> > $ setenv FILES = `ls`
>
> > Bonus: Let's say that I want to convert the names of the files to
> > lowercase? As 'tolower()'
>
> I'd just like to mention one more python nicety: list comprehension.
> If you wanted the filenames as a list of strings, with each made
> lowercase then the following would serve well:
>
> import os
> filenames = os.listdir('.')
> filenames_lower = [fn.lower() for fn in filenames]
>
> You could also combine this into one line:
>
> import os
> filenames_lower = [fn.lower() for fn in os.listdir('.')]
>
> Regards, Alex
The syntax reminds me of Lots of Interspersed Silly Parentheses
(L.I.S.P.), but without the parentheses.
:-)
-Ramon
--
http://mail.python.org/mailman/listinfo/python-list
Re: Would like to add an "upload" facility to my web site
On Jan 31, 11:36 am, Luis M. González wrote: > On Jan 31, 1:50 pm, Ramon F Herrera wrote: > > > > > On Jan 31, 10:49 am, Ramon F Herrera wrote: > > > > (newbie alert) > > > > This is what I have so far: > > > >http://patriot.net/~ramon/upload_facility.html > > > > The code is shown below. It seems I need that actual script that > > > performs the file transfer. I would prefer it in Python. > > > > TIA, > > > > -Ramon > > > > --- > > > > > > > > > > > > > > > > Name of file to be uploaded: > > > > > > > > > > > > > > > > > > > > > > > > IMPORTANT Bonus question: > > > Where should I post this type of question about writing stuff for the > > web > > > -Ramon > > I guess this question is framework specific. No. > Are you using any framework (django, pylons, etc...)? Luis: I have a commercial shell account. I can only edit the directory ~ramon/public_html. I published one file already, I need the other. Gracias! -Ramon -- http://mail.python.org/mailman/listinfo/python-list
Re: Would like to add an "upload" facility to my web site
On Jan 31, 11:36 am, Luis M. González wrote: > On Jan 31, 1:50 pm, Ramon F Herrera wrote: > > > > > On Jan 31, 10:49 am, Ramon F Herrera wrote: > > > > (newbie alert) > > > > This is what I have so far: > > > >http://patriot.net/~ramon/upload_facility.html > > > > The code is shown below. It seems I need that actual script that > > > performs the file transfer. I would prefer it in Python. > > > > TIA, > > > > -Ramon > > > > --- > > > > > > > > > > > > > > > > Name of file to be uploaded: > > > > > > > > > > > > > > > > > > > > > > > > IMPORTANT Bonus question: > > > Where should I post this type of question about writing stuff for the > > web > > > -Ramon > > I guess this question is framework specific. > Are you using any framework (django, pylons, etc...)? Luis, Allow me to make this more clear. I have my own servers, all of them running Linux. I have been Linux sysadmin for more time than I care to remember. However, I (on purpose) provided an example hosted at my commercial shell provider. That was a deliberate decision, because I am looking for the simplest possible solution. I guess the question now is: Do I need root access to uploads files?? Gracias, -Ramon -- http://mail.python.org/mailman/listinfo/python-list
Help the visibility of Python in computational science
Hi everyone, There is currently a competition running that could help give Python in computational science a bit of visibility. The competition is for the most popular recently published article on the Scholarpedia website, one of which is about a Python package "Brian" for computational neuroscience simulations. If you could take one minute to make sure you are signed in to your Google+ account and click the g+1 icon near the top right of the page, it has a chance of winning the competition. Here's the link to the article: http://www.scholarpedia.org/article/Brian_simulator Full disclosure, I'm the first author of that article, and I'd be happy to win the competition too. :) More details: Scholarpedia is an alternative to wikipedia with slightly tighter control: contributions only allowed from scholars, etc. "Brain Corporation" is offering $1 in prizes to the top 3 most popular entries published between last October and this June based on google +1 votes. It's a bit of a silly popularity contest because of this, but I still think it would be great if a Python based thing could win it. "Brian" is a package I wrote (with several others) to do simulations of spiking neural networks in Python. Read the article if you want to know more! :) Thanks all for your attention, Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Help the visibility of Python in computational science
On Thursday, January 31, 2013 10:06:44 PM UTC-5, Terry Reedy wrote: > On 1/31/2013 8:05 PM, [email protected] wrote: > > Here's the link to the article: > > http://www.scholarpedia.org/article/Brian_simulator > > 'Brian' is obviously a play on 'brain', with two letters transposed. But > > comparison of the logo on the page above with the image on > > https://en.wikipedia.org/wiki/Life_of_brian > > shows another source ;-). > Pure coincidence I assure you, I'm just very stuck in the old days of web design with 3D text logos. ;) Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Help the visibility of Python in computational science
On Friday, February 1, 2013 12:09:04 AM UTC-5, Chris Angelico wrote: > On Fri, Feb 1, 2013 at 4:00 PM, Steven D'Aprano > > wrote: > > > [email protected] wrote: > > > > > >> If you could take one minute to make sure you > > >> are signed in to your Google+ account > > > > > > Which Google+ account would that be? I have so few. > > > > > > > It's a thing non-nerds do, Steven. You wouldn't understand. Sadly for me though, I think the nerds are in the majority here. As of yesterday I got only two additional +1's. Ah well. I had to create a Google+ account for it myself actually. ;) Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: (SPAM: 50) Mail Delivery (failure [email protected]) (VIRUS REMOVED)
We want to thank you for your interest in joining the Google team. We received your email inquiry and look forward to the opportunity to review your background and experience. Unfortunately, we are unable to give a personal reply to every applicant. However, please know that we do review all resumes by hand so it takes us just a little bit longer to get back to applicants we feel might be a fit for one of our positions. If you do not hear from one of us, we may not have a position available for you at this time. We thank you for your patience and want to thank you again for your interest in Google. Google Staffing http://www.google.com/jobs.html -- http://mail.python.org/mailman/listinfo/python-list
Magic function
Hi all,
I'm part of a small team writing a Python package for a scientific
computing project. The idea is to make it easy to use for relatively
inexperienced programmers. As part of that aim, we're using what we're
calling 'magic functions', and I'm a little bit concerned that they
are dangerous code. I'm looking for advice on what the risks are (e.g.
possibility of introducing subtle bugs, code won't be compatible with
future versions of Python, etc.).
Quick background: Part of the way our package works is that you create
a lot of objects, and then you create a new object which collects
together these objects and operates on them. We originally were
writing things like:
obj1 = Obj(params1)
obj2 = Obj(params2)
...
bigobj = Bigobj(objects=[obj1,obj2])
bigobj.run()
This is fine, but we decided that for clarity of these programs, and
to make it easier for inexperienced programmers, we would like to be
able to write something like:
obj1 = Obj(params1)
obj2 = Obj(params2)
...
run()
The idea is that the run() function inspects the stack, and looks for
object which are instances of class Obj, creates a Bigobj with those
objects and calls its run() method.
So, any comments on that approach?
I'm including the code I've written to do this, and if you have time
to look through it, I'd also be very grateful for any more specific
comments about the way I've implemented it (in particular, can it be
made faster, is my program creating cycles that stop the garbage
collection from working, etc.). I hope the code will be formatted
correctly:
def
getInstances(instancetype,level=1,includeglobals=True,containersearchdepth=1,exclude={},predicate=lambda
x:True):
"""Find all instances of a given class at a given level in the
stack
"""
vars = {}
# Note: we use level+1 because level refers to the level relative
to the function calling this one
if includeglobals: vars.update(stack()[level+1][0].f_globals)
vars.update(stack()[level+1][0].f_locals)
# Note that you can't extract the names from vars.itervalues() so
we provide via knownnames the names vars.iterkeys(),
# containersearchdepth+1 is used because vars.itervalues() is the
initial container from the point of view of this
# function, but not from the point of view of the person calling
getInstances
objs, names =
extractInstances(instancetype,vars.itervalues(),containersearchdepth
+1,knownnames=vars.iterkeys(),exclude=exclude,predicate=predicate)
return (objs,names)
def
extractInstances(instancetype,container,depth,containingname='vars()',knownnames=None,exclude={},predicate=lambda
x:True):
if depth<=0: return ([],[])
if isinstance(container,str): return ([],[]) # Assumption: no need
to search through strings
# Ideally, this line wouldn't be here, but it seems to cause
programs to crash, probably because
# some of the simulator objects are iterable but shouldn't be
iterated over normally
# TODO: Investigate what is causing this to crash, and possibly
put in a global preference to turn this line off?
if not isinstance(container,
(list,tuple,dict,type({}.itervalues(: return ([],[])
# Note that knownnames is only provided by the initial call of
extractInstances and the known
# names are from the dictionary of variables. After the initial
call, names can only come from
# the __name__ attribute of a variable if it has one, and that is
checked explicitly below
if knownnames is None:
knewnames = False
knownnames = repeat(containingname)
else:
knewnames = True
objs = []
names = []
try: # container may not be a container, if it isn't, we'll
encounter a TypeError
for x,name in zip(container,knownnames):
# Note that we always have a name variable defined, but if
knewnames=False then this is just
# a copy of containingname, so the name we want to give it
in this instance is redefined in this
# case. We have to use this nasty check because we want to
iterate over the pair (x,name) as
# variables in the same position in the container have the
same name, and we can't necessarily
# use __getitem__
if hasattr(x,'__name__'): name = x.__name__
elif not knewnames: name = 'Unnamed object, id =
'+str(id(x))+', contained in: '+containingname
if isinstance(x,instancetype):
if x not in exclude and predicate(x):
objs.append(x)
names.append(name)
else: # Assumption: an object of the instancetype is not
also a container we want to search in.
# Note that x may not be a container, but then
extractInstances will just return an empty list
newobjs, newnames =
extractInstances(instancetype,x,depth-1,containingname=name,predicate=predicate)
objs += newobjs
names += newnames
return (objs,names)
except: # if we encounter a TypeError from
Re: Magic function
Thanks everyone for the comments. I had previously thought about the possibility of the classes keeping track of their instances. I guess this could probably be done quite transparently with a decorator too (as we have many different types of objects being collected together). The only issue is that this approach forces you to use what are essentially global variables, whereas the searching through the stack method allows you to use the structure of the program to organise what objects each 'magic' function sees. Is this a good idea or not? I'm not entirely sure. I think that personally I would lean towards using this method of classes keeping track of their instances. It's not entirely my decision so I'll see what the others say about it. Any comments on this possibility: classes could keep track of their instances, and also keep track of which function or module the instances were defined in. Then, the magic functions could pick out objects defined in the same function or module rather than looking at the stack. This would achieve a similar thing, but is there any great advantage in doing it this way? My first thought is that you'd still have to go digging around in the stack to do this, but just not as much. Also, does anyone know of any specific things I should be aware of in taking this stack searching approach? I'm thinking of, for example, any planned changes in the execution model of Python or the inspect.stack() function in the next version of Python. Paul, "Your users are *scientists*, and you don't trust their intellectual ability to learn a programming language as simple as Python?" Well, it's not quite as simple as that. One thing is that we're not going to be able to force people to use our package. We believe it's going to be considerably better - particularly in terms of ease of use and extensibility - than the existing alternatives, but one of the factors that will affect how many people start using it is how simple we can make it to do basic things that they'll be familiar with. Many scientists are using Python now, but it's not yet quite well known enough that we can just assume that people will know it, and having to learn the details of a new programming language is a considerable disincentive for someone thinking about switching to a new piece of software (even if, as you say, Python is not the most difficult language to learn). Although the difference between the two pieces of hypothetical code I presented seems quite trivial to an experienced programmer, I think that the clarity and simplicity of the version that uses the magic functions might make a difference. The difference between being able to define and run a model with 10 lines or 20-30 lines of code might, somewhat perversely, be a significant factor. (The example I gave was simplified to illustrate what was going on, but the actual situation is more like you have 5 or 6 different types of object, each of which uses other types of object to initialise themselves, so that the magic function approach really reduces the length of the program considerably.) So, there's an aspect of PR about our wanting to have something like the magic functions, but it's not entirely about self promotion, because we think that in the long term it will be better for the users if they switch to using our package (or something like it). The reason being that the alternatives available at the moment all use their own custom made programming languages which have nothing like the power of a well developed general purpose language like Python, and are much more difficult to use and extend. One of them is a stack based language of all things! Carl, "Even if you implement magic functions, don't get rid of the straightforward "hard way"." Absolutely not! A very good point. In fact, the magic functions don't actually do any work themselves, they just create and call the 'hard way' functions (which are still visible to the user). They're an additional layer of abstraction which you can choose to use or not use. And actually, there will be situations where there is no alternative but to use the 'hard way'. We already learnt this lesson: a couple of our magic functions were behaving differently and causing some odd behaviour, so we changed them and now we're working on building a more consistent and explicit interface (and enforcing it works as expected with the unit testing module, a tedious but hopefully very useful exercise in the long run). -- http://mail.python.org/mailman/listinfo/python-list
Re: Magic function
Hi Rüdiger, Thanks for your message. I liked your approach and I've been trying something along exactly these sorts of lines, but I have a few problems and queries. The first problem is that the id of the frame object can be re-used, so for example this code (where I haven't defined InstanceTracker and getInstances, but they are very closely based on the ideas in your message): class A(InstanceTracker): gval = 0 def __init__(self): self.value = A.gval # each time you make a new object, give A.gval += 1 # it a value one larger def __repr__(self): return str(self.value) def f2(): a = A() # objects 0 and 2 return getInstances(A) def f3(): a = A() # object 1 return f2() inst2 = f2() inst3 = f3() print inst2 print inst3 The output is: [0] [0, 2] The A-variable with value 0 is not being garbage collected because it's saved in the variable inst2, but it's also being returned by the second call to getInstances because the frame of f2 is the same each time (which makes sense, but may be implementation specific?). The same problem doesn't exist when you use the stack searching method because from f2's point of view, the only bound instance of A is the one in that particular call of f2. If you had at the end instead of the inst2, inst3 stuff: print f2() print f3() The output is: [0] [2] Again, I guess this because A with value 0 is being garbage collected between print f2() and print f3(), but again I think this is implementation specific? You don't have a guarantee that this object will be garbage collected straight away do you? So my concern here is that this approach is actually less safe than the stack based approach because it depends on implementation specific details in a non-straightforward way. That said, I very much like the fact that this approach works if I write: a = [A()] a = [[A()]] etc. To achieve the same thing with the stack based approach you have to search through all containers to (perhaps arbitrary) depth. I also have another problem which is that I have a function decorator which returns a callable object (a class instance not a function). Unfortunately, the frame in which the callable object is created is the frame of the decorator, not the place where the definition is. I've written something to get round this, but it seems like a bit of a hack. Can anyone suggest an approach that combines the best of both worlds, the instance tracking approach and the stack searching approach? Or do I need to just make a tradeoff here? Thanks again for all your help everyone, Dan Goodman -- http://mail.python.org/mailman/listinfo/python-list
Just for fun: Countdown numbers game solver
Ever since I learnt to program I've always loved writing solvers for
the Countdown numbers game problem in different languages, and so now
I'm wondering what the most elegant solution in Python is.
If you don't know the game, it's simple: you're given six randomly
chosen positive integers, and a target (another randomly chosen
positive integer), and you have to make the target using only the
numbers you're given, and +,-,* and / (and any number of brackets you
like). You're not allowed fractions as intermediate values. So, given
2, 3 and 5 say, and a target of 21, you could do (2+5)*3 = 21.
So what's the best algorithm? And, what's the most elegant way to code
it in Python? I've posted my most elegant version below (I have a
faster version which is slightly less elegant). Can anyone do better?
Refs:
* This academic paper describes an implementation of an algorithm to
solve the problem in Haskell. I found it after I'd written mine but it
uses a very similar algorithm. http://www.cs.nott.ac.uk/~gmh/countdown.pdf
* My web page where I put both versions of my code:
http://thesamovar.net/countdownnumbers
* The web page of the TV show the problem is based on:
http://www.channel4.com/entertainment/tv/microsites/C/countdown/index.html
My version:
class InvalidExpressionError(ValueError):
pass
subtract = lambda x,y: x-y
def add(x,y):
if x<=y: return x+y
raise InvalidExpressionError
def multiply(x,y):
if x<=y or x==1 or y==1: return x*y
raise InvalidExpressionError
def divide(x,y):
if not y or x%y or y==1:
raise InvalidExpressionError
return x/y
add.display_string = '+'
multiply.display_string = '*'
subtract.display_string = '-'
divide.display_string = '/'
standard_operators = [ add, subtract, multiply, divide ]
class Expression(object): pass
class TerminalExpression(Expression):
def __init__(self,value,remaining_sources):
self.value = value
self.remaining_sources = remaining_sources
def __str__(self):
return str(self.value)
def __repr__(self):
return str(self.value)
class BranchedExpression(Expression):
def __init__(self,operator,lhs,rhs,remaining_sources):
self.operator = operator
self.lhs = lhs
self.rhs = rhs
self.value = operator(lhs.value,rhs.value)
self.remaining_sources = remaining_sources
def __str__(self):
return '('+str(self.lhs)+self.operator.display_string
+str(self.rhs)+')'
def __repr__(self):
return self.__str__()
def
ValidExpressions(sources,operators=standard_operators,minimal_remaining_sources=0):
for value, i in zip(sources,range(len(sources))):
yield TerminalExpression(value=value,
remaining_sources=sources[:i]+sources[i+1:])
if len(sources)>=2+minimal_remaining_sources:
for lhs in
ValidExpressions(sources,operators,minimal_remaining_sources+1):
for rhs in ValidExpressions(lhs.remaining_sources,
operators, minimal_remaining_sources):
for f in operators:
try: yield BranchedExpression(operator=f, lhs=lhs,
rhs=rhs, remaining_sources=rhs.remaining_sources)
except InvalidExpressionError: pass
def TargetExpressions(target,sources,operators=standard_operators):
for expression in ValidExpressions(sources,operators):
if expression.value==target:
yield expression
def FindFirstTarget(target,sources,operators=standard_operators):
for expression in ValidExpressions(sources,operators):
if expression.value==target:
return expression
raise IndexError, "No matching expressions found"
if __name__=='__main__':
import time
start_time = time.time()
target_expressions = list(TargetExpressions(923,[7,8,50,8,1,3]))
target_expressions.sort(lambda x,y:len(str(x))-len(str(y)))
print "Found",len(target_expressions),"solutions, minimal string
length was:"
print target_expressions[0],'=',target_expressions[0].value
print
print "Took",time.time()-start_time,"seconds."
--
http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
Hi Marek, That's a really nice solution (and ultrafast). Unfortunately I realise I stated the problem imprecisely. You're only allowed to use each number once (otherwise there's a trivial solution for every problem, i.e. x/x + x/x + x/x + ... + x/x repeated y times for target y given any source number x). Trying your program on 234 from [100,9,7,6,3,1] gives you 9*9*3-9 using the 9 three times. Does your solution adjust to deal with this additional requirement? At first I thought it would be an easy fix, but maybe it's a little more complicated than I thought... Dan Goodman -- http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
Hi all, It's great how many different sorts of solutions (or almost solutions) this puzzle has generated. Speedwise, for reference my solution posted above takes about 40 seconds on my 1.8GHz laptop, and the less elegant version (on my webpage linked to in the original post) takes about 15 seconds. It seems to me like surely this problem can be more efficiently solved than that? My version isn't very Pythonic (it could almost be written in C++ the way I've done it) so I liked the idea of the first solution, but I don't think it can be fixed. I adapted it so that it doesn't use the same number more than once, but it still has some problems. Firstly, it only finds solution ((a op b) op c) op d etc. and won't find (for example (1+2)*(3+4). Secondly, it stores a dictionary value->how to get to value which is fine if you can re-use numbers because one way to get to a given value is as good as another, but sometimes you can get to the same number in two different ways using different numbers, so it misses solutions. Paul: 758 = 8+(5*((2+4)*25)) Arnaud: I haven't had time to play with your solution yet - how quick does it run? My fantasy is that there is a solution that isn't TOO slow where you can just look at the code and go 'Oh yes, of course that works!' and understand it immediately. Maybe that's too much to ask even of Python! ;-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
Decided I may as well post my other solution while I'm at it. The neat
trick here is redefining the add, mul, etc. functions so that they
raise exceptions for example if x>y then add(x,y) raises an exception
which is handled by the search algorithm to mean don't continue that
computation - this stops you from having to evaluate x+y AND y+x, etc.
sub = lambda x,y:x-y
def add(x,y):
if x<=y: return x+y
raise ValueError
def mul(x,y):
if x<=y or x==1 or y==1: return x*y
raise ValueError
def div(x,y):
if not y or x%y or y==1:
raise ValueError
return x/y
add.disp = '+'
mul.disp = '*'
sub.disp = '-'
div.disp = '/'
standard_ops = [ add, sub, mul, div ]
def strexpression(e):
if len(e)==3:
return '('+strexpression(e[1])+e[0].disp+strexpression(e[2])
+')'
elif len(e)==1:
return str(e[0])
# I don't like this function, it's nice and short but is it clear
# what it's doing just from looking at it?
def expressions(sources,ops=standard_ops,minremsources=0):
for i in range(len(sources)):
yield ([sources[i]],sources[:i]+sources[i+1:],sources[i])
if len(sources)>=2+minremsources:
for e1, rs1, v1 in expressions(sources,ops,minremsources+1):
for e2, rs2, v2 in expressions(rs1,ops,minremsources):
for o in ops:
try: yield ([o,e1,e2],rs2,o(v1,v2))
except ValueError: pass
def findfirsttarget(target,sources,ops=standard_ops):
for e,s,v in expressions(sources,ops):
if v==target:
return strexpression(e)
return None
print findfirsttarget(923,[7,8,50,8,1,3])
gives:
((7*(((8*50)-1)/3))-8)
Dan Goodman
--
http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
Arnaud and Terry, Great solutions both of you! Much nicer than mine. I particularly like Arnaud's latest one based on folding because it's so neat and conceptually simple. For me, it's the closest so far to my goal of the most elegant solution. So anyone got an answer to which set of numbers gives the most targets from 100 onwards say (or from 0 onwards)? Is Python up to the task? A thought on that last one. Two ways to improve speed. First of all, you don't need to rerun from scratch for each target. Secondly, you can try multiple different sets of numbers at the same time by passing numpy arrays instead of single values (although you have to give up the commutativity and division by zero optimisations). Dan Goodman -- http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
Well I tried the NumPy array thing that I was talking about, to parallelise the problem, and there were some difficulties with it. Firstly, the pruning makes a really big difference to the speed, and you don't get that if you're trying to parallelise the problem because what is an equivalent calculation for one set of numbers is obviously not for another set. I think this problem disappears when you consider very large sets of numbers though (where the cost of doing equivalent computations vanishes in comparison to the alternative cost of starting up the whole recursive computation from scratch many times). The second problem is that you can't weed out division by zero and intermediate fractions. I haven't looked at the internals of how NumPy deals with these though, so this might be fixable or it might not. For my part, I'd consider the following equivalences to be right for defining equivalent expressions (where here a, b and c are any subexpression, and 1 and 0 means any subexpression that evaluates to 1 and 0). Commutativity: a*b <-> b*a a+b <-> b+a Associativity: (a+b)+c <-> a+(b+c) (a+b)-c <-> a+(b-c) (a-b)+c <-> a-(b-c) (a-b)-c <-> a-(b+c) (a*b)*c <-> a*(b*c) (a*b)/c <-> a*(b/c) (a/b)*c <-> a/(b/c) (a/b)/c <-> a/(b*c) Units (1 is multiplicative unit, 0 is additive unit): a*1 <-> a a/1 <-> a a+0 <-> a a-0 <-> a Substitution (swapping equal starting numbers is equivalent): expr(a,b,c,...,z) <-> expr(s(a,b,c,...,z)) where a,b,c,...,z are the original numbers given and s is a permutation of (a,b,c...,z) so that (a,b,c,...z) evaluates to the same thing as s(a,b,c,...,z) or equivalently, expr1 <-> expr2 if str(expr1)==str(expr2) Then, any two expressions which can be transformed into one another by the equivalences above are equivalent. Commutativity and units can be easily implemented as you go and most of the programs suggested so far do this, by for example only allowing a*b or a+b if a>=b, and not allowing a*1 or 1*a, etc. Substitution can be implemented by just taking set(map(str,expressions)) at the end. The associativity ones are a little more tricky to implement, but Arnaud's idea of the fully ordered binary tree seems to do it. Another way of saying it is that any subexpression consisting only of + and - operations should be reduced to a+b+c+...-z-y-x- where a>b>c>... and z>y>x>... (and similarly for an expression involving only * and /). Terry, I'd also be interested in a copy of your stack simulation code, btw. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python self-evaluating strings
It's a bit cheap, but how about >>> from inspect import getsource >>> print getsource(getsource) or similarly def f(g): import inspect return inspect.getsource(g) print f(f) Dan -- http://mail.python.org/mailman/listinfo/python-list
explicit protocols and duck typing
Hi all, As I understand it, the idea behind duck typing is that you just take an object and if it has the methods you want to use you use it assuming it to be the right type of object. I'm interested in extending this idea a bit, but I feel the sort of thing I have in mind has already been thought of. So for example, in the program I'm writing a 'state variable' specifier can be either an integer or a string (the string name is mapped to an integer by another function getvarindex(name)). In this case, I can't do duck typing by seeing if the object has a method or not, because both of the types are built in types. I don't want to have to force the user to have objects like StateVariableSpecifier(name). Now at the moment, what I'm doing is accepting anything as a state variable specifier, and just passing it through the getvarindex function when I want to use it. This sort of specifies a protocol for state variable specifiers without making it explicit (like the sequence or mapping protocols built in to Python). What I'm wondering though is whether there is any value in making this more explicit? Say, have a class which makes explicit the various relationships involved, such as that the type of a state variable specifier can be correct or incorrect (it has to be an int or a string), that the value has to be correct (the integer has to be between 0 and n for some n, and the string has to be in a dict of names), and that there is a relationship between state variable specifiers (int, string) and the underlying data type (the index of the variable in an array). Making it more explicit seems like a good idea, the question is in what way to make it more explicit. I can make it explicit just by documenting the behaviour, or I can make it explicit by adding code that enforces certain ways of using things. For this simple example, it seems like just documenting it is the best route, but I have similar issues with other more complicated parts of the code. At the moment, a model for instance can be a Model object, an Equation object or a tuple of functions, but this could be subject to change in the future. The issue I want to address is the long term maintainability of the code when possibly many people might be contributing, the transparency for other users, and the ease of documenting it. Any opinions? Dan Goodman -- http://mail.python.org/mailman/listinfo/python-list
Adding properties to an instance
Hi all,
So I understand that properties belong to a class not an instance, but
nonetheless I want to add properties to an instance. I have a class
which when an instance is created runs some fairly complicated code
and produces a set of names which I'd like to be able to access via
properties. At the moment, I'm using something like obj.getvar(name)
but I'd like to be able to write obj.name. (Note that they can't be
just standard attributes because they only get computed when they are
accessed.) I could generate functions like obj.name() but I want it to
be obj.name instead.
The solution I've come up with is to create a new class for each
object which is just the real class with some extra properties, and
then dynamically change the class of the object to this new class.
This seems to work, but I wonder if (a) there is a nicer solution than
the one I'll post below, (b) if there are any dangers or pitfalls of
this approach. The obvious difficulty is with derived classes. At the
moment, I'm insisting that a derived class has to call a makeprops()
method to create the properties.
It's kind of similar to this recipe:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/197965
but that recipe has a much simpler situation in which the properties
and values are known at the time of the creation of the object (by
contrast, I don't know what the properties are until the end of the
__init__ method).
Any thoughts?
Code below to illustrate my approach.
import warnings
from operator import itemgetter
class A(object):
def __init__(self,**kwds):
self._kwds = kwds
self.makeprops()
def __getitem__(self,i):
return self._kwds[i]
def makeprops(self):
if not hasattr(self,'_madeprops'):
self._madeprops = set()
self._failedprops = set()
class _A(self.__class__):
pass
for k,v in self._kwds.items():
if not k in self._madeprops and k in dir(self):
if not k in self._failedprops:
warnings.warn("Cannot create property "+k+",
already used in object "+str(self),RuntimeWarning)
self._failedprops.add(k)
else:
setattr(_A,k,property(fget=itemgetter(k)))
self._madeprops.add(k)
self.__class__ = _A
class B(A):
def __init__(self,**kwds):
super(B,self).__init__(**kwds)
self.makeprops()
class C(A):
def __init__(self,**kwds):
self._kwds = kwds
a = A(x=1)
b = B(x=2,makeprops=3)
c = C(x=3)
print isinstance(a,A), isinstance(a,B), isinstance(a,C) # True False
False
print isinstance(b,A), isinstance(b,B), isinstance(b,C) # True True
False
print isinstance(c,A), isinstance(c,B), isinstance(c,C) # True False
True
print a.__class__ #
print b.__class__ #
print c.__class__ #
print a.x # 1
print b.x # 2
print b.makeprops # >
try:
print c.x # raises exception
except AttributeError:
print "c has no element x"
c.makeprops()
print c.x # 3
print a.__class__ #
print b.__class__ #
print c.__class__ #
---
Dan Goodman
http://thesamovar.net/contact
--
http://mail.python.org/mailman/listinfo/python-list
Re: Adding properties to an instance
On Feb 6, 10:54 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> I'd suggest a small improvement: _A as a class name isn't very nice.
> Replace the inner class statement with:
> _A = type(self.__class__.__name__ + '_autoprops', (self.__class__,), {})
Ah yes, that's much nicer.
> A problem with this approach is that instances aren't pickleable (perhaps
> that could be solved using __reduce__)
I think because the properties can be computed from knowing the rest
of the data of the object, it would be safe when pickling to just
pickle a copy of the object with the __class__ changed back to A (and
then when you load it again, you can just generate the properties
anew). I haven't really thought much about pickling but it would
certainly be a nice feature to have.
--
Dan
--
http://mail.python.org/mailman/listinfo/python-list
Re: Adding properties to an instance
On Feb 6, 11:09 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > While this is technically possible (I tried a couple years ago), it > requires hacking the __getattribute__ method, which is something I > would not recommand, not only because it can be tricky, but mostly > because this is a very critical path wrt/ perfs. (IIRC it also > required using custom descriptors, but I'm not really sure about this > last point). Performance is pretty important for the class I'm designing so I think __getattribute__ is probably out. The computed properties are only infrequently accessed, but using __getattribute__ slows everything down, right? > Before new-style classes, we used the __getattr__/__setattr__ hooks > for computed attributes. While this approach is now a bit abandonned > in favor of descriptors (properties or custom ones), it still works > fine, and is probably the best solution to your problem. Ah, I didn't know about these - it looks as though they might be just the thing since it seems they're only called after all the other methods fail. That looks like there would be no performance hit, I wouldn't need to mess around with dynamically changing the class, and it would automatically deal with the (irritating) feature of having to check if there is already something in the object's dir() with that name. I'll look into this tomorrow - I hope this feature isn't going to be removed in future versions of Python? -- Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding properties to an instance
> As a side note: the naming symetry between __getattr__ and __setattr__ > is a gotcha, since __setattr__ is mostly symetric to __getattribute__ - > IOW, customizing __setattr__ is a bit tricky. The naive approach, ie: Ah I see - so __setattr__ is called immediately whereas __getattr__ is only called if the other methods fail. Does this mean that __setattr__ incurs the same performance penalty that overriding __getattribute__ would? Possibly I can live with this because I think that most of what I'm doing is getting attributes, or modifying mutable ones, rather than setting them. -- Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: Adding properties to an instance
> > Does this mean that __setattr__ > > incurs the same performance penalty that overriding __getattribute__ > > would? > > Not quite AFAICT - there's less going on here. Also, getting an > attribute is (usually at least) more common than setting it. > > > Possibly I can live with this because I think that most of what > > I'm doing is getting attributes, or modifying mutable ones, rather > > than setting them. > > Well... Using the __setattr__/__getattr__ hooks is IMHO the simplest > solution that can possibly work - far simpler than your previous one at > least. As far as I'm concerned, and unless some other point of your > specs make this unusable or unpractical, I'd go for this solution first > and run a couple benchs on real-life-or-close-to conditions to check if > the performance hit is acceptable. I think you're right - I've just tried implementing a simple version of this in my code and it seems that in the time critical parts of it __setattr__ isn't called even once. So I think I'll go with this solution (after having run a few tests). Thanks for your suggestion! -- Dan -- http://mail.python.org/mailman/listinfo/python-list
How to tell if I'm being run from a shell or a module
Hi all, Is there any standard way to tell if the user is running from a module or from an interactive shell like IDLE or IPython? The best I've come up with so far is for a function to look at getouterframes(currentframe())[1][1] (the filename in the frame record of the frame that called the function), and check if it exists or not with os.path.exists. IPython gives '(ipython console)' and IDLE gives 'pyshell#0' whereas running from a module gives its filename. This seems a bit hacky. Any better ideas? -- Dan Goodman http://thesamovar.net/contact -- http://mail.python.org/mailman/listinfo/python-list
Re: How to tell if I'm being run from a shell or a module
Thanks for the replies, but it's not what I meant. What I want to be able to determine is whether or not the user is running from an interactive shell (like IPython or IDLE). Checking if __name__=='__main__' checks if the current module is the one being run, but suppose you have two modules A and B, with the function f defined in module B that should print 'Interactive' or 'Module' say. The module A just consists of: import B; B.f(). Now whenever f is called, __name__ will not be '__main__' for it. Someone using IDLE could write import B then B.f() too. The question is: is there a way for f to determine if someone was using an interactive shell to call it or if it was being called some other way. The way I came up with works in these limited cases but won't work in a more general situation (but perhaps there is no way for it to know in the more general situation). Dan On Feb 14, 7:01 pm, Chris <[EMAIL PROTECTED]> wrote: > If you're just trying to prevent some actions from happening if > something loads your script as a module just put the 'action items' > under an if like: > if __name__ == '__main__': > do_the_cool_stuff() > > All your functions inside the file will remain in-tact but it won't > execute anything. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to tell if I'm being run from a shell or a module
On Feb 14, 11:06 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote: > It depends on what you mean by "an interactive shell"? If you start your > script with: > python -i whatever.py > is it an interactive shell or not? > > I tried these two criteria: > a) See if the __main__ module has a __file__ attribute. > b) See if sys.stdin is a real tty Right, so my idea of an 'interactive shell' seems to be a little ill defined. Nonetheless, looking if the main module has a file attribute looked to be a good idea until I tried it on IPython - returns True... I think I'll do what I said in the first post, but add an option to override the behaviour for less usual circumstances. Seems to be the best compromise. Dan -- http://mail.python.org/mailman/listinfo/python-list
CUDA
Hi all, Has anyone managed to get any of the Python CUDA libraries working on Windows using cygwin? Which one, and was anything special required? Thanks in advance for any advice. Dan -- http://mail.python.org/mailman/listinfo/python-list
Re: type-checking support in Python?
I also wrote a units package which I'm using for a project of my own (a spiking neural network simulator package called 'Brian'), released separately as a package called Piquant which you can get at sourceforge: http://sourceforge.net/projects/piquant I'm also looking for people to help improve it (get in touch!). The way the package works is to have a Quantity class derived from float, with extra operations. One thing that is different from the other packages out there (and the reason I went to the effort of writing my own package rather than using unum or scalar) is that we also have a QuantityArray or qarray class that derives from numpy.ndarray. There are at the moment two supported types of qarray, with homogeneous units (one unit for the whole array), and heterogeneous units (different unit for each item in the array). At the moment the heterogeneous units implementation is horrible and very slow, but I have a plan for a nicer version at some point (based on numpy's broadcasting rules, so allowing you to have one unit for each row or each column in a matrix for example). Actually I think it would be a really good idea for someone at some point to make a standardised system for units and add it to numpy/ scipy. I'd love to do it myself, but at the moment I have grant applications, papers to finish, etc... :-( Dan Sebastien Binet wrote: > hi, > > On Oct 7, 3:24�am, Bas <[EMAIL PROTECTED]> wrote: > > I have heard about some python package which overloads numbers and > > calculations to include units (quick google found unum, not sure if > > that is the only one). I guess that unless you are dealing with life- > > critical equipment or are using extreme programming, this is overkill > > (but I guess python is not really the right language for that anyway, > > imagine a garbage collection just when you want to launch your > > shuttle). > > FWIW, the python papers volume 3 issue 1 is mentionning another > package, 'scalar': > http://archive.pythonpapers.org/ThePythonPapersVolume3Issue1.pdf > http://russp.us/scalar.htm > > cheers, > sebastien. -- http://mail.python.org/mailman/listinfo/python-list
