Re: Can Python help?
Lad schrieb: > On my website I allow users to upload files. I would like a user to see > how much time is left before a file is uploaded. So, I would like to > have a progress bar during a file uploading. Can Python help me with > that?Or how can be a progress bar made? > Thank you for ideas. > La. > http://docs.turbogears.org/1.0/FileUploadProgressBar -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
On Tue, 26 Dec 2006 15:49:10 +0800, [EMAIL PROTECTED] wrote: > Hi, > > I'm writing a program which imports an external module writing in C and > calls a function provided by the module to do my job. But the method > produces > a lot of output to the stdout, and this consumes most of the running time. > > My question is, is there a way to depress the output produced by the > function and hence make my program run faster? It's too complicated for me > to modify the source code and recompile the external module. Try something like this: # WARNING: untested def run_without_stdout(*args, **kwargs): function = args[0] args = args[1:] savestdout = sys.stdout sys.stdout = cStringIO.StringIO() result = None try: result = function(*args, **kwargs) finally: # don't forget to restore stdout, or you # really will regret it... sys.stdout = savestdout return result -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
On Tuesday, 26.12.06 at 21:28, Steven D'Aprano wrote: > > # WARNING: untested > def run_without_stdout(*args, **kwargs): > function = args[0] > args = args[1:] > savestdout = sys.stdout > sys.stdout = cStringIO.StringIO() > result = None > try: > result = function(*args, **kwargs) > finally: > # don't forget to restore stdout, or you > # really will regret it... > sys.stdout = savestdout > return result > There's no need for savestdout. There's a backup copy in sys.__stdout__ -Luis -- http://mail.python.org/mailman/listinfo/python-list
Splitting lines from a database query
I have an application where I need to take a query from an existing database and send it to a web api. Here's a cut down version of my existing code: for foo in mssql.fetch_array(); bar = foo[2] #trims the first result which we don't use for x in bar: for y in x: print y print This produces each value on a line, here's an example result: Jane Mary SIMPSON 0411231234 Dr I Feelgood 2006-12-27 15:00:00 John DOE None Dr I Feelgood 2006-12-27 15:30:00 Frank SPENCER Dr I Feelgood 2006-12-27 16:00:00 There are always 5 values, but some are blank and some are 'None'. I'd like to split the lines so I get something resembling XML, like this: Frank Spencer Dr I Feelgood 2006-12-27 16:00:00 Thanks in advance for your help, Peter. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to stop program when threads is sleeping
many_years_after wrote: > Carsten Haese wrote: > > On Sun, 2006-12-24 at 22:55 -0800, many_years_after wrote: > > > Hi, pythoners: > > > > > > There is a problem I couldn't dispose. I start a thread in the my > > > program. The thread will do something before executing time.sleep(). > > > When the user give a signal to the main thread (such as click the 'end' > > > button or close the window), the thread should end it's running. But > > > how to end the threading when it's sleeping? I set an flag to the > > > thread, but it doesn't work. > > > > Is the thread supposed to do some additional work after being woken up? > > If not, there is no point in going to sleep in the first place and the > > thread should just terminate when it has completed its task. If yes, I'd > > use a threading.Event object to .wait() on in the sub-thread rather than > > putting it to sleep, and then .set() the event object in the main thread > > when it's time to wake up the sub-thread. > > > > Hope this helps, > > > > Carsten. > > While , there is something wrong in my expression. What I mean is the > thread will wait some time after doing some tasks. I want to know is > there any method to end the thread or make it out of execution of > waiting. I use time.sleep() to let the thread wait. So you want a way of stopping a thread while its blocked (waiting) what ever seconds in the time.sleep() method ? > Thanks. If the thread is supposed to do more operation then i would follow what Carsten suggested. Cheers -- http://mail.python.org/mailman/listinfo/python-list
Formatting a string to be a columned block of text
Hi, I'm creating a python script that can take a string and print it to the screen as a simple multi-columned block of mono-spaced, unhyphenated text based on a specified character width and line hight for a column. For example, if i fed the script an an essay, it would format the text like a newspaper on the screen. Text processing is very new to me, any ideas on how I could achieve a multi-columned text formatter. Are there any libraries that already do this? Thanks and happy holidays! Leon -- http://mail.python.org/mailman/listinfo/python-list
Re: BeautifulSoup vs. loose & chars
John Nagle wrote: > I've been parsing existing HTML with BeautifulSoup, and occasionally > hit content which has something like "Design & Advertising", that is, > an "&" instead of an "&". Is there some way I can get BeautifulSoup > to clean those up? There are various parsing options related to "&" > handling, but none of them seem to do quite the right thing. > >If I write the BeautifulSoup parse tree back out with "prettify", > the loose "&" is still in there. So the output is > rejected by XML parsers. Which is why this is a problem. > I need valid XML out, even if what went in wasn't quite valid. > > John Nagle So do you want to remove "&" or replace them with "&" ? If you want to replace it try the following; import urllib, sys try: location = urllib.urlopen(url) except IOError, (errno, strerror): sys.exit("I/O error(%s): %s" % (errno, strerror)) content = location.read() content = content.replace("&", "&") To do this with BeautifulSoup, i think you need to go through every Tag, get its content, see if it contains an "&" and then replace the Tag with the same Tag but the content contains "&" Hope this helps. Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Peter Machell wrote: > I have an application where I need to take a query from an existing > database and send it to a web api. [...] > There are always 5 values, but some are blank and some are 'None'. > I'd like to split the lines so I get something resembling XML, like this: > Frank > Spencer > > Dr I Feelgood > 2006-12-27 16:00:00 quick and dirty solution: bar = ( ("Jane Mary","SIMPSON","0411231234","Dr I Feelgood","2006-12-27 15:00:00"), ("John","DOE","None","Dr I Feelgood","2006-12-27 15:30:00"), ("Frank","SPENCER","","Dr I Feelgood","2006-12-27 16:00:00") ) spam ="FNAME", "SNAME", "PHONE", "DR","TIME" for x in bar: i=0 while i<5: if x[i] != 'None': print "<%s>%s" % (spam[i], x[i], spam[i]) else: print "<%s>" % (spam[i], spam[i]) i+=1 print -- Under construction -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
Steven D'Aprano wrote: > Try something like this: > > # WARNING: untested > def run_without_stdout(*args, **kwargs): > function = args[0] > args = args[1:] > savestdout = sys.stdout > sys.stdout = cStringIO.StringIO() > result = None > try: > result = function(*args, **kwargs) > finally: > # don't forget to restore stdout, or you > # really will regret it... > sys.stdout = savestdout > return result > Thanks! I have tried your method, but I found it didn't work as expected. The output produced by the external function couldn't be depressed, but the "print " statement i wrote in python is depressed. It seems make cStringIO.StringIO() as a temporary replacement of sys.stdout has no effect on the external function. Here is an example to make myself clear(actually it's modified version of Steven's code): def run_without_stdout(*args, **kwargs): function = args[0] args = args[1:] savestdout = sys.stdout sys.stdout = cStringIO.StringIO() print "something" result = None try: result = function(*args, **kwargs) finally: # don't forget to restore stdout, or you # really will regret it... sys.stdout = savestdout print "some other thing" return result When run_without_stdout() is called, the "print" statements wrote in python don't produce output, but function() produces output to the standard output just as before:( I have tried to replace sys.stdout globally with cStringIO.StringIO() in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a globall statement), but it worked just as previous version did. Regards, xiaojf -- http://mail.python.org/mailman/listinfo/python-list
embedded python : can't get or set a variable
Hello, I'm trying to write a program to send python statements to a python server via tcp and then get back results via a tcp connection. It nearly works ... but I'm totally lost with the embedded dictionary (I'm quite new to python). The first part of the server start the python interpreter via Py_Initialize() and then waits for python statements. To send command, I get some strings and evaluate them through PyRun_String. To get value, I try to build a dictionary and get the result via PyDictGetItem. Here is a sum-up of the c++ python part: Py_Initialize(); pDictionary = PyDict_New(); PyRun_String(PyCommand.str().c_str(),Py_file_input,pDictionary,pDictionary); ... PyDict_SetItemString(pDictionary, "__name__", PyEval_GetBuiltins()); pResult = PyDict_GetItemString(pDictionary, "a"); ... if (!PyArg_Parse(pResult, "i", &intValue)) { cout << "tcp-server: wrong type" << endl; } When I send a statement, here is a log of the messages I get : ./tcp-client 2100 send a=15 Parameter 0 : ./tcp-client Parameter 1 : 2100 Parameter 2 : send Parameter 3 : a=15 line to send : send a=15 tcp-client: send or stop command Quit: tcp-client connected to 127.0.0.1:45577 -- send a=15 send command: line = a=15 Traceback (most recent call last): File "", line 1, in ? NameError: name 'a' is not defined Waiting for TCP connection on port 2100 ... When I get a statement, here is a log of the message I get: Parameter 0 : ./tcp-client Parameter 1 : 2100 Parameter 2 : get Parameter 3 : a line to send : get a tcp-client: get command connected to 127.0.0.1:45578 -- get a get command - variable to get : a . Dictionary size = 2 DEBUG: print a print type(a ) DEBUG: pResult = 0x804fd08 DEBUG: pDictionary = 0xb7c44d74 Traceback (most recent call last): File "", line 1, in ? NameError: name 'a' is not defined ['__builtins__', '__doc__', '__name__'] DEBUG: intValue = 15 tcp-server: get = 15 tcp-client: result = 15 Quit: tcp-client My question is: how to get a global variables via PyDict_* ? Why does the python interpreter prints an error message when I send my command "a=15" ? Why does it finally accepts my command "a=15" ? Your sincerely, Yann COLLETTE -- http://mail.python.org/mailman/listinfo/python-list
Re: some OT: how to solve this kind of problem in our program?
For people that will read the posts in the future, there is a little bug (it doesn't change the output of this program): items = alist[:] Has to be: alist = alist[:] Sorry, bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
Leon wrote: > Hi, > > I'm creating a python script that can take a string and print it to the > screen as a simple multi-columned block of mono-spaced, unhyphenated > text based on a specified character width and line hight for a column. > For example, if i fed the script an an essay, it would format the text > like a newspaper on the screen. Text processing is very new to me, any > ideas on how I could achieve a multi-columned text formatter. Are there > any libraries that already do this? > > Thanks and happy holidays! > Leon I did something similar, read in text file format it to 79 characters and print it out to a multiple images of 480x272 so i could read them on my PSP. Anyway the code to handle the formatting (even though this is one column of 78 characters with one character space from the top and bottom, and sides) corpus = open("essay.txt","r") wordcount = 0 pctline = "" pctlines = [ for line in corpus.readlines(): # i dont need the newline character as it will be printed onto images line = line[:-1] # i want individual words where each word is separated by a space character words = line.split(" ") for word in words: if wordcount + len(word) + 1 < 78: wordcount = wordcount + len(word) + 1 pctline = pctline + word + " " else: wordcount = len(word) pctlines.append(pctline) pctline = None pctline = word + " " corpus.close() what it does is keeps a list of words and iterate through it and see if the length of that word and one space character and the line doesn't exceed 78, if it doesnt then add it to the pctlines, add the count to the wordcount + 1. If it does then we have one line either 78 characters long or less and add it to pctlines which is a list of all the lines. Hope this helps. Cheers -- http://mail.python.org/mailman/listinfo/python-list
Re: How to stop program when threads is sleeping
On 25 Dec 2006 21:50:15 -0800, many_years_after wrote > While , there is something wrong in my expression. What I mean is the > thread will wait some time after doing some tasks. I want to know is > there any method to end the thread or make it out of execution of > waiting. I use time.sleep() to let the thread wait. We can't help you if you don't tell us what you need. It's still not quite clear what exactly you need, but one thing you should know is that you can't stop a thread while it's sleep()ing, so using sleep() is probably the wrong approach. The wait() method of the threading.Event object from my previous response can take an optional timeout argument. This will make your thread wait until the timeout elapses or until the event is .set() by another thread, whichever comes first. Hope this helps. If this doesn't help, please tell us exactly what you need and maybe show us some code. Carsten. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
[EMAIL PROTECTED] wrote: > Steven D'Aprano wrote: > > Try something like this: > > > > # WARNING: untested > > def run_without_stdout(*args, **kwargs): > > function = args[0] > > args = args[1:] > > savestdout = sys.stdout > > sys.stdout = cStringIO.StringIO() > > result = None > > try: > > result = function(*args, **kwargs) > > finally: > > # don't forget to restore stdout, or you > > # really will regret it... > > sys.stdout = savestdout > > return result > > > Thanks! > > I have tried your method, but I found it didn't work as expected. > > The output produced by the external function couldn't be depressed, > but the "print " statement i wrote in python is depressed. It seems > make cStringIO.StringIO() as a temporary replacement of sys.stdout > has no effect on the external function. > > Here is an example to make myself clear(actually it's modified version > of Steven's code): > > def run_without_stdout(*args, **kwargs): > function = args[0] > args = args[1:] > savestdout = sys.stdout > sys.stdout = cStringIO.StringIO() > print "something" > result = None > try: > result = function(*args, **kwargs) > finally: > # don't forget to restore stdout, or you > # really will regret it... > sys.stdout = savestdout > print "some other thing" > return result > > When run_without_stdout() is called, the "print" statements wrote in python > don't produce output, but function() produces output to the standard output > just as before:( > > I have tried to replace sys.stdout globally with cStringIO.StringIO() > in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a > globall statement), but it worked just as previous version did. Perhaps try redirecting sys.stderr instead of sys.stdout. André > > Regards, > > xiaojf -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
[EMAIL PROTECTED] wrote: > > I have tried your method, but I found it didn't work as expected. > > The output produced by the external function couldn't be depressed, > but the "print " statement i wrote in python is depressed. It seems > make cStringIO.StringIO() as a temporary replacement of sys.stdout > has no effect on the external function. > > Here is an example to make myself clear(actually it's modified version > of Steven's code): > > def run_without_stdout(*args, **kwargs): > function = args[0] > args = args[1:] > savestdout = sys.stdout > sys.stdout = cStringIO.StringIO() > print "something" > result = None > try: > result = function(*args, **kwargs) > finally: > # don't forget to restore stdout, or you > # really will regret it... > sys.stdout = savestdout > print "some other thing" > return result > > When run_without_stdout() is called, the "print" statements wrote in python > don't produce output, but function() produces output to the standard output > just as before:( > > I have tried to replace sys.stdout globally with cStringIO.StringIO() > in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a > globall statement), but it worked just as previous version did. > After some trials I found that put "os.close(1)" before calling the function will depress the output. In fact, "os.close(1)" closed standard output, but I don't know how to open it again after the function's execution. Still trying... Regards, xiaojf -- http://mail.python.org/mailman/listinfo/python-list
keypressed() function
I need a function (blocking or non-blocking) that tells me if a key has been pressed (even before it has been released etc.). Also, I would of course like to know _which_ key has been pressed. I know that this probably does not exist in the Python library already as a platform-independant abstraction (even though it probably could), but then I would at least like solutions that works on Windows and on Linux. /David -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
Hi Luis, Luis Armendariz wrote: > There's no need for savestdout. There's a backup copy in sys.__stdout__ Depending on the code that ran before the call to the function run_without_stdout, sys.stdout may not be the same as sys.__stdout__ . Of course, you also have to be careful regarding threads which also use sys.stdout, perhaps implicitly via print statements. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote > [EMAIL PROTECTED] wrote: >> >> I have tried your method, but I found it didn't work as expected. >> >> The output produced by the external function couldn't be depressed, >> but the "print " statement i wrote in python is depressed. It seems >> make cStringIO.StringIO() as a temporary replacement of sys.stdout >> has no effect on the external function. >> >> Here is an example to make myself clear(actually it's modified >> version of Steven's code): >> >> def run_without_stdout(*args, **kwargs): >> function = args[0] >> args = args[1:] >> savestdout = sys.stdout >> sys.stdout = cStringIO.StringIO() >> print "something" >> result = None >> try: >> result = function(*args, **kwargs) >> finally: >> # don't forget to restore stdout, or you >> # really will regret it... >> sys.stdout = savestdout >> print "some other thing" >> return result >> >> When run_without_stdout() is called, the "print" statements wrote in >> python don't produce output, but function() produces output to the >> standard output just as before:( >> >> I have tried to replace sys.stdout globally with cStringIO.StringIO() >> in my program(I mean, make "sys.stdout = cStringIO.StringIO()" as a >> globall statement), but it worked just as previous version did. >> > After some trials I found that put "os.close(1)" before calling the > function will depress the output. In fact, "os.close(1)" closed > standard output, but I don't know how to open it again after the > function's execution. > > Still trying... On Linux systems you may try os.open('/dev/stdout', os.O_WRONLY'). This will connect to lowest available file descriptor to standard output. If you're lucky and no files have been opened after closing standard output, sys.stdout will point to standard output again. Bye Sebastian 'lunar' Wiesner -- Freedom is always the freedom of dissenters. (Rosa Luxemburg) -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
"Leon" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > > I'm creating a python script that can take a string and print it to the > screen as a simple multi-columned block of mono-spaced, unhyphenated > text based on a specified character width and line hight for a column. > For example, if i fed the script an an essay, it would format the text > like a newspaper on the screen. Text processing is very new to me, any > ideas on how I could achieve a multi-columned text formatter. Are there > any libraries that already do this? > > Thanks and happy holidays! > Leon > Check out the textwrap module, new in 2.3. Here is code to do one- and two-column output. -- Paul gettysburgAddress = """Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth. """.split('\n') import textwrap # wrap text at 50 characters for line in gettysburgAddress: print "\n".join( textwrap.wrap(line,50) ) print # create two columns of text wrappedLines = sum([ textwrap.wrap(line,35) + [''] for line in gettysburgAddress ],[])[:-1] numLines = len(wrappedLines) halfway = numLines/2 twoCol = [ "%-38s%s" % pair for pair in zip(wrappedLines[:halfway],wrappedLines[halfway:]) ] print "\n".join(twoCol) -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
On 26 Dec 2006 04:14:27 -0800, Leon <[EMAIL PROTECTED]> wrote: > I'm creating a python script that can take a string and print it to the > screen as a simple multi-columned block of mono-spaced, unhyphenated > text based on a specified character width and line hight for a column. Hi, Leon, For putting the columns together zip is your friend. Let me lay out an example: # get your text and strip the newlines: testdata = file('something').read().replace('\n','') # set some parameters (these are arbitrary, pick what you need):: colwidth = 35 colheight = 20 numcol = 2 rowperpage = colheight * numcol # first split into lines (this ignores word boundaries # you might want to use somehting more like placid posted for this) data1 = [testdata[x:x+colwidth] for x in range(0,len(testdata),colwidth)] # next pad out the list to be an even number of rows - this will give # a short final column. If you want them balanced you're on your own ;) data1.extend(['' for x in range(rowsperpage - len(data1) % rowsperpage)]) # then split up the list based on the column length you want: data2 = [data1[x:x+colheight] for x in range(0,len(data1),colheight)] # then use zip to transpose the lists into columns pages = [zip(*data2[x:x+numcol]) for x in range(0,len(data2),numcol)] # finally unpack this data with some loops and print: for page in pages: for line in page: for column in line: print ' %s ' % column, #<- note the comma keeps newlines out print '\n' print '\f' -dave -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
Thanks, Paul. I didn't know about textwrap, that's neat. Leon, so in my example change > data1= [testdata[x:x+colwidth] for x in range(0,len(testdata),colwidth)] to > data1 = textwrap.wrap(testdata,colwidth) > data1 = [x.ljust(colwidth) for x in data1] oh and I made a mistake that double spaces it. the "print '\n'" line needs to be either print '' or print '\n', (with a comma) -dave -- http://mail.python.org/mailman/listinfo/python-list
Re: BeautifulSoup vs. loose & chars
On 26 Dec 2006 04:22:38 -0800, placid <[EMAIL PROTECTED]> wrote: > So do you want to remove "&" or replace them with "&" ? If you want > to replace it try the following; I think he wants to replace them, but just the invalid ones. I.e., This & this & that would become This & this & that No, i don't know how to do this efficiently. =/... I think some kind of regex could do it. -- Felipe. -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
"Paul McGuire" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > gettysburgAddress = """Four score and seven years ago... By the way, this variable contains only 3 (very long) lines of text, one for each paragraph. (Not immediately obvious after Usenet wraps the text.) -- Paul -- http://mail.python.org/mailman/listinfo/python-list
PySchool Phase II
PySchool Phase II I just wanted to thank everyone for filling out the survey a few months back for my PySchool graduate school project. I was able to capture a lot of good information from the community. For the people who mentioned to me that I should expand the "What Linux Operating System do you use" question, unfortunately it was too late to make any additions to the survey. However, I will be gathering this information in the future, so I will make sure to incorporate this request. I have completed the fall semester and I have my complete plan of action for how I will be constructing my on-line training environment for Python web frameworks. I am in the process of finalizing the framework(s) that I will use and the technology that I will use to deliver the instruction. The current schedule that I have submitted to my professor has the instruction running from the beginning of February until the middle of March. If you are interested in participating in this free on-line course; send me an email (bobjohnson11 [at] gmail {Dot} com). Please remember that the on-line course is a work in progress, which means that when you have completed the material I would appreciate any constructive feedback that you can give me. I want the instruction to be as effective as possible and to serve as a complementary educational vehicle to the documentation, tutorials and books that are available. I also want to mention that I will be Blogging about this whole experience. I have not had a chance to update my Blog for some time but I plan to start today given the fact that I have a lot more time. Once again, I would like to thank all of the people who filled out the survey and who gave me feedback about my project. You have help me tremendously!!! Thanks Rob Johnson (MAIT Candidate) Blog: http://pyschool.blogspot.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
"Paul McGuire" <[EMAIL PROTECTED]> wrote: > By the way, this variable contains only 3 (very long) lines of text, > one for each paragraph. (Not immediately obvious after Usenet wraps > the text.) Usenet doesn't wrap text, all it has is a convention which suggests that people posting to usenet should wrap their text at 72 characters. Your newsreader probably wrapped the text for you when you were posting, but for deliberately long lines many newsreaders will have an option somewhere to turn off the text wrapping (and the reader probably also has to turn off text wrapping when they read it). For source code where you want long unwrapped strings you would have been better to do the text wrapping in an editor first and use backslash line continuations. e.g. gettysburgAddress = """\ Four score and seven years ago our fathers brought forth on this \ continent, a new nation, conceived in Liberty, and dedicated to the \ proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, \ or any nation so conceived and so dedicated, can long endure. We are \ met on a great battle-field of that war. We have come to dedicate a \ portion of that field, as a final resting place for those who here \ gave their lives that that nation might live. It is altogether fitting \ and proper that we should do this. But, in a larger sense, we can not dedicate -- we can not consecrate \ -- we can not hallow -- this ground. The brave men, living and dead, \ who struggled here, have consecrated it, far above our poor power to \ add or detract. The world will little note, nor long remember what we \ say here, but it can never forget what they did here. It is for us the \ living, rather, to be dedicated here to the unfinished work which they \ who fought here have thus far so nobly advanced. It is rather for us \ to be here dedicated to the great task remaining before us -- that \ from these honored dead we take increased devotion to that cause for \ which they gave the last full measure of devotion -- that we here \ highly resolve that these dead shall not have died in vain -- that \ this nation, under God, shall have a new birth of freedom -- and that \ government of the people, by the people, for the people, shall not \ perish from the earth.\ """.split('\n') -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie: what is a usefull IDE for Python on Windows ?
Osiris wrote: > what is a usefull IDE for Python on Windows ? > > I saw Eric mentioned.. is that WinXP or Linux ? > > What does "everybody" use ? > > I was considering using old and very stable C-code in a new web > application via Python/Plone/Zope. I like pyscripter, available here: http://mmm-experts.com/Products.aspx?ProductId=4 -Larry -- http://mail.python.org/mailman/listinfo/python-list
Type text global
Hey, I'm a noob at python so.. I just want to know, is there a function to type text global not in the program but in other programs(like you where typing it). For example in a textbox, in a program like "cmd.exe" or "notebook.exe". I'm using windows xp. Thanks! :) /Scripter47 -- http://mail.python.org/mailman/listinfo/python-list
Re: BeautifulSoup vs. loose & chars
"Felipe Almeida Lessa" <[EMAIL PROTECTED]> wrote: > On 26 Dec 2006 04:22:38 -0800, placid <[EMAIL PROTECTED]> wrote: >> So do you want to remove "&" or replace them with "&" ? If you >> want to replace it try the following; > > I think he wants to replace them, but just the invalid ones. I.e., > > This & this & that > > would become > > This & this & that > > > No, i don't know how to do this efficiently. =/... > I think some kind of regex could do it. > Since he's asking for valid xml as output, it isn't sufficient just to ignore entity definitions: HTML has a lot of named entities such as but xml only has a very limited set of predefined named entities. The safest technique is to convert them all to numeric escapes except for the very limited set also guaranteed to be available in xml. Try this: from cgi import escape import re from htmlentitydefs import name2codepoint name2codepoint = name2codepoint.copy() name2codepoint['apos']=ord("'") EntityPattern = re.compile('&(?:#(\d+)|(?:#x([\da-fA-F]+))|([a-zA-Z]+));') def decodeEntities(s, encoding='utf-8'): def unescape(match): code = match.group(1) if code: return unichr(int(code, 10)) else: code = match.group(2) if code: return unichr(int(code, 16)) else: return unichr(name2codepoint[match.group(3)]) return EntityPattern.sub(unescape, s) >>> escape( decodeEntities("This & this & that é")).encode( 'ascii', 'xmlcharrefreplace') 'This & this & that é' P.S. apos is handled specially as it isn't technically a valid html entity (and Python doesn't include it in its entity list), but it is an xml entity and recognised by many browsers so some people might use it in html. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie: what is a usefull IDE for Python on Windows ?
On Dec 26, 8:53 am, Larry Bates <[EMAIL PROTECTED]> wrote: > Osiris wrote: > > what is a usefull IDE for Python on Windows ? I am a happy user of jEDIT. -- http://mail.python.org/mailman/listinfo/python-list
Installing python.org distro over ActivePython?
Hey gang- I just ran into the fabled "Secure Sockets not enabled in ActivePython," and the ActiveState FAQ says I should just grab _ssl.pyd from "somewhere", offering up the python.org distribution as a possible source. I'm on 2.4 at this time, and python.org has what appears to be a considerably newer release than ActiveState in the first place, so I was wondering if I could just install this entire package right over the ActiveState installation, and everything would Just Work? thanks, -tom! -- -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting a string to be a columned block of text
"Dave Borne" <[EMAIL PROTECTED]> wrote in news:[EMAIL PROTECTED]: > Thanks, Paul. I didn't know about textwrap, that's neat. > > Leon, > so in my example change >> data1= [testdata[x:x+colwidth] for x in >> range(0,len(testdata),colwidth)] > to >> data1 = textwrap.wrap(testdata,colwidth) >> data1 = [x.ljust(colwidth) for x in data1] > > oh and I made a mistake that double spaces it. the "print '\n'" > line needs to be either > print '' > or > print '\n', > (with a comma) > The solutions so far serve up text that is split within the confines of the column width, but leave a "ragged right" margin, where the line lengths appear to differ. I had the impression that the OP wanted right-and-left justified text, such as would typically be found in a newspaper column. Here's a shot at doing that for fixed-width fonts. To use it, first split your lines as others have suggested, then print aline(line,colwid) for each line in your data. # aline - align text to fit in specified width # This module arbitrarily chooses to operate only on long-enough # lines, where "long-enough" is defined as 60% of the specified # width in this case. # def aline(line, wid): line = line.strip() lnlen = len(line) if wid > lnlen > wid*0.6: ls = line.split() diff = wid - lnlen #nspaces = len(ls)-1 eix = 1 bix = 1 while diff > 0: # and nspaces > 0: if len(ls[bix]) == 0 or ls[bix].find(' ') >= 0: ls[bix] += ' ' else: ls.insert(bix,'') diff -= 1 bix += 2 if bix >= 1+len(ls)/2: bix = 1 if diff > 0: if len(ls[-eix]) == 0 or ls[-eix].find(' ') >= 0: ls[-eix] += ' ' else: ls.insert(-eix,'') diff -= 1 eix += 2 if eix >= 1+len(ls)/2: eix = 1 line = ' '.join(ls) return line -- rzed -- http://mail.python.org/mailman/listinfo/python-list
Re: BeautifulSoup vs. loose & chars
Felipe Almeida Lessa wrote: > On 26 Dec 2006 04:22:38 -0800, placid <[EMAIL PROTECTED]> wrote: > >> So do you want to remove "&" or replace them with "&" ? If you want >> to replace it try the following; > > > I think he wants to replace them, but just the invalid ones. I.e., > > This & this & that > > would become > > This & this & that > > > No, i don't know how to do this efficiently. =/... > I think some kind of regex could do it. Yes, and the appropriate one is: krefindamp = re.compile(r'&(?!(\w|#)+;)') ... xmlsection = re.sub(krefindamp,'&',xmlsection) This will replace an '&' with '&' if the '&' isn't immediately followed by some combination of letters, numbers, and '#' ending with a ';' Admittedly this would let something like '&xx#2;', which isn't a legal entity, through unmodified. There's still a potential problem with unknown entities in the output XML, but at least they're recognized as entities. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Fuzzy string comparison
I'm looking for a module to do fuzzy comparison of strings. I have 2 item master files which are supposed to be identical, but they have thousands of records where the item numbers don't match in various ways. One might include a '-' or have leading zeros, or have a single character missing, or a zero that is typed as a letter 'O'. That kind of thing. These tables currently reside in a mysql database. I was wondering if there is a good package to let me compare strings and return a value that is a measure of their similarity. Kind of like soundex but for strings that aren't words. Thanks, Steve Bergman -- http://mail.python.org/mailman/listinfo/python-list
Re: SPAM-LOW: Re: BeautifulSoup vs. loose & chars
Duncan Booth skrev: > "Felipe Almeida Lessa" <[EMAIL PROTECTED]> wrote: > > >> On 26 Dec 2006 04:22:38 -0800, placid <[EMAIL PROTECTED]> wrote: >> >>> So do you want to remove "&" or replace them with "&" ? If you >>> want to replace it try the following; >>> >> I think he wants to replace them, but just the invalid ones. I.e., >> >> This & this & that >> >> would become >> >> This & this & that >> >> >> No, i don't know how to do this efficiently. =/... >> I think some kind of regex could do it. >> >> > > Since he's asking for valid xml as output, it isn't sufficient just to > ignore entity definitions: HTML has a lot of named entities such as > but xml only has a very limited set of predefined named entities. > The safest technique is to convert them all to numeric escapes except > for the very limited set also guaranteed to be available in xml. > > Try this: > > from cgi import escape > import re > from htmlentitydefs import name2codepoint > name2codepoint = name2codepoint.copy() > name2codepoint['apos']=ord("'") > > EntityPattern = > re.compile('&(?:#(\d+)|(?:#x([\da-fA-F]+))|([a-zA-Z]+));') > > def decodeEntities(s, encoding='utf-8'): > def unescape(match): > code = match.group(1) > if code: > return unichr(int(code, 10)) > else: > code = match.group(2) > if code: > return unichr(int(code, 16)) > else: > return unichr(name2codepoint[match.group(3)]) > return EntityPattern.sub(unescape, s) > > escape( > decodeEntities("This & this & that é")).encode( > 'ascii', 'xmlcharrefreplace') > 'This & this & that é' > > > P.S. apos is handled specially as it isn't technically a > valid html entity (and Python doesn't include it in its entity > list), but it is an xml entity and recognised by many browsers so some > people might use it in html. > Hey i fund this site: http://www.htmlhelp.com/reference/html40/entities/symbols.html I hope that its what you mean. /Scripter47 -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy string comparison
Steve Bergman wrote: > I'm looking for a module to do fuzzy comparison of strings. [...] Check module difflib, it returns difference between two sequences. -- http://mail.python.org/mailman/listinfo/python-list
Re: perl better than python for users with disabilities?
Dan Jacobson wrote: > Can I feel even better about using perl vs. python, as apparently > python's dependence of formatting, indentation, etc. vs. perl's > "(){};" etc. makes writing python programs perhaps very device > dependent. Whereas perl can be written on a tiny tiny screen, and can > withstand all kinds of users with various disabilities, etc.? > Also perl is easier to squeeze into makefiles. My esteemed colleague does not agree. http://googleblog.blogspot.com/2006/07/finding-easy-to-read-web-content_20.html Reposted with permission: """ As for programming in Python while not being able to see, I think it comes down to the tools you have to do it with. When in Grad School, all I had was a screenreader, and I stayed away from python because of the whitespace having semantics problem. However once i wrote emacspeak, and speech-enabled Barry Warsaw's python-mode, programming in python has always been a pleasure -- and the paucity of delimiters actually makes the code more speakable. """ n -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does Python never add itself to the Windows path?
Ben Sizer wrote: > I've installed several different versions of Python across several > different versions of MS Windows, and not a single time was the Python > directory or the Scripts subdirectory added to the PATH environment > variable. Every time, I've had to go through and add this by hand, to > have something resembling a usable Python installation. No such > problems on Linux, whether it be Mandrake/Mandriva, Fedora Core, or > Kubuntu. So why is the Windows install half-crippled by default? I just > rediscovered this today when trying to run one of the Turbogears > scripts, but this has puzzled me for years now. > That would put together a great mix-up if app installations add to the PATH. Think of multiple installations etc. ( Note: this is also _NOT_ done on *nix - just a sym link is possibly put into /usr/local/bin or so ) To have something similar on Win I have a C:\bin folder where I put all my small .exe's / installations and all the start vectors (.exe, .bat's). The python.exe on Win is very small and just loads the pythonXX.dll. It also finds the right default DLL due to the version engraved in python.exe. Thus I just copy the small python.exe from my favorite/default Python installation (2.3) into my C:\bin. Thats probably next to what you obviously want - a "default link". The Test&Fun-Pythons ( ... 2.2 2.4 2.5 2.6 ) - I have them renamed as e.g. python25.exe also in C:\bin . I make similar links on *nix also. Perhaps in the future the Python Windows installer should create such pythonXX.exe "links" and maybe even a default python.exe (upon Checkbox in Installer!) into the Windows\system32 ? Robert -- http://mail.python.org/mailman/listinfo/python-list
Re: SPAM-LOW: Re: BeautifulSoup vs. loose & chars
Andreas Lysdal <[EMAIL PROTECTED]> wrote: >> P.S. apos is handled specially as it isn't technically a >> valid html entity (and Python doesn't include it in its entity >> list), but it is an xml entity and recognised by many browsers so some >> people might use it in html. >> > Hey i fund this site: > http://www.htmlhelp.com/reference/html40/entities/symbols.html > > I hope that its what you mean. Try http://www.w3.org/TR/html4/sgml/entities.html#entities for a more complete list. -- http://mail.python.org/mailman/listinfo/python-list
Google Custom Search Engine
Hi, I created a Google Custom Search Engine for searching on: "Software / Compilers / Optimization" This is basically a regular full Google search giving preference to technical sites such as IEEE, ACM, citeseer, the Universities, news-group (this one included), commercial sites such as Intel, Microsoft, HP, Sun, IBM, and others. The search results are further biased towards software development, programming languages, and optimizing compilers The results are indeed much more specific than the regular Google search. Try ity out! Additionally, if you are interested, please send me an email and you can contribute to this search engine and add sites you believe should be given preference to. Over time, it should become better and better... I "donated" a no-nonsense URL: www.codeopt.com Check it out - and let me know what you think (no rants, please)! Cheers -- Robert Hundt HP -- http://mail.python.org/mailman/listinfo/python-list
Re: keypressed() function
[EMAIL PROTECTED] wrote: > I need a function (blocking or non-blocking) that tells me if a key has > been pressed (even before it has been released etc.). Also, I would of > course like to know _which_ key has been pressed. > > I know that this probably does not exist in the Python library already > as a platform-independant abstraction (even though it probably could), > but then I would at least like solutions that works on Windows and on > Linux. > > /David > Its a terminal I/O function - not a platform function. E.g. On Win only in a rough console msvcrt.kbhit() does it. In PythonWin, IPython, Crust ... things are of course different. On regular Unix terminals you have the sys.stdin file: sys.stdin.read(1) #maybe in a thread and interthread-pass it to your main loop or possibly trick with fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NDELAY|os.O_NONBLOCK) when nothing is on sys.stdin - you get immediately an IOError: >>> sys.stdin.read(1) Traceback (most recent call last): File "", line 1, in ? IOError: [Errno 11] Resource temporarily unavailable And see also other ioctl, termios, tty, cbreak, curses .. functions to get things early before \n buffering depending on the terminal mode ) Robert -- http://mail.python.org/mailman/listinfo/python-list
Q: How to generate code object from bytecode?
Hi, It is possible to get bytecode from code object. Reversely, is it possible to create code object from bytecode? ex. ## python code (not a module) pycode = '''\ print "\n" for item in items: print "%s\n" % item print "\n" ''' ## compile it and get bytecode code = compile(pycode, kind='exec') bytecode = code.co_code open('hoge.pyc', 'wb').write(bytecode) ## load bytecode and eval it bytecode = open('hoge.pyc', 'rb').read() code = create_code(bytecode)## how to ? output = eval(code, globals, {'items': ['A', 'B', 'C']}) -- regards, kwatch -- http://mail.python.org/mailman/listinfo/python-list
Mod_python
In my web application I use Apache and mod_python. I allow users to upload huge files( via HTTP FORM , using POST method) I would like to store the file directly on a hard disk and not to upload the WHOLE huge file into server's memory first. Can anyone suggest a solution? Thank you LB -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: How to generate code object from bytecode?
[EMAIL PROTECTED] wrote: > It is possible to get bytecode from code object. > Reversely, is it possible to create code object from bytecode? > > ex. > ## python code (not a module) > pycode = '''\ > print "\n" > for item in items: > print "%s\n" % item > print "\n" > ''' > > ## compile it and get bytecode > code = compile(pycode, kind='exec') > bytecode = code.co_code > open('hoge.pyc', 'wb').write(bytecode) > > ## load bytecode and eval it > bytecode = open('hoge.pyc', 'rb').read() > code = create_code(bytecode)## how to ? > output = eval(code, globals, {'items': ['A', 'B', 'C']}) use the marshal module; see the last script on this page for an example: http://effbot.org/librarybook/marshal -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: How to generate code object from bytecode?
On Tue, 2006-12-26 at 11:15 -0800, [EMAIL PROTECTED] wrote: > Hi, > > It is possible to get bytecode from code object. > Reversely, is it possible to create code object from bytecode? > > ex. > ## python code (not a module) > pycode = '''\ > print "\n" > for item in items: > print "%s\n" % item > print "\n" > ''' > > ## compile it and get bytecode > code = compile(pycode, kind='exec') > bytecode = code.co_code > open('hoge.pyc', 'wb').write(bytecode) > > ## load bytecode and eval it > bytecode = open('hoge.pyc', 'rb').read() > code = create_code(bytecode)## how to ? > output = eval(code, globals, {'items': ['A', 'B', 'C']}) As Fredrik has said, you should use marshal to handle the writing and reading of the code object. I'd like to point out the following additional facts that may not be apparent to you: * Code objects come in two flavors: statements and expressions. * exec can execute a 'statement' flavored code object. * eval can evaluate an 'expression' flavored code object. * Your code snippet is a statement, actually, a suite of statements. You need to exec it, not eval it. * You seem to think that eval'ing or exec'ing a code object will magically capture its output stream. It won't. What do you actually want to accomplish? Instead of having us poke at your first attempt at a solution, it might be more helpful if you told us what problem you're actually trying to solve. -Carsten -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: How to generate code object from bytecode?
On Tue, 2006-12-26 at 14:48 -0500, Carsten Haese wrote: > * Code objects come in two flavors: statements and expressions. > * exec can execute a 'statement' flavored code object. > * eval can evaluate an 'expression' flavored code object. > * Your code snippet is a statement, actually, a suite of statements. You > need to exec it, not eval it. And to reply to myself before the effbot corrects me, it turns out that the separation between expressions and statements is not quite so strict. For one, an expression can be used like a statement. Also, apparently eval() can evaluate the code object of a statement, even though it can't evaluate the source code of a statement, which I find oddly inconsistent: >>> eval("print 'Hi'") Traceback (most recent call last): File "", line 1, in File "", line 1 print 'Hi' ^ SyntaxError: invalid syntax >>> eval(compile("print 'Hi'", "
Re: Splitting lines from a database query
ZeD wrote: > Peter Machell wrote: > >> I have an application where I need to take a query from an existing >> database and send it to a web api. > > [...] > >> There are always 5 values, but some are blank and some are 'None'. >> I'd like to split the lines so I get something resembling XML, like this: >> Frank >> Spencer >> >> Dr I Feelgood >> 2006-12-27 16:00:00 > > quick and dirty solution: > > bar = ( > ("Jane Mary","SIMPSON","0411231234","Dr I Feelgood","2006-12-27 15:00:00"), > ("John","DOE","None","Dr I Feelgood","2006-12-27 15:30:00"), > ("Frank","SPENCER","","Dr I Feelgood","2006-12-27 16:00:00") > ) > > spam ="FNAME", "SNAME", "PHONE", "DR","TIME" > > for x in bar: > i=0 > while i<5: > if x[i] != 'None': > print "<%s>%s" % (spam[i], x[i], spam[i]) > else: > print "<%s>" % (spam[i], spam[i]) > i+=1 > print > Thanks very much ZeD. This will do what I need to. The next step is to do some regex on the phone number to ensure it's local and correct. How can I split these up so each value has a key? Peter. -- http://mail.python.org/mailman/listinfo/python-list
Re: some OT: how to solve this kind of problem in our program?
At Monday 25/12/2006 21:24, Paul McGuire wrote: For example, for all the complexity in writing Sudoku solvers, there are fewer than 3.3 million possible permutations of 9 rows of the digits 1-9, and far fewer permutations that match the additional column and box constraints. Why not just compute the set of valid solutions, and compare an input mask with these? Are you sure? There are 9!=362880 rows of digits 1-9; taking 9 of these at random gives about 10**50 possibilities. Of course just a few match the additional constraints. Maybe you can trivially reduce them (just looking for no dupes on the first column) but anyway its a large number... (Or I'm wrong computing the possibilities...) -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Peter Machell wrote: > ZeD wrote: > > Thanks very much ZeD. This will do what I need to. > The next step is to do some regex on the phone number to ensure it's > local and correct. How can I split these up so each value has a key? Well, you should try that, unless you intend to get the newsgroup to write your code for you. Come back with your efforts and any problems you have with them. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: some OT: how to solve this kind of problem in our program?
On 12/26/06, Gabriel Genellina <[EMAIL PROTECTED]> wrote: > At Monday 25/12/2006 21:24, Paul McGuire wrote: > > >For example, for all the complexity in writing Sudoku solvers, there are > >fewer than 3.3 million possible permutations of 9 rows of the digits 1-9, > >and far fewer permutations that match the additional column and box > >constraints. Why not just compute the set of valid solutions, and compare > >an input mask with these? > > Are you sure? There are 9!=362880 rows of digits 1-9; taking 9 of > these at random gives about 10**50 possibilities. Of course just a > few match the additional constraints. Maybe you can trivially reduce > them (just looking for no dupes on the first column) but anyway its a > large number... (Or I'm wrong computing the possibilities...) > According to Wikipedia, there are 6,670,903,752,021,072,936,960 possible classical Sudoku layouts. Ref. http://www.research.att.com/~njas/sequences/A107739 -- http://mail.python.org/mailman/listinfo/python-list
Re: Q: How to generate code object from bytecode?
Thanks Fredrik and Carsten, I'll try marshal module. > * Your code snippet is a statement, actually, a suite of statements. You > need to exec it, not eval it. > * You seem to think that eval'ing or exec'ing a code object will > magically capture its output stream. It won't. Oh, it's my mistake. > What do you actually want to accomplish? I'm now developping embedded python text converter. ex. example.pyhtml title ${item} ex. converted python code _buf = []; _buf.append('''title \n'''); for item in items: _buf.append(''' '''); _buf.append(escape(to_str(item))); _buf.append('''\n'''); #end _buf.append('''\n'''); -- regards, kwatch -- http://mail.python.org/mailman/listinfo/python-list
Re: some OT: how to solve this kind of problem in our program?
On Tue, 2006-12-26 at 17:39 -0300, Gabriel Genellina wrote: > At Monday 25/12/2006 21:24, Paul McGuire wrote: > > >For example, for all the complexity in writing Sudoku solvers, there are > >fewer than 3.3 million possible permutations of 9 rows of the digits 1-9, > >and far fewer permutations that match the additional column and box > >constraints. Why not just compute the set of valid solutions, and compare > >an input mask with these? > > Are you sure? There are 9!=362880 rows of digits 1-9; taking 9 of > these at random gives about 10**50 possibilities. Of course just a > few match the additional constraints. Maybe you can trivially reduce > them (just looking for no dupes on the first column) but anyway its a > large number... (Or I'm wrong computing the possibilities...) > > > -- > http://mail.python.org/mailman/listinfo/python-list Fortunately, somebody has already written a paper on the subject: http://www.afjarvis.staff.shef.ac.uk/sudoku/sudoku.pdf It looks like the number is actually rather large, and I'd expect even with a specialized data structure for compression (probably some kind of tree with bitwise digit packing?) would not fit in memory on any box I own. I would wonder if loading that much data isn't slower than solving the puzzle. -- John Krukoff <[EMAIL PROTECTED]> Land Title Guarantee Company -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy string comparison
Wojciech Mula wrote: > Steve Bergman wrote: > > I'm looking for a module to do fuzzy comparison of strings. [...] > > Check module difflib, it returns difference between two sequences. and it's intended for comparing text files, and is relatively slow. Google "python levenshtein". You'll probably find this a better fit for typoed keys in a database. What I would do for a quick start on this exercise (as described) is: To compare two strings, take copies, and: 1. strip out all spaces (including \xA0 i.e. if the data has been anywhere near the web) from each string; also all "-" (in general strip frequently occurring meaningless punctuation) 2. remove leading zeroes from each string 3. d = levenshtein_distance(string_a, string_b) # string_a etc is the reduced string, not the original 4. error_metric = float(d) / max(len(string_a), len(string_b)) The error_metric will be 0.0 if the strings are the same (after removing spaces, leading zeroes, etc) and 1.0 if they are completely different (no characters in common). ... and you don't want anything "kind of like soundex". That's a bit like saying you'd like to travel in an aeroplane "kind of like the Wright brothers' " ;-) Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
BeautifulSoup bug when ">>>" found in attribute value
This, which is from a real web site, went into BeautifulSoup: And this came out, via prettify: >>&linkurl;=/Europe/Spain/Madrid/Apartments/Offer/2408" /> BeautifulSoup seems to have become confused by the ">>>" within a quoted attribute value. It first parsed it right, but then stuck in an extra, totally bogus line. Note the entity "&linkurl;", which appears nowhere in the original. It looks like code to handle a missing quote mark did the wrong thing. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: keypressed() function
At Tuesday 26/12/2006 10:25, [EMAIL PROTECTED] wrote: I need a function (blocking or non-blocking) that tells me if a key has been pressed (even before it has been released etc.). Also, I would of course like to know _which_ key has been pressed. On Windows you can listen to the messages WM_KEYDOWN/WM_KEYUP. Or, for a specific key, you can use GetKeyState/GetAsyncKeyState. For the whole keyboard use GetKeyboardState. -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Type text global
At Tuesday 26/12/2006 13:57, Andreas Lysdal wrote: I'm a noob at python so.. I just want to know, is there a function to type text global not in the program but in other programs(like you where typing it). For example in a textbox, in a program like "cmd.exe" or "notebook.exe". I'm using windows xp. There is a module named SendKeys which may be useful, try to find it. -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Why does Python never add itself to the Windows path?
Ross Ridge wrote: > Ben Sizer wrote: > > I've installed several different versions of Python across several > > different versions of MS Windows, and not a single time was the Python > > directory or the Scripts subdirectory added to the PATH environment > > variable. > > Personally, I hate Windows applications that add themselves to the > PATH. So much crap gets put in there that I don't even use the default > system PATH and just set my own explicitly. Agreed. A good Windows application minimizes its footprint, only adding registry keys, environment variables, system tray icons, services, shell extensions, etc, etc, when absolutely necessary. Python is such an application. But on the other hand, I use Python on the Windows command line all the time. Having to manually set PATH for each machine I use Python on is a minor annoyance. One solution would be an optional feature on the MSI to prepend the install directory to PATH. In fact, this is what ActivePython does. Any reason why the official Python distribution shouldn't? --Ben Cartwright -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy string comparison
On Tue, 2006-12-26 at 13:08 -0800, John Machin wrote: > Wojciech Mula wrote: > > Steve Bergman wrote: > > > I'm looking for a module to do fuzzy comparison of strings. [...] > > > > Check module difflib, it returns difference between two sequences. > > and it's intended for comparing text files, and is relatively slow. > > Google "python levenshtein". You'll probably find this a better fit for > typoed keys in a database. > [...] Using the Levenshtein distance in combination with stripping "noise" characters is a good start, but the OP might want to take it a step further. One of the OP's requirements is to recognize visually similar strings, but 241O (Letter O at the end) and 241X have the same Levenshtein distance from 2410 (digit zero at the end) while the former is visually much closer to 2410 than the latter. It seems to me that this could be achieved by starting with a standard Levenshtein implementation such as http://hetland.org/python/distance.py and altering the line "change = change + 1" to something like "change = change + visual_distance(a[j-1], b[i-1])". visual_distance() would be a function that embodies the OP's idea of which character replacements are tolerable by returning a number between 0 (the two characters are visually identical) and 1 (the two characters are completely different). Hope this helps, -Carsten -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy string comparison
At Tuesday 26/12/2006 18:08, John Machin wrote: Wojciech Mula wrote: > Steve Bergman wrote: > > I'm looking for a module to do fuzzy comparison of strings. [...] > > Check module difflib, it returns difference between two sequences. and it's intended for comparing text files, and is relatively slow. Google "python levenshtein". You'll probably find this a better fit for typoed keys in a database. Other alternatives: trigram, n-gram, Jaro's distance. There are some Python implem. available. -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Scott David Daniels wrote: > Peter Machell wrote: >> ZeD wrote: >> >> Thanks very much ZeD. This will do what I need to. >> The next step is to do some regex on the phone number to ensure it's >> local and correct. How can I split these up so each value has a key? > > Well, you should try that, unless you intend to get the newsgroup to > write your code for you. Come back with your efforts and any problems > you have with them. As we say in Australia, fair enough. I can almost do it this way: for x in bar: fname = x[0] if fname == "": fname == "None" sname = x[1] if sname == "": sname == "None" print ""+fname+""+""+sname+"" Except that I should be using a list and loop to do the null checking, and it still stops when (I think) it hits a blank value: TypeError: cannot concatenate 'str' and 'NoneType' objects thanks, Peter. -- http://mail.python.org/mailman/listinfo/python-list
Persistent variables in python
Found out a quite fun way to store persistent variables in functions in python. def doStuff(): try: #Will throw exception if not set doStuff.timesUsed ## #Insert stuff to do each time the function is called here ## doStuff.timesUsed+=1 print "Function call!" except AttributeError: doStuff.timesUsed = 0 # set up the variable ## #Put other stuff to call the first #time the function is called here ## print "First call!" doStuff() #recursive call. Will not throw exception now Some output testing this: >>> doStuff() First call! Function call! >>> doStuff() Function call! >>> doStuff() Function call! >>> doStuff() Function call! >>> doStuff.timesUsed 4 Is this concidered bad coding practice since I guess persistent variables in functions are not meant to be? /buffi (www.buffis.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Fuzzy string comparison
Carsten Haese wrote: > On Tue, 2006-12-26 at 13:08 -0800, John Machin wrote: > > Wojciech Mula wrote: > > > Steve Bergman wrote: > > > > I'm looking for a module to do fuzzy comparison of strings. [...] > > > > > > Check module difflib, it returns difference between two sequences. > > > > and it's intended for comparing text files, and is relatively slow. > > > > Google "python levenshtein". You'll probably find this a better fit for > > typoed keys in a database. > > [...] > > Using the Levenshtein distance in combination with stripping "noise" > characters is a good start, but the OP might want to take it a step > further. One of the OP's requirements is to recognize visually similar > strings, but 241O (Letter O at the end) and 241X have the same > Levenshtein distance from 2410 (digit zero at the end) while the former > is visually much closer to 2410 than the latter. > > It seems to me that this could be achieved by starting with a standard > Levenshtein implementation such as http://hetland.org/python/distance.py > and altering the line "change = change + 1" to something like "change = > change + visual_distance(a[j-1], b[i-1])". visual_distance() would be a > function that embodies the OP's idea of which character replacements are > tolerable by returning a number between 0 (the two characters are > visually identical) and 1 (the two characters are completely different). Ya ya ya, I could have told him a whole lot more -- please consider that what I did tell him was IMHO over-generous in response to an OT question asking for assistance with performing a Google search. ... and given his keys are described as "numbers", a better example might be 241O or 241o false-matching with 2416. ... and it might be a good idea if he ran the simplistic approach first and saw what near-misses he actually came up with before complicating it and slowing down what is already an O(N**2*L**2) exercise in the traditional/novice implementation where N is the number of keys and L is their average length. The OP needs to think about 123456789 compared with 123426789; are they the same account or not? What other information does he have? HTH, John -- http://mail.python.org/mailman/listinfo/python-list
Re: some OT: how to solve this kind of problem in our program?
"Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > At Monday 25/12/2006 21:24, Paul McGuire wrote: > >>For example, for all the complexity in writing Sudoku solvers, there are >>fewer than 3.3 million possible permutations of 9 rows of the digits 1-9, >>and far fewer permutations that match the additional column and box >>constraints. Why not just compute the set of valid solutions, and compare >>an input mask with these? > > Are you sure? There are 9!=362880 rows of digits 1-9; taking 9 of these at > random gives about 10**50 possibilities. Of course just a few match the > additional constraints. Maybe you can trivially reduce them (just looking > for no dupes on the first column) but anyway its a large number... (Or > I'm wrong computing the possibilities...) > > > -- > Gabriel Genellina > Softlab SRL Der, I was thinking 9 times 362880, not 326880 P 9. Thanks for straightening me out! 10**50 was a good ballpark guess. My permutation calculator says that 362880 items taken 9 at a time yields 10909986439491560573748665829986337733726798848 permutations (~10**50). I assume the smaller Wikipedia number (6.7*10**21) factors in the additional column and box constraints. So I guess we can rule out brute force as a viable strategy after all. -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Peter Machell wrote: > I can almost do it this way: > > for x in bar: > fname = x[0] > if fname == "": > fname == "None" > sname = x[1] > if sname == "": > sname == "None" > > print ""+fname+""+""+sname+"" for this: for row in bar: print "%s%s" % ( row[0] or 'None', row[1] or 'None') > Except that I should be using a list and loop to do the null checking, > and it still stops when (I think) it hits a blank value: > TypeError: cannot concatenate 'str' and 'NoneType' objects What's the data and program that does that? --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
At Tuesday 26/12/2006 18:57, Peter Machell wrote: for x in bar: fname = x[0] if fname == "": fname == "None" sname = x[1] if sname == "": sname == "None" print ""+fname+""+""+sname+"" Except that I should be using a list and loop to do the null checking, and it still stops when (I think) it hits a blank value: TypeError: cannot concatenate 'str' and 'NoneType' objects Perhaps this is what you intended to write: if fname == "": fname = "None" and since None != "", you have to test for it: if fname is None or fname == "": fname = "None" Note that doing this will deliberately write the text "None" whenever the field is empty, so you will never get , instead: None. If you want to get the former, use: if fname is None: fname = "" -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent variables in python
At Tuesday 26/12/2006 19:13, buffi wrote: def doStuff(): try: #Will throw exception if not set doStuff.timesUsed Is this concidered bad coding practice since I guess persistent variables in functions are not meant to be? I don't think so, since Python proudly says that functions are first-class objects. CherryPy does a similar thing to mark a method as "exposed". But perhaps I'd write the code this way to avoid an unneeded and risky recursive call: def doStuff(some, arguments, may, *be, **required): try: doStuff.timesUsed += 1 except AttributeError: doStuff.timesUsed = 1 # ... special case for first call ... # ...common code... If you need to code something special for the 2nd and following calls, add an else: clause -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: Splitting lines from a database query
Peter Machell wrote: > Scott David Daniels wrote: > > Peter Machell wrote: > >> ZeD wrote: > >> > >> Thanks very much ZeD. This will do what I need to. > >> The next step is to do some regex on the phone number to ensure it's > >> local and correct. How can I split these up so each value has a key? > > > > Well, you should try that, unless you intend to get the newsgroup to > > write your code for you. Come back with your efforts and any problems > > you have with them. > > As we say in Australia, fair enough. > I can almost do it this way: As we say in Australia, it's good to see that you're neither a bludger nor an utter dill/drongo/nong :-) > > for x in bar: Insert some code to show you what you've actually got: print repr(x) > fname = x[0] > if fname == "": > fname == "None" > sname = x[1] > if sname == "": > sname == "None" > > print ""+fname+""+""+sname+"" > > Except that I should be using a list and loop to do the null checking, That's *not* a null that you're checking for, mate, it's a zero-length string. > and it still stops when (I think) it hits a blank value: > TypeError: cannot concatenate 'str' and 'NoneType' objects > It's not a "blank value", it's an object None which you get as a substitute for a NULL in the database; it means "no value at all", as opposed to a zero-length string, which is a value. You can test for None by: if thing is None: You can test for both "" and None in one hit by doing this: if not thing: BTW, why do you want to fill in the missing data with the string value "None" (which could conceivably be a valid surname), instead of leaving it blank or empty? Cheers, John -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent variables in python
> I don't think so, since Python proudly says that functions are > first-class objects. > CherryPy does a similar thing to mark a method as "exposed". > > But perhaps I'd write the code this way to avoid an unneeded and > risky recursive call: > > def doStuff(some, arguments, may, *be, **required): > try: > doStuff.timesUsed += 1 > except AttributeError: > doStuff.timesUsed = 1 > # ... special case for first call ... > # ...common code... True, the recursivity is not needed there I guess :) It just feels so ugly to use try/except to enable the variable but I've found it useful at least once. /buffi (buffis.com) -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent variables in python
> Found out a quite fun way to store persistent variables in functions in > python. > > Is this concidered bad coding practice since I guess persistent > variables in functions are not meant to be? I am using is in one of my recent projects. I was thinking of it sort of like "static" variables in C. I use a decorator to create the variables ... def static(**kw): ''' Used to create a decorator function that will add an attribute to a function and initialize it. >>> @static(foo=5) ... def bar(): ... print bar.foo ... bar.foo += 1 ... >>> bar() 5 >>> bar() 6 ''' def decorator(f): f.__dict__.update(kw) return f return decorator -- http://mail.python.org/mailman/listinfo/python-list
Re: BeautifulSoup vs. loose & chars
John Nagle wrote: > Felipe Almeida Lessa wrote: > >> On 26 Dec 2006 04:22:38 -0800, placid <[EMAIL PROTECTED]> wrote: >> >> >>> So do you want to remove "&" or replace them with "&" ? If you want >>> to replace it try the following; >>> >> I think he wants to replace them, but just the invalid ones. I.e., >> >> This & this & that >> >> would become >> >> This & this & that >> >> >> No, i don't know how to do this efficiently. =/... >> I think some kind of regex could do it. >> > > Yes, and the appropriate one is: > > krefindamp = re.compile(r'&(?!(\w|#)+;)') > ... > xmlsection = re.sub(krefindamp,'&',xmlsection) > > This will replace an '&' with '&' if the '&' isn't > immediately followed by some combination of letters, numbers, > and '#' ending with a ';' Admittedly this would let something > like '&xx#2;', which isn't a legal entity, through unmodified. > > There's still a potential problem with unknown entities in the output XML, but > at least they're recognized as entities. > > John Nagle > > > Here's another idea: >>> s = ''' htm tag should not translate > & should be & > &xx#2; isn't a legal entity and should translate > { is a legal entity and should not translate >>> import SE # http://cheeseshop.python.org/pypi/SE/2.3 >>> HTM_Escapes = SE.SE (definitions) # See definitions below the dotted line >>> print HTM_Escapes (s) htm tag should not translate > & should be & > &xx#2; isn"t a legal entity and should translate > { is a legal entity and should not translate Regards Frederic -- definitions = ''' # Do # Don't do # " = " == # 32 20 (34)=&dquot; &dquot;== # 34 22 &=& &== # 38 26 '=" "== # 39 27 <=< <==# 60 3c >=> >==# 62 3e ©=© ©== # 169 a9 ·=· ·==# 183 b7 »=»»== # 187 bb À=À À==# 192 c0 Á=Á Á==# 193 c1 Â=ÂÂ== # 194 c2 Ã=Ã Ã==# 195 c3 Ä=Ä Ä== # 196 c4 Å=ÅÅ== # 197 c5 Æ=ÆÆ== # 198 c6 Ç=Ç Ç==# 199 c7 È=È È==# 200 c8 É=É É==# 201 c9 Ê=ÊÊ== # 202 ca Ë=Ë Ë== # 203 cb Ì=Ì Ì==# 204 cc Í=Í Í==# 205 cd Î=ÎÎ== # 206 ce Ï=Ï Ï== # 207 cf Ð=&Eth; &Eth;== # 208 d0 Ñ=Ñ Ñ==# 209 d1 Ò=Ò Ò==# 210 d2 Ó=Ó Ó==# 211 d3 Ô=ÔÔ== # 212 d4 Õ=Õ Õ==# 213 d5 Ö=Ö Ö== # 214 d6 Ø=Ø Ø==# 216 d8 Ù=&Ugrve;&Ugrve;== # 217 d9 Ú=Ú Ú==# 218 da Û=ÛÛ== # 219 db Ü=Ü Ü== # 220 dc Ý=Ý Ý==# 221 dd Þ=&Thorn;&Thorn;== # 222 de ß=ßß== # 223 df à=à à==# 224 e0 á=á á==# 225 e1 â=ââ== # 226 e2 ã=ã ã==# 227 e3 ä=ä ä== # 228 e4 å=åå== # 229 e5 æ=ææ== # 230 e6 ç=ç ç==# 231 e7 è=è è==# 232 e8 é=é é==# 233 e9 ê=êê== # 234 ea ë=ë ë== # 235 eb ì=ì ì==# 236 ec í=í í==# 237 ed î=îî== # 238 ee ï=ï ï== # 239 ef ð=ð ð== # 240 f0 ñ=ñ ñ==# 241 f1 ò=ò ò==# 242 f2 ó=ó ó==# 243 f3 ô=&ocric;&ocric;== # 244 f4 õ=õ õ==# 245 f5 ö=ö ö== # 246 f6 ø=ø ø==# 248 f8 ù=ù ù==# 249 f9 ú=ú ú==# 250 fa û=ûû== # 251 fb ü=ü ü== # 252 fc ý=ý ý==# 253 fd þ=þþ== # 254 fe (xff)=ÿ # 255 ff == # All numeric codes "~<(.|\n)*?>~==" # All HTM tags ''' If the ampersand is all you need to handle you can erase the others in the first column. You need to keep the second column though, except the last entry, because the tags don't need protection if '<' and '>' in the first column are gone. Definitions are easily edited and can be kept in text files. The SE constructor accepts a file name instead of a definitions string: >>> HTM_Escapes = SE.SE ('definition_file_name') --- -- http://mail.python.org/mailman/listinfo/python-list
Noobie: Open file -> read characters & multiply
I've been using Python for a few days. It's such the perfect language for parsing data! I really like it so far, but I'm having a hard time reading a file, reading the first few hex characters & converting them to an integer. Once the characters are converted to an integer, I'd like to write the data to another file. Here's the code snipped to the bare minimum: --- # Open File AP_File= open("AP.txt", "r") decoded_File= open("decoded.txt", "w") # read & process data line by line for line in AP_File: time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely hosed! decodedFile.write(time) #close files AP_File.close() decoded_File.close() --- AP.txt 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 00d5 26 0601 80 00 80 00 02 37 fe 54 01 09 00d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 00d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 00d5 26 0604 80 00 fe 54 02 09 80 00 01 5d 00d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b 00d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 00d5 4f 0611 00 00 00 50 00 00 00 00 07 00 00d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b 00d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 00d5 06 0614 80 00 e0 00 02 01 60 79 82 2b 00d5 0a 0615 08 00 00 00 00 00 00 00 00 00 00d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 (actual files are 250MB!) decodedTime.txt (should be) 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 ... My boss and I are trying to complete the same task (he figured out how to do it, but his code uses a while != "" loop and doesn't look pythony (it looks too 'c'). Not that there's anything wrong with that! Any help is really appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
On Tue, 26 Dec 2006 03:21:57 -0800, Luis Armendariz wrote: > On Tuesday, 26.12.06 at 21:28, Steven D'Aprano wrote: >> >> # WARNING: untested >> def run_without_stdout(*args, **kwargs): >> function = args[0] >> args = args[1:] >> savestdout = sys.stdout >> sys.stdout = cStringIO.StringIO() >> result = None >> try: >> result = function(*args, **kwargs) >> finally: >> # don't forget to restore stdout, or you >> # really will regret it... >> sys.stdout = savestdout >> return result >> > > There's no need for savestdout. There's a backup copy in sys.__stdout__ You shouldn't assume that sys.stdout is standard output when the function is called. It may already have been reassigned to something else. My code will restore sys.stdout to whatever state it was in before the function ran. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: Persistent variables in python
On Tue, 26 Dec 2006 15:01:40 -0800, buffi wrote: >> def doStuff(some, arguments, may, *be, **required): >> try: >> doStuff.timesUsed += 1 >> except AttributeError: >> doStuff.timesUsed = 1 >> # ... special case for first call ... >> # ...common code... > > True, the recursivity is not needed there I guess :) > > It just feels so ugly to use try/except to enable the variable but I've > found it useful at least once. That's a matter of taste. Try replacing the try...except block with hasattr: def doStuff(): if hasattr(doStuff, timesUsed): doStuff.timesUsed += 1 else: doStuff.timesUsed = 1 do_common_code Here is another alternative, using the fact that Python creates default values for arguments once when the function is compiled, not each time it is run: def doStuff(some, *arguments, # don't mess with the following private argument __private={'timesUsed': 0, 'otherData': 'Norwegian Blue'}): """ Do stuff with some arguments. Don't pass the __private argument to the function unless you know what you are doing, it is for private use only. """ __private['timesUsed'] += 1 do_common_code -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie: Open file -> read characters & multiply
gonzlobo wrote: > I've been using Python for a few days. It's such the perfect language > for parsing data! > > I really like it so far, but I'm having a hard time reading a file, > reading the first few hex characters & converting them to an integer. > Once the characters are converted to an integer, I'd like to write the > data to another file. > > Here's the code snipped to the bare minimum: > --- > # Open File > AP_File= open("AP.txt", "r") > decoded_File= open("decoded.txt", "w") > > # read & process data line by line > for line in AP_File: > time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > hosed! > decodedFile.write(time) > > #close files > AP_File.close() > decoded_File.close() > --- > AP.txt > 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 > 00d5 26 0601 80 00 80 00 02 37 fe 54 01 09 > 00d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 > 00d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 > 00d5 26 0604 80 00 fe 54 02 09 80 00 01 5d > 00d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b > 00d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 > 00d5 4f 0611 00 00 00 50 00 00 00 00 07 00 > 00d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b > 00d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 > 00d5 06 0614 80 00 e0 00 02 01 60 79 82 2b > 00d5 0a 0615 08 00 00 00 00 00 00 00 00 00 > 00d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 > (actual files are 250MB!) > > decodedTime.txt (should be) > 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 > ... > > My boss and I are trying to complete the same task (he figured out how > to do it, but his code uses a while != "" loop and doesn't look > pythony (it looks too 'c'). Not that there's anything wrong with that! > > Any help is really appreciated. for line in AP_file: print >>decoded_File, '%s.%04d' % divmod(int(line[:8], 16), 1 ), line[9:].rstrip() or: for line in AP_file: print >>decoded_File, '%.4f' % (int(line[:8], 16) * .0001 ), line[9:].rstrip() --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie: Open file -> read characters & multiply
On Tue, 26 Dec 2006 16:50:06 -0700, gonzlobo wrote: > I've been using Python for a few days. It's such the perfect language > for parsing data! > > I really like it so far, but I'm having a hard time reading a file, > reading the first few hex characters & converting them to an integer. > Once the characters are converted to an integer, I'd like to write the > data to another file. > > Here's the code snipped to the bare minimum: > --- > # Open File > AP_File= open("AP.txt", "r") > decoded_File= open("decoded.txt", "w") > > # read & process data line by line > for line in AP_File: >time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > hosed! >decodedFile.write(time) What does "this line is completely hosed!" mean? Does it crash your PC? Does it raise an exception? Do the wrong thing? Try this: for line in AP_File: # Typical line looks like this: # 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 # This should convert to: # 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 time = int(line[0:8], 16) * 0.0001 line = str(time) + line[8:] decodedFile.write(line) You might need to think about how many decimal places you want the time to store. > My boss and I are trying to complete the same task (he figured out how > to do it, but his code uses a while != "" loop and doesn't look > pythony (it looks too 'c'). Not that there's anything wrong with that! You can convert this: AP_File= file("AP.txt", "r") # file is recommended over "open" line = AP_File.readline() while line != "": do_something_with_line line = AP_File.readline() AP_File.close() into this: AP_File= file("AP.txt", "r") for line in AP_File: do_something_with_line AP_File.close() You need to look at what your boss does with the lines, not how he does the loop. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
[EMAIL PROTECTED] wrote: > After some trials I found that put "os.close(1)" before calling the > function will depress the output. In fact, "os.close(1)" closed > standard output, but I don't know how to open it again after the function's > execution. Try this: fd = os.dup(1) os.close(1) sys.stdout = os.fdopen(fd) It's poor design and extremely inconsiderate for a library to print to stdout except as an overridable default.(Not to mention it gives ammo to people who want dynamically scoped globals.) A long term solution might be to haggle the author to stop being such a jerk. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: How to suppress the output of an external module ?
[EMAIL PROTECTED] wrote: > Hi, > > I'm writing a program which uses an external module written in C > and calls a function provided by the module to do my job. The > function produces a lot of output to the stdout. > > Is there a way to suppress the output produced by the function and > hence make my program run faster? > It's too complicated for me to modify the source code and recompile > the external module. This would be the best method, you could define printf and fprintf macros that would eliminate the output code altogether. > Any hints will be greatly appreciated. Well, it will depend on your OS, but the trick is to essentially replace the C stdout channel with a file which has been opened to write to "/dev/null" or "NUL.txt" (unix and Windows respectively). You'll need to first copy the channel to another so you can use it again after the function is done (a system call). Next do the raw open (which should get the available channel), and the C stdout stuff is successfully redirected. Once done w/ your function, close your new stdout and copy the channel back. --Scott David Daniels [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to depress the output of an external module ?
Carl Banks wrote: > [EMAIL PROTECTED] wrote: > > After some trials I found that put "os.close(1)" before calling the > > function will depress the output. In fact, "os.close(1)" closed > > standard output, but I don't know how to open it again after the function's > > execution. > > Try this: > > fd = os.dup(1) > os.close(1) > sys.stdout = os.fdopen(fd) Also, right after closing file descriptor 1, you might want to set it to something so as to eliminate an annoying warning message when Python tries to close it upon termination but finds it already closed. This opens /dev/null and puts it in file descriptor 1 if not there already (the fdz != 1 test might be unnecessary; I don't know if all flavors of Unix always use the lowest available file descriptor). fdz = os.open("/dev/null",os.O_WRONLY) if fdz != 1: os.dup2(fdz,1) os.close(fdz) Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie: Open file -> read characters & multiply
gonzlobo wrote: > I've been using Python for a few days. It's such the perfect language > for parsing data! > > I really like it so far, but I'm having a hard time reading a file, > reading the first few hex characters & converting them to an integer. > Once the characters are converted to an integer, I'd like to write the > data to another file. > > Here's the code snipped to the bare minimum: > --- > # Open File > AP_File= open("AP.txt", "r") > decoded_File= open("decoded.txt", "w") > > # read & process data line by line > for line in AP_File: >time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > hosed! >decodedFile.write(time) > > #close files > AP_File.close() > decoded_File.close() > --- > AP.txt > 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 > 00d5 26 0601 80 00 80 00 02 37 fe 54 01 09 > 00d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 > 00d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 > 00d5 26 0604 80 00 fe 54 02 09 80 00 01 5d > 00d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b > 00d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 > 00d5 4f 0611 00 00 00 50 00 00 00 00 07 00 > 00d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b > 00d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 > 00d5 06 0614 80 00 e0 00 02 01 60 79 82 2b > 00d5 0a 0615 08 00 00 00 00 00 00 00 00 00 > 00d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 > (actual files are 250MB!) > > decodedTime.txt (should be) > 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 > ... > > My boss and I are trying to complete the same task (he figured out how > to do it, but his code uses a while != "" loop and doesn't look > pythony (it looks too 'c'). Not that there's anything wrong with that! > > Any help is really appreciated. Use the built-in int(). It has an optional argument "radix" which specifies the base for the conversion. For example: >>> int("0x0A", 16) >>> 10 -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie: Open file -> read characters & multiply
WaterWalk wrote: > gonzlobo wrote: > > I've been using Python for a few days. It's such the perfect language > > for parsing data! > > > > I really like it so far, but I'm having a hard time reading a file, > > reading the first few hex characters & converting them to an integer. > > Once the characters are converted to an integer, I'd like to write the > > data to another file. > > > > Here's the code snipped to the bare minimum: > > --- > > # Open File > > AP_File= open("AP.txt", "r") > > decoded_File= open("decoded.txt", "w") > > > > # read & process data line by line > > for line in AP_File: > >time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > > hosed! > >decodedFile.write(time) > > > > #close files > > AP_File.close() > > decoded_File.close() > > --- > > AP.txt > > 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 > > 00d5 26 0601 80 00 80 00 02 37 fe 54 01 09 > > 00d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 > > 00d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 > > 00d5 26 0604 80 00 fe 54 02 09 80 00 01 5d > > 00d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b > > 00d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 > > 00d5 4f 0611 00 00 00 50 00 00 00 00 07 00 > > 00d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b > > 00d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 > > 00d5 06 0614 80 00 e0 00 02 01 60 79 82 2b > > 00d5 0a 0615 08 00 00 00 00 00 00 00 00 00 > > 00d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 > > (actual files are 250MB!) > > > > decodedTime.txt (should be) > > 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 > > ... > > > > My boss and I are trying to complete the same task (he figured out how > > to do it, but his code uses a while != "" loop and doesn't look > > pythony (it looks too 'c'). Not that there's anything wrong with that! > > > > Any help is really appreciated. > > Use the built-in int(). It has an optional argument "radix" which > specifies the base for the conversion. For example: > >>> int("0x0A", 16) > >>> 10 Oh I forget that ">>>" will cause the line to be hidden by default. The example is: int("0x0A", 16)# will return 10 -- http://mail.python.org/mailman/listinfo/python-list
Re: Noobie: Open file -> read characters & multiply
WaterWalk wrote: > WaterWalk wrote: > > gonzlobo wrote: > > > I've been using Python for a few days. It's such the perfect language > > > for parsing data! > > > > > > I really like it so far, but I'm having a hard time reading a file, > > > reading the first few hex characters & converting them to an integer. > > > Once the characters are converted to an integer, I'd like to write the > > > data to another file. > > > > > > Here's the code snipped to the bare minimum: > > > --- > > > # Open File > > > AP_File= open("AP.txt", "r") > > > decoded_File= open("decoded.txt", "w") > > > > > > # read & process data line by line > > > for line in AP_File: > > >time = int(hex(line[0:8]), 16) * 0.0001 # this line is completely > > > hosed! > > >decodedFile.write(time) > > > > > > #close files > > > AP_File.close() > > > decoded_File.close() > > > --- > > > AP.txt > > > 00d5 26 0600 80 00 ec 80 02 03 7d db 02 33 > > > 00d5 26 0601 80 00 80 00 02 37 fe 54 01 09 > > > 00d5 06 0602 80 00 e0 00 01 29 fe d2 69 99 > > > 00d5 06 0603 80 00 e0 00 02 29 fe d2 6a 99 > > > 00d5 26 0604 80 00 fe 54 02 09 80 00 01 5d > > > 00d5 06 0605 80 00 e0 00 02 15 fc 71 ca 0b > > > 00d5 4a 0610 81 00 86 00 02 26 12 00 02 a6 > > > 00d5 4f 0611 00 00 00 50 00 00 00 00 07 00 > > > 00d5 06 0612 80 00 e0 00 01 15 fc 71 c9 0b > > > 00d5 0a 0613 08 5c 04 88 08 98 00 00 00 00 > > > 00d5 06 0614 80 00 e0 00 02 01 60 79 82 2b > > > 00d5 0a 0615 08 00 00 00 00 00 00 00 00 00 > > > 00d5 26 0616 80 00 80 00 02 5d 04 22 3a 88 > > > (actual files are 250MB!) > > > > > > decodedTime.txt (should be) > > > 0.0213 26 0600 80 00 ec 80 02 03 7d db 02 33 > > > ... > > > > > > My boss and I are trying to complete the same task (he figured out how > > > to do it, but his code uses a while != "" loop and doesn't look > > > pythony (it looks too 'c'). Not that there's anything wrong with that! > > > > > > Any help is really appreciated. > > > > Use the built-in int(). It has an optional argument "radix" which > > specifies the base for the conversion. For example: > > >>> int("0x0A", 16) > > >>> 10 > > Oh I forget that ">>>" will cause the line to be hidden by default. The > example is: > int("0x0A", 16)# will return 10 I misunderstand the question, sorry for this. Why not just split the line read since each number is separated by space or tab. After splitting there is a list of numbers, then convert the first element and write the list into a file. -- http://mail.python.org/mailman/listinfo/python-list
Re: Newbie: what is a usefull IDE for Python on Windows ?
Osiris wrote: > what is a usefull IDE for Python on Windows ? The Zeus IDE: http://www.zeusedit.com/python.html Jussi -- http://mail.python.org/mailman/listinfo/python-list
loose methods : Smalltalk asPython
Hi. One of the things I'd like to do with Python is find a way to consistently implement Smalltalk's "loose methods". This is a capability whereby additional methods can be added dynamically to existing classes. In some respects, it's possible with Python. While "object" cannot be touched, it's possible to define class MutableObject( object ) : pass ; and derive subclasses from MutableObject instead of object. Thereafter, for instance, if devising a class class Foo( MutableObject ) : isFoo = True ; ... you can also put in its module MutableObject.isFoo = False ; So, at least for the inheritance hierarchy descending from MutableObject, for an arbitrary object instance x, x.isFoo will return whether it's a Foo or not, effectively although not transparently extending the builtin type(.). Like, then, what of other classes which descend from object and not MutableObject? You'd love to be able to set methods and attributes within them. Can you? These provide on-the-spot extensions of their capability without having to subclass them (not such a big deal) or get everyone to use the new subclass (a very big deal). Comments? Suggestions? Thanks, -- Jan -- http://mail.python.org/mailman/listinfo/python-list
module with a threading-like api that uses processes?
I could have sworn someone was working on a module recently with a threading-like API that used subprocesses under the covers, but 10 minutes or so of googling didn't yield anything. Pointers appreciated. Thx, Skip -- http://mail.python.org/mailman/listinfo/python-list
Re: module with a threading-like api that uses processes?
[EMAIL PROTECTED] wrote: > I could have sworn someone was working on a module recently with a > threading-like API that used subprocesses under the covers, but 10 minutes > or so of googling didn't yield anything. Pointers appreciated. http://www.python.org/dev/summary/2006-10-01_2006-10-15/#processes-and-threading-module-api STeVe -- http://mail.python.org/mailman/listinfo/python-list
Re: module with a threading-like api that uses processes?
At Wednesday 27/12/2006 01:58, [EMAIL PROTECTED] wrote: I could have sworn someone was working on a module recently with a threading-like API that used subprocesses under the covers, but 10 minutes or so of googling didn't yield anything. Pointers appreciated. Perhaps this project? I just wrote rthread library that makes distributed computing look like writing multi-threaded programs. The only thing required from remote hosts is ssh server and executable rthread_server (included in the package) in PATH. (The library executes "ssh -C -c blowfish remotehost 'rthread_server'", and uses stdin and stdout of that process as a communication channel.) There is no need for interface definitions, no need to open communication ports, no need to copy remotely run function code to remote hosts. http://www.cs.tut.fi/~ask/rthread/index.html -- Gabriel Genellina Softlab SRL __ Preguntá. Respondé. Descubrí. Todo lo que querías saber, y lo que ni imaginabas, está en Yahoo! Respuestas (Beta). ¡Probalo ya! http://www.yahoo.com.ar/respuestas -- http://mail.python.org/mailman/listinfo/python-list
Re: merits of Lisp vs Python
On Sat, 23 Dec 2006 12:38:30 -0800, Fuzzyman wrote: > > Lars Rune Nøstdal wrote: >> On Fri, 08 Dec 2006 03:07:09 -0800, Mark Tarver wrote: >> >> > How do you compare Python to Lisp? What specific advantages do you >> > think that one has over the other? >> > >> > Note I'm not a Python person and I have no axes to grind here. This is >> > just a question for my general education. >> > >> > Mark >> >> Kill this frakkin thread; Lisp rules -- while Python is boring (but better >> than many other alternatives). E.O.F. >> > > Perhaps only with the addendum that although 'Lisp roolz', no-one uses > for anything of relevance anymore and it is continuing it's geriatric > decline into obscurity. ;-) Screw it; I'll die a "non-professional" programmer if I have to. Meanwhile ridiculing and scorning all the fools using inferior languages. *hah!* If I can't do what I love when it comes to programming I'd rather have a shitty non-programming job that enables me to instantly forget what I've been doing while at work as soon as I'm done for the day. My trade -- my tools. :} -- Lars Rune Nøstdal http://nostdal.org/ -- http://mail.python.org/mailman/listinfo/python-list