Re: [Tutor] Python equivalent of kill -0 PID
On 04/26/2012 02:24 PM, Sean Carolan wrote: In bash you can do this to see if a process is running: [scarolan@kurobox:~/bin]$ kill -0 24275 [scarolan@kurobox:~/bin]$ echo $? 0 Is there a python equivalent? I tried using os.kill() but did not see any way to capture the output. You might start with something like this ... # python2.x try: os.kill(24275, 0) except OSError, e: if e.errno != 3: # 3 = No such process raise # process is not running, do something else: # process is running, do something It would seem os.kill returns nothing if the signal is sent successfully, and will raise an exception if not. HTH! Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help with choices for new database program
On 07/03/2010 10:25 AM, Jim Byrnes wrote: Jeff Johnson wrote: [snip] http://dabodev.com/ Please check it out. And go to www.leafe.com and subscribe to the dabo-user email list. I would like to try out Dabo, but I don't see it in the Ubuntu repositories and I would like to avoid using svn if I can. I didn't subscribe to the mailing list but I did read the archives and saw a thread about making a deb package. It seems to have ended in April without a clear resolution. So is there a package available so I can use the Ubuntu package manager to install it? Unfortunately, after poking around a bit it would seem the only reliable way of installing dabo for Linux at the moment is checking out trunk from the project's subversion repository. Someone better informed should feel free to set the record straight, if I am mistaken. If your interest in a deb package is mainly the ability to uninstall, then I'd recommend using virtualenv[1] until a suitable deb package is released. The steps would be roughly this (untested) ... $ sudo apt-get install python-reportlab python-wxgtk2.8 $ sudo apt-get install subversion python-virtualenv $ virtualenv daboenv $ cd daboenv $ source bin/activate # this is important # now we install dabo as recommended, adapted from: # http://wiki.dabodev.com/InstallationOnLinux (daboenv)$ (daboenv)$ mkdir src cd src (daboenv)$ svn co http://svn.dabodev.com/dabo/trunk dabo (daboenv)$ cd dabo (daboenv)$ python setup.py install # no sudo! # and run the demo to verify the installation (daboenv)$ demo/DaboDemo.py ... Hmm, this might seem like a lot of work -- but by using this method, dabo is installed under daboenv and not in the system-wide site-packages -- particularly useful for evaluation, IMO. YMMV. HTH, Marty [1] http://virtualenv.openplans.org/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PIL problem
Jim Byrnes wrote: Running Unbuntu 9.10. The Synaptic Pkg Mgr reports python-imaging - 1.1.6-3ubuntu1 - Python Imaging Library is installed. But trying to import PhotoImage gives these results: from ImageTk import PhotoImage Traceback (most recent call last): File stdin, line 1, in module ImportError: No module named ImageTk What have I gotten wrong? Apparently, ImageTk is part of a separate ubuntu package called python-imaging-tk. HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] accessing Postgres db results by column name
Serdar Tumgoren wrote: Hey everyone, Ricardo was nice enough to post his solution as a recipe on ActiveState. For anyone interested in bookmarking it, here's the link: http://code.activestate.com/recipes/577186-accessing-cursors-by-field-name/ Serdar I really like Ricardo's solution ... attribute access is a nice touch, bookmarking it now. FWIW, it would seem that psycopg2 also has a DictCursor (and DictConnection). http://initd.org/psycopg/docs/extras.html HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Running a dos program with python
Armstrong, Richard J. wrote: Hello all, This is my first post to the Tutor@python.org mailto:Tutor@python.org mailing list. I am in the process of switching from Matlab to Python and there is one task that I am having a hard time doing and cannot find the answer on the web. I want to write a script in python that will open up a windows dos program, send three inputs (file names) into program and then run it. I know one way to open up the dos program with os.system(r”c:\shake91.txt”) but cannot do the rest. Use the subprocess module: http://docs.python.org/library/subprocess.html untested, but should work: subprocess.Popen([r'c:\shake91.txt', 'param1', 'paramN-1', 'paramN']) if you want to communicate with the process you can add , stdout=subprocess.PIPE, stdin=subprocess.PIPE) to the function call. Check the docs for more info. HTH, Wayne Wayne, It kindof works. I wrote subprocess.Popen([r'c:\shake91.exe', 'FLAC.txt', 'a.txt', 'b.txt']) The dos program pops up and if I hit the enter key three times then it runs. How can I add these three “enters” into the script? Then perhaps something more like this (untested) ... from subprocess import Popen, PIPE proc = Popen([r'c:\shake91.exe'], stdin=PIPE, stdout=PIPE, stderr=PIPE) stdout, stderr = proc.communicate('FLAC.txt\na.txt\nb.txt\n') If that doesn't work you may need something like 'expect' for windows (or, pexpect and cygwin). HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Example of use of (?Pname) and (?P=name) in Python regular expressions?
Michael Hannon wrote: Greetings. While looking into the use of regular expressions in Python, I saw that it's possible to name match groups using: (?Pname...) and then refer to them using: (?P=name) I'm not sure you've got that quite right. IIUC, the (?P=name) syntax is used to match a previously defined group, in the regular expression itself. (http://docs.python.org/library/re.html) snip x = 'Free Fri Fro Fro From' y = re.sub( r'(?PtestFro) (?P=test)', r'Frodo (--matched from \gtest, twice in a row)', x ) # y == 'Free Fri Frodo (--matched from Fro, twice in a row) From' But, as you can see, to refer to the match I used the \g notation (that I found some place on the web). The \g notation is appropriate for re.sub. HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: Martin, def __iter__(self): while True: for logline in self.logfile: heappush(self.heap, (timestamp(logline), logline)) if len(self.heap) = self.jitter: break try: yield heappop(self.heap) except IndexError: raise StopIteration In this __iter__ method, why are we wrapping a for loop in a while True? S. Are you more interested in one of the loops over the other? Do you have a more specific question? The for loop is a means to populate the heap to a predefined size before this iterator yields any values. You may have noticed that the first time through the heap is empty so it will loop a number of times, as defined by self.jitter, while subsequent entries into the loop will break after only one execution. Perhaps not the most efficient approach -- not sure either way, you'll have to test. The while True loop is where values are yielded, until the heap is empty again (heappop raises an IndexError) -- a fairly common pattern for generators in general, I think. The way I see it (not a programmer by trade or training, FWIW), both loops have something like a do-until style. HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: Nope - but I can look it up. The problem I have is that the source logs are rotated at 0400 hrs, so I need two days of logs in order to extract 24 hrs from to 2359 (which is the requirement). At present, I preprocess using sort, which works fine as long as the month doesn't change. Still not sure without more detail, but IIRC from your previous posts, your log entry timestamps are formatted with the abbreviated month name instead of month number. Without the -M flag, the sort command will ... well, erm ... sort the month names alphabetically. With the -M (--month-sort) flag, they are sorted chronologically. Just a guess, of course. I suppose this is drifting a bit off topic, in any case, but it may still serve to demonstrate the importance of converting your string based timestamps into something that can be sorted accurately by your python code -- the most obvious being time or datetime objects, IMHO. snip class LogFile(object): def __init__(self, filename, jitter=10): self.logfile = gzip.open(filename, 'r') self.heap = [] self.jitter = jitter def __iter__(self): while True: for logline in self.logfile: heappush(self.heap, (timestamp(logline), logline)) if len(self.heap) = self.jitter: break Really nice way to handle the batching of the initial heap - thank you! try: yield heappop(self.heap) except IndexError: raise StopIteration snip ... which probably won't preserve the order of log entries that have the same timestamp, but if you need it to -- should be easy to accommodate. I don't think that is necessary, but I'm curious to know how... I'd imagine something like this might work ... class LogFile(object): def __init__(self, filename, jitter=10): self.logfile = open(filename, 'r') self.heap = [] self.jitter = jitter def __iter__(self): line_count = 0 while True: for logline in self.logfile: line_count += 1 heappush(self.heap, ((timestamp(logline), line_count), logline)) if len(self.heap) = self.jitter: break try: yield heappop(self.heap) except IndexError: raise StopIteration The key concept is to pass additional unique data to heappush, something related to the order of lines from input. So, you could probably do something with file.tell() also. But beware, it seems you can't reliably tell() a file object opened in 'r' mode, used as an iterator[1] -- and in python 3.x attempting to do so raises an IOError. [1] http://mail.python.org/pipermail/python-list/2008-November/156865.html HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: I think I'm having a major understanding failure. Perhaps this will help ... http://www.learningpython.com/2009/02/23/iterators-iterables-and-generators-oh-my/ snip So in essence this: logs = [ LogFile( /home/stephen/qa/ded1353/quick_log.gz, 04/Nov/2009 ), LogFile( /home/stephen/qa/ded1408/quick_log.gz, 04/Nov/2009 ), LogFile( /home/stephen/qa/ded1409/quick_log.gz, 04/Nov/2009 ) ] Gives me a list of LogFiles - each of which has a getline() method, which returns a tuple. I thought I could merge iterables using Kent's recipe, or just with heapq.merge() But, at this point are your LogFile instances even iterable? AFAICT, the answer is no, and I think you should want them to be in order to use heapq.merge. Have a look at the documentation (http://docs.python.org/library/stdtypes.html#iterator-types) and then re-read Kent's advice, in your previous thread ('Logfile multiplexing'), about using the iterator protocol (__iter__). And, judging by the heapq docs (http://docs.python.org/library/heapq.html#heapq.merge) ... Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files). Returns an iterator over the sorted values. ... using heapq.merge appears to be a reasonable approach. You might also be interested to know, that while heapq.merge is(was) new in 2.6, it's implementation is very similar (read: nearly identical) to the one of the cookbook recipes referenced by Kent. It's unclear from your previous posts (to me at least) -- are the individual log files already sorted, in chronological order? I'd imagine they are, being log files. But, let's say you were to run your hypothetical merge script against only one file -- would the output to be identical to the input? If not, then you'll want to sort the inputs first. But how do I get from a method that can produce a tuple, to some mergable iterables? I'm going to re-word this question slightly to How can I modify the LogFile class, for instances to be usable by heapq.merge? and make an attempt to answer. The following borrows heavily from Kent's iterator example, but removes your additional line filtering (if self.stamp.startswith(date), etc) to, hopefully, make it clearer. import time, gzip, heapq def timestamp(line): # replace with your own timestamp function # this appears to work with the sample logs I chose stamp = ' '.join(line.split(' ', 3)[:-1]) return time.strptime(stamp, '%b %d %H:%M:%S') class LogFile(object): def __init__(self, filename): self.logfile = gzip.open(filename, 'r') def __iter__(self): for logline in self.logfile: yield (timestamp(logline), logline) logs = [ LogFile(/home/stephen/qa/ded1353/quick_log.gz), LogFile(/home/stephen/qa/ded1408/quick_log.gz), LogFile(/home/stephen/qa/ded1409/quick_log.gz) ] merged = heapq.merge(*logs) with open('/tmp/merged_log', 'w') as output: for stamp, line in merged: output.write(line) Will it be fast enough? I have no clue. Good luck! Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Iterable Understanding
Stephen Nelson-Smith wrote: It's unclear from your previous posts (to me at least) -- are the individual log files already sorted, in chronological order? Sorry if I didn't make this clear. No they're not. They are *nearly* sorted - ie they're out by a few seconds, every so often, but they are in order at the level of minutes, or even in the order of a few seconds. It was precisely because of this that I decided, following Alan's advice, to pre-filter the data. I compiled a unix sort command to do this, and had a solution I was happy with, based on Kent's iterator example, fed into heapq.merge. However, I've since discovered that the unix sort isn't reliable on the last and first day of the month. So, I decided I'd need to sort each logfile first. The code at the start of *this* thread does this - it uses a heapq per logfile and is able to produce a tuple of timestamp, logline, which will be in exact chronological order. What I want to do is merge this output into a file. Well, you haven't described the unreliable behavior of unix sort so I can only guess, but I assume you know about the --month-sort (-M) flag? I did misunderstand your intent for this thread, so thanks for clarifying. The fact remains that if you are interested in using heapq.merge, then you need to pass it iterable objects. And, I don't see any reason to avoid adapting your approach to fit heapq.merge. How about something like the following (completely untested) ... import gzip from heapq import heappush, heappop, merge def timestamp(line): # replace with your own timestamp function # this appears to work with the sample logs I chose stamp = ' '.join(line.split(' ', 3)[:-1]) return time.strptime(stamp, '%b %d %H:%M:%S') class LogFile(object): def __init__(self, filename, jitter=10): self.logfile = gzip.open(filename, 'r') self.heap = [] self.jitter = jitter def __iter__(self): while True: for logline in self.logfile: heappush(self.heap, (timestamp(logline), logline)) if len(self.heap) = self.jitter: break try: yield heappop(self.heap) except IndexError: raise StopIteration logs = [ LogFile(/home/stephen/qa/ded1353/quick_log.gz), LogFile(/home/stephen/qa/ded1408/quick_log.gz), LogFile(/home/stephen/qa/ded1409/quick_log.gz) ] merged_log = merge(*logs) with open('/tmp/merged_log', 'w') as output: for stamp, line in merged_log: output.write(line) ... which probably won't preserve the order of log entries that have the same timestamp, but if you need it to -- should be easy to accommodate. HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Not workin!
Luke Paireepinart wrote: In this case you are saying is their input equal to this list with many elements? and the answer is always going to be No because a string won't be equal to a list unless both are empty. I know you probably didn't mean this as it reads, or as I'm reading it, but an empty string and an empty list aren't 'equal' either. In [1]: '' == [] Out[1]: False HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Confusion with $PATH
Wayne wrote: On Fri, Sep 25, 2009 at 11:32 AM, David Eric cii...@gmail.com mailto:cii...@gmail.com wrote: ok this is really weird . . . i tried running the helloworld again, and it doesnt work :/ DTm:~ davidteboul$ echo $PATH PATH$/Library/Frameworks/ Python.framework/Versions/3.1/bin:/Library/Frameworks/Python.framework/Versions/3.0/bin:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin/Users/davidteboul/bin/pythonStuff Also, it looks like you missed a colon, to separate the newly added path from the last one in the list. {...}/sbin:/usr/local/bin:/usr/X11/bin:/Users/davidteboul/bin/pythonStuff ^ HTH, Marty ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: FWIW: When using relative paths I got extra ../../ terms, so I changed join_relative() to: def join_relative(base, path): return os.path.normpath(os.path.join(script_dir(base), path)) Seems to work... Yeah, good catch ... looks great, and thanks for sharing your mod. Glad I could return the favor. :) Since then, I took it a bit further for use in my code... I am not finished with it yet, but am curios what you think. (I am not sure named things right or structured it logically) ### import os class HomePath(object): For finding paths based on a home/install directory. def __init__(self, home_dir = None, home_file = None): Must be called with either a path to a directory or, as a shortcut, a file in that directory. if home_file != None: # Set home based on a path to a file in its directory self.home = os.path.normpath(self.fix_path(home_file)) elif home_dir != None: # Set home based on its path self.home = os.path.normpath(self.get_dir(home_dir)) else: raise Exception(Must call with either a path to a directory or, as a shortcut, a file in that directory.) def abs(self, rel_from_home): Return an absolute path when passed a path relative to home. return self.join_relative(self.home, rel_from_home) def fix_path(self, base): return os.path.realpath(os.path.abspath(base)) def get_dir(self, base): return os.path.dirname(self.fix_path(base)) def join_relative(self, base, path): return os.path.normpath(self.fix_path(os.path.join(self.home, path))) # I'm not sure I understand your end goal, given the args to __init__. I'd guess, perhaps incorrectly, that you may be trying to shoehorn conflicting purposes. If your only aim is to calculate an absolute path to a resource's parent, or home, folder then your class can be simplified a lot, IMHO. Specifically, I don't see a reason to differentiate between home_dir and home_file. Please feel free to clarify if I'm missing the point. import os class HomePath(object): def __init__(self, path): # fix_path and get_dir on base path self.home = os.path.dirname( os.path.realpath( os.path.abspath(path) ) ) def join(self, relative_path): return os.path.normpath(os.path.join(self.home, relative_path)) Also, have a look at http://docs.python.org/library/os.path.html#os.path.abspath ... the main point is that your initial path element must have enough information in it to construct an abspath, otherwise os.getcwd() can pollute the calculation. I believe if you use __file__ you're good, and sys.argv[0], while potentially relative in nature should always be relative to os.getcwd() AFAICT, so you're good there too. Otherwise avoid constructing a HomePath object with a relative start path. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: What is the recommended way to configure my application find the various database and/or configuration files it needs? Recommemded by whom? A lot depends on the OS. Apple for example have one set of recommendations for MacOS, Windows has another and Linux has several to choose from! Thank you good point. Planning on using a ConfigParser based .ini file located in the ./config folder. For instance my folder layout: /path_to_app/app.py /path_to_app/lib/ /path_to_app/database/ /path_to_app/config/ /path_to_app/photos and so on. I would like to make an .ini in the config folder Seems fair enough, however on a Unix system you should also consider allowing the user to have their own personalised version in their home directory. Thus at startup you get the current user ID / home directory and look for a suitable config file. If it exists read it, if not read the default one in your config directory. 1) How does my main app file find the config file in the first place? Generally use a relative path so normally your app will run from its home folder so you can look in ./config. You might also set a system environment variable - for example the CLASSPATH or PYTHONPATH variables, or the ORACLE_HOME used by Oracle for their database. If the environment var is not set then look in ./config Assuming the application could be invoked in odd ways that may alter the notion of the current working directory, how do I unambiguously find the absolute path to the current python source file? (So I can load the nearby .ini) I use a helper function that calculates the absolute path of my app script, then I am able to os.path.join relative elements to my heart's content. I haven't had a problem with this approach so far, which of course doesn't mean it's the right way, or even a correct implementation for that matter. Something like this ... # lib/mypaths.py # -- import os def script_path(base): return os.path.realpath(os.path.abspath(base)) def script_dir(base): return os.path.dirname(script_path(base)) def join_relative(base, path): return os.path.join(script_dir(base), path) # app.py # -- ... from lib.mypaths import join_relative print join_relative(__file__, 'config/prefs.ini') # this may work when calling from # other modules, I suppose (untested) import sys print join_relative(sys.argv[0], 'config/prefs.ini') ... HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: snip As a follow-up question, how do give my modules stored under ./lib access to the data in my ConfigParser object? (For instance, database connection string, storage path, etc.) I guess a global ConfigParser object would work, but that seems wrong. And yet, to me it seems wrong to have more than one instance of config data floating around. Instead of using a global you can pass the config object, or just the appropriate attributes, around to your lib functions/classes, as necessary -- and keep the flow in your main script. For example (highly speculative, FWIW), # app.py from database.connection import getcursor from lib.persist import Storage def main(): confp = ConfigParser() confp.read(join_relative(__file__, 'config/prefs.ini')) config = confp.defaults() curs = getcursor(config[db_connection_string]) ... storage = Storage(config[storage_path]) ... HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: Something like this ... # lib/mypaths.py # -- import os def script_path(base): return os.path.realpath(os.path.abspath(base)) def script_dir(base): return os.path.dirname(script_path(base)) def join_relative(base, path): return os.path.join(script_dir(base), path) # app.py # -- ... from lib.mypaths import join_relative print join_relative(__file__, 'config/prefs.ini') # this may work when calling from # other modules, I suppose (untested) import sys print join_relative(sys.argv[0], 'config/prefs.ini') ... FWIW: When using relative paths I got extra ../../ terms, so I changed join_relative() to: def join_relative(base, path): return os.path.normpath(os.path.join(script_dir(base), path)) Seems to work... Yeah, good catch ... looks great, and thanks for sharing your mod. Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: Martin Walsh mwa...@mwalsh.org Allen Fowler wrote: As a follow-up question, how do give my modules stored under ./lib access to the data in my ConfigParser object? (For instance, database connection string, storage path, etc.) I guess a global ConfigParser object would work, but that seems wrong. And yet, to me it seems wrong to have more than one instance of config data floating around. Instead of using a global you can pass the config object, or just the appropriate attributes, around to your lib functions/classes, as necessary -- and keep the flow in your main script. For example (highly speculative, FWIW), # app.py from database.connection import getcursor from lib.persist import Storage def main(): confp = ConfigParser() confp.read(join_relative(__file__, 'config/prefs.ini')) config = confp.defaults() curs = getcursor(config[db_connection_string]) ... storage = Storage(config[storage_path]) I hear your point. It makes sense. The above sample code seems like the way to go... if I can structure my modules like that. For an object that needs many settings, what about passing in an instance of ConfigParser? (Or should I extract the settings to a dict, first?) Thank you again, :) I don't see a problem with either of those approaches -- I suppose it depends a little on the kind of config data you're working with, the overall design of your app, which approach would be easiest to maintain. If your intent is to package software that the general public will use, then I believe Alan and Dave have correctly advised to follow the packaging conventions for your target platform(s), and to be consistent with user expectations (ie. how do popular apps typically handle config?) I tend to write a lot of system automation scripts (sysadmin-style), where I'm one of the few internal end users, and my toolbox module contains wrappers for the various sources of config data (configparser, optparse, os.environ, etc) each providing a common interface that I like to use (and remember easily!) for dealing with config data in new function and class definitions. But I use these a lot, and wouldn't necessarily recommend the approach if you only need to consume config data now and again. I think the point I may be trying to make is that it took me a while to settle, after much trial and error, to find the approach that works best for me -- others will have a different story to tell, and I hope they do! :) Good luck! Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to change the working directory in IDLE
Martin Walsh wrote: Elisha Rosensweig wrote: Hi Tutors, Im using Python 2.6.2 and the IDLE tool (also v. 2.6.2). However, when I open the editor I cannot seem to change the directory so as to allow for easy access to my modules. So, for example, the following occurs: os.chdir('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') os.getcwd() '/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests' import CSITest Traceback (most recent call last): File pyshell#9, line 1, in module import CSITest ImportError: No module named CSITest What am I doing wrong? http://docs.python.org/tutorial/modules.html#the-module-search-path You probably want to append to sys.path in your script, or to the PYTHONPATH environment variable, instead of using os.chdir. Sorry, 'in your script' should read something like 'in Idle'. Here's another doc reference. http://docs.python.org/tutorial/modules.html#tut-standardmodules import sys sys.path.append('/Users/elisha/Documents/workspace/CacheNetFramework/src/Tests') import CSITest HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Help..Concatenaton Error
ayyaz wrote: Randy Trahan wrote: Attached is an error I cannot get to work, I was doing a print concatenation but it won't let me get past + ibly impressive. \ (then to next line) Also Programming Lanquage Question: I have studied and an fairly proficient at XHTML and CSS, I tried Javascript but just didn't like it for some reason..so I am trying Python which so far fits my personality, needs, whatever that part is that makes you choose a lanquage. Will I be able to use Python in web pages as I would of used Javascript? From what I have read there are Python to Javascript converters?... The only python to javascript project I am aware of is pyjamas (http://pyjs.org/), which is a python port of the Google Web Toolkit. Not sure if that's exactly what you're looking for. There are several javascript libraries available now-a-days that help make programming javascript more like programming in python. Mochikit comes to mind (http://www.mochikit.com/). But, I'm not a web developer. Thanks in advance.. -- Randy Trahan Owner, BullDog Computer Services, LLC 478-396-2516 Hello Randy, You have two plus signs after + ibly impressive. \. That might be the problem. And, it appears you've missed the quote before \nThis string , as well. Perhaps also important to note, the line continuation character '\' may not be followed by any character on the same line, including whitespace, so ... print \nThis string + \space may not ... where space indicates the presence of ... er, well ... a whitespace character, would be invalid (a SyntaxError). PEP8 (http://www.python.org/dev/peps/pep-0008/) outlines some alternatives to the line continuation character. For example, s = (This is a + ... long line + ... .) s 'This is a long line.' Or, you can leave out the +, as string concatenation is implicit in the following ... s = (This is a ... long line ... .) s 'This is a long line.' ... but, beware -- it's easy to overlook when constructing a list (or tuple). lst = ['This is a', ... 'long' # - missing comma ... 'line', ... '.'] lst ['This is a', 'longline', '.'] HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] recursive glob -- recursive dir walk
spir wrote: Hello, A foolow-up ;-) from previous question about glob.glob(). I need to 'glob' files recursively from a top dir (parameter). Tried to use os.walk, but the structure of its return value is really unhandy for such a use (strange, because it seems to me this precise use is typical). On the other hand, os.path.walk seemed to meet my needs, but it is deprecated. I often use Fredrik Lundh's implementation, when I need a recursive 'glob'. And even though it was contributed some time ago, it appears to be 3.x compatible. http://mail.python.org/pipermail/python-list/2001-February/069987.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How o convert spaces into tabs??
vince spicer wrote: regex will do it import re line = re.sub(r\s+, \t, line) print line The above replaces the newline, which reminds me that even seemingly trivial uses of 're' can become not-so-trivial in a hurry. In [1]: import re In [2]: line = '1 2 3 4 5\n' In [3]: re.sub('\s+', '\t', line) Out[3]: '1\t2\t3\t4\t5\t' Maybe this is closer to your intent, but I refuse to guarantee it ;) Better to stick with str methods whenever possible. In [4]: re.sub('[ ]+', '\t', line) Out[4]: '1\t2\t3\t4\t5\n' HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sqlite3 format data from tables
David wrote: David wrote: I have a budget program I am using to learn from. http://linuxcrazy.pastebin.com/f3b301daf I can not figure out how to get the transaction details to return so that it looks nice. It returns like this now. Your transaction History is: [(1, u'Food', -100), (2, u'Deposit', -200), (3, u'Deposit', 500), (4, u'Python Book', -50)] Here is how it gets to that point; def get_transactions(self): ct=self.connection.cursor() ct.execute(select * from transact;) return ct.fetchall() def report_transactions(self): return self.database.get_transactions() def display_transactions(self): print Your transaction History is:,self.bl.report_transactions() self.menu[5]=(Transaction History,self.display_transactions) Can I use the numbers 1,2,3,4 as a way to return the history? Thanks, I hope I explained it correctly :) -david Ok this seems to work OK; def display_transactions(self): transactions = self.bl.report_transactions() for data in transactions: print Transaction History: , data[-2], data[-1] It would be helpful if you showed the output you expect. Here is another option ... def display_transactions(self): transactions = self.bl.report_transactions() print Your transaction History is: for n, k, v in transactions: print ' %-15s %10.2f' % (k, v) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding difference in time
Kent Johnson wrote: On Fri, May 15, 2009 at 12:46 AM, R K wolf85boy2...@yahoo.com wrote: Gurus, I'm trying to write a fairly simple script that finds the number of hours / minutes / seconds between now and the next Friday at 1:30AM. I have a few little chunks of code but I can't seem to get everything to piece together nicely. import datetime,time now = datetime.datetime.now() i = 0 dayOfWeek = datetime.datetime.now().strftime( '%a' ) while dayOfWeek != 'Fri': delta = datetime.timedelta( days = i ) tom = ( now + delta ).strftime( '%a' ) if tom != 'Fri': i = i + 1 else: print i print tom break So with this code I can determine the number of days until the next Friday (if it's not Friday already). This could be simpler. I would write nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) while nextFriday.weekday() != 4: nextFriday += datetime.timedelta(days=1) Note the use of datetime attributes instead of relying on strftime(). What do you want the answer to be if you run the script at 1am Friday? at 2am Friday? If you want the next Friday in both cases, you could write this as nextFriday = datetime.datetime(now.year, now.month, now.day, 1, 30, 0) nextFriday += datetime.timedelta(days=1) # Make sure to get a Friday in the future while nextFriday.weekday() != 4: nextFriday += datetime.timedelta(days=1) I don't believe you'll get much better than that, IMHO. But, dateutil (3rd party) probably deserves a mention, at least, for this kind of job. I'm pretty sure the dateutil equivalent of Kent's second approach would look something like this, but I haven't thoroughly tested it, YMMV ... from datetime import datetime from dateutil.relativedelta import relativedelta, FR now = datetime.now() delta = relativedelta( days=1, weekday=FR, hour=1, minute=30, second=0, microsecond=0 ) nextFriday = now + delta print nextFriday ... dateutil totally rocks, and I hope someday soon it will be included in the standard library. http://labix.org/python-dateutil HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python popen command using cat textfile .... how to terminate
MK wrote: Hi there, i am using this code to send an cat ThisIsMyUrl with popen. Of cos cat now waits for the CTRL+D command. How can i send this command ? def console_command(cmd): print cmd console = os.popen(cmd,r) output = console.read() console.close() return output command=cat + working_dir + / + subdir + www.thisismyurl.com console_command(command) Ignoring the subprocess module for a moment, you could use os.popen2 instead of os.popen and then close the stdin fd to simulate a CTRL-D(EOF). stdin, stdout = os.popen2(cmd) stdin.write('This is line 1.\n') stdin.close() # CTRL-D With the subprocess module, you might do something like this (untested) ... from subprocess import Popen, PIPE console = Popen( cmd, shell=True, stdin=PIPE, stdout=PIPE, close_fds=True ) console.stdin.write('This is line 1.\n') console.stdin.close() # ... or just ... # console.communicate('This is line 1.\n') But, I can't figure out why you would want to do this. Or I should say it's not at all clear to me what you are trying to accomplish -- although I suspect you are making it difficult for yourself. Can you provide a brief explanation of what you are trying to achieve? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] returning the entire line when regex matches
Nick Burgess wrote: So far the script works fine, it avoids printing the lines i want and I can add new domain names as needed. It looks like this: #!/usr/bin/python import re outFile = open('outFile.dat', 'w') log = file(log.dat, 'r').read().split('Source') # Set the line delimiter for line in log: if not re.search(r'notneeded.com|notneeded1.com',line): outFile.write(line) There is a subtle problem here -- the '.' means match any single character. I suppose it's unlikely to bite you, but it could -- for example, a line containing a domain named notneeded12com.net would match. You should probably escape the dot, and while you're at it compile the regular expression. # untested pattern = re.compile(r'notneeded\.com|notneeded1\.com') for line in log: if not pattern.search(line): outFile.write(line) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add newline's, wrap, a long string
David wrote: Martin Walsh wrote: ... but, you don't need to use subprocess at all. How about (untested), # grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\ -f2 infof = open('/tmp/comprookie2000/emerge_info.txt') for line in infof: if 'USE' in line: USE = line.split('')[1] break else: USE = '' infof.close() Thanks Martin works perfect. So I understand correctly, line.split('') splits the list on so [1] grabs after USE = and up till [2] starts. That's right, in other words it emulates the behavior of 'cut -d\ -f2'. Python having 0-based indexing, you use 1 instead of 2, etc. Again thanks, I like the comments :) -david Welcome, glad it helped. Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add newline's, wrap, a long string
Sander Sweers wrote: 2009/4/29 David da...@abbottdavid.com: Here is the whole program so far, what it does is it logs into a druple web site and posts. I would like to make it better, as you can see I do the same thing over and over. http://linuxcrazy.pastebin.com/m7689c088 What you can do is define all the variables upfront. This way you can get rid of the else. Below is an example how you can do this with only looping once over the fle. CBUILD = '' ACCEPT_KEYWORDS = '' for line in fname: if 'ACCEPT_KEYWORDS' in line: output = line.split('')[1] ACCEPT_KEYWORDS = textwrap.fill(output, 80) if 'CBUILD' in line: output = line.split('')[1] CBUILD = textwrap.fill(output, 80) What is particularly important to note here is that Sander is iterating over the file 'fname' only once. When you loop thru the lines of a file with this form it is consumed, and you get nothing back for subsequent attempts, unless you re-open the file, or explicitly move back to the beginning, ie. fname.seek(0). HTH, Marty etc etc Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding mismatched or unpaired html tags
A.T.Hofkamp wrote: Dinesh B Vadhia wrote: I'm processing tens of thousands of html files and a few of them contain mismatched tags and ElementTree throws the error: Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, column 8 I now want to scan each file and simply identify each mismatched or unpaired tags (by line number) in each file. I've read the ElementTree docs and cannot see anything obvious how to do this. I know this is a common problem but feeling a bit clueless here - any ideas? Don't use elementTree, use BeautifulSoup instead. elementTree expects perfect input, typically generated by another computer. BeautifulSoup is designed to handle your everyday HTML page, filled with errors of all possible kinds. But it also modifies the source html by default, adding closing tags, etc. Important to know, I suppose, if you intend to re-write the html files you parse with BeautifulSoup. Also, unless you're running python 3.0 or greater, use the 3.0.x series of BeautifulSoup -- otherwise you may run into the same issue. http://www.crummy.com/software/BeautifulSoup/3.1-problems.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding mismatched or unpaired html tags
Dinesh B Vadhia wrote: A.T. / Marty I'd prefer that the html parser didn't replace the missing tags as I want to know where and what the problems are. Also, the source html documents were generated by another computer ie. they are not web page documents. My sense is that it is only a few files out of tens of thousands. Cheers ... Dinesh If this is a one time task, write a script to iterate over the html files, and collect the traceback info from those that throw a 'mismatched tag' error. Based on your example below, it appears to contain the line number. You'd only get one error per file per run, but you can run it until there are no errors remaining. I hope that makes sense. HTH, Marty Message: 7 Date: Tue, 28 Apr 2009 08:54:33 -0500 From: Martin Walsh mwa...@mwalsh.org Subject: Re: [Tutor] finding mismatched or unpaired html tags To: tutor@python.org tutor@python.org Message-ID: 49f70a99.3050...@mwalsh.org Content-Type: text/plain; charset=us-ascii A.T.Hofkamp wrote: Dinesh B Vadhia wrote: I'm processing tens of thousands of html files and a few of them contain mismatched tags and ElementTree throws the error: Unexpected error opening J:/F2/663/blahblah.html: mismatched tag: line 124, column 8 I now want to scan each file and simply identify each mismatched or unpaired tags (by line number) in each file. I've read the ElementTree docs and cannot see anything obvious how to do this. I know this is a common problem but feeling a bit clueless here - any ideas? Don't use elementTree, use BeautifulSoup instead. elementTree expects perfect input, typically generated by another computer. BeautifulSoup is designed to handle your everyday HTML page, filled with errors of all possible kinds. But it also modifies the source html by default, adding closing tags, etc. Important to know, I suppose, if you intend to re-write the html files you parse with BeautifulSoup. Also, unless you're running python 3.0 or greater, use the 3.0.x series of BeautifulSoup -- otherwise you may run into the same issue. http://www.crummy.com/software/BeautifulSoup/3.1-problems.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add newline's, wrap, a long string
David wrote: I am getting information from .txt files and posting them in fields on a web site. I need to break up single strings so they are around 80 characters then a new line because when I enter the info to the form on the website it has fields and it errors out with such a long string. here is a sample of the code; #!/usr/bin/python import subprocess import os u_e = subprocess.Popen( 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\-f2', Did you copy and paste this faithfully? The 'cut -d\\-f2' looks a bit odd. Is the delimiter a (double-quote)? Perhaps you left out a space before the -f2? shell=True, stdout=subprocess.PIPE,) os.waitpid(u_e.pid, 0) 'u_e.wait()' would wait the way you intend, as well, I believe. USE = u_e.stdout.read().strip() Or, if you use the communicate() method of the Popen object, the wait is implicit. As in, stdout, stderr = u_e.communicate() ... or perhaps ... USE = u_e.communicate()[0].strip() ... but, you don't need to use subprocess at all. How about (untested), # grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\ -f2 infof = open('/tmp/comprookie2000/emerge_info.txt') for line in infof: if 'USE' in line: USE = line.split('')[1] break else: USE = '' infof.close() L = len(USE) print L print USE L returns 1337 cosmic :) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Add newline's, wrap, a long string
David wrote: vince spicer wrote: first, grabbing output from an external command try: import commands USE = commands.getoutput('grep USE /tmp/comprookie2000/emege_info.txt |head -n1|cut -d\\-f2') then you can wrap strings, import textwrap Lines = textwrap.wrap(USE, 80) # return a list so in short: import commands, textwrap data = textwrap.wrap(commands.getoutput('my command'), 80) Vince Thanks Vince, I could not get command to work, but I did not try very hard; [cut: the delimiter must be a single character Try `cut --help' for more, 'information. head: write error: Broken pipe'] Ah, I see. This error is most likely due to the typo (missing space before -f2). But textwrap did the trick, here is what I came up with; #!/usr/bin/python import subprocess import os import textwrap import string def subopen(): u_e = subprocess.Popen( 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut -d\\ -f2', shell=True, stdout=subprocess.PIPE,) os.waitpid(u_e.pid, 0) USE = u_e.stdout.read().strip() L = textwrap.wrap(USE, 80) # return a list Lines = string.join(L, '\n') Just one more comment, string.join is deprecated, yet join is a method of str objects. So ... Lines = '\n'.join(L) ... or use textwrap.fill which returns a string with the newlines already in place ... Lines = textwrap.fill(USE, 80) HTH, Marty fname = 'usetest.txt' fobj = open(fname, 'w') fobj.write(Lines) fobj.close subopen() Here is the output; http://linuxcrazy.pastebin.com/m66105e3 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PDF to text conversion
Robert Berman wrote: Hello Emad, I have seriously looked at the documentation associated with pyPDF. This seems to have the page as its smallest element of work, and what i need is a line by line process to go from .PDF format to Text. I don't think pyPDF will meet my needs but thank you for bringing it to my attention. Thanks, Robert Berman Have you looked at pdfminer? http://www.unixuser.org/~euske/python/pdfminer/index.html Looks promising. HTH, Marty Emad Nawfal (عماد نوفل) wrote: On Tue, Apr 21, 2009 at 12:54 PM, bob gailer bgai...@gmail.com mailto:bgai...@gmail.com wrote: Robert Berman wrote: Hi, I must convert a history file in PDF format that goes from May of 1988 to current date. Readings are taken twice weekly and consist of the date taken mm/dd/yy and the results appearing as a 10 character numeric + special characters sequence. This is obviously an easy setup for a very small database application with the date as the key, the result string as the data. My problem is converting the PDF file into a text file which I can then read and process. I do not see any free python libraries having this capacity. I did see a PDFPILOT program for Windows but this application is being developed on Linux and should also run on Windows; so I do not want to incorporate a Windows only application. I do not think i am breaking any new frontiers with this application. Have any of you worked with such a library, or do you know of one or two I can download and work with? Hopefully, they have reasonable documentation. If this is a one-time conversion just use the save as text feature of adobe reader. My development environment is: Python Linux Ubuntu version 8.10 Thanks for any help you might be able to offer. Robert Berman ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org mailto:Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I tried pyPdf once, just for fun, and it was nice: http://pybrary.net/pyPdf/ -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي No victim has ever been more repressed and alienated than the truth Emad Soliman Nawfal Indiana University, Bloomington ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sending a disconnect after openssl s_client command?
Kayvan Sarikhani wrote: On Mon, Apr 20, 2009 at 1:17 PM, Martin Walsh mwa...@mwalsh.org mailto:mwa...@mwalsh.org wrote: from subprocess import Popen, PIPE openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' openssl = Popen( openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE ) stdout, stderr = openssl.communicate('GET /') Alternatively, if you're using python 2.6 and above, it looks like you can do something similar with a few lines of code, and the ssl module from the standard lib ... # untested! import ssl try: cert = ssl.get_server_certificate( ('somewebsitename', 443), ssl.PROTOCOL_SSLv2 ) except ssl.SSLError, ex: # site may not support sslv2 ... HTH, Marty Thanks Marty; this does indeed help...it just also means I need to really learn how subprocess works. ;) I wish I could claim to be using 2.6, but unfortunately the most current version at work is Python 2.5.2...most boxes here are even below, and I can't convince them to upgrade. Ah, well. Yep, subprocess is the way to go. In that case, if you're not offended by the extra dependency, then you might be interested in http://pypi.python.org/pypi/ssl, which appears to be a backport of the 2.6 ssl module. I haven't tried it myself, but it has a get_server_certificate helper also, so I'd expect it to work the same way. Although, you'll probably want to explore in greater detail the properties of the exception that is raised by a site not supporting sslv2. When I tried I received an SSLError(errno=6) for a server configured w/o sslv2. Thanks again though! You're welcome, glad it helped. :) Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Sending a disconnect after openssl s_client command?
Kayvan Sarikhani wrote: Tutors, I'm working on a script to verify whether a particular website supports SSLv2 via the following: --- BEGIN --- #!/usr/bin/python import os, re checkssl_out = open('checkssl.txt','w') website = 'somewebsitename' sslv2 = 'Protocol : SSLv2' print 'Checking:', website checksslv2 = os.popen('openssl s_client -ssl2 -connect somewebsitename:443').read().strip() if re.search(sslv2, checksslv2) == None: print checkssl_out, website, 'does NOT support SSLv2' else: print checkssl_out, website, 'supports: SSLv2' checkssl_out.close() --- END --- It works, but the problem is that OpenSSL does not automatically disconnect after end of input. I was curious if there's a way to send a CTRL-C at the end of the command, so that it *does* capture the output, and breaks after it. Any suggestions or help is appreciated! You can do something like the following (untested) to simulate a CTRL-C, but I'd recommend against it, as I doubt it will work as you expect ... import os, signal from subprocess import Popen, PIPE openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' openssl = Popen(openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE) os.kill(openssl.pid, signal.SIGINT) # dead, I bet, before any output is generated stdout, stderr = openssl.communicate() Instead, you may want to try to mimic this command-line behavior ... echo GET / | openssl s_client -ssl2 -connect somewebsitename:443 ... in which case, you can try something like this ... from subprocess import Popen, PIPE openssl_cmd = 'openssl s_client -ssl2 -connect somewebsitename:443' openssl = Popen( openssl_cmd, shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE ) stdout, stderr = openssl.communicate('GET /') Alternatively, if you're using python 2.6 and above, it looks like you can do something similar with a few lines of code, and the ssl module from the standard lib ... # untested! import ssl try: cert = ssl.get_server_certificate( ('somewebsitename', 443), ssl.PROTOCOL_SSLv2 ) except ssl.SSLError, ex: # site may not support sslv2 ... HTH, Marty K ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String Encoding problem
Matt wrote: Hey everyone, I'm hoping someone here can help me solve an odd problem (bug?). I'm having trouble with string encoding, object deletion, and the xml.etree library. If this isn't the right list to be posting this question, please let me know. I'm new to Python and don't know of any other help me Python mailing lists. I have tried debugging this ad-infinitem. Anyway, at the bottom of this e-mail you will find the code of a python file. This is a gross over-simplification of my code, with little exception handling so that the errors are obvious. Running this interactively, if you finish off with 'del db', it exits fine and creates a skeleton xml file called 'db.xml' with text 'root /'. However, if you instead CTRL-D, it throws at exception while quitting and then leaves an empty 'db.xml' which won't work. Can anyone here help me figure out why this is? Stuff I've done: I've traced this down to the self.commit() call in __del__. The stacktrace and a few print statements injected into xml.etree leads me to the call 'root'.encode('us-ascii') throwing a LookupError on line 751 of xml.etree.ElementTree. This makes no sense to me, since it works fine normally. The environment available to __del__ methods during program termination is wonky, and apparently not very consistent either. I can't say that I completely understand it myself, perhaps someone else can provide a better explanation for both of us, but some of the causes are described in the documentation: http://docs.python.org/reference/datamodel.html#object.__del__ What is your rationale for using __del__? Are you trying to force a 'commit()' call on Database instances when your program terminates -- in the case of an unhandled exception, for example? HTH, Marty Thank you very much. Any and all help or pointers are appreciated. ~Matt db.py ### from xml.etree import ElementTree as ET import os class Database(object): def __init__(self, path): self.__dbpath = path## Path to the database self.load() def __del__(self): ## FIXME: Known bug: ## del db at command line works properly ## Ctrl-D, when there is no db file present, results in a LookupError ##and empty xml file from StringIO import StringIO from traceback import print_exc trace = StringIO() try: print 5 self.commit() print 7 except Exception: print_exc(100, trace) print trace.getvalue() def load(self): if os.path.exists(self.__dbpath): self.root = ET.parse(self.__dbpath).getroot() else: self.root = ET.Element(root) def commit(self): ET.ElementTree(self.root).write(self.__dbpath) db = Database('db.xml') ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] PyCon Videos
Hi All, Not sure if it's common knowledge, particularly for those who didn't make it to PyCon this year, but all of the talks were recorded and will be available online in good time, thanks to Carl Karsten and his merry band of A/V volunteers. I can't even begin to grasp how much work is required to accomplish such a monumental task. I noticed this morning that the tutorials, are making their way to the blip.tv site, including Internet Programming with Python presented by our very own Wesley Chun. And I presume An Introduction to Object-Oriented Programming, announced on this list a couple months ago by Michael Goldwasser, will be available soon as well. I'm really looking forward to watching both. Not a replacement for attending a PyCon in person -- which I highly recommend for anyone interested in python, no matter your skill level -- but certainly the next best thing. http://pycon.blip.tv/posts?view=archivensfw=dc It seems, many presenters included links to additional material (code examples, slides, etc) on the scheduled-talks page [1] -- not sure about the tutorials [2], but it doesn't look like it. [1] http://us.pycon.org/2009/conference/schedule/ [2] http://us.pycon.org/2009/tutorials/schedule/ HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list to string and string to list
johnf wrote: On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote: johnf jfabi...@yolo.com wrote I want to save the list to the field and when I retrieve the string convert it back to a list. But this does NOT work. mylist=[1,2,3,4] mystr=str(mylist) newlist= list(mystr) I keep thinking there must be a simple way of get this done. Is this a good way? newlist = eval(mystr) eval has all sorts of security implications so I wouldn't recommend it where you are reading data fropm an external source. One thing that might work is this: L = [1,2,3,4,5] s1 = ','.join(str(n) for n in L) s1 '1,2,3,4,5' newlist = [int(n) for n in s1.split(',')] newlist [1, 2, 3, 4, 5] Provided your original data doesn't have commas to start with it should work, I think... And the data needs to be pretty homogenous to allow a single conversion function. Kent Johnson suggested newlist = map(int, mystr[1:-1].split(',')) Maybe that's a question in disguise, but I would think both suggestions are valid. Another, less valid, suggestion would be to pickle or shelve the list before storing it in the database -- although this poses similar security implications as eval. And the resulting string is not particularly easy to read, if that's important to you. import cPickle as pickle mylist = [1,2,3,4] mystr = pickle.dumps(mylist) # '(lp1\nI1\naI2\naI3\naI4\na.' newlist = pickle.loads(mystr) # [1, 2, 3, 4] HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list to string and string to list
Martin Walsh wrote: johnf wrote: On Thursday 16 April 2009 05:04:39 pm Alan Gauld wrote: johnf jfabi...@yolo.com wrote I want to save the list to the field and when I retrieve the string convert it back to a list. But this does NOT work. mylist=[1,2,3,4] mystr=str(mylist) newlist= list(mystr) I keep thinking there must be a simple way of get this done. Is this a good way? newlist = eval(mystr) eval has all sorts of security implications so I wouldn't recommend it where you are reading data fropm an external source. One thing that might work is this: L = [1,2,3,4,5] s1 = ','.join(str(n) for n in L) s1 '1,2,3,4,5' newlist = [int(n) for n in s1.split(',')] newlist [1, 2, 3, 4, 5] Provided your original data doesn't have commas to start with it should work, I think... And the data needs to be pretty homogenous to allow a single conversion function. Kent Johnson suggested newlist = map(int, mystr[1:-1].split(',')) Maybe that's a question in disguise, but I would think both suggestions are valid. Another, less valid, suggestion would be to pickle or shelve the list before storing it in the database -- although this poses similar security implications as eval. And the resulting string is not particularly easy to read, if that's important to you. import cPickle as pickle mylist = [1,2,3,4] mystr = pickle.dumps(mylist) # '(lp1\nI1\naI2\naI3\naI4\na.' newlist = pickle.loads(mystr) # [1, 2, 3, 4] Sorry to reply to my own post, but the json module in python 2.6+ (formerly 3rd party, simplejson) might work for your purposes also. But I must confess, I'm not that familiar. import json s = json.dumps([1, 2, 3, 4]) # '[1, 2, 3, 4]' l = json.loads(s) # [1, 2, 3, 4] HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Executing a C Program from RH Linux in Python for Win
Wayne Watson wrote: If you can execute a C program compiled on a Linux with SWIG, then that's what I'm looking for. There's really no RH dependency according to the above posts. If it were compiled on Debian or Ubuntu, it appears it would not make any difference. That is, one could execute a RH executable from C on Ubuntu. Yeah, probably -- if it's a static build, or if the dependencies (required libraries/versions) are installed, assuming the program has dependencies. But, I suppose we may be drifting a bit OT. Is there a simple example of this in action from a Python program and some small C Linux executable program? http://docs.python.org/library/subprocess.html The subprocess module is commonly recommended for this type of task (as opposed to os.system, etc). In fact, I believe Alan already suggested it in this thread. And speaking of ... Alan's tutorial has several very good examples of using the subprocess module (OS topic under Manipulating Processes). http://www.freenetpages.co.uk/hp/alan.gauld/tutos.htm --- I'll hazard a wild guess that you don't really want SWIG based on your original question, and subsequent comments. IIUC, SWIG is intended to ease the creation of a python wrapper (extension module) for existing C/C++ code. And, I'm not sure you've given enough information about the C program to determine if SWIG would be useful. Regardless, I suggest you get a feel for running an external program using python first. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Grayson's Example 5_14
Martin Walsh wrote: Wayne Watson wrote: it. It works pretty well, but puts up a a few probably top level windows that are blank. How do I get around them, and is there anything root = Tk() Try adding this, root.withdraw() dialog = GetPassword(root) HTH, Marty Wayne Watson wrote: That worked. Why the extra blank window though? I'm no Tkinter expert -- far from it. But I would assume that GetPassword, as a subclass of Dialog, creates a new window, and thus you have no need for the root window. root.withdraw() hides the root window from view, and allows the application to continue running. I imagine there are other ways to accomplish the same thing. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fun with Label and Entry--Why NoneType?
Wayne Watson wrote: The program below is derived from an example in Grayson for showing how one might a dialog for entering passwords. The structure seems just like the original, down to the use of self, Label and Entry plus . Yet the print here statement produces: here None type 'NoneType' I'm missing something. The NoneType causes the print of the self.lat to fail with get(). The original returns something from the corresponding body function, but taking it out does nothing in either program. The original program is posted above under Modifying Grayson's Example 5_14. # Derived from Grayson 5_14.py from Tkinter import * from tkSimpleDialog import Dialog import tkSimpleDialog import tkMessageBox #import Pmw class DialogPrototype(Dialog): def body(self, master): self.title(Enter Site Data) Label(master, text='Latitude:').grid(row=0, sticky=W) self.lat=Entry(master, width=12).grid(row=0, column=1) This is where you diverge from the Grayson example. What you're saying is that self.lat should be set to the result of Entry(...).grid(...) which is always None, presumably. What I think you want is self.lat to be a reference to the Entry widget itself. Try this, self.lat = Entry(master, width=12) self.lat.grid(row=0, column=1) Label(master, text='Longitude:').grid(row=0, column=2) self.long=Entry(master, width=12).grid(row=0, column=3) ... and, self.long = Entry(master, width=12) self.long.grid(row=0, column=3) print here, self.long,type(self.long) return def apply(self): print apply print self.lat.get() print self.long.get() print setting lat=1.0 long=0.0 Is that the way you indented the above, really? I suppose it could be intentional, but probably not. root = Tk() root.withdraw() dialog = DialogPrototype(root) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Grayson's Example 5_14
Wayne Watson wrote: ... it. It works pretty well, but puts up a a few probably top level windows that are blank. How do I get around them, and is there anything ... root = Tk() Try adding this, root.withdraw() dialog = GetPassword(root) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] merging dictionary values based on key
greg whittier wrote: On Thu, Mar 12, 2009 at 4:24 PM, ski nor...@khine.net wrote: mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c': 'a23'}, {'a': 'x234', 'c': 'XX123'} ] where mylist has nth number of dictionaries and i want to merge the values of the keys that are the same? If I understand what you mean by merging, I think you want mylist = [{'a': 'x123', 'b':'12'}, {'a': 'x234', 'b': 'd33', 'c': 'a23'}, {'a': 'x234', 'c': 'XX123'} ] merged_dict = {} for dictionary in mylist: for key, value in dictionary.items(): merged_dict.setdefault(key,[]).append(value) Or similarly with defaultdict: from collections import defaultdict merged_dict = defaultdict(list) for d in mylist: for k, v in d.items(): merged_dict[k].append(v) ski wrote: how would you do this for a specific key instead of all the keys? alist = [d['a'] for d in mylist if d.has_key('a')] HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] probelm pyhton shell doesnt open help please
mustafa akkoc wrote: it gives this message socket error image text IDLE's subprocess didn't make a connection. Either IDLE can't start a subprocess or personal firewall software is blocking the connection. /image text IIRC, this was once a known issue with IDLE when combined with the windows firewall service, or when running multiple instances of IDLE (perhaps inadvertently). But, I'm having difficulty tracking down the pertinent bug report(s) -- maybe these have been fixed? or I'm just tired, probably the latter. A couple of suggestions from memory ... 1. Check the process/task list for errant pythonw.exe processes, and end them. 2. Launch IDLE with the -n flag from a terminal (command prompt). 3. Report back to the list with your results, and include the python and windows version info if you continue to have trouble. HTH, Marty PS. Keep in mind, some of us won't see images you post to the list so you should generally include a text version of error messages whenever possible. Or at least, note when you've included an image so that those with sufficient interest can make the extra effort to view it, if necessary. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] re.format a file
A.T.Hofkamp wrote: prasad rao wrote: helloThank you Lie and Kent. I forgot about newline character and the fact that string can be sliced. Thanks for your timely help BTW I have gone through the Python library reference and find no examples in fileinput module. The fileinput module only deals with reading and writing data from/to files, it does not deal with manipulating that data. How to manipulate strings is in the 'strings' or 'text' section of a tutorial. z=fileinput.input(file,inplace=1) for line in z: ???if len(line)60:pass ???if len(line)60: ??line=line[:60]+'\n'+line[60:] Is it the right way to do? A nice step forward, I'd say. Did you consider what to do with ridiculous long lines, eg 200, 500, 1000 or 1 characters long? If you want to deal with them, you'd need to repeatedly split the line. You could use a while loop for it. Or if the lines resemble paragraphs, then one might use the textwrap module which breaks on word boundaries by default I think, and provides several options for tweaking -- perhaps not what the OP is looking for. # untested import textwrap for line in z: line = textwrap.fill(line, width=60) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple CGI script and Apache configuration
wormwood_3 wrote: I wasn't sure if that was needed, so I took it out, sorry about that. I put ScriptAlias /cgi-bin/ /var/www/samuelhuckins.com/cgi-bin/ in place, reloaded, and it works! I think the problem throughout was that I was mixing up what was necessary between CGI and mod_python. The apache2 documentation isn't clear about the relationship between ExecCGI and ScriptAlias (rather, it's not clear to me) and unless I've missed something, seems to imply that either ScriptAlias or ExecCGI alone should be sufficient. Unfortunately, I don't have time to experiment. IIRC, all of the vanilla apache configs I've worked with recently include both in definitions for cgi-bin. In any case, glad it worked. If you'd like a random programming epigram served up by this new config, check out: http://samuelhuckins.com/cgi-bin/qotd.py Very cool, thanks. -Sam wormwood_3 wrote: Thanks for all the suggestions! I tried to go through them, and will relate what results I encountered. I changed my Apache config to: Directory /var/www/samuelhuckins.com/cgi-bin/ AllowOverride None Options ExecCGI Order allow,deny Allow from all /Directory I in fact did not have the cgi module enabled, so I did that. Then I ran sudo /etc/init.d/apache2 reload, and hit http://samuelhuckins.com/cgi-bin/hello.py, which contains simply: #!/usr/bin/python print Content-type: text/html print print html print centerHello!/center print /html I get prompted to download the file, but it does not execute or appear in plain text. The logs just show the request being made. What is the missing element to get this script to execute? When you look at the downloaded file, is it your python script? Looks like you changed the path where you're keeping your cgi script, did you update the ScriptAlias directive to suit? You may find this more helpful ... http://httpd.apache.org/docs/2.0/howto/cgi.html ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple CGI script and Apache configuration
wormwood_3 wrote: Hello all, Hi Sam, I'll try to give as much detail as I can, but this is a somewhat vague problem. I have a very simple script that I would like to implement as a CGI script, just so I can hit a URL and get some output. However, after following a number of tutorials, I am still seeing some very odd results. I am almost sure it's in my Apache configuration, but I figured a few people on this list would likely know what the minimal related Apache config should be. (The Apache docs are pretty daunting...) Local version wise, I am on Ubuntu 8.10, with Apache 2.2.9 and Python 2.5.2. I have libapache2-mod-python installed. Apache config is out of the box, along with: ScriptAlias /cgi-bin/ /var/www/cgi-bin/ You need more than this to make apache cgi work, I think. Firstly, mod_cgi should be loaded -- look for a symlink named cgi.load or some such, in /etc/apache2/mods-enabled. Run 'a2enmod cgi' if you don't have one. Secondly, ExecCGI is usually enabled using the Options directive within a Directory definition -- but, less commonly, you might see something like 'AddHandler cgi-program .py' in an apache or site config. Of course, the script needs to be executable by the apache user (which would be 'www-data' on ubuntu, IIRC), and contain an appropriate shebang (#!) on the first line -- but it sounds like you have that covered. Both 'Options ExecCGI' and 'Addhandler cgi-program .py' are allowed in .htaccess also, given an appropriate AllowOverride directive for the path in question. Something to look for on the working system, if all else fails. You do *not* need mod python to run python cgi scripts. In /var/www/cgi-bin, I have hello.py http://hello.py: #!/usr/bin/python import cgitb cgitb.enable() print Content-type: text/html print print html print centerHello!/center print /html Reload, hit http://localhost/cgi-bin/hello.py in a browser, I get centered text just fine. Now I want to do this same process on my remote webserver. On there, I am on Ubuntu 7.10, with Apache 2.2.4 and Python 2.5.1. I add: ScriptAlias /python/ /var/www/samuelhuckins.com/python You can try appending something like this (untested): Directory /var/www/samuelhuckins.com/python/ AllowOverride None Options ExecCGI # or, Options +ExecCGI to merge # with options from parent dir(s) Order allow,deny Allow from all /Directory Reload, hit http://samuelhuckins.com/python/hello.py, and I get a 404? The perms and ownership on the file is the same as in other directories. Do I need to add some sort of handler, with mod_python.publisher? I think I am just missing one of the basics of this whole process. Hmmm, interesting. It's unlikely that any of my advice will help you with a 404. With an incomplete apache cgi config, the response I'd expect would be either a 403 (Forbidden), or the script itself in plain text. Do the logs provide any additional information? Re-check your spelling. A 404 with vanilla apache config might just indicate a typo. When you say 'Reload', I assume you mean the apache daemon (ie. /etc/init.d/apache2 reload or apache2ctl reload)? Again, you do *not* need mod python to run python cgi scripts. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Simple CGI script and Apache configuration
wormwood_3 wrote: Thanks for all the suggestions! I tried to go through them, and will relate what results I encountered. I changed my Apache config to: Directory /var/www/samuelhuckins.com/cgi-bin/ AllowOverride None Options ExecCGI Order allow,deny Allow from all /Directory I in fact did not have the cgi module enabled, so I did that. Then I ran sudo /etc/init.d/apache2 reload, and hit http://samuelhuckins.com/cgi-bin/hello.py, which contains simply: #!/usr/bin/python print Content-type: text/html print print html print centerHello!/center print /html I get prompted to download the file, but it does not execute or appear in plain text. The logs just show the request being made. What is the missing element to get this script to execute? When you look at the downloaded file, is it your python script? Looks like you changed the path where you're keeping your cgi script, did you update the ScriptAlias directive to suit? You may find this more helpful ... http://httpd.apache.org/docs/2.0/howto/cgi.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Changing the Attribute of a Variable
Marc Tompkins wrote: Also - config_var_list is a tuple of lists. (I'm guessing you intended to make it a list of lists - that's what the name indicates, after all - but putting it between ( ) makes it a tuple.) Sound advice, but a subtle clarification is warranted I think. It's the comma(s) that make a tuple not the parens (and an absence of square brackets, I suppose). Consider the following: In [1]: a = (1) In [2]: a Out[2]: 1 In [3]: type(a) Out[3]: type 'int' In [4]: b = (1,) In [5]: b Out[5]: (1,) In [6]: type(b) Out[6]: type 'tuple' In [7]: c = 1, 2, 3 In [8]: c Out[8]: (1, 2, 3) In [9]: type(c) Out[9]: type 'tuple' ... Wayne, I second Marc's advice that you're making it hard on yourself. Understandable to a degree, if you are trying to avoid major modification to inherited code. But, loading and saving configuration data is a 'solved' problem in that there are many choices of ready-made tools to help you accomplish the task. And, I don't see anything in your description that would indicate a custom solution is necessary -- except perhaps for the learning experience, almost never a bad idea, IMHO. Marc has already pointed out ConfigObj, which is excellent, but I thought I would also suggest ConfigParser -- part of the standard lib, if a bit less feature-full. Back to your original question, and I'm probably overstating the obvious at this point, but the underlying problem is that the operation causing the exception is expecting a datetime.time object and getting a str. So, it's less about adding a strftime attribute to the str object, and more about 'converting' the str into a datetime.time object. Short of re-writing for ConfigObj (which provides a type conversion and validation mechanism), or pickle, or similar -- you'll need to work out how to convert between str and datetime.time. Here are some (untested) examples: def time_to_str(t): return t.strftime('%H:%M:%S') def str_to_time(s): h, m, s = [int(u) for u in s.split(':')] return datetime.time(h, m, s) HTH, Marty PS. You can avoid posting images of tracebacks by enabling 'Quick Edit' mode in your windows command prompt. More info here: http://support.microsoft.com/kb/282301 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Reply All Dilemma of This List
Wayne Watson wrote: I belong to many, many forums, Yahoo Groups, (Usenet) newsgroups, and mail lists. Probably 100 or more. I think it's fair to say that none of them but this one has an implicit Reply All. For newsgroups and mail lists, I just press my Mozilla Seamonkey mailer Reply button and the resulting message is ready to be seen by everyone, a single address. Here a Reply goes only to the poster, none to Tutor. Elsewhere, for e-mail-like posts, if I really want to make a special effort to single out the poster too, Reply All works to additionally get it directly to them (actually they'd get two messages directly) and the entire list. For YGs and forums, the Reply All is implicit in a response. Since this group, in my world, is unique in these matters, I'll just offer the following header for a mail list I belong to, the ASTC, for someone's consideration. I suppose that someone might be whoever created this mail list. It' definitely different than used here, and no one uses Reply All to my knowledge. Maybe they can figure out if it has applicability here. This is a contentious topic which comes up at least once a year on this list. A search of the archives will turn up some interesting debate, most likely. FWIW, I like the behavior of this list as opposed to others. You may find these additional references illuminating ... you may not. http://effbot.org/pyfaq/tutor-why-do-my-replies-go-to-the-person-who-sent-the-message-and-not-to-the-list.htm http://woozle.org/~neale/papers/reply-to-still-harmful.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Want to write a Perl based IP to Domain Name converter.
wesley chun wrote: The script in the following can do the batch conversion from domain name to IP: This is a Python list, not Perl! OMG! It's my mistake, sorry for this. Lol..thats okay. Now that you are here and have seen what fun we have writing Python code - why not join the party? Don't be shy to join us :p - i bet you won't be sorry. well, he had to join Tutor to post, unless a mod allowed his msg, so there should be *some* interest in doing it in Python. after all, the equivalent code is already slightly shorter and easier to read than the Perl version... import socket import sys for hp in sys.argv[1:]: h, p = hp.strip().split(':') print '%s - %s:%s' % (hp, socket.gethostbyname(h), p) Here's my attempt, a few lines longer. The 'split /:/ or next' part confuses me a bit, though I suspect it's a bug ... 'cause I guess, as long as the line has value even if it doesn't match the host:port pattern, it won't be an exception, or undef, or false, or whatever you call it in perl. :D import socket import fileinput for line in fileinput.input(): line = line.rstrip('\n') # chomp try: # split /:/ host, port = line.split(':') except ValueError: continue # or next ip = socket.gethostbyname(host) print '%s - %s:%s' % (line, ip, port) ... and the opposite ... for line in fileinput.input(): line = line.rstrip('\n') try: ip, port = line.split(':') except ValueError: continue host = socket.gethostbyaddr(ip)[0] print '%s - %s:%s' % (line, host, port) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Date comparison across time zones
frenc1z 1z wrote: Hello, I would like to compare some dates (date+time really). The dates all have the following RFC2822 format: Eg. is d1 d2? d1 = Tue, 13 Jan 2009 03:27:29 -0800 d2 = Tue, 13 Jan 2009 02:40:00 -0600 My thinking is that I first need to make these two dates comparable, and eliminate the impact of time zone and dst. Understand that I can acheive this using strptime. Been trying to parse these dates as follows: print datetime.strptime(Tue, 13 Jan 2009 02:40:00 -0800, %a, %d %b %Y %H:%M:%S %z). I get the following error on the conversion ValueError: 'z' is a bad directive in format '%a, %d %b %Y %H:%M:%S %z'. I'm not sure how one would accomplish this with only the stdlib, but there are at least two 3rd party modules which you may find useful. dateutil: http://labix.org/python-dateutil mx.DateTime: http://www.egenix.com/products/python/mxBase/mxDateTime/ Each of these implement a timezone aware date-string parser, here is an (untested) example or two: d1 = Tue, 13 Jan 2009 03:27:29 -0800 d2 = Tue, 13 Jan 2009 02:40:00 -0600 from dateutil import parser dparser = parser.parser() p1 = dparser.parse(d1) p2 = dparser.parse(d2) print p1 p2 from mx.DateTime import DateTimeFrom m1 = DateTimeFrom(d1) m2 = DateTimeFrom(d2) print m1 m2 HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing string in place
Jon Crump wrote: Dear all, I've been around and around with this and can't seem to conceptualize it properly. I've got a javascript object in a text file that I'd like to treat as json so that I can import it into a python program via simplejson.loads(); however, it's not proper json because it has new Date() object declarations in it. So I thought to pre-process it by translating the dates into ISO format, but RE is making me cross-eyed. example string: s = {title : Hebertot, Normandie, start : new Date(1203,10,7), description : Hardy's long name: Hebertot, Normandie. lt;brgt; lt;img src=\document.png\ style=\cursor: pointer\ onclick=\SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false\/gt;pg.035: 1203-10-10 to 1203-11-18},{title : Newark-Upon-Trent, Nottinghamshire, start : new Date(1216,9,16), end : new Date(1216,9,18), description : Hardy's long name: Newark-Upon-Trent, Nottinghamshire. lt;brgt; } I can locate the dates with: jdate = re.compile('new Date\(\d{4},\d{1,2},\d{1,2}\)') I think you're pretty close... # note the extra parens jdate = re.compile('new Date\((\d{4}),(\d{1,2}),(\d{1,2})\)') ... then ... print jdate.sub(r'\1-\2-\3', s) {title : Hebertot, Normandie, start : 1203-10-7, description : Hardy's long name: Hebertot, Normandie. lt;brgt; lt;img src=document.png style=cursor: pointer onclick=SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false/gt;pg.035: 1203-10-10 to 1203-11-18},{title : Newark-Upon-Trent, Nottinghamshire, start : 1216-9-16, end : 1216-9-18, description : Hardy's long name: Newark-Upon-Trent, Nottinghamshire. lt;brgt; } You can also use named groups which *might* help make clearer what's happening above, something like: jdate = re.compile('new Date\((?PYear\d{4}),(?PMonth\d{1,2}),(?PDay\d{1,2})\)') print jdate.sub(r'\gYear-\gMonth-\gDay', s) More info here: http://docs.python.org/library/re.html#re.sub HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] changing string in place
Jon Crump wrote: I'm still faced with the problem of the javascript months being 0 indexed. I have to add 1 to group \2 in order to get my acurate date-string. Obviously I can't do print jdate.sub(r'\1-\2+1-\3', s) because the first argument to sub() is a string. How can I act on \2 before it's substituted for the matched string? Ah, sorry I missed that part the first time through. The first argument to jdate.sub ('repl') can also be a function that accepts an re.match object and returns a string, so something like the following (*untested*) may be helpful: jdate = re.compile('new Date\((\d{4}),(\d{1,2}),(\d{1,2})\)') def repldate(match): y, m, d = map(int, match.groups()) return '%04d-%02d-%02d' % (y, m+1, d) print jdate.sub(repldate, s) HTH, Marty Thanks again, Jon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Script to take a screenshot of my website.
Bryan Fodness wrote: I would like to take a screenshot of my website without opening the browser or importing extra packages. Is there a way to do this? Unless you're keen on writing your own html/css/javascript/etc rendering engine, I'm pretty sure you're going to need extra package(s), or a browser (even if opened w/o a visible ui). Perhaps these links will provide inspiration... http://lapin-blanc.net/09/11/2008/django-website-thumbnail-generator/ http://www.gruppo4.com/~tobia/pythumbnail.shtml HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess adds %0A to end of string
Hi David, David wrote: Hi everyone. Just learning :) I have one program to parse a podcast feed and put it into a file. Welcome! snip def getFeed(): url = raw_input(Please enter the feed: ) data = feedparser.parse(url) for entry in data.entries: sys.stdout = open(podcast.txt, a) You should probably try to avoid reassigning sys.stdout. This is usually a bad idea, and can cause odd behavior that is difficult to troubleshoot, especially for a beginner. A reasonable approach is to assign the open file object to a name of your own choosing... . podcast_file = open('podcast.txt', 'a') ... and then, use the write method of the file object ... . podcast_file.write('%s: %s' % (entry.updated, entry.link)) More info here: http://www.python.org/doc/2.5.3/tut/node9.html#SECTION00920 print '%s: %s' % (entry.updated, entry.link) sys.stdout.close() for entry in data.entries: sys.stdout = open(podcast_links.txt, a) print '%s' % (entry.link) sys.stdout.close() getFeed() next to get the latest item; snip lname = podcast_links.txt L = open(lname, 'r') print The Latest Link\n download = L.readline() The readline method returns a line from the file *including* the newline character(s) ('\n'). print download answer = raw_input(Do you want to download the podcast? ) if answer == y: wget = wget subprocess.call([wget, download]) else: print oops OK. There's the problem. Let's assume that after 'download = L.readline()' that download equals this (you can confirm by adding a 'print repr(download)'): 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n' ... then the call becomes (behind the scenes) subprocess.call(['wget', 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3\n']) ... so the newline is passed as part of the first argument to the wget command. Not so coincidentally, the '%0A' represents a newline ('\n') in a properly quoted/escaped URL. . import urllib2 . urllib2.unquote('%0A') '\n' I suspect it is the wget command which is quoting the newline, not the subprocess call, as subprocess doesn't know anything about valid characters for urls. You can work around this problem as you already have by dropping the last character as in 'download[:-1]', or use the strip (or rstrip) str method: . download.rstrip() 'http://linuxcrazy.com/podcasts/LC-44-arne.mp3' More info here: http://www.python.org/doc/2.5.3/lib/string-methods.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess adds %0A to end of string
David wrote: Martin Walsh wrote: Welcome! thanks welcome (uh oh, infinite loop warning) This podcast_file.write('%s: %s' % (entry.updated, entry.link)) writes it in one very long string Copy and paste gets me every time. Try this, and note the presence of the newline ('\n'): podcast_file.write('%s: %s\n' % (entry.updated, entry.link)) The Latest Link http://linuxcrazy.com/podcasts/LC-44-arne.ogghttp://linuxcrazy.com/podcas= and sys.stdout prints to the file one line at a time The primary issue is not that you're writing to sys.stdout, it's that you're using the print statement which implicitly adds a newline. Technically you can also do the following (python = 2.5, and maybe 2.6) which should demonstrate the concept... podcastfile = file(somepath, 'a') print podcastfile, '%s: %s' (entry.updated, entry.link) ... however, I would recommend against using the above print statement syntax for anything other than experimentation because 1) it's not common practice IMHO, and 2) it's gone starting with python 3.0. 'print' will be a builtin function instead of a statement going forward. I second Alan's recommendation to read a tutorial or two to solidify your understanding of the basic concepts. Alan's tutorial is very good, and covers everything we have discussed so far, particularly the Handling Files section. Check the link in his email sig. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Extend Help
Omer wrote: Hey. I'm trying to do something I think is basic and am failing. The goal is: [mimicking the google urlopen syntax] try: from google.appengine.api.urlfetch import fetch except: from urllib import urlopen as fetch How do I add this fetch the property of content? Apparently, the urlfetch.fetch function returns a 'Response' object which holds the attribute 'content' (among others). http://code.google.com/appengine/docs/urlfetch/responseobjects.html So, I suppose it makes sense to do something similar. Here's another idea (untested): from urllib2 import urlopen def fetch(url): class Response(object): pass response = Response() response.content = urlopen('http://www.example.com').read() return response ... or if you later want to match the appengine api more closely, then just add what you need (something like) ... from urllib2 import urlopen class Response(object): def __init__(self, url): response = urlopen(url) self.status_code = response.code ... self.content = response.read() def fetch(url): return Response(url) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] listen in on other program's tcp connections
phpfood wrote: On windows XP, I'm running a program that sends TCP connections on port 5039. I'v ran wireshark to determine this. I want to create a simple program that listens for these connections and intercepts and then turns the data transferred into a string. From there I'd obviously like my program to act and manipulate those strings, but for now just spitting out that intercepted TCP data is good enough. IIRC, wireshark uses libpcap for which there are no fewer than two python extension modules (though, I have not used either): http://pylibpcap.sourceforge.net/ http://code.google.com/p/pypcap/ Less like a socket proxy, more like packet sniffing. I was reading up on Twisted. http://twistedmatrix.com/ Can someone get me started on how to do this with twisted framework or anything else? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Newbie Wondering About Threads
Damon Timm wrote: On Sat, Dec 6, 2008 at 6:25 PM, Python Nutter [EMAIL PROTECTED] wrote: I'm on my phone so excuse the simple reply. From what I skimmed you are wrapping shell commands which is what I do all the time. Some hints. 1) look into popen or subprocess in place of execute for more flexibility. I use popen a lot and assigning a popen call to an object name let's you parse the output and make informed decisions depending on what the shell program outputs. So I took a peak at subprocess.Popen -- looks like that's the direction I would be headed for parallel processes ... a real simple way to see it work for me was: p2 = subprocess.Popen([lame,--silent,test.wav,test.mp3]) p3 = subprocess.Popen([lame,--silent,test2.wav,test2.mp3]) p2.wait() p3.wait() top showed that both cores get busy and it takes half the time! So that's great -- when I tried to add the flac decoding through stdout I was able to accomplish it as well ... I was mimicing the command of flac --decode --stdout test.flac | lame - test.mp3 ... see: p = subprocess.Popen([flac,--decode,--stdout,test.flac], stdout=subprocess.PIPE) p2 = subprocess.Popen([lame,-,test.mp3], stdin=subprocess.PIPE) p2.communicate(p.communicate()[0]) That did the trick - it worked! However, it was *very* slow! The python script has a real time of 2m22.504s whereas if I run it from the command line it is only 0m18.594s. Not sure why this is ... I'm not certain this completely explains the poor performance, if at all, but the communicate method of Popen objects will wait until EOF is reached and the process ends. So IIUC, in your example the process 'p' runs to completion and only then is its stdout (p.communicate()[0]) passed to stdin of 'p2' by the outer communicate call. You might try something like this (untested!) ... p1 = subprocess.Popen( [flac,--decode,--stdout,test.flac], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p2 = subprocess.Popen( [lame,-,test.mp3], stdin=p1.stdout, # -- stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p2.communicate() ... where you directly assign the stdin of 'p2' to be the stdout of 'p1'. The last piece of my puzzle though, I am having trouble wrapping my head around ... I will have a list of files [file1.flac,file2.flac,file3.flac,etc] and I want the program to tackle compressing two at a time ... but not more than two at a time (or four, or eight, or whatever) because that's not going to help me at all (I have dual cores right now) ... I am having trouble thinking how I can create the algorithm that would do this for me ... Interesting problem, and not an easy one IMHO, unless you're content with waiting for a pair of processes to complete before starting two more. In which case you can just grab two filenames at a time from the list, define the Popen calls, and wait for (or communicate with) both before continuing with another pair. But since you probably want your script to stay busy, and it's reasonable to assume (I think!) that one of the processes may finish much sooner or much later than the other... well, it is a bit tricky (for me, anyway). Here is my simplistic, not-very-well-thought-out, attempt in pseudo-code, perhaps it will get you started ... paths = [file1.flac,file2.flac, ... file11.flac] procs = [] while paths or procs: procs = [p for p in procs if p.poll() is None] while paths and len(procs) 2: flac = paths.pop(0) procs.append(Popen(['...', flac], ...)) time.sleep(1) The idea here is to keep track of running processes in a list, remove them when they've terminated, and start (append) new processes as necessary up to the desired max, only while there are files remaining or processes running. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] using windows wide proxy settings
Tim Michelsen wrote: Hello, is there any possibility in python to retrieve the system wide internet connection settings? I would like to access the proxy settings stored in Internet Explorer - Extras - Options - Connection - LAN settings. I wouldn't believe it if I didn't just see it work this way, but... On windows, proxy settings configured the way you describe above (if no auth is required) are retrieved from the registry and used automatically by urllib(2).urlopen. from the urllib docs, http://www.python.org/doc/2.5.2/lib/module-urllib.html The urlopen() function works transparently with proxies which do not require authentication. In a Unix or Windows environment, set the http_proxy, ftp_proxy or gopher_proxy environment variables to a URL that identifies the proxy server before starting the Python interpreter. For example (the % is the command prompt): ... In a Windows environment, if no proxy environment variables are set, proxy settings are obtained from the registry's Internet Settings section. The urllib and urllib2 modules also provide a helper function for retrieving proxy info... import urllib2 urllib2.getproxies() {'ftp': 'ftp://10.0.0.100:', 'http': 'http://10.0.0.100:'} HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] array of different datatypes
Reed O'Brien wrote: On Sep 22, 2008, at 11:50 PM, Steve Willoughby wrote: Dinesh B Vadhia wrote: Thanks Steve. How do you sort on the second element of each list to get: a' = [[42, 'fish'], [1, 'hello'] [2, 'world'] ] something like this would do the trick: a_prime = sorted(a, key=(lambda i: i[1])) sorted(a) returns a new list consisting of the elements in a but in sorted order. the key= parameter says how to derive the sort key from any given element; in this case, the elements being sorted are themselves lists, and element #1 in the sub-list (a.k.a. row) is the key. try itemgetter: In [1]: a = [[42, 'fish'], ...: [2, 'world'], ...: [1, 'hello']] In [2]: from operator import itemgetter In [3]: sorted(a, key=itemgetter(1)) Out[3]: [[42, 'fish'], [1, 'hello'], [2, 'world']] From: Steve Willoughby Sent: Monday, September 22, 2008 8:16 PM To: Dinesh B Vadhia Cc: tutor@python.org Subject: Re: [Tutor] array of different datatypes Dinesh B Vadhia wrote: I have looked (honestly!) and cannot see an array structure to allow different datatypes per column. I need a 2 column array with column 1 = an integer and column 2 = chars, and after populating the array, sort on column 2 with column 1 sorted relatively. itemgetter also allows you to do something like this (2.5 and later)... In [1]: a = [[42, 'fish'], [1, 'hello'], [2, 'world'], [41, 'fish']] In [2]: sorted(a, key=itemgetter(1, 0)) Out[2]: [[41, 'fish'], [42, 'fish'], [1, 'hello'], [2, 'world']] ... which would give you the relative sort you asked about for column 1. But at that point, if I had control over the input data structure -- I would probably reverse the order, and then just use a vanilla sorted call without any key arg. If by array you mean a regular Python list, the data type of every single element may be different. So it's just how lists always work. a = [[1, 'hello'], [2, 'world'], [42, 'fish'], ] Thanks! Dinesh ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Addition, Dictionary, KeysError
Rilindo Foster wrote: Scratch that, I'm a dork: OrderDict[(o[0])] = OrderDict.get(o[0],0) + float(o[1]) http://www.faqts.com/knowledge_base/view.phtml/aid/4571/fid/541 :D For this case you might also be interested in collections.defaultdict, added in python 2.5 I believe. from collections import defaultdict orders = defaultdict(float) orders[o[0]] += float(o[1]) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Addition, Dictionary, KeysError
Martin Walsh wrote: Rilindo Foster wrote: Scratch that, I'm a dork: OrderDict[(o[0])] = OrderDict.get(o[0],0) + float(o[1]) http://www.faqts.com/knowledge_base/view.phtml/aid/4571/fid/541 :D For this case you might also be interested in collections.defaultdict, added in python 2.5 I believe. from collections import defaultdict orders = defaultdict(float) orders[o[0]] += float(o[1]) HTH, Marty It is so unlike me to respond without the obligatory doc reference: http://docs.python.org/lib/defaultdict-objects.html http://docs.python.org/lib/defaultdict-examples.html FWIW, you can also accomplish something similar by subclassing dict and defining a __missing__ method (also added in 2.5). Something like this (untested): class OrderDict(dict): def __missing__(self, key): return 0.0 orders = OrderDict() orders[o[0]] += float(o[1]) See http://docs.python.org/lib/typesmapping.html Note that it doesn't actually update the dict with any access as defaultdict would... In [3]: orders = defaultdict(float) In [4]: orders['somekey'] Out[4]: 0.0 In [5]: orders Out[5]: defaultdict(type 'float', {'somekey': 0.0}) In [6]: orders = OrderDict() In [7]: orders['somekey'] Out[7]: 0.0 In [8]: orders Out[8]: {} HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] No Blank Separator between Date and Time Valid?
Wayne Watson wrote: This program segment allows an invalid date to go undetected. See below. def set_time_stamp(d1): # /mm/dd hh:mm:ss in, vmmdd_hhmmss.27 out formatin = '%Y/%m/%d %H:%M:%S' d1 = d1.lstrip() try: date1 = datetime(*(time.strptime(d1, formatin)[0:6])) except ValueError: print; print Invalid date input. Use /mm/dd hh:mm:ss. return False snip Enter date and time: 2008/1/100:00:30 - Why is this valid. The fields are not spearated. dout: 20080110_30 prefix: v20080110_30.27 OK: v20080110_30.27 I have confirmed there is a difference in the behavior of time.strptime between python 2.4 and 2.5, and I assume you're using 2.4. It is possibly related to this bug (but hard to say for sure without looking at the source)... http://bugs.python.org/issue1340337 ... and the subsequent fix for 2.5. But, clearly something changed between releases. from http://www.python.org/download/releases/2.5/NEWS.txt - Bug #1340337: change time.strptime() to always return ValueError when there is an error in the format string. - Bug #1290505: Fix clearing the regex cache for time.strptime(). HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] cmd module
Kent Johnson wrote: On Thu, Sep 4, 2008 at 1:00 AM, Tony Cappellini [EMAIL PROTECTED] wrote: I was just reading the PYMOTW article on the cmd module, and trying the examples. http://www.doughellmann.com/PyMOTW/cmd/cmd.html Scroll down to Auto-Completion. Does the tab key work for anyone running Windows ? Is this an OS specific feature? The command completion uses the Python readline library which, according to the docs, is only available on Unix. There is pyreadline for windows, based on the installation instructions it might take a little effort to get it to work with the cmd module -- I've never tried. http://ipython.scipy.org/moin/PyReadline/Intro HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python open of c:\ path Problem
Alan Gauld wrote: Wayne Watson [EMAIL PROTECTED] wrote BTW, how does one continue a long statement that has, say, a long path to a file? You can create a long string by adding the shorter string elements : f = open( a:/very/long/path/name/that/needs/a/whole/line/to./itself.py, w) becomes f = open(a:/very/long/path/name/ + that/needs/a/whole/ + line/to./itself.py,w) or by using a line continuation character. f = open(a:/very/long/path/name/ \ that/needs/a/whole/ \ line/to./itself.py, w) or just f = open(a:/very/long/path/name/ that/needs/a/whole/ line/to./itself.py, w) because of the enclosing parentheses. This works for expressions in brackets or braces as well -- any enclosing form. PEP8[1] suggests this as the preferred style (adding that sometimes using a backslash looks better), which is a little surprising to me, but not in a bad way. Then again, perhaps I've misunderstood -- which would *not* be surprising. # for those who aren't familiar [1] http://www.python.org/dev/peps/pep-0008/ HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] something is fundamentally wrong...
Joshua Nikkel wrote: IDLE 1.2.2 No Subprocess s = 'supercalifragilisticexpialidocious' len(s) Traceback (most recent call last): File pyshell#1, line 1, in module len(s) TypeError: 'str' object is not callable My guess would be that you've reassigned the name 'len' to some other object, in this case a string. Does the error persist after you restart IDLE (or Restart Shell from the Shell menu)? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Raw string
Neven Goršić wrote: I read from one file plenty of parameters and among them one file name of other file. That file name is 'e:\mm tests\1. exp files\5.MOC-1012.exp' and I hold it in variable s. As John pointed out, if you're really reading this string from a file (with something like file.read or ConfigParser) it should already be escaped properly, and you'd get back something like 'e:\\mm tests\\1. exp files\\5.MOC-1012.exp' instead. Which would probably work for your purposes. Can you show us an example of the approach you're using to 'read' parameters from a file, including the variable assignment? Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuouse loop
Monika Jisswel wrote: Thanks or your replies, in fact I need my server to recieve queries from some 40 clients, process the recieved text (some calculations) send a response back to them, in my previouse application I used a database, (they entered thier queries into the db my calculating script looks at new records in db every 2 minutes inserts the answers into the db too so that the users could see them on thier interface. but now I need to move to a server/client design because at the time i had created this program I didn't have much time I had to put it into production. Assuming your 40 clients represent real human users, and not automated processes, I almost always approach this kind of problem with a web application, usually a cgi script or simple mod_python handler. That way I can spend more time solving the problem at hand, and less on client/server design details or client installation/upgrade scheduling. But then browsers and web servers are very common in our environment. Right now I am working on some code using the SOCKET module as it was the only module that allows inter-process communication was simple enough for me to understand how it works. In that case, you might want to have a look at the SocketServer module also. http://www.python.org/doc/lib/module-SocketServer.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating pop method for stack class
Christopher Spears wrote: I see what you mean. I have tested it, and I have gotten a weird result: def shorten(lst): ... lst = lst[:-1] ... lista = [1,2,3,4] shorten(lista) print lista [1, 2, 3, 4] lista = [1,2,3,4] lista = lista[:-1] print lista [1, 2, 3] Strange...why does it work outside of the function but not in it? Let me try something else: def shorten(lst): ... lst = lst[:-1] Perhaps it would be helpful to consider the following... def shorten1(lst): lst[:] = lst[:-1] ... or ... def shorten2(lst): lst.pop() Why might these exhibit different behavior? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] continuouse loop
Monika Jisswel wrote: Would a program using a continuouse loop such as in this code take up resources on the system if left for long period ? import sys while 1: self.data = sys.stdin.readline() self.function_1(data) Not much, I would think, until something is written to stdin of this program, and then it would depend on what function_1 does. What are my other options is I want to have a running program other programs communicate with it get responses from it ? If I understand what you're asking, there are a few options outlined in the python library reference, here: http://docs.python.org/lib/ipc.html I would add named pipes as another option for *nix, windows may have something similar. And, depending on what you're trying to accomplish maybe xmlrpclib, soappy, pyro, or perhaps even cgi. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] parsing sendmail logs
Monika Jisswel wrote: to say the truth I never thought about additional overhead of getting the input/output data transferred because the suprocess itself will contain the (bash)pipe to redirect output to the next utility used not the python subprocess.PIPE pipe so it will be like one subprocess with each utility piping stdout to the next as if run from the shell, what I agree with Alan. Personally, I find trying to replace shell scripts with python code, just plain awk-ward ... ahem, please forgive the pun. :) No doubt the subprocess module is quite handy. But in this case it would be hard to beat a shell script, for simplicity, with a chain of subprocess.Popen calls. I realize this is subjective, of course. python comes in for ? well, its always sweet to work with python as it will allow you to make whatever logic you have in yoru head into real life with ease and at the end of the subprocess you can always parse the stdout using python this time load results to some database. If you follow the unix philosophy(tm) it might make more sense to pipe the result (of a shell pipeline) to a python script that does only the database load. I have to say that I have seen awk, grep sort, wc, work on files of handreds of Mbytes in a matter of 1 or 2 seconds ... why would I replace such a fast tools ? I can think of a few reasons, not the least of which is the OP's -- as a programming exercise. Alan do you think python can beat awk in speed when it comes to replacing text ? I always wanted to know it ! Well, maybe. But IMHO, the the question should really be is python 'fast enough'. Especially when you consider how the OP is using awk in the first place. But the only way to know, is to try it out. Any pragmatic advice on building or working with a framework to get to the point where i can do analysis on my logs would be cool. As an exercise, I think it would be a reasonable approach to write python derivatives of the shell commands being used, perhaps tailored to the data set, to get a feel for working with text data in python. Then ask questions here if you get stuck, or need optimization advice. I think you'll find you can accomplish this with just a few lines of python code for each (sort -u, grep, awk '{print $n}', etc), given your use of the commands in the examples provided. Write each as a function, and you'll end up with code you can reuse for other log analysis projects. Bonus! HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another assert() question
Dick Moores wrote: At 11:44 AM 7/13/2008, Steve Willoughby wrote: Dick Moores wrote: Yes! A rule, not logic. I'm not contradicting Kent, just helping myself understand. First the rule, then logic in the application of the rule. And I assume the rule is there in Python because it makes things work better. Yes, so a statement like if foo: becomes an idiom for if the collection foo has stuff in it: which is handy whether foo is a text string or a list of objects. Yes, I've been using that, a bit uneasily. One question about the data I listed. Why is bool(set([])) false, whereas bool([[]]) is true? In the first example you're passing an empty list to the set constructor, and getting back an empty set object. In the second, you're providing a list with one element, which just so happens to be an empty list, but it doesn't matter -- since the outer list is not empty. Perhaps a better comparison would be bool(list([])) = False. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Another assert() question
Dick Moores wrote: At 07:39 PM 7/12/2008, Kent Johnson wrote: On Sat, Jul 12, 2008 at 6:03 PM, Dick Moores [EMAIL PROTECTED] wrote: At 01:34 PM 7/12/2008, Kent Johnson wrote: In [2]: assert(False, Asserted false) This is assert condition where the condition is a tuple with two elements, hence true so there is no output. In [13]: assert(3 2 , qwerty) In [14]: I don't understand that logic. Could you unpack it for me? (False, Asserted false) is a tuple containing two values, False and Asserted false. assert x evaluates x as a boolean; if it evaluates to False, the assertion is raised. A tuple with two elements will always evaluate to True so the assertion is never raised. But why will a tuple with two elements will always evaluate to True? In [2]: (3,5) == True Out[2]: False In [3]: (qwerty, asdfg) == True Out[3]: False In [4]: You might find it easier to think about this way: In [1]: bool((3, 5)) Out[1]: True In [2]: bool((qwerty, asdfg)) Out[2]: True More info here: http://docs.python.org/lib/truth.html http://docs.python.org/lib/node34.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list objects are unhashable
Hi Norman, Norman Khine wrote: for brain in brains: x = getattr(brain, horizontal) x = string.join(x, '' ) y = getattr(brain, vertical) y = string.join(y, '' ) if x and y and (x, y) in table: table[(x, y)] += 1 table[(x, '')] += 1 table[('', y)] += 1 table[('', '')] += 1 For what it's worth, string.join has been deprecated since the addition of the join method for str and unicode types. Other deprecated string module functions are documented here: http://docs.python.org/lib/node42.html If I'm not mistaken, the conventional form would be: x = ''.join(x) So now my list becomes a string, which is not really good for me, as this fails when there is more then one item. Is there a better way to loop through this and sum up all occurrences of each entry ie 'airport-car-parking' Maybe, can you show us a brief excerpt of what 'table' might look like before the loop, and what you expect it to look like after one iteration, with data samples for both x and y? Most likely it's just me, but I'm having trouble reconciling your code examples with your questions. AFAICT, either you want more than just a simple count of occurrences from your data set, or you have some confusion regarding dictionaries (if 'table' is a dictionary, of course). If you want a count of each unique occurrence in a list -- not sure if it's better, but something like this might get you started (untested): from sets import Set x = ['airlines-scheduled', 'airport-car-parking', 'more-than-100ml', 'do-not-bring-toothpaste', 'airport-car-parking', 'airlines-scheduled'] entity_count = dict((item, x.count(item)) for item in Set(x)) print entity_count['airlines-scheduled'] # 2 HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem Euler 26
kinuthiA muchanE wrote: Hi, Hi Kinuthia, I am trying to solve Problem Number 26 (http://projecteuler.net/index.php?section=problemsid=26) on project Euler but apparently the answer I am submitting is wrong. I am a big fan of Project Euler also. Fun stuff. I suspect I have completely misunderstood the question. You're probably right, but it's not clear from your description (to me anyway), what your understanding of the question is. Any ideas? Thanks! Perhaps it would be helpful to look at the next few values of d, as defined by the question (note: the parens enclose the repeating pattern): 1.0/11 = 0.(09) 1.0/12 = 0.08(3) 1.0/13 = 0.(076923) 1.0/14 = 0.0(714285) 1.0/15 = 0.0(6) 1.0/16 = 0.0625 1.0/17 = 0.(0588235294117647) No spoilers there, hopefully :) Have fun! Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem Euler 26
kinuthiA muchanE wrote: (28, '035714286') (38, '026315789') (81, '012345679') For 28, the digit, in the fractional part, after 8 is 5, so 5 is repeated and as for, 81 the next digit after 7 is 0, so again 0 occurs twice. But for 38, the next digit after 9 is 4, and because it has NOT occurred before, I assume 38 is the correct answer... and I am wrong! Ah, sorry -- I didn't parse this properly the first time through. You should also be aware of the limitations of representing a decimal fractions as a float. I'll defer to the docs, instead of trying (and failing) to explain this myself: http://docs.python.org/tut/node16.html Also, have a look in the standard library, there was a module added in 2.4 which will provide additional assistance. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] local variable 'l1' referenced before assignment
Douglas Drumond wrote: In a2() you do l1 += l2, ie, l1 = l1 + l2 A subtle clarification is warranted I think. l1 += l2 is not the same as l1 = l1 + l2, when l1 and l2 are lists. l1 += l2 is an augmented assignment statement, and as such will perform the operation in place if possible, IIUC. Consider the following: In [1]: l1 = l2 = [1, 2, 3] In [2]: l1 is l2 Out[2]: True In [3]: l1 += [4, 5, 6] # in-place In [4]: l1 is l2 Out[4]: True In [5]: l1 = l1 + [7, 8, 9] # not In [6]: l1 is l2 Out[6]: False Perhaps there is a better reference, but this behavior is discussed briefly here: http://docs.python.org/ref/augassign.html But if you don't have l1 defined yet, you can't add to l2 It's like: def a2(): l1 = foo + l2 UnboundLocalError: local variable 'foo' referenced before assignment It's because l1 (and foo at above example) is a local variable. a1's l1 is different from a2's l1. Yes, but as Alan pointed out it's considered local because of the assignment attempt. Obligatory doc reference: http://www.python.org/doc/2.4/ref/naming.html snip = \ If a name binding operation occurs anywhere within a code block, all uses of the name within the block are treated as references to the current block. This can lead to errors when a name is used within a block before it is bound. This rule is subtle. Python lacks declarations and allows name binding operations to occur anywhere within a code block. The local variables of a code block can be determined by scanning the entire text of the block for name binding operations. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dollarize.py
Jordan Greenberg wrote: def addcommas(s): # assumes type(s)==str b=[] l=len(s) for i,v in enumerate(s): i+=1 # easier to understand w/ 1-based indexing, i think. b.append(v) if (l-i)%3==0 and not i==l: b.append(',') return ''.join(b) or, somewhat more tersely: def addcommas2(s): l=len(s) return ''.join(v+',' if (l-(i+1))%3==0 and not i+1-l==0 else v for i,v in enumerate(s)) Excellent, thanks Jordan! That's much more elegant, and efficient, than my juggling act :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] os.waitpid non spawned pid
John [H2O] wrote: Hello, I would like to write a script that would have a command line option of a pid# (known ahead of time). Then I want my script to wait to execute until the pid is finished. How do I accomplish this? I tried the following: #!/usr/bin/env python import os import sys def run_cmd(cmd): RUN A BASH CMD import subprocess as sub p = sub.Popen( ['/bin/bash' , '-c' , cmd ], stdout = sub.PIPE , stderr = sub.STDOUT ) output = p.stdout.read() return output r=os.waitpid(sys.argv[1],0); To approximate the behavior you're looking for, I've seen it suggested that you can send signal number 0 to a non-child process until you get a meaningful result. Never used this approach myself, but it might look something like this: import os, time def waitncpid(pid): while 1: try: os.kill(pid, 0) time.sleep(1) except OSError: break waitncpid(sys.argv[1]) Not sure what this would do on a windows machine. And, I suppose it is possible that your process could end, and another start with the same pid while sleeping between kill attempts, but this seems unlikely to me, YMMV. cmd = 'echo Now %s has finished ' %r run_cmd(cmd) HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] dollarize.py
Christopher Spears wrote: I'm working on an exercise from Core Python Programming. I need to create a function that takes a float value and returns the value as string rounded to obtain a financial amount. Basically, the function does this: dollarize(1234567.8901) returns- $1,234,567,89 I strip off the symbols, break the string apart, convert it to a float, and then round it. I'm not sure how to add the commas back after the string has been converted to a rounded float value. Any hints? Quick and dirty attempt (and did I mention ugly?) ... def addcommas(f): This amounts to reversing everything left of the decimal, grouping by 3s, joining with commas, reversing and reassembling. # assumes type(f) == float left, right = ('%0.2f' % f).split('.') rleft = [left[::-1][i:i+3] for i in range(0, len(left), 3)] return ','.join(rleft)[::-1] + '.' + right I think you can also accomplish your broader goal with the locale module (python2.5), though I don't know it very well. import sys, locale def dollarize(s): s = ''.join([n for n in str(s) if n not in ('$', ',')]) if sys.platform == 'win32': lcl = 'US' else: # linux and mac? lcl = 'en_US.UTF8' locale.setlocale(locale.LC_MONETARY, lcl) return locale.currency(float(s), 1, 1) print dollarize(12345678.) # $12,345,679.00 print dollarize('123456780.0999') # $123,456,780.10 print dollarize('$12345670.') # $12,345,671.00 print dollarize('$12,345,678.123') # $12,345,678.12 HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] for loop
Sean Novak wrote: I know I'm going to feel stupid on this one.. I would normally write this in PHP like this: for($i=1; i count($someArray); $i++) { print $someArray[i] } essentially,, I want to loop through an array skipping someArray[0] but in python the for syntax is more like foreach in PHP.. I can think of a few approaches, in no particular order -- the decision is somewhat data dependent, IMO. 1. Use the enumerate built-in somelist = ['one', 'two', 'three', 'four'] for count, element in enumerate(somelist): if count 0: print element 2a. Slice the list to omit the first element for element in somelist[1:]: print element 2b. If somelist is a sequence of strings... print '\n'.join(somelist[1:]) I've tried this to no avail count = 0 for i in range(1,10): if count == 0: continue else: count += 1 print i continue it prints absolutely nothing. This never reaches 'print i', because count is not incremented when count == 0, and so it remains for all of the values in range(1, 10). HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying files in a directory.
[EMAIL PROTECTED] wrote: . soup = BeautifulSoup(html) . ord_tbl_price = soup.find('td', {'class': 'order_tbl_price'}) . ord_tbl_price td class=order_tbl_pricespan class=order_table_price_small /span $32.66/td So now, how do I reduce the price by 15% and write it back to the document? Not sure if this is the right way, but it seems to work for me, YMMV: html = \ td class=order_tbl_pricespan class=order_table_price_smallFrom/span $32.66/td soup = BeautifulSoup(html) otp = soup.find('td', {'class': 'order_tbl_price'}) price = float(otp.contents[1].lstrip(' $')) otp.contents[1].replaceWith('$%0.2f' % (price * 0.85)) print soup.renderContents() td class=order_tbl_pricespan class=order_table_price_smallFrom/span$27.76/td I'll second Kent's suggestion to experiment on the command line, and go one further -- download and install the ipython interpreter[1]. It offers tab-completion of methods and attributes of an object (for example, type yourobject.tab), easy access to doc-strings with a single ?, the source code (when available) with ??, and much much more[2]. It's a real time saver, even if you don't use the fancier features it beats dir()/help() hands down. [1] http://ipython.scipy.org/moin/ [2] http://ipython.scipy.org/doc/manual/node4.html HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] free loop device
Nathan McBride wrote: Yup, I got some help in IRC. What I ended up doing was using regex to pull out each /dev/loopX. Then took the X and fed it to max which in turn gave me the highest numbered loop device in use. After which I then just added 1 to X and added it to the end of /dev/loop_. The only other thing I had to do was put a check in incase there were no used loop devices in which case then it defaults to /dev/loop0. One might also consider reimplementing 'losetup -f' in python, but after my own attempt I realize it might not be that practical, and is potentially dangerous I suppose -- if, like me, you don't fully understand the underlying system calls. I've attached my attempt for the sake of discussion only, and not as a solution -- perhaps someone with interest will correct any errors and make it usable. I would definitely appreciate it. Drifting off topic now... I copied most of the find_unused_loop_device implementation in util-linux/lomount.c[1]. The main points of interest, and potential doom due to my ignorance, are related to the fcntl.ioctl call: 1) the LOOP_GET_STATUS ioctl op const, isn't exposed in any of the typical python modules that I can find, and as such I'm worried that it's value is somehow platform dependent. 2) for a similar reason, I am passing a string of the largest allowed length as the 3rd arg to the fcntl.ioctl call on line 33, since the size of the returned data seems to be governed by a struct defined in loop.h, which needs dev_t from a kernel header. Whew. This seems to work fine on my ubuntu system, if sloppy. But, since I don't know, I tend to assume it could cause problems with stability or security. Anyway, thanks for the interesting question Nathan. Now I have some reading to do. :) Marty [1] http://www.google.com/codesearch?hl=enq=show:3q3vE6vLdaY:0lRVP2J7BtU:j-QqODsRp3ssa=Nct=rdcs_p=ftp://ftp.kernel.org/pub/linux/utils/util-linux/testing/util-linux-2.13-pre7.tar.gzcs_f=util-linux-2.13-pre7/mount/lomount.cstart=1 #!/usr/bin/env python2.5 import os import stat import errno import fcntl if os.uname()[4] == 'x86_64': LOOP_GET_STATUS = 0x4C05 else: LOOP_GET_STATUS = 0x4C03 def find_unused_loop_device(): Return the next unused loop device node, returns None if the next device cannot be determined (and swallows exceptions encountered along the way: permission denied, no such file, etc). for loop_format in ['/dev/loop%d', '/dev/loop/%d']: for i in range(256): dev = loop_format % i try: st = os.stat(dev) except OSError: break # assume invalid loop_format if stat.S_ISBLK(st.st_mode): try: fd = os.open(dev, os.O_RDONLY) except OSError: pass # assume permission denied else: try: fcntl.ioctl(fd, LOOP_GET_STATUS, 1024*'\x00') except IOError, e: if e.errno == errno.ENXIO: os.close(fd) return dev os.close(fd) if __name__ == '__main__': print find_unused_loop_device() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] seeking help to a problem w/ sockets
James Duffy wrote: I have a problem w/ a file transfer receiver. They way it works is it binds a port for incoming transfer , when the file transfer is complete. It closes the connection and the socket, then loops back and restarts the bind and listen. I have it set so that the socket is reuseable, which is why this works. However, if the program that is using this function is closed while listening, it appears that it does not ”un-bind” because when the program is reopened and a listen attepted to start I get a “port already in use” error. Only a reboot fixes this issue. This code is imported into a main GUI script. We have it set to execute some cleanup functions on exit, I need a function that can dig It should be noted that my socket, threading, and related gui skillz are lacking, so beware. IIUC, the usual cause for an 'address already in use' error is when the server closes it's end of the socket first, leaving it in a TIME_WAIT state. Presumably, this would be a feature of tcp, and the socket is released after some timeout period to be sure the client is no longer communicating. However setting the socket options as you have, with setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1), is the typically suggested workaround in this situation. Also, given that you need to reboot to clear the error, I suspect that your gui program isn't actually terminating -- but instead hanging on the Receiver thread, and thus holding on to the socket. You can, of course, confirm with netstat and/or by checking the process list. If you just want the thread to die when you close the gui app, then you could also try to setDaemon(True) on your Receiver class -- which should allow the program to exit, that is -- if there are no non-daemon threads remaining. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib2.urlopen(url)
Monika Jisswel wrote: Hi, can i stop urllib2.urlopen() from following redirects automatically ? It doesn't answer your question directly, but if you care more about the initial request/response than the content at the other end of a redirect -- you can use httplib. It might look something like this: import urlparse import httplib url = urlparse.urlsplit('http://google.com/search?q=python') host = url[1] path = urlparse.urlunsplit(('', '')+url[2:]) con = httplib.HTTPConnection(host) con.request('GET', path) response = con.getresponse() print 'status:', response.status print 'reason:', response.reason print 'document:', response.read() You lose many advantages of the higher-level urllib2, as it does much of the mundane work for you -- parsing urls, choosing http or https transparently, etc -- but I think httplib is still appropriate if your needs are simple. More information here: http://docs.python.org/lib/module-httplib.html HTH, Marty thanks in advance Monika Jissvel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyMOTW: difflib
Dick Moores wrote: See http://blog.doughellmann.com/2007/10/pymotw-difflib.html And my try with the Differ example, http://py77.python.pastebin.com/f41ec1ae8, which also shows the error, E:\Python25\pythonw.exe -u E:\PythonWork\demo_pymotw-difflib.py Traceback (most recent call last): File E:\PythonWork\demo_pymotw-difflib.py, line 12, in module from difflib_data import * ImportError: No module named difflib_data What is difflib_data ? It is the example data provided with the PyMOTW tutorial. Near the top of the article (from the link you provided) you'll see the heading Test Data. I assume the author wants you to copy and paste the source into a new file named difflib_data.py in your working dir. Alternatively, it looks like you can download all the source and example data for all PyMOTWs in one compressed file: http://www.doughellmann.com/projects/PyMOTW/ PyMOTW has a cheese shop entry also (http://pypi.python.org/), so one would assume you could get the source with easy_install as well, but I've never tried it. HTH, Marty Thanks, Dick Moores ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to get response from os.system()
Hi Nathan, Nathan McBride wrote: Yup I use the pexpect module for a lot however couldn't get 'pexpect.run' to work with mysqldump piping to gzip Just to hazard a guess -- when you want to pipe commands with pexpect you have to spawn ('run', it seems, would work the same way) the shell command as an argument to bash (or similar) since pexpect does not natively interpret shell operators or wildcards, like redirect, pipe, etc... from http://pexpect.sourceforge.net/pexpect.html Remember that Pexpect does NOT interpret shell meta characters such as redirect, pipe, or wild cards (, |, or *). This is a common mistake. If you want to run a command and pipe it through another command then you must also start a shell. For example:: child = pexpect.spawn('/bin/bash -c ls -l | grep LOG log_list.txt') child.expect(pexpect.EOF) HTH, Marty -Original Message- From: Jeff Younker [EMAIL PROTECTED] Sent: Sunday, March 16, 2008 6:59 PM To: Nathan McBride [EMAIL PROTECTED] Cc: tutor@python.org Subject: Re: [Tutor] how to get response from os.system() Would you mind perhaps show an example running an interactive command like su and show how to send input to the commands waiting propmts? If you're doing that then you *really* want to be using the pexpect module. cmd = pexpect.spawn('su - SOMEINTERACTIVECOMMAND') cmd.expect('# ') # the prompt cmd.sendline('A COMMAND') cmd.expect('# ') # wait for the prompt again output = cmd.before # the stuff before the prompt cmd.sendline('exit') cmd.close() - Jeff Younker - [EMAIL PROTECTED] - ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Processing unix style timestamp
Ravi Kondamuru wrote: Hi, I have a log file that prints the date and time in the following format: Mon Feb 11 01:34:52 CST 2008 I am expecting multiple timezone entries (eg: PST, PDT and GMT) on the system running in America/Los Angeles time zone. I am looking for a way to internally store all the different timezone entries in GMT. I looked at datetime, but it seems slightly complex to work with non GMT timestamps. Any pointers? If you are not offended by a 3rd-party module, then the string parser included in the egenix mxDateTime module[1] is hard to beat. You may even have it installed already, as it appears to be a popular dependency of other 3rd-party modules, especially db adapters. In [1]: import mx.DateTime In [2]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 CST 2008') In [3]: d Out[3]: mx.DateTime.DateTime object for '2008-02-11 07:34:52.00' at b788d138 In [4]: d.strftime('%a %b %d %H:%M:%S %Y') Out[4]: 'Mon Feb 11 07:34:52 2008' In [5]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 EST 2008') In [6]: d.strftime('%a %b %d %H:%M:%S %Y') Out[6]: 'Mon Feb 11 06:34:52 2008' In [7]: d = mx.DateTime.DateFrom('Mon Feb 11 01:34:52 UTC 2008') In [8]: d.strftime('%a %b %d %H:%M:%S %Y') Out[8]: 'Mon Feb 11 01:34:52 2008' HTH, Marty [1] http://www.egenix.com/products/python/mxBase/mxDateTime/ thanks, Ravi. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output of list
János Juhász wrote: It is nice place to use a generator: def pairs(sliceit): streamlist = list(sliceit) streamlist.reverse() while streamlist: pair = streamlist.pop() try:pair += streamlist.pop() except: pass yield pair ## Probably it is easier to understand def pairs2(sliceit): try: while sliceit: yield sliceit[:2] sliceit = sliceit[2:] except: # oops, it was odd length yield sliceit ... Or, by extending Alan's solution ... def splitStringByN(s, n): for m in range(0, len(s), n): yield s[m:m+n] k = 'abcdefghi' list(splitStringByN(k, 2)) As it turns out, this is similar to an ASPN Cookbook recipe contributed by Dmitry Vasiliev: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302069 HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output of list
János Juhász wrote: Dear Marty, Hi Janos, ... Or, by extending Alan's solution ... def splitStringByN(s, n): for m in range(0, len(s), n): yield s[m:m+n] k = 'abcdefghi' list(splitStringByN(k, 2)) It seems to be the most readable solution for me. For completeness, one could also pull it out of the function def and use a generator expression directly. If I'm not mistaken, this would be more or less equivalent: k = 'abcdefghi' list((k[m:m+2] for m in range(0, len(k), 2))) As it turns out, this is similar to an ASPN Cookbook recipe contributed by Dmitry Vasiliev: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/302069 HTH, Marty Thanks for the link. No problem. Happy Holidays! Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Output of list
Ricardo Aráoz wrote: Emil wrote: hey I want to be capable of converting a string into a list where all the items, in the list, have a fixed length not equal to 1 e.g i have k = 'abcdefgh' and I want the fixed length for all the the items to be 2 then the list would look like ['ab', 'cd', 'ef, 'gh']. How do i do this? Also : [''.join(i) for i in zip(k[::2], k[1::2])] Cool use of 'zip' and extended slicing! Just thought I would add that 'zip' truncates after the shortest sequence, which would cause data loss for strings of odd length -- of course, the OP may not consider this a problem. In [1]: k = 'abcdefghi' # - note the 'i' In [2]: len(k) Out[2]: 9 In [3]: [''.join(i) for i in zip(k[::2], k[1::2])] Out[3]: ['ab', 'cd', 'ef', 'gh'] # - 'i' is gone HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Selecting a browser
Ricardo Aráoz wrote: Martin Walsh wrote: Hi Marty, thanks for your help. I've tried your suggestions but they don't seem to work for me. In W's system window I can do : C:/ S:\FirefoxPortable\FirefoxPortable.exe http://www.google.com and it will open my browser ok. But no matter what I try : c:/program files/mozilla firefox/firefox.exe %s or c:/program files/mozilla firefox/firefox.exe %s as input to webbrowser.get() it won't work. Hi Ricardo, Never would have guessed that you were using a portable browser :) But it really shouldn't matter. And by the way, the '' has special meaning to the webbrowser.get method -- it determines whether a BackgroundBrowser or GenericBrowser object is returned. Here's my session : import webbrowser ff = webbrowser.get(S:\FirefoxPortable\FirefoxPortable.exe %s ) ff.open('http://www.google.com') False I suspect (with no way to confirm at the moment) that something in the webbrowser module is confused by the backslashes. As you may know, the backslash has special meaning in python strings, used as an escape character to denote newlines (\n), tabs (\t), among others. I believe it is helpful to be aware of this when using subprocess.Popen also. And, I think you have more than one option for dealing with slashes in windows paths, but I typically just replace the backslashes with forward slashes: ff = webbrowser.get(S:/FirefoxPortable/FirefoxPortable.exe %s ) Besides looking at the Python 2.5 documentation gave me no clues. It is very poorly documented e.g. : register( name, constructor[, instance]) Register the browser type name. Once a browser type is registered, the get() function can return a controller for that browser type. If instance is not provided, or is None, constructor will be called without parameters to create an instance when needed. If instance is provided, constructor will never be called, and may be None. This entry point is only useful if you plan to either set the BROWSER variable or call get with a nonempty argument matching the name of a handler you declare. But it does not define what a 'constructor' is (I guess a function, but no clue about what it's signature or functionality should be) or what an instance is (but I can guess). Trouble is when you guess and things don't work you don't have a clue if the fault lies in your 'guess' or in your code. Note that it mentions 'the BROWSER variable' but does not say Based only on my own review of the webbrowser module source, the constructor is the '*Browser' class object [ in other words BackgroundBrowser and not BackgroundBrowser() ], presumably it should be a subclass of webbrowser.GenericBrowser. And instance is what you would think, an instance of a '*Browser' class. what it is, what it's value should be, nor any clue about it's use. Probably I lack knowledge in the subject, but hey! the whole purpose of this module is to be able to call the browser WITHOUT any knowledge of the subject. I guess I'll have to look at the code when I have a couple of free hours. I would highly recommend it. It's straight forward, and very readable. A helpful tip, perhaps: one of my favorite features of ipython, allows fairly trivial access to a module's python source (not that it's difficult by other means, really): In [1]: import webbrowser In [2]: webbrowser?? A single question mark prints the doc string if available, where the ?? pages the source. On linux, not sure about windows, I can move around and search for text with all the same familiar keyboard commands (less style). I'd rather go with the 'webbrowser' module than the subprocess.Popen option, as it 'should' abstract me from browser management. Anyway if I I think that's the intent of the module, and a worthwhile pursuit. But, IMHO you may find it's more work, and less flexible, than keeping track of the browser paths yourself and calling subprocess.Popen -- on Windows at least. YMMV. And it is certainly possible that you'll discover some feature of the webbrowser module that I've overlooked. So, I hope you'll report back to the list with your progress. Good luck! Marty can't advance through webbrowser I'll give your examples a try. Thanks a lot for your help. Ricardo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Selecting a browser
Ricardo Aráoz wrote: Hi, I've checked webbrowser module and so far I find no way of selecting a browser other than the default one. Say I want certain places opened with IE and others with Mozilla, and I don't want to mess with the user's setting of the default browser. Any tips? TIA I think one would normally use the form webbrowser.get('firefox'), on unix systems. But if I understand correctly, the problem with the webbrowser module on windows (and perhaps it is similar on a mac) is that unless the program can be found on your system PATH, only a generic 'windows-default' browser class is registered, which uses os.startfile, releasing control to the os, and serves to open the url in the user's default browser. If you're determined to use the webbrowser module on windows, you might be able to do something like this: import webbrowser ffcommand = c:/program files/mozilla firefox/firefox.exe %s ff = webbrowser.get(ffcommand) ff.open(http://www.example.com;) iecommand = c:/program files/internet explorer/iexplore.exe %s ie = webbrowser.get(iecommand) ie.open(http://www.example.com;) I suppose you could also register them manually for later use with the webbrowser.get(browser_name) form. webbrowser.register('firefox', None, ff) webbrowser.get('firefox').open('http://example.com') Personally, I would probably just cut out the middle module and use subprocess.Popen to start the browser, after checking if it is installed (with os.path.isfile, or similar) -- which seems to be, more or less, what the webbrowser module does if it finds one of the predefined browsers on your system PATH. Something like this (pseudo-code): browser = 'c:/program files/mozilla firefox/firefox.exe' if os.path.isfile(browser): p = subprocess.Popen([browser, 'http://www.example.com']) # if you want to wait for the browser to # close before continuing use p.wait() here else: ... web browser not found ... For dispatching based on site (url, or some other criteria), one idea would be to wrap something like the above in a function which accepts the web browser program path as an argument, and then pass the function a path appropriate for the given criteria. Here is another (untested) example to demonstrate: import subprocess import urlparse import sys, os FFPATH = 'c:/program files/mozilla firefox/firefox.exe' IEPATH = 'c:/program files/internet explorer/iexplore.exe' IESITES = ['microsoft.com', 'www.microsoft.com'] def launch(url, browser, wait=False): if os.path.isfile(browser): p = subprocess.Popen([browser, url]) if wait: p.wait() else: print 'Invalid browser.' def main(url): # pick browser path by domain name netloc = urlparse.urlparse(url)[1] if netloc in IESITES: launch(url, IEPATH) else: launch(url, FFPATH) if __name__ == '__main__': if sys.argv[1:]: main(sys.argv[1]) else: print 'Not enough arguments.' In theory, if you run this script from a console on windows with any microsoft.com url as an argument, it should open in IE -- where all others open in firefox. Really rough, but I hope it helps. Regards, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python on T-Mobile Wing???
Trey Keown wrote: Hey all, I just got a brand new T-Mobile Wing, and, as you might guess, I want to install python on it. Well, I tried both the pythonce main build (with the .exe), and the build for the smartphone (also used .exe), but once I downloaded them, when I tried to run them, a dialog comes up saying that this is not a valid pocket pc app. Any ideas? I think the exe installer is for running on a windows pc with activesync -- you would need the cab version to install directly on a windows mobile device. From the pythonce wiki: http://pythonce.sourceforge.net/Wikka/Installing http://pythonce.sourceforge.net/Wikka/Downloading Note- the wing uses windows mobile 6. The specs can be found at- http://www.t-mobile.com/shop/phones/Detail.aspx?device=acc8102d-4506-4eaa-bc2f-9c7b8ec1b1e0 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess help, nohup
John wrote: Hello, Hi John, I didn't see a response to your question, so I'll make an attempt ... I've written a script which conducts several subprocess calls and then ultimately calls a shell script which runs even more programs... my script is using subprocess to execute a few sed calls, and then execute the script. I'm getting strange behavior: Just a suggestion, if you find you are preforming sed replace operations regularly, you might consider writing your own sed-like replace function. This way you avoid spawning a subprocess altogether for search/replace functionality, and as an added bonus you can re-use it in future scripts. The following (untested) code should get you started, if you choose to go this route: import re def sed_replace(search, replace, text): pattern = re.compile(search) return pattern.sub(replace, text) def infile_sed_replace(search, replace, path): text = file(path).read() newtext = sed_replace(search, replace, text) file(path, 'w').write(newtext) ... infile_sed_replace('RunMin=[0-9][0-9]*', 'RunMin=%s' % k, runFile) ... You'll notice, the last subprocess call is commented out. Right now I'm just getting to that point to make sure everything is working. So, it seems to work, but I'm not sure how to get it to work if I change the command to nohup. I still want python to wait for it to return, in fact, I would like to set the python job running in the background as well... so what I'm looking at doing is: % nohup myControl.py --- which will make several subprocess.call(s) including some that should be 'nohupped' as well... This strikes me as nohup abuse, though I'm not entirely certain what you are trying to accomplish. Since you plan to nohup *and* background your script anyway, you might be better served by preemptively detaching from the controlling terminal (or daemonizing) instead. If so, you may find one or both of the following recipes useful: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012 Of course, you may need to handle logging a little differently. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrong version of Python being executed
Alan Gauld wrote: Tony Cappellini [EMAIL PROTECTED] wrote I have to switch between 2.3 and 2.5, so to make it easy, I use an environment variable called CURRENT_PYTHON. (someone on this list or the wxPython list told me I should NOT use PYTHONPATH and modify it the way I am using CURRENT_PYTHON) CURRENT_PYTHON=C:\PYTHON2X path=%CURRENT_PYTHON% (The existing path isn't shown, only for brevity) Note, these are entered in Ctrl Panel, System environment variables, NOT at the command line. That seems like a perfectly rational approach, and good advice. Unfortunately I don't have a windows machine to test/confirm at the moment, but IIRC you can set both user-space and system-wide environment variables this way. I think PATH is special, in that user-space PATH is appended to the *end* of the system-wide PATH, where other vars set in user-space override the system defaults? Is it possible you have python23 in your system-wide path, and you've edited user-space path to include %CURRENT_PYTHON%? Regardless, as a test you might try to add C:\Python25 to the front of your system-wide PATH temporarily. This should aid in diagnosing a simple PATH lookup problem. But, I'm stumped also. I don't know of any reason why you would be seeing this behavior if you are invoking python in a consistent way. Umm, have you rebooted? Probably an obvious step but I don't think environment vars get reset in real time. They didn't used to on NT but that may have changed in W2K or XP... I haven't checked in a while. I believe that this has changed somewhat, where the environment is read at program startup. So the env is not refreshed until you close and re-launch the program in question (in this case 'cmd'). Not sure how windows explorer is effected, which may require a logoff/logon. But, again I don't have a windows machine available to confirm, so take that for what it's worth. It's not going to hurt to reboot, that's for certain. Othewise I'm as stumped as you. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrong version of Python being executed
Tony Cappellini wrote: Martin Walsh mwalsh at groktech.org Sun Nov 11 06:13:10 CET 2007 That is odd. Try using the full path to python, just to be sure: c:\python25\python script.py -- do you get the same behavior? This works just fine- I would expect it to. Actually, I would have expected the opposite. My initial thought based on your description was that python2.5 is being invoked with PYTHON* env vars from a previous install, or some site module weirdness. But, the fact that running python.exe with it's full path corrects the issue, seems to indicate a problem with your PATH, rather than any python specific environment setting. What does 'set PATH' report? Though this still doesn't explain why you get python2.5 interactively, and python2.3 when running a script -- perhaps I'm still unclear what you are seeing. Would the following be an accurate description of the behavior? assuming: - you run inside a fresh 'cmd' console each time (typing 'cmd' at the run dialog, or similar), to be sure there is no app environment kruft - the current working directory doesn't contain any programs, scripts or possibly links that could interfere (preferably an empty path) - you don't have any PYTHON* environment vars set (including PYTHONSTARTUP) you observe: - when you type 'python' (only 'python') at the prompt, you get python2.5 interactively - when you use the form 'python script.py', the script is run with python2.3 (can you verify with sys.version?) with sys.path appropriate for 2.3 - when you use the form 'c:\python25\python.exe script.py', the script is executed with python2.5 and you have the correct sys.path (for 2.5) Also, if you haven't already, you can run python with the -E and/or -S flags (ex. 'c:\python25\python -E -S script.py'). The -E flag will cause the PYTHONPATH and PYTHONHOME environment variables to be ignored. And This also works just fine. I've tried both switches independently, and the scrip runs normally when I use either and both at the same time. If I don't use them, then Python2.3 is being invoked somehow. Yeah, very odd indeed. Very strange indeed. It's starting to remind me of an episode from The Twilight Zone ;-) Is ti possible that my registry is corrupted? I wouldn't think so, but I suppose it is possible. I believe all the pertinent registry keys are store under HKLM\Software\Python\Pythoncore\version, so you could have a look. There are settings stored elsewhere, but I think they are all related to file associations, and enabling double-click launching etc. I hope someone will correct or clarify, if I'm wrong. If it is a registry issue, re-installing python2.5 *may* provide a quick fix. BTW, are you using an alternate distribution of python (ex. ActiveState), or the standard python.org version? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Wrong version of Python being executed
Tony Cappellini wrote: What do you get if you print sys.path from the interpreter? I've printed out sys.path from inside the script as well, and all references to Python25 are replaced with Python23 FWIW- This isn't a problem unique to this script. I've just printed out sys.path from another script in another directory, and Python2.3 is referenced. So, it's a system wide issue- but I still don't know how or why it's happening. That is odd. Try using the full path to python, just to be sure: c:\python25\python script.py -- do you get the same behavior? Also, if you haven't already, you can run python with the -E and/or -S flags (ex. 'c:\python25\python -E -S script.py'). The -E flag will cause the PYTHONPATH and PYTHONHOME environment variables to be ignored. And the -S flag prevents 'import site' at python startup. Regardless, even if these are configured incorrectly I would expect the same behavior if running a script or using the interactive interpreter. You mentioned that python 2.4 is installed also... does it have the same sys.path problem? HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor