Re: [Tutor] Popen? or something else
Rumor has it that Ertl, John may have mentioned these words: All, I hate to ask this but I have just installed 2.4 and I need to get some info from a subprocess (I think that is correct term). At the Linux command line if I input dtg I get back a string representing a date time group. How do I do this in Python? I would think Popen but I just don't see it. It could, but there's also a better (IMHO), 'pythonic' way, something like this: def gettoday(): import time today = time.strftime('%Y%m%d%H',time.localtime(time.time())) return (today) $ dtg 2004122212 If you wanted to use popen, it would look rather like this: import os dtg_s = os.popen("/path/to/dtg").readlines()[0] But this may use more system resources (spawning child shells & whatnot) than doing everything internally with the time module in Python. HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | A new truth in advertising slogan SysAdmin, Iceberg Computers | for MicroSoft: "We're not the oxy... [EMAIL PROTECTED] | ...in oxymoron!" ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Popen? or something else
Rumor has it that Ertl, John may have mentioned these words: Roger, I have been doing it the Pythonic way (that is why I have no idea about how Popen works) but I need to make sure (the systems guys called me on it) I use the same dtg as everyone else...it is possible (has not happened yet in 20 years) it could be set to something else. Is the example you gave using the new 2.4 Popen? It looks like the older popen. I can get the older popen to work but not Popen. My newest is 2.3.3 - I had no idea they modified popen for 2.4, but try this: dtg_s = Popen("/path/to/dtg", shell=True, stdout=PIPE).stdout If I've read the dox for 2.4 correctly, this has almost a fair chance of working... ;-) I cannot test this, however, as I don't have 2.4. (Glad I didn't upgrade, either -- I have several programs that rely on the present os.popen() ) here's a good page with how the new Popen works: http://www.python.org/peps/pep-0324.html with examples on how to replace os.popen[234]() and os.system(). HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] silly question
Rumor has it that Jason Child may have mentioned these words: ## I've got a silly question. ### P1 = "prefix1" P2 = "prefix2" def my_func(list, items): s = 0 out = "" for i in range(len(list)): if s == 0: p = P1 s = 1 else: p = P2 s = 0 for j in range(len(items)): out += p +items[j] return out If my input was: list = ["car list 1","car list 2"] items = ["torino","mustang","elantra"] for output I get: prefix1torinoprefix1mustangprefix1elantraprefix1torinoprefix1mustangprefix1elantra when I expect: prefix1torinoprefix1mustangprefix1elantraprefix2torinoprefix2mustangprefix2elantra Why not just do this: ### prefixes = ["prefix1" , "prefix2" ] def my_func(list, items): out = "" for prefixloop in prefixes: for itemloop in items: out += prefixloop + itemloop return out It's what's called a 'nested loop' - and that way if you had to add a 3rd prefix, it would "just work" instead of having to add yet another variable to the loop you'd designed. By the way, other than getting the length of 'list' you don't use anything in that list. Another thing (which might also be a problem in your code) using 'list' as a variable name is not a good idea, as it's a reserved word, if I'm not mistaken. Consider this snippet: Python 2.2.2 (#1, Feb 24 2003, 19:13:11) [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> q = (1,2,3) >>> print list(q) [1, 2, 3] Hope this helps, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | "Profile, don't speculate." SysAdmin, Iceberg Computers | Daniel J. Bernstein [EMAIL PROTECTED] | ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What am I doing wrong...
Rumor has it that Ken Stevens may have mentioned these words: I am a elative new comer to python. I wrote the following test snippet. #!/usr/bin/env python def main (): play_test() def play_test (): print "Hi! -- in play test" When I do "python test.py" absolutely nothing happens. Correct. I expect it to do the print statement. Thanks in advance for your help. You defined functions, but not the main code of the program, therefore it does nothing. Appended to your program, you need this: ### This defines where the actual program starts... if __name__ == '__main__': main() ### Now the program is done. ;-) Hope this Helps! Roger "Merch" Merchberger -- Roger "Merch" Merchberger | Anarchy doesn't scale well. -- Me [EMAIL PROTECTED] | SysAdmin, Iceberg Computers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] O.T.
Rumor has it that Bob Gailer may have mentioned these words: [snip] Programmed in more languages than I care to recall. The more interesting/arcane include Motorola 8080? Assembler and Machine language Yup - that sure is arcane -- It's either an Intel 8080 or a Motorola 6800. ;-) Me: 37, married 12 years, 3 kids (2 personally crafted ;-) one dog, and live in one of the oldest cities in the US. Been interested in computers since '76, but didn't get a chance to really work on them until '84. First computer: Tandy Color Computer 2. My computer languages: APL, COBOL, Basic09 (which is 1/2 Pascal), umpteen other dialects of Basic (for most 8-bitters on up), Logo, Moto6800/6809 Assembler & Machine Language, Atmel AVR Assembler, Pascal, Lisp (mostly AutoLisp), JCL, ColdFusion, some C, some PHP, enough Java to know I don't much care for it... and was a devout Perl follower until I found Python. If it isn't listed above, I prolly forgot I ever learned it! ;-) Interests: Old computers, still photography, cooking, I'm starting to teach myself woodworking, and if you consider it an interest: construction. My house is ancient, and sucks. I'm interested in making it "suck less," therefore, I'm interested in construction. ;-) Happy Hollydaze, Roger "Merch" Merchberger -- Roger "Merch" Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counting no of words
Rumor has it that Bill Mill may have mentioned these words: [snip] Once you're connected to a word document, you'll have to figure out what the command to count the words in the document is, but that's just a matter of recording a macro in word where you count the words, then repeating it in python. Another option would be to write the python program from within OpenOffice.org if it's available -- it has facilities of opening and accessing word documents as well. I'd help you with that, but I'm on linux. Most things I do are on Linux as well; but the OpenOffice solution should work equally well on either platform. HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] better resolution on time.sleep()?
I'm running an application that has a polling loop to check a serial port for certain signals, and on my laptop I can get about 6700 samples per second, which (of course) consumes 100% CPU; which may impact battery life. BTW, I'm running Python 2.2.2 on my laptop... not had a need to upgrade yet. I really only need between 500 and 1000 samples per second, but as the smallest sleep available normally is time.sleep(.01) -- which brings my samples/sec to a nice 100; but I need it a little faster. I found this: http://freshmeat.net/projects/rtsched/ which is supposed to bring real-time scheduling to Python (which the docs say is dangerous if used incorrectly) and also implementations of msleep/usleep/nanosleep. I downloaded it, compiled it, installed it, and popped in an import rtsched at the top and rtsched.msleep(1) where the time.sleep(.01) used to go... and now I get around 50 samples per second. I tried the usleep(1) and nanosleep(1) and the same thing happens. The rtsched implementation *is* working, as I can put in rtsched.msleep(100) and my samples/sec drops to 10. I'm guessing there's so much overhead to calling rtsched that it's b0rking up the works. :-/ Anyone know of: 1) a working millisleep/microsleep implementation for python 2.2+? -or- 2) will upgrading to a newer version of Python get me what I need? [[ I looked thru the "New in 2.4" dox and nothing I needed was listed...]] I've googled (off and on for more than a few days) and searched the archives, and came up empty so far... This isn't life or death by any means... My laptop known for long battery life, so everything should be OK as is, but why grind the CPU if it's not necessary, eh? ;-) Just a niggling little thing that's been on the back of my mind... [[ Oh, I found a reference to wxwindows that might have something that could work, but I was hoping for a little less overkill... my app barely uses ncurses. ]] Thanks! Roger "Merch" Merchberger -- Roger "Merch" Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] better resolution on time.sleep()?
Rumor has it that [EMAIL PROTECTED] may have mentioned these words: >Quoting Roger Merchberger <[EMAIL PROTECTED]>: > > > I really only need between 500 and 1000 samples per second, but as the > > smallest sleep available normally is time.sleep(.01) -- which brings my > > samples/sec to a nice 100; but I need it a little faster. > >Where did you find that out? Imperical testing -- I put in values smaller than .01, yet never got more than 100 samples per second. ;-) I then googled around and I saw a few other people found the same thing; but with no resolutions. I *can* post some ugly code if necessary... >I just did some experimenting ... I am running ActiveState Python 2.4.1 under >Windows XP / cygwin. I'm running under Linux From Scratch, book 4.0, kernel 2.4.29. [code snippage] >The first test is fairly consistent with time.sleep(0.001) doing what you >expect. The second is a bit puzzling to me, though... Yea, seems to be a bit of weirdness there... Hrm Anyway, maybe I'll go snag 2.4.x and see what that does for me... Laterz, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | Anarchy doesn't scale well. -- Me [EMAIL PROTECTED] | SysAdmin, Iceberg Computers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] better resolution on time.sleep()?
Rumor has it that Roger Merchberger may have mentioned these words: Yea, yea, I'm replying my own message you'll see why later! ;-) >Rumor has it that [EMAIL PROTECTED] may have mentioned these words: > >I just did some experimenting ... I am running ActiveState Python 2.4.1 > under > >Windows XP / cygwin. > >I'm running under Linux From Scratch, book 4.0, kernel 2.4.29. I downloaded and compiled Python 2.4.1 this evening, and the granularity of sleep hasn't changed on my system... maybe it's a kernel limitation? Hrm... Despite my pitiful C knowledge, I found a code snippet I modified to make a command-line "msleep" command which sleeps for 1 millisecond & exits, and called that with an os.system('msleep') call. The best I could get then is around 32 samples for second, so that seemed "marginally less efficient" calling my msleep command thru the OS compared to the "realtime usleep" function I downloaded earlier to run on Python 2.2.2. The realtime package wouldn't compile with Python 2.4.1 (not that it did me much good before... ;-). =-=-= I'm downloading the source to wxPython now, there is a wx.usleep function in there. As I mentioned, it seems a bit'o'overkill to me, but what the heck, in for a penny, in for a pound, eh? ;^> I'll let y'all know how it turns out... Laterz, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | Anarchy doesn't scale well. -- Me [EMAIL PROTECTED] | SysAdmin, Iceberg Computers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] better resolution on time.sleep()?
Rumor has it that Alan G may have mentioned these words: > > I'm running an application that has a polling loop to check a serial >port > > for certain signals, and on my laptop I can get about 6700 samples >per > > second, which (of course) consumes 100% CPU; which may impact >battery life. > >Consuming 100% CPU won't make much difference, the CPU is running all >the time anyway (when its not asleep). Not mine... my laptop runs a Crusoe processor, and it can automatically scale itself from 300Mhz to 933Mhz. It's called "LongRun" technology, and it works quite well, too. On my extended battery at "near idle" ... say, playing Spider, I get 5+ hours of battery life (and the battery's 2 years old, just like the laptop). Running the program at full tilt, I get just over 2 hours (but admittedly, it's scanning my USB->RS232 dongle, and I don't know how much current that may take... However, it's not transmitting, so it shouldn't be an overly huge amount.) I can run more tests if anyone's interested, but... Since the first Pentium (and possibly '486s) the CPU could put itself in a wait state to conserve CPU cycles that weren't being used -- and "wake itself up" automatically when needed. WinNT 4.0 and newer (and most *nixen) could use that, altho it didn't make *as much* difference for thermal or power aspects back then; the ability was there mainly for multiprocessor applications. Now they've improved it for both thermal and power usage functions as well. > What will consume batteries much >faster is if you are accessing hardware such as disk or modem or other >power hungry devices. I would only get about 1.5 hours (when the battery was new) compiling my LinuxFromScratch -- 100% CPU, HD getting hammered and snagging packages off of the CDROM... yea, the battery (and external charger) got quite a workout those few weeks... ;-) >I've never tried in Python but can you use select()? >I've used it in C for short duration intervals. Never used it yet (altho I saw references that Python's time.sleep() actually *uses* select instead of usleep/nanosleep. Not being a C hacker, I don't know much about it, but I'll check it out. Thanks! =-=-= I was also looking into trying the usleep implementation in wxPython -- well, I couldn't get the rascal compiled on my old Linux From Scratch version 4 installation (my main working Linux for that laptop) no matter what I turned off in .configure... I guess I should prioritize my LFS6 install a bit higher. ;-) Thanks one and all, and back to "quiet mode" I go... ;-) Roger "Merch" Merchberger -- Roger "Merch" Merchberger | "Profile, don't speculate." SysAdmin, Iceberg Computers | Daniel J. Bernstein [EMAIL PROTECTED] | ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Building an SQL query
Rumor has it that Alan G may have mentioned these words: >Hmm, I dunno ADOpy but assume it somehow miraculously turns your data >set into a dictionary of some sort? I dunno ADOpy, but the pg module for PostgreSQL can return a list of dictionaries from a query. >>> import pg >>> pg.set_defuser('example') >>> pg.set_defpasswd('example') >>> pg.set_defbase('spam') >>> pgpasswd = pg.DB() >>> pgpasswd.query('select dtg, classc, helo from ips limit 3;').dictresult() [{'helo': 'veda.cz', 'dtg': '2005-03-30', 'classc': '85.39.122'}, {'helo': 'ck336290-a.dokku1.fr.home.nl', 'dtg': '2005-03-30', 'classc': '217.123.211'}, {'helo': 'keymaster.com', 'dtg': '2005-03-30', 'classc': '220.73.88'}] *** output edited slightly with carriage returns *** > How it guesses which order the >SELECT will return the fields is a mystery to me, It's a mystery to some RDBs as well, IIRC with SQL there's no 'default behavior' -- if it's not defined, it can be spit out in any order it chooses; prolly depends on the implementation. It might also matter how the indices & primary keys are set up as to what comes out first... > but maybe it has >knowledge of the Postgres hashing function or somesuch. With dictionaries, it doesn't matter nearly so much. ;-) The ease with which data can be I/O'd thru PostgreSQL with Python is one of the main factors of my dumping Perl for it; I can spend more time diddling with the data than I need to do making the proggies run. ;-) HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | "Bugs of a feather flock together." sysadmin, Iceberg Computers | Russell Nelson [EMAIL PROTECTED] | ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Corrupt files
Rumor has it that geon may have mentioned these words: >Øyvind napsal(a): > > >Hello. > > > >I have created a program that automatically downloads some files I need. > >It can be .zip, .jpg, .mpg or .txt. However, a lot of the time urlretrieve > >downloads the file, gives no error, but the downloaded file is corrupted. > >I use a simple check for the size of the file. If it is way too small, I > >automatically remove it. > > > >remove it and try to download it again, lets say , 3times until you get >correct copy... Or, depending on it's extension, try to open the newly downloaded file with the corresponding Python extension, i.e. (.jpg|.gif|.png) try to open with PIL[1], .zip files with the python zipfile module, etc. I *think* there's modules for .mpgs and maybe .avi's... but .txt files might be tough to authenticate, as there's no actual 'file format.' This python pseudocode might explain more: =-=-=-=-=-=-= snip here =-=-=-=-=-=-= def openzip(filename): import zipfile try: filebuf = zipfile.open(filename) except: return "Error 99: cannot open file; corrupted zipfile." filebuf.close() return None if __name__ == '__main__': if filename[:-4] == '.zip': retcode = openzip(filename) if retcode != None: print "Corrupt File - Try to download again!" =-=-=-=-=-=-= snip here =-=-=-=-=-=-= Just put an 'openzip' or 'openjpg' type of function for each type of file you'll need to authenticate. [[ This code will *not* run -- it certainly won't run in Eudora (where it's being typed), so it's only for showing the general idea... YMMV, UAYOR, etc. ;-) ]] Hope this helps, Roger "Merch" Merchberger [1] PIL == Python Image Library; it will open almost any type of static graphic image available. -- Roger "Merch" Merchberger | "Bugs of a feather flock together." sysadmin, Iceberg Computers | Russell Nelson [EMAIL PROTECTED] | ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Does a module for DirectFB exist?
DirectFB is short for Direct Frame Buffer, and allows access to a graphical frame buffer system in *nix (and I think maybe MacOSX) from a text prompt without going through X. Anyone know of a module to access this through Python? I've googled for it, but didn't know if anyone here knew about something along these lines. Thanks, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | A new truth in advertising slogan SysAdmin, Iceberg Computers | for MicroSoft: "We're not the oxy... [EMAIL PROTECTED] | ...in oxymoron!" ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Does a module for DirectFB exist?
Rumor has it that Roger Merchberger may have mentioned these words: >DirectFB is short for Direct Frame Buffer, and allows access to a graphical >frame buffer system in *nix (and I think maybe MacOSX) from a text prompt >without going through X. > >Anyone know of a module to access this through Python? I've googled for it, >but didn't know if anyone here knew about something along these lines. Yea, I like talking to myself... This is just fer the archives anywho... The DirectFB can make use of the SDL which does access the hardware directly, and I did find a python module for libSDL, but it looked like it hadn't been supported for 3-4 years... according to the webpage, all of the functionality of that module was built into a module called PyGame. PyGame makes direct access to the hardware possible through SDL or DirectFB (altho for some reason the DirectFB component couldn't compile on my system. :-/ ) and with other SDL modules supports blitting graphics and TTF fonts directly to the framebuffer. It took a little work getting it working, but now I can print text & graphics of any size to the framebuffer without needing X to do it (and X is kinda limited when you're talking 200-pixel text rendering in an xterm... PyGame does it easily as it doesn't have xterm's restraints.). Anyway, I hope this helps someone else out there... Laterz, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | "Profile, don't speculate." SysAdmin, Iceberg Computers | Daniel J. Bernstein [EMAIL PROTECTED] | ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] curses delwin functionality?
Rumor has it that Bernd Prager may have mentioned these words: [snippety] > # curses.delwin(s) <-- that doesn't exist :-/ I've *only* done curses in python, so quite often I don't know the C analogue, but try: curses.endwin() I'm not sure if that's exactly what your looking for, but if not, try here: http://heather.cs.ucdavis.edu/~matloff/Python/PyCurses.pdf Google is your friend! ;-) HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger -- SysAdmin, Iceberg Computers [EMAIL PROTECTED] Hi! I am a .signature virus. Copy me into your .signature to join in! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor