Best way to do data source abstraction

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

    #1

    Best way to do data source abstraction

    What is the best way to do data source abtraction? For example have
    different classes with the same interface, but different
    implementations .

    I was thinking of almost having classA as my main class, and have
    classA dynamically "absorb" classFood into to based on the extension
    of the input file received by classA. But this doesn't seem possible.

    Please advise.

    Thank you.

    --
    To be updated...
  • Sybren Stuvel

    #2
    Re: Best way to do data source abstraction

    Arthur Pemberton enlightened us with:[color=blue]
    > What is the best way to do data source abtraction?[/color]

    That depends on your data source. For files, file-like objects are an
    abstraction. For databases there is PEP 249.
    [color=blue]
    > I was thinking of almost having classA as my main class, and have
    > classA dynamically "absorb" classFood into to based on the extension
    > of the input file received by classA. But this doesn't seem
    > possible.[/color]

    You don't explain the most important part - "absorb". What does that
    mean? And what does it mean to have classA "almost" as your main
    class?

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa

    Comment

    • bruno at modulix

      #3
      Re: Best way to do data source abstraction

      Arthur Pemberton wrote:[color=blue]
      > What is the best way to do data source abtraction? For example have
      > different classes with the same interface, but different
      > implementations .
      >
      > I was thinking of almost having classA as my main class, and have
      > classA dynamically "absorb" classFood into to based on the extension
      > of the input file received by classA. But this doesn't seem possible.[/color]

      Could you explain more accurately what you're trying to do ? FWIW, it
      seems that a plain old factory function would do ?

      class DatasourceAbstr action(object):
      """ base class, factoring common stuff """
      # implementation here

      class JpegFile(Dataso urceAbstraction ):
      # ....


      class PdfFile(Datasou rceAbstraction) :
      # ....

      class TxtFile(Datasou rceAbstraction) :
      # ....

      # etc
      _classes = {
      'jpg' : JpegFile,
      'txt' : TxtFile,
      'pdf' : PdfFile,
      # etc..
      }

      def Datasource(inpu tfile):
      ext = os.path.splitex t(inputfile)
      return _classes.get(ex t, <SomeDefaultCla ssHere>)(inputf ile)

      The fact that there's no 'new' keyword in Python, and that classes are
      callable objects acting as factories means that it's a no-brainer to use
      a plain function (eventually disguised as a Class - the client code just
      doesn't care !-) as factory...

      Now if I missed the point, please give more explanations...

      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • Larry Bates

        #4
        Re: Best way to do data source abstraction

        Arthur Pemberton wrote:[color=blue]
        > What is the best way to do data source abtraction? For example have
        > different classes with the same interface, but different
        > implementations .
        >
        > I was thinking of almost having classA as my main class, and have
        > classA dynamically "absorb" classFood into to based on the extension
        > of the input file received by classA. But this doesn't seem possible.
        >
        > Please advise.
        >
        > Thank you.
        >[/color]

        The best method I've found is to have a class that abstracts
        the data and presents the values from the data source via
        class attributes. If you also implement it as an iterator
        (e.g. give it __iter__ and __next__ methods), you can easily
        iterate over the resultset from each data source. With this
        method I've abstracted data in CSV files, fixed ASCII files,
        SQL tables, Excel Spreadsheets, tab delimited files that are
        members of a .ZIP archive, you name it.

        Short example (not tested):
        class foo:
        '''
        Class to abstract tab delimited files
        '''
        def __init__(self, filepath, columnnames):
        self.fp=open(fi lepath, 'r')
        self.columnname s=columnnames
        return

        def __iter__(self):
        return self

        def next(self):
        #
        # Try to get the next line from file
        #
        try: line=self.fp.ne xt()
        except StopIteration:
        self.fp.close()
        raise

        #
        # Decode the tab delimited line into its parts
        #
        line=line.rstri p()
        if not line: raise StopIteration
        values=line.spl it('\t')
        print "values=", values
        l=zip(self.colu mnnames, values)
        print l
        for column, value in l:
        setattr(self, column, value)

        return

        if __name__ == "__main__":
        obj=foo('abc.tx t', ['name', 'address1', 'address2', 'city', 'state', 'zip'])

        for entry in obj:
        print ""
        print "Name...... ..", obj.name
        print "Address1.. ..", obj.address1
        print "Address2.. ..", obj.address2
        print "City...... ..", obj.city
        print "State..... ..", obj.state
        print "Zip....... ..", obj.zip


        -Larry Bates

        Comment

        • Larry Bates

          #5
          Re: Best way to do data source abstraction

          Arthur Pemberton wrote:[color=blue]
          > What is the best way to do data source abtraction? For example have
          > different classes with the same interface, but different
          > implementations .
          >
          > I was thinking of almost having classA as my main class, and have
          > classA dynamically "absorb" classFood into to based on the extension
          > of the input file received by classA. But this doesn't seem possible.
          >
          > Please advise.
          >
          > Thank you.
          >[/color]

          The best method I've found is to have a class that abstracts
          the data and presents the values from the data source via
          class attributes. If you also implement it as an iterator
          (e.g. give it __iter__ and __next__ methods), you can easily
          iterate over the resultset from each data source. With this
          method I've abstracted data in CSV files, fixed ASCII files,
          SQL tables, Excel Spreadsheets, tab delimited files that are
          members of a .ZIP archive, you name it.

          Short example (not tested):
          class foo:
          '''
          Class to abstract tab delimited files
          '''
          def __init__(self, filepath, columnnames):
          self.fp=open(fi lepath, 'r')
          self.columnname s=columnnames
          return

          def __iter__(self):
          return self

          def next(self):
          #
          # Try to get the next line from file
          #
          try: line=self.fp.ne xt()
          except StopIteration:
          self.fp.close()
          raise

          #
          # Decode the tab delimited line into its parts
          #
          line=line.rstri p()
          if not line: raise StopIteration
          values=line.spl it('\t')
          print "values=", values
          l=zip(self.colu mnnames, values)
          print l
          for column, value in l:
          setattr(self, column, value)

          return

          if __name__ == "__main__":
          obj=foo('abc.tx t', ['name', 'address1', 'address2', 'city', 'state', 'zip'])

          for entry in obj:
          print ""
          print "Name...... ..", obj.name
          print "Address1.. ..", obj.address1
          print "Address2.. ..", obj.address2
          print "City...... ..", obj.city
          print "State..... ..", obj.state
          print "Zip....... ..", obj.zip


          -Larry Bates

          Comment

          Working...