class factory question
I am extending a parser and need to create many classes that are all subclassed from the same object (defined in an external library). When my module is loaded I need all the classes to be created with a particular name but the behavior is all the same. Currently I have a bunch of lines like this: class Vspace(Base.Command): pass class Boldpath(Base.Command): pass There are a bunch of lines like that. Is there a better way? Something like newclasses = ['Vspace', 'Boldpath', ... ] for name in newclasses: tmp = type(name, (Base.Command,) {}) tmp.__name__ = name Is there a more pythonic way? thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: class factory question
On Wednesday, June 26, 2013 9:39:12 AM UTC-4, Peter Otten wrote: > Tim wrote: > > I am extending a parser and need to create many classes that are all > > subclassed from the same object (defined in an external library). When my > > module is loaded I need all the classes to be created with a particular > > name but the behavior is all the same. Currently I have a bunch of lines > > like this: > > > > class Vspace(Base.Command): pass > > class Boldpath(Base.Command): pass > > > > There are a bunch of lines like that. > > Is there a better way? Something like > > > > newclasses = ['Vspace', 'Boldpath', ... ] > > for name in newclasses: > > tmp = type(name, (Base.Command,) {}) > > tmp.__name__ = name > > > > Is there a more pythonic way? > > What is your objection against that approach? > By the way, I don't think you need > > tmp.__name__ = name I am not completely understanding the type function I guess. Here is an example from the interpreter: In [1]: class MyClass(object): ...: pass ...: In [2]: type('Vspace', (MyClass,), {}) Out[2]: __main__.Vspace In [3]: x = Vspace() --- NameError Traceback (most recent call last) C:\Python27\Scripts\ in () > 1 x = Vspace() NameError: name 'Vspace' is not defined In [4]: Vspace = type('Vspace', (MyClass,), {}) In [5]: x = Vspace() In [6]: type(x) Out[6]: __main__.Vspace I don't understand how to make `Vspace` usable for creating instances later (line 3) when I just call `type`; that is why I thought adding the `__name__` attribute would work. Hmm, now I know that doesn't work either: In [8]: del Vspace In [9]: m = type('Vspace', (MyClass,), {}) In [10]: m.__name__ = 'Vspace' In [11]: x = Vspace() --- NameError Traceback (most recent call last) C:\Python27\Scripts\ in () > 1 x = Vspace() NameError: name 'Vspace' is not defined In [11]: m Out[12]: __main__.Vspace Maybe this is too much trouble just to save a few lines (~50) of code. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: class factory question
On Wednesday, June 26, 2013 10:46:55 AM UTC-4, Peter Otten wrote: > Tim wrote: > > I am not completely understanding the type function I guess. Here is an > > example from the interpreter: > > No, you are not understanding how Python namespaces work ;) > To get a Vspace in the global namespace you'd have to bind that name > Vspace = type(...) > > which defeats your plan of mass creation of such names. The clean way to > cope with the situation is to use a dict: > > classnames = ["Vspace", ...] > classes = {name: type(name, ...) for name in classnames} > > Then you can access the Vspace class with > classes["Vspace"] > > If that is inconvenient for your usecase you can alternatively update the > global (module) namespace: > globals().update((name, type(name, ...) for name in classnames) > > For example: > >>> class A(object): pass > ... > >>> globals().update((n, type(n, (A,), {})) for n in ["Beta", "Gamma"]) > >>> Beta > > >>> issubclass(Beta, A) > True Thank you for that great explanation. That is exactly what I needed to know! What a fantastic group this is. --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: class factory question
On Thursday, June 27, 2013 9:16:50 AM UTC-4, Joshua Landau wrote: > On 26 June 2013 14:09, Tim wrote: > > > I am extending a parser and need to create many classes that are all > > subclassed from the same object (defined in an external library). When my > > module is loaded I need all the classes to be created with a particular > > name but the behavior is all the same. Currently I have a bunch of lines > > like this: > > > > class Vspace(Base.Command): pass > > class Boldpath(Base.Command): pass > > > > There are a bunch of lines like that. > > Is there a better way? Something like > > newclasses = ['Vspace', 'Boldpath', ... ] > > for name in newclasses: > > tmp = type(name, (Base.Command,) {}) > > tmp.__name__ = name > > > > Is there a more pythonic way? > > I've just realised -- why on earth are you doing this? Surely there's > a better way than having 50 identical classes. :/ The reason is that I'm using a parser that creates a DOM from a LaTeX file. The plasTeX package (http://plastex.sourceforge.net/) provides the library to accomplish that. The parser needs to know how to consume some custom-defined tokens (like \Vspace, \Boldpath, etc). The behavior for these is all the same so they're subclassed from one base class, but they need to have these particular names so the parser knows how to consume them when encountered in the source file. That is, for every custom command the parser encounters, it looks for a class of that name in order to tokenize it. I agree that this is an abnormal circumstance and normally I would just subclass in the normal way. But I think this situation is different enough that it is okay to break a convention. thanks for thinking about this. --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: class factory question
On Thursday, June 27, 2013 11:56:24 AM UTC-4, Irmen de Jong wrote: > On 27-6-2013 15:48, Dave Angel wrote: > >> The behavior for these is all the same so they're subclassed > >> from one base class, but they need to have these particular names so the > >> parser knows > >> how to consume them when encountered in the source file. That is, for > >> every custom > >> command the parser encounters, it looks for a class of that name in order > >> to tokenize it. > ^ > > How does it look for a class? > Can you perhaps override the way it looks for a class of that name? > So that you can instead return something sensible rather than having to > define one of 50 > almost identical classes... > Irmen hmm, the package author describes inheriting like I was doing in the first place. With a parser, I really don't know any better way but then I'm not a parsing expert. I will delve into the code and try to get a better handle on how the parser finds the definitions it needs in order to tokenize the input. thanks! --Tim -- http://mail.python.org/mailman/listinfo/python-list
timing issue: shutil.rmtree and os.makedirs
I have the following function (Python2.7 on FreeBSD) that results in an OSError. My intent is to pass it a directory name or path and if it exists, use shutil.rmtree to remove whatever is there (if it isn't a directory, try to unlink it); then use os.makedirs to create a new directory or path: def make_clean_dir(directory): if os.path.exists(directory): if os.path.isdir(directory): shutil.rmtree(directory) else: os.unlink(directory) os.makedirs(directory) The last bit of the traceback is: File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir os.makedirs(directory) File "/usr/local/lib/python2.7/os.py", line 157, in makedirs mkdir(name, mode) OSError: [Errno 17] File exists: '/users/tim/testing/testing_html' The directory 'testing_html' existed when I executed the function; So I suppose the directory wasn't finished being removed by the time os.makedirs was invoked. How can avoid this? (A context manager maybe?). thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: timing issue: shutil.rmtree and os.makedirs
On Monday, July 29, 2013 7:52:36 PM UTC-4, Chris Angelico wrote: > On Mon, Jul 29, 2013 at 8:16 PM, Tim wrote: > > My intent is to pass it a directory name or path and if it exists, use > > shutil.rmtree to remove whatever is there (if it isn't a directory, try to > > unlink it); then use os.makedirs to create a new directory or path: > > def make_clean_dir(directory): > > if os.path.exists(directory): > > if os.path.isdir(directory): > > shutil.rmtree(directory) > > else: > > os.unlink(directory) > > os.makedirs(directory) > > > > The last bit of the traceback is: > > File "/develop/myproject/helpers/__init__.py", line 35, in make_clean_dir > > os.makedirs(directory) > > File "/usr/local/lib/python2.7/os.py", line 157, in makedirs > > mkdir(name, mode) > > OSError: [Errno 17] File exists: '/users/tim/testing/testing_html' > > > > The directory 'testing_html' existed when I executed the function; > > First thing I'd check is: Did rmtree succeed? Try removing the > makedirs and test it again; then, when your process has completely > finished, see if the directory is there. If it is, the problem is in > rmtree - for instance: > * You might not have permission to remove everything > * There might be a messed-up object in the file system > * If the directory is a remote share mount point, the other end might > have lied about the removal > * Something might have been created inside the directory during the removal > * Myriad other possibilities > As I understand rmtree's docs, any errors *that it detects* will be > raised as exceptions (since you haven't told it to suppress or handle > them), but possibly there's an error that it isn't able to detect. > Worth a test, anyhow. > > ChrisA Thanks Chris, but the directory was actually removed on the first run in spite of the traceback; when I run it a second time (immediately after the first time), it runs fine. That's why I thought it was a timing issue. I thought about just putting a sleep in there, but that made me feel dirty. hmm, now that you mention it, this is executing on a remote box with access to the same file system my local calling program is on. That is, there is a local call to an intermediate script that connects to a socket on the remote where the above program actually runs, but the file system is the same place for both local and remote. But even so, since the script that does the rmtree and mkdir is running on the same machine (even though it's remote), I would think the mkdir couldn't execute until the rmtree was completely finished. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: timing issue: shutil.rmtree and os.makedirs
On Tuesday, July 30, 2013 9:27:10 AM UTC-4, Chris Angelico wrote: > On Tue, Jul 30, 2013 at 2:10 PM, Tim wrote: > > hmm, now that you mention it, this is executing on a remote box with access > > to the same file system my local calling program is on. That is, there is a > > local call to an intermediate script that connects to a socket on the > > remote where the above program actually runs, but the file system is the > > same place for both local and remote. > > > > But even so, since the script that does the rmtree and mkdir is running on > > the same machine (even though it's remote), I would think the mkdir > > couldn't execute until the rmtree was completely finished. > > Hmm. What system is used for the file system sharing? I know quite a > few of them lie about whether something's been completely done or not. > Can you use inotify to tell you when the directory's been deleted? > Seems stupid though. > Worst case, all you need is a quick loop at the bottom, eg: > <> > ChrisA Argg, this isn't the first time I've had troubles with the file system. This is FreeBSD and NFS. I will code up a progressive delay as you mentioned (with Steve's correction). thanks much! --Tim -- http://mail.python.org/mailman/listinfo/python-list
web access to configuration database
I want to make some configuration data available via a web service (bottle) that returns JSON data to a client which may be a python app or a jqgrid-based web page ui. The data structure is a list of dicts with strings, other dicts, and lists. It doesn't fit the relational database scheme; it looks like a nested key-value store. I want to handle get/post for crud operations. Any thoughts as to the best way to store the data? The magic solution would be a json-based database supporting get/post I guess. I've been looking at MongoDB, Redis, CouchDB, and ZODB. (apache thrift?) Currently learning Redis. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: communicate with external process via pty
On Wednesday, October 10, 2012 2:58:55 AM UTC-4, Peter Otten wrote: > Tim Arnold wrote: > > > import os,sys > > import subprocess > > import shlex > > import pty > > cmd = 'tralics --interactivemath' > > (master, slave) = pty.openpty() > > p = subprocess.Popen(shlex.split(cmd),close_fds=True, > > stdin=slave,stdout=slave,stderr=slave) > > > > os.read(master,1024)# start the process > > os.write(master,'$\sqrt{x}$\n') # feed it an equation > > mathml.append(os.read(master,1024)) # get the mathml in a string > > os.write(master,'$\sqrt{x}$\n') # feed more equations > > mathml.append(os.read(master,1024)) # get the next string > > > Do you know about pexpect? > > http://pypi.python.org/pypi/pexpect Thanks Peter. I knew of it, but hadn't tried it. I've been playing with it today and I think it should work fine for my project. With some help from stackoverflow, this seems to do what I need: import pexpect c = pexpect.spawn('tralics --interactivemath') c.expect('>') c.sendline('$x+y=z$') c.expect("") print c.math.group() c.expect('>') c.sendline('$a+b=c$') c.expect("") print c.math.group() etc. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
simple web/html testing
hi, I'm new to web testing and after having googled for a day and a half I figured it might be better to ask here. What I've got is a tree of static HTML documentation I want to test. For example to test that o referenced images exist and are not corrupted, o links to files from the table of contents exist o links to files/anchors from the generated index exist o all internal links resolve o filesizes do not exceed some level (say 1meg) I also need to do some testing of htmlhelp auxiliary files (hhp, hhc, hhk) and xml files that need to pass some schema validation. I'm pretty sure I'll have to write that part myself, but I wondered about using a test framework for the first set of tests. I've read about jenkins, selenium, webtest, twill, and mechanize. The kind of tests I need are maybe too simple for a web testing framework, but I like the idea of automatic generation of test result reports. Any pointers or suggestions for this type of web testing (I guess it's really html testing since this is static html)? thanks, --Tim Arnold -- http://mail.python.org/mailman/listinfo/python-list
Re: simple web/html testing
On Jun 7, 2:05 pm, Miki Tebeka wrote: > http://pypi.python.org/pypi/selenium? I looked at Selenium and it may be what I need, but when I searched for selenium and "broken link" (one of the things I need to test for), I found only an unanswered question: http://groups.google.com/group/selenium-users/browse_thread/thread/7d8e0874d6c2595f Do you think I could configure selenium to run the tests? At this moment, I'm looking at using Jenkins to run the tests as self- written python scripts using lxml, among other things. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: simple web/html testing
On Jun 8, 9:57 am, Santi wrote: > For static html testing, I'd avoid using Selenium. Even though Selenium is > *the* tool for RIA and javascript intensive environments, feels like bringing > up a browser with all the coordination and resources that it takes just to > crawl the website and find 404s is an overkill. > > What we implemented for doing that is just a simple crawler based on urllib: > > < nice code snipped > > > Hope this helps > Hi, Yes, Santi that does help, a lot. And Burhan thanks--I see how I can use selenium for broken links, but for now at least, I'm going to write my own tests for the static html. After hours of searching and reading, I think my direction is to use Jenkins and Nose together so I can have centralized control over my test runs along with nice reports. I may write a Nose plugin later on for static html, but for now I'll just write simple tests and see how that goes. I don't see that anyone is using nose in this non-standard way (on static html rather than on python code), but I so far I don't see any reason why it won't work. Santi, you gave me a start with that code--I can rewrite that as a nose test and execute it with Jenkins and the broken-link portion of the tests should be done. Does that sound appropriate for my use-case (jenkins/nose-> static html)? The testing is for a large set of documentation that is built daily. thanks again, --Tim -- http://mail.python.org/mailman/listinfo/python-list
python2.7.1 freebsd development version
hi, I can't seem to find the development version of python2.7.1; maybe there isn't one any longer, but according to this post, there was a bug in the configure script that affects freebsd machines; the patch to fix it was made, but I think I'm not picking that fix up. http://bugs.python.org/file19836/freebsd_ldshared.patch Where should I look for the updated version? thanks, --Tim Arnold -- http://mail.python.org/mailman/listinfo/python-list
Re: python2.7.1 freebsd development version
On Apr 1, 10:55 am, Tim Golden wrote: > On 01/04/2011 15:25, Tim wrote: > > > hi, > > I can't seem to find the development version of python2.7.1; maybe > > there isn't one any longer, but according to this post, there was a > > bug in the configure script that affects freebsd machines; the patch > > to fix it was made, but I think I'm not picking that fix up. > >http://bugs.python.org/file19836/freebsd_ldshared.patch > > > Where should I look for the updated version? > > This is the Mercurial repo: > > http://hg.python.org/cpython/ > > Look for the 2.7 named branch which tracks 2.7.x development > > TJG hi Tim, thanks -- just what I was looking for, just couldn't find it on my own at all! --T -- http://mail.python.org/mailman/listinfo/python-list
Re: Let's talk about debuggers!
On Wednesday, October 25, 2017 at 9:07:47 AM UTC-4, Thomas Jollans wrote: > Hi, > > I just wanted to know what tools everyone used for debugging Python > applications - scripts / backend / desktop apps / notebooks / whatever. > Apart from the usual dance with log files and strategically inserted > print() calls, that is. > > Thomas Jollans One more vote for pudb. Somehow it fits my brain better than most. -- https://mail.python.org/mailman/listinfo/python-list
segfault using shutil.make_archive
I need to zip up a directory that's about 400mb. I'm using shutil.make_archive and I'm getting this response: Segmentation fault: 11 (core dumped) The code is straightforward (and works on other, smaller dirs): shutil.make_archive(os.path.join(zip_dir, zname), 'zip', tgt_dir) I guess I could drop the convenience of make_archive and use zipfile but that seems to be exactly what make_archive does. I'm on FreeBSD, python2.7. Anyone seen this before? thanks, --tim -- https://mail.python.org/mailman/listinfo/python-list
Re: segfault using shutil.make_archive
On Thursday, October 6, 2016 at 2:04:20 PM UTC-4, Random832 wrote: > On Thu, Oct 6, 2016, at 13:45, Random832 wrote: > > On Thu, Oct 6, 2016, at 12:46, Tim wrote: > > > I need to zip up a directory that's about 400mb. > > > I'm using shutil.make_archive and I'm getting this response: > > > > > > Segmentation fault: 11 (core dumped) > > > > > > The code is straightforward (and works on other, smaller dirs): > > > > Are you able to make a test case that reproduces it reliably without any > > specifics about what data you are using (e.g. can you generate a > > directory full of blank [or /dev/urandom if the compressed size is a > > factor] files that causes this)? Is it a large number of files or a > > large total size of files? > > Also consider passing a logging object to make_archive and see if it > prints out anything interesting before the segfault. > > import shutil > import logging > import sys > logger = logging.getLogger() > logger.setLevel(logging.DEBUG) > logger.addHandler(logging.StreamHandler(sys.stderr)) > shutil.make_archive(..., logger=logger) Interesting. I tried the above in an interactive python session and never saw the problem at all. So then I added the logger to my program code and watched it go. Using the program, I still get the segfault. It happens on the same file; I removed it and then it happens on the next file in the list. It looks like it (python process) has no more space, but the machine has plenty of disk space and available memory. I didn't think about the number of files, I'll look into that. For now, I'm just using subprocess and the zip command, which is working. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: segfault using shutil.make_archive
On Friday, October 7, 2016 at 5:18:11 AM UTC-4, Chris Angelico wrote: > On Fri, Oct 7, 2016 at 5:24 PM, dieter wrote: > > Memory allocations are frequent (in many places) and they fail rarely. > > Therefore, there is quite a high probability that some of those > > allocations fail to check that the allocation has been successful. > > If this happens (and the allocation fails), then a "SIGSEGV" is > > almost certain. > > Which, BTW, doesn't stop them from being bugs. If this can be pinned > down, it definitely warrants a bugs.python.org issue. > > ChrisA Thanks. I can make a test case. It doesn't look like it has been reported before; I'll add an issue once I can reproduce. --tim -- https://mail.python.org/mailman/listinfo/python-list
Re: segfault using shutil.make_archive
On Friday, October 7, 2016 at 1:05:43 PM UTC-4, Michael Torrie wrote: > On 10/06/2016 10:46 AM, Tim wrote: > > I need to zip up a directory that's about 400mb. > > I'm using shutil.make_archive and I'm getting this response: > > > > Segmentation fault: 11 (core dumped) > > > > The code is straightforward (and works on other, smaller dirs): > > > > shutil.make_archive(os.path.join(zip_dir, zname), 'zip', tgt_dir) > > > > I guess I could drop the convenience of make_archive and use zipfile but > > that seems to be exactly what make_archive does. > > > > I'm on FreeBSD, python2.7. > > > > Anyone seen this before? > > Does normal the normal zip utility crash also when trying to zip up this > large directory? I'm not familiar with how shutils.make_archive works, > but since it's part of shutils, I suspect it's calling the zip command > as a subprocess, rather than using the Python zip module. Given the > size of your directory I doubt the zip module would work anyway. > > If the zip command works, you could just execute it using popen from > within Python. I'm now using subprocess and the zip command from FreeBSD, and it's working. So far I can't reproduce a small example of the problem with make_archive. --Tim -- https://mail.python.org/mailman/listinfo/python-list
__init__ patterns
I saw a thread on reddit/python where just about everyone said they never put code in their __init__ files. Here's a stackoverflow thread saying the same thing. https://stackoverflow.com/questions/1944569/how-do-i-write-good-correct-package-init-py-files That's new to me. I like to put functions in there that other modules within the module need. Thought that was good practice DRY and so forth. And I never do 'from whatever import *' Ever. The reddit people said they put all their stuff into different modules and leave init empty. What do you do? I like my pattern but I'm willing to learn. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: How do you draw this layout with wxpython?
Young H. Rhiu wrote: > See: http://hilug.org/img/app_layout.GIF > > I'm implementing an album-like application with wxpython but I'm new to > wxPython though I know how to program with python. The problem is that > it's not easy for me to deal with drawing layout stuff with wxpython. > What layout I'm thinking of looks like the gif image above. > > The application is about saving some comments for each pictures on the > filesystem. There are two windows. The above one is a window for > loading some images and the below one is a TextCtrl for writing > comments and if img1, for instance, is clicked, the first notebook(?) > folder should be active. > > Below is the program I tried but I don't know how to attach a notebook > window under the image window. > Any help is greatly appreciated. > > Thanks in Advance, Rhiu > > My Code: > from wxPython.wx import * > > class smallAppFrame(wxFrame): > def __init__(self, parent, id, title): > wxFrame.__init__(self, parent = None, id = -1, > title = "MySmallApp", pos = wxPoint(200, 200), > size = wxSize(460, 200), style = > wxDEFAULT_FRAME_STYLE) > > self.bitmap01 = wxStaticBitmap(self, -1, > wxEmptyBitmap(100,100)) > self.bitmap02 = wxStaticBitmap(self, -1, > wxEmptyBitmap(100,100)) > self.bitmap03 = wxStaticBitmap(self, -1, > wxEmptyBitmap(100,100)) > self.bitmap04 = wxStaticBitmap(self, -1, > wxEmptyBitmap(100,100)) > > box = wxBoxSizer(wxHORIZONTAL) > > box.Add((10,10)) > box.Add(self.bitmap01, 0, wxALIGN_CENTER) > box.Add((10,10)) > box.Add(self.bitmap02, 0, wxALIGN_CENTER) > box.Add((10,10)) > box.Add(self.bitmap03, 0, wxALIGN_CENTER) > box.Add((10,10)) > box.Add(self.bitmap04, 0, wxALIGN_CENTER) > box.Add((10,10)) > > self.SetAutoLayout(True) > self.SetSizer(box) > > class MySmallApp(wxApp): > def OnInit(self): > frame = smallAppFrame(None, -1, "MySmallApp") > frame.Show(true) > self.SetTopWindow(frame) > return true > > app = MySmallApp(0) > app.MainLoop() > Hi Take a LONG look at Boa Constructor. Boa is young, imperfect, and a little emotional at times. BUT after spending a few hours with it - I think I could create your screen layout in a matter of minutes. Really is worth the steep learning curve and the frustration. timb -- http://mail.python.org/mailman/listinfo/python-list
Controlling windows gui applications from python
Hi all, I'm almost as new to this list as to python so I hope I don't get a "this has been answered a 100 times before" or anything... Currently I am using a program named 'Macro Scheduler' for automating programs that don't have a command line version. Its a simple scripting language that allows you to automate button clicks, mouse movement, starting programs, checking the state of a window, changing the focus, type text into an input field...etc. Is there a way to do these things from within Python? Thanks Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling windows gui applications from python
Thanks for this tip, this looks like exactly what I need. Is there a more extended documentation for watsup somewhere ? I didn't find info on: how to send keystrokes to a program. how to control ComboBox elements... trying out the examples, here are some problems I am running into: I tried adapting the example4.py and example4a.py scripts for running an app of my own and clicking a button. I commented out everything except for running the application and clicking a button. here's what i left in: ### from watsup.launcher import launchApp,terminateApp from watsup.winGuiAuto import findTopWindows, findControl,getEditText,clickButton from watsup.performance import PerformanceCheck,PerformanceCheckError from time import sleep,time def main(myExecutable,myWantedText): # find an instance of SimpleForm. If one isn't there, launch it forms=findTopWindows(wantedText=myWantedText) if forms: form=forms[0] else: form=launchApp(myExecutable,wantedText=myWantedText) button=findControl(form,wantedText='Pref') sleep(1) clickButton(button) #sleep(5) #terminateApp(form) if __name__=='__main__': print ' please run example4a or 4b' ### when I un-comment the line : "terminateApp(form)" i get the following error: Traceback (most recent call last): File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject in __main__.__dict__ File "C:\Python24\Lib\tim_vets\kip\examples\example4a_gnmidi.py", line 3, in ? main('C:\\tools\\MFiTool3.0_INTL_1.04.00\\MFiTool3.exe','MFi Tool 3') File "C:\Python24\Lib\tim_vets\kip\examples\example4_gnmidi.py", line 14, in main button=findControl(form,wantedText='Pref') File "C:\Python24\lib\site-packages\watsup\winGuiAuto.py", line 327, in findControl raise WinGuiAutoError("EnumChildWindows failed with win32gui.error " + WinGuiAutoError: EnumChildWindows failed with win32gui.error 853268, wantedText='Pref', wantedClass=None, selectionFunction=None, maxWait=1, retryInterval=0.1 with this line commented out, getting the 'Pref' button clicked works sometimes... most of the times i get something like: File "example4_gnmidi.py", line 14, in main button=findControl(form,wantedText='Pref') File "C:\Python24\lib\site-packages\watsup\winGuiAuto.py", line 327, in findControl raise WinGuiAutoError("EnumChildWindows failed with win32gui.error " + WinGuiAutoError: EnumChildWindows failed with win32gui.error 1442992, wantedText='Pref', wantedClass=None, selectionFunction=None, maxWait=1, retryInterval=0.1 Opening the application is the only thing that always works... thanks! Simon Brunning wrote: >On 18/11/05, tim <[EMAIL PROTECTED]> wrote: > > >>Hi all, I'm almost as new to this list as to python so I hope I don't >>get a "this has been answered a 100 times before" or anything... >> >>Currently I am using a program named 'Macro Scheduler' for automating >>programs that don't have a command line version. >>Its a simple scripting language that allows you to automate button >>clicks, mouse movement, starting programs, checking the state of a >>window, changing the focus, type text into an input field...etc. >>Is there a way to do these things from within Python? >> >> > ><http://www.tizmoi.net/watsup/intro.html> > >-- >Cheers, >Simon B, >[EMAIL PROTECTED], >http://www.brunningonline.net/simon/blog/ > > > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Controlling windows gui applications from python
Thanks for this tip, this looks like exactly what I need. Is there a more extended documentation for watsup somewhere ? I didn't find info on: how to send keystrokes to a program. how to control ComboBox elements... trying out the examples, here are some problems I am running into: I tried adapting the example4.py and example4a.py scripts for running an app of my own and clicking a button. I commented out everything except for running the application and clicking a button. here's what i left in: ### from watsup.launcher import launchApp,terminateApp from watsup.winGuiAuto import findTopWindows, findControl,getEditText,clickButton from watsup.performance import PerformanceCheck,PerformanceCheckError from time import sleep,time def main(myExecutable,myWantedText): # find an instance of SimpleForm. If one isn't there, launch it forms=findTopWindows(wantedText=myWantedText) if forms: form=forms[0] else: form=launchApp(myExecutable,wantedText=myWantedText) button=findControl(form,wantedText='Pref') sleep(1) clickButton(button) #sleep(5) #terminateApp(form) if __name__=='__main__': print ' please run example4a or 4b' ### when I un-comment the line : "terminateApp(form)" i get the following error: Traceback (most recent call last): File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject in __main__.__dict__ File "C:\Python24\Lib\tim_vets\kip\examples\example4a_gnmidi.py", line 3, in ? main('C:\\tools\\MFiTool3.0_INTL_1.04.00\\MFiTool3.exe','MFi Tool 3') File "C:\Python24\Lib\tim_vets\kip\examples\example4_gnmidi.py", line 14, in main button=findControl(form,wantedText='Pref') File "C:\Python24\lib\site-packages\watsup\winGuiAuto.py", line 327, in findControl raise WinGuiAutoError("EnumChildWindows failed with win32gui.error " + WinGuiAutoError: EnumChildWindows failed with win32gui.error 853268, wantedText='Pref', wantedClass=None, selectionFunction=None, maxWait=1, retryInterval=0.1 with this line commented out, getting the 'Pref' button clicked works sometimes... most of the times i get something like: File "example4_gnmidi.py", line 14, in main button=findControl(form,wantedText='Pref') File "C:\Python24\lib\site-packages\watsup\winGuiAuto.py", line 327, in findControl raise WinGuiAutoError("EnumChildWindows failed with win32gui.error " + WinGuiAutoError: EnumChildWindows failed with win32gui.error 1442992, wantedText='Pref', wantedClass=None, selectionFunction=None, maxWait=1, retryInterval=0.1 Opening the application is the only thing that always works... thanks! Simon Brunning wrote: > On 18/11/05, tim <[EMAIL PROTECTED]> wrote: > > >> Hi all, I'm almost as new to this list as to python so I hope I don't >> get a "this has been answered a 100 times before" or anything... >> >> Currently I am using a program named 'Macro Scheduler' for automating >> programs that don't have a command line version. >> Its a simple scripting language that allows you to automate button >> clicks, mouse movement, starting programs, checking the state of a >> window, changing the focus, type text into an input field...etc. >> Is there a way to do these things from within Python? >> > > > <http://www.tizmoi.net/watsup/intro.html> > > -- > Cheers, > Simon B, > [EMAIL PROTECTED], > http://www.brunningonline.net/simon/blog/ > > > > > -- http://mail.python.org/mailman/listinfo/python-list
hex string to hex value
This is probably another newbie question...but... even after reading quite some messages like '..hex to decimal', 'creating a hex value' , I can't figure this out: If i do >>> m=66 >>> n=hex(m) >>> n '0x42' i cannot use n as value for a variable that takes hex values, because it throws: error: required argument is not an integer >>> int(n) gives me: Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for int(): 0x42 how do I convert '0x42' to 0x42 ? thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: hex string to hex value
but then i get : >>> m 66 >>> n=int(hex(m)) Traceback (most recent call last): File "", line 1, in ? ValueError: invalid literal for int(): 0x42 >>> what am I missing here ? thank you Tim avnit wrote: >If you just want to convert a string to an integer, it would be: > > > >>>>int(n) >>>> >>>> > >in your case it would be: > > > >>>>m=66 >>>>n=int(hex(m)) >>>> >>>> > > > -- http://mail.python.org/mailman/listinfo/python-list
[Fwd: Re: hex string to hex value]
ok, but if i do >>> n=66 >>> m=hex(n) >>> m '0x42' >>> h=int(m,16) >>> h 66 >>> I end up with 66 again, back where I started, a decimal, right? I want to end up with 0x42 as being a hex value, not a string, so i can pas it as an argument to a function that needs a hex value. (i am trying to replace the 0x42 in the line midi.note_off(channel=0, note=0x42) with a variable) [EMAIL PROTECTED] wrote: > avnit wrote: > > >> If you just want to convert a string to an integer, it would be: >> >> >> > int(n) > > > That's what the OP tried and it didn't work. > > BECAUSE you have to tell the int function what base the string is in > (even though it has "0x" at the start). > > > int(n,16) >>> > 66 > > > >> in your case it would be: >> >> >> > m=66 > n=int(hex(m)) > > > Wrong again, you still have to tell it what base. > > > h=int(hex(m),16) h >>> > 66 > > > -- http://mail.python.org/mailman/listinfo/python-list
Re: hex string to hex value
[EMAIL PROTECTED] wrote: >tim wrote: > > >>but then i get : >> >> >>> m >>66 >> >>> n=int(hex(m)) >>Traceback (most recent call last): >> File "", line 1, in ? >>ValueError: invalid literal for int(): 0x42 >> >>> >> >>what am I missing here ? >> >> > >Avnit's solution was wrong. When converting a string, you >must state what base you are converting from. > > > >>>>int(hex(m),16) >>>> >>>> >66 > >Fredrik Lundh's solution works if the hex string starts with "0x" >(which it will when the string is created with the hex function). > > > aren't you converting from a hex string to a decimal value here? -- http://mail.python.org/mailman/listinfo/python-list
Re: [Fwd: Re: hex string to hex value]
Brett g Porter wrote: > tim wrote: > >> >> I end up with 66 again, back where I started, a decimal, right? >> I want to end up with 0x42 as being a hex value, not a string, so i >> can pas it as an argument to a function that needs a hex value. >> (i am trying to replace the 0x42 in the line >> midi.note_off(channel=0, note=0x42) with a variable) > > > > As far as Python is concerned, 66 decimal and 42 hex are exactly the > same thing. There's absolutely no difference in calling > > note_off(0x42) > and > > note_off(66) > > See: > > >>> def f(c): > ...print c > ... > >>> f(66) > 66 > >>> f(0x42) > 66 > >>> > > > so it was easier than I thought! great :D thanks for your help guys! -- http://mail.python.org/mailman/listinfo/python-list
Python Midi Package: writing events non-chronologically
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers at the beginning of the midifile (because I want to be able to make those dependant on what notes were just written) def midctrls(): global roffset, melchan, roffset, gmt, timedisplay, out_file, midi, usednotes, n midi.reset_time() #seems to do nothing for cha in range(16): if cha==1: midi.abs_time=0 #seems to do nothing midi._relative_time = 0 #seems to do nothing, but I can imagine why midi._absolute_time = 0 #seems to do nothing midi.update_time(new_time=0, relative=0) #although I give the variable relative=0 it seems to be relative ? midi.continuous_controller(cha, 0, 122) (snip) With this code I want a controller number 0 with value 122 to be written at the beginning of my midifile. It is written at the end, how I move the writing position to the beginning? thanks for any help! -- http://mail.python.org/mailman/listinfo/python-list
[Fwd: Python Midi Package: writing events non-chronologically]
(...I sent this one a second time, waited for 60 minutes, it didn't appear, sorry if it's a double...) Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers at the beginning of the midifile (because I want to be able to make those dependant on what notes were just written) def midctrls(): global roffset, melchan, roffset, gmt, timedisplay, out_file, midi, usednotes, n midi.reset_time() #seems to do nothing for cha in range(16): if cha==1: midi.abs_time=0 #seems to do nothing midi._relative_time = 0 #seems to do nothing, but I can imagine why midi._absolute_time = 0 #seems to do nothing midi.update_time(new_time=0, relative=0) #although I give the variable relative=0 it seems to be relative ? midi.continuous_controller(cha, 0, 122) (snip) With this code I want a controller number 0 with value 122 to be written at the beginning of my midifile. It is written at the end, how I move the writing position to the beginning? thanks for any help! --- Begin Message --- Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers at the beginning of the midifile (because I want to be able to make those dependant on what notes were just written) def midctrls(): global roffset, melchan, roffset, gmt, timedisplay, out_file, midi, usednotes, n midi.reset_time() #seems to do nothing for cha in range(16): if cha==1: midi.abs_time=0 #seems to do nothing midi._relative_time = 0 #seems to do nothing, but I can imagine why midi._absolute_time = 0 #seems to do nothing midi.update_time(new_time=0, relative=0) #although I give the variable relative=0 it seems to be relative ? midi.continuous_controller(cha, 0, 122) (snip) With this code I want a controller number 0 with value 122 to be written at the beginning of my midifile. It is written at the end, how I move the writing position to the beginning? thanks for any help! --- End Message --- -- http://mail.python.org/mailman/listinfo/python-list
backslash plague: paths containing "\a" somewhere
trying to work with a directory in windows, that has "\a" in the full pathname this works: >>> c = string.replace('c:\content\ce\cw\cvd', '\\', '\\') >>> c 'c:\\content\\ce\\cw\\cvd' this doesn't: >>> c = string.replace('c:\content\a\a\avd', '\\', '\\') >>> c 'c:\\content\x07\x07\x07vd' same goes for paths containing "\b" what's the workaround? thank you! -- http://mail.python.org/mailman/listinfo/python-list
problem compiling executable with py2exe
I wrote this very simple program that checks a directory and makes a list of files that surpass a given size. I tried to compile it using py2exe. I used py2exe before with more complex programs without any problem. Now, although my program works fine when I run it from PythonWin, it won't compile to an executable. here's the program: # import os blacklist=[] d=raw_input('path') s=int(raw_input('maxsize (in bytes):')) f = os.listdir(d) outfile = open(d + r'\filelist_for_this_location.txt', 'wb') outfile.write('files found in ' + d + '\n'+ 'checking maximum filesize: ' + str(s) + ' bytes...\n') for n in f: filepath = os.path.join(d,n) if os.path.isfile(filepath): outfile.write(n + '\n') currsize = int(os.path.getsize(filepath)) if currsize >= s: outfile.write('> is too big\n') blacklist.append(filepath) outfile.write('\n'+'\n'+ 'blacklist\n') for bl in blacklist: outfile.write(bl + '\n') outfile.close() ## here's the py2exe error log: Traceback (most recent call last): File "filesizechecker.py", line 4, in ? EOFError: EOF when reading a line any idea? Tim -- http://mail.python.org/mailman/listinfo/python-list
pyext - google
Hi, I'm playing around with the python external for PureData. (pyext) I want to enable a patch to lookup something on the net. I modified the "search.py" example from Dive Into Python and kind of pasted it into the simple.py example from the pyext docs to be loaded in a pd patch. Loading the script in the patch works, and it receives messages and sends messages back, so I think on the pd side I've got it set up right. If I call my "srch" function (see script) I get this error: Traceback (most recent call last): File "c:/pd/scripts\\searchggl.py", line 86, in srch_1 res = searchgoogle(q) File "c:/pd/scripts\\searchggl.py", line 101, in searchgoogle results = _server.doGoogleSearch( File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 470, in __call__ return self.__r_call(*args, **kw) File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 492, in __r_call self.__hd, self.__ma) File "C:\\Python24\\lib\\site-packages\\SOAPpy\\Client.py", line 406, in __call raise p SOAPpy.Types.faultType: deserialize a 'http://www.w3.org/1999/XMLSchema:Symbol' using encoding style 'http://schemas.xmlsoap.org/soap/encoding/'.> attached are the original search.py script, and my modified pd-pyext version. Any clues appreciated! Tim """Search Google from the command line This program is part of "Dive Into Python", a free Python book for experienced programmers. Visit http://diveintopython.org/ for the latest version. """ __author__ = "Mark Pilgrim ([EMAIL PROTECTED])" __version__ = "$Revision: 1.2 $" __date__ = "$Date: 2004/05/20 18:53:59 $" __copyright__ = "Copyright (c) 2004 Mark Pilgrim" __license__ = "Python" from SOAPpy import WSDL # you'll need to configure these two values; # see http://www.google.com/apis/ WSDLFILE = '/path/to/copy/of/GoogleSearch.wsdl' APIKEY = 'YOUR_GOOGLE_API_KEY' _server = WSDL.Proxy(WSDLFILE) def search(q): """Search Google and return list of {title, link, description}""" results = _server.doGoogleSearch( APIKEY, q, 0, 10, False, "", False, "", "utf-8", "utf-8") return [{"title": r.title.encode("utf-8"), "link": r.URL.encode("utf-8"), "description": r.snippet.encode("utf-8")} for r in results.resultElements] if __name__ == '__main__': import sys for r in search(sys.argv[1])[:5]: print r['title'] print r['link'] print r['description'] print # py/pyext - python script objects for PD and MaxMSP # # Copyright (c) 2002-2003 Thomas Grill ([EMAIL PROTECTED]) # For information on usage and redistribution, and for a DISCLAIMER OF ALL # WARRANTIES, see the file, "license.txt," in this distribution. # """This is an example script for the py/pyext object's basic functionality. pyext Usage: - Import pyext - Inherit your class from pyext._class - Specfiy the number of inlets and outlets: Use the class members (variables) _inlets and _outlets If not given they default to 1 You can also use class methods with the same names to return the respective number - Constructors/Destructors You can specify an __init__ constructor and/or an __del__ destructor. The constructor will be called with the object's arguments e.g. if your PD or MaxMSP object looks like [pyext script class arg1 arg2 arg3] then the __init__(self,args) function will be called with a tuple argument args = (arg1,arg2,arg3) With this syntax, you will have to give at least one argument. By defining the constructor as __init__(self,*args) you can also initialize the class without arguments. - Methods called by pyext The general format is 'tag_inlet(self,args)' resp. 'tag_inlet(self,*args)': tag is the PD or MaxMSP message header.. either bang, float, list etc. inlet is the inlet (starting from 1) from which messages are received. args is a tuple which corresponds to the content of the message. args can be omitted. The inlet index can be omitted. The method name then has the format 'tag_(self,inlet,args)'. Here, the inlet index is a additional parameter to the method You can also set up methods which react on any message. These have the special forms _anything_inlet(self,args) or _anything_(self,inlet,args) Please see below for examples. Any return values are ignored - use _outlet (see below).
Creating Python wrapper for DLL
I have a DLL, and a C .h file that exports a bunch of functions from the DLL. I would like to create a Python extension module for these functions. I have read the "Extending and Embedding" documentation in the Python 2.4 release. I understand how to extend C code for use in Python if I have access to the C source code (I think). But... Will I be able to create Python extensions for my DLL functions if I do not have access to the DLL source code? -- http://mail.python.org/mailman/listinfo/python-list
Re: Creating Python wrapper for DLL
Thanks guys, I'll take a look! -- http://mail.python.org/mailman/listinfo/python-list
Re: Regex substitution trouble
On Tuesday, October 28, 2014 7:03:00 AM UTC-4, mass...@msn.com wrote: > Hi everyone, > I'm not really sure if this is the right place to ask about regular > expressions, but since I'm usin python I thought I could give a try :-) > Here is the problem, I'm trying to write a regex in order to substitute all > the occurences in the form $"somechars" with another string. This is what I > wrote: > newstring = re.sub(ur"""(?u)(\$\"[\s\w]+\")""", subst, oldstring) > > This works pretty well, but it has a problem, I would need it also to handle > the case in which the internal string contains the double quotes, but only if > preceeded by a backslash, that is something like > $"somechars_with\\"doublequotes". > Can anyone help me to correct it? > Thanks in advance! You have some good answers already, but I wanted to let you know about a tool you may already have which is useful for experimenting with regexps. On windows, the file `redemo.py` is in the Tools/Scripts folder. If you're on a Mac, see http://stackoverflow.com/questions/1811236/how-can-i-run-redemo-py-or-equivalent-on-a-mac It has really helped me work on some tough regexps. good luck, --Tim -- https://mail.python.org/mailman/listinfo/python-list
help with creating dict from string
hi, I have strings coming in with this format: '[one=two, three=four five, six, seven=eight]' and I want to create from that string, this dictionary: {'one':'two', 'three':'four five', 'six':True, 'seven':'eight'} These are option strings, with each key-value pair separated by commas. Where there is a value, the key-value pair is separated by '='. This is how I started (where s is the string): s = s.replace('[','').replace(']','') s = [x.split('=') for x in s.split(',')] [['one', 'two'], [' three', 'four five'], [' six'], [' seven', 'eight']] I know I can iterate and strip each item, fixing single-element keys as I go. I just wondered if I'm missing something more elegant. If it wasn't for the leading spaces and the boolean key, the dict() constructor would have been sweet. thanks for any ideas, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: help with creating dict from string
On Thursday, November 6, 2014 12:41:10 PM UTC-5, Peter Otten wrote: > Tim wrote: > > > hi, I have strings coming in with this format: > > > > '[one=two, three=four five, six, seven=eight]' > > > > and I want to create from that string, this dictionary: > > {'one':'two', 'three':'four five', 'six':True, 'seven':'eight'} > > > > snip > > Not everything has to be a one-liner ;) If it works I don't think something > > >>> s = '[one=two, three=four five, six, seven=eight]' > >>> def fix(pair): > ... key, eq, value = pair.partition("=") > ... return key.strip(), value if eq else True > ... > >>> dict(fix(t) for t in s.strip("[]").split(",")) > {'three': 'four five', 'seven': 'eight', 'one': 'two', 'six': True} > > is particularly inelegant. Are you sure that your grammar is not more > complex than your example, e. g. that "," cannot occur in the values? hi Peter, I definitely wouldn't say that is inelegant :-) I had never used the partition method and I didn't realize (or maybe remember) that strip could take a string of characters, not just one. Oh yes, I am positive about the grammar--no commas are allowed in the values. I think your solution is pretty elegant. Thanks for your help! --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: Working with HTML5 documents
On Thursday, November 20, 2014 12:04:09 PM UTC-5, Denis McMahon wrote: > On Wed, 19 Nov 2014 13:43:17 -0800, Novocastrian_Nomad wrote: > > > On Wednesday, November 19, 2014 2:08:27 PM UTC-7, Denis McMahon wrote: > >> So what I'm looking for is a method to create an html5 document using > >> "dom manipulation", ie: > >> > >> doc = new htmldocument(doctype="HTML") > >> html = new html5element("html") > >> doc.appendChild(html) > >> head = new html5element("body") > >> html.appendChild(head) > >> body = new html5element("body") > >> html.appendChild(body) > >> title = new html5element("title") > >> txt = new textnode("This Is The Title") > >> title.appendChild(txt) > >> head.appendChild(title) > >> para = new html5element("p") > >> txt = new textnode("This is some text.") > >> para.appendChild(txt) > >> body.appendChild(para) > >> > >> print(doc.serialise()) > >> > >> generates: > >> > >> This Is The Title >> head>This is some text. > >> > >> I'm finding various mechanisms to generate the structure from an > >> existing piece of html (eg html5lib, beautifulsoup etc) but I can't > >> seem to find any mechanism to generate, manipulate and produce html5 > >> documents using this dom manipulation approach. Where should I be > >> looking? > > > Use a search engine (Google, DuckDuckGo etc) and search for 'python > > write html' > > Surprise surprise, already tried that, can't find anything that holds the > document in the sort of tree structure that I want to manipulate it in. > > Everything there seems to assume I'll be creating a document serially, eg > that I won't get to some point in the document and decide that I want to > add an element earlier. > > bs4 and html5lib will parse a document into a tree structure, but they're > not so hot on manipulating the tree structure, eg adding and moving nodes. > > Actually it looks like bs4 is going to be my best bet, although limited > it does have most of what I'm looking for. I just need to start by giving > it "" to parse. > > -- > Denis McMahon I believe lxml should work for this. Here's a snippet that I have used to create an HTML document: from lxml import etree page = etree.Element('html') doc = etree.ElementTree(page) head = etree.SubElement(page, 'head') body = etree.SubElement(page, 'body') table = etree.SubElement(body, 'table') etc etc with open('mynewfile.html', 'wb') as f: doc.write(f, pretty_print=True, method='html') (you can leave out the method= option to get xhtml). hope that helps, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Recurring Task
Hi All Looking for some advice. I'm creating a small netwok poller and wondered what people recommend to use? Will be polling up to 100 hosts every ten reconds or so Options I can see Home grown using worker threads with Queue and dispatcher based on timestamp Twisted timer service Python Scheduler Has anyone done anything similar and can share some insight? Thanks! Tim -- https://mail.python.org/mailman/listinfo/python-list
recursive function: use a global or pass a parameter?
I have this type of situation and wonder if I should use a global variable outside the recursive function instead of passing the updated parameter through. I want to get a union of all the values that any 'things' key may have, even in a nested dictionary (and I do not know beforehand how deep the nesting might go): d = {'things':1, 'two':{'things':2}} def walk(obj, res): if not hasattr(obj, 'keys'): return set(), set() if 'things' in obj: res.add(obj['things']) for k in obj: walk(obj[k], res) return res walk(d, set()) # returns {1, 2} Is it better to use a global to keep track of the values or does it even matter? thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: recursive function: use a global or pass a parameter?
On Friday, January 16, 2015 at 1:34:51 PM UTC-5, Peter Otten wrote: >> Tim wrote: >> > Globals are generally bad as they make code non-reentrant; when two calls of > the function run simultaneously the data will be messed up. > > I recommend that you use a generator: > > >>> def walk(obj): > ... if not hasattr(obj, "keys"): > ... return > ... if "things" in obj: > ... yield obj["things"] > ... for v in obj.values(): > ... yield from walk(v) > ... > >>> d = {'things':1, 'two':{'things':2}} > >>> set(walk(d)) > {1, 2} > > In Python before 3.3 you have to replace > > yield from walk(v) > > with a loop: > > for t in walk(v): > yield t Ah, a generator, I wouldn't have seen the problem in this way, but with your example, it looks so natural. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
nose config help?
hi, I'm using nose to generate and run some tests not for python code, but for an html repository. I know this isn't the typical way to use nose, and so I'm asking here if the following code smells wrong. I pass some args with the testconfig plugin and run a class setup method one time to get some information from a database. Then the tests get generated and run. During the run, a database is updated with the result. That is an intentional side-effect of running the testis that okay or something that should never be done? I've shortened the actual code to make it readable, but the main thing is the setup (setting class vars), the test generator, and the actual test (plus the database update): class HtmlTester(object): ''' Run this setup method once before tests begin. Reads from tconfig args to get document from database. ''' @classmethod def setup_class(cls): name = tconfig['name'] language = tconfig.get('language', 'en') report = db.conn({'name':name, 'language':language, 'format':'html}) @attr('images') def test_images(self): for image in self.report['images']: yield (self.check_image, image) def check_image(self, image): ''' True if image can be read, otherwise False ''' r = False if get_imagesize(image): p = ImageFile.Parser() try: p.feed(open(image).read()) except IOError: r = False else: r = True self.report.update({'image':image, 'result':r, 'name': self.name, 'language': self.language}) assert_true(r) if __name__ == '__main__': args = sys.argv[:1] + ['--tc=name:mybook', '--tc=language:en' ] + sys.argv[1:] nose.run(argv=args) I've used nose before in simpler situations, but not an expert. This is complex enough that I wonder if the above approach is correct. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
closure = decorator?
I've read a couple of articles about this, but still not sure. When someone talks about a closure in another language (I'm learning Lua on the side), is that the same concept as a decorator in Python? It sure looks like it. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: closure = decorator?
On Saturday, October 12, 2013 4:54:34 PM UTC-4, Peter Cacioppi wrote: > On Thursday, October 10, 2013 6:51:21 AM UTC-7, Tim wrote: > > > I've read a couple of articles about this, but still not sure. > > When someone talks about a closure in another language (I'm learning Lua on > > the side), is that the same concept as a decorator in Python? > > > It sure looks like it. > > Thanks everyone for discussing. I see that closures are completely different from decorators and now I'm not really sure how I got them confused in the first place. I will have to stare at the replies for a while before I can say I really 'get' closures. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: HELP!! How to ask a girl out with a simple witty Python code??
On Wednesday, March 4, 2015 at 8:34:16 PM UTC-5, Xrrific wrote: > Guys, please Help!!! > > I am trying to impress a girl who is learning python and want ask her out at > the same time. > > Could you please come up with something witty incorporating a simple python > line like If...then... but..etc. > > You will make me a very happy man!!! > > Thank you very much!!! r = False while r != 'yes': r = raw_input('do you like me (yes/no): ') print 'i like you too!' might make her smile --Tim -- https://mail.python.org/mailman/listinfo/python-list
aiohttp vs requests: async failures
hi, I posted this on stackoverflow but got no comment or answer. Any suggestion on what's going on, or how I can make this a better question? http://stackoverflow.com/questions/29827642/asynchronous-aiohttp-requests-fails-but-synchronous-requests-succeed With the following code I get "Cannot connect to host ...:443 ssl:True" when I use the asynchronous aiohttp. When I use synchronous requests, it succeeds. The whitehouse.gov links fail, but the google.com succeeds for both async and sync cases. What is going wrong? This is with python 3.4.2 on FreeBSD8, aiohttp 0.14.4, requests 2.5.3 CODE: import asyncio import aiohttp import requests urls = [ 'http://www.whitehouse.gov/cea/', 'http://www.whitehouse.gov/omb', 'http://www.google.com'] def test_sync(): for url in urls: r = requests.get(url) print(r.status_code) def test_async(): for url in urls: try: r = yield from aiohttp.request('get', url) except aiohttp.errors.ClientOSError as e: print('bad eternal link %s: %s' % (url, e)) else: print(r.status) if __name__ == '__main__': print('async') asyncio.get_event_loop().run_until_complete(test_async()) print('sync') test_sync() RESULT: async bad eternal link http://www.whitehouse.gov/cea: Cannot connect to host www.whitehouse.gov:443 ssl:True bad eternal link http://www.whitehouse.gov/omb: Cannot connect to host www.whitehouse.gov:443 ssl:True 200 sync 200 200 200 thanks for any advice, --Tim -- https://mail.python.org/mailman/listinfo/python-list
mixing set and list operations
I noticed this today, using Python2.7 or 3.4, and wondered if it is implementation dependent: You can use 'extend' to add set elements to a list and use 'update' to add list elements to a set. >>> m = ['one', 'two'] >>> p = set(['three', 'four']) >>> m.extend(p) >>> m ['one', 'two', 'four', 'three'] >>> m = ['one', 'two'] >>> p = set(['three', 'four']) >>> p.update(m) >>> p set(['four', 'three', 'two', 'one']) Useful if you don't care about ordering. Not sure if it's dangerous. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: mixing set and list operations
On Thursday, April 30, 2015 at 1:05:05 PM UTC-4, Ben Finney wrote: > Tim writes: > > You can use 'extend' to add set elements to a list and use 'update' to > > add list elements to a set. > > And you can use both of those methods to add items from a file:: > > >>> foo = ['one', 'two'] > >>> bar = open('/usr/share/common-licenses/GPL-3') > >>> foo.extend(bar) > >>> foo > ['one', 'two', 'GNU GENERAL PUBLIC LICENSE\n', > ' Version 3, 29 June 2007\n', '\n', > ... > > You have merely discovered that 'list.extend' and 'set.update' accept an > iterable https://wiki.python.org/moin/Iterator>. > > Sets and lists and files and many other collections are all iterables, > so any of them can be passed to a function that accepts an iterable. Thanks for the answers, that makes perfect sense. It sure is useful too. --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: Is it normal to cry when given XML?
On Tuesday, May 5, 2015 at 5:28:37 AM UTC-4, Sayth Renshaw wrote: > Hi > > Just checking if the reaction to cry when given XML is normal. > > Sayth Hi Sayth, My experience in general is just like what Chris said. Except when dealing with DocBook XML which is probably not what you have. But I cannot say enough good things about lxml, which is my favorite 3rd-party package ever. Its support for xpath really makes it easy to traverse and select elements in a document tree. Plus the ability to drop subtrees, move elements around in a live tree, etc. Great library. Using lxml to parse and restructure the xml to a dictionary isn't too hard, but of course it depends on the xml you have to deal with. Sometimes weeping is just part of the job. good luck, --Tim -- https://mail.python.org/mailman/listinfo/python-list
method returns object reference
I spent a while finding this problem which looks something like the old "mutable default argument" problem. http://docs.python-guide.org/en/latest/writing/gotchas/#mutable-default-arguments I'm not crystal clear on the details, but once I found it, the fix was easy. I'm posting here just because I thought it was interesting. The situation is that I have a Worker() which has a Client(). The worker queries the client; client returns its self.response worker manipulates that response. Now client's response has same changes. The code below prints the following two lines, showing how the 'newkey' is now inside the Client response, even though it was set in the worker. This must be bad practice! In my real code, the response is no longer an instance variable, which fixed the problem. It never had any business being bound to the client anyway. output: [('text', 'first thing')] [('text', 'second thing'), ('newkey', 'my new value')] class Client(object): def __init__(self): self.response = dict() def query(self, text): self.response['text'] = text print self.response.items() return self.response class Worker(object): def __init__(self): self.client = Client() def work(self, expression): data = self.client.query(expression) data['newkey'] = 'my new value' if __name__ == '__main__': t = Worker() t.work('first thing') t.work('second thing') -- https://mail.python.org/mailman/listinfo/python-list
packaging code with compiled libraries
I have a package I want to share but have a question about packaging. Mostly the package is pure python code, but it also requires some binary libraries (*.so, *.dll, *.dylib). I want to bundle these libs so users don't have to compile. The package will run on *nix/windows/mac platforms. Currently I handle this in setup.py. In the 'build' phase, I copy the platform-specific libs to a subdirectory called 'libs'. class MyBuilder(build_py): def run(self): conditional logic for copying appropriate library files to 'libs' etc etc. build_py.run() And that seems to work, but after reading more from the Python Packaging Authority, I wonder if that is the right way. Should I be using wheels instead? I think my brain fried a little bit while going through the doc. thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: packaging code with compiled libraries
On Tuesday, October 6, 2015 at 5:51:48 AM UTC-4, Oscar Benjamin wrote: > On 5 October 2015 at 20:43, Tim wrote: > > > > I have a package I want to share but have a question about packaging. > > > > Mostly the package is pure python code, but it also requires some binary > > libraries (*.so, *.dll, *.dylib). I want to bundle these libs so users > > don't have to compile. The package will run on *nix/windows/mac platforms. > The idea of a wheel is that you want to distribute your code fully > precompiled to end users who will be able to install it without > needing any C compilers etc. Of course this requires you to supply > wheels for each platform of interest. If this is what you want to do > then yes absolutely use wheels. Note that if you have installed > setuptools and wheel and you use setuptools in your setup.py then > building a wheel is as simple as running "python setup.py bdist_wheel" > (once your setup.py is complete). > > If the binary libraries in question are extension modules then you > should just declare them as such in your setup.py and > distutils/setuptools/wheel will take care of bundling them into the > wheel. > > If the binary libraries are not extension modules and you are building > them separately (not using distutils) then you can declare them as > "datafiles" [1] so that they will be bundled into your wheel and > installed alongside your python code. > > [1] https://packaging.python.org/en/latest/distributing/#package-data > > -- > Oscar Thanks for that description. I read through the recent archives on distutils-sig and it's pretty much over my head. These libs are built separately (not extension modules) so I will use wheels with the 'datafiles' approach. --Tim -- https://mail.python.org/mailman/listinfo/python-list
ANN: driver to convert LaTeX math to MathML, using Tralics
This is an announcement of a python driver to convert LaTeX math snippets to MathML (tralics_driver, MIT license). It may be of interest to Python developers who work with LaTeX and MathML. There are several tools to convert LaTeX math to MathML; this tool is a driver that uses Tralics: Tralics is free software whose purpose is to convert a LaTeX document into an XML file, from http://www-sop.inria.fr/marelle/tralics/ This tool is not affiliated with Tralics or the Tralics team, it is only a driver. The driver is written in and meant to be used from Python. It requires: * Python pexpect package * Tralics installation * Python lxml package (elementtree could be used as well) The driver is available on GitHub; the documentation is here: http://tiarno.github.io/tralics_driver/ thanks, --Tim Arnold -- https://mail.python.org/mailman/listinfo/python-list
Re: Not Responding When Dealing with Large Data
On Wednesday, June 18, 2014 1:20:13 PM UTC-4, cutey Love wrote: > I'm trying to read in 10 lines of text, use some functions to edit them > and then return a new list. > The problem is my program always goes not responding when the amount of lines > are a high number. > I don't care how long the program takes to work, just need it to stop > crashing? Please show some code. What kind of text (how long are the lines roughly)? thanks, --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: xslt with python
On Tuesday, July 15, 2014 4:10:57 AM UTC-4, varun bhatnagar wrote: > I am trying to merge two xmls using xslt in python but the content of first > xml is not getting copied. The rules written in xsl file created are correct > because if I am executing it without python (directly from eclipse as I have > xslt plugin installed) it is getting merged fine. Can anybody help me? I am > pasting my code snippet below: > > import lxml.etree as ET > dom = ET.parse(r'test_file_2.xml') > xslt = ET.parse(r'TestMergeRules.xsl') > transform = ET.XSLT(xslt) > print transform > > newdom = transform(dom) > print(ET.tostring(newdom, pretty_print=True)) > > I am opening other xml file (test_file_1.xml) in xsl file using "document()" > function. > > Once this code is executed I can see only the content of test_file_2.xml. > Am I doing something wrong? Is there any other way to do this because my xslt > rules are working fine without python. > Hi BR, Your code looks very close but not exactly equal to the tutorial here: http://lxml.de/xpathxslt.html#xslt Can you try the example on that page and report the results? Plus your platform, python version and lxml details as described here: http://lxml.de/FAQ.html#i-think-i-have-found-a-bug-in-lxml-what-should-i-do thanks, --Tim Arnold -- https://mail.python.org/mailman/listinfo/python-list
Re: Removing xml element and strip extra space
On Tuesday, July 22, 2014 8:53:35 AM UTC-4, varun bhatnagar wrote: > Hi, > Thank you so much for the suggestion. > I tried using the rstrip() function but that did not work. Still getting a > blank space between and as mentioned in the above > output xml file: > > > Is there any other way through which this can be achieved? Can't this be > handled by xslt itself in some way? > > Thanks, > > BR, > Varun > On Tue, Jul 22, 2014 at 2:23 PM, Monte Milanuk wrote: > On 2014-07-22, varun bhatnagar wrote: > > I want to strip the space between ** and ** > > Can anyone suggest a way out to do that? > Look at str.rstrip() - by default it removes trailing whitespace > including carriage returns. > -- Hi Varun, The whitespace is part of your original xml; the xslt is only preserving that whitespace. Do you have any control over the construction of that original xml? It looks like it has been tidied and whitespace perhaps added. I think you will get what you want if the original has the newlines removed: does that get you what you're looking for? Is the whitespace actually necessary in the original or problematic in the result? --Tim Arnold -- https://mail.python.org/mailman/listinfo/python-list
Re: Anyway to reduce size of pdf using python script.
On Friday, February 1, 2013 8:03:41 PM UTC-5, access...@gmail.com wrote: > I have a batch file that exports ArcGIS pdf maps to a directory. I would like > to include a step in the script where the pdf file is reduced in size instead > of manually opening each file in Acrobat X Pro after the script has run and > doing it there. > > Can this be done using python scripting or does the automation stop at > exporting the map? > > Thanks I don't have a definitive answer but I use qpdf to optimize my pdf files. Whether that reduces the size I'm not sure. http://qpdf.sourceforge.net/ --Tim -- https://mail.python.org/mailman/listinfo/python-list
Re: Recommended hosting
On Thursday, October 2, 2014 9:30:38 AM UTC-4, writeson wrote: > Hi all, > I'd like to build a web site for myself, essentially a "vanity" web site > web applications written with Python and Flask, running as uwsgi > applications. These would support dynamic HTML where needed, but mostly it > would provide REST API's. > static content delivered by Nginx Hi, I second webfaction--you can indeed run flask and nginx. They have good documentation and helpful support. I've used them over the years for Django sites, bottle applications, and plain static blog sites and never had a complaint. good luck! --Tim Arnold -- https://mail.python.org/mailman/listinfo/python-list
correct usage of a generator?
Hi, I need to generate a list of file names that increment, like this: fname1 fname2 fname3 and so on. I don't know how many I'll need until runtime so I figure a generator is called for. def fname_gen(stem): i = 0 while True: i = i+1 yield '%s%d' % (stem,i) blarg = fname_gen('blarg') boo = fname_gen('boo') n = 3 for w in range(0,n): in_name = blarg.next() out_name = boo.next() This works, but is it the 'normal' way to accomplish the task when you don't know 'n' until run-time? thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: correct usage of a generator?
thanks everyone. I thought blarg.next() looked a little strange--I'm just learning generators now and I'm glad too see that next(blarg) is the way to go. The example really was just a toy or I would use an iterator. I do need the two (well, the several) generators to not be coupled together so they increment independently. I'll keep the zero-padding advice in mind--I didn't think of that one. what a great group this is. thanks again! --Tim -- http://mail.python.org/mailman/listinfo/python-list
program with raw_input prompt behaves differently after compile
I want to write a program that looks into a given folder, groups files that have a certain part of the filename in common and then copy those groups one at a time to another place, using the raw_input prompt to continue or break. here's what I have: ### def makegroepen(): global p import os from longestcommonprefix import longestcommonprefix p = raw_input('path') l = os.listdir(p) l.sort() groepen=[] groep=[] basenames=[] for r in l: if r.find('_poly16.mp3'): baselist = r.split('_') mopobasename = baselist[0] if mpbasename not in basenames: basenames.append(mpbasename) for s in l: if longestcommonprefix([s,mpbasename])==mpbasename: print mpbasename if s not in groep: groep.append(s) if len(groep)==6: groepen.append(groep) groep=[] print groepen return groepen def movegr(): global p, groepen for t in groepen: contprompt=raw_input('continue? (n to stop)') if contprompt=='n': break for curr in t: if os.path.isfile(p+'\\'+curr): tempfile = open(p+'\\'+curr, 'rb') tempfile.seek(0) tempfilecont = tempfile.read() dircondition = os.path.exists('c:\\content\\workfolder') if dircondition == False: os.makedirs('c:\\content\\workfolder') destfile = open('c:\\content\\workfolder\\'+curr, 'wb') destfile.write(tempfilecont) destfile.close() if __name__=='__main__': global groepen groepen = makegroepen() movegr() (I renamed 'commonprefix' to 'longestcommonprefix', it is actually just the 'binary search version' I found at http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252177 ) It works fine when I run this from PythonWin IDE, but after compiling an executable from it (py2exe) it exits whatever I type in the 'continue?' prompt. What am I doing wrong? Thanks, Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: program with raw_input prompt behaves differently after compile
It was kindof a stupid mistake on my part: I had to put 'import os' at the very beginning, and not only in one of my two function definitions. Thanks anyway, thanks to your link I also found how to change the colour of the console...neat :p ! Tim Hans Nowak wrote: >tim wrote: > > > >>I want to write a program that looks into a given folder, groups files >>that have a certain part of the filename in common and then copy those >>groups one at a time to another place, using the raw_input prompt to >>continue or break. >> >> >> > > [...] > > >>It works fine when I run this from PythonWin IDE, but after compiling an >>executable from it (py2exe) it exits whatever I type in the 'continue?' >>prompt. >>What am I doing wrong? >> >> > >Maybe this helps: > >http://forums.devshed.com/python-programming-11/eof-error-with-raw-input-from-a-exe-text-color-187633.html > >The solution described here was to compile the program as a console app, >rather than a Windows app. > > > -- http://mail.python.org/mailman/listinfo/python-list
break into running code PythonWin
Very often this doesn't work and I am forced to do ctrl+alt+del to quit pythonwin from the task manager. Is there a better way to interrupt my testruns when they hang ? thank you, Tim -- http://mail.python.org/mailman/listinfo/python-list
MidiToText : EventDispatcher instance has no attribute 'sysex_events'
Trying to convert midi to text using MidiToText.py. I get the following: midi_port: 0 Traceback (most recent call last): File "MidiToText.py", line 176, in ? midiIn.read() File "C:\Python24\Lib\site-packages\midi\MidiInFile.py", line 24, in read p.parseMTrkChunks() File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 167, in parseMTrkChunks self.parseMTrkChunk() # this is where it's at! File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 129, in parseMTrkChunk dispatch.sysex_events(sysex_data) AttributeError: EventDispatcher instance has no attribute 'sysex_events' any ideas? thank you, Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: MidiToText : EventDispatcher instance has no attribute 'sysex_events'
Carsten Haese wrote: On Fri, 2005-12-30 at 09:52, tim wrote: Trying to convert midi to text using MidiToText.py. I get the following: midi_port: 0 Traceback (most recent call last): File "MidiToText.py", line 176, in ? midiIn.read() File "C:\Python24\Lib\site-packages\midi\MidiInFile.py", line 24, in read p.parseMTrkChunks() File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 167, in parseMTrkChunks self.parseMTrkChunk() # this is where it's at! File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 129, in parseMTrkChunk dispatch.sysex_events(sysex_data) AttributeError: EventDispatcher instance has no attribute 'sysex_events' Try changing "def sysex_event(self, data):" in ...\midi\EventDispatcher.py to "def sysex_events(self, data):" Hope this helps, Carsten. Yes thank Carsten! it works. Now I am trying to generate a string or a list from the output of MidiToText instead of printing to the console. I tried simply replacing the "print" in MidiToText by "return" everywhere, and renamed it to MidiToTextFile so that I would be able to do: for test_file in fulldir: f = open(test_file, 'rb') # do parsing from MidiInFile import MidiInFile midiIn = MidiInFile(MidiToTextFile(), f) f.close() outmidtxt.write(midiIn.read()) or something, to have the text version of several midi's written into one large textfile, but I got lost somewhere along the way how to do this? ...thanks again --- Begin Message --- Carsten Haese wrote: On Fri, 2005-12-30 at 09:52, tim wrote: Trying to convert midi to text using MidiToText.py. I get the following: midi_port: 0 Traceback (most recent call last): File "MidiToText.py", line 176, in ? midiIn.read() File "C:\Python24\Lib\site-packages\midi\MidiInFile.py", line 24, in read p.parseMTrkChunks() File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 167, in parseMTrkChunks self.parseMTrkChunk() # this is where it's at! File "C:\Python24\Lib\site-packages\midi\MidiFileParser.py", line 129, in parseMTrkChunk dispatch.sysex_events(sysex_data) AttributeError: EventDispatcher instance has no attribute 'sysex_events' Try changing "def sysex_event(self, data):" in ...\midi\EventDispatcher.py to "def sysex_events(self, data):" Hope this helps, Carsten. Yes thank Carsten! it works. Now I am trying to generate a string or a list from the output of MidiToText instead of printing to the console. I tried simply replacing the "print" in MidiToText by "return" everywhere, and renamed it to MidiToTextFile so that I would be able to do: for test_file in fulldir: f = open(test_file, 'rb') # do parsing from MidiInFile import MidiInFile midiIn = MidiInFile(MidiToTextFile(), f) f.close() outmidtxt.write(midiIn.read()) or something, to have the text version of several midi's written into one large textfile, but I got lost somewhere along the way how to do this? ...thanks again --- End Message --- -- http://mail.python.org/mailman/listinfo/python-list
Re: Ann: Tkinter drag and drop module
The module can be used with both standard Tkinter and Tix, and makes it quite easy to e.g. drop a bunch of files from a file manager onto any Tkinter widget. It comes with a basic reference manual and a small demo app. It can be found at . Any feedback is much appreciated. Best regards Michael Hi Michael and list, I tried to run dnddemo.py (win XP, Python2.4). but get: File "C:\Documents and Settings\Tim\Bureaublad\installes_pc_mac\programming\python\TkinterDnD-0.4\TkinterDnD\TkinterDnD.py", line 333, in _require tkdndver = tkroot.tk.call('package', 'require', 'tkdnd') TclError: can't find package tkdnd Probably something went wrong installing tkdnd. I got the libtkdnd10.dll from http://mirror.optusnet.com.au/sourceforge/t/tk/tkdnd/ but am not so sure where to put it. The installation section on http://www.iit.demokritos.gr/~petasis/Tcl/tkDND/tkDND.html 'If your platform is Microsoft Windows, then allong with the distribution there is a stubs enabled dynamic library (dll) that was built against tk8.4a1, with the use of VC++ 5.0. If you cannot use the provided binary, then you can always create the required library by using the VC++ project files located in the "win" directory of the tkDND distribution. In all other cases, you should create the library only from the files located in the directories "win" (*.cpp *.h) and "generic" (*.c *.h). You will need a C++ compiler for this.' wasn't very helpful for me. Maybe someone can give me a simple howto for installing this? thank you, Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: easy questions from python newbie
James Stroud wrote: > John Machin wrote: > > James Stroud wrote: > > > >>walterbyrd wrote: > >> > >>>This is the first real python program I have ever worked on. What I > >>>want to do is: > >>>1) count identical records in a cvs file > >>>2) create a new file with quantities instead duplicate records > >>>3) open the new file in ms-excel > >>> > >>>For example, I will start with a file like: > >>> > >>>1001 > >>>1012 > >>>1008 > >>>1012 > >>>1001 > >>>1001 > >>> > >>>and finish with a file like: > >>> > >>>1001,3 > >>>1008,1 > >>>1012,2 > >>> > >>>What I need to know: > >>>1) is there a function in python that will sort a file into another > >>>file. Something like: > >>>sort file1.txt > file2.txt from the DOS command line. I know there is > >>>also a similar "sort" funtion in Unix. > >> snip... > >>>3) I will probably be working with 50 items, or less, would it be best > >>>for me to do this with a > >>>multi-diminsional array? For example: sort the file, read a rec into > >>>the array, if the next rec is the same then incr the count, otherwise > >>>add a new rec with a count of 1. Then write the array to a file? > >>> > >> > >>Ah, a real question. Use a dict: > >> > >>if adict.has_key(some_key): > > > > > > Hey, d00d, ask the department sysadmin to update your Python for you, > > then you'll be able to use this: > > > > if some_key in adict: > > > > > >> adict[some_key] += 1 > >>else: > >> adict[some_key] = 1 > >> > > Last I checked, both worked. > > James > Alternatively, the whole if can be replaced with:- adict[some_key] = adict.get(some_key, 0) + 1 Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Dynamic objects
Mark Shewfelt wrote: > Hello, > > I have implemented a series of classes representing a Building, its > respective Equipment, and then various Components of that equipment > like so (as you'll be able to tell, I'm a newbie): > > class Building: > equipment = {} > def AddEquipment( name, data ): > equipment[ name ] = Equipment( data ) > > class Equipment: > components = {} > def AddComponent( name, data ): >components[ name ] = Component( data ) > > class Component: > data = "" > > These classes are used like so: > > test = Building() > test.AddEquipment( "equipment 1", data ) > test.AddEquipment( "equipment 2", data ) > test.equipment["equipment 1"].AddComponent( "component 1", data ) > test.equipment["equipment 1"].AddComponent( "component 2", data ) > test.equipment["equipment 2"].AddComponent( "component 3", data ) > > But it appears as though the instance of "equipment 1" has ALL of the > components in its components dictionary. I was hoping that the > test.equipment["equipment 1"].components dictionary would only have > those components that were assigned to "equipment 1". > > I have implemented __init__ functions for all of the classes, but all > they do is initialize some data that I haven't shown here. > > I think I'm trying to use a C++ way of doing this (without the new > operator) so if anyone would be so kind as to help with the Python way > of doing this sort of thing I will be eternally grateful. > > Cheers, > > Mark Shewfelt > > I don't see how your examples could work, helps if you post the actual code. Try these classes, I think they accomplish what your trying to do. class Building: def __init__(self, data = ''): self.data = data self.equipment = {} def AddEquipment(self, name, data ): self.equipment[ name ] = Equipment( data ) class Equipment: def __init__(self, data = ''): self.data = data self.components = {} def AddComponent(self, name, data ): self.components[ name ] = Component( data ) class Component: def __init__(self, data): self.data = data -- http://mail.python.org/mailman/listinfo/python-list
Re: CGI script running not completely in HTML
Yong Wang wrote: > Hi, All: > I have written a python CGI script to run in html web page. When I access > to > the html page, it only runs part of the script, then abort because the late > part of > the script is involved in database access, it is slow. I wonder whether there > is > a way to control html running speed so that it can wait for CGI script to > complete > execution, then write the results to html page ? > Thanks a lot . > > Yong > > Yong, Are you reffering to your browser connection to web server timing out waiting for query to complete? I had some long running scripts that would do that so I used a timer thread to print comments to the browser IE: . Brute force approach but it worked using IE with apache. If I remember right you have to call sys.stdout.flush() to force the write over socket to happen right away. Tim -- http://mail.python.org/mailman/listinfo/python-list
Problem with tokenize module and indents
I ran into a problem with a script i was playing with to check code indents and need some direction. It seems to depend on if tabsize is set to 4 in editor and spaces and tabs indents are mixed on consecutive lines. Works fine when editors tabsize was 8 regardless if indents are mixed. Below are how the 3 test files are laid out, the sample code and output I get. Any help on how to detect this correctly would be appreciated. # nano -T4 tabspacing_4.py class Test: """triple quote""" #indent is 1 tab def __init__(self, msg):#indent is 4 spaces << this gets reported as a dedent when there is no change in indent level self.msg = msg#indent is 2 tabs #nano -T8 tabspacing_8A.py class Test: """triple quote""" #indent is 1 tab def __init__(self, msg):#indent is 8 spaces<< no indent change reported self.msg = msg#indent is 1 tab + 4 spaces #nano -T8 tabspacing_8B.py class Test: """triple quote""" #indent is 1 tab def __init__(self, msg):#indent is 1 tab << no indent change reported self.msg = msg#indent is 1 tab + 4 spaces My script #!/usr/bin/env python import tokenize from sys import argv indent_lvl = 0 line_number = 0 lines = file(argv[1]).readlines() done = False def parse(): def feed(): global line_number, lines if line_number < len(lines): txt = lines[line_number] line_number += 1 else: txt = '' return txt def indents(type, token, start, end, line): global indent_lvl, done if type == tokenize.DEDENT: indent_lvl -= 1 elif type == tokenize.INDENT: indent_lvl += 1 elif type == tokenize.ENDMARKER: done = True return else: return print "token=%s, line_number=%i, indent_lvl=%i" % (tokenize.tok_name[type], start[0], indent_lvl), line.strip() while not done: tokenize.tokenize(feed, indents) parse() $ ./sample.py tabspacing_4.py token=INDENT, line_number=3, indent_lvl=1 """triple quote""" #indent is 1 tab token=DEDENT, line_number=4, indent_lvl=0 def __init__(self, msg): #indent is 4 spaces <-- PROBLEM HERE token=INDENT, line_number=5, indent_lvl=1 self.msg = msg #indent is 2 tabs token=DEDENT, line_number=8, indent_lvl=0 $ ./sample.py tabspacing_8A.py token=INDENT, line_number=3, indent_lvl=1 """triple quote""" #indent is 1 tab token=INDENT, line_number=5, indent_lvl=2 self.msg = msg #indent is 1 tab + 4 spaces token=DEDENT, line_number=8, indent_lvl=1 token=DEDENT, line_number=8, indent_lvl=0 $ ./sample.py tabspacing_8B.py token=INDENT, line_number=3, indent_lvl=1 """triple quote""" #indent is 1 tab token=INDENT, line_number=5, indent_lvl=2 self.msg = msg #indent is 1 tab + 4 spaces token=DEDENT, line_number=8, indent_lvl=1 token=DEDENT, line_number=8, indent_lvl=0 -- http://mail.python.org/mailman/listinfo/python-list
Can Readlines() go to next line after a Tab
I have a module based app that can load many modules at startup. The modules are text based with a '\t' separating the keyword from the definition. The app reads each module to extract the keywords, but because readlines() must read to the end of the current line to get to the next, the module loading is slow. Is there a way to have readlines() go to the next line when it finds a tab '\t' instead of a newline '\n'? Is there a better way to do this? I know a second file could be made of the keywords only, but I would like to have only one file. Thanks, Tim -- http://mail.python.org/mailman/listinfo/python-list
shared memory pointer
Hello Everyone, I am getting shared memory in python using the following. szName = c_char_p(name) hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READONLY, 0, TABLE_SHMEMSIZE, szName) if (hMapObject == 0): print "OpenKey: Could not open name file mapping object" raise WinError() self.pData = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE) This seems to work OK. How do I get to the data in self.pData? It is suppose to be an array of floats. I think MapViewOfFile returns a point to the data. Here is what I tried. current_time = 45.55 memcpy( current_time, self.pData, 4 ) but I get an error: ArgumentError: argument 1: : Don't know how to convert parameter 1 I need to get my floating point array casted from the self.pData. Any help would be appreciated! Thanks Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: shared memory pointer
On Sep 10, 10:11 am, Tim Golden <[EMAIL PROTECTED]> wrote: > Tim wrote: > > Hello Everyone, > > > I am getting shared memory in python using the following. > > > szName = c_char_p(name) > > hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, > > None, PAGE_READONLY, 0, TABLE_SHMEMSIZE, szName) > > if (hMapObject == 0): > > print "OpenKey: Could not open name file mapping object" > > raise WinError() > > > self.pData = windll.kernel32.MapViewOfFile(hMapObject, > >FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE) > > Without answering your question directly, why not use > the stdlib mmap module, which does exactly this for > you behind the scenes? > > Docs -http://docs.python.org/lib/module-mmap.html > Example -http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413807 > > TJG I saw the mmap function in the shared memory example. I had some concern with my large memory size being written to the disk drive. I though it might slow down my application. The reason I am writting this new code is because the existing method using a file. I thought shared memory would be much faster. I have tried using the memcpy and addresses, casting, etc. Is there a way to use memcpy with the pData returned by MapViewOfFile()? Thanks Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: shared memory pointer
On Sep 10, 10:11 am, Tim Golden <[EMAIL PROTECTED]> wrote: > Tim wrote: > > Hello Everyone, > > > I am getting shared memory in python using the following. > > > szName = c_char_p(name) > > hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, > > None, PAGE_READONLY, 0, TABLE_SHMEMSIZE, szName) > > if (hMapObject == 0): > > print "OpenKey: Could not open name file mapping object" > > raise WinError() > > > self.pData = windll.kernel32.MapViewOfFile(hMapObject, > >FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE) > > Without answering your question directly, why not use > the stdlib mmap module, which does exactly this for > you behind the scenes? > > Docs -http://docs.python.org/lib/module-mmap.html > Example -http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/413807 > > TJG I reviewed the mmap function and I have a question. In the example code below, what is the connection between the data in shared memory and the mmap function. The fileno is zero. Why is it zero? The size makes sense because there is 256 bytes in shared memory. The tag is MyFileMappingObject. This tag does not have anything in common with the shared memory. How can mmap point to any data in shared memory? There is no coorelation. pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, SHMEMSIZE) if (pBuf == 0): print "Could not map view of file" raise WinError() else: memcpy = cdll.msvcrt.memcpy memcpy(pBuf, szMsg, len(szMsg)) shmem = mmap.mmap(0, 256, "MyFileMappingObject", mmap.ACCESS_WRITE) shmem.write("Message from Python process") -- http://mail.python.org/mailman/listinfo/python-list
memcpy
How do I memcpy from a pointer to an array of floats in python? I get errors: NameError: global name 'row' is not defined I want to be able to get the row[i] array element. In C I would normally place the address of row as the first argument. cdll.msvcrt.memcpy( row, pData, 256 ) If I define row as the following I also get the following error: row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float ) ArgumentError: argument 1: : Don't know how to convert parameter 1 Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: memcpy
On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote: > > How do I memcpy from a pointer to an array of floats in python? > > > I get errors: NameError: global name 'row' is not defined > > Well than the (global) name `row` is not defined. Quite clear message, > isn't it? ;-) > > > I want to be able to get the row[i] array element. In C I would > > normally place the address of row as the first argument. > > > cdll.msvcrt.memcpy( row, pData, 256 ) > > > If I define row as the following I also get the following error: > > > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float ) > > > ArgumentError: argument 1: : Don't know > > how to convert parameter 1 > > You don't give enough information so we have to guess. For example I > guess the `ones()` function comes from one of the packages `numeric`, > `numarray` or `numpy`!? > > This function returns a Python object. You can't use arbitrary Python > objects with `ctypes`. `memcpy` expects a pointer not an object. > > Ciao, > Marc 'BlackJack' Rintsch Can I initialize something in Python that I can get access to it's pointer? How about: self.data = TOTAL_OUTPUT_PARMETERS*[TOTAL_PARAMETER_ENTRIES*[c_float()] or self.data = TOTAL_OUTPUT_PARMETERS*[c_char_p("temp")] or self.data = [TOTAL_OUTPUT_PARMETERS*1.0]*TOTAL_PARAMETER_ENTRIES I need to be able to copy the contents of a pointer to shared memory into an array of floats so I can index into that array and see my data. I have a pointer to shared meory but I don't know how to access it. Here is what I would like to write: shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE) memcpy( self.data, shared_memory_pointer, my_size ) Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: memcpy
On Sep 11, 8:01 am, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > On Tue, 11 Sep 2007 05:09:58 -0700, Tim wrote: > > On Sep 10, 3:31 pm, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > >> On Mon, 10 Sep 2007 11:38:50 -0700, Tim wrote: > >> > How do I memcpy from a pointer to an array of floats in python? > > >> > I get errors: NameError: global name 'row' is not defined > > >> Well than the (global) name `row` is not defined. Quite clear message, > >> isn't it? ;-) > > >> > I want to be able to get the row[i] array element. In C I would > >> > normally place the address of row as the first argument. > > >> > cdll.msvcrt.memcpy( row, pData, 256 ) > > >> > If I define row as the following I also get the following error: > > >> > row = ones( TOTAL_PARAMETER_ENTRIES, dtype=float ) > > >> > ArgumentError: argument 1: : Don't know > >> > how to convert parameter 1 > > >> You don't give enough information so we have to guess. For example I > >> guess the `ones()` function comes from one of the packages `numeric`, > >> `numarray` or `numpy`!? > > >> This function returns a Python object. You can't use arbitrary Python > >> objects with `ctypes`. `memcpy` expects a pointer not an object. > > > Can I initialize something in Python that I can get access to it's > > pointer? > > "It's pointer"? Then you have a pointer to a structure that represents > the Python object but still no idea what data is at that pointer. This is > an implementation detail of the Python version and of the particular > object. > > > Here is what I would like to write: > > > shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject, > > FILE_MAP_ALL_ACCESS, > > 0, 0, TABLE_SHMEMSIZE) > > > memcpy( self.data, shared_memory_pointer, my_size ) > > I haven't tested but it should be possible to declare the return type of > `windll.kernel32.MapViewOfFile()` as ``ctypes.POINTER(ctypes.c_double * > 256)`` and then do: > > test_data = numpy.ones(1000) > shared_memory_pointer.contents[0:256] = test_data[0:256] > > Ciao, > Marc 'BlackJack' Rintsch- Hide quoted text - > > - Show quoted text - Is this what you mean? Python did not like the word c_types in front of POINTER. Do you know why? How can I re-declare a function's return type if it is declared somewhere else? test_data = numpy.ones(1000) shared_memory_pointer = POINTER(c_float*256) shared_memory_pointer = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, TABLE_SHMEMSIZE) test_data[0:256]= shared_memory_pointer.contents[0:256] print 'data:', test_data[0] -- http://mail.python.org/mailman/listinfo/python-list
Re: shared memory pointer
WowYou put some detail in this response. Thanks so much! I think I want to stay away from mmap because it uses the disk to store my memory. I am trying to stay away from that. I am building strip charts in this python project to view my data. Currently, I am using a file and I have to open the file when my simulation is complete. The simulation takes too long to execute when it has to dump all of the data to a file. The simulation is written in C++ but I want the strip charts to be written in python so I can using the free plots in matplotlib. I am trying to place the very large data block in shared memory so I don't have to wait for the data to be written to a disk. I have about 200 shared memory segments that the simulation creates. I did not know that my code would write to the hard drive because I use ctypes. Could you explain that. Even if I use mmap, how could I mmap 200 shared memory segments? The first argument to mmap is a zero. How does mmap know to map the returned object from MapViewOfFile to the tag? I have been looking into using a dll to do all of the mappings. Then, I can call the dll from my python script to access my array of floats. What do you think of this approach? I am having trouble finding good documentation of this approach. Can you help? Thanks On Sep 11, 7:58 am, Tim Golden <[EMAIL PROTECTED]> wrote: > Tim wrote: > > I saw the mmap function in the shared memory example. I had some > > concern with my large memory size being written to the disk drive. I > > though it might slow down my application. The reason I am writting > > this new code is because the existing method using a file. I thought > > shared memory would be much faster. > > > I have tried using the memcpy and addresses, casting, etc. Is there a > > way to use memcpy with the pData returned by MapViewOfFile()? > > Tim, let's try to back up on this a moment. As I understand, you're > running under Windows and you want to use shared memory, presumably > between two processes. You know about the CreateFileMapping/MapViewOfFile > API, possibly from this example on MSDN [1] and you want to use that > technique in Windows *without* an explicit file backing. > > Am I right so far? > > I suggested the mmap module, and you seemed to pick up on it > and be trying to use both your ctypes solution *and* the mmap > module as two halves of the same mechanism. Maybe I misunderstood, > but that's what it looked like. > > Then you asked a question about getting hold of a Python object's > memory address to be able to pass it into your ctypes solution. > (Which makes sense, given the nature of that solution). > > What you seem to have missed is that your ctypes code is doing > *exactly* what the mmapmodule.c code (which is the implementation > of the mmap module) is doing for you behind the scenes. > > Here's your code (very slightly reformatted): > > > szName = c_char_p(name) > hMapObject = windll.kernel32.CreateFileMappingA( >INVALID_HANDLE_VALUE, >None, >PAGE_READONLY, >0, >TABLE_SHMEMSIZE, >szName > ) > if (hMapObject == 0): >print "OpenKey: Could not open name file mapping object" >raise WinError() > > self.pData = windll.kernel32.MapViewOfFile( >hMapObject, >FILE_MAP_ALL_ACCESS, >0, >0, >TABLE_SHMEMSIZE > ) > > > > and here's the code from mmapmodule.c (also reformatted > and snipped about): > > > m_obj->map_handle = CreateFileMapping( >m_obj->file_handle, >NULL, >flProtect, >size_hi, >size_lo, >m_obj->tagname > ); > if (m_obj->map_handle != NULL) { >m_obj->data = (char *) MapViewOfFile( > m_obj->map_handle, > dwDesiredAccess, > 0, > 0, > 0 > ); > > > > I hope you can see that they're basically doing the same > thing. (given the appropriate parameters). The only > clear difference is that the final param to MapViewOfFile > is 0 in the Python code, which the MSDN docs[2] indicate > "If this parameter is 0 (zero), the mapping extends > from the specified offset to the end of the file mapping." > It's not clear from that how it applies to a non-file-backed > FileMapping, but I *assume* that the Python devs have tested > that out. > > In short, to have two Python processes talk via shared > memory (not linking to a specific file) the following > works for me: > > > import mmap > > # > # The 0 special file value can be -1 in Python 2.5 > # > shmem = mmap.mmap (0, 1000, "TJG", mmap.ACCESS_WRITE) > shmem.write ("blah blah") > > > > import mmap > > shmem = mmap.mmap (0
py2exe: zipfile=None raised ImportError
I'm at the end of my limited experience... I'm using py2exe to create an executable. I'm using bundle level 1. When I don't use the zipfile option, the executable and library.zip get created and the executable works correctly. When I add the zipfile=None option to put everything into the exe, the exe doesn't work. It raises an ImportError whose traceback is: File "...boot_common.py" ... no module name linecache File " ... no module named zipextimporter File "my.py" ... no module name optparse It looks like the imported modules are not being found in the zip, or something didn't get included that needed to be included. Does anybody know what's happening and how to fix it? Thanks. -- Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe: zipfile=None raised ImportError
Thomas Heller wote: [..] > Tim schrieb: [...] > > > File "...boot_common.py" ... no module name linecache > > > File " ... no module named zipextimporter > > > File "my.py" ... no module name optparse [...] > Another tip: You can examine what is in the zipfile, or the exe (if using > zipfile=None) when you rename the file to a *.zip file, and open it with > winzip or another archiver. I clean before every compile. When I look at the exe as a zip I see all the modules in the error message: optparse.pyc, zipextimporter.pyc, and linecache.pyc. I don't see boot_common.py. Why is it being called? -- Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe: zipfile=None raised ImportError
I see the py2exe mail list posts to this group... I think I have a handle on the problem: After I build the exe, I rename it. The renamed exe is the one that raises the error. The original exe works fine. -- Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: 345 free programming books
Thanks for notification... I am still working on to find new books.. Timetime...so little time -- http://mail.python.org/mailman/listinfo/python-list
Re: 345 free programming books
Anyone has any free books to submit? -- http://mail.python.org/mailman/listinfo/python-list
Programmer Need
Hi, I have a program of about 300 lines of code that I wrote in AutoHotKeys. I am looking for someone who might be interested in making a little $ to help me bring this program into a mac compatable language. I can be reached at tim at rileyphotographic dot come -- http://mail.python.org/mailman/listinfo/python-list
nth root
In PythonWin I'm running a program to find the 13th root (say) of millions of hundred-digit numbers. I'm using n = 13 root = base**(1.0/n) which correctly computes the root to a large number of decimal places, but therefore takes a long time. All I need is the integer component. Is there a quicker way? -- http://mail.python.org/mailman/listinfo/python-list
ReportLab PDF Toolkit v2.4 released
We're pleased to announce the latest version of the ReportLab open source PDF toolkit, now available for download here: https://www.reportlab.com/software/opensource/rl-toolkit/download/ The ReportLab Toolkit is a library for programatically creating documents in PDF format. It's free, open-source software written in Python, and released under a BSD type license. Thanks, -The ReportLab Team https://www.reportlab.com/software/opensource/rl-toolkit/ download/">ReportLab Toolkit v2.4 - The Open Source Library for creating PDF Documents. (21-Jan-2010) -- http://mail.python.org/mailman/listinfo/python-list
Re: Sikuli: the coolest Python project I have yet seen...
On Jan 25, 11:18 am, Ron wrote: > Sikuli is the coolest Python project I have ever seen in my ten year > hobbyist career. An MIT oepn source project, Sikuli uses Python to > automate GUI tasks (in any GUI or GUI baed app that runs the JVM) by > simply drag and dropping GUI elements into Python scripts as function > arguments. Download athttp://sikuli.csail.mit.edu/I also did this > podcast about > Sikulihttp://media.libsyn.com/media/awaretek/Python411_20100124_Sikuli.mp3 Wow , It look likes like better than autoIt !! Very impressed. -- http://mail.python.org/mailman/listinfo/python-list
os.system and loggers
hi, I'm using a 3rd-party python program that uses the python logging facility and also makes calls to os.system. I'm trying to capture its output to a file. In my own code, I've taken control of the loggers that are setup in the other program by removing its StreamHandler and replacing with FileHander. But when it comes to the call to os.system I'm at a loss. I want to capture the stdout from that os.system call in my FileHandler. I thought this might work, before I call the other program's class/method: sys.stdout = getLogger('status').handlers[0].stream but no dice. Is there any clean way to get what I want? If not, I guess I'll override the other method with my own, but it will basically be a bunch of code copied with os.sytem replaced with subprocess, using getLogger('status').handlers[0].stream for stdout/ stderr. thanks, --Tim Arnold -- http://mail.python.org/mailman/listinfo/python-list
Re: os.system and loggers
On Jan 7, 11:24 am, Tim wrote: > hi, I'm using a 3rd-party python program that uses the python logging > facility and also makes calls to os.system. I'm trying to capture its > output to a file. > > In my own code, I've taken control of the loggers that are setup in > the other program by removing its StreamHandler and replacing with > FileHander. But when it comes to the call to os.system I'm at a loss. > > I want to capture the stdout from that os.system call in my > FileHandler. I thought this might work, before I call the other > program's class/method: > sys.stdout = getLogger('status').handlers[0].stream > > but no dice. Is there any clean way to get what I want? If not, I > guess I'll override the other method with my own, but it will > basically be a bunch of code copied with os.sytem replaced with > subprocess, using getLogger('status').handlers[0].stream for stdout/ > stderr. > > thanks, > --Tim Arnold Replying to my own post I think I may have included too much fluff in my original question. The main thing I wonder is whether I can attach a log handler to stdout in such a way that os.system calls will write to that handler instead of the console. thanks, --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: os.system and loggers
On Jan 10, 1:01 pm, Carl Banks wrote: > On Jan 10, 8:29 am, Tim wrote: > > > > > > > > > > > On Jan 7, 11:24 am, Tim wrote: > > > > hi, I'm using a 3rd-party python program that uses the python logging > > > facility and also makes calls to os.system. I'm trying to capture its > > > output to a file. > > > > In my own code, I've taken control of the loggers that are setup in > > > the other program by removing its StreamHandler and replacing with > > > FileHander. But when it comes to the call to os.system I'm at a loss. > > > > I want to capture the stdout from that os.system call in my > > > FileHandler. I thought this might work, before I call the other > > > program's class/method: > > > sys.stdout = getLogger('status').handlers[0].stream > > > > but no dice. Is there any clean way to get what I want? If not, I > > > guess I'll override the other method with my own, but it will > > > basically be a bunch of code copied with os.sytem replaced with > > > subprocess, using getLogger('status').handlers[0].stream for stdout/ > > > stderr. > > > > thanks, > > > --Tim Arnold > > > Replying to my own post > > > I think I may have included too much fluff in my original question. > > The main thing I wonder is whether I can attach a log handler to > > stdout in such a way that os.system calls will write to that handler > > instead of the console. > > No, but you could replace os.system with something that does work. > (It would replace it globally for all uses, so you may need some logic > to decide whether to leave the call alone, or to modify it, perhaps by > inspecting the call stack.) > > The simplest thing to do is to append a shell redirection to the > command (>/your/log/file), so something like this: > > _real_os_system = os.system > > def my_os_system(cmd): > if test_log_condition: > return _real_os_system(cmd + "2> /my/log/file") > return _real_os_system(cmd) > > os.system = my_os_system > > That could backfire for any number of reasons so you probably should > only do this if you know that it works with all the commands it > issues. > > The better way might be to call the subprocess module instead, where > you can dispatch the command with redirection to any stream. I doubt > there's a foolproof way to do that given an arbitrary os.system > command, but the subprocess way is probably safer. > > Carl Banks Thanks Carl. I will use subprocess. I made this little toy example to prove to myself that subprocess does handle a filehandler stream, so I include it here for completeness' sake: import subprocess,logging,shlex # create the logger, filehandler and get the stream log = logging.getLogger('mytest') fh = logging.FileHandler('mytest.log') log.addHandler(fh) log.setLevel(logging.INFO) mylog = logging.getLogger('mytest').handlers[0].stream # write a test line to the log log.info('my first line') # execute the subprocess using the stream as stdout cmd = 'ls -l' p = subprocess.Popen(shlex.split(cmd),stdout=mylog) # if you don't wait(), the output won't be necessarily in chronological order. r = p.wait() # write a last test line to the log log.info('done %s'% r) and it works as expected. thanks, --Tim Arnold -- http://mail.python.org/mailman/listinfo/python-list
client server socket interaction (inetd)
Hi, I have an inetd service on freebsd that calls a program (daemon.py) with which I want the remote user to communicate. I can call daemon.py from the command line on the host machine and it works fine. What I don't understand is how to make my remote client script actually communicate. If I'm understanding correctly, the code below just takes a message and sends it to inetd and writes the stdout from the process to the client. How can I modify the code to send a response back? Here's the outline of what I want to do: (1) client sends the message to the server (client -> inetd -> daemon.py), (2) client receives output back from the server, (3) client user responds to a question from the remote process (4) client continues to receive output back. where 2-3-4 happen as needed by the remote process. Cries out for a while loop doesn't it? I just don't know what to put in it. Currently I just have steps 1 and 2 working. Client sends one message and gets all output back. If server asks a question, processes deadlock with server waiting and client unable to respond. thanks, --Tim Arnold # imports, constants set #def line_buffer(sock): #code to yield the string response in #blocks of 1024 bytes def client(ip,port,message): sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM) sock.connect((ip,port)) sock.send(message) for line in line_buffer(sock): print line sock.close() if __name__ == '__main__': message = ' '.join(sys.argv[1:])) print 'working... %s %s' % (SERVER_IP,SERVER_PORT) client(SERVER_IP,SERVER_PORT,message) print 'done.' -- http://mail.python.org/mailman/listinfo/python-list
Re: client server socket interaction (inetd)
On Feb 17, 2:41 pm, Martin Gregorie wrote: > On Thu, 17 Feb 2011 08:14:36 -0800, Tim wrote: > > Hi, I have an inetd service on freebsd that calls a program (daemon.py) > > with which I want the remote user to communicate. I can call daemon.py > > from the command line on the host machine and it works fine. > > > What I don't understand is how to make my remote client script actually > > communicate. If I'm understanding correctly, the code below just takes a > > message and sends it to inetd and writes the stdout from the process to > > the client. > > > How can I modify the code to send a response back? > > -- > martin@ | Martin Gregorie > gregorie. | Essex, UK > org | Thanks Martin, you're right: > Each time you run the client it: > - connects to the server > - sends a request > - reads the response(s) > - closes the socket and exits. that is exactly what it's doing. But. The server may encounter a problem during the process and ask the user for more information like 'abort/retry' or something like that. What my code does *not* do is allow the user to respond to such a mid- process question (so the server can take in that information and proceed with its process). The server can ask, but there's no mechanism for the user to respond to a question. > Without seeing the code for the server and the corresponding inetd > configuration line its not possible to say more. I'm not trying to be opaque, but the reason I left out the code for the server (daemon.py) is that it works as expected when exec'd from the command line. That is, the process begins, asks a question, gets an answer and continues. The inetd configuration is: myservice stream tcp nowait root /local/daemon.py daemon.py -- http://mail.python.org/mailman/listinfo/python-list
Re: client server socket interaction (inetd)
On Feb 17, 6:09 pm, Martin Gregorie wrote: > On Thu, 17 Feb 2011 13:02:08 -0800, Tim wrote: > > But. The server may encounter a problem > > during the process and ask the user for more information like > > 'abort/retry' or something like that. > > Servers never ask the client for information: they are strictly request/ > response handlers. To do what you're asking about, you'll need to change > both the client and server: > > - the client must analyse every response and, if necessary, ask for > more input from the user. IMO it would be best to design it so it > can accept several commands, with 'quit' being one of them. Either > that or all request/response pairs must be designed so that the client > can always send a single request, output the result and exit no matter > what the server returns. > > - the server must always return a response to every possible request > or log a failure reason, preferably to the system log which is > /var/log/messages in Linux, and die. The server must thoroughly > validate requests and return an adequate error message if errors were > found. I'd strongly suggest that every request generates a single > (newline terminated?) response message because that makes error > detection much easier. If the response is multi-line, send it as a > single line, possibly as a comma-separated list, and let the client > format it for display > > I tend to precede every request and response message with a fixed length > byte count: this way the recipient process always knows how many bytes to > read and can report errors if the received length is wrong. Using an 'end > of message marker' (NEWLINE unless a message can legally contain internal > newlines) can also be useful. I tend to put an error flag in the response > message because this simplifies the client. I also design all messages to > be plain ASCII because this makes debugging a lot easier. If things > really turn to worms you can use wireshark to watch the traffic and read > it straight off the screen without needing to decode binary values, etc. > So, an invalid request and response might look like this: > > Request: "0013,WHOIS,elvis\n" > Response: "0030,ERROR,Unknown request: WHOIS\n" > > > The inetd configuration is: > > myservice stream tcp nowait root /local/daemon.py daemon.py > > From what I recall about inetd that should be OK - I guess it must be > since you can get one request through, and I assume (you didn't say) that > a second request is also accepted without restarting the server. > > -- > martin@ | Martin Gregorie > gregorie. | Essex, UK > org | Thanks for helping me to understand. I don't know if you're familiar with LaTeX, but that's part of what daemon.py calls via subprocess, and that is the underlying process I wanted the user to be able to interact with. When LaTeX encounters a problem it stops processing, asks the user what to do (like abort/retry, kind-of), and does whatever the user says. The daemon.py script handles that okay from the command line, but if I'm understanding you this will be much harder if not impossible to accomplish from a client communicating over a socket. I thought that maybe I needed to fork the socket (somehow) so it could listen after the message is sent, but it looks like it will be more complex. thanks, I'm going to back to reading more on socket programming. --Tim -- http://mail.python.org/mailman/listinfo/python-list
throwing exceptions from csv.DictReader or even csv.reader
Hullo Csv is a very common format for publishing data as a form of primitive integration. It's an annoyingly brittle approach, so I'd like to ensure that I capture errors as soon as possible, so that I can get the upstream processes fixed, or at worst put in some correction mechanisms and avoid getting polluted data into my analyses. A symptom of several types of errors is that the number of fields being interpreted varies over a file (eg from wrongly embedded quote strings or mishandled embedded newlines). My preferred approach would be to get DictReader to throw an exception when encountering such oddities, but at the moment it seems to try to patch over the error and fill in the blanks for short lines, or ignore long lines. I know that I can use the restval parameter and then check for what's been parsed when I get my results back, but this seems brittle as whatever I use for restval could legitimately be in the data. Is there any way to get csv.DictReader to throw and exception on such simple line errors, or am I going to have to use csv.reader and explicitly check for the number of fields read in on each line? cheers Tim -- http://mail.python.org/mailman/listinfo/python-list
main in Python
Hi, I learned that a Python script is written in this way: def main(): ... if __name__ == "__main__": main() Today, when I read a script, I found it has a different way: def main(): ... main() It can run as well. Can someone explain why and the rules that Python scripts get run? Thanks and regards, Tim -- http://mail.python.org/mailman/listinfo/python-list
Popen
Hi, I wonder if I use Popen, the parent process will wait for the child process to finish or continue without waiting? Thanks and regards! -- http://mail.python.org/mailman/listinfo/python-list
Re: Popen
Thanks! Yes I mean subprocess.Popen. I was wondering the meaning of "asynchronously" Here is some code I am reading recently: " result = Popen(cmdline,shell=True,stdout=PIPE).stdout for line in result.readlines(): if find(line,"Cross") != -1: return float(split(line)[-1][0:-1]) " The computation in the program "cmdline" takes a long time, at the end of which the results will be output to stdout. "asynchronous" seems to mean Popen returns to the parent process immediately and the parent and child processes continue to be executed. However, if Popen returns immediately to the parent process, then there will be nothing in "result", not to mention extracting information from the output. Thus it seems to me the parent process has to wait till the child process finish. So how to understand the meaning of "asynchronous"? Thanks and regards! --- On Fri, 7/24/09, Kushal Kumaran wrote: > From: Kushal Kumaran > Subject: Re: Popen > To: "Tim" > Cc: python-list@python.org > Date: Friday, July 24, 2009, 10:58 AM > On Fri, Jul 24, 2009 at 7:33 PM, > Tim > wrote: > > > > Hi, > > I wonder if I use Popen, the parent process will wait > for the child process to finish or continue without > waiting? > > Thanks and regards! > > > > Assuming you mean subprocess.Popen, the child is executed > asynchronously. You can use the wait() method on the > Popen object if > you want the parent to wait for the child to finish. > > -- > kushal > -- http://mail.python.org/mailman/listinfo/python-list