Re: [Tutor] function and module
linda.s [EMAIL PROTECTED] wrote I am reading a sample code and want to figure out where a function (for instance, abc) is from. There are many lines such as from XXX import * This is one of the chief reasons why 'from x import *' is bad practice. Is there a way not going through all these imported modules to find where the abc is from (by the way, the abc function is not in the current module)? There are a couple of possibilities. 1) if you can import the moduile into a propmpt you could ask abc where it's from. print abc.__file__ should work 2) if the import won't work I think the next best technoque is probably to use grep, especially if the modules are all in a common directory tree. grep def abc */*.py Or write a short Python script to traverse the tree using os.walk and try doing 'from foo import abc' on each file HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Of integers, relations and trees
I have this problem which I thought would be trivial, but I can't seem to figure out a decent way to do it. Say I have the following file: 10 -100 -101 -103 -108 --1080 ---1080.10 ---1080.11 12 -120 -125 20 30 -300 --3010 ---3010.3 These numbers represents a tree-like structure. In lack of a better explanation, here is how it works: A level in the tree follows the following: x * 10^level x * 10^1 belongs to level 1 in the three x * 10^2 belongs to level 2 in the three. etc. So, the top-nodes in the three are 10, 12, 20 and 30. The childs of 10 is 100, 101, 103 and 108. The child of 108 is 1080. The child of 1080 is 1080.10 and 1080.11 and these are the leaves. I decided to make this pretty straightforward so I wrote a Node class and a Tree class. A Node has a key which is an integer, as well as some additional information that isn't relevant to the structure. It also has a link to it's sibling, which is the next node on the same level. And a link to it's first child. So for 10, it looks like this.: 10 - 20 (siblings of 10) | (child) 100 - 101 - 103 - 108 (siblings of 100) Inserting a sibling is done like this: - def addSibling(self, newNode): tmp = self.node # current node last = self.node # previous node # find the node that is the direct sibling to the new node while( tmp != None tmp['key'] newNode['key']): last = tmp tmp = tmp['sibling'] # insert the new node after the node with a lower key last['sibling'] = newNode # If there is a node with a higher key, add it as a sibling to the new node if( tmp != None ): newNode['sibling'] = tmp - This code does not handle a special case where the newNode has the smallest key among the siblings and should be placed first and thus be the direct child of the parent level. But, that doesn't really matter. How do I know if I have a sibling or a child? Simple, I just check the length: - if( len(str(node1[key])) == len(str(node2[key])) ): - If the length, amount of integers, is the same, they are siblings. My problem is this: Say I have created a new node with the key 2080. I start of with my root node which has a key of 0. 2080 is not a sibling of 0, so I call a recursive function named addChild(). addChild checks the child of 0, which is 10 and determines that 2080 is not a sibling of 10. But, it's not a child either. Here comes my query: How do I determine that 2080 is not a child of 10. Or how do i determine that 536798 is not a child of 536780? And how do I determine that it is a child? I'll try to explain again: 5.36798 * 10^5 is at level 5 in the tree. It's direct children looks like this: 5.36798x * 10^6. 5.36797x * 10^6 is NOT a child, it is a child of 5.36797 * 10^5. Does this make sense to anyone? :) Also, consider the following: 5.36798xxx * 10^8 While this is not a direct child of 5.36798 * 10^5, it is a child of a child and belongs in that subtree. I can't seem to rack my brains around a solution for this. Maybe it's my tree-structure that is making this more complex than it should be? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function and module
Alan Gauld wrote: There are a couple of possibilities. 1) if you can import the moduile into a propmpt you could ask abc where it's from. print abc.__file__ should work __file__ is a module attribute, not a function attribute. abc.__module__ will give the name of the module abc is from. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] problems pickling functions
Hi, I'm writing a program for tagging which requires a long time to calculate the parameters. I have therefore tried to write a long program that pickles all the data, and pickles a function that uses these parameters to tag an input sentence. But I'm having trouble with loading the function. The odd thing is that it works fine in the interpreter to pickle and load a function: import pickle def simple(): ... print This works ... f=open(simple.txt,w) pickle.dump(simple,f) f.close() s() Traceback (most recent call last): File stdin, line 1, in module NameError: name 's' is not defined f=open(simple.txt,r) s=pickle.load(f) s() This works However when I try to do this with the script simple.py (with the exact same commands as above) it doesn't work: $ cat simple.py import pickle def simple(): print This works f = open(simple.txt,w) pickle.dump(simple,f) f.close() $ python simple.py $ python Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type help, copyright, credits or license for more information. import pickle f2 = open(simple.txt,r) s Traceback (most recent call last): File stdin, line 1, in module NameError: name 's' is not defined s = pickle.load(f2) Traceback (most recent call last): File stdin, line 1, in module File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1370, in load return Unpickler(file).load() File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 858, in load dispatch[key](self) File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1090, in load_global klass = self.find_class(module, name) File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1126, in find_class klass = getattr(mod, name) AttributeError: 'module' object has no attribute 'simple' I don't get this error message, and I'm annoyed – because I'm used to that things that work in the interpreter also work when written as a program. Can anyone help? regards, Arild Næss ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems pickling functions
Arild B. Næss wrote: Hi, I'm writing a program for tagging which requires a long time to calculate the parameters. I have therefore tried to write a long program that pickles all the data, and pickles a function that uses these parameters to tag an input sentence. But I'm having trouble with loading the function. The odd thing is that it works fine in the interpreter to pickle and load a function: import pickle def simple(): ... print This works ... f=open(simple.txt,w) pickle.dump(simple,f) f.close() s() Traceback (most recent call last): File stdin, line 1, in module NameError: name 's' is not defined f=open(simple.txt,r) s=pickle.load(f) s() This works However when I try to do this with the script simple.py (with the exact same commands as above) it doesn't work: $ cat simple.py import pickle def simple(): print This works f = open(simple.txt,w) pickle.dump(simple,f) f.close() $ python simple.py $ python Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type help, copyright, credits or license for more information. import pickle f2 = open(simple.txt,r) s Traceback (most recent call last): File stdin, line 1, in module NameError: name 's' is not defined s = pickle.load(f2) Traceback (most recent call last): File stdin, line 1, in module File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1370, in load return Unpickler(file).load() File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 858, in load dispatch[key](self) File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1090, in load_global klass = self.find_class(module, name) File /Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/pickle.py, line 1126, in find_class klass = getattr(mod, name) AttributeError: 'module' object has no attribute 'simple' I don't get this error message, and I'm annoyed – because I'm used to that things that work in the interpreter also work when written as a program. From the docs for pickle (13.1.4 What can be pickled and unpickled?): Note that functions (built-in and user-defined) are pickled by ``fully qualified'' name reference, not by value. This means that only the function name is pickled, along with the name of module the function is defined in. Neither the function's code, nor any of its function attributes are pickled. Thus the defining module must be importable in the unpickling environment, and the module must contain the named object, otherwise an exception will be raised. Your code works from the interpreter because 'simple' is still defined. If you 'del simple' before you unpickle I think you will get the same error as you get in the script. Searching comp.lang.python for 'pickle function' yields a few possible workarounds but they are messy. Why do you need to pickle the function? Is it created dynamically? Can you just pickle the data? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problems pickling functions
Arild B. Næss wrote: Den 8. des. 2006 kl. 14.05 skrev Kent Johnson: Why do you need to pickle the function? Is it created dynamically? Can you just pickle the data? Kent Thanks. I guess it's not absolutely necessary to pickle the function. I tried to do this because I wanted to use the function in the interpreter without having to write it in there line by line. I'm used to working in R and Matlab, where you often run scripts from the active interpreter. In that way you can actually examine the data a script generates, instead of having the script print it to screen or file. I'm having trouble getting used to python like this because I get trouble trying to paste in several lines at once from emacs, and I haven't found a way to run scripts in the interpreter. Two suggestions: - Use an editor / IDE that allows you to run Python scripts. IDLE will do this. I think emacs has good support for Python too but someone who uses emacs will have to help you with that one. - Save your function in a module and import the module from the interpreter. Then you can run the function in the interpreter. For example if you have funcs.py in the working directory and it contains a function def doItAll(): pass then in the interpreter you can type import funcs funcs.doItAll() to run the function. If you change the function in an external editor you have to reload it in the interpreter to get the revised version: reload(funcs) Kent PS Please reply to the list, not to me directly. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter Canvas
Hi Folks, I have a Tkinter canvas and a yscrollbar attached to it. It is working fine but what I want is that when the mouse is in the canvas region, the scroll button of the mouse should be able to control the upward and downward movement, How to achieve this? Regards, Asrarahmed Kadri -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] function and module
Kent Johnson [EMAIL PROTECTED] wrote There are a couple of possibilities. 1) if you can import the moduile into a propmpt you could ask abc where it's from. print abc.__file__ should work __file__ is a module attribute, not a function attribute. abc.__module__ will give the name of the module abc is from. Oops! Thanks for the catch Kent. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OT GPL project finished, presentation looming
On Thursday 07 December 2006 22:35, Alan Gauld wrote: Dave S [EMAIL PROTECTED] wrote They will be concerned about using my app because I am one person. What if I get hit by a bus ! what if I leave ? This is a common problem in big companies including my own. For years they wouldn't even use the GNU software because it was unsupported. I even had to buy a commercial version of emacs for about $300... Ouch that must have hurt :( Eventually cygnus started offering support (for $5K per year) and they allowed us to use emacs, gcc etc Eventually even X windows. Cool :) Now they are more relaxed and we use Linux to run our internal DHCP and DNS, even some web servers... ...(It appears that none of the company's IT professionals can program !) That's also not unusual. In fact our company appears to be slowly heading that way. We used to have 11,000 IT professionals of which around 5000 were developers. Now we have 13,000 IT professionals of whom about 1000 still write code. The coding is mainly offshored to India and Eastern Europe. Our internal people are being retrained on higher value roles like business analysis, design/architecture, deployment/integration and project management. So sad - programming is s much creative fun. I have heard it being called an art form - I would agree with that. So I now program in python as and when I can and call it prototyping... They call it progress. 'progress' ... mmm 'modern man management' ... (cynical mmm ...) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter Canvas
Asrarahmed Kadri wrote: Hi Folks, I have a Tkinter canvas and a yscrollbar attached to it. It is working fine but what I want is that when the mouse is in the canvas region, the scroll button of the mouse should be able to control the upward and downward movement, How to achieve this? You should be able to bind the events that the mouse wheel generates in the canvas widget to the function that causes the scroll bar to scroll (forget what it's called.) Have you tried this yet? Regards, Asrarahmed Kadri -- To HIM you shall return. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mlab and numeric
On Friday 08 December 2006 06:25, linda.s wrote: can anyone tell me the relationship between MLab and Numeric? Especially MLab, there is very little information about it. There seem to be several Mlab modules. At least Numeric, Matplotlib and Numarray have an Mlab module. They all seem to be additional functions to make users with matlab experience happy. Maybe usefull for you is this longer explanation: http://matplotlib.sourceforge.net/pylab_commands.html Eike. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Create a script to make bootable USB device.
Just curious as to why nobody has at least attempted an answer for this. Is what I'm asking simply unknown? Or it is impossible to do? No big deal either way... just curious because I'm seriously interested in this. Thanks. On 12/6/06, Chris Hengge [EMAIL PROTECTED] wrote: Is this something I can do using just python and libraries? I know I could automate other utilities, but I'd like to write some kind of neat utility myself that I could play with for more experience. Goal: make USB drive bootable to a dos prompt (dont care what dos, assuming I need a bootable image for this) make script prompt for file(s) to move to disk (already know how to do this) Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor