Generating boiler-plate code

In programming I dislike creating boiler plate code. While I understand why it is needed, I feel it is error-prone and manually intensive.

I feel this especially when working on SQLAlchemy’s ORM to create mapping tables. In general with SQLAlchemy one does need to create a mapping table to encapsulate properties of a table in the database.
It can be easily done with the autoload functionality as below:

Base = declarative_base()
engine = create_engine('put your database connect string here')
metadata = MetaData(bind=engine)

#Reflect each database table we need to use, using metadata
class Tests(Base):
    __table__ = Table('Tests', metadata, autoload=True)

Which is similar to using pandas to query data. You let pandas figure out stuff for you.

But, autoload is no panacea. I have encountered difficulties with autoload, especially when querying from a database that is complex or has bad data definitions (e.g. non-unique primary keys).

Creating your own boiler-plate mapping table is not for nothing. Among others, it avoids all these data issues and gives one better control of the data being queried. However, this brings back to the problem of creating boiler-plate code.

Classic Chicken and egg problem!

Therefore, I created the following script https://github.com/bhatt-keshav/niftyPy/blob/master/orm_mapping.py to automate creating boiler-plate code for ORM.
Ready to run, copy and use 🙂
See demo below:

The script has just one function 😉 and is well documented.
Please feel free to modify for your own purposes and do share feedback 🙂