[Tutor] Global presets ?
Hi there, I have some common data directories, like /home/dave/mygg/gg1.3/logs /home/dave/mygg/gg1.3/data /home/dave/mygg/gg1.3/datacore /home/dave/mygg/gg1.3/arch_data which increasing numbers of scripts are accessing. At the begining of each script I end up putting in declarations like arch_data_dir='/home/dave/mygg/gg1.3/arch_data' data_dir='/home/dave/mygg/gg1.3/data' over & over. This is OK until I want to move a directory Somewhere I read about importing a script to define common globals for all the scripts that import it. I tried this, and failed - the variable was only valid for the module, to be expected really :) Can anyone make a suggestion howto set up common global presets. Cheers Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Global presets ?
Thanks Guys, They are both good ways of getting round my problem, I appreciate your input & will have a play. Cheers Dave :-) :-) :-) :-) ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Global presets ?
Alan Gauld wrote: have you considered making the root directory an environment variable? That way you can read the value (os.getenv) at the start of the script. And if you ever need to move the structure you can simply change the environment value. It also means different users can use their own structures by defining their own environment value... # File myvars.py value1 = 42 value2 = 'spam' Got ya so far .. # # File: prog1.py import myvars localvar = myvars.value1 myvars.value2 = 'Alan' Never thought of setting 'myvars.value2 = 'Alan'' I guess this would just set the variable in the myvars namespace since it could not change myvars.py itself. ## # File prog2.py import myvars newvar = myvars.value2 With you ... print myvars.value1 - 27 Have I misunderstood, should this not be 42 ? Typo or me not understanding ? ## Does that help? Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Accuracy of time.sleep()
OK I may be pushing it, ;-) I need a script to sleep from any point to 8:05AM when in needs to re-start. So I calculate the number of seconds with the following def secs_till_805(): # Returns the number of seconds till 8:05AM secs_5min=5*60 secs_24hr=24*60*60 secs_8hr=(8*60*60)+secs_5min secs_8hr_24hr=secs_24hr-secs_8hr hours=int(strftime('%H')) mins=int(strftime('%M')) secs=int(strftime('%S')) sec_left=secs_24hr-((hours*60*60)+(mins*60)+secs) # If we are before 8:05, then ... if sec_left>secs_8hr_24hr: return sec_left-secs_8hr_24hr # If we are after 8:05, then ... return sec_left+secs_8hr Then I ... sleep(secs_till_805()) I expected the script to re-start 2-3 seconds after 8:05, python reloading after a long sleep etc, what I get is the script restarting at 08:04.55, earlier ??? OK this is not a world stopping problem, more of a curiosity. Any suggestions Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accuracy of time.sleep()
I expected the script to re-start 2-3 seconds after 8:05, python reloading after a long sleep etc, what I get is the script restarting at 08:04.55, earlier ??? OK this is not a world stopping problem, more of a curiosity. Any suggestions Dave Thanks for your input guys, I have used cron (fcron for me) in the past (Im a Gentoo Linux guy :-) ) I was just trying to keep it all pure python. As I said its more of a curiosity. It must be cummulative error over 10s of thousands of seconds. Its a bodge (& cron or at are better) but I suppose I could calculate seconds to 8:05 sleep(seconds*0.95), re calculate secs to 8:05 sleep(seconds) which should reduce the error to almost zip. Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accuracy of time.sleep()
Tim Peters wrote: First, thank you for such a brilliant answer :-) [Dave S <[EMAIL PROTECTED]>] OK I may be pushing it, ;-) Yup . I need a script to sleep from any point to 8:05AM when in needs to re-start. So I calculate the number of seconds with the following def secs_till_805(): # Returns the number of seconds till 8:05AM secs_5min=5*60 secs_24hr=24*60*60 secs_8hr=(8*60*60)+secs_5min secs_8hr_24hr=secs_24hr-secs_8hr hours=int(strftime('%H')) mins=int(strftime('%M')) secs=int(strftime('%S')) Ouch. Never try to pick apart the current time by computing it more than once. For example, if the time at the start of that block is just a fraction of a second before 9AM, it's quite possible you'll end up with hours==8 and mins==secs==0 (because the time is 8:59:59 at the time you do the "%H" business, and but it's 9:00:00 by the time you get to "%M"). That would throw you off by an hour. The same kind of thing can happen a little before the (any) minute changes too. This is a possibility that had not enterd my mind, but also very true. Thanks for saving me from that particular black hole. Its always that 1 in a thousand possibility that sends things south at the worst possible moment ! sec_left=secs_24hr-((hours*60*60)+(mins*60)+secs) # If we are before 8:05, then ... if sec_left>secs_8hr_24hr: return sec_left-secs_8hr_24hr # If we are after 8:05, then ... return sec_left+secs_8hr Here's a different way, computing current time only once, and using the datetime module to do all the fiddly work: def seconds_until(h, m=0, s=0): from datetime import datetime, time, timedelta target_time = time(h, m, s) now = datetime.now() target = datetime.combine(now.date(), target_time) if target < now: target += timedelta(days=1) diff = target - now return diff.seconds + diff.microseconds / 1e6 This returns seconds as a float, which is good (Python's time.sleep() can make sense of floats, and sleep for fractional seconds). OK Im pydoc'ing & looking at datetime, a module I have not explored before. This is stretching me a bit but its a good way to learn. Then I ... sleep(secs_till_805()) With the above, you'd do time.sleep(seconds_until(8, 5)) instead. I expected the script to re-start 2-3 seconds after 8:05, python reloading after a long sleep etc, what I get is the script restarting at 08:04.55, earlier ??? You'll probably never know why for sure. Python calls platform C library gimmicks to sleep, which in turn invoke operating system facilities. Understanding the whole story would require that you understand everything all of those do. If only I had the time ... (no pun intended) [later] It must be cummulative error over 10s of thousands of seconds. Maybe. Its a bodge (& cron or at are better) but I suppose I could calculate seconds to 8:05 sleep(seconds*0.95), re calculate secs to 8:05 sleep(seconds) which should reduce the error to almost zip. That's also a good idea in order to avoid surprises due to crossing daylight time boundaries (assuming you mean 8:05 according to the local wall clock). Here's a function building on the above: def sleep_until(h, m=0, s=0): from time import sleep while True: delay = seconds_until(h, m, s) if delay < 10.0: sleep(delay) return else: sleep(delay / 2) Thats neat, and more elegent than my hamfisted attempt, I err might borrow it for my code, on a tempory basis you understand ;-) sleep_secs=secs_till_805() log('II','ftsed','Sleeping for '+str(sleep_secs)+' Seconds') # To compensate for the commulative error over 86,000 secs ! sleep(sleep_secs*0.95) sleep(secs_till_805()) That is, it cuts the sleep time in half repeatedly, until less than 10 seconds remain. It can sleep for hours at a time, but as the target time approaches it wakes up more frequently. This should keep the program loaded in memory as the target time gets near. Cheers Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Global presets ?
Brian van den Broek wrote: Hi Dave, Kent, and all, I have a caution about the from Config import * idiom that Kent didn't mention. It can lead to namespace pollution, in that if you have a module 'foo' with a name 'bar' and you are witting a script which says from foo import * you have to be very careful that your script doesn't also assign to the name 'bar', else you may end up thinking you have two different things available when you don't. ('bar' will either point to your script's bar or to Config.bar, depending on whether you imported Config before or after your scripts assignment to bar.) The first time this bites you, it can eat up hours of your life. (But I'm not bitter;-) I avoid this by using the import examplemodule as em That imports everything so that you accesses it by em.some_name rather than examplemodule.some_name I find that really handy for the handful of utility modules I import into most of my scripts. Then, I just have to be sure to avoid a small set of names -- 'em' in this case. And my python files have nice descriptive names, but I only have to type then once. Best, Brian vdB ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor Thanks for pointing that out. Im a bit of a Python coward and opted for from config import data_dir,HTML_addr Mainly so I can see where these variables come from. I have never seen the 'as' operator on an import before, so much to learn (and remember) ;-) Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Accuracy of time.sleep()
Jacob S. wrote: You know, since time.sleep() builds up errors, this is what I do to keep it purely pythonic... (not tested) from time import gmtime alarmhr = 8 alarmmin = 5 alarmsec = 0 while 1: t = gmtime() hour = t[3] min = t[4] sec = t[5] if (alarmhr,alarmmin,alarmsec) == (hour,min,sec): print "It is 8:05 AM. Please do whatever you are supposed to at this time. raw_input() break Yep this is an option that makes sense to me, getting time once & breaking it down with []'s to avoid the trap I almost fell into. I know cron is probarbly the way to go but Its kind of nice to keep it all Python if you know what I mean ;-) Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python structure advice ?
Im sorry to bang on about Python structure, but I do struggle with it, having in the past got into very bad habits with loads of BASIC where everything was global, and Forth, and hand coded 8031, 8051, 6502 I cant get my head round how you guys handle a modern structured language :-) (PS before anyone flames me - I think Python is great and am determined to learn it ;-) ) I have ended up with my application in several separate directories. I have 'live_datad' a demon that extracts web data, at preset times and archives it, this will be run as a thread, and possible using a queue ... (still digesting info from query about IPCing) I have a 'data_core' which accepts data from either live_datad real time or the archive for testing, it builds up a large multi dimensional array with various pointers into the array. I have a statistical module 'data_stats' which analises the array pulling various stats. And finally I have an analytical module 'data_predict' which using the output from 'data_stats' & data directly from the 'data_core' outputs statistical predictions of future data. I have written my 'live_datad', I have written my 'data_core' & have a fairly good idea how to write the rest. My problem is that pretty much all the modules need to fix where they are when they exit and pick up from that point later on, ie more data comes from live_datad, it is passed to 'data_core' which updates the matrix, then 'data_stats' then 'data_predict' all called form the main script. This OK till the main script realizes that more data is avalible from 'live_datad', passes it to 'data_core' which must remember where it was and move on, and the same for the rest of the modules. To make the problem more acute the modules may not be called in exactly the same order depending on what I am trying to achieve. The 'remembering where is was' seems a continuous stumbling block for me. I have though of coding each module as a class but this seems like a cheat. I could declare copious globals, this seems messy, I could define each module as a thread & get them talking via queues, given this serious thought but heeded warning in previous posts. I have thought about returning an list of saved 'pointers' which would be re-submitted when the function is called. I don't know which way to turn. With my code now running to a few hundred lines (Don't laugh this is BIG for me :-D ) I am going to have to make a structure decision and any suggestions would be appreciated. How would you approach it ? Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Dave S wrote: Im sorry to bang on about Python structure, but I do struggle with it, having in the past got into very bad habits with loads of BASIC where everything was global, and Forth, and hand coded 8031, 8051, 6502 I cant get my head round how you guys handle a modern structured language :-) (PS before anyone flames me - I think Python is great and am determined to learn it ;-) ) I have ended up with my application in several separate directories. I have 'live_datad' a demon that extracts web data, at preset times and archives it, this will be run as a thread, and possible using a queue ... (still digesting info from query about IPCing) I have a 'data_core' which accepts data from either live_datad real time or the archive for testing, it builds up a large multi dimensional array with various pointers into the array. I have a statistical module 'data_stats' which analises the array pulling various stats. And finally I have an analytical module 'data_predict' which using the output from 'data_stats' & data directly from the 'data_core' outputs statistical predictions of future data. I have written my 'live_datad', I have written my 'data_core' & have a fairly good idea how to write the rest. My problem is that pretty much all the modules need to fix where they are when they exit and pick up from that point later on, ie more data comes from live_datad, it is passed to 'data_core' which updates the matrix, then 'data_stats' then 'data_predict' all called form the main script. This OK till the main script realizes that more data is avalible from 'live_datad', passes it to 'data_core' which must remember where it was and move on, and the same for the rest of the modules. To make the problem more acute the modules may not be called in exactly the same order depending on what I am trying to achieve. The 'remembering where is was' seems a continuous stumbling block for me. I have though of coding each module as a class but this seems like a cheat. I could declare copious globals, this seems messy, I could define each module as a thread & get them talking via queues, given this serious thought but heeded warning in previous posts. I have thought about returning an list of saved 'pointers' which would be re-submitted when the function is called. I don't know which way to turn. With my code now running to a few hundred lines (Don't laugh this is BIG for me :-D ) I am going to have to make a structure decision and any suggestions would be appreciated. How would you approach it ? Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor Having written this email, it has put my thoughts in order, though it seems a bit cheaty, wouldn't defining all modules that have to remember their internal state as classes be the best bet ? Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Kent Johnson wrote: Dave S wrote: Dave S wrote: The 'remembering where is was' seems a continuous stumbling block for me. I have though of coding each module as a class but this seems like a cheat. I could declare copious globals, this seems messy, I could define each module as a thread & get them talking via queues, given this serious thought but heeded warning in previous posts. I have thought about returning an list of saved 'pointers' which would be re-submitted when the function is called. I don't know which way to turn. Having written this email, it has put my thoughts in order, though it seems a bit cheaty, wouldn't defining all modules that have to remember their internal state as classes be the best bet ? Dave Why do you say this is 'cheaty'? A class is basically a collection of data (state) and functions to operate on that state. Sorry for the delay, real world work got in the way ... Well I understand classes to be used when multiple instances are required, I will only need one instance and as such it seemed a bit of a cheat, The trouble is I now pretty well understand the tools, but don't know how you guys use them in the real world. You might be interested in this essay: http://www.pycs.net/users/323/stories/15.html I found this particularly usefull, It might well make sense to organize your program as a collection of cooperating classes, or maybe a collection of classes with a top-level function that stitches them all together. Yes, this is the way I see things progressing, from 20,000ft this makes a lot of sense. You might also want to learn about iterator classes and generator functions, they are a technique for returning a bit of data at a time while maintaining state. You might be able to structure your input stage as an iterator or generator. http://docs.python.org/tut/node11.html#SECTION001190 http://docs.python.org/lib/typeiter.html I remeber iterators from 'learning python', I was concerned about several modules all 'having a iterator' to the next, debuging would be scary ! I think I will go the class route. Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Sorry for the delay, real world work took me away ... everything was global, how you guys handle a modern structured language Don't worry this is one of the hardest bad habits to break. You are not alone. The easiest way is to just pass the data from function to function in the function parameters. Its not at all unusual for functions to have lots of parameters, "global" programmers tend to panic when they have more than a couple, yep ! but its not at all bad to have 5 or 6 - more than that gets unweildy I admit and is usually time to start thinking about classes and objects. I have ended up with my application in several separate directories. Separate modules is good. Separate directories for anything other than big programs (say 20 or more files?) is more hassle than its worth. The files are better kept in a single directory IMHO. The exception being modules designed for reuse... It just makes life simpler! Ive tried to be hyper organized and added my dirs in /usr/lib/python2.3/site-packages/mypath.pth /home/dave/mygg/gg1.3/live_datad /home/dave/mygg/gg1.3/logger /home/dave/mygg/gg1.3/utils /home/dave/mygg/gg1.3/datacore /home/dave/mygg/gg1.3 /home/dave/mygg/gg1.3/configs This works OK but I sometimes have to search around a bit to find where the modules are. Probarby part of the problem is I tend to write lots of small modules, debug them & then import them into one controlling script, It works OK but I start to drown in files, eg my live_datad contains ... exact_sleep.py garbage_collect.py gg ftsed.e3p html_strip.py live_datad.py valid_day.pyc exact_sleep.pyc garbage_collect.pyc gg ftsed.e3s html_strip.pyc valid_day.py When I get more experienced I will try & write fewer, bigger modules :-) My problem is that pretty much all the modules need to fix where they are when they exit and pick up from that point later on, There are two "classic" approaches to this kind of problem: 1) batch oriented - each step of the process produces its own output file or data structure and this gets picked up by the next stage. Tis usually involved processing data in chunks - writing the first dump after every 10th set of input say. This is a very efficient way of processing large chuinks of data and avoids any problems of synchronisation since the output chunks form the self contained input to the next step. And the input stage can run ahead of the processing or the processing aghead of the input. This is classic mainframe strategy, ideal for big volumes. BUT it introduces delays in the end to end process time, its not instant. I see your point, like a static chain, one calling the next & passing data, the problem being that the links of the chain will need to remember their previous state when called again, so their output is a function of previous data + fresh data. I guess their state could be written to a file, then re-read. 2) Real time serial processing, typically constructs a processing chain in a single process. Has a separate thread reading the input data Got that working live_datad ... and kicks off a separate processing thread (or process) for each bit of data received. Each thread then processes the data to completion and writes the output. OK A third process or thread then assembles the outputs into a single report. Interesting ... This produces results quickly but can overload the computer if data starts to arrive so fast that the threads start to back up on each other. Also error handling is harder since with the batch job data errors can be fixed at the intermediate files but with this an error anywhere means that whole data processing chain will be broken with no way to fix it other than resubmitting the initial data. An interesting idea, I had not thought of this approach as an option even with its stated drawbacks. Its given me an idea for some scripting I have to do later on ... With my code now running to a few hundred lines (Don't laugh this is BIG for me :-D ) Its big for me in Python, I've only writtenone program with more than a thousand lines of Python wheras I've written many C/C++ programs in ecess of 10,000 lines Boy am I glad I chose to learn Python rather than C++, probarbly still be at 'hello world' ;-) and worked on several of more than a million lines. But few if any Python programs get to those sizes. HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Jeff Shannon wrote: Dave S wrote: Kent Johnson wrote: Why do you say this is 'cheaty'? A class is basically a collection of data (state) and functions to operate on that state. Sorry for the delay, real world work got in the way ... Well I understand classes to be used when multiple instances are required, I will only need one instance and as such it seemed a bit of a cheat, The trouble is I now pretty well understand the tools, but don't know how you guys use them in the real world. For what it's worth, it seems to me to be perfectly normal to have classes that are only ever intended to have a single instance. For example, you're never likely to need more than one HTML parser, and yet htmllib.HTMLParser is a class... Well if its good enough for a Python lib ... As Kent said, the main point of a class is that you have a collection of data and operations on that data bundled together. Whether you have one set of data to operate on, or many such sets, is mostly irrelevant (though classes are even more valuable when there *are* many sets of data). Defining a class isn't so much a statement that "I want lots of things like this", as it is a declaration of modularity -- "This stuff all belongs together as a unit". OK Im a reformed ('L' plate programmer) its going to be classes :-) Jeff Shannon Technician/Programmer Credit International ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Alan Gauld wrote: 1) batch oriented - each step of the process produces its own output file or data structure and this gets picked up by the next stage. Tis usually involved processing data in chunks - writing the first dump after every 10th set of input say. I see your point, like a static chain, one calling the next & passing data, the problem being that the links of the chain will need to remember their previous state when called again, so their output is a function of previous data + fresh data. I guess their state could be written to a file, then re-read. Yes. Just to expand: the typical processing involves three files: 1) the input which is the output of the preceding stage 2) the output which will form input to the next stage 3) the job log. This will contain references to any input data items that failed to process - typically these will be manually inspected, corrected and a new file created and submitted at the end of the batch run. BUT 3) will also contain the sequence number of the last file and/or last data item processed so that when the next cycle runs it knows where to start. It is this belt and braces approach to data processing and error recovery that makes mainframes so reliable, not just the hardware, but the whole culture there is geared to handling failure and being able to *recover* not just report on it. After all its the mainframes where the really mission critical software of any large enterprise runs! As an ex Unix head I learned an awful lot about reliable computing from the 18 months I spent working on a mainframe project. These guys mostly live in a highly specialised microcosm of their own but they have learned a lot of powerful tricks over the last 40 years that the rest of us ignore at our peril. I strongly recommend that anyone who gets the chance of *a short* contract in mainframe land, with training, to grab the opportunity with both hands! < Steps off soapbox now :-) > Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld You get on that soapbox whenever you want :-) , its good to hear a range of views ! Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python structure advice ?
Kent Johnson wrote: Dave S wrote: Separate modules is good. Separate directories for anything other than big programs (say 20 or more files?) is more hassle than its worth. The files are better kept in a single directory IMHO. The exception being modules designed for reuse... It just makes life simpler! Ive tried to be hyper organized and added my dirs in /usr/lib/python2.3/site-packages/mypath.pth /home/dave/mygg/gg1.3/live_datad /home/dave/mygg/gg1.3/logger /home/dave/mygg/gg1.3/utils /home/dave/mygg/gg1.3/datacore /home/dave/mygg/gg1.3 /home/dave/mygg/gg1.3/configs This works OK but I sometimes have to search around a bit to find where the modules are. Probarby part of the problem is I tend to write lots of small modules, debug them & then import them into one controlling script, It works OK but I start to drown in files, eg my live_datad contains ... exact_sleep.py garbage_collect.py gg ftsed.e3p html_strip.py live_datad.py valid_day.pyc exact_sleep.pyc garbage_collect.pyc gg ftsed.e3s html_strip.pyc valid_day.py When I get more experienced I will try & write fewer, bigger modules :-) It's just a guess from the filenames, but it looks like your live_datad package (directory) contains everything needed by live_datad.py. Spot on I would like to suggest a different organization. I tend to organize packages around a single functional area, and by looking at the dependencies of the modules in the package on other packages. For example, in my current project some of the packages are: - common.util - this is a catchall for modules that are not specific to this application, and don't depend on any other packages - common.db - low-level database access modules - cb.data - application-specific database access - the data objects and data access objects that the application works with - cb.import - modules that import legacy data into the application - cb.writer - modules that generate files - cb.gui - GUI components - cb.app - application-level drivers and helpers I have been getting in a muddle, html_strip.py, strips HTML, mines for data & when it finds specific patterns returns a dictionary containing them. However I also use one of its functions in a utility convert_data.py reading in archived semi-processed HTML files. This cross dependance has occured several times and is getting messy, yours is an interesting approach, Its started me thinking... Anyway, the point is, if you organize your modules according to what they do, rather than by who uses them, you might make a structure that is less chaotic. HTH Kent ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Problems with class, no output
Hello again :-) , This script is very much a work in progress, and I think only the second time I have tried classes. What I expected it to do was to at least print 'hi there' so I know its alive, then dump some status info to my log program. It does zip, executes OK but no output. I just know you guys are going to tell me I have missed a () or . somewhere Any help gratefully appreciated Dave #!/usr/bin/env python # -*- coding: iso8859_1 -*- """ arch_data.py 1.0 This is the read archive data script """ from os import listdir from cPickle import load from logger import log from config import data_dir debug=True class Arch_data: def __init__(self): self.file_names=listdir(data_dir) self.file_names.sort() self.file_name_ptr=0 def read_next(): """ when executed reads next data file from data_dir extracts date, time,julian day,sequence count & flag from the file name. Also adds last_file flag and contents of file to the list of returned parameters """ print 'hi there' file_name=self.file_names[self.file_name_ptr] # Ignore the archive_read.py utility script in the data dir ##if file_name=='archive_read.py': ##if debug: ##log('II','arch_data','Ignoring "archive_read.py"') ##self.file_name_ptr+=1 ##read.next() ##return # Extract info from the encoded filename :-) file_yy=file_name[:4] file_mm=file_name[4:6] file_dd=file_name[6:8] file_HH=file_name[9:11] file_MM=file_name[11:13] file_jj=file_name[14:17] file_seq=file_name[18:21] file_flag=file_name[22:24] if debug: log('II','arch_data','unpickling '+file_name) pickle_file=open(data_dir+file_name,'r') file=load(pickle_file) pickle_file.close() print file_yy,file_mm,file_dd,file_HH,file_MM,file_jj,file_seq,file_flag self.file_name_ptr+=1 arch_data=Arch_data() arch_data.read_next ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems with class, no output
Dave S wrote: Hello again :-) , This script is very much a work in progress, and I think only the second time I have tried classes. What I expected it to do was to at least print 'hi there' so I know its alive, then dump some status info to my log program. It does zip, executes OK but no output. I just know you guys are going to tell me I have missed a () or . somewhere Any help gratefully appreciated Dave #!/usr/bin/env python # -*- coding: iso8859_1 -*- """ arch_data.py 1.0 This is the read archive data script """ from os import listdir from cPickle import load from logger import log from config import data_dir debug=True class Arch_data: def __init__(self): self.file_names=listdir(data_dir) self.file_names.sort() self.file_name_ptr=0 def read_next(): """ when executed reads next data file from data_dir extracts date, time,julian day,sequence count & flag from the file name. Also adds last_file flag and contents of file to the list of returned parameters """ print 'hi there' file_name=self.file_names[self.file_name_ptr] # Ignore the archive_read.py utility script in the data dir ##if file_name=='archive_read.py': ##if debug: ##log('II','arch_data','Ignoring "archive_read.py"') ##self.file_name_ptr+=1 ##read.next() ##return # Extract info from the encoded filename :-) file_yy=file_name[:4] file_mm=file_name[4:6] file_dd=file_name[6:8] file_HH=file_name[9:11] file_MM=file_name[11:13] file_jj=file_name[14:17] file_seq=file_name[18:21] file_flag=file_name[22:24] if debug: log('II','arch_data','unpickling '+file_name) pickle_file=open(data_dir+file_name,'r') file=load(pickle_file) pickle_file.close() print file_yy,file_mm,file_dd,file_HH,file_MM,file_jj,file_seq,file_flag self.file_name_ptr+=1 arch_data=Arch_data() arch_data.read_next ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor Dope, too quick to ask for help - sorry def read_next(self): and arch_data.read_next() sorted it :-[ ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problems with class, no output
Kent Johnson wrote: Dave, For some reason the indentation is very strange in this code. I think it must have something to do with how you posted it (or my mailer is messing it up?), as it would be a syntax error at runtime. If you can have more consistent indentation in your posts it will make them much easier to read. Glad you got it working! Kent Point taken, I had copied & pasted into my email, looked OK when I pasted it, but on re-opening my own email, Yuk. I will attatch file in future, Cheers Dave ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Marry Xmas + httplib.py problem :)
First, merry Xmas everyone, wishing you all well, :-) While my head is still clear ;-) , I have an unexpected problem. One of my apps which tracks a ftse website all day has suddenly popped up with an exception & exited somewhat ungraciously. This is the second time this has occured (512KB Broadband) bash-2.05b$ ./live_datad.py Traceback (most recent call last): File "./live_datad.py", line 196, in ? live_datad() File "./live_datad.py", line 189, in live_datad ftse_day() File "./live_datad.py", line 142, in ftse_day new_data=html_strip(load_webpage()) File "./live_datad.py", line 33, in load_webpage url_data=urlopen(HTML_addr) File "/usr/lib/python2.3/urllib.py", line 76, in urlopen return opener.open(url) File "/usr/lib/python2.3/urllib.py", line 181, in open return getattr(self, name)(url) File "/usr/lib/python2.3/urllib.py", line 297, in open_http h.endheaders() File "/usr/lib/python2.3/httplib.py", line 712, in endheaders self._send_output() File "/usr/lib/python2.3/httplib.py", line 597, in _send_output self.send(msg) File "/usr/lib/python2.3/httplib.py", line 564, in send self.connect() File "/usr/lib/python2.3/httplib.py", line 532, in connect socket.SOCK_STREAM): IOError: [Errno socket error] (-3, 'Temporary failure in name resolution') bash-2.05b$ Is this is a DNS problem, if so its very strange since my app has downloaded approx 60 web pages from this location today just before the exception occured ?, or maybe something else ? Should I check for this exception when I ... new_data=html_strip(load_webpage()) & if it occures, try again ? Any comments gratefully recieved. Cheers (Hik!) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] simple list query
OK simple query, I have a list consisting of about 250 items, I need to know if a particular item is in the list. I know this is better suited to a dictionary but thats not the way it ended up ;-) I could do a for loop to scan the list & compare each one, but I have a suspission that there is a better way ? Any suggestions Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple list query
Patrick Hall wrote: Hi Dave, I have a list consisting of about 250 items, I need to know if a particular item is in the list. I know this is better suited to a dictionary but thats not the way it ended up ;-) I could do a for loop to scan the list & compare each one, but I have a suspission that there is a better way ? Indeed there is: just use the built-in "in": li = ['a', 'b', 'c', 'd', 'e', 'f', 'a', 'b', 'c'] 'a' in li True 'z' in li False That's a small example, but it will work equally well with a long list. For instance, we can check to see if the words "Ahab", "whale", and "pizza" are in the text of "Moby Dick" (I have the text in a file called "moby.txt".) moby = open('moby.txt').read() # Moby Dick, the whole thing! mobywords = moby.split() # now mobywords has all the words in the text 'Ahab' in mobywords True 'whale' in mobywords True 'pizza' in mobywords False These results are unsurprising. 8^) A list of 250 words is no problem -- "Moby Dick" has a couple hundred thousand: len(mobywords) 214112 I'm not sure I understand why you think a dictionary would be better in this case, a list seems fine to me. Best, Pat ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thanks guys, something was rattling round by brain that there was a way, but I just could not work it out :-) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Am I storeing up problems ?
Hi there again, My matrix 'self.data' consists of a list of 110 items, each item is a dictionary of 250 keys, each key holds two lists, one of four items, one of 12 items. I needed to copy this matrix to 'self.old_data', so I have been using .deepcopy(), which works OK but is SLOW (12+ secs) Once the matrix is copied, 'self.data' is re-constructed as the programme gathers data. To speed things up I changed my code to # This is the speeded up deepcopy() self.old_data = self.data self.data = [] for i in range(110): self.data.append({}) Query: Is all this OK, it works and quick as well, but I am concerned I may be leaving garbage in the Python PVM since I am shrugging off old 'self.old_data's which may be mounting up ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Am I storeing up problems ?
Danny Yoo wrote: On Sun, 2 Jan 2005, Dave S wrote: My matrix 'self.data' consists of a list of 110 items, each item is a dictionary of 250 keys, each key holds two lists, one of four items, one of 12 items. Hi Dave, Hmmm... what kind of data is being copied here? Python's data structures are desigined for flexibility, but sometimes that flexibility comes at the cost of some space. If we knew more about the data that's being stored, maybe we can think of a more compact representation. OK my app is tracking the ftse 250, the 110 items are time slots through a trading day, the 250 keys are ftse company names added dynamicly , the two lists are (4) a series of linked cookies to transmit info forward per company, and (12) financial stats for the particular company at a particular time. Companies in the 250 change randomly and there are not always exactly 250 (realy!), a linked cookie system is needed for a variety of info going forward not least as a count of how long any particular company has been on the ftse 250. The copy is needed so two days ftse figures can be viewed at any one time. Thurther apps can pull out ftse figures for a particular company going backwards .. you get the idea ! It all works & its been a great learning experience to code. I needed to copy this matrix to 'self.old_data', so I have been using .deepcopy(), which works OK but is SLOW (12+ secs) Perhaps we can avoid making a separate copy of the data? Copying data structures can often be avoided. Why does your program try to copy the data structure? (Aside: one nonobvious example where copying can be avoided is in Conway's Game of Life: when we calculate what cells live and die in the next generation, we can actually use the 'Command' design pattern to avoid making a temporary copy of the world. We can talk about this in more detail if anyone is interested.) Once the matrix is copied, 'self.data' is re-constructed as the programme gathers data. To speed things up I changed my code to # This is the speeded up deepcopy() self.old_data = self.data self.data = [] for i in range(110): self.data.append({}) Query: Is all this OK, it works and quick as well, but I am concerned I may be leaving garbage in the Python PVM since I am shrugging off old 'self.old_data's which may be mounting up ? Whenever we reassign to self.old_data: self.old_data = self.data then whatever self.old_data was pointing at, before the assignment, should get garbage collected, if there are no other references to the old data. The code there isn't really doing any copying at all, but is rather just constructing a whole new empty data structure into 'self.data'. If so, then it does seem that you can avoid copying. I'd love to know a little bit more about the data that the program is collecting; if you have time, please feel free to tell us more details. Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Am I storeing up problems ?
Alan Gauld wrote: I needed to copy this matrix to 'self.old_data', so I have been using .deepcopy(), which works OK but is SLOW (12+ secs) Are you sure you need to copy it./ A simple reassignment should work and then reconstruct the structure using fresh lists/dictionary etc. That should work faster. Alan G. Yep this is what I ended up doing, 12+ secs becomes 0.5 sec :-) My query was To speed things up I changed my code to # This is the speeded up deepcopy() self.old_data = self.data self.data = [] for i in range(110): self.data.append({}) Query: Is all this OK, it works and quick as well, but I am concerned I may be leaving garbage in the Python PVM since I am shrugging off old 'self.old_data's which may be mounting up ? But apparently this is ok (as he cancelles order gor 1GB RAM ;-) ) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] 2d list matrix 7 x 75 problem
Hello, I need to generate a list 2d matrix of the kind ... [['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']] except its dimensions need to be 7 x 75. I thought I had it sorted with map2 = [ [''] *7 ] *75 until the coding screwed up & I realised I had 75 references to the same list :-( so I thought I would be clever with ... >>> a=[0]*5 >>> a [0, 0, 0, 0, 0] >>> b=[a[:]]*5 same problem. It seems realy simple but how do I generate a 7 x 75 list matrix ? Dave Oh PS Is there a more elegant solution to if string == 'sun' or string == 'mon' or string == 'tue' or string == 'wed' or string == 'thu' or string == 'fri' or string == 'sat': the above works but any suggestions would be welcome :-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] 2d list matrix 7 x 75 problem
Jeff Shannon wrote: On Sun, 06 Mar 2005 09:49:43 +, Dave S <[EMAIL PROTECTED]> wrote: I need to generate a list 2d matrix of the kind ... [['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', ''], ['', '', '', '', '']] except its dimensions need to be 7 x 75. I thought I had it sorted with map2 = [ [''] *7 ] *75 until the coding screwed up & I realised I had 75 references to the same list :-( Try: map2 = [['']*7 for n in range(75)] The list comprehension will execute ['']*7 each iteration, creating a new list instead of just creating new references to the same list. Oh PS Is there a more elegant solution to if string == 'sun' or string == 'mon' or string == 'tue' or string == 'wed' or string == 'thu' or string == 'fri' or string == 'sat': Try: if string in ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat']: (Or perhaps look into using the datetime module, depending on how detailed your needs are.) Jeff Shannon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Many thanks Dave :-) :-) :-) :-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Acessing files in Windows 2000
I have a script that converts data relating to my work. It works great on my Linux system but some of my colleagues run windows. I am attempting to convert the file paths to windows but am having no luck. I need to access 'memo.txt' in 'my documents' on windows & am struggling. I have tried just about every combination of \ and / and \\ and // but to no avail. I need something like ... C:\My Documents\memo.txt Can anyone advise me ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Acessing files in Windows 2000
John Purser wrote: Try c:\\my documents\\memo.txt John Unfortunately thats a no go ... palm.py palm memo.txt to oocalc data.csv convertor, written by lentil ;) [EMAIL PROTECTED] - enter "palm software" in the title or it will be junked 09-03-2005 v1.02w Coded for Windows 07-03-2005 v1.02 Coded capitalization() + default to mon 03-03-2005 v1.01 Squished 1/9 decode bug 02-03-2005 v1.00 Coded for Linux Traceback (most recent call last): File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 268, in ? palm_conv() File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 54, in palm_conv memo = open(memo_file, 'r') IOError: [Errno 2] No such file or directory: 'c:\\my documents\\memo.txt' >>> If I try c:\\ or c:\ it always comes out as c:\\... and fails Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Acessing files in Windows 2000
Danny Yoo wrote: [Windows bashing cut] Python's support for Windows stuff is actually quite good, thanks to the work of Mark Hammond: http://starship.python.net/crew/mhammond/ A lot of us here do use Windows for COM programming. Let's get back to talking about Python programming and let's help Dave with his problem. The os.path module contains a function called expanduser() that returns the home directory of a given user. http://www.python.org/doc/lib/module-os.path.html#l2h-1720 I believe this should reliably return a path that looks something like '\\Documents and Settings\\[username]' on Windows systems. Dave, what happens if you try something like this? ## import os.path print os.path.expanduser('~/memo.txt') f = open(os.path.expanduser('~/memo.txt')) ## Show us what you see, and we can work from there. Can you also give us the complete path where you see 'memo.txt' on your system? Just right click the file; it should show up somewhere on the file's property page. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Hello again, Wow what a lot of replies. OK here goes. I know little about win 2000 so when I My computer > C > My documents I assumed that was where the my documents folder was, Having gone through what you guys said I also have C:\Documents and Settings\Administrator\My Documents So I copied memo.txt to C:\Documents and Settings\Administrator\My Documents, ammended my code memo_file = 'C:\Documents and Settings\Administrator\My Documents\memo.txt' csv_file = 'c:\data.csv' def palm_conv(): # The money sheet map1 = [[0]*5 for n in xrange(42)] map1_count = 6 # The time sheet map2 = [['']*7 for n in xrange(75)] map2_count = 0 day_map = {'sun':0, 'mon':1, 'tue':2, 'wed':3, 'thu':4, 'fri':5, 'sat':6} sh_offset = 0 ot_offset = 0 ca_offset = 0 pa_offset = 0 su_offset = 0 time_slot = 0 text_slot = 0 memo = open(memo_file, 'r') count = 0 raw_line = ' ' ... And got the following again ... Traceback (most recent call last): File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 268, in ? palm_conv() File "C:\Documents and Settings\Administrator\Desktop\palm.py", line 54, in palm_conv memo = open(memo_file, 'r') IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\Administrator\\My Documents\\memo.txt' >>> So I tried your test code & got ... >>> >>> >>> import os.path >>> print os.path.expanduser('~/memo.txt') C:\Documents and Settings\Administrator/memo.txt >>> f = open(os.path.expanduser('~/memo.txt')) Traceback (most recent call last): File "", line 1, in ? f = open(os.path.expanduser('~/memo.txt')) IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\Administrator/memo.txt' >>> Now starting to doubt my sanity I again re-checked C:\Documents and Settings\Administrator\My Documents and yes I do have a memo.txt there. In C:\Documents and Settings I have the folders administrator, all users and compy, I put a copy of memo.txt in compy as well & changed my code, same problem Any ideas Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Acessing files in Windows 2000
[EMAIL PROTECTED] wrote: import os.path print os.path.expanduser('~/memo.txt') C:\Documents and Settings\Administrator/memo.txt f = open(os.path.expanduser('~/memo.txt')) Traceback (most recent call last): File "", line 1, in ? f = open(os.path.expanduser('~/memo.txt')) IOError: [Errno 2] No such file or directory: 'C:\\Documents and Settings\\Administrator/memo.txt' Now starting to doubt my sanity I again re-checked C:\Documents and Settings\Administrator\My Documents and yes I do have a memo.txt there. Um --- So you have a file 'C:\Documents and Settings\Administrator\My Documents\memo.txt'... But you are attempting to open the file 'C:\Documents and Settings\Administrator\memo.txt'. There is a difference there! mmm ... I kind of see what you mean. Does anyone have like a realy large shovel so I can dig a hole and hide ? Thanks Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Windows user variable ?
Hi there, Probably no one will remember but I had a problem porting a python script to windows, I had a 'no such file error' Turned out that I called the file 'memo.txt', Windows decided it was a text file and it ended up as 'memo.txt.txt'. The windows display strips anything after the '.', found the problem via dos :) Anyhow - When logged into 2000 as an administrator or user is there a system variable that I can read to let the script know who I am logged in as ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Windows user variable ?
Thanks for your help guys Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OT python Licences
This is a bit OT but here goes. My work wants me to write a fairly large python script to analyze some technical ASCII data files. Python and its libraries are GPL. That being the case am I right in thinking that my script would also have to be GPL and I would have to inform my employer as I hand it over ? Secondly it would have to run in Windows, The results could pop up on a DOS window. However I was looking at QT until I read the Windows license. Are there any widget libraries that would allow me to program for windows commercially without any license or fees ? Thanks in advance Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT python Licences
Many thanks for all your input, you have been great. At the moment Tkinter is a favorite, I have started reading the documentation and it seems fairly straightforward + can do what I need. Many thanks once again :-) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter query?
Hi all, Im on the Tkinter GUI learning curve :-) . Its going quite well - im reading O'reilly 'programming python' but I am unclear on the following I understand the standard form ie ... root = Tk() test = button(root, text = ..).pack() etc I also understand independant_win = Toplevel(root) But I do not understand where Frame fits in to this ... ie from Tkinter import * class Hello(Frame): def __init__(self, parent=None): Frame.__init__(self, parent) self.pack() Why not just use 'class Hello(root)' ? Or have I missed the point :-[ Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter query?
Dave S wrote: >Hi all, > >Im on the Tkinter GUI learning curve :-) . Its going quite well - im >reading O'reilly 'programming python' but I am unclear on the following > >I understand the standard form ie ... > >root = Tk() >test = button(root, text = ..).pack() >etc > >I also understand > >independant_win = Toplevel(root) > >But I do not understand where Frame fits in to this ... ie > >from Tkinter import * >class Hello(Frame): > >def __init__(self, parent=None): >Frame.__init__(self, parent) >self.pack() > > >Why not just use 'class Hello(root)' ? Or have I missed the point :-[ > >Dave > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > > OK am I right in thinging a 'Frame' always has a parent of a Tk() or possibly Toplevel() and its used to help in positioning a widget window with .pack() ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter query?
Kent Johnson wrote: >Dave S wrote: > > >>>But I do not understand where Frame fits in to this ... ie >>> >>> >>> >>>from Tkinter import * >> >> >> >>>class Hello(Frame): >>> >>> def __init__(self, parent=None): >>> Frame.__init__(self, parent) >>> self.pack() >>> >>> >>>Why not just use 'class Hello(root)' ? Or have I missed the point :-[ >>> >>> >>OK am I right in thinging a 'Frame' always has a parent of a Tk() or >>possibly Toplevel() and its used to help in positioning a widget window >>with .pack() ? >> >> > >Yes, that is one use of Frame - to help with positioning groups of widgets. > Ahh good > A Frame subclass also can be used to package a group of widgets as a reusable > whole > So you can subclass it - that makes sense >and as a place to put controller code that makes a group of widgets work >together. > > Haven't got to this bit yet, may bee in next chapter ... :-) >Kent > >___ >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] Tkinter query?
> > Frames are used to help with positioning other widgets, yes. They are > also used > to affect how the application looks: you can change the background > colour of > Frames, and also the border (to make them look like they are sticking > out, for > example). But you can (and frequently will) put Frames inside other > Frames. > > Example: > > from Tkinter import * > > tk = Tk() > tk.config(background='pink') > tk.geometry('400x400') > > frame1 = Frame(tk, background='blue') > frame1.pack(side=LEFT, padx=15, pady=15, expand=True, fill=BOTH) > > frame2 = Frame(tk, background='green', borderwidth=3, relief=RAISED) > frame2.pack(side=LEFT, padx=15, pady=15, expand=True, fill=BOTH) > > frame3 = Frame(frame1, background='red', borderwidth=3, relief=SUNKEN) > frame3.pack(side=TOP, padx=15, pady=15, expand=True, fill=BOTH) > > frame4 = Frame(frame1, background='white', borderwidth=3, relief=GROOVE) > frame4.pack(side=TOP, padx=15, pady=15, expand=True, fill=BOTH) > > label1 = Label(frame4, text='Hello world!') > label1.pack(expand=True) > > tk.mainloop() > Thank you, that has clarified a few points but raised a query. tk.geometry('400x400') does not appear to have any effect on its size when the window is drawn, the window is always the same size and short of maximizing it to full screen it is not adjustable. Just a minor query but interesting Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] smtplib mail header problem
Hi all, I have a python script that works out & emails my employer with parts that I have used. It has been great for 11ish months, now all my mail is classed as spam & junked :( Apparently the reason for it being junked is ... 'No subject' and 'No Sender' I set up a small test script to isolate the problem using the core of the email code ... #!/usr/bin/env python # -*- coding: iso8859_1 -*- from smtplib import SMTP from time import sleep, strftime from datetime import datetime import sys email_SMTP = 'mail.pusspaws.net' email_addr = '[EMAIL PROTECTED]' def email_test(): for trys in xrange(10): try: mail_server = SMTP(email_SMTP) mail_server.login('', '') subject = 'Test Email' from_ = 'Dave Selby<[EMAIL PROTECTED]>' to = email_addr return_path = email_addr blog="""Hi,\n\nThis is just a test""" msg = mail_headers(subject, from_, to, return_path) +'\r\n\r\n'+blog mail_server.sendmail(subject , email_addr, msg) mail_server.quit() # If we get to here, all is well, drop out of the loop break except: print 'Mailing error ... Re-trying ... '+str(trys+1)+' of 10\n' sleep(300) if trys==9: raise 'Mail Failure\n'+str(sys.exc_type)+'\n'+str(sys.exc_value) def mail_headers(subject, from_, to, return_path): """ Generate a formatted mail header string """ header = 'Return-Path: ' + return_path header = header + '\r\nFrom: ' + from_ header = header + '\r\nReply-To: ' + return_path header = header + '\r\nTo: ' + to header = header + '\r\nSubject: '+subject now=datetime.now() day = now.strftime('%a') date = now.strftime('%d %b %Y %X') header = header + '\r\nDate : ' + day + ', ' + date + ' -\r\n' return (header) email_test() >From this I receive the following Return-Path: Delivered-To: [EMAIL PROTECTED] Received: (qmail 21696 invoked by uid 503); 6 Dec 2005 20:35:48 - Received: from unknown (HELO localhost.localdomain) ([EMAIL PROTECTED]) by host201.com with SMTP; 6 Dec 2005 20:35:48 - Return-Path: [EMAIL PROTECTED] From: Dave Selby<[EMAIL PROTECTED]> Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Test Email Date: Tue, 06 Dec 2005 20:35:47 - X-Bogosity: Ham, tests=bogofilter, spamicity=0.288675, version=0.95.2 X-UID: Status: R X-Status: NGC X-KMail-EncryptionState: X-KMail-SignatureState: X-KMail-MDN-Sent: Hi, This is just a test I cannot seem to set the top 'Return-path' correctly, any ideas how I do this ? also I have a sender so I am unsure why the spam filter picked this out. Any suggestions Cheers dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] smtplib mail header problem
On Tuesday 06 December 2005 21:13, Liam Clarke-Hutchinson wrote: > Hi Dave, > > IIRC The first argument to sendmail() is the name of the account that's > sending... So when you include your subject there, it seems your ISP is > somewhat forgiving. > > Liam Clarke-Hutchinson| Contact Centre Advisor| Ministry of Economic > Development > DDI +64 3 962 2639 | Fax +64 3 962 6220 > www.med.govt.nz Thanks for that heads up, I will amend the code & try to re-send it ... Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] smtplib mail header problem
On Tuesday 06 December 2005 22:23, Liam Clarke-Hutchinson wrote: > Okay, just checked the docs - > > "sendmail( from_addr, to_addrs, msg[, mail_options, rcpt_options]) > > Note: The from_addr and to_addrs parameters are used to construct the > message envelope used by the transport agents. The SMTP does not modify the > message headers in any way." > > So my guess is that the spam agent is checking that from_addr, and getting > an invalid email address and spitting the dummy. > Most ISPs won't allow an invalid from_addr, although mine does.. > > Regards, > > Liam Clarke-Hutchinson > Thanks for that I 'pydoc smtplib' but got somewhat confused :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] smtplib mail header problem
On Wednesday 07 December 2005 22:47, Liam Clarke-Hutchinson wrote: > Heheh, yah, the Python docs take a bit of scrutinisation to yield fruit. > Also, when working with emails, you'll probably end up trying to figure > what exactly what a RFC or three mean. > > Good luck, > > Liam > But its all worth it, problem now solved & work gets my emails :) Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Looking for an open source project
Hi all, I have been playing around with Python for a while now and am looking for open source KDE projects written in Python that I can help with / learn from. Something relatively simple that uses Qt. (I am still a bit green) Any idea where I can get a list of projects. I checked out sourceforge but could not work out how to filter for Python. Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Looking for an open source project
On Friday 14 April 2006 11:07, Kent Johnson wrote: > Dave S wrote: > > Hi all, > > > > I have been playing around with Python for a while now and am looking > > for open source KDE projects written in Python that I can help with / > > learn from. > > > > Something relatively simple that uses Qt. (I am still a bit green) > > > > Any idea where I can get a list of projects. I checked out sourceforge > > but could not work out how to filter for Python. > > Go to the SF software map and click on a category, then you will be able > to filter for language. > Thanks for that :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] can't import module
Hi all, I wrote some script a while back but can no longer get it to run, since then I have upgraded my system from breezy to dapper (kubuntu) I now cannot import a module I wrote. I just know its got to be simple and am somewhat embarrassed that I cannot see the problem :( [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ ./live_datad.py Traceback (most recent call last): File "./live_datad.py", line 15, in ? from logger import log ImportError: No module named logger OK that's my problem [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ echo $PYTHONPATH /home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ ls -al /home/dave/my_files/my_gg/gg1.4/gg_utils total 92 drwxr-xr-x 2 dave dave 4096 2006-03-27 13:03 . drwxr-xr-x 13 dave dave 4096 2006-06-07 22:12 .. -rwxr-xr-x 1 dave dave 1301 2005-01-31 20:35 archive_read.py -rw-r--r-- 1 dave dave 1612 2005-02-06 10:18 archive_read.pyc -rw-r--r-- 1 dave dave 1612 2005-02-06 12:44 archive_read.pyo -rwxr-xr-x 1 dave dave 2630 2005-02-04 22:54 convert_data.py -rw-r--r-- 1 dave dave 2631 2005-02-06 10:18 convert_data.pyc -rw-r--r-- 1 dave dave 2631 2005-02-06 12:44 convert_data.pyo -rw-r--r-- 1 dave dave 1146 2005-01-31 20:35 cookie_string.py -rw-r--r-- 1 dave dave 1315 2005-11-20 15:21 cookie_string.pyc -rw-r--r-- 1 dave dave 1420 2005-02-06 12:44 cookie_string.pyo -rwxr-xr-x 1 dave dave 1531 2005-02-04 12:34 dump_key.py -rw-r--r-- 1 dave dave 1470 2005-02-04 12:34 dump_key.pyc -rw-r--r-- 1 dave dave 1470 2005-02-06 12:44 dump_key.pyo -rwxr-xr-x 1 dave dave 1906 2005-01-31 20:35 html_strip.py -rw-r--r-- 1 dave dave 2069 2005-11-20 15:13 html_strip.pyc -rw-r--r-- 1 dave dave 2380 2005-02-06 12:44 html_strip.pyo -rwxr--r-- 1 dave dave 2646 2005-01-31 20:35 logger.py -rw-r--r-- 1 dave dave 3212 2005-11-20 15:13 logger.pyc -rw-r--r-- 1 dave dave 3546 2005-02-06 12:44 logger.pyo -rwxr-xr-x 1 dave dave 1873 2005-01-31 20:35 tail_log.py -rw-r--r-- 1 dave dave 1990 2005-02-06 10:18 tail_log.pyc -rw-r--r-- 1 dave dave 1990 2005-02-06 12:44 tail_log.pyo [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ so logger.py is in PYTHONPATH so why can't live_data.d see it ? Any ideas .. Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can't import module
On Sunday 02 July 2006 11:29, Kent Johnson wrote: > Dave S wrote: > > > [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ ./live_datad.py > > > > Traceback (most recent call last): > > File "./live_datad.py", line 15, in ? > > from logger import log > > ImportError: No module named logger > > > > [EMAIL PROTECTED]:~/my_files/my_gg/gg1.4/get_data$ echo $PYTHONPATH > > /home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/l > >ogs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1 > >.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/ > >gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils > > I don't know why your import is failing, but from your PYTHONPATH it > looks like you should consider making all the gg1.4 directories into > packages, > then you would have imports such as > from gg_utils.logger import log > > This would simplify PYTHONPATH and I think reflect more accurately what > you are doing. > Thanks for replying :) The app is fairly big and distributed around the gg1.4 directory. I get the feeling that 'from logger import log' is the first of a lot of import problems, this script alone imports from another 7 modules from urllib import urlopen from time import strftime from cPickle import dump from datetime import datetime, time, timedelta from os import remove from logger import log from html_strip import html_strip from garbage_collect import garbage_collect from valid_day import valid_day from exact_sleep import sleep_delay,sleep_until from cookie_string import cookie_string from data_core import Data_Core from config import data_dir,HTML_addr, ipc_dir As far a putting everything into a package - I am a bit lost. Do you mean one big .py script or am I misunderstanding you ? Dave > Kent > > ___ > 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] can't import module
On Sunday 02 July 2006 17:02, Dave Kuhlman wrote: > On Sun, Jul 02, 2006 at 11:55:24AM +0100, Dave S wrote: > > [snip] > > > Thanks for replying :) > > > > The app is fairly big and distributed around the gg1.4 directory. I get > > the feeling that 'from logger import log' is the first of a lot of import > > problems, this script alone imports from another 7 modules > > > > from urllib import urlopen > > from time import strftime > > from cPickle import dump > > from datetime import datetime, time, timedelta > > from os import remove > > > > from logger import log > > from html_strip import html_strip > > from garbage_collect import garbage_collect > > from valid_day import valid_day > > from exact_sleep import sleep_delay,sleep_until > > from cookie_string import cookie_string > > from data_core import Data_Core > > > > from config import data_dir,HTML_addr, ipc_dir > > So, ask yourself: Where is logger.py? Modules logger, > html_strip, etc are not it the Python standard library (see: > http://docs.python.org/lib/lib.html). Are they in your > application? Are (were) they in some package that you had > previously installed (but that needs to be re-installed)? > They are all from my fair hand - coded about a year ago. I have just copied the whole directory to my new dapper system, changed PYTHONPATH and was somewhat supprised it has not just worked. > One possible explanation for your problem is that when you > upgraded from Ubuntu/Kubuntu breezy to dapper, you also upgraded > from Python 2.3 to Python 2.4. If so, Python is now importing > from: > > /usr/lib/python2.4/site-packages/ > > instead of: > > /usr/lib/python2.3/site-packages/ I see your point but because these are all my own packages I would guess the python version would be irrelevant > > If so, there might be Python modules that need to be re-installed > by running Python 2.4 (instead of Python 2.3). I know that on my > Kubuntu system, there is both Python 2.4 and 2.3. > > If the above is *not* the problem, then you need to find out where > your application used to find these modules (e.g. logger). That > will help you (or someone on this list) figure out how to enable > Python and your live_datad.py application to find them. OK I am going to try a couple of real basic scripts and import a module to see where it all falls down. I will email back with the results :) Cheers Dave > > Hope this helps. > > Dave K. > > [snip] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can't import module
Here goes ... I have two files test1 and test2 ... [EMAIL PROTECTED]:~$ ls -l total 37620 drwx-- 8 dave dave 4096 2006-06-30 23:26 Desktop drwxr-xr-x 9 dave dave 4096 2006-06-15 22:48 google-earth drwxr-xr-x 13 dave dave 4096 2006-05-27 09:51 my_files drwxr-xr-x 2 dave dave 4096 2006-06-24 21:29 Pictures drwxr-xr-x 4 dave dave 4096 2006-07-02 03:00 PodCasts drwxr-xr-x 2 dave dave 4096 2006-06-16 21:30 qemu drwxr-xr-x 9 dave dave 4096 2006-06-30 22:30 SecondLife_1_10_5_1 -rw-r--r-- 1 dave dave 38438806 2006-06-30 18:53 SecondLife_1_10_5_1.tar.bz2 drwxr-xr-x 2 dave dave 4096 2006-07-02 17:43 test1 drwxr-xr-x 2 dave dave 4096 2006-07-02 17:41 test2 Two simple scripts parent and child ... [EMAIL PROTECTED]:~$ cd test2 [EMAIL PROTECTED]:~/test2$ cat child.py #!/usr/bin/env python # -*- coding: iso8859_1 -*- def hello(): print 'child says hello' [EMAIL PROTECTED]:~/test2$ cd ../test1 [EMAIL PROTECTED]:~/test1$ cat parent.py #!/usr/bin/env python # -*- coding: iso8859_1 -*- from child import hello def test(): print 'parent says hello' hello() test() And I hit my problem :( [EMAIL PROTECTED]:~/test1$ ./parent.py Traceback (most recent call last): File "./parent.py", line 4, in ? from child import hello ImportError: No module named child PYTHONPATH ... [EMAIL PROTECTED]:~/test1$ [EMAIL PROTECTED]:~/test1$ [EMAIL PROTECTED]:~/test1$ echo $PYTHONPATH /home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils:/home/dave/test2 [EMAIL PROTECTED]:~/test1$ And where PYTHONPATH is set ... [EMAIL PROTECTED]:~/test1$ cat ~/.bashrc # ~/.bashrc: executed by bash(1) for non-login shells. # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc) # for examples # If not running interactively, don't do anything [ -z "$PS1" ] && return # don't put duplicate lines in the history. See bash(1) for more options #export HISTCONTROL=ignoredups # check the window size after each command and, if necessary, # update the values of LINES and COLUMNS. shopt -s checkwinsize # make less more friendly for non-text input files, see lesspipe(1) [ -x /usr/bin/lesspipe ] && eval "$(lesspipe)" # set variable identifying the chroot you work in (used in the prompt below) if [ -z "$debian_chroot" -a -r /etc/debian_chroot ]; then debian_chroot=$(cat /etc/debian_chroot) fi # set a fancy prompt (non-color, unless we know we "want" color) case "$TERM" in xterm-color) PS1='${debian_chroot:+($debian_chroot)}\[\033[01;[EMAIL PROTECTED]: \[\033[01;34m\]\w\[\033[00m\]\$ ' ;; *) PS1='${debian_chroot:+($debian_chroot)[EMAIL PROTECTED]:\w\$ ' ;; esac # Comment in the above and uncomment this below for a color prompt #PS1='${debian_chroot:+($debian_chroot)}\[\033[01;[EMAIL PROTECTED]: \[\033[01;34m\]\w\[\033[00m\]\$ ' # If this is an xterm set the title to [EMAIL PROTECTED]:dir case "$TERM" in xterm*|rxvt*) PROMPT_COMMAND='echo -ne "\033]0;[EMAIL PROTECTED]: ${PWD/$HOME/~}\007"' ;; *) ;; esac # Alias definitions. # You may want to put all your additions into a separate file like # ~/.bash_aliases, instead of adding them here directly. # See /usr/share/doc/bash-doc/examples in the bash-doc package. #if [ -f ~/.bash_aliases ]; then #. ~/.bash_aliases #fi # enable color support of ls and also add handy aliases if [ "$TERM" != "dumb" ]; then eval "`dircolors -b`" alias ls='ls --color=auto' #alias dir='ls --color=auto --format=vertical' #alias vdir='ls --color=auto --format=long' fi # some more ls aliases #alias ll='ls -l' #alias la='ls -A' #alias l='ls -CF' # enable programmable completion features (you don't need to enable # this, if it's already enabled in /etc/bash.bashrc and /etc/profile # sources /etc/bash.bashrc). #if [ -f /etc/bash_completion ]; then #. /etc/bash_completion #fi PYTHONPATH=/home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils:/home/dave/test2 export EDITOR=vi [EMAIL PROTECTED]:~/test1$ Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can't import module
On Sunday 02 July 2006 20:12, Alan Gauld wrote: > > As far a putting everything into a package - I am a bit lost. Do you > > mean one > > big .py script or am I misunderstanding you ? > > You misunderstand him. > > Python allows you to reate a package structure of directories and > files > such that all of the files within the top level directory and the > subdirectories > are searched so you only need to specify the top level directory for > all the > files in subndirectories to be found. > > This requires creating some magic python definition files. > Check the docs for details. > > The end result is that in pythonpath you add > > /some/path/myPackage > > and in your code you do > > from myPackage import someModule > > Check the docs for details. > > Alan G. Thanks for that - I never realised you could do that. It sound like a good solution and a lot less messy than the way I have created my own modules & scripts. Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SOLVED :)
On Sunday 02 July 2006 18:32, Danny Yoo wrote: > > PYTHONPATH=/home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/my > >_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_file > >s/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dave/my_f > >iles/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/common_utils > >:/home/dave/test2 > > Have you marked PYTHONPATH to be exportable? Your shell may not do this > automatically unless explicitely told to do so. You solved it ! starting my PYTHONPATH with export did the trick for my demo files & for live_datad.py. :) Thank you so much, I knew it had to be something relatively simple but I could just not see it. Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] can't import module
On Sunday 02 July 2006 18:48, Dave Kuhlman wrote: > On Sun, Jul 02, 2006 at 10:32:51AM -0700, Danny Yoo wrote: > > > PYTHONPATH=/home/dave/my_files/my_gg/gg1.4/configs:/home/dave/my_files/ > > >my_gg/gg1.4/logs:/home/dave/my_files/my_gg/gg1.4/get_data:/home/dave/my_ > > >files/my_gg/gg1.4/gg_utils:/home/dave/my_files/my_gg/gg1.4/ipc:/home/dav > > >e/my_files/my_gg/gg1.4/process_data:/home/dave/my_files/my_gg/gg1.4/comm > > >on_utils:/home/dave/test2 > > > > Have you marked PYTHONPATH to be exportable? Your shell may not do this > > automatically unless explicitely told to do so. > > A simple test -- In order to check PYTHONPATH do the following: > > $ python > Python 2.4.3 (#1, Apr 11 2006, 20:59:32) > [GCC 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import sys > >>> print sys.path > > Dave K. Thank you also. inadvertently I think you have given me the key to another problem that has been bugging me for a couple of weeks to do with paths and library's a python app accesses. ['', '/home/dave/my_files/my_gg/gg1.4/configs', '/home/dave/my_files/my_gg/gg1.4/logs', '/home/dave/my_files/my_gg/gg1.4/get_data', '/home/dave/my_files/my_gg/gg1.4/gg_utils', '/home/dave/my_files/my_gg/gg1.4/ipc', '/home/dave/my_files/my_gg/gg1.4/process_data', '/home/dave/my_files/my_gg/gg1.4/common_utils', '/home/dave/test2', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/HTMLgen', '/usr/lib/python2.4/site-packages/Numeric', '/usr/lib/python2.4/site-packages/PIL', '/usr/lib/python2.4/site-packages/cairo', '/usr/lib/python2.4/site-packages/gst-0.10', '/usr/lib/python2.4/site-packages/gtk-2.0'] One question, where is the rest of PYTHONPATH defined, my section in .bashrc is only the first part, where do you get to define the /usr/lib part ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Basic QT query
Hi all, I am trying to get to grips with QT, putting a friendly face on some of my apps :) Its early days and my first attempt but I expected the following to print 'hi it works' every second. There problem I am stuck with is ... The debugged program raised the exception unhandled RuntimeError "underlying C/C++ object has been deleted" At line 24 ie "self.timer = self.startTimer(1000)". First of all I did not have an assignment ie I just had "self.startTimer(1000)" realising Python would garbage collect I added an assignment but still the same problem. Also I am struggling with "QObject.connect(self.tickerevent, PYSIGNAL("ticker"), self.hello)" I am unsure of what the source of the connect is - I suspect my guess of self.tickerevent is wrong. The source of the event should be PYSIGNAL("ticker") ? Any ideas or suggestions much appreciated Cheers Dave ** MY QT MASTERPIECE :) ** import sys from qt import * from frm_livedata import frm class Ticker(QObject): def __init__(self): QObject.__init__, (self) self.timer = self.startTimer(1000) def timerEvent(self, ev): self.emit(PYSIGNAL("ticker")()) QObject.connect(self.tickerevent, PYSIGNAL("ticker"), self.hello) def hello(): print 'hi it works' x=Ticker() if __name__ == '__main__': app = QApplication(sys.argv) win = frm() app.setMainWidget(win) win.show() QObject.connect(app, SIGNAL('lastWindowClosed()'),app, SLOT('quit()')) app.exec_loop() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Basic QT query #2
On Friday 14 July 2006 19:21, Dave S wrote: > Hi all, > > I am trying to get to grips with QT, putting a friendly face on some of my > apps :) Its early days and my first attempt but I expected the following to > print 'hi it works' every second. > > There problem I am stuck with is ... > > The debugged program raised the exception unhandled RuntimeError > "underlying C/C++ object has been deleted" At line 24 > > ie "self.timer = self.startTimer(1000)". First of all I did not have an > assignment ie I just had "self.startTimer(1000)" realising Python would > garbage collect I added an assignment but still the same problem. > > Also I am struggling with "QObject.connect(self.tickerevent, > PYSIGNAL("ticker"), self.hello)" I am unsure of what the source of the > connect is - I suspect my guess of self.tickerevent is wrong. The source of > the event should be PYSIGNAL("ticker") ? > > Any ideas or suggestions much appreciated > > Cheers > > Dave > > ** MY QT MASTERPIECE :) ** > > > import sys > from qt import * > from frm_livedata import frm > > > class Ticker(QObject): > > def __init__(self): > QObject.__init__, (self) > self.timer = self.startTimer(1000) > > def timerEvent(self, ev): > self.emit(PYSIGNAL("ticker")()) > QObject.connect(self.tickerevent, PYSIGNAL("ticker"), > self.hello) > > def hello(): > print 'hi it works' > > x=Ticker() > > if __name__ == '__main__': > > app = QApplication(sys.argv) > win = frm() > app.setMainWidget(win) > win.show() > QObject.connect(app, SIGNAL('lastWindowClosed()'),app, SLOT('quit()')) > app.exec_loop() > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Doh - Sorted it rearranged the defs & '__init__,' becomes '__init__' its always the simple things that eat the hours :) I am writting a QT front end displaying some information about my demon script 'live_datad.py'. The QT display now comes up (cheer!) but I need a couple of things ,,. First I need to know if if a 'live_datad' is running - Already tried looking in modules & os.system('ps ax') no go. I need an equivalent of ps ax | grep live_datad. Second I need to get some simple data from the daemon, no more than 2-3 strings would do it reflecting the daemons status. I don't want to re-code it in classes, or do anything with sockets (they seem way too complex for me) is there a more elegant way than the daemon writing its status in an ascii file every few seconds and my QT app reading it - or is this an OK way of doing it ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [SOLVED] Basic QT query #2
On Friday 14 July 2006 23:21, Dave S wrote: > On Friday 14 July 2006 19:21, Dave S wrote: > > Hi all, > > > > I am trying to get to grips with QT, putting a friendly face on some of > > my apps :) Its early days and my first attempt but I expected the > > following to print 'hi it works' every second. > > > > There problem I am stuck with is ... > > > > The debugged program raised the exception unhandled RuntimeError > > "underlying C/C++ object has been deleted" At line 24 > > > > ie "self.timer = self.startTimer(1000)". First of all I did not have an > > assignment ie I just had "self.startTimer(1000)" realising Python would > > garbage collect I added an assignment but still the same problem. > > > > Also I am struggling with "QObject.connect(self.tickerevent, > > PYSIGNAL("ticker"), self.hello)" I am unsure of what the source of the > > connect is - I suspect my guess of self.tickerevent is wrong. The source > > of the event should be PYSIGNAL("ticker") ? > > > > Any ideas or suggestions much appreciated > > > > Cheers > > > > Dave SOLVED - Sometimes trying & trying is a good way to learn :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] open(file, 'rw') problem
Good morning, I am having a problem with file open, read & write. The example shows my problem. I would have expected it to print a sequence 1,2,3,4 incrementing every time it is run. #!/usr/bin/env python import os, shutil, time basher_dir = '/home/dave/PodCasts' bit_bucket = basher_dir + '/xbit_bucket' cd_count = bit_bucket + '/xcd_count' if not os.path.isfile(cd_count): f = open(cd_count, 'w') f.write('0') f.close() f = open(cd_count,'rw') n = int(f.read()) n += 1 print n f.seek(0) f.write(str(n)) f.close() Instead I get ... [EMAIL PROTECTED]:~/my_files/python_develop/unison/podsplit$ ./test.py 1 Traceback (most recent call last): File "./test.py", line 20, in ? f.write(str(n)) IOError: [Errno 9] Bad file descriptor [EMAIL PROTECTED]:~/my_files/python_develop/unison/podsplit$ It appears to be a problem with me opening the file as 'rw' although I have googled and opening as 'rw' appears legal. Can you only open a file as 'r' or 'w' - ie open & close the file twice ? Any ideas Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] open(file, 'rw') problem
On Thursday 27 July 2006 09:08, Andre Engels wrote: > 2006/7/27, Dave S <[EMAIL PROTECTED]>: > > It appears to be a problem with me opening the file as 'rw' although I > > have googled and opening as 'rw' appears legal. Can you only open a file > > as 'r' or 'w' - ie open & close the file twice ? > > If you use "open(filename, 'rw')", the 'w' does not have effect, and > you have opened the file read-only. To do what you want to do (both > read and write with a file), you can use "open(filename, 'r+')" > instead. Ahh .. thanks for the heads up - I thought there had to be a better way Cheers dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating a method name on the fly
I need to scan a long list of QT tickboxes in a dialog. I need to execute pseudo code something like ... list = ['error_button', 'print_button' ... etc ] for key in list: button= list[key] print button, self.button.isChecked() where self.button.isChecked() becomes self.error_button.isChecked() ... self.print_button.isChecked() etc etc Can anyone tell me how to do this ? I have looked at apply but with no luck Thanks Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a method name on the fly
Thanks for all your input - the discussion wandered off list a bit ! This solution works a treat ... button= getattr(self, 'error_button') print button, button.isChecked() Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] rstrip() failure ?
I have a string problem. The following code skips the loop if the string 'app' is on the banned list ... self.ban_app_list = [' ', 'live_dat', 'exact_sl', 'html_str', 'valid_da', 'datacore', 'check_co', 'logger'] if app in self.ban_app_list: continue the string 'app' is derived from some more string splicing. All works well except for 'logger', the 'app' string becomes 'logger ' so no match. So I app.rstrip() thinking it would sort the problem. no go :( len(app) is still 8. OK ... I am stuck ... Is there a way to show the ascii values of the string - kind of hexedit for a string I am sure the padding blanks are just spaces rstrip() appears to disagree Any help greatly appreciated Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Doh!] rstrip() failure ?
On Wednesday 09 August 2006 20:45, dave s wrote: > I have a string problem. The following code skips the loop if the string > 'app' is on the banned list ... > > self.ban_app_list = > [' ', 'live_dat', 'exact_sl', 'html_str', 'valid_da', 'datacore', > 'check_co', 'logger'] > > if app in self.ban_app_list: > continue > > the string 'app' is derived from some more string splicing. All works well > except for 'logger', the 'app' string becomes 'logger ' so no match. So I > > app.rstrip() > > thinking it would sort the problem. no go :( > > len(app) is still 8. OK ... I am stuck ... > > Is there a way to show the ascii values of the string - kind of hexedit for > a string > > I am sure the padding blanks are just spaces rstrip() appears to disagree > > Any help greatly appreciated > > Dave > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Stupid basic error held me up for a couple of hours :) app.rstrip() should have been app = app.rstrip() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rstrip() failure ?
On Wednesday 09 August 2006 21:11, you wrote: > > Is there a way to show the ascii values of the string - kind of hexedit > > for a string > > Try running repr() on the string: it will show an external > programmer-friendly representation that should be easier to read. For > example: > > ## > > >>> msg = 'hello\000world' > >>> print repr(msg) > > 'hello\x00world' > ## Thanks for that :) Have just found my solution without resorting to ascii strings after all but the above will be usefull Thanks once again Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Style query
As my programs become more complex I am aware of the need to adopt a consistent style. To differentiate between classes, instances & objects I use capital letters for example: A class uses 'MyClass' A class instance 'myInstance' A def uses 'myDef' An object 'myobject' or 'my_object' etc Can any of you more experienced programmers outline your style or critique my way of doing this ? Tell me if you have a different system - I am trying to get myself into good habits :) Is there a Python style guide anywhere ? Cheers Dave PS I have started using packages, so much easier and more flexable than long PYTHONPATH declarations and am now proramming with QT :). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Style query
On Sunday 13 August 2006 12:02, you wrote: > I believe you can find it here: > > http://www.python.org/doc/essays/styleguide.html > > Authored by Guido Van Rossum himself. > Thanks :) Dave > > Cheers, > > Richard > > On 8/13/06, dave s <[EMAIL PROTECTED]> wrote: > > As my programs become more complex I am aware of the need to adopt a > > consistent style. To differentiate between classes, instances & objects I > > use > > capital letters for example: > > > > A class uses 'MyClass' > > A class instance 'myInstance' > > A def uses 'myDef' > > An object 'myobject' or 'my_object' etc > > > > Can any of you more experienced programmers outline your style or > > critique my > > way of doing this ? Tell me if you have a different system - I am trying > > to > > get myself into good habits :) > > > > Is there a Python style guide anywhere ? > > > > Cheers > > > > Dave > > > > > > PS > > > > I have started using packages, so much easier and more flexable than long > > PYTHONPATH declarations and am now proramming with QT :). > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] A list in list problem
Help :) I have been playing with this for several hours & am totally stuck ! I am happy with the following ... >>> a=[1,2,3] >>> b=[5,6,7] >>> a [1, 2, 3] >>> b [5, 6, 7] >>> g=[] >>> g [] >>> g.append(a) >>> g [[1, 2, 3]] >>> g.append(b) >>> g [[1, 2, 3], [5, 6, 7]] >>> So when I needed to make a list of a list in the following code I thought no problem ... def CSV_Lines(self, csv, from_, to): """Returns a list of cleaned up lines from csv 'from_' line number 'to' line number""" clean_line = clean_csv = [] for string in range(from_, to): split_string = csv[string].split(',') split_string = split_string[1:-1] # Strip the LHS column + the /n' if split_string[0] == '' : continue # Skip empty lines print '##' print 'split_string ', split_string for string in split_string: if len(string) > 0: clean_line.append(string[1:-1]) print 'clean_line ',clean_line clean_csv.append(clean_line) print 'clean_csv ',clean_csv clean_line = [] But I get clean_csv trouble ... [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ ./crosscheck.py ## split_string ['"temp1"', '"wow a variable"', '', '', ''] clean_line ['temp1', 'wow a variable'] clean_csv ['temp1', 'wow a variable', [...]] ## split_string ['"temp2"', '', '', '', ''] clean_line ['temp2'] clean_csv ['temp1', 'wow a variable', [...], ['temp2']] [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ ie clean_csv ends up as ['temp1', 'wow a variable', [...], ['temp2']] instead of [[temp1, wow a variable], [temp2]] Please help Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A list in list problem
On Monday 21 August 2006 10:59, dave s wrote: Several hours +1 Sometimes it is usefull spelling out my problem in an email - seems to clarify it - maybe when I get to the 'im stuck' I should send an email to myself :) clean_csv.append(clean_line) ... should by clean_csv.append(clean_line[:]) But the real showstopper was clean_line = clean_csv = [] and then if len(string) > 0: clean_line.append(string[1:-1]) changes clean_csv as well, I guess because its an appens not a reassignment Sorry for bothering you guys Dave > Help :) > > I have been playing with this for several hours & am totally stuck ! > > I am happy with the following ... > > >>> a=[1,2,3] > >>> b=[5,6,7] > >>> a > > [1, 2, 3] > > >>> b > > [5, 6, 7] > > >>> g=[] > >>> g > > [] > > >>> g.append(a) > >>> g > > [[1, 2, 3]] > > >>> g.append(b) > >>> g > > [[1, 2, 3], [5, 6, 7]] > > > So when I needed to make a list of a list in the following code I thought > no problem ... > > >def CSV_Lines(self, csv, from_, to): > """Returns a list of cleaned up lines from csv 'from_' line > number 'to' line number""" > > clean_line = clean_csv = [] > for string in range(from_, to): > split_string = csv[string].split(',') > split_string = split_string[1:-1] # Strip the LHS column + > the /n' > if split_string[0] == '' : continue # Skip empty lines > print '##' > print 'split_string ', split_string > for string in split_string: > if len(string) > 0: clean_line.append(string[1:-1]) > print 'clean_line ',clean_line > clean_csv.append(clean_line) > print 'clean_csv ',clean_csv > clean_line = [] > > But I get clean_csv trouble ... > > [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ ./crosscheck.py > ## > split_string ['"temp1"', '"wow a variable"', '', '', ''] > clean_line ['temp1', 'wow a variable'] > clean_csv ['temp1', 'wow a variable', [...]] > ## > split_string ['"temp2"', '', '', '', ''] > clean_line ['temp2'] > clean_csv ['temp1', 'wow a variable', [...], ['temp2']] > [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ > > ie clean_csv ends up as ['temp1', 'wow a variable', [...], ['temp2']] > instead of [[temp1, wow a variable], [temp2]] > > Please help > > Dave > > > > > > > > > > > > > > > ___ > 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] A list in list problem
On Monday 21 August 2006 13:40, Alan Gauld wrote: > > So when I needed to make a list of a list in the following code I > > thought no > > problem ... > > > > > > def CSV_Lines(self, csv, from_, to): > >"""Returns a list of cleaned up lines from csv 'from_' line > > number 'to' line number""" > > > >clean_line = clean_csv = [] > > So clean_line is a reference to clean_csv which is a reference > to a list. That is, both variables point to the same list? > I don't think thats what you wanted... Remember that Python > variables are references to objects, in this case they both > reference the same list object. In the past I have assigned multiple variables by var1 = var2 = 0 when one of the variables is assigned a different value, another object is created and var1 & var2 are separate. Without thinking I tried to do the same with the two lists. >>> >>> a=b=[] >>> a [] >>> b [] >>> a=[1,2,3] >>> a [1, 2, 3] >>> b [] >>> Tinkering some more I think it is the append that did it. >>> >>> a=b=[] >>> a [] >>> b [] >>> a.append([1,2,3]) >>> a [[1, 2, 3]] >>> b [[1, 2, 3]] >>> It appended to the common object and did not create a separate one ? I guess var1 = var2 = 0 is generally bad programming style ?. I keep trying to get my code more compact using list comprehension etc - in this case compact got me into trouble. I have tried looking at some open source projects eg kdissert,sbackup to get into good codeing habits but they are a bit above me. So I am learning by trying. (1 x GUI app written, a more advanced one on the way) Thanks for all your help Dave PS This is probably an impossible question but I always struggle to find names for variables - any hints ? > > >print 'clean_line ',clean_line > >clean_csv.append(clean_line) > > So the list has just appended itself to itself, that why python > shows the slightly wierd [...] because its a recursively defined list. That sounds bad - and sure confused me ! > > >print 'clean_csv ',clean_csv > >clean_line = [] > > And now you make clean_line point to a new empty list > So next time round the vcontents ofg the new clean line > will be appended to the old one. > > Go back and initialise your lists as two separate lists and > you will get what you expect. > > > clean_line ['temp1', 'wow a variable'] > > clean_csv ['temp1', 'wow a variable', [...]] > > The original with itself tagged on. > > > clean_line ['temp2'] > > clean_csv ['temp1', 'wow a variable', [...], ['temp2']] > > The above list with the new list added > > Just as expected :-) > > HTH, > > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A list in list problem
On Monday 21 August 2006 13:58, János Juhász wrote: > Hi Dave, > > > From: dave s <[EMAIL PROTECTED]> > > Subject: [Tutor] A list in list problem > > To: python tutor > > Message-ID: <[EMAIL PROTECTED]> > > Content-Type: text/plain; charset="us-ascii" > > > > def CSV_Lines(self, csv, from_, to): > > """Returns a list of cleaned up lines from csv 'from_' line > > number 'to' line number""" > > > >clean_line = clean_csv = [] > >for string in range(from_, to): > > split_string = csv[string].split(',') > > split_string = split_string[1:-1] # Strip the LHS column > > + the /n' > > > if split_string[0] == '' : continue # Skip empty lines > > print '##' > > print 'split_string ', split_string > > for string in split_string: > > if len(string) > 0: > > clean_line.append(string[1:-1]) > > > print 'clean_line ',clean_line > > clean_csv.append(clean_line) > > print 'clean_csv ',clean_csv > > clean_line = [] > > > > But I get clean_csv trouble ... > > > > [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ ./crosscheck.py > > ## > > split_string ['"temp1"', '"wow a variable"', '', '', ''] > > clean_line ['temp1', 'wow a variable'] > > clean_csv ['temp1', 'wow a variable', [...]] > > ## > > split_string ['"temp2"', '', '', '', ''] > > clean_line ['temp2'] > > clean_csv ['temp1', 'wow a variable', [...], ['temp2']] > > [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/main$ > > > > ie clean_csv ends up as ['temp1', 'wow a variable', [...], ['temp2']] > > instead > > > of [[temp1, wow a variable], [temp2]] > > You have used string as variable name two times, once as an integer and > once as a field in the line. > It should be avoided. > > I like list comprehension in this case. > > def CSV_Lines2(csv, from_, to): > csv = csv[from_ : to] # We are > interested just here > csv = [line.split(',') for line in csv] # Make a 2D array from the > list > return [LineItems[1:-1] for LineItems in csv if len(LineItems) > 2] > # filter out first and last columns, and lines with > too less items > That is so neat, and a lot more elegant than my code ! - It may well be ahem Incorporated into my app :) I always seem to see codeing from my particular angle - I would never have dreamt of doing it that way. Thank you Dave > > > csv = """Header1 > Header2 > temp1,12,20,1 > temp2,22,22,2 > temp3,33,44,3 > temp4,34,64,4 > Footer1 > Footer2""" > > csv = csv.split('\n') > print CSV_Lines2(csv, 2, 6) > > >>>[['12', '20'], ['22', '22'], ['33', '44'], ['34', '64']] > > Yours sincerely, > __ > Janos Juhasz ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A list in list problem
On Monday 21 August 2006 17:41, Alan Gauld wrote: > a=b=[] > a > > > > [] > > > b > > > > [] > > These are the same list. > > a=[1,2,3] > > But here you create a new list and assign it to a. > > a > > > > [1, 2, 3] > > > b > > > > [] > > So a points to the new list and b to the original. > > > Tinkering some more I think it is the append that did it. > > Yes, the append adds the data to the original list. > > a=b=[] > a > > > > [] > > > b > > > > [] > > > a.append([1,2,3]) > a > > > > [[1, 2, 3]] > > > b > > > > [[1, 2, 3]] > > Exactly so. > > > It appended to the common object and did not create a separate one ? > > Yes, the first assignment to 'a' created a new list and broke the > shared reference. > > I almost never use the x=y=value style for this reason. > For the minimal amount of typing I prefer either > > x = value1 > y = value2 > > or tuple assignment: > > x,y = v1,v2 > > Which for lists is: > > x,y = [],[] > > ie two separate empty lists. > > > I guess var1 = var2 = 0 is generally bad programming style ? > > Its fine if you're sure it's what you want, but what it looks like > isn't always what you get... as you discovered :-) > > > get my code more compact using list comprehension etc > > Compact code is not always a virtue. > Tuple assignment seems to me a good compromise. > And FWIW I try to limit tuple assignment to 3 values max > just for clarity. Also I try to ensure the variables are linked > in some way - like x,y coordinates, or similar types of variable: > max_len, min_len etc > Tupples, hadn't thought of that - I like it :) > > PS This is probably an impossible question but I always struggle to > > find names > > for variables - any hints ? > > Write a description of what it is there for - what does it do in the > program. > Abbreviate that to a couple of key words. That's your name... If you > want > a more detailed view find a copy of Code Complete by McConnell, it > has a whole chapter on variable naming issues... > Your variable names looked ok to me FWIW. Ahh buts that's the 5th time I have changed them - you should have seen the previous trys :) > > Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Larger GUI design ?
I have managed to write a small GUI app, it had a frm_app.py from eric3s QT designer & a dlg_app.py which defined a class that inherited the frm_app.py class ... all AOK I am now on a more ambitious project. There will be a main app screen, some dialogue screens for more info etc and a backend script analysing a database which will take some time to run. The backend script is almost there, just tons of auditing rules to write for it but the backend 'engine' is working. How to fit the GUI around it ? If I have my dlg_app.py inhereting frm_app.py all is well until I need a poppup dialogue. Would I define another module say dlg_info.py with its frm_info.py which I would import and call when needed generating its own QT object and window. And in that module code something like the following would make the window on the fly ? app = QApplication(sys.argv) win = info() app.setMainWidget(win) win.show() QObject.connect(app, SIGNAL('lastWindowClosed()'),app, SLOT('quit()')) app.exec_loop() Secondly my backend will take a while to run & I would like to display a status bar in the GUI. The only way i can see to do this is to either (1) make my backend a class & inherit dlg_app.py so I can access the QT widget directly or (2) pass info from the backend via a socket (yep know about them now!) to a QT script running in timerEvent() Which is the best method ? Or is there a better way ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Larger GUI design ?
On Tuesday 22 August 2006 17:15, Alan Gauld wrote: > > I am now on a more ambitious project. There will be a main app > > screen, some > > dialogue screens for more info etc and a backend script analysing a > > database > > which will take some time to run. > > > > How to fit the GUI around it ? > > > > If I have my dlg_app.py inhereting frm_app.py all is well until I > > need a > > poppup dialogue. > > Its usually better to make popups inherit directly from TopLevel > rather > than the parent form OK I will look into that > > > Would I define another module say dlg_info.py with its frm_info.py > > Its usually best to put each significant window/dialog in its own > form. > It makes them easier to reuse in other projects for one thing! > Code reuse is good > > would import and call when needed generating its own QT > > object and window. > > Not sure how QT works so can't comment on that. > > > app = QApplication(sys.argv) > > Are you sure there isn't a QDialog base class somewhere in QT? > Thats usually been the case in other GUI toolkits I've used. Base class ? OK you lost me - I will dig into the docs > > > Secondly my backend will take a while to run & I would like to > > display a > > status bar in the GUI. The only way i can see to do this is to > > either > > Sounds like a job for a thread... > > > (2) pass info from the backend via a socket (yep know about them > > now!) to a QT > > script running in timerEvent() > > You can do this but threads are less resource greedy. I had not thought of a thread - thats a cool solution Thanks for your suggestions, Its probably obvious for you old hands but its a whole new world for us beginners :) You have given me enough info to run with - I will give it a go & see what happens Dave > > HTH, > > Alan Gauld > Author of the Learn to Program web site > http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Package problem
I am having a problem with packaging. I have setup the PYTHONPATH to the core dir with __init__.py in the sub directories. It all worked as expected - then I hit a problem :( I have a module scanDBFs in dir main with a def of getDbfData The module I am executing is DocViewDoc in QT DocViewDoc can import with ... from main.scanDBFs import getDbfDir and execute it with ... a = getDbfData(dbf) So I know it works ... however I prefer to import main.scanDBFs and execute it with ... a = scanDBFs.getDbfData(dbf) So I can see which module it comes from However when i try the latter I get ... File "./DocViewDoc.py", line 80, in AuditEngine a = scanDBFs.getDbfData(dbf) NameError: global name 'scanDBFs' is not defined [EMAIL PROTECTED]:~/python_develop/unison/PxQxAudit/QT$ I am stuck ! - The only other thing Is that this is executed within a class, but that should not cause a problem. I have tried a = main.scanDBFs.getDbfData(dbf) but keep hitting the same problem. Any ideas ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] exit app withour raise ?
In the middle of an application, if someone presses the quit button I want to exit. At the moment i raise 'Quit button pressed' which works but spews a stack trace leading to the raise statement. Is there a neat way to just exit without a stack trace ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exit app withour raise ?
On Sunday 17 September 2006 17:54, Alan Gauld wrote: > > In the middle of an application, if someone presses the quit button > > I want to > > exit. At the moment i > > > > raise 'Quit button pressed' > > > > which works but spews a stack trace leading to the raise statement. > > Is there a > > neat way to just exit without a stack trace ? > > raise SystemExit > > or more commonly > > import sys > > sys.exit() > > You can add an argument to exit and that will be the error value > returned > to the OS. > > Alan G. Thanks for that - it works great :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] module file copy ?
OK have I missed it - but which module is file copy in ? I looked all around OS but no luck - I can find rename(src, dst) but that's about it. Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] module file copy ?
On Thursday 21 September 2006 21:48, Mike Hansen wrote: > > -Original Message- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Dave S > > Sent: Thursday, September 21, 2006 2:41 PM > > To: Python Tutor > > Subject: [Tutor] module file copy ? > > > > OK have I missed it - but which module is file copy in ? I > > looked all around > > OS but no luck - I can find rename(src, dst) but that's about it. > > > > Cheers > > > > Dave > > > > ___ > > Tutor maillist - Tutor@python.org > > http://mail.python.org/mailman/listinfo/tutor > > I think I had the same problem when I started using Python. > > It's the shutil module. > shutil.copyfile(src, dst) > Thats tucked away. thanks for pointing it out Dave > Mike > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] file open (take 2)
Hi, I am trying to read in an ascii text file, do some alterations and write it back. file = open(self.config.get('pdf','cert') + '/cert.pdf' , 'r+') lines = file.readlines() ... process lines ... file.writelines(lines) file.close() works but ends up appending a second modified copy to the original ... as per the python ref. Am I right in thinking that the only way is to open with a 'r', close them open with a 'w' ? Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] file open (take 2)
On Wednesday 27 September 2006 21:59, Dave S wrote: > Hi, > > I am trying to read in an ascii text file, do some alterations and write it > back. > > file = open(self.config.get('pdf','cert') + '/cert.pdf' , 'r+') > lines = file.readlines() > > ... process lines ... > > file.writelines(lines) > file.close() > > works but ends up appending a second modified copy to the original ... as > per the python ref. > > Am I right in thinking that the only way is to open with a 'r', close them > open with a 'w' ? > > Cheers > > Dave > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor Thanks for all your input - that's great - I also learnt about file.rewind() and file.seek(0) :) Thanks once again Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] getting 'pwd' for XP ?
I currently running XP (like a fish out of water :) and I need to know the dir that the python script is executed from. a linux 'pwd' How can I achieve this - I have looked in sys & os & os.path but found nothing suitable Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getting 'pwd' for XP ?
On Thursday 28 September 2006 16:42, Shantanoo Mahajan wrote: > +++ Dave S [28-09-06 16:10 +0100]: > | I currently running XP (like a fish out of water :) and I need to know > | the dir that the python script is executed from. a linux 'pwd' How can I > | achieve this - I have looked in sys & os & os.path but found nothing > | suitable > > > Python 2.4.3 (#2, Sep 26 2006, 15:27:42) > [GCC 3.4.4 [FreeBSD] 20050518] on freebsd6 > Type "help", "copyright", "credits" or "license" for more information. > > >>> import os > >>> os.getcwd() > > '/tmp' > > os.getcwd() it is - must have missed it - thanks Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] free IDE for Python?
On Monday 13 November 2006 23:03, Vadhri, Srinivas wrote: > Hi > > > > A newbie to Python. What is the free IDE for Python development > activities? ActiveState's Komodo IDE needs a license and a fee. > > > > Any recommendations? Eric3/4 works for me :) http://www.die-offenbachs.de/detlev/eric3-screenshots.html > > Regards, > > Srinivas Vadhri ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] .sort(key = ???)
Hi, I have a bunch of lists within lists that I need to sort ref item [4], I can't access my code at the moment but I basically ... a = [[...], [...], ] a.sort(item4) def item4(a,b): return a[4], b[4] Having googled I think there is a better way of doing this with the key attribute a.sort(key= ?? ) I cant get a handle on how key works or how to code it for my needs. Can anyone help ? Thanks in advance Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .sort(key = ???)
On Thursday 16 November 2006 22:35, John Fouhy wrote: > On 17/11/06, Dave S <[EMAIL PROTECTED]> wrote: > > Hi, > > > > I have a bunch of lists within lists that I need to sort ref item [4], I > > can't access my code at the moment but I basically ... > > [...] > > > Having googled I think there is a better way of doing this with the key > > attribute > > > > a.sort(key= ?? ) > > Yep, that's right. You need at least python2.4 for this, though. > > You need to pass the key= parameter a function that will take an > element of your list, and return the comparison key. > > eg: > > def item4(elem): > return elem[4] > > a.sort(key=item4) > > Since this is a common thing to do, there is a function in the > operator module that you can use, instead of defining your own: > > import operator > a.sort(key=operator.itemgetter(4)) > > (operator.itemgetter(4) will return a function that is basically > equivalent to item4() above) > > HTH! Sure does - cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] exception not raised XP file ?
Due to some sloppy programming on a commercial app :) I have a problem. I have some directories on an XP machine, I need to know which of these directories (*.cab_tmp) contains a file that that is being accessed by another XP program. I set it up so that a known file (SIZES.DBF) in a known directory is being accessed by said program. If I attempt to open said file with wordpad I get the error 'the document ...SIZES.DBF... is being used by another application and cannot be accessed !' So I wrote the following code posDirNames = filter((lambda x: x[-7:] == 'cab_tmp'), os.listdir(gsrpath)) for dirName in posDirNames: print dirName for fileName in os.listdir(gsrpath + '/' + dirName): try: file = gsrpath + '/' + dirName + '/' + fileName if fileName == 'SIZES.DBF': print file, f = open(file, 'w') f.close() except: print 'xx', print fileName, sys.exc_info() Expecting it to raise an exception when it hits SIZES.DBF because I am opening the file to write - no exception is raised. I know it is scanning SIZES.DBF because my if statement picks it up. Any ideas ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exception not raised XP file ?
On Saturday 18 November 2006 16:08, Roel Schroeven wrote: > Dave S schreef: > > Due to some sloppy programming on a commercial app :) I have a problem. > > > > I have some directories on an XP machine, I need to know which of these > > directories (*.cab_tmp) contains a file that that is being accessed by > > another XP program. > > To me the easiest solution seems to be using Process Explorer or Handle > from Sysinternals (at http://www.microsoft.com/technet/sysinternals > currently). > > At first sight your Python solution looks good to me; I don't know why > it doesn't work. > > Another approach I have used in the past is trying to rename the > directory instead of trying to open a file in that directory: Windows > cannot rename the directory when some program has a file open in that > directory. At least that's my experience. Thanks for the tip :) Is it just me - I seem to run into a lot of weird behaviour in windows (but then I am a Linux Junky :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] finding AcroRd32.exe - a better way ?
My app generates an on the fly PDF manual by using reportlab, once generated I would like it to be automatically opened and displayed by XP adobe reader. Here's where I get stuck. ... os.execv('E:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe', ('/n', '/s', 'user.pdf')) Does what I need and works great but I manualy searched around to find the path to AcroRd32.exe. Short of writting some code to scan any adobe dirs for this exe is there a more elegant way for me to ensure my app will work on any windows machine ? Any pointers would be greatly appreciated Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding AcroRd32.exe - a better way ?
On Thursday 30 November 2006 20:50, Terry Carroll wrote: > On Thu, 30 Nov 2006, Dave S wrote: > > My app generates an on the fly PDF manual by using reportlab, once > > generated I would like it to be automatically opened and displayed by XP > > adobe reader. > > > > Here's where I get stuck. ... > > > > os.execv('E:\Program Files\Adobe\Acrobat 7.0\Reader\AcroRd32.exe', > > ('/n', '/s', 'user.pdf')) > > > > Does what I need and works great but I manualy searched around to find > > the path to AcroRd32.exe. Short of writting some code to scan any adobe > > dirs for this exe is there a more elegant way for me to ensure my app > > will work on any windows machine ? > > try this: > > os.startfile('user.pdf') Neat, cool & 100% what I was looking for :):):) Cheers Dave PS I would offer to buy you a pint - but you probably don't live near Bedford England ! > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] XP & catching execl o/p ?
Hi all, Struggling with python & XP again. My app needs to know if a certain program is running on my XP box Ideal world - I can get the output of 'tasklist.exe' into a string. I have tried os.execl('') It throws the output to the terminal + I need the exact path to the executable (a bit of a trial) and os.startfile('...') it throws the output to a different window, but no exact path needed Any ideas how I can catch the output ? Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XP & catching execl o/p ?
On Tuesday 05 December 2006 20:58, Luke Paireepinart wrote: > Dave S wrote: > > Hi all, > > > > Struggling with python & XP again. My app needs to know if a certain > > program is running on my XP box > > > > Ideal world - I can get the output of 'tasklist.exe' into a string. > > > > I have tried > > > > os.execl('') > > It throws the output to the terminal + I need the exact path to the > > executable (a bit of a trial) > > > > and > > os.startfile('...') > > it throws the output to a different window, but no exact path needed > > > > Any ideas how I can catch the output ? > > You could try redirecting sys.stdout to a file-like class instance you > create. > OK ... could you give me a bit more on that ? ... probably me being a bit dense :) Cheers Dave > > Cheers > > > > Dave > > > > > > ___ > > 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] XP & catching execl o/p ?
On Tuesday 05 December 2006 22:42, Luke Paireepinart wrote: > Dave S wrote: > > On Tuesday 05 December 2006 20:58, Luke Paireepinart wrote: > >> Dave S wrote: > >>> Hi all, > >>> > >>> Struggling with python & XP again. My app needs to know if a certain > >>> program is running on my XP box > >>> > >>> Ideal world - I can get the output of 'tasklist.exe' into a string. > >>> > >>> I have tried > >>> > >>> os.execl('') > >>> It throws the output to the terminal + I need the exact path to the > >>> executable (a bit of a trial) > >>> > >>> and > >>> os.startfile('...') > >>> it throws the output to a different window, but no exact path needed > >>> > >>> Any ideas how I can catch the output ? > >> > >> You could try redirecting sys.stdout to a file-like class instance you > >> create. > > > > OK ... could you give me a bit more on that ? ... probably me being a bit > > dense :) > > Um... > > #test.py > class FileLikeObject(object): > def __init__(self,current_stdout): > self.data = [] > self.oldstdout = current_stdout > > def write(self,arg): > if arg != '\n': > self.data.append(arg) > > def output(self): > sys.stdout = self.oldstdout > print self.data > sys.stdout = self > > > > import sys > > f = FileLikeObject(sys.stdout) > sys.stdout = f > > print "hello." > f.output() > print "hi" > f.output() > #--- > > Be careful playing with the dark magic > Don't forget that print goes to stdout. > HTH, > -Luke Indeed it is dark :) Thanks for that - I will play Dave > > > Cheers > > > > Dave > > > >>> Cheers > >>> > >>> Dave > >>> > >>> > >>> ___ > >>> 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] XP & catching execl o/p ?
On Tuesday 05 December 2006 23:32, Alan Gauld wrote: > "Dave S" <[EMAIL PROTECTED]> wrote > > > Struggling with python & XP again. My app needs to know if a certain > > program > > is running on my XP box > > > > os.execl('') > > It throws the output to the terminal + I need the exact path to the > > executable > > (a bit of a trial) > > > > Any ideas how I can catch the output ? > > Look at the popen family of functions in the os module, and then > look at the subporocess module which supercedees them > (but the docs are expressed in tems of the oold functions!) > > Use subprocess because the older popen functions don't always > work reliably on Windows - there is a separate popen as part of > the winall package, but I think subprocess.Popen works Ok. > > There is more on this, including a simple example using subprocess, > in my OS topic in my tutorial. > > HTH, Thanks for your help - I will take a look. :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XP & catching execl o/p ?
On Tuesday 05 December 2006 23:32, Alan Gauld wrote: > "Dave S" <[EMAIL PROTECTED]> wrote > > > Struggling with python & XP again. My app needs to know if a certain > > program > > is running on my XP box > > > > os.execl('') > > It throws the output to the terminal + I need the exact path to the > > executable > > (a bit of a trial) > > > > Any ideas how I can catch the output ? > > Look at the popen family of functions in the os module, and then > look at the subporocess module which supercedees them > (but the docs are expressed in tems of the oold functions!) > > Use subprocess because the older popen functions don't always > work reliably on Windows - there is a separate popen as part of > the winall package, but I think subprocess.Popen works Ok. > > There is more on this, including a simple example using subprocess, > in my OS topic in my tutorial. > > HTH, OK playing around I knocked up some test code ... #!/usr/bin/env python # -*- coding: iso8859_1 -*- import subprocess a = subprocess.Popen('tasklist.exe', bufsize=0, shell=False, stdout=subprocess.PIPE, stderr=None, stdin=None, universal_newlines=True) op = a.stdout.readlines() for i in op: #print i #print pass #raw_input() This gives me what I need except when it runs windows flashes up a large black terminal window for a split second (yuk) This test would be performed while my app is running so thats not so good :) Any ideas on how to stop it displaying ? Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XP & catching execl o/p ?
On Wednesday 06 December 2006 08:48, Tim Golden wrote: > | Struggling with python & XP again. My app needs to know if a > | certain program is running on my XP box > > As a complete alternative, consider using WMI: > > > import os, sys > import wmi > > c = wmi.WMI () > for process in c.Win32_Process (Name="excel.exe"): > print "Excel is running" > break > else: > print "Excel is not running" > > > > (Uses: http://timgolden.me.uk/python/wmi.html) > > TJG Just looked at WMI - didn't know it existed ! Am going down the subprocess route first as its 'built in'. If that does not work - hello WMI Cheers Dave > > > This e-mail has been scanned for all viruses by Star. The > service is powered by MessageLabs. For more information on a proactive > anti-virus service working around the clock, around the globe, visit: > http://www.star.net.uk > > ___ > 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] XP & catching execl o/p ?
On Wednesday 06 December 2006 11:04, Kent Johnson wrote: > Dave S wrote: > > Hi all, > > > > Struggling with python & XP again. My app needs to know if a certain > > program is running on my XP box > > > > Ideal world - I can get the output of 'tasklist.exe' into a string. > > > > I have tried > > > > os.execl('') > > It throws the output to the terminal + I need the exact path to the > > executable (a bit of a trial) > > > > and > > os.startfile('...') > > it throws the output to a different window, but no exact path needed > > I'm not sure if this is helpful, but here is a program that uses > subprocess.Popen() to capture the output of cmd.exe. It makes a text > file which contains all the command help. By Scott David Daniels, taken > from > http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&; > > import subprocess as subp > > p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE, > stdout=subp.PIPE, stderr=subp.STDOUT) > flag = "(@@}" > print >>p.stdin, "PROMPT", flag > print >>p.stdin, "HELP" > print >>p.stdin, "EXIT" > text = p.stdout.read() > p.wait() > helptext = text[text.index(flag + 'HELP') + len(flag) + 4 : >text.index(flag + 'EXIT')] > words = [line.split(None, 1)[0] > for line in helptext.split('\n') > if line.strip()] > commands = [word for word in words if word.isupper()] > > dest = open('cmd_help.txt', 'wb') > p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE, > stdout=dest, stderr=subp.STDOUT) > print >>p.stdin, "PROMPT ()" > print >>p.stdin, "HELP" > for command in commands: >print >>p.stdin, "HELP", command > print >>p.stdin, "EXIT" > p.wait() > dest.close() > > > Kent The above is usefull, I am starting to print out these snippets for reference :). I have got subprocess to get the data I am now trying to get rid of the windows terminal flashing on my screen. Cheers Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] XP & catching execl o/p ?
On Wednesday 06 December 2006 11:43, Dave S wrote: > On Tuesday 05 December 2006 23:32, Alan Gauld wrote: > > "Dave S" <[EMAIL PROTECTED]> wrote > > > > > Struggling with python & XP again. My app needs to know if a certain > > > program > > > is running on my XP box > > > > > > os.execl('') > > > It throws the output to the terminal + I need the exact path to the > > > executable > > > (a bit of a trial) > > > > > > Any ideas how I can catch the output ? > > > > Look at the popen family of functions in the os module, and then > > look at the subporocess module which supercedees them > > (but the docs are expressed in tems of the oold functions!) > > > > Use subprocess because the older popen functions don't always > > work reliably on Windows - there is a separate popen as part of > > the winall package, but I think subprocess.Popen works Ok. > > > > There is more on this, including a simple example using subprocess, > > in my OS topic in my tutorial. > > > > HTH, > > OK playing around I knocked up some test code ... > > #!/usr/bin/env python > # -*- coding: iso8859_1 -*- > import subprocess > > a = subprocess.Popen('tasklist.exe', bufsize=0, shell=False, > stdout=subprocess.PIPE, stderr=None, stdin=None, universal_newlines=True) > op = a.stdout.readlines() > for i in op: > #print i > #print > pass > > #raw_input() > > This gives me what I need except when it runs windows flashes up a large > black terminal window for a split second (yuk) > > This test would be performed while my app is running so thats not so good > :) > > Any ideas on how to stop it displaying ? > > Dave 10 carrot idiot here :) change from test.py to test.pyw :) dave > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] subprocess & pyw conflict ?
Hi all, I thought I had my solution with subprocess ... my test code ... #!/usr/bin/env python # -*- coding: iso8859_1 -*- import subprocess a = subprocess.Popen('tasklist.exe', bufsize=0, stdout=subprocess.PIPE, universal_newlines=True) op = a.stdout.readlines() for i in op: if i.split(' ')[0] == 'gmanager.exe': f = open('E:\Documents and Settings\All Users\Desktop\gsr_running', 'w') f.close() works a treat when I run it as proc.py detects the process I am looking for & writes a dummy file to the desktop. :) but I get a black windows terminal flash up. The code will eventually run in an app.pyw so to check it would be OK I renamed my working proc.py to proc.pyw - it fails :(, No window (as expected), no dummy file (not expected) - the process gmanager.exe is running. So there seems to be a problem with subprocess & pyw Googling I found ... https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1358527&group_id=5470 So I tried the suggested workaround with proc.pyw ... #!/usr/bin/env python # -*- coding: iso8859_1 -*- import subprocess a = subprocess.Popen('tasklist.exe', bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, universal_newlines=True) op = a.stdout.readlines() a.stdin.close() for i in op: if i.split(' ')[0] == 'gmanager.exe': f = open('E:\Documents and Settings\All Users\Desktop\gsr_running', 'w') f.close() Still zip, and because there is no terminal, I cannot view any errors ! Any suggestions welcome :) Dave ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] subprocess & pyw conflict ?
On Thursday 07 December 2006 00:31, Luke Paireepinart wrote: > Dave S wrote: > > Hi all, > > > > I thought I had my solution with subprocess ... my test code ... > > > > > > > > #!/usr/bin/env python > > # -*- coding: iso8859_1 -*- > > > > import subprocess > > > > a = subprocess.Popen('tasklist.exe', bufsize=0, stdout=subprocess.PIPE, > > universal_newlines=True) > > op = a.stdout.readlines() > > > > for i in op: > > if i.split(' ')[0] == 'gmanager.exe': > > f = open('E:\Documents and Settings\All > > Users\Desktop\gsr_running', 'w') > > f.close() > > > > > > > > works a treat when I run it as proc.py detects the process I am looking > > for & writes a dummy file to the desktop. :) but I get a black windows > > terminal flash up. > > > > The code will eventually run in an app.pyw so to check it would be OK I > > renamed my working proc.py to proc.pyw - it fails :(, No window (as > > expected), no dummy file (not expected) - the process gmanager.exe is > > running. > > > > So there seems to be a problem with subprocess & pyw > > > > Googling I found ... > > https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1358527&grou > >p_id=5470 So I tried the suggested workaround with proc.pyw ... > > > > > > > > #!/usr/bin/env python > > # -*- coding: iso8859_1 -*- > > > > import subprocess > > > > a = subprocess.Popen('tasklist.exe', bufsize=0, stdin=subprocess.PIPE, > > stdout=subprocess.PIPE, universal_newlines=True) > > op = a.stdout.readlines() > > a.stdin.close() > > > > for i in op: > > if i.split(' ')[0] == 'gmanager.exe': > > f = open('E:\Documents and Settings\All > > Users\Desktop\gsr_running', 'w') > > f.close() > > > > > > Still zip, and because there is no terminal, I cannot view any errors ! > > > > Any suggestions welcome :) > > Well, because I'm batting 0 on this thread so far, I think I'll just go > ahead and suggest another bad solution! > You could try redirecting the output of sys.stderr to a file, and you > might be able to see the error message! :D > > If you hadn't said 'any suggestions welcome' I might've kept this to > myself :P Ahhh .. 'any suggestions' is an SOS call - all any any ideas are warmly greeted :) > > >>> import sys > >>> class TestClass(object): > > def __init__(self): > self.data = [] > def write(self,item): > self.data.append(item) > > >>> a = TestClass() > >>> sys.stderr = a > >>> salfjdsljfka321423 > >>> print a.data > > ['\nTraceback (most recent call last):', '\n', ' File "", > line 1, in -toplevel-\n', 'salfjdsljfka321423\n', "NameError: name > 'salfjdsljfka321423' is not defined\n"] > > Except instead of a file-like class, you could just use a real file. > But then it would only leave the last line intact. > So you'd probably want to make a class that wraps a file object, where > the write method just appends to an internal list, > and it writes it all out to the file when you call the Class.close() or > whatever. > Actually, I guess the program stops executing on an exception... > Hmm, not really sure what you'd do exactly. > > > Sure, there are better solutions, and this doesn't really help you with > your original problem, but it at least lets you see your error message! > HTH, > -Luke Thanks for that - I will give it a go & post back :) > > > ___ > > 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 & pyw conflict ?
On Thursday 07 December 2006 10:25, Dave S wrote: > On Thursday 07 December 2006 00:31, Luke Paireepinart wrote: > > Dave S wrote: > > > Hi all, > > > > > > I thought I had my solution with subprocess ... my test code ... > > > > > > > > > > > > #!/usr/bin/env python > > > # -*- coding: iso8859_1 -*- > > > > > > import subprocess > > > > > > a = subprocess.Popen('tasklist.exe', bufsize=0, stdout=subprocess.PIPE, > > > universal_newlines=True) > > > op = a.stdout.readlines() > > > > > > for i in op: > > > if i.split(' ')[0] == 'gmanager.exe': > > > f = open('E:\Documents and Settings\All > > > Users\Desktop\gsr_running', 'w') > > > f.close() > > > > > > > > > > > > works a treat when I run it as proc.py detects the process I am looking > > > for & writes a dummy file to the desktop. :) but I get a black windows > > > terminal flash up. > > > > > > The code will eventually run in an app.pyw so to check it would be OK I > > > renamed my working proc.py to proc.pyw - it fails :(, No window (as > > > expected), no dummy file (not expected) - the process gmanager.exe is > > > running. > > > > > > So there seems to be a problem with subprocess & pyw > > > > > > Googling I found ... > > > https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1358527&gr > > >ou p_id=5470 So I tried the suggested workaround with proc.pyw ... > > > > > > > > > > > > #!/usr/bin/env python > > > # -*- coding: iso8859_1 -*- > > > > > > import subprocess > > > > > > a = subprocess.Popen('tasklist.exe', bufsize=0, stdin=subprocess.PIPE, > > > stdout=subprocess.PIPE, universal_newlines=True) > > > op = a.stdout.readlines() > > > a.stdin.close() > > > > > > for i in op: > > > if i.split(' ')[0] == 'gmanager.exe': > > > f = open('E:\Documents and Settings\All > > > Users\Desktop\gsr_running', 'w') > > > f.close() > > > > > > > > > Still zip, and because there is no terminal, I cannot view any errors ! > > > > > > Any suggestions welcome :) > > > > Well, because I'm batting 0 on this thread so far, I think I'll just go > > ahead and suggest another bad solution! > > You could try redirecting the output of sys.stderr to a file, and you > > might be able to see the error message! :D > > > > If you hadn't said 'any suggestions welcome' I might've kept this to > > myself :P > > Ahhh .. 'any suggestions' is an SOS call - all any any ideas are warmly > greeted :) > > > >>> import sys > > >>> class TestClass(object): > > > > def __init__(self): > > self.data = [] > > def write(self,item): > > self.data.append(item) > > > > >>> a = TestClass() > > >>> sys.stderr = a > > >>> salfjdsljfka321423 > > >>> print a.data > > > > ['\nTraceback (most recent call last):', '\n', ' File "", > > line 1, in -toplevel-\n', 'salfjdsljfka321423\n', "NameError: name > > 'salfjdsljfka321423' is not defined\n"] > > > > Except instead of a file-like class, you could just use a real file. > > But then it would only leave the last line intact. > > So you'd probably want to make a class that wraps a file object, where > > the write method just appends to an internal list, > > and it writes it all out to the file when you call the Class.close() or > > whatever. > > Actually, I guess the program stops executing on an exception... > > Hmm, not really sure what you'd do exactly. > > > > > > Sure, there are better solutions, and this doesn't really help you with > > your original problem, but it at least lets you see your error message! > > HTH, > > -Luke > > Thanks for that - I will give it a go & post back :) > Oh my head OK after much tinkering I got the following to work with .pyw # scan windows task list to see if GSR is running f = os.popen('tasklist.exe', 'r') plist = f.readlines() f.close gsr_running = False # scan for GSR program for line in plist: if line.split(' ')[0] == 'gmanager.exe': gsr_running = True A Dave can relax, chill, project finished ... all works ... god .. (homer simpson moment !) > > > ___ > > > Tutor maillist - Tutor@python.org > > > http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OT GPL project finished, presentation looming
OK this is an OT question I have just finished a Python QT project for work, written in my own free time over the last several months (and with a lot of help from you guys :). Its 5500 lines of python over several modules (for me that huge) and a CSV 'rules' file of 500 lines, all GPL'd We use a commercial program that connects to remote intruder alarm systems and downloads their configs. Up to 80,000 software attributes per site. My python QT app scans the database files (.dbf), decodes them, scans them against a CSV list of rules and throws any anomaly's out to a GUI. It essentially audits the site programming checking for incorrect programming, configuration errors etc. In addition it generates audited PDF certificates, engineer summary's PDFs and user manuals PDFs for each specific site. Sometime in January I have to give a presentation and I know one of the questions will be. "Its what we want but you are not a company, what if you leave ?" I need an answer,.. What am I asking ... If need be are any of you for hire ? Dave PS it looks great, would love to post some pngs but it would violate the list rules :( ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT GPL project finished, presentation looming
On Thursday 07 December 2006 17:54, Luke Paireepinart wrote: > Dave S wrote: > > [snip explanation of program] > > Sounds really cool! Its a bit niche - but cool > > > Sometime in January I have to give a presentation and I know one of the > > questions will be. "Its what we want but you are not a company, what if > > you leave ?" I need an answer,.. > > My understanding is that if it's GPL'ed, they can use it whether you > work there or not. > Is that not true? Sorry I did not mean that, the GPL thing is AOK > > > What am I asking ... If need be are any of you for hire ? > > Why would hiring someone else help in this situation? I work for national company, they are very interested in what I am doing, there is nothing else in the marketplace that does this, but they are used to dealing with large software company's. They will be concerned about using my app because I am one person. What if I get hit by a bus ! what if I leave ? what happens when a new version of controller comes out & new CSV rules need to be written - what would they do ? Its all GPL so no secrets, my guess is that if $$ was offered to the Python community someone would be willing to maintain the code but I am unsure of how it would actually work ? Is it a case of "hello, python programmer for hire ?" or is there an 'official' procedure that I can include in my presentation ? I would guess this is a common problem for GPL software being introduced into commercial settings (the whole support thing). (It appears that none of the company's IT professionals can program !) > > Dave > > > > PS it looks great, would love to post some pngs but it would violate the > > list rules :( > > You can just upload the PNGs to a free webhost and link us to them, like > Photobucket or Imageshack. > > I imagine people just don't want big attachments in their e-mail. If > you give them a link to the picture > then they only have to download it if they really want to see what it is. OK I will do my presentation then do that as a kind of thank you for all your help :) > > HTH, > -Luke ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT GPL project finished, presentation looming
On Thursday 07 December 2006 22:35, Alan Gauld wrote: > "Dave S" <[EMAIL PROTECTED]> wrote > > > They will be concerned about using my app because I am one person. > > What if I > > get hit by a bus ! what if I leave ? > > This is a common problem in big companies including my own. > For years they wouldn't even use the GNU software because it > was "unsupported". I even had to buy a commercial version of > emacs for about $300... Ouch that must have hurt :( > > Eventually cygnus started offering support (for $5K per year) > and they allowed us to use emacs, gcc etc Eventually even > X windows. > Cool :) > Now they are more relaxed and we use Linux to run our > internal DHCP and DNS, even some web servers... > > > ...(It appears that none of the > > company's IT professionals can program !) > > That's also not unusual. In fact our company appears to be > slowly heading that way. We used to have 11,000 IT professionals > of which around 5000 were developers. Now we have 13,000 IT > professionals of whom about 1000 still write code. The coding > is mainly offshored to India and Eastern Europe. Our internal > people are being retrained on "higher value" roles like business > analysis, design/architecture, deployment/integration and > project management. So sad - programming is s much creative fun. I have heard it being called an art form - I would agree with that. > > So I now program in python as and when I can and call > it prototyping... > > They call it progress. > 'progress' ... mmm 'modern man management' ... (cynical mmm ...) > Alan G. > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor