Re: How to get outer class name from an inner class?

2012-05-09 Thread alex23
On May 9, 6:05 am, John Gordon gor...@panix.com wrote:
 I'd like to group the classes underneath a parent class, like so:

 class Question(ApplicationException):

     class TooShort(ApplicationException):
         pass

     class TooLong(ApplicationException):
         pass

 This will make it easier in the future for organizing lots of sub-errors.

 My problem is this: the get_message() method in the base class only knows
 the current class name, i.e. TooShort or TooLong.  But that's not
 enough; I also need to know the outer class name, i.e. Question.TooShort
 or Question.TooLong.  How do I get the outer class name?

This might do the trick:

  import inspect

  def exception_members(scope):
  classes = (m[1] for m in inspect.getmembers(scope,
inspect.isclass))
  return set(
  c for c in classes if Exception in c.__mro__
  )

  class ApplicationException(Exception):
  @property
  def outer_scope(self):
  for _class in
exception_members(inspect.getmodule(self.__class__)):
  if self.__class__ in exception_members(_class):
  return _class

  def get_message(self):
  scope = self.outer_scope
  class_name = scope.__name__ + '.' if scope else ''
  class_name += self.__class__.__name__
  return class_name

When get_message is run, it looks in the module where the exception
was defined for any new classes derived from Exception, then looks at
the members of each of those to see if it matches the current object's
class.

Not really well tested, so beware :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get outer class name from an inner class?

2012-05-09 Thread Ulrich Eckhardt
Am 08.05.2012 22:05, schrieb John Gordon:
[...]
 class QuestionTooShortError(ApplicationException):
 User entered a security question which is too short.
 pass
 
 class QuestionTooLongError(ApplicationException):
 User entered a security question which is too long.
 pass
 
 This scheme works, but I'd like to make it more streamlined.  Specifically,
 I'd like to group the classes underneath a parent class, like so:
 
 class Question(ApplicationException):
 
 class TooShort(ApplicationException):
 pass
 
 class TooLong(ApplicationException):
 pass
 
 This will make it easier in the future for organizing lots of sub-errors.

What is it that this parent class represents? What is the relation
between class Question and class TooShort? In general terms, it isn't
even a parent class but just an outer class, a parent class implies that
child classes inherit from it.

I think that you're going about this the wrong way, and that a module
represents much better what you are trying to express here. Your code
actually looks a bit like it was written with a strong Java or C++
background, could that be the case?


 My problem is this: the get_message() method in the base class only knows
 the current class name, i.e. TooShort or TooLong.  But that's not
 enough; I also need to know the outer class name, i.e. Question.TooShort
 or Question.TooLong.  How do I get the outer class name?

# in module Question
class _Exception(ApplicationException):
def get_message(self):
return self._lookup_message(Question. +
self.__class__.__name__)
class TooLong(_Exception):
pass


You might even be able to look up the module name instead of hard-coding
it in one place.

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


Re: How to get outer class name from an inner class?

2012-05-09 Thread Adam Skutt
On May 8, 4:05 pm, John Gordon gor...@panix.com wrote:
 I'm trying to come up with a scheme for organizing exceptions in
 my application.

 Currently, I'm using a base class which knows how to look up the text
 of a specific error in a database table, keyed on the error class name.

 The base class looks like this:

 class ApplicationException(Exception):
     Base class for application-specific errors.

     def get_message(self):
         Return the error message associated with this class name.

         class_name = self.__class__.__name__
         return UserMessage.objects.get(key=class_name).text

 And then I define a bunch of subclasses which all have different names:

 class QuestionTooShortError(NetIDAppsError):
     User entered a security question which is too short.
     pass

 class QuestionTooLongError(NetIDAppsError):
     User entered a security question which is too long.
     pass

 This scheme works, but I'd like to make it more streamlined.  Specifically,
 I'd like to group the classes underneath a parent class, like so:

 class Question(ApplicationException):

     class TooShort(ApplicationException):
         pass

     class TooLong(ApplicationException):
         pass

 This will make it easier in the future for organizing lots of sub-errors.

It's no more or less organized than using a module, so use a module.
This is why they exist, after all.

That being said, this seems like a bad idea to me: this is a lot of
code and types just for a message lookup!  Exception types should
usually be created based on what you expect users to catch, not based
on what you could throw.  If all of these exceptions will be handled
in the same way, then they shouldn't be distinct types.

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


Re: How to get outer class name from an inner class?

2012-05-09 Thread Jean-Michel Pichavant

John Gordon wrote:

I'm trying to come up with a scheme for organizing exceptions in
my application.

Currently, I'm using a base class which knows how to look up the text
of a specific error in a database table, keyed on the error class name.

The base class looks like this:

class ApplicationException(Exception):
Base class for application-specific errors.

def get_message(self):
Return the error message associated with this class name.

class_name = self.__class__.__name__
return UserMessage.objects.get(key=class_name).text

And then I define a bunch of subclasses which all have different names:

class QuestionTooShortError(NetIDAppsError):
User entered a security question which is too short.
pass

class QuestionTooLongError(NetIDAppsError):
User entered a security question which is too long.
pass

This scheme works, but I'd like to make it more streamlined.  Specifically,
I'd like to group the classes underneath a parent class, like so:

class Question(ApplicationException):

class TooShort(ApplicationException):
pass

class TooLong(ApplicationException):
pass

This will make it easier in the future for organizing lots of sub-errors.

My problem is this: the get_message() method in the base class only knows
the current class name, i.e. TooShort or TooLong.  But that's not
enough; I also need to know the outer class name, i.e. Question.TooShort
or Question.TooLong.  How do I get the outer class name?

Thanks,

  


You're going way too much into details regarding your exceptions.
Basically, you need to create an exception class is at some point you 
need different handlers.

For instance,

try:
   whatever()
except Question.TooShort:
   dosomething()
except Question.TooLong:
   dosomethingelse()

But I'm not sure you really need this.

Try to keep it simple.

You fetch you exception message from a database, is that really required 
? Why can't you just write it in your code ?
Another problem is getting the database key from the class name, this 
makes difficult changing any class name, I'm not sure this is a good idea.


Anyway I'm not sure I'm helping here, providing more questions than answer.

What you could do :


class AppException(Exception): pass

class Question(AppException): pass
class TooShort(Question): pass
class TooLong(Question): pass

def getName(cls):
   if not hasattr(cls, '__base__'):
   raise ValueError('Not a new class style')   
   if cls is AppException:

   return cls.__name__
   return getName(cls.__base__)+'.' + cls.__name__

 getName(TooShort)
 'AppException.Question.TooShort'

This implies you're using only inheritance, not using class as 
ccontainer/namespace.


JM


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


How to get outer class name from an inner class?

2012-05-08 Thread John Gordon
I'm trying to come up with a scheme for organizing exceptions in
my application.

Currently, I'm using a base class which knows how to look up the text
of a specific error in a database table, keyed on the error class name.

The base class looks like this:

class ApplicationException(Exception):
Base class for application-specific errors.

def get_message(self):
Return the error message associated with this class name.

class_name = self.__class__.__name__
return UserMessage.objects.get(key=class_name).text

And then I define a bunch of subclasses which all have different names:

class QuestionTooShortError(NetIDAppsError):
User entered a security question which is too short.
pass

class QuestionTooLongError(NetIDAppsError):
User entered a security question which is too long.
pass

This scheme works, but I'd like to make it more streamlined.  Specifically,
I'd like to group the classes underneath a parent class, like so:

class Question(ApplicationException):

class TooShort(ApplicationException):
pass

class TooLong(ApplicationException):
pass

This will make it easier in the future for organizing lots of sub-errors.

My problem is this: the get_message() method in the base class only knows
the current class name, i.e. TooShort or TooLong.  But that's not
enough; I also need to know the outer class name, i.e. Question.TooShort
or Question.TooLong.  How do I get the outer class name?

Thanks,

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, The Gashlycrumb Tinies

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


Re: How to get outer class name from an inner class?

2012-05-08 Thread John Gordon
In jobua0$m3f$1...@reader1.panix.com John Gordon gor...@panix.com writes:

 class QuestionTooShortError(NetIDAppsError):
 User entered a security question which is too short.
 pass

 class QuestionTooLongError(NetIDAppsError):
 User entered a security question which is too long.
 pass

Oops!  These classes inherit from ApplicationException, not NetIDAppsError.

-- 
John Gordon   A is for Amy, who fell down the stairs
gor...@panix.com  B is for Basil, assaulted by bears
-- Edward Gorey, The Gashlycrumb Tinies

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


Re: How to get outer class name from an inner class?

