Re: strange behavor....
m...@distorted.org.uk (Mark Wooding) writes: >> So even if the globals() dictionary is custom, its __setitem__ method is >> *not* called. > > Fascinating. Thank you. In case it's not obvious, that is because CPython assumes the type for many of its internal or semi-internal structures, and calls the appropriate functions, such as dict.__setitem__ (PyDict_SetItem in C) directly. So python doesn't break the encapsulation of dict itself, it just takes the liberty to assume that globals() is a non-subclassed dict, at least as far as __setitem__ is concerned. -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On Mon, 15 Nov 2010 22:40:00 -0700, Ian Kelly wrote: > On 11/15/2010 10:26 PM, Steven D'Aprano wrote: >> t = foo()+bar()+baz() if pred(it) else baz()-foo()-bar() >> >> What does "it" mean here? > > "it" would mean the result of the expression foo()+bar()+baz(). What > else could it mean? It could mean the last expression, baz(). Or the entire compound expression, foo()+bar()+baz(). > There are valid objections to the proposal, but the > intended semantics seem perfectly clear. Fair point, my example was terrible and didn't show the point that was clear in my head. Mea culpa. How about this instead? t = foo()+it if pred(it) else bar() Should that be a SyntaxError, or is `it` a variable that holds its value from statement to statement? t = it t = (foo() if pred(it) else bar()) if cond(it) else baz() For what it's worth, Apple's defunct Hypertalk language had a couple of syntax elements very much like that. Hypertalk had a special variable, "it", which you used like this: get the number of cards put it into field "Card number" I trust I don't have to explain this? :) Hypertalk also had a special function, "the result", which worked something like this: ask "This is a dialog box. Please type your answer here:" put the result into field "Your answer" (or you could use "result()" instead). Both of these worked quite well with Hypertalk, particularly with it's focus on non-programmers, but they were designed into the language from the beginning. In Python they would be the proverbial round peg in a square hole. BTW, I frequently use "it" for iterators, and making "it" a reserved word would break a lot of my code. This would make me quite peeved. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting references to objects without incrementing reference counters
On Nov 15, 10:06 pm, John Nagle wrote: > On 11/14/2010 11:08 AM, Artur Siekielski wrote: > > > Hi. > > I'm using CPython 2.7 and Linux. In order to make parallel > > computations on a large list of objects I want to use multiple > > processes (by using multiprocessing module). In the first step I fill > > the list with objects and then I fork() my worker processes that do > > the job. > > > This should work optimally in the aspect of memory usage because Linux > > implements copy-on-write in forked processes. > > There used to be a memory leak when using Pickle to talk > to subprocesses. See what I wrote at It's something different - I'm not using serialization at all - I have full Python objects "copied" into child processes :). -- http://mail.python.org/mailman/listinfo/python-list
[RELEASED] Python 3.2 alpha 4
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On behalf of the Python development team, I'm happy to announce the fourth and (this time really) final alpha preview release of Python 3.2. Python 3.2 is a continuation of the efforts to improve and stabilize the Python 3.x line. Since the final release of Python 2.7, the 2.x line will only receive bugfixes, and new features are developed for 3.x only. Since PEP 3003, the Moratorium on Language Changes, is in effect, there are no changes in Python's syntax and built-in types in Python 3.2. Development efforts concentrated on the standard library and support for porting code to Python 3. Highlights are: * numerous improvements to the unittest module * PEP 3147, support for .pyc repository directories * PEP 3149, support for version tagged dynamic libraries * an overhauled GIL implementation that reduces contention * many consistency and behavior fixes for numeric operations * countless fixes regarding string/unicode issues; among them full support for a bytes environment (filenames, environment variables) * a sysconfig module to access configuration information * a pure-Python implementation of the datetime module * additions to the shutil module, among them archive file support * improvements to pdb, the Python debugger For an extensive list of changes in 3.2, see Misc/NEWS in the Python distribution. To download Python 3.2 visit: http://www.python.org/download/releases/3.2/ 3.2 documentation can be found at: http://docs.python.org/3.2/ Please consider trying Python 3.2 with your code and reporting any bugs you may notice to: http://bugs.python.org/ Enjoy! - -- Georg Brandl, Release Manager georg at python.org (on behalf of the entire python-dev team and 3.2's contributors) -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.16 (GNU/Linux) iEYEARECAAYFAkzij74ACgkQN9GcIYhpnLCbtwCgi4whRruM0Oi6yfgjVclYErFa OJcAn0U8UBBsQBFyGcnKJRbls6B+guQ2 =Vuqf -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On 11/16/2010 3:42 AM, Steven D'Aprano wrote: On Mon, 15 Nov 2010 22:40:00 -0700, Ian Kelly wrote: On 11/15/2010 10:26 PM, Steven D'Aprano wrote: t = foo()+bar()+baz() if pred(it) else baz()-foo()-bar() What does "it" mean here? "it" would mean the result of the expression foo()+bar()+baz(). What else could it mean? It could mean the last expression, baz(). Or the entire compound expression, foo()+bar()+baz(). Unless the precedence rules were to change, the three expressions in the example that are operated on by the ternary are these: 1. foo()+bar()+baz() 2. pred(it) 3. baz()-foo()-bar() So the antecedent would have to be one of those in order to make any sense at all, and obviously the only choice of those that would be useful is the first one. There are valid objections to the proposal, but the intended semantics seem perfectly clear. Fair point, my example was terrible and didn't show the point that was clear in my head. Mea culpa. How about this instead? t = foo()+it if pred(it) else bar() Should that be a SyntaxError, or is `it` a variable that holds its value from statement to statement? SyntaxError. Implementing this without making 'it' a keyword would be very sloppy, IMO. Another option would be to use a special variable named '__', similar to the variable '_' that the REPL uses to store the result of the last command. This might break some existing code, but probably not very much. Mostly it would just be confusing. t = (foo() if pred(it) else bar()) if cond(it) else baz() More problematic: t = foo() if pred(bar() if pred2(it) else str(it)) else bar() Does the first 'it' belong to the inner or outer ternary? Probably the inner one. What about the second 'it'? BTW, I frequently use "it" for iterators, and making "it" a reserved word would break a lot of my code. This would make me quite peeved. I do the same, and yes, it would make upgrading a real pain. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
first attempt with pybluez on XP fail
Hi, Wanted to write a first simple example with pybluez and offer a serial connection service with a given name. What I tried (being inspired by http://people.csail.mit.edu/albert/bluez-intro/x290.html ) is: server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) port = bluetooth.PORT_ANY # original example used get_available_port() # buy this seems obsolete server_sock.bind(("",port)) server_sock.listen(1) print "listening on port %d" % port loc_host, loc_port = server_sock.getsockname() print "HOST", loc_host, "PORT??", loc_port uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848" bluetooth.advertise_service( server_sock, "FooBar Service", uuid ) The error message, that I get is: listening on port 0 HOST XX:XX:XX:XX:XX:XX PORT?? 1 Traceback (most recent call last): File "XXX\bt_01.py", line 94, in cmd(args) File "XXX\bt_01.py", line 71, in srvr srvr = BTSerService() File "XXX\bt_01.py", line 51, in __init__ bluetooth.advertise_service( server_sock, "FooBar Service", uuid ) File "C:\Python26\lib\site-packages\bluetooth\msbt.py", line 173, in advertise _service sock._sdp_handle = bt.set_service_raw (sock._raw_sdp_record, True) IOError: An invalid argument was supplied. What might I be doing wrong? I got rid of the obsolete get_available_port() and wonder what else might have changed? Thanks a lot for your help. -- http://mail.python.org/mailman/listinfo/python-list
Pickle in a POST/GET request give EOFError
Hi everybody ! First time I write to this mailing list :) I started writing in python last week, that's probably why I can't understand the following problem... I create a list called web_site_list. This list contain dictionaries called web_site. And some values in this dictionaries are list too. I do that in a function and I return this : return pickle.dumps(web_site_list) This is working fine :) If I do : print "%s" % pickle.loads(system.get_web_site_list()) I've got the right stuffs. For example it returns : [{'documentroot_size': '120', 'servername': '---default---', 'client': 'undefined', 'documentroot': '/var/www/', 'client_contact': 'undefined', 'serveralias': []}] I send this to a web service. I send it like that : #I put it in params def system_updateweb_site(server, login, password): params = {} params['login'] = login params['password'] = password params['action'] = 'updateweb_site' params['servername'] = get_servername() params['hosted_web_site'] = get_web_site_list() return call_system_ws(server, params) #Here's how I send it (I tried in GET and POST) def call_system_ws(host, params): query_string = urllib.urlencode(params) #GET # f = urllib.urlopen("http://%s/ws?%s"; % (host, query_string)) #POST f = urllib.urlopen("http://%s/ws"; % (host), query_string) result = f.readline().strip() if result == 'ERROR': msg = f.readline().strip() return (False, msg) return (True, result) On the server side : if action == 'updateweb_site': if not (fields.has_key('servername') and fields.has_key('hosted_web_site')): raise WSError('missing parameter : servername or hosted_web_site') log ('ERROR : missing parameter : servername or hosted_web_site') else: servername=g.db.escape_string(fields['servername']) hosted_web_site=g.db.escape_string(fields['hosted_web_site']) output = systemserver.updateweb_site(cursor, servername, hosted_web_site) In systemserver.py : def updateweb_site(cursor, host, hosted_web_site): web_site_list = pickle.loads(hosted_web_site) return "%s" % (web_site_list) I catch this error :* *: args = () message = '' Why ? If I just print hosted_web_site, I get this on my web page : (lp0\n(dp1\nS\'documentroot_size\'\np2\nS\'120\'\np3\nsS\'servername\'\np4\nS\'default\'\np5\nsS\'client\'\np6\nS\'undefined\'\np7\nsS\'documentroot\'\np8\nS\'/var/www/\'\np9\nsS\'client_contact\'\np10\ng7\nsS\'serveralias\'\np11\n(lp12\nsa. It's the "pickled view" of [{'documentroot_size': '120', 'servername': '---default---', 'client': 'undefined', 'documentroot': '/var/www/', 'client_contact': 'undefined', 'serveralias': []}] Can someone help me please ? I spend my afternoon to google to try to find a solution... Thanks in advance !!! Romaric Defaux smime.p7s Description: S/MIME Cryptographic Signature -- http://mail.python.org/mailman/listinfo/python-list
Re: [Python-Dev] [RELEASED] Python 3.2 alpha 3
Hi Georg, On Tue, 12 Oct 2010 14:40:52 +0200 Georg Brandl wrote: > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > On behalf of the Python development team, I'm happy to announce the > third and final alpha preview release of Python 3.2. [snip] I built & ran Py3.2a4's tests & they were fine for me on Debian testing 64-bit. I also tested all the Python 3 book's examples and with one exception---due to (3) below---they all work fine:-) I think it might be worth mentioning in What's New: (1) In "Other Language Changes", the second item about str() might benefit from a note; perhaps: "This change may break doctests that compare floats. (For example, in older Python 3's str(math.pi) produces '3.14159265359'.) (2) In "New, Improved, and Deprecated Modules", it still has "XXX mention argparse". Maybe replace with: "A new option parsing module, argparse, has been added. This module is more flexible and pragmatic than the deprecated optparse module it supercedes." After all, the module's own documentation is sufficient for the details and "argparse" is a link to it. (3) There is no mention of a subtle change in ElementTree. In older Python 3's xml.etree.ElementTree.parse() would raise an xml.parsers.expat.ExpatError if the parse failed; in 3.2a4 it seems to raise xml.etree.ElementTree.ParseError instead. This is much nicer but could probably do with a mention since it can break things (with an "During handling of the above exception, another exception occurred" error). Perhaps in the "New, Improved, and Deprecated Modules" you might add: "The xml.etree.ElementTree now raises an xml.etree.ElementTree.ParseError when a parse fails; previously it raised a xml.parsers.expat.ExpatError." It would also be nice to mention this in the ElementTree module's documentation for the parse() function. -- Mark Summerfield, Qtrac Ltd, www.qtrac.eu C++, Python, Qt, PyQt - training and consultancy "Programming in Python 3" - ISBN 0321680561 http://www.qtrac.eu/py3book.html -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On Nov 15, 2:39 am, Dmitry Groshev wrote: > Here are some proposals. They are quite useful at my opinion and I'm > interested for suggestions. It's all about some common patterns. > First of all: how many times do you write something like > t = foo() > t = t if pred(t) else default_value Never! [snip] > And the third. The more I use python the more I see how "natural" it > can be. By "natural" I mean the statements like this: > [x.strip() for x in reversed(foo)] > which looks almost like a natural language. But there is some > pitfalls: > if x in range(a, b): #wrong! This is true only if x is an integer such that a <= x < b > it feels so natural to check it that way, but we have to write > if a <= x <= b This is true if x is an integer OR a float. Two very different cases, deserving of different notation. André -- http://mail.python.org/mailman/listinfo/python-list
Re: [RELEASED] Python 3.2 alpha 3
On Nov 16, 9:23 am, Mark Summerfield wrote: > I think it might be worth mentioning in What's New: FWIW, I'll be updating the What's New document for the Beta. Raymond -- http://mail.python.org/mailman/listinfo/python-list
Re: Help: Guide needed in trying to delete/flush the content of a fifo file.
On 16/11/2010 06:52, Ton wrote: On Nov 16, 1:47 am, MRAB wrote: On 15/11/2010 11:03, Ton wrote: On Nov 14, 11:55 pm, MRABwrote: On 14/11/2010 14:48, ton ph wrote:>Hi python geeks, I have problem which i have been trying to find out for the past some days, i have a device which feeds info to my fifo continuosly, and a thread of mine reads the fifo continuosly. Now when i change a parameter in the device, it sends me different values. Now my problem is that , i want to get rid of the contents of my previous info which is present in my buffer of the fifo.So i want to flush the fifo content when my device starts sending different info i am implementing writing and reading the fifo using two different threads. Please someone guide me solving my problem. I highly apologise everyone in case my post is not so clear... Thanks everyone in advance. When the info changes, the thread which is putting items into the fifo could flush it just by getting items from it (using a non-blocking get) until it's empty. Hi Mrab, Thanks for your immediate reply , can you please guide me with any tool or library function which i can flush my fifo content .. i use os.mkfifo() to make the fifo. Or is there any other way i could do this ... Thanks Ah, you're using pipes; I thought you might've been using a queue. I don't know of a way of flushing a pipe. I wonder whether it's a good idea for the producer to keep filling the fifo, because the consumer know how 'old' the info is when it arrives, and there's your problem of discarding stale info. Perhaps the producer should wait until the consumer has sent an acknowledgement before sending more info. Thanks Mrab, the problem now is that the producer continously dumps the information and since i am implementing the producer using the subprocess as like this cmd = ['my files'] proc = subprocess.Popen(cmd, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT ) while flag == True: line = proc.stdout.read() Now what i want is to synchronise the problem of actual info the device is sending and the proper reading as the device is sending and not about reading the previous old info in the fifo... hope i have made clearer my prob. even i have tried proc.stdout.flush(), to flush the previous old info in the fifo. Thnks Use two pipes, one each way. The consumer could use send a message to the producer requesting an update, which the producer could send back via the other pipe. -- http://mail.python.org/mailman/listinfo/python-list
Re: What was your strategy?
In article , Dennis Lee Bieber wrote: > On Sun, 14 Nov 2010 16:32:24 -0600, Jorge Biquez > declaimed the following in gmane.comp.python.general: > > > I was wondering if you can share what was the strategy you followed > > to master Python (Yes I know I have to work hard study and practice a > > lot). I mean did you use special books, special sites, a plan to > > I picked up the first edition "Programming Python" (and a now > forgotten competitor book) since the Amiga was mentioned... I'll jump in and recommend the book "Python in a Nutshell" by Martelli. It may be a little dated now, but it covers many Python topics in good detail without becoming a bloated reference. Nicely written. It's still the first book I reach for after 6 years of Python coding and it rarely disappoints. -- -- Lou Pecora -- http://mail.python.org/mailman/listinfo/python-list
argparse subparser problem
I want to have subparsers, but I also want to be able to say: myprogram --version and get the version # --- import argparse def stop(): pass parser = argparse.ArgumentParser() parser.add_argument ('--version', action='store_true') subparsers = parser.add_subparsers() parser_stop = subparsers.add_parser ('stop') parser_stop.add_argument ('stop', action='store_true') parser_stop.set_defaults (func=stop) opt = parser.parse_args (['--version']) -- python test_p1.py usage: test_p1.py [-h] [--version] {stop} ... test_p1.py: error: too few arguments -- http://mail.python.org/mailman/listinfo/python-list
Re: What was your strategy?
On 11/16/2010 2:22 PM, Lou Pecora wrote: > I'll jump in and recommend the book "Python in a Nutshell" by Martelli. > It may be a little dated now, but it covers many Python topics in good > detail without becoming a bloated reference. Nicely written. It's still > the first book I reach for after 6 years of Python coding and it rarely > disappoints. +1 It's encyclopedic. regards Steve -- Steve Holden +1 571 484 6266 +1 800 494 3119 PyCon 2011 Atlanta March 9-17 http://us.pycon.org/ See Python Video! http://python.mirocommunity.org/ Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
SQLite3 and lastrowid
I am fairly new to Python (no development experience) and brand new to using sqlite through Python. With that said, I created a database with two tables. The first has about 30,000 rows of static data. The second has 9 rows of static data. Before I added the second table I could simply run 'print(cursor.lastrowid)' and it would give me the id number. However, with two tables I am unable to do this. Does anyone know if there is a way to reference one table or another to get lastrowid? Thanks!!! -- http://mail.python.org/mailman/listinfo/python-list
first attempts with pybluez on Win XP fail
Hi, My post from Google groups doesn't sem to get through. SO here once more posted from gmane. Wanted to write a first simple example with pybluez and offer a serial connection service with a given name. What I tried (being inspired by http://people.csail.mit.edu/albert/bluez-intro/x290.html ) is: server_sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM ) port = bluetooth.PORT_ANY # original example used get_available_port() # buy this seems obsolete server_sock.bind(("",port)) server_sock.listen(1) print "listening on port %d" % port loc_host, loc_port = server_sock.getsockname() print "HOST", loc_host, "PORT??", loc_port uuid = "1e0ca4ea-299d-4335-93eb-27fcfe7fa848" bluetooth.advertise_service( server_sock, "FooBar Service", uuid ) The error message, that I get is: listening on port 0 HOST XX:XX:XX:XX:XX:XX PORT?? 1 Traceback (most recent call last): File "XXX\bt_01.py", line 94, in cmd(args) File "XXX\bt_01.py", line 71, in srvr srvr = BTSerService() File "XXX\bt_01.py", line 51, in __init__ bluetooth.advertise_service( server_sock, "FooBar Service", uuid ) File "C:\Python26\lib\site-packages\bluetooth\msbt.py", line 173, in advertise _service sock._sdp_handle = bt.set_service_raw (sock._raw_sdp_record, True) IOError: An invalid argument was supplied. What might I be doing wrong? I got rid of the obsolete get_available_port() and wonder what else might have changed? Thanks a lot for your help. -- http://mail.python.org/mailman/listinfo/python-list
how to use socket to get packet which destination ip is not local?
Hi, Maybe it's a very simple question. I'm trying to write a dhcpclient code with python. The dhcpclient does not have ip address at the very beginning, it sends out dhcpdiscover and then server sends back dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but that ip is not client's local ip yet. How can I make my socket to receive that packet? I tried socket bind to 0.0.0.0, but it only binds to any local ip, not any ip which may not be local. therefore the socket cannot get that dhcp offer packet even I can use wireshark to see that packet did come to this pc. How to solve it? Thanks in advance. Rgds, Hans -- http://mail.python.org/mailman/listinfo/python-list
Help in language development
Hello, my name Pedro Igor, I am a student and develop applications in python for 1 year. I enrolled in the group to contribute in developing this wonderful language that helps me both in day-to-day, but I'm going through some difficulties because they do not know where to start, can someone please give me some steps so that I can contribute at once development for all, where do I start, if the documentation or a book. I know that most people have more important issues to address in other posts but assistance does not cost anything. Graciously -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 1:00 pm, fuglyducky wrote: > Before I added the second table I could simply run > 'print(cursor.lastrowid)' and it would give me the id number. However, > with two tables I am unable to do this. It would help if you would show the code where you're trying to do this. Without your actual code to look at, we can't tell you why it doesn't work. > Does anyone know if there is a way to reference one table or another > to get lastrowid? cursor.lastrowid is always in reference to the last query executed by the cursor, not in reference to a table. If you don't capture the value, and then you execute another query on the same cursor, the previous value of cursor.lastrowid no longer exists. If you need it now, then either you should have captured it when you had the chance, or you should not have executed another query on the same cursor. But perhaps this is not what you're actually trying to do. I can't tell, because I haven't seen the code. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Re: how to use socket to get packet which destination ip is not local?
On 16/11/2010 20:38, Hans wrote: Hi, Maybe it's a very simple question. I'm trying to write a dhcpclient code with python. The dhcpclient does not have ip address at the very beginning, it sends out dhcpdiscover and then server sends back dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but that ip is not client's local ip yet. How can I make my socket to receive that packet? I tried socket bind to 0.0.0.0, but it only binds to any local ip, not any ip which may not be local. therefore the socket cannot get that dhcp offer packet even I can use wireshark to see that packet did come to this pc. How to solve it? Thanks in advance. Have you read this: http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol -- http://mail.python.org/mailman/listinfo/python-list
How to implement a callback COM object in Python
Hi, I'm trying to write a COM client to a COM server which controls a power meter. The COM server fires events that should be handled by the client. The docs state that I have to supply a class which implements the IFM2DeviceEvents interface. I need some instructions on how to translate the VB sample code to Python: Option Explicit Implements IFM2DeviceEvents Private m_CallbackEvent As String Private m_CallbackMessage As String Private m_DeviceIndex As Integer Private m_SerialNumber As String Private m_ZeroDeviceTimeoutCounter As Integer ' Methods Private Sub IFM2DeviceEvents_DisplayErrorToClient() frmTest.DisplayErrorMessage m_CallbackMessage End Sub Private Sub IFM2DeviceEvents_NotifyData(ByVal CallbackData As IFM2DeviceEvents) frmTest.NotifyData CallbackData End Sub Private Sub IFM2DeviceEvents_NotifyDeviceStatus(ByVal CallbackData As IFM2DeviceEvents, ByVal DevicesList As cFM2Devices) frmTest.NotifyDeviceStatus CallbackData, DevicesList End Sub Private Sub IFM2DeviceEvents_DisplayZeroDeviceProgressToClient() frmTest.DisplayZeroDeviceProgress m_CallbackMessage, m_ZeroDeviceTimeoutCounter End Sub ' Read/write properties Private Property Let IFM2DeviceEvents_CallbackEvent(ByVal RHS As String) m_CallbackEvent = RHS End Property Private Property Get IFM2DeviceEvents_CallbackEvent() As String IFM2DeviceEvents_CallbackEvent = m_CallbackEvent End Property ... -- http://mail.python.org/mailman/listinfo/python-list
Re: Help in language development
[Portuguese talking, excuse me guys] Pedro, vocês podem dar um olhada no bug tracking, e estudar os códigos pra encontrar soluções. 2010/11/16 pedro igor sampaio avelino > Hello, my name Pedro Igor, I am a student and develop applications in > python for 1 year. I enrolled in the group to contribute in developing > this wonderful language that helps me both in day-to-day, but I'm > going through some difficulties because they do not know where to > start, can someone please give me some steps so that I can contribute > at once development for all, where do I start, if the documentation or > a book. I know that most people have more important issues to address > in other posts but assistance does not cost anything. > > Graciously > -- > http://mail.python.org/mailman/listinfo/python-list > -- Felipe Bastos Nunes -- http://mail.python.org/mailman/listinfo/python-list
Python OGL package
Hi list, could someone recommend a good python ogl package (open source is preferred) ? I found these ones so far: http://pyopengl.sourceforge.net/ http://wiki.wxpython.org/wxOGL thanks -mab -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 12:54 pm, Ian wrote: > On Nov 16, 1:00 pm, fuglyducky wrote: > > > Before I added the second table I could simply run > > 'print(cursor.lastrowid)' and it would give me the id number. However, > > with two tables I am unable to do this. > > It would help if you would show the code where you're trying to do > this. Without your actual code to look at, we can't tell you why it > doesn't work. > > > Does anyone know if there is a way to reference one table or another > > to get lastrowid? > > cursor.lastrowid is always in reference to the last query executed by > the cursor, not in reference to a table. If you don't capture the > value, and then you execute another query on the same cursor, the > previous value of cursor.lastrowid no longer exists. If you need it > now, then either you should have captured it when you had the chance, > or you should not have executed another query on the same cursor. > > But perhaps this is not what you're actually trying to do. I can't > tell, because I haven't seen the code. > > Cheers, > Ian Thanks for the input. Sorry...I should have included the code...it's just a simple query... # import sqlite3 import random db_connect = sqlite3.connect('test.db') cursor = db_connect.cursor() print(cursor.lastrowid) # Choose random index from DB - need to understand lastrowid #row_count = cursor.lastrowid #random_row = random.randrange(0, row_count) cursor.execute("SELECT * FROM table1 WHERE id = 2002") print(cursor.fetchmany()) #for row in cursor: # print(row) db_connect.commit() cursor.close() # -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Tue, 16 Nov 2010 13:08:15 -0800, fuglyducky wrote: > On Nov 16, 12:54 pm, Ian wrote: >> On Nov 16, 1:00 pm, fuglyducky wrote: >> >> > Before I added the second table I could simply run >> > 'print(cursor.lastrowid)' and it would give me the id number. >> > However, with two tables I am unable to do this. >> >> It would help if you would show the code where you're trying to do >> this. Without your actual code to look at, we can't tell you why it >> doesn't work. >> >> > Does anyone know if there is a way to reference one table or another >> > to get lastrowid? >> >> cursor.lastrowid is always in reference to the last query executed by >> the cursor, not in reference to a table. If you don't capture the >> value, and then you execute another query on the same cursor, the >> previous value of cursor.lastrowid no longer exists. If you need it >> now, then either you should have captured it when you had the chance, >> or you should not have executed another query on the same cursor. >> >> But perhaps this is not what you're actually trying to do. I can't >> tell, because I haven't seen the code. >> >> Cheers, >> Ian > > Thanks for the input. Sorry...I should have included the code...it's > just a simple query... > > # > > import sqlite3 > import random > > db_connect = sqlite3.connect('test.db') cursor = db_connect.cursor() > > print(cursor.lastrowid) > > # Choose random index from DB - need to understand lastrowid #row_count > = cursor.lastrowid > #random_row = random.randrange(0, row_count) > > cursor.execute("SELECT * FROM table1 WHERE id = 2002") > print(cursor.fetchmany()) Perhaps insert here: cursor.execute("SELECT count() from table2") table2lastRow = cursor.lastrowid() > > #for row in cursor: > # print(row) > > > db_connect.commit() > cursor.close() > > # -- http://mail.python.org/mailman/listinfo/python-list
RE: What was your strategy?
It helps to try to solve a real (to you) problem, that way you discover what you don't know. If your code ends up nested 3 levels or your methods are more than 10 lines, ask for help. -Original Message- From: python-list-bounces+frsells=adventistcare@python.org [mailto:python-list-bounces+frsells=adventistcare@python.org] On Behalf Of Jorge Biquez Sent: Sunday, November 14, 2010 5:32 PM To: python-list@python.org Subject: What was your strategy? Hello all. Quick question. I know some of you are with Python since started, some other maybe later. I was wondering if you can share what was the strategy you followed to master Python (Yes I know I have to work hard study and practice a lot). I mean did you use special books, special sites, a plan to learn each subject in a special way. I would like to know, if possible, comments specially from some of you who in the past had other languages, frameworks and platforms and left (almost) all of them and stayed with Python. Thanks in advance Jorge Biquez -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 2:08 pm, fuglyducky wrote: > db_connect = sqlite3.connect('test.db') > cursor = db_connect.cursor() > > print(cursor.lastrowid) At this point you haven't executed a query yet, so there is no meaningful value that cursor.lastrowid can take. > # Choose random index from DB - need to understand lastrowid > #row_count = cursor.lastrowid > #random_row = random.randrange(0, row_count) This is wrong. The lastrowid is not the number of rows in the table. It's the row-id of the row that was inserted (if any) by the last query executed by the cursor. In the case of sqlite3, I think the row- id is just the primary key if it's an integer (but in general the row- id is database-dependent), so you *might* be able to get away with it if you always let it autoincrement when inserting, never delete any rows, and never change their primary keys. But it's unreliable and only available immediately after an insert, so don't do it that way. The proper way to get the number of rows is to use the COUNT aggregate function, e.g., "SELECT COUNT(*) FROM TABLE1", which will return a single row with a single column containing the number of rows in table1. Cheers, Ian -- http://mail.python.org/mailman/listinfo/python-list
Raw Unicode docstring
Hello, how does one write a raw unicode docstring? If I have backslashes in the docstring, I must tuck an 'r' in front of it, like this: r"""This is a raw docstring.""" If I have foreign letters in the docstring, I must tuck a 'u' in front of it, like this: u"""This is a Unicode docstring.""" What if I have foreign characters *and* backslashes in my docstring? How to write the docstring then? ru"""My raw unicode docstring.""" or ur"""My unicode docstring.""" Please answer my question, although it may sound like a noobish one. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python OGL package
Also try Pyglet, in combination of PyOpenGL. Cheers, Xav On 17 November 2010 04:36, Marc-Andre Belzile < marc-andre.belz...@autodesk.com> wrote: > Hi list, > > > > could someone recommend a good python ogl package (open source is > preferred) ? I found these ones so far: > > > > http://pyopengl.sourceforge.net/ > > http://wiki.wxpython.org/wxOGL > > > > thanks > > > > -mab > > -- > http://mail.python.org/mailman/listinfo/python-list > > -- http://mail.python.org/mailman/listinfo/python-list
Re: Komodo 6, why "Workspace Restore" after every restart?
[overquoting follows to prove my point] Someone who claims to care as much as you do about Usenet ought to have better quoting habits. You should be ashamed of yourself. In article <4ce0f788$0$30018$c3e8da3$76a7c...@news.astraweb.com>, John Doe wrote: >alex23 wrote: > >> John Doe wrote: > >>> Google Groups spews a massive amount of sewage onto UseNet >>> everyday. Google Groups is your news server. You should be >>> ashamed >> >> Get over yourself. > >You too, and stop using the spammers den while you are at it. >-- > > > > > > > > > > > > > > > > >See also googled groups >> Path: >> news.astraweb.com!border5.newsrouter.astraweb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!g20g2000prg.googlegroups.com!not-for-mail >> From: alex23 >> Newsgroups: comp.lang.python >> Subject: Re: Komodo 6, why "Workspace Restore" after every restart? >> Date: Mon, 15 Nov 2010 00:42:36 -0800 (PST) >> Organization: http://groups.google.com >> Lines: 6 >> Message-ID: <15016ae4-b341-4f4e-ac84-62fe7f93f217 >> g20g2000prg.googlegroups.com> >> References: <4ce07839$0$17214$c3e8da3$eb767761 news.astraweb.com> >> <428b98ca-72bf-4e3b-ad8a-dafe2fc4d553 v28g2000prn.googlegroups.com> >> <4ce0c065$0$23246$c3e8da3$a9097924 news.astraweb.com> >> <6ae54329-c652-4a59-b0e2-4c2038f58d5a x4g2000pre.googlegroups.com> >> <4ce0ea79$0$29271$c3e8da3$88b277c5 news.astraweb.com> >> NNTP-Posting-Host: 115.64.196.128 >> Mime-Version: 1.0 >> Content-Type: text/plain; charset=ISO-8859-1 >> X-Trace: posting.google.com 1289810556 23916 127.0.0.1 (15 Nov 2010 08:42:36 >> GMT) >> X-Complaints-To: groups-abuse google.com >> NNTP-Posting-Date: Mon, 15 Nov 2010 08:42:36 + (UTC) >> Complaints-To: groups-abuse google.com >> Injection-Info: g20g2000prg.googlegroups.com; posting-host=115.64.196.128; >> posting-account=rYyWJQoAAACVJO77HvcyJfa3TnGYCqK_ >> User-Agent: G2/1.0 >> X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) >> AppleWebKit/534.12 (KHTML, like Gecko) Chrome/9.0.576.0 >> Safari/534.12,gzip(gfe) >> -- Aahz (a...@pythoncraft.com) <*> http://www.pythoncraft.com/ "Look, it's your affair if you want to play with five people, but don't go calling it doubles." --John Cleese anticipates Usenet -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Unicode docstring
On 16/11/2010 21:56, Boštjan Mejak wrote: Hello, how does one write a raw unicode docstring? If I have backslashes in the docstring, I must tuck an 'r' in front of it, like this: r"""This is a raw docstring.""" If I have foreign letters in the docstring, I must tuck a 'u' in front of it, like this: u"""This is a Unicode docstring.""" What if I have foreign characters *and* backslashes in my docstring? How to write the docstring then? ru"""My raw unicode docstring.""" or ur"""My unicode docstring.""" Please answer my question, although it may sound like a noobish one. Thanks. Why not try them both and see what happens? -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Unicode docstring
On 11/16/10 3:56 PM, Boštjan Mejak wrote: Hello, how does one write a raw unicode docstring? If I have backslashes in the docstring, I must tuck an 'r' in front of it, like this: r"""This is a raw docstring.""" If I have foreign letters in the docstring, I must tuck a 'u' in front of it, like this: u"""This is a Unicode docstring.""" What if I have foreign characters *and* backslashes in my docstring? I would just use a double-backslash u'\\' for each backslash that is supposed to be in the actual string. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Unicode docstring
On 11/16/2010 4:56 PM, Boštjan Mejak wrote: Hello, how does one write a raw unicode docstring? If I have backslashes in the docstring, I must tuck an 'r' in front of it, like this: r"""This is a raw docstring.""" You only need (and want that) if you want '\' to be taken literally. And even if you do, '\\' will put one '\' in the string, so raw mode is never needed; it is only a convenience when one needs lots of literal backslashes. If I have foreign letters in the docstring, I must tuck a 'u' in front of it, like this: u"""This is a Unicode docstring.""" What if I have foreign characters *and* backslashes in my docstring? How to write the docstring then? ru"""My raw unicode docstring.""" or ur"""My unicode docstring.""" Please answer my question, although it may sound like a noobish one. Thanks. You could look in the manual, which has been improved in this regard for 2.7.1. Or you could try both and see which gives a syntax error ;=). That would have been faster than posting this. -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Unicode docstring
On 16.11.2010 22:56, Boštjan Mejak wrote: Hello, how does one write a raw unicode docstring? If I have backslashes in the docstring, I must tuck an 'r' in front of it, like this: r"""This is a raw docstring.""" If I have foreign letters in the docstring, I must tuck a 'u' in front of it, like this: u"""This is a Unicode docstring.""" What if I have foreign characters *and* backslashes in my docstring? How to write the docstring then? ru"""My raw unicode docstring.""" or ur"""My unicode docstring.""" Please answer my question, although it may sound like a noobish one. Thanks. One of Python's main strength is it's brilliant interactive mode, where you can easily try things out (Hint: Try ipython for an even better interactive experience) Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ru"Scheißt\nderBär\nim Wald?" File "", line 1 ru"Scheißt\nderBär\nim Wald?" ^ SyntaxError: invalid syntax >>> ur"Scheißt\nderBär\nim Wald?" u'Schei\xdft\\nderB\xe4r\\nim Wald?' >>> Looks like ur"" works fine. -- http://mail.python.org/mailman/listinfo/python-list
Is Unladen Swallow dead?
There has been little or no activity at all in this project in the last months, and the last comments on their mailing list seem to conrfim that it's future is uncertain. It's also very strange the lack of updates, news or discussions, specially considering that the merging plan has been approved. Or it hasn't? -- http://mail.python.org/mailman/listinfo/python-list
Re: Some syntactic sugar proposals
On Nov 14, 11:30 pm, alex23 wrote: > On Nov 15, 4:39 pm, Dmitry Groshev wrote: > > > if x in range(a, b): #wrong! > > Only in Python 3.x, it's perfectly valid in Python 2.x. To achieve the > same in Python 3.x, try: > > if x in list(range(a, b,)): # BUT SEE MY COMMENT BELOW > > > it feels so natural to check it that way, but we have to write > > if a <= x <= b > > I understand that it's not a big deal, but it would be awesome to have > > some optimisations - it's clearly possible to detect things like that > > "wrong" one and fix it in a bytecode. > > This seems more like a pessimisation to me: your range version > constructs a list just to do a single container check. That's a _lot_ > more cumbersome than two simple comparisons chained together. Also: testing for the membership of x in a set is NOT the same thing as testing using inequality operators. The inequality operators will return True for any FLOATING-POINT value within the range (a...b), but the set test will only return True for set members. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Unladen Swallow dead?
I'm not a contributor to the U-S project, but I have been monitoring alternative python implementations' progress some, and seem to be adding something to pypy all of a sudden. I think unladen swallow has produced performance improvements, and they are likely to be merged into cpython 3.3. However, the improvements are not quite as substantial as were hoped for, and pypy seems to be getting some steam behind it. With pypy looking likely to even be able to provide source-level compatibility with C extension modules, the need for unladen swallow is perhaps somewhat lessened. At the outset, the U-S people looked at pypy and got the impression it was years from being a fast alternative to cpython for production code, but it sounds like pypy's coming along nicely now. Then again, the pypy people don't seem to feel that C extensions are going to perform that well in pypy (it sounds like an issue of initialization overhead and infeasibility of JIT compiling C extensions). I've been testing some of my own code in cpython 2.6, cpython 3.1 and pypy 1.3; with this specific program pypy seems to be about 4x faster than cpython 2.6, and almost that much faster than cpython 3.1 (I have the same - albeit unfinished - code running on all 3). This code is I/O intensive and somewhat math-intensive. On Tue, Nov 16, 2010 at 2:30 PM, laspi wrote: > There has been little or no activity at all in this project in the > last months, and the last comments on their mailing list seem to > conrfim that it's future is uncertain. > It's also very strange the lack of updates, news or discussions, > specially considering that the merging plan has been approved. Or it > hasn't? > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: SQLite3 and lastrowid
On Nov 16, 1:52 pm, Ian wrote: > On Nov 16, 2:08 pm, fuglyducky wrote: > > > db_connect = sqlite3.connect('test.db') > > cursor = db_connect.cursor() > > > print(cursor.lastrowid) > > At this point you haven't executed a query yet, so there is no > meaningful value that cursor.lastrowid can take. > > > # Choose random index from DB - need to understand lastrowid > > #row_count = cursor.lastrowid > > #random_row = random.randrange(0, row_count) > > This is wrong. The lastrowid is not the number of rows in the table. > It's the row-id of the row that was inserted (if any) by the last > query executed by the cursor. In the case of sqlite3, I think the row- > id is just the primary key if it's an integer (but in general the row- > id is database-dependent), so you *might* be able to get away with it > if you always let it autoincrement when inserting, never delete any > rows, and never change their primary keys. But it's unreliable and > only available immediately after an insert, so don't do it that way. > > The proper way to get the number of rows is to use the COUNT aggregate > function, e.g., "SELECT COUNT(*) FROM TABLE1", which will return a > single row with a single column containing the number of rows in > table1. > > Cheers, > Ian Ahhh...great...thanks for the info! I'll do the row count then!!! -- http://mail.python.org/mailman/listinfo/python-list
Re: argparse subparser problem
On Tue, 16 Nov 2010, Neal Becker wrote: > I want to have subparsers, but I also want to be able to say: > > myprogram --version > and get the version # > > --- [...] AFAIK, it is not possible ATM to have optional subparsers with argparse: http://code.google.com/p/argparse/issues/detail?id=47 To do something like what you want, I've done my own pre-parsing of the command-line to separate out sub-options, and then sent them to multiple separate top-level ArgumentParser instances. Regards, john -- http://mail.python.org/mailman/listinfo/python-list
Re: QT window closes immediately
Martin Caum writes: > I am attempting to open a window on mouse activity which works, but > the window fails to stay open. > I set it to terminate when the escape key is pressed even when the > program is not currently selected. This works fine. Originally I had > it create the window only with a right click, but when I noticed the > window closed immediately I set it to any mouse activity for easier > debugging. My script is as follows: > [code] > import pythoncom, pyHook > import sys > import win32api > from PyQt4 import QtGui, QtCore > > class WindowObject(QtGui.QWidget): > def __init__(self, parent=None): > QtGui.QWidget.__init__(self, parent) > self.setWindowTitle('Window') > self.resize(50, 250) > > def OnKeyboardEvent(event): > if event.KeyID == 27: > win32api.PostQuitMessage() > return 1 > > def OnMouseEvent(event): > x = event.Position[0] > y = event.Position[1] > createWindow(x, y) > return 1 > > def createWindow(x, y): > menu = WindowObject() > menu.move(x, y) > menu.show() > > hm = pyHook.HookManager() > hm.MouseAll = OnMouseEvent > hm.KeyDown = OnKeyboardEvent > hm.HookMouse() > hm.HookKeyboard() > > app = QtGui.QApplication(sys.argv) > pythoncom.PumpMessages()[/code] > I'm fairly certain that this is due to my lack of understanding in the > PumpMessages command and the HookManager. Any advice? 1) Keep a reference to your window-object. I can only guess (PyQt not being a regular topic when hacking for me.. .sadly), but GC can interfere with these kind of things. 2) what is the win32 stuff about? Qt should abstract from that. 3) if in doubt, as on the PyQt mailing list. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: QT window closes immediately
On 11/15/2010 02:04 AM, Martin Caum wrote: > I am attempting to open a window on mouse activity which works, but > the window fails to stay open. > I set it to terminate when the escape key is pressed even when the > program is not currently selected. This works fine. Originally I had > it create the window only with a right click, but when I noticed the > window closed immediately I set it to any mouse activity for easier > debugging. My script is as follows: > [code] > import pythoncom, pyHook > import sys > import win32api > from PyQt4 import QtGui, QtCore > > class WindowObject(QtGui.QWidget): > def __init__(self, parent=None): > QtGui.QWidget.__init__(self, parent) > self.setWindowTitle('Window') > self.resize(50, 250) > > def OnKeyboardEvent(event): > if event.KeyID == 27: > win32api.PostQuitMessage() > return 1 > > def OnMouseEvent(event): > x = event.Position[0] > y = event.Position[1] > createWindow(x, y) > return 1 > > def createWindow(x, y): > menu = WindowObject() > menu.move(x, y) > menu.show() > > hm = pyHook.HookManager() > hm.MouseAll = OnMouseEvent > hm.KeyDown = OnKeyboardEvent > hm.HookMouse() > hm.HookKeyboard() > > app = QtGui.QApplication(sys.argv) > pythoncom.PumpMessages()[/code] > I'm fairly certain that this is due to my lack of understanding in the > PumpMessages command and the HookManager. Any advice? Shouldn't every QT application have an event loop. I would have expecte a call to app._exec() as you need already pumpMessages() fro the COM interface, I assume you had to use the threading or multiprocessing module as well. -- http://mail.python.org/mailman/listinfo/python-list
Cannot Remove File: Device or resource busy
I'm spawning a subprocess to fix some formating errors with a library of PDFs with pdftk: try: sp = subprocess.Popen('pdftk.exe "%s" output %s' % (pdfFile, outputFile)) sp.wait() del sp except Exception, e: return "Unable to open file: %s with error: %s" % (pdfFile, str(e)) And then I test the result: try: pdf_handle = open(outputFile, "rb") pdf_pypdf = PdfFileReader(pdf_handle) del pdf_pypdf del pdf_handle except Exception, e: return "Unable to open file: %s with error: %s" % (outputFile, str(e)) Both of which appear to work. But when I try to delete the original pdfFile, I get an error message saying that the file is still in use. if I use: sp = subprocess.Popen('rm "%s"' % pdfFile) sp.wait() I get the message - the standard error message from rm and if I use: cwd = os.getcwd() os.remove(cwd + "\\" + pdfFile) I get "WindowsError: [Error 32]" saying much the same thing. What am I missing? Any suggestions would be appreciated. Details: Python 2.6 Windows XP Sincerely, Brett Bowman -- http://mail.python.org/mailman/listinfo/python-list
Re: Cannot Remove File: Device or resource busy
On 17/11/2010 01:37, Brett Bowman wrote: I'm spawning a subprocess to fix some formating errors with a library of PDFs with pdftk: try: sp = subprocess.Popen('pdftk.exe "%s" output %s' % (pdfFile, outputFile)) sp.wait() del sp except Exception, e: return "Unable to open file: %s with error: %s" % (pdfFile, str(e)) And then I test the result: try: pdf_handle = open(outputFile, "rb") pdf_pypdf = PdfFileReader(pdf_handle) del pdf_pypdf del pdf_handle except Exception, e: return "Unable to open file: %s with error: %s" % (outputFile, str(e)) Both of which appear to work. But when I try to delete the original pdfFile, I get an error message saying that the file is still in use. if I use: sp = subprocess.Popen('rm "%s"' % pdfFile) sp.wait() I get the message - the standard error message from rm and if I use: cwd = os.getcwd() os.remove(cwd + "\\" + pdfFile) I get "WindowsError: [Error 32]" saying much the same thing. What am I missing? Any suggestions would be appreciated. Details: Python 2.6 Windows XP Try Process Explorer, available from Microsoft at: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx and use Find. That will tell you what has it open. -- http://mail.python.org/mailman/listinfo/python-list
strange subprocess behavior when calling ps
Hi all, I have encountered a strange problem with some code I am writing to search the system process list for certain running processes. I am using subprocess.Popen() to call '/bin/ps -e'. When I save my code to the file pid.py (whose first line is #!/usr/bin/python) and run it with the command % ./pid.py it works perfectly fine, retrieving lines from my pipe to the /bin/ps output which look exactly as if I had typed the command '/bin/ps -e' myself into a shell window. Here is a sample line from that output: 1891 ttys0000:00.12 -tcsh Now for the weird part -- when I run this code using the command % python pid.py I get entirely different output. It only prints out a very few processes instead of the entire table, and each line also has lots of environment variable values displayed. Here is the line from that output which corresponds to the line immediately above: 1891 s000 S+ 0:00.12 -tcsh PATH=/usr/bin:/bin:/usr/sbin:/sbin TMPDIR=/var/folders/3e/3e-TyTQIG-aOa4x37pbTbk++-H6/-Tmp-/ SHELL=/bin/ tcsh HOME=/Users/hmrgsoft USER=hmrgsoft LOGNAME=hmrgsoft DISPLAY=/tmp/ launch-c1YZNr/org.x:0 SSH_AUTH_SOCK=/tmp/launch-AJ9xbl/Listeners Apple_PubSub_Socket_Render=/tmp/launch-BsRx5Y/Render COMMAND_MODE=unix2003 __CF_USER_TEXT_ENCODING=0x532:0:0 TERM_PROGRAM=Apple_Terminal TERM_PROGRAM_VERSION=273 LANG=en_US.UTF-8 TERM=xterm-color It's like it's calling up an entirely different ps, or passing it different command arguments. In both cases, however, I am explicitly calling /bin/ps with the same -e argument, and there appear to be no other ps commands on my system, neither do I appear to have any ps builtin command in any shell. I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have appended the code below. I am running both commands directly in a Terminal window running tcsh. Can anyone explain this? Thanks! Roger Davis # code follows #!/usr/bin/python import sys import subprocess def main(): psargs= ["/bin/ps", "-e"] try: ps= subprocess.Popen(psargs, stdout=subprocess.PIPE, close_fds=True) psout= ps.communicate()[0] pslines= psout.splitlines() for line in pslines: print "%s" % line except KeyboardInterrupt: print "Keyboard interrupt received -- terminating." sys.stdout.flush() sys.exit(-1) except: print "%s: unexpected error in generation of system process list" % prognm sys.stdout.flush() sys.exit(-1) main() -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
On Wed, Nov 17, 2010 at 12:33 PM, Roger Davis wrote: > Hi all, > > I have encountered a strange problem with some code I am writing to > search the system process list for certain running processes. I am > using subprocess.Popen() to call '/bin/ps -e'. When I save my code to > the file pid.py (whose first line is #!/usr/bin/python) and run it > with the command > > % ./pid.py > > it works perfectly fine, retrieving lines from my pipe to the /bin/ps > output which look exactly as if I had typed the command '/bin/ps -e' > myself into a shell window. Here is a sample line from that output: > > 1891 ttys000 0:00.12 -tcsh > > Now for the weird part -- when I run this code using the command > > % python pid.py > > I get entirely different output. It only prints out a very few > processes instead of the entire table, and each line also has lots of > environment variable values displayed. Here is the line from that > output which corresponds to the line immediately above: > > 1891 s000 S+ 0:00.12 -tcsh PATH=/usr/bin:/bin:/usr/sbin:/sbin > TMPDIR=/var/folders/3e/3e-TyTQIG-aOa4x37pbTbk++-H6/-Tmp-/ SHELL=/bin/ > tcsh HOME=/Users/hmrgsoft USER=hmrgsoft LOGNAME=hmrgsoft DISPLAY=/tmp/ > launch-c1YZNr/org.x:0 SSH_AUTH_SOCK=/tmp/launch-AJ9xbl/Listeners > Apple_PubSub_Socket_Render=/tmp/launch-BsRx5Y/Render > COMMAND_MODE=unix2003 __CF_USER_TEXT_ENCODING=0x532:0:0 > TERM_PROGRAM=Apple_Terminal TERM_PROGRAM_VERSION=273 LANG=en_US.UTF-8 > TERM=xterm-color > > It's like it's calling up an entirely different ps, or passing it > different command arguments. In both cases, however, I am explicitly > calling /bin/ps with the same -e argument, and there appear to be no > other ps commands on my system, neither do I appear to have any ps > builtin command in any shell. > > I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have > appended the code below. I am running both commands directly in a > Terminal window running tcsh. > > Can anyone explain this? Thanks! > > Roger Davis > > # code follows > > #!/usr/bin/python > import sys > import subprocess > > def main(): > > psargs= ["/bin/ps", "-e"] > try: > ps= subprocess.Popen(psargs, stdout=subprocess.PIPE, > close_fds=True) > psout= ps.communicate()[0] > pslines= psout.splitlines() > for line in pslines: > print "%s" % line > except KeyboardInterrupt: > print "Keyboard interrupt received -- terminating." > sys.stdout.flush() > sys.exit(-1) > except: > print "%s: unexpected error in generation of system process > list" % > prognm > sys.stdout.flush() > sys.exit(-1) > > main() Roger, why not use the nicely written and documented psutil module ? http://pypi.python.org/pypi/psutil/0.2.0 http://code.google.com/p/psutil/ cheers James -- -- James Mills -- -- "Problems are solved by method" -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
On Tue, Nov 16, 2010 at 6:33 PM, Roger Davis wrote: > Hi all, > > I have encountered a strange problem with some code I am writing to > search the system process list for certain running processes. I am > using subprocess.Popen() to call '/bin/ps -e'. When I save my code to > the file pid.py (whose first line is #!/usr/bin/python) and run it > with the command > > % ./pid.py > > it works perfectly fine, retrieving lines from my pipe to the /bin/ps > output which look exactly as if I had typed the command '/bin/ps -e' > myself into a shell window. Here is a sample line from that output: > > 1891 ttys000 0:00.12 -tcsh > > Now for the weird part -- when I run this code using the command > > % python pid.py > > I get entirely different output. It only prints out a very few > processes instead of the entire table, and each line also has lots of > environment variable values displayed. > It's like it's calling up an entirely different ps, or passing it > different command arguments. In both cases, however, I am explicitly > calling /bin/ps with the same -e argument, and there appear to be no > other ps commands on my system, neither do I appear to have any ps > builtin command in any shell. > > I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have > appended the code below. I am running both commands directly in a > Terminal window running tcsh. > > Can anyone explain this? Thanks! > # code follows > > #!/usr/bin/python Have you checked whether those commands are running under the same Python? What output do you get from tcsh for the following?: which python python -V /usr/bin/python -V ls -l /usr/bin/python Also, did you upgrade your system Python or something? I'm running Mac OS 10.6.5 and the built-in /usr/bin/python is v2.6.1, so I find the implied claim that your /usr/bin/python is v2.6.6 to be rather bizarre. I am unable to reproduce your problem with either my v2.6.1 system Python or my v2.6.6 Python from Fink. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
Thanks, Chris, you're at least on the right track. I did upgrade from python.org and the python in my shell PATH is /Library/Frameworks/ Python.framework/Versions/2.6/bin/python: % python Python 2.6.6 (r266:84374, Aug 31 2010, 11:00:51) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin /usr/bin/python is the Apple-distributed 2.6.1: % /usr/bin/python Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin This still doesn't explain the behavior to me, however. In either case python is supposed to be forking a child process with a specific executable (/bin/ps) and a specific argument list (-e) and reading that command's output lines. Why should those output lines be different whether I use 2.6.1, 2.6.6 or 8.9.10 for that matter? In fact, this makes the problem that much worse -- the newer python 2.6.6 is the one producing the incorrect output. Changing the first line of the script to read #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python does not help, it still prints out the wrong output whether I do % ./pid.py or % /Library/Frameworks/Python.framework/Versions/2.6/bin/python ./ pid.py Any ideas? Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: how to use socket to get packet which destination ip is not local?
On Nov 16, 12:57 pm, MRAB wrote: > On 16/11/2010 20:38, Hans wrote: > > > Hi, > > > Maybe it's a very simple question. I'm trying to write a dhcpclient > > code with python. The dhcpclient does not have ip address at the very > > beginning, it sends out dhcpdiscover and then server sends back > > dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but > > that ip is not client's local ip yet. How can I make my socket to > > receive that packet? > > > I tried socket bind to 0.0.0.0, but it only binds to any local ip, not > > any ip which may not be local. therefore the socket cannot get that > > dhcp offer packet even I can use wireshark to see that packet did come > > to this pc. > > > How to solve it? Thanks in advance. > > Have you read this: > > http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol Thanks for the response. Yes, I read it. it tells me how dhcp works. But I cannot find answer for my question, dhcp response may be unicast or broadcast depends on the flag in request. my question is how to use socket to read that response. In my test, sniffer already shows server sends back an offer response(unicast in my case), but because the dst- ip in response is not local ip, then my socket cannot read it. Can you help? thank you very much! -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
In article <55f26d5c-aba9-4892-9e2c-1caa9988e...@v23g2000vbi.googlegroups.com>, Roger Davis wrote: > I have encountered a strange problem with some code I am writing to > search the system process list for certain running processes. I am > using subprocess.Popen() to call '/bin/ps -e'. When I save my code to > the file pid.py (whose first line is #!/usr/bin/python) and run it > with the command > > % ./pid.py > > it works perfectly fine, retrieving lines from my pipe to the /bin/ps > output which look exactly as if I had typed the command '/bin/ps -e' > myself into a shell window. Here is a sample line from that output: > > 1891 ttys0000:00.12 -tcsh > > Now for the weird part -- when I run this code using the command > > % python pid.py > > I get entirely different output. It only prints out a very few > processes instead of the entire table, and each line also has lots of > environment variable values displayed. Here is the line from that > output which corresponds to the line immediately above: > > 1891 s000 S+ 0:00.12 -tcsh PATH=/usr/bin:/bin:/usr/sbin:/sbin > TMPDIR=/var/folders/3e/3e-TyTQIG-aOa4x37pbTbk++-H6/-Tmp-/ SHELL=/bin/ > tcsh HOME=/Users/hmrgsoft USER=hmrgsoft LOGNAME=hmrgsoft DISPLAY=/tmp/ > launch-c1YZNr/org.x:0 SSH_AUTH_SOCK=/tmp/launch-AJ9xbl/Listeners > Apple_PubSub_Socket_Render=/tmp/launch-BsRx5Y/Render > COMMAND_MODE=unix2003 __CF_USER_TEXT_ENCODING=0x532:0:0 > TERM_PROGRAM=Apple_Terminal TERM_PROGRAM_VERSION=273 LANG=en_US.UTF-8 > TERM=xterm-color > > It's like it's calling up an entirely different ps, or passing it > different command arguments. In both cases, however, I am explicitly > calling /bin/ps with the same -e argument, and there appear to be no > other ps commands on my system, neither do I appear to have any ps > builtin command in any shell. > > I am running 2.6.6 under MacOS 10.6.4 on a MacBook Pro Intel. I have > appended the code below. I am running both commands directly in a > Terminal window running tcsh. See "man compat". What you are seeing is the difference between ps(1) output in "legacy" mode, attempting to duplicate the old, non-POSIX behavior from 10.3 days, or "unix2003" mode. Terminal login sessions are normally automatically started with the COMMAND_MODE environment variable set: $ echo $COMMAND_MODE unix2003 Adding an "env={"COMMAND_MODE": "unix2003"}" argument to your subprocess Popen call should do the trick. -- Ned Deily, n...@acm.org -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
Hi James, Thanks for the pointer to psutil. I actually did look around on python.org before coding this up to see if there was such a package available but there is not, at least not where I'm looking -- on the other hand, I can't find my car keys most of the time. I would really like to restrict my code dependencies to the standard Python distribution (if there actually is such a thing, I'm new to the language). Of course, that's assuming that I can actually get my code to work in that fashion. If I can't get to the bottom of this issue and figure out why my existing code does not work I will investigate psutil further. Thanks! Roger -- http://mail.python.org/mailman/listinfo/python-list
Re: how to use socket to get packet which destination ip is not local?
libpcap On Wed, Nov 17, 2010 at 4:57 AM, MRAB wrote: > On 16/11/2010 20:38, Hans wrote: > >> Hi, >> >> Maybe it's a very simple question. I'm trying to write a dhcpclient >> code with python. The dhcpclient does not have ip address at the very >> beginning, it sends out dhcpdiscover and then server sends back >> dhcpoffer. the dhcpoffer will use assigned ip as destination ip, but >> that ip is not client's local ip yet. How can I make my socket to >> receive that packet? >> >> I tried socket bind to 0.0.0.0, but it only binds to any local ip, not >> any ip which may not be local. therefore the socket cannot get that >> dhcp offer packet even I can use wireshark to see that packet did come >> to this pc. >> >> How to solve it? Thanks in advance. >> >> Have you read this: > >http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: remote server and effective uid
"Tim Harig" wrote in message news:ibs8h9$jm...@speranza.aioe.org... > On 2010-11-15, Tim Arnold wrote: >> On Nov 15, 10:41 am, Tim Harig wrote: >>> On 2010-11-15, Tim Arnold wrote: >>> >>> > How can I enable the server process to write into the client's >>> > directories? >>> > If I change the inetd service to run as 'root', I guess that would >>> > work, but then the client couldn't remove the files put there after >>> > the request. >>> >>> Python provides os.setuid() and os.seteuid() which wrap the system >>> functions. See you systems man pages for these functions for more >>> information. >> >> Thanks -- that was a lot easier than I thought it was going to be. >> pass the client's uid in the message to the server like so >> >> argstring, local_dir, uid = message.split(':') >> os.seteuid(int(uid)) > > I am not sure exactly what you are doing; but, I would advise great > caution as messing this up could easily open your system to exploitation. > Be very sure that you know what you are doing. I can see how that looks dangerous, but I think it's okay. I have inetd listening on a port and whatever it receives, it passes on to that line above "argstring, local_dir, uid message.split(':'). The argstring is parsed using 'argparse' the resulting list of args is passed to a Python class that can only do work for a specific set of args. I can't think of a way someone could pass in an evil argstring that could do anything but fail. Thanks for your reply, and if you still think it's dangerous please let me know. --Tim -- http://mail.python.org/mailman/listinfo/python-list
Re: Raw Unicode docstring
On Nov 17, 9:34 am, Alexander Kapps wrote: > >>> ur"Scheißt\nderBär\nim Wald?" Nicht ohne eine Genehmigung von der Umwelt Erhaltung Abteilung. -- http://mail.python.org/mailman/listinfo/python-list
How can I catch segmentation fault in python?
Hi all, I am calling a program written in C inside Python using ctypes, and it seems that sometimes the program in C crashes while it's being used in Python. Even under the circumstances, I want to get the Python program going by handling the segmentation fault. I've already searched the Internet, but couldn't get the right answer to catch them. Could any of you please let me know how to deal with this and catch the segmentation fault in Python? Thanks, Justin. -- http://mail.python.org/mailman/listinfo/python-list
Re: remote server and effective uid
On Tue, Nov 16, 2010 at 9:37 AM, Tim Arnold wrote: > "Tim Harig" wrote in message > news:ibs8h9$jm...@speranza.aioe.org... > > On 2010-11-15, Tim Arnold wrote: > >> On Nov 15, 10:41 am, Tim Harig wrote: > >>> On 2010-11-15, Tim Arnold wrote: > >>> > >>> > How can I enable the server process to write into the client's > >>> > directories? > >>> > If I change the inetd service to run as 'root', I guess that would > >>> > work, but then the client couldn't remove the files put there after > >>> > the request. > >>> > >>> Python provides os.setuid() and os.seteuid() which wrap the system > >>> functions. See you systems man pages for these functions for more > >>> information. > >> > >> Thanks -- that was a lot easier than I thought it was going to be. > >> pass the client's uid in the message to the server like so > >> > >> argstring, local_dir, uid = message.split(':') > >> os.seteuid(int(uid)) > > > > I am not sure exactly what you are doing; but, I would advise great > > caution as messing this up could easily open your system to exploitation. > > Be very sure that you know what you are doing. > > I can see how that looks dangerous, but I think it's okay. I have inetd > listening on a port and whatever it receives, it passes on to that line > above "argstring, local_dir, uid message.split(':'). The argstring is > parsed using 'argparse' the resulting list of args is passed to a Python > class that can only do work for a specific set of args. I can't think of a > way someone could pass in an evil argstring that could do anything but > fail. > > Thanks for your reply, and if you still think it's dangerous please let me > know. > What if an attacker passes something other than their own uid across a socket to your inetd-launched process? EG, what if they pass a 0 (for root) instead of getuid()? It might be fine in your case, but in many cases, it's a problem. -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I catch segmentation fault in python?
On Nov 17, 10:26 am, justin wrote: > Hi all, > > I am calling a program written in C inside Python using ctypes, > and it seems that sometimes the program in C crashes while it's being > used in Python. > Even under the circumstances, I want to get the Python program going > by handling the segmentation fault. > > I've already searched the Internet, but couldn't get the right answer > to catch them. > Could any of you please let me know how to deal with this and catch > the segmentation fault in Python? > > Thanks, > Justin. Segmentation fault isn't exactly an exception that you can catch. It usually means something has gone horribly wrong, like dereferencing invalid pointer, trying to access memory out of process's range. Since if you run out of memory Python simply raises MemoryError exception, which you can catch. So that is not the case for segmentation fault. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Unladen Swallow dead?
On Nov 17, 3:30 am, laspi wrote: > There has been little or no activity at all in this project in the > last months, and the last comments on their mailing list seem to > conrfim that it's future is uncertain. > It's also very strange the lack of updates, news or discussions, > specially considering that the merging plan has been approved. Or it > hasn't? AFAIK, the merging plan was approved by Guido early this year. I guess Google is expecting the community to drive the project from here on. That was the whole idea for merging it to mainline. From my last conversation with Collin, they are targeting Python 3.3 -- http://mail.python.org/mailman/listinfo/python-list
Re: strange subprocess behavior when calling ps
Thanks, Ned! That really helps to explain what is going on. Now, just a couple more questions and I think I will know all I need to know. First, I *still* don't quite understand why this happens with my 2.6.6 interpreter but not my 2.6.1, and why another of the respondents to this thread (Chris) could not duplicate this problem with his own 2.6.6 environment. Is there something defective with my Python environment having to do with the 2.6.6 upgrade I installed directly from python.org? (Chris used Fink, apparently.) If so, how can I clean it up? I do have an easier time believing that this is a mangled installation issue now that the problem has essentially been reduced to the handling of an environment variable which I can well imagine could be easily garbled somewhere in a version mismatch scenario. Second, I do need to make this work on multiple platforms, primarily Linux in addition to the Mac but also with a potential variety of Python releases on both MacOS and Linux. I'm not sure if Linux will object to this COMMAND_MODE business, I guess I'll just have to try and see what happens. Any thoughts on that? Finally, to split hairs a bit concerning your suggested solution, I have a question about the additional env={'COMMAND_MODE': 'unix2003'} argument. This works for me, but my reading of the subprocess documentation leads me to believe that this will actually wipe out the entire environment that would otherwise be inherited from the parent and replace it with that single setting. I don't think this has any ill effects here with regard to ps, at least none I can detect at the moment, but wouldn't a perhaps safer solution be to do os.environ['COMMAND_MODE']= 'unix2003' prior to the Popen() to instead augment/correct the existing environment which will then later be inherited by the child, assuming no explicit env= optional argument is used? This also works for me, and I think I'm more inclined to go this route unless you can think of a good reason not to do so. Thanks very much for your help! -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I catch segmentation fault in python?
On Tue, Nov 16, 2010 at 9:26 PM, justin wrote: > Hi all, > > I am calling a program written in C inside Python using ctypes, > and it seems that sometimes the program in C crashes while it's being > used in Python. > Even under the circumstances, I want to get the Python program going > by handling the segmentation fault. > > I've already searched the Internet, but couldn't get the right answer > to catch them. > Could any of you please let me know how to deal with this and catch > the segmentation fault in Python? You can't "catch" it. It's not a mere Exception, it's a fatal error that effectively crashes the interpreter itself. At best, you could perhaps use the `multiprocessing` or `subprocess` modules to segregate the part of your program that uses ctypes and then try and recover when you detect that the subprocess has crashed. Instead, if possible, fix the C program and/or ensure that there's not an error in your ctypes interfacing code. Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Is Unladen Swallow dead?
On 11/16/2010 10:24 PM, swapnil wrote: On Nov 17, 3:30 am, laspi wrote: There has been little or no activity at all in this project in the last months, and the last comments on their mailing list seem to conrfim that it's future is uncertain. It's also very strange the lack of updates, news or discussions, specially considering that the merging plan has been approved. Or it hasn't? AFAIK, the merging plan was approved by Guido early this year. I guess Google is expecting the community to drive the project from here on. That was the whole idea for merging it to mainline. From my last conversation with Collin, they are targeting Python 3.3 I think it's dead. They're a year behind on quarterly releases. The last release was Q3 2009. The project failed to achieve its stated goal of a 5x speedup. Not even close. More like 1.5x (http://www.python.org/dev/peps/pep-3146) The Google blog at "http://groups.google.com/group/unladen-swallow/browse_thread/thread/f2011129c4414d04"; says, as of November 8, 2010: "Jeffrey and I have been pulled on to other projects of higher importance to Google. Unfortunately, no-one from the Python open-source community has been interested in picking up the merger work, and since none of the original team is still full-time on the project, it's moving very slowly. Finishing up the merger into the py3k-jit branch is a high priority for me this quarter, but what happens then is an open question." So Google has pulled the plug on Unladen Swallow. It looks like they underestimated the difficulty of speeding up the CPython model. The performance improvement achieved was so low that cluttering up CPython with a JIT system and LLVM probably is a lose. John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Re: How can I catch segmentation fault in python?
On 11/16/2010 10:15 PM, swapnil wrote: On Nov 17, 10:26 am, justin wrote: Hi all, I am calling a program written in C inside Python using ctypes, and it seems that sometimes the program in C crashes while it's being used in Python. Even under the circumstances, I want to get the Python program going by handling the segmentation fault. I've already searched the Internet, but couldn't get the right answer to catch them. Could any of you please let me know how to deal with this and catch the segmentation fault in Python? Thanks, Justin. Segmentation fault isn't exactly an exception that you can catch. It usually means something has gone horribly wrong, like dereferencing invalid pointer, trying to access memory out of process's range. Since if you run out of memory Python simply raises MemoryError exception, which you can catch. So that is not the case for segmentation fault. Either fix the program so it doesn't crash,or run the offending module and the C code in a subprocess. John Nagle -- http://mail.python.org/mailman/listinfo/python-list