[Tutor] Graphical Toolkit for new XLIFF translation editor
Hello list, KhmerOS is going to write an XLIFF translation editor with Python. Before starting, we would like to know which Graphical Toolkit should be used for the editor which supports many languages including Latin, Indic, Arabic and so on. However, there are many GUI toolkits such as QT, GTK, wxPython, Tkinter that have many different features that we don't know. So could you advice us which one should we use and why? Thanks, Phon ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner: using optional agument in __init__ breaks my code
Dnia niedziela, 25 czerwca 2006 22:45, Bob Gailer napisał: > To get the behavior I think you want try: > > def __init__(self, q = None): > if not q: q = [] > self.queue = q I would disagree... This sure works for empty arguments, but: >>> u = [1,2,3] >>> a = Queue(u) >>> b = Queue(u) >>> a.insert(12) >>> print b gives [1, 2, 3, 12] which is still wrong. However assigning self.queue a COPY of parameter (empty or not) gives the desired result: def __init__(self, q = []): self.queue = q[:] -- Pawel Kraszewski ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] doubt in Regular expressions
Luke, You are confusing me for the OP. Please read carefully next time before you respond to the wrong person. I bumped because the OP's question was not specific and I thought I talked about people making their requests or questions very specific if they expect any useful replies. So, Luke take note; the message was not from me! - Original Message - From: "Luke Paireepinart" <[EMAIL PROTECTED]> To: "Evans Anyokwu" <[EMAIL PROTECTED]>; Sent: Sunday, June 25, 2006 10:26 PM Subject: Re: [Tutor] doubt in Regular expressions > Post Script: Sorry for the double e-mail, Evans. I forgot to forward it > to the list the first time. > Also, why don't replies automatically forward themselves to the list like > the pygame mailing list does? > For privacy reasons, in case you want to reply to someone separately? > End of P.S. > - > > Evans Anyokwu wrote: >> bump > Please don't send useless messages like this to the list. If you don't > get a reply you want in a few days, send the message again. > You waited 30 minutes and bumped the post. This is a huge turn-off for me > and makes me not even want to consider your problem. > However, I'll assume that you're used to asking questions on high-traffic > message boards where this type of thing is necessary. > > [snip header] >> >> hello all, >> i want to search strings in the database available and return >> the link of the string instead simply returning the words... by >> using regular expressions(RE) i got a way to perform the string >> matchesgive some info regarding how to return the link of the >> matched strings... >> ravi. >> > Again, you need to be more specific. We have no idea what your data > structure looks like so we have no idea how to help you. > This is the way I would do it. > > class SearchObj(object): > def __init__(self): > self.database = {} > def addElement(astr,alink): > self.database[astr] = alink > def searchElement(astr): > bstr = "blah blah" #insert code to match the element they're trying > to search for with the closest one in the database. > try: return self.database[bstr] > except: pass > > This sounds like what you're trying to do, but I don't think it's a very > good way to make a search engine. > no matter how your string-matching is, a single keyword shouldn't be > mapped to a single site link. That doesn't make sense! > so if someone searches for "Puppy" they'll only get a single link back? > What's the point of that? > I want to search through the sites not have you search through them for > me, find out which you think I want, and only give me the link to that > one. > If I did I would use Google's I'm Feeling Lucky. > For that matter, I would use Google for any search. > Why are you wanting to make this search engine? > If it's for practice, I feel that it's not a very good project to practice > on, because the design issues are so much larger than the programming > itself. > For practicing programming you should probably use some simple example > that requires a lot of code. Like Tetris or Pong. > If you're doing this for commercial use, you should just look into adding > a Google SiteSearch to your page. > python.org did this and it works fantastically. > If you insist on continuing in this, I wish you luck and I hope everything > turns out how you want it. > Are you going to send us a link to it when you're done? > -Luke > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How can I add my folder to pythonpath?
> That is close but not quite right. The correct import will be > from mymodules import mymodule1 > > Kent My apologies for misleading instructions. Emily > >> >> >> Laszlo Antal wrote: >>> Hi, >>> >>> This is how my directory looks >>> myprogram(this is the main folder for my program) >>> | >>> myprogram.py >>> mymodules (this is where I store my modules) >>> | >>> mymodule1.py >>> mymodule2.py >>> >>> I would like to import from mymodules folder my modules1.py, >>> mymodules2.py into myprogram.py. >>> >>> How can I add mymodules folder to pythonpath >>> from myprogram.py.? >>> So if I copy myprogram folder into an other pc than myprogram.py can >>> take care of adding mymodules folder to pythonpath. >>> >>> Thank you in advance >>> Laszlo Antal >>> ___ >>> 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 maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions
On Sun, 25 Jun 2006, ravi sankar wrote: >we are doing an intranet portral search.we had a look at the > tutorials for Regular expressions...we need an insight on how to > implement this.we r in the process of developing a unified search... If you have a question, please ask it directly. Are you asking for help in finding search engine implementations in Python, or how to implement regular expressions? Another question that comes up directly is: if you do have to implement a search engine, why? The technology for search engines is already established. Have you looked at common implementations such as Lucene or ht://Dig yet? http://lucene.apache.org/java/docs/ http://www.htdig.org/ Other than that, this really sounds like an assignment for a data-mining class. It skirts too close to homework territiory. So unless you have specific Python questions, the help we can give you will be limited. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginner: using optional agument in __init__ breaks my code
>> The values of optional arguments are only once evaluated (when Python >> reads the definition). If you place there mutable objects like e.g. a >> list most of the time the effect you see is not what you want. So you >> have to write it a bit different. >> >> def __init__(self, q = None): >> if not q: q = [] >> self.queue = q >> >> Now you get a fresh list for each instance. >> > Thank you very much. I will use your code as a "recipe", while I still > try to understand the mechanism and the reasons behind it. For me this > feels odd. Hi Barbara, It is odd. *grin* There were at least two possible design choices to Python's behavior here. On every __init__, should Python re-evaluate the default argument expression? Or should that value be fixed? The designers of Python chose to the former, which leads to slight weirdness like this. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] doubt in Regular expressions
> You are confusing me for the OP. Please read carefully next time before > you respond to the wrong person. > > I bumped because the OP's question was not specific and I thought I > talked about people making their requests or questions very specific if > they expect any useful replies. Hi Evans, Let get this out in the open. I'm not certain that "bumping" helps on Python-Tutor. It's not like we're voting for certain questions to be answered. And the volume of questions isn't yet high enough to "bury" anything. We're not Digg or Slashdot. The key to mailing lists like this is to keep signal-to-noise as low as we can: otherwise, it ends up driving away volunteers who want to help but who have limited time. Bumping doesn't add signal, but it does annoy volunteers, and that's bad. So, let's please not do that again. Addressing a general problem: I'm getting the strong sense that some data-mining class has just given an assignment to write a search engine in Python. In this scenario, the instructor may link Python-Tutor as a good resource for asking questions. All of this would be perfectly fine. Well, sorta. If one is in an "upper-level" course such as data-mining, Python-tutor should not be much help, since we focus on basic programming questions that should already be familiar to any CS student. I don't mean this to be snobbery; this is just a statement of my expectations. In any case, we're getting hit now with questions on how to do this search engine assignment. If I'm reading this situation correctly: can someone contact that instructor and tell them to tell their students not to abuse Python-tutor? We're not a one-stop shop for homework or project help, and these sort of questions tend to hit the morale of volunteers a bit. Again, it's not healthy for the list. Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] database help for newbie, fetchall()
On Sun, 2006-06-25 at 20:14 -0400, Rene Bourgoin wrote: > What is the best way to handle the resutls to a fetchall() command? > The result seems to be a list of tuples [(aaa,bbb,ccc0,(11,222,333,)]. Correct > I'm new to programming but it seems that what ever I try to accomplish > at some point i need the results to end up as strings. The python sql module will have converted the data items into Python data types. That is usually preferable. If you really need all of your data as strings, your code could look something like: def strcols(cursor): for row in cursor.fetchall(): yield [str(col) for col in row] ... for cols in strcols(cursor): # cols will be a list of row values converted to strings # do what needs to be done ... > Even if you loop through the list you are left with a tuple that represents > each column. (aa,bbb,x,ccc) > Then you need to loop through the tuple to get your data into strings to use > in your app some where. > It seems to be more steps then it should be. > Is there a cleaner way to do this? You'll need to decide if that is cleaner than your current code. If you want named access to the column values, some of the Python database packages (such as MySQLdb) can be configured to return the rows as dictionaries rather than tuples. Also the strcols function could be changed to yield a dictionary rather than a list. The cursor.description provides the column names, along with other data. > > > > > ___ > No banners. No pop-ups. No kidding. > Make My Way your home on the Web - http://www.myway.com > > > ___ > 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
[Tutor] (no subject)
Stop the emails??? im getting more than i need regards _ The new MSN Search Toolbar now includes Desktop search! http://join.msn.com/toolbar/overview ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] database help for newbie, fetchall()
> What is the best way to handle the resutls to a fetchall() command? > The result seems to be a list of tuples > [(aaa,bbb,ccc0,(11,222,333,)]. Yes, thats usually the most convenient way to present groups of data. > I'm new to programming but it seems that what ever I try to > accomplish > at some point i need the results to end up as strings. Thats pretty unusual. Usually strings are only needed for display (other than where the data is a string by default - eg a name!) Mostly its moire convenient to treat numbers as numbers, dates as dates, money as money etc and only turn them into strings for display purposes - which is where the format string operator comes into its own. > Even if you loop through the list you are left with a tuple that > represents each column. (aa,bbb,x,ccc) Correct and you can extract each item using indexing: for group in result: myDate = group[0] myCash = group[3] myString = group[2] etc... > Then you need to loop through the tuple to get your data into > strings > to use in your app some where. Why do you need to convert to strings? If the data is string data it should still be string data when you extract it. If its not string data why do you need to convert it? Other than for display, and that can be done in one line, like: displayString = "%s\t%0.2f\t%s" % (myDate, myCash,myString) > It seems to be more steps then it should be. > Is there a cleaner way to do this? The question is probably more about why you feel the need for strings? 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] MySQLdb: cant get '... where field in %s' to work
>>> ARTICLES = ('XXX9', 'ABZ2') >>> TESTARTICLENAME = """SELECT * FROM tblForTransfer2Prodsite >>> WHERE articleName IN %r""" % (ARTICLES,) This uses normal string substitution so puts the tuple ARTICLES in the query using the repr format - includes parens) which is what you want. (BTW using uppercase for variable names is considered bad practice - it makes your code much harer to read, and in Python uppercase usually means a constant.) >>> SQLARTICLENAME = """SELECT * FROM tblForTransfer2Prodsite >>> WHERE articleName IN %s""" >>> print cur.execute(SQLARTICLENAME, (ARTICLES,)) This puts a tuple in the tuple but the substitution uses the substitution character of your database adaptor . Are you sure your adaprtor uses %s to substitute a tuple? You mat need to use str() on it first: print cur.execute(SQLARTICLENAME, (str(ARTICLES)) > Sorry, I neglected to state that I do not get any error message. > I expected both 'execute' statements to print 2 but the second > prints 0. > For integers, I get the results I expected. But I'm guessing... 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] Unit testing
Hey Everybody, First off, I like to thank Kent, Alan, and Danny for their invaluable help. You guys are amazing! I do have some questions about unit testing. I have read through the diving into python section about unit testing as well as the documentation from the python docs. While that gives me a good beginning, of course I'm hungry for more and the doc doesn't go far enough. Here are the questions: I have a part of the code that writes to the filesystem. Is the only way of unit testing that section of code to write a piece of code that will actually go out and test to see if that particular is out there or is there another way? How do I simulate my queues and other sources of data. In Java, there is a program called JMock that you can use to simulate that. Is there anything like that in Python or do I roll my own and create the appropriate queues with some data in the unit test? How would I unit test python GUIs Could you recommend a book on unit testing, and maybe a book python and unit testingThanks and much gratitude,Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
"Tino Dai" <[EMAIL PROTECTED]> writes: > How do I simulate my queues and other sources of data. In Java, there > is a program called JMock that you can use to simulate that. Is there > anything like that in Python or do I roll my own and create the appropriate > queues with some data in the unit test? >From JMock's page you get to mockobject's page and from there there's a link to Python-mock. http://sourceforge.net/projects/python-mock I hope it helps. -- Jorge Godoy <[EMAIL PROTECTED]> "Quidquid latine dictum sit, altum sonatur." - Qualquer coisa dita em latim soa profundo. - Anything said in Latin sounds smart. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
Tino Dai wrote: > Hey Everybody, > > First off, I like to thank Kent, Alan, and Danny for their > invaluable help. You guys are amazing! You are welcome! >I do have some questions about unit testing. > >I have a part of the code that writes to the filesystem. Is the > only way of unit testing that section of code to write a piece of code > that will actually go out and test to see if that particular is out > there or is there another way? I often write unit tests that do this. In my opinion it is simple and straightforward and effective. Some purists will insist that a unit test shouldn't write the file system or touch a database or use any other external resource, but I think that is silly - if the job of a bit of code is to write a file or interact with the database, then the simplest way to test it is to check the file or database. As long as the tests run fast enough it's OK. (For me, a unit test on a single module should ideally run in well under a second.) Alternately you can use StringIO or other substitutes for files in your tests. But somewhere in your test system you probably want to make sure the actual file is there on disk, whether it is in a unit test or acceptance test. Kent PS to the list - I am out of town this week so don't expect my usual volume of postings :-) ___ 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] (no subject)
> -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of shane davin > Sent: Monday, June 26, 2006 9:15 AM > To: Tutor@python.org > Subject: [Tutor] (no subject) > > Stop the emails??? > im getting more than i need > regards > > _ > The new MSN Search Toolbar now includes Desktop search! > http://join.msn.com/toolbar/overview > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Go to http://mail.python.org/mailman/listinfo/tutor See the bottom of the page to unsubscribe. Mike ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] for teachers and students: xturtle.py a new turtle graphics module
Gregor: Doesn't this really belong on Python-announce-list instead of here? You aren't asking any questions and you are announcing a new python-based application. That's what Python-announce-list is for. Post your announcement there and you will get plenty of feedback for your next release. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed > Date: Sun, 25 Jun 2006 23:42:09 +0200 > From: Gregor Lingl <[EMAIL PROTECTED]> > Subject: [Tutor] for teachers and students: xturtle.py a new tutle > graphicsmodule > To: Tutor > Message-ID: <[EMAIL PROTECTED]> > Content-Type: text/plain; charset=ISO-8859-15; format=flowed > > xturtle.py, extended turtle graphics > a new turtle graphics module for Python and Tkinter > > Version 0.9 of xturtle.py has been released. It can be found at: > > http://ada.rg16.asn-wien.ac.at/~python/xturtle > > xturtle should work properly on all major platforms (Mac, Linux and > Windows) Feedback would be appreciated to take it into account for > polishing it to a final version 1.0. > <> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
I often write unit tests that do this. In my opinion it is simple and straightforward and effective. Some purists will insist that a unit test shouldn't write the file system or touch a database or use any other external resource, but I think that is silly - if the job of a bit of code is to write a file or interact with the database, then the simplest way to test it is to check the file or database. As long as the tests run fast enough it's OK. (For me, a unit test on a single module should ideally run in well under a second.) Alternately you can use StringIO or other substitutes for files in your tests. But somewhere in your test system you probably want to make sure the actual file is there on disk, whether it is in a unit test or acceptance test. Ok, that leads me to my next question. Currently, I have a class that I want to unit test, but it contains a semaphore from another class. Now, I could make the semaphore a global variable, or I bring in the other class. One violates "good" programming principles and the other violates the unit testing principles. Is there another way? -Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Here's some interesting float oddness.
I tried this expecting an exceptionIn [2]: math.tan(math.pi/2)Out[2]: 16331778728383844.0so I thought maybe that was a float limit which it probably is as you get weird results from higher values but it seems strange that it tries to run with it. In [5]: 16331778728383844.0Out[5]: 16331778728383844.0In [6]: 16331778728383845.0Out[6]: 16331778728383844.0In [7]: 16331778728383846.0Out[7]: 16331778728383846.0In [8]: 16331778728383845.0 Out[8]: 16331778728383844.0In [9]: 16331778728383847.0Out[9]: 16331778728383848.0In [10]: 16331778728383848.0Out[10]: 16331778728383848.0weird huh? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Why doesn't it save the data before exiting?
Hey all, I am needing help on this. Why isn't it saving the data beore exiting the program? I don't get an error before exiting. Here's the code so far: accountlist = {} def load_file(ac): import os import pickle filename = 'accounts.txt' if os.path.exists(filename): store = open(filename, 'r') ac = pickle.load(store) else: store = open(filename, 'w') store.close() def save_file(ac): import pickle store = open('accounts.txt', 'w') pickle.dump(ac, store) store.close() def main_menu(): print "1) Add a new account" print "2) Remove a account" print "3) Print all info" print "4) Find account" print "5) Deposit" print "6) Withdraw funds" print "9) Save and exit." def add(): print "Add a new account" account = raw_input("Account Name: ") amount = float(raw_input("Amount: ")) accountlist[account] = amount def remove(): print "Remove a account" account = raw_input("Account: ") if accountlist.has_key(account): del accountlist[account] else: print account," was not found." def printall(): print "Account Info" for account in accountlist.keys(): print account+"\t $"+str(accountlist[account]),"\n" def lookup(): print "Specific Account Info" account = raw_input("Account: ") if accountlist.has_key(account): print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." def deposit(): print "Deposit funds" account = raw_input("Account: ") if accountlist.has_key(account): amount = float(raw_input("Amount: ")) accountlist[account] += amount print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." def withdraw(): print "Withdraw Funds." account = raw_input("Account: ") if accountlist.has_key(account): amount = float(raw_input("Amount: ")) accountlist[account] -= amount print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." print "Account Tracker"print "By Nathan Pinno"printload_file(accountlist)while 1: main_menu() menu_choice = int(raw_input("Which item? ")) if menu_choice == 1: add() elif menu_choice == 2: remove() elif menu_choice == 3: printall() elif menu_choice == 4: lookup() elif menu_choice == 5: deposit() elif menu_choice == 6: withdraw() elif menu_choice == 9: break else: print "That's not an option. Please choose a valid option."save_file(accountlist)print "Have a nice day!" Thanks for the help so far! Nathan Pinno ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why doesn't it save the data before exiting?
Nathan Pinno wrote: > Hey all, > > I am needing help on this. Why isn't it saving the data beore exiting > the program? But it does save it. What evidence do you have that it is not? Please in the future always tell us what the evidence of a problem is. Also I suggest you add validation of user input, to avoid the program terminating if the user hits the wrong key. In fact the whole menu thing would be easier to manage if the choices were character rather than integer. Then you don't need int() conversion and the exception raising if the user does not enter an integer string. Similar comment regarding checking input before applying float(). Consider % formatting for the outputs as in: print "%s\t $%2f\n" % (account, accountlist[account]) # instead of print account+"\t $"+str(accountlist[account]),"\n" Also I suggest you not open store for writing until just before the pickle.dump. Otherwise it is possible to have an empty file on which pickle.load will raise an exception. > I don't get an error before exiting. Good. You should not, unless you enter something that won't convert to integer, or string, or you leave an empty account.txt file. > > Here's the code so far: > accountlist = {} > > def load_file(ac): > import os > import pickle > filename = 'accounts.txt' > if os.path.exists(filename): > store = open(filename, 'r') > ac = pickle.load(store) > else: > store = open(filename, 'w') > store.close() > > def save_file(ac): > import pickle > store = open('accounts.txt', 'w') > pickle.dump(ac, store) > store.close() > > def main_menu(): > print "1) Add a new account" > print "2) Remove a account" > print "3) Print all info" > print "4) Find account" > print "5) Deposit" > print "6) Withdraw funds" > print "9) Save and exit." > > def add(): > print "Add a new account" > account = raw_input("Account Name: ") > amount = float(raw_input("Amount: ")) > accountlist[account] = amount > > def remove(): > print "Remove a account" > account = raw_input("Account: ") > if accountlist.has_key(account): > del accountlist[account] > else: > print account," was not found." > > def printall(): > print "Account Info" > for account in accountlist.keys(): > print account+"\t $"+str(accountlist[account]),"\n" > > def lookup(): > print "Specific Account Info" > account = raw_input("Account: ") > if accountlist.has_key(account): > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > > def deposit(): > print "Deposit funds" > account = raw_input("Account: ") > if accountlist.has_key(account): > amount = float(raw_input("Amount: ")) > accountlist[account] += amount > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > > def withdraw(): > print "Withdraw Funds." > account = raw_input("Account: ") > if accountlist.has_key(account): > amount = float(raw_input("Amount: ")) > accountlist[account] -= amount > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > > print "Account Tracker" > print "By Nathan Pinno" > print > load_file(accountlist) > while 1: > main_menu() > menu_choice = int(raw_input("Which item? ")) > if menu_choice == 1: > add() > elif menu_choice == 2: > remove() > elif menu_choice == 3: > printall() > elif menu_choice == 4: > lookup() > elif menu_choice == 5: > deposit() > elif menu_choice == 6: > withdraw() > elif menu_choice == 9: > break > else: > print "That's not an option. Please choose a valid option." > save_file(accountlist) > print "Have a nice day!" > > Thanks for the help so far! > Nathan Pinno > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
Bob Gailer wrote: > Nathan Pinno wrote: > >> Hey all, >> >> I am needing help on this. Why isn't it saving the data beore exiting >> the program? >> > But it does save it. What evidence do you have that it is not? > > Please in the future always tell us what the evidence of a problem is. > > Also I suggest you add validation of user input, to avoid the program > terminating if the user hits the wrong key. In fact the whole menu thing > would be easier to manage if the choices were character rather than > integer. Then you don't need int() conversion and the exception raising > if the user does not enter an integer string. > > Similar comment regarding checking input before applying float(). > > Consider % formatting for the outputs as in: > print "%s\t $%2f\n" % (account, accountlist[account]) # instead of > print account+"\t $"+str(accountlist[account]),"\n" > > Also I suggest you not open store for writing until just before the > pickle.dump. Otherwise it is possible to have an empty file on which > pickle.load will raise an exception. > >> I don't get an error before exiting. >> > Good. You should not, unless you enter something that won't convert to > integer, or string [ I MEANT float ], or you leave an empty account.txt file. > >> >> Here's the code so far: >> accountlist = {} >> >> def load_file(ac): >> import os >> import pickle >> filename = 'accounts.txt' >> if os.path.exists(filename): >> store = open(filename, 'r') >> ac = pickle.load(store) >> else: >> store = open(filename, 'w') >> store.close() >> >> def save_file(ac): >> import pickle >> store = open('accounts.txt', 'w') >> pickle.dump(ac, store) >> store.close() >> >> def main_menu(): >> print "1) Add a new account" >> print "2) Remove a account" >> print "3) Print all info" >> print "4) Find account" >> print "5) Deposit" >> print "6) Withdraw funds" >> print "9) Save and exit." >> >> def add(): >> print "Add a new account" >> account = raw_input("Account Name: ") >> amount = float(raw_input("Amount: ")) >> accountlist[account] = amount >> >> def remove(): >> print "Remove a account" >> account = raw_input("Account: ") >> if accountlist.has_key(account): >> del accountlist[account] >> else: >> print account," was not found." >> >> def printall(): >> print "Account Info" >> for account in accountlist.keys(): >> print account+"\t $"+str(accountlist[account]),"\n" >> >> def lookup(): >> print "Specific Account Info" >> account = raw_input("Account: ") >> if accountlist.has_key(account): >> print account+"\t $"+str(accountlist[account]),"\n" >> else: >> print account," was not found." >> >> def deposit(): >> print "Deposit funds" >> account = raw_input("Account: ") >> if accountlist.has_key(account): >> amount = float(raw_input("Amount: ")) >> accountlist[account] += amount >> print account+"\t $"+str(accountlist[account]),"\n" >> else: >> print account," was not found." >> >> def withdraw(): >> print "Withdraw Funds." >> account = raw_input("Account: ") >> if accountlist.has_key(account): >> amount = float(raw_input("Amount: ")) >> accountlist[account] -= amount >> print account+"\t $"+str(accountlist[account]),"\n" >> else: >> print account," was not found." >> >> print "Account Tracker" >> print "By Nathan Pinno" >> print >> load_file(accountlist) >> while 1: >> main_menu() >> menu_choice = int(raw_input("Which item? ")) >> if menu_choice == 1: >> add() >> elif menu_choice == 2: >> remove() >> elif menu_choice == 3: >> printall() >> elif menu_choice == 4: >> lookup() >> elif menu_choice == 5: >> deposit() >> elif menu_choice == 6: >> withdraw() >> elif menu_choice == 9: >> break >> else: >> print "That's not an option. Please choose a valid option." >> save_file(accountlist) >> print "Have a nice day!" >> >> Thanks for the help so far! >> Nathan Pinno >> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> >> > > > -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
How do I know? Simple. Next time I load it and ask it to print the list of accounts and how much in each, I only get: Account Info then the menu again, with no info. Nathan Pinno - Original Message - From: "Bob Gailer" <[EMAIL PROTECTED]> To: "Bob Gailer" <[EMAIL PROTECTED]> Cc: "Nathan Pinno" <[EMAIL PROTECTED]>; Sent: Monday, June 26, 2006 5:19 PM Subject: Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION > Bob Gailer wrote: >> Nathan Pinno wrote: >> >>> Hey all, >>> I am needing help on this. Why isn't it saving the data beore exiting >>> the program? >>> >> But it does save it. What evidence do you have that it is not? >> >> Please in the future always tell us what the evidence of a problem is. >> >> Also I suggest you add validation of user input, to avoid the program >> terminating if the user hits the wrong key. In fact the whole menu thing >> would be easier to manage if the choices were character rather than >> integer. Then you don't need int() conversion and the exception raising >> if the user does not enter an integer string. >> >> Similar comment regarding checking input before applying float(). >> >> Consider % formatting for the outputs as in: >> print "%s\t $%2f\n" % (account, accountlist[account]) # instead >> of >> print account+"\t $"+str(accountlist[account]),"\n" >> >> Also I suggest you not open store for writing until just before the >> pickle.dump. Otherwise it is possible to have an empty file on which >> pickle.load will raise an exception. >> >>> I don't get an error before exiting. >>> >> Good. You should not, unless you enter something that won't convert to >> integer, or string [ I MEANT float ], or you leave an empty account.txt >> file. >> >>> Here's the code so far: >>> accountlist = {} >>> def load_file(ac): >>> import os >>> import pickle >>> filename = 'accounts.txt' >>> if os.path.exists(filename): >>> store = open(filename, 'r') >>> ac = pickle.load(store) >>> else: >>> store = open(filename, 'w') >>> store.close() >>>def save_file(ac): >>> import pickle >>> store = open('accounts.txt', 'w') >>> pickle.dump(ac, store) >>> store.close() >>> def main_menu(): >>> print "1) Add a new account" >>> print "2) Remove a account" >>> print "3) Print all info" >>> print "4) Find account" >>> print "5) Deposit" >>> print "6) Withdraw funds" >>> print "9) Save and exit." >>> def add(): >>> print "Add a new account" >>> account = raw_input("Account Name: ") >>> amount = float(raw_input("Amount: ")) >>> accountlist[account] = amount >>> def remove(): >>> print "Remove a account" >>> account = raw_input("Account: ") >>> if accountlist.has_key(account): >>> del accountlist[account] >>> else: >>> print account," was not found." >>> def printall(): >>> print "Account Info" >>> for account in accountlist.keys(): >>> print account+"\t $"+str(accountlist[account]),"\n" >>> def lookup(): >>> print "Specific Account Info" >>> account = raw_input("Account: ") >>> if accountlist.has_key(account): >>> print account+"\t $"+str(accountlist[account]),"\n" >>> else: >>> print account," was not found." >>> def deposit(): >>> print "Deposit funds" >>> account = raw_input("Account: ") >>> if accountlist.has_key(account): >>> amount = float(raw_input("Amount: ")) >>> accountlist[account] += amount >>> print account+"\t $"+str(accountlist[account]),"\n" >>> else: >>> print account," was not found." >>> def withdraw(): >>> print "Withdraw Funds." >>> account = raw_input("Account: ") >>> if accountlist.has_key(account): >>> amount = float(raw_input("Amount: ")) >>> accountlist[account] -= amount >>> print account+"\t $"+str(accountlist[account]),"\n" >>> else: >>> print account," was not found." >>> print "Account Tracker" >>> print "By Nathan Pinno" >>> print >>> load_file(accountlist) >>> while 1: >>> main_menu() >>> menu_choice = int(raw_input("Which item? ")) >>> if menu_choice == 1: >>> add() >>> elif menu_choice == 2: >>> remove() >>> elif menu_choice == 3: >>> printall() >>> elif menu_choice == 4: >>> lookup() >>> elif menu_choice == 5: >>> deposit() >>> elif menu_choice == 6: >>> withdraw() >>> elif menu_choice == 9: >>> break >>> else: >>> print "That's not an option. Please choose a valid option." >>> save_file(accountlist) >>> print "Have a nice day!" >>> Thanks for the help so far! >>> Nathan Pinno >>> >>> >>> ___ >>> Tutor maillist - Tutor@python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> >> > > > --
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
Nathan Pinno wrote: > How do I know? Simple. Next time I load it and ask it to print the > list of accounts and how much in each, I only get: > > Account Info > > then the menu again, with no info. Ah. But the program starts setting accountlist = {}. When you reach printall, accountlist is still {}. Can you figure out why? Hint: load_file(accountlist) does not change accountlist. > > Nathan Pinno > - Original Message - From: "Bob Gailer" <[EMAIL PROTECTED]> > To: "Bob Gailer" <[EMAIL PROTECTED]> > Cc: "Nathan Pinno" <[EMAIL PROTECTED]>; > Sent: Monday, June 26, 2006 5:19 PM > Subject: Re: [Tutor] Why doesn't it save the data before exiting? > CORRECTION > > >> Bob Gailer wrote: >>> Nathan Pinno wrote: >>> Hey all, I am needing help on this. Why isn't it saving the data beore exiting the program? >>> But it does save it. What evidence do you have that it is not? >>> >>> Please in the future always tell us what the evidence of a problem is. >>> >>> Also I suggest you add validation of user input, to avoid the >>> program terminating if the user hits the wrong key. In fact the >>> whole menu thing would be easier to manage if the choices were >>> character rather than integer. Then you don't need int() conversion >>> and the exception raising if the user does not enter an integer string. >>> >>> Similar comment regarding checking input before applying float(). >>> >>> Consider % formatting for the outputs as in: >>> print "%s\t $%2f\n" % (account, accountlist[account]) # >>> instead of >>> print account+"\t $"+str(accountlist[account]),"\n" >>> >>> Also I suggest you not open store for writing until just before the >>> pickle.dump. Otherwise it is possible to have an empty file on which >>> pickle.load will raise an exception. >>> I don't get an error before exiting. >>> Good. You should not, unless you enter something that won't convert >>> to integer, or string [ I MEANT float ], or you leave an empty >>> account.txt file. >>> Here's the code so far: accountlist = {} def load_file(ac): import os import pickle filename = 'accounts.txt' if os.path.exists(filename): store = open(filename, 'r') ac = pickle.load(store) else: store = open(filename, 'w') store.close() def save_file(ac): import pickle store = open('accounts.txt', 'w') pickle.dump(ac, store) store.close() def main_menu(): print "1) Add a new account" print "2) Remove a account" print "3) Print all info" print "4) Find account" print "5) Deposit" print "6) Withdraw funds" print "9) Save and exit." def add(): print "Add a new account" account = raw_input("Account Name: ") amount = float(raw_input("Amount: ")) accountlist[account] = amount def remove(): print "Remove a account" account = raw_input("Account: ") if accountlist.has_key(account): del accountlist[account] else: print account," was not found." def printall(): print "Account Info" for account in accountlist.keys(): print account+"\t $"+str(accountlist[account]),"\n" def lookup(): print "Specific Account Info" account = raw_input("Account: ") if accountlist.has_key(account): print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." def deposit(): print "Deposit funds" account = raw_input("Account: ") if accountlist.has_key(account): amount = float(raw_input("Amount: ")) accountlist[account] += amount print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." def withdraw(): print "Withdraw Funds." account = raw_input("Account: ") if accountlist.has_key(account): amount = float(raw_input("Amount: ")) accountlist[account] -= amount print account+"\t $"+str(accountlist[account]),"\n" else: print account," was not found." print "Account Tracker" print "By Nathan Pinno" print load_file(accountlist) while 1: main_menu() menu_choice = int(raw_input("Which item? ")) if menu_choice == 1: add() elif menu_choice == 2: remove() elif menu_choice == 3: printall() elif menu_choice == 4: lookup() elif menu_choice == 5: deposit() elif menu_choice == 6: withdraw() elif menu_choice == 9: break else: print "That's not an option. Please choose a valid op
Re: [Tutor] Unit testing
Regards, Tino: I agree with Kent on this. As much as possible, a unit test should test what it is supposed to do. > Date: Mon, 26 Jun 2006 15:50:36 -0400 > From: "Tino Dai" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] Unit testing > To: "Kent Johnson" <[EMAIL PROTECTED]>, tutor@python.org > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > > > I often write unit tests that do this. In my opinion it is simple and > > straightforward and effective. Some purists will insist that a unit test > > shouldn't write the file system or touch a database or use any other > > external resource, but I think that is silly - if the job of a bit of > code > > is to write a file or interact with the database, then the simplest way > to > > test it is to check the file or database. As long as the tests run fast > > enough it's OK. (For me, a unit test on a single module should ideally > run > > in well under a second.) > > > > Alternately you can use StringIO or other substitutes for files in your > > tests. But somewhere in your test system you probably want to make sure > the > > actual file is there on disk, whether it is in a unit test or acceptance > > test. > > > Ok, that leads me to my next question. Currently, I have a class that I > want to unit test, but it contains a semaphore from another class. Now, I > could make the semaphore a global variable, or I bring in the other class. > One violates "good" programming principles and the other violates the unit > testing principles. Is there another way? > > -Tino > -- next part -- > An HTML attachment was scrubbed... > URL: > http://mail.python.org/pipermail/tutor/attachments/20060626/bf127497/att ac > hment.html > > -- I think the same principle applies here. The code you are testing involves a semaphore. By all means, include the other class and test the semaphore. I'm not familiar with the 'unit testing principles' that would forbid your doing this. Even if I were, principles are to be followed when they make sense. When it makes better sense to violate a principle, then violate it. Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
Would it be this line? pickle.dump(ac, store) Would this make it work? ac = pickle.dump(store) Nathan - Original Message - From: "Bob Gailer" <[EMAIL PROTECTED]> To: "Nathan Pinno" <[EMAIL PROTECTED]> Cc: Sent: Monday, June 26, 2006 5:33 PM Subject: Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION > Nathan Pinno wrote: >> How do I know? Simple. Next time I load it and ask it to print the list >> of accounts and how much in each, I only get: >> >> Account Info >> >> then the menu again, with no info. > Ah. But the program starts setting accountlist = {}. When you reach > printall, accountlist is still {}. Can you figure out why? > > Hint: load_file(accountlist) does not change accountlist. >> >> Nathan Pinno >> - Original Message - From: "Bob Gailer" <[EMAIL PROTECTED]> >> To: "Bob Gailer" <[EMAIL PROTECTED]> >> Cc: "Nathan Pinno" <[EMAIL PROTECTED]>; >> Sent: Monday, June 26, 2006 5:19 PM >> Subject: Re: [Tutor] Why doesn't it save the data before exiting? >> CORRECTION >> >> >>> Bob Gailer wrote: Nathan Pinno wrote: > Hey all, > I am needing help on this. Why isn't it saving the data beore exiting > the program? > But it does save it. What evidence do you have that it is not? Please in the future always tell us what the evidence of a problem is. Also I suggest you add validation of user input, to avoid the program terminating if the user hits the wrong key. In fact the whole menu thing would be easier to manage if the choices were character rather than integer. Then you don't need int() conversion and the exception raising if the user does not enter an integer string. Similar comment regarding checking input before applying float(). Consider % formatting for the outputs as in: print "%s\t $%2f\n" % (account, accountlist[account]) # instead of print account+"\t $"+str(accountlist[account]),"\n" Also I suggest you not open store for writing until just before the pickle.dump. Otherwise it is possible to have an empty file on which pickle.load will raise an exception. > I don't get an error before exiting. > Good. You should not, unless you enter something that won't convert to integer, or string [ I MEANT float ], or you leave an empty account.txt file. > Here's the code so far: > accountlist = {} > def load_file(ac): > import os > import pickle > filename = 'accounts.txt' > if os.path.exists(filename): > store = open(filename, 'r') > ac = pickle.load(store) > else: > store = open(filename, 'w') > store.close() >def save_file(ac): > import pickle > store = open('accounts.txt', 'w') > pickle.dump(ac, store) > store.close() > def main_menu(): > print "1) Add a new account" > print "2) Remove a account" > print "3) Print all info" > print "4) Find account" > print "5) Deposit" > print "6) Withdraw funds" > print "9) Save and exit." > def add(): > print "Add a new account" > account = raw_input("Account Name: ") > amount = float(raw_input("Amount: ")) > accountlist[account] = amount > def remove(): > print "Remove a account" > account = raw_input("Account: ") > if accountlist.has_key(account): > del accountlist[account] > else: > print account," was not found." > def printall(): > print "Account Info" > for account in accountlist.keys(): > print account+"\t $"+str(accountlist[account]),"\n" > def lookup(): > print "Specific Account Info" > account = raw_input("Account: ") > if accountlist.has_key(account): > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > def deposit(): > print "Deposit funds" > account = raw_input("Account: ") > if accountlist.has_key(account): > amount = float(raw_input("Amount: ")) > accountlist[account] += amount > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > def withdraw(): > print "Withdraw Funds." > account = raw_input("Account: ") > if accountlist.has_key(account): > amount = float(raw_input("Amount: ")) > accountlist[account] -= amount > print account+"\t $"+str(accountlist[account]),"\n" > else: > print account," was not found." > print "Account Tracker" > print "By Nathan Pinno" > print > load_file(accountlist) > while 1: > main_menu() > menu_choice = int(raw_input("Which
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
Nathan Pinno wrote: I already told you that the data is being saved to the file. And is being reloaded by ac = pickle.load(store). But the reloaded data is not being assigned to accountlist, since parameters to functions are treated as local variables. Assigning to a parameter in a function does NOT change the value of the parameter in the call. You can demonstrate this as follows: >>> x = 2 >>> def f(y): ...y = 3 >>> print x 2 To fix your program, define load_file as: def load_file(): import os import pickle filename = 'accounts.txt' if os.path.exists(filename): store = open(filename, 'r') ac = pickle.load(store) store.close() else: ac = {} return ac And change load_file(accountlist) to: accountlist = load_file() [snip] -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
> Ok, that leads me to my next question. Currently, I have a class > that I > want to unit test, but it contains a semaphore from another class. > Now, I > could make the semaphore a global variable, or I bring in the other > class. > One violates "good" programming principles and the other violates > the unit > testing principles. Is there another way? Reconsider your definition of a "Unit" maybe? A Unit should stand alone, it is the smallest amount of code that can stand alone. If your class relies on another class maybe both classes need to be considered as a single unit? Or maybe the classes need to be refactored to make them less closely coupled? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Why doesn't it save the data before exiting? CORRECTION
Hi Nathan, > How do I know? Simple. Next time I load it and ask it to print the > list of accounts and how much in each, I only get: OK, but that could be down to: 1) The save file not working, 2) the load file not working 3) the print accounts not working Have you checked whether the store file exists and is non zero in size? Have you checked that the load file is populating your accountlist? The code looks OK on first inspection, so we need to do a bit more detailed digging. I would definitely tidy it up accoding to the previous suggestions, in particular the spurious open(store('w') in the loadfile should be removed, its doing no good and could be causing harm... And using a format string defined at the top of the code and then reused in each function would improve consistency, maintainability and performance. Good luck Alan G. >>> Consider % formatting for the outputs as in: >>> print "%s\t $%2f\n" % (account, accountlist[account]) # >>> instead fmt = "%s\t $.2f\n" print fmt % (account,accountList[account]) def load_file(ac): import os import pickle filename = 'accounts.txt' if os.path.exists(filename): store = open(filename, 'r') ac = pickle.load(store) else: store = open(filename, 'w') The else bit does nothing useful. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Unit testing
On 6/26/06, Tino Dai <[EMAIL PROTECTED]> wrote: [...] > How would I unit test python GUIs Few weeks back I wrote a small article, may be helpful, so here it is : http://baijum81.livejournal.com/11598.html Regards, Baiju M ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor