Re: [Tutor] IDLE Caching
> I have a problem with IDLE on Windows. > While I'm executing a python script in IDLE subsequence > editing and executions on the file sometimes doesn't reflect the > changes. This is a common issue, you are presumably importing your program rather than using IDLE's save/run feature? If so you will need to reload the module after changes using the reload() function. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE Caching
class Tree: def Tree(self): self.rootNode = None def Tree(self, rootNode): self.rootNode = rootNode def getRootNode(self): return self.rootNode def setRootNode(self, rootNode): self.rootNode = rootNodeclass Node: def Node(self): self.content = '' self.isleaf = False self.childnode = None self.label = '' def setContent(self, content=''): self.content = content def getContent(self): return self.content def setLeaf(self, leaf): self.isleaf = leaf def isLeaf(self): return self.isleaf def setChild(self, childnode): self.childnode = childnode def getChild(self): return self.childnode def setLabel(self, label=''): self.label = label def getLabel(self): return self.label t = Tree()n = Node()look at this code. Edit this code with IDLE. For example change one of the 'self' statementin the code. For instance change self => slf then save the file and press F5(Run Module) It doesn't complain about the code !!! this is my problem. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE Caching
[snip code] > look at this code. Edit this code with IDLE. For example change one of > the 'self' statement > in the code. For instance change self => slf then save the file and > press F5(Run Module) > It doesn't complain about the code !!! this is my problem. > I think maybe you're just confused about what 'self' is. 'self' is not a reserved keyword that's used for the name of class instances. for example: #-start code--- class Foo(object): def __init__(abracadabra): print 'BAR!' print abracadabra abracadabra.random_variable = 42 #make an instance of Foo and call its __init__ x = Foo() #print the value of random_variable print x.random_variable #end code- #---start output BAR! <__main__.Foo object at 0x00F0C930> 42 #---end output does this clear anything up for you? There's no reason to name your first arguments of methods of class instances 'self' other than readability and tradition. To the compiler it makes no difference. Do you still think it's a problem with IDLE? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE Caching
Really, I have realized the difference Luke said. However sometimes IDLE runs theold version, this is the thing I cant realized.On 7/24/06, Luke Paireepinart <[EMAIL PROTECTED]> wrote: [snip code]> look at this code. Edit this code with IDLE. For example change one of> the 'self' statement> in the code. For instance change self => slf then save the file and> press F5(Run Module) > It doesn't complain about the code !!! this is my problem.>I think maybe you're just confused about what 'self' is.'self' is not a reserved keyword that's used for the name of classinstances. for example:#-start code---class Foo(object):def __init__(abracadabra): print 'BAR!' print abracadabra abracadabra.random_variable = 42#make an instance of Foo and call its __init__ x = Foo()#print the value of random_variableprint x.random_variable#end code-#---start outputBAR!<__main__.Foo object at 0x00F0C930>42#---end output does this clear anything up for you?There's no reason to name your first arguments ofmethods of class instances 'self' other thanreadability and tradition. To the compilerit makes no difference. Do you still think it's a problem with IDLE? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] AssertionError issue
Hi,I have a class in python which creates an ssh connection to a remote machine.In a function of the class, I have put the code in try/except block.try: -- --except AssertionError: print "Error Condition"In this code, when error occurs, it raises the AssertionError but the destuctor isnt called itself.As a result the ssh conenctions are not closed.Whenever such errors occurs should I call a function closing the ssh connection?What is the general way a python script exits on encountering an error?ThanksAkanksha Do you Yahoo!? Get on board. You're invited to try the new Yahoo! Mail Beta.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Joe's learning Tk
> Thanks for the great email.I am beginning to see the light on event > binding, callbacks and functions. I have attached my program to show my > efforts so far. I think I need a some help in application. Hi Joe, I haven't seen your message about this on Tutor yet; have you reposted your question there? I'm still a bit busy and haven't had a chance to look at this thoroughly; the others on Python-tutor should take a look at this. If I understand your program, it appears to be a mineral or material hardness reference program? It may be nice to organize the data to make it easier to extend. Concretely, it looks like the program is dealing with hierarchical data: Materials Aluminum Wrought Die Cast Sand Cast Steel Low Carbon Medium/High Carbon Alloy ... and so keeping this data in a hierarchical data structure seems like a useful thing. Even something simple like: { 'Aluminum' : ['Wrought', ...], 'Steel', : ['Low Carbon', ...] } could be a first step in distilling out the interesting data from your program into a single place. At the moment, that knowledge seems spread out in the individual GUI elements, and it seems difficult to add more information without modifying several parts of the code. > I seem to be having problems applying things I read. May be I am > spending too much time just looking for similar finished programs on the > web were I can alter a few things to get results. It'll take both practice and feedback from others to get more comfortable with program design. Feel free to post your code on the mailing list to get feedback. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE Caching
> class Tree: > >def Tree(self): >self.rootNode = None >def Tree(self, rootNode): >self.rootNode = rootNode This second definition overrides the first - you cannot do function overloading based on parameters in Python. > > t = Tree() > n = Node() These just create instances they don;t excercise any of the methods - especially since you havenot defined any constructors. (ie __init__() methods) > look at this code. Edit this code with IDLE. For example change one > of the > 'self' statement > in the code. For instance change self => slf then save the file and > press > F5(Run Module) > It doesn't complain about the code !!! this is my problem. You aren't executing the faulty code so it won't see the mistake. You should write a tst program to execute all of the methods in your classes, then call that function to check the code after each change. 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] IDLE Caching
So from the answers, i want to imagine how python exercise the codewhen you push the button execute. My guess before looking at the docsor other material,1. check python syntax 2. transform to byte code.3. execute the byte code. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] simple Model-View-Controller example for QT/PyQT
Does anyone here have a example/demo using the MVC pattern, in a simple QT/pyQT program?thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Is there a method like this already?
Is there a method in Python like this already: [code] #This program calculates how many days it has been from one day to the other. def first_date(): 1y = int(raw_input("Enter the year of the first date: ")) 1m = int(raw_input("Enter the month of the first date: ")) 1d = int(raw_input("Enter the day of the first date: ")) def second_date(): 2y = int(raw_input("Enter the year of the second date: ")) 2m = int(raw_input("Enter the month of the second date: ")) 2d = int(raw_input("Enter the day of the second date: ")) def calculate_days(year, month, day): [/code] Just being curious, Nathan Pinno ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there a method like this already?
On 25/07/06, Nathan Pinno <[EMAIL PROTECTED]> wrote: > > Is there a method in Python like this already: > > [code] > #This program calculates how many days it has been from one day to the > other. Have a look at the datetime module: if date1 and date2 are both datetime.date instances, then (date1-date2) is a datetime.timedelta, and (date1-date2).days is the number of days in the timedelta. Reading the documentation helps! -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there a method like this already?
Nathan Pinno wrote: > Is there a method in Python like this already: > > [code] > #This program calculates how many days it has been from one day to the > other. Hi, Check the datetime module, it will give you distance between dates in the units you specify. It is included in the standard distribution. Hugo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] What's the invalid syntax?
What's the invalid syntax? [code] from datetime import * def menu(): print "(A)dd days to current date." print "(F)ind how many days have passed since a date." print "(E)xit." def menu_choice(): choice = raw_input("Enter the letter of your choice: ") return choice def date1(): y1 = int(raw_input("Enter the year: ")) m1 = int(raw_input("Enter the month: ")) d1 = int(raw_input("Enter the day: ")) date1 = date(y1, m1, d1) return date1 def date2(): y2 = int(raw_input("Enter the 2nd year: ")) m2 = int(raw_input("Enter the 2nd month: ")) d2 = int(raw_input("Enter the 2nd day: ")) date2 = date(y2, m2, d2) return date2 print "Date Calculator"print "By Nathan Pinno"printwhile 1: menu() menu_choice() if choice == A: date1() days = int(raw_input("Enter the number of days to add: ")) date3 = date1 + days print date(date3).isoformat(), " is the date ", days, " from today." elif choice == F: date1() date2() days = date2 - date1 print days, " days seperate " date2.isoformat(), " from ", date1.isoformat(), "." elif choice = E: break() else: print "That's not an option. Try again, please." [/code] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the invalid syntax?
Ignore this. I've created too many errors to even begin to solve. - Original Message - From: Nathan Pinno To: Tutor mailing list Sent: Monday, July 24, 2006 7:24 PM Subject: What's the invalid syntax? What's the invalid syntax? [code] from datetime import * def menu(): print "(A)dd days to current date." print "(F)ind how many days have passed since a date." print "(E)xit." def menu_choice(): choice = raw_input("Enter the letter of your choice: ") return choice def date1(): y1 = int(raw_input("Enter the year: ")) m1 = int(raw_input("Enter the month: ")) d1 = int(raw_input("Enter the day: ")) date1 = date(y1, m1, d1) return date1 def date2(): y2 = int(raw_input("Enter the 2nd year: ")) m2 = int(raw_input("Enter the 2nd month: ")) d2 = int(raw_input("Enter the 2nd day: ")) date2 = date(y2, m2, d2) return date2 print "Date Calculator"print "By Nathan Pinno"printwhile 1: menu() menu_choice() if choice == A: date1() days = int(raw_input("Enter the number of days to add: ")) date3 = date1 + days print date(date3).isoformat(), " is the date ", days, " from today." elif choice == F: date1() date2() days = date2 - date1 print days, " days seperate " date2.isoformat(), " from ", date1.isoformat(), "." elif choice = E: break() else: print "That's not an option. Try again, please." [/code] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What's the invalid syntax?
On 25/07/06, Nathan Pinno <[EMAIL PROTECTED]> wrote: > > What's the invalid syntax? I don't know, what did python tell you when you tried to run the code? -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Is there a method like this already?
> Is there a method in Python like this already: > #This program calculates how many days it has been from one day to > the other. Look at the datetime module it has methods for getting differences in daes/times etc. > def first_date(): >1y = int(raw_input("Enter the year of the first date: ")) >1m = int(raw_input("Enter the month of the first date: ")) >1d = int(raw_input("Enter the day of the first date: ")) Doesn't return anything so the data is lost as soon as you exit the function. You need to return the data(as a tuple?) so you can store it in a variable: return (1y,1m,1d) date1 = first_date() > def second_date(): > 2y = int(raw_input("Enter the year of the second date: ")) etc And this does exactly the same as first_date so you don't need it. Rename first_date to get_date(), remove the ones, and just call it twice: def get_date(): y = int(raw_input("Enter the year: ")) m = int(raw_input("Enter the month: ")) d = int(raw_input("Enter the day: ")) return (y,m,d) print "For the first date" date1 = get_date() print "For the second date" date2 = get_date() > def calculate_days(year, month, day): And this would dake the two dates as arguments and return a number of dats: def calculate_days(firstDate, secondDate): # use datetime module here 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] IDLE Caching
> So from the answers, i want to imagine how python exercise the code > when you push the button execute. My guess before looking at the > docs > or other material, > > 1. check python syntax > 2. transform to byte code. > 3. execute the byte code. What you have is correct for a main program. If its a module being imported there is a slight difference: 0) check if module already loaded, if yes stop 1) check if a pre-compiled version already exists 2) check if the source file is newer 3) if newer compile code to byte code and save pyc file 4) import byte code This is described in more detail in the documentation. 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] AssertionError issue
> In a function of the class, I have put the code in try/except block. > > try: > -- > -- > except AssertionError: > print "Error Condition" > > In this code, when error occurs, it raises the AssertionError > but the destuctor isnt called itself. The destructor will only get called if the object is destroyed. The object will be destroyed once all references to it are broken either by it moving out of scope (eg exiting a function/method) or by you del()'ing it sufficient times to reduce its reference count to zero.. > As a result the ssh conenctions are not closed. > What is the general way a python script exits on encountering an > error? raise SystemError or call sys,.exit() will exit completely. But a try/finally (as opposed to a try/except) will also be useful to guarantee execution of a block of code that can tidy up things like open sockets/files etc. 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] confused by linked queue
I am working out of How To Think Like A Computer Scientist. I am on the chapter that covers linked queues. Here is some code that creates a linked queue class: class Queue: def __init__(self): self.length = 0 self.head = None def isEmpty(self): return (self.length == 0) def insert(self, cargo): node = Node(cargo) node.next = None if self.head == None: # if list is empty the new node goes first self.head = node else: # find the last node in the list last = self.head while last.next: last = last.next # append the new node last.next = node self.length = self.length + 1 def remove(self): cargo = self.head.cargo self.head = self.head.next self.length = self.length - 1 return cargo The node is defined by: class Node: def __init__(self, cargo=None, next=None): self.cargo = cargo self.next = next def __str__(self): return str(self.cargo) I am confused by the insert class. I am not quite sure how it works. More to the point, I am not sure if it works: >>> from linked_queue import * >>> queue = Queue() >>> queue.isEmpty() True >>> queue.insert("cargo") >>> print queue.head cargo >>> print queue.length 1 >>> queue.insert("more_cargo") >>> print queue.length 2 >>> print queue.head cargo Where is my second node? How can I access it? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] confused by linked queue
On 25/07/06, Christopher Spears <[EMAIL PROTECTED]> wrote: >>>> from linked_queue import * > >>> queue = Queue() > >>> queue.isEmpty() > True > >>> queue.insert("cargo") > >>> print queue.head > cargo > >>> print queue.length > 1 > >>> queue.insert("more_cargo") > >>> print queue.length > 2 > >>> print queue.head > cargo > > Where is my second node? How can I access it? Try 'print queue.head.next' :-) A queue is, well, a queue. Visualise a queue of people. Initially, the queue is empty: there's no one standing there. Then someone comes and stands at the head of the queue; now the queue has one person in it (queue.length is 1). Then, another person comes along. That person starts at the head of the queue, and walks down the queue until they find the end. Then they join the end. This is what is happening in this bit of code --- here, you walk down the queue until you get to the end (ie: a node with no "next" node): # find the last node in the list last = self.head while last.next: last = last.next And here, you add the new node on to the end: # append the new node last.next = node It appears there is a bug in the code; these two lines should NOT be part of the while loop (ie: the indendation is incorrect). The result looks something like this: (best viewed with a fixed-width font) head --> /\/\ | next --+--> | next --+--> None |||| | cargo-\|| cargo-\| \---+/\---+/ | | \|/ \|/ "cargo""more cargo" Hope this helps; if not, ask more questions :-) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] confused by linked queue
After reading John's reply, I think I get it now: >>> from linked_queue import * >>> queue = Queue() >>> queue.isEmpty() True >>> queue.insert("cargo") >>> queue.length 1 >>> queue.insert("more cargo") >>> queue.length 2 >>> print queue.head cargo >>> print queue.head.next more cargo >>> queue.insert("more and better cargo") >>> print queue.head.next more cargo >>> print queue.head.next.next more and better cargo >>> queue.insert("snakes") >>> print queue.head cargo >>> last = queue.head >>> last.next >>> print last.next more cargo >>> print last.next.next more and better cargo >>> print last.next.next.next snakes One of my problems in conecptualizing this is that I thought a linked queue was just a linked list. Is a linked queue a linked list? There seems to be a subtle difference... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE Caching
let me dig into documentation before thinking in the blind :) thanks a lot Alan !On 7/25/06, Alan Gauld < [EMAIL PROTECTED]> wrote:> So from the answers, i want to imagine how python exercise the code > when you push the button execute. My guess before looking at the> docs> or other material,>> 1. check python syntax> 2. transform to byte code.> 3. execute the byte code. What you have is correct for a main program.If its a module being imported there is a slight difference:0) check if module already loaded, if yes stop1) check if a pre-compiled version already exists 2) check if the source file is newer3) if newer compile code to byte code and save pyc file4) import byte codeThis is described in more detail in the documentation.Alan GauldAuthor 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