UDP and Python2.7 and 2.7 documentation gives error!
UDP and Python2.7 and 2.7 documentation gives error! I am trying to send UDP messages from one PC to another, using P2.7. BUT I get an error I do not understand, this as I am following the doc's for Python2.7! Listing of the script with error result following: import socket UDP_IP = '192.168.0.90' UDP_PORT = 45121 msg = '7654321' print ("UDP target IP:", UDP_IP) print ("UDP target port:", UDP_PORT) print ("message:", msg) sock = socket.socket(socket.AF_INET, # Internet socket.SOCK_DGRAM) # UDP sock.bind((UDP_IP, UDP_PORT)) sock.send( msg ) C:\Utveckling\Counter_python>Counter_send.py ('UDP target IP:', '192.168.0.90') ('UDP target port:', 45121) ('message:', '7654321') Traceback (most recent call last): File "C:\Utveckling\Counter_python\Counter_send.py", line 13, in sock.bind((UDP_IP, UDP_PORT)) File "C:\Python27\lib\socket.py", line 228, in meth return getattr(self._sock,name)(*args) socket.error: [Errno 10049] The requested address is not valid in its context C:\Utveckling\Counter_python> So, please tell me where I am doing wrong, or is doc's wrong?? Nils in Uppsala -- -- https://mail.python.org/mailman/listinfo/python-list
Re: how to iterate over sequence and non-sequence ?
On Oct 19, 10:58 am, Steven D'Aprano <[EMAIL PROTECTED]> wrote: > On Fri, 19 Oct 2007 01:24:09 +0200, stef mientki wrote: > > hello, > > > I generate dynamically a sequence of values, but this "sequence" could > > also have length 1 or even length 0. > > > So I get some line in the form of: > > line = '(2,3,4)' > > line = '' > > line = '(2)' > > (in fact these are not constant numbers, but all kind of integer > > variables, coming from all over the program, selected from a tree, that > > shows all "reachable" variables) > > > So in fact I get the value from an exec statement, like this > > exec 'signals = ' + line > > And then, one day, somebody who doesn't like you will add the following > to your input data: > > "0; import os; os.system('rm # -rf /')" > > [ Kids: don't try this at home! Seriously, running that command will be > bad for your computer's health. Or at least it would, if I hadn't put a > spike in it. ] > > Don't use exec in production code unless you know what you're doing. In > fact, don't use exec in production code. > > > Now I want to iterate over "signals", which works perfect if there are 2 > > or more signals, > > but it fails when I have none or just 1 signal. > > for value in signals : > > do something > > No, I would say it already failed before it even got there. > > >>> line = '' > >>> exec 'signals = ' + line > > Traceback (most recent call last): > File "", line 1, in ? > File "", line 1 > signals = > ^ > SyntaxError: unexpected EOF while parsing > > This is the right way to deal with your data: > > input_data = """ (2, 3 , 4) > > (2) > (3,4,5) > ( 1, 2,3) > """ > > for line in input_data.split('\n'): > line = line.strip().strip('()') > values = line.split(',') > for value in values: > value = value.strip() > if value: > print(value) > > > As this meant for real-time signals, I want it fast, so (I think) I > > can't afford extensive testing. > > Don't guess, test it and see if it is fast enough. Some speed ups: > > If you're reading from a file, you can just say: "for line in file:" > instead of slurping the whole lot into one enormous string, then > splitting over newlines. > > If you can guarantee that there is no extra whitespace in the file, you > can change the line > > line = line.strip().strip('()') > > to the following: > > line = line.strip('\n()') > > and save a smidgen of time per loop. Likewise, drop the "value = > value.strip()" in the inner loop. > > -- > Steven. why not: >>> for i in eval('(1,2,3)'): ... print i 1 2 3 -- http://mail.python.org/mailman/listinfo/python-list
Re: dynamic invoke
On Oct 19, 12:39 pm, [EMAIL PROTECTED] wrote: > Hello, > > Is there any way (other then eval) to invoke a method by passing > method name in a string. > It's very simple in php: > $oFoo = new Foo(); > $dynamiMethod = "bar"; > $oFoo->$dynamiMethod(); > > Unfortunately I can't find a good solution to do the same thing in > python. Does it have some build-in function to do it? > > Kind Regards, > > Lukasz. Use apply(): http://docs.python.org/lib/non-essential-built-in-funcs.html Nils -- http://mail.python.org/mailman/listinfo/python-list
pre-uninstall script in bdist_wininst
Hi. I am using a postinstall-script like this: setup( ... scripts=['scripts\install.py'], options = { ... "bdist_wininst" : { "install_script" : "install.py", ... }, } ) According to the docs in [1] this script is a) called after install (with the "-install" parameter) - this works fine for me... b) called before uninstall (with tho "-remove" parameter) - this however does not work. Can someone please point me to the direction on how to get a pre- uninstall script working? btw: With that I am trying to register a com-server on install and de- register on uninstall - so if other ideas are around I'd love to hear them, too... Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: pre-uninstall script in bdist_wininst
On 5 Aug., 20:26, Nils wrote: > According to the docs in [1] [...] and with [1] I meant http://docs.python.org/distutils/builtdist.html#the-postinstallation-script Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: pre-uninstall script in bdist_wininst
On 6 Aug., 04:02, Mark Hammond wrote: > According to a comment in pywin32's post-install script: > > elif arg == "-remove": > # bdist_msi calls us before uninstall, so we can undo what we > # previously did. Sadly, bdist_wininst calls us *after*, so > # we can't do much at all. > Sadly, I can not confirm this. I wrote the simplest install-script (dump call-parameters to a txt-file) and tested with 2.6 and 2.7 On bdist_wininst my install_script was called on install with parameter "-install" On bdist_wininst my install_script was called on install without parameters My script was never (ever) called on uninstall... > I'd suggest using py2exe to package the object and inno installer or > similar to handle the install and uninstall parts. Yes, I'll try that, thanks. -- http://mail.python.org/mailman/listinfo/python-list
use of gtk in a nautilus extension
Hi, I am having some trouble opening a simple message/dialog to the user from a natilus extension.. I have written a simple nautilus extension using python. It adds one MenuItem to the context menu. for testing I wanted to open a simple dialog when the user clicks this menuitem. (The code can be found - with some nice formatting here: http://stackoverflow.com/questions/3325772/use-gtk-in-a-nautilus-extension-using-python) The code is as follows: --- cut --- import gtk import nautilus import os def alert(message): """A function to debug""" dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_CLOSE, message) dialog.run() dialog.destroy() class TestExtension(nautilus.MenuProvider): def __init__(self): pass def get_file_items(self, window, files): items = [] """Called when the user selects a file in Nautilus.""" item = nautilus.MenuItem("NautilusPython::test_item", "Test", "Test") item.connect("activate", self.menu_activate_cb, files) items.append(item) return items def menu_activate_cb(self, menu, files): """Called when the user selects the menu.""" for name in files: alert(name) --- cut --- When I click on the menuitem nothing happens... But when I replace def alert(message) like this: def alert(message): """A function to debug""" easygui.msgbox(message) and obviously drop import gtk and add import easygui, The dialog does appear. Can someone tell me why this is ?? Yours, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking for X availability
On Tue, Jan 11, 2005 at 03:32:01AM -0800, Flavio codeco coelho wrote: > So my question is: how can I check for the availability of X? i.e., > How will my program know if its running in a text only console or in > console window over X? Well, one way to do it is to check whether the environment variable DISPLAY is set (which it is when running under X, and should not be otherwise). Cheers, -- Nils Nordman <[EMAIL PROTECTED]> -- http://mail.python.org/mailman/listinfo/python-list
round() wrong in Python 2.4?
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) [GCC 3.3.6 (Debian 1:3.3.6-6)] on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>> $ python2.4 Python 2.4.1 (#2, Jul 12 2005, 09:22:25) [GCC 4.0.1 (Debian 4.0.1-1)] on linux2 >>> round(0.0225, 3) 0.021999 >>> "%.3f" % round(0.0225, 3) '0.022' >>> (Is this due to the different GCC used?) How do you correctly output floating-point numbers in 2.4? I do not like the "print number + EPS" solution, as you would need different EPS for different exponent sizes. In C you could get it by taking integer 1, and &-ing in the right exponent, and then casting to double via void*. This would not be very portable, though. Klem fra Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: round() wrong in Python 2.4?
I am running Debian unstable for 386. Python 2.4 is from the official package archive, and seems to be compiled with GCC 4.0.2. $ dpkg -l python2.4 ii python2.4 2.4.1-4 ... $ python2.4 Python 2.4.1+ (#2, Sep 4 2005, 21:58:51) [GCC 4.0.2 20050821 (prerelease) (Debian 4.0.1-6)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> $ gcc-4.0 --version gcc-4.0 (GCC) 4.0.2 20050725 (prerelease) (Debian 4.0.1-3) Klem fra Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2
Chris, Thanks for answering. Yes, it's email.mime.MIMEApplication. I've pasted a snippet with the imports below. I'm trying to use this to build a multi-part MIME message, with this as one part. I really can't figure out any way to attach a binary part like this to a multi-part MIME message without the encoding issue... any help would be greatly appreciate! Nils - import io from email.mime.application import MIMEApplication from email.generator import BytesGenerator from email.encoders import encode_noop app = MIMEApplication(b'Q\x0dQ', _encoder=encode_noop) b = io.BytesIO() g = BytesGenerator(b) g.flatten(app) for i in b.getvalue()[-3:]: print("%02x " % i, end="") print() On Wednesday, September 25, 2013 9:11:31 PM UTC-7, Chris Angelico wrote: > On Thu, Sep 26, 2013 at 2:38 AM, wrote: > > > app = MIMEApplication(b'Q\x0dQ', _encoder=encode_noop) > > > > What is MIMEApplication? It's not a builtin, so your test case is > > missing an import, at least. Is this email.mime.MIMEApplication? > > > > ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2
Hi Neil, Thanks for looking at this. I'm trying to create a multipart MIME for an HTTP POST request, not an email. This is for a third-party API that requires a multipart POST with a binary file, so I don't have the option to just use a different encoding. Multipart HTTP is standardized in HTTP 1.0 and supports binary parts. Also, no one will re-interpret contents of HTTP on the wire, as binary is quite normal in HTTP. The issue seems to be some parts of the python MIME encoder still assume it's for email only, where everything would be b64 encoded. Maybe I have to roll my own to create a multipart msg with a binary file? I was hoping to avoid that. Nils ps. You probably know this, but in case anyone else reads this thread, HTTP requires all headers to have CRLF, not native line endings. The python MIME modules can do that properly as of python 3.2 (fixed as of this bug http://hg.python.org/cpython/rev/ebf6741a8d6e/) > > I got interested in it since I have never used any of the > > modules. So I played with it enough to discover that the part of > > the code above that converts the \r to \n is the flatten call. > > > > I got to here and RFC 2049 and gave up. > > > >The following guidelines may be useful to anyone devising a data > >format (media type) that is supposed to survive the widest range of > >networking technologies and known broken MTAs unscathed. Note that > >anything encoded in the base64 encoding will satisfy these rules, but > >that some well-known mechanisms, notably the UNIX uuencode facility, > >will not. Note also that anything encoded in the Quoted-Printable > >encoding will survive most gateways intact, but possibly not some > >gateways to systems that use the EBCDIC character set. > > > > (1) Under some circumstances the encoding used for data may > > change as part of normal gateway or user agent > > operation. In particular, conversion from base64 to > > quoted-printable and vice versa may be necessary. This > > may result in the confusion of CRLF sequences with line > > breaks in text bodies. As such, the persistence of > > CRLF as something other than a line break must not be > > relied on. > > > > (2) Many systems may elect to represent and store text data > > using local newline conventions. Local newline > > conventions may not match the RFC822 CRLF convention -- > > systems are known that use plain CR, plain LF, CRLF, or > > counted records. The result is that isolated CR and LF > > characters are not well tolerated in general; they may > > be lost or converted to delimiters on some systems, and > > hence must not be relied on. > > > > So putting a raw CR in a binary chunk maybe be intolerable, and > > you need to use a different encoder. But I'm out of my element. > > > > -- > > Neil Cerutti -- https://mail.python.org/mailman/listinfo/python-list
Re: Newline interpretation issue with MIMEApplication with binary data, Python 3.3.2
Hi all, I was able to workaround this problem by encoding a unique 'marker' in the binary part, then replacing the marker with the actual binary content after generating the MIME message. See my answer on Stack Overflow http://stackoverflow.com/a/19033750/526098 for the code. Thanks, your suggestions helped me think of this. Nils On Wednesday, September 25, 2013 9:38:17 AM UTC-7, Nils Bunger wrote: > Hi, > > > > I'm having trouble encoding a MIME message with a binary file. Newline > characters are being interpreted even though the content is supposed to be > binary. This is using Python 3.3.2 > > > > Small test case: > > > > app = MIMEApplication(b'Q\x0dQ', _encoder=encode_noop) > > b = io.BytesIO() > > g = BytesGenerator(b) > > g.flatten(app) > > for i in b.getvalue()[-3:]: > > print ("%02x " % i, end="") > > print () > > > > This prints 51 0a 51, meaning the 0x0d character got reinterpreted as a > newline. > > > > I've tried setting an email policy of HTTP policy, but that goes even > further, converting \r to \r\n > > > > This is for HTTP transport, so binary encoding is normal. > > > > Any thoughts how I can do this properly? -- https://mail.python.org/mailman/listinfo/python-list
Re: multiprocessing problems
Hi Doxa, DoxaLogos wrote: [...] I found out my problems. One thing I did was followed the test queue example in the documentation, but the biggest problem turned out to be a pool instantiated globally in my script was causing most of the endless process spawn, even with the "if __name__ == "__main__":" block. Problems who solves them self, are the best problems ;) One tip: currently your algorithm has some overhead. 'Cause you are starting 4 time an additional python interpreter, compute the files and, closing all new spawned interpreter and starting again 4 interpreter, which are processing the files. For such kind of jobs I prefer to start processes once and feeding them with data via a queue. This reduces some overhead and increase runtime performance. This could look like this: (due some pseudo functions not directly executeable -> untested) import multiprocessing import Queue class Worker(multiprocessing.Process): def __init__(self, feeder_q, queue_filled): multiprocessing.Process.__init__(self) self.feeder_q = feeder_q self.queue_filled = queue_filled def run(self): serve = True # start infinite loop while serve: try: # scan queue for work, will block process up to 5 seconds. If until then no item is in queue a Queue.Empty will be raised text = self.feeder_q.get(True, timeout=5) if text: do_stuff(text) # very important! tell the queue that the fetched work has been finished # otherwise the feeder_q.join() would block infinite self.input_queue.task_done() except Queue.Empty: # as soon as queue is empty and all work has been enqueued # process can terminate itself if self.queue_filled.is_set() and feeder_q.empty(): serve = False return if __name__ == '__main__': number_of_processes = 4 queue_filled = multiprocessing.Event() feeder_q = multiprocessing.JoinableQueue() process_list =[] # get file name which need to be processed all_files = get_all_files() # start processes for i in xrange(0,number_of_processes): process = Worker(feeder_q, queue_filled) process.start() process_list.append(thread) # start feeding for file in all_files: feeder_q.put(file) # inform processes that all work has been ordered queue_filled.set() # wait until queue is empty feeder_q.join() # wait until all processed have finished their jobs for process in process_list: process.join() Cheers, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: multiprocessing problems
Hi, Adam Tauno Williams wrote: [...] Here's the guts of my latest incarnation. def ProcessBatch(files): p = [] for file in files: p.append(Process(target=ProcessFile,args=file)) for x in p: x.start() for x in p: x.join() p = [] return Now, the function calling ProcessBatch looks like this: def ReplaceIt(files): processFiles = [] for replacefile in files: if(CheckSkipFile(replacefile)): processFiles.append(replacefile) if(len(processFiles) == 4): ProcessBatch(processFiles) processFiles = [] #check for left over files once main loop is done and process them if(len(processFiles) > 0): ProcessBatch(processFiles) According to this you will create files is sets of four, but an unknown number of sets of four. This is not correct, 'cause of the x.join(). This will block the parent process until all processes have been terminated. So as soon as the current set of processes have finished their job, a new set will be spawned. Cheers, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: source install of python2.7 and rpm install of cx_Oracle collision
Hi Jim, Jim Qiu wrote: [...] I find out that only libpython2.7.a generated when I install python2.7, who can tell me what I need to do ? I want a libpython2.7.so.1.0 generated when I've didn't read your complete mail... In addition to the steps I've described in my other mail, you need to the "configure" script, that you like to have shared libraries. So you need to add --enable-shared to your configure call: ./configure --prefix=/opt/Python2.7a --enable-shared Now you got the shared libraries in the lib folder. Cheers, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: source install of python2.7 and rpm install of cx_Oracle collision
Hi Jim, Jim Qiu wrote: I make installed python 2.7 from source, and also installed the RPM version of cx_Oracle for python 2.7. But ldd tells me : #ldd cx_Oracle.so libpython2.7.so.1.0 => not found I find out that only libpython2.7.a generated when I install python2.7, who can tell me what I need to do ? I want a libpython2.7.so.1.0 generated when [...] Due to the fact that you have compiled python from source, the python library is not in the defaul library path. Due to that the lib can't be found. As a quick workourd you can extend the LD_LIBRARY_PATH variable with the path and check if cx_Orcacle get now the lib. Lets assume you "installed" python at /opt/Python2.7a, then you need to extend the variable in this way: export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/Python2.7a/lib" afterwards do the ldd again and check if the lib could be found now. If not, you've got the wrong path. Now you need to perist the changed library path. For this, you need to be root: 1. echo "/opt/Python2.7a/lib" > /etc/ld.so.conf.d/Pathon2.7a.conf 2. refresh your shared library cache with "ldconfig" Now you OS knows the new library location. But this is not clean. As Daniel mention, you should try to get a rpm. Otherwise you may get in trouble, if you install a newer Python2.7 version and forget to maintain you library paths. Cheers, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: seeking thru a file
Hi Mag, Mag Gam wrote: > I have a compressed CSV gziped file. I was wondering if it is possible > to seek thru a file > > For example: > > I want to load the first 100 lines into an array. Process the data > > Seek from 101 line to 200 lines. Process the data (remove lines 0 - > 100) from memory > > Seek 201 to 300 line. Process the data (remove 200-300)) from memory > > etc..etc.. > This would be very easy. Here is one way you could do it: (I didn't test the code; I've just write it down, assuming you use python 2.6, cause you didn't mentioned which version you are using...) import gzip step = 100 data_present = True with gzip.open(foo.csv.gzip) as my_file: counter = 0 my_buffer = [] while data_present: while counter <= step: line = my_file.readline() if line: my_buffer.append(my_file.readline()) counter += 1 else: data_present = False break if len(my_buffer) > 0: do_something(my_buffer) counter = 0 my_buffer = [] Kind Regards, Nils -- http://mail.python.org/mailman/listinfo/python-list
performance problem with time.strptime()
Hi everyone, In my free time I translate scripts from open source projects or write my own, to train my python skills. ATM I convert the aplogmerge.pl from awstats. It merges multiple apache logfiles and sort the output by the timestamps of each line. My first version of this script hasn't a good performance, so I started profiling. It turned out that the script spend much time for converting the timestamps of the line into a struct_time object. Here a code example (Python 2.6.2 on Ubuntu 7.10): Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def strptime(): m = Rec.match(Line) if m: date_string = m.group(1) # date_string example: '02/Jul/2009:01:50:26' return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") with timeit this functions takes approximate 125 sec and 29.004.081 function calls (I've configured timeit with 1.000.000 runs). A look at the output of cProfile told me that more than the half time is spent in locale.py: 102 11.7120.000 19.5920.000 locale.py:316(normalize) 1023.6390.000 23.2310.000 locale.py:382(_parse_localename) 1025.1620.000 30.2710.000 locale.py:481(getlocale) I studied the time documentation and thought that I had to set TZ in os environ: os.environ['TZ'] = 'Europe/Berlin' time.set() but that had no effect. I don't know why time.strptime() looks every time for my locale. Maybe it's a problem with my OS locale... However. I've introduced a work around, which works perfectly for my problem. For time comparison I could use any sort of time representation and so I convert to epoch: # needed to translate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) with this workaround I had a speed up to 4 times; it tooks only 31 sec with only 5.000.009 function calls. Maybe this helps some of you, who had the similar problems with time conversion ...But one big question remains: Why time.strptime() checks everytime the locale? had I missed something or could I have a problem with my OS locale? With python 3.1 there is no difference, unless that time.strptime() took approx 12 sec longer... :( regards, Nils Here a complete test script: #!/opt/python/2.6.2/bin/python import time import timeit import cProfile import re # needed to tranlate month str to dig repr Shortmonth = {'Jan' : '01', 'Feb' : '02', 'Mar' : '03', 'Apr' : '04', 'May' : '05', 'Jun' : '06', 'Jul' : '07', 'Aug' : '08', 'Sep' : '09', 'Oct' : '10', 'Nov' : '11', 'Dec' : '12'} Rec1 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(?P\d{2})/(?P\w+)/(?P\d{4}):(?P\d{2}):(?P\d{2}):(?P\d{2})\s\+\d{4}\].*") Rec2 = re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - "-" "www.example.org" "-" "-" "-"' def epoch(): m = Rec1.match(Line) if m: result = m.groupdict() date_tuple = (result["year"], Shortmonth[result["month"]], result["day"], result["hour"], result["min"], result["sec"], -1, -1, -1) date_tuple = map(int,date_tuple) return time.mktime(date_tuple) def strptime(): m = Rec2.match(Line) if m: date_string = m.group(1) return time.strptime(date_string, "%d/%b/%Y:%H:%M:%S") if __name__ == "__main__": t1 = timeit.Timer("epoch()","from __main__ import epoch") t2 = timeit.Timer("strptime()", "from __main__ import strptime") cProfile.run("t1.timeit();print") print "" cProfile.run("t2.timeit();print") -- http://mail.python.org/mailman/listinfo/python-list
Re: performance problem with time.strptime()
Hi Casey Casey Webster wrote: > On Jul 2, 7:30 am, Nils Rüttershoff wrote: > > >> Rec = >> re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") >> Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 >> - "-" "www.example.org" "-" "-" "-"' >> > > I'm not sure how much it will help but if you are only using the regex > to get the date/time group element, it might be faster to replace the > regex with: > > >>>> date_string = Line.split()[3][1:-1] >>>> Indeed this would give a little speed up (by 100 iteration approx 3-4 sec). But this would be only a small piece of the cake. Although thx :) The problem is that time.strptime() consult locale.py for each iteration. Here the hole cProfile trace: first with epoch and second with strptime (condensed): 509 function calls in 33.084 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0000.000 33.084 33.084 :1() 12.4172.417 33.084 33.084 :2(inner) 1009.6480.000 30.6670.000 time_test.py:30(epoch) 10.0000.000 33.084 33.084 timeit.py:177(timeit) 1003.7110.0003.7110.000 {built-in method groupdict} 1004.3180.0004.3180.000 {built-in method match} 10.0000.0000.0000.000 {gc.disable} 10.0000.0000.0000.000 {gc.enable} 10.0000.0000.0000.000 {gc.isenabled} 1007.7640.0007.7640.000 {map} 10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects} 1005.2250.0005.2250.000 {time.mktime} 20.0000.0000.0000.000 {time.time} 2909 function calls in 124.449 CPU seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 10.0000.000 124.449 124.449 :1() 12.2442.244 124.449 124.449 :2(inner) 1003.5000.000 33.5590.000 _strptime.py:27(_getlang) 100 41.8140.000 100.7540.000 _strptime.py:295(_strptime) 1004.0100.000 104.7640.000 _strptime.py:453(_strptime_time) 100 11.6470.000 19.5290.000 locale.py:316(normalize) 1003.6380.000 23.1670.000 locale.py:382(_parse_localename) 1005.1200.000 30.0590.000 locale.py:481(getlocale) 1007.2420.000 122.2050.000 time_test.py:37(strptime) 10.0000.000 124.449 124.449 timeit.py:177(timeit) 1001.7710.0001.7710.000 {_locale.setlocale} 1001.7350.0001.7350.000 {built-in method __enter__} 1001.6260.0001.6260.000 {built-in method end} 1003.8540.0003.8540.000 {built-in method groupdict} 1001.6460.0001.6460.000 {built-in method group} 2008.4090.0008.4090.000 {built-in method match} 10.0000.0000.0000.000 {gc.disable} 10.0000.0000.0000.000 {gc.enable} 10.0000.0000.0000.000 {gc.isenabled} 2002.9420.0002.9420.000 {len} 10.0000.0000.0000.000 {method 'disable' of '_lsprof.Profiler' objects} 3004.5520.0004.5520.000 {method 'get' of 'dict' objects} 1002.0720.0002.0720.000 {method 'index' of 'list' objects} 1001.5170.0001.5170.000 {method 'iterkeys' of 'dict' objects} 2003.1130.0003.1130.000 {method 'lower' of 'str' objects} 2003.2330.0003.2330.000 {method 'replace' of 'str' objects} 2002.9530.0002.9530.000 {method 'toordinal' of 'datetime.date' objects} 1001.4760.0001.4760.000 {method 'weekday' of 'datetime.date' objects} 1004.3320.000 109.0970.000 {time.strptime} 20.0000.0000.0000.000 {time.time} -- http://mail.python.org/mailman/listinfo/python-list
Re: Spam? Re: whizBase vs. Python
NurAzije wrote: > On Jun 29, 11:04 am, Tim Harig wrote: > >> On 2009-06-29, NurAzije wrote: >> >> >>> Hi, >>> I am working on a study and I need expert opinion, I did not work with >>> Python before, can anyone help me with a comparison betweenWhizBase >>> (www.whizbase.com) and Python please. >>> >> Given posts >> like:http://groups.google.com/group/Server-side-programing/browse_thread/t... >> is this just thinly veiled spam? The domain and the posting IP address are >> both out of Sarajevo, Bosnia. >> >> Obviously you alreadly know your product is nothing but a database >> macro processor. Python is a dynamic programming language and therefore >> far more capable. >> >> Your product is proprietary with less capability while Python is free and >> powerful. >> > > Hi Tim, > I am not a spammer, WhizBase is from Sarajevo and I am from Sarajevo, > for that it is interesting for me. > > regards, > Ashraf Gheith > Hi Ashraf, hm... but it seems you know WhizBase very well, here a blog (http://whizbase-scripting-language.blogspot.com/) of you with very detailed information about WhizBase. Why you are asking nearly any programming list/group about the difference?: http://www.highdots.com/forums/javascript-discussion-multi-lingual/whizbase-vs-javascript-281242.html http://groups.google.com/group/alt.comp.lang.learn.c-c++/browse_thread/thread/a7a47adb904319ad http://groups.google.com/group/comp.lang.lisp/browse_thread/thread/9dfa76ad94d75168/01a999b7b8638621?lnk=raot http://www.mail-archive.com/dotnetdevelopm...@googlegroups.com/msg07381.html Hm here the main blog: http://whizbase.blogspot.com/ There is a second contributer Faik Djikic and Whizbase is from a company called "Djikic Software Development".. What's the Topic of your study? Whizbase compared to other languages? Why Whizbase? Is your study public and if so were we could read it? ;) Regards, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: Seeding the rand() Generator
Hi Fred, I just saw your SQL Statement An example would be: SELECT first, second, third, fourth, fifth, sixth from sometable order by rand() limit 1 and I feel me constrained to give you an advice. Don't use this SQL statement to pick up a random row, your user and maybe DBA would much appreciate it. You are certainly asking why. Lets have a brief look what you are asking your mysql DB: Fetch all rows from 'sometable', but only with attribute 'first, second,...' sort them all starting at 'random row' and afterward through anything away you did before, but the first line If you have a table with 10 rows you would fetch and sort up to 10 rows, pick up one row and discard up to 9 rows. That sounds not very clever, right? So please take a look at this site to get a better alternate way for that approach: http://jan.kneschke.de/projects/mysql/order-by-rand/ if you want to know more please check this article too: http://jan.kneschke.de/2007/2/22/analyzing-complex-queries regards, Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: Standardizing RPython - it's time.
Hi, On 10/12/2010 07:41 AM, John Nagle wrote: [...] With Unladen Swallow looking like a failed IT project, a year behind schedule and not delivering anything like the promised performance, Google management may pull the plug on funding. Since there hasn't been a "quarterly release" in a year, that may already have effectively happened. are you sure that the project has failed? Yes the project page is outdated, but there is also pep-3146 [1] "Merging Unladen Swallow into CPython" Regarding the accepted pep, the merge is planed for Python 3.3. So for me it looks like the development itself has finished and the merging has started... Does anyone has more details? Regrads, Nils [1] http://www.python.org/dev/peps/pep-3146/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Standardizing RPython - it's time.
On 10/12/2010 05:18 PM, Nils Ruettershoff wrote: Hi, On 10/12/2010 07:41 AM, John Nagle wrote: [...] With Unladen Swallow looking like a failed IT project, a year behind schedule and not delivering anything like the promised performance, Google management may pull the plug on funding. Since there hasn't been a "quarterly release" in a year, that may already have effectively happened. are you sure that the project has failed? Yes the project page is outdated, but there is also pep-3146 [1] "Merging Unladen Swallow into CPython" Regarding the accepted pep, the merge is planed for Python 3.3. So for me it looks like the development itself has finished and the merging has started... I've just found a statement from Collin Winter [1]. He says, that he is working on other google projects, but they are still targeting the merge Unladen Swallow with cPython 3.3. The last commitmend to the branch (py3-jit) was in June this year So I would say the project is not dead or has failed, but delayed. Cheers, Nils [1] http://groups.google.com/group/unladen-swallow/browse_thread/thread/f2011129c4414d04 -- http://mail.python.org/mailman/listinfo/python-list
Keyboard problems with Python shell over SSH
Hello I'm not sure this is Python-related but it might be since Bash and vim works perfectly. I connect to my server using SSH and then run 'python' to enter the shell. I can't use the arrow buttons (up, down, left and right). Instead I get this ^[[A , ^[[B, ^[[C or ^[[D. How do I get my arrow buttons to work? Sorry if this is offtopic. Nils Emil P. Larsen -- My reply-address is valid. www.bios-flash.dk Min svar-adresse er gyldig. Redning af døde BIOS'er -- http://mail.python.org/mailman/listinfo/python-list
Re: Keyboard problems with Python shell over SSH
Hello Stian >Your Python installation is probably compiled without readline support. >It is the readline library that enables arrow keys and Ctrl-R and stuff >to work. >Try "import readline" - you will probably get an error. You are indeed right. "import readline" generated an error. I downloaded, compiled and installed GNU readline. Then I downloaded Python 2.4 source and configured it with ./configure --with-readline make make install This did the trick! Thank you! Nils Emil P. Larsen -- My reply-address is valid. www.bios-flash.dk Min svar-adresse er gyldig. Redning af døde BIOS'er -- http://mail.python.org/mailman/listinfo/python-list
Comm. between Python and PHP
Hello I'm building a daemon in Python. It will measure and control some physical devices on a serial bus. Since it is a daemon, it will never terminate and I can't interfere with the regulation of the devices by using command line parameters. I want to control the regulation by using a Internet browser. What is the easiest way to make my threaded Python daemon communicate with a PHP-script running from Apache2 on localhost? Thank you so far! Nils Emil P. Larsen -- My reply-address is valid. www.bios-flash.dk Min svar-adresse er gyldig. Redning af døde BIOS'er -- http://mail.python.org/mailman/listinfo/python-list
Re: Comm. between Python and PHP
Hello >Python is perfectly capable of generating HTML. You don't have to demean >yourself by working in PHP. Thanks for the tip about using Python instead of PHP to generate web pages. I may follow it. Nils Emil -- My reply-address is valid. www.bios-flash.dk Min svar-adresse er gyldig. Redning af døde BIOS'er -- http://mail.python.org/mailman/listinfo/python-list
Re: Comm. between Python and PHP
Hello Sorry for not being too exact in my request! >If the data you want to pass is structured then you might consider >XML-RPC which is a cross platform way of passing structured data XML-RPC looks like something very suitable for my needs. It seems Python handles the remote procedure calls very easily (almost like local functions). It also works quite well in PHP. I didn't recompile anything but downloaded Keith Devens XML-RPC Library (500 lines code). With this I can call RPC-functions almost as easily as with Python. It took me just about an hour to get it working... Thanks everybody! Nils Emil P. Larsen -- My reply-address is valid. www.bios-flash.dk Min svar-adresse er gyldig. Redning af døde BIOS'er -- http://mail.python.org/mailman/listinfo/python-list
Critic: New Python Front Page
Hi, (Please forgive (or correct) my mistakes, i am non native) http://www.python.org/community/sigs/retired/parser-sig/towards-standard/ * Formatting bug * Needs breadcrumb navigation ("Where am i?") (i came there via http://theory.stanford.edu/~amitp/yapps/) * blue background makes top links invisible when i look on laptop screen from a 60% angle. Prefer white. * 1 third of space on y-axis is used for 15 navigation links and a search bar. * 1 third of space on x-axis is used for one sentence about and a link to PSF. * I can not easily get rid of this by using e.g. HackTheWeb (https://addons.mozilla.org/de/firefox/addon/hack-the-web/). Please compare with e.g. Wikipedia, it is easy there to isolate the main content. http://www.python.org/community/ That picture is scary. Regards, Nils -- Nils-Hero Lindemann -- https://mail.python.org/mailman/listinfo/python-list
Re: Critic: New Python Front Page
Hello, > The source for the site is on github, and they are taking and resolving > issues there: https://github.com/python/pythondotorg/issues Thanks for pointing me to the right place. I copypasted my mail to ... https://github.com/python/pythondotorg/issues/266 also i added a comment to ... https://github.com/python/pythondotorg/issues/265 Regards, Nils -- Nils-Hero Lindemann -- https://mail.python.org/mailman/listinfo/python-list
Re: Letting a Python application phone home
Am Freitag, 14. Juli 2006 15:26 schrieb Dieter Vanderelst: This is surely possible. You need to define a protocol for the communication between client and server. As you are planning to send data over the internet you should build it on top of tcp. Look at the python module "socket" resp. "SocketServer" for low level tcp functions. As the data should not be visible to everyone you need some kind of encryption. Either you rely on the ssl capabilities of the "socket" module, this way securing the transport layer (tcp) or you build encryption into your newly designed application layer protocol. For the latter have a look at the "python cryptography toolkit". Sorry I have no url at hand, simply google. Concerning the question of stopping the program when the internet connection breaks: let your program contact the server periodically. If this fails quit the program. Regards Nils -- http://mail.python.org/mailman/listinfo/python-list
Google code search (Was: Names changed to protect the guilty)
Google has a cool new service. http://www.google.com/codesearch You can use regular expressions! (I found at least 13 distinct utilities that used the idiom.) Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: The Nature of the “Unix Philosophy”
Xah Lee wrote: > The Nature of the “Unix Philosophy” > > Xah Lee, 2006-05 > > In the computing industry, especially among unix community, we often > hear that there's a “Unix Philosophy”. In this essay, i dissect the > nature and characterization of such “unix philosophy”, as have been > described by Brian Kernighan, Rob Pike, Dennis Ritchie, Ken Thompson, > and Richard P Gabriel et al, and in recent years by Eric Raymond. > > There is no one definite set of priciples that is the so-called “unix > philosophy”, but rather, it consistest of various slogans developed > over the decades by unix programers that purport to describe the way > unix is supposed to have been designed. The characteristics include: > “keep it simple”, “make it fast”, “keep it small”, “make > it work on 99% of cases, but generality and correctness are less > important”, “diversity rules”, “User interface is not > important, raw power is good”, “everything should be a file”, > “architecture is less important than immediate workability”. Often, > these are expressed by chantible slogans that exhibits juvenile humor, > such as “small is beautiful”, “KISS (Keep It Simple, Stupid)”. Perhaps you should take a peek at the ideas in Plan 9 from Bell Labs, which is a continuation of this philosophy, unlike the "modern" unix clones. -- http://mail.python.org/mailman/listinfo/python-list
Re: calling a class instance of function
Hi, Methods i.e functions bound to a class instance (or object) the self argument in their definition: [code] class pid: def add(self, toadd): pass #or some sensible code [/code] If you want to define a method without that implicit self argument you'll have to make this method a static: [code] class pid: @staticmethod def staticadd (param1, param2): pass #or some sensible code [/code] Furthermore, your test() seems to be rather a function than a class: You want to use this function to test the pid class but what you do with your code below is to define a class test which inherits pid. I'm not really sure what the following lines do ... this would usually be the place to introduce class variables. Hope that Helps! Greetings Nils Original-Nachricht Datum: 21 Dec 2006 11:09:32 +1100 Von: Pyenos <[EMAIL PROTECTED]> An: python-list@python.org Betreff: calling a class instance of function > > class pid: > "pid" > def add(original_pid,toadd): > "add pid" > original_pid.append(toadd) > return original_pid > def remove(something_to_remove_from,what): > "remove pid" > something_to_remove_from.remove(what) > return something_to_remove_from > def modify(source,element,changewiththis): > "modify pid" > source[source.index(element)]=changewiththis > return source > > class test(pid): > pid.original=[1,2,3] > pid.toadd=4 > pid.add(pid.original,pid.toadd) # error here says that > # it expects pid instance as first arg > # not a list that it got. > > why do i get an error? > -- > http://mail.python.org/mailman/listinfo/python-list -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer -- http://mail.python.org/mailman/listinfo/python-list
Re: what is wrong with my code?
Hi, this is the line that breaks your code: def progressTable(progress_table, action, task, pid=len(progress_table) your parameter progress_table is known inside the function, not inside its definition. So "pid=len(progress_table)" won't do. If you really think that it is possible that pid is anything else but "len(progress_table)", you should use for example -1 as the default value and calculate the length inside your functions if the parameter is not different. Otherwise dump this parameter. Hope that helps! Greetings Nils Original-Nachricht Datum: 21 Dec 2006 09:16:58 +1100 Von: Pyenos <[EMAIL PROTECTED]> An: python-list@python.org Betreff: what is wrong with my code? > import cPickle, shelve > > could someone tell me what things are wrong with my code? > > class progress: > > PROGRESS_TABLE_ACTIONS=["new","remove","modify"] > DEFAULT_PROGRESS_DATA_FILE="progress_data" > PROGRESS_OUTCOMES=["pass", "fail"] > > > def unpickleProgressTable(pickled_progress_data_file): > > return unpickled_progress_table > > def pickleProgressTable(progress_table_to_pickle): > > return pickled_progress_data_file > > # Of course, you get progress_table is unpickled progress table. > def progressTable(progress_table, action, task, > pid=len(progress_table), outcome=PROGRESS_OUTCOMES[1]): > pid_column_list=progress_table[0] > task_column_list=progress_table[1] > outcome_column_list=progress_table[2] > > # But a task must also come with an outcome! > def newEntry(new_task, new_outcome): > new_pid=len(task_column_list) > > pid_column_list.extend(new_pid) > task_column_list.extend(new_task) > outcome_column_list.extend(new_outcome) > > def removeEntry(pid_to_remove, task_to_remove): > > if > pid_column_list.index(pid_to_remove)==task_column_list.index(task_to_remove): > # Must remove all columns for that task > index_for_removal=pid_column_list.index(pid_to_remove) > > pid_column_list.remove(index_for_removal) > task_column_list.remove(index_for_removal) > outcome_column_list.remove(index_for_removal) > > # Default action is to modify to pass > def modifyEntry(pid_to_modify, > outcome_to_modify=PROGRESS_OUTCOMES[0]): > index_for_modifying=pid_column_list.index(pid_to_modify) > > # Modify the outcome > outcome_column_list[index_for_modifying]=outcome_to_modify > -- > http://mail.python.org/mailman/listinfo/python-list -- Der GMX SmartSurfer hilft bis zu 70% Ihrer Onlinekosten zu sparen! Ideal für Modem und ISDN: http://www.gmx.net/de/go/smartsurfer -- http://mail.python.org/mailman/listinfo/python-list
Re: Python is removing my quotes!
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Robert Dailey schrieb: > Thank you for your response. The back slashes work! It's a bit annoying; but > I have Microsoft to thank for that. It's not Microsoft, you would experience the same problems under any Unix I know of. At least the bash treats quotes as special characters, so does afaik the sh and a couple of other shells in the Unixverse. You will also need to escape blanks and depending on the shell a number of other special characters (like for example the pipe). Greetings Nils -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGyye3zvGJy8WEGTcRAuWiAJ9SVUmNtQDkeGO1zBIXLeL548kXswCeMAEA zYMRE8LSV/9kYiAQkCz1PAU= =oDke -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: How to optimise this code?
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 David N Montgomery schrieb: > class testCase: > def __init__(self, tc): > if tc == 1:self.testCase1() > if tc == 2:self.testCase2() > if tc == 3:self.testCase3() > if tc == 4:self.testCase4() > if tc == 5:self.testCase5() > if tc == 6:self.testCase6() > > def testCase1(self): > print "tc1" > > def testCase2(self): > print "tc2" > > def testCase3(self): > print "tc3" > > def testCase4(self): > print "tc4" > > def testCase5(self): > print "tc5" > > def testCase6(self): > print "tc6" > > > def testCaseX(self): > print "tcX" > > totalNumberOfTestCases = 6 > x = 0 > while x <= totalNumberOfTestCases: > x += 1 > testCase(x) > > > This template code is working, but I envisage having 100+ test cases and > am concerned about my useage of if statements. I would be grateful for > any pointers as to how I can run all tests cases, regardless of how > many, in a more efficient manner. > > Thank you in advance. Hi, generally a flexible way to avoid huge blocks of if elsif elsif elsif ... ad infintum is to use pythons built in dictionary like this: class Test: def __init__(self): self.cases = {1:self.test1, 2:self.test2, 3:self.test3 } self._member = "I'm Test" def test1(self, param): print "one" print param print self._member def test2(self, param): print "two" print param print self._member def test3(self, param): print "three" print param print self._member if __name__ == "__main__": t = Test() for x in range(1, 4): t.cases[x](x) hope that helps ... -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGyyDxzvGJy8WEGTcRAn2XAJ97z8o6Sxpi7euPtPUsm/FrD1bgEgCfeRcs TKzwnkIdbs3GRI0yXcVUugM= =HIJc -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Threads and racing conditions
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Flavio Preto schrieb: > Hi, > > I have a doubt. Supose that i have the minimun class below: > > class db: > def __init__(self): > self.db = {} > def read(self, key): > return self.db[key] > def write(self, key, value): > self.db[key] = value > > > and an object of this class is shared among some threads. Is it possible > that in a read, the method return a value that is not an old or a new value? > In other words, is it possible that a 'read' return (due to a 'write' at the > same time by another thread) an invalid value that was never supposed to be > there? > > Thanks, > Flavio Preto > > Make the class thread safe by using Lock or RLock ... that will save you a lot of trouble. Your limited example might or might not work, but once your class gets more complex you will most likely run into serious problems. Greetings Nils -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGzHsizvGJy8WEGTcRAgN1AJ42cM1P/NW7Ei3F5ViSsmTcKDvCIgCeIUGG GD7DAb3f+Lmcav663F+wkQg= =kJ0G -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Class destruction
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Robert Dailey schrieb: > Hi, > > I'm wondering where the most appropriate location is to cleanup class > objects. For example, i have a file handle as an instance attribute in one > of my classes and I need to call f.close() on it when the class object falls > out of scope. Any ideas? I've tried __del__() but I don't remember this > working for some reason. I might try it again later just to be positive. __del__(self) is the perfectly right place for such cleanup ... it gets called once your instance is either deleted explicitly by you or it's handled by the garbage collector when there are no more references. The possible problem why this did not work four you is, that the destruction by the garbage collector cannot be predicted ... it may well take some time. If you try to open the same file from another class before yours gets cleaned you run into trouble. If you want to "reuse" the file, you will have to delete your classes instance explicitly using the del statement ... this will also call the destructor. The below code works fine: def __del__( self ): self._file.close() print "File closed" Hope that helps ... Nils -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFGzISBzvGJy8WEGTcRAiOwAJ94fJza4/GVQsFmbXwsP8kdvQjV5wCfQktw F/zPJAw0ayjYe5MGxPR1YqI= =4Hl6 -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: pdf to text
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 have a look at the pdflib (www.pdflib.com). Their Text Extraction Toolkit might be what you are looking for, though I'm not sure whether you can use it detached from the pdflib itself. hth Nils tubby schrieb: > I know this question comes up a lot, so here goes again. I want to read > text from a PDF file, run re searches on the text, etc. I do not care > about layout, fonts, borders, etc. I just want the text. I've been > reading Adobe's PDF Reference Guide and I'm beginning to develop a > better understanding of PDF in general, but I need a bit of help... this > seems like it should be easier than it is. Here's some code: > > import zlib > > fp = open('test.pdf', 'rb') > bytes = [] > while 1: > byte = fp.read(1) > #print byte > bytes.append(byte) > if not byte: > break > > for byte in bytes: > > op = open('pdf.txt', 'a') > > dco = zlib.decompressobj() > > try: > s = dco.decompress(byte) > #print >> op, s > print s > except Exception, e: > print e > > op.close() > > fp.close() > > I know the text is compressed... that it would have stream and endstream > makers and BT (Begin Text) and ET (End Text) and that the uncompressed > text is enclosed in parenthesis (this is my text). Has anyone here done > this in a simple fashion? I've played with the pyPdf library some, but > it seems overly complex for my needs (merge PDFs, write PDFs, etc). I > just want a simple PDF text extractor. > > Thanks -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.3 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFFuSPozvGJy8WEGTcRAnY0AJ0VZez3XRbLm/JXZKhn/rgHP0R3qwCfWAnT EupBECHab2kG33Rmnh+xf74= =INM5 -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Interpreter window
I have created a Python application in Windows XP which uses WxWidgets. When I start the application from the Python interpreter I get one empty interpreter window in addition to the application window. Is there a way to close the interpreter window without closing the application? Or, can I start the interpreter and the application script without starting the interpreter window? -- http://mail.python.org/mailman/listinfo/python-list
Re: Interpreter window
On 2 Feb, 13:07, "skyofdreams" <[EMAIL PROTECTED]> wrote: > "Nils Overas Bergen" <[EMAIL PROTECTED]> > [EMAIL PROTECTED] > > >I have created a Python application in Windows XP which uses > > WxWidgets. When I start the application from the Python interpreter I > > get one empty interpreter window in addition to the application > > window. Is there a way to close the interpreter window without closing > > the application? Or, can I start the interpreter and the application > > script without starting the interpreter window? > > do you mean console window? > you can try pythonw.exe instead of python.exe > > -sods Thanks! Now my application starts without the console window. Nils -- http://mail.python.org/mailman/listinfo/python-list
Re: Cant run application as ./myapp.py
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi Robert, I have to guesses: a) The user as which you are trying to run the script is missing the execute right -> fix by chmod u+x MyApplication.px b) You did not specify the interpreter -> the first line of your script should look something like #!/usr/bin/python If none of this is the problem please post the error message you (hopefully) get. hth Nils Robert Rawlins schrieb: > Hello Guys, > > > > I’ve got an application here which for some reason won’t start using the > following syntax from the command line: > > > > Cd /mydirectory > > ./MyApplication.py > > > > I have to run this as a fully qualified python launch such as: > > > > Cd /mydirectory > > python MyApplication.py > > > > This is a little bit confusing to me as I have other applications which > run just fine using the ./somthing.py syntax. Is there any reason why > this doesn’t work? > > > > Cheers, > > > > Robert > -BEGIN PGP SIGNATURE- iD8DBQFHzALBzvGJy8WEGTcRAtE8AJ4jGFTjZ8G8ayZM2AUcLcArnF5d1QCdH0gj kCdp0414HwPaIMIDv/SSTZA= =tF3K -----END PGP SIGNATURE- begin:vcard fn;quoted-printable:Nils Oliver Kr=C3=B6ger n;quoted-printable:Kr=C3=B6ger;Nils Oliver email;internet:[EMAIL PROTECTED] version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Re: Exception or not
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hello, I don't think it is a good pattern because you are kind of mixing exceptions with return codes which makes the code a lot less readable later on. I personally would strongly opt for return codes in this case as one would intuitively expect a function named validateThisAndThat to return the result of a validation. This might me a simple true/false, a numeric code with 0=OK, 1=password not correct, 2=user does not exist or a string, whatever you need. In my opinion, such a function should raise an exception if it is unable to fullfill its task. For example lost connection to user database or things like that. A function should never propagate an expected result as an exception. Greetings Nils Monica Leko schrieb: | Suppose you have some HTML forms which you would like to validate. | Every field can have different errors. For example, this are the | forms: | | username | password | etc | | And you want to validate them with some class. Is this good pattern: | | class Foo(object): | def validateUsername(username): | if username isn't correct: | raise ValidationError() | def validatePassword(password): | if password isn't correct: | raise ValidationError() | | code: | try: | usernameError = validateUsername() | except ValidationError: | usernameError = error from exception | try: | passwordError = validatePassword() | except ValidationError: | passwordError = error from exception | | So, if there wasn't any errors, usernameError and passwordError both | contains None, and there was error, both contains some string? Should | I use exception or just return None or some string without | exception? -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.6 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFHzCGhzvGJy8WEGTcRAvbGAJoDjn39xCmiOLmkc//0RTfeVXJFTACePRIG uYoDiQBZwRsShUn60LN/9oQ= =zvAY -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Query: Related to locking a resource in a multithreaded environment
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi, I don't think the global interpreter lock is what you need ... read here for reference: http://docs.python.org/api/threads.html My approach what be to write one class for reading and writing to the configuration and make that class thread-safe using the RLock. Then you create one and only one instance of this class and let all your threads use (not import) that one instance. You could achieve that "one and only one" either by having a main program which creates the instance and pass the reference to each thread via its __init__ or you implement the Configuration class as a singleton like this: class Singleton(object): """ A simple example implementing the singleton design pattern in python """ #we need two class attributes (which translate to static attributes in java) __lock = Lock() #a lock for thread safety __instance = None #and to remember the one instance #first of all: make pythons usual way of creating objects # unusable because we cannot just hide them as one would in java # or C++ def __new__(cls, *args, **kwargs): pass def __init__(self): pass @classmethod def getInstance(cls, *args, **kwargs): """ The famous gatInstance method which resturns the one instance of our class. params: cls - reference to the class *args - the tuple of arguments paassed by position **kwargs - the dictionary of arguments passed by keyword """ #enter critical section cls.__lock.acquire() try: if cls.__instance is None: #use the superclasses __new__ for creation cls.__instance = object.__new__(cls, *args, **kwargs) #place initialisation code (everything which #would usually happen in __init__) here cls.__instance.SomeInt = 1 finally: #leave critical section cls.__lock.release() return cls.__instance Add your methods for accessing as instance methods to this class and get the same instance of this class in each thread by calling Singleton.getInstance(). Hope that helps Regards Nils tarun schrieb: > I think I need something called global interpreter lock which is > accessible to all the threads. But I am not sure how to implement this. > > > On Tue, Aug 19, 2008 at 11:28 AM, tarun <[EMAIL PROTECTED] > <mailto:[EMAIL PROTECTED]>> wrote: > > Hello All, > > I've a configuration.ini file. This particular can be accessed by > several threads of my application. Each thread can read/write > configuration parameters from/to the configuration.ini file. I am > using threading (Rlock) to lock the resource (configuration.ini > file) while accessing it from any thread. I use the file available > at http://www.voidspace.org.uk/python/configobj.html for read/write. > > I did the following in one of my files: > > import threading > class Synchronization: > def __init__(self): > self.mutex = threading.RLock() > > Now this I can create instances of the class Synchronization and use > acquire/release to work on the shared resource. But every thread > would need to import this class and hence each thread would create a > separate instance of the class. This means several lock variables. > But I think we need a global lock flag that is accessible across all > the threads. > > How do i do this? > > Please let me know ASAP. > > Thanks In Advance, > Tarun > > > > > > -- > http://mail.python.org/mailman/listinfo/python-list -BEGIN PGP SIGNATURE- iD8DBQFIqxfCzvGJy8WEGTcRApe+AJ9MNqWI9FOsJIKuTKxy8ZNSGYTy2gCdHtGc clDPMMAPRoIxsBvVm4ygi6U= =vIPW -END PGP SIGNATURE- begin:vcard fn;quoted-printable:Nils Oliver Kr=C3=B6ger n;quoted-printable:Kr=C3=B6ger;Nils Oliver email;internet:[EMAIL PROTECTED] version:2.1 end:vcard -- http://mail.python.org/mailman/listinfo/python-list
Python 3.5.0 Feedback
Hallo, Zuerst mal, ein dickes Danke an alle, die an Python mitarbeiten! Hier ein paar Dinge, die mir an 3.5.0 aufgefallen sind (ich komme von 3.2.3): === Informitis === * Die Namen der Links im Startmenü und die erste Zeile der interaktiven Kommandozeile sind von (noch größerer) Informitis befallen. Wie auch schon die Doku seit langem. Informitis: Informationen im unpassenden Format an eine unpassende Stelle setzen. === Symbole === * Die Python-Dateien haben keine Symbole im Explorer. * Das Symbol von IDLE is grau und wenig ansehnlich. * In der Symboleiste: 16x16 Symbole von Kommandozeilen haben einen schwarzen "Schmutzfleck" oben links. Gräßlich! === Installer === * Der neue Installer lässt beim Vorkompilieren nicht mehr die gerade kompilierten Dateien durchrasen, so wirkt es, als sei der Installer eingefroren. * Auch verwendet der Installer bei der fortgeschrittenen Installation einige Formulierungen, die schwer zu verstehen sein könnten: "requires Elevation" (Was ist "Elevation"? leo.org gibt nur: "Erhöhung") "Install Debugging Symbols" (Was sind Debugging Symbole? Icons?) "Create Shortcuts for installed applications" (Das sollte "create shortcuts in Startmenu and on Desktop" sein) === HTML Doku === * JavaScript-Suchfunktion der HTML Doku zeigt in der Beschreibung von gefundenen Links Dinge wie '= (etc)' an. * Globale Funktionen wie 'open' sind nicht mehr an der ersten Stelle bei der Suche (welches Sie zwischendurch mal waren, glaube ich). === Bugs === * Python 3.5 verträgt a=input('\n') nicht. Gruß, Nils -- Nils-Hero Lindemann -- https://mail.python.org/mailman/listinfo/python-list