2012-05-08 Thread Chris Rebert
On Tue, May 8, 2012 at 1:05 PM, John Gordon gor...@panix.com wrote:
 I'm trying to come up with a scheme for organizing exceptions in
 my application.

 Currently, I'm using a base class which knows how to look up the text
 of a specific error in a database table, keyed on the error class name.

 The base class looks like this:

 class ApplicationException(Exception):
    Base class for application-specific errors.

    def get_message(self):
        Return the error message associated with this class name.

        class_name = self.__class__.__name__
        return UserMessage.objects.get(key=class_name).text

 And then I define a bunch of subclasses which all have different names:

 class QuestionTooShortError(NetIDAppsError):
    User entered a security question which is too short.
    pass

 class QuestionTooLongError(NetIDAppsError):
    User entered a security question which is too long.
    pass

 This scheme works, but I'd like to make it more streamlined.  Specifically,
 I'd like to group the classes underneath a parent class, like so:

 class Question(ApplicationException):

    class TooShort(ApplicationException):
        pass

    class TooLong(ApplicationException):
        pass

 This will make it easier in the future for organizing lots of sub-errors.

You could use modules instead…
Nested classes are stylistically questionable.

 My problem is this: the get_message() method in the base class only knows
 the current class name, i.e. TooShort or TooLong.  But that's not
 enough; I also need to know the outer class name, i.e. Question.TooShort
 or Question.TooLong.  How do I get the outer class name?

Use __qualname__ and chop off the last dotted part (i.e. the innermost
class' name, thus yielding its parent's name).
PEP 3155 -- Qualified name for classes and functions:
http://www.python.org/dev/peps/pep-3155/

Cheers,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get outer class name from an inner class?

2012-05-08 Thread Terry Reedy

On 5/8/2012 4:05 PM, John Gordon wrote:

I'm trying to come up with a scheme for organizing exceptions in
my application.

Currently, I'm using a base class which knows how to look up the text
of a specific error in a database table, keyed on the error class name.

The base class looks like this:

class ApplicationException(Exception):
 Base class for application-specific errors.

 def get_message(self):
 Return the error message associated with this class name.

 class_name = self.__class__.__name__
 return UserMessage.objects.get(key=class_name).text

And then I define a bunch of subclasses which all have different names:

class QuestionTooShortError(NetIDAppsError):
 User entered a security question which is too short.
 pass

class QuestionTooLongError(NetIDAppsError):
 User entered a security question which is too long.
 pass

This scheme works, but I'd like to make it more streamlined.  Specifically,
I'd like to group the classes underneath a parent class, like so:

class Question(ApplicationException):

 class TooShort(ApplicationException):
 pass

 class TooLong(ApplicationException):
 pass

This will make it easier in the future for organizing lots of sub-errors.


I think maybe you are being much too fine-grained in your exception 
hierarchy. Python has, for instance, just one ValueError, with details 
in the error. The details can include specific values only known at the 
point of the error. You are putting some specifics in the exception name 
and then (apparently) given them somewhat redundant canned messages 
lacking situation-specific details.


For instance
errfmt = Security question length {:d} not in legal range {:d} - {:d}
if not (secquemin = len(secque) = secquemax):
  raise ValueError(errfmt.format(len(secque), secquemin, secquemax))
# or ApplicationError or AppValueError if you want.

The messages in Python are not this good, but they slowly improve as 
people raises an issue on the tracker and others implement and apply fixex.


--
Terry Jan Reedy

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


Re: How to get outer class name from an inner class?

2012-05-08 Thread Steve Howell
On May 8, 1:05 pm, John Gordon gor...@panix.com wrote:
 I'm trying to come up with a scheme for organizing exceptions in
 my application.

 Currently, I'm using a base class which knows how to look up the text
 of a specific error in a database table, keyed on the error class name.

 The base class looks like this:

 class ApplicationException(Exception):
     Base class for application-specific errors.

     def get_message(self):
         Return the error message associated with this class name.

         class_name = self.__class__.__name__
         return UserMessage.objects.get(key=class_name).text

 And then I define a bunch of subclasses which all have different names:

 class QuestionTooShortError(NetIDAppsError):
     User entered a security question which is too short.
     pass

 class QuestionTooLongError(NetIDAppsError):
     User entered a security question which is too long.
     pass

 This scheme works, but I'd like to make it more streamlined.  Specifically,
 I'd like to group the classes underneath a parent class, like so:

 class Question(ApplicationException):

     class TooShort(ApplicationException):
         pass

     class TooLong(ApplicationException):
         pass

 This will make it easier in the future for organizing lots of sub-errors.

 My problem is this: the get_message() method in the base class only knows
 the current class name, i.e. TooShort or TooLong.  But that's not
 enough; I also need to know the outer class name, i.e. Question.TooShort
 or Question.TooLong.  How do I get the outer class name?

This may be somewhat relevant to you, although it doesn't specifically
answer your question for pre-3.3:

http://www.python.org/dev/peps/pep-3155/ (Qualified name for classes
and functions)
-- 
http://mail.python.org/mailman/listinfo/python-list