Re: [Tutor] Python code pretty-print?
On Tue, 3 Jan 2006, Lance E Sloan wrote: > A colleague of mine often develops Python code under Mac OS X. He would > like to be able to print the code in an easily readable format. He's > found that kwrite on Linux does a very nice job, but it's inconvenient > to copy his code to a Linux box just to print it out. > > We've found mention (mostly in manpages) of a program called > "pyhtmlizer", but couldn't find source or binary for it. Hi Did you try subethaedit ( http://www.codingmonkeys.de/subethaedit/ )? Despite of the strange name IMHO the best editor for OS X. Excellent syntax highlighting included. Cheers Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression matching a dot?
Hi Frank & Kent & Hugo, Didn't have the time to read the list yesterday ... Thanks for pointing me to the regex-debuggers. Though I don't considered myself a regex-beginner I had to learn, that now that I'm using regexes only occasionally I might need some help here and there. Cheers, Christian Frank Bloeink wrote: > Hi [Christian|List] > > This post is not regarding your special problem (which anyway has been > solved by now), but I'd like to share some general tip on working with > regular expressions. > There are some nice regex-debuggers out there that can help clearify > what went wrong when a regex doesn't match when it should or vice > versa. > > Kodos http://kodos.sourceforge.net/ is one of them, but there are many > others that can make your life easier ; at least in terms of > regex-debugging ;) > > Probably most of you (especially all regex-gurus) know about this > already, but i thought it was worth the post as a hint for all > beginners > > hth Frank ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression matching a dot?
Thanks, corrected. I was happy now - and then too fast ;-). Cheers Christian On 19 Oct 2005, at 13:50, Kent Johnson wrote: > Christian Meesters wrote: >> Actually, your answer did help to open my eyes. The expression is >> "\d+S[S|E]\.INF": Ouch! > > That will work, but what you really mean is one of these: > "\d+S[SE]\.INF" > "\d+S(S|E)\.INF" > > Your regex will match 0S|.INF > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression matching a dot?
Actually, your answer did help to open my eyes. The expression is "\d+S[S|E]\.INF": Ouch! Thanks a lot, Christian On 19 Oct 2005, at 12:11, Misto . wrote: > [ Workaround ] > What about using the glob module? > > http://docs.python.org/lib/module-glob.html > > you can use something like > glob.glob('./[0-9][0-9]S[E|S].INF') > (Not tested) > > > Misto > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression matching a dot?
Hi Misto, Thanks, but this is no option for me - though I use glob a lot in other cases (mostly the quick and dirty hacks). But here I'm working on a list of names - not the files directly. Besides: REs are a lot more powerful (usually ;-), e.g. I'm using \d+ since I don't now that there are always only two digits, but there need to be one. Cheers, Christian On 19 Oct 2005, at 12:11, Misto . wrote: > [ Workaround ] > What about using the glob module? > > http://docs.python.org/lib/module-glob.html > > you can use something like > glob.glob('./[0-9][0-9]S[E|S].INF') > (Not tested) > > > Misto > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] regular expression matching a dot?
Hi I've got the problem that I need to find a certain group of file names within a lot of different file names. Those I want to match with a regular expression are a bit peculiar since they all look like: 07SS.INF , 10SE.INF, 13SS.INF, 02BS.INF, 05SS.INF. Unfortunately there are similar file names that shouldn't be matched, like: 01BE.INF, 02BS.INF Any other extension than 'INF' should also be skipped. (There are names like 07SS.E00, wich I don't want to see matched.) So I tried the following pattern (using re): \d+[SS|SE]\.INF - as there should be at least one digit, the group 'SE' or 'SS' followed by a dot and the extension 'INF'. Well, this doesn't work, no match. However, if I change the pattern to \d+[SS|SE] it somehow works and all all valid names are matched. But if there is a different extension than 'INF' it matches too - but it shouldn't. (Surrounding the dot with [] doesn't help, though I have no idea as for why.) Any ideas what I could do else? TIA Christian PS Hope that I described the problem well enough ... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] PyDoc problem
Hi Paul, works great and within a seond, if I just uncomment the scipy imports. This was easy, but without your help I still would stare at my screen and think it's hopeless. Thanks, Christian On 23 Sep 2005, at 13:18, paul brian wrote: > THis sounds like a recursive import, which frankly should not happen > but perhaps pydoc imports differently to the standard. Or it is trying > to document the entire scipy framework, rather than just your module > > try firstly moving the module alone to directory foo and ensuring you > are calling only that module > >> pydoc -w mymodule > > Perhaps also try uncommenting imports at the top of the file (ie # > import scipy) to see if that helps. > > > > On 9/23/05, Christian Meesters <[EMAIL PROTECTED]> wrote: >> Hi, >> >> PyDoc is working well, if invoked without flags on the modul in >> question. But when I use it with '-w' to produce html-output, I get an >> extremely long Traceback after a runtime of an hour or two. Here is a >> short part from it: >> >> Traceback (most recent call last): >> File "/usr/bin/pydoc", line 4, in ? >> pydoc.cli() >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/pydoc.py", line 2117, in cli >> writedoc(arg) >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/pydoc.py", line 1383, in writedoc >> page = html.page(describe(object), html.document(object, name)) >> File >> "/Users/maxwell/Packages/SciPy_complete-0.3.2/build/lib.darwin-7.7.0- >> Power_Macintosh-2.3/scipy_base/ppimport.py", line 397, in >> _scipy_pydoc_Doc_document >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/pydoc.py", line 283, in document >> if inspect.ismodule(object): return self.docmodule(*args) >> >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/repr.py", line 49, in repr_list >> s = s + self.repr1(x[i], level-1) >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/pydoc.py", line 319, in repr1 >> return self.escape(cram(stripid(repr(x)), self.maxother)) >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/pydoc.py", line 111, in stripid >> if re.search(pattern, repr(Exception)): >> File >> "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ >> python2.3/sre.py", line 137, in search >> return _compile(pattern, flags).search(string) >> RuntimeError: maximum recursion depth exceeded >> >> What's going wrong? When I ask Pydoc to write html documentation for >> other modules there is no problem at all. It just works fine. And this >> traceback tell me nothing about my module, only about PyDoc. For it >> matters: I'm stuck with Python2.3 for compatibility reasons in this >> project and the module I'd like to see documented uses scipy and it is >> not really a small module anymore (about 1000 lines, including >> docstrings and comments). >> Anybody an idea how I can force PyDoc to produce html output on this >> module? >> >> TIA >> Christian >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > > -- > -- > Paul Brian > m. 07875 074 534 > t. 0208 352 1741 > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] PyDoc problem
Hi, PyDoc is working well, if invoked without flags on the modul in question. But when I use it with '-w' to produce html-output, I get an extremely long Traceback after a runtime of an hour or two. Here is a short part from it: Traceback (most recent call last): File "/usr/bin/pydoc", line 4, in ? pydoc.cli() File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/pydoc.py", line 2117, in cli writedoc(arg) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/pydoc.py", line 1383, in writedoc page = html.page(describe(object), html.document(object, name)) File "/Users/maxwell/Packages/SciPy_complete-0.3.2/build/lib.darwin-7.7.0- Power_Macintosh-2.3/scipy_base/ppimport.py", line 397, in _scipy_pydoc_Doc_document File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/pydoc.py", line 283, in document if inspect.ismodule(object): return self.docmodule(*args) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/repr.py", line 49, in repr_list s = s + self.repr1(x[i], level-1) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/pydoc.py", line 319, in repr1 return self.escape(cram(stripid(repr(x)), self.maxother)) File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/pydoc.py", line 111, in stripid if re.search(pattern, repr(Exception)): File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/sre.py", line 137, in search return _compile(pattern, flags).search(string) RuntimeError: maximum recursion depth exceeded What's going wrong? When I ask Pydoc to write html documentation for other modules there is no problem at all. It just works fine. And this traceback tell me nothing about my module, only about PyDoc. For it matters: I'm stuck with Python2.3 for compatibility reasons in this project and the module I'd like to see documented uses scipy and it is not really a small module anymore (about 1000 lines, including docstrings and comments). Anybody an idea how I can force PyDoc to produce html output on this module? TIA Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] unittest: looking for examples
Hi Does anybody know examples using the unittest module for the following cases (or some parts of it)? - unittests for methods of classes getting all data from a file as a classmethod - unittests for reading and saving files in a specific way (= sanity tests for file handling) Point is that I want to test such methods directly, but also that I cannot set up unittest in my case without getting some data from a file (otherwise my testing module would get way too big). I've found lots of material on unittesting in various books and on the net, but none of the sources I've seen covers those two questions. I'd appreciate just any pointer on such material. TIA Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] fourier transform
Hi Jeff, On 3 Aug 2005, at 02:03, Jeff Peery wrote: > hope this is more clear. from the output I would expect that two > spikes appear with amplitude = 1. [snip] > I don't understand the output amplitudes. they should all be zero > except for at one herz it should be one. not sure about the frequency > for that matter either. I gotta dig up my math book and figure this > out. in the meantime any suggestions for what this is outputing would > be greatly appreciated! thanks. > Yes, well, the math book idea might be a good one ;-). No, but seriously: You are just making a little mistake. First of all you are NOT exactly calculating a Fourier transform but a numerical estimation of it (you are dealing with an array of discrete data points, which is unavoidable when dealing with computers ;-)). Look on your sine wave. Is it a perfect one? Then, what do you see when you plot your data? Some 'noise' and one little peak. The noise is due to errors which have to do with floating point arithmetics (see http://docs.python.org/tut/node16.html). It's really small and in most cases I'm sure it's neglectable as well. But when looking on your idea of what you would expect, I'm wondering whether you actually want a power spectrum estimation (see http://mathworld.wolfram.com/PowerSpectrum.html)? Try myFFT = abs(fft.fft(inp)) instead of myFFT = fft.real_fft(inp) This a least might come close to what I think you want to see, right? You might have a look on myFFt = fft.fft(inp).real as well, because it might make things to appear a little clearer. One more remark: Try to avoid overwriting python key words like 'input'. Hope this helped. Cheers Christian PS Here is the code I used. It looks a bit different from yours - I hope the comments help a bit: from numarray import * import numarray.fft as fft #at least one of the numbers should be floating point here period = 1.0 #use numarray's / Numpy's pi instead of 3.14... inp = sin(arange(0,64)*pi*2/64.0) myFFT = abs(fft.fft(inp)) #or #myFFt = fft.fft(inp).real #or #myFFT = fft.real_fft(inp) #depending on what you really want dtime = period/64.0 dfreq = 1.0/dtime for i in range(len(myFFT)): print myFFT[i], dfreq*i ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] fourier transform
Hi Pawel Kraszewski wrote: > 4. The answer is symmetrical - usually you take only half of it. I > don't > remember the exact difference between the halves, but you may find it > in any > article on FFT. The real part is identical the imaginary part has the opposite sign ("same amplitude, opposite phase"). Jeff Peery wrote: > thanks for the help. I think I'm understanding this a bit better. > although I still don't completely understand the output. here is an > example... for the input I have 1024 samples taken from a 1 Hz square > wave with amplitude = 1. for the output I would expect an infinite > number of frequencies. the output from FFT.fft(myData).real is this: > > . > . > . > -0.498 1 > 0.0 2 > -0.498 3 > 0.0 4 > -0.498 5 > 0.0 6 > -0.498 7 > 0.0 8 Frankly, I don't understand this. After your description I thought your input is like "array([0, 1, 0, ..., 1, 0, 1])". But this can't be. Could you show us how exactly your input array looks like? And how do we have to read your output? Is this a 1d-array? What do the two numbers per line mean? Cheers Christian PS Sorry for the late reply. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] fourier transform
Jeff Peery wrote: > > Hello, I have a signal that I want to do a fourier transform on. I > tried using FFT.fft(data).real but I don't understand the output. > what is output from this function, and why doesn't it require > amplitude and time as inputs? Hi Jeff, As Danny wrote, your input are real numbers and your output will be complex numbers. I don't want to point you to the numarray documentation once more, but you might to check out this: http://mathworld.wolfram.com/FourierSeries.html as for why you get complex numbers back. (To get only the real value part try real_fft() in numarrayl.) In general in numerical computation your input is simply a series of real numbers - of equal spacing in the dimension you want to consider. Which dimension you choose (amplitudes and time/phase or amplitudes and spacial frequencies and so on) is up to you. It depends on your physical or mathematical question. IMHO the best introduction you can get on Fourier Transforms & programming can be found in Numerical Recipies (here the "Numerical Recipies in C"-link http://www.library.cornell.edu/nr/bookcpdf.html - check chapter 12). Have fun with FFTs! Cheers Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] finding path to resource files in a GUI
On 9 Jun 2005, at 12:00, Michael Lange wrote: > Hi Christian, > > try > > self.cwd = os.path.abspath(sys.path[0]) > > sys.path[0] is the directory of your main program file, with > os.path.abspath() you can > get rid of the "../../" stuff at the beginning of the path if the > program is called from a link. > > I hope this helps It does! Actually I tried abspath, but didn't think of this very combination with sys.path ... Thanks a lot! Danke, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] finding path to resource files in a GUI application
Hi Currently I'm writing a GUI application with wxPython (on OS X, but I guess the problem is the same, regardless of the UNIX derivative one is using). When I start the main script where it is located the application finds all resource files (non-Python files like images and html files for html windows) without any problem. However, if a put a link in /usr/local/bin and start the script using the link the application cannot find those resource files - unless, of course, I will use full absolute paths to point to those files. One brief example to illustrate the problem: The class Goals is in a file called Help.py, located in '/gui_lib/' as seen from my main script. class Goals(wx.Frame): def __init__(self,parent,frame,title,size,pos=wx.DefaultPosition): wx.Frame.__init__(self,parent, -1,title,size,style=wx.DEFAULT_FRAME_STYLE) self.frame = frame self.cwd = os.getcwd() self.html = HtmlWindow(self,-1) self.html.LoadPage(os.path.join(self.cwd,'gui_lib/Goals.html')) #this, of course, won't work #if the main script is called from somewhere else and not the directory where the main script #is located Any ideas what I could use instead of os.getcwd to construct a relative path which will work even if I port the script to an other machine? Cheers Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "classmethods"
Hi Thanks Kent and Alan for your input. Kent's example is working like a charm. It's just that I didn't find the time to give any feedback until now. Have a nice weekend! Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] "classmethods"
Hi I've asked a similar question before, but still I have to admit that I didn't find a solution with this particular problem here: Imaging you have a class with a certain __init__ function like: class MyClass: def __init__(parameter1, parameter2=default,*args,**kwargs): #handle all input And a member function like: def fromFile(cls,path): adict = {} alist = [] #... #some part to read a file and to process data Now, how do I create an instance of MyClass when calling: x = MyClass.fromfile(path) ? When I have a line return parameter1,parameter2,...,d in fromFile, a tuple is returned, which is not quite what I want. Is there a way to make fromFile a true classmethod? Any hint would be greatly appreciated. Regards, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] one line code
Hi, Great! My own solution was more like Wolfram's. But your solution, Pierre, really runs under "obfuscating enhanced". I like it. And your idea, Andrei, is something I really didn't expect. Anyway, sorry for my late reply. Guess I learned a lot again. Thanks, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] one line code
Hi Yesterday night I was thinking about the following problem: I do have a list like l = ['1','2','3','abc','','4'] - for instance like a list one could get from a file import with the csv module (which is where my 'problem' comes from). Now I would like to generate the following list, preferably with one line of code: l2 = [1.0,2.0,3.0,'abc','',4.0] With other words I'd like to tell Python: Convert into a float if possible, otherwise append anyway. Is this possible in one line? (Right now my code is a lot longer.) I was trying with 'filter' + lambda forms, list comprehensions etc., but could not find a solution. Could it be that a C-like solution with '?' and ':' is more straightforward than a solution with Python or am I just too blind to see a real pythonic solution here? I am aware that putting a solution in one line of code might be against the 'Zen of Python' (... Complex is better than complicated ... Readability counts ...), but since I'm just asking out of curiosity, perhaps I'll get an answer anyway. ;-) Thanks a lot in advance. Cheers Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to initialize a class with arbitrary argument list from a file?
Hi How can I initialize a class like this from a file: class Some: def __init__(self,data,axis,**kwargs): pass 'data' and 'axis' should be lists of floats. Meta data can be passed to kwargs like 'name="name",date="2/3/05",...'. Right now the return value of my function reading the file looks like this: dummy = fromFile(path) print dummy [[[datalist],[axislist],'name="name"','date="2/3/05"',...],...] And, of course, initializing Some with dummy[0] like: Some(dummy[0]) will throw an exception; a TypeError, since the argument list is not of the right length and of the wrong type. Well, I've tried a few things to solve this problem, but didn't succeed: How do I have to alter FromFile() so, that I can use dummy to initialize Some? Oh, and I should mention that **kwargs can be of arbitrary length: __init__ has default values for every keyword not given. And for the record: I'm still stuck with version 2.3 until I'll upgrade to Tiger (on OSX). I'm totally lost. It would be great if you could help me out here. Thanks a lot in advance, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Initializing with a call like: someClass.open("someFile")?
Thanks Guys, Guess I have to look once more into decorator functions - and guess it's worth doing so. Anyway: My problem is solved. Cheers Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Initializing with a call like: someClass.open("someFile")?
Hi My cryptic subject is perhaps not sufficient - I'll try to make it a little better: Assume you'd like to write something like: import someClass x = someClass.open("someFile") Here '.open' should read in the data and initialize the instance - with or without calling __init__. How is this to implement? Up to now I rather wrote something like x = someClass(somePrepFunction("someFile")) where the return value of somePrepFunction was used to initialize x or called the conventional 'open' within __init__. But is there no direct approach like in my pseudo-code? My problem is that if I simply define open as a method of someClass (with def open(self,file_name): #somecode pass ) all I get is: TypeError: unbound method open() must be called with someClass instance as first argument (got str instance instead) Which is perfectly understandable. But what is a possible workaround? (Instead of 'open' I could, of course, better use some other keyword which is no Python keyword.) Any hints? Thanks a lot in advance. Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] German Totorial!?!
Hoi Michael, Apart from Alan's tutorial in German and the link Andrew provided you might want to have a look here: http://python.sandtner.org/ (The German Python forum.) You'll find not only links to other German sources, but also friendly support in German ;-). Gruss, Christian ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] unittesting and nested functions
Ken Johnson wrote: This should be self.failUnlessRaises(IOError, DNA, '1 ATG') In your usage, you are calling DNA() and passing the result of the call to failUnlessRaises(). The call to DNA raises IOError. This happens *before* the call to failUnlessRaises(), so it is interpreted as an error. Ah, I see. BTW, failUnlessRaises() is implemented using the extended call syntax that came up recently in another thread, and it is a good example of why this syntax is useful. It's also interesting that the except: statement uses the class that was passed in; I had no idea an except was dynamic in this way. Indeed, this is remarkable. Perhaps it's time for me to look into this once? Anyway, thank you Kent, now everything is working just fine. Everybody a Merry Christmas! Christian ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] unittesting and nested functions
Hi I've written some unittests using the unittest module for my 'DNA-class'. This class can cope with almost every input, but with no 'numbers' whatsoever. Well, one of the tests is: class TestFunctions(unittest.TestCase): ... def test__init__(self): """testing whether __init__ will fail if nonsense data are passed for initialization""" self.failUnlessRaises(IOError,DNA('1 ATG')) Now, if 'nonsense data' are passed to the testing function I expect an IOError to be raised. And this happens indeed. Still this test fails (actually it doesn't: only an error occurs), but why? This is the Traceback I get: == ERROR: testing whether __init__ will fail if nonsense data are passed for initialization -- Traceback (most recent call last): File "src/DNA.py", line 862, in test__init__ self.failUnlessRaises(IOError,DNA('1 ATG')) File "src/DNA.py", line 100, in __init__ raise IOError IOError -- Ran 35 tests in 0.485s FAILED (errors=1) According to the documentation everything should be just fine: -- 5.3.5 TestCase Objects ... failUnlessRaises(exception, callable, ...) Test that an exception is raised when callable is called with any positional or keyword arguments that are also passed to assertRaises(). The test passes if exception is raised, is an error if another exception is raised, or fails if no exception is raised. To catch any of a group of exceptions, a tuple containing the exception classes may be passed as exception. -- So, any idea, where my mistake is? (Before I get comments about having the unittest in the same file as the file to be tested: I'll change that! ;-) ) Thanks a lot in advance. Cheers Christian ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor