Re: extract certain values from file with re
Fabian Braennstroem wrote: > Hi, > > I would like to remove certain lines from a log files. I had > some sed/awk scripts for this, but now, I want to use python > with its re module for this task. > > Actually, I have two different log files. The first file looks > like: > >... >'some text' >... > >ITER I- GLOBAL ABSOLUTE RESIDUAL -I > I FIELD VALUES AT MONITORING LOCATION --I > NOUMOM VMOM WMOM MASS T EN DISS ENTH > UVWP TE EDT > 1 9.70E-02 8.61E-02 9.85E-02 1.00E+00 1.61E+01 7.65E+04 0.00E+00 > 1.04E-01-8.61E-04 3.49E-02 1.38E-03 7.51E-05 1.63E-05 2.00E+01 > 2 3.71E-02 3.07E-02 3.57E-02 1.00E+00 3.58E-01 6.55E-01 0.00E+00 > 1.08E-01-1.96E-03 4.98E-02 7.11E-04 1.70E-04 4.52E-05 2.00E+01 ... Just a thought, but what about using exceptions - something like: for line in logfile: vals=line.split() try: no=int(vals[0]) # parse line as needed except ValueError: #first item is not a number pass # ignore line, or parse it separately Coming from C++, using exceptions in this way still feels a bit creepy to me, but I've been assured that this is very pythonic, and I'm slowly adopting this style in my python code. Parsing the line can be easy too: (umom,vmom,wmom,mass...) = map(float,vals[1:]) -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: n-body problem at shootout.alioth.debian.org
Peter Maas wrote: > I have noticed that in the language shootout at shootout.alioth.debian.org > the Python program for the n-body problem is about 50% slower than the Perl > program. This is an unusual big difference. I tried to make the Python program > faster but without success. Has anybody an explanation for the difference? > It's pure math so I expected Perl and Python to have about the same speed. > > Peter Maas, Aachen Well, one implementation difference is that the Perl script uses parallel global arrays (one array for each positional component, one for each velocity component, and one for mass) whereas the python implementation uses planet objects. Now, this may not actually make too much of a difference as it is, but if you really want to make the python version faster, I'd use the Numeric or Numpy (parts of which may soon be standardized), and do array-wide computations, rather than using python loops (at least replacing the innermost loops). My reasonably well-informed guess is that the speed would improve markedly. Of course, numpy is not a standard package (though there is a proposal to add a standard 'array' package to python, based of numpy/numeric), but if you want to do any numerics with python, you shouldn't be without it. -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: list of polynomial functions
Josiah Manson wrote: > In the following program I am trying to learn how to use functional > programming aspects of python, but the following program will crash, > claiming that the recursion depth is too great. I am attempting to make > a list of polynomial functions such that poly[0](3) = 1, poly[1](3) = > 3, poly[2](3) = 9, etc. Could someone point me in the right direction? > Thanks. > > def make_polys(n): > """Make a list of polynomial functions up to order n. > """ > p = lambda x: 1 > polys = [p] > > for i in range(n): > polys.append(lambda x: polys[i](x)*x) > > return polys > > # construct a vector of polynomials > polys = make_polys(5) > > # print > for p in polys: > print p(3) Many wise things have been said in this thread. I would like to proffer another solution which does not rely on default arguments, just for a more functional flavor. def makepolys(n): if n==0: return [lambda x: 1] else: tailfunc=makepolys(n-1) return tailfunc + [ lambda x: x * tailfunc[-1](x)] Another way, which is properly tail recursive is the following: def makepolys2helper(n,retval): if n==0: return retval else: return makepolys2helper(n-1,retval+[lambda x: x* retval[-1](x)]) def makepolys2(n): return makepolys2helper(n,[lambda x: 1]) (Note: this could be collapsed into a single function either with a default argument, or a nested function, but I thought this was a bit clearer) This last approach could, at least theoretically, create an arbitrarily long list of polys, without overflowing any kind of stack. In practice, python does not seem to perform tail recursion optimizations, and conks out after makepolys(997) on my machine. And yes - I did just sit in on my first functional programming class :) -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Scientific computing and data visualization.
Fie Pye wrote: > Hallo > > I would like to have a high class open source tools for scientific > computing and powerful 2D and 3D data visualisation. Therefore I chose > python, numpy and scipy as a base. Now I am in search for a visualisation > tool. I tried matplotlib and py_opendx with OpenDx. OpenDx seems to me very > good but the project py_opendx looks like closed. After py_opendx instalation > and subsequent testing I got an error that needs discussion with author or an > experienced user. Unfortunately a mail to author returned as undeliverable. > > Does anybody now about suitable visualisation tool? > > Does anybody have an experience with OpenDx and py_opendx instalation? > > Thanks for your response. > > fiepye As another poster pointed out below, VTK is a very strong vis tool. It is actively supported and has bindings to several languages (C++, Python, Java, and Tcl at last count). I have used the combination of python and VTK together to produce many scientific visualizations, including production quality animations (Usually, I use Python/VTK to generate isosurfaces or the like, and import the resulting geometry data into Maya or another high-quality renderer) One hurdle to overcome is transferring array data from Numeric/Numpy into VTK. I have a sort of ad-hoc method to do that (mainly for volume data). If anyone knows of any elegant solution, or a module to ease the pain, I'd like to hear about it. If you are working with NetCDF files, you may wish to add ScientificPython (distinct from SciPy) to your toolset. It has a very nice NetCDF interface. Unfortunately, it is ancient, and you would have to install Numeric Python (ancestor to NumPy). However, it is easy to convert Numeric arrays into Numpy arrays: >>> my_numpy_array=numpy.array(my_numeric_array) -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: mapping subintervals
On Jun 13, 2:32 pm, Lee Sander <[EMAIL PROTECTED]> wrote: > hi, > I have the following problem which is turning out to be non-trivial. I > realize that this is not > exactly a python problem but more of an algorithm problem -- but I > post it here because > I want to implement this in python. > > I want to write a code that given an interval (integer tuple: > start,stop) will find which other > interval it matches to. Here is an example, > suppose I have the following NON-OVERLAPPING intervals > > 10 - 21 ==> 'a1' > 34 - 51 ==> 'a2' > 77 - 101 ==> 'a3' > etc > > So, suppose I'm give an interval such as (42,50), I should go through > the list and map it to "a2" etc > if the region is a subset of an exiting interval. If the query > interval does not exist in the table or > maps to two or more intervals (eg, (45, 80)) the program return a > None. ...snip... > Many thanks > Lee OK - I'm going to assume your intervals are inclusive (i.e. 34-51 contains both 34 and 51). If your intervals are all really all non-overlapping, one thing you can try is to put all the endpoints in a single list, and sort it. Then, you can use the bisect module to search for intervals, which will give you a logarithmic time algorithm. Here, I'm going to assume you just need the index of the containing interval. If you really need a name (i.e. 'a1' or 'a2'), you can use a list of names, and index into that. I hope those assumptions are valid! if so, the following should work: from bisect import bisect # assume initial intervals are sorted intervals=[(10,21),(34,51), (77,101)] # get a sorted list of endpoints endpts=sorted([a for a,b in intervals]+[b for a,b in intervals]) def test_interval(ivl,endpts): # Find where the left endpoint of ivl would lie in the list # i.e. the index of the first element greater than ivl[0] l_idx=bisect(endpts,ivl[0]) # If l_idx is even, then it lies between two intervals, and thus # is not contained in any interval. Otherwise it returns the index if l_idx % 2 == 0: return None # if l_idx is out of bounds (i.e. ==len(endpts)), then ivl is # not contained in any interval (too large) if l_idx==len(endpts): return None # Now, all we need to do is check that the right endpoint of ivl is # less than or equal to the corresponding endpoint in the list. # Happily, that endpoint is at index l_idx if ivl[1]<=endpts[l_idx]: # So return the index of the interval: return (l_idx-1)/2 else: return None Then, from a shell: >>> print tst.test_interval((0,13),endpts) None >>> print tst.test_interval((15,21),endpts) 0 >>> print tst.test_interval((35,40),endpts) 1 >>> print tst.test_interval((40,80),endpts) None >>> print tst.test_interval((109,200),endpts) None -- http://mail.python.org/mailman/listinfo/python-list
Re: Appending a log file and see progress as program executes
On May 30, 1:03 pm, "Karim Ali" <[EMAIL PROTECTED]> wrote: > Hi, > > I am writing a program that will take several days to execute :) and would > like to append to a log file but be able to open that file at any time and > see the errors that have occured. > > So this is what I am doing: > > -- > flog = open('out.log', 'a') > > when needed: > sys.stdout=flog > print "error message" > I imagine that your problem is that stdout is buffered, and hence only writes to the output when the buffer is full. To ameliorate this, you can use flog.flush() after every print statement. Other alternatives: - Use stderr for printing log messages, and run python in unbuffered mode (python -u script.py) You can store the log file by redirecting stderr. Using bash, this would be: python -u script.py 2>out.log - Write an autoflush class: class AutoFlush: def __init__(self, stream): self.stream = stream def write(self, text): self.stream.write(text) self.stream.flush() ... flog=open('out.log','a') flog=AutoFlush(flog) print >>flog,"I'm a message!" Note that instead of changing stdout, you can also print directly to a file with: print >>flog,"Something to print" cheers, -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Appending a log file and see progress as program executes
On May 30, 1:29 pm, Matteo <[EMAIL PROTECTED]> wrote: > - Write an autoflush class: > class AutoFlush: > def __init__(self, stream): > self.stream = stream > def write(self, text): > self.stream.write(text) > self.stream.flush() > > ... > flog=open('out.log','a') > flog=AutoFlush(flog) > > print >>flog,"I'm a message!" Oops! According to another thread, this won't work for files (but will work for stdio/stderr) Check the thread entitled "print bypasses calling write method for objects inheriting from file?" -- http://mail.python.org/mailman/listinfo/python-list
Extracting attributes from compiled python code or parse trees
Hello- I am trying to get Python to extract attributes in full dotted form from compiled expression. For instance, if I have the following: param = compile('a.x + a.y','','single') then I would like to retrieve the list consisting of ['a.x','a.y']. I have tried using inspect to look at 'co_names', but when I do that, I get: >>> inspect.getmembers(param)[23] ('co_names', ('a', 'x', 'y')) with no way to determine that 'x' and 'y' are both attributes of 'a'. The reason I am attempting this is to try and automatically determine data dependencies in a user-supplied formula (in order to build a dataflow network). I would prefer not to have to write my own parser just yet. Alternatively, I've looked at the parser module, but I am experiencing some difficulties in that the symbol list does not seem to match that listed in the python grammar reference (not surprising, since I am using python2.5, and the docs seem a bit dated) In particular: >>> import parser >>> import pprint >>> import symbol >>> tl=parser.expr("a.x").tolist() >>> pprint.pprint(tl) [258, [326, [303, [304, [305, [306, [307, [309, [310, [311, [312, [313, [314, [315, [316, [317, [1, 'a']], [321, [23, '.'], [1, 'x', [4, ''], [0, '']] >>> print symbol.sym_name[316] power Thus, for some reason, 'a.x' seems to be interpreted as a power expression, and not an 'attributeref' as I would have anticipated (in fact, the symbol module does not seem to contain an 'attributeref' symbol) (for the curious, here is the relevant part of the AST for "a**x": [316, [317, [1, 'a']], [36, '**'], [315, [316, [317, [1, 'x' ) Anyway, I could write an AST analyzer that searches for the correct pattern, but it would be relying on undocumented behavior, and I'm hoping there is a better way. (By the way, I realize that malicious users could almost certainly subvert my proposed dependency mechanism, but for this project, I'm guarding against Murphy, not Macchiavelli) Thanks, -matt -- http://mail.python.org/mailman/listinfo/python-list
c++ extension, problem passing argument
Hi all, I wrote a few c++ classes for some data analysis on a physics esperiment. Now I want to glue the lot with Python, so I built the necessary wrap code with SWIG and compiled the library with Distutils. All is fine, I can import the module and classes work as expected... ...but for one thing. In the "Fitter" class I have a "findTrack" method, which takes as argument (in c++) an array of pointers to "WireHit" object (another class). In the python code I filled a list with WireHit objects but passing that to the function raises an exception: here is the (relevant) code: import wiredevents as we ... hits_array = [] for i in range(nhit): hit_data = array('l') hit_data.fromfile(input, hit_data_size) hits_array.append(we.WireHit(*hit_data)) track = we.Fitter.findTrack(hits_array, nhit) this crashes with: Traceback (most recent call last): File "./python_version.py", line 37, in track = we.Fitter.findTrack(hits_array, nhit) TypeError: in method 'Fitter_findTrack', argument 1 of type 'WireHit **' how can I pass the right type to the function, without changing the C+ + code? -- http://mail.python.org/mailman/listinfo/python-list
Re: c++ extension, problem passing argument
On 13 Mar, 18:19, Aaron Brady wrote: > On Mar 13, 12:03 pm, Matteo wrote: > > > > > Hi all, > > > I wrote a few c++ classes for some data analysis on a physics > > esperiment. Now I want to glue the lot with Python, so I built the > > necessary wrap code with SWIG and compiled the library with Distutils. > > All is fine, I can import the module and classes work as expected... > > > ...but for one thing. In the "Fitter" class I have a "findTrack" > > method, which takes as argument (in c++) an array of pointers to > > "WireHit" object (another class). In the python code I filled a list > > with WireHit objects but passing that to the function raises an > > exception: > > > here is the (relevant) code: > > > import wiredevents as we > > ... > > hits_array = [] > > for i in range(nhit): > > hit_data = array('l') > > hit_data.fromfile(input, hit_data_size) > > hits_array.append(we.WireHit(*hit_data)) > > track = we.Fitter.findTrack(hits_array, nhit) > > > this crashes with: > > Traceback (most recent call last): > > File "./python_version.py", line 37, in > > track = we.Fitter.findTrack(hits_array, nhit) > > TypeError: in method 'Fitter_findTrack', argument 1 of type 'WireHit > > **' > > > how can I pass the right type to the function, without changing the C+ > > + code? > > Just some ideas: hits_array.tostring( ), hits_array.buffer_info( ) > [ 0 ], or PyObject_AsReadBuffer( hits_array, (some_buffer), > (buffer_len) ). Doesn't seem to work. The relevant lines in wiredevents_wrap.cxx should be: SWIGINTERN PyObject *_wrap_Fitter_findTrack(PyObject *SWIGUNUSEDPARM (self), PyObject *args) { if (!PyArg_ParseTuple(args,(char *)"OO:Fitter_findTrack",&obj0,&obj1)) SWIG_fail; SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "Fitter_findTrack" "', argument " "1"" of type '" "WireHit **""'"); -- http://mail.python.org/mailman/listinfo/python-list
Re: c++ extension, problem passing argument
hmmm... looks like SWIG has a problem with double pointers. I googled around a bit and found: http://osdir.com/ml/programming.swig/2003-02/msg00029.html anyone knows how to write a small wrapper to do the appropriate dereferencing? -- http://mail.python.org/mailman/listinfo/python-list
Re: c++ extension, problem passing argument
On 13 Mar, 22:35, Aaron Brady wrote: > On Mar 13, 1:34 pm, Matteo wrote: > > > hmmm... looks like SWIG has a problem with double pointers. I googled > > around a bit and found: > > >http://osdir.com/ml/programming.swig/2003-02/msg00029.html > > > anyone knows how to write a small wrapper to do the appropriate > > dereferencing? > > 'ctypes' may be able to do it. I've done something like this in the > past: > > double_ptr= ctypes._cast( PyObject, sing_ptr ) > > Up your alley? Thanks for your suggestions, but still no luck here. ctypes appears to work only with its own types, I tried its cast, byref and pointer functions, but only received TypeError exceptions such as: TypeError: byref() argument must be a ctypes instance, not 'list' TypeError: _type_ must have storage info This is getting really annoying :( The following link may contain useful info, but I find it somewhat obscure http://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapFAQ.html#26 -- http://mail.python.org/mailman/listinfo/python-list
Re: parsing tab separated data efficiently into numpy/pylab arrays
On 13 Mar, 23:19, per wrote: > hi all, > > what's the most efficient / preferred python way of parsing tab > separated data into arrays? for example if i have a file containing > two columns one corresponding to names the other numbers: > > col1 \t col 2 > joe \t 12.3 > jane \t 155.0 > > i'd like to parse into an array() such that i can do: mydata[:, 0] and > mydata[:, 1] to easily access all the columns. > > right now i can iterate through the file, parse it manually using the > split('\t') command and construct a list out of it, then convert it to > arrays. but there must be a better way? > > also, my first column is just a name, and so it is variable in length > -- is there still a way to store it as an array so i can access: mydata > [:, 0] to get all the names (as a list)? > > thank you. I think you can do it through: array.fromfile() array.reshape() but you should look up the reference for those. -- http://mail.python.org/mailman/listinfo/python-list
Re: c++ extension, problem passing argument
On 14 Mar, 02:08, Aaron Brady wrote: > On Mar 13, 5:42 pm, Matteo wrote: > > > > > On 13 Mar, 22:35, Aaron Brady wrote: > > > > On Mar 13, 1:34 pm, Matteo wrote: > > > > > hmmm... looks like SWIG has a problem with double pointers. I googled > > > > around a bit and found: > > > > >http://osdir.com/ml/programming.swig/2003-02/msg00029.html > > > > > anyone knows how to write a small wrapper to do the appropriate > > > > dereferencing? > > > > 'ctypes' may be able to do it. I've done something like this in the > > > past: > > > > double_ptr= ctypes._cast( PyObject, sing_ptr ) > > > > Up your alley? > > > Thanks for your suggestions, but still no luck here. > > > ctypes appears to work only with its own types, I tried its cast, > > byref and pointer functions, but only received TypeError exceptions > > such as: > > TypeError: byref() argument must be a ctypes instance, not 'list' > > TypeError: _type_ must have storage info > > > This is getting really annoying :( > > > The following link may contain useful info, but I find it somewhat > > obscurehttp://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapF... > > I'm on my last one (suggestion). To get a pointer to the actual > address of an mmap instance, (and access it's 'data' attribute), I > used the following: > > from _ctypes import _cast_addr > from ctypes import * > _mmap_cast= _data_cast= PYFUNCTYPE(py_object, py_object, py_object, > py_object)(_cast_addr) > _offset_cast= _record_cast = PYFUNCTYPE(py_object, c_void_p, > py_object, py_object)(_cast_addr) > > class MMAP(Structure): > _fields_= [ > #("next",c_int), > #("prev",c_int), > ("refcnt",c_int), > ("type",c_int), > ("data",c_long) > ] > > def pdata( map ): > a= _data_cast( map, None, POINTER( MMAP ) ) > return a.contents.data > > It worked on my machine in 2.5, no promises. It was a workaround for > the '_type_ must have storage info' error. Lo and behold, > 'a.contents' was an MMAP, so '.data' was a 'c_long' interpretation of > the 'char* data' member of the C structure, PyMmapObject or > something. I found it by studying the 'ctypes.cast' code. Well good > luck and sorry for the suggestions. Umph... couldn't get this to work either. I'm starting a new thread, reproducing the problem with the minimum amount of c++ and python code, now that I know where the problem lies. Again, thanks for your time. -- http://mail.python.org/mailman/listinfo/python-list
SWIG, c++ to Python: array of pointers (double pointer) not working
Re-posting in more simple and precise terms from a previous thread http://groups.google.it/group/comp.lang.python/browse_thread/thread/6dd7bd9a09b8a011/5119cf15ebfa38b8 Problem: SWIG doesn't properly wrap c++ arrays of pointers, therefore when you try to call a c++ function which requires them, a TypeError exception is raised. Similar story here: http://osdir.com/ml/programming.swig/2003-02/msg00064.html Already tried: - some ctypes functions - tuple or string instead of list Possibile solutions: something like http://embedded.eecs.berkeley.edu/Alumni/pinhong/scriptEDA/pyTypemapFAQ.html#20 that didn't work either, but I think I was not able to adapt the code to my case, since the example is poorly explained. Code to reproduce error: I made a dptest.cpp function that calculates the sum of an array of pointers to ints. #include "dptest.h" //the header file is just //int sum(int**, int); int sum(int** dp, int len){ int sum = 0; for (int i = 0; i < len; i++){ sum += *(dp[i]); } return sum; } swig -c++ -python, then setup.py build_ext --inplace gets it nicely compiled and wrapped for python use. It also is imported without problems, but then... mat...@matteo:~/lab/sandbox$ python Python 2.5.2 (r252:60911, Oct 5 2008, 19:24:49) [GCC 4.3.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import dptest as dp >>> l = [1, 2, 3, 4] >>> size = len(l) >>> dp.sum(l,size) Traceback (most recent call last): File "", line 1, in TypeError: in method 'sum', argument 1 of type 'int **' NOTE: A pure c++ program works as expected: #include int sum(int**, int); int main(){ int **array_of_ptr = new int*[4]; for (int i = 0; i < 4; i++){ array_of_ptr[i] = new int; *array_of_ptr[i] = i+1; //fill it with 1,2,3,4: 1+2+3+4 = 10 } std::cout << sum(array_of_ptr, 4) << std::endl; } int sum(int** dp, int len){ int sum = 0; for (int i = 0; i < len; i++){ sum += *(dp[i]); } return sum; } compiling and running prints the correct result: mat...@matteo:~/lab/sandbox$ ./purecpp 10 -- http://mail.python.org/mailman/listinfo/python-list
Re: Downloading binary files - Python3
> srcdata = urlopen(url).read() > dstfile = open(path,mode='wb') > dstfile.write(srcdata) > dstfile.close() > print("Done!") Have you tried reading all files first, then saving each one on the appropriate directory? It might work if you have enough memory, i.e. if the files you are downloading are small, and I assume they are, otherwise it would be almost useless to optimize the code, since the most time consuming part would always be the download. Anyway, I would try and time it, or timeit. ;) Anyway, opening a network connection does take some time, independent of the size of the files you are downloading and of the kind of code requesting it, you can't do much about that. If you had linux you could probably get better results with wget, but that's another story altogether. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending SMS using python script
I use a programme, written in Python, which sends sms through the sms providers. You might want to have a look to the source code: http://www.moioli.net/Progetti___1/MoioSMS___Messaggi_GRATIS_da_Internet22.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Sending SMS using python script
I don't really know, because I didn't write it myself ;) I think it basically logs in into a service provider site with pycurl, follows the right links, reads captcha's and writes an SMS, which is then sent by the provider itself. I can give you the direct link to the source code from the same site: http://www.moioli.net/files/MoioSMS2.18-src.zip -- http://mail.python.org/mailman/listinfo/python-list
Re: "Pythoner",Wish me luck!
On Apr 3, 9:05 am, Linuxwell wrote: > Starting today I would like to study Python,Wish me luck! Good luck! Don't forget to... >>> print 'Hello World!' ;) -- http://mail.python.org/mailman/listinfo/python-list
Reading 3 objects at a time from list
Hi all, let's see if there is a more "pythonic" way of doing what I'm trying to do. I have a lot of strings with numbers like this one: string = "-1 1.3 100.136 1 2.6 100.726 1 3.9 101.464 -1 5.2 102.105" I need to pass the numbers to a function, but three at a time, until the string ends. The strings are of variable length, but always a multiple of three. That's what I did: num = string.split() for triple in zip(num[::3], num[1::3], num[2::3]): func(*triple) it works and I like slices, but I was wondering if there was another way of doing the same thing, maybe reading the numbers in groups of arbitrary length n... any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Play sound at wanted frequency
I need to playback a sound on a linux machine of a pre-determined frequency like, say, 440 Hz. How can I do that with python? I found the ossaudiodev package, but it says that the ossaudiodev.write() method accepts data as a raw string. It doesn't explain what the string should be like, and the oss documentation is mainly about C++. Do you have any tips to share? -- http://mail.python.org/mailman/listinfo/python-list
Re: Play sound at wanted frequency
On 15 Apr, 19:25, Scott David Daniels wrote: > Diez B. Roggisch wrote: > > Matteo schrieb: > >> I need to playback a sound on a linux machine of a pre-determined > >> frequency like, say, 440 Hz. How can I do that with python? I found > >> the ossaudiodev package, but it says that the ossaudiodev.write() > >> method accepts data as a raw string. It doesn't explain what the > >> string should be like, and the oss documentation is mainly about C++. > > >> Do you have any tips to share? > > > Try pygame. You can create samples with it, and play them. > > > See e.g. this:http://www.pygame.org/docs/ref/sndarray.html > > And realize that if you can get that to work, you can always cut into > pygame to see how _it_ accomplishes your task, and mimic that. > > --Scott David Daniels > scott.dani...@acm.org I'll give it a try, thanks ;) -- http://mail.python.org/mailman/listinfo/python-list
Fill Javascript form
Hi everybody, I have to fill a web form to authenticate and connect to the internet. I thought it would have been easy to make a script to do that automatically on startup. Unfortunately, it turned out that the form is written in JavaScript, and urllib2 therefore fails to even fetch the form. The form itself is very simple, just name and password fields and a "submit" button. Do you know of any workaround? -- http://mail.python.org/mailman/listinfo/python-list
Re: Fill Javascript form
On 11 Mag, 23:06, Shawn Milochik wrote: > How is the form "written in JavaScript"? Is it dynamically generated? > > In any case, can you just send a POST request if you know the values required? The problem is indeed that the form is dynamically generated. That's the .js file: if (navigator.appVersion.indexOf("MSIE")!=-1) document.write(""); if (!window.queryObj) { window.queryObj = new Object(); window.location.search.replace(new RegExp("([^?=&]+)(=([^&] *))?","g"), function($0,$1,$2,$3) { queryObj[$1] = $3; }); if (queryObj['uamip'] == null && queryObj['uamport'] == null) {queryObj['uamip']='10.192.0.1';queryObj['uamport']='3990';} } if (queryObj['uamip'] != null && queryObj['uamport'] != null) { var script = document.getElementById('chillicontroller'); if (script == null) { //document.write('manutenzione'); script = document.createElement('script'); script.id = 'chillicontroller'; script.type = 'text/javascript'; script.src = 'http://'+queryObj['uamip']+':3990/www/ chillijs.chi'; var head = document.getElementsByTagName("head")[0]; if (head == null) head = document.body; head.appendChild(script); } script=document.getElementById('chillicontroller'); if(script==null)document.write('manutenzione'); script.src = 'http://'+queryObj['uamip']+':3990/www/chillijs.chi'; } else { //document.write('manutenzione'); var noLocation = document.getElementById("noLocation"); if (noLocation != null && noLocation.style) { noLocation.style.display = 'inline'; } } -- http://mail.python.org/mailman/listinfo/python-list
Passing a sqlite connection into an extension module
Hello- I'm trying to find out if there is a way to share a sqlite database connection between python and an extension module written in C or C+ +. My setup is that I have some pretty intensive OpenGL rendering code that gets its values from a largish sqlite database, performs a fair bit of computation, and then draws. I'm throwing everything I can at it to keep the code in python (numpy, Cython, vertex arrays and drawlists), but the initial rendering is very slow, and it's fast becoming much more time consuming for me to optimize the python than it would be to write equivalent code in a C++ extension module. However, I would like to have access to the same database from within Python for running lesser queries, and for prototyping new features. The problem is that I may be running from an in-memory database, and I might also be using temporary tables (for a cache), which are not shared across separate connections. I would like to grab the pointer to the sqlite3 connection struct, and pass it to an extension module so that I can use sqlite's native API. I can not find any way to do such a thing in either the standard sqlite module, or in APSW. Has anyone had any experience with this? I can think of some workarounds (such as hack APSW, among other ideas). Any caveats? Thanks, -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: Sqlite3. Substitution of names in query.
On Oct 30, 7:10 am, Lacrima wrote: > Hello! > > I use sqlite3 module for my sqlite database. I am trying to substitute > table name in sql query. > > >>> import sqlite3 > >>> con = sqlite3.connect('mydb') > >>> cur = con.execute("select * from table where name='Joe'") > > That's ok > > >>> cur = con.execute("select * from table where name=?", ('Joe',)) > > That's ok too > > >>> cur = con.execute("select * from ? where name=?", ('table', 'Joe')) > > Traceback (most recent call last): > File "", line 1, in > sqlite3.OperationalError: near "?": syntax error > > So what's wrong now? > Is it impossible to substitute table names, using DB API? > If so, what is a way to make table name substitutions? Are string > operations like > 'select * from %s where...' % tablename > ok in this case? > > Thanks in advance! I've found myself wanting this ability too, but alas, it is not possible. SQLite statements are compiled into an intermediate bytecode so that they can execute very quickly. This bytecode allows for placeholders to be used for values, so that the same compiled bytecode can be run for a multitude of values (handy for large INSERTS, of course) without recompilation. As I understand it, the bytecode is specific to the table(s) and columns used in the statement. I don't know the specific mechanism, but I would suspect that a column name gets converted to an offset into a row, or to a pointer to a table's column array, or somesuch. In particular, the code generated is probably drastically different depending on whether or not a column in a table is indexed or not. Thus, if a placeholder was used for a column, then the whole statement would have to be recompiled each time it was run, which would do very nasty things to efficiency. So, if you really need that ability, you must use normal python string interpolation. -- http://mail.python.org/mailman/listinfo/python-list
Re: Sqlite3. Substitution of names in query.
On Oct 30, 7:27 am, "Diez B. Roggisch" wrote: [snip] > Or even better, by > not doing it at all - because usually, your datamodel is tied to your > program, so the need for this kind of dynamicity shouldn't arise in the > first place. > > Die Perhaps that is true in the majority of cases, but there are exceptions. I can think of a couple of instances where one might need to do it: 1) A general database exploration or visualization application, or 2) Where one needs to perform a similar operation on several different tables. In a current case of mine, I'm converting from several externally provided tab-delimited tables to an in-memory sqlite database. Most of my app is tied closely to the model, and needs no such dynamicity. However, I want to automate the conversion, so I don't have to write 20 or so similar functions. -- http://mail.python.org/mailman/listinfo/python-list
Re: logging sound / speech handler?
On Sep 9, 6:02 pm, Tim Chase wrote: > > For an application in an industrial environment where the workers are > > not always sitting in front of the monitor, but are within earshot of > > the PC I would need an sound / speech handler for the standard logging > > system. It should beep or better say the logging message. (with > > standard filtering etc.) > > I google search was not successfull. > > Does anybode know of such a handler? ... > > On *nix, there's the Festival/Flite/Mbrola suite of TTS tools. > You might be able to have one of their toolkit listen on a pipe > to read whatever comes in, and then just log to that FIFO target > as a file. > > -tkc Also, in the unlikely event that you are using a Mac for industrial applications, you can use the OS X "say" command in a similar manner, using Popen. Or, for very infrequent messages, just os.system("say " + log_text). -matt -- http://mail.python.org/mailman/listinfo/python-list
Re: a problem about "print"
in the code2 aList=[1,2,3,4,5,6,7,8,9,10] aList=str(aList) #<--- here you convert the list in a string print aList print aList[2] #<-- here you are printing the third caracter of the string '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' not the list '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' regards Matteo Il 04/07/2012 09:28, levi nie ha scritto: Hi,Harrison. Your method is cool. But i doubt this, if bList and aList just are attached to the same List when i write bList=aList,but why the output of the following two code are different? code1: aList=[1,2,3,4,5,6,7,8,9,10] bList=aList bList=str(bList) print aList print aList[2] code2: aList=[1,2,3,4,5,6,7,8,9,10] aList=str(aList) print aList print aList[2] i'm puzzled now. 2012/7/4 Harrison Morgan <mailto:harrison.mor...@gmail.com>> On Wed, Jul 4, 2012 at 12:38 AM, levi nie mailto:levinie...@gmail.com>> wrote: that's good,thanks. new problem. when i write bList=aList del bList[2] bList and aList both change,how can i make aList not changed? Lists are mutable. That means that when you do bList = aList, you're just creating another reference to aList. They both point to the same list, just using different names. You should read up a bit on immutable vs. mutable objects. Here's something that I found that might explain it a bit better. http://henry.precheur.org/python/copy_list -- http://mail.python.org/mailman/listinfo/python-list
Initial pointers for web based LAMP installation
Hi all This is what I want to do: On a LAMP server, people are able to request for an instance of a particular LAMP application by submitting a few options. That involves creating a couple of directories, getting the code, writing a config file, setting file permissions and creating a mysql user. I can do it except for the interface with a shell script. Because of the web interface I was told that python is the right language but I have no knowledge in python. Could you pl. give me the initial pointers for this scheme? Matteo -- http://mail.python.org/mailman/listinfo/python-list
gc.get_objects()
Hi All, I'm facing some trouble with a win32com application, it seems, that some com object (win32com) are still in the gc.get_objetc() list, even if I set to non the objetc and I'm out of the scope of that objects. What I'm try to do is to remove this object from the list. but I do know how.. the .__del__() method dose not work on this object... there is someone that can give me some help on this ? Regards, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: gc.get_objects()
from my gc.get_object() I extract the sub system of the object that I would like to delete: this is the object: Class name win32com.gen_py.F4503A16-F637-11D2-BD55-00500400405Bx0x1x0.ITDProperty.ITDProperty that is traked and the reference are: get_referents >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> RefCount 5 >>>> (0x026ACB58>,) RefCount 5 >>>> '__int__': , '__module__': 'win32com.gen_py.F45 RefCount 8 >>>> ITDProperty RefCount 9 >>>> RefCount 9 >>>> get_referrers >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> >>>>>>>>>>>> RefCount 15 >>>> 'python_version': 34014192, 'defaultUnnamedArg': RefCount 6 >>>> win32com.gen_py.F4503A16-F637-11D2-BD55-00500400405Bx0x1x0.ITDProperty.I RefCount 4 >>>> (u'ItemsListCreator', u'trick', u'pVal'), (3, 49, '0', None), (16393, 10, None, RefCount 4 >>>> RefCount 7 >>>> RefCount 5 >>>> '{39AAEA35-F71F-11D2-BD59-00500400405B}': win32com.gen_py.F4503A16-F637- how can I understand how to clean up this situation or were are the references that I need to delete ? From the cad non python script I call an in process python com object, and before coming back to the cad application I need to clean up all com reference, because if I do not do that I corrupt the cad application . so I urgently need to clean up all reference before coming back to the cad application. any idea? regards, Matteo Il 17/09/2012 18:09, Chris Angelico ha scritto: On Tue, Sep 18, 2012 at 12:16 AM, Steven D'Aprano wrote: The __del__ method does not delete an object. Remember, objects are only deleted when there are no references to it. Otherwise you could have some code that tries to use a deleted object, and you would get a system crash or BSOD. There is a conceptually viable alternative: destroy an object immediately and force all references to it to become some sentinel value (eg None). Python currently doesn't have this, but it would be rather convenient at times. Could be part of a construct like 'with' to say "make this, use it, and then dispose of it". ChrisA -- http://mail.python.org/mailman/listinfo/python-list
Tkinter deadlock on graceful exit
Hi list, recently I started to work on an application [1] which makes use of the Tkinter module to handle interaction with the user. Simply put, the app is a text widget displaying a file filtered by given criteria, with a handy feature that the window is raised each time a new line is added to the widget. The application mainly consists of three threads: the first one, the file processor, reads the file, filters the lines of interest, and pushes them into a shared queue (henceforth `lines_queue`); the second one, the gui_updater, pops elements from `lines_queue`, and schedule GUI updates using the `after_idle` method of the Tkinter module; finally the last one, the worker spawner, receives commands by the gui (by means of a shared queue, `filters_queue`), and drives the application, terminating or spawning new threads. For example, let's see what happens when you start the application, fill the filter entry and press Enter button: 1 the associated even handler is scheduled (we should be inside the Tkinter mainloop thread), and the filter is pushed into `filters_queue`; 2 the worker spawner receives the new filter, terminate a possibly running working thread, and once done, create a new file processor; 3 the file processor actually processes the file and fills the `lines_queue` with the lines matching given filter; 4 the gui updater schedules GUI updates as soon as items are pushed into `lines_queue` 5 Tkinter mainloop thread updates the gui when idle What happens when the main window is closed? Here is how I implemented the graceful shutdown of the app: 1 a quit event is scheduled and a _special_ message is pushed into both `filter_queue` and `lines_queue` 2 the gui updater threads receives the _special_ message, and terminates 3 the worker spawner receives the message, terminates the working thread and interrupts her execution. 4 Tk.quit() is called after the quit event handler, and we finally quit the mainloop Honestly speaking, I see no issues with the algorithm presented above; however, if I close the window in the middle of updates of the text widget, the applications hangs indefinitely. On the other hand, everything works as expected if I close the app when the file processor, for example, is waiting for new content to filter. I put some logging messages to analyze the deadlock (?!), and noticed that both the worker spawner and the file processor are terminated correctly. The only thread still active for some strange reasons, is the gui updater. Do you see anything wrong with the description presented above? Please say so, because I can't figure it out! Regards, Matteo [1] https://bitbucket.org/iamFIREcracker/logfilter -- http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter deadlock on graceful exit
On May/28, Matteo Landi wrote: > Hi list, > recently I started to work on an application [1] which makes use of the > Tkinter > module to handle interaction with the user. Simply put, the app is a text > widget displaying a file filtered by given criteria, with a handy feature that > the window is raised each time a new line is added to the widget. > > The application mainly consists of three threads: the first one, the file > processor, reads the file, filters the lines of interest, and pushes them into > a shared queue (henceforth `lines_queue`); the second one, the gui_updater, > pops elements from `lines_queue`, and schedule GUI updates using the > `after_idle` method of the Tkinter module; finally the last one, the worker > spawner, receives commands by the gui (by means of a shared queue, > `filters_queue`), and drives the application, terminating or spawning new > threads. > > For example, let's see what happens when you start the application, fill the > filter entry and press Enter button: > 1 the associated even handler is scheduled (we should be inside the Tkinter > mainloop thread), and the filter is pushed into `filters_queue`; > 2 the worker spawner receives the new filter, terminate a possibly running > working thread, and once done, create a new file processor; > 3 the file processor actually processes the file and fills the `lines_queue` > with the lines matching given filter; > 4 the gui updater schedules GUI updates as soon as items are pushed into > `lines_queue` > 5 Tkinter mainloop thread updates the gui when idle > > What happens when the main window is closed? Here is how I implemented the > graceful shutdown of the app: > 1 a quit event is scheduled and a _special_ message is pushed into both > `filter_queue` and `lines_queue` > 2 the gui updater threads receives the _special_ message, and terminates > 3 the worker spawner receives the message, terminates the working thread and > interrupts her execution. > 4 Tk.quit() is called after the quit event handler, and we finally quit the > mainloop > > Honestly speaking, I see no issues with the algorithm presented above; > however, > if I close the window in the middle of updates of the text widget, the > applications hangs indefinitely. On the other hand, everything works as > expected if I close the app when the file processor, for example, is waiting > for > new content to filter. > > I put some logging messages to analyze the deadlock (?!), and noticed that > both > the worker spawner and the file processor are terminated correctly. The only > thread still active for some strange reasons, is the gui updater. > > Do you see anything wrong with the description presented above? Please say > so, > because I can't figure it out! > > > Regards, > Matteo > > [1] https://bitbucket.org/iamFIREcracker/logfilter > > -- > http://www.matteolandi.net Bump -- http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter deadlock on graceful exit
On Thu, May 31, 2012 at 3:42 AM, Terry Reedy wrote: > On 5/30/2012 6:19 PM, Matteo Landi wrote: >> >> On May/28, Matteo Landi wrote: >>> >>> Hi list, >>> recently I started to work on an application [1] which makes use of the >>> Tkinter >>> module to handle interaction with the user. Simply put, the app is a >>> text >>> widget displaying a file filtered by given criteria, with a handy feature >>> that >>> the window is raised each time a new line is added to the widget. >>> >>> The application mainly consists of three threads: the first one, the >>> file >>> processor, reads the file, filters the lines of interest, and pushes them >>> into >>> a shared queue (henceforth `lines_queue`); the second one, the >>> gui_updater, >>> pops elements from `lines_queue`, and schedule GUI updates using the >>> `after_idle` method of the Tkinter module; finally the last one, the >>> worker >>> spawner, receives commands by the gui (by means of a shared queue, >>> `filters_queue`), and drives the application, terminating or spawning new >>> threads. >>> >>> For example, let's see what happens when you start the application, fill >>> the >>> filter entry and press Enter button: >>> 1 the associated even handler is scheduled (we should be inside the >>> Tkinter >>> mainloop thread), and the filter is pushed into `filters_queue`; >>> 2 the worker spawner receives the new filter, terminate a possibly >>> running >>> working thread, and once done, create a new file processor; >>> 3 the file processor actually processes the file and fills the >>> `lines_queue` >>> with the lines matching given filter; >>> 4 the gui updater schedules GUI updates as soon as items are pushed into >>> `lines_queue` >>> 5 Tkinter mainloop thread updates the gui when idle >>> >>> What happens when the main window is closed? Here is how I implemented >>> the >>> graceful shutdown of the app: >>> 1 a quit event is scheduled and a _special_ message is pushed into both >>> `filter_queue` and `lines_queue` >>> 2 the gui updater threads receives the _special_ message, and terminates >>> 3 the worker spawner receives the message, terminates the working thread >>> and >>> interrupts her execution. >>> 4 Tk.quit() is called after the quit event handler, and we finally quit >>> the >>> mainloop >>> >>> Honestly speaking, I see no issues with the algorithm presented above; >>> however, >>> if I close the window in the middle of updates of the text widget, the >>> applications hangs indefinitely. On the other hand, everything works as >>> expected if I close the app when the file processor, for example, is >>> waiting for >>> new content to filter. >>> >>> I put some logging messages to analyze the deadlock (?!), and noticed >>> that both >>> the worker spawner and the file processor are terminated correctly. The >>> only >>> thread still active for some strange reasons, is the gui updater. >>> >>> Do you see anything wrong with the description presented above? Please >>> say so, >>> because I can't figure it out! > > > Since no-one else answered, I will ask some questions based on little > tkinter experience, no thread experience, and a bit of python-list reading. > > 1. Are you only using tkinter in one thread? (It seems like so from the > above)? Yes, provided that `after_idle` queues a job for the gui thread > > 2. Is root.destroy getting called, as in 24.1.2.2. A Simple Hello World > Program in the most recent docs? (I specify 'most recent' because that > example has been recently revised because the previous version sometimes > left tkinter hanging for one of the code paths, perhaps similar to what you > describe. No, I'm not calling the destroy method of the main window but, why that only happens while doing gui updates? > > 3. Have you tried making the gui thread the master thread? (I somehow expect > that the gui thread should be the last to shut down.) No but, same question as above. I'm not home right now, so I will try those solutions as soon as possible. Thanks. Cheers, Matteo > > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list -- http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter deadlock on graceful exit
On Thu, May 31, 2012 at 3:02 PM, Matteo Landi wrote: > On Thu, May 31, 2012 at 3:42 AM, Terry Reedy wrote: >> On 5/30/2012 6:19 PM, Matteo Landi wrote: >>> >>> On May/28, Matteo Landi wrote: >>>> >>>> Hi list, >>>> recently I started to work on an application [1] which makes use of the >>>> Tkinter >>>> module to handle interaction with the user. Simply put, the app is a >>>> text >>>> widget displaying a file filtered by given criteria, with a handy feature >>>> that >>>> the window is raised each time a new line is added to the widget. >>>> >>>> The application mainly consists of three threads: the first one, the >>>> file >>>> processor, reads the file, filters the lines of interest, and pushes them >>>> into >>>> a shared queue (henceforth `lines_queue`); the second one, the >>>> gui_updater, >>>> pops elements from `lines_queue`, and schedule GUI updates using the >>>> `after_idle` method of the Tkinter module; finally the last one, the >>>> worker >>>> spawner, receives commands by the gui (by means of a shared queue, >>>> `filters_queue`), and drives the application, terminating or spawning new >>>> threads. >>>> >>>> For example, let's see what happens when you start the application, fill >>>> the >>>> filter entry and press Enter button: >>>> 1 the associated even handler is scheduled (we should be inside the >>>> Tkinter >>>> mainloop thread), and the filter is pushed into `filters_queue`; >>>> 2 the worker spawner receives the new filter, terminate a possibly >>>> running >>>> working thread, and once done, create a new file processor; >>>> 3 the file processor actually processes the file and fills the >>>> `lines_queue` >>>> with the lines matching given filter; >>>> 4 the gui updater schedules GUI updates as soon as items are pushed into >>>> `lines_queue` >>>> 5 Tkinter mainloop thread updates the gui when idle >>>> >>>> What happens when the main window is closed? Here is how I implemented >>>> the >>>> graceful shutdown of the app: >>>> 1 a quit event is scheduled and a _special_ message is pushed into both >>>> `filter_queue` and `lines_queue` >>>> 2 the gui updater threads receives the _special_ message, and terminates >>>> 3 the worker spawner receives the message, terminates the working thread >>>> and >>>> interrupts her execution. >>>> 4 Tk.quit() is called after the quit event handler, and we finally quit >>>> the >>>> mainloop >>>> >>>> Honestly speaking, I see no issues with the algorithm presented above; >>>> however, >>>> if I close the window in the middle of updates of the text widget, the >>>> applications hangs indefinitely. On the other hand, everything works as >>>> expected if I close the app when the file processor, for example, is >>>> waiting for >>>> new content to filter. >>>> >>>> I put some logging messages to analyze the deadlock (?!), and noticed >>>> that both >>>> the worker spawner and the file processor are terminated correctly. The >>>> only >>>> thread still active for some strange reasons, is the gui updater. >>>> >>>> Do you see anything wrong with the description presented above? Please >>>> say so, >>>> because I can't figure it out! >> >> >> Since no-one else answered, I will ask some questions based on little >> tkinter experience, no thread experience, and a bit of python-list reading. >> >> 1. Are you only using tkinter in one thread? (It seems like so from the >> above)? > > Yes, provided that `after_idle` queues a job for the gui thread > >> >> 2. Is root.destroy getting called, as in 24.1.2.2. A Simple Hello World >> Program in the most recent docs? (I specify 'most recent' because that >> example has been recently revised because the previous version sometimes >> left tkinter hanging for one of the code paths, perhaps similar to what you >> describe. > > No, I'm not calling the destroy method of the main window but, why > that only happens while doing gui updates? > >> >> 3. Have you tried making the gui thread the master thread? (I somehow expect &
Re: Tkinter deadlock on graceful exit
On Jun/01, Matteo Landi wrote: > On Thu, May 31, 2012 at 3:02 PM, Matteo Landi wrote: > > On Thu, May 31, 2012 at 3:42 AM, Terry Reedy wrote: > >> On 5/30/2012 6:19 PM, Matteo Landi wrote: > >>> > >>> On May/28, Matteo Landi wrote: > >>>> > >>>> Hi list, > >>>> recently I started to work on an application [1] which makes use of the > >>>> Tkinter > >>>> module to handle interaction with the user. Simply put, the app is a > >>>> text > >>>> widget displaying a file filtered by given criteria, with a handy feature > >>>> that > >>>> the window is raised each time a new line is added to the widget. > >>>> > >>>> The application mainly consists of three threads: the first one, the > >>>> file > >>>> processor, reads the file, filters the lines of interest, and pushes them > >>>> into > >>>> a shared queue (henceforth `lines_queue`); the second one, the > >>>> gui_updater, > >>>> pops elements from `lines_queue`, and schedule GUI updates using the > >>>> `after_idle` method of the Tkinter module; finally the last one, the > >>>> worker > >>>> spawner, receives commands by the gui (by means of a shared queue, > >>>> `filters_queue`), and drives the application, terminating or spawning new > >>>> threads. > >>>> > >>>> For example, let's see what happens when you start the application, fill > >>>> the > >>>> filter entry and press Enter button: > >>>> 1 the associated even handler is scheduled (we should be inside the > >>>> Tkinter > >>>> mainloop thread), and the filter is pushed into `filters_queue`; > >>>> 2 the worker spawner receives the new filter, terminate a possibly > >>>> running > >>>> working thread, and once done, create a new file processor; > >>>> 3 the file processor actually processes the file and fills the > >>>> `lines_queue` > >>>> with the lines matching given filter; > >>>> 4 the gui updater schedules GUI updates as soon as items are pushed into > >>>> `lines_queue` > >>>> 5 Tkinter mainloop thread updates the gui when idle > >>>> > >>>> What happens when the main window is closed? Here is how I implemented > >>>> the > >>>> graceful shutdown of the app: > >>>> 1 a quit event is scheduled and a _special_ message is pushed into both > >>>> `filter_queue` and `lines_queue` > >>>> 2 the gui updater threads receives the _special_ message, and terminates > >>>> 3 the worker spawner receives the message, terminates the working thread > >>>> and > >>>> interrupts her execution. > >>>> 4 Tk.quit() is called after the quit event handler, and we finally quit > >>>> the > >>>> mainloop > >>>> > >>>> Honestly speaking, I see no issues with the algorithm presented above; > >>>> however, > >>>> if I close the window in the middle of updates of the text widget, the > >>>> applications hangs indefinitely. On the other hand, everything works as > >>>> expected if I close the app when the file processor, for example, is > >>>> waiting for > >>>> new content to filter. > >>>> > >>>> I put some logging messages to analyze the deadlock (?!), and noticed > >>>> that both > >>>> the worker spawner and the file processor are terminated correctly. The > >>>> only > >>>> thread still active for some strange reasons, is the gui updater. > >>>> > >>>> Do you see anything wrong with the description presented above? Please > >>>> say so, > >>>> because I can't figure it out! > >> > >> > >> Since no-one else answered, I will ask some questions based on little > >> tkinter experience, no thread experience, and a bit of python-list reading. > >> > >> 1. Are you only using tkinter in one thread? (It seems like so from the > >> above)? > > > > Yes, provided that `after_idle` queues a job for the gui thread > > > >> > >> 2. Is root.destroy getting called, as in 24.1.2.2. A Simple Hello World > >> Program in the most recent doc
Re: Tkinter deadlock on graceful exit
On Jun/02, Dennis Lee Bieber wrote: > On Sat, 2 Jun 2012 14:57:17 +0200, Matteo Landi > declaimed the following in gmane.comp.python.general: > > > > Lesson learned: never invoke Tkinter functions / methods outside the > > mainloop > > thread.. NEVER! > > > > Typically, that advice would apply to ANY GUI library... > > The exception being operations that /add/ an event to the pending > unprocessed GUI events, thereby signaling the GUI to perform the desired > function. You right, definitely! I made a bit of GTK programming before, and if I remember correctly, there is a *thread-safe* function, `gobject.idle_add`, that let you queue functions to be executed by the main thread. For that reason, I thought it was safe to invoke `tk.after_idle` outside the mainloop's thread, but oviously I was wrong! > > If you want a head-ache... Try coding an application in which the > old GKS is used for the main data display (I needed display-list > capability with identified "segments" so I could "blank"/"show" > different aspects of the data -- without rerunning some 15-minute > process read months of data points) on top of a nearly raw X-Window > systems (xt and DECWindows). AND the graphics commands for the data to > be plotted were coming in as ASCII text over a VMS mailbox. Oh, and no > threading -- the graphics commands had to be processed a few at a time > via an xt idle event handler (if there were no GUI events, call my > handler to read a command from the mailbox, dispatch to the GKS > emulation of the command [the commands mapped to Ramtek 9300 graphics > engine -- the original implementation], and return to check for next > event). This was ca. 1990! > Nice, good job! > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/ > > -- > http://mail.python.org/mailman/listinfo/python-list Cheers, Matteo -- http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
osaic - create photo mosaics w/ Python
Hi list, yesterday I released a new version of "osaic", a Python library which enables users to create photo mosaics in a very simple way. Once installed, a bare ``python -mosaic IMG1 IMG2 IMG3 ..`` is enough to create and show on screen a mosaic where IMG2, IMG3 and others are combined together to mimic IMG1. The module is available for download on bitbucket [1] and PyPI [2]; on both you can find a README page where is explained the structure of the module and how users are supposed to work with it. I don’t want anyone of you to debug and spot bugs here and there all around the source code, unless you are really interested in it :P. Instead, I’m here looking for some feedback, suggestions or comments. I would really appreciate your advices. Thank you so much. Cheers, Matteo [1] http://bitbucket.org/iamFIREcracker/osaic [2] http://pypi.python.org/pypi/osaic/2.0.0 -- http://mail.python.org/mailman/listinfo/python-list
Re: Playing WAV file with Python
I imagine he is looking for a cross-platform solution: n this case, I guess the most suitable solution is pygame. Regards, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: else condition in list comprehension
Luis M. Gonzalez wrote: Hi there, I'd like to know if there is a way to add and else condition into a list comprehension. I'm sure that I read somewhere an easy way to do it, but I forgot it and now I can't find it... for example: z=[i+2 for i in range(10) if i%2==0] what if I want i to be "i-2" if i%2 is not equal to 0? You could use [(i-2, i+2)[bool(i%2 == 0)] for i in range(10)] or, in a less general but shorter way [(i+2, i-2)[i%2] for i in range(10)] or even [i%2 and i-2 or i+2 for i in range(10)] The "if" clause in comprehensions is used as a filter condition. -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
benchmarking with threading module
Hi, I'd like to know if it is possible to use the threading module to benchmark a web server. I'd like to write a python script which connect to a web server asking for a page (like a php page querying a mysql db). The script should launch many different queries to the web page at the same time. The plotting the time VS number of queries I'd like to understand which would be the critical number of simultaneous connection to the web server. Do you think that using the Threading module would be a good idea? Any other suggestion? Thank you very much Matteo Memelli -- http://mail.python.org/mailman/listinfo/python-list
Re: set of sets
Paolino wrote: > I thought rewriting __hash__ should be enough to avoid mutables problem > but: > > class H(set): > def __hash__(self) > return id(self) > > s=H() > > f=set() > > f.add(s) > f.remove(s) > > the add succeeds > the remove fails eventually not calling hash(s). Why don't you just use "frozenset"? -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: set of sets
Paolo Veronelli wrote: > And mostly with sets remove operation expense should be sublinear or am > I wrong? > Is this fast as with lists? It's faster then with lists... in sets, as with dicts, remove is on average O(1). > Obviously if I use the ids as hash value nothing is guaranted about the > objects contents to be unique but I don't care. > My work is a self organizing net,in which the nodes keep a structure to > link other nodes.As the nature of the net,the links are moved frequently > so remove and add operations and contains query should be optimized. > Why objects need to be hashable for this? Isn't __hash__ there to solve > the problem? The idea of a set of mutable sets looks a bit odd to me... I don't get why the outer container should be a set, since you don't care about uniqueness... if you are representing a graph (which seems the case to me), I'd use an identifier for each node, and a dictionary mapping node-ids to its adjacency set for each node. For instance, 0 <-- 1 --> 2 --> 3 | | v v 4 --> 5 would be represented as {0: set([]), 1: set([0, 2]), 2: set([2,4]), 3: set([5]), 4: set([5]), 5: set([])} If node ids are consecutive integers, you could also of course use a list as the outer structure. PS: we could also discuss this in italian in it.comp.lang.python :) -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: set of sets
Paolo Veronelli wrote: > Yes this is really strange. > > from sets import Set > class H(Set): > def __hash__(self): > return id(self) > > s=H() > f=set() #or f=Set() > > f.add(s) > f.remove(s) > > No errors. > > So we had a working implementation of sets in the library an put a > broken one in the __builtins__ :( > > Should I consider it a bug ? Looks like the builtin "set" implicitly converts sets arguments to remove to frozensets. That way, remove looks for "frozenset()" instead of "H()", so it won't work. Doesn't look like a documented behaviour to me, though. -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: returning True, False or None
nghoffma wrote: sorry, that should have been: py>>import sets py>>def doit(thelist): ... s = sets.Set(thelist) ... if s == sets.Set([None]): ... return None ... else: ... return max(s - sets.Set([None])) Since a function that doesn't return is equivalent to one that returns None, you can write it as: >>> def doit(lst): ... s = set(lst) - set([None]) ... if s: return max(s) that looks to me as the most elegant so far, but this is just because it's mine :-) You can also filter out Nones with a list/generator comprehension, but sets are just more elegant... -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: Pre-PEP: Dictionary accumulator methods
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty def appendlist(self, key, *values): try: self[key].extend(values) except KeyError: self[key] = list(values) They look as a special-case to me. They don't solve the problem for lists of sets or lists of deques for instance, not to mention other possible user-defined containers. defaultdicts look to me as a solution that is more elegant and solves more problems. What is the problem with them? -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: Pre-PEP: Dictionary accumulator methods - typing & initialising
Kay Schluehr wrote: Why do You set d.defaultValue(0) d.defaultValue(function=list) but not d.defaultValue(0) d.defaultValue([]) ? I think that's because you have to instantiate a different object for each different key. Otherwise, you would instantiate just one list as a default value for *all* default values. In other words, given: class DefDict(dict): def __init__(self, default): self.default = default def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: return self.default you'll get In [12]: d = DefDict([]) In [13]: d[42].extend(['foo']) In [14]: d.default Out[14]: ['foo'] In [15]: d[10].extend(['bar']) In [16]: d.default Out[16]: ['foo', 'bar'] In [17]: d[10] Out[17]: ['foo', 'bar'] In [18]: d[10] is d.default Out[18]: True and this isn't what you really wanted. By the way, to really work, I think that Duncan's proposal should create new objects when you try to access them, and to me it seems a bit counterintuitive. Nevertheless, I'm +0 on it. And why not dict(type=int), dict(type=list) instead where default values are instantiated during object creation? A consistent pythonic handling of all types should be envisioned not some ad hoc solutions that go deprecated two Python releases later. I don't really understand you. What should 'type' return? A callable that returns a new default value? That's exactly what Duncan proposed with the "function" keyword argument. -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: Pre-PEP: Dictionary accumulator methods - typing & initialising
Kay Schluehr wrote: I think that's because you have to instantiate a different object for each different key. Otherwise, you would instantiate just one list as a default value for *all* default values. Or the default value will be copied, which is not very hard either or type(self._default)() will be called. This is all equivalent and it does not matter ( except for performance reasons ) which way to go as long only one is selected. I don't like it very much... it seems too implicit to be pythonic. Also, it won't work with non-copyable objects, and type(42)() = 0, and getting 0 when the default is 42 looks very strange. I prefer the explicit "give me a callable" approach. If the dict has a fixed semantics by applying defaultValue() and it returns defaults instead of exceptions whenever a key is missing i.e. behavioural invariance the client of the dict has nothing to worry about, hasn't he? For idioms like d[foo].append('blah') to work properly, you'd have to set the default value every time you access a variable. It can be really strange to fill up memory only by apparently accessing values. I suspect the proposal really makes sense only if the dict-values are of the same type. Filling it with strings, custom objects and other stuff and receiving 0 or [] or '' if a key is missing would be a surprise - at least for me. Instantiating dict the way I proposed indicates type-guards! This is the reason why I want to delay this issue and discuss it in a broader context. But I'm also undecided. Guidos Python-3000 musings are in danger to become vaporware. "Now is better then never"... Therefore +0. Having duck-typing, we can have things that have common interface but no common type. For instance, iterables. I can imagine a list of iterables of different types, and a default value of maybe [] or set([]). -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: For loop extended syntax
George Sakkis wrote: I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for in" syntax so that X can include default arguments ? This would be very useful for list/generator comprehensions, for example being able to write something like: [x*y-z for (x,y,z=0) in (1,2,3), (4,5), (6,7,8)] instead of the less elegant explicit loop version that has to check for the length of each sequence. What do you think ? How did you get the data in that format in the first place? It looks a bit strange to me. Wouldn't it be easier to fill in default values when you gather data as opposed to when you use it? -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Cookbook, 2'nd. Edition is published
Larry Bates wrote: I received my copy on Friday (because I was a contributor). I wanted to thank Alex, Anna, and David for taking the time to put this together. I think it is a GREAT resource, especially for beginners. This should be required reading for anyone that is serous about learning Python. +1. The Python Cookbook is really great, and being included in the contributors, even if for a little tiny idea that got heavily refactored, feels wonderful. I'm really grateful to the python community. -- Ciao, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: How to create python web framework for ERP
Use the only odoo framework.. without addons .. will be the best choice .. do not reinvent the whell if you do not need web interface, you can have a look at http://www.tryton.org/ otherwise you could have flask for the top of flexibility regards, Matteo Il 09/09/2014 12:06, Vimal Rughani ha scritto: On Tuesday, 9 September 2014 14:09:48 UTC+5:30, Stéphane Wirtel wrote: On 9 Sep 2014, at 10:25, Vimal Rughani wrote: Hi All, Greetings ! I am bit familiar with Django and Python. I want to create ERP on python. Initially I feel Django will be good option for My Own ERP, but after working bit on that I feel it doesn't fit with my requirement. So I decided to create my own python based web framework for ERP. Can you please suggest me better book / video / resources / content which help me to build proper web framework for ERP. Thanks -- https://mail.python.org/mailman/listinfo/python-list Use Odoo. -- Stéphane Wirtel - http://wirtel.be - @matrixise Thanks Stéphane, for your suggestion. I know about Odoo and Odoo is good choice for domain related to Business. I am developing solution for Education / schools / colleges. Requirements are bit complex so I want to have own framework. -- https://mail.python.org/mailman/listinfo/python-list
Losing com pointer
Hi all, I need some help to a com problem.. I got this class: class foo(object): def setComObject(comObject): self.comO=comObject #This is a com object from a cad application def showForm(self) # use the self.comO to read some information from the cad application # Show the pyqt form as child of cad application # do somthing with the form # do somthing with the self.comO <- Here if pass some time I'm not able to call any method to the com object a=foo() o="get istance of a cad application via com" a.setComObject(o) a.showForm() #< here if pass some time I'm not able to call any method to the com object but I I' re call the a.setComObject(o) #Faster and I take less the 30 seconds on the form object it works well It seems a problem of the garbage collector .. but I'm not sure how to debug it .. any help is really appreciated.. it's the last dangerous bug in our application ... Regards, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: Insert trusted timestamp to PDF
have a look at: http://www.boscolini.eu/Boscolini/index.php?option=com_content&view=article&id=64%3Anumbering-pdf-file-in-python&catid=38%3Aprogramming&Itemid=55&lang=en <http://www.boscolini.eu/Boscolini/index.php?option=com_content&view=article&id=64%3Anumbering-pdf-file-in-python&catid=38%3Aprogramming&Itemid=55&lang=en> here I number some pdf files .. regards, Matteo Il 07/12/2011 20:41, Hegedüs, Ervin ha scritto: Hello Everyone, I'm looking for a tool, which can add a trusted timestamp to an existing PDF file (and can sign - but currently only have to add TS). Could anybody help? Thanks: a. -- http://mail.python.org/mailman/listinfo/python-list
Re: while True or while 1
Probably because of the fact it is possible to set True equal to False and consequently then invalidate loop logic as presented below: True = False while True: ... On the other hand `1' will always be evaluated as a constant. Don't know, just guessing. Matteo On Jan/21, Andrea Crotti wrote: > I see sometimes in other people code "while 1" instead of "while True". > I think using True is more pythonic, but I wanted to check if there is > any difference in practice. > > So I tried to do the following, and the result is surprising. For what > I can see it looks like the interpreter can optimize away the 1 boolean > conversion while it doesn't with the True, the opposite of what I > supposed. > > Anyone can explain me why is that, or maybe is my conclusion wrong? > > def f1(): > while 1: > pass > > def f2(): > while True: > pass > > In [10]: dis.dis(f) > 2 0 SETUP_LOOP 3 (to 6) > > 3 >>3 JUMP_ABSOLUTE3 > >>6 LOAD_CONST 0 (None) > 9 RETURN_VALUE > > In [9]: dis.dis(f1) > 2 0 SETUP_LOOP 10 (to 13) > >>3 LOAD_GLOBAL 0 (True) > 6 POP_JUMP_IF_FALSE 12 > > 3 9 JUMP_ABSOLUTE3 > >> 12 POP_BLOCK > >> 13 LOAD_CONST 0 (None) > 16 RETURN_VALUE > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Import Question
Il 20/02/2013 21:53, eli m ha scritto: How long does it take for the program to import something? I am asking this because i have like 7 imports at the beginning of my program and i am thinking thats the reason why it is slow to start up. Thanks in advance. It depend of your code module code.. if inside your module there is some code (no def or class) this code will be executed, and if for example you have some loop or some db query this will be executed too. regards, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: 2-D drawing/map with python
Il 12/03/2013 16:58, Huseyin Emre Guner ha scritto: Hello, I am newbie in Python. I would like to make a project using python. The main ideo of this project is that a user enters the x,y values to the Gui(PyQt or Gtk) and then a 2-D map is plotted due to the x,y values. First, I use Pygame commands "(pygame.draw.line(window, (255, 255, 255), (10, 10), (200, 400))" However I could not establish a gui with pygame. Now I would like to use the PyQt. Could you please give me advice for this project? have a look at : http://sourceforge.net/projects/pythoncad/ it's also integrated with sympy .. regards, Matteo -- http://mail.python.org/mailman/listinfo/python-list
Re: help to code...
The error is self expleined .. print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' *'+ * this line have a + at the end,the interpreter need something to add .. so remove it and it will work regards, Matteo Il 02/05/2013 15:50, leonardo selmi ha scritto: dear python community, i wrote the following program: from datetime import datetime now = datetime.now() current_month = now.month current_day = now.day current_year = now.year current_hour = now.hour current_minute = now.minute current_second = now.second print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ print str(current_hour) + str(current_minute) + str(current_second) and i got this error: Traceback (most recent call last): File "runner.py", line 125, in compilecode File "python", line 9 print str(current_month) + '/' + str(current_day) + '/' + str(current_year) +' '+ ^ SyntaxError: invalid syntax how can i write the last two lines correctly? thanks! -- http://mail.python.org/mailman/listinfo/python-list
ANNOUNCE: PyQt3Support r1 - Python bindings for Qt3Support
PyQt3Support - Python bindings for Qt3Support http://www.develer.com/oss/PyQt3Support What is this? PyQt3Support is an extension to PyQt4 that adds bindings to Qt's Qt3Support library for usage from the Python language. This is very helpful to migrate existing PyQt3 applications to PyQt4. Why? Porting from Qt3 to Qt4 can be tedious and bug-prone. For C++ programmers, Trolltech provides a library, called Qt3Support, that immensely helps. With Qt3Support, a C++ programmer basically only needs mechanical changes to your source code. The library is made of two different parts: * A new family of widgets (Q3*) with the same API of Qt3. * New member functions (or overloads) within standard Qt4 widgets. For Python programmers, the situation is worse: PyQt4 does not bind Qt3Support to Python. Developers of PyQt3 are forced to manually upgrade their code to PyQt4, class by class. This package fills the gap. By providing a new module PyQt4.Qt3Support, it enables PyQt3 developers to access Trolltech's migration library, and thus upgrade their code much easily and faster, with almost only mechanical changes. It's not a panacea of course: you probably still need minor manual adjustments and supervising, but it can still be of great help. Where? PyQt3Support has been developed and tested under both Windows (2000, XP, Vista) and Linux (Ubuntu, Fedora). License Qt3Support follows whatever license you have for PyQt3 and PyQt4, because its source code is machine-generated from PyQt3's and PyQt4's source code. Thus, Qt3Support can be freely used under both the GPL or the commercial license offered by Qt/PyQt producers. In case you are interested in developing Qt3Support itself, you want to know that the script that generates Qt3Support is released under the GPL license. Status PyQt3Support is not complete: it binds about 30% of the Qt3Support, but don't be fooled by this figure: it's the part that is probably used most in existing programs (more common widgets, constructors, ecc.). Moreover, since it is fully machine-generated, it is very easy to extend it to cover more classes and functions. See below as per how to contribute to the development. * PyQt3 ported classes: Q3VBox, Q3HBox, Q3Frame, Q3Grid, Q3Accel, Q3PopupMenu, Q3MenuData, Q3DockWindow, Q3DockArea, Q3ListView, Q3ScrollView, Q3ColorGroup, Q3Header, Q3ListBox, Q3StrList, Q3Table, Q3MemArray, Q3MainWindow, Q3ToolBar, Q3Action, Q3SimpleRichText, Q3StyleSheet, Q3Mime, Q3ComboBox, Q3GroupBox, Q3FileDialog, Q3Url, Q3WidgetStack, Q3HGroupBox, Q3VGroupBox, Q3IconView, Q3DragObject, Q3Picture, Q3ValueList, Q3CString, Q3ButtonGroup, Q3VButtonGroup * PyQt4 qt3supported classes: Gui.QBoxLayout, Core.QNamespace, Gui.QLCDNumber, Gui.QGridLayout, Gui.QApplication, Gui.QPushButton, OpenGL.QGLWidget, Core.QObject, Gui.QLabel, Gui.QPixmap, Core.QTextCodec, Gui.QToolButton, Gui.QTabWidget, Gui.QMenu, Core.QTimer, Gui.QLayout, Gui.QPalette, Gui.QMenuBar, Gui.QLineEdit, Gui.QDialog, Gui.QInputDialog, Gui.QCheckBox, Gui.QWidget, Gui.QTextEdit, Gui.QEvent, Gui.QSlider Download http://www.develer.com/oss/PyQt3Support -- Matteo Bertini - [EMAIL PROTECTED] Develer S.r.l. - http://www.develer.com Software Solutions -- http://mail.python.org/mailman/listinfo/python-list
string.count issue (i'm stupid?)
Hi all, i've noticed a strange beaviour of string.count: in my mind this code must work in this way: str = "a_a_a_a_" howmuch = str.count("_a_") print howmuch -> 3 but the count return only 2 Ok this can be fine, but why? The doc string tell that count will return the number of substring in the master string, if we spoke about substring i count 3 substring... Can someone explain me this? And in which way i can count all the occurrence of a substring in a master string? (yes all occurrence reusing already counter character if needed) Thanks a lot Matteo Rattotti www.rknet.it Powered by: - MacOsX - Gnu / Linux Debian Sarge - Amiga Os 3.9 - Milk -- http://mail.python.org/mailman/listinfo/python-list
Re: plot debugging problem
I imagine you have to create a separate thread for it. Just thoughts. On Tue, May 11, 2010 at 6:08 PM, Sandy Sandy wrote: > Hi friends > pls help with debugging problem > the mutter is: > during debugging the debug processes stacks when fig is created > for example, in code > > import random > > import matplotlib.pyplot as plt > from pylab import * > > > x= 23; > y = 11; > print(23456) > plt.plot(range(10)) > > plot([1,2,3]) > show() > > print() > > a=888 > > it is impossible after show() to continue debug > as stated in > Beginning Python Visualization - Crafting Visual Transformation Scripts > (2009) > page 187 > > Note If you’re not using matplotlib interactively in Python, be sure > to call the function show() after all > graphs have been generated, as it enters a user interface main loop > that will stop execution of the rest of > your code. The reason behind this behavior is that matplotlib is > designed to be embedded in a GUI as well. > In Windows, if you’re working from interactive Python, you need only > issue show() once; close the figures > (or figures) to return to the shell. Subsequent plots will be drawn > automatically without issuing show(), and > you’ll be able to plot graphs interactively. > > Best Regards > Sandy > ____ > Hotmail: Free, trusted and rich email service. Get it now. > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: plot debugging problem
Well, I cannot tell you how to do that in a precise way, but googling a bit I found this (expecially the second example): http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/ Take a look also at the Matplotlib cookbook: http://www.scipy.org/Cookbook/Matplotlib ps. when you answer back, remember to include the list, or the flow will be cut! On Tue, May 11, 2010 at 7:49 PM, Sandy Sandy wrote: > great!!! > how to do it? > > this way it is not working: > > from pylab import plot,show,close > x = range(10) > plot(x) > from threading import Timer > t = Timer(0, show) > t.start() > > y = [2, 8, 3, 9, 4] > plot(y) > close() > > Best Regards > Sandy > > > > > > > > > > > > > >> From: landima...@gmail.com >> Date: Tue, 11 May 2010 19:46:27 +0200 >> Subject: Re: plot debugging problem >> To: c...@live.com >> CC: python-list@python.org >> >> I imagine you have to create a separate thread for it. Just thoughts. >> >> On Tue, May 11, 2010 at 6:08 PM, Sandy Sandy wrote: >> > Hi friends >> > pls help with debugging problem >> > the mutter is: >> > during debugging the debug processes stacks when fig is created >> > for example, in code >> > >> > import random >> > >> > import matplotlib.pyplot as plt >> > from pylab import * >> > >> > >> > x= 23; >> > y = 11; >> > print(23456) >> > plt.plot(range(10)) >> > >> > plot([1,2,3]) >> > show() >> > >> > print() >> > >> > a=888 >> > >> > it is impossible after show() to continue debug >> > as stated in >> > Beginning Python Visualization - Crafting Visual Transformation Scripts >> > (2009) >> > page 187 >> > >> > Note If you’re not using matplotlib interactively in Python, be sure >> > to call the function show() after all >> > graphs have been generated, as it enters a user interface main loop >> > that will stop execution of the rest of >> > your code. The reason behind this behavior is that matplotlib is >> > designed to be embedded in a GUI as well. >> > In Windows, if you’re working from interactive Python, you need only >> > issue show() once; close the figures >> > (or figures) to return to the shell. Subsequent plots will be drawn >> > automatically without issuing show(), and >> > you’ll be able to plot graphs interactively. >> > >> > Best Regards >> > Sandy >> > >> > Hotmail: Free, trusted and rich email service. Get it now. >> > -- >> > http://mail.python.org/mailman/listinfo/python-list >> > >> > >> >> >> >> -- >> Matteo Landi >> http://www.matteolandi.net/ > > > Hotmail: Powerful Free email with security by Microsoft. Get it now. -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: plot debugging problem
On Tue, May 11, 2010 at 8:59 PM, Sandy Sandy wrote: > > 1 > > remember to include the list, > > what does it mean?? I mean that you are leaving out the mlist from your answers. Instead of press _reply_, look for a _reply-to-all_ button. > > 2 > > do you mean > > Pseudo Color Plots > > in > > http://www.scipy.org/Cookbook/Matplotlib > > ?? > > 3 > > could you pls clarify what to see in > > http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/ > > > http://www.scipy.org/Cookbook/Matplotlib > As I stated in the previous email, I have no experience with matplotlib, but in the first link I posted there is an example on how to integrate matplotlib within a wxpython application. You can start from it, and look how to attach plot within a gui mailoop. > S > > Well, I cannot tell you how to do that in a precise way, but googling > a bit I found this (expecially the second example): > > http://eli.thegreenplace.net/2008/08/01/matplotlib-with-wxpython-guis/ > > Take a look also at the Matplotlib cookbook: > > http://www.scipy.org/Cookbook/Matplotlib > > ps. when you answer back, remember to include the list, or the flow will be > cut! > > On Tue, May 11, 2010 at 7:49 PM, Sandy Sandy wrote: >> great!!! >> how to do it? >> >> this way it is not working: >> >> from pylab import plot,show,close >> x = range(10) >> plot(x) >> from threading import Timer >> t = Timer(0, show) >> t.start() >> >> y = [2, 8, 3, 9, 4] >> plot(y) >> close() >> >> Best Regards >> Sandy >> >> >> >> >> >> >> >> >> >> >> >> >> >>> From: landima...@gmail.com >>> Date: Tue, 11 May 2010 19:46:27 +0200 >>> Subject: Re: plot debugging problem >>> To: c...@live.com >>> CC: python-list@python.org >>> >>> I imagine you have to create a separate thread for it. Just thoughts. >>> >>> On Tue, May 11, 2010 at 6:08 PM, Sandy Sandy wrote: >>> > Hi friends >>> > pls help with debugging problem >>> > the mutter is: >>> > during debugging the debug processes stacks when fig is created >>> > for example, in code >>> > >>> > import random >>> > >>> > import matplotlib.pyplot as plt >>> > from pylab import * >>> > >>> > >>> > x= 23; >>> > y = 11; >>> > print(23456) >>> > plt.plot(range(10)) >>> > >>> > plot([1,2,3]) >>> > show() >>> > >>> > print() >>> > >>> > a=888 >>> > >>> > it is impossible after show() to continue debug >>> > as stated in >>> > Beginning Python Visualization - Crafting Visual Transformation Scripts >>> > (2009) >>> > page 187 >>> > >>> > Note If you’re not using matplotlib interactively in Python, be sure >>> > to call the function show() after all >>> > graphs have been generated, as it enters a user interface main loop >>> > that will stop execution of the rest of >>> > your code. The reason behind this behavior is that matplotlib is >>> > designed to be embedded in a GUI as well. >>> > In Windows, if you’re working from interactive Python, you need only >>> > issue show() once; close the figures >>> > (or figures) to return to the shell. Subsequent plots will be drawn >>> > automatically without issuing show(), and >>> > you’ll be able to plot graphs interactively. >>> > >>> > Best Regards >>> > Sandy >>> > >>> > Hotmail: Free, trusted and rich email service. Get it now. >>> > -- >>> > http://mail.python.org/mailman/listinfo/python-list >>> > >>> > >>> >>> >>> >>> -- >>> Matteo Landi >>> http://www.matteolandi.net/ >> >> >> Hotmail: Powerful Free email with security by Microsoft. Get it now. > > > > -- > Matteo Landi > http://www.matteolandi.net/ > > >> From: landima...@gmail.com >> Date: Tue, 11 May 2010 20:37:39 +0200 >> Subject: Re: plot debugging problem >> To: c...@live.com >> CC: python-list@python.org >> >> Well, I cannot tell you how to do that in a precise way, but googling >> a bit I found this (expeciall
Re: stopping execution window on error - newbie
Place a try .. except surrounding the body of your program, and finally call the input() function. On Thu, May 13, 2010 at 11:00 PM, a wrote: > I'm coding on an old windows laptop > > i write the code and double click the icon. it runs the program and > writes results to a window. > > when the code finishes, the window closes, i do a time.sleep(10) to > see what has happened. > > unfortunately when there is an error it just closes the window. > anyway of seeing the error messages? > > thanks > > a > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: MySQL, Python, NumPy and formatted read
I know anything about mysqldb and fetchone method, but it's easy to create a numpy array, given a tuple of data: >>> import numpy >>> >>> t = ('1', '2', '3') >>> numpy.array(t, int) array([1, 2, 3]) >>> I made the assumption that mysqldb.fetchone return a tuple of strings, so we need to create an array by specifying the type of the needed values. On Mon, May 24, 2010 at 12:30 AM, Ian Hoffman wrote: > Hello, > > I'm having significant Python difficulties (and I'm new to Python). > I'm trying to read BLOB ASCII (numerical) data from a MySQL database > using MySQLdb in a formatted fashion. The BLOB data is a sequence of > numbers separated by newlines (\n), like this: > 5 > 6 > 10 > 45 > etc. > > When I read the data using the fetchone() command I get a single > tuple. What I'd like is to somehow put the tuple into a NumPy array > with each value as one element. Then I can continue to do some > numerical processing. > > Any advice/help? > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: function that counts...
What about avoiding the string conversion and use mod/div operations in order to create a list of digits for a number? Now that you have the sequences it's easy to count which sums up to m. On Mon, May 24, 2010 at 4:14 PM, Raymond Hettinger wrote: > On May 19, 12:58 pm, superpollo wrote: >> ... how many positive integers less than n have digits that sum up to m: >> >> In [197]: def prttn(m, n): >> tot = 0 >> for i in range(n): >> s = str(i) >> sum = 0 >> for j in range(len(s)): >> sum += int(s[j]) >> if sum == m: >> tot += 1 >> return tot >> .: >> >> In [207]: prttn(25, 1) >> Out[207]: 348 >> >> any suggestion for pythonizin' it? > > Your code is readable and does the job just fine. > Not sure it is an improvement to reroll it into a one-liner: > > def prttn(m, n): >return sum(m == sum(map(int, str(x))) for x in range(n)) >>>> prttn(25, 1) > 348 > > Some people find the functional style easier to verify because of > fewer auxilliary variables and each step is testable independently. > The m==sum() part is not very transparent because it relies on > True==1, so it's only readable if it becomes a common idiom (which it > could when you're answering questions of the form "how many numbers > have property x"). > > If speed is important, the global lookups can be localized: > > def prttn(m, n, map=itertools.imap, int=int, str=str, range=range): >return sum(m == sum(map(int, str(x))) for x in range(n)) > > Raymond > > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax question
Anyway I suggest you to use a syntax like: >>>b = list(a) in order to copy a list, it should be better than slicing. On Wed, Jun 2, 2010 at 7:56 PM, geremy condra wrote: > On Wed, Jun 2, 2010 at 10:40 AM, pmz wrote: >> Dear Group, >> >> It's really rookie question, but I'm currently helping my wife in some >> python-cases, where I'm non-python developer and some of syntax-diffs >> make me a bit confused. >> >> Could anyone give some light on line, as following: >> "ds = d[:]" ### where 'd' is an array > > I'm guessing you mean that d is a list. The square > braces with the colon is python's slicing notation, > so if I say [1,2,3,4][0] I get a 1 back, and if I say > [1,2,3,4][1:4] I get [2,3,4]. Python also allows a > shorthand in slicing, which is that if the first index > is not provided, then it assumes 0, and that if the > second index is not provided, it assumes the end > of the list. Thus, [1,2,3,4][:2] would give me [1,2] > and [1,2,3,4][2:] would give me [3, 4]. Here, neither > has been provided, so the slice simply takes the > items in the list from beginning to end and returns > them- [1,2,3,4][:] gives [1,2,3,4]. > > The reason someone would want to do this is > because lists are mutable data structures. If you > fire up your terminal you can try the following > example: > >>>> a = [1,2,3,4] >>>> b = a >>>> c = [:] >>>> b[0] = 5 >>>> b > [5,2,3,4] >>>> # here's the issue >>>> a > [5,2,3,4] >>>> # and the resolution >>>> c > [1,2,3,4] > > Hope this helps. > > Geremy Condra > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax question
Yes it is; d[i:j] is equal to "give me the array from the d[i] to d[j - 1]", and if you omit i and j then the i and j are respectively assumed as 0 and len(d) - 1. On Wed, Jun 2, 2010 at 8:01 PM, pmz wrote: > On 2 Cze, 19:56, geremy condra wrote: >> On Wed, Jun 2, 2010 at 10:40 AM, pmz wrote: >> > Dear Group, >> >> > It's really rookie question, but I'm currently helping my wife in some >> > python-cases, where I'm non-python developer and some of syntax-diffs >> > make me a bit confused. >> >> > Could anyone give some light on line, as following: >> > "ds = d[:]" ### where 'd' is an array >> >> I'm guessing you mean that d is a list. The square >> braces with the colon is python's slicing notation, >> so if I say [1,2,3,4][0] I get a 1 back, and if I say >> [1,2,3,4][1:4] I get [2,3,4]. Python also allows a >> shorthand in slicing, which is that if the first index >> is not provided, then it assumes 0, and that if the >> second index is not provided, it assumes the end >> of the list. Thus, [1,2,3,4][:2] would give me [1,2] >> and [1,2,3,4][2:] would give me [3, 4]. Here, neither >> has been provided, so the slice simply takes the >> items in the list from beginning to end and returns >> them- [1,2,3,4][:] gives [1,2,3,4]. >> >> The reason someone would want to do this is >> because lists are mutable data structures. If you >> fire up your terminal you can try the following >> example: >> >> >>> a = [1,2,3,4] >> >>> b = a >> >>> c = [:] >> >>> b[0] = 5 >> >>> b >> [5,2,3,4] >> >>> # here's the issue >> >>> a >> [5,2,3,4] >> >>> # and the resolution >> >>> c >> >> [1,2,3,4] >> >> Hope this helps. >> >> Geremy Condra > > Thank you for such fast answer! I quite catch, but: > As I see, the d[:] is equal to sentence "get the d array from the > first to the last element"? :) > > P. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: List of lists surprising behaviour
Yes you are. List comprehension makes you create list of lists without reference-sharing. You should also find a recipe about that on the python cookbook. On Thu, Jun 17, 2010 at 12:21 PM, candide wrote: > Let's the following code : > >>>> t=[[0]*2]*3 >>>> t > [[0, 0], [0, 0], [0, 0]] >>>> t[0][0]=1 >>>> t > [[1, 0], [1, 0], [1, 0]] > > Rather surprising, isn't it ? So I suppose all the subarrays reférence the > same array : > >>>> id(t[0]), id(t[1]), id(t[2]) > (3077445996L, 3077445996L, 3077445996L) >>>> > > > So what is the right way to initialize to 0 a 2D array ? Is that way correct > : > > >>>> t=[[0 for _ in range(2)] for _ in range(3)] > > It seems there is no more trouble now : > >>>> t > [[0, 0], [0, 0], [0, 0]] >>>> t[0][0]=1 >>>> t > [[1, 0], [0, 0], [0, 0]] >>>> > > Correct ? > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Serializing functions
Some weeks ago, here on the mailing list I read about picloud[1], a python library used for cloud-computing; I was impressed by its simplicity, here is an example: >>>import cloud >>>def square(x): ... return x * x >>>cloud.call(square, 10) >>>cloud.result() 100 So, I tried to figure out how to achieve the same result, i.e. define a local generic function and send it somewhere for remote execution, and the get the result back. So I thought about serialization (pickle): I made a few tries and it seemed to work.. but I was wrong, because I never tried to load pickled data from another session different from the one used to pickle data itself. If you try and pickle a function, it is not pickled as a whole, indeed, once you unpickle it, it will raise an exception telling you that the target function was not found in the current module. So I'm here, with nothing in my hands; how would you implement this? Thanks in advance. [1] http://www.picloud.com/ -- Matteo Landi http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing functions
On Thu, 2010-06-17 at 07:37 -0700, Paul Rubin wrote: > Matteo Landi writes: > > If you try and pickle a function, it is not pickled as a whole, > > indeed, once you unpickle it, it will raise an exception telling you > > that the target function was not found in the current module. > > > > So I'm here, with nothing in my hands; how would you implement this? > > Use marshal rather than pickle. Note that requires both ends to be I could be wrong, but it seems functions are not marshable objects, is it right? > running the same python version. Be aware of the obvious security > issues of running code that came from remote machine, either because the > remote machine itself may be untrusted or compromised, or because the > network between the two machines may be compromised (remote machine is > being spoofed or commnications are being modified). -- Matteo Landi http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing functions
On Thu, 2010-06-17 at 07:37 -0700, Paul Rubin wrote: > Matteo Landi writes: > > If you try and pickle a function, it is not pickled as a whole, > > indeed, once you unpickle it, it will raise an exception telling you > > that the target function was not found in the current module. > > > > So I'm here, with nothing in my hands; how would you implement this? > > Use marshal rather than pickle. Note that requires both ends to be I could be wrong, but it seems functions are not marshable objects, is it right? > running the same python version. Be aware of the obvious security > issues of running code that came from remote machine, either because the > remote machine itself may be untrusted or compromised, or because the > network between the two machines may be compromised (remote machine is > being spoofed or commnications are being modified). -- Matteo Landi http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Serializing functions
On Thu, 2010-06-17 at 08:31 -0700, Stephen Hansen wrote: > On 6/17/10 6:23 AM, Matteo Landi wrote: > > itself. If you try and pickle a function, it is not pickled as a whole, > > indeed, once you unpickle it, it will raise an exception telling you > > that the target function was not found in the current module. > > You can pickle functions-- and classes, instances, and such-- just fine. > If you're having specific difficulty with one instance of doing so, show > the actual code. > > >>> def test(one): > ... print one + 1 > ... > >>> import pickle > >>> outp = pickle.dumps(test) > >>> outp > 'c__main__\ntest\np0\n.' > >>> fn = pickle.loads(outp) > >>> fn(1) > 2 > > Now, complex stuff like classes and functions have to "live" in the same > place with pickle, and it does need the source to be available and > already imported, but it works fine. > > If by "pickle a function" you mean, "take an arbitrary function and > pickle it into some random destination and have it by itself still be > enough to be executable", then no, pickle doesn't do that. To get that, > you'd have to distribute the source to the destination and import it > before-hand. This is the problem, and I excuse me if I ain't been clear enough while showing it inside my previous email. > > That or do some crazy-complicated sort of code object marshalling and > manipulation on the other side. I have no idea where to even begin in > that situation (hw do you turn a code object into something you can > actually pass arguments to, hmm? I only know how to 'exec' a bare code > object) > So it seems one should go for sending the function and the module with its definition in order to make the remote side able to execute it. I'm curious to see how picloud is able to discover the needed modules, sources etc etc to run the function specified inside picloud.call(); it seems not an easy task. -- Matteo Landi http://www.matteolandi.net -- http://mail.python.org/mailman/listinfo/python-list
Re: super() woes (n00b)
I found few error in your code: 1 the constructor of P class seems to be wrong: >>> class P(object): ...def __init__(self): ...print("I am a member of class P") ... 2 super() works with new style classes, i.e. the ones which inherit from 'object' >>> class P: ...def __init__(__class__,self): ...print("I am a member of class P") ... 3 super() need a type as first argument, and an instance as second one: >>> class C(P): ...def __init__(self): ...super().__init__(self) ...print("I am a member of class C") Now it should work (not tested). On Thu, Jun 17, 2010 at 6:53 PM, Deadly Dirk wrote: > On Thu, 17 Jun 2010 16:36:10 +, Deadly Dirk wrote: > >> I cannot get right the super() function: Python 3.1.1+ (r311:74480, Nov >> 2 2009, 14:49:22) [GCC 4.4.1] on linux2 >> Type "copyright", "credits" or "license()" for more information. No >> Subprocess >>>>> class P: >> def __init__(__class__,self): >> print("I am a member of class P") >> >> >>>>> class C(P): >> def __init__(self): >> super().__init__(self) >> print("I am a member of class C") >> >> >> >> class P: >> def __init__(self): >> print("I am a member of class P") >> >> class C(P): >> def __init__(self): >> super().__init__(self) >> print("I am a member of class C") >> >> x=C() >> >> That is more or less the text from the "Quick Python Book". What am I >> doing wrong? > > I tried this, too: > >>>> class C(P): > def __init__(self): > super(__class__).__init__(self) > print("I am a member of class C") > > >>>> x=C() > Traceback (most recent call last): > File "", line 1, in > x=C() > File "", line 3, in __init__ > super(__class__).__init__(self) > TypeError: must be type, not C >>>> > > > > -- > The missionaries go forth to Christianize the savages - > as if the savages weren't dangerous enough already. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: how to delete "\n"
I hope this could help: >>> f = open('powersave.sh') >>> map(lambda s: s.strip(), f.readlines()) ['echo 1 > /sys/module/snd_hda_intel/parameters/power_save', 'echo min_power > /sys/class/scsi_host/host0/link_power_management_policy', 'echo 1 > /sys/module/snd_hda_intel/parameters/power_save'] I know for sure someone else will address you to other better solutions :) On Mon, Jul 12, 2010 at 10:27 PM, Jia Hu wrote: > Hi, I just want to delete "\n" at each line. My operating system is ubuntu > 9.1. The code is as follows > > #!/usr/bin/python > import string > fileName=open('Direct_Irr.txt', 'r') # read file > directIrr = fileName.readlines() > fileName.close() > for line in directIrr: > line.rstrip('\n') > print directIrr > > But I found there is still "\n" . Could someone help me why it is not > correct? > > Thank you > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: measuring a function time
This should be enough >>>import time >>>tic = time.time() >>>function() >>>toc = time.time() >>>print toc - tic On Thu, Jul 29, 2010 at 2:34 PM, Mahmood Naderan wrote: > Hi, > I want to measure a function run time. I read > http://docs.python.org/library/time.html but I am confused and don't know > which one is suitable. I don't know is daylight timing important or not > or is Y2K issue important for my case or not I also don't know how epoch > time is related to my work. > > I just want to do this (pseudocode): > start_time = get_current_time; > function(); > end_time = get_current_time; > print (end_time - start_time) > > the output should be 7600 (s) for example. What is the best and easiest way > to do that? > > Thanks, > > // Naderan *Mahmood; > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: pylint scores
What are the messages one should really care about while evaluating its code using pylint? It's easy to get 5 scored with a "lot of public methods" or bad named variables such as 'x' or 'y' .. Have you got any config file to share? On Sat, Jul 31, 2010 at 2:48 AM, Dan Stromberg wrote: > > On Fri, Jul 30, 2010 at 12:18 PM, News123 wrote: >> >> On 07/30/2010 03:12 PM, wheres pythonmonks wrote: >> > I am starting to use pylint to look at my code and I see that it gives a >> > rating. >> > What values do experienced python programmers get on code not >> > targeting the benchmark? >> > >> > I wrote some code, tried to keep it under 80 characters per line, >> > reasonable variable names, and I got: >> > >> > 0.12 / 10. >> > >> > Is this a good score for one not targeting the benchmark? (pylint >> > running in default mode) >> > >> It's not a goodf core, but arrives easily if you never ran pylint before. >> With very little effort you should be able to be above 5 >> with a little more effort above 7 >> >> >> > Somewhat related: Is the backslash the only way to extend arguments >> > to statements over multiple lines? (e.g.) >> >> if you have an opening parenthesis, or bracked, then you don't need a >> backslash >> >> so instead of >> if longlonglonglonglonglonglonglongvar == \ >> otherlonglonglonglongvar: >> >> you could also write: >> >> if (longlonglonglonglonglonglonglongvar == >> otherlonglonglonglongvar): >> >> >> same works of course with asserts. >> >> > >> >>>> def f(x,y,z): return(x+y+z); >> > ... >> >>>> f(1,2, >> > ... 3) >> > 6 >> >>>> assert f(1,2,3)>0, >> > File "", line 1 >> > assert f(1,2,3)>0, >> > ^ >> > SyntaxError: invalid syntax >> >>>> >> > >> > In the above, I could split the arguments to f (I guess b/c of the >> > parens) but not for assert. I could use a backslash, but I find this >> > ugly -- it that my only (best?) option? >> > >> > [I really like to assert my code to correctness and I like using the >> > second argument to assert, but this resulted in a lot of long lines >> > that I was unable to break except with an ugly backslash.] >> > >> > W >> > IMO, the important thing about pylint's scoring is that it's but one way of > many of producing good Python code. However, it's also one of the easier > ways of producing good python code. > I personally like to get my scores up near 10, by annotating in comments > about the few things that pylint flags that I can't just code around. This > requires jumping through some slightly silly hoops (EG the previously > mentioned "too few public methods", which my various container classes > always trip over), but going through this process is worthwhile for > highlighting the hoops pylint can detect that -aren't- so silly. > The one thing I like to leave unfixed is FIXME's - otherwise my preference > would be to go for a score of 10 for production code. > I also like to create a ./this-pylint script for my various projects, that > have global overrides - things like identifier rules, line length, and... I > don't get blanks instead of tabs. Blanks are fine if you don't understand > tabs (or think someone in the future who doesn't understand tabs will need > to work on your code), but tabs allow everyone to see code indented the way > -they- want to see it, not just the way the original author wanted to see > it. > This script (./this-pylint) will also save output from the test in a text > file, for make (or other dependency handling program) to use to avoid > re-pylint'ing unmodified code. It'll give an error typically, if pytlint > detects any errors other than FIXME's (excluding ones, as I mentioned > before, that have a comment disabling the warning, of course). > I'm more than a little sad that pylint doesn't seem to be moving to python 3 > in any big hurry. > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Normalizing A Vector
On Mon, Aug 2, 2010 at 1:50 AM, Terry Reedy wrote: > On 7/30/2010 7:46 AM, Lawrence D'Oliveiro wrote: >> >> Say a vector V is a tuple of 3 numbers, not all zero. You want to >> normalize >> it (scale all components by the same factor) so its magnitude is 1. >> >> The usual way is something like this: >> >> L = math.sqrt(V[0] * V[0] + V[1] * V[1] + V[2] * V[2]) >> V = (V[0] / L, V[1] / L, V[2] / L) >> >> What I don’t like is having that intermediate variable L leftover after >> the >> computation. > > So, instead of fooling around with error-prone, hard-to-type constructions, > just delete the damn thing! > > def L del L :P > > Compare those foolproof 5 chars to what at first did not work right and even > what did. And, as other said, in most real applications, you will normalize > in more than one place. In fact, you may well want a vlen function. > > def vlen(seq): return math.sqrt(sum(x*x for x in seq)) > > -- > Terry Jan Reedy > > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL
I don't think yours is a permitted conversion[1]. It seems that PIL supports xpm format only for reading, but I could be wrong. Regards. [1] http://www.daniweb.com/forums/thread260074.html On Tue, Aug 3, 2010 at 10:45 AM, Navid Parvini wrote: > Dear All, > > I want to convert a .png file to .xpm using PIL. I used the following > command: > Image.open( "t1.png").save("a1.xpm"). But it doesn't work and I could not > convert it. > > Would you please help me and let me know that how can I convert/save .xpm > files in PIL. > > Thank you in advance. > > Thanks. > Navid > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Get name of file from directory into variable
I suggest you to take a look at walk function inside the os module [1]; IIRC, on the list you would find a discussion on how to create a wrapper for os.walk with support for filters or wildcards. Regards. [1] http://docs.python.org/library/os.html?highlight=os.walk#os.walk On Tue, Aug 3, 2010 at 12:21 PM, loial wrote: > In a unix shell script I can do something like this to look in a > directory and get the name of a file or files into a variable : > > MYFILE=`ls /home/mydir/JOHN*.xml` > > > Can I do this in one line in python? > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Get name of file from directory into variable
Ops I miss the "one line" request, so my previous answer was definitely OT. glob seems to be your solution. On Tue, Aug 3, 2010 at 12:44 PM, Rory Campbell-Lange wrote: > On 03/08/10, Alex Willmer (a...@moreati.org.uk) wrote: >> On Aug 3, 11:21?am, loial wrote: >> > In a unix shell script I can do something like this to look in a >> > directory and get the name of a file or files into a variable : >> > >> > MYFILE=`ls /home/mydir/JOHN*.xml` >> > >> > Can I do this in one line in python? >> >> import glob >> my_files = glob.glob('/home/mydir/JOHN*.xml') > > import os; my_files = [f for f in os.listdir('/home/mydir/') if 'JOHN' in f > and 'xml' in f] > > But in fact glob uses os.listdir and fnmatch.fnmatch functions > internally, so is definitely the way to go. > > http://docs.python.org/library/glob.html > > -- > Rory Campbell-Lange > r...@campbell-lange.net > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Global variables problem
Usually, modify global variables in a multi-thread/multi-process scenario is not the right to operate: you better re-implement your solution in a way that the shared resource is either protected with synchronized objects or accessed by a single thread/process (and in this case, it won't be a shared resource anymore). Think about the the needs of the shared resources: in this extremely simple example, you can see that the increment of the session number could be done by the server before spawning the child, but obviously this is not a real scenario. If you can't give up with shared resources, I recommend you to create a synchronized object owned by the server but shared with the children (take a look at the args keywords of the Process constructor). Regards. On Wed, Aug 4, 2010 at 9:47 AM, Navkirat Singh wrote: > : ( > False alarm, the earlier solution breaks multiprocessing. Whats happening > here is the child needs to change a variable in the parent process, So I > think I am looking at shared memory (maybe). Any suggestions? > Regards, > Nav > > > On 04-Aug-2010, at 12:41 PM, Navkirat Singh wrote: > > Thanks a lot guys !! > I solved the problem: > In the lines: >>> >>> new_process = process(target=newprocess) >>> new_process.start() > > The target=newprocess is pointing towards a variable, instead of a function. > So, appending a () will make it goto that function, thereby changing the > global variable : ) > Thanks, > Nav > > On 04-Aug-2010, at 11:42 AM, Daniel da Silva wrote: > > Your problem lies somewhere in the use of the Process class, not with global > variables. > > If you replace your "p = ..." and "p.start()" lines with a direct call to > self.handle_connection(), your code works as expected. I don't know much > about the multiprocessing module, so I can't really comment on what you're > doing wrong, but I hope this points you in the right direction. > > Sorry I couldn't be of more help, > > Daniel > > > On Tue, Aug 3, 2010 at 9:48 PM, Navkirat Singh wrote: >> >> On 04-Aug-2010, at 9:46 AM, Daniel da Silva wrote: >> >> Please post approximate code that actually works and displays the problem. >> >> On Tue, Aug 3, 2010 at 9:00 PM, Navkirat Singh >> wrote: >>> >>> Hey guys, >>> >>> I am using a multiprocessing program, where the new process is supposed >>> to change a variable in the main class that it branches out from. This is >>> somehow not working, following is an approximate code. Would really >>> appreciate any insight into this matter: >>> >>> >>> var = {} >>> >>> class Something(): >>> >>> def set_var(self): >>> global var >>> var = somevalue >>> >>> def get_var(self): >>> return var >>> >>> def newprocess(self): >>> self.set_var() >>> >>> def do_multiprocessing(self): >>> while true: >>> self.get_var() >>> new_process = process(target=newprocess) >>> new_process.start() >>> >>> >>> I am really confused here ! >>> >>> Any help would be awesome : ) >>> >>> Regards, >>> Nav >>> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> >> This is a working code, streamlined, but it is where the problem is: >> from multiprocessing import * >> dicts = 0 >> print('global ', dicts) >> class WebServer(): >> def set_sessionInfo(self): >> global dicts >> dicts = dicts + 1 >> def get_sessionInfo(self): >> return dicts >> def handle_connection(self): >> self.set_sessionInfo() >> def serve_forever(self): >> for x in range(10): >> p = Process(target=self.handle_connection) >> p.start() >> print(self.get_sessionInfo()) >> ws = WebServer() >> ws.serve_forever() >> print(dicts) >> > > > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Script Cannot Write to Directory
On Wed, Aug 4, 2010 at 9:27 AM, Chris Rebert wrote: > On Wed, Aug 4, 2010 at 12:21 AM, News123 wrote: > >> 3.) try following python >> >> import os >> print os.getcwd() >> import shutil >> shutil("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME") > > WTF; modules aren't callable. Typo? I suppose he/she would have written: shutil.copyfile("YOUR_SOURCE_FILE_NAME","DESTINATION_DIRECTORY/DSTNTN_FILE_NAME") Cheers. > > Cheers, > Chris > -- > http://blog.rebertia.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: pylint scores
On Sun, Aug 1, 2010 at 12:27 PM, News123 wrote: > Hi, > > > On 07/31/2010 11:04 AM, Matteo Landi wrote: >> What are the messages one should really care about while evaluating >> its code using pylint? It's easy to get 5 scored with a "lot of public >> methods" or bad named variables such as 'x' or 'y' .. Have you got any >> config file to share? > > > The most important ones are of course the errors. > Some of them might be false, but in our team we agreed, that no file is > allowed to report pylint errors. > This means > - just fixing errors (in most cases) > - rewriting code (in a few cases) > - masking errors with pylint directives in the source (some other few > errrors) > > > > > If you only want to see the errros, then just run > pylint -E filename.py > > Note: This is a rather new but very useful switch.. It doesn't exist > on Ubuntu 10.4's release pylint 0.19.0, but exists on pylint 0.21.1 > > > > Apart from that. You should discuss within your team, which > errors you'd like to have ignored and adapt the pylintrc. This > is a rather personal decision. > For one project we disiabled for example following warnings: > ## C0322 = "Operator not preceded by a space" > ## C0323 = "Operator not followed by a space" > ## C0324 = "Comma not followed by a space" > As we did not have time to rewrite all of the existing code, that > violated these rules. > We prefered to 'hide' these warnings in order to sett the more important > ones. > > On another new project however we did NOT comment therese rules > and decided, that all new code should follow these rules > > > We disabled some special global variables, which we'd like to have in > lower case > > const-rgx ==((specialvar)|(v_[a-z_][a-z0-9_]*)|([A-Z_][A-Z0-9_]*)|(__.*__))$ > > > you could also modify variables like > # Maximum number of attributes for a class (see R0902). > max-attributes=7 > > # Minimum number of public methods for a class (see R0903). > min-public-methods=2 > > # Maximum number of public methods for a class (see R0904). > max-public-methods=20 > > > For some graphics module functions for example we wanted to > be allowed to use variable names like x,y as they are > completely meaningful names for pixel coordinates. > > > so change the entry good-names > good-names=x,y,ex,Run,_ Thank you so much, these are very precious settings. > > > Hope, that this gave you some ideas > > > >> >> On Sat, Jul 31, 2010 at 2:48 AM, Dan Stromberg wrote: >>> >>> On Fri, Jul 30, 2010 at 12:18 PM, News123 wrote: >>>> >>>> On 07/30/2010 03:12 PM, wheres pythonmonks wrote: >>>>> I am starting to use pylint to look at my code and I see that it gives a >>>>> rating. >>>>> What values do experienced python programmers get on code not >>>>> targeting the benchmark? >>>>> >>>>> I wrote some code, tried to keep it under 80 characters per line, >>>>> reasonable variable names, and I got: >>>>> >>>>> 0.12 / 10. >>>>> >>>>> Is this a good score for one not targeting the benchmark? (pylint >>>>> running in default mode) >>>>> >>>> It's not a goodf core, but arrives easily if you never ran pylint before. >>>> With very little effort you should be able to be above 5 >>>> with a little more effort above 7 >>>> >>>> >>>>> Somewhat related: Is the backslash the only way to extend arguments >>>>> to statements over multiple lines? (e.g.) >>>> >>>> if you have an opening parenthesis, or bracked, then you don't need a >>>> backslash >>>> >>>> so instead of >>>> if longlonglonglonglonglonglonglongvar == \ >>>> otherlonglonglonglongvar: >>>> >>>> you could also write: >>>> >>>> if (longlonglonglonglonglonglonglongvar == >>>> otherlonglonglonglongvar): >>>> >>>> >>>> same works of course with asserts. >>>> >>>>> >>>>>>>> def f(x,y,z): return(x+y+z); >>>>> ... >>>>>>>> f(1,2, >>>>> ... 3) >>>>> 6 >>>>>>>> assert f(1,2,3)>0, >>>>> File "", line 1 >>>>> assert f(1,2,3)>0, >>>>>
Re: Python Developer - HFT Trading firm - Chicago, IL
Hi Rich, I think it's better for you to post the message here ( http://www.python.org/community/jobs/ ). Regards, On Tue, Aug 17, 2010 at 6:07 PM, Rich Moss wrote: > > Python developer needed for math/trading applications and research at > leading HFT firm. The person we are searching for will have a strong > background with python programming and the ability to work with very > large historical datasets. You should have a very strong math > background as well. This can involve writing very complicated python > scripts and programs! You will work very closely with traders and > quantitative analysts in their equities trading group on state-of-the- > art trading strategy and execution systems. > > Requires: > > Strong python programming experience developing applications and > scripts using complex regular expressions > > Strong math knowledge and education > Experience working with massive datatsets/historical data > > This company is a top-tier electronic, algorithmic trading firm, > located in Chicago, IL. This firm is one of the most advanced high > frequency electronic trading firms in the world and uses python > throughout the company, as well as other languages. This firm has a > culture that rewards creativity and hard work. No third parties, > please. We will not consider candidates from outside the USA. No > telecommuting. We offer very generous compensation (best in the > industry), fantastic benefits and very generous relocation packages. > Please contact me immediately with a resume! > > Send resumes to: > > Rich Moss > r...@mossltd.com > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: palindrome iteration
> Yes, this is a correct observation, but it is not necessary to compare > the halves; Simply compare the complete string with its reverse. If > they match, it is a palindrome. I've always used to implement the is_palindrome function as you suggest, i.e. comparing the original string with the reverse one, but while reading, I tought about a imho nicer version which prevent from creating another string. Here are both the recursive/iterative versions of the function: def palindrome(str, i=0, j=-1): try: if str[i] == str[j]: return palindrome(str, i + 1, j - 1) return False except IndexError: return True def palindrome(str, i=0, j=-1): try: while True: if str[i] != str[j]: return False i, j = i + 1, j - 1 return True except IndexError: return True Regards, Matteo > >> Here's a possible (and a >> bit tricky) Python 2.x implementation: >> >> def is_palindrom(s): >> s = s.lower() >> slen = len(s) >> until = slen / 2 # Python 2x integer division >> offset = int(not(slen % 2)) >> runtil = until - offset >> return s[0:until] == s[-1:runtil:-1] >> >> > > At first glance this seems to be correct, but it is tricky indeed. > Particularly the assignment of the offset variable, casting a bool to > an integer of a negated expression. Given that Baba notes that this is > a beginners level query, it wouldn't have hurt to be a little bit more > verbose there. > > Richard > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Fibonacci: How to think recursively
I suggest you to memoize results in order to prevent overlapping recursion. Regards, Matteo On Sun, Aug 29, 2010 at 2:23 AM, Ben Finney wrote: > Baba writes: > >> my brainstorming so far brought me to a stand still as i can't seem to >> imagine a recursive way to code this: >> >> my attempted rough code: >> >> def fibonacci(n): >> # base case: >> result = fibonacci (n-1) + fibonacci (n-2) >> >> this will end up in a mess as it will create overlapping recursions > > It also never returns anything (which, in Python, means it returns the > None object). > > Worse, it will endlessly recurse; every time it's called it will call > itself (twice). > > Perhaps a way to approach the problem is: How will your function know > when *not* to call itself? What will it do instead? Try writing that > case first, and then write the rest of it on that basis. > > -- > \ “Science is a way of trying not to fool yourself. The first | > `\ principle is that you must not fool yourself, and you are the | > _o__) easiest person to fool.” —Richard P. Feynman, 1964 | > Ben Finney > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: palindrome iteration
Well, I tried the also the solution posted above (recursive w/o slicing and iterative), and I discovered they were the slowest.. is_palindrome_recursive 2.68151649808 is_palindrome_slice 0.44510699381 is_palindrome_list 1.93861944217 is_palindrome_reversed 3.28969831976 is_palindrome_recursive_no_slicing 6.78929775328 is_palindrome_iterative 4.88826141315 Nothing to say about the iterative function, but the benchmark of the recursive was unexpected, at least for my point of view: do you think it is due to the try/except overhead? On Sun, Aug 29, 2010 at 8:53 AM, Josh English wrote: > This whole conversation got interesting, so I thought I'd run some > speed tests: > > The code: > from timeit import Timer > > def is_palindrome_recursive(s): > if len(s) <= 1: > return True > if s[0] != s[-1]: > return False > else: > return is_palindrome(s[1:-1]) > > def is_palindrome_slice(s): > return s == s[::-1] > > def is_palindrome_list(s): > l = list(s) > l.reverse() > return s == ''.join(l) > > def is_palindrome_reversed(s): > return s == ''.join(reversed(s)) > > t = Timer("is_palindrome_recursive('madamimadam')", "from __main__ > import is_palindrome_recursive") > print "is_palindrome_recursive", min(t.repeat()) > > t = Timer("is_palindrome_slice('madamimadam')", "from __main__ import > is_palindrome_slice") > print "is_palindrome_slice", min(t.repeat()) > > t = Timer("is_palindrome_list('madamimadam')", "from __main__ import > is_palindrome_list") > print "is_palindrome_list", min(t.repeat()) > > t = Timer("is_palindrome_reversed('madamimadam')", "from __main__ > import is_palindrome_reversed") > print "is_palindrome_reversed", min(t.repeat()) > > The results: > is_palindrome_recursive 6.32680866827 > is_palindrome_slice 1.23618350114 > is_palindrome_list 4.60104846653 > is_palindrome_reversed 5.99355296513 > > The slice method is uglier, I have to admit, but it's the fastest of > these four on my machine. > > Josh > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: palindrome iteration
I thought they reached you. Here they are again: def palindrome(str, i=0, j=-1): try: if str[i] == str[j]: return palindrome(str, i + 1, j - 1) return False except IndexError: return True def palindrome(str, i=0, j=-1): try: while True: if str[i] != str[j]: return False i, j = i + 1, j - 1 return True except IndexError: return True On Sun, Aug 29, 2010 at 12:36 PM, Arnaud Delobelle wrote: > Matteo Landi writes: > >> Well, I tried the also the solution posted above (recursive w/o >> slicing and iterative), and I discovered they were the slowest.. >> >> is_palindrome_recursive 2.68151649808 >> is_palindrome_slice 0.44510699381 >> is_palindrome_list 1.93861944217 >> is_palindrome_reversed 3.28969831976 >> is_palindrome_recursive_no_slicing 6.78929775328 >> is_palindrome_iterative 4.88826141315 > > What are the last two functions? > > I suggest another: > > def is_palindrome(s): > return all(map(str.__eq__, s, reversed(s))) > > :) > >> Nothing to say about the iterative function, but the benchmark of the >> recursive was unexpected, at least for my point of view: do you think >> it is due to the try/except overhead? >> >> On Sun, Aug 29, 2010 at 8:53 AM, Josh English >> wrote: >>> This whole conversation got interesting, so I thought I'd run some >>> speed tests: >>> >>> The code: >>> from timeit import Timer >>> >>> def is_palindrome_recursive(s): >>> if len(s) <= 1: >>> return True >>> if s[0] != s[-1]: >>> return False >>> else: >>> return is_palindrome(s[1:-1]) > > This should be return is_palindrome_recursive(s[1:-1]). If this is > copy-pasted, then you may call a different is_palindrome function and > invalidate the timings! > > [...] > > -- > Arnaud > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: That interesting notation used to describe how long a loop will take.
Here you are: http://en.wikipedia.org/wiki/Big_O_notation Best regards, Matteo On Mon, Oct 4, 2010 at 8:38 PM, Tobiah wrote: > It gets used here frequently, but not > having majored in programming, I'm not > familiar with it. One might say: > > Don't do it that way, it will result in O(n**2)! > > Or something like that. I read this to mean > that the execution time varies with the square > of the number of iterations, or items being sorted > etc.. > > I want to google this, but I'm not sure what > keywords to use. Is there a wikipedia article about this > subject? I imagine that it has a concise name. > > Thanks, > > Tobiah > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to convert a string into a list
What about using the json library? It could handle errors for you: >>>import json >>>s = '["1", "2"]' >>>json.loads(s) [u'1', u'2'] Now you can convert then to integer values. Best regards, Matteo On Tue, Oct 5, 2010 at 3:41 PM, Mark Phillips wrote: > Thanks to everyone for their suggestions. I learned a lot from them! > > Mark > > On Mon, Oct 4, 2010 at 11:54 PM, Chris Rebert wrote: >> >> On Mon, Oct 4, 2010 at 10:33 PM, Arnaud Delobelle >> wrote: >> > MRAB writes: >> >> On 05/10/2010 02:10, Mark Phillips wrote: >> >>> I have the following string - "['1', '2']" that I need to convert into >> >>> a >> >>> list of integers - [1,2]. The string can contain from 1 to many >> >>> integers. Eg "['1', '7', '4',..,'n']" (values are not sequential) >> >>> >> >>> What would be the best way to do this? I don't want to use eval, as >> >>> the >> >>> string is coming from an untrusted source. >> >>> >> >> I'd probably use a regex, although others might think it's overkill. >> >> :-) >> >> >> >>>>> import re >> >>>>> s = "['1', '2']" >> >>>>> [int(n) for n in re.findall(r'-?\d+', s)] >> >> [1, 2] >> >> >> >> An alternative is: >> >> >> >>>>> s = "['1', '2']" >> >>>>> [int(n.strip("'[] ")) for n in s.split(",")] >> >> [1, 2] >> > >> > I'll add: >> > >> >>>> s = ['1', '2', '42'] >> >>>> [int(x) for x in s.split("'")[1::2]] >> > [1, 2, 42] >> >> There's also: >> >>> s = "['1', '2']" >> >>> from ast import literal_eval >> >>> [int(n) for n in literal_eval(s)] >> [1, 2] >> >> Which is safe, but less strict. >> >> Cheers, >> Chris >> -- >> http://blog.rebertia.com >> -- >> http://mail.python.org/mailman/listinfo/python-list > > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Control webbrowser from Python script
Well, if you need to issue http POST/GET commands, you can take a look at urllib/urllib2 modules. Instead if you want to take control of the web-browser I've heard about selenium, but I've never used it. Best regards, Matteo On Sat, Oct 9, 2010 at 11:39 AM, Johny wrote: > Is it possible to control any webbrowser from Python ? For example to > issue http POST and GET command > Thanks > Johny > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Does everyone keep getting recruiting emails from google?
I got one a couple of months ago. I answered back I was interested and then we scheduled a phone conversation. Good luck, Matteo On Thu, Oct 14, 2010 at 5:58 PM, Grant Edwards wrote: > On 2010-10-14, Daniel Fetchinson wrote: > >> I keep getting recruiting emails from charlesngu...@google.com about >> working for google as an engineer. The messages are pretty much the >> same and go like this: > > I got one a year or two back (from somebody else at google). I > replied saying that I wasn't interested, and that was the end of it. > > -- > Grant Edwards grant.b.edwards Yow! Oh my GOD -- the > at SUN just fell into YANKEE > gmail.com STADIUM!! > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How to implement retrying a lock tidily in Python?
You can use the 'else' keyword outside the for loop: for : if : break else The execution will step inside the else branch if the for loop ends normally, i.e. without encountering a break keyword. Hope it helps. Regards, Matteo On Sun, Oct 17, 2010 at 6:58 PM, wrote: > I'm writing some code that writes to a mbox file and want to retry > locking the mbox file a few times before giving up. I can't see a > really tidy way to implement this. > > Currently I have something like:- > > dest = mailbox.mbox(mbName, factory=None) > > for tries in xrange(3): > try: > dest.lock() > # > # > # Do some stuff to the mbox file > # > dest.unlock() > break # done what we need, carry on > > except mailbox.ExternalClashError: > log("Destination locked, try " + str(tries)) > time.sleep(1) > # and try again > > ... but this doesn't really work 'nicely' because the break after > dest.unlock() takes me to the same place as running out of the number > of tries in the for loop. I need a way to handle the case where we > run out of tries (and *haven't* done what we needed to do) separately > from the case where it worked OK. > > I can see all sorts of messy ways to handle this with a flag of some > sort but is there a proper elegant way of doing it? > > > -- > Chris Green > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: pylint -- should I just ignore it sometimes?
Another situation in which I needed to disable such kind of warnings is while working with graphics modules. I often use variable names such as x, y, z for coordinates, or r,g,b for colors. Would longer names make the reader's life easier? Best regards, Matteo On Wed, Oct 20, 2010 at 9:37 PM, Steven D'Aprano wrote: > On Wed, 20 Oct 2010 12:47:02 +0200, Jean-Michel Pichavant wrote: > >> except ValueError, e: >> >> Use meaningful names, this is so important. 'e' is not meaningful. >> 'exception' would be slighly better. > > While I agree with everything else you had to say, I have to take > exception to this comment [pun intended]. > > "e" as a short name for a generic exception instance is perfectly > reasonable, like: > > i, j, k for an index, or a loop variable > e.g. for i in range(100) > n for some other integer variable > s for a string > x for a float, or an arbitrary sequence object > e.g. [x.spam() for x in some_sequence] > > and similar. > > The last example is very instructive. What do you gain by racking your > brain for a "more meaningful" name instead of x? The obvious > alternatives, "obj" or "item", are equally generic as "x", they don't add > any further information. And how much information do you need? It's easy > to parody: > > [some_sequence_item.spam() for some_sequence_item in some_sequence] > > The very shortness of the name is valuable because it reduces the *human* > parsing time in reading, and there is no cost because the conventions are > so familiar. The convention of "for i in ..." says "this is a loop over > an integer" so strongly, that I would argue that "for index in ..." would > actually *delay* comprehension. > > Furthermore, the use of a single letter cues the reader that this > variable isn't notable -- there's nothing unusual or unconventional about > it, or it isn't the important part of the algorithm, or that its scope is > severely limited. For instance, consider the classic example of > exchanging two variables in Python: > > a, b = b, a > > versus: > > thing, other_thing = other_thing, thing > > The first example puts the emphasis on the *technique*, not the > variables. The second obscures it behind needlessly longer but still > generic names. > > You are absolutely right to insist on meaningful variable names. Where > you go wrong is to assume that single letter names can't be meaningful. > > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Dictionary of lists strange behaviour
On Tue, Nov 9, 2010 at 3:14 PM, Ciccio wrote: > Hi all, > > hope you can help me understanding why the following happens: > > In [213]: g = {'a': ['a1','a2'], 'b':['b1','b2']} > In [214]: rg = dict.fromkeys(g.keys(),[]) The argument you pass which is used to fill the values of the new dict, is created once; this means that the empty list is shared between all the keys of the dict. I think you need to create the new dict by hand. Regards, Matteo > In [215]: rg > Out[215]: {'a': [], 'b': []} > In [216]: rg['a'].append('x') > In [217]: rg > Out[217]: {'a': ['x'], 'b': ['x']} > > What I meant was appending 'x' to the list pointed by the key 'a' in the > dictionary 'rg'. Why rg['b'] is written too? > > Thanks. > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: An easier way to do this? (spoiler if you're using pyschools for fun)
I agree with Peter: * iterate over the list directly * use %10 instead of string conversion + slice (*) use genexps Good luck, Matteo On Tue, Nov 9, 2010 at 8:18 PM, Terry Reedy wrote: > On 11/9/2010 2:00 PM, Matty Sarro wrote: > >> I'm working on one of the puzzles on pyschools.com >> <http://pyschools.com>, and am trying to figure out if I can make my >> solution a bit more elegant. > > Definitely > >> def getSumOfLastDigit(numList): >> sumOfDigits=0 >> for i in range(0, len(numList)): >> num=str(numList.pop()) > > This is an awkward way to iterate through a list ;-) > >> sumOfDigits+=int(num[-1:]) >> return sumOfDigits > >> Write a function: getSumOfLastDigit(numList) that takes in a list of >> positive numbers and returns the sum of all the last digit in the list. >> >> *Examples* >> >>> getSumOfLastDigit([12, 23, 34]) >> 9 >> >>> getSumOfLastDigit([2, 3, 4]) >> 9 >> >>> getSumOfLastDigit([1, 23, 456]) >> 10 > > # Straightforward version of what you did > > def getSumOfLastDigit(numList): > sumOfDigits=0 > for i in numList: > sumOfDigits+=int(str(i)[-1:]) > return sumOfDigits > > print(getSumOfLastDigit([12, 23, 34]), > getSumOfLastDigit([2, 3, 4]), > getSumOfLastDigit([1, 23, 456]) ) > # 9 9 10 > > # Use generator expression with built-in sum function > > def getSumOfLastDigit(numList): > return sum(int(str(i)[-1:]) for i in numList) > > print(getSumOfLastDigit([12, 23, 34]), > getSumOfLastDigit([2, 3, 4]), > getSumOfLastDigit([1, 23, 456]) ) > # 9 9 10 > > -- > Terry Jan Reedy > > -- > http://mail.python.org/mailman/listinfo/python-list > -- Matteo Landi http://www.matteolandi.net/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Perl-Python-a-Day: split a file full path
Hello, I'm a cs student from Milano (Italy). I don't use scsh fequently but this should work: (open srfi-11 ;let-values srfi-28) ;format (define my-path "/Users/t/web/perl-python/I_Love_You.html") (let-values (((dir-name file-base-name file-extension) (parse-file-name my-path))) (format "~a~%" dir-name) ;/Users/t/web/perl-python/ (format "~a~%" file-base-name) ;I_Love_You (format "~a~%" file-extension)) ;.html You can find more useful function here (scsh reference manual): http://www.scsh.net/docu/html/man-Z-H-6.html#node_sec_5.1 matteo -- http://mail.python.org/mailman/listinfo/python-list