How can I obtain the exception object on a generlized except statement?

2007-06-10 Thread Chris Allen
I am confused on one aspect of exception handling.  If you specify the
exception object type to match in an except statement it is possible
to also obtain the exception object itself, but I can't figure out how
to get the exception object when I don't specify a match.

for example:

>>> try: urlopen('http://www.google.com')
>>> except socket.error, msg:
>>> print str(msg)

this works and I can do what I like with the exception object (msg).
but I can't do this with a simple except statment.

>>> except msg:

this won't work because it will think msg is the type to match

>>> except ,msg:

syntax error

>>> except *,msg:

syntax error

>>> except (),msg:

Hmm I didn't try that one before I started this post.  It doesn't give
me a syntax error.  I'll experiment.
Even if the above syntax works, is this the way to do this?  It seems
sort of funky.

How do I do this?  Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How can I obtain the exception object on a generlized except statement?

2007-06-10 Thread Chris Allen
Just what I was looking for thanks Diez and John.

-- 
http://mail.python.org/mailman/listinfo/python-list


Global package variable, is it possible?

2007-08-03 Thread Chris Allen
Hello fellow pythoneers.  I'm stumped on something, and I was hoping
maybe someone in here would have an elegant solution to my problem.
This is the first time I've played around with packages, so I'm
probably misunderstanding something here...

Here's what I'd like to do in my package.  I want my package to use a
configuration file, and what I'd like is for the config file to appear
magically in each module so I can just grab values from it without
having to load and parse the config file in each package module.  Not
quite understanding how the __init__.py file works, I expected it to
be as easy as just setting up the ConfigParser object and then I
figured (since a package is a module) that it would now be global to
my package and I could access it in my modules, but I was wrong...  I
don't want to have to pass it in to each module upon init, or anything
lame like that.  A main reason for this is that I'd like to be able to
reload the config file dynamically and have all my modules
automatically receive the new config.  There must be a way to do this,
but seeing how __init__.py's namespace is not available within the
package modules, I don't see a simple and elegant way to do this.
Does anybody have any suggestions?  Thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global package variable, is it possible?

2007-08-03 Thread Chris Allen
> Hmm. So maybe something like this makes sense:
>
> __init__py:
> ###
> _default_cfg_file = 'config.conf'
>
> import configure
> def loadcfg(filename):
> configure.cfgfile = filename
> try:
> reload(pkg_module1)
> reload(pkg_module2)
> except NameError:
> pass
> cfg = loadcfg(_default_cfg_file)
>
> import pkg_module1
> import pkg_module2
> # EOF
>
> confgure.py:
> ###
> cfgfile = None
> def loadcfg()
> global cfgfile
> if not cfgfile:
> return None
> file = open(cfgfile)
> cfg = SafeConfigParser()
> cfg.readfp(file)
> return cfg
> # EOF
>
> pkg_module1:
> ###
> import configure
> cfg = configure.loadcfg()
> # EOF
>
> It's a little bit convoluted, but I think it solves most of my
> gripes.  Anybody have a better idea of how to do this?  Thanks again
> Fabio.

Ugh... I wasn't thinking... Of course this won't work either for the
same reasons above.  changing configure.cfgfile from __init__.py will
have no effect on the separate configure instances loaded in other
modules.  I still don't understand why python's __init__.py namespace
isn't global to all modules in the package.  Is this a feature, to
prevent sloppy code?  I think there are certain instances when it
would make sense.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global package variable, is it possible?

2007-08-03 Thread Chris Allen
On Aug 3, 11:16 am, Carsten Haese <[EMAIL PROTECTED]> wrote:
> On Fri, 2007-08-03 at 17:51 +, Fabio Z Tessitore wrote:
> > Heve you tried to do something like:
>
> > # module configure.py
> > value1 = 10
> > value2 = 20
> > ...
>
> > # other module
> > from configure import *
>
> > # now I'm able to use value1 value2 etc.
> > var = value1 * value2
>
> Right idea, wrong execution. Note that the OP said "I'd like to be able
> to reload the config file dynamically and have all my modules
> automatically receive the new config."
>
> "from configure import *" will import the settings into the current
> namespace, and subsequent changes in the original namespace will, in
> general, not have any effect in the current namespace.
>
> This should do the trick:
>
> # module configure.py
> value1 = 10
> value2 = 20
> ...
>
> # other module
> import configure
>
> var = configure.value1 * configure.value2
>
> HTH,
>
> --
> Carsten Haesehttp://informixdb.sourceforge.net

It does help thanks.  Okay now after reading your post I really need
to do some experimenting.  I was under the impression when I wrote my
last post that changing an attribute in one modules instance in
__init__.py would not effect other modules.  Sorry, I'm relatively new
to python, and things like this are still sometimes "gotchas!" for me.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global package variable, is it possible?

2007-08-03 Thread Chris Allen
On Aug 3, 10:51 am, Fabio Z Tessitore <[EMAIL PROTECTED]>
wrote:
> Heve you tried to do something like:
>
> # module configure.py
> value1 = 10
> value2 = 20
> ...
>
> # other module
> from configure import *
>
> # now I'm able to use value1 value2 etc.
> var = value1 * value2
>
> bye


Thanks for the response Fabio.  I thought about this, but I don't
think this will work well in my situation either.  Take a look at the
following:

__init__py:
###
_default_cfgfile = 'config.conf'
from configure import *
cfg = loadcfg(_default_cfgfile)

import pkg_module1
import pkg_module2
# EOF


configure.py:

from ConfigParser import SafeConfigParser

def loadcfg(filename):
file = open(filename)
cfg = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:

_default_cfgfile = 'config.conf'
from configure import *
cfg = loadcfg(_default_cfgfile)
# EOF


One problem I see with this approach is that we must define the
configuration file in every module.  Alternatively a better approach
would be to only define the configuration file within configure.py,
however this also seems less than ideal.  I don't like it because I
believe something as important as the location of the package
configuration file, used by all modules, should defined in __init__
not tucked away in one of it's modules.

Another problem is that after the package is loaded and we want to
load in a new config file, what will happen?  With this approach we'll
have to reload every module the package uses for them to read the new
config.  And even then, how do we tell each module what the new config
file is?  If I did define the configuration file in configure.py then
I suppose what I could do is set a cfgfile variable in configure.py to
the new file location and reload all the modules.  If there is no
global package variables, then maybe this isn't so bad...

Hmm. So maybe something like this makes sense:

__init__py:
###
_default_cfg_file = 'config.conf'

import configure
def loadcfg(filename):
configure.cfgfile = filename
try:
reload(pkg_module1)
reload(pkg_module2)
except NameError:
pass
cfg = loadcfg(_default_cfg_file)

import pkg_module1
import pkg_module2
# EOF

confgure.py:
###
cfgfile = None
def loadcfg()
global cfgfile
if not cfgfile:
return None
file = open(cfgfile)
cfg = SafeConfigParser()
cfg.readfp(file)
return cfg
# EOF

pkg_module1:
###
import configure
cfg = configure.loadcfg()
# EOF

It's a little bit convoluted, but I think it solves most of my
gripes.  Anybody have a better idea of how to do this?  Thanks again
Fabio.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global package variable, is it possible?

2007-08-03 Thread Chris Allen
> Only for knowing more about modules: is there a way to dinamically reload
> an already imported module?
>
> bye
> Fabio

Yeah you can reload modules with the reload builtin function.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Global package variable, is it possible?

2007-08-06 Thread Chris Allen
On Aug 6, 12:41 am, Bruno Desthuilliers  wrote:
> Chris Allen a écrit :
>
>
>
> > Hello fellow pythoneers.  I'm stumped on something, and I was hoping
> > maybe someone in here would have an elegant solution to my problem.
> > This is the first time I've played around with packages, so I'm
> > probably misunderstanding something here...
>
> > Here's what I'd like to do in my package.  I want my package to use a
> > configuration file, and what I'd like is for the config file to appear
> > magically in each module so I can just grab values from it without
> > having to load and parse the config file in each package module.  Not
> > quite understanding how the __init__.py file works, I expected it to
> > be as easy as just setting up the ConfigParser object and then I
> > figured (since a package is a module) that it would now be global to
> > my package and I could access it in my modules, but I was wrong...  I
> > don't want to have to pass it in to each module upon init, or anything
> > lame like that.  A main reason for this is that I'd like to be able to
> > reload the config file dynamically and have all my modules
> > automatically receive the new config.  There must be a way to do this,
> > but seeing how __init__.py's namespace is not available within the
> > package modules, I don't see a simple and elegant way to do this.
> > Does anybody have any suggestions?  Thanks!
>
> Hi Chris...
> I've read all the thread, and it seems that your problem is mostly to
> share a single dynamic state (the config) between several modules. So I
> do wonder: have you considered the use of the Singleton pattern (or one
> of it's variants...) ?

Thanks, I don't know anything about python singletons.  But I'll look
it up.

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Global package variable, is it possible?

2007-08-06 Thread Chris Allen
On Aug 6, 2:27 am, Ben Finney <[EMAIL PROTECTED]>
wrote:
> Bruno Desthuilliers <[EMAIL PROTECTED]> writes:
> > I've read all the thread, and it seems that your problem is mostly
> > to share a single dynamic state (the config) between several
> > modules. So I do wonder: have you considered the use of the
> > Singleton pattern (or one of it's variants...) ?
>
> Python modules are effectively singletons. So the idiomatic way to do
> this is to create a module for configuration (perhaps named 'config'),
> import that into every other module that needs it, and use its
> attributes.
>
> --
>  \   "[On the Internet,] power and control will shift to those who |
>   `\   are actually contributing something useful rather than just |
> _o__) having lunch."  -- Douglas Adams |
> Ben Finney

Yes, that's what I ended up doing.  Which is creating a configure
module and then importing it into my modules that use it.  I did
something very similar to the code I posted above.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about a web server

2007-08-20 Thread Chris Allen
On Aug 20, 12:02 am, Frank Millman <[EMAIL PROTECTED]> wrote:
> Hi all
>
> I have just started to dabble in writing my own web server.
>
> I googled for 'python web server', and this is the first hit -
>
>http://fragments.turtlemeat.com/pythonwebserver.php
>
> It has the source code for a simple web server, based on HTTPServer
> and BaseHTTPRequestHandler.
>
> It demonstrates the concepts of
>   - returning a static page
>   - constructing and returning a dynamic page
>   - sending a form with a POST method, and responding to the result
>
> I typed it in and ran it, and it 'just worked', with one exception.
>
> When responding to the initial request, it sends a 200 response,
> followed by a content-type header, followed by an html page. This
> works fine.
>
> When responding to the POST data received, it sends a 301 response, no
> headers, and then the html page.
>
> This works with a Firefox browser on Linux, but MSW IE6 displays 'the
> page cannot be displayed'.
>
> According to the notes, "You don't have to know much about the HTTP
> protocol at all. Except some basic that when the client request
> something it is a "GET", and when the client sends something it is in
> our case a POST. Some basic responce codes like 200 is OK for GET, and
> 404 is file not found, 301 is OK for a Post."
>
> I googled for 'http response code 301', and found that it is actually
> a redirection code. It seems that the notes are misleading.
>
> So I guess my questions are -
>   1. what is the correct response for a POST?
>  I think the answer is 200.
>  However, I assume the author is trying to demonstrate a
> particular technique.
>  Can anyone explain what that may be.
>   2. why does the above work with Firefox?
>
> TIA for any enlightenment.
>
> Frank Millman


Yes HTTP response code 200 does indicate a normal response.  What you
really need to look at to build an HTTP server is is the RFC for
HTTP.  It is HTTP 1.1 is RFC 2616 and you can view it here:
http://www.faqs.org/rfcs/rfc2616.html

That should get you on your way...



-- 
http://mail.python.org/mailman/listinfo/python-list


sys.argv is munging my command line options

2007-08-29 Thread Chris Allen
The command line syntax for my program is as follows:

action key=value key=value...

Where action is a required string (ie. 'backup', 'init', 'restore',
etc) and the program can accept one or more key value pairs.  I know
this syntax isn't standard, but I think it works great for my program
as each key can override an identically named field in a configuration
file, that way the user doesn't have to have two different syntaxes to
do the same thing.  I could do this with --key value, but key=value is
cleaner IMHO because it matches the config file syntax.

But I'm running into a problem with this which is that sys.argv splits
my key=value options.  I need to know the option associations, and
there's no way to know this by inspecting sys.argv.  Can I get access
to the command line string as python saw it before it split it into
sys.argv or is there another way?  Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sys.argv is munging my command line options

2007-08-29 Thread Chris Allen
Thanks for the reply. Oops...  I forget that I was calling the program
from a shell script, the shell script was responsible for goofing up
my command line options.  Solved.  Thanks again.


On Aug 29, 12:28 pm, Ant <[EMAIL PROTECTED]> wrote:
> On Aug 29, 8:11 pm, Chris Allen <[EMAIL PROTECTED]> wrote:
> ...
>
> > But I'm running into a problem with this which is that sys.argv splits
> > my key=value options.  I need to know the option associations, and
> > there's no way to know this by inspecting sys.argv.  Can I get access
> > to the command line string as python saw it before it split it into
> > sys.argv or is there another way?  Thanks.
>
> Could you show us some example code that demonstrates this? The
> following works as expected for me on win32:
>
> # test.py
> import sys
>
> for arg in sys.argv[1:]:
> print arg
>
> >From the command prompt:
>
> C:\0>test.py action key=value key=value
> action
> key=value
> key=value
>
> --
> Ant.


-- 
http://mail.python.org/mailman/listinfo/python-list