[issue45762] Missing `list` symbols in the object inventory
New submission from Matteo Bertucci : Sphinx generates a quite useful inventory file, listing all the different symbols available inside the documentation. You can find the docs.python.org inventory over at https://docs.python.org/objects.inv. The syntax of this file can be found here https://sphobjinv.readthedocs.io/en/latest/syntax.html. We use it over at Python Discord to power up our `!docs` command. It allows us to look up symbols such as `asyncio.run`, have nicely formatted documentation and links to the web page. The problem is due to where the `list` method documentations are located, inside `/tutorial/datastructures` (https://docs.python.org/3/tutorial/datastructures.html), no symbol is exported for those, making commands such as `!docs list.append` fail, which is quite a bummer. It would be very nice to have access to those. -- assignee: docs@python components: Documentation messages: 406004 nosy: Akarys, docs@python priority: normal severity: normal status: open title: Missing `list` symbols in the object inventory versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9 ___ Python tracker <https://bugs.python.org/issue45762> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue43653] Typo in the random.shuffle docs
Matteo Bertucci added the comment: Oh I see, I was used to seeing open intervals using ]a, b[. All good then! -- ___ Python tracker <https://bugs.python.org/issue43653> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue43653] Typo in the random.shuffle docs
Change by Matteo Bertucci : -- stage: -> resolved status: open -> closed ___ Python tracker <https://bugs.python.org/issue43653> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue43653] Typo in the random.shuffle docs
New submission from Matteo Bertucci : Hello! The current documentation for random.shuffle reads: > The optional argument random is a 0-argument function returning a random > float in [0.0, 1.0); by default, this is the function random(). I believe the range here should use matching symbols, unless I am missing something. -- assignee: docs@python components: Documentation messages: 389669 nosy: Akarys, docs@python priority: normal severity: normal status: open title: Typo in the random.shuffle docs type: enhancement ___ Python tracker <https://bugs.python.org/issue43653> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue42240] Add Maxheap version of a heappush into heapq module
Matteo Dell'Amico added the comment: Personally, I'd find a maxheap in the standard library helpful, and a quick Google search tells me I'm not alone. I generally have to deal with numeric values, so I have these choices: - ugly code (e.g., `minus_distance, elem = heappop(heap)`, `distance = -minus_distance`) - slow code (e.g., wrapping heapq in a class) Since most of maxheap is already implemented in the library, I wonder what is the rationale for not including it. A couple of use cases for max-heap that I ran into: - maintaining k-nearest-neighbor structures (the farthest known one is at the top of the queue) - running median (requires both a minheap and a maxheap) -- nosy: +della ___ Python tracker <https://bugs.python.org/issue42240> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue39603] [security] http.client: HTTP Header Injection in the HTTP method
Mauro Matteo Cascella added the comment: Hello, CVE-2020-26116 has been requested/assigned for this flaw via MITRE form: https://cveform.mitre.org/ I suggest mentioning it in the related vulnerability page: https://python-security.readthedocs.io/vuln/http-header-injection-method.html Also note that httplib (python-2.7.18) seems to be affected too. Any particular reason for it not to be listed in the same vulnerability page? Thank you, -- nosy: +mcascella ___ Python tracker <https://bugs.python.org/issue39603> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40439] Error in an external reference
Change by Matteo Bertucci : -- keywords: +patch nosy: +Akarys nosy_count: 4.0 -> 5.0 pull_requests: +19485 stage: -> patch review pull_request: https://github.com/python/cpython/pull/20184 ___ Python tracker <https://bugs.python.org/issue40439> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38200] Adding itertools.pairwise to the standard library?
Matteo Dell'Amico added the comment: Sorry for taking so long to answer, I didn't see notifications somehow. Raymond, my use case is in general something that happens when I'm doing analytics on sequences of events (e.g., URLs visited by a browser) or paths in a graph. I look at pairs and do something based on the pair of events (e.g., did the user likely clicked an advertising link? did they go to a potentially risky webpage, possibly by clicking a link?) I see the argument for generalizing to a sliding window, although that may lead people to choosing inefficient algorithms for sliding average or median. -- ___ Python tracker <https://bugs.python.org/issue38200> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue38200] Adding itertools.pairwise to the standard library?
New submission from Matteo Dell'Amico : I use itertools.pairwise all the time and I wonder if the same happens to others. I'm thinking that others may be in the same situation, and having this simple recipe already included in the library would be definitely more convenient than copy/pasting the recipe. Also, it may improve its visibility... -- components: Library (Lib) messages: 352642 nosy: della priority: normal severity: normal status: open title: Adding itertools.pairwise to the standard library? ___ Python tracker <https://bugs.python.org/issue38200> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue14156] argparse.FileType for '-' doesn't work for a mode of 'rb'
Matteo Bertini added the comment: Bumped in this bug yesterday, sadly a script working (by chance) in Python2 doesn't work in Python3 because of this bug. -- nosy: +naufraghi ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue14156> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue29039] Segmentation fault when using PyUnicode_FromString
New submission from Matteo Cafasso: The following code snippet: -- #include #include int main() { char *broken_string[8]; char broken_char = 4294967252; sprintf(broken_string, "%c", broken_char); PyUnicode_FromString(broken_string); } -- Produces a Segmentation Fault. Is this behaviour the expected one? The real life example comes when reading a malformed path on a Ext4 filesystem. The read string causes PyUnicode_FromString to segfault. -- components: Extension Modules messages: 283777 nosy: noxdafox priority: normal severity: normal status: open title: Segmentation fault when using PyUnicode_FromString type: crash versions: Python 3.5 ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue29039> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23246] distutils fails to locate vcvarsall with Visual C++ Compiler for Python
Matteo Bertini added the comment: I'm back on this issue with a minimal patch, and a longer motivation. Distutils does not support Visual C++ for Python compiler, but it could, with a one-line patch. The proposed workaround is to use `setuptools`. But, we are not alone in this World, am I supposed to fork each third party package still using `distutils` and upgrade them to `setuptools`? No, someone will say, you can "simply" `import setuptools`, and it will monkey-patch `distutils` adding the support for VS for Python. 1) this is implicit, very very against the Python Zen 2) the modifications are not limited and back-compatible. Some packages, say `sip` from `PyQt`, are broken simply adding this `import setuptools`, and other are too. That said, I think this minimal patch to the `find_vcvarsall` code, can save a lot of time to every Python 2.7 users, time I see better spent upgrading to Python 3. Sorry for the rant-mode, but I very liked Python, and I still like Python more than other languages, but I don't think that providing half broken solutions and very limited support to all the developer still running Python 2.7 in some big old project is a good strategy to push people to Python 3. Python 3 is already a better language, but Python as a language can be a better language only if Python 2.7 will be a first class citizen till 2020. -- keywords: +patch nosy: +naufraghi type: -> enhancement Added file: http://bugs.python.org/file45916/vsforpython.diff ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue23246> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue25084] remove semi-busy loop in py2.7 threading.Condition.wait(timeout=x)
Changes by matteo <mbrancale...@gmail.com>: -- nosy: +matteo ___ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue25084> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue24068] statistics module - incorrect results with boolean input
Changes by Matteo Dell'Amico de...@linux.it: -- nosy: +della ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue24068 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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
[issue19185] Allow multiprocessing Pool initializer to return values
Matteo Cafasso added the comment: On 09/10/13 22:59, Richard Oudkerk wrote: Yes. But my point was that somebody might have used such a function as the initializer argument. The proposed change would break a program which does with Pool(initializer=os.nice, initargs=(incr,)) as p: ... Indeed in cases like that the backward compatibility would break if the passed function is accepting a fixed amount of positional arguments. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19185 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19185] Allow multiprocessing Pool initializer to return values
Matteo Cafasso added the comment: On 07/10/13 13:32, Richard Oudkerk wrote: Richard Oudkerk added the comment: I think misuse is an exageration. Various functions change some state and return a value that is usually ignored, e.g. os.umask(), signal.signal(). These functions are compliant with POSIX standards and the return values are actually useful, they return the previously set masks and handlers, often are ignored but in complex cases it's good to know their previous state. The problem here is quite different, the interface is giving the opportunity of executing a function but it ignores the returned values, this is pretty limiting from an API point of view. It is quite counterintuitive and also not documented, proof is the amount of questions on how to use the initializer (just a couple of examples): http://stackoverflow.com/questions/10117073/how-to-use-initializer-to-set-up-my-multiprocess-pool http://stackoverflow.com/questions/9944370/use-of-initialize-in-python-multiprocessing-worker-pool Global variables usage is a pattern which might lead to code errors and many developers discourage from following it. What sort of code errors? This really seems a stylistic point. Maybe such developers would be happier using class methods and class variables rather than functions and globals variables. http://c2.com/cgi/wiki?GlobalVariablesAreBad It is a pretty common code practice to avoid global variables whenever possible; as always: is the way a tool is used to make it evil not the tool itself; yet I agree with the fact that a global variable change is hard to track down into the code and when the code grows it can lead to very tricky errors. Out of interest, what do you usually do in your initializer functions? I mainly develop back-end systems which take great advantage from the Worker Pool pattern. We are talking about services which uses third party libraries to execute CPU bounded tasks trying to scale up with the number of CPU cores. Many of these libraries, unfortunately, are stateful (I would say state-full) and their initialization is time-consuming. Typically a worker initializes some of those objects (which currently are stored in global variables) and starts crunching some data, meanwhile the state of these objects keeps changing and here the global variables pattern shows its worst side. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19185 ___ -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19185 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19185] Allow multiprocessing Pool initializer to return values
Matteo Cafasso added the comment: I agree with your point, I've probably made my considerations too quickly. The consideration was based on the fact that returning any value previously was a misuse (without consequences) of the initializer itself. Now the misuse would be exposed by the new implementation, probably meeting the requirements that leds to the misuse itself. Aim of the patch is to give an alternative to the use of global variables. Global variables usage is a pattern which might lead to code errors and many developers discourage from following it. I do believe that forcing such pattern in order to accomplish the desired goals is quite restrictive from an API. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19185 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue19185] Allow multiprocessing Pool initializer to return values
New submission from Matteo Cafasso: This patch allows the pool initializer function to return the initialized values. The returned values will be passed to the called function as first positional argument. Previously the common pattern was to store the initialized objects into global variables making the code more difficult to manage. The patch is not breaking any backward compatibility as the previous initializers were not supposed to return any value, if the initializer does not return anything the behavior is the same as usual. -- files: pool_initializer.patch keywords: patch messages: 199116 nosy: noxdafox priority: normal severity: normal status: open title: Allow multiprocessing Pool initializer to return values type: enhancement versions: Python 3.4 Added file: http://bugs.python.org/file31978/pool_initializer.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue19185 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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
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: 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
[issue16285] Erroneously encoding tilde to %7e in quote
New submission from Matteo Parrucci: RFC 3986 (http://www.ietf.org/rfc/rfc3986.txt) says: For consistency, percent-encoded octets in the ranges of ALPHA (%41-%5A and %61-%7A), DIGIT (%30-%39), hyphen (%2D), period (%2E), underscore (%5F), or tilde (%7E) should not be created by URI producers and, when found in a URI, should be decoded to their corresponding unreserved characters by URI normalizers. From python documentation talking about urllib.quote() Example: quote('/~connolly/') yields '/%7econnolly/'. I think ~ should be added to the safe characters of quote function -- messages: 173319 nosy: Matteo.Parrucci priority: normal severity: normal status: open title: Erroneously encoding tilde to %7e in quote ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16285 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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 type 'tuple' (class win32com.client.DispatchBaseClass at 0x026ACB58,) RefCount 5 type 'dict' '__int__': function __int__ at 0x03A50770, '__module__': 'win32com.gen_py.F45 RefCount 8 type 'str' ITDProperty RefCount 9 type 'function' function __getattr__ at 0x026C2630 RefCount 9 type 'function' function __setattr__ at 0x026C2670 get_referrers RefCount 15 type 'dict' 'python_version': 34014192, 'defaultUnnamedArg': PyOleEmpty object at 0x00447E RefCount 6 type 'list' class win32com.gen_py.F4503A16-F637-11D2-BD55-00500400405Bx0x1x0.ITDProperty.I RefCount 4 type 'list' (u'ItemsListCreator', u'trick', u'pVal'), (3, 49, '0', None), (16393, 10, None, RefCount 4 type 'frame' frame object at 0x029721D8 RefCount 7 type 'frame' frame object at 0x02760148 RefCount 5 type 'dict' '{39AAEA35-F71F-11D2-BD59-00500400405B}': class 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 steve+comp.lang.pyt...@pearwood.info 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
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
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 harrison.mor...@gmail.com mailto:harrison.mor...@gmail.com On Wed, Jul 4, 2012 at 12:38 AM, levi nie levinie...@gmail.com 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
Re: Tkinter deadlock on graceful exit
On Jun/02, Dennis Lee Bieber wrote: On Sat, 2 Jun 2012 14:57:17 +0200, Matteo Landi mat...@matteolandi.net 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
Re: Tkinter deadlock on graceful exit
On Jun/01, Matteo Landi wrote: On Thu, May 31, 2012 at 3:02 PM, Matteo Landi mat...@matteolandi.net wrote: On Thu, May 31, 2012 at 3:42 AM, Terry Reedy tjre...@udel.edu 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/ Doing further investigation, I found this post [1] dated 2005, which probably explains why I'm gatting this strange deadlock. Quoting the linked article: All Tkinter access must be from the main thread (or, more precisely, the thread that called mainloop). Violating this is likely to cause nasty and mysterious symptoms such as freezes or core dumps. Yes this makes combining multi-threading and Tkinter very difficult. The only fully safe technique I have found is polling (e.g. use after from the main loop to poll a threading Queue that your thread writes). I have seen it suggested that a thread can safely use event_create to communicate with the main thread, but have found this is not safe. Well, if this still applies, then I'm invoking `after_idle` from a thread different from
Re: Tkinter deadlock on graceful exit
On Thu, May 31, 2012 at 3:02 PM, Matteo Landi mat...@matteolandi.net wrote: On Thu, May 31, 2012 at 3:42 AM, Terry Reedy tjre...@udel.edu 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/ Doing further investigation, I found this post [1] dated 2005, which probably explains why I'm gatting this strange deadlock. Quoting the linked article: All Tkinter access must be from the main thread (or, more precisely, the thread that called mainloop). Violating this is likely to cause nasty and mysterious symptoms such as freezes or core dumps. Yes this makes combining multi-threading and Tkinter very difficult. The only fully safe technique I have found is polling (e.g. use after from the main loop to poll a threading Queue that your thread writes). I have seen it suggested that a thread can safely use event_create to communicate with the main thread, but have found this is not safe. Well, if this still applies, then I'm invoking `after_idle` from a thread different from the mainloop's one. I'll try to implement the poll/queue fix, and see if this solves the issue. Matteo [1] http://mail.python.org/pipermail/tkinter-discuss/2005
Re: Tkinter deadlock on graceful exit
On Thu, May 31, 2012 at 3:42 AM, Terry Reedy tjre...@udel.edu 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 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
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: 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
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_contentview=articleid=64%3Anumbering-pdf-file-in-pythoncatid=38%3AprogrammingItemid=55lang=en http://www.boscolini.eu/Boscolini/index.php?option=com_contentview=articleid=64%3Anumbering-pdf-file-in-pythoncatid=38%3AprogrammingItemid=55lang=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
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: 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 tjre...@udel.edu 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: Dictionary of lists strange behaviour
On Tue, Nov 9, 2010 at 3:14 PM, Ciccio franap...@gmail.com 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: 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 st...@remove-this-cybersource.com.au 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: How to implement retrying a lock tidily in Python?
You can use the 'else' keyword outside the for loop: for condition: if condition: break else some operations 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, tinn...@isbd.co.uk 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: 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 inva...@invalid.invalid wrote: On 2010-10-14, Daniel Fetchinson fetchin...@googlemail.com 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: 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 pyt...@hope.cz 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: 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 m...@phillipsmarketing.biz 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 c...@rebertia.com wrote: On Mon, Oct 4, 2010 at 10:33 PM, Arnaud Delobelle arno...@gmail.com wrote: MRAB pyt...@mrabarnett.plus.com 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: 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 t...@rcsreg.com 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: 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 joshua.r.engl...@gmail.com 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 arno...@googlemail.com wrote: Matteo Landi landima...@gmail.com 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 joshua.r.engl...@gmail.com 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: 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 ben+pyt...@benfinney.id.au wrote: Baba raoul...@gmail.com 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
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: 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 moss.r...@gmail.com 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: pylint scores
On Sun, Aug 1, 2010 at 12:27 PM, News123 news1...@free.fr 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 drsali...@gmail.com wrote: On Fri, Jul 30, 2010 at 12:18 PM, News123 news1...@free.fr 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 stdin, 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
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 navkir...@gmail.com 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 navkir...@gmail.com 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 navkir...@gmail.com 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 c...@rebertia.com wrote: On Wed, Aug 4, 2010 at 12:21 AM, News123 news1...@free.fr wrote: snip 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: 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 parvini_na...@yahoo.comwrote: 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 jldunn2...@gmail.com 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 r...@campbell-lange.net wrote: On 03/08/10, Alex Willmer (a...@moreati.org.uk) wrote: On Aug 3, 11:21?am, loial jldunn2...@gmail.com 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: Normalizing A Vector
On Mon, Aug 2, 2010 at 1:50 AM, Terry Reedy tjre...@udel.edu 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: 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 drsali...@gmail.com wrote: On Fri, Jul 30, 2010 at 12:18 PM, News123 news1...@free.fr 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 stdin, 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: 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 nt_mahm...@yahoo.com 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: 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 huji...@gmail.com 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
[issue9031] distutils uses invalid -Wstrict-prototypes flag when compiling C++ extension module
New submission from Matteo Vescovi matteo.vesc...@yahoo.co.uk: Hi, It seems like distutils passes an invalid -Wstrict-prototypes flag when compiling a C++ extension module on Linux (tested on Debian lenny and sid). running build running build_py creating build creating build/lib.linux-i686-2.6 copying presage.py - build/lib.linux-i686-2.6 running build_ext building '_presage' extension creating build/temp.linux-i686-2.6 gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -O2 -g -Wall -O2 -fPIC -I../../src/lib -I/usr/include/python2.6 -c presage_wrap.cpp -o build/temp.linux-i686-2.6/presage_wrap.o cc1plus: warning: command line option -Wstrict-prototypes is valid for Ada/C/ObjC but not for C++ g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -L/usr/local/lib -Wl,--as-needed -g -O2 -g -Wall -O2 build/temp.linux-i686-2.6/presage_wrap.o -L../../src/lib/.libs -lpresage -o build/lib.linux-i686-2.6/_presage.so running build_scripts creating build/scripts-2.6 copying and adjusting presage_python_demo - build/scripts-2.6 changing mode of build/scripts-2.6/presage_python_demo from 644 to 755 I did a bit of digging on this one. It looks like distutils reuses whatever flags python was built with. m...@burrow:/usr/lib/python2.5/distutils$ python2.5 Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. import sysconfig sysconfig.get_config_var(CFLAGS) '-fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes' In /usr/lib/python2.5/distutils/unixcompiler.py I found this comment: # XXX Things not currently handled: # * optimization/debug/warning flags; we just use whatever's in Python's # Makefile and live with it. Is this adequate? If not, we might # have to have a bunch of subclasses GNUCCompiler, SGICCompiler, # SunCCompiler, and I suspect down that road lies madness. It would be nice is distutils was smart enough to recognize that a c++ module was being built and to strip the unneeded flags. Please consider this as a wishlist request, as this causes only a warning, not a failure. Cheers, - Matteo -- assignee: tarek components: Distutils messages: 108136 nosy: matteo.vescovi, tarek priority: normal severity: normal status: open title: distutils uses invalid -Wstrict-prototypes flag when compiling C++ extension module type: feature request versions: Python 2.5, Python 2.6 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue9031 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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 cand...@free.invalid 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 landima...@gmail.com 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 landima...@gmail.com 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 d...@plfn.invalid 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 pyshell#30, line 1, in module x=C() File pyshell#29, 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: 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 debat...@gmail.com wrote: On Wed, Jun 2, 2010 at 10:40 AM, pmz przemek.zaw...@gmail.com 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 przemek.zaw...@gmail.com wrote: On 2 Cze, 19:56, geremy condra debat...@gmail.com wrote: On Wed, Jun 2, 2010 at 10:40 AM, pmz przemek.zaw...@gmail.com 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: 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 pyt...@rcn.com wrote: On May 19, 12:58 pm, superpollo ute...@esempio.net 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: 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 ith...@gmail.com 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: 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 oxfordenergyservi...@googlemail.comwrote: 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: plot debugging problem
On Tue, May 11, 2010 at 8:59 PM, Sandy Sandy c...@live.com 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 c...@live.com 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 c...@live.com 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 (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 c...@live.com 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 c...@live.com 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
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 c...@live.com 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 c...@live.com 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 c...@live.com 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: Sqlite3. Substitution of names in query.
On Oct 30, 7:27 am, Diez B. Roggisch de...@nospam.web.de 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
[issue7111] core dump when stderr is moved
Changes by Matteo Bertini matt...@naufraghi.net: -- nosy: +naufraghi ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7111 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7111] stdout closed
Changes by Matteo Bertini matt...@naufraghi.net: -- title: core dump when stderr is moved - stdout closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7111 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue7111] core dump when stderr is moved
Matteo Bertini matt...@naufraghi.net added the comment: sorry, title restored! -- title: stdout closed - core dump when stderr is moved ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue7111 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1488934] file.write + closed pipe = no error
Changes by Matteo Bertini matt...@naufraghi.net: -- nosy: +naufraghi type: - behavior ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1488934 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1596] Broken pipes should be handled better in 2.x
Changes by Matteo Bertini matt...@naufraghi.net: -- nosy: +naufraghi ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue1596 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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: logging sound / speech handler?
On Sep 9, 6:02 pm, Tim Chase python.l...@tim.thechases.com 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: Fill Javascript form
On 11 Mag, 23:06, Shawn Milochik sh...@milochik.com 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(script type='text/javascript' id='chillicontroller'/ script); 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
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
[issue5867] No way to create an abstract classmethod
New submission from Matteo Dell'Amico de...@linux.it: Is there a way to define an abstract classmethod? The two obvious ways don't seem to work properly. Python 3.0.1+ (r301:69556, Apr 15 2009, 17:25:52) [GCC 4.3.3] on linux2 Type help, copyright, credits or license for more information. import abc class C(metaclass=abc.ABCMeta): ... @abc.abstractmethod ... @classmethod ... def f(cls): print(42) ... Traceback (most recent call last): File stdin, line 1, in module File stdin, line 3, in C File /usr/lib/python3.0/abc.py, line 24, in abstractmethod funcobj.__isabstractmethod__ = True AttributeError: 'classmethod' object has no attribute '__isabstractmethod__' class C(metaclass=abc.ABCMeta): ... @classmethod ... @abc.abstractmethod ... def f(cls): print(42) ... class D(C): pass ... D.f() 42 -- components: Library (Lib) messages: 86744 nosy: della severity: normal status: open title: No way to create an abstract classmethod type: behavior versions: Python 3.0 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5867 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
Re: Play sound at wanted frequency
On 15 Apr, 19:25, Scott David Daniels scott.dani...@acm.org 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
[issue5779] _elementtree import can fail silently
New submission from Matteo Bertini matt...@naufraghi.net: (the patch is old, I forwarded it to Fredrik but I forgot to open the bug) Playing with PyInstaller I have found that the final part of _elementtree.c: Index: Modules/_elementtree.c === --- Modules/_elementtree.c (revisione 59540) +++ Modules/_elementtree.c (copia locale) @@ -2780,7 +2780,10 @@ ); -PyRun_String(bootstrap, Py_file_input, g, NULL); +if (PyRun_String(bootstrap, Py_file_input, g, NULL) == NULL) { +m = PyErr_Occurred(); +return; +} elementpath_obj = PyDict_GetItemString(g, ElementPath); executes a bit of python code without checking the return value. That can lead to weird things playing with import hooks, for example an assert like this can fail: Index: Lib/test/test_elemettree.py === --- Lib/test/test_elemettree.py (revisione 0) +++ Lib/test/test_elemettree.py (revisione 0) @@ -0,0 +1,21 @@ +#! /usr/bin/env python + +def importHook(*args, **kwargs): +if 'xml.etree' in args: +raise ImportError +else: +return __real__import__(*args, **kwargs) + +import os +import __builtin__ +__real__import__ = __builtin__.__import__ +__builtin__.__import__ = importHook + +try: +import xml.etree.cElementTree as cET +except ImportError: +pass +else: +out = os.popen(python -c 'import xml.etree.cElementTree as cET; print dir(cET)').read().strip() +assert str(dir(cET)) == out, (str(dir(cET)), out) + Quite a novice with python internals, so comments are welcome. Matteo Bertini -- components: XML messages: 86062 nosy: naufraghi severity: normal status: open title: _elementtree import can fail silently ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5779 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5779] _elementtree import can fail silently
Matteo Bertini matt...@naufraghi.net added the comment: Ups, I duplicated myself... issue3475 -- status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5779 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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
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
Re: Pythoner,Wish me luck!
On Apr 3, 9:05 am, Linuxwell ahqylang...@gmail.com 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
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
[issue5647] MutableSet.__iand__ implementation calls self.discard while iterating on self
New submission from Matteo Dell'Amico de...@linux.it: The current MutableSet.__iand__ implementation calls self.discard while iterating on self. This creates strange problems while implementing MutableSet with simple choices. For example, consider the attached file which implements set by delegating either to a set or a list. In the first cases, an exception is raised; in the second, the result is not what is expected. Python 2.6+ (r26:66714, Oct 22 2008, 09:21:39) [GCC 4.3.2] on linux2 Type help, copyright, credits or license for more information. from simpleset import WithSet, WithList s = WithSet([1,2]) s = () Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.6/_abcoll.py, line 290, in __iand__ for value in self: RuntimeError: Set changed size during iteration s = WithList([1,2]) s = () list(s) [2] -- components: Library (Lib) files: simpleset.py messages: 85006 nosy: della severity: normal status: open title: MutableSet.__iand__ implementation calls self.discard while iterating on self versions: Python 2.6, Python 2.7, Python 3.0, Python 3.1 Added file: http://bugs.python.org/file13538/simpleset.py ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5647 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue5647] MutableSet.__iand__ implementation calls self.discard while iterating on self
Matteo Dell'Amico de...@linux.it added the comment: I suggest solving the problem by changing the implementation to: def __iand__(self, c): self -= self - c: or to def __iand__(self, c): for item in self - c: self.discard(item) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue5647 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
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: c++ extension, problem passing argument
On 14 Mar, 02:08, Aaron Brady castiro...@gmail.com wrote: On Mar 13, 5:42 pm, Matteo tadweles...@gmail.com wrote: On 13 Mar, 22:35, Aaron Brady castiro...@gmail.com wrote: On Mar 13, 1:34 pm, Matteo tadweles...@gmail.com 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 stdin, line 1, in module TypeError: in method 'sum', argument 1 of type 'int **' NOTE: A pure c++ program works as expected: #include iostream 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