Skip to content

Instantly share code, notes, and snippets.

View ShakeM's full-sized avatar

Jonathan Jiang ShakeM

View GitHub Profile
@ShakeM
ShakeM / SQLAlchemy Easy ForeignKey
Last active November 12, 2018 16:02
SQLAlchemy foreign key class decorator
def foreign_key(table):
"""class decorator, add a foreign key to a sqlalchemy model.
parameter table is the destination table, in a one-to-many relationship, table is the "one" side.
"""
def ref_table(cls):
setattr(cls, '{0}_id'.format(table), db.Column(db.String(50), db.ForeignKey('{0}.id'.format(table))))
setattr(cls, table,
db.relationship(table.capitalize(), backref=db.backref(cls.__name__.lower() + 's', lazy='dynamic')))
return cls
@ShakeM
ShakeM / restful
Last active November 12, 2018 16:20
from flask import jsonify
class HttpCode:
success = 200
unauth_error = 401
params_error = 400
server_error = 500
@ShakeM
ShakeM / README.md
Created October 26, 2018 15:18 — forked from twolfson/README.md
Python unittest `setUp` inheritance

In some cases for Python unit tests, we want to automatically perform setUp methods in as declared in a base class. However, we still want setUp to work as per normal in the subclass. The following code will proxy the new setUp function to run it's base class' and the new one.

# Define a common test base for starting servers
class MyBaseTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        """On inherited classes, run our `setUp` method"""
        # Inspired via http://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class/17696807#17696807
        if cls is not MyBaseTestCase and cls.setUp is not MyBaseTestCase.setUp:
@ShakeM
ShakeM / main.py
Last active June 6, 2019 13:26
Python Resize Image
import os
from PIL import Image
def resize(file_name):
img = Image.open(file_name)
width = 1700
height = int(img.size[1] / img.size[0] * width)
img = img.resize((width, height), Image.ANTIALIAS)