Re: [Tutor] problem with a sub-class
Alan Gauld via Tutor wrote: > On 30/11/17 15:37, Shall, Sydney wrote: > >> My problem is with constructing a sub-class. >> >> My sub-class is constructed as follows: >> >> import Population_ProductivityV24 as POCWP > > Note that POCWP is an alias for the *module* Population_ProductivityV24. > It is not a class. > >> line 27 : class SimulateCycleZero(POCWP): > > Here you define a class that subclasses your imported module. > Frankly I'm surprised that you don't get an error there > but hey... > >> line 28 : def __init__(self, dirname_p): > > But this is now an init for a subclass of module. > >> The error message is as follows: >> >>File >> "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", >> line 27, in >> class SimulateCycleZero(POCWP): >> >> TypeError: module.__init__() takes at most 2 arguments (3 given) > > So I'm guessing the use of a module to subclass > has confused things and I confess I'm not clear on > exactly what is going on and why you get this message. A class is an instance of its metaclass. class A: pass is roughly equivalent to A = type("A", (), {}) # classname, base classes, class attributes and class B(A):>>> class A: ... def __init__(self, *args): ... print("__init__{}".format(args)) ... >>> class B(A()): pass ... __init__() __init__('B', (<__main__.A object at 0x7f3db8a1c048>,), {'__module__': '__main__', '__qualname__': 'B'}) >>> assert isinstance(B, A) >>> isinstance(B, A) >>> >>> True >>> B() >>> >>> Traceback (most recent call last): File "", line 1, in TypeError: 'A' object is not callable foo = 42 is roughly equivalent to B = type(A)("B", (A,), {"foo": 42}) When you subclass from an instance of A instead of A itself this becomes a = A() B = type(a)("B", (a,), {"foo": 42}) which can be simplified to B = A("B", (a,), {"foo": 42}) If this succeeds B is bound to an instance of A, but usually you'll see a TypeError, either immediately as the OP, >>> class A: pass ... >>> class B(A()): pass ... Traceback (most recent call last): File "", line 1, in TypeError: object() takes no parameters or later when you try to instantiate B: >>> class A: ... def __init__(self, *args): ... print("__init__{}".format(args)) ... >>> class B(A()): pass ... __init__() __init__('B', (<__main__.A object at 0x7f3db8a1c048>,), {'__module__': '__main__', '__qualname__': 'B'}) >>> isinstance(B, A) >>> >>> True >>> B() >>> >>> Traceback (most recent call last): File "", line 1, in TypeError: 'A' object is not callable > But I'm pretty sure you don;t want to subclass your > imported module and thats the mistake. __
[Tutor] [cleaned-up] Re: problem with a sub-class
Peter Otten wrote: Sorry for the mess; second attempt: A class is an instance of its metaclass. class A: pass is roughly equivalent to A = type("A", (), {}) # classname, base classes, class attributes and class B(A): foo = 42 is roughly equivalent to B = type(A)("B", (A,), {"foo": 42}) When you subclass from an instance of A instead of A itself this becomes a = A() B = type(a)("B", (a,), {"foo": 42}) which can be simplified to B = A("B", (a,), {"foo": 42}) If this succeeds B is bound to an instance of A, but usually you'll see a TypeError, either immediately as the OP, >>> class A: pass ... >>> class B(A()): pass ... Traceback (most recent call last): File "", line 1, in TypeError: object() takes no parameters or later when you try to instantiate B: >>> class A: ... def __init__(self, *args): ... print("__init__{}".format(args)) ... >>> class B(A()): pass ... __init__() __init__('B', (<__main__.A object at 0x7f3db8a1c048>,), {'__module__': '__main__', '__qualname__': 'B'}) >>> isinstance(B, A) >>> >>> True >>> B() >>> >>> Traceback (most recent call last): File "", line 1, in TypeError: 'A' object is not callable ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with a sub-class
On 30/11/2017 22:08, Alan Gauld via Tutor wrote: On 30/11/17 15:37, Shall, Sydney wrote: My problem is with constructing a sub-class. My sub-class is constructed as follows: import Population_ProductivityV24 as POCWP Note that POCWP is an alias for the *module* Population_ProductivityV24. It is not a class. line 27 : class SimulateCycleZero(POCWP): Here you define a class that subclasses your imported module. Frankly I'm surprised that you don't get an error there but hey... line 28 : def __init__(self, dirname_p): But this is now an init for a subclass of module. The error message is as follows: File "/Users/sydney/AnacondaProjects/Capital/Capital_with_productivity/Current_Versions/Simulate_Cycle_Zero_V3.py", line 27, in class SimulateCycleZero(POCWP): TypeError: module.__init__() takes at most 2 arguments (3 given) So I'm guessing the use of a module to subclass has confused things and I confess I'm not clear on exactly what is going on and why you get this message. But I'm pretty sure you don;t want to subclass your imported module and thats the mistake. Thanks to Alan and Peter for explaining sub-classing to me. I understand a bit better now. My program is corrected and does not give the error any more. -- Sydney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Problem with 'IF' condition
Hi, I am trying to compare two different values using "IF" condition in my program. Everything is working fine except this. I copied my program as plain text below, in the last section I used "IF" condition. - If you see in my code, I'm writing to "test1.txt" and saving that value in "nao" as well. On the other side, I'm reading from "test3.txt" and saving that value in "abb" just like above. Now, my goal is to compare these two variables nao & abb. As it written below. However, result never gives me "true" when actually this both variable contain same values. This code always giving me "not same" result. I'm not sure why this is not working. I would greatly appreciate your help. Please let me know for any question. tts.say("Hi") x = "C:/FTP_Folder/test1.txt" f = open(x) r = f.read(500) tts.say("Current position number in the text file is") tts.say(r) f.close() f1 = open(x,'w') z = f1.write("1") f1.close() tts.say("writing to file") tts.say("A.B.B. robot; go to my directed position") f = open(x) r0 = f.read(500) tts.say(r0) nao = r0 f.close() import time time.sleep(5) # delays for 5 seconds ##f1 = open(x,'w') ##f1.write("0") ##f1.close() y = "C:/FTP_Folder/test3.txt" f2 = open(y) r1 = f2.read(500) abb = r1 if nao == abb: print("Positions are same") print r, r1 else: print("not same") print nao, abb Best Regards, Achyut Ajmera ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
On 01/12/17 14:02, a.ajm...@incycleautomation.com wrote: > - If you see in my code, I'm writing to "test1.txt" and saving that value in > "nao" as well. > On the other side, I'm reading from "test3.txt" and saving that value in > "abb" just like above. > > Now, my goal is to compare these two variables nao & abb. As it written below. > However, result never gives me "true" when actually this both variable > contain same values. Are you absolutely sure they are the same? Have you checked, for example for newline characters or other whitespeace? You could do that by printing the repr() of the values: print repr(nao), repr(abb) Now simplifying your code slightly: > x = "C:/FTP_Folder/test1.txt" > f = open(x) > r = f.read(500) > f.close() > f1 = open(x,'w') > z = f1.write("1") > f1.close() Note that this will have deleted everything in x and replaced it with "1" > f = open(x) > r0 = f.read(500) r0 will now contain '1' > nao = r0 As will nao > f.close() > y = "C:/FTP_Folder/test3.txt" > f2 = open(y) > r1 = f2.read(500) > abb = r1 abb now contains whatever was in test3.txt. Did it contain '1'? > if nao == abb: > print("Positions are same") > print r, r1 r contains the original contents of test1 r1 contains the content of test3 > else: > print("not same") > print nao, abb whereas nao contains '1' and abb contains the same as r1 Is my interpretation what you see, and is it what you expect? In future with thee kinds of issues its good to include an actual cut n paste of the program output. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
a.ajm...@incycleautomation.com wrote: > I am trying to compare two different values using "IF" condition in my > program. Everything is working fine except this. I copied my program as > plain text below Your code has indentation errors, my analysis assumes # the following was added to make it runnable class TTS: def say(self, message): print "TTS says:", message tts = TTS() # end of my addition tts.say("Hi") x = "C:/FTP_Folder/test1.txt" f = open(x) r = f.read(500) tts.say("Current position number in the text file is") tts.say(r) f.close() f1 = open(x,'w') z = f1.write("1") f1.close() tts.say("writing to file") tts.say("A.B.B. robot; go to my directed position") f = open(x) r0 = f.read(500) tts.say(r0) nao = r0 f.close() import time time.sleep(5) # delays for 5 seconds ##f1 = open(x,'w') ##f1.write("0") ##f1.close() y = "C:/FTP_Folder/test3.txt" f2 = open(y) r1 = f2.read(500) abb = r1 if nao == abb: print("Positions are same") print r, r1 else: print("not same") print nao, abb > , in the last section I used "IF" condition. - If you see > in my code, I'm writing to "test1.txt" and saving that value in "nao" as > well. On the other side, I'm reading from "test3.txt" and saving that > value in "abb" just like above. > > Now, my goal is to compare these two variables nao & abb. As it written > below. However, result never gives me "true" when actually this both > variable contain same values. No, they don't. nao will contain the first 500 bytes of the file "C:/FTP_Folder/test1.txt" while abb will contain the first 500 bytes of the file "C:/FTP_Folder/test3.txt". But even if you start with two files with the same contents -- with the following lines > f1 = open(x,'w') > z = f1.write("1") > f1.close() you overwrite "C:/FTP_Folder/test1.txt" which now contains only a single "1". Thus the nao == abb test will only compare equal if "C:/FTP_Folder/test3.txt" contains a single "1", too. > This code always giving me "not same" > result. > > I'm not sure why this is not working. > I would greatly appreciate your help. > > Please let me know for any question. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with 'IF' condition
On Fri, Dec 01, 2017 at 07:02:47AM -0700, a.ajm...@incycleautomation.com wrote: > I copied my program as plain text below, Unfortunately you didn't, as the text you quote below will not run as Python code due to indentation errors. So you have (accidentally, I trust) messed up the indentation. Please take more care to copy and paste accurately, without adding extra spaces at the start of lines. > tts.say("Hi") Since tts is not defined, this fails immediately with a NameError. How is this line tts.say() relevant to your problem? It just adds extra code, and makes it impossible for us to run your code. There is no need to show us irrelevant code. > x = "C:/FTP_Folder/test1.txt" > f = open(x) > r = f.read(500) > tts.say("Current position number in the text file is") > tts.say(r) > f.close() > f1 = open(x,'w') This line has a spurious space added to the start. That's what I mean about accidentally messing up the indentation. > z = f1.write("1") > f1.close() You have now over-written the contents of file 'x' with a single digit '1'. > tts.say("writing to file") > tts.say("A.B.B. robot; go to my directed position") > f = open(x) > r0 = f.read(500) > tts.say(r0) > nao = r0 > f.close() And now you re-read the same file 'x', reading a single '1' (because that is all that is inside the file). r0 and nao will always be '1'. > import time Another copy-and-paste error introducing spurious indentation. > time.sleep(5) # delays for 5 seconds What is the point of this sleep? This has nothing to do with your problem. Take it out. > ##f1 = open(x,'w') > ##f1.write("0") > ##f1.close() > y = "C:/FTP_Folder/test3.txt" > f2 = open(y) > r1 = f2.read(500) > abb = r1 Now you read from a completely different file. > if nao == abb: > print("Positions are same") > print r, r1 > else: > print("not same") > print nao, abb Why do you expect them to be the same? You are reading from two completely different files, one has been overwritten by the digit '1' each time. The only possible way for these to be the same will be: (1) Start file x as anything. (2) Start file y with *exactly* a single digit '1' and nothing else. Be careful because some text editors will automatically add a newline to the end of your file when they save. (3) You read file x. (4) Then you over-write x with a single digit '1'. (5) Then you re-read x, reading the single digit '1' again. (6) Now you read y, getting '1'. If you get *anything* else, even a single space or newline, they won't match. (7) If they do match, your code prints the ORIGINAL (now over-written) contents of x, and the contents of y, which has to be '1' or else it will never match the NEW (over-written) contents of x. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting iterations of a function
On Thu, Nov 30, 2017 at 10:26:20AM -0600, Michael Dowell wrote: > Hello, I'm trying to code a magic 8-ball program and it needs to include a > counter for the number of questions asked. I'm having a hard time > implementing a count, and need a little help. I had tried to implement it > in the main() function, but that (naturally) led it to only counting '1' > for each question, for each question asked. Is there a way to count how > many times a give line of code is executed? For instance, every time the > oracle() function is run, a counter goes up. Thanks for any help. I haven't even looked at your code, but the obvious way to count the number of times a specific function is called is to have a separate counter for that function and have the function increment it. FOr example: oracle_count = 0 def oracle(): global oracle_count oracle_count += 1 ... Will that solve your problem? -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python
On Thu, Nov 30, 2017 at 09:20:32AM +0100, Jeroen van de Ven wrote: > Hello, > Can you build these 4 programms for me? Certainly. My rate is AUD$175 an hour, payable in advance. I estimate that it will take at least three hours to write and test the first three programs. You haven't shown the fourth program, so I don't know how long it will take, but let's say... ten hours in total? Better make it twelve to be on the safe side. Oh, just a reminder... if these are assignment questions at your college or school, the school might have something to say about plagiarism and submitting other people's work as your own. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor