[Tutor] Scope of exceptions
Howdy, I am in the process of moving all my perl scripts to python and also trying to learn to program rather than just know enough to automate sections of my job. I am trying exit cleanly if I force the script to exit early. I am working my way through core python but not yet read properly about exceptions so this is how I guessed exceptions to work. try: statements extractData() more_statements even_more_statements except KeyboardInterrupt: exit('Exiting') Once it enters/runs the extractData func and if I choose to exit (control c) it throws an exception rather than exiting cleanly. Do I need to put a 'KeyboardInterrupt' exception for every function? Thanks in advance, Craig ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scope of exceptions
Instead of putting it in every function, you could probably do it once in your "main" function that does all the work... On Wed, Aug 5, 2009 at 4:34 PM, Craig McDonald wrote: > Howdy, > > I am in the process of moving all my perl scripts to python and also > trying to learn to program rather than just know enough to automate > sections of my job. I am trying exit cleanly if I force the script to > exit early. I am working my way through core python but not yet read > properly about exceptions so this is how I guessed exceptions to work. > > try: >statements >extractData() >more_statements >even_more_statements > except KeyboardInterrupt: >exit('Exiting') > > Once it enters/runs the extractData func and if I choose to exit > (control c) it throws an exception rather than exiting cleanly. Do I > need to put a 'KeyboardInterrupt' exception for every function? > > Thanks in advance, > > Craig > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- http://alitechtips.blogspot.com/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Scope of exceptions
"Craig McDonald" wrote try: statements extractData() more_statements even_more_statements except KeyboardInterrupt: exit('Exiting') Once it enters/runs the extractData func and if I choose to exit (control c) it throws an exception rather than exiting cleanly. Do I need to put a 'KeyboardInterrupt' exception for every function? It should work as you've shown. What should happen is that if you type Ctrl-C while the script is executing extractData() then Python will raise the exception and the function will not know what to do with it, so it raises it to the next level where your handler should catch it. So what does your real handler look like? And what does the exception that is raised look like? 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] Configuaration files and paths?
Hello, What is the recommended way to configure my application find the various database and/or configuration files it needs? For instance my folder layout: /path_to_app/app.py /path_to_app/lib/ /path_to_app/database/ /path_to_app/config/ /path_to_app/photos and so on. (app.py being the main file, and the lib folder containing my custom modules.) I would like to make an .ini in the config folder that contains entries that point to the other folders. Seems like a resonable idea. So my questions are: 1) How does my main app file find the config file in the first place? 2) How should I point to those other folders in the ini file? Thank you ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
"Allen Fowler" wrote What is the recommended way to configure my application find the various database and/or configuration files it needs? Recommemded by whom? A lot depends on the OS. Apple for example have one set of recommendations for MacOS, Windows has another and Linux has several to choose from! For instance my folder layout: /path_to_app/app.py /path_to_app/lib/ /path_to_app/database/ /path_to_app/config/ /path_to_app/photos and so on. I would like to make an .ini in the config folder Seems fair enough, however on a Unix system you should also consider allowing the user to have their own personalised version in their home directory. Thus at startup you get the current user ID / home directory and look for a suitable config file. If it exists read it, if not read the default one in your config directory. 1) How does my main app file find the config file in the first place? Generally use a relative path so normally your app will run from its home folder so you can look in ./config. You might also set a system environment variable - for example the CLASSPATH or PYTHONPATH variables, or the ORACLE_HOME used by Oracle for their database. If the environment var is not set then look in ./config 2) How should I point to those other folders in the ini file? Once you locate config the others are simply ../xxx relative to config. 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
Re: [Tutor] Configuaration files and paths?
> > What is the recommended way to configure my application find the various > database and/or configuration files it needs? > > Recommemded by whom? A lot depends on the OS. Apple for example have one set > of > recommendations for MacOS, Windows has another and Linux has several to > choose > from! > Thank you good point. Planning on using a ConfigParser based .ini file located in the ./config folder. > > For instance my folder layout: > > > > /path_to_app/app.py > > /path_to_app/lib/ > > /path_to_app/database/ > > /path_to_app/config/ > > /path_to_app/photos > > > > and so on. I would like to make an .ini in the config folder > > Seems fair enough, however on a Unix system you should also consider allowing > the user to have their own personalised version in their home directory. Thus > at > startup you get the current user ID / home directory and look for a suitable > config file. If it exists read it, if not read the default one in your config > directory. > > > 1) How does my main app file find the config file in the first place? > > Generally use a relative path so normally your app will run from its home > folder > so you can look in ./config. You might also set a system environment variable > - > for example the CLASSPATH or PYTHONPATH variables, or the ORACLE_HOME used by > Oracle for their database. If the environment var is not set then look in > ./config > Assuming the application could be invoked in odd ways that may alter the notion of the current working directory, how do I unambiguously find the absolute path to the current python source file? (So I can load the nearby .ini) As a follow-up question, how do give my modules stored under ./lib access to the data in my ConfigParser object? (For instance, database connection string, storage path, etc.) I guess a global ConfigParser object would work, but that seems wrong. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Configuaration files and paths?
Allen Fowler wrote: What is the recommended way to configure my application find the various database and/or configuration files it needs? Recommemded by whom? A lot depends on the OS. Apple for example have one set of recommendations for MacOS, Windows has another and Linux has several to choose from! Thank you good point. Planning on using a ConfigParser based .ini file located in the ./config folder. For instance my folder layout: /path_to_app/app.py /path_to_app/lib/ /path_to_app/database/ /path_to_app/config/ /path_to_app/photos and so on. I would like to make an .ini in the config folder Seems fair enough, however on a Unix system you should also consider allowing the user to have their own personalised version in their home directory. Thus at startup you get the current user ID / home directory and look for a suitable config file. If it exists read it, if not read the default one in your config directory. 1) How does my main app file find the config file in the first place? Generally use a relative path so normally your app will run from its home folder so you can look in ./config. You might also set a system environment variable - for example the CLASSPATH or PYTHONPATH variables, or the ORACLE_HOME used by Oracle for their database. If the environment var is not set then look in ./config Assuming the application could be invoked in odd ways that may alter the notion of the current working directory, how do I unambiguously find the absolute path to the current python source file? (So I can load the nearby .ini) As a follow-up question, how do give my modules stored under ./lib access to the data in my ConfigParser object? (For instance, database connection string, storage path, etc.) I guess a global ConfigParser object would work, but that seems wrong. Regardless of the OS, you need to consider whether more than one user is going to share the code and constant data, but have independent versions of the variable data. If so, then putting the .ini file in the code tree makes no sense. You also have to decide whether there is going to be an install process, or whether the program files will be simply unzipped or copied to a single target directory. And if there is an install, the library you use could very likely influence what you wind up doing. But for raw answers:you can tell where a running python module is located, simply by using the __file__ attribute. You can then parse this with os.path.dirname() to get the directory the file is located in. Then you can use ..\config to get to the config directory. You're right; don't use current directory to find any of this. Use it to find user data, usually, if the command is entered interactively. And don't use it at all, for things started by Explorer (in Windows). I would expect to have two directory structures, one fixed, and one variable. The fixed one is stuff that doesn't get modified once install (or copy) is complete. And the variable one is derived in some way from the user's login. On Windows, that could be a registry entry (HKCU), or it could be the HOMEPATH ( c:\Documents and Settings\username, on XP ). Once you've calculated this initial variable directory, create an INI file that says where all the rest of the variable data is. I claim that this would be an absolute path, set up the first time the user runs File->Configuration. The default would be relative to the same directory, but it should be changeable. I try to set up my personal systems with two main partitions. One has (mostly) constant data and programs, modified each install. It also has junk, like temp files, the swapfile, etc. All user data is on a different partition, with very different backup rules. Windows puts my HOMEPATH on the C: drive, but I avoid programs that force me to leave any quantity of information there. As for how various library modules should find data gathered in your main script, you just pass an instance of the object containing that data. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor