Re: [Tutor] this module
"Mark Young" wrote Thanks everybody, I didn't know modules ran code when you imported them, I just thought they defined the functions, etc. in them. They do that too. But in Python a function definition is created by running the code! So you write def foo(): return 42 To define foo you need to execute those two lines of code. The result is that you create a function object. So when you import a moduile it runs all of the code in there, and most of the code usually consists of function and variable definitions. But it can be anything at all. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] hi everyone!
i am new one to this programming language. i like to learn python,what i do? what i do to learn python in proper manner. -- Regards, S.Malathi. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] this module
Alan Gauld wrote: "Mark Young" wrote Thanks everybody, I didn't know modules ran code when you imported them, I just thought they defined the functions, etc. in them. They do that too. But in Python a function definition is created by running the code! So you write def foo(): return 42 To define foo you need to execute those two lines of code. The result is that you create a function object. So when you import a moduile it runs all of the code in there Almost. It runs all the top-level code. def foo(): print 3 def foo(): will run, defining the function foo print 3 will NOT run unless some other top-level code CALLs foo such as foo() most of the code usually consists of function and variable and class definitions. But it can be anything at all. Note also that "usually" is very hard to measure unless you read and or write many many modules. -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: >>> FWIW: >>> >>> When using relative paths I got extra ../../ terms, so I changed >> join_relative() to: >>> def join_relative(base, path): >>> return os.path.normpath(os.path.join(script_dir(base), path)) >>> >>> >>> Seems to work... >> >> Yeah, good catch ... looks great, and thanks for sharing your mod. >> > > > Glad I could return the favor. :) > > Since then, I took it a bit further for use in my code... I am not finished > with it yet, but am curios what you think. (I am not sure named things right > or structured it logically) > > > ### > > import os > class HomePath(object): > """For finding paths based on a home/install directory. > > """ > > def __init__(self, home_dir = None, home_file = None): > """Must be called with either a path to a directory or, as a > shortcut, a file in that directory. > > """ > > if home_file != None: > # Set home based on a path to a file in its directory > self.home = os.path.normpath(self.fix_path(home_file)) > > elif home_dir != None: > # Set home based on its path > self.home = os.path.normpath(self.get_dir(home_dir)) > > else: > raise Exception("Must call with either a path to a directory > or, as a shortcut, a file in that directory.") > > def abs(self, rel_from_home): > """Return an absolute path when passed a path relative to home. > > """ > > return self.join_relative(self.home, rel_from_home) > > def fix_path(self, base): > return os.path.realpath(os.path.abspath(base)) > > def get_dir(self, base): > return os.path.dirname(self.fix_path(base)) > > def join_relative(self, base, path): > return os.path.normpath(self.fix_path(os.path.join(self.home, > path))) > # I'm not sure I understand your end goal, given the args to __init__. I'd guess, perhaps incorrectly, that you may be trying to shoehorn conflicting purposes. If your only aim is to calculate an absolute path to a resource's parent, or home, folder then your class can be simplified a lot, IMHO. Specifically, I don't see a reason to differentiate between home_dir and home_file. Please feel free to clarify if I'm missing the point. import os class HomePath(object): def __init__(self, path): # fix_path and get_dir on base path self.home = os.path.dirname( os.path.realpath( os.path.abspath(path) ) ) def join(self, relative_path): return os.path.normpath(os.path.join(self.home, relative_path)) Also, have a look at http://docs.python.org/library/os.path.html#os.path.abspath ... the main point is that your initial path element must have enough information in it to construct an abspath, otherwise os.getcwd() can pollute the calculation. I believe if you use __file__ you're good, and sys.argv[0], while potentially relative in nature should always be relative to os.getcwd() AFAICT, so you're good there too. Otherwise avoid constructing a HomePath object with a relative start path. HTH, Marty ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] hi everyone!
"malathi selvaraj" wrote i am new one to this programming language. Hello and welcome. what i do to learn python in proper manner. First of all make sure you download Python v2.6 rather than v3. v3 is not ideal for learning just yet it is still rather new. Do you know any other programming language? If so go to the official tutorial on the python web site (or download the docs) and work through it. That should be enough to get you started. Ask questions here, show us the code and any errors you get (in full) http://docs.python.org/tutorial/ If this is your first programming language then well done, you made a good choice! :-) Go to here: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers pick a tutorial(not the official one!) and work through it. Every time you get stuck - and you will, so don't worry - send a question here explaining what is bugging you and any code plus error messages. (It also helps to mention the Python version, the OS and the tutorial you are using) Either way have fun. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] droplet like behaviour in Python
On 2009-08-08 19:34:44 -0400, "Alan Gauld" said: "pedro" wrote is one thing I am not really sure how to do. I want to be able to drop a file onto a python script (or app I guess) and have the python script use the path to the file that was dropped on it as sys.argv[1] I may be wrong but I thought that, provided you had the path and shebang etc set up properly that it just happened. Have you tried a simpe script that just prints argv say? Well I made a script called droplet.py which looks like this: #!/usr/bin/env python # encoding: utf-8 import sys theFilePath = sys.argv[1] print theFilePath But when I try to drop something on it nothing happens. Sorry I guess there is something fundamental that I am missing. Pete ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor