title: Python/SqlAlchemy: Binding attributes (100s) to Base sub-classes sensibly
body: <p>My project involves data-wrangling some government form filings. I have a decade of filings, about a dozen sections per year, in the form of CSV. Each CSV is paired with a file with the field names, types, and sizes. Since the data is originally from a relational DB of some sort, I figured the most sensible way to handle the data is pop it into Sqlite and use SqlAlchemy to query it. </p>
  
  <p>I'm just getting my feet wet with SqlAlchemy and it seems the typical way of creating database objects is to create a class with a Column attribute for each field, as such: </p>
  
  <pre><code>Base = declarative_base()
  class Person(Base):
      __tablename__ = 'person'
      id = Column(Integer, primary_key=True)
      name = Column(String(250), nullable=False)
      ...
  </code></pre>
  
  <p>and then access rows from the table with something like</p>
  
  <pre><code>person = session.query(Person).filter(Person.name == 'bob').all()
  </code></pre>
  
  <p>However, there are over a hundred of these such tables, and some tables have upwards of 150 fields. Yikes, not coding that by hand. I wrote a quick script to generate the .py file from the list of fields, which works, but feels a bit un-pythonic. </p>
  
  <p>It seems like from <a href="http://pythoncentral.io/introductory-tutorial-python-sqlalchemy/" rel="nofollow">some examples</a> I need to pass the <strong>class</strong>, not the <strong>instance</strong>, but I could be missing some nuance here. I tried binding attributes using setattr to a base class, but after some mucking with factory functions and mangling inheritance in various ways, it was clear python's type management was not going to play ball. The only other thing that comes to mind is trying messing around with class instances and deepcopy, but that also seems dodgy. </p>
  
  <p>Is there a nice/elegant way of creating a hundred declarative_base subclasses with a hundred fields from a template, or more importantly, is there a better way in general of interfacing with these tables? </p>