Re: [Tutor] Musical note on python
winsound.Beep() takes only integral values for frequency. Therefore you cannot use it if you want either just intonation or equal temperment scales exactly. -- regards, Sarma. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] All possible 16 character alphanumeric strings?
On Sat, Sep 15, 2012 at 6:50 PM, Scurvy Scott wrote: > > I'm trying to generate a list of every possible 16 character string > containing only 2-7 and a-z lowercase. This type of problem is just nested iteration: >>> seq = '01' >>> r = [] >>> for g0 in seq: ... for g1 in seq: ... for g2 in seq: ... r.append( ''.join([g0, g1, g2])) ... >>> r ['000', '001', '010', '011', '100', '101', '110', '111'] You can generalize this with a recursive generator, or use itertools.product(seq, repeat=N): def seq_prod(seq, n): if n > 1: for left in seq: for right in seq_prod(seq, n - 1): yield (left,) + right elif n == 1: for item in seq: yield (item,) >>> [''.join(s) for s in seq_prod('01', 3)] ['000', '001', '010', '011', '100', '101', '110', '111'] >>> [''.join(s) for s in itertools.product('01', repeat=3)] ['000', '001', '010', '011', '100', '101', '110', '111'] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to run this block of code dozens of times
Hello all, I'm just wondering how to run this block of code X amount of times (a lot) and then store the ouput to a .txt file. The code I've written is below. from Crypto.PublicKey import RSA import hashlib m = RSA.generate(1024) b = hashlib.sha1() b.update(str(m)) a = b.hexdigest() print a[:16] + '.onion' Thanks in advance ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] find('') returns 0
This behavior seems strange to me: the find method of a string returns the position zero when you search for an empty string (granted, I can't quite figure out why you'd search for an empty string, either). >>> 'abc'.find('') 0 Anyone care to share a good explantion for this behavior and possible use cases? Thanks! Take care, Don ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
scratch that, new code is below for your perusal: from Crypto.PublicKey import RSA import hashlib def repeat_a_lot(): count = 0 while count < 20: m = RSA.generate(1024) b = hashlib.sha1() b.update(str(m)) a = b.hexdigest() print a[:16] + '.onion' count += 1 repeat_a_lot() Thanks again, Scott ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
On 09/16/2012 07:56 PM, Scurvy Scott wrote: > scratch that, new code is below for your perusal: > > from Crypto.PublicKey import RSA > import hashlib > > def repeat_a_lot(): > count = 0 > while count < 20: You're kidding, aren't you? while loops are meant for those times when you don't know how many times the loop is to iterate. > m = RSA.generate(1024) > b = hashlib.sha1() > b.update(str(m)) > a = b.hexdigest() > print a[:16] + '.onion' > count += 1 > repeat_a_lot() > > > def repeat_a_lot(): for _ in xrange(20): m = RSA.generate(1024) b = hashlib.sha1() b.update(str(m)) a = b.hexdigest() print a[:16] + '.onion' repeat_a_lot() -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] find('') returns 0
On Sun, Sep 16, 2012 at 7:51 PM, Don Jennings wrote: > This behavior seems strange to me: the find method of a string returns the > position zero when you search for an empty string (granted, I can't quite > figure out why you'd search for an empty string, either). > 'abc'.find('') > 0 > > Anyone care to share a good explantion for this behavior and possible use > cases? Thanks! It actually returns the value of "start": >>> 'abc'.find('', 0) 0 >>> 'abc'.find('', 1) 1 >>> 'abc'.find('', 2) 2 It's looking for the length 0 substring ''. So it will match a 0 length slice at the given start position: >>> 'abc'[0:0] '' >>> 'abc'[1:1] '' >>> 'abc'[2:2] '' When you find 'b', for example, it searches for a length 1 slice: >>> 'abc'.find('b') 1 >>> 'abc'[1:2] 'b' The 'in' operator also searches for a substring: >>> '' in 'abc' True ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
On Sun, Sep 16, 2012 at 5:23 PM, Dave Angel wrote: > On 09/16/2012 07:56 PM, Scurvy Scott wrote: > > scratch that, new code is below for your perusal: > > > > from Crypto.PublicKey import RSA > > import hashlib > > > > def repeat_a_lot(): > > count = 0 > > while count < 20: > > >You're kidding, aren't you? while loops are meant for those times when > >you don't know how many times the loop is to iterate. > > Acutally, Dave, I would be the one setting just how many times the loop would run manually. Actually the loop would run 2^80 times, so it was more of a test to see if the output was different each time it ran more than trying to run it the correct amount of times. > > m = RSA.generate(1024) > > b = hashlib.sha1() > > b.update(str(m)) > > a = b.hexdigest() > > print a[:16] + '.onion' > > count += 1 > > repeat_a_lot() > > > > > > > > def repeat_a_lot(): > for _ in xrange(20): > m = RSA.generate(1024) > b = hashlib.sha1() > b.update(str(m)) > a = b.hexdigest() > print a[:16] + '.onion' > > repeat_a_lot() > > Why would you use an underscore rather than a letter or name like I've always seen. I've never seen an underscore used before. Scott > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] find('') returns 0
On Sun, Sep 16, 2012 at 8:33 PM, eryksun wrote: > > The 'in' operator also searches for a substring: > > >>> '' in 'abc' > True I forgot to mention, this use of slices is a peculiarity to strings. In contrast, list.index and list.__contains__ ("in") match against individual items. An empty list slice [] is not "in" ['a', 'b', 'c']. Be careful about this use of substrings: >>> 'care' in 'careful programming' True >>> 'care' in 'careful programming'.split() False ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
On 17/09/2012 01:56, Scurvy Scott wrote: Why would you use an underscore rather than a letter or name like I've always seen. I've never seen an underscore used before. Try reading some of the documentation here http://www.python.org/doc/ It's amazing what you can learn. -- Cheers. Mark Lawrence. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] reducing a list evenly when deleting elements by index
Hi, I have a bezier line with 20 points on it and I am trying to reduce this to a line with 4 points, keeping the first and last points and 2 evenly spaced points in between like so: ** becomes: *. . . .* These points are removable only by index, so if I remove point 2, point 3 then becomes point 2 in the next iteration, so I end up having a spacing like this which is not what I want: *. . . .* Here is the code that I have so far. I hope it makes sense: ## for aNode in nuke.allNodes(): if aNode.shown(): theRotoNode = aNode curveKnob = theRotoNode['curves'] rootLayer = curveKnob.rootLayer theNumberOfPointToReduceTo = 5 for aShape in rootLayer: theOriginalShape = aShape thePointList = [] for aPoint in aShape: thePointList.append(aPoint) if len(thePointList) > 5: theNumberOfPoints = len(thePointList) keepEvery = float(theNumberOfPoints)/theNumberOfPointToReduceTo theIndicesToKeep = [] theIndicesToKeep.append(0) theIndicesToKeep.append(1) for i in range(theNumberOfPoints)[1:-1]: if i*keepEvery < theNumberOfPoints: theIndicesToKeep.append(int(round(i*keepEvery))) theIndicesToKeep.append(theNumberOfPoints-2) theIndicesToKeep.append(theNumberOfPoints-1) thePointsToRemove = tuple(set(range(theNumberOfPoints)) - set(theIndicesToKeep)) #everything is good up to here, the code below doesn't work properly and I'm kinda stuck counter = -1 for aPointIndex in thePointsToRemove: counter+=1 aPointIndex = aPointIndex-(counter) print aPointIndex aShape.remove(aPointIndex) Any advice would be greatly appreciated. Pete ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
On 17 September 2012 02:15, Pete O'Connell wrote: > Hi, I have a bezier line with 20 points on it and I am trying to reduce > this to a line with 4 points, keeping the first and last points and 2 > evenly spaced points in between like so: > ** > becomes: > *. . . .* > > These points are removable only by index, so if I remove point 2, > point 3 then becomes point 2 in the next iteration, so I end up having a > spacing like this which is not what I want: > *. . . .* But removing item 19 doesn't affect the position of item 2. The trick is to start the loop at the end of the list and work back to the beginning. Oscar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
Ah of course. Thanks very much Oscar! Pete On Mon, Sep 17, 2012 at 1:29 PM, Oscar Benjamin wrote: > On 17 September 2012 02:15, Pete O'Connell wrote: > >> Hi, I have a bezier line with 20 points on it and I am trying to reduce >> this to a line with 4 points, keeping the first and last points and 2 >> evenly spaced points in between like so: >> ** >> becomes: >> *. . . .* >> >> These points are removable only by index, so if I remove point 2, >> point 3 then becomes point 2 in the next iteration, so I end up having a >> spacing like this which is not what I want: >> *. . . .* > > > But removing item 19 doesn't affect the position of item 2. The trick is > to start the loop at the end of the list and work back to the beginning. > > Oscar > -- - ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
On 09/16/2012 08:56 PM, Scurvy Scott wrote: > On Sun, Sep 16, 2012 at 5:23 PM, Dave Angel wrote: > >> On 09/16/2012 07:56 PM, Scurvy Scott wrote: >>> scratch that, new code is below for your perusal: >>> >>> from Crypto.PublicKey import RSA >>> import hashlib >>> >>> def repeat_a_lot(): >>> count = 0 >>> while count < 20: >>> You're kidding, aren't you? while loops are meant for those times when >>> you don't know how many times the loop is to iterate. >> > Acutally, Dave, I would be the one setting just how many times the loop > would run manually. Actually the loop would run 2^80 times, so it was more > of a test to see if the output was different each time it ran more than > trying to run it the correct amount of times. > Since you know the count before you start the loop, then use xrange(), rather than while. If you had to test some condition other than a simple count, then you might want to use while. As the loop was coded, you have three uses of a variable that doesn't matter to anyone reading the code. one to initialize it, one to test it, and one to increment it. >>> m = RSA.generate(1024) >>> b = hashlib.sha1() >>> b.update(str(m)) >>> a = b.hexdigest() >>> print a[:16] + '.onion' >>> count += 1 >>> repeat_a_lot() >>> >>> >>> >> def repeat_a_lot(): >> for _ in xrange(20): >> m = RSA.generate(1024) >> b = hashlib.sha1() >> b.update(str(m)) >> a = b.hexdigest() >> print a[:16] + '.onion' >> >> repeat_a_lot() >> >> > Why would you use an underscore rather than a letter or name like I've > always seen. I've never seen an underscore used before Standard convention for an unused loop variable. But if you prefer, no harm in naming it something like for unused_counter_here in xrange(20): The unadorned underscore leads the reader to expect that you don't use the loop variable anywhere, like for indexing some matrix. -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] simple random string generator
hey im new to the tutor thing so please correct me if im doing something wrong. Anyway what i am trying to do is make a simple string (word) generator and get it to make strings (or words) starting at 10 chars up to 100 in steps of 5 using a make word function. Now my problem is how do i get it to run the mkword function and manipulate the n value to fit those requirements? the main goal of the assignment is to get it to generate the strings (words) and compare it to itself so we can see the time it takes to compare using different methods. http://pastie.org/4735416 the problem lies in the "main()" function i know that. there is my code...in no way am i asking anyone to do it for me! i really want to learn but i feel like my professor is kinda doing a questionable job teaching it...this will be my second semester of python and i have just started dealing with classes and multiple functions about a week ago and also trying to switch from 2.7 to 3.2. also another question and hopefully someone has some suggestions for me...i am just having problems learning python...like in understand it, i can read it, and know what is going on and what needs to be done...but when it comes to writing it i just dont know how to do it... does anyone else have this prob? and is there anything i can do to start really soaking it in? thanks in advance guys :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
On 17/09/12 10:56, Scurvy Scott wrote: Why would you use an underscore rather than a letter or name like I've always seen. I've never seen an underscore used before. An underscore on its own is often used to mean "don't care". Like a scratch variable to hold a result when you don't actually need the result. So in a for-loop: for _ in range(20): ... the idea is to tell the reader we don't actually care about the indexes 0, 1, 2, ... but only care that the loop happens 20 times. Other uses are: * a single leading underscore usually means "private, don't touch" E.g. if you see a function called _func then you shouldn't use it in your own code, because the author is stating that the function is for private use only and it might go away or change in the next version. * double leading and trailing underscore names have special meaning to Python, e.g.: __name__ __class__ __len__ __add__ and many others -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] reducing a list evenly when deleting elements by index
On 17/09/12 11:15, Pete O'Connell wrote: Hi, I have a bezier line with 20 points on it and I am trying to reduce this to a line with 4 points, keeping the first and last points and 2 evenly spaced points in between like so: In general in Python, it is faster and much easier to create a new list rather than to mess about deleting items from a list. So instead of calling del 16 times, which has to move list items around, it will be faster and simpler to create a new list: new = [old[0], old[6], old[13], old[19]] then just use the new list. If you really need to modify the old list in place (perhaps because other parts of the code need to see the same change) then you can do a slice-assignment: old[:] = new # note the [:] slice -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple random string generator
On 17/09/12 12:45, Matthew Dalrymple wrote: Anyway what i am trying to do is make a simple string (word) generator and get it to make strings (or words) starting at 10 chars up to 100 in steps of 5 using a make word function. Now my problem is how do i get it to run the mkword function and manipulate the n value to fit those requirements? Use a for-loop. for n in range(10, 101, 5): print(n) The print is just as a demonstration. In your code, you would instead call your mkword function. [...] i am just having problems learning python...like in understand it, i can read it, and know what is going on and what needs to be done...but when it comes to writing it i just dont know how to do it... Hang in there mate, practice makes perfect. Writing code is a combination of creativity and careful, precise, logical reasoning. Some people find it easy, some people don't. When you have a task, try breaking it up into simple steps, like you were giving instructions to your two-year-old cousin. (In reality, computers are *much* dumber than a two-year old, but that's a good start.) does anyone else have this prob? I'm sure most people have had this problem for at least a little while. and is there anything i can do to start really soaking it in? Write code. Lots of code. It doesn't matter if it is junk code, or toy code, just write lots of code, until it becomes second nature. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
Wow, thanks Dave, et al., for explaining things the way they did. I'm not trying to and apologize for top posting, gmail wasn't giving me the option of replying to all. I definitely understand what was going on and why when you all were explaining the code portions to me. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run this block of code dozens of times
Scurvy Scott wrote: > Actually the loop would run 2^80 times Remember the previous thread? This means the loop will not terminate in the next few million years. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor