Production Code

There’s a lot of courses on YouTube, Udemy, etc.. that teach you to “code” but when you’ve completed all of the courses and all is said and done, you try out your first little project only to realize that you know NOTHING.

No matter how these courses sell it, it’s one thing to put out a “hello world” app and deploy it to Heroku and something completely different when you have a database of 90 million rows with 100 separate columns that you want to query on and display data to an end user.  This is all before considering that Heroku considers your postgres database as a separate service and you will be charged accordingly.  I’ve learned all of the basics.  CSS, HTML, Python, Flask, Postgresql through Udemy and reading documentation but when it comes to writing a REAL web app, that is a WHOLE DIFFERENT ANIMAL.

Things I’ve come to realize/learn in the last few months:

  1. There’s a neat little nonsensical thing called “create_app” used in Flask that is actually used in most production web apps instead of the basic “app=Flask(__name__)” which most Udemy courses will teach you.  It might help to learn about programming patterns such as the Factory Pattern and implementation with this pattern is a bit different than with a simple “app=Flask”.
  2. Storing production keys in an “.ENV” file is usually used in real world applications.  And you will need to learn a different syntax/folder structure than BS examples on Udemy show you.  Setting up a “Config” folder with a “settings.py” with something like “app.config.from_object(‘config.settings’)” in your “create_app” function will be useful.  Though some courses will show you how to structure your application, most of them are poorly explained.
  3. App / Folder structure is important.  There are lots of posts, guides and boilerplates teaching you to import modules from a __init__.py creating a “circular dependency”.  Your app will work but as it grows, you will face a cyclical dependency.  I found Lepture whom is a Flask contributor and wrote a great blog post on this topic: https://lepture.com/en/2018/structure-of-a-flask-project
    1. There are two main ways to structure an app:
      1. Functional Based Structure.
        1. Project
          1. Models
          2. Routes
          3. Templates
          4. Services.
      2. App Based Structure (Which I prefer)
        1. Project
          1. db.py
          2. auth/
          3. blog/
  4. SQLAlchemy / Postgresql – Though I’m sure it’s very useful for a lot of people, when you get into some of the advanced features of Postgresql it is not immediately obvious and poorly documented on how to integrate the advanced features through SQLAlchemy.  For example, I was toying around with my Postgres Database of 90 million rows with 100 columns and took a bunch of time to optimize my queries which I was so proud to cut down from 1+ hours to milliseconds.  Then when it came time to implement them in a Flask App, I stared blankly at the screen for what seemed like 10 hours in total amazement at how I had no idea what steps to even take to accomplish this.  Things like Materialized Views are not easily implemented through SQLAlchemy which means I’m back to the Psycopg2 Library again and learning how to implement that directly through Flask.  Only the psycopg2 library is much more poorly documented and there aren’t many examples of how to implement a good database design with it.  I did find one guy who was really able to pick apart the code for SQLAlchemy and implement MatViews so I’ll be trying to toy with this for the next couple weeks or so.  http://www.jeffwidman.com/blog/847/using-sqlalchemy-to-create-and-manage-postgresql-materialized-views/
  5. Docker – When deploying to production, your server provider will provide you with a Virtual Machine.  If you have multiple apps, instead of spinning up multiple Virtual Machines to fulfill your apps, you can have your apps share resources and spin up containers on your VM.  I’ll be learning a bunch more about Docker soon so I’ll have more to contribute in the coming weeks.  Looking forward to deploying some containerized apps in the coming months.

 

This entry was posted in Personal. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s