Re: Is this the right way to use unicode in a user defined Exception?
一首诗 wrote: # class MyError(Exception): def __init__(self): self.message = u'Some Chinese:中文' def __str__(self): return self.message.encode('utf8') # This is an exception that I defined. I have to pass it to third party libraries. As many libraries simply use str(e) to log, if I don't encode it in __str___, they will fail. But I am not quite certain if it's the right thing to do. Shouldn't every library expect to use unicode everywhere? Shouldn't they use something like : log(unicode(e)) In 3.0, text is unicode. So libraries will mostly expect it. -- http://mail.python.org/mailman/listinfo/python-list
The Python's regex different from Perl's ,I want know what's the different?
The Python's regex different from Perl's ,I want know what's the different? -- http://mail.python.org/mailman/listinfo/python-list
i have problem with glob.glob() in remotely directory
hey i want to count number of files in remote computer example of my code is import glob import os import time from datetime import date today=date.today() dir_count, file_count=0, 0 for files in glob.glob('\\192.168.0.45\files\*.txt'): file_count += len(files) print 'the count of ',today, '---' print 'Found', dir_count, 'sub-directories in cwd' print 'Found', file_count, 'files in cwd' print 'Found', dir_count + file_count, 'files sub-directories in cwd' filename=reconciliation.txt file_string= str(file_count)+','+ str(today)+'\n' File=open(filename,a) File.writelines( file_string) File.close() i get zero results since glob.glob( )can not traverse in \\192.168.0.45 can I get help please how can i do that??? -- http://mail.python.org/mailman/listinfo/python-list
Proposed implementation for an Ordered Dictionary
Here's a proposed implementation for Py2.7 and Py3.1: http://code.activestate.com/recipes/576669/ Would like you guys to kick the tires, exercise it a bit, and let me know what you think. The recipe runs under 2.6 and 3.0 without modification so it should be easy to play with. The main virtue of the proposed API is a near-zero learning curve. The API is a same as for regular dictionaries but it remembers the insertion order and used that for iteration, repr, etc. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: i have problem with glob.glob() in remotely directory
On Thu, Feb 26, 2009 at 1:05 AM, lameck kassana chela...@gmail.com wrote: hey i want to count number of files in remote computer example of my code is import glob import os import time from datetime import date today=date.today() dir_count, file_count=0, 0 for files in glob.glob('\\192.168.0.45\files\*.txt'): Remember that *backslash is the escape character* in Python, so you need to double-up your backslashes in the string literal (or just use forward slashes instead, Windows doesn't seem to care for Python in most cases). Right now, the path really only starts with 1 backslash and it has a formfeed character in it (\f), so it's obviously invalid; thus, your problem. So you want: #looks ugly, doesn't it? for files in glob.glob('192.168.0.45\\files\\*.txt'): Or: #will probably but not assuredly work for files in glob.glob('//192.168.0.45/files/*.txt'): Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: File Path retrieving problem
Hi Dennis Thanks for your reply and also the detailed explanation. Or do you mean you want something that, given a bare name, searches your file system to find where a file with that name actually is? Yes, this is what i exactly needed. I have found something interesting to do this using the os.walk(path) function, and it worked I am pasting the code which i used for this: testpath=rC:\\ logpath=rC:\Pathfinder.txt csvpath=rC:\Pathfinder.csv import os, csv def logData(d={}, logfile=c://filename999.txt, separator=\n): Takes a dictionary of values and writes them to the provided file path logstring=separator.join([str(key)+: +d[key] for key in d.keys()])+\n f=open(logfile,'a') f.write(logstring) f.close() return def walker(topPath,csvpath): fDict={} logDict={} limit=1000 freed_space=0 items=0 fileHandle = csv.writer(open(csvpath, 'w')) fileHandle.writerow(['Index','Path']) for root, dirs, files in os.walk(topPath): for name in files: fpath=os.path.join(root,name) logDict[Name]=name logDict[Path]=fpath if name.find(NEWS): continue else: logData(logDict, logpath, \t) fDict[items] = name items=len(fDict.keys()) fileHandle.writerow([items,fpath]) print Dict entry: ,items, print Path: ,fpath if items limit: break if items limit: break walker(testpath,csvpath) Finally thank you all very much for the patience to listen to me. Regards, Sudhir -- http://mail.python.org/mailman/listinfo/python-list
Re: a look at the browser scene emacs
On Feb 26, 12:57 am, Miles Bader mi...@gnu.org wrote: There is ample room for people to discuss this evolution, but approaches that start with first, toss out the existing user interface aren't gonna fly. Who said to toss out existing user interface, you? Are you saying that i start my suggestion with “throw out existing UI”? If so, please point out where. Emacs isn't going to turn into a fancy notepad clone, regardless of what modern users may (think they) want... In what way you imagine emacs is going to be a fancy Microsoft Notepad clone? I've wrote the following suggestions on emacs modernization in the past 3 years: • The Modernization of Emacs http://xahlee.org/emacs/modernization.html • Suggestions on Emacs's Scratch Buffer http://xahlee.org/emacs/modernization_scratch_buffer.html • Emacs's M-‹key› Notation vs Alt+‹key› Notation http://xahlee.org/emacs/modernization_meta_key.html • Emacs's Menu Usability Problem http://xahlee.org/emacs/modernization_menu.html • Emacs's Mode Line Modernization Suggestions http://xahlee.org/emacs/modernization_mode_line.html • Usability Problems With Emacs's Letter-Case Commands http://xahlee.org/emacs/modernization_upcase-word.html • Suggestions on Emacs's mark-word Command http://xahlee.org/emacs/modernization_mark-word.html • Suggestions on Emacs's Line-Cutting Commands http://xahlee.org/emacs/modernization_fill-paragraph.html • Emacs Should Adopt HTML To Replace Texinfo http://xahlee.org/emacs/modernization_html_vs_info.html • Emacs Should Support HTML Mail http://xahlee.org/emacs/modernization_html_mail.html • Emacs's HTML Mode Sucks http://xahlee.org/emacs/emacs_html_sucks.html which item, in which article, do you think that emacs is going to turn into Notepad clone? is the suggestion of using modern standard shortcut set of X C V for Cut, Copy, Paste, of which Linux uses, means it is turning emacs to a fancy Notepad clone? Is fixing emacs's confusing undo and no redo, that is periodically bitched by programer in blogs, considered making emacs into a Notepad clone? Is the suggestion for a statistics based ergonomic keybinding design that are more faster to execute, easier on the fingers, and easier to remember, mean it is turning emacs to a fancy notepad clone? is the suggestion of getting rid of *scratch* buffer, and introduce a command “New” with shortcut Ctrl+n, that creates new buffer anytime anywhere, which lets user create multiple scratch buffers defaulting to any mode and compatible for the rest of Linux's shortcuts, means it is a fancy Microsoft Notepad? is the suggestion of changing notation from C- and M- to Ctrl+ and Alt +, such that it reflects the lable on the keyboard, and Richard Stallman agrees may be a good idea, means it's Notepad? is the suggestion of supporting html mail, and interface to gmail out of the box, means it's becoming Microsoft Notepad? is it simply the fact that making things easier to use, means kissing Microsoft's ass? Is the open source Firefox, and Google's extremely advanced technologies and easy to use applications such as gmail, google map, google earth, google code, all becoming Microsoft Notepad clone? Xah ∑ http://xahlee.org/ ☄ -- http://mail.python.org/mailman/listinfo/python-list
RE: Queries
It is, however I was able to get the first 8 done, I am struggling with these 3 particular ones. I have to make an SQL file based off of it, so this seems to be a blockage in my works. -Original Message- From: Gary Herron [mailto:gher...@islandtraining.com] Sent: Thursday, February 26, 2009 1:46 AM To: mmcclaf; python-list@python.org Subject: Re: Queries mmcclaf wrote: I have to make some queries for 4 tables I have. The following relations are: Classes(class, type, country, numGuns, bore, displacement) Ships (name, class, launched) Battles (name, date) Outcomes (ship, battle, result) The three queries I'm stuck on are the following: 1. Find the classes that have only one ship as a member of that class (not all ships are listed in the Ship table) 2. Find the countries that had both battleships and battlecruisers (those fall under type in Classes) 3. Find those ships that lived to fight another day; they were damaged in one battle, but later fought in another. The best way for me to understand would be relational algebra for each of the statements. Sounds like a homework assignment.Good luck with it. Any help in this would be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list No virus found in this incoming message. Checked by AVG - www.avg.com Version: 8.0.237 / Virus Database: 270.11.3/1971 - Release Date: 02/25/09 06:40:00 -- http://mail.python.org/mailman/listinfo/python-list
Building python extensions
Lets say I compile python 2.6.1 without readline support due to not having the readline headers installed prior. Is there a standard way to recompile the extension without having to rebuild the entire source. Looking at the makefile, I found the gcc line (modified to dump the relocatable in the source root ): $ gcc -pthread -fPIC -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -I/py/site/Python-2.6.1/./Include -I. -IInclude -I./Include -I/usr/local/include -I/py/site/Python-2.6.1/Include -I/py/site/Python-2.6.1 -c /py/site/Python-2.6.1/Modules/readline.c -o build/temp.linux-x86_64-2.6/py/site/Python-2.6.1/Modules/readline.o -o readline.so This works (cp -av readline.so /py/2.6.1/lib/python2.6/lib-dynload/), but is less than ideal. Maybe I am missing something in the docs? In php there is phpize, is there something like this for python? 'python setup.py build' builds all the extensions in Modules/ This is my first time posting here and I am pretty new to python, so pardon me if my my logic is a bit off. I love reading, so feel free to link to the relevant documentation :) Jrod -- http://mail.python.org/mailman/listinfo/python-list
Python-URL! - weekly Python news and links (Feb 26)
QOTW: I believe this is because Microsoft failed to understand the original meaning of ___, and persisted with this ghastly error in the name of backwards compatibility, justifying it by suggesting that _. Somebody should have cards printed up... - the comedy duo of Steve Holden and Grant Edwards http://groups.google.com/group/comp.lang.python/msg/5f44b59ba85c94c7 Code challenge: generate all integers using only digits 1,2,3 and arithmetic operations http://groups.google.com/group/comp.lang.python/t/b387f99deb376392/ How pythonic is to put code inside __init__.py? http://groups.google.com/group/comp.lang.python/t/ee5fe57066843c94/ datetime.time treats midnight as False - is that sensible? http://groups.google.com/group/comp.lang.python/t/9e3914f85feb4ddc/ SQLAlchemy got very much appreciation in this thread: does a lot more than just an ORM, but can be used as a better DB-API too http://groups.google.com/group/comp.lang.python/t/53ddd0045c89e315/ FAQ: I create a big object and later destroy it. Why does memory usage not return to its initial value? http://groups.google.com/group/comp.lang.python/t/2c6ed3f94378b29c/ How mmap (and numpy.memmap) works: http://groups.google.com/group/comp.lang.python/t/4fc4966065012dad/ The right way to print 'bytes' in Python 3.0: http://groups.google.com/group/comp.lang.python/t/95033de64e9c35e8/ How do arguments get passed to functions? http://groups.google.com/group/comp.lang.python/t/fb8c61eb265123db/ Everything Python-related you want is probably one or two clicks away in these pages: Python.org's Python Language Website is the traditional center of Pythonia http://www.python.org Notice especially the master FAQ http://www.python.org/doc/FAQ.html PythonWare complements the digest you're reading with the marvelous daily python url http://www.pythonware.com/daily Just beginning with Python? This page is a great place to start: http://wiki.python.org/moin/BeginnersGuide/Programmers The Python Papers aims to publish the efforts of Python enthusiats: http://pythonpapers.org/ The Python Magazine is a technical monthly devoted to Python: http://pythonmagazine.com Readers have recommended the Planet sites: http://planetpython.org http://planet.python.org comp.lang.python.announce announces new Python software. Be sure to scan this newsgroup weekly. http://groups.google.com/group/comp.lang.python.announce/topics Python411 indexes podcasts ... to help people learn Python ... Updates appear more-than-weekly: http://www.awaretek.com/python/index.html The Python Package Index catalogues packages. http://www.python.org/pypi/ The somewhat older Vaults of Parnassus ambitiously collects references to all sorts of Python resources. http://www.vex.net/~x/parnassus/ Much of Python's real work takes place on Special-Interest Group mailing lists http://www.python.org/sigs/ Python Success Stories--from air-traffic control to on-line match-making--can inspire you or decision-makers to whom you're subject with a vision of what the language makes practical. http://www.pythonology.com/success The Python Software Foundation (PSF) has replaced the Python Consortium as an independent nexus of activity. It has official responsibility for Python's development and maintenance. http://www.python.org/psf/ Among the ways you can support PSF is with a donation. http://www.python.org/psf/donations/ The Summary of Python Tracker Issues is an automatically generated report summarizing new bugs, closed ones, and patch submissions. http://search.gmane.org/?author=status%40bugs.python.orggroup=gmane.comp.python.develsort=date Although unmaintained since 2002, the Cetus collection of Python hyperlinks retains a few gems. http://www.cetus-links.org/oo_python.html Python FAQTS http://python.faqts.com/ The Cookbook is a collaborative effort to capture useful and interesting recipes. http://code.activestate.com/recipes/langs/python/ Many Python conferences around the world are in preparation. Watch this space for links to them. Among several Python-oriented RSS/RDF feeds available, see: http://www.python.org/channews.rdf For more, see: http://www.syndic8.com/feedlist.php?ShowMatch=pythonShowStatus=all The old Python To-Do List now lives principally in a SourceForge reincarnation. http://sourceforge.net/tracker/?atid=355470group_id=5470func=browse
Re: a look at the browser scene emacs
Xah Lee xah...@gmail.com writes: Hi Xah, is the suggestion of using modern standard shortcut set of X C V for Cut, Copy, Paste, of which Linux uses, means it is turning emacs to a fancy Notepad clone? The functionality stays the same, but IMO it would confuse most users. Killing is not cutting, yanking is not pasting. The whole concepts (kill-ring vs. simple copypaste) are much different. Is fixing emacs's confusing undo and no redo, that is periodically bitched by programer in blogs, considered making emacs into a Notepad clone? It's much more advanced than the usual sequential undo, but I admit that it can get confusing sometimes. So instead of dropping it I'd prefer to think about a better UI for it. Is the suggestion for a statistics based ergonomic keybinding design that are more faster to execute, easier on the fingers, and easier to remember, mean it is turning emacs to a fancy notepad clone? Users use different commands and your bindings may be better for you on your querty keyboard, but I'm sure they're not on my German Dvorak Type II keyboard. is the suggestion of getting rid of *scratch* buffer, and introduce a command “New” with shortcut Ctrl+n, that creates new buffer anytime anywhere, which lets user create multiple scratch buffers defaulting to any mode and compatible for the rest of Linux's shortcuts, means it is a fancy Microsoft Notepad? Such a easy key like C-n is much too valuable for such a rarely used command. C-x b foobar RET is ok, isn't it? is the suggestion of changing notation from C- and M- to Ctrl+ and Alt +, such that it reflects the lable on the keyboard, and Richard Stallman agrees may be a good idea, means it's Notepad? Nope, but I'm not sure if it's possible for emacs to get the right key. Here, M is Alt, but Ctrl is indeed on the CapsLock key... And it makes key sequences much longer to write with little or no benefit. is the suggestion of supporting html mail, and interface to gmail out of the box, means it's becoming Microsoft Notepad? Definitively not. AFAIK Gnus can handle gmail accounts quite well. Reading HTML mail works nice, too (with emacs-w3m as helper). A simple editor would be nice for some people, too. But I guess that most current devs arent interested in writing one, cause in the tech geekers world mail is in text/plain. Bye, Tassilo -- http://mail.python.org/mailman/listinfo/python-list
subprocess module: execution of standard binaries without shell?
hi all while getting used to with subprocess module i failed in executuing a) but succeeded in running b). Can anyone explain me why as i am providing absolute path? Is this has to do anything with shared library.. which must be accessed based on system variables? a) pipe = subprocess.Popen(/bin/ls /, stdout=subprocess.PIPE, close_fds=True) ==OSError: [Errno 2] No such file or directory b) pipe = subprocess.Popen(/bin/ls /, stdout=subprocess.PIPE, close_fds=True, shell=True) -- Thanks Regards visco -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess module: execution of standard binaries without shell?
Visco Shaun schrieb: hi all while getting used to with subprocess module i failed in executuing a) but succeeded in running b). Can anyone explain me why as i am providing absolute path? Is this has to do anything with shared library.. which must be accessed based on system variables? a) pipe = subprocess.Popen(/bin/ls /, stdout=subprocess.PIPE, close_fds=True) ==OSError: [Errno 2] No such file or directory You have to use a list instead of a string here. pipe = subprocess.Popen([/bin/ls, /], stdout=subprocess.PIPE) -- http://mail.python.org/mailman/listinfo/python-list
EOL for sys.stdin.readline() and raw_input()
Hi, Are there any way to change the EOL character for sys.stdin.readline() and raw_input()? My problem is that i cannot rely on that the clients connection to my application will end all lines with \n or \r\n. Sometimes they will use \r\000 as EOL. Example from my code: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Any suggestions? Regards, jkv -- http://mail.python.org/mailman/listinfo/python-list
Re: subprocess module: execution of standard binaries without shell?
On Thu, Feb 26, 2009 at 2:41 AM, Visco Shaun visc...@gmail.com wrote: hi all while getting used to with subprocess module i failed in executuing a) but succeeded in running b). Can anyone explain me why as i am providing absolute path? Is this has to do anything with shared library.. which must be accessed based on system variables? a) pipe = subprocess.Popen(/bin/ls /, stdout=subprocess.PIPE, close_fds=True) ==OSError: [Errno 2] No such file or directory You need to use a list of arguments, not just a string. You're currently telling Python to try and run a nonexistent directory (specifically, the ls subdirectory of /bin), since the string way of calling Popen assumes that the *entire* string is the path to the executable when shell=False. The correct way is to provide the path to the binary and then each of its arguments, in a list: pipe = subprocess.Popen([/bin/ls, /], stdout=subprocess.PIPE, close_fds=True) b) pipe = subprocess.Popen(/bin/ls /, stdout=subprocess.PIPE, close_fds=True, shell=True) This works because shell=True sends the string through the shell, which tokenizes it and runs it, effectively splitting the string into a list for you. However, shell=True is dangerous as you need to be careful to escape special characters, whereas that's not necessary for the 'shell=False and list' way of calling Popen. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Delete all items in the list
Hi. This must be a simple command but I just can't find it in the Phthon manual. How do I delete all items with a certain condition from a list? For instance: L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? I would really appreciate your help. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
sending gmail with smtplib from behind a proxy
Hi, I tried the script below to send mail by gmail from python using smptlib. I got the script here (http://kutuma.blogspot.com/2007/08/ sending-emails-via-gmail-with-python.html) It works fine from home, but at work not. I think it is because I am behind a http or a socks proxy server at work. Can I adapt this script to work from behind the http or socks proxy? I couldnt understand from the smtplib if I can specify this somehow? Sincerely, bjorn johansson Script- #!/usr/bin/python import smtplib from email.MIMEMultipart import MIMEMultipart from email.MIMEBase import MIMEBase from email.MIMEText import MIMEText from email import Encoders import os gmail_user = your_em...@gmail.com gmail_pwd = your_password def mail(to, subject, text, attach): msg = MIMEMultipart() msg['From'] = gmail_user msg['To'] = to msg['Subject'] = subject msg.attach(MIMEText(text)) part = MIMEBase('application', 'octet-stream') part.set_payload(open(attach, 'rb').read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename=%s' % os.path.basename(attach)) msg.attach(part) mailServer = smtplib.SMTP(smtp.gmail.com, 587) mailServer.ehlo() mailServer.starttls() mailServer.ehlo() mailServer.login(gmail_user, gmail_pwd) mailServer.sendmail(gmail_user, to, msg.as_string()) # Should be mailServer.quit(), but that crashes... mailServer.close() mail(some.per...@some.address.com, Hello from python!, This is a email sent with python, my_picture.jpg) -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
On Thu, Feb 26, 2009 at 3:05 AM, Clarendon jine...@hotmail.com wrote: Hi. This must be a simple command but I just can't find it in the Phthon manual. How do I delete all items with a certain condition from a list? For instance: L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? There are several ways. I'd go with a list comprehension: L = [i for i in L if i != 'a'] Or to modify the list in-place: L[:] = [i for i in L if i != 'a'] Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python's regex different from Perl's ,I want know what's the different?
The Python's regex different from Perl's ,I want know what's the different? Easy: bash$ diff (lynx -dump http://perldoc.perl.org/perlre.html) (lynx -dump http://docs.python.org/library/re.html) Had you tried the great Google in the sky, you might have found such pages as: http://mail.python.org/pipermail/python-list/2005-April/319030.html or even a popular book on the subject: http://oreilly.com/catalog/9780596002893/ -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: i have problem with glob.glob() in remotely directory
i did try but still not working.But also i try os.walk() for remote computer like os.walk('\\192.168.0.45') it also failed Thats it is my main problem do i need any new imports besides import os On 2/26/09, Chris Rebert c...@rebertia.com wrote: On Thu, Feb 26, 2009 at 1:05 AM, lameck kassana chela...@gmail.com wrote: hey i want to count number of files in remote computer example of my code is import glob import os import time from datetime import date today=date.today() dir_count, file_count=0, 0 for files in glob.glob('\\192.168.0.45\files\*.txt'): Remember that *backslash is the escape character* in Python, so you need to double-up your backslashes in the string literal (or just use forward slashes instead, Windows doesn't seem to care for Python in most cases). Right now, the path really only starts with 1 backslash and it has a formfeed character in it (\f), so it's obviously invalid; thus, your problem. So you want: #looks ugly, doesn't it? for files in glob.glob('192.168.0.45\\files\\*.txt'): Or: #will probably but not assuredly work for files in glob.glob('//192.168.0.45/files/*.txt'): Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Efficient searching through objects
I have written a program that reads data and updates the records for some people. They are represented by objects, and I need to read the data from a file, look the person up and then update his record. I have implemented this by creating a list with all the people's names and another list with their objects (their data). It works but after profiling the code it turns out that half the time spent in the program is spent in the list.index() function looking up names. Isn't there a less goofy and more efficient way of doing this? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there something easier than ORM?
On Feb 17, 10:28 pm, alex23 wuwe...@gmail.com wrote: On Feb 18, 3:10 am, Robert Kern robert.k...@gmail.com wrote: Its public image definitely suffers from the impression that it's an ORM that can be compared on equal terms with packages that actually are just ORMs. I describe it as a very powerful toolkit for solving a wide variety of problems involving SQL databases. One of those tools happens to be an ORM. I'm going to have to steal that description the next time I try to sell a co-worker on the value of SQLAlchemy. There's always a strong reaction against the mention of ORMs, generally along the lines of it moving the programmer too far away from the real action. But my experience is identical to both andrew's and your's; there is far far more of value in SQLA than the ORM alone. I just saw this thread via the weekly Python URL email and wanted to add one bit here. When I've been selling people on using SQLAlchemy, one argument that I make is that if you're using a relational database for storage but your program is using objects (and good Python programs do!), then you're doing ORM. If you're not using SQLAlchemy (or similar), you're likely doing ORM badly. SQLAlchemy's SQL layer definitely makes it a different beast from most ORMs. Kevin -- http://mail.python.org/mailman/listinfo/python-list
Efficient searching through objects
sert je...@hotmail.com wrote: I have written a program that reads data and updates the records for some people. They are represented by objects, and I need to read the data from a file, look the person up and then update his record. I have implemented this by creating a list with all the people's names and another list with their objects (their data). It works but after profiling the code it turns out that half the time spent in the program is spent in the list.index() function looking up names. Isn't there a less goofy and more efficient way of doing this? It sounds like what you are looking for is the dictionary data type. --RDM -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient searching through objects
sert: I have implemented this by creating a list with all the people's names and another list with their objects (their data). It works but after profiling the code it turns out that half the time spent in the program is spent in the list.index() function looking up names. Isn't there a less goofy and more efficient way of doing this? Try using a dict instead, where keys are the names and objects the values (it turns a linear search in a quick hash look up). . Then tell us the performance changes. Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list
problem with glob in remote directory or os.walk in remote directory in windos
i am trying to write script which will count files for remote directory which having certain pattern. i tried with my code and have a look at this code. --- import glob import os import time from datetime import date today=date.today() dir_count, file_count=0, 0 for files in glob.glob('*\\192.168.0.45\files\*.txt'*): file_count += len(files) print 'the count of ',today, '---' print 'Found', dir_count, 'sub-directories in cwd' print 'Found', file_count, 'files in cwd' print 'Found', dir_count + file_count, 'files sub-directories in cwd' filename=reconciliation.txt file_string= str(file_count)+','+ str(today)+'\n' File=open(filename,a) File.writelines( file_string) File.close() the problem the glob.glob() does not work in remotely directory especially in window environment i don't know why?? i also try os.walk() for remote directory it failed i am wondering why . Do i have to import a new module in import besides import glob,os -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient searching through objects
bearophileh...@lycos.com wrote in news:37632421-5475-4859-be83-07ae2eca1...@r4g2000yqa.googlegro ups.com: Try using a dict instead, where keys are the names and objects the values (it turns a linear search in a quick hash look up). . Then tell us the performance changes. It halved the overall execution time. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: File Path retrieving problem
music24...@gmail.com wrote: On Feb 26, 9:03 am, Steve Holden st...@holdenweb.com wrote: music24...@gmail.com wrote: On Feb 26, 2:35 am, Emile van Sebille em...@fenx.com wrote: Peter Otten wrote: Maybe it's about access rights? $ mkdir alpha $ touch alpha/beta $ python -cimport os; print os.path.exists('alpha/beta') True $ chmod u-x alpha $ python -cimport os; print os.path.exists('alpha/beta') False $ I Don't know how this is handled on Windows... Here's one way Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\mkdir alpha C:\touch alpha\beta # cygwin at work here... C:\python -cimport os; print os.path.exists('alpha/beta') True C:\cacls alpha /E /R Everyone processed dir: C:\alpha C:\cacls alpha /E /R Users processed dir: C:\alpha C:\cacls alpha /E /R Administrators processed dir: C:\alpha C:\python -cimport os; print os.path.exists('alpha/beta') False Hi, This is the following code which i have used to retrieve the pathname of a filename given by the user filename = self.enText.get() # get the text entered in the text box if os.path.exists(filename): # checking if the filename exists , had replaced it with os.path.exists(os.path.abspath (filename)) print os.path.abspath(filename) else: print Not found actually i need to list all the files/folders with the given filename alongwith path and store it in an CSV file with index. But i am unable to get even a single filepath correctly. Whatever filename i enter the output is as : C:\Python25\WorkSpace \xyz i.e the name i entered is being appended to my workspace and displayed. Guys, i am not able to understand what shall i do here...plz help me out. Look, we aren't psychic. We can't debug code and directories we can't see. Work with us here - give us the information we need to help you. The actual code. A directory listing? Any error messages? regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Hi Steve, Following is the code which i have written to display a GUI textbox and get any name entered by the user and search the path of this filename and then list it into an CSV file. For this i initially created an textbox widget and added an button to it. Now, i have entered a filename (like NEWS.txt) in the text boz, as you can see when i press the button i retrieve the text and then search its filepath. currently i am printing this filepath on the shell. from Tkinter import * So this is Python 2.x ... import os from os.path import realpath, exists, abspath import tkMessageBox import Tkinter Not sure why you bother to import Tkinter when you have above just imported everything *from* Tkinter ... #import filePath class GUIFrameWork(Frame): This is the GUI def __init__(self,master=None): Initialize yourself Initialise the base class Frame.__init__(self,master) Set the Window Title self.master.title(Enter Text to search) Display the main window with a little bit of padding self.grid(padx=10,pady=10) self.CreateWidgets() def CreateWidgets(self): self.enText = Entry(self) self.enText.grid(row=0, column=1, columnspan=3) Create the Button, set the text and the command that will be called when the button is clicked self.btnDisplay = Button(self, text=Display!, command=self.Display) self.btnDisplay.grid(row=0, column=4) def Display(self): Called when btnDisplay is clicked, displays the contents of self.enText filename = self.enText.get() if os.path.exists(os.path.abspath(filename)): print os.path.abspath(filename) First, move this line up one (losing an indent level) so you always print the full path. This will verify you are looking where you think you are looking. else: print Not found #tkMessageBox.showinfo(Text, You typed: %s % os.path.abspath(os.path.dirname(filename))) #filepath.mydir(self.enText.get()) if __name__ == __main__: guiFrame = GUIFrameWork() guiFrame.mainloop() User Input: NEWS.txt Output: Not Found (though this file is actually present) Thanks for the information. I'm wondering whether there might be some filename encoding issue here, but others may have some better ideas. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Clarendon a écrit : Hi. This must be a simple command but I just can't find it in the Phthon manual. How do I delete all items with a certain condition from a list? For instance: L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? L[:] = [item for item in L if item != 'a'] -- http://mail.python.org/mailman/listinfo/python-list
Re: File Path retrieving problem
Dennis Lee Bieber wrote: On Wed, 25 Feb 2009 07:37:21 -0800 (PST), music24...@gmail.com declaimed the following in comp.lang.python: WARNING -- I will probably NOT see your response; since 90+% of the spam in comp.lang.python is injected via googlegroups and gmail accounts, my client is configured to filter out messages with a from address having gmail.com. I only found this message by doing fetches on the reference header of the replies that did get by the filter. Well, since it's more likely you'll see this, let me tell you that if you access the group via gmane (as I do) you rarely see the spam anyway. There's been a lot more work on spam filtering lately. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
Murray wrote: [top-posting corrected] -Original Message- From: Gary Herron [mailto:gher...@islandtraining.com] Sent: Thursday, February 26, 2009 1:46 AM To: mmcclaf; python-list@python.org Subject: Re: Queries mmcclaf wrote: I have to make some queries for 4 tables I have. The following relations are: Classes(class, type, country, numGuns, bore, displacement) Ships (name, class, launched) Battles (name, date) Outcomes (ship, battle, result) The three queries I'm stuck on are the following: 1. Find the classes that have only one ship as a member of that class (not all ships are listed in the Ship table) Investigate a GROUP BY solution that selects groups having a count of 1. 2. Find the countries that had both battleships and battlecruisers (those fall under type in Classes) Look at EXISTS for one possible solutions. 3. Find those ships that lived to fight another day; they were damaged in one battle, but later fought in another. From your model description I don't even see where the position and attitude of each ship is stored, so I don't think I can give you any help at all with this one. The best way for me to understand would be relational algebra for each of the statements. Sounds like a homework assignment.Good luck with it. It is, however I was able to get the first 8 done, I am struggling with these 3 particular ones. I have to make an SQL file based off of it, so this seems to be a blockage in my works. Good luck with the homework. Remember to acknowledge the help you've had from this list (particularly your earlier issues: here you just have hints). regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: i have problem with glob.glob() in remotely directory
Chris Rebert wrote: On Thu, Feb 26, 2009 at 1:05 AM, lameck kassana chela...@gmail.com wrote: hey i want to count number of files in remote computer example of my code is import glob import os import time from datetime import date today=date.today() dir_count, file_count=0, 0 for files in glob.glob('\\192.168.0.45\files\*.txt'): Remember that *backslash is the escape character* in Python, so you need to double-up your backslashes in the string literal (or just use forward slashes instead, Windows doesn't seem to care for Python in most cases). Right now, the path really only starts with 1 backslash and it has a formfeed character in it (\f), so it's obviously invalid; thus, your problem. So you want: #looks ugly, doesn't it? for files in glob.glob('192.168.0.45\\files\\*.txt'): Or: #will probably but not assuredly work for files in glob.glob('//192.168.0.45/files/*.txt'): Or: for files in glob.glob(r'\\192.168.0.45\files\*.txt'): Raw string literals are very useful for handling strings with lots of backslashes in them. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Run a python script as an exe and run a new process from it
Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. My requirement may be little confusing so please get back if you didn't understand what I meant above. Thank you, Venu. -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Chris Rebert wrote: On Thu, Feb 26, 2009 at 3:05 AM, Clarendon jine...@hotmail.com wrote: ... L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? There are several ways. I'd go with a list comprehension: and for a couple other ways while 'a' in L : L.remove('a') L = filter('a'.__ne__,L) -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
2009/2/26 Steve Holden st...@holdenweb.com Murray wrote: [top-posting corrected] -Original Message- From: Gary Herron [mailto:gher...@islandtraining.com] Sent: Thursday, February 26, 2009 1:46 AM To: mmcclaf; python-list@python.org Subject: Re: Queries mmcclaf wrote: I have to make some queries for 4 tables I have. The following relations are: Classes(class, type, country, numGuns, bore, displacement) Ships (name, class, launched) Battles (name, date) Outcomes (ship, battle, result) The three queries I'm stuck on are the following: 1. Find the classes that have only one ship as a member of that class (not all ships are listed in the Ship table) Investigate a GROUP BY solution that selects groups having a count of 1. DISTINCT may be a simpler solution. 2. Find the countries that had both battleships and battlecruisers (those fall under type in Classes) Look at EXISTS for one possible solutions. 3. Find those ships that lived to fight another day; they were damaged in one battle, but later fought in another. From your model description I don't even see where the position and attitude of each ship is stored, so I don't think I can give you any help at all with this one. The best way for me to understand would be relational algebra for each of the statements. Sounds like a homework assignment.Good luck with it. It is, however I was able to get the first 8 done, I am struggling with these 3 particular ones. I have to make an SQL file based off of it, so this seems to be a blockage in my works. Good luck with the homework. Remember to acknowledge the help you've had from this list (particularly your earlier issues: here you just have hints). regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list -- - Bret -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
On Thu, 2009-02-26 at 04:55 -0800, venutaurus...@gmail.com wrote: Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). I don't know how windows deals with this part In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. standard daemon behavior I'd have thought This shows how you would do it (with lots of comments). Assume this should work on Windows. http://code.activestate.com/recipes/278731/ note the two child threads to prevent zombies - may not be needed on windows but good to use them anyway. -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient searching through objects
2009/2/26 sert je...@hotmail.com: bearophileh...@lycos.com wrote in news:37632421-5475-4859-be83-07ae2eca1...@r4g2000yqa.googlegro ups.com: Try using a dict instead, where keys are the names and objects the values (it turns a linear search in a quick hash look up). . Then tell us the performance changes. It halved the overall execution time. Thanks. Good to see that you've already got an improvement. For what it's worth, this seems to be classic relational database stuff, and a relational database will be optimised to do these operations and so is likely to be faster still. I think there are a couple that Python works well with, but I've never looked into that -- others will no doubt be along with recommendations now I've raised the subject. -- Tim Rowe -- http://mail.python.org/mailman/listinfo/python-list
Re: Efficient searching through objects
relational database will be optimised to do these operations and so is likely to be faster still. I think there are a couple that Python works well with, but I've never looked into that -- others will no doubt be along with recommendations now I've raised the subject. batteries-included support for sqlite[1] was added in 2.5 which would work pretty well for this application without the overhead (resource OR administrative) of something like MySQL or PostgreSQL. -tkc [1] http://docs.python.org/library/sqlite3.html -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with glob in remote directory or os.walk in remote directory in windos
lameck kassana wrote: i am trying to write script which will count files for remote directory which having certain pattern. Please refrain from repeating questions. When you posted this a reply had already been made to your original posting. Remember, this isn't a paid help desk ... regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with glob in remote directory or os.walk in remote directory in windos
ok my original question is how can count the files of ceratin pattern(eg *.txt) in remote directory .It seems use of glob.glob() for remote directory is not working .Example I want to count the files in following shared folder \\192.168.0.45\files file://192.168.0.45/files how can do it by using glob.glob() On Thu, Feb 26, 2009 at 4:46 PM, Steve Holden st...@holdenweb.com wrote: lameck kassana wrote: i am trying to write script which will count files for remote directory which having certain pattern. Please refrain from repeating questions. When you posted this a reply had already been made to your original posting. Remember, this isn't a paid help desk ... regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
On Feb 26, 6:10 pm, Tim Wintle tim.win...@teamrubber.com wrote: On Thu, 2009-02-26 at 04:55 -0800, venutaurus...@gmail.com wrote: Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). I don't know how windows deals with this part In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. standard daemon behavior I'd have thought This shows how you would do it (with lots of comments). Assume this should work on Windows. http://code.activestate.com/recipes/278731/ note the two child threads to prevent zombies - may not be needed on windows but good to use them anyway. Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. Thank you, venu -- http://mail.python.org/mailman/listinfo/python-list
Re: class property not working in python 2.5.1
On Feb 26, 9:56 am, Christian Heimes li...@cheimes.de wrote: Dan Barbus schrieb: Hi, I have a problem with setting a property to a class instance, in python 2.5.1. The property is defined through get and set methods, but when I set it, the setter doesn't get called. Instead, I believe the property in the instance gets replaced with a new object (string). I have the following code (stripped down from my program): class Model(): def __init__(self, title = ''): self._views, self._title = [], None self.setTitle(title) *snip* Properties don't work correctly on old style classes. You have to subclass from object in order to get a new style class. class Model(object): pass Christian Thanks, that solved the problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: Queries
On Feb 26, 7:51 am, Steve Holden st...@holdenweb.com wrote: Murray wrote: [top-posting corrected] -Original Message- From: Gary Herron [mailto:gher...@islandtraining.com] Sent: Thursday, February 26, 2009 1:46 AM To: mmcclaf; python-l...@python.org Subject: Re: Queries mmcclaf wrote: I have to make some queries for 4 tables I have. The following relations are: Classes(class, type, country, numGuns, bore, displacement) Ships (name, class, launched) Battles (name, date) Outcomes (ship, battle, result) The three queries I'm stuck on are the following: 1. Find the classes that have only one ship as a member of that class (not all ships are listed in the Ship table) Investigate a GROUP BY solution that selects groups having a count of 1. 2. Find the countries that had both battleships and battlecruisers (those fall under type in Classes) Look at EXISTS for one possible solutions. 3. Find those ships that lived to fight another day; they were damaged in one battle, but later fought in another. From your model description I don't even see where the position and attitude of each ship is stored, so I don't think I can give you any help at all with this one. The best way for me to understand would be relational algebra for each of the statements. Sounds like a homework assignment. Good luck with it. It is, however I was able to get the first 8 done, I am struggling with these 3 particular ones. I have to make an SQL file based off of it, so this seems to be a blockage in my works. Good luck with the homework. Remember to acknowledge the help you've had from this list (particularly your earlier issues: here you just have hints). regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ Yeppers always do. And thanks! As for the number 3 one... to know if they were damaged would appear in Result of the Outcomes table -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
On Feb 26, 7:00 pm, venutaurus...@gmail.com venutaurus...@gmail.com wrote: On Feb 26, 6:10 pm, Tim Wintle tim.win...@teamrubber.com wrote: On Thu, 2009-02-26 at 04:55 -0800, venutaurus...@gmail.com wrote: Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). I don't know how windows deals with this part In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. standard daemon behavior I'd have thought This shows how you would do it (with lots of comments). Assume this should work on Windows. http://code.activestate.com/recipes/278731/ note the two child threads to prevent zombies - may not be needed on windows but good to use them anyway. Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. Thank you, venu Adding to the above.. I've to do it in Windows platform and what I can see from the illustration (/dev/null) etc. they are for Unix Environment. Thank you, Venu -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with glob in remote directory or os.walk in remote directory in windos
lameck kassana wrote: ok my original question is how can count the files of ceratin pattern(eg *.txt) in remote directory .It seems use of glob.glob() for remote directory is not working .Example I want to count the files in following shared folder \\192.168.0.45\files file://192.168.0.45/files how can do it by using glob.glob() glob.glob(r\\houseboy\sholden\*.txt) ['houseboy\\sholden\\pgin1.txt', 'houseboy\\sholden\\POSTGRES.TXT', 'houseboy\\sholden\\gatesquote.txt', 'houseboy\\sholden\\UbuntuWhatNow.txt', 'houseboy\\sholden\\patch001.txt'] glob.glob(r\\192.168.2.200\sholden\*.txt) ['192.168.2.200\\sholden\\pgin1.txt', '192.168.2.200\\sholden\\POSTGRES.TXT', '192.168.2.200\\sholden\\gatesquote.txt', '192.168.2.200\\sholden\\UbuntuWhatNow.txt', '192.168.2.200\\sholden\\patch001.txt'] Working just fine for me. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
2009/2/26 venutaurus...@gmail.com venutaurus...@gmail.com On Feb 26, 7:00 pm, venutaurus...@gmail.com venutaurus...@gmail.com wrote: On Feb 26, 6:10 pm, Tim Wintle tim.win...@teamrubber.com wrote: On Thu, 2009-02-26 at 04:55 -0800, venutaurus...@gmail.com wrote: Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). I don't know how windows deals with this part In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. standard daemon behavior I'd have thought This shows how you would do it (with lots of comments). Assume this should work on Windows. http://code.activestate.com/recipes/278731/ note the two child threads to prevent zombies - may not be needed on windows but good to use them anyway. Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. Thank you, venu Adding to the above.. I've to do it in Windows platform and what I can see from the illustration (/dev/null) etc. they are for Unix Environment. For making a script executable try this link http://www.python.org/doc/faq/windows/#how-do-i-make-python-scripts-executable -- - Bret -- http://mail.python.org/mailman/listinfo/python-list
Re: thread safe to lock on key, val pairs on a dict instead of entire dict?
En Thu, 26 Feb 2009 00:39:30 -0200, birdsong david.birds...@gmail.com escribió: Dictionaries just store references to objects, right? So is it thread safe to lock a specific key/val pair on a dictionary and modify its val and release the lock? example snippet: # assuming d_lock was initialized long ago in a thread-safe manner d_lock.acquire() d = {} d[1] = (threading.Lock(), []) d_lock.release() # test key level locking for key, data in d.items(): row_lock, rows = data row_lock.acquire() rows.append(1) row_lock.release() Of course, I'll have to lock the entire dict when adding keys that dont exist, but further calls and acquire the key specific lock before doing any write operations. (All of this applies to CPython only) Subscript assignment is a single opcode. If the key's __hash__ and __eq__/__cmp__ don't call any Python code, nor does the destructor of the previous dict entry, then the operation is atomic (no other thread may be executed due to the GIL). list.append is atomic. Note that the *append* operation is atomic (or the dict assignment), not the whole statement: L.append(some[values]+to**be.computed(L)) Evaluating the argument to append isn't atomic at all. References: http://effbot.org/pyfaq/what-kinds-of-global-value-mutation-are-thread-safe.htm http://effbot.org/zone/thread-synchronization.htm http://coreygoldberg.blogspot.com/2008/09/python-thread-synchronization-and.html -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: memory recycling/garbage collecting problem
On Feb 16, 11:21 pm, Yuanxin Xi xi11w...@gmail.com wrote: Could anyone please explain why this happens? It seems some memory are not freed. There is a bug in versions of Python prior to 2.5 where memory really isn't released back to the OS. Python 2.5 contains a new object allocator that is able to return memory to the operating system that fixes this issue. Here's an explanation: http://evanjones.ca/python-memory-part3.html What version of Python are you using? I have a machine running several long-running processes, each of which occasionally spike up to 500M memory usage, although normally they only require about 25M. Prior to 2.5, those processes never released that memory back to the OS and I would need to periodically restart them. With 2.5, this is no longer a problem. I don't always see memory usage drop back down immediately but the OS does recover the memory eventually. Make sure you use 2.5 if this is an issue for you. --David -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
On Thu, 2009-02-26 at 06:00 -0800, venutaurus...@gmail.com wrote: Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. That would be a simple way of doing it :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: memory recycling/garbage collecting problem
Tim Peters showed a way to demonstrate the fix in http://mail.python.org/pipermail/python-dev/2006-March/061991.html For simpler fun, run this silly little program, and look at memory consumption at the prompts: x = [] for i in xrange(100): x.append([]) raw_input(full ) del x[:] raw_input(empty ) For example, in a release build on WinXP, VM size is about 48MB at the full prompt, and drops to 3MB at the empty prompt. In the trunk (without this patch), VM size falls relatively little from what it is at the full prompt (the contiguous vector holding a million PyObject* pointers is freed, but the obmalloc arenas holding a million+1 list objects are never freed). For more info about the patch, see Evan's slides from _last_ year's PyCon: http://evanjones.ca/memory-allocator.pdf I'm not sure what deleting a slice accomplishes (del x[:]); the behavior is the same whether I do del x or del x[:]. Any ideas? --David -- http://mail.python.org/mailman/listinfo/python-list
RE: XML Parsing
You flatter me sir (or madam? can't tell from your name...), but I wouldn't presume to so lofty a title among this crowd. I'd save that for the likes of Alan Gauld and Kent Johnson, who are much more prolific and informative contributors to this list than I. -- Paul -Original Message- From: hrishy [mailto:hris...@yahoo.co.uk] Sent: Wednesday, February 25, 2009 11:36 PM To: python-list@python.org; Paul McGuire Subject: Re: XML Parsing Ha the guru himself responding :-) --- On Wed, 25/2/09, Paul McGuire pt...@austin.rr.com wrote: From: Paul McGuire pt...@austin.rr.com Subject: Re: XML Parsing To: python-list@python.org Date: Wednesday, 25 February, 2009, 2:04 PM On Feb 25, 1:17 am, hrishy hris...@yahoo.co.uk wrote: Hi Something like this snip solution using ElementTree Note i am not a python programmer just a enthusiast and i was curious why people on the list didnt suggest a code like above You just beat the rest of us to it - good example of ElementTree for parsing XML (and I Iearned the '//' shortcut for one or more intervening tag levels). To the OP: if you are parsing XML, I would look hard at the modules (esp. ElementTree) that are written explicitly for XML, before considering using regular expressions. There are just too many potential surprises when trying to match XML tags - presence/absence/ order of attributes, namespaces, whitespace inside tags, to name a few. -- Paul -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
OT: handling multiple software repositories
Hi, this is a bit off topic. In our company we have several SVN repositories. According to [1] Trac by default only handles one repository. Of course Trac links like changeset [1234] would not work anymore. You would need [repro/1234] or something like this. I think many (python) programmer have several repositories and need to handle this somehow. How do you do this? I want a common wiki and a common bug tracker. It should be possible to attach a bug to several repositories. Up to now I don't use Trac, if someone has an alternative, please tell me! Thomas [1] http://trac.edgewall.org/wiki/MultipleRepositorySupport -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettli (*) thomas-guettler + de -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
En Thu, 26 Feb 2009 11:00:30 -0200, Boris Borcic bbor...@gmail.com escribió: Chris Rebert wrote: On Thu, Feb 26, 2009 at 3:05 AM, Clarendon jine...@hotmail.com wrote: ... L=['a', 'b', 'c', 'a'] I want to delete all 'a's from the list. But if L.remove('a') only deletes the first 'a'. How do you delete all 'a's? There are several ways. I'd go with a list comprehension: and for a couple other ways while 'a' in L : L.remove('a') This is probably the worst way, takes cuadratic time (and two searches per loop). L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ so a more generic answer would be: from functools import partial from operator import ne L = filter(partial(ne, 'a'), L) Of course, this is only relevant if L is large and there are many duplicates. In other cases I'd stick with the list comprehension as posted by Chris Rebert. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: PyCrypto AES MODE_CBC - How to?
On 2009-02-25 13:25, Helmut Jarausch wrote: Helmut Jarausch wrote: Hi, I've just tried to write a simple example using PyCrypto's AES (CBC mode) #!/usr/bin/python from Crypto.Cipher import AES PWD='abcdefghijklmnop' Initial16bytes='0123456789ABCDEF' crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes) # crypt = AES.new(PWD, AES.MODE_ECB) txt = 'ea523a664dabaa4476d31226a1e3bab0' c = crypt.encrypt(txt) txt_plain=crypt.decrypt(c) print txt_plain Unfortunately, txt_plain differs from txt - why? (Using MODE_ECB does work however) I just discovered that the following variant seems to work crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes) c = crypt.encrypt(txt) crypt = AES.new(PWD, AES.MODE_CBC,Initial16bytes) # re-initialize txt_plain=crypt.decrypt(c) So, the crypt object seems to keep some state. I haven't seen this mentioned in the documentation. In CBC mode, all previous encryptions affect the next one, since the blocks are chained: http://en.wikipedia.org/wiki/Cipher_block_chaining#Cipher-block_chaining_.28CBC.29 In ECB mode, they are not, but then it doesn't provide much security unless you change the key frequently: http://en.wikipedia.org/wiki/Cipher_block_chaining#Electronic_codebook_.28ECB.29 (the wiki pages provide some nice graphics to see what's going on and why CBC is better than ECB) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Feb 26 2009) Python/Zope Consulting and Support ...http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/ ::: Try our new mxODBC.Connect Python Database Interface for free ! eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48 D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg Registered at Amtsgericht Duesseldorf: HRB 46611 http://www.egenix.com/company/contact/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
On Feb 26, 7:47 pm, Tim Wintle tim.win...@teamrubber.com wrote: On Thu, 2009-02-26 at 06:00 -0800, venutaurus...@gmail.com wrote: Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. That would be a simple way of doing it :-) When I run that script on Windows it fails throwing the below error: AttributeError: 'module' object has no attribute 'fork' Please help me out.. -- http://mail.python.org/mailman/listinfo/python-list
Re: The Python's regex different from Perl's , I want know what's the different?
The regular expression syntax is basically exactly the same. The main difference is that a regex can't just be written into a statement as part of the syntax; you have to create a regular expression object and use its methods. So, instead of: new_thing =~ s/pattern/replacement/ it's: myPattern = re.compile(r'pattern') new_thing = myPattern.sub(replacement, input_string) or: new_thing = re.sub(pattern, replacement, input_string) Also, backreferences are \1 instead of $1, and you have to escape the backslash or use a raw string for the backslash. I've found this page to be pretty helpful: http://www.regular-expressions.info/python.html Shawn -- http://mail.python.org/mailman/listinfo/python-list
RE: Delete all items in the list
L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ so a more generic answer would be: from functools import partial from operator import ne L = filter(partial(ne, 'a'), L) And don't forget this traditional solution: L=['a', 'b', 'c', 'a'] filter(lambda arg: arg != 'a', L) ['b', 'c'] -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.11850 http://www.pctools.com/en/spyware-doctor-antivirus/ -- http://mail.python.org/mailman/listinfo/python-list
functools.partial (was: Delete all items in the list)
from functools import partial from operator import ne L = filter(partial(ne, 'a'), L) I learned about functools.partial only recently (on this list). functools is implemented in C, not Python, so I couldn't answer this question for myself: Is functools.partial completely equivalent to a manually-coded closure? Help, please. -John E-mail message checked by Spyware Doctor (6.0.0.386) Database version: 5.11850 http://www.pctools.com/en/spyware-doctor-antivirus/ -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: handling multiple software repositories
Thomas Guettler wrote: Hi, this is a bit off topic. In our company we have several SVN repositories. According to [1] Trac by default only handles one repository. Of course Trac links like changeset [1234] would not work anymore. You would need [repro/1234] or something like this. I think many (python) programmer have several repositories and need to handle this somehow. How do you do this? I want a common wiki and a common bug tracker. It should be possible to attach a bug to several repositories. Up to now I don't use Trac, if someone has an alternative, please tell me! Have a look at DrProject[1], a Trac-alike which handles multiple projects. (Not used it myself). TJG [1] https://www.drproject.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
jkv wrote: Hi, Are there any way to change the EOL character for sys.stdin.readline() and raw_input()? My problem is that i cannot rely on that the clients connection to my application will end all lines with \n or \r\n. Sometimes they will use \r\000 as EOL. Example from my code: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Any suggestions? Could you insert some kind of filter into the input stream? The filter would replace '\r\000' with '\r\n' (or just '\000' with '\n'). old_stdin = sys.stdin try: sys.stdin = MyFilter(sys.stdin) ... finally: sys.stdin = old_stdin I don't know how many methods you would need to provide. -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: handling multiple software repositories
On Thursday 26 February 2009 11:10:15 am Tim Golden wrote: Have a look at DrProject[1], a Trac-alike which handles multiple projects. (Not used it myself). [1] https://www.drproject.org/ I've used DrProject a bit (my gf is one of the developers). If you like trac, chances are that you will love DrProject. It's quite good (and easy to install, unlike gforge), but the developers are moving on to a version 2.0 that you can find around here: http://basieproject.org/ (yeah, the website is ugly). If you like trac, take a look at DrProject and keep an eye on Basie. -- Luis Zarrabeitia (aka Kyrie) Fac. de Matemática y Computación, UH. http://profesores.matcom.uh.cu/~kyrie -- http://mail.python.org/mailman/listinfo/python-list
Re: memory recycling/garbage collecting problem
David Niergarth wrote: Tim Peters showed a way to demonstrate the fix in http://mail.python.org/pipermail/python-dev/2006-March/061991.html For simpler fun, run this silly little program, and look at memory consumption at the prompts: x = [] for i in xrange(100): x.append([]) raw_input(full ) del x[:] raw_input(empty ) For example, in a release build on WinXP, VM size is about 48MB at the full prompt, and drops to 3MB at the empty prompt. In the trunk (without this patch), VM size falls relatively little from what it is at the full prompt (the contiguous vector holding a million PyObject* pointers is freed, but the obmalloc arenas holding a million+1 list objects are never freed). For more info about the patch, see Evan's slides from _last_ year's PyCon: http://evanjones.ca/memory-allocator.pdf I'm not sure what deleting a slice accomplishes (del x[:]); the behavior is the same whether I do del x or del x[:]. Any ideas? del x removes the name x from the current namespace, garbage collecting the object to which it referred. del x[:] leaves x referencing a cleared list. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
En Thu, 26 Feb 2009 13:32:27 -0200, venutaurus...@gmail.com venutaurus...@gmail.com escribió: On Feb 26, 7:47 pm, Tim Wintle tim.win...@teamrubber.com wrote: On Thu, 2009-02-26 at 06:00 -0800, venutaurus...@gmail.com wrote: Thanks for the reply, Being a newbie to python, I am finding it difficult to understand the logic even after thorough reading of comments. Is there any simpler way where I can just run a python script from the main script and exit without disturbing the second one(This will end based on some other constraints which can be handled). Or can some one throw some light on where should I run the function os.system( python script2.py) from the main one. That would be a simple way of doing it :-) When I run that script on Windows it fails throwing the below error: AttributeError: 'module' object has no attribute 'fork' Please help me out.. Unless you have any special needs, just stick to os.system(...) -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
embedding Python in a shared library
I have embedded Python in a shared library. This works fine in Windows (dll), but I get the following error is Ubuntu when I try to load modules: /usr/lib/python2.5/lib-dynload/time.so: error: symbol lookup error: undefined symbol: PyExc_ValueError I found many postings on this issue on the internet, but I was not able to find a solution that worked. I tried to load libpython2.5.so.1 into my program using dlopen, but that crashed the program for some reason. I tried building my library using the libpython2.5.a, but the same error was there. I am sure someone has a solution to this, since it seems like a general issue. -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposed implementation for an Ordered Dictionary
Raymond Hettinger python at rcn.com writes: Here's a proposed implementation for Py2.7 and Py3.1: http://code.activestate.com/recipes/576669/ Would like you guys to kick the tires, exercise it a bit, and let me know what you think. The recipe runs under 2.6 and 3.0 without modification so it should be easy to play with. Why not just inherit from collections.MutableMapping? -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
jkv wrote: Hi, Are there any way to change the EOL character for sys.stdin.readline() and raw_input()? My problem is that i cannot rely on that the clients connection to my application will end all lines with \n or \r\n. Sometimes they will use \r\000 as EOL. Example from my code: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Can you put that in a separate thread with timeout? -- http://mail.python.org/mailman/listinfo/python-list
Re: Run a python script as an exe and run a new process from it
Try this as an outline: script1.py from subprocess import Popen if __name__ == '__main__': scriptname = script2.py Popen(python %s % scriptname, shell=True) print I'm done script2.py from time import sleep if __name__ == '__main__': while (True): print waiting.. sleep(2) ### If you install python using the Windows Installer (.exe), it should set up .py files to be opened by python when double-clicking on them. Alternatively, you can right-click on your .py file and go to Open With... then choose program, then click Browse..., you can Browse to the python executable, so Explorer will use it to open .py files when you double-click on them. As someone else mentioned there is also a py2exe program. Google it. 2009/2/26 venutaurus...@gmail.com venutaurus...@gmail.com: Hello all, I've a strange requirement where I need to run a python script just as we run an exe (by double clicking through windows explorer or by typing the script name at command prompt). In that process I should be able to execute another python script in such a way that, the second script should continue running but the main one should terminate without effecting the second one. My requirement may be little confusing so please get back if you didn't understand what I meant above. Thank you, Venu. -- http://mail.python.org/mailman/listinfo/python-list -- Ray, when someone asks you if you're a god, you say YES! -- http://mail.python.org/mailman/listinfo/python-list
What functions, other than sleep(), can be interrupted by Ctrl-C?
Greetings, I'm looking for a function in the standard library or pywin32 package that will block until a certain condition is met or it is interrupted by Ctrl-C. For example, time.sleep() would have been perfect for my needs if thread.interrupt_main() could interrupt the call from another thread in the same way that Ctrl-C does. Unfortunately, that is not the case. Another thing I tried was creating a pipe with os.pipe() and issuing a read call on it. The event to exit was a single byte written to the other end of the pipe, but Ctrl-C could not interrupt the read call. The threading.Event class does not work for me, because it uses short sleep intervals for timed waits. I need the reaction to be as close to instant as possible, something like this will not do: while not some condition is met: sleep(0.01) I actually replaced threading.Event with my own version that uses native Windows events, and waits on those also cannot be interrupted by Ctrl-C. I'm trying to achieve the same effect as that while loop and I don't care what the condition to exit is, but the loop needs to exit as soon as the condition is met without waiting for up to X additional milliseconds. Any ideas? - Max -- http://mail.python.org/mailman/listinfo/python-list
Re: memory recycling/garbage collecting problem
Steve Holden wrote: David Niergarth wrote: Tim Peters showed a way to demonstrate the fix in http://mail.python.org/pipermail/python-dev/2006-March/061991.html For simpler fun, run this silly little program, and look at memory consumption at the prompts: x = [] for i in xrange(100): x.append([]) raw_input(full ) del x[:] raw_input(empty ) For example, in a release build on WinXP, VM size is about 48MB at the full prompt, and drops to 3MB at the empty prompt. In the trunk (without this patch), VM size falls relatively little from what it is at the full prompt (the contiguous vector holding a million PyObject* pointers is freed, but the obmalloc arenas holding a million+1 list objects are never freed). For more info about the patch, see Evan's slides from _last_ year's PyCon: http://evanjones.ca/memory-allocator.pdf I'm not sure what deleting a slice accomplishes (del x[:]); the behavior is the same whether I do del x or del x[:]. Any ideas? del x removes the name x from the current namespace, garbage collecting the object to which it referred. If there is another reference to the list, which there well might be in an actual application with memory problems, then 'del x' only disassociates the name but the object and its large contents are not gc'ed. del x[:] leaves x referencing a cleared list. which is guaranteed to be cleared, regardless of other refs. Terry -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Gabriel Genellina wrote: L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ In Py3, all classes inherit .__ne__ from 'object'. -- http://mail.python.org/mailman/listinfo/python-list
Re: memory recycling/garbage collecting problem
Terry Reedy wrote: Steve Holden wrote: David Niergarth wrote: [...] I'm not sure what deleting a slice accomplishes (del x[:]); the behavior is the same whether I do del x or del x[:]. Any ideas? del x removes the name x from the current namespace, garbage collecting the object to which it referred. If there is another reference to the list, which there well might be in an actual application with memory problems, then 'del x' only disassociates the name but the object and its large contents are not gc'ed. del x[:] leaves x referencing a cleared list. which is guaranteed to be cleared, regardless of other refs. Nice catch, Terry! You correctly spotted I was assuming that d represented the only reference to the list. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Terry Reedy wrote: Gabriel Genellina wrote: L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ In Py3, all classes inherit .__ne__ from 'object'. Isn't that inherited method just an identity comparison? However in this particular case the main point is that str *does* implement __ne__. So as long as that built-in method doesn't call the other operand's __ne__ there should be no problem. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
What is it about with statements that makes you want to so frequently embed them in try/except statements? Skip -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
Terry Reedy wrote: jkv wrote: Hi, Are there any way to change the EOL character for sys.stdin.readline() and raw_input()? My problem is that i cannot rely on that the clients connection to my application will end all lines with \n or \r\n. Sometimes they will use \r\000 as EOL. Example from my code: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Can you put that in a separate thread with timeout? I guess i could - wouldn't know how thou, just getting started in python. Is there any way i can read stdin character by character? In C i would loop over 'ch = getch();' and in each iteration push the character to the password variable unless ch equals to \r. Anyway to do something like this in python? Regards, jkv -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
On Thu, 26 Feb 2009 11:46:24 +0100, jkv j...@unixcluster.dk wrote: Hi, Are there any way to change the EOL character for sys.stdin.readline() and raw_input()? My problem is that i cannot rely on that the clients connection to my application will end all lines with \n or \r\n. Sometimes they will use \r\000 as EOL. Example from my code: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Any suggestions? It sounds like you might want to use a real implementation of the telnet protocol in your application. Such an implementation would deal with the fact that telnet encodes \r as \r\0 for you. Twisted includes a telnet implementation, as well as facilities for reading stdin asynchronously (which will let you avoid indefinite hangs). Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
How to parse form in client side?
Hi Folks, cgi module can easily acquire the all fields of data input from client side, through a form. Then, a simple line of code: form_dict = cgi.FieldStorage() grabs all data into a dictionary form_dict. The rest becomes a piece of cake by querying the form_dict. Nice! However, it is done in the server side. Now I want to do the same in the client side. first of all, I get the source code of a HTML form by using urllib, from server, with code below: html_source = urllib.urlopen(URL).read() Then, I need to parse this html_source file, at client side. Is there a module to handle this? Can somebody help? Thanks! Muddy Coder -- http://mail.python.org/mailman/listinfo/python-list
Re: How to parse form in client side?
On Thu, Feb 26, 2009 at 12:19 PM, Muddy Coder cosmo_gene...@yahoo.com wrote: Hi Folks, cgi module can easily acquire the all fields of data input from client side, through a form. Then, a simple line of code: form_dict = cgi.FieldStorage() grabs all data into a dictionary form_dict. The rest becomes a piece of cake by querying the form_dict. Nice! However, it is done in the server side. Now I want to do the same in the client side. first of all, I get the source code of a HTML form by using urllib, from server, with code below: html_source = urllib.urlopen(URL).read() Then, I need to parse this html_source file, at client side. Is there a module to handle this? Can somebody help? Thanks! http://wwwsearch.sourceforge.net/ClientForm/ Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Steve Holden wrote: Terry Reedy wrote: Gabriel Genellina wrote: L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ In Py3, all classes inherit .__ne__ from 'object'. Isn't that inherited method just an identity comparison? Yes. And so is, by default, the snipped slower proposed alternative of lambda x: a!= x. However in this particular case the main point is that str *does* implement __ne__. So as long as that built-in method doesn't call the other operand's __ne__ there should be no problem. regards Steve -- http://mail.python.org/mailman/listinfo/python-list
How can I know when a sub-process is effectively initialized?
Hi, I'm working on a Python module called psutil [1] for reading process information in a cross-platform way. I'm having a problem with psutil test suite. Almost all the test cases we implemented so far look like this: def test_foo(self): test_process = subprocess.Popen(sys.executable, stdout=DEVNULL, stderr=DEVNULL) time.sleep(0.1) # XXX: provisional, give some time the sub process to initialize p = psutil.Process(test_process.pid) # start test here ... As you can see we put a time.sleep(0.1) call after subprocess.Popen() as a provisional workaround to let the sub process initialize properly. We're searching for some kind of way to know when the child process is properly initialized so that we can actually start testing cases without worries. Does someone has an idea how could we do that? [1] http://code.google.com/p/psutil/ Thanks a lot in advance, --- Giampaolo [1] http://code.google.com/p/pyftpdlib -- http://mail.python.org/mailman/listinfo/python-list
Re: Delete all items in the list
Terry Reedy wrote: Steve Holden wrote: Terry Reedy wrote: Gabriel Genellina wrote: L = filter('a'.__ne__,L) And this is probably the fastest. But not all types define __ne__ In Py3, all classes inherit .__ne__ from 'object'. Isn't that inherited method just an identity comparison? Yes. And so is, by default, the snipped slower proposed alternative of lambda x: a!= x. But shouldn't that have been lambda x: 'a' != x, which would have invoked the string (3.0, Unicode) comparison method? regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Extending Python Questions .....
Ben bnsili...@gmail.com wrote: On Feb 24, 11:31?am, Nick Craig-Wood n...@craig-wood.com wrote: So do you want to embed python into your code? I'm still not clear what you are trying to achieve with python, though I have a better idea what SLAG is now! Actually no, I want to EXTEND python using the lower levels of S-lang screen modules. Ah! My Modules are written in C and are a frame work for building pull- down menus and data entry screens. Very nice for writing business applications. Think along the lines of FoxPro and/or the Screen section in Cobol and you have a pretty good idea of what i have done. You've got several choices. 1) Write a python module in C which interfaces with your C code. This is the traditional way, but it is quite time consuming and easy to slip up with reference counting. You need to learn the python API to use this. Will require compiling by your users (not a big hurdle if you make a setup.py). 2) Write an interface with ctypes If your C code presents itself as a shared object (dll/so) then this is a really nice way of using it. You don't have to write any C code or worry about any reference counting - you do it all in python. You'll need to learn how ctypes maps python onto C, but as you know C well you won't find this difficult. ctypes is included with python now. 3) Use Cython Cython is a sort of hybrid C and python which can be used for interfacing with C code. This requires learning exactly what you can and can't do in Cython (it is almost python but not quite). This will require your users to install Cython and compile stuff. Again easy if you make a setup.py but more stuff to install. 4) Use Swig A solid approach, quicker than 1) but not as quick as 2) or 3) This will require your users to install swig and a compiler. ... My prefered approach is 2) ctypes at the moment. I have used all 4 of the above approaches in the past though! -- Nick Craig-Wood n...@craig-wood.com -- http://www.craig-wood.com/nick -- http://mail.python.org/mailman/listinfo/python-list
Re: What functions, other than sleep(), can be interrupted by Ctrl-C?
En Thu, 26 Feb 2009 16:17:42 -0200, Maxim Khitrov mkhit...@gmail.com escribió: I'm looking for a function in the standard library or pywin32 package that will block until a certain condition is met or it is interrupted by Ctrl-C. For example, time.sleep() would have been perfect for my needs if thread.interrupt_main() could interrupt the call from another thread in the same way that Ctrl-C does. Unfortunately, that is not the case. Another thing I tried was creating a pipe with os.pipe() and issuing a read call on it. The event to exit was a single byte written to the other end of the pipe, but Ctrl-C could not interrupt the read call. The threading.Event class does not work for me, because it uses short sleep intervals for timed waits. I need the reaction to be as close to instant as possible, something like this will not do: while not some condition is met: sleep(0.01) I actually replaced threading.Event with my own version that uses native Windows events, and waits on those also cannot be interrupted by Ctrl-C. I'm trying to achieve the same effect as that while loop and I don't care what the condition to exit is, but the loop needs to exit as soon as the condition is met without waiting for up to X additional milliseconds. Any ideas? You may try MsgWaitForMultipleObjects - send a message to the main thread from the other thread. An alertable wait (like SleepEx) plus QueueUserAPC should work, I presume, but I've never actually tried in Python. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I know when a sub-process is effectively initialized?
On Thu, 26 Feb 2009 12:27:26 -0800 (PST), Giampaolo Rodola' gne...@gmail.com wrote: Hi, I'm working on a Python module called psutil [1] for reading process information in a cross-platform way. I'm having a problem with psutil test suite. Almost all the test cases we implemented so far look like this: def test_foo(self): test_process = subprocess.Popen(sys.executable, stdout=DEVNULL, stderr=DEVNULL) time.sleep(0.1) # XXX: provisional, give some time the sub process to initialize p = psutil.Process(test_process.pid) # start test here ... As you can see we put a time.sleep(0.1) call after subprocess.Popen() as a provisional workaround to let the sub process initialize properly. We're searching for some kind of way to know when the child process is properly initialized so that we can actually start testing cases without worries. Does someone has an idea how could we do that? Hi Giampaolo, Properly initialized is application specific. Generally, what you want to do is have the child process tell you when it's ready. A common way to do this is for the child to write some bytes to a file descriptor the parent is monitoring when it is ready. The parent then just waits for those bytes. It looks like you entirely control the child in this case, so that should be possible here. Alternatively, maybe by properly initialized you mean something very specific about how subprocess.Popen creates processes. If so, can you elaborate a bit? Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
best way to parse a function-call-like string?
I have some strings that look like function calls, e.g. junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma') and I need to split them into the function name and list of parms, e.g. junkpkg.f1, [] junkpkg.f1, [] junkpkg.f1, ['aaa'] junkpkg.f1, ['aaa','bbb'] junkpkg.f1, ['aaa','bbb','ccc'] junkpkg.f1, ['aaa','with,comma'] What's the best way to do this? I would be interested in either of two approaches: - a real way which comprehensively - a quick-and-dirty way which handles most cases, so that I can get my coding partner running quickly while I do the real way. will the csv module do the right thing for the parm list? Many TIA!! Mark -- Mark Harrison Pixar Animation Studios -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
On Thu, 2009-02-26 at 21:29 +, m...@pixar.com wrote: I have some strings that look like function calls, e.g. junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma') and I need to split them into the function name and list of parms, e.g. junkpkg.f1, [] junkpkg.f1, [] junkpkg.f1, ['aaa'] junkpkg.f1, ['aaa','bbb'] junkpkg.f1, ['aaa','bbb','ccc'] junkpkg.f1, ['aaa','with,comma'] quick and dirty for s in string_list: if ( in s and s[-1] == ): parts = s.split(() fn, args = s[0],s[1][:-1].split(,) Tim Wintle -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
On Thu, 2009-02-26 at 21:39 +, Tim Wintle wrote: On Thu, 2009-02-26 at 21:29 +, m...@pixar.com wrote: junkpkg.f1, ['aaa','with,comma'] oops - missed this one, ignore my last reply. -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
On 2009-02-26 15:29, m...@pixar.com wrote: I have some strings that look like function calls, e.g. junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma') and I need to split them into the function name and list of parms, e.g. junkpkg.f1, [] junkpkg.f1, [] junkpkg.f1, ['aaa'] junkpkg.f1, ['aaa','bbb'] junkpkg.f1, ['aaa','bbb','ccc'] junkpkg.f1, ['aaa','with,comma'] What's the best way to do this? I would be interested in either of two approaches: - a real way which comprehensively - a quick-and-dirty way which handles most cases, so that I can get my coding partner running quickly while I do the real way. will the csv module do the right thing for the parm list? Use the compiler module to generate an AST and walk it. That's the real way. For me, that would also be the quick way because I am familiar with the API, but it may take you a little bit of time to get used to it. -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposed implementation for an Ordered Dictionary
Raymond Hettinger pyt...@rcn.com writes: Here's a proposed implementation for Py2.7 and Py3.1: http://code.activestate.com/recipes/576669/ Several methods like __contains__() and __getitem__() are not overridden, so their performance is just as fast as a regular dictionary. Methods like __setitem__ and __delitem__ are overridden but have a fast path depending on whether or not the key already exists. It seems that __delitem__ of an existing key is O(n), whereas it's amortized constant time for dicts. (__setitem__ is constant time for both.) Is there a way to avoid this? If not, it should probably be documented, since it differs from dict. -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
Robert Kern robert.k...@gmail.com wrote: On 2009-02-26 15:29, m...@pixar.com wrote: Use the compiler module to generate an AST and walk it. That's the real way. For me, that would also be the quick way because I am familiar with the API, but it may take you a little bit of time to get used to it. ah, that's just what I was hoping for... in the meantime, here's my quick and dirty: import csv def fakeparse(s): print === argl=[] ix=s.find('(') if ix == -1: func=s argl=[] else: func=s[0:ix] argstr=s[ix+1:] argstr=argstr[:argstr.rfind(')')] print argstr for argl in csv.reader([argstr], quotechar='): pass print s print func print argl return func,argl print fakeparse(junkpkg.f1) print fakeparse(junkpkg.f1()) print fakeparse(junkpkg.f1('aaa')) print fakeparse(junkpkg.f1('aaa','bbb')) print fakeparse(junkpkg.f1('aaa','bbb','ccc')) print fakeparse(junkpkg.f1('aaa','xx,yy')) -- Mark Harrison Pixar Animation Studios -- http://mail.python.org/mailman/listinfo/python-list
Can someone explain this behavior to me?
I have one module called foo.py - class Foo: foo = None def get_foo(): return Foo.foo if __name__ == __main__: import bar Foo.foo = foo bar.go() - And another one called bar.py - import foo def go(): assert foo.get_foo() == foo -- When I run foo.py, the assertion in bar.py fails. Why? -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
On Feb 27, 8:29 am, m...@pixar.com wrote: I have some strings that look like function calls, e.g. junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma') Examples are better than no examples, but a grammar would be a great help. What about junkpkg.f1('aaa','with)parenthesis') junkpkg.f1('aaa','with''ONEapostrophe') and I need to split them into the function name and list of parms, e.g. junkpkg.f1, [] junkpkg.f1, [] junkpkg.f1, ['aaa'] junkpkg.f1, ['aaa','bbb'] junkpkg.f1, ['aaa','bbb','ccc'] junkpkg.f1, ['aaa','with,comma'] What's the best way to do this? I would be interested in either of two approaches: - a real way which comprehensively - a quick-and-dirty way which handles most cases, so that I can get my coding partner running quickly while I do the real way. will the csv module do the right thing for the parm list? It should, for most cases. Any reason you can't try it out for yourself? It appears to work in the sense that if you have isolated a string containing the parameters, the csv module can be used to parse it: | Python 2.6.1 (r261:67517, Dec 4 2008, 16:51:00) [MSC v.1500 32 bit (Intel)] on win32 | Type help, copyright, credits or license for more information. | import csv, cStringIO | argstring = 'aaa','with,comma' | csvargs = lambda s: list(csv.reader(cStringIO.StringIO(s), quotechar=')) | csvargs(argstring) | [['aaa', 'with,comma']] | csvargs('aaa','with''ONEapostrophe') | [['aaa', with'ONEapostrophe]] | HTH John -- http://mail.python.org/mailman/listinfo/python-list
Re: Can someone explain this behavior to me?
On Thu, Feb 26, 2009 at 1:48 PM, Jesse Aldridge jessealdri...@gmail.com wrote: I have one module called foo.py - class Foo: foo = None def get_foo(): return Foo.foo if __name__ == __main__: import bar Foo.foo = foo bar.go() - And another one called bar.py - import foo def go(): assert foo.get_foo() == foo -- When I run foo.py, the assertion in bar.py fails. Why? Not sure, but circular imports are *evil* anyway, so I'd suggest you just rewrite the code to avoid doing any circular imports in the first place. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Can someone explain this behavior to me?
I'm pretty sure that Foo is getting replaced once you import Foo, why not pass the Foo() object to bar's go? I'm sure there are other ways, but yes, circular imports are indeed evil. On Thu, Feb 26, 2009 at 5:13 PM, Chris Rebert c...@rebertia.com wrote: On Thu, Feb 26, 2009 at 1:48 PM, Jesse Aldridge jessealdri...@gmail.com wrote: I have one module called foo.py - class Foo: foo = None def get_foo(): return Foo.foo if __name__ == __main__: import bar Foo.foo = foo bar.go() - And another one called bar.py - import foo def go(): assert foo.get_foo() == foo -- When I run foo.py, the assertion in bar.py fails. Why? Not sure, but circular imports are *evil* anyway, so I'd suggest you just rewrite the code to avoid doing any circular imports in the first place. Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
PythonWin -vs- Idle
Hello, Python newbie here. I am working with Python and geoprocessing in ArcGIS. I'm taking a training course online and the exercise I'm working on makes mention of using PythonWin instead of Idle. I am using version 2.5 and have not been able to locate PythonWin. The download just includes Idle for the environment. Can anyone point me in the right direction to download the PythonWin piece of this puzzle? Thank you _ It’s the same Hotmail®. If by “same” you mean up to 70% faster. http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_HM_AE_Same_022009-- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
Jean-Paul Calderone wrote: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Any suggestions? It sounds like you might want to use a real implementation of the telnet protocol in your application. Such an implementation would deal with the fact that telnet encodes \r as \r\0 for you. Twisted includes a telnet implementation, as well as facilities for reading stdin asynchronously (which will let you avoid indefinite hangs). I am running this script inside a wrapper (honeyd), so i cant really use twisted since i don't have raw network access - i only got stdout, stdin and stderr to play with. But i found a solution, posting it here for the archives: password = while 1: c = sys.stdin.read(1) if c == '\r' or c == '\n': break password = password + c Regards, jkv -- http://mail.python.org/mailman/listinfo/python-list
Re: Battleship style game
On Feb 26, 3:50 am, Shawn Milochik sh...@milochik.com wrote: On Wed, Feb 25, 2009 at 11:38 AM, Marco Mariani ma...@sferacarta.com wrote: Yes it's in Python alright, but it's not Pythonese yet. You could try avoiding the getter/setter stuff, and camelCase method naming, things like that, for a start. -- http://mail.python.org/mailman/listinfo/python-list What do you mean avoiding the getter/setter stuff? If I understand correctly, you're saying to directly access the attributes, which I specifically want to avoid because I may want to enforce some rules (such as not changing a ship length after it's created). I just want to add the note that since you're planning to move the code to Java, although you should avoid getter and setter in python (since it's redundant because of property), you should use getter and setter in the Java version because it is the best practice in Java. The camel-case thing I get -- I use that and this_type quite a bit, probably because of the inconsistency of the languages I use regularly, and standards at work and conventions in my hobby programming. -- http://mail.python.org/mailman/listinfo/python-list
Re: PythonWin -vs- Idle
On Thu, Feb 26, 2009 at 2:23 PM, Gary Schells gsche...@hotmail.com wrote: Hello, Python newbie here. I am working with Python and geoprocessing in ArcGIS. I'm taking a training course online and the exercise I'm working on makes mention of using PythonWin instead of Idle. I am using version 2.5 and have not been able to locate PythonWin. The download just includes Idle for the environment. Can anyone point me in the right direction to download the PythonWin piece of this puzzle? Seems to be included in http://sourceforge.net/projects/pywin32/ Cheers, Chris -- Follow the path of the Iguana... http://rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Can someone explain this behavior to me?
On Feb 27, 8:48 am, Jesse Aldridge jessealdri...@gmail.com wrote: I have one module called foo.py - class Foo: foo = None def get_foo(): return Foo.foo if __name__ == __main__: import bar Foo.foo = foo bar.go() - And another one called bar.py - import foo def go(): assert foo.get_foo() == foo -- When I run foo.py, the assertion in bar.py fails. Why? AFAICT from that convoluted mess, because there are two rabbit holes, __main__.Foo.foo and foo.Foo.foo, and you poked foo down the wrong one. In any case the other one is not the right one -- as you have already been advised, circular imports are evil. -- http://mail.python.org/mailman/listinfo/python-list
Re: EOL for sys.stdin.readline() and raw_input()
On Thu, 26 Feb 2009 23:32:13 +0100, jkv j...@unixcluster.dk wrote: Jean-Paul Calderone wrote: sys.stdout.write('Password: '); #IAC DO ECHO sys.stdout.write('\xFF\xFB\x01') password = raw_input() If the client sends 'qwerty\r\n' everything is fine and the password get stored in the variable. But sometimes, depending on how broken the telnet client are, the client will send 'qwerty\r\000' instead and then my application will hang at 'password = raw_input()' forever. Any suggestions? It sounds like you might want to use a real implementation of the telnet protocol in your application. Such an implementation would deal with the fact that telnet encodes \r as \r\0 for you. Twisted includes a telnet implementation, as well as facilities for reading stdin asynchronously (which will let you avoid indefinite hangs). I running this script inside a wrapper (honeyd), so i cant really use twisted since i dont have raw network access - i only got stdout, stdin and stderr to play with. But i found a solution, posting it here for the archives: password = while 1: c = sys.stdin.read(1) if c == '\r' or c == '\n': break password = password + c Glad you figured something out. Just for the record, you can use Twisted to write applications that interact with stdio, so the fact that you're using honeyd doesn't prevent you from using Twisted. Jean-Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Proposed implementation for an Ordered Dictionary
[Benjamin Peterson] Why not just inherit from collections.MutableMapping? It makes the recipe shorter to inherit some methods from dict. Also, subclassing from dict gives a speedup for __getitem__(), __len__(), and get(). -- http://mail.python.org/mailman/listinfo/python-list
Re: best way to parse a function-call-like string?
On Feb 26, 3:29 pm, m...@pixar.com wrote: I have some strings that look like function calls, e.g. junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma') and I need to split them into the function name and list of parms, e.g. Pyparsing will easily carve up these function declarations, and will give you some room for easy extension once your parsing job starts to grow to include other variants (like arguments other than quoted strings, for instance). Using the results names (name and args), then parsed fields are easy to get at after the parsing is done. -- Paul from pyparsing import Word, alphas, alphanums, delimitedList, \ Optional, Literal, Suppress, quotedString LPAR,RPAR = map(Suppress,()) ident = Word(alphas+_, alphanums+_) fnName = delimitedList(ident,.,combine=True) arg = quotedString fnCall = fnName(name) + Optional(LPAR + Optional(delimitedList(arg)) + RPAR, default=[])(args) tests = junkpkg.f1 junkpkg.f1() junkpkg.f1('aaa') junkpkg.f1('aaa','bbb') junkpkg.f1('aaa','bbb','ccc') junkpkg.f1('aaa','with,comma').splitlines() for t in tests: fn = fnCall.parseString(t) print fn.name for a in fn.args: print -,a print Prints: junkpkg.f1 junkpkg.f1 junkpkg.f1 - 'aaa' junkpkg.f1 - 'aaa' - 'bbb' junkpkg.f1 - 'aaa' - 'bbb' - 'ccc' junkpkg.f1 - 'aaa' - 'with,comma' -- http://mail.python.org/mailman/listinfo/python-list