Re: output from external commands
quoth the James Colannino: > Hey everyone. First off, I'm new to the list. I had had a little bit > of experience with Perl before discovering Python. The more Python I > learn, the more I love it :) I just have a quick question to ask. I > know that this is probably a simple question, but I've been googling > around, and partly because I'm not sure exactly what to search for, I've > been unsuccessful at finding an answer. What I'd like to do is be able > to take the output of an external command and assign it as an array of > strings. So, for example, in Perl I could do something like: > > @files = `ls`; > > So I guess I'm looking for something similiar to the backticks in Perl. > Forgive me if I've asked something that's a bit basic for this list. > Any help would be greatly appreciated :) Thanks very much in advance. If all you want is filenames this will work: >>> import glob >>> files = ["%s" % f for f in glob.glob("*")] Else use os.popen to iterate over lines of output: >>> import os >>> for line in os.popen("ls -l").readlines(): >>> . . . process(line) Or check out subprocess if you have 2.4.. > James > > -- > My blog: http://www.crazydrclaw.com/ > My homepage: http://james.colannino.org/ -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgpPYTSvHOmSy.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
[OT] Re: output from external commands
quoth the Fredrik Lundh: > (using either on the output from glob.glob is just plain silly, of course) Silly? Sure. os.listdir() is more on point. Never said I was the smartest. However, I will defend my post by pointing out that at the time it was the only one that actually included code that did what the OP wanted. Recall I wrote: "If all you want is filenames this will work:" not: "This is how you should do it" And I invite you to prove me wrong ... it does work. As a novice, I do appreciate getting set strait when I code something dumb, but going off about the efficiency of "%s % foo" over "str(foo)" hardly helps the OP, and is not very pertinant to my glob faux pas either. An explanation of why glob is silly would perhaps teach me better than just stating it as fact. It is things like this that make me wary of posting to this list, either to help another, or with my own q's. All I usually want is help with a specific problem, not a critique involving how brain-dead my code is. I'm a beginner, of course my code is going to be brain-dead ;) I thought the idea was "make it work first, then optimize"? In any event, I will refrain from trying to help people here until I get over this silly stage I seem to be stuck in... it just doesn't seem worth it. I am not trying to sound like a whiner here, I just wish you experts would go easy on us novices... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgprtsZtFVPaz.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Windows vs Linux [was: p2exe using wine/cxoffice]
quoth the Tim Golden: > As it happens, (and I suspect I'll have to don my flameproof suit here), > I prefer the Windows command line to bash/readline for day-to-day use, > including in Python. Why? Because it does what I can't for the life of > me get readline to do: you can type the first few letters of a > previously-entered command and press F8. This brings up (going backwards > > with further presses) the last command which starts like that. And > *then* > you can just down-arrow to retrieve the commands which followed it. > If someone can tell me how to do this with bash/readline I will be > indebted to them and it will increase my chances of switching to Linux > a bit! (Although not at work where I have no choice!) Try ctrl-r in bash, then type your first few letters... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgpWqBHmbFD3S.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
exception KeyboardInterrupt and os.system command
Hello all. I have a python script here which is just a wrapper for 2 or more system commands. I would estimate the program spends at least 95.5% of 'real' time running the system commands. I want to trap the [crtl-c] key combo and exit (somewhat) gracefully if the user decides to abort the program. I am using os.system for the system call, and I have wrapped the entire main loop in a try: except KeyboardInterrupt statement to try to attain these ends. As it is though, if the program is currently in the system command, only that system command is terminated, and the next loop of my program starts. Is there a way to make this work? ie: terminate the entire script? Will popen do this? I don't really want to use popen because all I need is for the system command to run, and check the exit status. Also, popen will pooch the output of the system commands (which I want to be printed to the console) because the system commands (faad, mpg123, and oggenc) have buffered output which won't properly be displayed if I simply print each line of the file object returned by popen. I don't want to use subprocess because I can't expect my users to have 2.4 installed... OS is Linux, if it matters. If you need to see the code it is here: http://badcomputer.org/unix/dir2ogg/dir2ogg.bot Although, this code is the program as it stands, not the code I am testing. Thanks, -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgpBKWYaruFLt.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: exception KeyboardInterrupt and os.system command
Thanks for the tips everyone, although it turns out this is not a python problem at all. After several tests with mpg123 both direct on the cli, and wrapped in an os.system() call, I see it is _always_ returning 0 exit status whether I interrupt it or not. I went to the mpg123 website to see if I could find a reason for this behavior, and the site tells me the package is unmaintained, and has security flaws which will not be fixed. So I guess I will find a new mp3 decoder... -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgprr1YweK46i.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Favorite flavor of Linux? (for python or anything else)
quoth the Mike Meyer: > A lot of the > rough edges of Gentoo have been dealt with in FreeBSD. For instance, > you can update from source, but you can also get binary updates. You can sort of do this with Gentoo. Check out the "--usepkg", "--getbinpkg" and "--buildpkg" emerge options. The only problem is that I don't think there are many (any?) official repositories of binary packages, and if there are, they don't have the full array of all packages available from portage. I haven't checked in a while though, so this may be different now. In any event, it is an excellant timesaver if you have a network of similar systems. emerge from source on your staging server, build a bin package, and push it to the rest of the systems. -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org/ "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgprAoS9mLaTO.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Challenge ahead [NEW] for riddle lovers
quoth the Shane Hathaway: > pythonchallenge wrote: > > For the riddles' lovers among you, you are most invited to take part > > in the Python Challenge, the first python programming riddle on the net. > > > > You are invited to take part in it at: > > http://www.pythonchallenge.com > > That was pretty fun. Good for a Friday. Too bad it comes to an abrupt > "temporary end". > > Shane > > P.S. I hope I didn't hammer your server on step 3. I was missing the > mark. :-) You're not the only one. This is where I am currently stuck. It's starting to hurt my head. There are 478 results in the form *BBBsBBB* but the thing said 'exactly' right, well there are 10 results in the form *sBBBsBBBs* None of them seem to work... I quit ;) -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgpi12dSB8kSg.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Twisted vs POS (Plain-old sockets)
Hey all,I have a (FOSS) project here that I am about to start that requires TCP networking support, and in fact, will require me to design and implement a (text based) protocol from scratch.I have been playing with Twisted today and managed to get a simple client/server talking to each other. However, the twisted framework does seem very complex, and includes many, many, features I will never need. The docs seem a little lacking (or perhaps just confusing) as well. Twisted's good points are that it will save me from having to write many things from scratch, asynchronous networking being the biggie.I guess I am wondering if given the fact I need a custom protocol, and need to talk TCP/IP should I stick with twisted or just use plain old sockets and build it myself? Is there a third option I should consider? Have others found themselves in this situation? Thoughts? Comments? I am really just fishing for opinions here...If it makes a difference: Depending on performance parts of this app may well end up as a prototype for a final (...alternative?) C++ implementation.Thanks for consideration, -d-- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..."- Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: Twisted vs POS (Plain-old sockets)
On 9/3/06, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote: > On Sun, 3 Sep 2006 00:19:17 -0700, Darren Kirby <[EMAIL PROTECTED]> wrote: > >Hey all, > > > >I have a (FOSS) project here that I am about to start that requires TCP > >networking support, and in fact, will require me to design and implement a > >(text based) protocol from scratch. > > I'm sorry. Don't be sorry, I am doing this for fun and to learn... > > If there are features you don't need, then don't use them. What does their > existence cost you? Are you going to use the sunaudio module from the > standard library? If not, is this an argument in favor of using C++ instead > of Python? Well, the question I have is if it is worth digging through all the complexity (in the code itself and docs) for the few nuggets I do need... I am well aware I do not need to use everything... > As for documentation, many people say it is lacking, but perhaps one person > in a thousand points out _how_ or _where_ it is lacking. Unfortunately it > is difficult to improve things (or even determine if they really are lacking) > with this level of feedback. I am certainly not trying to dump on twisted. As for what is lacking, the many methods I looked up that say simply "Not Documented" would be the biggest problem > Keep in mind that in addition to the online documentation, there is a Twisted > book, an extremely helpful twisted mailing list (with years of archives > online), and an IRC channel populated at nearly all hours of the day with > people who can answer Twisted questions. I am aware of the book, and if I decide to go the twisted route I would certainly purchase it. However, not that I am overly swayed by amazon reviews, but the consistent majority of them have said that the book is big on specifics (as in, explaining the example code and not much else), and small on the 'big-picture' so to speak. If this is true I might as well stay with the docs. > > Talking to the TCP/IP stack is surprisingly difficult to get right. Since > it is extremely unlikely that you actually _care_ about all of the random, > stupid differences between different TCP implementations, you should use > Twisted, since it does its best to hide these differences and instead > present a uniform API. Fair enough... > If you use bare sockets, you will need to learn many of these quirks yourself, > frequently through a bug report from a user, since many of them are > undocumented. True, though keep in mind this is as much of a learning exercise for me as it is to get an app out the door quick. > > Twisted is great. It will speed up your development time and reduce the > amount of defects you need to deal with. It will > OK, I will stick with twisted and see if I can't figure it out, and perhaps play with asyncore and see for myself what will work. Please note I was really just looking for some anecdotes from experienced programmers that may have found themselves in my situation, and the solutions they chose... > Hope this helps, Sure it did, thanks for taking the time to respond, also thanks to Guido and Istvan, > Jean-Paul -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: stock quotes
On 9/13/06, Donlingerfelt <[EMAIL PROTECTED]> wrote: > I would like to download stock quotes from the web, store them, do > calculations and sort the results. However I am fairly new and don't have a > clue how to parse the results of a web page download. I can get to the > site, but do not know how to request the certain data need. Does anyone > know how to do this? I would really appreciate it. Thanks. Some have already directed to webscraping tools, but you don't need to go that route if you can settle for using finance.yahoo.com, as you can ask for the data in a particular format ie: plain text, csv etc... I have also written some code that does this. As an added bonus it also does currency conversion. Maybe the code will help get you started: http://badcomputer.org/unix/code/stock.html HTH -d -- http://badcomputer.org -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Challenge ahead [NEW] for riddle lovers
quoth the Ganesan Rajagopal: > I am stuck on level 3. I've tried every re that I can think of. Some body > give me a clue. > > Ganesan > > -- > Ganesan Rajagopal >>> t = /text of page source.../ >>> re.findall('[a-z][A-Z]{3}[a-z]{1}[A-Z]{3}[a-z]', t) You should get ten results. Consider all ten together to get your solution... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgp8pPHoyZC6s.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Challenge ahead [NEW] for riddle lovers
quoth the Reinhold Birkenfeld: > > Somehow writing '[a-z]{1}' is strange... > > Reinhold >>> t = /text of page source.../ >>> re.findall('[a-z][A-Z]{3}[a-z][A-Z]{3}[a-z]', t) Sorry dude! When it comes to logic puzzles I am easily frustrated, and that leads to unclear thinking... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgppGkifujJIs.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting Time
quoth the Ognjen Bezanov: > I never thought id need help with such a thing as time formatting > (admittadly i never did it before) but ok, i guess there is a first for > everything. > > I have a float variable representing seconds, and i want to format it > like this: > > 0:00:00 (h:mm:ss) > > Now search as I might i am finding this quite elusive, i had a look at > the time module but that seems overly complicated for this. Anyone got > any simple solutions to doing this? cheers! def printTime(seconds): hours = seconds / 3600 seconds = seconds - (hours * 3600) minutes = seconds / 60 seconds = seconds - (minutes * 60) print "%i:%s:%s" % (hours, str(minutes).zfill(2), str(seconds).zfill(2)) I am certainly no pro at python but this seems to do it for me. I am sure someone could write it much more elegantly. I am still a little confused as to how you have a float that represents seconds? This will only work if 'seconds' is an int ( al la ' 44342865') but maybe it could help you? -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 pgp6I7q4f3fVW.pgp Description: PGP signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Can a low-level programmer learn OOP?
quoth the Wayne Brehaut: > (I started with Royal McBee LGP 30 machine language (hex input) in > 1958, and their ACT IV assembler later! Then FORTRAN IV in 1965. By > 1967 I too was using (Burroughs) Algol-60, and 10 years later upgraded > to (DEC-10) Simula-67.) > > Going---going--- Mel? Is that you? http://www.pbm.com/~lindahl/mel.html -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: help - python can't find file
quoth the enquiring mind: > - but now I get a error message 21 saying file or directory doesn't > exist. You must be in the same directory (in konsole) as the python script for this to work, else enter the relative path to the file: Assuming you are in your home directory (this is where a new konsole will start you), and the py scripts are in a directory 'pythondir': $ cd pythondir $ python myscript.py or: $ python pythondir/myscript.py You could also chmod the script to be executable and run it as a regular command ...however... I don't mean this to sound like RTFM but I do think that you could use some reading on Linux CLI usage. You say you have some Linux books? I say this as my reading of your message indicates your problems lie with misunderstanding the shell/paths etc, not with Python itself... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: Reading (and writing?) audio file tags
quoth the Paul Moore: > I'd like to write some scripts to analyze and manipulate my music > files. The files themselves are in MP3 and FLAC format (mostly MP3, > but FLAC where I ripped original CDs and wanted a lossless format). > I've no idea what form of tags are used in the files (ID3v1, ID3v2, > OGG, APE, ...) Flac files use Vorbis comments, the same that Ogg Vorbis files use. As for MP3, they use ID3v2 or ID3v1, or both. Anyway, what you want is Mutagen. It handles both Flac and Mp3 tags, as well as many others: http://www.sacredchao.net/quodlibet/wiki/Development/Mutagen > I just used whatever the program that set them up used. > I'm completely confused by the various tag formats that exist - there > seems to be little standardisation, and quite a few compatibility > pitfalls. For example, I have files with tags using accented > characters - I suspect that this causes some tools to switch format > (because I've seen what looked like corrupt data at times, which > turned out to be the program displaying the "wrong format" of tag). > > I've seen various Python libraries that talk about ID3 tag reading - > but I'm not clear if they read other tag formats (many applications > which call themselves "ID3 readers" actually handle multiple formats, > but I don't know if that's true for (Python) libraries. Also, there > seem to be few libraries that will *write* tags. ID3 = MP3 only. A lot of people call _all_ tags 'id3' tags to save having to say 'Flac tags, and Vorbis tags, and Ape tags' etcthese people are the source of your confusion. > Is there a good "music file tag handling" library for Python that's > worth looking at? I use Windows, so it would have to be for that > platform, and although I have a compiler, I don't really want to spend > a lot of time collecting and porting/building support libraries, so > I'd be looking for a binary distribution. >From the read me: "Mutagen works on Python 2.3+ and has no dependencies outside the CPython standard library" so it should work on Windows I think. It is just pure Python so there you go... > In the absence of something suitable, I'll probably go back to dumping > the tags via a generic "MP3 tag reader" program, then manipulate them > as a text file, then try to do some sort of bulk reload. > > Thanks, > Paul. -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: PHP5 programmer learning Python
quoth the romiro: > Hi all, ... > Anyway, my first question was if anyone knows of a tutorial that > focuses on PHP -> Python learning, in such that there might be a block > of PHP code alongside an example of how to do the same thing in > Python. One example of something I've already mapped a comparison to > thanks to standard tutorials is a PHP numeric indexed array being > similar to a list and a PHP associative array being similar to a > dictionary. Of course finding such of a tutorial isn't a deal breaker > by any means, but I figured that having it available would be a boon > for me to actually make some headway in my Python learning adventure. Not a tutorial, and the code is not alongside each other, but the PLEAC [1] website may serve as a decent code comparison between PHP and Python. As for a tutorial, if you are already experienced you will probably want to check out "Dive into Python. [2] Have fun, -d [1] http://pleac.sourceforge.net/ [2] http://diveintopython.org/toc/index.html -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
My 'time' module is broken, unsure of cause
Hi all, I have a strange error here and I am unsure how to further investigate it: Python 2.4.4 (#1, Aug 23 2007, 10:51:29) [GCC 4.1.2 (Gentoo 4.1.2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import time 40:42:0 >>> now = time.time() Traceback (most recent call last): File "", line 1, in ? AttributeError: 'module' object has no attribute 'time' Notice the '40:42:0' that always gets output. I searched Gentoo's bugzilla but can not see anything relevant. I rebuilt python but the behavior recurs. I am unsure if the issue is with Python, Gentoo, or perhaps with the underlying lib (presumably glibc) that Python uses for the time module. This is working fine on another machine (also 2.4.4, GCC 3.4.6, Gentoo Hardened). Anyone seen this? Any hints for me to track this issue down? Any further information I could provide? Thanks for consideration, -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: My 'time' module is broken, unsure of cause
quoth the Calderone: > > [EMAIL PROTECTED]:~$ echo "print '40:42:0'" > time.py > [EMAIL PROTECTED]:~$ python > Python 2.4.3 (#2, Oct 6 2006, 07:52:30) > [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import time > > 40:42:0 > > >>> time.time() > > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'module' object has no attribute 'time' > > >>> print time.__file__ > > time.py > > >>> ^D > > [EMAIL PROTECTED]:~$ rm time.py > [EMAIL PROTECTED]:~$ rm time.pyc > [EMAIL PROTECTED]:~$ python > Python 2.4.3 (#2, Oct 6 2006, 07:52:30) > [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import time > >>> time.time() > > 1187890226.9293921 > > >>> print time.__file__ > > /usr/lib/python2.4/lib-dynload/time.so > > > Jean-Paul Ahh, so it was pebkac. Thanks Jean-Paul, I can be thick sometimes... -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list
Re: passing command line arguments
quoth the Brian McCann: > Hi, > > when I run the script show_args2.py > > # ./show_args2.py 1 2 3 > > I get the following error > > Traceback (most recent call last): > File "./show_args2.py", line 4, in ? > print 'The arguments of %s are "%s"' %s \ > NameError: name 's' is not defined > > > # > <mailto:[EMAIL PROTECTED]> > this is the script > #!/usr/bin/python > import sys, string > print 'The arguments of %s are "%s"' %s \ > (sys.argv[0], string.join(sys.argv[1:])) You don't want the 's' on the last format operator. Try: print 'The arguments of %s are "%s"' % \ (sys.argv[0], string.join(sys.argv[1:])) > any help would be greatly appreciated > > -Brian -d -- darren kirby :: Part of the problem since 1976 :: http://badcomputer.org "...the number of UNIX installations has grown to 10, with more expected..." - Dennis Ritchie and Ken Thompson, June 1972 -- http://mail.python.org/mailman/listinfo/python-list