Re: [Tutor] pure function problem
The problem is that your class definition doesn't do anything to explicitly set those attributes. On Thu, Sep 23, 2010 at 4:58 AM, Roelof Wobben wrote: > class tijd : > pass You're not doing any explicit setting of attributes at the class level. > time = tijd() > time.hour = 20 > time.minutes = 20 > time.seconds = 20 You set them on this instance. > seconds = 20 > uitkomst = tijd() But not on this one. What you probably want to do is something like this: class tijd(object): def __init__(self): self.hour = 20 self.minutes = 20 self.seconds = 20 Or if you prefer to set these when you create the instance, you can pass in values like this: class tijd(object): def __init__(self, hour=20, minutes=20, seconds=20): self.hour = hour self.minutes = minutes self.seconds = seconds I noticed something odd just a sec ago. You have this: > uitkomst = tijd() > uitkomst = increment(time, seconds) > print uitkomst.minutes, uitkomst.seconds You're creating a tijd instance, binding uitkomst to it, then overwriting that instance with what you return from increment(). Anyway, hth. - jmj ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am looking for a book on Beginners who never programmed before or have no experience in programming
On Sat, Sep 25, 2010 at 11:50 PM, Preetinder Singh wrote: > Hi I am trying to learn how to program, I want to become a software > developer so I can develop a software and also understand how to write my > own software not copying someone else. So if there any book or online > tutorials for complete beginners. I went to the python website and on wiki > python and there are so many books to choose from, please help me choose > one. > "Beginning Python" by Magnus lie Hetland is good. I have the first edition, but not the second. I can only imagine that it got better. "Head First Programming: A Learner's Guide to Programming Using the Python Language" by David Griffiths and Paul Barry was a great read. It's unconventional (by their nature, all Head First books are), but excellent. It's about programming in general, but uses Python to demonstrate. There's also a new book coming out shortly that will probably benefit you: "Head First Python". Again, it's unconventional, but thoroughly enjoyable and informative. Disclaimer: I tech reviewed all 3 of these books. I don't get any more $ for you buying them, though. Another great read is the Python Cookbook. You can find the recipes online, also, but sometimes it's good to just sit with a hard copy of the book. The cookbook will walk you through tons of code examples, which is really helpful when you're learning a new language (or any language for the first time). HTH, - jmj > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am looking for a book on Beginners who never programmed before or have no experience in programming
On Tue, Sep 28, 2010 at 7:47 AM, Steven D'Aprano wrote: > On Tue, 28 Sep 2010 07:37:12 pm Jeremy Jones wrote: > >> "Head First Programming: A Learner's Guide to Programming Using the >> Python Language" by David Griffiths and Paul Barry was a great read. >> It's unconventional (by their nature, all Head First books are), > > > I've never heard of "Head First Programming" -- how are they > unconventional? > Both HF Programming (currently out) and HF Python (currently being written) are very example-driven and in the course of each example, introduce a number of concepts. So, rather than having a chapter on lists, the books introduce lists as a part of an evolving example. The "conventional" approach (in my view) is splitting a book into chapters of such topics as variables, operators, lists, dicts, OO, etc. So, I hope that I conveyed the "unconventional" as a good thing, because I definitely see it as such. I love these books. Honestly, it's enjoyable to me just to see the craft these folks use to evolve a problem and weave in new concepts all along the way. > > > -- > Steven D'Aprano > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Negative IF conditions
Mark Brown wrote: Hi, I'm a newbie and was wondering which of these IF conditions is better structure: if not os.path.exists('filename'): if os.path.exists('filename') == False: My preference would be the first (if not os.path.exists). os.path.exists returns a boolean (I think), so the "if" will directly check that rather than having to setup a second truth statement (== False) for "if" to check. It's more readable to me and just "feels better." They both work so if one preferred over the other? Thanks Mark Brown ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Jeremy Jones ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with refactoring needed -- which approach is morePythonic?
Brian van den Broek wrote: Alan Gauld said unto the world upon 2005-02-10 02:58: Pseudo code: class Body: def __init__(self,content): self.contents = contents self.nodes = [] def parse(self): for line in self.contents: if line == NodeStartTag: node = Node() if line == NodeEndTag: self.nodes.append(node) node.append(line) class Node: def __init__(self,lines=[]): self.lines = lines def append(self,item): self.lines.append(item) def parse(self): # your parsing method here. Hi all, YAQ (Yet Another Question): Following the general pattern, I end up with a Body object which has an attribute .nodes that consists of a list of Node objects. So, something like: My Example Body Node List Node the first Node the second Is there any way to make methods of the Node class access attributes of `parents' of instances? I would like a Node instance such as Node the first above to be aware just what it is a node of and what its siblings are. Does this make sense? I think so. I haven't tested this (pseudo) code which I took from your above post and just modified it, but I think you want something like this: Pseudo code: class Body: def __init__(self,content): self.contents = contents self.nodes = [] def parse(self): for line in self.contents: if line == NodeStartTag: node = Node(self) #when you create a node, pass in the parent object like this if line == NodeEndTag: self.nodes.append(node) node.append(line) class Node: def __init__(self, parent, lines=[]): self.lines = lines self.parent = parent #and store the parent like this def append(self,item): self.lines.append(item) def parse(self): # your parsing method here. def show_siblings(self): print self.parent.nodes # and you can access the parent kinda like this. You don't want to get too carried away with something like this, though. You may want to read up on the Law of Demeter. This (in my opinion) is fine, though. Best to all, Brian vdB PS Thanks for the reply to my venting question, Kent. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Jeremy Jones ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What does this mean
jrlen balane wrote: what does (*args, **kwargs) mean??? i'm sort of a bit confused... thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor *args is notation for list of arguments. **kwargs is notation for keyword arguments. Here is an example: ### In [1]: def foo(bar, *args, **kwargs): ...: print "bar", bar ...: print "args", args ...: print "kwargs", kwargs ...: In [2]: foo('1') bar 1 args () kwargs {} In [3]: foo('1', 1, 2, 3) bar 1 args (1, 2, 3) kwargs {} In [4]: foo('1', 1, 2, 3, bam=1, baz=2, boo=3) bar 1 args (1, 2, 3) kwargs {'bam': 1, 'baz': 2, 'boo': 3} ### In the definition (at prompt [1]), I only stated 3 arguments to pass in: bar, args, and kwargs. At prompt [2], I pass in a "1" string as my only argument, which gets assigned to "bar". At prompt [3], I pass in "1", 1, 2, 3. "1" gets assigned to foo as in the previous example. (1,2,3) gets assigned to args. The *args notations says, "Assign any following arguments to the args variable." At prompt [4], I pass in the same thing as at [3], but pass in keyword arguments (bam=1, baz=2, boo=3). Everything gets assigned as it did at [3] except kwargs is a dictionary with keys 'bam', 'baz', and 'boo' with respective values 1,2,3. The **kwargs notations says, "Assign any subsequent keyword arguments to kwargs." NOTE - you don't have to use *args and **kwargs. You just have to use the * and **. Jeremy Jones ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] count words
Ron Nixon wrote: I know that you can do this to get a count of home many times a word appears in a file f = open('text.txt').read() print f.count('word') Other than using a several print statments to look for seperate words like this, is there a way to do it so that I get a individual count of each word: word1 xxx word2 xxx words xxx etc. Like this? A 14 AND 1 Abantes 3 Abarbarea 1 Abas 1 Abians 1 Ablerus 1 About 2 Abydos 3 Acamas 11 Accept 2 Acessamenus 1 Achaea 1 Achaean 34 Achaeans 540 Achelous 2 Achilles 423 Acrisius 1 Actaea 1 Actor 8 Adamas 5 Admetus 4 Adrastus 2 Adresteia 1 Adrestus 8 Aeacus 20 Aegae 2 Aegaeon 1 Aegeus 1 Aegialeia 1 Aegialus 1 Aegilips 1 Aegina 1 Aegium 1 Aeneas 86 Aenus 1 Aeolus 1 Aepea 2 Aepytus 1 Aesculapius 7 Aesepus 2 Aesopus 4 Aesyetes 2 Aesyme 1 Aesymnus 1 ... wronged 2 wronging 1 wrongs 1 wroth 1 wrought 24 wrung 1 yard 3 yarded 1 yards 2 yawned 1 ye 3 yea 1 year 13 yearling 2 yearned 4 yearning 2 years 15 yellow 5 yesterday 5 yet 160 yield 10 yielded 3 yielding 3 yieldit 1 yoke 24 yoked 11 yokes 1 yokestraps 1 yolking 1 yonder 3 you 1712 young 44 younger 9 youngest 6 your 592 yourelf 1 yours 7 yourself 60 yourselves 17 youselves 1 youth 17 youths 18 zeal 2 I ran the following script on "The Iliad": #!/usr/bin/env python import string text = open('iliad.txt', 'r').read() for punct in string.punctuation: text = text.replace(punct, ' ') words = text.split() word_dict = {} for word in words: word_dict[word] = word_dict.get(word, 0) + 1 word_list = word_dict.keys() word_list.sort() for word in word_list: print "%-25s%d" % (word, word_dict[word]) Jeremy Jones ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Queued threads
Not to beat a dead horse, but Liam Clarke wrote: Oops, you probably want to do this then- for i in range( 0, 3 ): oThread = Thread( target=mainFunction ).start() Thread.start() looks like it returns None. # In [23]: from threading import Thread In [24]: import time In [25]: def foo(): : print "doing..." : time.sleep(15) : print "done" : In [26]: t = Thread(target=foo).start() doing... In [27]: print t None In [28]: done In [28]: print t None In [29]: t = Thread(target=foo) In [30]: t.start() doing... In [31]: t.isAlive() Out[31]: True In [32]: done In [32]: t.isAlive() Out[32]: False # So, checking the return of Thread.start() doesn't seem like it would do what you think it would do. You probably want to get the thread object and check that directly with isAlive(). Oh, and if you spawn a bunch of threads at once and you want to wait until the all complete before doing something else, do something like this: # #create a list to contain the threads thread_list = [] for i in range(10): t = Thread(target=foo) print "creating thread", t #put each thread in the list thread_list.append(t) #iterate over thread list and start each thread for t in thread_list: print "starting thread", t t.start() #iterate over thread list and wait for each thread for t in thread_list: print "waiting for thread", t while 1: if not t.isAlive(): break time.sleep(.2) # It'll give you output something like this: # creating thread creating thread creating thread creating thread creating thread creating thread creating thread creating thread creating thread creating thread starting thread starting thread starting thread starting thread doing... starting thread doing... doing... starting thread doing... starting thread doing... doing... starting thread starting thread starting thread doing... doing... doing... waiting for thread doing... done done done done done done waiting for thread waiting for thread waiting for thread done done done done waiting for thread waiting for thread waiting for thread waiting for thread waiting for thread waiting for thread # But in this situation, I totally agree with Max. You don't need threads for this. Just use os.system. You could use one of the popens (or the new subprocess module - never used that one myself), but os.system blocks until the called program exits. while oThread: print 'sleeping 3 seconds' time.sleep( 3 ) A if generally has an implicit else: pass clause as I think of it, so it will just keep reiterating if the condition isn't met. Jeremy Joens ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem with variables
. Sm0kin'_Bull wrote: wrote this, It's a bit lame though I = "Allen" me = "Allen" my = "Allen's" print \ """ %s woke up early in the morning. But, it was unusal by %s. %s pillow was with %s. %s didn't want to wake up But, %s tried my best and woke up. it was so amazing!""" % (I,me,my,me,I,I) raw_input("\n\\t\t\t- The End -") But it looks like this... Allen woke up early in the morning. But, it was unusal by Allen. Allen's pillow was with Allen. Allen didn't want to wake up But, Allen tried my best and woke up. it was so amazing - The End - the problem is about lining I want it to print like this... Allen woke up early in the morning. But, it was unusal by Allen. Allen's pillow was with Allen. Allen didn't want to wake up But, Allen tried my best and woke up. it was so amazing This is what I got: In [45]: I = "Allen" In [46]: me = "Allen" In [47]: my = "Allen's" In [48]: In [48]: print \ : """ : %s woke up early in the morning. But, it was unusal by %s. %s pillow : was with %s. %s didn't want to wake up But, %s tried my best and woke up. : it was so amazing!""" % (I,me,my,me,I,I) Allen woke up early in the morning. But, it was unusal by Allen. Allen's pillow was with Allen. Allen didn't want to wake up But, Allen tried my best and woke up. it was so amazing! It looks like it should. If you want it to show up exactly like posted at the end, you need something more like this: In [50]: print \ : """ : %s woke up early in the morning. But, it was unusal by %s. : %s pillow was with %s. %s didn't want to wake up But, %s : tried my best and woke up. it was so amazing!""" % (I,me,my,me,I,I) Allen woke up early in the morning. But, it was unusal by Allen. Allen's pillow was with Allen. Allen didn't want to wake up But, Allen tried my best and woke up. it was so amazing! ### Jeremy Jones FREE pop-up blocking with the new MSN Toolbar MSN Toolbar Get it now! ___ 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] Interesting problem
Smith, Jeff wrote: >Consider a class with a lt of properties. I would like a member >function which generates a dictionary where the keys are the property >names and the values are the property values? > >Is this clear? > >How might I go about this? > >Jeff >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Like .__dict__? Given this class: 1 class Foo: 2 def __init__(self, **kw): 3 self.__dict__.update(kw) 4 And creating an instance of it like this: In [17]: foo = Foo(**{"bar":"b", "foo":"f", "bam":"bm"}) And accessing the properties of it like this: In [18]: foo.foo Out[18]: 'f' In [19]: foo.bar Out[19]: 'b' In [20]: foo.bam Out[20]: 'bm' You can get all properties of it like this: In [21]: foo.__dict__ Out[21]: {'bam': 'bm', 'bar': 'b', 'foo': 'f'} Is this what you're looking for? JJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Please Help: Hacking
Suranga Sarukkali wrote: > Python's Great fro Hacking Right? Please tell me more like when you > tell it to a College Student (That tell's What I'm) and since I > Sort of new to Programming in Python it's going be easy if done so. > Please reply to me at your earliest convenience and by the way General > Hacking Education will be Cool since the only hacking I've done is on > a Hacking Simulation Game I downloaded yeas ago from > http://g1.acid-play.com/download/a599b964/Hackerv1.zip witch got all > the tools like Password Crackers Inbuilt to Simulate Hacking Large > Company's and making cash from it. You can privately email me on > [EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> > > I've been on the Mailing List some time and It's Great! Thanks for the > People Developed it. > >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > Here you go. This should be enlightening: http://www.catb.org/~esr/writings/unix-koans/script-kiddie.html Jeremy Jones ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] i know this should be easy
nephish wrote: >Hey there, >i have a simple question. >if something returns a true or false, how do i test for it with an if >statement? >i am using pygtk with a toggle button that returns TRUE if checked and >FALSE if not. >do i need quotes around the TRUE / FALSE or should i use 1 and 0? > >Auto_Tog = self.AutoCheckRaw.get_active() >if str(AddDay_Tog)== '1': >do so and so. > > If ``get_active()`` returns True or False (or 1 or 0), you could just do:: if self.AutoCheckRaw.get_active(): do_your_True_stuff_here() else: do_your_False_stuff_here() >is this right? > >thanks >___ >Tutor maillist - Tutor@python.org >http://mail.python.org/mailman/listinfo/tutor > > > Jeremy ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor