Extract the numeric and alphabetic part from an alphanumeric string
Hi, I have a string as str='123ACTGAAC'. I need to extract the numeric part from the alphabetic part which I did using >>>numer=re.findall(r'\d+',str) >>>numer 123 To get the alphabetic part, I could do >>>alpha=str.replace('123','') >>>alpha ACTGAAC But when I give >>>alpha=str.replace(numer,'') Traceback (most recent call last): File "", line 1, in TypeError: expected a character buffer object How do I blank out the initial numeric part so as to get just the alphabetic part. The string is always in the same format. Please help. Regards, Sandhya -- http://mail.python.org/mailman/listinfo/python-list
Re: A Bug By Any Other Name ...
On Sun, 2009-08-02 at 14:14 +, Albert van der Horst wrote: > >This is actually quite thoroughly untrue. In python, *indentation* > is > >significant. Whitespace (internal to a line) is not. You can even > call > >methods like this if you want: > > You totally don't get it. You describe how python is now. > I propose a change to be made to python. Small wonder that that is > different from what it is now. > > > > s = 'abc' > s. upper() > >ABC > > You prove nothing by giving examples. > You can disprove by giving one counter example, > here it goes. > > Whitespace (internal to a line) is significant. > In Python you cannot change > xleftgoing = 12300 > to > x left going = 123 000 000 > > (You can in Algol68) I had a feeling that counterexample would be coming sooner or later. However, it doesn't really address the change you're looking for. Internal whitespace *is* irrelevant, except insofar as it can be used to delimit different tokens in parsing. If tokens are separate, they are separate, and no more or less whitespace is going to make any difference. Again, I'm describing how python is now. Which is not to say it couldn't be changed, I just want to make sure you understand how deep into the heart of python you are trying to cut. You make it sound like a small change, but it is not. You are proposing changing the parsing rules, which completely changes the scope of what is possible and what isn't with python syntax. All to solve a problem that, so far, hasn't been proven to exist in anything other than a speculative way. You're trying to turn an ocean liner around because you left your sunscreen on the dock. Cheers, Cliff -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Question
2009/8/3 Victor Subervi : -> Hi; -> How do I search and replace something like this: > aLine = re.sub('[<]?[p]?[>]? a-zA-Z0-9"\'=:]*>[<]?[b]?[>]?', '', aLine) > where RE *only* looks for the possibility of "" at the beginning of the > string; that is, not the individual components as I have it coded above, but > the entire 3-character block? > TIA, > Victor I'm not sure I totally understand your problem, try this: aLine = re.sub(r'(?:)?]*>(?:)'.format(str(x)), '', aLine) tiefeng wu 2009-08-03 -- http://mail.python.org/mailman/listinfo/python-list
[ANNC] pybotwar-0.3
pybotwar is a fun and educational game where players create computer programs to control simulated robots to compete in a battle arena. http://pybotwar.googlecode.com/ pybotwar uses pybox2d for the physical simulation, and uses pygame and pygsear for the visualization. pybotwar is released under GPLv3. Changes in pybotwar-0.3: - make tarball extract in to versioned directory _ With Windows Live, you can organize, edit, and share your photos. http://www.microsoft.com/middleeast/windows/windowslive/products/photo-gallery-edit.aspx -- http://mail.python.org/mailman/listinfo/python-list
Re: Test for Pythonwin?
"steve" wrote in message news:4a728aac$0$9744$5a62a...@per-qv1-newsreader-01.iinet.net.au... > Is there a good way to check if a script is running inside Pythonwin? > Perhaps a property or method that is exposed by that environment? > > or, alternatively, is there a better place to ask :-) > > Steven > > 'pywin' in sys.modules Didn't want to check for redirection because my redirection code is in a different thread, and I'd have to think about thread synchronization as well. Thanks guys, Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with colors in a portable way
In article <33e19169-19b4-497a-b262-2bcf7b563...@r38g2000yqn.googlegroups.com>, Robert Dailey wrote: > >Anyone know of a way to print text in Python 3.1 with colors in a >portable way? In other words, I should be able to do something like >this: > >print_color( "This is my text", COLOR_BLUE ) > >And this should be portable (i.e. it should work on Linux, Mac, >Windows). Much as I hate to say, use a cross-platform GUI -- Tkinter comes with Python, and you can also use SDL via PyGame, wxWindows, pyQT, PyOpenGL, and so on. If that's not an option, you should have said "command-line program". ;-) -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with colors in a portable way
On Aug 2, 9:52 pm, a...@pythoncraft.com (Aahz) wrote: [snip] > Much as I hate to say, use a cross-platform GUI -- Tkinter comes with > Python, [snip] Why is Tkinter such a whipping boy of the Python community? I know it's very simple and does not have all the bells and whistles of wx, but i think the gentle learning curve is very important for those struggling to learn GUI's. Even today I always use Tkinter first, and then only reach for wx when absolutely necessary. Seems to me the relativity small footprint(GUI wise) compared to the payoffs are worth the inclusion. I think if Tkinter where ever removed from Python it would be not only a disservice to the language but also a detriment to the *new* users of the language. Just my humble opinion -- http://mail.python.org/mailman/listinfo/python-list
Re: Is python buffer overflow proof?
Marcus Wanner schrieb: On 8/2/2009 10:43 AM, Christian Heimes wrote: Marcus Wanner wrote: I believe that python is buffer overflow proof. In fact, I think that even ctypes is overflow proof... No, ctypes isn't buffer overflow proof. ctypes can break and crash a Python interpreter easily. Christian I see. I thought that it said "invalid array index" when you try to read/write outside of an array's bounds, though... But you can cast the resulting pointer to an array of larger size, and there you are. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Processes not exiting
On 2 Aug, 21:49, Piet van Oostrum wrote: > > MRAB (M) wrote: > >M> I wonder whether one of the workers is raising an exception, perhaps due > >M> to lack of memory, when there are large number of jobs to process. > > But that wouldn't prevent the join. And you would probably get an > exception traceback printed. > > I wonder if something fishy is happening in the multiprocessing > infrastructure. Or maybe the Fortran code goes wrong because it has no > protection against buffer overruns and similar problems, I think. > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p...@vanoostrum.org I don't think it's a memory problem, the reason for the hard and easy queue is because for larger examples it uses far more RAM. If I run all of workers with harder problems I do begin to run out of RAM and end up spending all my time switching in and out of swap so I limit the number of harder problems I run at the same time. I've watched it run to the end (a very boring couple of hours) and it stays out of my swap space and everything appears to be staying in RAM. Just hangs after all "poison" has been printed for each process. The other thing is that I get the message "here" telling me I broke out of the loop after seeing the poison pill in the process and I get all the things queued listed as output surely if I were to run out of memory I wouldn't expect all of the jobs to be listed as output. I have a serial script that works fine so I know individually for each example the fortran code works. Thanks Matt -- http://mail.python.org/mailman/listinfo/python-list
Re: problem in event handling on change of variable value.
On Aug 1, 2:22 pm, sirjee wrote: > hello; > > i m facing a problem in handling events on change of value of > environment variable in a toolCANoe. > > class CANoeEvents: > def OnChange(self,value): > print "value of environment variable has changed" > def OnOpen(self,App): > print "opening Application" > > App_Event = DispatchWithEvents('CANoe.Application',CANoeEvents) > time.sleep(2) > App_Event.Open("C:\projectConfig.cfg") # printd "opening Application" > and event handled properly. > ...(now a code file runs in thecanoein which value of > environment variable called dummy is changed during execution. Just > like OnOpen; OnChange is defined in specification which should be > called on change of environment variable.But it is not happening.) > while (): > myenvVar = App_Event.Environment.GetVariable("dummy") > > The VB does it very easily and defines a subroutine called : > Sub myenvVar _OnChange(value) > print "value of environment variable has changed" > End Sub > //however the > alternative for subroutine for OnOpen in VB (defined below) works fine > as shown above in python. > Sub AppEvent_OnOpen(App) > print "opening Application" > End Sub > > for VB; all this stuff is cake work object_eventname subroutine > and done.what in python? hi all; strange i got answer to the problem posted by me only. now I have very good control of CANoe tool from python if anybody faces any problem wrt automation of CANoe tool using python; do contact me. here actual problem lied in analysing and implementing com heirarchy. ( which is really bit cumbersome) onChange event is associated with App_Event.Environment.GetVariable ("dummy") object. So we need to associate separate class with this object and then write onChange function: class evtClass: def __init__(self): print "initializing event class" def OnChange(self,value): print "value of environment variable changing" print "changed value of environment variable is" print value see = App_Event.Environment.GetVariable("dummy") see_temp=DispatchWithEvents(see,evtClass) this will solve problem. So we need to create separate classes for events associated with separate objects. regards sirjee -- http://mail.python.org/mailman/listinfo/python-list
Re: Is python buffer overflow proof?
On 8/3/2009 3:45 AM, Diez B. Roggisch wrote: Marcus Wanner schrieb: On 8/2/2009 10:43 AM, Christian Heimes wrote: Marcus Wanner wrote: I believe that python is buffer overflow proof. In fact, I think that even ctypes is overflow proof... No, ctypes isn't buffer overflow proof. ctypes can break and crash a Python interpreter easily. Christian I see. I thought that it said "invalid array index" when you try to read/write outside of an array's bounds, though... But you can cast the resulting pointer to an array of larger size, and there you are. Diez Ah, that makes sense. I had forgotten about ctypes.cast(). Marcus -- http://mail.python.org/mailman/listinfo/python-list
"PAKISTAN NEWS" "PAKISTANI NEWSPAPERS" "MAGAZINES ON LINE" "URDU LANGUAGE SOFTWARES" "URDU POETRY PAKISTAN" "PAKISTANI WEB SITE DIRECTOR" "PAKISTANI NEWSPAPERS" "PAKISTAN" "PAKISTAN" on www.pak-web-p
"PAKISTAN NEWS" www.pak-web-pages.blogspot.com "PAKISTANI NEWSPAPERS" www.pak-web-pages.blogspot.com "MAGAZINES ON LINE" www.pak-web-pages.blogspot.com "URDU LANGUAGE SOFTWARES" www.pak-web-pages.blogspot.com "URDU POETRY PAKISTAN" www.pak-web-pages.blogspot.com "PAKISTANI WEB SITE DIRECTOR" www.pak-web-pages.blogspot.com "PAKISTANI NEWSPAPERS" www.pak-web-pages.blogspot.com "PAKISTAN" www.pak-web-pages.blogspot.com "PAKISTAN" www.pak-web-pages.blogspot.com on www.pak-web-pages.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Executing remote command with paramiko
Hey, I'm trying to run a sudo guarded command over SSH using paramiko +++ s = paramiko.SSHClient() s.load_system_host_keys() s.connect(hostname, port, username, passwd) stdin, stdout, stderr = s.exec_command('sudo -s') stdin.write('password\n') stdin.flush() print 'Flushing' stdin, stdout, stderr = s.exec_command('harvester') print stdout.read() s.close() +++ It seems to me that the sudo -s isn't getting executed at all. I commented the sudo -s code lines and no error is shown. Thanks for help and time. -- http://mail.python.org/mailman/listinfo/python-list
Re: heapq "key" arguments
"Gabriel Genellina" wrote: > Ok, it's not strictly the same, but usually it doesn't hurt. The heaqp > module doesn't promise anything about equal elements: it may keep the > original order, rearrange them at will, reverse them, whatever. The documentation doesn't say anything directly about stability, but the implementation is actually stable. You can probably assume it must be at least for nlargest and nsmallest otherwise the stated equivalence wouldn't hold: e.g. nsmallest documentation says: Equivalent to: sorted(iterable, key=key)[:n] -- Duncan Booth http://kupuguy.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: New implementation of re module
On Jul 28, 2:34 am, MRAB wrote: > Hi all, > > I've been working on a new implementation of the re module. The details > are athttp://bugs.python.org/issue2636, specifically > fromhttp://bugs.python.org/issue2636#msg90954. I've included a .pyd file for > Python 2.6 on Windows if you want to try it out. > Where/how should we report perceived bugs: On that bugs.python.org issue? Here? Private e-mail? -- http://mail.python.org/mailman/listinfo/python-list
Announcing PythonTurtle
Hello, I wanted to announce that I have just released my little side project, PythonTurtle. Here is its website: http://pythonturtle.com Its goal is to be the lowest-threshold way to learn (or teach) Python. You can read more about it and download it on the website. Ram. -- http://mail.python.org/mailman/listinfo/python-list
Python configuration question when python scripts are executed using Appweb as web server.
Hi All I have installed python 2.6.2 in windows xp professional machine. I have set the following environment variables -- PYTHONPATH. It points to following windows folders: python root folder, the lib folder and lib-tk folder. I have configured IIS to execute python scripts. I do not have any issues executing python scripts from the python interpreter window or from the browser using IIS as the web server. However, when I use the open source Appweb web browser to execute python scripts. It is able to execute only simple script without issues. For example, hello.py executes fine. hello.py #!/usr/bin/env python print "Content-type: text/html\n\n" print "Hello world!" However I am not able to execute python scripts that contain import statements. I need to point out here that i do not have issue with using os or sys packages. When I tried to execute the sample client script that came with the gnutls wrapper for python, I got error. I narrowed it down to the "from ... import" line in the example. from gnutls.crypto import * from gnutls.connection import * Next I modified it to the functions used in the client python scripts (NOTE: I have added some additional functions to connection and crypto python scripts). from gnutls.crypto import X509Certificate, X509PrivateKey, X509CRL from gnutls.connection import AnonClientCred Next step, in order to debug, I used the module finder to see what is happening. To being with I used the module finder to find out the modules loaded for the simple script hello.py. hello2.py #!/usr/bin/env python import modulefinder import pdb pdb.set_trace() finder = modulefinder.ModuleFinder() finder.run_script('hello.py') print "Content-type: text/html\n\n" print "" print sys.exc_info() print 'Loaded modules:' for name, mod in finder.modules.iteritems(): print '%s: ' % name, print ','.join(mod.globalnames.keys()[:3]) print '-'*50 print 'Modules not imported:' print '\n'.join(finder.badmodules.iterkeys()) print "" I got the error. Please see below for error as captured from Appweb logs. See lines 3 to 5. cgi:5 main 356: CGI Response: read 89 bytes cgi:6 main 356: Data is: > c:\appweb\web\hello2.py(9)() -> finder = modulefinder.ModuleFinder() (Pdb) cgi:5 main 356: CGI Response: read 0 bytes cmd:7 main getExitCode: process 0, code 1 default:1 main Error: 503 "Service Unavailable" for "/hello2.py", file "c:/appweb/web/hello2.py": CGI process /hello2.py: exited abnormally with exit code: 1 request:5 main 356: flushOutput: background 0 Once again, let me stress that this issue is happening only when I use appweb as the web server. Some details on Appweb that I found out: Appweb opens up a new console process and executes the python scripts. See sample below: cmd:5 pool.0 Running: C:\Dev\Ironic\Tools\Python26\python.exe c:/ appweb/web/hello2.py When python scripts are executed in this fashion, should there be additional command line arguments be passed? Am I missing any additional settings. Also can you help with improving the debugging lines in hello2.py so that I can identify the issue. Please help. Thanks. Ram G. -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 10:18 am, cool-RR wrote: > Hello, > > I wanted to announce that I have just released my little side project, > PythonTurtle. > Here is its website:http://pythonturtle.com > > Its goal is to be the lowest-threshold way to learn (or teach) Python. > You can read more about it and download it on the website. > > Ram. Why not make the source available? At the very least, people that do not run windows could try it too. André -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
André schrieb: I wanted to announce that I have just released my little side project, PythonTurtle. Here is its website:http://pythonturtle.com Its goal is to be the lowest-threshold way to learn (or teach) Python. You can read more about it and download it on the website. Ram. Why not make the source available? At the very least, people that do not run windows could try it too. Isn't this the source: http://github.com/cool-RR/PythonTurtle/tree/master ? Linked at the bottom of the page. Best, Hilmar -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 5:53 pm, André wrote: > On Aug 3, 10:18 am, cool-RR wrote: > > > Hello, > > > I wanted to announce that I have just released my little side project, > > PythonTurtle. > > Here is its website:http://pythonturtle.com > > > Its goal is to be the lowest-threshold way to learn (or teach) Python. > > You can read more about it and download it on the website. > > > Ram. > > Why not make the source available? At the very least, people that do > not run windows could try it too. > > André Sorry that the link is obscure like that. I'll change it so it's easier to see. -- http://mail.python.org/mailman/listinfo/python-list
RE: Extract the numeric and alphabetic part from an alphanumeric string
> Hi, > > I have a string as str='123ACTGAAC'. > > I need to extract the numeric part from the alphabetic part which I > did using > >>>numer=re.findall(r'\d+',str) > >>>numer > 123 > > To get the alphabetic part, I could do > >>>alpha=str.replace('123','') > >>>alpha > ACTGAAC > But when I give > >>>alpha=str.replace(numer,'') > Traceback (most recent call last): > File "", line 1, in > TypeError: expected a character buffer object > > How do I blank out the initial numeric part so as to get just the > alphabetic part. The string is always in the same format. > > Please help. > > Regards, > Sandhya If the format's always the same, you could use slicing instead. >>> s = '123ACTGAAC' >>> s[:3] '123' >>> s[3:] 'ACTGAAC' BTW, you should avoid using built-ins like str for variable names. Bad things will happen. Cheers, Drea -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract the numeric and alphabetic part from an alphanumeric string
Sandhya Prabhakaran writes: > Hi, > > I have a string as str='123ACTGAAC'. > > I need to extract the numeric part from the alphabetic part which I > did using numer=re.findall(r'\d+',str) numer > 123 > > To get the alphabetic part, I could do alpha=str.replace('123','') alpha > ACTGAAC > But when I give alpha=str.replace(numer,'') > Traceback (most recent call last): > File "", line 1, in > TypeError: expected a character buffer object > > How do I blank out the initial numeric part so as to get just the > alphabetic part. The string is always in the same format. Firstly, you really should read the Regular Expression HOWTO: http://docs.python.org/howto/regex.html#regex-howto Secondly, is this what you wanted to do? >>> p = re.compile(r'^\d+') >>> p.sub('', '123ACTGAAC') 'ACTGAAC' Regards, Peter -- Peter Brett Remote Sensing Research Group Surrey Space Centre -- http://mail.python.org/mailman/listinfo/python-list
Re: Printing with colors in a portable way
Nobody wrote: On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote: Anyone know of a way to print text in Python 3.1 with colors in a portable way? In other words, I should be able to do something like this: print_color( "This is my text", COLOR_BLUE ) And this should be portable (i.e. it should work on Linux, Mac, Windows). The way that terminals (and emulators) handle colour is fundamentally different from the DOS/Windows console. If you want something which will work on both, you will have write separate implementations for terminals and the DOS/Windows console. For terminals, you can use the "curses" package, e.g.: import curses curses.setupterm() setaf = curses.tigetstr('setaf') setab = curses.tigetstr('setab') def foreground(num): if setaf: sys.stdout.write(curses.tparm(setaf, num)) def background(num): if setab: sys.stdout.write(curses.tparm(setab, num)) For the Windows console, you'll need to use ctypes to interface to the SetConsoleTextAttribute() function from Kernel32.dll. FYI http://github.com/jbowes/markymark/blob/59511b36a752b40243cc18fb0fb9800c74549ac1/markymark.py If the URL ever becomes invalid, then google for markymark.py You can use it either to color your Linux/Unix terms, or you can just look at the python code to see how to set colors and attributes. JM -- http://mail.python.org/mailman/listinfo/python-list
Compiling regex inside function?
Hi all, I have a regex that has no use outside of a particular function. From an encapsulation point of view it should be scoped as restrictively as possible. Defining it inside the function certainly works, but if re.compile () is run every time the function is called, it isn't such a good idea after all. E.g. def entries (l): r = re.compile ('([0-9]+) entr(y|ies)') match = r.search (l) if match: return match.group (1) So the question is: does "r" get regex-compiled once at py-compile time or repeatedly at entries() run time? Frederic -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract the numeric and alphabetic part from an alphanumeric string
Sandhya Prabhakaran wrote: Hi, I have a string as str='123ACTGAAC'. I need to extract the numeric part from the alphabetic part which I did using numer=re.findall(r'\d+',str) numer 123 [snip] I get: ['123'] which is a _list_ of the strings found. -- http://mail.python.org/mailman/listinfo/python-list
M2Crypto: X509.X509_Extension_Stack() throws AssertionError
Hello python-list members Why is the following code snippet throwing an AssertionError? Is that behavior a bug within X509.X509_Extension_Stack()? How would you suggest popping every element from the stack? Regards, Matthias Güntert - from M2Crypto import X509 if __name__ == '__main__': cert_extension_stack = X509.X509_Extension_Stack() cert_extension_1 = X509.new_extension("basicConstraints", "CA:FALSE") cert_extension_1.set_critical() cert_extension_2 = X509.new_extension("keyUsage", "10100") cert_extension_2.set_critical() cert_extension_3 = X509.new_extension("subjectAlternativeName", "DNS:www.mydomain.tld") print cert_extension_stack.push(cert_extension_1) print cert_extension_stack.push(cert_extension_2) print cert_extension_stack.push(cert_extension_3) print cert_extension_stack.pop() - - Traceback (most recent call last): File "test.py", line 18, in print cert_extension_stack.pop() File "/usr/lib64/python2.6/site-packages/M2Crypto/X509.py", line 159, in pop assert len(self.pystack) == 0 AssertionError 1 2 3 - -- http://mail.python.org/mailman/listinfo/python-list
Re: Generate a new object each time a name is imported
Steven D'Aprano wrote: I would like to generate a new object each time I import a name from a module, rather than getting the same object each time. For example, currently I might do something like this: # Module count = 0 def factory(): # Generate a unique object each time this is called global count count += 1 return "Object #%d" % count # Calling module from Module import factory a = factory() # a == "Object #1" b = factory() # b == "Object #2" del factory I'm looking for a way to hide the generation of objects from the caller, so I could do something like this: from Module import factory() as a # a == "Object #1" from Module import factory() as b # b == "Object #2" except of course that syntax is illegal. Why making standard statements do what they're not meant to do ? You could write >import Module > >a = factory() >b = factory() But you already know that. So what's the purpose of making from Module import factory as a from Module import factory as b return 2 different objects ? If I had to write this code I would expect 'a is b' to return 'True'. This is no "don't do that" answer, it's a sincere question: what is the benefit of your /new/ syntax ? JM -- http://mail.python.org/mailman/listinfo/python-list
Re: Queryable Daemon
Justin DeCell wrote: > I was hoping for a little help with a project I'm working on. I'm > writing a daemon in python that I want to be queryable (i.e. I should > be able to run foo -s and it will report some internal information > about the foo daemon if it's running) but I can't figure out a way get > information from that process from an external process. I was thinking > that there should be a way to open a file descriptor, have the daemon > write to that every so often and then have the query process just read > from that fd but it looks like on the process reading from an fd has > to be a child of the daemon for this to work. The only other way I > thought of would be to write to a file on disk every so often from the > daemon and just read the from the query process but it seems like > there should be a more elegant way to do this... Most daemon programs I know of that do this use unix sockets. The socket is typically put in /tmp, or probably better in /var/run, or some other appropriate place. Often this is configurable in a config file that both the daemon and the program or instance that checks the daemon read. Another possibility is shared memory segments. I'm not sure how security is done in this case. Anyway, shared memory is a common IPC mechanism. -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract the numeric and alphabetic part from an alphanumeric string
On Mon, Aug 3, 2009 at 8:47 PM, Sandhya Prabhakaran wrote: > Hi, > > I have a string as str='123ACTGAAC'. > > I need to extract the numeric part from the alphabetic part which I > did using numer=re.findall(r'\d+',str) numer > 123 > The docs for re.findall say that it returns a list of matches. So '123' will be numer[0]. > To get the alphabetic part, I could do alpha=str.replace('123','') alpha > ACTGAAC > But when I give alpha=str.replace(numer,'') > Traceback (most recent call last): > File "", line 1, in > TypeError: expected a character buffer object > That's what would happen if you pass in a list instead of a string to replace. > How do I blank out the initial numeric part so as to get just the > alphabetic part. The string is always in the same format. > > Please help. > -- kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: New implementation of re module
John Machin wrote: On Jul 28, 2:34 am, MRAB wrote: Hi all, I've been working on a new implementation of the re module. The details are athttp://bugs.python.org/issue2636, specifically fromhttp://bugs.python.org/issue2636#msg90954. I've included a .pyd file for Python 2.6 on Windows if you want to try it out. Where/how should we report perceived bugs: On that bugs.python.org issue? Here? Private e-mail? Probably on http://bugs.python.org/issue2636. -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling regex inside function?
Anthra Norell wrote: > Hi all, > >I have a regex that has no use outside of a particular function. From > an encapsulation point of view it should be scoped as restrictively as > possible. Defining it inside the function certainly works, but if > re.compile () is run every time the function is called, it isn't such a > good idea after all. E.g. > > def entries (l): > r = re.compile ('([0-9]+) entr(y|ies)') > match = r.search (l) > if match: return match.group (1) > > So the question is: does "r" get regex-compiled once at py-compile time > or repeatedly at entries() run time? This can't be answered as simple yes/no-question. While the statement is executed each time, the resulting pattern-object isn't re-created, instead there is a caching-mechanism inside the module - so unless you create a situation where that cache's limits are exceeded and pattern objects are removed from it, you are essentially having the overhead of one function-call & a dict-lookup. Certainly worth it. As an additional note: r"" has *nothing* todo with this, that's just so-called raw string literals which have a different escaping-behavior - thus it's easier to write regexes in them. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
cool-RR wrote: Hello, I wanted to announce that I have just released my little side project, PythonTurtle. Here is its website: http://pythonturtle.com Its goal is to be the lowest-threshold way to learn (or teach) Python. You can read more about it and download it on the website. Ram. It looks neat but it's not yet ready for prime time. Colin W. -- http://mail.python.org/mailman/listinfo/python-list
ANNOUNCE: AwstatsReader 0.01
ABOUT THE MODULE AwstatsReader is an attempt at a pythonic interface to AWStats data cache files. Using it, you can access year, month, and individual data points via dictionary-like accessors. Download here: http://azariah.com/open_source.html ABOUT THE AUTHOR Joshua Kugler (jos...@azariah.com) is a programmer and system administrator with over 10 years of industry experience. He is currently looking for a job. Happen to have one you could offer him? :) Resume at: http://jjncj.com/papers/KuglerAll.pdf DISCLAIMER == This is a "release early, release often" release, codnamed Joshua-hopes- somebody-downloads-this-and-likes-code-quality-and-hires-him. This is an early release...probably pre-alpha. There are no tests yet (haven't generated cache files I can release publically), not much documentation, and the interface may change (but I hope not too much). And I haven't even put this in a public repository, as the name might (but probably won't) change. I wrote this via examples from an AWStats cache file, so I'm sure there are sections for which I do not have definitions. If you would send me those sections, I'll be sure to add them. The error handling is probably a little light. Certainly could be improved. Right now, this will parse and display cache files from AWStats 6.5. I've not tested other versions yet, as 6.5 is the only version I've had access to so far. INSTALLATION See INSTALL LICENSE === See COPYING EXAMPLE === import AwstatsReader obj = AwstatsReader.AwstatsReader('/path/to/awstats_logs', 'example.com') print obj[2007] print obj[2008][6] m = obj[2009][7] print m['general'] # Access like a dictionary... print m['general']['LastLine'] #...or like an object attribute print m['general'].LastLine print m.general.LastLine FEEDBACK Please send questions/comments/suggestions to awstatsrea...@azariah.com For now, you can find the latest version here: http://azariah.com/open_source.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Python docs disappointing
Carl Banks wrote: On Jul 31, 1:55 pm, Mark Lawrence wrote: Apart from that what have the Pythonistas ever done for us? Nothing!:) Please don't feed the trolls. And if you do feed the trolls don't smile at them. Carl Banks And if you do smile at them, don't show your teeth! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
urllib2.urlopen timeout
Hi, I would like to ask how I should set timeout for a call: f = urllib2.urlopen(url) I am using Python 2.5. I have already tried socket.setdefaulttimeout(3). However, this adversely affects other connections the application makes, since it seems to affect all socket connections. I know that Python 2.6 offers urllib2.urlopen(url[, data][, timeout]) which would elegantly solved my problem, but I have to stick to Python 2.5. I am doing urlopen in a thread so I want to avoid really long hanging on this call, which happens at times, until some default timeout ticks out. All hints appreciated. Thanks, Zdenek -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract the numeric and alphabetic part from an alphanumeric string
Sandhya Prabhakaran wrote: > I have a string as str='123ACTGAAC'. You shouldn't use 'str' as a label like that, it prevents you from using the str() function in the same body of code. > How do I blank out the initial numeric part so as to get just the > alphabetic part. The string is always in the same format. >>> sample = '123ACTGAAC' >>> numer = ''.join(s for s in sample if s.isdigit()) >>> alpha = ''.join(s for s in sample if s.isalpha()) >>> numer, alpha ('123', 'ACTGAAC') If by 'always in the same format' you mean the positions of the numbers & alphas, you could slightly abuse the struct module: >>> import struct >>> sample = '123ACTGAAC' >>> format = '3s7s' # a string of 3 + a string of 7 >>> struct.unpack(format, sample) ('123', 'ACTGAAC') But seriously, you should use slicing: >>> sample = '123ACTGAAC' >>> sample[0:3], sample[3:] ('123', 'CTGAAC') You can also label the slices, which can be handy for self-documenting your code: >>> num = slice(3) >>> alp = slice(4,10) >>> sample[num], sample[alp] ('123', 'CTGAAC') -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 7:04 pm, "Colin J. Williams" wrote: > cool-RR wrote: > > Hello, > > > I wanted to announce that I have just released my little side project, > > PythonTurtle. > > Here is its website: > >http://pythonturtle.com > > > Its goal is to be the lowest-threshold way to learn (or teach) Python. > > You can read more about it and download it on the website. > > > Ram. > > It looks neat but it's not yet ready for prime time. > > Colin W. I agree there's a lot that can be improved - I wanted to put it out there so I could get feedback and fix the important things first. So I'd appreciate you telling me what you think needs improvement. Ram. -- http://mail.python.org/mailman/listinfo/python-list
Re: Extract the numeric and alphabetic part from an alphanumeric string
Oh yes indeed! Now that works :D Thanks a lot !! 2009/8/3 Kushal Kumaran > > On Mon, Aug 3, 2009 at 8:47 PM, Sandhya > Prabhakaran wrote: > > Hi, > > > > I have a string as str='123ACTGAAC'. > > > > I need to extract the numeric part from the alphabetic part which I > > did using > numer=re.findall(r'\d+',str) > numer > > 123 > > > > The docs for re.findall say that it returns a list of matches. So > '123' will be numer[0]. > > > To get the alphabetic part, I could do > alpha=str.replace('123','') > alpha > > ACTGAAC > > But when I give > alpha=str.replace(numer,'') > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: expected a character buffer object > > > > That's what would happen if you pass in a list instead of a string to > replace. > > > How do I blank out the initial numeric part so as to get just the > > alphabetic part. The string is always in the same format. > > > > Please help. > > > > -- > kushal > -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
Dave Angel wrote: sturlamolden wrote: On 20 Jul, 18:27, Phillip B Oldham wrote: Tuples are used for passing arguments to and from a function. Common use of tuples include multiple return values and optional arguments (*args). That's from Mesa, the Xerox PARC language of the 1970s. Mesa used tuples for subroutine arguments in a very straightforward way. Every function took one tuple as an argument, written as parameters in parentheses separated by commas. Every function returned a tuple as an argument. This had a nice symmetry; function outputs and function inputs had the same form. Mesa was the first language to break through the "single return value" syntax problem. Python doesn't go that far. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: heapq "key" arguments
[Duncan Booth] > The documentation doesn't say anything directly about stability, but the > implementation is actually stable. You can probably assume it must be at > least for nlargest and nsmallest otherwise the stated equivalence wouldn't > hold: > > e.g. nsmallest documentation says: > > Equivalent to: sorted(iterable, key=key)[:n] Yes. The code for nsmallest and nlargest preserves stability so that the equivalence is maintained. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling regex inside function?
Anthra Norell wrote: > def entries (l): > r = re.compile ('([0-9]+) entr(y|ies)') > match = r.search (l) > if match: return match.group (1) > > So the question is: does "r" get regex-compiled once at py-compile time > or repeatedly at entries() run time? The docs say: The compiled versions of the most recent patterns passed to re.match (), re.search() or re.compile() are cached, so programs that use only a few regular expressions at a time needn’t worry about compiling regular expressions. (But they don't say how few is 'only a few'...) If you're concerned about it, you could always set the compiled pattern to a default value in the function's argspec, as that _is_ only executed the once: def entries(line, regex = re.compile('([0-9]+) entr(y|ies)'): match = regex.search(line) ... -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
John Nagle wrote: > Every function returned a tuple as an argument. This had a nice > symmetry; function outputs and function inputs had the same form. > Mesa was the first language to break through the "single return > value" syntax problem. > > Python doesn't go that far. I assume here you're referring to the former point and not the latter? -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate lines with regex
On 2009-08-01 14:39, Michael Savarese wrote: I'm a python newbie and I'm trying to test several regular expressions on the same line before moving on to next line. it seems to move on to next line before trying all regular expressions which is my goal. it only returns true for first regular expression does the curser have to be rest to beginning of line? If so, how would I do that. remember I'm new thanks in advance to any advice Mike S. the following is part of my code, readThis=open('c:/9320.txt','r') for line in readThis: try: thisKey = key.search(line).group(1) thisMap = map.search(line).group(1) thisParcel = parcel.search(line).group(1) except: continue What is happening is that either key or map fails to match and trying to get a group raises an exception. This is caught, and the "continue" statement tells the for-loop to go to the next line in readThis. The "continue" statement does not mean "go back to where the error was raised and continue executing as if no error happened". The key to using try: except: properly is to surround as little code as possible. Also, to explicitly state which exceptions you are going to catch. In this case, though, you don't want to use exceptions. You should deal with the match objects that are the result of the .search() method. for line in readThis: key_match = key.search(line) if key_match is not None: this_key = key_match.group(1) # ... do something with this_key map_match = map.search(line) if map_match is not None: this_map = map_match.group(1) # ... do something with this_map parcel_match = parcel.search(line) if parcel_match is not None: this_parcel = parcel_match.group(1) # ... do something with this_parcel -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Question
En Sun, 02 Aug 2009 18:22:20 -0300, Victor Subervi escribió: How do I search and replace something like this: aLine = re.sub('[<]?[p]?[>]?[<]?[b]?[>]?', '', aLine) where RE *only* looks for the possibility of "" at the beginning of the string; that is, not the individual components as I have it coded above, but the entire 3-character block? An example would make it more clear; I think you want to match either "" is optional. Use a normal group or a non-capturing group: r'()?That said, using regular expressions to parse HTML or XML is terribly fragile; I'd use a specific tool (like BeautifulSoup, ElementTree, or lxml) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Queryable Daemon
On Sun, 02 Aug 2009 19:36:08 -0600, Michael Torrie wrote: > Another possibility is shared memory segments. I'm not sure how > security is done in this case. Shared memory segments have an owner, group, and the standard ugo=rwx permissions (execute permission is present but ignored); see the shmget(2) and shmctl(2) manpages. But Unix-domain sockets are supported by the standard library, while shared memory would require a separate library, e.g.: http://semanchuk.com/philip/PythonIpc/ Also, there is unlikely to be an advantage to using shared memory in Python. In most cases, you would have to explicitly serialise Python objects into the shared memory region. It's more useful in C, where you can treat the region as a struct which is accessed directly from the program. -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate lines with regex
Robert Kern wrote: [snip] for line in readThis: key_match = key.search(line) if key_match is not None: this_key = key_match.group(1) # ... do something with this_key map_match = map.search(line) if map_match is not None: this_map = map_match.group(1) # ... do something with this_map parcel_match = parcel.search(line) if parcel_match is not None: this_parcel = parcel_match.group(1) # ... do something with this_parcel re.search and re.match will return a MatchObject if successful or None if unsuccessful. A MatchObject is always true, so you can simplify to: ... if key_match: this_key = key_match.group(1) # ... do something with this_key ... -- http://mail.python.org/mailman/listinfo/python-list
Re: Run pyc file without specifying python path ?
Barak, Ron wrote: -Original Message- From: Dave Angel [mailto:da...@ieee.org] Sent: Sunday, August 02, 2009 12:36 To: Barak, Ron Cc: 'python-list@python.org' Subject: Re: Run pyc file without specifying python path ? Barak, Ron wrote: Hi Dave, It seems like I don't understand your solution. I use the (attached) soapAPI.py as the wrapper to parsing.pyc. However, if I do (for instance): $ python -u parsing.pyc -U aaa The last line of the output is (as expected): return_code: 12 ; params: {'username': 'aaa'} But, if I try the following: $ soapAPI.py -U aaa I don't get this line. Only the output to stderr gets printed to the screen. Bye, Ron. Hi Ron, To make it easier for anybody following this thread, let me post the minimum equivalent source files, inline. parsing.py: -- #!/usr/bin/env python import sys def main(): print >> sys.stderr, "This is stderr output" return 5, sys.argv if __name__ == "__main__": return_code, params = main() print "return_code:",return_code,"; params:",params sys.exit(return_code) --- soapapi.py: --- #!/usr/bin/env python import sys import parsing parsing.main() -- When I run soapapi.;py, it indeed prints only the stderr output. The solution is to move (most or all) of the top-level code of parsing.py into a main() function. Since you already have a main(), I'll rename that, and make a new one that calls it. new parsing.py: --- #!/usr/bin/env python import sys def innermain(): print >> sys.stderr, "This is stderr output" return 5, sys.argv def main(): return_code, params = innermain() print "return_code:",return_code,"; params:",params sys.exit(return_code) if __name__ == "__main__": main() --- The output is now two lines, one from innermain(), and one from main(). And it's the same whether the user runs parsing.py or soapAPI.py To clarify what happened, realize that when the user invokes parsing.py, the module is considered a script, and gets a pseudo-name of "__main__"When that same module is imported by another one, it is considered a library module, and gets its own name "parsing" So any logic that explicitly checks for "__main__" has to change, because we want identical behavior in the two cases. DaveA Hi Dave, Trying your above scripts, I get other results than you do. E.g.: $ python parsing.py --help return_code: 5 ; params: ['parsing.py', '--help'] This is stderr output $ python soapapi.py --help This is stderr output $ So, in my environment (cygwin on win xp, python Python 2.5.2) I'm getting different results than you get, namely - soapapi.py on my environment does not show the stdout output. On which environment did you get identical results from your parsing.py and soapapi.py ? Bye, Ron. $ cat parsing.py #!/usr/bin/env python import sys def main(): print >> sys.stderr, "This is stderr output" return 5, sys.argv if __name__ == "__main__": return_code, params = main() print "return_code:",return_code,"; params:",params sys.exit(return_code) $ cat soapapi.py #!/usr/bin/env python import sys import parsing parsing.main() Please read my message again, especially starting with the words "the solution is." I showed two versions of parsing.py, and you're still using the broken version. I posted both versions, with descriptions of what I changed and why, so that you could see why the change was important. I'm using sys.version == 2.6.2 (r262:71600, Apr 21 2009, 15:05:37) [MSC v.1500 32 bit (Intel)] Running on XP Sp3. And 2.5 will give identical results, though I don't have it installed any more. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: heapq "key" arguments
[Joshua Bronson]: > According tohttp://docs.python.org/library/heapq.html, Python 2.5 > added an optional "key" argument to heapq.nsmallest and > heapq.nlargest. I could never understand why they didn't also add a > "key" argument to the other relevant functions (heapify, heappush, > etc). The problem is that heapq acts on regular lists, so it does not have exclusive access to the structure. So, there is no reliable way for it to maintain a separate list of keys. Since the keys can't be saved in the structure (without possibly breaking other code), the fine grained heapq functions (like heappop and heappush) would need to call key functions every time they are invoked. This is at odds with the implicit guarantee of the key function that it will be called no more than once per key. The overall problem is one of granularity. A key function should be applied once in an initial pass, not on every call to a push/pop function. The everyday solution that most people use is to operate on a list of (key, record) tuples and let tuple comparison do the work for you. Another solution is to build a Heap class that does have exclusive access to the structure, but the API sugar often isn't worth the slightly weaker performance. One other thought. Heaps are a lazy evaluation structure, so their fined-grained mutation functions only work well with just a single ordering function, so there is not need to have (and every reason to avoid) changing key functions in mid-stream. IOW, the key function needs to be constant across all accesses. Contrast this with other uses of key functions where it makes perfect sense to run minage=min(data, key=attrgetter('age')) and then running minsal=min(data, key=attrgetter('salary')). The flexibility to change key functions just doesn't make sense in the context of the fine-grained heap functions. Accordingly, this is why I put key functions in nlargest() and nsmallest() but not in heappush() and friends. The former can guarantee no more than one key function call per entry and they evaluate immediately instead of lazily. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: Compiling regex inside function?
alex23 wrote: > The docs say: > The compiled versions of the most recent patterns passed to re.match > (), re.search() or re.compile() are cached, so programs that use only > a few regular expressions at a time neednt worry about compiling > regular expressions. > > (But they don't say how few is 'only a few'...) If you're interested then it isn't all that hard to open up re.py in an editor and see that the cache holds up to 100 items. Of course that's just an implementation detail so other versions might have a different size cache. Also implementation dependant, but it might be worth knowing that when you hit that limit the cache is simply cleared: there's no gradual clearing of the oldest or least used entries. -- http://mail.python.org/mailman/listinfo/python-list
Re: urllib2.urlopen timeout
Zdenek Maxa wrote: > Hi, > > I would like to ask how I should set timeout for a call: > > f = urllib2.urlopen(url) > > > I know that Python 2.6 offers > urllib2.urlopen(url[, data][, timeout]) > which would elegantly solved my problem, but I have to stick to Python 2.5. > There are three solutions that I know about: 1) Make your own little HTTP library and set timeouts for each of your sockets. 2) The same as the above, except use asynchronous sockets and the the select module. 3) If you're on a Unix style system, use the signal module (the example is helpful http://docs.python.org/library/signal.html#example) Here's some code: import urllib2 import signal class TimeoutException(Exception): """SIGALARM was sent to the process""" pass def raise_timeout(signum, frame): raise TimeoutException("Timeout!") signal.signal(signal.SIGALRM, raise_timeout) try: signal.alarm(5) # raise alarm in 5 seconds data = urllib2.urlopen("http://www.google.com";).readlines() except TimeoutException, ex: data = None finally: signal.alarm(0) # disable alarm HTH, Ben -- http://mail.python.org/mailman/listinfo/python-list
Can python do something like the onclick events in javascript ?
I'm trying to use python to build a simple web page that make use of the onclick behavior, instead of requiring users to click the 'submit' button. I realize in javascript there are onclick, onchange events. Is python capable of doing the same? Thanks in Advance -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate lines with regex
On 2009-08-03 12:29, MRAB wrote: Robert Kern wrote: [snip] for line in readThis: key_match = key.search(line) if key_match is not None: this_key = key_match.group(1) # ... do something with this_key map_match = map.search(line) if map_match is not None: this_map = map_match.group(1) # ... do something with this_map parcel_match = parcel.search(line) if parcel_match is not None: this_parcel = parcel_match.group(1) # ... do something with this_parcel re.search and re.match will return a MatchObject if successful or None if unsuccessful. A MatchObject is always true, so you can simplify to: ... if key_match: this_key = key_match.group(1) # ... do something with this_key ... True, but I prefer to maintain a consistent style for None-testing regardless of whether or not I am sure the not-None type will have a suitable __nonzero__ implementation. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Can python do something like the onclick events in javascript ?
Leo Brugud schrieb: I'm trying to use python to build a simple web page that make use of the onclick behavior, instead of requiring users to click the 'submit' button. If that's the only reason, don't use JS for that, it's annoying. I realize in javascript there are onclick, onchange events. Is python capable of doing the same? Somewhat, through a python-2-javascript-translation: http://pyjs.org/ Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Can python do something like the onclick events in javascript ?
On Mon, Aug 3, 2009 at 2:39 PM, Leo Brugud wrote: > I'm trying to use python to build a simple web page that make use of > the onclick behavior, instead of requiring users > to click the 'submit' button. > > I realize in javascript there are onclick, onchange events. Is python > capable of doing the same? > It depends. Python is usually used as a server-side language while Javascript is client-side. The client's web browser requests a web page, your program processes that request and sends the client a web page. That web page can contain anything you want, including Javascript. Your python program never hears from that client again until the *client* initiates contact, be it through a submit button or a javascript event that submits an HTTP request. You'll have to add the onclick and onchange code to the resulting web page, using Javascript, because Python never handles sending the request, jsut receiving it. Of course, this isn't the case if you are using something like pyjs, which compiles Python code to Javascript, your code will be running as Javascript code and not Python code. > > Thanks in Advance > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
"MAGZINES IN URDU" "PAKISTANI MAGZINES" PAKISTAN" "PAKISTAN" "PAKISTANI NEWSPAPERS" "tHE eXPRTESS" "JANG NEWS" "WAQT" "DAWN" ON www.pak-web-pages.blogspot.com
"MAGZINES IN URDU" "PAKISTANI MAGZINES" PAKISTAN" "PAKISTAN" "PAKISTANI NEWSPAPERS" "tHE eXPRTESS" "JANG NEWS" "WAQT" "DAWN" ON www.pak-web-pages.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Online payment module
Hello all. I'm considering building a module to provide a cross-payment-gatewat API for making online payments. In the Perl world we have a module like this called Business::OnlinePayment ( http://search.cpan.org/~jasonk/Business-OnlinePayment-2.01/OnlinePayment.pm). Is there anything like this in Python? My searches so far haven't turned up anything. If I don't find one I'll probably build one, covering PayPal and Authorize.net to start. If anyone is interested in discussing the project and/or getting involved shoot me an email. Thanks, -sam -- http://mail.python.org/mailman/listinfo/python-list
Re: .dbf tables and Null
John Machin wrote: On Aug 1, 3:41 am, Ethan Furman wrote: Mornin'! and a good one, too, I hope. Question for you... First part of the question: What is the general value in having Null capability for fields? In general, in any database system, so that one can distinguish between "the customer has no 'middle name'" ('') and "the customer's 'middle name' is unknown" (NULL). Second part: Is there a tangible difference between Null, and the nothing of 0, '', False, etc, in python? 0 is the zero thing, it is not nothing. False is not nothing. Third part: If there is a tangible difference, do those of us who use python and these old, refuse-to-die, .dbf files actually have need of, or have tables including, Null values? P.S. part (for John Machin, if he sees this ;) Will the dbf package you are working on support Null values? My philosophy when digging stuff out of arcane storages is to expose what is found and leave any kludging/sanitising to the next layer. For example, None is returned for an N (Number) field that's all spaces; it's up to the caller to decide whether to treat None as zero, raise an exception, pop up a data-collection dialogue box, ... If you mean specifically the Visual Foxpro v3 _NullFlags hack, yes, it already supports that, as well as the VFP9 abuse of that hack for Varchar and Varbinary fields :-) Thank you. It's been an interesting journey learning Python. Probably the two most difficult concepts for me to really internalize have been "batteries included" (thank you for the pointer to struct!) (I finally 'found' and incorporated array into my dbf module), and "everything is an object, even functions!". The unittest module worked wonderfully in making sure everything still worked after switching over to array as well, and the 30+% speed-up is also nice! :) All in all, I *love* Python! ~Ethan~ -- http://mail.python.org/mailman/listinfo/python-list
kw param question
I want to write a decorator that, among other things, returns a function that has one additional keyword parameter, say foo=None. When I try def my_decorator(f): # blah, blah def wrapper(*p, foo=None, **kw): x = f(*p, **kw) if (foo): # blah, blah else # blah blah return wrapper ...i get a syntax error where it says "foo=None". I get similar errors with everything else I've tried. Is is possible to do this in Python? TIA! kynn -- http://mail.python.org/mailman/listinfo/python-list
Re: M2Crypto: X509.X509_Extension_Stack() throws AssertionError
Matthias Güntert wrote: > Why is the following code snippet throwing an AssertionError? Is that > behavior a bug within X509.X509_Extension_Stack()? How would you suggest > popping every element from the stack? > > cert_extension_2 = X509.new_extension("keyUsage", "10100") Maybe your OpenSSL is too old. keyUsage needs to be defined in OpenSSL obj_dat.h file for this to work. > cert_extension_3 = X509.new_extension("subjectAlternativeName", > "DNS:www.mydomain.tld") And I believe you mispelled this, try subjectAltName. With the change in spelling for subjectAltName your test passes for me using OpenSSL 1.0.0beta3 and M2Crypto 0.20beta with the (still incomplete) patch to make M2Crypto work with OpenSSL 1.0: https://bugzilla.osafoundation.org/show_bug.cgi?id=12855 -- Heikki Toivonen - http://heikkitoivonen.net -- http://mail.python.org/mailman/listinfo/python-list
Re: kw param question
On Mon, 2009-08-03 at 19:59 +, kj wrote: > > I want to write a decorator that, among other things, returns a > function that has one additional keyword parameter, say foo=None. > > When I try > > def my_decorator(f): > # blah, blah > def wrapper(*p, foo=None, **kw): > x = f(*p, **kw) > if (foo): > # blah, blah > else > # blah blah > return wrapper > > ...i get a syntax error where it says "foo=None". I get similar > errors with everything else I've tried. > Not exactly sure what you're trying to do.. but, regular arguments must be used *before* positional and keyword arguments so the definition: def wrapper(*p, foo=None, **kw): is syntactically incorrect whereby the following is correct: def wrapper(foo=None, *p, **kw): But if what you are wanting is to actually add 'foo' to kw then I would do this: def my_decorator(f): # blah, blah def wrapper(*p, **kw): if 'foo' not in kw: kw['foo'] = None x = f(*p, **kw) if kw['foo']: # blah blah else: # blah blah return wrapper -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
Hello, I wanted to announce that I have just released my little side project, PythonTurtle. [snip] I think it looks great --haven't download the source yet-- but i really like the screenshot. This will be more "inviting" to the new, inexperianced users. I like the idea of packaging up the command prompt and the canvas into one very friendly interface. I especially like that you are using a "real" turtle. I can remember the first time i used turtle (in python stdlib) and i kept saying to myself... "Were the heck is this damn turtle?!?!" (>_<) :-) -- http://mail.python.org/mailman/listinfo/python-list
easy_install: unresolved external symbol
Hello, I keep getting errors when trying to use easy_install to install bbfreeze or cxfreeze (same errors). This is the output: http://pastebin.com/m65ba474d The error message unresolved external symbol keeps popping up. I have no idea how to solve this. Can anyone give me a hint? Thanks in advance -- http://mail.python.org/mailman/listinfo/python-list
Ordering of dict keys & values
I understand that the keys in a dictionary are ordered not randomly but something practically close to it, but if I create a SQL query like so: query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()), ','.join(stats.values())) Can I at least rely on the value being in the same index as its corresponding key? -- Wells Oliver we...@submute.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie Question regarding __init__()
On Aug 2, 5:51 am, Dave Angel wrote: > Simon wrote: > > Okay I will fix my code and include "self" and see what happens. I > > know I tried that before and got another error which I suspect was > > another newbie error. > > > The idea behind the init_Pre is that I can put custom code here to > > customize the __init__ instead of creating a new subclass. This kind > > of hook pattern allows you to flatten your inheritance hierarchy. I > > can choose in the init_Pre method to execute code before the init_exec > > (which contains the default __init__ code) and still execute the > > init_Exec method or I can completely customize the entire __init__ by > > returning False from init_Pre and prevent the init_Exec from being > > called. I use this type of pattern with almost all my methods. In > > this way I can create a less complicated inheritance chain but still > > have have custom code when needed without sub-classing. > > > I am use to Visual FoxPro which where you can do > > > =is.init_Pre().And.This.init_Exec() and the result is discarded so > > that is why it looks the way it does. In this form init_Exec has to > > return a value. However, If self.init_Pre(): self.init_Exec() would > > work the same and then I could avoid returning a value. > > > Thanks, > > Simon > > > On Aug 1, 5:52 am, Dave Angel wrote: > > >> Nat Williams wrote: > > >>> As MRAB described, ALL instance methods need to accept 'self' as a first > >>> parameter, as that will be passed to them implicitly when they are called. > >>> This includes __init__. The name 'self' is just a commonly accepted > >>> convention for the name of the instance object passed to methods. You > >>> don't > >>> have to call it that, but you really should. > > >>> Take a look athttp://docs.python.org/tutorial/classes.html#class-objects > >>> It might help shed some light on how methods and instances work. > > >>> One other thing. I'm a little confused by the first line of > >>> dcObject.__init__: > > >>> self.init_Pre() and self.init_Exec() > > >>> I suspect this does not do what you think it does. init_Pre and init_Exec > >>> will both be called by this expression (unless init_Pre throws an > >>> exception, > >>> of course). You're not getting anything here that you wouldn't by just > >>> calling each method on a separate line, except just making it harder to > >>> read. > > >> Read the doc-string for init_Pre() and for init_Exec(). The final > >> version of init_Pre() will return False in some circumstances, and in > >> those circumstances Simon doesn't want init_Exec() to be called. He's > >> deliberately using the short-circuit evaluation of 'and' to accomplish > >> that. > > >>> On Fri, Jul 31, 2009 at 8:53 PM, Simon wrote: > > Hi > > So should the dcObject class include the "self" as well since I have > not defined an __init__ method in dcCursor? > > Simon > > -- > http://mail.python.org/mailman/listinfo/python-list > > >> Every one of those methods in both of those classes need a "self" first > >> argument. As others have said, all instance methods need a 'self.' > > (Please don't top-post. You should put new responses at the end of > quoted text, or sometimes inline if that's clearer.) > > I don't understand your comparison to Foxpro. read on. > > As your code was last posted, you don't need a return value from > init_Exec() Every function that doesn't have an explicit return will > return None. And None is interpreted as False in an "and" expression. > If you had an "if" around the whole thing, then you'd care. > > DaveA All I meant by the FoxPro comment was the idea of using the equal sign without a variable to throw away the result. Also in FoxPro there is no such thing as automatically returning None. If there is no explicit return then True is returned. Thanks I did not know that None is interpreted as False. Simon -- http://mail.python.org/mailman/listinfo/python-list
Re: kw param question
In Albert Hopkins writes: >On Mon, 2009-08-03 at 19:59 +, kj wrote: >> >> I want to write a decorator that, among other things, returns a >> function that has one additional keyword parameter, say foo=None. >> >> When I try >> >> def my_decorator(f): >> # blah, blah >> def wrapper(*p, foo=None, **kw): >> x = f(*p, **kw) >> if (foo): >> # blah, blah >> else >> # blah blah >> return wrapper >> >> ...i get a syntax error where it says "foo=None". I get similar >> errors with everything else I've tried. >> >Not exactly sure what you're trying to do.. Yeah, I wasn't too clear. I figured out how to do what I wanted to do: def my_decorator(f): # blah, blah def wrapper(*p, **kw): foo = kw.pop('force', None) x = f(*p, **kw) if (foo): # blah, blah else # blah blah return wrapper Now the definitions of the original functions do not include the foo=None argument, but "actual" functions (i.e. the ones generated by the decorator) all accept the optional foo parameter. The only remaining problem is how to document this... I don't see how pydoc could possibly figure this one out. I guess this is sufficient argument to abandon this idea. Bummer. kynn -- http://mail.python.org/mailman/listinfo/python-list
Re: iterate lines with regex
On Aug 3, 11:44 am, Robert Kern wrote: > On 2009-08-03 12:29, MRAB wrote: > > > > > > > Robert Kern wrote: > > [snip] > > >> for line in readThis: > >> key_match = key.search(line) > >> if key_match is not None: > >> this_key = key_match.group(1) > >> # ... do something with this_key > >> map_match = map.search(line) > >> if map_match is not None: > >> this_map = map_match.group(1) > >> # ... do something with this_map > >> parcel_match = parcel.search(line) > >> if parcel_match is not None: > >> this_parcel = parcel_match.group(1) > >> # ... do something with this_parcel > > > re.search and re.match will return a MatchObject if successful or None > > if unsuccessful. A MatchObject is always true, so you can simplify to: > > > ... > > if key_match: > > this_key = key_match.group(1) > > # ... do something with this_key > > ... > > True, but I prefer to maintain a consistent style for None-testing regardless > of > whether or not I am sure the not-None type will have a suitable __nonzero__ > implementation. There's one thing to keep in mind. In the unknown future, it's possible that re.match could be changed to return an object with a false value that contains some information about the reason for failure, or partial match information, or something like that. If that happens, code like: if m: will continue to work, while code like if m is not None: will suddenly fail. Granted, it's not likely to happen for regexps but it could happen in similar situations in other places. I am one of the biggest critics of implicit boolean values, but given that Python has it, I think it is best to adhere strongly idiomatic uses like regexps. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Is python buffer overflow proof?
On 2 Aug, 15:50, Jizzai wrote: > Is a _pure_ python program buffer overflow proof? > > For example in C++ you can declare a char[9] to hold user input. > If the user inputs 10+ chars a buffer overflow occurs. Short answer: NO Bounds checking on sequence types is a protection against buffer overflow, but is certainly not sufficient. The Python interpreter is written in C. Python extension modules are written in C (or something similar). If you find an unprotected buffer in this C code, you can possibly overflow this buffer. This can be used for nasty things like corrupting the stack and injecting malicious code. There is a reason why the Python sandbox (rexec and Bastion modules) was disabled in Python 2.3. IronPython and Jython provides better protection against buffer overflow than CPython, as these interpreters are written in safer languages (C# and Java). You thus get an extra layer of protection between the Python code and the unsafe C (used in JVM and .NET runtimes). -- http://mail.python.org/mailman/listinfo/python-list
no-clobber dicts?
I use the term "no-clobber dict" to refer to a dictionary D with the especial property that if K is in D, then D[K] = V will raise an exception unless V == D[K]. In other words, D[K] can be set if K doesn't exist already among D's keys, or if the assigned value is equal to the current value of D[K]. All other assignments to D[K] trigger an exception. The idea here is to detect inconsistencies in the data. This is a data structure I often need. Before I re-invent the wheel, I thought I'd ask: is it already available? TIA! kynn -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering of dict keys & values
On Mon, Aug 3, 2009 at 1:47 PM, Wells Oliver wrote: > I understand that the keys in a dictionary are ordered not randomly but > something practically close to it, but if I create a SQL query like so: > > query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()), > ','.join(stats.values())) > > Can I at least rely on the value being in the same index as its > corresponding key? Yes. Per http://docs.python.org/library/stdtypes.html#dict.items : """ Note: Keys and values are listed in an arbitrary order which is non-random, varies across Python implementations, and depends on the dictionary’s history of insertions and deletions. If items(), keys(), values(), iteritems(), iterkeys(), and itervalues() are called with no intervening modifications to the dictionary, the lists will directly correspond. This allows the creation of (value, key) pairs using zip(): pairs = zip(d.values(), d.keys()). """ Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
WindowsError: exception: access violation writing 0x00000000
Hello! I am using cTypes on Windows to interface with a dll and I keep getting an error when I execute this method: def eDigitalIn(self, channel, idNum = None, demo = 0, readD=0): """ Name: U12.eAnalogIn(channel, idNum = None, demo = 0, readD=0) Args: See section 4.4 of the User's Guide Desc: This is a simplified version of Counter. Reads & resets the counter (CNT). """ if idNum is None: idNum = self.id ljid = ctypes.c_long(idNum) state = ctypes.c_long(999) ecode = staticLib.ECount(ctypes.byref(ljid), demo, channel, readD, ctypes.byref(state)) if ecode != 0: raise LabJackException(ecode) if ljid == -1: raise LabJackException(-1, "LabJack not found.") return {"idnum":ljid.value, "state":state.value} Here is the error message: Traceback (most recent call last): File "", line 1, in device.eDigitalIn(0) File "C:\Documents and Settings\All Users\Documents\Python \LabJackPython_new\u12.py", line 118, in eDigitalIn ecode = staticLib.ECount(ctypes.byref(ljid), demo, channel, readD, ctypes.byref(state)) WindowsError: exception: access violation writing 0x Here is the signature of the method (which is known to work with C++ programs): long _stdcall EDigitalIn(long *idnum, long demo, long channel, long readD, long *state); staticLib is declared with staticLib = ctypes.windll.LoadLibrary ("ljackuw"). Any ideas? Thanks, Sam -- http://mail.python.org/mailman/listinfo/python-list
Trying to get ABC to work
Hi, Looking for ideas on getting Abstract Base Classes to work as intended within a metaclass. I was wondering if I could use an abc method within a metaclass to force a reimplementation when a class is instantiated from the metaclass. It seems like I cannot do so. I implemented the following test case: import abc class MetaExample(type): def __init__(cls, name, bases, ns): setattr(cls, 'cls_meth', cls.cls_meth) # cls method as instance method setattr(cls, 'cls_abc', cls.cls_abc)# abc cls method as instance method def cls_meth(cls): print('Class method defined stub') @abc.abstractmethod def cls_abc(cls): try: print('Class-Abstract method defined stub') except NotImplementedError, err: print('Must implement cls_abc.') except: print('General exception at cls_abc method.') Then I create class MyKlass from the metaclass and instantiate it as myklass: MyKlass(object): __metaclass__ = MetaExample myklass = MyKlass() myklass.cls_meth() --> prints "Class method defined stub" myklass.cls_abc() --> prints "Class-Abstract method defined stub" I was hopping for myklass.cls_abc() to print "Must implement cls_abc." However, this makes sense since MyKlass implements from the metaclass the cls_abc method and there will never be an abstraction of this method. Any ideas on how to get this done? Any way I could define an abstract method within a metaclass and have it behave with abstraction when the class is created off the metaclass? I other words, I want to force an implementation of cls_abc() method when MyKlass(object): __metaclass__ = MetaExample is declared, or else get NotImplementedError exception. Thanks, Boris Arloff -- http://mail.python.org/mailman/listinfo/python-list
Re: no-clobber dicts?
On Aug 3, 4:07 pm, kj wrote: > I use the term "no-clobber dict" to refer to a dictionary D with > the especial property that if K is in D, then > > D[K] = V > > will raise an exception unless V == D[K]. In other words, D[K] > can be set if K doesn't exist already among D's keys, or if the > assigned value is equal to the current value of D[K]. All other > assignments to D[K] trigger an exception. > > The idea here is to detect inconsistencies in the data. > > This is a data structure I often need. Before I re-invent the > wheel, I thought I'd ask: is it already available? > > TIA! > > kynn Not sure if something like this already exists, but it would be trivial to implement by overriding dict.__setitem__() badda-bing baby! -- http://mail.python.org/mailman/listinfo/python-list
Re: no-clobber dicts?
On Mon, Aug 3, 2009 at 2:47 PM, r wrote: > On Aug 3, 4:07 pm, kj wrote: >> I use the term "no-clobber dict" to refer to a dictionary D with >> the especial property that if K is in D, then >> >> D[K] = V >> >> will raise an exception unless V == D[K]. In other words, D[K] >> can be set if K doesn't exist already among D's keys, or if the >> assigned value is equal to the current value of D[K]. All other >> assignments to D[K] trigger an exception. >> >> The idea here is to detect inconsistencies in the data. >> >> This is a data structure I often need. Before I re-invent the >> wheel, I thought I'd ask: is it already available? >> >> TIA! >> >> kynn > > Not sure if something like this already exists, but it would be > trivial to implement by overriding dict.__setitem__() That is, if you don't care about .update() not preserving the invariant. Otherwise, one will need to look at the UserDict module. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Registation is open for the 9th PyWeek game programming challenge!
Richard Jones wrote: The ninth PyWeek challenge will run between: Sunday 30th August to Sunday 6th September (00:00UTC to 00:00UTC) Yow, hard on the heels of Pyggy! I'd hoped there might be a bit more breathing room, sorry about that! Hope the Pyggy entrants aren't feeling too burned out to participate. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 11:35 pm, r wrote: > Hello, > I wanted to announce that I have just released my little side > project, > PythonTurtle. > [snip] > > I think it looks great --haven't download the source yet-- but i > really like the screenshot. This will be more "inviting" to the new, > inexperianced users. I like the idea of packaging up the command > prompt and the canvas into one very friendly interface. I especially > like that you are using a "real" turtle. > > I can remember the first time i used turtle (in python stdlib) and i > kept saying to myself... > > "Were the heck is this damn turtle?!?!" (>_<) > > :-) Thanks for the compliments; The things you mentioned you liked are all things that I was specifically thinking about when I decided to make PythonTurtle. Well, maybe minus the screenshot :) -- http://mail.python.org/mailman/listinfo/python-list
Re: socket policy flash help
On Aug 2, 12:25 pm, Piet van Oostrum wrote: > > NighterNet (N) wrote: > >N> Here the full code. > >N> flashpolicy.xml > >N> [[[ > >N> > >N> > >N> > >N> > >N> ]]] > >N> flashpolicytest_server3x.py > >N> [[[ > >N> #!/usr/local/bin/python > >N> ''' > >N> Still under testing... > >N> python version 3.x.x > >N> ''' > >N> importsocket > >N> import threading > >N> import sys > >N> import os > >N> file_name = 'flashpolicy.xml' > >N> fh = open(file_name, "r") > >N> policy = fh.read(10001) > > You never use that variable. > > > > >N> host = ''; #out side network > >N> port = ; > >N> print ("# - Init... - #"); > >N> class ClientThread (threading.Thread): > >N> global policy; > >N> allClients = []; > >N> vlock = threading.Lock(); > >N> id = 0 # next available thread number > >N> def __init__(self,clientSocket): > >N> threading.Thread.__init__(self) > >N> self.sockfd = clientSocket; #socketclient > >N> self.name = ''; > >N> ClientThread.id += 1 > >N> self.id = ClientThread.id > >N> self.nickName = ''; > >N> self.allClients.append(self.sockfd); > >N> def sendAll(self,buff): > >N> for index,clientSock in enumerate(self.allClients): > >N> try: > >N> clientSock.send(buff); > > There is no guarantee that send will indeed transmit all of buff. It is > better to use clientSock.sendall(buff). Or you should check the return > value of send and check if it is equal to the length of buff. And repeat > if not. (But sendall is easier). > > Also don't use ; at the end of the statement. It's not pythonic. > > >N> except (socket.error): > >N> print ('errorsocket%s\n',index,"| clean"); > >N> clientSock.close() > >N> del self.allClients[index] > >N> def run(self): > >N> while True: > >N> buff = self.sockfd.recv(1028); > > There is no guarantee that recv will get the whole message. It may even > get as little as 1 byte. So you should check the return value of recv. > The official way is to keep reading until you have enough input or until > you hit end of file. > > >N> if not buff: > >N> print ("connect close...(client side)"); > >N> self.sockfd.close(); > >N> break #incase it loop infinite > >N> if str(buff) == > >str("b\'\\x00\'"): > > What you check here is whether buff contains the byte sequence that starts > with a > b, then a ' ... and ending with the 5 bytes \ x 0 0 ' which is wrong. > > Actually it should be: > > if buff == b\'\x00': > or > if buff == b\'\0': > > >N> print ('policy FOUND >>> sending...') > >N> print(buff) > >N> b = b' >version=\"1.0\"?> >N> from domain=\"*\" to-ports=\"*\" />' > >N> print (b) > >N> self.sockfd.send(b); > >N> self.sockfd.sendall(b); > > Only self.sockfd.sendall; delete the line with self.sockfd.send(b). And > remove the semicolons for esthetical reasons. > > >N> Some odd reason I can't send flash policy from python to flashsocket > >N> to agrees with the connection. > > -- > Piet van Oostrum > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4] > Private email: p...@vanoostrum.org thanks it help. I was not sure about the format most the time. -- http://mail.python.org/mailman/listinfo/python-list
Obtaining Python version
This works, but it seems too cute: >>> pyver = map(int,sys.version.split()[0].split('.')) >>> print(pyver) [2, 6, 1] Is it guaranteed that the Python version string will be in a form suitable for that? In other words, does "sys.version" begin N.N.N other stuff in all versions, and will it stay that way? Are there ever non-numeric versions, like "3.2.rc1"? John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: fast video encoding
On Sun, 02 Aug 2009 03:44:17 +0100, sturlamolden wrote: On 29 Jul, 10:14, gregorth wrote: for a scientific application I need to save a video stream to disc for further post processing. I have worked a bit on this as well. There are two things that make scientific applications different form common video encoding: First, a scientific video stream is often very different from a 'movie': There are usually very little 'movement'. For example, when I have filmed a mouse swimming in a water maze (a pool of milky white water), the only thing that moves is the rat. So I could achieve excellent compression just by saving the pixels that changed. These are referred to as P-frames; any encoder that gets further than the most basic version of any video protocol will do this for you. Unfortunately, water pixels change a lot from frame to frame, even when the camera is static, so it doesn't gain you as much as you might hope in cases like you mention. Second, scientific data should be stored with lossless compression if possible. If data storage is no object, be my guest. Of course, you're going to have to write your own codecs for this, since pretty much every video protocol in existence is lossy. In reality, lossless compression isn't necessary. As long as your quantisation isn't completely up the spout, the added precision isn't actually going to gain you anything unless you're working in very specialised situations; even then, if it does make a significant difference then you needed greater resolution anyway. -- Rhodri James *-* Wildebeest Herder to the Masses -- http://mail.python.org/mailman/listinfo/python-list
Re: Obtaining Python version
On Aug 3, 7:19 pm, John Nagle wrote: > This works, but it seems too cute: > > >>> pyver = map(int,sys.version.split()[0].split('.')) > >>> print(pyver) > [2, 6, 1] > You can also do: >>> import sys >>> sys.version_info (2, 5, 2, 'final', 0) or >>> sys.version_info[:3] (2, 5, 2) > Is it guaranteed that the Python version string will be in a form > suitable for that? In other words, does "sys.version" begin > > N.N.N other stuff > > in all versions, and will it stay that way? Are there ever > non-numeric versions, like "3.2.rc1"? > > John Nagle I strongly suspect that sys.version_info would never change... André -- http://mail.python.org/mailman/listinfo/python-list
Re: RE Question
That worked. Thank you again :) Victor On Mon, Aug 3, 2009 at 12:13 AM, Gabriel Genellina wrote: > En Sun, 02 Aug 2009 18:22:20 -0300, Victor Subervi < > victorsube...@gmail.com> escribió: > > > How do I search and replace something like this: >> aLine = re.sub('[<]?[p]?[>]?> a-zA-Z0-9"\'=:]*>[<]?[b]?[>]?', '', aLine) >> where RE *only* looks for the possibility of "" at the beginning of the >> string; that is, not the individual components as I have it coded above, >> but >> the entire 3-character block? >> > > An example would make it more clear; I think you want to match either > "" is > optional. Use a normal group or a non-capturing group: > r'()? > That said, using regular expressions to parse HTML or XML is terribly > fragile; I'd use a specific tool (like BeautifulSoup, ElementTree, or lxml) > > -- > Gabriel Genellina > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 5:03 pm, cool-RR wrote: [snip] > Thanks for the compliments; The things you mentioned you liked are all > things that I was specifically thinking about when I decided to make > PythonTurtle. Well, maybe minus the screenshot :) I *may* get roasted for this comment, but i think a turtle module along the lines of what you have here would better serve the stdlib. The current implementation is great, but the whole purpose of turtle is to help children or non-programmers get a feel of the language in a very, very, simple and fun way. Your app is presented in much the same way as any over-the-counter windows app any non-programmer has seen before making them *feel* a little more comfortable. [warning wild speculations ahead!!] I just guessing here, but i'll bet many of these *noobs* have found the current turtle just a wee-bit more advanced than they can handle, and *some* have been scared off. I would also venture to say a key-map of sorts that is available thru the help menu where one could push an "Up" button, or a "rotate" button, and have the proper command inserted in the prompt, and then have the command execute, may also help make the connections here, a sort of *real* Visual Basic programming -- sorry Microsoft :P -- http://mail.python.org/mailman/listinfo/python-list
Re: Obtaining Python version
04-08-2009 o 00:19:22 John Nagle wrote: This works, but it seems too cute: >>> pyver = map(int,sys.version.split()[0].split('.')) >>> print(pyver) [2, 6, 1] Is it guaranteed that the Python version string will be in a form suitable for that? In other words, does "sys.version" begin N.N.N other stuff in all versions, and will it stay that way? Are there ever non-numeric versions, like "3.2.rc1"? Why not to use: sys.version_info "A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0)." http://docs.python.org/library/sys.html#sys.version_info Cheers, *j -- Jan Kaliszewski (zuo) -- http://mail.python.org/mailman/listinfo/python-list
Problem with reading CSV file from URL, last record truncated.
Hi, I am trying to download from a URL, a CSV using the following: import re import urllib, urllib2, cookielib import mechanize import csv import numpy import os def return_ranking(): cj = mechanize.MSIECookieJar(delayload=True) cj.load_from_registry() # finds cookie index file from registry # set things up for cookies opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) reply = opener.open('http://ichart.finance.yahoo.com/table.csv? s=CSCO&a=00&b=01&c=2009&d=01&e=2&f=2010&g=d&ignore=.csv').read() fout=open('csco.csv','wb') fout.write(reply) fout.close fin=open('csco.csv','rb') table = csv.reader(fin) fin.close for row in table: print row return_ranking() I need to use cookies etc (mechanize/urllib2) for a different, more complex URL but since it wasn't working, I went back to a simple Yahoo example (above) which I have working with urllib (not urllib2). The behaviour I am seeing is that the last record is being truncated: (sample output) ['2009-04-08', '17.29', '17.33', '16.94', '17.13', '45389100', '17.13'] ['2009-04-07', '17.20', '17.25', '16.58', '16.85', '59902600', '16.85'] ['200'] A friend said I should do the above writing out to a file and have csvreader read in the file, but as you can see, to no avail! Any help greatly appreciated! Note that urllib.urlretrieve works perfectly but I give up the ability to import cookies from my registry which is all important (AFAIK anyway mechanize requires urllib2). Any help greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with reading CSV file from URL, last record truncated.
On Aug 3, 3:54 pm, KB wrote: > Hi, > > I am trying to download from a URL, a CSV using the following: > > import re > import urllib, urllib2, cookielib > import mechanize > import csv > import numpy > import os > > def return_ranking(): > > cj = mechanize.MSIECookieJar(delayload=True) > cj.load_from_registry() # finds cookie index file from registry > > # set things up for cookies > > opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) > > urllib2.install_opener(opener) > > reply = opener.open('http://ichart.finance.yahoo.com/table.csv? > s=CSCO&a=00&b=01&c=2009&d=01&e=2&f=2010&g=d&ignore=.csv').read() > > fout=open('csco.csv','wb') > fout.write(reply) > fout.close > > > return_ranking() > > I need to use cookies etc (mechanize/urllib2) for a different, more > complex URL but since it wasn't working, I went back to a simple Yahoo > example (above) which I have working with urllib (not urllib2). > > The behaviour I am seeing is that the last record is being truncated: > > (sample output) > ['2009-04-08', '17.29', '17.33', '16.94', '17.13', '45389100', > '17.13'] > ['2009-04-07', '17.20', '17.25', '16.58', '16.85', '59902600', > '16.85'] > ['200'] > > A friend said I should do the above writing out to a file and have > csvreader read in the file, but as you can see, to no avail! > > Any help greatly appreciated! Note that urllib.urlretrieve works > perfectly but I give up the ability to import cookies from my registry > which is all important (AFAIK anyway mechanize requires urllib2). > > Any help greatly appreciated. By moving: > fin=open('csco.csv','rb') > table = csv.reader(fin) > fin.close > > for row in table: > print row outside of the routine and into the mainline, it works like a charm. Would like to know why though, so would love to hear any clues! -- http://mail.python.org/mailman/listinfo/python-list
Re: Executing remote command with paramiko
> Hussein B (HB) wrote: >HB> Hey, >HB> I'm trying to run a sudo guarded command over SSH using paramiko >HB> +++ >HB> s = paramiko.SSHClient() >HB> s.load_system_host_keys() >HB> s.connect(hostname, port, username, passwd) >HB> stdin, stdout, stderr = s.exec_command('sudo -s') >HB> stdin.write('password\n') >HB> stdin.flush() >HB> print 'Flushing' >HB> stdin, stdout, stderr = s.exec_command('harvester') >HB> print stdout.read() >HB> s.close() >HB> +++ >HB> It seems to me that the sudo -s isn't getting executed at all. >HB> I commented the sudo -s code lines and no error is shown. >HB> Thanks for help and time. Your command 'harvester' is not run in the sudo shell, but separately in a new session. so it will run under 'username', not under 'root'. You could use stdin, stdout, stderr = s.exec_command('sudo harvester') instead. Or use the lower-level constructs: s = paramiko.SSHClient() s.load_system_host_keys() s.connect(hostname, port, username, password) t = s.get_transport() chan = t.open_session() chan.exec_command('sudo -s') print 'writing password' chan.send(password + '\n') print 'write command' chan.send('whoami\n') print "try to read" print chan.recv() For more control you can use chan.recv_ready() to see if output is available. -- Piet van Oostrum URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4] Private email: p...@vanoostrum.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Help understanding the decisions *behind* python?
John Nagle wrote: Mesa used tuples for subroutine arguments in a very straightforward way. Every function took one tuple as an argument Python doesn't go that far. I believe that a very early version of Python did do something like that, but it was found to be a bad idea, because there was an ambiguity between multiple args and a single arg that happened to be a tuple. This was before keyword arguments existed -- they would have been hard to incorporate into the old scheme. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: no-clobber dicts?
On Aug 3, 5:00 pm, Chris Rebert wrote: > On Mon, Aug 3, 2009 at 2:47 PM, r wrote: [snip] > > Not sure if something like this already exists, but it would be > > trivial to implement by overriding dict.__setitem__() > > That is, if you don't care about .update() not preserving the > invariant. Otherwise, one will need to look at the UserDict module. > > Cheers, > Chris > --http://blog.rebertia.com Good catch Chris. However since the OP said this was for testing purposes, i just *assumed* he would be smart enough *not* to call update() on the dict at hand ;-). But sometimes i need to be protected from myself too -- at least thats what my therapist keeps telling me :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Problem with reading CSV file from URL, last record truncated.
KB wrote: On Aug 3, 3:54 pm, KB wrote: Hi, I am trying to download from a URL, a CSV using the following: import re import urllib, urllib2, cookielib import mechanize import csv import numpy import os def return_ranking(): cj = mechanize.MSIECookieJar(delayload=True) cj.load_from_registry() # finds cookie index file from registry # set things up for cookies opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) urllib2.install_opener(opener) reply = opener.open('http://ichart.finance.yahoo.com/table.csv? s=CSCO&a=00&b=01&c=2009&d=01&e=2&f=2010&g=d&ignore=.csv').read() fout=open('csco.csv','wb') fout.write(reply) fout.close This should be: fout.close() return_ranking() [snip] By moving: fin=open('csco.csv','rb') table = csv.reader(fin) fin.close This should be: fin.close() for row in table: print row outside of the routine and into the mainline, it works like a charm. Would like to know why though, so would love to hear any clues! The parentheses aren't optional; without them you're just referring to the method, not calling it. Because you weren't closing the file the text wasn't all written to disk. When it returns from return_ranking() there's no longer any reference to 'fout', so the file object is available for collection by the garbage collector. When the file object is collected it writes the remaining text to disk. In CPython the file object is collected as soon as there's no reference to it, but in other implementations that might not be the case. -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 3, 8:18 am, cool-RR wrote: > Hello, > > I wanted to announce that I have just released my little side project, > PythonTurtle. > Here is its website:http://pythonturtle.com > > Its goal is to be the lowest-threshold way to learn (or teach) Python. > You can read more about it and download it on the website. I dunno, looks kind of worthless to me. http://www.mensanator.com/mensanator/PythonTurtle/PythonTurtle.htm > > Ram. -- http://mail.python.org/mailman/listinfo/python-list
Extracting text from html
Hello all, Does anyone know of a good tool to get a minimally-formatted text document out of an html document? Something along the lines of what you would get with a lynx -dump, but in Python. I have lxml installed, so I can roll my own if I need to. However, this seemed like the sort of thing that someone would have solved already. Thanks, Van -- http://mail.python.org/mailman/listinfo/python-list
Re: Python configuration question when python scripts are executed using Appweb as web server.
En Mon, 03 Aug 2009 11:04:07 -0300, IronyOfLife escribió: I have installed python 2.6.2 in windows xp professional machine. I have set the following environment variables -- PYTHONPATH. It points to following windows folders: python root folder, the lib folder and lib-tk folder. Why? Did you read it somewhere? Usually there is no need to set the PYTHONPATH variable at all; remove it. I have configured IIS to execute python scripts. I do not have any issues executing python scripts from the python interpreter window or from the browser using IIS as the web server. Try again after PYTHONPATH is removed, and make sure you can import all the required modules from the Python console. Only then try to run the cgi scripts. However, when I use the open source Appweb web browser to execute python scripts. It is able to execute only simple script without issues. [...]However I am not able to execute python scripts that contain import statements. I need to point out here that i do not have issue with using os or sys packages. When I tried to execute the sample client script that came with the gnutls wrapper for python, I got error. I narrowed it down to the "from ... import" line in the example. from gnutls.crypto import * from gnutls.connection import * Check that the above lines work fine from inside the Python console. If not, something went wrong when you installed the gnutls Python package. Next step, in order to debug, I used the module finder to see what is happening. To being with I used the module finder to find out the modules loaded for the simple script hello.py. Debugging cgi scripts isn't easy sometimes; try adding this line at the start of your script: import cgitb;cgitb.enable() You should get a nicely formatted stack trace whenever an exception is raised. If you get an HTTP error instead (like 500 Internal Server Error) it's likely a syntax error, you must fix it before continuing. Then, you may find that modulefinder is no more needed. import pdb pdb.set_trace() pdb cannot work inside a CGI script; remove that. Once again, let me stress that this issue is happening only when I use appweb as the web server. I don't know appweb, but if you first make sure the script works from the command line, it should be much easier to make it work in the web server. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: easy_install: unresolved external symbol
En Mon, 03 Aug 2009 17:39:44 -0300, Bart Smeets escribió: I keep getting errors when trying to use easy_install to install bbfreeze or cxfreeze (same errors). This is the output: http://pastebin.com/m65ba474d Can't you use the binary packages? -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Ordering of dict keys & values
En Mon, 03 Aug 2009 17:47:23 -0300, Wells Oliver escribió: I understand that the keys in a dictionary are ordered not randomly but something practically close to it, but if I create a SQL query like so: query = 'INSERT INTO Batting (%s) VALUES(%s)' % (','.join(stats.keys()), ','.join(stats.values())) Can I at least rely on the value being in the same index as its corresponding key? You already got the answer you wanted, but note that building a SQL statement that way is unsafe [1]. I prefer this way: query= 'INSERT INTO Batting (%s) VALUES(%s)' % ( ','.join(stats.keys()), ','.join(['?']*len(stats))) cursor.execute(query, stats.values()) [1] If you don't know what "SQL injection" means, see http://xkcd.com/327/ -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: Is python buffer overflow proof?
En Mon, 03 Aug 2009 18:04:53 -0300, sturlamolden escribió: On 2 Aug, 15:50, Jizzai wrote: Is a _pure_ python program buffer overflow proof? For example in C++ you can declare a char[9] to hold user input. If the user inputs 10+ chars a buffer overflow occurs. Short answer: NO Bounds checking on sequence types is a protection against buffer overflow, but is certainly not sufficient. The Python interpreter is written in C. Python extension modules are written in C (or something similar). If you find an unprotected buffer in this C code, you can possibly overflow this buffer. This can be used for nasty things like corrupting the stack and injecting malicious code. There is a reason why the Python sandbox (rexec and Bastion modules) was disabled in Python 2.3. (I think the reason rexec and bastion were disabled has nothing to do with the possibility of buffer overflows in extension modules) IronPython and Jython provides better protection against buffer overflow than CPython, as these interpreters are written in safer languages (C# and Java). You thus get an extra layer of protection between the Python code and the unsafe C (used in JVM and .NET runtimes). I disagree. You've just translated the responsability to check for buffer overflows, from the Python VM, to the Java VM or the .Net runtime (and all three suffered from buffer overruns and other problems in some way or another). Also, Python extensions written in C are equivalent to using JNI in Java or unmanaged code in C#: all three are likely to have hidden problems. It's always the same story: a *language* may declare that such things are impossible, but a particular *implementation* may have bugs and fail to comply with the specification. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: merge two png pic
On Jul 31, 2:52 pm, Peter Otten <__pete...@web.de> wrote: > cocobear wrote: > > On Jul 29, 9:20 am, cocobear wrote: > >> Thistwopngfile has their own palette > > >> >>> im1.mode > >> 'P' > >> >>> im.mode > >> 'P' > >> >>> im.getpalette == im1.getpalette > > >> False > > >> I can use this code tomergetwopngpictogether: > > >> Map = Image.new("RGB", (x,y)) > >> Map.paste(im, box) > >> Map.paste(im1,box) > > >> Map = Map.convert("L", optimize=True, palette=Image.ADAPTIVE) > > >> But if thetwopngpicis too big , or if I have tomergemorepic > >> together, I will get MemoryError: > > >> >>> Image.new("RGB",(44544,38656)) > > As a workaround you could split the image into tiles that fit into your > machine's RAM. Or you could try to do the rendering on a 64-bit system. > You'll need at least > > >>> 3*44544*38656./(2**30) > > 4.8109130859375 > > 5 gigabytes for the target image plus memory for the biggest source image. > Alternatively there are probably tools that can keep parts of the image on > disk (imagemagick, maybe? you'll have to check yourself or ask in a > specialized forum). As a last resort you should be able to write such a tool > yourself. I don't know how hard it would be to generate the PNG, but the RGB > pasting should be trivial. > Thanks for you reply. Map = Image.new("RGB", ((x2-x1+1)*256, (y2-y1+1)*256)) for x in range(x1,x2+1): for y in range(y1,y2+1): #print x,y filename = "v=cn1.11&hl=zh-CN&x=%d&y=%d&z=%d&s=Galile" % (x,y,z) # box = ((x-x1)*256, (y-y1)*256, (x-x1)*256+256, (y-y1) *256+256) #print box im = Image.open(filename+".png") Map.paste(im, box) temp = "temp_map.png" Map.save(temp) del Map print "converting" Map = Image.open(temp) Map = Map.convert("P", palette=Image.ADAPTIVE) Now I use code above, the problem is when I create a RGB image, I need four times spaces then a 'P' mode image. Can I directly create a 'P' mode image, and paste those images on? (NOTICE: this small image has different palette) > >> Traceback (most recent call last): > >> File "", line 1, in > >> File "/usr/lib/python2.5/site-packages/PIL/Image.py", line 1710, in > >> new > >> return Image()._new(core.fill(mode, size, color)) > >> MemoryError > > >> How can I directlymergetwopicto a ‘P' modepngwith palette. > > > Nobody replied. > > What do you want to do with such a big image? You will run into the same > limitation when you are trying to display it. > I want to download a map from google map with high precision > Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: Announcing PythonTurtle
On Aug 4, 6:35 am, r wrote: [snip] > > I can remember the first time i used turtle (in python stdlib) and i > kept saying to myself... > > "Were the heck is this damn turtle?!?!" (>_<) > > :-) In Python2.6, try this: >>> turtle.shape('turtle') -- http://mail.python.org/mailman/listinfo/python-list
Re: Seeding the rand() Generator
On Sun, 2 Aug 2009 17:00:40 -0700 (PDT), Carl Banks wrote: I appreciate the response. I am executing a statement to retrieve one record at random. An example would be: SELECT first, second, third, fourth, fifth, sixth from sometable order by rand() limit 1 It would be contained as: stmt = "SELECT first, second, third, fourth, fifth, sixth from sometable order by rand() limit 1" Then I would execute it with the command: cursor.execute(stmt) How would I correctly seed the random generator in the MySQL statement from within Python? Regards, Fred -- http://mail.python.org/mailman/listinfo/python-list
[OT] Re: Ordering of dict keys & values
On Mon, Aug 3, 2009 at 9:18 PM, Gabriel Genellina wrote: > > [1] If you don't know what "SQL injection" means, see http://xkcd.com/327/ I love how XKCD is one of the preferred learning tools (along with Wikipeida) for people on this list. I think Randall Munroe should make a comic about it. :) -- http://mail.python.org/mailman/listinfo/python-list
Re: RUBY vs COMMON LISP
On Aug 3, 1:19 am, p...@informatimago.com (Pascal J. Bourguignon) wrote: > fft1976 writes: > > By the way, here is in 1 line of BF, a complete BF reader that is able > > to > > read all the BF syntax needed to write it: > > > ,+[-.,+] > > > Here's how to try it: > > > $ sudo apt-get install bf > > $ cat > reader.bf > > ,+[-.,+] > > $ bf reader.bf < reader.bf > > > Your 150 lines don't look very impressive now, do they? > > > Ruby < Lisp <<< BF! > > I specified a syntactic reader. Not just a reader. It is a syntactic reader. BF's syntax is just a sequence of characters. If you throw in illegal characters, the behavior is "undefined". Lisp's syntax is more complicated: it's a tree of identifiers (in its idealized form; of course, Common Lisp had to fuck it up). Ruby's and Python's syntaxes are even more complicated. The above was to illustrate the wrongness of your argument that the length of a self-parser determines the usefulness of the language. Hell, I know that BF can be a little *too* awesome. By the way, Python's syntax is much better than Ruby's. Dollar signs in front of variables? WTF were the designers smoking? That's like Perl! Haven't you learned your lesson? Python's syntax might even be better than Lisp's, but it's certainly harder to parse. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie Question regarding __init__()
Simon wrote: On Aug 2, 5:51 am, Dave Angel wrote: I don't understand your comparison to Foxpro. read on. As your code was last posted, you don't need a return value from init_Exec() Every function that doesn't have an explicit return will return None. And None is interpreted as False in an "and" expression. If you had an "if" around the whole thing, then you'd care. DaveA All I meant by the FoxPro comment was the idea of using the equal sign without a variable to throw away the result. Also in FoxPro there is no such thing as automatically returning None. If there is no explicit return then True is returned. Thanks I did not know that None is interpreted as False. Simon To throw away the result of an expression in Python is even easier. Just don't use it. func1() and func2() is a valid expression whose result is not used. And func2()'s result is therefore irrelevant. But shortcircuiting means that func2() is only called if func1() returned False (or something equivalent to it, like 0 or an empty list) -- http://mail.python.org/mailman/listinfo/python-list