Design question regarding exceptions.

2007-07-17 Thread asincero
I have a class called Users that provides a higher level of
abstraction to an underlying "users" table in a pgsql database.  It
has methods like "addUser()" and "deleteUser()" which, obviously, wrap
the corresponding SQL statements.  My question is would it better to
let any exceptions thrown by the underlying DB-API calls bubble up
from these methods, or should I catch them inside the methods, wrap
them inside my own custom exceptions, and throw those exceptions
instead?

Thanks.

-- Arcadio

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


Re: Using a switch-like if/else construct versus a dictionary?

2007-06-20 Thread asincero
Ahh .. yes of course, you are right.  I mis-typed.  I like how you
defined the dictionary all in one statement, though.  I didn't think
of doing it that way.

-- Arcadio


On Jun 19, 4:11 pm, heltena <[EMAIL PROTECTED]> wrote:
> asincero ha escrit:
>
>
>
> > def foo():
> >def doCase1():
> >   pass
> >def doCase2():
> >   pass
> >def doCase3():
> >   pass
> >def doCase4():
> >   pass
> >def doCase5():
> >   pass
>
> >handle_case = {}
> >handle_case[1] = doCase1()
> >handle_case[2] = doCase2()
> >handle_case[3] = doCase3()
> >handle_case[4] = doCase4()
> >handle_case[5] = doCase5()
>
> Sorry, but I think this is not correct. Now, you put the result of the
> function call into the dictionary, but you want the function address.
>
> The correct code is:
> handle_case = {}
> handle_case[1] = doCase1
> handle_case[2] = doCase2
> handle_case[3] = doCase3
> handle_case[4] = doCase4
> handle_case[5] = doCase5
>
> Or:
> handle_case = { 1: doCase1, 2: doCase2, 3: doCase3, 4: doCase4, 5:
> doCase 5 }
>
> Or:
>try:
>   { 1: doCase1, 2: doCase2, 3: doCase3, 4: doCase4, 5: doCase 5 }
> [c]()
>except:
>   print "Catch the correct exception"
>
> But this solutions only works fine if the params of the functions are
> the same for all calls (in if/else construct you don't need it).
>
> Bye!
>
> --
> Helio Tejedor


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


Using a switch-like if/else construct versus a dictionary?

2007-06-19 Thread asincero
Which is better: using an if/else construct to simulate a C switch or
use a dictionary?  Example:

def foo():
   if c == 1:
  doCase1()
   elif c == 2:
  doCase2()
   elif c == 3:
  doCase3()
   elif c == 4:
  doCase4()
   elif c == 5:
  doCase5()
   else:
  raise "shouldn't get here"


or:

def foo():
   def doCase1():
  pass
   def doCase2():
  pass
   def doCase3():
  pass
   def doCase4():
  pass
   def doCase5():
  pass

   handle_case = {}
   handle_case[1] = doCase1()
   handle_case[2] = doCase2()
   handle_case[3] = doCase3()
   handle_case[4] = doCase4()
   handle_case[5] = doCase5()
   handle_case[c]()

Note that in this situation using OO polymorphism instead of a switch-
like construct wouldn't be applicable, or at least I can't see how it
could be.

Thanks in advance for any tips!

-- Arcadio

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


Is this a good idea or a waste of time?

2006-08-24 Thread asincero
Would it be considered good form to begin every method or function with
a bunch of asserts checking to see if the parameters are of the correct
type (in addition to seeing if they meet other kinds of precondition
constraints)?  Like:

def foo(a, b, c, d):
   assert type(a) == str
   assert type(b) == str
   assert type(c) == int
   assert type(d) == bool
   # rest of function follows

This is something I miss from working with more stricter languages like
C++, where the compiler will tell you if a parameter is the wrong type.
 If anything, I think it goes a long way towards the code being more
self documenting.  Or is this a waste of time and not really "the
Python way"?

-- Arcadio

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


How to catch these kind of bugs in Python?

2006-08-19 Thread asincero
Is there anyway to catch the following type of bug in Python code:

message = 'This is a message'
# some code
# some more code
if some_obscure_condition:
   nessage = 'Some obscure condition occured.'
# yet more code
# still more code
print message


In the above example, message should be set to 'Some obscure condition
occured.' if some_obscure_condition is True.  But due to a lack of
sleep, and possibly even being drunk, the programmer has mistyped
message.  These types of bugs would easily be caught in languages that
have a specific keyword or syntax for declaring variables before use.
I'm still fairly new to using Python on a more than casual basis, so I
don't know if Python has anyway to help me out here.

-- Arcadio

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