Skip to content

Instantly share code, notes, and snippets.

@nitred
Last active July 31, 2018 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitred/f31c79f8f127458d0b300bda2fc9d067 to your computer and use it in GitHub Desktop.
Save nitred/f31c79f8f127458d0b300bda2fc9d067 to your computer and use it in GitHub Desktop.
Common Utilities

About

Common Utility functions

Table of Contents

  • utils_argparse.py
  • utils_config.py
    • read_config
  • utils_datetime.py
    • get_month_start_date_from_date
  • utils_networking.py
    • download_url_to_file_with_progress_bar
  • utils_jinja2.md
    • basic jinja2 templating
  • External Resources:
    • Creating database sessions using SQLAlchemy and Flask-SQLAlchemy simultaneously: Link
import argparse
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--filename', action='store', dest='filename',
required=True, help='Some filename.')
parser.add_argument('--use_cpu', action='store_true', dest='use_cpu',
required=False, default=False, help='Use CPU instead GPU.')
command_args = parser.parse_args()
"""Config related utilities.
################################################################################
DEPENDENCIES
File: dev_environment.yml
Create Environment: conda env create --force -f dev_environment.yml
################################################################################
name: utils-config
channels:
- defaults
dependencies:
- python=3.6
- pip:
- future
- pyyaml
"""
import yaml
def read_config(config_filename):
"""Read and return config from filename.
Args:
config_filename (str): Path to config file
Returns:
dict: Config file as a dictionary.
"""
with open(config_filename, 'r') as f:
config = yaml.load(f)
return config
import datetime
def get_month_start_date_from_date(any_date):
"""Return the start date of the month of any date of datetime.
Args:
any_date (datetime.datetime or datetime.date): Any date or datetime.
Returns:
datetime.date: The start date of the month.
"""
if isinstance(any_date, datetime.datetime):
any_date = datetime.date
elif isinstance(any_date, datetime.date):
pass
else:
raise ValueError("`any_date` must be either datetime.datetime or datetime.date instead it is of type {}."
.format(type(any_date)))
month_start_date = any_date.replace(day=1)
return month_start_date

About

Flask snippets

Snippets

Shared Database Session

db_session.py.

"""Shared database session instantiation.

SOURCE: http://flask.pocoo.org/snippets/22/
"""
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker as SessionMaker
from sqlalchemy.orm import scoped_session

engine = None
sessionmaker = SessionMaker()
db_session = scoped_session(sessionmaker)


def init_engine(uri, **kwargs):
    """."""
    global engine, sessionmaker
    engine = create_engine(uri, **kwargs)
    db_session.remove()
    sessionmaker.configure(bind=engine)
    return engine

server.py

from db_models import db  # module where Tables are declared
from db_session import db_session, init_session

# ...
# Initialize app with database and other wiring
# ...

def register_database(app):
  """Register the app with the database."""
  db.init_app(app)
  with app.app_context():
      # Extensions like Flask-SQLAlchemy now know what the "current" app
      # is while within this block. Thereforse, you can now run........
      db.create_all()
      init_session(db)

Install Jinja2

pip install Jinja2

Basic Jinja2 Templating

"""Basic Jinja2 Templating."""
from jinja2 import Template

template = Template('Hello {{ name }}!')
template.render(name='John Doe')
import requests
from tqdm import tqdm
def download_url_to_file_with_progress_bar(url, filename):
# Streaming, so we can iterate over the response.
r = requests.get(url, stream=True)
# Total size in bytes.
total_size = int(r.headers.get('content-length', 0)) # Bytes
write_size = 1000 * 1000 # Bytes, 1MB
total_iters = (total_size // write_size) + 1
with open(filename, 'wb') as f:
for data in tqdm(r.iter_content(write_size), total=total_iters, unit='MB', unit_scale=True):
f.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment