[Tutor] creating a sound file from a string
hello, i have a string that is generated that contains letters and numbers. i also have a folder which contains the following: sounds/ upper-a.wav upper-b.wav .. lower-a.wav lower-b.wav ... sign-£.wav sign-|.wav .. number-0.wav number-1.wav .. does anyone know of a python module which would create a single sound file based on the randomly generated string? i have looked at the python wave module, but if i am not wrong this works with one file at the time. any advise much appreciated. many thanks norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] MySqldb problem
hi all, i am a newbie to python programming. i just know basics of the language. i came across MySqldb. i able to manipulate database with MySqldb but i wasn't able to add files in database through the lib MySqldb. any help will be welcomed gracefully. thanks. MANOJ SHEOKAND (+919728523528) "some day there won't be a song in ur heart, sing anyway"___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Mon, Jan 19, 2009 at 9:47 PM, Emad Nawfal (عماد نوفل) wrote: > Thanks John for this. Although the decorate-sort-undecorate idiom looks so > natural to me now, I don't think I would have found it on my own. I have > that deja vu effect towards it. decorate-sort-undecorate is pretty much obsolete since the key= parameter was added to sort() in Python 2.4. Since Python 2.5 you can also use key= with min() and max() so your problem can be solved very simply and tersely: In [1]: words = "man woman children he".split() In [2]: min(words, key=len) Out[2]: 'he' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about pygame/tkinter interaction
On Mon, 19 Jan 2009 15:46:27 -0800, Steve Willoughby wrote: > On Mon, Jan 19, 2009 at 05:30:01PM -0500, Kent Johnson wrote: >> My guess is that pygame and Tkinter are both going to want to control >> the event loop. Googling 'pygame tkinter' gives both hints that it >> might be possible and hints of trouble... > > Yeah, I was thinking that, but since what I saw up until the point I > asked the list was that for pygame you seem to write your own event > loop, like > > while True: > handle_event_myself(pygame.event.get()) > > whereas Tkinter wants you to just hand it control and let your program > flow disappear into that black box entirely, until it pokes callbacks at > you, I thought... "Well, maybe..." I guess it shouldn't be too difficult if what you're intending is to have one window controlled by Tkinter and the other by pygame, it's just a matter of having Tkinter calling pygame's event handler once in a while (so instead of while True: handle_pygame_event() insert an event that makes Tkinter calls handle_pygame_event() every once in a while. ) What I don't know is whether it is possible to "embed" a pygame- controlled frame into Tkinter-controlled window. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
On Tue, Jan 20, 2009 at 4:49 AM, Norman Khine wrote: > does anyone know of a python module which would create a single sound file > based on the randomly generated string? > > i have looked at the python wave module, but if i am not wrong this works > with one file at the time. I think you can do this with the wave module. The code would be something like this: open output file and set number of channels, etc for each letter of input: open wave file for the letter read frames from letter file write frames to output file close output file Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
2009/1/20 Kent Johnson > On Mon, Jan 19, 2009 at 9:47 PM, Emad Nawfal (عماد نوفل) > wrote: > > > Thanks John for this. Although the decorate-sort-undecorate idiom looks > so > > natural to me now, I don't think I would have found it on my own. I have > > that deja vu effect towards it. > > decorate-sort-undecorate is pretty much obsolete since the key= > parameter was added to sort() in Python 2.4. Since Python 2.5 you can > also use key= with min() and max() so your problem can be solved very > simply and tersely: > > In [1]: words = "man woman children he".split() > > In [2]: min(words, key=len) > Out[2]: 'he' > > Kent > Thank you all for the beautiful solutions. When you say that something is obsolete, what does this mean? Is that just because there is a simpler way, or are there other technical considerations? -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
Thanks I am looking at this now. From unix I could run sox so that $ sox uppercase-a.wav uppercase-b.wav ab.wav this produces a merged uppercase-a.wav uppercase-b.wav perhaps this is a better way, but requires having to execute an external programme. any thoughts? cheers norman Kent Johnson wrote: On Tue, Jan 20, 2009 at 4:49 AM, Norman Khine wrote: does anyone know of a python module which would create a single sound file based on the randomly generated string? i have looked at the python wave module, but if i am not wrong this works with one file at the time. I think you can do this with the wave module. The code would be something like this: open output file and set number of channels, etc for each letter of input: open wave file for the letter read frames from letter file write frames to output file close output file Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MySqldb problem
On 20 Jan 2009 11:10:14 - "Manoj kumar" wrote: > i am a newbie to python programming. i just know basics of the > language. i came across MySqldb. i able to manipulate database with > MySqldb but i wasn't able to add files in database through the lib > MySqldb. Right. Whatever you want to do in the database, you'll have to have a user defined in MySQL with the necessary permissions and have that user configured in your program. Details: Generally, you want to follow the principle of least permissions for security in your app. Almost all database apps need select privileges for their database user. A good many need insert, update and/or delete privileges. Dynamically generated tables aren't sound relational design, so your tables should be created using an administrative user for the database rather than giving the MySQL user defined for your app unnecessary privileges. As long as you're in development and no untrusted users have access to the app, do what you want. Before setting it wild or running it as server, whatever it does, replace the user that you've defined as having "ALL" privilege on the database with one that has only those privileges needed for what the app does. Alternative: Also, MySqldb is the manual way to use a MySQL database. It's fine if you want to generate query strings manually because you know what you're doing and you're sure that your code will never be used with a different database. The problem for a new user is that using MySqldb means learning Python and SQL at the same time to build your app. You aren't learning Python because you're a mental masochist. You're learning Python because it's an easy way to get programs working. A more Pythonic way to work with databases would be to use an Object Relational Mapper like SQLAlchemy. The simple explanation is that the ORM lets you just work with objects in your app instead of generating query strings. This approach also gets you a lot of other features for free like independence from being database specific and protection from SQL injection attacks. Chris -- Thank you everyone! USAK is live on its new connection. So far you have given $198 towards next quarter. To make a donation, use this link (opens on PayPal): http://tinyurl.com/USAK2009 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
Is this a safe way to generate random sound files on a web server? >>> from os import popen >>> merge = popen('sox uppercase-b.wav uppercase-a.wav merge.wav') Thanks Norman Kent Johnson wrote: On Tue, Jan 20, 2009 at 4:49 AM, Norman Khine wrote: does anyone know of a python module which would create a single sound file based on the randomly generated string? i have looked at the python wave module, but if i am not wrong this works with one file at the time. I think you can do this with the wave module. The code would be something like this: open output file and set number of channels, etc for each letter of input: open wave file for the letter read frames from letter file write frames to output file close output file Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, Jan 20, 2009 at 7:13 AM, Emad Nawfal (عماد نوفل) wrote: > 2009/1/20 Kent Johnson >> decorate-sort-undecorate is pretty much obsolete since the key= >> parameter was added to sort() in Python 2.4. > When you say that something is obsolete, what does this mean? Is that just > because there is a simpler way, or are there other technical considerations? In this case, it is because there is a better, simpler way. Decorate-sort-undecorate used to be the recommended way to sort on a key; now key= is preferred. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
On Tue, Jan 20, 2009 at 8:12 AM, Norman Khine wrote: > Is this a safe way to generate random sound files on a web server? > from os import popen merge = popen('sox uppercase-b.wav uppercase-a.wav merge.wav') I'm not sure but you may have to read from merge to allow the process to complete. You might want to use os.system() or one of the subprocess replacements: http://docs.python.org/library/subprocess.html#subprocess-replacements if your webserver allows multiple simultaneous connections, and you are going to serve merge.wav to a client, make sure you use different names for each connection so they don't collide. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Mon, 19 Jan 2009 19:13:32 -0800, Marc Tompkins wrote: > 2009/1/19 John Fouhy > >> 2009/1/20 Emad Nawfal (عماد نوفل) : Of course, >> this is not necessarily the best answer for your particular problem. >> The problem with sorting is that you have to look at some elements more >> than once. For short lists, it's not a problem, but it can slow you >> down on bigger lists. You could also find the shortest element by >> going through the list, remembering the shortest element you've seen so >> far. This will be quicker if you only want to find the single >> shortest. >> >> > Here's the first thing that came to mind: > Using sys.maxint to prime minLen is overkill, of course - > "antidisestablishmentarianism" is only 28 letters long, after all - but > it should be larger than any word you can expect to see. This doesn't > catch ties, though... could do that like so: Other than overkill, it is wrong. If the shortest "word" is longer than maxint it'd give wrong result. You could just simply use the len of the first word. The best solution though, have been answered by Kent Johnson. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
Thanks, Kent Johnson wrote: On Tue, Jan 20, 2009 at 8:12 AM, Norman Khine wrote: Is this a safe way to generate random sound files on a web server? from os import popen merge = popen('sox uppercase-b.wav uppercase-a.wav merge.wav') I'm not sure but you may have to read from merge to allow the process to complete. You might want to use os.system() or one of the subprocess replacements: http://docs.python.org/library/subprocess.html#subprocess-replacements How do I check if the process has completed? Would I have to explicitly close the file? if your webserver allows multiple simultaneous connections, and you are going to serve merge.wav to a client, make sure you use different names for each connection so they don't collide. Each merge.wav file will be unique and only for the specific session, after which it will be removed from the server. Kent What I am trying to do is a sound captcha, just to learn in putting the pieces together. Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a sound file from a string
On Tue, Jan 20, 2009 at 9:00 AM, Norman Khine wrote: >> from os import popen >> merge = popen('sox uppercase-b.wav uppercase-a.wav merge.wav') >> >> I'm not sure but you may have to read from merge to allow the process >> to complete. You might want to use os.system() or one of the >> subprocess replacements: >> http://docs.python.org/library/subprocess.html#subprocess-replacements > > How do I check if the process has completed? > > Would I have to explicitly close the file? I suggest something like subprocess.call(['sox', 'uppercase-b.wav', 'uppercase-a.wav', 'merge.wav']) which will wait for the process to complete. sox is responsible for closing the file. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ip address
On Mon, 19 Jan 2009 19:47:54 -0800, wormwood_3 wrote: > Hello, > > This is definitely possible. It's more a matter of system and OS > configuration than Python though, so you might want to check out some > Linux forums ( http://www.linuxforums.org/ ) for additional help. In > short, I think the simplest would be: Have 3 separate network interfaces > in your physical box, have your router provide one of the three > addresses to each. Then you could configure the client to only connect > through its associated interface/IP. > > If this sort of setup wouldn't work, please tell us more about your > configuration between your client and your network. Definitely not what he expected, I'm sure. It is impossible for a single network interface to have more than 1 IP address. I want to ask what is he going to do with such set up though. It is trivial to emulate IP addressess through ports, so use that instead. Also inter-process communication in a single machine is usually better done using pipes. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Finding the shortest word in a list of words
"Finding the shortest word among a list of words" sounds like something of a trick question to me. I think a more complete problem statement would be "Find the list of words that are the shortest", since there is no guarantee that the list does not contain two words of the same shortest length. If you just add "me" to your sample set, you now get misleading answers: words = "man woman children he me".split() print min(words, key=len) prints: he What happened to "me"? It is just as short as "he"! To get *all* the words that are the shortest length, I'll show two approaches. The first uses a defaultdict, from the collections module of the Python stdlib. With a defaultdict, I can have new dict entries get initialized with a default value using a specified factory function, without first having to check to see if that entry exists. I would like to create a dict of lists of words, keyed by the lengths of the words. Something that would give me this dict: { 2 : ['he', 'me'], 3 : ['man'], ... etc. } from which I could then find the minimum length using min(wordlendict.keys()). By using a defaultdict, I can just build things up by iterating over the list and adding each word to the entry for the current word's length - if the current word is the first one for this length to be found, the defaultdict will initialize the value to an empty list for me. This allows me to safely append the current word regardless of whether there was an existing entry or not. Here is the code that does this: from collections import defaultdict wordlendict = defaultdict(list) for w in words: wordlendict[len(w)].append(w) minlen = min(wordlendict.keys()) minlist = wordlendict[minlen] print minlist prints: ['he', 'me'] Now we are getting a more complete answer! A second approach uses the groupby method of the itertools module in the stdlib. groupby usually takes me several attempts to get everything right: the input must be sorted by the grouping feature, and then the results returned by groupby are in the form of an iterator that returns key-iterator tuples. I need to go through some mental gymnastics to unwind the data to get the group I'm really interested in. Here is a step-by-step code to use groupby: from itertools import groupby grpsbylen = groupby(sorted(words,key=len),len) mingrp = grpsbylen.next() minlen = mingrp[0] minlist = list(mingrp[1]) print minlist The input list of words gets sorted, and then passed to groupby, which uses len as the grouping criteria (groupby is not inherently aware of how the input list was sorted, so len must be repeated). The first group tuple is pulled from the grpsbylen iterator using next(). The 0'th tuple element is the grouping value - in this case, it is the minimum length 2. The 1'th tuple element is the group itself, given as an iterator. Passing this to the list constructor gives us the list of all the 2-character words, which then gets printed: ['he', 'me'] Again, 'me' no longer gets left out. Maybe this will get you some extra credit... -- Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Ip address
wormwood_3 wrote: Hello, This is definitely possible. It's more a matter of system and OS configuration than Python though, so you might want to check out some Linux forums ( http://www.linuxforums.org/ ) for additional help. In short, I think the simplest would be: Have 3 separate network interfaces in your physical box, have your router provide one of the three addresses to each. Then you could configure the client to only connect through its associated interface/IP. If this sort of setup wouldn't work, please tell us more about your configuration between your client and your network. -Sam ___ Samuel Huckins Homepage - http://samuelhuckins.com Tech blog - http://dancingpenguinsoflight.com/ Photos - http://www.flickr.com/photos/samuelhuckins/ AIM - samushack | Gtalk - samushack | Skype - shuckins It is about Ubuntu 8.04. I have 5 Ip address there (4 aliases). The idea is simple, i want to grab something from internet (website) using this client which was created in python. When the client is running, on the other server i see the first ip address from client server interface. What do i want is to have the possibility to chose or to use another ip from the list. I think Wayne told me the answer, using proxy or i was thinking about socks. But if anyone know anything better than this, i will be glad to hear. Thank you, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, Jan 20, 2009 at 10:33 AM, Paul McGuire wrote: > "Finding the shortest word among a list of words" sounds like something of > a > trick question to me. I think a more complete problem statement would be > "Find the list of words that are the shortest", since there is no guarantee > that the list does not contain two words of the same shortest length. If > you just add "me" to your sample set, you now get misleading answers: > > words = "man woman children he me".split() > print min(words, key=len) > > prints: > he > > What happened to "me"? It is just as short as "he"! > > To get *all* the words that are the shortest length, I'll show two > approaches. The first uses a defaultdict, from the collections module of > the Python stdlib. With a defaultdict, I can have new dict entries get > initialized with a default value using a specified factory function, > without > first having to check to see if that entry exists. I would like to create > a > dict of lists of words, keyed by the lengths of the words. Something that > would give me this dict: > > { 2 : ['he', 'me'], 3 : ['man'], ... etc. } > > from which I could then find the minimum length using > min(wordlendict.keys()). By using a defaultdict, I can just build things > up > by iterating over the list and adding each word to the entry for the > current > word's length - if the current word is the first one for this length to be > found, the defaultdict will initialize the value to an empty list for me. > This allows me to safely append the current word regardless of whether > there > was an existing entry or not. Here is the code that does this: > > from collections import defaultdict > wordlendict = defaultdict(list) > for w in words: >wordlendict[len(w)].append(w) > minlen = min(wordlendict.keys()) > minlist = wordlendict[minlen] > print minlist > > prints: > ['he', 'me'] > > Now we are getting a more complete answer! > > A second approach uses the groupby method of the itertools module in the > stdlib. groupby usually takes me several attempts to get everything right: > the input must be sorted by the grouping feature, and then the results > returned by groupby are in the form of an iterator that returns > key-iterator > tuples. I need to go through some mental gymnastics to unwind the data to > get the group I'm really interested in. Here is a step-by-step code to use > groupby: > > from itertools import groupby > grpsbylen = groupby(sorted(words,key=len),len) > mingrp = grpsbylen.next() > minlen = mingrp[0] > minlist = list(mingrp[1]) > print minlist > > The input list of words gets sorted, and then passed to groupby, which uses > len as the grouping criteria (groupby is not inherently aware of how the > input list was sorted, so len must be repeated). The first group tuple is > pulled from the grpsbylen iterator using next(). The 0'th tuple element is > the grouping value - in this case, it is the minimum length 2. The 1'th > tuple element is the group itself, given as an iterator. Passing this to > the list constructor gives us the list of all the 2-character words, which > then gets printed: > ['he', 'me'] > > Again, 'me' no longer gets left out. > > Maybe this will get you some extra credit... > > -- Paul Thank you so much Paul for this. In my original post, I wrote word(s), which means word or words. Your solutions look a little bit too advanced to me. I never used the collections module, and used itertools only once or twice. I will study these solutions for sure, not for extra credit, simply because I'm simply a linguistics person who uses corpora to find information without any official interest in computer science or programming classes. Maybe when I'm good enough at programming, I will take a Python class for credit, although I'm past the classes thing now. Thank you all again for helping me get a better understanding of Python. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] MySqldb problem
Manoj kumar wrote: > > > hi all, > > i am a newbie to python programming. i just know basics of the > language. i came across MySqldb. i able to manipulate database with > MySqldb but i wasn't able to add files in database through the lib > MySqldb. > > any help will be welcomed gracefully. > > thanks. > > MANOJ SHEOKAND > > (+919728523528) > > "some day there won't be a song in ur heart, sing anyway" > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > I am new to python also, here are my notes, it helps me understand how it works, may be of some help; http://asterisklinks.com/wiki/doku.php?id=wiki:mysql_fun -david -- powered by Gentoo/GNU Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about pygame/tkinter interaction
On Tue, 2009-01-20 at 08:04 -0800, Steve Willoughby wrote: > > In this case, that might be enough. I just need to show a video clip > in > an otherwise fairly simple GUI. A decorationless, borderless window > popped up on the screen over the Tk stuff would probably work. Thanks > for the advice, I'll play with this a bit. I've never used decoration-less pygame window, but I suspect it'd be much easier/better to popup a fully decorated window with a separate control panel (like mplayer). I think a lot of problems might arise with decorator-less window, such as minimizing/resizing/moving Tk window doesn't minimize/resize/move pygame's. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] lists
Hi What am I doing wrong >>> media_list = ['upper_b.wav', 'upper_a.wav'] >>> print '%s' % (for x in media_list) File "", line 1 print '%s' % (for x in media_list) ^ SyntaxError: invalid syntax >>> print '%s' % (x for x in media_list) I want to replace %s with the items of the list as individual item string, i.e 'upper_b.wav', 'upper_a.wav' if I change the list to string I still get one item: "'upper_b.wav', 'upper_a.wav'" thanks norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists
On Tue, Jan 20, 2009 at 11:30 AM, Norman Khine wrote: > Hi > What am I doing wrong > media_list = ['upper_b.wav', 'upper_a.wav'] print '%s' % (for x in media_list) > File "", line 1 >print '%s' % (for x in media_list) >^ > SyntaxError: invalid syntax print '%s' % (x for x in media_list) > > > I want to replace %s with the items of the list as individual item string, > i.e > > 'upper_b.wav', 'upper_a.wav' I'm not sure what you want, maybe one of these? In [1]: media_list = ['upper_b.wav', 'upper_a.wav'] In [2]: print ', '.join(media_list) upper_b.wav, upper_a.wav In [3]: print ', '.join(repr(x) for x in media_list) 'upper_b.wav', 'upper_a.wav' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, Jan 20, 2009 at 5:42 AM, Lie Ryan wrote: > > Using sys.maxint to prime minLen is overkill, of course - > > "antidisestablishmentarianism" is only 28 letters long, after all - but > > it should be larger than any word you can expect to see. This doesn't > > catch ties, though... could do that like so: > > > Other than overkill, it is wrong. If the shortest "word" is longer than > maxint it'd give wrong result. What language do you speak (or machine do you use), where the shortest word in a potential list is longer than maxint? On my machine maxint is 2,147,483,647. That's what I meant by overkill. You could just simply use the len of the first word. True dat. Requires an extra step or two, though - initializing with some impossibly huge number is quick. > The best solution though, have been answered by Kent Johnson. > Extremely terse and elegant, doesn't find ties. def MinKent(corpora=""): > words= corpora.split() > return min(words, key=len) > > print MinKent("No victim has ever been more repressed and alienated than > the truth is") > Output: No Mine returns (2, ['No', 'is'], 9, ['repressed', 'alienated']). I could be wrong - it seemed more like what the OP actually wanted, but he'd be the judge of that. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists
hi! On Tue, Jan 20, 2009 at 10:30 PM, Kent Johnson wrote: > On Tue, Jan 20, 2009 at 11:30 AM, Norman Khine wrote: >> Hi >> What am I doing wrong >> > media_list = ['upper_b.wav', 'upper_a.wav'] > print '%s' % (for x in media_list) >> File "", line 1 >>print '%s' % (for x in media_list) >>^ >> SyntaxError: invalid syntax > print '%s' % (x for x in media_list) >> >> >> I want to replace %s with the items of the list as individual item string, >> i.e >> >> 'upper_b.wav', 'upper_a.wav' > > I'm not sure what you want, maybe one of these? > > In [1]: media_list = ['upper_b.wav', 'upper_a.wav'] > > In [2]: print ', '.join(media_list) > upper_b.wav, upper_a.wav > > In [3]: print ', '.join(repr(x) for x in media_list) > 'upper_b.wav', 'upper_a.wav' > > Kent may be you want to do it this way: In [10]: for x in media_list: : print "%s" % x : : upper_b.wav upper_a.wav -- Regards, Arun Tomar blog: http://linuxguy.in website: http://www.solutionenterprises.co.in ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists
Hi, actually what I was trying to do carries from my last post, where my media files are a list that I want to add to the subprocess.call(['sox', ', '.join(media_list), 'list.wav']) >>> print ', '.join(media_list) upper_b.wav, upper_a.wav >>> subprocess.call(['sox', ', '.join(media_list), 'list.wav']) sox formats: can't open input file `upper_b.wav, upper_a.wav': No such file or directory 2 >>> but this is treated as one item, rather than the two. Kent Johnson wrote: On Tue, Jan 20, 2009 at 11:30 AM, Norman Khine wrote: Hi What am I doing wrong media_list = ['upper_b.wav', 'upper_a.wav'] print '%s' % (for x in media_list) File "", line 1 print '%s' % (for x in media_list) ^ SyntaxError: invalid syntax print '%s' % (x for x in media_list) I want to replace %s with the items of the list as individual item string, i.e 'upper_b.wav', 'upper_a.wav' I'm not sure what you want, maybe one of these? In [1]: media_list = ['upper_b.wav', 'upper_a.wav'] In [2]: print ', '.join(media_list) upper_b.wav, upper_a.wav In [3]: print ', '.join(repr(x) for x in media_list) 'upper_b.wav', 'upper_a.wav' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Concerning Programming Exercise #6 of Chapter 2 of Python Programming John Zelle Textbook
Hi! I need help with program exericse #6 entitled "Futval.py" which is a program that computes the value of an investment carried 10 years into the future. This is the coding I have.. yet it does not seem to spit out the correct numbers (Am I using an incorrect equation?) *def main(): print "This program calculates the future value" print "of a 10-year investment." principal = input("Enter the amount invested each year: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") for i in range (1, years + 1): principal = principal * (1 + apr)**i print "The value in", i, " years is:", principal main()* The output is suppose to appear like this: This program calculates the future value of a 10-year investment. Enter the amount invested each year: 100.00 Enter the annual interst rate: 0.05 Enter the number of years: 10 The value in 1 years is 105.0 The value in 2 years is 215.25 The value in 3 years is 331.0125 The value in 4 years is 452.563125 And so on.. ^Please help me!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists
On Tue, Jan 20, 2009 at 1:02 PM, Norman Khine wrote: > Hi, > actually what I was trying to do carries from my last post, where my media > files are a list that I want to add to the > > subprocess.call(['sox', ', '.join(media_list), 'list.wav']) OK, you want a list, not a string. You can use + to concatenate lists: ['sox'] + media_list + ['list.wav'] or args = ['sox'] args.extend(media_list) args.add('list.wav') Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concerning Programming Exercise #6 of Chapter 2 of Python Programming John Zelle Textbook
Donna Belle Ibarra wrote: Hi! I need help with program exericse #6 entitled "Futval.py" which is a program that computes the value of an investment carried 10 years into the future. This is the coding I have.. yet it does not seem to spit out the correct numbers (Am I using an incorrect equation?) Take a look at http://en.wikipedia.org/wiki/Time_value_of_money#Present_value_of_a_future_sum for the various formulas. I am not sure which applies in your case. def main(): print "This program calculates the future value" print "of a 10-year investment." principal = input("Enter the amount invested each year: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") for i in range (1, years + 1): principal = principal * (1 + apr)**i print "The value in", i, " years is:", principal main() The output is suppose to appear like this: This program calculates the future value of a 10-year investment. Enter the amount invested each year: 100.00 Enter the annual interst rate: 0.05 Enter the number of years: 10 The value in 1 years is 105.0 The value in 2 years is 215.25 The value in 3 years is 331.0125 The value in 4 years is 452.563125 And so on.. ^Please help me!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, 20 Jan 2009 09:20:22 -0800, Marc Tompkins wrote: > On Tue, Jan 20, 2009 at 5:42 AM, Lie Ryan wrote: > >> > Using sys.maxint to prime minLen is overkill, of course - >> > "antidisestablishmentarianism" is only 28 letters long, after all - >> > but it should be larger than any word you can expect to see. This >> > doesn't catch ties, though... could do that like so: > >> Other than overkill, it is wrong. If the shortest "word" is longer than >> maxint it'd give wrong result. > > What language do you speak (or machine do you use), where the shortest > word in a potential list is longer than maxint? On my machine maxint is > 2,147,483,647. That's what I meant by overkill. what I meant as wrong is that it is possible that the code would be used for a string that doesn't represent human language, but arbitrary array of bytes. Also, it is a potential security issue. > You could just simply use the len of the first word. > > True dat. Requires an extra step or two, though - initializing with > some impossibly huge number is quick. len() is fast, and it also removes the need to import sys, which actually removes an extra step or two >> The best solution though, have been answered by Kent Johnson. >> > Extremely terse and elegant, doesn't find ties. > Output: No which, at the time of writing, was my impression on the OP's request. > Mine returns (2, ['No', 'is'], 9, ['repressed', 'alienated']). I could > be wrong - it seemed more like what the OP actually wanted, but he'd be > the judge of that. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lists
Norman Khine wrote: > Hi, > actually what I was trying to do carries from my last post, where my > media files are a list that I want to add to the > > subprocess.call(['sox', ', '.join(media_list), 'list.wav']) Something like this may work; #!/usr/bin/python import subprocess def play(filename): player = "/usr/bin/sox" fname = "/home/yorname/dir/" + filname subprocess.call([player, fname]) def playall(files): for name in files: play(name) def run(): files =['wav1.wav', 'wav2.wav'] playall(files) run() -- powered by Gentoo/GNU Linux http://linuxcrazy.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, Jan 20, 2009 at 11:23 AM, Lie Ryan wrote: > what I meant as wrong is that it is possible that the code would be used > for a string that doesn't represent human language, but arbitrary array > of bytes. Also, it is a potential security issue. This is something I need to know, then - sys.maxint is a potential security issue? How? Should it be avoided? (Guess I'd better get Googling...) > > You could just simply use the len of the first word. > > > > True dat. Requires an extra step or two, though - initializing with > > some impossibly huge number is quick. > > len() is fast, and it also removes the need to import sys, which actually > removes an extra step or two > Unintended consequence - initializing minLen with the length of the first word results in trying to append to a list that doesn't exist yet - so must create minWord and maxWord ahead of time. (Could use try/except... no.) Also - it occurred to me that the input might be sentences, and sentences contain punctuation... I already took the liberty of adding "is" to the end of the OP's signature quotation; now I add a period: > corpus = "No victim has ever been more repressed and alienated than the > truth is." Now "is." has length 3, not 2; probably not what we had in mind. So, new version: > def MinMax(corpus=""): > import string > corpus = "".join( [x for x in corpus if x not in string.punctuation] ) > words = corpus.split() > minLen = len(words[0]) > maxLen = 0 > minWord, maxWord = [],[] > for word in words: > curLen = len(word) > if curLen == minLen: > minWord.append(word) > if curLen == maxLen: > maxWord.append(word) > if curLen > maxLen: > maxWord = [word] > maxLen = curLen > if curLen < minLen: > minWord = [word] > minLen = curLen > return minLen, minWord, maxLen, maxWord > Is there a good/efficient way to do this without importing string? Obviously best to move the import outside the function to minimize redundancy, but any way to avoid it at all? which, at the time of writing, was my impression on the OP's request. > Quote: "I need to find the shortest / longest word(s) in a sequence of words." I'm sure the OP has moved on by now... time I did likewise. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding the shortest word in a list of words
On Tue, Jan 20, 2009 at 3:14 PM, Marc Tompkins wrote: > On Tue, Jan 20, 2009 at 11:23 AM, Lie Ryan wrote: > >> what I meant as wrong is that it is possible that the code would be used >> for a string that doesn't represent human language, but arbitrary array >> of bytes. Also, it is a potential security issue. > > > This is something I need to know, then - sys.maxint is a potential security > issue? How? Should it be avoided? (Guess I'd better get Googling...) > > >> > You could just simply use the len of the first word. >> > >> > True dat. Requires an extra step or two, though - initializing with >> > some impossibly huge number is quick. >> >> len() is fast, and it also removes the need to import sys, which actually >> removes an extra step or two >> > Unintended consequence - initializing minLen with the length of the first > word results in trying to append to a list that doesn't exist yet - so must > create minWord and maxWord ahead of time. (Could use try/except... no.) > Also - it occurred to me that the input might be sentences, and sentences > contain punctuation... I already took the liberty of adding "is" to the end > of the OP's signature quotation; now I add a period: > >> corpus = "No victim has ever been more repressed and alienated than the >> truth is." > > Now "is." has length 3, not 2; probably not what we had in mind. > So, new version: > > >> def MinMax(corpus=""): >> import string >> corpus = "".join( [x for x in corpus if x not in string.punctuation] ) >> words = corpus.split() >> minLen = len(words[0]) >> maxLen = 0 >> minWord, maxWord = [],[] >> for word in words: >> curLen = len(word) >> if curLen == minLen: >> minWord.append(word) >> if curLen == maxLen: >> maxWord.append(word) >> if curLen > maxLen: >> maxWord = [word] >> maxLen = curLen >> if curLen < minLen: >> minWord = [word] >> minLen = curLen >> return minLen, minWord, maxLen, maxWord >> > > Is there a good/efficient way to do this without importing string? > Obviously best to move the import outside the function to minimize > redundancy, but any way to avoid it at all? > > > which, at the time of writing, was my impression on the OP's request. >> > > Quote: "I need to find the shortest / longest word(s) in a sequence of > words." > > I'm sure the OP has moved on by now... time I did likewise. > -- > www.fsrtechnologies.com > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Yes, I have moved on. I got what i needed . I have nonetheless been following your advanced discussion trying to understand what you're saying. Have not done this fully yet As far as punctuation is concerned, I separate the punctuation marks from the text before I find for the shortest / longest words. I'm working with two agglutinative languages (Swahili and Arabic), and I wanted to see how much agglutination there could be in both languages. Thank you all for your help -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Concerning Programming Exercise #6 of Chapter 2 of Python Programming John Zelle Textbook
Since you are updating the principal value each year, use the following to grow the principal by the interest rate each year: principal = principal * (1 + apr) -Mark "bob gailer" wrote in message news:49761fdc.5020...@gmail.com... Donna Belle Ibarra wrote: Hi! I need help with program exericse #6 entitled "Futval.py" which is a program that computes the value of an investment carried 10 years into the future. This is the coding I have.. yet it does not seem to spit out the correct numbers (Am I using an incorrect equation?) Take a look at http://en.wikipedia.org/wiki/Time_value_of_money#Present_value_of_a_future_sum for the various formulas. I am not sure which applies in your case. def main(): print "This program calculates the future value" print "of a 10-year investment." principal = input("Enter the amount invested each year: ") apr = input("Enter the annual interest rate: ") years = input("Enter the number of years: ") for i in range (1, years + 1): principal = principal * (1 + apr)**i print "The value in", i, " years is:", principal main() The output is suppose to appear like this: This program calculates the future value of a 10-year investment. Enter the amount invested each year: 100.00 Enter the annual interst rate: 0.05 Enter the number of years: 10 The value in 1 years is 105.0 The value in 2 years is 215.25 The value in 3 years is 331.0125 The value in 4 years is 452.563125 And so on.. ^Please help me!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 -- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor