Re: flattening a dict
On Feb 17, 3:56 am, Benjamin [EMAIL PROTECTED] wrote: How would I go about flattening a dict with many nested dicts within? The dicts might look like this: {mays : {eggs : spam}, jam : {soda : {love : dump}}, lamba : 23} I'd like it to put / inbetween the dicts to make it a one dimensional dict and look like this: {mays/eggs : spam, jam/soda/love : dump, lamba : 23 } In Python you can do anything, even flatten a dictionary: from itertools import chain def flattendict(d, pfx='', sep='/'): if isinstance(d, dict): if pfx: pfx += sep return chain(*(flattendict(v, pfx+k, sep) for k, v in d.iteritems())) else: return (pfx, d), test = {mays : {eggs : spam}, jam : {soda : {love : dump}}, lamba : 23 } print dict(flattendict(test)) {'lamba': 23, 'mays/eggs': 'spam', 'jam/soda/love': 'dump'} You an even have other separators ;) print dict(flattendict(test, sep='.')) {'lamba': 23, 'jam.soda.love': 'dump', 'mays.eggs': 'spam'} -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter. Why the Need for a Frame, or no Frame?
On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson wrote: from Tkinter import * class App: def __init__(self, master): fm = Frame(master) Button(fm, text='Left').pack(side=LEFT) Button(fm, text='This is the Center button').pack(side=LEFT) Button(fm, text='Right').pack(side=LEFT) fm.pack() root = Tk() root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 2) display = App(root) root.mainloop() The obvious question is: why don't you run both and see what happens? Anyway, Tk() already opens a frame, so in the first example the buttons are created inside that frame, while in the second example two frames are created: the one creaded by Tk() il left empty but you should see it (maybe very small in a corner) if you run the program. Ciao - FB -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
Arnaud Delobelle wrote: In Python you can do anything, even ...pass the Turing test with a one-liner. Back after 9/11, when US patriotism was the rage, Python knew how to answer correctly the query filter(lambda W : W not in 'ILLITERATE','BULLSHIT') And Python 3.0 slated for next August offers it a chance to change its mind before the next elections... Cheers, BB -- http://mail.python.org/mailman/listinfo/python-list
Re: sockets -- basic udp client
Paul Rubin wrote: [EMAIL PROTECTED] [EMAIL PROTECTED] writes: Historically, though, the ultimate authority on this kind of stuff is Richard Stevens and his Unix and TCP/IP books I recommend these books if you want to get into network programming. I keep wanting to get that book, but it gets older and older. Have things really not changed since it was written? TCP is much like a funicular railway: it's been around long enough that the basic engineering principles are known and the basic bugs have been ironed out. Stevens is still an excellent reference. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: class static variables and __dict__
On Feb 16, 11:59 pm, Zack [EMAIL PROTECTED] wrote: Zack wrote: Diez B. Roggisch wrote: Zack schrieb: If I have a class static variable it doesn't show up in the __dict__ of an instance of that class. class C: n = 4 x = C() print C.__dict__ {'__module__': '__main__', '__doc__': None, 'n': 4} print x.__dict__ {} This behavior makes sense to me as n is not encapsulated in x's namespace but what method can you use on x to find all available attributes for that class? x.__class__.__dict__ Diez This would leave out any attributes of base classes. Not that I asked for that functionality in my original post but is there a way to get all attributes qualified by x. ? I see that I could walk the dict of x, x.__class__ and x.__class__.__bases__ until I exhaust the tree. But is there a built in method for doing this? I believe this accomplishes what I'm looking for. I'm not positive it is correct or if there are cases I've missed. It would be nice if there is a simple python builtin for finding the fully qualified dict. def fullDict(obj): ''' Returns a dict with all attributes qualified by obj. obj is an instance of a class ''' d = obj.__dict__ # update existing items into new items to preserve inheritance tmpD = obj.__class__.__dict__ tmpD.update(d) d = tmpD supers = list(obj.__class__.__bases__) for c in supers: tmpD = c.__dict__ tmpD.update(d) d = tmpD supers.extend(c.__bases__) return d -- Zack Child class attributes override base class ones, so your function will get the wrong dict I think in cases like: class A(object): x = 1 class B(A): x = 2 Why not do something like this: def fulldict(obj): d = {} for t in reversed(type(obj).__mro__): d.update(t.__dict__) d.update(obj.__dict__) return d -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: xmltramp with python2.(4-5)
On 22:51, 16Feb2008, John Machin wrote: On Feb 17, 5:40 pm, Pradnyesh Sawant wrote: fire up python2.4 interactive prompt do this: import sys; sys.path import xmltramp; xmltramp.__file__ then fire up python2.5 interactive prompt do this: import sys; sys.path Hey, thanks a lot for that reply. it made me realise that xmltramp was something that is there in '/usr/lib/python2.4/site-packages/xmltramp.pyc' (a site-package for 2.4), and is hence not available for 2.5 it also showed me that I had _not_ installed it using apt for debian. unforutnately I dunno where I had got it from, and searching online didn't give me the result I wanted. anyways, thanks for leading me in the proper direction :) If that doesn't give you enough to nut out where xmltramp is and hence why it's on one sys.path and not on the other, come back with the full output from the above (including the info that python prints that will tell us helpful things like what platform you are on) -- http://mail.python.org/mailman/listinfo/python-list -- warm regards, Pradnyesh Sawant -- We are not just our behaviour. We are the person managing our behaviour. --The One Minute Manager signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
blog
I have new blog. please seemy blog and give me score for my blog. My blog is www.cahenom.blogspot.com -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with mod_python
On Feb 17, 3:29 pm, Pradnyesh Sawant [EMAIL PROTECTED] wrote: Hello, I have a small program which does 'import hashlib'. This program runs fine with python2.5. But when I try running the same program through mod_python, I get the error: 'ImportError: No module named hashlib' in the apache2 error.log Searching online suggested me to include md5.so or md5module.so in apache2. but I don't see that in a package for debian lenny (the system I'm using). So, my Q is, is it possible to make mod_python use the same PYTHONPATH as the python2.5 interpreter? if so, how? any other suggestions to solve the above problem are welcome too. thanks! Your mod_python isn't compiled against Python 2.5 but is using an older version. You will need to rebuild mod_python to use Python 2.5 instead. You cannot just point mod_python at the Python 2.5 module directories as they are incompatible. Graham -- http://mail.python.org/mailman/listinfo/python-list
Re: problem with mod_python
On 02:07, 17Feb2008, Graham Dumpleton wrote: On Feb 17, 3:29 pm, Pradnyesh Sawant wrote: Hello, Your mod_python isn't compiled against Python 2.5 but is using an older version. You will need to rebuild mod_python to use Python 2.5 instead. You cannot just point mod_python at the Python 2.5 module directories as they are incompatible. Thanks a lot! You were correct in pointing out the problem. I've found out that hashlib is present in python2.5, and not in python2.4. I've decided to go with python2.4, for the present, and use 'md5' instead of 'hashlib.md5'. thanks again! Graham -- http://mail.python.org/mailman/listinfo/python-list -- warm regards, Pradnyesh Sawant -- We are not just our behaviour. We are the person managing our behaviour. --The One Minute Manager signature.asc Description: Digital signature -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter. Why the Need for a Frame, or no Frame?
On Feb 16, 8:40 pm, W. Watson [EMAIL PROTECTED] wrote: The following two examples are from Grayson's book on Tkinter. He's making a simple dialog with three buttons. In the first example, he does not use the Frame class, but in the second he does. Doesn't the first example need a container? What's the difference here? ==5.1 from Tkinter import * class App: def __init__(self, master): Button(master, text='Left').pack(side=LEFT) Button(master, text='Center').pack(side=LEFT) Button(master, text='Right').pack(side=LEFT) root = Tk() root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 1) display = App(root) root.mainloop() ==5.2== from Tkinter import * class App: def __init__(self, master): fm = Frame(master) Button(fm, text='Left').pack(side=LEFT) Button(fm, text='This is the Center button').pack(side=LEFT) Button(fm, text='Right').pack(side=LEFT) fm.pack() root = Tk() root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 2) display = App(root) root.mainloop() === -- Wayne Watson (Watson Adventures, Prop., Nevada City, CA) (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time) Obz Site: 39° 15' 7 N, 121° 2' 32 W, 2700 feet I know that this defies the law of gravity, but you see, I never studied law. -- Bugs Bunny Web Page: www.speckledwithstars.net/ -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net Every Tkinter program is required to have a 'root window'. A root window is a container in its own right. To create a root window, you write: root = Tk() Then what's the point of using a frame in the second example? None really--except to demonstrate that a frame is a container. A frame is used to organize a group of widgets. If you only have one frame, then that's not much different than having no frame. However, if you have two frames, each frame can organize its widgets differently. Note that you can write an even simpler Tkinter program: import Tkinter as tk b1 = tk.Button(text='Left') b2 = tk.Button(text='Center') b3 = tk.Button(text='Right') b1.pack(side=tk.LEFT) b2.pack(side=tk.LEFT) b3.pack(side=tk.LEFT) tk.mainloop() Note that the program doesn't explicitly create a root window or a frame. The program works because if you don't explicitly create a root window, Tkinter automatically creates a root window for you. Subsequently, if you create a widget and don't specify a parent container, Tkinter automatically adds the widget to the root window. On Feb 17, 1:29 am, Francesco Bochicchio [EMAIL PROTECTED] wrote: Anyway, Tk() already opens a frame, so in the first example the buttons are created inside that frame, while in the second example two frames are created: the one creaded by Tk() il left empty but you should see it (maybe very small in a corner) if you run the program. That's incorrect. In the second example, the frame specifies the root window as its parent, and the buttons specify the frame as their parent, so the buttons are inside the frame which is inside the root window. You can easily prove that there's only one window by setting root's size to something large and specifying its background color as red--that way if root is a separate window hiding somewhere it will no longer go unnoticed: from Tkinter import * class App: def __init__(self, master): fm = Frame(master) Button(fm, text='Left').pack(side=LEFT) Button(fm, text='This is the Center button').pack(side=LEFT) Button(fm, text='Right').pack(side=LEFT) fm.pack() root = Tk() root.geometry('600x400') root.config(background='red') root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 2) display = App(root) root.mainloop() -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter. Why the Need for a Frame, or no Frame?
On Feb 17, 9:29 am, Francesco Bochicchio [EMAIL PROTECTED] wrote: Anyway, Tk() already opens a frame, so in the first example the buttons are created inside that frame, while in the second example two frames are created: the one creaded by Tk() il left empty but you should see it (maybe very small in a corner) if you run the program. The container returned by Tk() (root) is passed into App and the frame inside App (fm) is packed into it. It's not left empty and you won't ever see it. On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson wrote: The following two examples are from Grayson's book on Tkinter. He's making a simple dialog with three buttons. In the first example, he does not use the Frame class, but in the second he does. Doesn't the first example need a container? What's the difference here? For simple Tk applications, it's not necessary to create Frames to pack inside the root container. However, more complicated GUIs will often include multiple nested frames. Because each container can only be managed by one geometry manager, multiple containers are the only way to create complex interfaces. Anyway, the answer is, it's not necessary to create a Frame in the first example, as the initial call to Tkinter.Tk() will return the root container, which can be used to pack any widget. In the first example, the three buttons are packed directly into the root container. In the second example, the three buttons are packed into a frame which is in turn packed into the root container. Happy to help, Pete Cable (formerly of Yuba City, CA) -- http://mail.python.org/mailman/listinfo/python-list
Is there a way to link a python program from several files?
Take a look at Fred Lundh's Squeeze programme. quote ... " If all you need is to wrap up a couple of Python scripts and modules into a single file, Squeeze might be what you need. The squeeze utility can be used to distribute a complete Python application as one or two files, and run it using a standard Python interpreter kit. squeeze compiles all Python modules used by the application (except for the standard library files), and packs them all in a single, usually compressed bytecode package. The import statement is then modified (using the ihooks module) to look in the package before searching for modules on the disk. You can also put arbitrary data files in the package, and access them via the __main__ module. ..." -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
Hi Arnaud Benjamin Here's a version that's a bit more general. It handles keys whose values are empty dicts (assigning None to the value in the result), and also dict keys that are not strings (see the test data below). It's also less recursive as it only calls itself on values that are dicts. But it returns a dict whose keys are tuples. You then need to decide what to do with this. Hence the helper function strdictflatten for the case when all dict keys can be converted to str. As I told Arnaud in email, I greatly prefer his version for its elegance. Terry def dictflatten(d, prefix=None): result = {} if prefix is None: prefix = tuple() for k, v in d.iteritems(): key = prefix + (k,) if isinstance(v, dict): if v: result.update(dictflatten(v, key)) else: result[key] = None else: result[key] = v return result def strdictflatten(d, sep='/'): return dict((sep.join(map(str, k)), v) for k, v in dictflatten(d).iteritems()) if __name__ == '__main__': test = { : {}, 4 : 5, mays : {eggs : spam}, jam : {soda : {love : dump}}, lamba : 23 } d = dictflatten(test) print strdictflatten(test) {'': None, 'lamba': 23, 'mays/eggs': 'spam', '4': 5, 'jam/soda/love': 'dump'} -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter. Why the Need for a Frame, or no Frame?
I did run them both, but not simultaneously. They looked the same to me. I should have probably captured both. I'll check for a small one somewhere. Francesco Bochicchio wrote: On Sat, 16 Feb 2008 19:40:51 -0800, W. Watson wrote: from Tkinter import * class App: def __init__(self, master): fm = Frame(master) Button(fm, text='Left').pack(side=LEFT) Button(fm, text='This is the Center button').pack(side=LEFT) Button(fm, text='Right').pack(side=LEFT) fm.pack() root = Tk() root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 2) display = App(root) root.mainloop() The obvious question is: why don't you run both and see what happens? Anyway, Tk() already opens a frame, so in the first example the buttons are created inside that frame, while in the second example two frames are created: the one creaded by Tk() il left empty but you should see it (maybe very small in a corner) if you run the program. Ciao - FB -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter. Why the Need for a Frame, or no Frame?
Thanks very much. I'm somewhat new to this, but I would think that Frame might carry some properties not available to the root. If so, then there might be some advantage to it. 7stud wrote: On Feb 16, 8:40 pm, W. Watson [EMAIL PROTECTED] wrote: The following two examples are from Grayson's book on Tkinter. He's making a simple dialog with three buttons. In the first example, he does not use the Frame class, but in the second he does. Doesn't the first example need a container? What's the difference here? ... snip Every Tkinter program is required to have a 'root window'. A root window is a container in its own right. To create a root window, you write: root = Tk() Then what's the point of using a frame in the second example? None really--except to demonstrate that a frame is a container. A frame is used to organize a group of widgets. If you only have one frame, then that's not much different than having no frame. However, if you have two frames, each frame can organize its widgets differently. Note that you can write an even simpler Tkinter program: import Tkinter as tk b1 = tk.Button(text='Left') b2 = tk.Button(text='Center') b3 = tk.Button(text='Right') b1.pack(side=tk.LEFT) b2.pack(side=tk.LEFT) b3.pack(side=tk.LEFT) tk.mainloop() Note that the program doesn't explicitly create a root window or a frame. The program works because if you don't explicitly create a root window, Tkinter automatically creates a root window for you. Subsequently, if you create a widget and don't specify a parent container, Tkinter automatically adds the widget to the root window. On Feb 17, 1:29 am, Francesco Bochicchio [EMAIL PROTECTED] wrote: Anyway, Tk() already opens a frame, so in the first example the buttons are created inside that frame, while in the second example two frames are created: the one creaded by Tk() il left empty but you should see it (maybe very small in a corner) if you run the program. That's incorrect. In the second example, the frame specifies the root window as its parent, and the buttons specify the frame as their parent, so the buttons are inside the frame which is inside the root window. You can easily prove that there's only one window by setting root's size to something large and specifying its background color as red--that way if root is a separate window hiding somewhere it will no longer go unnoticed: from Tkinter import * class App: def __init__(self, master): fm = Frame(master) Button(fm, text='Left').pack(side=LEFT) Button(fm, text='This is the Center button').pack(side=LEFT) Button(fm, text='Right').pack(side=LEFT) fm.pack() root = Tk() root.geometry('600x400') root.config(background='red') root.option_add('*font', ('verdana', 12, 'bold')) root.title(Pack - Example 2) display = App(root) root.mainloop() -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
nexes wrote: there is more data that needed to be assigned(i.e. a couple megs of data) it would be simpler (and more efficient) to do a compare rather then assigning all that data to an array, since you are only going to be using 1 value and the rest of the data in the array is useless. Ouch, you're hurting my brain... Okay, let's state this first: A lookup table IS the more efficient solution. Somehow you seem to think, that a lookup table will require more resources (memory I guess you thought) than a sequence of comparisons. However you didn't take into account, that the program code itself requires memory, too (for the operation codes). For the sake of simplicity let's assume, that every operation code and every variable takes exactly one blurp of memory (what a blurp is exactly we leave undefined here, it's just the smallest unit of memory our hypothetically machine can process). Let's also assume, that you can pack everything, that happens in a operation into a single opcode, which by definition fits into a blurp: In case of a comparision this would include the value, to operand to compare to and where to jump if the comparision satisfies. Assignment is a own operation, thus it doesn't go into the compare operation (it would be rather inefficient to try to fit all possible things you do after a comparision into a huge variety of comparision opcodes). In the following example each line should be considered as a single operation. And let's assume that every operation takes one tick to execute. Labels take no memory. A table is a label with a number of values following. The label of the table itself doesn't consume memory either, however a compiler/assembler might decide to insert a relative jump just before the table so that the program doesn't run into it. The operations are like this: compare $variable $value $jump_label assign $variable $value | $table[$index] jump $jump_label $jump_label: $table $number_of values: $value $value ... The shortest compare/assign program for the month problem would look like this compare month 1 month_31 compare month 2 month_28 compare month 3 month_31 compare month 4 month_30 ... compare month 11 month_30 compare month 12 month_31 month_30: assign $days 30 jump end month_31: assign $days 31 jump end month_28: assign $days 28 end: This program consists of 12+5 = 18 operations. There are no tables, to the program consumes only those 18 blurps. Running time is between 3 and 14 ticks. Now let's have a look on the table based variant days_in_month 12: 31 30 28 31 ... 30 31 assign $days days_in_month[$month] This program consists of 2 operations (table jump and assignment) and 12 values. This makes a memory consumption of 12+2 = 14 blurps. Running time is always 2 ticks. You see that the table based approach both takes less memory (14 blurbs vs. 18 blurps) and always runs faster (2 ticks vs. 3...14 ticks) than the comparison/jump/assignment based solution. And that was just this artificial example. In most real world applications the operands to a comparision would take additional memory, at it's best the opcode would also address some register(s), but not more, so this would also add the instructions to fill the registers with the values. Now you might want to state the above examples are indeed artificial. But OTOH they're not so artificial then again. Most assembler code works that way and if you take the AVR architecture it almost looks that way there. And if you're thinking of Python you can bet, that an opcode will not contain all information, but have the operands encoded in additional values, consuming memory. Look up Table is more efficient: Debate solved. Wolfgang Draxinger -- E-Mail address works, Jabber: [EMAIL PROTECTED], ICQ: 134682867 -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
On Feb 17, 12:18 pm, Terry Jones [EMAIL PROTECTED] wrote: Hi Arnaud Benjamin Here's a version that's a bit more general. It handles keys whose values are empty dicts (assigning None to the value in the result), and also dict keys that are not strings (see the test data below). It's also less recursive as it only calls itself on values that are dicts. But it returns a dict whose keys are tuples. You then need to decide what to do with this. Hence the helper function strdictflatten for the case when all dict keys can be converted to str. As I told Arnaud in email, I greatly prefer his version for its elegance. Terry def dictflatten(d, prefix=None): result = {} if prefix is None: prefix = tuple() for k, v in d.iteritems(): key = prefix + (k,) if isinstance(v, dict): if v: result.update(dictflatten(v, key)) else: result[key] = None else: result[key] = v return result It's nice to do it this way I think. Here I post a generator-powered version of the same idea. from itertools import chain def flattendict(d): def gen(d, pfx=()): return chain(*(gen(v, pfx+(k,)) if isinstance(v, dict) else ((pfx+(k,), v),) for k, v in d.iteritems())) return dict(gen(d)) BTW, I keep using the idiom itertools.chain(*iterable). I guess that during function calls *iterable gets expanded to a tuple. Wouldn't it be nice to have an equivalent one-argument function that takes an iterable of iterables and return the 'flattened' iterable? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: mapping problem
On 16 fév, 11:35, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: (snip) If you're not a native English speaker, please say so Actually, from looking at the OP's GoogleGroup profile, I think we can safely assert he's not a native English speaker. -- http://mail.python.org/mailman/listinfo/python-list
Sys.exit() does not fully exit
I have this script: import os, thread, threading, time, sys class Script1(threading.Thread): def run(self): os.system('runScript1.py') class Script2(threading.Thread): def run(self): os.system('runScript2.py') if __name__ == '__main__': s = Script1() s.start() time.sleep(5) a = Script2() a.start() time.sleep(5) while True: answer = raw_input('Type x to shutdown: ') if answer == 'x': print 'Shutting down' time.sleep(1) print 'Done' sys.exit(0) When x is typed, everything does shut down but the main script never fully ends. Is there any way to get it to quit? Thanks, Harlin Seritt -- http://mail.python.org/mailman/listinfo/python-list
Re: Sys.exit() does not fully exit
Harlin Seritt wrote: When x is typed, everything does shut down but the main script never fully ends. Is there any way to get it to quit? You don't need threads. Please use the subprocess module instead of threads + os.system. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: sockets -- basic udp client
In article [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: If you don't care about the address of the sender, e.g. you are not going to send anything back, is there an advantage to using recv()? At the system call level, recv() is marginally faster since there's less data to pass back and forth between the kernel and user space. Not that this is likely to be significant in any real-world application. The bigger advantage to recv() is that the interface is simpler, so there's less code to write. For the C interface, using recv() instead of recvfrom() frees you from having to pass in two arguments that you're not going to use. From the Python interface, it frees you from having to unpack the tuple that recvfrom() returns. Instead of: data, address = recvfrom(bufsize) you write data = recv(bufsize) It's not just a bunch less typing, it's also easier to understand. You don't leave some future maintainer of your code scratching their head trying to figure out where 'address' is used, when in fact, it's not. -- http://mail.python.org/mailman/listinfo/python-list
how to set image from double values of pixels
hi i am using PIL to get and set image data.Using image.getdata() i can get a tuple of ints for each pixel. also i can use im.putdata(data) to set pixels . suppose i am given a double value as a pixel value (say 7245654.32456 which i may get from some image processing calc..) and thus i have an array of doubles to represent the image.can i use im.putdata() to set the pixel values without loss of information? can someone help? -- http://mail.python.org/mailman/listinfo/python-list
Memory Issue
Afternoon Guys, I've got what I would consider to be a strange memory leak within an application I've been working on. When the application is running the consumed system memory creeps up slowly, getting higher and higher. However, when looking at 'top' to display the memory allocation for all the applications, the actual assigned memory to the 'python' process doesn't appear to change, it consistently looks like: 2414 root 20 0 9516 6316 3148 S 0.0 2.5 0:03.62 python However the currently used memory for the entire system, shown here: Mem:256760k total,35588k used, 221172k free, 2096k buffers Continues to escalate until the system runs out of memory. I can be pretty sure that this is something to do with my application as whenever the app isn't running then the memory usage doesn't increase. An additional strangeness to this is that when my application is killed, the memory doesn't drop back down again? Is that normal? My understanding of a memory leak was that when the application dies the memory will be freed back up again. Can any offer any suggestions as to what is causing this problem? Is it perhaps not my actual application but something I'm calling from within it? Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Re: basic wxpython help
On Feb 16, 3:06 pm, Vamp4L [EMAIL PROTECTED] wrote: Thanks Mike, Simple enough! I was wandering about the close method too, I had to hack that together from what I knew about python already. I'll be sure to join that mailing list. If you get the chance, check out the wxPython in Action book by Robin Dunn. It's a good reference, although it doesn't have some of the most recent changes, including the widget browser thing. As for the mailing list, Dunn responds to a lot of questions there. He's the creator of wxPython, so he's extremely knowledgeable. There are also a few wxPython developers on the list that have creates their own widgets and they tend to answer a lot of general and specific questions there too. I still recommend this list for non-wxPython questions though. Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: Critique of first python code
Dan Bishop wrote: I will say, however, that hasattr(item, '__iter__') isn't a perfect way of checking whether an object is iterable: Objects that just define __getitem__ are iterable too (e.g., UserList). Speaking of which, what *is* the best way to check if an object is iterable? I always wrap iter(item) in a try/except block, but is there an isiterable() function somewhere? MWM -- http://mail.python.org/mailman/listinfo/python-list
Re: Sys.exit() does not fully exit
Christian Heimes wrote: Harlin Seritt wrote: When x is typed, everything does shut down but the main script never fully ends. Is there any way to get it to quit? You don't need threads. Please use the subprocess module instead of threads + os.system. Christian That's a good answer. However, it you *do* want threads, and you don't want the main thread to wait for the threads to quit, you can make the threads daemon threads. See setDaemon method on Thread objects in the threading module. Gary Herron -- http://mail.python.org/mailman/listinfo/python-list
Animated GIF in Tkinter
Hello, I have created a frame in which i want to display an animated progressbar in a canvas. I use PhotoImage function of PIL. But it doesn't display the animated gif code looks like this self.imgobj = PhotoImage(file=imgpath) self.c = Canvas(self.frame2, bg='white',width=64,height=310) self.c.place(x=x0,y=y0) self.c.create_image(x0,y0,image=self.imgobj,anchor=NW) did a lot of googling on it. but nothing found. I can't switch to wxPython. Please help me. thanks -- Surya Prakash Garg Bangalore +919886801350 -- http://mail.python.org/mailman/listinfo/python-list
Re: Critique of first python code
On Feb 17, 10:17 am, Matthew Marshall [EMAIL PROTECTED] wrote: Dan Bishop wrote: I will say, however, that hasattr(item, '__iter__') isn't a perfect way of checking whether an object is iterable: Objects that just define __getitem__ are iterable too (e.g., UserList). Speaking of which, what *is* the best way to check if an object is iterable? I always wrap iter(item) in a try/except block, Yes, that's the most foolproof way. but is there an isiterable() function somewhere? Not AFAIK, but if I had to write one, I would use isiterable = lambda x: hasattr(x, '__iter__') or hasattr(x, '__getitem__') In Python 3 it will be isinstance(x, Iterable). George -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
On Feb 17, 7:51 am, Arnaud Delobelle [EMAIL PROTECTED] wrote: BTW, I keep using the idiom itertools.chain(*iterable). I guess that during function calls *iterable gets expanded to a tuple. Wouldn't it be nice to have an equivalent one-argument function that takes an iterable of iterables and return the 'flattened' iterable? Indeed; I don't have any exact numbers but I roughly use this idiom as often or more as the case where chain() takes a known fixed number of arguments. The equivalent function you describe is trivial: def chain2(iter_of_iters): for iterable in iter_of_iters: for i in iterable: yield i but I usually don't bother, although if it was available in itertools I'd use it instead. Apart from introducing a new function for something quite similar, another idea would be to modify chain(*iterables) semantics if it is passed a single argument. Currently chain() is useful for 2 or more arguments; chain(x) is equivalent to iter(x), there's no reason to use it ever. On the downside, this might break backwards compatibility for cases like chain(*some_iterable) where some_iterable has length of 1 but I'd guess this is quite rare. George -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
George Sakkis wrote: On Feb 17, 7:51 am, Arnaud Delobelle [EMAIL PROTECTED] wrote: BTW, I keep using the idiom itertools.chain(*iterable). I guess that during function calls *iterable gets expanded to a tuple. Wouldn't it be nice to have an equivalent one-argument function that takes an iterable of iterables and return the 'flattened' iterable? Indeed; I don't have any exact numbers but I roughly use this idiom as often or more as the case where chain() takes a known fixed number of arguments. The equivalent function you describe is trivial: def chain2(iter_of_iters): for iterable in iter_of_iters: for i in iterable: yield i or fwiw chainstar = lambda iters : (x for it in iters for x in it) - a form that better suggests how to inline it in the calling expression, if applicable. Cheers, BB -- http://mail.python.org/mailman/listinfo/python-list
Re: flattening a dict
Arnaud == Arnaud Delobelle [EMAIL PROTECTED] writes: Arnaud BTW, I keep using the idiom itertools.chain(*iterable). I guess Arnaud that during function calls *iterable gets expanded to a tuple. Arnaud Wouldn't it be nice to have an equivalent one-argument function Arnaud that takes an iterable of iterables and return the 'flattened' Arnaud iterable? This reminds me of a function I wrote a while back that iterates over all its arguments, even calling passed functions and iterating over their results. I knew *even less* about Python then than I do now; please set flamethrowers to Constructive Criticism. http://www.fluidinfo.com/terry/2007/05/07/iteranything/ Terry -- http://mail.python.org/mailman/listinfo/python-list
Re: Sys.exit() does not fully exit
Gary Herron wrote: That's a good answer. However, it you *do* want threads, and you don't want the main thread to wait for the threads to quit, you can make the threads daemon threads. See setDaemon method on Thread objects in the threading module. In general you are right. But I don't think daemon threads are going to solve the problem here. The threads may stop but Python may not have returned from the system() syscall yet. In order to stop the syscall you have to send a KILL or TERM signal to the child process. Threads and signals don't mix well and you can get in all sorts of trouble. Christian -- http://mail.python.org/mailman/listinfo/python-list
Python 2.5.1 - sqlite3.dll issue
-- http://mail.python.org/mailman/listinfo/python-list
CFP: DTVCS 2008 - Design, Testing and Formal Verification Techniques for Integrated Circuits and Systems
Apologies for any multiple copies received. We would appreciate it if you could distribute the following call for papers to any relevant mailing lists you know of. CALL FOR PAPERS Special Session: Design, Testing and Formal Verification Techniques for Integrated Circuits and Systems DTVCS 2008 August 18-20, 2008 (Kailua-Kona, Hawaii, USA) http://digilander.libero.it/systemcfl/dtvcs = Special Session in the IASTED International Conference on Circuits and Systems (CS 2008) - The IASTED International Conference on Circuits and Systems (CS 2008) will take place in Kailua-Kona, Hawaii, USA, August 18-20, 2008. URL: http://www.iasted.org/conferences/cfp-625.html. Aims and Scope - The main target of the Special Session DTVCS is to bring together engineering researchers, computer scientists, practitioners and people from industry to exchange theories, ideas, techniques and experiences related to the areas of design, testing and formal verification techniques for integrated circuits and systems. Contributions on UML and formal paradigms based on process algebras, petri-nets, automaton theory and BDDs in the context of design, testing and formal verification techniques for integrated circuits and systems are also encouraged. Topics -- Topics of interest include, but are not limited to, the following: * digital, analog, mixed-signal and RF test * built-in self test * ATPG * theory and foundations: model checking, SAT-based methods, use of PSL, compositional methods and probabilistic methods * applications of formal methods: equivalence checking, CSP applications and transaction-level verification * verification through hybrid techniques * verification methods based on hardware description/system-level languages (e.g. VHDL, SystemVerilog and SystemC) * testing and verification applications: tools, industrial experience reports and case studies Industrial Collaborators and Sponsors -- This special session is partnered with: * CEOL: Centre for Efficiency-Oriented Languages Towards improved software timing, University College Cork, Ireland (http://www.ceol.ucc.ie) * International Software and Productivity Engineering Institute, USA ( http://www.intspei.com) * Intelligent Support Ltd., United Kingdom (http://www.isupport-ltd.co.uk) * Minteos, Italy (http://www.minteos.com) * M.O.S.T., Italy (http://www.most.it) * Electronic Center, Italy (http://www.el-center.com) * Legale Fiscale, Italy (http://www.legalefiscale.it) This special session is sponsored by: * LS Industrial Systems, South Korea (http://eng.lsis.biz) * Solari, Hong Kong (http://www.solari-hk.com/) Technical Program Committee * Prof. Vladimir Hahanov, Kharkov National University of Radio Electronics, Ukraine * Prof. Paolo Prinetto, Politecnico di Torino, Italy * Prof. Alberto Macii, Politecnico di Torino, Italy * Prof. Joongho Choi, University of Seoul, South Korea * Prof. Wei Li, Fudan University, China * Prof. Michel Schellekens, University College Cork, Ireland * Prof. Franco Fummi, University of Verona, Italy * Prof. Jun-Dong Cho, Sung Kyun Kwan University, South Korea * Prof. AHM Zahirul Alam, International Islamic University Malaysia, Malaysia * Dr. Emanuel Popovici, University College Cork, Ireland * Dr. Jong-Kug Seon, System LSI Lab., LS Industrial Systems Co. Ltd., South Korea * Dr. Umberto Rossi, STMicroelectronics, Italy * Dr. Graziano Pravadelli, University of Verona, Italy * Dr. Vladimir Pavlov, International Software and Productivity Engineering Institute, USA * Dr. Jinfeng Huang, Philips LiteOn Digital Solutions Netherlands, Advanced Research Centre, The Netherlands * Dr. Thierry Vallee, Georgia Southern University, Statesboro, Georgia, USA * Dr. Menouer Boubekeur, University College Cork, Ireland * Dr. Ana Sokolova, University of Salzburg, Austria * Dr. Sergio Almerares, STMicroelectronics, Italy * Ajay Patel (Director), Intelligent Support Ltd, United Kingdom * Monica Donno (Director), Minteos, Italy * Alessandro Carlo (Manager), Research and Development Centre of FIAT, Italy * Yui Fai Lam (Manager), Microsystems Packaging Institute, Hong Kong University of Science and Technology, Hong Kong Important Dates --- April 1, 2008: Deadline for submission of completed papers May 15, 2008: Notification of acceptance/rejection to authors Please visit our web-site for further information on the hosting conference of DTVCS, submission guidelines, proceedings and
Re: flattening a dict
On Feb 17, 4:03 pm, Boris Borcic [EMAIL PROTECTED] wrote: George Sakkis wrote: On Feb 17, 7:51 am, Arnaud Delobelle [EMAIL PROTECTED] wrote: BTW, I keep using the idiom itertools.chain(*iterable). I guess that during function calls *iterable gets expanded to a tuple. Wouldn't it be nice to have an equivalent one-argument function that takes an iterable of iterables and return the 'flattened' iterable? Indeed; I don't have any exact numbers but I roughly use this idiom as often or more as the case where chain() takes a known fixed number of arguments. The equivalent function you describe is trivial: def chain2(iter_of_iters): for iterable in iter_of_iters: for i in iterable: yield i or fwiw chainstar = lambda iters : (x for it in iters for x in it) - a form that better suggests how to inline it in the calling expression, if applicable. Indeed: def flattendict(d): def gen(d, pfx=()): return (x for k, v in d.iteritems() for x in (gen(v, pfx+(k,)) if isinstance(v, dict) else ((pfx+(k,), v),))) return dict(gen(d)) I don't know, I find the chain(*...) version more readable, although this one is probably better. Please, Mr itertools, can we have chainstar? -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list
Re: pyinstall and matplotlib
Anybody willing to help? On Feb 14, 11:17 am, John Henry [EMAIL PROTECTED] wrote: Thank you for the response. I am having trouble using the script. I am assuming the TUI is the application this script was developed for and did my best to replace that with the name of my own. To make things simple, let's say we take one of the sample matplotlib program MULTICOLOR.PY and place that in a directory by itself. I have attached the modified script below. Notice that I commented out the import TUI.Version and replaced TUI.Version.VersionStr with some arbitrary number. I have to create a directroy dest before running it. When I run this script, there is no error message. Just: running install running build running install_data and then there is an empty directory MULTICOLOR_2.31_Windows created and then nothing else. #= from distutils.core import setup import os import sys import matplotlib import py2exe # The following code is necessary for py2exe to find win32com.shell. # Solution from http://starship.python.net/crew/theller/moin.cgi/ WinShell import win32com import py2exe.mf as modulefinder for pth in win32com.__path__[1:]: modulefinder.AddPackagePath(win32com, pth) for extra in [win32com.shell]: __import__(extra) m = sys.modules[extra] for pth in m.__path__[1:]: modulefinder.AddPackagePath(extra, pth) tuiRoot = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) roRoot = os.path.join(tuiRoot, ROPackage) sys.path = [tuiRoot, roRoot] + sys.path #import TUI.Version mainProg = os.path.join(tuiRoot, multicolor.py) NDataFilesToPrint = 0 # number of data files to print, per directory def addDataFiles(dataFiles, fromDir, toSubDir=None, inclHiddenDirs=False): Find data files and format data for the data_files argument of setup. In/Out: - dataFiles: a list to which is appended zero or more of these elements: [subDir, list of paths to resource files] Inputs: - fromDir: path to root directory of existing resource files - toSubDir: relative path to resources in package; if omitted then the final dir of fromDir is used - inclHiddenDirs: if True, the contents of directories whose names start with . are included Returns a list of the following elements: lenFromDir = len(fromDir) if toSubDir == None: toSubDir = os.path.split(fromDir)[1] for (dirPath, dirNames, fileNames) in os.walk(fromDir): if not inclHiddenDirs: numNames = len(dirNames) for ii in range(numNames-1, -1, -1): if dirNames[ii].startswith(.): del(dirNames[ii]) if not dirPath.startswith(fromDir): raise RuntimeError(Cannot deal with %r files; %s does not start with %r %\ (resBase, dirPath, fromDir)) toPath = os.path.join(toSubDir, dirPath[lenFromDir+1:]) filePaths = [os.path.join(dirPath, fileName) for fileName in fileNames] dataFiles.append((toPath, filePaths)) # Add resources dataFiles = [] # TUI resources for resBase in (Help, Scripts, Sounds): toSubDir = os.path.join(MULTICOLOR, resBase) fromDir = os.path.join(tuiRoot, toSubDir) addDataFiles(dataFiles, fromDir, toSubDir) # RO resources for resBase in (Bitmaps,): toSubDir = os.path.join(RO, resBase) fromDir = os.path.join(roRoot, toSubDir) addDataFiles(dataFiles, fromDir, toSubDir) # Add tcl snack libraries pythonDir = os.path.dirname(sys.executable) snackSubDir = tcl\\snack2.2 snackDir = os.path.join(pythonDir, snackSubDir) addDataFiles(dataFiles, snackDir, snackSubDir) # Add matplotlib's data files. matplotlibDataPath = matplotlib.get_data_path() addDataFiles(dataFiles, matplotlibDataPath, matplotlibdata) if NDataFilesToPrint 0: print \nData files: for pathInfo in dataFiles: print pathInfo[0] nFiles = len(pathInfo[1]) for resPath in pathInfo[1][0:NDataFilesToPrint]: print , resPath if nFiles NDataFilesToPrint: print ...and %d more % (nFiles - NDataFilesToPrint) versDate = 2.31 # TUI.Version.VersionStr appVers = versDate.split()[0] distDir = MULTICOLOR_%s_Windows % (appVers,) inclModules = [ #email.Utils, # needed for Python 2.5.0 ] # packages to include recursively inclPackages = [ MULTICOLOR, RO, matplotlib, dateutil, # required by matplotlib pytz, # required by matplotlib #matplotlib.backends, #matplotlib.numerix, #encodings, #numpy, #email, # needed for Python 2.5 ] setup( options = dict( py2exe = dict ( dll_excludes = [ # the following are for matplotlib 0.87: libgdk_pixbuf-2.0-0.dll,
Python Memory Manager
I've been looking at the Python source code recently, more specifically trying to figure out how it's garbage collector works. I've gathered that it uses refcounting as well as some cycle-detection algorithms, but I haven't been able to figure out some other things. Does Python actually have a single 'heap' where all the data is stored? Because PyObject_HEAD seemed to imply to me it was just a linked list of objects, although perhaps I didnt understand this correctly. Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. (Also, if you know any resources for things like this, I'd be grateful for links/names) Thanks in advance, Pie Squared -- http://mail.python.org/mailman/listinfo/python-list
How to get current module object
Can I get reference to module object of current module (from which the code is currently executed)? I know __import__('filename') should probably do that, but the call contains redundant information (filename, which needs to be updated), and it'll perform unnecessary search in loaded modules list. It shouldn't be a real problem (filename can probably be extracted from the traceback anyway), but I wonder if there is more direct and less verbose way. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Pie Squared [EMAIL PROTECTED] writes: Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. As I understand it, Python primarily uses reference counting, with a mark and sweep scheme for cycle breaking tacked on as an afterthought. It doesn't move objects in memory during GC so you can get fragmentation. It's probably not feasible to change this in CPython without extensive rewriting of CPython and maybe a lot of external C modules. (Also, if you know any resources for things like this, I'd be grateful for links/names) If you mean about GC in general, the old book by Jones and Lins is still standard, I think. -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Consider what happens when you add two fractions: 1/2 + 1/5 To do that, you have to take the LCD of the denomintor, in this case 10, so you get 5/10 + 2/10 = 7/10 Now imagine that you're adding a lot of different numbers with a lot of different bases. That LCD's going to be pretty big. To make matters worse, imagine taking this number as the divisor in a later calculation: a new denominator would appear (7). So you get denominators that you didn't even input, which can make LCDs go higher. Any iteration with repeated divisions and additions can thus run the denominators up. This sort of calculation is pretty common (examples: compound interest, numerical integration). Wrong. Addition and subtraction would only grow the denominator up to a certain limit The thing I don't like about rationals is that they give a false sense of security. They are performing reasonably, and then you make a slight change or some circumstance changes slightly and suddenly they blow up. Or, to put it another way, rationals and floats both are dangerous, but in different ways. The performance of rationals can fall drastically, and floating point calculations can suddenly become inaccurate. You make your choice and take your chances. When I mean safety, fraction is safe in calculation integrity safety, it is always safe to do calculations in fraction (although at one time the huge calculation might stall the system, the calculation will always be correct all the time). But actually there is a way to ensure performance safety in Fraction class, fraction might grow uncontrollably only if the data is multiplied or divided. If you're just doing addition and subtraction, the denominator growth is limited to a certain range given a limited range of data. -- http://mail.python.org/mailman/listinfo/python-list
Linux/Python Issues
I went to Python.org, DL'd Python 2.5 source code per the usual inadequate instructions and ran the make files successfully (sort of). Python 2.5 works fine. But from Tkinter import * gets a What's Tkinter? message. IDLE's no where to be found. What's not in the instructions is what directory should I be in when I download? Where should I put the .bz2 file? What dir for running the make files? At present I'm working on a Windows machine, endangering what's left of my sanity. I'm using Linspire, so Debian directories are probably the ones that will get me up and running. Barring specific knowledge, even some good guesses would be appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Tkinter Confusion
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: from Tkinter import * foo = Tk() Google's great, but it has no truth meter. Do I inherit from Frame? Or is that a big mistake. (Both positions repeated frequently.) Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.) Here's the application. I'm creating a visual parser for my beginner's language. The starting position is a list of Statement objects, each being a list of Token objects. The statement is presented as a list of buttons with abbreviated token types ('Con_Int' for a CONSTANT_INTEGER token). Click the button and a dialog-like info display pops up with all the details about the token. During parsing, each recognition condenses tokens into productions, shortening the Statement. (Example: three Token buttons are replaced by one Addition production button.) An application window provides for stepping through the parsing and provides utility commands such as Close all those token windows I've got lying all over. Much less complex than IDLE, but GvR and cohorts seem to understand what's really going on. I don't. Help appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: sockets -- basic udp client
In article [EMAIL PROTECTED], [EMAIL PROTECTED] [EMAIL PROTECTED] writes: I have had some difficulty following the assertions, corrections, and misquoting in this article thread, so apologies in advance if I have missed a correction or misunderstood an assertion. [ quoting partially corrected: ] Here is the example above converted to a more straightforward udp client that isolates the part I am asking about: import socket, sys host = 'localhost' #sys.argv[1] port = 3300 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) data = 'hello world' num_sent = 0 while num_sent len(data): num_sent += s.sendto(data, (host, port)) print Looking for replies; press Ctrl-C or Ctrl-Break to stop. while 1: buf = s.recv(2048) #Will the following if statement do anything? if not len(buf): break print Received from server: %s % buf -- But, for the specific case that you have asked about, since the default for timeout is no timeout, or block forever, your question: #Will the following if statement do anything? if not len(buf): break The answer is that you are right, it will do nothing. But, if you set a time out on the socket, and you receive no data and the timeout expires, checking for the length of zero and a break is one way to jump out of the loop if you need to. That is not correct in my experience. The test not len(buf) -- or perhaps more clearly len(buf) == 0 is a valid way, perhaps the preferred way, of detecting a zero length datagram as transmitted via UDP (socket.SOCK_DGRAM). Zero length datagrams are perfectly valid in UDP and in fact are the recommended way of initiating certain protocol actions. See, for example, the Time Protocol (RFC 868, top of page 2). While POSIX regular files and TCP streams use a zero read result to indicate end-of-file, the action is different for message-based sockets (and STREAMS). In the case of UDP, there is no concept of end-of-file, and thus no API mechanism for indicating such. Instead, the zero return is used to indicate a zero-length message. Timeouts are indicated by raising an exception: - In the case of the settimeout method of socket, a socket.timeout exception is raised. - In the case of use of socket option socket.SO_SNDTIMEO, a socket.error exception is raised w/ errno = EAGAIN. For example: import socket, sys host = 'localhost' #sys.argv[1] port = 3300 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(1.0) buf = '' data = 'hello world' num_sent = 0 while num_sent len(data): num_sent += s.sendto(data, (host, port)) print Looking for replies; press Ctrl-C or Ctrl-Break to stop. while True: try: buf, addr = s.recvfrom(2048) except: pass #Will the following if statement do anything? # In this case it will cause the script to jump out of the loop # if it receives no data for a second. if not len(buf): break print Received from server: %s % buf The reason that this example *seems* to work is that you mask the exception. What is actually happening is that the timeout of the recvfrom call raises socket.timeout, which is ignored. Then because buf has been explicitly set to zero length (near the beginning of the program), and because it is *not* modified as a result of the recvfrom call, the length is still zero, and the break is executed. Try commenting out the try/except construct, or try actually providing at least one non-zero length response (such that buf is modified) and seeing if it ever terminates. I would also like to point out that the original example (quoted from the book) used connect' and recv w/ UDP). One of the purposes of using this construct (rather than using recvfrom) is to simplify identification of the remote system: When you connect to a UDP socket, the OS will only send messages to that system and will ignore messages that do not originate from that IP address (ignoring the issue IP address spoofing). - dmw -- . Douglas Wells . Connection Technologies . . Internet: -sp9804- -at - contek.com- . -- http://mail.python.org/mailman/listinfo/python-list
Tkinter = Rodney Dangerfield?
Tkinter gets no respect. But IDLE's a Tkinter-based app and every example I've Googled up shows Tkinter as needing about half as much code as wx to do the same job. I'm beginning to Tkinter up my language application. Am I making a big mistake? -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Paul Rubin wrote: Pie Squared [EMAIL PROTECTED] writes: Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. As I understand it, Python primarily uses reference counting, with a mark and sweep scheme for cycle breaking tacked on as an afterthought. It doesn't move objects in memory during GC so you can get fragmentation. It's probably not feasible to change this in CPython without extensive rewriting of CPython and maybe a lot of external C modules. (Also, if you know any resources for things like this, I'd be grateful for links/names) If you mean about GC in general, the old book by Jones and Lins is still standard, I think. You also need to be aware that there are certain allocation classes which use arenas, areas of storage dedicated to specific object types. When those objects are destroyed their space is returned to the arena, but the arena is either never returned to the free pool or only returned to it when the last allocated item is collected. There seem to be more of those kind of tricks coming up in 2.6 and 3.0. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter = Rodney Dangerfield?
[EMAIL PROTECTED] writes: Tkinter gets no respect. But IDLE's a Tkinter-based app and every example I've Googled up shows Tkinter as needing about half as much code as wx to do the same job. I'm beginning to Tkinter up my language application. Am I making a big mistake? I still use tkinter do to much less installation headache than wx. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
On Feb 17, 1:57 pm, Paul Rubin http://[EMAIL PROTECTED] wrote: Pie Squared [EMAIL PROTECTED] writes: Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. As I understand it, Python primarily uses reference counting, with a mark and sweep scheme for cycle breaking tacked on as an afterthought. It doesn't move objects in memory during GC so you can get fragmentation. It's probably not feasible to change this in CPython without extensive rewriting of CPython and maybe a lot of external C modules. (Also, if you know any resources for things like this, I'd be grateful for links/names) If you mean about GC in general, the old book by Jones and Lins is still standard, I think. Thanks for the quick reply! That answered my question, and I'll check out the book you're referring to - it's exactly what I need, I think. Again, thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
Lie wrote: Consider what happens when you add two fractions: 1/2 + 1/5 To do that, you have to take the LCD of the denomintor, in this case 10, so you get 5/10 + 2/10 = 7/10 Now imagine that you're adding a lot of different numbers with a lot of different bases. That LCD's going to be pretty big. To make matters worse, imagine taking this number as the divisor in a later calculation: a new denominator would appear (7). So you get denominators that you didn't even input, which can make LCDs go higher. Any iteration with repeated divisions and additions can thus run the denominators up. This sort of calculation is pretty common (examples: compound interest, numerical integration). Wrong. Addition and subtraction would only grow the denominator up to a certain limit The thing I don't like about rationals is that they give a false sense of security. They are performing reasonably, and then you make a slight change or some circumstance changes slightly and suddenly they blow up. Or, to put it another way, rationals and floats both are dangerous, but in different ways. The performance of rationals can fall drastically, and floating point calculations can suddenly become inaccurate. You make your choice and take your chances. When I mean safety, fraction is safe in calculation integrity safety, it is always safe to do calculations in fraction (although at one time the huge calculation might stall the system, the calculation will always be correct all the time). But actually there is a way to ensure performance safety in Fraction class, fraction might grow uncontrollably only if the data is multiplied or divided. If you're just doing addition and subtraction, the denominator growth is limited to a certain range given a limited range of data. Surely this assumes that the denominators are being reduced after each operation, which would certainly add to the time the operations take. The simplest and fasted implementation of rational addition and subtraction uses a common denominator which is the product of both. To avoid denominator growth at each operation you have to start extracting common factors, which is bound to slow you down. So, presuming your certain limit is the product of all mutually prime denominators that have been involved, you are slowing things down to maintain that limitation. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
I researched this for some Java I wrote. Try to avoid shuffling physical memory - you'll write a lot less code and it will be faster, too. Use an allocated list and an available list. Keep them in address order. Inserting (moving list elements from insertion point to end) and deleting (vice-versa) are near-zero cost operations on Intel boxes. ( Two millis to move a million ints at 1GHz 5 years ago when I wrote http://www.martinrinehart.com/articles/repz.html - probably half that today.) The worst choice is the best fit allocation algorithm. (Grabbing most of a free bit leaves a probably useless small bit. Grab from the first big piece you find.) Circular first-fit is probably best. (Testing was for compiler-type applications.) When space is freed, insert link in ordered chain of free space blocks. Then combine with prev/next blocks if they are free. And when you find the function in Python that matches Java's System.arraycopy(), please tell me what it's called. I'm sure there must be one. -- http://mail.python.org/mailman/listinfo/python-list
Re: pyinstall and matplotlib
hi John, John Henry wrote: Anybody willing to help? I struggled the past few days with the same problem, and with the help of Werner Bruhin (wxPython list) I found a solution. I had 2 problems: - not finding mpl datapath - matplotlib insisted on installing backends that were distorted on my system The first problem was solved with the following script: it has some special parts - remove the distro and build directories before running setup - a special matplot part, ensuring mpl-data is copied and installed - a lot of excludes for matplotlib ( which doesn't seem to work :-( ) Kill_Distro = True MatPlotLib_Wanted = True from distutils.core import setup import py2exe import sys subdirs = [ '..\\P24_support', '..\\P24_pictures', '..\\P24_Lib_Extensions' ] for subdir in subdirs: if not ( subdir in sys.path) : sys.path.append ( subdir ) from file_support import * import shutil import glob # *** # Some suggests that old build/dist should be cleared # *** dist_paths = [ 'D:\\Data_Python\\P24_PyLab_Works\\build', 'D:\\Data_Python\\P24_PyLab_Works\\dist' ] for path in dist_paths : if File_Exists ( path ) : shutil.rmtree ( path ) # *** # *** # *** data_files = [] packages = [] includes = [] excludes = [] dll_excludes = [] data_files.append ( ( '', glob.glob ( 'templates_*.*' ) ) ) # *** # For MatPlotLib # *** if MatPlotLib_Wanted : import matplotlib includes.append ( 'matplotlib.numerix.random_array' ) packages.append ( 'matplotlib' ) packages.append ( 'pytz' ) data_files.append ( ( r'mpl-data', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\*.*' ))) data_files.append ( ( r'mpl-data', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc' ))) data_files.append ( ( r'mpl-data\\images', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\images\\*.*' ))) data_files.append ( ( r'mpl-data\\fonts\\afm', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\fonts\\afm\\*.*' ))) data_files.append ( ( r'mpl-data\\fonts\\pdfcorefonts', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\fonts\\pdfcorefonts\\*.*' ))) data_files.append ( ( r'mpl-data\\fonts\\ttf', glob.glob ( r'P:\\Python\\Lib\\site-packages\\matplotlib\\mpl-data\\fonts\\ttf\\*.*' ))) excludes.append ( '_gtkagg') excludes.append ( '_tkagg' ) excludes.append ( '_agg2' ) excludes.append ( '_cairo' ) excludes.append ( '_cocoaagg' ) excludes.append ( '_fltkagg' ) excludes.append ( '_gtk' ) excludes.append ( '_gtkcairo') excludes.append ( 'backend_qt' ) excludes.append ( 'backend_qt4') excludes.append ( 'backend_qt4agg' ) excludes.append ( 'backend_qtagg' ) excludes.append ( 'backend_cairo' ) excludes.append ( 'backend_cocoaagg' ) excludes.append ( 'Tkconstants' ) excludes.append ( 'Tkinter' ) excludes.append ( 'tcl' ) excludes.append ( _imagingtk ) excludes.append ( PIL._imagingtk ) excludes.append ( ImageTk ) excludes.append ( PIL.ImageTk ) excludes.append ( FixTk ) dll_excludes.append ( 'libgdk-win32-2.0-0.dll' ) dll_excludes.append ( 'libgdk_pixbuf-2.0-0.dll' ) dll_excludes.append ( 'libgobject-2.0-0.dll') dll_excludes.append ( 'tcl84.dll' ) dll_excludes.append ( 'tk84.dll' ) dll_excludes.append ( 'tclpip84.dll' ) # *** # seems not to be found (imported in brick.py) includes.append ( 'PyLab_Works_properties' ) # *** # *** # If run without args, build executables, in quiet mode. if len(sys.argv) == 1: sys.argv.append(py2exe) setup ( windows = ['PyLab_Works.py'] , options = { 'py2exe' : { 'includes' : includes, 'excludes' : excludes, 'dll_excludes' : dll_excludes, 'packages' : packages, }}, data_files = data_files ) import subprocess result = subprocess.call ( [ 'P:\Program Files\Inno Setup 4\ISCC.exe', 'D:\Data_Python\P24_PyLab_Works\PyLab_Works.iss']) if (result==0) and Kill_Distro : for path in dist_paths : if File_Exists ( path ) : shutil.rmtree ( path ) Thé essential issue is not to use pylab to do the imports for you, but perform your own imports, this might be a lot of work: in my case the import looks like this (I don't include numerix,
Re: Linux/Python Issues
On 17 Feb, 20:38, [EMAIL PROTECTED] wrote: I went to Python.org, DL'd Python 2.5 source code per the usual inadequate instructions and ran the make files successfully (sort of). Python 2.5 works fine. But from Tkinter import * gets a What's Tkinter? message. IDLE's no where to be found. It could be that you don't have the Tcl/Tk libraries installed, or perhaps the header files for Tcl/Tk aren't installed. If so, Python wouldn't detect them when being configured itself, and then you probably wouldn't have the Tkinter extension installed. What's not in the instructions is what directory should I be in when I download? Where should I put the .bz2 file? What dir for running the make files? At present I'm working on a Windows machine, endangering what's left of my sanity. I'm using Linspire, so Debian directories are probably the ones that will get me up and running. Barring specific knowledge, even some good guesses would be appreciated. Here's one page which probably tells you stuff you already know: http://wiki.python.org/moin/BeginnersGuide/Download On Ubuntu, which is Debian-based, the python-tk package should make Tkinter available, so you could look for that in your repositories. As for building from source, you can put the .bz2 file anywhere, and unpack it anywhere that isn't going to make a mess for you to clean up later. For example, you could download the Python .bz2 file into a downloads directory residing in your home directory, then you could do this: mkdir software cd software tar jxf ~/downloads/Python-2.5.1.tar.bz2 cd Python-2.5.1 ./configure make You can then do a make install with the right privileges. Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
On Feb 17, 7:05 am, Wolfgang Draxinger [EMAIL PROTECTED] wrote: nexes wrote: there is more data that needed to be assigned(i.e. a couple megs of data) it would be simpler (and more efficient) to do a compare rather then assigning all that data to an array, since you are only going to be using 1 value and the rest of the data in the array is useless. Ouch, you're hurting my brain... Okay, let's state this first: A lookup table IS the more efficient solution. Somehow you seem to think, that a lookup table will require more resources (memory I guess you thought) than a sequence of comparisons. However you didn't take into account, that the program code itself requires memory, too (for the operation codes). For the sake of simplicity let's assume, that every operation code and every variable takes exactly one blurp of memory (what a blurp is exactly we leave undefined here, it's just the smallest unit of memory our hypothetically machine can process). Let's also assume, that you can pack everything, that happens in a operation into a single opcode, which by definition fits into a blurp: In case of a comparision this would include the value, to operand to compare to and where to jump if the comparision satisfies. Assignment is a own operation, thus it doesn't go into the compare operation (it would be rather inefficient to try to fit all possible things you do after a comparision into a huge variety of comparision opcodes). In the following example each line should be considered as a single operation. And let's assume that every operation takes one tick to execute. Labels take no memory. A table is a label with a number of values following. The label of the table itself doesn't consume memory either, however a compiler/assembler might decide to insert a relative jump just before the table so that the program doesn't run into it. The operations are like this: compare $variable $value $jump_label assign $variable $value | $table[$index] jump $jump_label $jump_label: $table $number_of values: $value $value ... The shortest compare/assign program for the month problem would look like this compare month 1 month_31 compare month 2 month_28 compare month 3 month_31 compare month 4 month_30 ... compare month 11 month_30 compare month 12 month_31 month_30: assign $days 30 jump end month_31: assign $days 31 jump end month_28: assign $days 28 end: This program consists of 12+5 = 18 operations. There are no tables, to the program consumes only those 18 blurps. Running time is between 3 and 14 ticks. Now let's have a look on the table based variant days_in_month 12: 31 30 28 31 ... 30 31 assign $days days_in_month[$month] This program consists of 2 operations (table jump and assignment) and 12 values. This makes a memory consumption of 12+2 = 14 blurps. Running time is always 2 ticks. You see that the table based approach both takes less memory (14 blurbs vs. 18 blurps) and always runs faster (2 ticks vs. 3...14 ticks) than the comparison/jump/assignment based solution. And that was just this artificial example. In most real world applications the operands to a comparision would take additional memory, at it's best the opcode would also address some register(s), but not more, so this would also add the instructions to fill the registers with the values. Now you might want to state the above examples are indeed artificial. But OTOH they're not so artificial then again. Most assembler code works that way and if you take the AVR architecture it almost looks that way there. And if you're thinking of Python you can bet, that an opcode will not contain all information, but have the operands encoded in additional values, consuming memory. Look up Table is more efficient: Debate solved. Wolfgang Draxinger -- E-Mail address works, Jabber: [EMAIL PROTECTED], ICQ: 134682867 m - 2 and 30 + bool(1 m 5546) or 28 sub x' x 2 cjump x' 0 $but_28 shift x'' 1 x and x'' 5546 cjump x'' 0 $not_31 add x'' 1 $not_31: add x'' 30 cjump x'' 0 $but_28 assign $days x'' jump $end $but_28: assign $days 28 jump $end $end: 22 blups, 4, 9, 10, 12, or 13 ticks. -- http://mail.python.org/mailman/listinfo/python-list
Re: QOTW: Re: dream hardware
On Feb 16, 2:59 pm, Jeff Schwab [EMAIL PROTECTED] wrote: Carl Banks wrote: On Feb 16, 1:39 pm, Jeff Schwab [EMAIL PROTECTED] wrote: Aahz wrote: In article [EMAIL PROTECTED], Jeff Schwab [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On Feb 14, 10:50 pm, [EMAIL PROTECTED] (Aahz) wrote: In article [EMAIL PROTECTED], Steven D'Aprano [EMAIL PROTECTED] wrote: On Tue, 12 Feb 2008 10:05:59 -0800, castironpi wrote: What is dream hardware for the Python interpreter? I'm not sure that the Python interpreter actually does dream, but if it's anything like me, it's probably a giant computer the size of a bus, made out of broccoli and oven-roasted garlic, that suddenly turns into Sylvester Stallone in a tutu just before my program returns its result. IHNTA, IJWTSA IJWTW? Anyone set up to profile CPython?... or step through? I give up. Is there a phrasebook somewhere, or do I need to hire an interpreter? http://acronyms.thefreedictionary.com/ Thanks, but... That defines IHNTA, but not IJWTSA or IJWTW. I just want to say...? I just want to watch? I Just Wanted To See (This) Again Aha. Thanks.- Hide quoted text - And now, to profile CPy. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Pie Squared wrote: I've been looking at the Python source code recently, more specifically trying to figure out how it's garbage collector works. I've gathered that it uses refcounting as well as some cycle-detection algorithms, but I haven't been able to figure out some other things. Python uses ref counting for all objects and an additional GC for container objects like lists and dicts. The cyclic GC depends on ref counting. Does Python actually have a single 'heap' where all the data is stored? Because PyObject_HEAD seemed to imply to me it was just a linked list of objects, although perhaps I didnt understand this correctly. In release builds PyObject_HEAD only contains the ref count and a link to the object type. In Py_DEBUG builds it also contains a double linked list of all allocated objects to debug reference counting bugs. Python uses its own optimized memory allocator. Be sure you have read Object/obmalloc.c! Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. (Also, if you know any resources for things like this, I'd be grateful for links/names) I don't think it's possible to implement a moving GC. You'd have to chance some fundamental parts of Python. Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
On Feb 17, 3:05 pm, [EMAIL PROTECTED] wrote: I researched this for some Java I wrote. Try to avoid shuffling physical memory - you'll write a lot less code and it will be faster, too. Use an allocated list and an available list. Keep them in address order. Inserting (moving list elements from insertion point to end) and deleting (vice-versa) are near-zero cost operations on Intel boxes. ( Two millis to move a million ints at 1GHz 5 years ago when I wrotehttp://www.martinrinehart.com/articles/repz.html- probably half that today.) It seems to me that another, perhaps better strategy, would be to allocate a large heap space, then store a pointer to the base of the heap, the current heap size, and the beginning of the free memory. When you need to 'allocate' more room, just return a pointer to some location in the heap and increment the start-of-free-memory pointer. That way, allocation really IS free, more or less. Wouldn't that be more efficient? Perhaps I'm missing something. As a side note, I'm new to Usenet, so I'm not exactly sure... are 'tangents' like this - since this IS a Python newsgroup, after all - okay? Anyway, thanks for the suggestion. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get current module object
On Feb 18, 5:25 am, Alex [EMAIL PROTECTED] wrote: Can I get reference to module object of current module (from which the code is currently executed)? I know __import__('filename') should probably do that, but the call contains redundant information (filename, which needs to be updated), and it'll perform unnecessary search in loaded modules list. It shouldn't be a real problem (filename can probably be extracted from the traceback anyway), but I wonder if there is more direct and less verbose way. Try this: C:\junktype whoami.py def showme(): import sys modname = globals()['__name__'] print repr(modname) module = sys.modules[modname] print repr(module) print dir(module) C:\junkpython Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32 Type help, copyright, credits or license for more information. import whoami whoami.showme() 'whoami' module 'whoami' from 'whoami.py' ['__builtins__', '__doc__', '__file__', '__name__', 'showme'] -- http://mail.python.org/mailman/listinfo/python-list
Re: call 'the following function' using decorators
On Feb 15, 7:54 pm, [EMAIL PROTECTED] wrote: I assert it's easier to write: start_new_thread( this_func ) def thrA(): normal_suite() than def thrA(): normal_suite() start_new_thread( thrA ) If you don't, stop reading. Nothing beats if forkthread(): but what are the chances of getting it in Python? AIR, as I recall, the SML equivalent is: var TM: thread_manager in def forkthread(): TM.add( True, False ) in if forkthread() thing_to_do() else other_thing_to() where programs can refer to a thread_manager along with a memory, which yes, is even in a functional language, well-defined. thread_manager altertantely beta-reduces one expression at a time in round-robin succession. thread_manager.add duplicates the current expression, and evaluates to True in one and False in the other. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linux/Python Issues
On 17 fév, 20:38, [EMAIL PROTECTED] wrote: (snip) What's not in the instructions is what directory should I be in when I download? Where should I put the .bz2 file? What dir for running the make files? Neither are the basic shell commands like cd, tar etc. Nothing Python- specific here, and I'm afraid you'll have the very same problem with any source distrib of any oss project. IOW: all this is assumed to be common *n*x knowledge. -- http://mail.python.org/mailman/listinfo/python-list
Re: Linux/Python Issues
[EMAIL PROTECTED] schrieb: I went to Python.org, DL'd Python 2.5 source code per the usual inadequate instructions and ran the make files successfully (sort of). Python 2.5 works fine. But from Tkinter import * gets a What's Tkinter? message. IDLE's no where to be found. What's not in the instructions is what directory should I be in when I download? Where should I put the .bz2 file? What dir for running the make files? At present I'm working on a Windows machine, endangering what's left of my sanity. I'm using Linspire, so Debian directories are probably the ones that will get me up and running. Barring specific knowledge, even some good guesses would be appreciated. Nothing special, just reading the configure --help will help you. You need Tcl/Tk + possible devel-packages so the header-files are found. I'm not an expert on the required versions, but that should be told you somewhere. But I doubt that there isn't a python2.5 already available for your distro - especially if it's debian based. Ubuntu for example has 2.5 as default. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Pie Squared [EMAIL PROTECTED] writes: It seems to me that another, perhaps better strategy, would be to allocate a large heap space, then store a pointer to the base of the heap, the current heap size, and the beginning of the free memory. When you need to 'allocate' more room, just return a pointer to some location in the heap and increment the start-of-free-memory pointer. That way, allocation really IS free, more or less. Wouldn't that be more efficient? Perhaps I'm missing something. The problem here is with a high allocation rate, you have to GC a lot more often, which typically involves copying live data. So now you have to figure out how to reduce the amount of copying using generational schemes, (these days) come up with ways to make all this work in the presence of parallel threads, etc. It sounds like you're new to the subject, so for now I'll summarize the situation by saying Python uses a fairly simple scheme that's a reasonable match for its implementation as a small, medium performance interpreter; however, higher performance GC'd language implementations end up doing much more complicated things. See the Jones and Lins book that I mentioned, and there's an even older one by Appel, and the Wikipedia article on garbage collection may have some links you can look at. Also, Structure and Interpretation of Computer Programs (full text online) includes a simple implementation of a copying GC, that might be more tutorial. As a side note, I'm new to Usenet, so I'm not exactly sure... are 'tangents' like this - since this IS a Python newsgroup, after all - okay? It's reasonably on topic, compared with last week's long digression about the dimensional analysis of the Kessel Run. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
On Feb 17, 10:01 pm, Pie Squared [EMAIL PROTECTED] wrote: [...] It seems to me that another, perhaps better strategy, would be to allocate a large heap space, then store a pointer to the base of the heap, the current heap size, and the beginning of the free memory. When you need to 'allocate' more room, just return a pointer to some location in the heap and increment the start-of-free-memory pointer. That way, allocation really IS free, more or less. Wouldn't that be more efficient? Perhaps I'm missing something. Deallocation? As a side note, I'm new to Usenet, so I'm not exactly sure... are 'tangents' like this - since this IS a Python newsgroup, after all - okay? It varies depending on the group, c.l.py is pretty tolerant as long as it's interesting ;-) To bring it back to Python, I was under the impression that the GC was a generational collector and not a simple mark-sweep, but it's been a while since I read about it... -- bjorn -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Also, if it does, how does it deal with memory segmentation? This question bothers me because I've been trying to implement a moving garbage collector, and am not sure how to deal with updating all program pointers to objects on the heap, and thought perhaps an answer to this question would give me some ideas. As I understand it, Python primarily uses reference counting, with a mark and sweep scheme for cycle breaking tacked on as an afterthought. That's not exactly true, i.e. it isn't mark-and-sweep, but some similar scheme that allows incremental collection without write barriers. This particular scheme heavily relies on refcounting itself (specifically, an object is garbage in a certain generation when all references to it come from the same generation). As for the consequences of the scheme (i.e. no compaction), you are right. Regards, Martin -- http://mail.python.org/mailman/listinfo/python-list
Re: XML pickle
Which xmlns:ns1 gets redefined because I just didn't figure out how get xmlns:ns0 definition into the Workbook tag. But too bad for me. What about actually *reading* the links I post? http://codespeak.net/lxml/tutorial.html#the-e-factory Hint: look out for the nsmap keyword argument. That solved the problem. nsmap doesn't set the root xmlns, but el.set does. So that cleared up the [hugevariable] gripe. Revision separates code from data. def Cell( p, index= None, styleid= None ): el= etree.SubElement( p, 'Cell' ) if index is not None: el.set( SS+ 'Index', index ) if styleid is not None: el.set( SS+ 'StyleID', styleid ) return el becomes class Cell( XMLable ): ctor= XMLable.CTor( ( 'index', SS+ 'Index' ), ( 'style', SS+ 'StyleID' ) ) ftor= XMLable.FTor( { 'style': lambda x: x.styleid } ) 29 lines, loosely packed, define base class; 29 define the subclasses Workbook, Worksheet, c., also loose. class Data( XMLable ): ctor= XMLable.CTor( ( 'type', SS+ 'Type' ), ( 'data', XMLable.Text ) ) XMLable.Text pseudo-flag indicates to call node.data= X where node.set( tag, X ) is called. Tag present in XMLable.ftor indicates to call node.set( tag, ftor[tag]( X ) ). class Font( XMLable ): #jtor= JTor( 'family', X+ 'Family', req='Swiss' ), JTor( 'bold', SS+ 'Bold', lambda x: str( int( x ) ) ) ctor= XMLable.CTor( ( 'family', X+ 'Family' ), ( 'bold', SS+ 'Bold' ) ) ftor= XMLable.FTor( { 'bold': lambda x: str( int( x ) ) } ) JTor combines CTor and FTor allowing for extension, separating data into data structure, but is developing. One could even put that spec in XML! Yes: in the example, the base class + derivatives comprise more code, 29 + 29 = 58 lines over the earlier 46. Actual construction underwent a slight adjustment, still 9 lines. Hearing arguments on payoff and extensibility. Full implementation present; remove in replies. Aside, in the C++ equivalent, each XMLable derivative has a class- static list of JTor derivatives, static JTor* specs[];, the population of which is declared and initialized globally, and a virtual JTor* GetSpec( int i ) { return specs[ i ]; } implementation. CMIIW, correct me if I'm wrong; +1 on Python. The for-statements in XMLable.__init__ could conflate; parameter extraction, which parameters may be specified by place or keyword, is unclear. Is if ca[1] is XMLable.Text: a special case, and if so, is it handled correctly? Ought JTor to contain a visit method to the end, which calls node.set in the base class? It leads to redundancy, but code is code. Thence comes the moral, 'No functions in constructors', and if ca[1] is XMLable.Text: is not a callback. Aside, what is? from lxml import etree class XMLable: cname= '' Text= object() class CTor: def __init__( self, *ar ): self.ar, self.kwar= ar, dict( ar ) ctor= CTor() FTor= dict ftor= {} def __init__( self, par= None, *ar, **kwar ): nsmap= kwar.pop( 'nsmap', None ) if par is None: self.node= etree.Element( self.cname or self.__class__.__name__, nsmap= nsmap ) else: self.node= etree.SubElement( par.node, self.cname or self.__class__.__name__, nsmap= nsmap ) for a, ca in zip( ar, self.ctor.ar ): if ca[0] in self.ftor: a= self.ftor[ ca[0] ]( a ) if ca[1] is XMLable.Text: self.node.text= a else: self.node.set( ca[1], a ) for k, v in kwar.items(): if k in self.ftor: v= self.ftor[ k ]( v ) if self.ctor.kwar[ k ] is XMLable.Text: self.node.text= v else: self.node.set( self.ctor.kwar[ k ], str( v ) ) SS= '{urn:schemas-microsoft-com:office:spreadsheet}' X= '{urn:schemas-microsoft-com:office:excel}' class Workbook( XMLable ): #jtor= JTor( 'xmlns', req= 'urn:schemas-microsoft- com:office:spreadsheet' ) def __init__( self ): nns= { 'x': 'urn:schemas-microsoft-com:office:excel', 'ss': 'urn:schemas-microsoft-com:office:spreadsheet' } XMLable.__init__( self, nsmap= nns ) self.node.set( 'xmlns', 'urn:schemas-microsoft- com:office:spreadsheet' ) self.styles= Styles( self ) class Worksheet( XMLable ): ctor= XMLable.CTor( ( 'name', SS+ 'Name' ) ) class Table( XMLable ): pass class Row( XMLable ): ctor= XMLable.CTor( ( 'index', SS+ 'Index' ) ) class Cell( XMLable ): ctor= XMLable.CTor( ( 'index', SS+ 'Index' ), ( 'style', SS+ 'StyleID' ) ) ftor= XMLable.FTor( {
Re: Tkinter Confusion
I'm just a beginner, but I think I understand some of this: The mainloop is not there to build the window, it is there to check for events, i.e. continually refresh all the widgets. Without, any events you bind will not be detected. Tk() is the first window you make, any after that are toplevel()s. Frame is a widget that I'm sure has benefits in some situations, but I havent found any difference between that and just inheriting from root. Hope this helps. On Feb 17, 2008 7:36 PM, [EMAIL PROTECTED] wrote: Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: from Tkinter import * foo = Tk() Google's great, but it has no truth meter. Do I inherit from Frame? Or is that a big mistake. (Both positions repeated frequently.) Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.) Here's the application. I'm creating a visual parser for my beginner's language. The starting position is a list of Statement objects, each being a list of Token objects. The statement is presented as a list of buttons with abbreviated token types ('Con_Int' for a CONSTANT_INTEGER token). Click the button and a dialog-like info display pops up with all the details about the token. During parsing, each recognition condenses tokens into productions, shortening the Statement. (Example: three Token buttons are replaced by one Addition production button.) An application window provides for stepping through the parsing and provides utility commands such as Close all those token windows I've got lying all over. Much less complex than IDLE, but GvR and cohorts seem to understand what's really going on. I don't. Help appreciated. -- http://mail.python.org/mailman/listinfo/python-list -- I intend to live forever - so far, so good. SaM -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Martin v. Löwis [EMAIL PROTECTED] writes: That's not exactly true, i.e. it isn't mark-and-sweep, but some similar scheme that allows incremental collection without write barriers. This particular scheme heavily relies on refcounting itself (specifically, an object is garbage in a certain generation when all references to it come from the same generation). Ah, thanks. I made another post which I guess is also somewhat wrong. -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
days_in_month 12: 31 30 28 31 ... 30 31 assign $days days_in_month[$month] This is missing days_in_month 12: 31 break 30 break Or the addition add $x' $x offset store $r0 $x' assign $days $r0 Is that 4 ticks or 5; or 24 blips? -- http://mail.python.org/mailman/listinfo/python-list
Re: Just for fun: Countdown numbers game solver
On 22 Jan., 23:56, [EMAIL PROTECTED] wrote: So anyone got an answer to which set of numbers gives the most targets from 100 onwards say (or from 0 onwards)? IsPythonupto the task? It's (5, 8, 9, 50, 75, 100): 47561 targets altogether (including negative ones), 25814 targets = 100. (BTW, 1226 sets of numbers give all 900 3-digit targets, but the above one doesn't; 954 and 981 are missing.) The following (mostly) straightforward program needed about 30 min for the calculation. I tried to keep all (intermediate) results in a dict (which turned out later to need too much memory). I wanted to use the same dict for memoization. So I let the dict fill itself on access. The disadvantage of this method is that the function needs to know that it's being memoized. The advantage is that it's easier (I think) to manipulate/control the dict. class AutoFillDict(dict): Values for missing keys are computed on-the-fly def __init__(self, func, *args, **kwargs): self.func = func super(AutoFillDict, self).__init__(*args, **kwargs) def __missing__(self, key): self[key] = self.func(self, key) return self[key] def subs_k(dic, (nums, k)): Return k-subsets of tuple nums, using dic as cache if k == 1: return [(i,) for i in nums] if k == len(nums): return [nums] a = nums[:1] b = nums[1:] return [a + i for i in dic[b, k-1]] + dic[b, k] def subs_and_complements(dic, nums): Return subsets and complements of tuple nums, using dic as cache if not nums: return set([((), ())]) a = nums[:1] b = nums[1:] res = set([(s, a + c) for s, c in dic[b]]) res.update([(a + s, c) for s, c in dic[b]]) return res subs_and_comps = AutoFillDict(subs_and_complements) def countdown(dic, nums, sac_dict=subs_and_comps): Return all possible goals for input nums, using dic as cache All numbers in nums must be used. if len(nums) == 1: return nums ret = set() for s, c in sac_dict[nums]: if s and c: xs = dic[s] ys = dic[c] ret.update([x + y for x in xs for y in ys]) ret.update([x - y for x in xs for y in ys]) ret.update([x * y for x in xs for y in ys]) ret.update([x // y for x in xs for y in ys if y != 0 and x % y == 0]) return ret def print_max(results): max_goals = max(results.values()) max_nums = [n for (n, g) in results.items() if g == max_goals] max_nums.sort() print Maximal number of reachable goals: %d % max_goals print Number of tuples: %d % len(max_nums) for n in max_nums: print n if __name__ == __main__: all_nums = range(1, 11)*2 + [25, 50, 75, 100] all_nums = tuple(sorted(all_nums)) subsets_k = AutoFillDict(subs_k) d = AutoFillDict(countdown) results = {} results100 = {} for i, nums in enumerate(sorted(set(subsets_k[all_nums, 6]))): # result is the union of reachable goals for all subsets result = set() for s, c in subs_and_comps[nums]: result.update(d[s]) results[nums] = len(result) results100[nums] = len([x for x in result if x = 100]) # Prevent MemoryError if i % 200 == 0: d.clear() print Goals: all integers print_max(results) print print Goals: integers = 100 print_max(results100) -- Wolfram -- http://mail.python.org/mailman/listinfo/python-list
Dont know what my class is called...
I am using the xml.sax package, and I'm running into a little problem. When I use the parse(url, ContentHandler()) method, I don't know what parse() is naming the instance of ContentHandler. I have a sub-class of ContentHandler make a dictionary of what it parses, but the problem is I don't know the name of instance for me to get at it. The only way I have gotten at my dict is to declare it a global value, and I know that is not the right way to do it. I though I would be clever and put print self inside the __int__ method of the ContentHandler sub-class, in hopes it would display its given name, but it returned a rather useless: __main__.FeedHandler instance at 0x02D8B5D0 So, any ideas on how to figure this out would be great. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dont know what my class is called...
Adam W. schrieb: I am using the xml.sax package, and I'm running into a little problem. When I use the parse(url, ContentHandler()) method, I don't know what parse() is naming the instance of ContentHandler. I have a sub-class of ContentHandler make a dictionary of what it parses, but the problem is I don't know the name of instance for me to get at it. The only way I have gotten at my dict is to declare it a global value, and I know that is not the right way to do it. I though I would be clever and put print self inside the __int__ method of the ContentHandler sub-class, in hopes it would display its given name, but it returned a rather useless: __main__.FeedHandler instance at 0x02D8B5D0 So, any ideas on how to figure this out would be great. It's a bit hard to get what you are after, but maybe this solves your problem? handler = FeedHandler() parse(handler) print handler.my_instance_variable_of_choice The above assumes that my_instance_variable_of_choice is created + filled within the handler of course. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
[EMAIL PROTECTED] wrote: days_in_month 12: 31 30 28 31 ... 30 31 assign $days days_in_month[$month] This is missing days_in_month 12: 31 break 30 break What shall there be missing? breaks? You noticed, that I defined some artificial architecture on purpose. days_in_month 12: tells it, that the next 12 blurps are tabular data, that can be indexed. If the interpreter hits the line days_in_month 12: it will unconditionally jump 12 instructions forward, where it hits the assign instruction, which assignes a value from a table by an index given into a variable. We're not talking about Python here. I introduced this architecture just to showcase that even in the most optimal situation, where special RISC operations are avaliable a LUT is still better suited. Wolfgang Draxinger -- E-Mail address works, Jabber: [EMAIL PROTECTED], ICQ: 134682867 -- http://mail.python.org/mailman/listinfo/python-list
Re: sockets -- basic udp client
On Feb 17, 12:15 pm, [EMAIL PROTECTED] (Douglas Wells) wrote: For example: import socket, sys host = 'localhost' #sys.argv[1] port = 3300 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.settimeout(1.0) buf = '' data = 'hello world' num_sent = 0 while num_sent len(data): num_sent += s.sendto(data, (host, port)) print Looking for replies; press Ctrl-C or Ctrl-Break to stop. while True: try: buf, addr = s.recvfrom(2048) except: pass #Will the following if statement do anything? # In this case it will cause the script to jump out of the loop # if it receives no data for a second. if not len(buf): break print Received from server: %s % buf The reason that this example *seems* to work is that you mask the exception. What is actually happening is that the timeout of the recvfrom call raises socket.timeout, which is ignored. Then because buf has been explicitly set to zero length (near the beginning of the program), and because it is *not* modified as a result of the recvfrom call, the length is still zero, and the break is executed. Try commenting out the try/except construct, or try actually providing at least one non-zero length response (such that buf is modified) and seeing if it ever terminates. Nice catch. I would also like to point out that the original example (quoted from the book) used connect' and recv w/ UDP). One of the purposes of using this construct (rather than using recvfrom) is to simplify identification of the remote system: When you connect to a UDP socket, the OS will only send messages to that system and will ignore messages that do not originate from that IP address (ignoring the issue IP address spoofing). I was hashing through that very issue last night. I was wondering how sendall() knew where to send the data. The author says this about the initial UPD example I posted(the one that calls connect()): ...there's no actual connection here. The call to connect() did nothing but initialize some internal parameters. I deduced that one such initialization was automatically resolving the hostname into an ip address. Thanks for the info on another important one. -- http://mail.python.org/mailman/listinfo/python-list
Re: Dont know what my class is called...
Adam W. [EMAIL PROTECTED] writes: I am using the xml.sax package, and I'm running into a little problem. When I use the parse(url, ContentHandler()) method, I don't know what parse() is naming the instance of ContentHandler. I'm not sure what you're asking. Why do you need to know the internal name for that parameter? I have a sub-class of ContentHandler make a dictionary of what it parses, but the problem is I don't know the name of instance for me to get at it. The only way I have gotten at my dict is to declare it a global value, and I know that is not the right way to do it. Perhaps this:: handler = ContentHandler() result = parse(url, handler) -- \ All my life I've had one dream: to achieve my many goals. -- | `\ Homer, _The Simpsons_ | _o__) | Ben Finney -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter Confusion
On Sun, 17 Feb 2008 11:36:25 -0800, MartinRinehart wrote: Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: from Tkinter import * foo = Tk() Depends on the platform if this shows a window. Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.) `Tk` is the main window, `Toplevel` for additional windows. Don't create several `Tk` instances. That usually causes very weird side effects. Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: Dont know what my class is called...
On Feb 17, 6:12 pm, Diez B. Roggisch [EMAIL PROTECTED] wrote: It's a bit hard to get what you are after, but maybe this solves your problem? handler = FeedHandler() parse(handler) print handler.my_instance_variable_of_choice The above assumes that my_instance_variable_of_choice is created + filled within the handler of course. Diez Doh! I never thought to try that because I assumed parse needed to initialize it itself or else it would go haywire, probably stemming from my belief that classes are comprised mostly of smoke and mirrors and are never to be fully understood ;) Thanks for tip. -- http://mail.python.org/mailman/listinfo/python-list
How to do a callback from externally triggered event
I am using PIL to make images that I need to display in a sequence. The image needs to change when a an event happens from a serial port. I call the following function to display the image, but then the application is waiting for an event. I need to return to the main code which is sending commands to a serial port and waiting for data to return then displaying the next chart. def display(R,G,B): img = Image.new('RGB',(1000,1000),(24,24,24)) draw = ImageDraw.Draw(img) draw.rectangle((400,400,600,600), fill=(R,G,B)) root = Tkinter.Tk() photo = ImageTk.PhotoImage(img) label = Tkinter.Label(root, image=photo) label.pack() root.mainloop() Here is the (very)rough program #!/usr/local/bin/pythonw # Import needed modules import Tkinter, ImageTk import Image, ImageDraw import time, serial def measure(): ser.write(M\n) line = ser.readline() print line, def display(R,G,B): img = Image.new('RGB',(1000,1000),(24,24,24)) draw = ImageDraw.Draw(img) draw.rectangle((400,400,600,600), fill=(R,G,B)) root = Tkinter.Tk() photo = ImageTk.PhotoImage(img) label = Tkinter.Label(root, image=photo) label.pack() root.mainloop() def setup_comm(): ser = serial.Serial('/dev/tty.KeySerial1') ser.write(PR701\n) time.sleep(.2) ser.flushOutput() line = ser.readline() print line # Program Starts here setup_comm() display(0, 255, 255) measure() display(255, 0, 255) measure() display(255, 255, 0) measure() -- http://mail.python.org/mailman/listinfo/python-list
Developing a Package with Sub Packages
I have created a group of scripts to manage an XML-based database. I'd like to make it into a proper package that will let me keep track of the code. I have a lot of files that are similar in name and they just get crowded in one folder. Here's a sample of the file structure: IMS/ IMS/__init__.py IMS/Config.py IMS/imsdefaults.cfg IMS/local.txt IMS/Data/ IMS/Data/stories.xml IMS/Story/ IMS/Story/__init__.py IMS/Story/StoryCreator.py When the IMS/__init__.py file is loaded, it creates the IMS/Data/ folder and if there are no xml files, it creates them and fills them in with some default values. The IMS/Config.py has a subclass of the ConfigParser, and reads the imsdefaults.cfg and local.txt files. When I run StoryCreator, buried in it's own package (that imports IMS) the data folder is created inside the Story folder, and I get an error message stating the ConfigParser object could not find the imsdefaults.cfg file. Here's what I think is happening: IMS/__init__.py uses os.getcwd() to establish the path to the data folder and the files inside of it. When I run StoryCreator, os.getcwd() returns the story folder. If I'm right, how can I get the IMS/__init__.py module to use relative paths to its own module, and not the current working directory the os module provides? This could also solve the problem with Config.py, I think. Thanks Josh English http://joshenglish.livejournal.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Looking for a Python Program/Tool That Will Add Line Numbers to a txt File
On Feb 14, 5:54 pm, W. Watson [EMAIL PROTECTED] wrote: See Subject. It's a simple txt file, each line is a Python stmt, but I need up to four digits added to each line with a space between the number field and the text. FWIW here is a Zeus editor, Python macro script to do this: import zeus def key_macro(): zeus.screen_update_disable() # get the line is the current document line_total = zeus.get_line_count() # message in status bar zeus.message(Line Count: %d % (line_total)) # write the line number to the start of each line for i in range(1, line_total + 1): zeus.set_line_pos(i, 1) number = %4d % (i) zeus.write(number) zeus.screen_update_enable() zeus.screen_update() key_macro() # run the macro To use the script, just load the text file into Zeus and then run the macro above from within Zeus. Jussi Jumppanen Author: Zeus for Windows IDE http://www.zeusedit.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter Confusion
[EMAIL PROTECTED] wrote: Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.) If you close the window created by Tk(), the program terminates. If you close a window created by Toplevel() only that window closes. The Tk() window remains open and the program continues to execute. -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter Confusion
On Feb 17, 12:36 pm, [EMAIL PROTECTED] wrote: Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: from Tkinter import * foo = Tk() You shouldn't care what happens in an interactive python session. In fact, you can take years off your life trying to figure out why the output of an interactive session is different from the output of a python program. Here is how to create a basic window with one widget: import Tkinter as tk root = tk.Tk() label = tk.Label(root, text='hello world') label.pack() #makes widget visible root.mainloop() Adding in some more details: import Tkinter as tk root = tk.Tk() root.geometry('600x400') root.config(background='red') label = tk.Label(root, text='hello world', background='gray') label.pack() #makes widget visible root.mainloop() Google's great, but it has no truth meter. Do I inherit from Frame? A frame is used to group widgets. You can have multiple frames each containing a group of widgets. That will allow you to place the group as a whole at a specific location in the window. Here's how to use frames to organize widgets: import Tkinter as tk root = tk.Tk() root.geometry('600x400') root.config(background='red') frame1 = tk.Frame(root) label1 = tk.Label(frame1, text='hello world', background='gray') label2 = tk.Label(frame1, text='goodbye', background='gray') label1.pack() #makes label visible label2.pack() #makes label visible frame1.pack(side=tk.BOTTOM) #makes frame visible frame2 = tk.Frame(root) label3 = tk.Label(frame2, text='yes', background='yellow') label4 = tk.Label(frame2, text='no', background='yellow') label5 = tk.Label(frame2, text='maybe', background='yellow') label3.pack() label4.pack() label5.pack() frame2.pack(side=tk.TOP) frame3 = tk.Frame(root) label6 = tk.Label(frame3, text='a', background='blue') label7 = tk.Label(frame3, text='b', background='blue') label8 = tk.Label(frame3, text='c', background='blue') label6.pack() label7.pack() label8.pack() frame3.pack(side=tk.LEFT) root.mainloop() Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.) Tk() for you first window; Toplevel() for any additional windows you want to open: import Tkinter as tk root = tk.Tk() root.geometry('300x200+50+50') #+x+y positions window root.config(background='red') label = tk.Label(root, text='hello world', background='gray') label.pack() window2 = tk.Toplevel() window2.geometry('300x200+400+50') root.mainloop() -- http://mail.python.org/mailman/listinfo/python-list
CSV module: incorrectly parsed file.
Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. When I use csv module to read this: import sys outf=open(sys.argv[1]+'.tsv','wt') import csv reader=csv.reader(open(sys.argv[1], rb)) for row in reader: outf.write('\t'.join([rr.strip() for rr in row]) +'\n') it mangles it, messing up the double double-quote. Can anyone help me? How do I use CSV to get it right? Tjhanks! c -- http://mail.python.org/mailman/listinfo/python-list
Cleaning Up an Application When the Window is Closed
Suppose I write some Win XP application using the Tkinter GUI, and do not provide a mechanism to exit other than the user clicking on the X in the upper right corner of a window. Is there a mechanism that will allow me to put up an save-dialog to give the user the option to save his present state of data? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module: incorrectly parsed file.
Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. The input format is ambiguous - how is the parser to distinguish between a double-quote in the field, and the double-quote that delimits the field? Excel would have written that field as tape 2 (it doubles double-quotes that appear within a field). You can turn off the double-double-quote handling by passing doublequote=False to the parser, but the results still might not be what you want (because the format is ambiguous). -- Andrew McNamara, Senior Developer, Object Craft http://www.object-craft.com.au/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
What shall there be missing? breaks? You noticed, that I defined some artificial architecture on purpose. days_in_month 12: tells it, that the next 12 blurps are tabular data, that can be indexed. If the interpreter hits the line days_in_month 12: it will unconditionally jump 12 instructions forward, where it hits the assign instruction, which assignes a value from a table by an index given into a variable. We're not talking about Python here. I introduced this architecture just to showcase that even in the most optimal situation, where special RISC operations are avaliable a LUT is still better suited. Month lengths are tabular, as opposed to formulaic. -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module: incorrectly parsed file.
On Feb 17, 8:09 pm, Christopher Barrington-Leigh [EMAIL PROTECTED] wrote: Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. What is needed to disambiguate this data is to only accept closing quotes if they are followed by a comma or the end of the line. In pyparsing, you can define your own quoted string format. Here is one solution using pyparsing. At the end, you can extract the data by field name, and print it out however you choose: data = \ number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 from pyparsing import * # very special definition of a quoted string, that ends with a only if # followed by a , or the end of line quotedString = ('' + ZeroOrMore(CharsNotIn('')|('' + ~FollowedBy(','|lineEnd))) + '') quotedString.setParseAction(keepOriginalText, removeQuotes) integer = Word(nums).setParseAction(lambda toks:int(toks[0])) value = integer | quotedString | Word(printables.replace(,,)) # first pass, just parse the comma-separated values for line in data.splitlines(): print delimitedList(value).parseString(line) print # now second pass, assign field names using names from first line names = data.splitlines()[0].split(',') def setValueNames(tokens): for k,v in zip(names,tokens): tokens[k] = v lineDef = delimitedList(value).setParseAction(setValueNames) # parse each line, and extract data by field name for line in data.splitlines()[1:]: results = lineDef.parseString(line) print Desc:, results.description print results.dump() Prints: ['number', 'name', 'description', 'value'] [1, 'wer', 'tape 2', 5] [1, 'vvv', 'hoohaa', 2] Desc: tape 2 [1, 'wer', 'tape 2', 5] - description: tape 2 - name: wer - number: 1 - value : 5 Desc: hoohaa [1, 'vvv', 'hoohaa', 2] - description: hoohaa - name: vvv - number: 1 - value : 2 -- Paul -- http://mail.python.org/mailman/listinfo/python-list
Re: Cleaning Up an Application When the Window is Closed
On Feb 17, 8:31 pm, W. Watson [EMAIL PROTECTED] wrote: Suppose I write some Win XP application using the Tkinter GUI, and do not provide a mechanism to exit other than the user clicking on the X in the upper right corner of a window. Is there a mechanism that will allow me to put up an save-dialog to give the user the option to save his present state of data? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net You just need to catch the close event, which appears to be WM_DELETE_WINDOW in Tkinter. The following article details its usage near the bottom of the page under the heading Protocols: http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm It actually mentions your problem specifically. I've never done it as I usually use wxPython. HTH Mike -- http://mail.python.org/mailman/listinfo/python-list
Help! pty interact with bash
#!/usr/bin/env python import os, pty, time class pty_Popen: def __init__ (self, command, *args): self.pid, self.fd = pty.fork () if self.pid == 0: os.execv (command, command, args) else: pass def read (self, max_read): return os.read (self.fd, max_read) def write (self, text): return os.write (self.fd, text) p=pty_Popen(/bin/bash) p.write(ls --color=always\nexit\n) print p.read(1024) I am implementing a wrapper for linux shells with codes above. This is not responding right, anybody know why? ps How can I tell which output is stdout or stderr in os.read() ? -- http://mail.python.org/mailman/listinfo/python-list
Pmw Use and Grayson's Book
I don't have Grayson's Tkinter book, but I see he uses something called Pmw. Why is it needed with Tkinter? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Cleaning Up an Application When the Window is Closed
Good. Thanks. Mike Driscoll wrote: On Feb 17, 8:31 pm, W. Watson [EMAIL PROTECTED] wrote: Suppose I write some Win XP application using the Tkinter GUI, and do not provide a mechanism to exit other than the user clicking on the X in the upper right corner of a window. Is there a mechanism that will allow me to put up an save-dialog to give the user the option to save his present state of data? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net You just need to catch the close event, which appears to be WM_DELETE_WINDOW in Tkinter. The following article details its usage near the bottom of the page under the heading Protocols: http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm It actually mentions your problem specifically. I've never done it as I usually use wxPython. HTH Mike -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Pmw Use and Grayson's Book
On Feb 17, 9:33 pm, W. Watson [EMAIL PROTECTED] wrote: I don't have Grayson's Tkinter book, but I see he uses something called Pmw. Why is it needed with Tkinter? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net It's not needed. It's just an extra set of widgets that are not included in the standard set. As I recall, PMW = Python Mega-Widgets. Check out their site: http://pmw.sourceforge.net/ You'll probably also come across Tix, another subset of widgets, which I think is now included with Python be default from 2.4 on up. It should be noted that PMW is not included with the standard Python distro though. Mike -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module: incorrectly parsed file.
On Feb 17, 7:09 pm, Christopher Barrington-Leigh [EMAIL PROTECTED] wrote: Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. When I use csv module to read this: import sys outf=open(sys.argv[1]+'.tsv','wt') import csv reader=csv.reader(open(sys.argv[1], rb)) for row in reader: outf.write('\t'.join([rr.strip() for rr in row]) +'\n') it mangles it, messing up the double double-quote. Can anyone help me? How do I use CSV to get it right? Tjhanks! c Try this: infile = open('data.txt') outfile = open('outfile.txt', 'w') for line in infile: pieces = line.strip().split(',') data = [] for piece in pieces: if piece[0] == '': data.append(piece[1:-2]) else: data.append(piece) out_line = '%s\n' % '\t'.join(data) outfile.write(out_line) -- http://mail.python.org/mailman/listinfo/python-list
Re: Pmw Use and Grayson's Book
I wonder why he uses it? If I want to run his examples, where do I put the lib he includes? Same folder as the example? Mike Driscoll wrote: On Feb 17, 9:33 pm, W. Watson [EMAIL PROTECTED] wrote: I don't have Grayson's Tkinter book, but I see he uses something called Pmw. Why is it needed with Tkinter? -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net It's not needed. It's just an extra set of widgets that are not included in the standard set. As I recall, PMW = Python Mega-Widgets. Check out their site: http://pmw.sourceforge.net/ You'll probably also come across Tix, another subset of widgets, which I think is now included with Python be default from 2.4 on up. It should be noted that PMW is not included with the standard Python distro though. Mike -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: Pop-up Menu of a Graphics Image?
Thanks. I'm just getting started in Python, so probably don't want to go far off the main stream. Don't see Misc. I see things like Presentations, Search this site, ... Mike Driscoll wrote: On Feb 15, 2:28 pm, W. Watson [EMAIL PROTECTED] wrote: I want to allow a user who is looking at a graphic to be able to right-click on the graphic to produce a menu of choices. ... snip wxPython can do a right-click menu like that and I know that PIL has been integrated into it as well. They have a Demo on the wxPython.org website that shows off all the official widgets as well as some custom scripts. If you run it, there is a tree control on the left. Go to the Miscellaneous section and check out the FileHistory demo. There are also a lot of graphics demos in there too under the Using Images category. Mike -- Wayne Watson (Nevada City, CA) Web Page: speckledwithStars.net -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module: incorrectly parsed file.
On Feb 17, 9:11 pm, 7stud [EMAIL PROTECTED] wrote: On Feb 17, 7:09 pm, Christopher Barrington-Leigh [EMAIL PROTECTED] wrote: Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. When I use csv module to read this: import sys outf=open(sys.argv[1]+'.tsv','wt') import csv reader=csv.reader(open(sys.argv[1], rb)) for row in reader: outf.write('\t'.join([rr.strip() for rr in row]) +'\n') it mangles it, messing up the double double-quote. Can anyone help me? How do I use CSV to get it right? Tjhanks! c Try this: infile = open('data.txt') outfile = open('outfile.txt', 'w') for line in infile: pieces = line.strip().split(',') data = [] for piece in pieces: if piece[0] == '': data.append(piece[1:-2]) else: data.append(piece) out_line = '%s\n' % '\t'.join(data) outfile.write(out_line) Whoops. The line: data.append(piece[1:-2]) should be: data.append(piece[1:-1]) -- http://mail.python.org/mailman/listinfo/python-list
Re: Solve a Debate
Wolfgang Draxinger wrote: Somehow you seem to think, that a lookup table will require more resources (memory I guess you thought) than a sequence of comparisons. However you didn't take into account, that the program code itself requires memory, too (for the operation codes). In Python, there's the additional twist that for lookup tables based on a mutable type, such as a dictionary, you need to execute code to build the dictionary. So you end up using very roughly twice the memory -- for the code to build the dictionary, and the dictionary itself. If the dictionary isn't too huge, and it's looked up many times during each run of the program, the extra speed of the dict lookup is probably worth the overhead of creating it. If the dict is very large, and is consulted relatively few times in a given run, then it might well be faster and not use too much more memory to use a tree (NOT a linear sequence!) of if-else statements in the code. You could also consider loading the dict from some other form, such as a pickle, instead of creating it using code. This would use less memory, although probably would take roughly the same time to set up the table. For extremely large infrequently-used tables, it's probably better to look at not loading the whole table into memory at all, and keeping it in some kind of external indexed structure such as a b-tree, relational database, etc. In other languages, the tradeoffs will be completely different. E.g. in C, if you can describe the table entirely with static data, it'll be very fast to load and incur no overhead for code to create it at all. Also, with demand-paging out of the executable file, and infrequent lookups, only parts of the table will actually get loaded anyway. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: CSV module: incorrectly parsed file.
7stud wrote: On Feb 17, 9:11 pm, 7stud [EMAIL PROTECTED] wrote: On Feb 17, 7:09 pm, Christopher Barrington-Leigh [EMAIL PROTECTED] wrote: Here is a file test.csv number,name,description,value 1,wer,tape 2,5 1,vvv,hoohaa,2 I want to convert it to tab-separated without those silly quotes. Note in the second line that a field is 'tape 2' , ie two inches: there is a double quote in the string. When I use csv module to read this: import sys outf=open(sys.argv[1]+'.tsv','wt') import csv reader=csv.reader(open(sys.argv[1], rb)) for row in reader: outf.write('\t'.join([rr.strip() for rr in row]) +'\n') it mangles it, messing up the double double-quote. Can anyone help me? How do I use CSV to get it right? Tjhanks! c Try this: infile = open('data.txt') outfile = open('outfile.txt', 'w') for line in infile: pieces = line.strip().split(',') data = [] for piece in pieces: if piece[0] == '': data.append(piece[1:-2]) else: data.append(piece) out_line = '%s\n' % '\t'.join(data) outfile.write(out_line) Whoops. The line: data.append(piece[1:-2]) should be: data.append(piece[1:-1]) Even when you have done all this you will still have problems. As Andrew pointed out the form is ambiguous, and you'd just better hope none of your data items look like Nails 2, soldiers for the use of because then you will be completely screwed. So there's a need for a certain amount of visual scrutiny of the data: I would definitely write a validation program first that tries to read the data and catches any exceptions like unmatched quotes or the wrong number of items in a line. If there aren't too many (and there usually aren't) just edit them out of your input data by hand. If this is to be a regular task then you'll have to program to recognize and correct the common error cases. regards Steve -- Steve Holden+1 571 484 6266 +1 800 494 3119 Holden Web LLC http://www.holdenweb.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: How about adding rational fraction to Python?
On Feb 17, 1:45 pm, Lie [EMAIL PROTECTED] wrote: Any iteration with repeated divisions and additions can thus run the denominators up. This sort of calculation is pretty common (examples: compound interest, numerical integration). Wrong. Addition and subtraction would only grow the denominator up to a certain limit I said repeated additions and divisions. Anyways, addition and subtraction can increase the denominator a lot if for some reason you are inputing numbers with many different denominators. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Paul Rubin wrote: As I understand it, Python primarily uses reference counting, with a mark and sweep scheme for cycle breaking tacked on as an afterthought. It's not mark-and-sweep, it's a cycle detector. It goes through all allocated objects of certain types, and all objects reachable from those objects, trying to find sets of objects whose reference counts are all accounted for by references within the set. Then it picks an arbitrary object in each set and clears all the references from that object. This breaks the cycle, and allows all the objects in it to be reclaimed by their reference counts dropping to zero. (There's some kind of generational scheme in there as well, I think, but I don't know the details.) In a sense, this is the opposite of mark-and sweep. A mark-and- sweep collector assumes that everything is garbage unless it can be proven that it's not. Python's cycle detector, on the other hand, assumes that nothing is garbage unless it can be proven that it is. An advantage of this refcount/cycle detection scheme is that there is no root set that must be maintained at all costs. This makes it fairly easy to interface Python with external libraries that have their own notions of how to manage memory. It doesn't move objects in memory during GC so you can get fragmentation. It never moves PyObject structs, but variable-sized objects such as lists have their contents stored in a separate block of memory, that can be moved when its size changes. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Memory Manager
Christian Heimes wrote: In release builds PyObject_HEAD only contains the ref count and a link to the object type. In Py_DEBUG builds it also contains a double linked list of all allocated objects to debug reference counting bugs. There's also a doubly-linked list used by the cycle detector, but it doesn't hold all objects, only those that need to participate in cyclic GC. Objects such as numbers and strings, which don't contain references to other objects, don't need to be on this list, since they can never be part of a cycle. Some other immutable types such as tuples also don't need to be on it, even though they contain references, because it's impossible to create a cycle consisting entirely of such objects. There has to be at least one mutable object in the cycle, and the GC will be able to find the cycle via that object. -- Greg -- http://mail.python.org/mailman/listinfo/python-list
Re: How to get current module object
En Sun, 17 Feb 2008 16:25:44 -0200, Alex [EMAIL PROTECTED] escribi�: Can I get reference to module object of current module (from which the code is currently executed)? I know __import__('filename') should probably do that, but the call contains redundant information (filename, which needs to be updated), and it'll perform unnecessary search in loaded modules list. It shouldn't be a real problem (filename can probably be extracted from the traceback anyway), but I wonder if there is more direct and less verbose way. sys.modules[__name__] Why do you want to get the module object? globals() returns the module namespace, its __dict__, perhaps its only useful attribute... -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list
[issue2130] [feature-request] Please add bool data type to optparse module
Guilherme Polo added the comment: Have you read http://docs.python.org/lib/optparse-standard-option-actions.html ? If yes, what is the problem with store_true/store_false ? -- nosy: +gpolo __ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue2130 __ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com