Class update detection

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Axium Computer Services

    Class update detection

    I have a relational database that has several tables each more or less
    translating to a python class and the fields of each table translating
    into attributes of the python class. I have no problem creating an
    instance of the class, running a query and filling the attributes. Then
    this information will be accessible from a UI. The problem I am running
    into is when the user is done working in the UI I need to determine if
    any of the class attributes have been changed to I can issue an update
    query to the database with the changes. Is there a generally accepted
    method to do this? Is there some way to create a checksum of the class
    attributes?

    Any help would be much appreciated.

    Thanks

    Steven Potter
  • Lars Heuer

    #2
    Re: Class update detection

    Hi Axium,

    [...][color=blue]
    > this information will be accessible from a UI. The problem I am running
    > into is when the user is done working in the UI I need to determine if
    > any of the class attributes have been changed to I can issue an update
    > query to the database with the changes. Is there a generally accepted[/color]
    [...]

    I.e. SQLObject (and other ORMs, too) does this job. If you update a
    class attribute, it automatically updates the underlying DB tables.



    Best regards,
    Lars


    Comment

    • Miki Tebeka

      #3
      Re: Class update detection

      Hello Steven,
      [color=blue]
      > I need to determine if
      > any of the class attributes have been changed to I can issue an update
      > query to the database with the changes. Is there a generally accepted
      > method to do this? Is there some way to create a checksum of the class
      > attributes?[/color]
      Do you mean something in the lines of:
      class C:
      def __init__(self, a, b):
      self.a, self.b = a, b
      self.dirty = 0
      def __setattr__(sel f, k, v):
      if k in ("a", "b"):
      self.dirty = 1
      self.__dict__[k] = v[color=blue][color=green]
      >> c = C(1,2)[color=darkred]
      >>> c.dirty[/color][/color][/color]
      0[color=blue][color=green][color=darkred]
      >>> c.a = 10
      >>> c.a[/color][/color][/color]
      10[color=blue][color=green][color=darkred]
      >>> c.dirty[/color][/color][/color]
      1[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Another option is to generate a checksum (using md5 and str) on all the
      attributes you're interested in at the end of __init__ and a function
      `dirty' will compute this checksum and return 1 if it was changed.
      Note that if you have nested objects this might be a problem.

      HTH.
      Miki

      Comment

      Working...