[Tutor] How To structure a program for cmd line mode & gui mode
I"m writing a cmd line program which will automate getting some modules out of cvs, based on some input criteria. Initiallly, I will do a cmd line version, but would like to make a gui version later with QT. I would like to find out how to structure the program so that when the gui version is finised, it will still be fully functional in cmd line mode (without gui). The gui itself will be very simple- A listbox, and button or two. python program.py would run incmd line mode python program.py -g would run in gui mode. How difficult is this to do? Can anyone think of a simple example of a python app that runs in gui mode & cmd line mode? thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How To structure a program for cmd line mode & gui mode
"Tony Cappellini" <[EMAIL PROTECTED]> wrote > I would like to find out how to structure the program so that when > the gui > version is finised, it will still be fully functional in cmd line > mode For complex GUIs this can be quite difficult since GUIs are essentially stateless and command lines are usually stateful. But... > The gui itself will be very simple- A listbox, and button or two. For simple GUIs you are usually just collecting data and then executing one of several *transactions*. To do this via a command line is fairly simple. Just present a text menu of the possible transactions, then capture the data needed for the chosen transavction, then fire the transaction. The key thing is that the transaction code should be UI neutral, it should not have any form of input other than its parameters and it should retuirn its result as a string, or a tuple (or dictionary) of values that the UI can display. Combining GUI and Command versions in the same program just means reading the options at startup and calling the appropriate start UI function. Use modules to separate your concerns - one main, one for the GUI, one for the command line UI and one for the shared transactions. Import the shared module into both GUI modules and import both GUI modules into your main module. 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] Rounding a float to n significant digits
At 09:20 PM 11/30/2006, Tim Peters wrote: >[Dick Moores] >>... >>But isn't there a PRECISE answer to my question? > >Of course ;-) > >>Or is it OT? > >Well, it's really more a question about your machine's floating-point >hardware than about Python. Good explanations of exact limits for >IEEE-754 floating-point hardware can be found many places on the web. Well, I did find (2 - 2**-52)*2**1023 on http://babbage.cs.qc.edu/IEEE-754/References.xhtml , which I realize now is equivalent to your (2**53-1)*2**971. I can't say I understand the explanation. >Does it really help to know that, e.g., for an 8-byte IEEE double, the >largest representable finite positive value is exactly >(2**53-1)*2**971? Well, I like your round_to_n() function, and it's useful to know at what point it will fail. round_to_n(1.7976931348623158e+308,16) will compute on my machine, but round_to_n(1.7976931348623159e+308,16) will not. I was also curious as to what the precise max integer to float would be, even if that integer is not precisely relevant to your function. Far from it. Even this computes: >>> i = (2**53-1)*2**971 >>> >>> round_to_n(i+10**291.999, 16) '1.797693134862316e+308' (but this will not: round_to_n(i+10**292, 16) ) For people who have lost my original post, here's Tim's round_to_n() again: def round_to_n(x, n): """ Rounds float x to n significant digits, in scientific notation. Written by Tim Peters. See his Tutor list post of 7/3/04 at http://mail.python.org/pipermail/tutor/2004-July/030324.html """ if n < 1: raise ValueError("number of significant digits must be >= 1") return "%.*e" % (n-1, x) >BTW, the best way to manipulate "exact" values like this is via math.ldexp: > math.ldexp(2**53-1, 971) # largest finite positive double >1.7976931348623157e+308 math.ldexp(2**53, 971) # add 1 to the mantissa and it's "too big" >Traceback (most recent call last): > File "", line 1, in >OverflowError: math range error Thanks for that. Dick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best Known Method for Filtering redundant list items.
On 11/30/06, John Fouhy <[EMAIL PROTECTED]> wrote: > For the same reason that dictionaries don't preserve order. > Basically, sets are (I think) implemented using a hash table. You can > read about hash tables on wikipedia (or many other places), but one of > the components of a hash table is a function mapping keys to integers > in a particular range. Why not just call a sigar for a sigar. A set is a set, it may be implemented using a hash or it may be implemed using some other datastructure. It could be implemented using lists which preserves order, all though that doesn't make much sense. How it is implemented does not really matter here. http://en.wikipedia.org/wiki/Set If you want a collection of ordered objects, you don't want a set. Not even if the current implementation of sets in Python did preserve order. Doing so could not be considered as anything else than a ugly hack or exploitation of the current implementation. And would be likely to break in the future. Tor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best Known Method for Filtering redundant list items.
Ok, well... I think people lost the scope of my question.. I'm happy using the first method that was given to my post, I have stated in two emails that the order doesn't matter.. What I asked was why the order was changed, or more directly, what is the command actually doing to my data? I'm sure the order isn't totally random, but based on how the items are checked and dropped. The reason I care is because I'm just nosey like that and what to know what it is doing differently then the method I mentioned in the start of this thread. Never did I question the validity of the answer the first reply gave me, it works for what I need, not only that, it works well for what I need. I never put any stipulation on the order of the final data, so I didn't expect an answer that was order related. On 12/1/06, Tor Hildrum <[EMAIL PROTECTED]> wrote: On 11/30/06, John Fouhy <[EMAIL PROTECTED]> wrote: > For the same reason that dictionaries don't preserve order. > Basically, sets are (I think) implemented using a hash table. You can > read about hash tables on wikipedia (or many other places), but one of > the components of a hash table is a function mapping keys to integers > in a particular range. Why not just call a sigar for a sigar. A set is a set, it may be implemented using a hash or it may be implemed using some other datastructure. It could be implemented using lists which preserves order, all though that doesn't make much sense. How it is implemented does not really matter here. http://en.wikipedia.org/wiki/Set If you want a collection of ordered objects, you don't want a set. Not even if the current implementation of sets in Python did preserve order. Doing so could not be considered as anything else than a ugly hack or exploitation of the current implementation. And would be likely to break in the future. Tor ___ 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] Best Known Method for Filtering redundant list items.
Somewhat less sarcastic... I did omg find that answer, and I didn't feel like this was such a unique thing to do that I needed to re-invent the wheel by building several methods to do something that a common library should already have. On 11/30/06, Jordan Greenberg <[EMAIL PROTECTED]> wrote: Chris Hengge wrote: > Anyone point me to something more efficient then > > for item in list1: > if item not in list2: > list2.append() > > This just seems to take a bit a time when there are thousands or dozens of > thousands of records just to filter out the dozen or so copies.. > > Thanks. somewhat unsurprisingly, the first thing google lists for "python list duplicates" is a quite good ASPN recipe to do just what you want. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52560 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best Known Method for Filtering redundant list items.
On Fri, 2006-12-01 at 11:58 -0800, Chris Hengge wrote: > Ok, well... I think people lost the scope of my question.. I'm happy > using the first method that was given to my post, I have stated in two > emails that the order doesn't matter.. > > What I asked was why the order was changed, or more directly, what is > the command actually doing to my data? I'm sure the order isn't > totally random, but based on how the items are checked and dropped. > > The reason I care is because I'm just nosey like that and what to know > what it is doing differently then the method I mentioned in the start > of this thread. Your original method stepped through list1 and tested each element for existence in list2. Since you stated that the proportion of duplicates is low, most searches of list2 are unsuccessful and require checking every element of list2 from beginning to end. For long lists the search time adds up. sets and dictionaries are hash based. The location in the collection is based on a hash value. A check for membership computes the hash which is used to pinpoint the location in the collection. There are issues where different elements have identical hashes, but those are handled efficiently. So searching for matches is dramatically faster than stepping through a long list. I believe the ordering within sets and dictionaries happens to match the hash ordering. for x in aset: print hash(x) comes out in ascending order on the 3 sets I checked. That's not really a surprise. In terms of correct programs, the order of set and dictionary items should be viewed as arbitrary. > > Never did I question the validity of the answer the first reply gave > me, it works for what I need, not only that, it works well for what I > need. I never put any stipulation on the order of the final data, so I > didn't expect an answer that was order related. > > On 12/1/06, Tor Hildrum <[EMAIL PROTECTED]> wrote: > On 11/30/06, John Fouhy <[EMAIL PROTECTED]> wrote: > > > For the same reason that dictionaries don't preserve order. > > Basically, sets are (I think) implemented using a hash > table. You can > > read about hash tables on wikipedia (or many other places), > but one of > > the components of a hash table is a function mapping keys to > integers > > in a particular range. > > Why not just call a sigar for a sigar. > > A set is a set, it may be implemented using a hash or it may > be > implemed using some other datastructure. It could be > implemented using > lists which preserves order, all though that doesn't make much > sense. > How it is implemented does not really matter here. > > http://en.wikipedia.org/wiki/Set > > If you want a collection of ordered objects, you don't want a > set. Not > even if the current implementation of sets in Python did > preserve > order. Doing so could not be considered as anything else than > a ugly > hack or exploitation of the current implementation. And would > be > likely to break in the future. > > Tor > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- Lloyd Kvam Venix Corp ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How To structure a program for cmd line mode & gui mode
On Fri, Dec 01, 2006 at 12:21:33AM -0800, Tony Cappellini wrote: > I"m writing a cmd line program which will automate getting some modules out > of cvs, based on some > input criteria. > > Initiallly, I will do a cmd line version, but would like to make a gui > version later with QT. > > I would like to find out how to structure the program so that when the gui > version is finised, it will still be fully functional in cmd line mode > (without gui). Suggestions: 1. Look at module cmd in the Python standard library if you have not already. (see http://docs.python.org/lib/module-cmd.html) 2. In your code, separate functionality from interface (GUI). Your goal should be to be able to use the functions (or classes) that implement function in both the command line code and the GUI code. Dave [snip] -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How To structure a program for cmd line mode & gui mode
I'm wondering if this (snipped from another poster) 1. Look at module cmd in the Python standard library if you have not already. (see http://docs.python.org/lib/module-cmd.html) would be used for something like a built in console found in alot of games? On 12/1/06, Tony Cappellini <[EMAIL PROTECTED]> wrote: I"m writing a cmd line program which will automate getting some modules out of cvs, based on some input criteria. Initiallly, I will do a cmd line version, but would like to make a gui version later with QT. I would like to find out how to structure the program so that when the gui version is finised, it will still be fully functional in cmd line mode (without gui). The gui itself will be very simple- A listbox, and button or two. python program.py would run incmd line mode python program.py -g would run in gui mode. How difficult is this to do? Can anyone think of a simple example of a python app that runs in gui mode & cmd line mode? thanks ___ 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] which file runs
Hi, if there is a pyc and a py file within a module and the py file has a later date which file will python run? John -- John Fabiani ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which file runs
My understanding is that whenever you run a script that has a newer.py, the .py runs, otherwise it automagically will use the .pyc On 12/1/06, johnf <[EMAIL PROTECTED]> wrote: Hi, if there is a pyc and a py file within a module and the py file has a later date which file will python run? John -- John Fabiani ___ 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] which file runs
Chris Hengge schrieb: > My understanding is that whenever you run a script that has a newer.py, > the .py runs, otherwise it automagically will use the .pyc Not exactly. Whenever there is a .py file and no .pyc file is present or the .pyc file is older, Python will read the .py file, compile it into bytecode and execute that and also tries to write the bytecode to the .pyc file. If there already is a .pyc file and it is newer, Python will use it directly. (The rule is similar for .pyo files, which will be looked for/generated when Python is started with the -O option). Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] which file runs
Addendum: these rules only apply to Python *modules*. AFAIK, when you call a Python file as a script, no matching .pyc/.pyo file is used or generated. There are some clever tricks to run a .pyc file as a script. I think it is described in the Python CGI FAQ. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor