Validation of parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kless

    #1

    Validation of parameters

    Which is the best mode for validation of parameters? I'm using the
    next way but it is not well testable.

    -----------------------------
    if not self._foo:
    try:
    raise ValueError('Val ueError: foo not found')
    except ValueError, err:
    print (err.message)
    print("Valid values: %s\n" % self.valid_foo)
    sys.exit(1)

    ....
    ....
    -----------------------------
  • Paddy

    #2
    Re: Validation of parameters

    On Aug 10, 9:13 am, Kless <jonas....@goog lemail.comwrote :
    Which is the best mode for validation of parameters? I'm using the
    next way but it is not well testable.
    >
    -----------------------------
    if not self._foo:
        try:
            raise ValueError('Val ueError: foo not found')
        except ValueError, err:
            print (err.message)
            print("Valid values: %s\n" % self.valid_foo)
            sys.exit(1)
    >
    ...
    ...
    -----------------------------
    If you just want the absense of _foo to raise an exception then why to
    just reference self._foo and leave the hard work to ptyhon? Something
    like:

    self._foo

    If it is present then fine, if it is not then an exception is raised.

    Here it is in the Python shell:
    >>class tmp(object):
    def __init__(self):
    self._foo

    >>dummy = tmp()
    Traceback (most recent call last):
    File "<pyshell#4 >", line 1, in <module>
    dummy = tmp()
    File "<pyshell#3 >", line 3, in __init__
    self._foo
    AttributeError: 'tmp' object has no attribute '_foo'
    >>>
    - Paddy.

    Comment

    • Kless

      #3
      Re: Validation of parameters

      On 10 ago, 15:03, Paddy <paddy3...@goog lemail.comwrote :
      If you just want the absense of _foo to raise an exception then why to
      just reference self._foo and leave the hard work to ptyhon? Something
      like:
      >
        self._foo
      >
      If it is present then fine, if it is not then an exception is raised.
      It only was an example but I need show a better message. You can see
      code here [1]:


      [1] http://github.com/kless/cryha/tree/m...hasher.py#L141

      Comment

      • Terry Reedy

        #4
        Re: Validation of parameters



        Kless wrote:
        On 10 ago, 15:03, Paddy <paddy3...@goog lemail.comwrote :
        >If you just want the absense of _foo to raise an exception then why to
        >just reference self._foo and leave the hard work to ptyhon? Something
        >like:
        >>
        > self._foo
        >>
        >If it is present then fine, if it is not then an exception is raised.
        It only was an example but I need show a better message.
        The usual idiom is
        try: self._foo
        except AttrError as m: #3.0
        <better message>

        Comment

        Working...