Objects and validation

Discussion in 'Python' started by python@lists.fastmail.net, Dec 12, 2010.

  1. Guest

    I have a routine in Python which is extracting information from a
    website. This information is read and inserted into objects.

    I currently have all the validations and checks implemented in the
    routines which are reading the HTML and creating the objects. It is
    however also possible to move all the validations into the class itself.
    What is considered the best practice for this: validation in the
    functions which read the information and creates the objects or in the
    class itself?

    Thijs
    , Dec 12, 2010
    #1
    1. Advertising

  2. pakalk Guest

    On 12 Gru, 15:28, wrote:
    > I have a routine in Python which is extracting information from a
    > website. This information is read and inserted into objects.
    >
    > I currently have all the validations and checks implemented in the
    > routines which are reading the HTML and creating the objects. It is
    > however also possible to move all the validations into the class itself.
    > What is considered the best practice for this: validation in the
    > functions which read the information and creates the objects or in the
    > class itself?


    setter

    Data sources may differ, and may be used for many reasons, so
    validating data there is not a good idea. Object has to keep correct
    data, so it should validate it. If you want to have object created no
    matter what, you can add validate() method, but you may also use
    setters for this - error will be raised every try to set wrong data.
    pakalk, Dec 12, 2010
    #2
    1. Advertising

  3. Guest

    On 12 déc, 15:28, wrote:
    > I have a routine in Python which is extracting information from a
    > website. This information is read and inserted into objects.
    >
    > I currently have all the validations and checks implemented in the
    > routines which are reading the HTML and creating the objects. It is
    > however also possible to move all the validations into the class itself.
    > What is considered the best practice for this: validation in the
    > functions which read the information and creates the objects or in the
    > class itself?
    >


    There's no one-size-fits-all answer to this question. Part of the work
    really belongs to the layer that accepts data from the outside world,
    part of it belong to the 'model' object itself. In any case, the
    'model' object shouldn't have to worry about data conversion etc -
    like, if one of the attribute is supposed to be a datetime, it's the
    client code's responsability to provide a proper datetime object, not
    a string that may (or not) be the textual representation of a date and
    time.
    , Dec 13, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?dmlkeWE=?=
    Replies:
    1
    Views:
    739
    Kevin Spencer
    Jun 2, 2005
  2. bigbinc
    Replies:
    3
    Views:
    387
    Michael Borgwardt
    Nov 18, 2003
  3. 7stud
    Replies:
    11
    Views:
    667
    Dennis Lee Bieber
    Mar 20, 2007
  4. ghoetker
    Replies:
    1
    Views:
    365
  5. Replies:
    7
    Views:
    372
    David Lamb
    Oct 29, 2012
Loading...

Share This Page