Re: [Tutor] geeks like us and the rest of THEM
"Kirk Bailey" <[EMAIL PROTECTED]> wrote > couple. A solid and drool-proof server is the remaining leg of the Take a look at xitami http://www.xitami.com/download.htm Free, small, flexible, configurable and fast being written in C. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Group sequence pairwise
Given a sequence, how can I group it pairwise, so that I get [(s0, s1), (s2, s3), ... , (sn-1, sn)] or, if len(s)%2 != 0 [(s0, s1), (s2, s3), ... , (sn, None)] I have tried to find a solution, using itertools, but I'm not very experienced in functional stuff, so I got confused. There is a recipe ("pairwise") in the itertools docs, that does something similar but not quite what I want. Ultimately, I want to display the items of the sequence in a two-column table. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
Christopher Arndt wrote: > Given a sequence, how can I group it pairwise, so that I get > > [(s0, s1), (s2, s3), ... , (sn-1, sn)] > > or, if len(s)%2 != 0 > > [(s0, s1), (s2, s3), ... , (sn, None)] > > > I have tried to find a solution, using itertools, but I'm not very > experienced in functional stuff, so I got confused. Do you mean you're not experienced in using functions or do you mean you're inexperienced at functional programming? (If you are not completely sure you know what functional programming is, that's probably not what you're doing) I don't know very much about FP, so I can't tell if this is some common idiom in that style or not. > There is a recipe > ("pairwise") in the itertools docs, that does something similar but not > quite what I want. > Well, this is fairly simple to do with list comprehensions... >>> x = [1,2,3,4,5,6,7] >>> if len(x) % 2 != 0: x.append(None) >>> [(x[a],x[a+1]) for a in range(0,len(x),2)] [(1, 2), (3, 4), (5, 6), (7, None)] Dunno if that's what you're after, also note it modifies the list by adding a None at the end. That's just the way I chose to do it, but if you need it to not modify it should be possible as well. HTH, -Luke > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
Luke Paireepinart schrieb: > Christopher Arndt wrote: >> I have tried to find a solution, using itertools, but I'm not very >> experienced in functional stuff, so I got confused. > Do you mean you're not experienced in using functions or do you mean > you're inexperienced at functional programming? I mean functional programming. > Well, this is fairly simple to do with list comprehensions... > >>> x = [1,2,3,4,5,6,7] > >>> if len(x) % 2 != 0: x.append(None) > > >>> [(x[a],x[a+1]) for a in range(0,len(x),2)] > [(1, 2), (3, 4), (5, 6), (7, None)] I came a up with a similar solution: for i in xrange(0, len(s), 2): do_something(s[i]) if i+1 <= len(s): do_something(s[i+1]) else: do_something(None) or try: for i in xrange(0, len(s), 2): do_something(s[i]) do_something(s[i+1]) except IndexError: do_something(None) raise StopIteration > Dunno if that's what you're after, Not exactly. I wonder if this is possible without modifying the original sequence (could be not a list too) and I'd also like to find a solution that generally applies to iterables. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] geeks like us and the rest of THEM
Keep in mind that things generally become extremely reliable only after extensive real-world testing. TANSTAAFL On Feb 25, 2007, at 1:14 AM, Kirk Bailey wrote: > This has to be baby carriage reliable and simple for the business road > warrior who has not a geekified bone in their body. -- -dave After all, it is not *that* inexpressible. -H.H. The Dalai Lama ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
I found this by "using Google". You should be able to make a simple modification (I can think of a couple of ways to do it) to have it pad the end with "None". It is 100% iterator input and output. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 On Feb 25, 2007, at 7:06 AM, Christopher Arndt wrote: > Luke Paireepinart schrieb: >> Christopher Arndt wrote: >>> I have tried to find a solution, using itertools, but I'm not very >>> experienced in functional stuff, so I got confused. >> Do you mean you're not experienced in using functions or do you mean >> you're inexperienced at functional programming? > > I mean functional programming. > >> Well, this is fairly simple to do with list comprehensions... > x = [1,2,3,4,5,6,7] > if len(x) % 2 != 0: x.append(None) >> > [(x[a],x[a+1]) for a in range(0,len(x),2)] >> [(1, 2), (3, 4), (5, 6), (7, None)] > > I came a up with a similar solution: > > for i in xrange(0, len(s), 2): > do_something(s[i]) > if i+1 <= len(s): > do_something(s[i+1]) > else: > do_something(None) > > or > > try: > for i in xrange(0, len(s), 2): > do_something(s[i]) > do_something(s[i+1]) > except IndexError: > do_something(None) > raise StopIteration > >> Dunno if that's what you're after, > > Not exactly. I wonder if this is possible without modifying the > original > sequence (could be not a list too) and I'd also like to find a > solution > that generally applies to iterables. > > Chris > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor -- -dave After all, it is not *that* inexpressible. -H.H. The Dalai Lama ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] List and comprehension questions
I'm getting use to using list iteration and comprehension but still have some questions. 1. I know to replace for i in range(len(list1)): do things with list1[i] with for li in list1: do things with li but what if there are two lists that you need to access in sync. Is there a simple way to replace for i in range(len(list1)): do things with list1[i] and list2[i] with a simple list iteration? 2. I frequently replace list iterations with comprehensions list2 = list() for li in list1: list2.append(somefun(li)) becomes list2 = [somefun(li) for li in list1] but is there a similar way to do this with dictionaries? dict2 = dict() for (di, dv) in dict1.iteritems(): dict2[di] = somefun(dv) 3. Last but not least. I understand the replacement in #2 above is the proper Pythonic idiom, but what if a list isn't being created. Is it considered properly idiomatic to replace for li in list1: somefun(li) with [somefun(li) for li in list1] Thanks for the input! Jeff ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List and comprehension questions
Smith, Jeff wrote: > I'm getting use to using list iteration and comprehension but still have > some questions. > > 1. I know to replace > for i in range(len(list1)): > do things with list1[i] > with > for li in list1: > do things with li > but what if there are two lists that you need to access in sync. Is > there a simple way to replace > for i in range(len(list1)): > do things with list1[i] and list2[i] > with a simple list iteration? Use zip() to generate pairs from both (or multiple) lists: for i1, i2 in zip(list1, list2): do things with i1 and i2 > > 2. I frequently replace list iterations with comprehensions > list2 = list() > for li in list1: > list2.append(somefun(li)) > becomes > list2 = [somefun(li) for li in list1] > but is there a similar way to do this with dictionaries? > dict2 = dict() > for (di, dv) in dict1.iteritems(): > dict2[di] = somefun(dv) You can construct a dictionary from a sequence of (key, value) pairs so this will work (using a generator expression here, add [] for Python < 2.4): dict2 = dict( (di, somefun(dv) for di, dv in dict1.iteritems() ) > > 3. Last but not least. I understand the replacement in #2 above is the > proper Pythonic idiom, but what if a list isn't being created. Is it > considered properly idiomatic to replace > for li in list1: > somefun(li) > with > [somefun(li) for li in list1] I think this is somewhat a matter of personal preference; IMO it is ugly, I reserve list comps for when I actually want a list. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Group sequence pairwise
David Perlman schrieb: > I found this by "using Google". You should be able to make a simple > modification (I can think of a couple of ways to do it) to have it > pad the end with "None". It is 100% iterator input and output. > > http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/303279 Yes, this looks just like what I need, thanks for the pointer. I guessed that the islice function would probably be part of the solution, but I couldn't quite grok how it works. Chris ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Embedding Python
On Sat, Feb 24, 2007 at 02:50:41AM -0800, Dj Gilcrease wrote: > I am attempting to embed Python in a C++ app and have a question > before I get too far into it. > > Is is possible to to dynamically create an extension module? > eg, I want a module name spam and I have a stuct that contains all my > method names, and a pointer to the proper c++ function to call If you have not already, you will want to look at SWIG (http://www.swig.org/). SWIG will generate C or C++ code from a header file containing structs and classes and function declarations. That generated code can then be compiled and linked to create a shared library (.so on Linux/UNIX or .dll on Windows), which can then be loaded with the Python "import" statement. It seems a bit of a stretch to me, but I suppose that could all be done from within your application, perhaps by using os.system or popen.popenx (x = 2,3, 4). See http://docs.python.org/lib/module-popen2.html. I'l let others on the list comment on whether this is dangerous. I suppose if all the code (that is compiled by SWIG) is under your control or if you trust your users, it might not be too dangerous. Dave -- Dave Kuhlman http://www.rexx.com/~dkuhlman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Embedding Python
On 2/25/07, Dave Kuhlman <[EMAIL PROTECTED]> wrote: > If you have not already, you will want to look at SWIG > (http://www.swig.org/). SWIG will generate C or C++ code from a > header file containing structs and classes and function > declarations. That generated code can then be compiled and linked > to create a shared library (.so on Linux/UNIX or .dll on Windows), > which can then be loaded with the Python "import" statement. >From what I can tell SWIG cannot be used to create Python modules that talk to a running C++ app, which is why I am embedding then extending Python, so the import mymodule will only work while my C++ app is running ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] List and comprehension questions
Kent Johnson wrote: > Smith, Jeff wrote: > >> I'm getting use to using list iteration and comprehension but still have >> some questions. >> >> 1. I know to replace >> for i in range(len(list1)): >> do things with list1[i] >> with >> for li in list1: >> do things with li >> but what if there are two lists that you need to access in sync. Is >> there a simple way to replace >> for i in range(len(list1)): >> do things with list1[i] and list2[i] >> with a simple list iteration? >> > > Use zip() to generate pairs from both (or multiple) lists: > for i1, i2 in zip(list1, list2): >do things with i1 and i2 > > >> 2. I frequently replace list iterations with comprehensions >> list2 = list() >> for li in list1: >> list2.append(somefun(li)) >> becomes >> list2 = [somefun(li) for li in list1] >> but is there a similar way to do this with dictionaries? >> dict2 = dict() >> for (di, dv) in dict1.iteritems(): >> dict2[di] = somefun(dv) >> > > You can construct a dictionary from a sequence of (key, value) pairs so > this will work (using a generator expression here, add [] for Python < 2.4): > dict2 = dict( (di, somefun(dv) for di, dv in dict1.iteritems() ) > Missing )? dict((di, somefun(dv)) for di, dv in dict1.iteritems()) > >> 3. Last but not least. I understand the replacement in #2 above is the >> proper Pythonic idiom, but what if a list isn't being created. Is it >> considered properly idiomatic to replace >> for li in list1: >> somefun(li) >> with >> [somefun(li) for li in list1] >> > > I think this is somewhat a matter of personal preference; IMO it is > ugly, I reserve list comps for when I actually want a list. > > Kent -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] miniwiki 1.3 BETA bugs
RE leaves me totally confuzzzeddded. Yep, so confuised I'm having trouble spelling it. Sp this one line will replace both words and give a reliable result? Barnaby Scott wrote: [snip] > No idea if it has anything to do with your problem, but it struck me > that the iswikiword() function (and processword() which seems to be a > helper for it) could be replaced with one line, and it would be reliable! > > def iswikiword(word): > return bool(re.match('^([A-Z][a-z]+){2,}$', word)) > > Of course you need to import re, but that seems a small price to pay! > > HTH > > Barnaby Scott > > -- end Very Truly yours, - Kirk Bailey, Largo Florida kniht +-+ | BOX | +-+ think ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] wav audio playback
hello from a programming newbie. I am writing a metronome function. Currently, I am using a hack with the system bell: [code] def tick(rate): while true: #do until C-c print '\a' #system bell inside terminal time.sleep(rate) #pause at desired rate [/code] I would like to use any audio clip for a 'beat' of the metronome. I have looked into pyaudio for .wav clips. I installed the binary for pyaudio but it put the files into site-packages folder in my python-2.4 framework directory; I copied the files to the site-packages folder under 2.5 but when I import pyaudio at the Python 2.5 interpreter I get: """ /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pyaudio.py:101: RuntimeWarning: Python C API version mismatch for module _portaudio: This Python has API version 1013, module _portaudio has version 1012. import _portaudio as pa """ It does play sound but how can I get rid of this error? Do I have to wait for the newer version of portaudio and/or pyaudio to be released to keep this error from happening? Should I be concerned with this warning? Is there another, better, sound playback module that anyone recommend I could use? I have mac ppc os X 10.4.8 (would like to have cross platform functionality, eventually) Using Python 2.5 cheers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python shell not working like it used to
hello, I just upgraded to python 2.5 and wxpython 2.6. I'm not sure the correct list for this but I'm trying to shove some variables into a py shell using the below code. this worked pretty well before, but now it gives me an error on the last line of my brief example. The error is: 'dict' object has no attribute 'this' it occurs on line 171 in shell.py. so I looked in shell.py and it looks like this: def __init__(self, other): """Create a ShellFacade instance.""" d = self.__dict__ d['other'] = other d['helpText'] = HELP_TEXT d['this'] = other.this "other" here is the dictionary I pass in (I think), so it's for some reason looking for some attribute in my dictionary called 'this'. of course my dictionary doesn't have this attribute. I have no idea what this is. any ideas? my few lines of code are below. import py import wx.py as py partList = {'this is some dictionary':1} pyWindow2 = py.editor.EditWindow(py.editor.Editor, splitterWindow1, -1) pyWindow1 = py.shell.Shell(splitterWindow1, -1, introText = None) pyWindow1.interp.locals['partList'] = py.shell.ShellFacade(partList) - Never Miss an Email Stay connected with Yahoo! Mail on your mobile. Get started!___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] wav audio playback
You are getting these errors because the API for Python varies by release (e.g. 2.4 to 2.5) due to additions, bug fixes, etc.. The warning you are receiving is letting you know that it was compiled and meant to be run on Python 2.4, and also subtly warning that problems may arise when you run or write your scripts in the 2.5 environment. To get rid of the error safely, if possible compile PyAudio for Python 2.5, or if you can regress back to Python 2.4 until PyAudio package is available for Python 2.5. There might also be a flag that can be set to ignore warnings when you run the Python, but I am not sure about this. Some of the more Pythonic vets on the list might be able to provide feed back on that. However, from my past experience, regressing back to Python 2.4 seems to be the easiest and safest option in most cases. Regards, Adam On 2/26/07, Isaac <[EMAIL PROTECTED]> wrote: > hello from a programming newbie. > > I am writing a metronome function. > Currently, I am using a hack with the system bell: > > [code] > def tick(rate): > while true: #do until C-c > > print '\a' #system bell inside terminal > > time.sleep(rate) #pause at desired rate > > [/code] > > I would like to use any audio clip for a 'beat' of the metronome. > I have looked into pyaudio for .wav clips. I installed the binary for > pyaudio but it put the files into site-packages folder in > my python-2.4 framework directory; I copied the files to the site-packages > folder under 2.5 but when I import pyaudio at the Python 2.5 interpreter I > get: > > """ > /Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages/pyaudio.py:101: > RuntimeWarning: Python C API version mismatch for module _portaudio: This > Python has API version 1013, module _portaudio has version 1012. > import _portaudio as pa """ > > It does play sound but how can I get rid of this error? Do I have to wait > for the newer version of portaudio and/or pyaudio to be released to keep > this error from happening? Should I be concerned with this warning? > Is there another, better, sound playback module that anyone recommend I > could use? > > I have mac ppc os X 10.4.8 (would like to have cross platform > functionality, eventually) > Using Python 2.5 > > cheers > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor