Skip to content

Instantly share code, notes, and snippets.

View takwas's full-sized avatar
🏠
Working from home

Olúwátóósìn Anímáṣahun takwas

🏠
Working from home
View GitHub Profile
@takwas
takwas / tracker.py
Last active October 9, 2015 00:31
This Python script finds all paths leading from one point (a SOURCE) to another (a DESTINATION).
#!/usr/bin/python
# This code finds all the paths leading from a given
# point, [the SOURCE] to another point [the DESTINATION]
#
# The data is mapped in such a way that every point maps
# to every point that can "directly" be reached from it.
#
# Then there is a recursive traversal over each point
@takwas
takwas / __init__.py
Last active November 7, 2015 09:57
Snippets from Sancta Crux app
#Path:/sancta_crux_app/__init__.py
# Module that initialises and runs the app
from flask import Flask, render_template
from flask.ext.bootstrap import Bootstrap
from flask.ext.moment import Moment
@takwas
takwas / Traceback
Created November 13, 2015 00:46
Debugging: UndefinedError: 'flask.ctx._AppCtxGlobals object' has no attribute 'user'
# Traceback
Traceback (most recent call last):
File "/home/acetakwas/dev/my_virtualenvs/flask_venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/acetakwas/dev/my_virtualenvs/flask_venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/acetakwas/dev/my_virtualenvs/flask_venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
@takwas
takwas / Traceback
Last active November 13, 2015 12:27
Issue with mutliple config subclasses being called
# Traceback prints this:
APP CREATED AS DEV
APP CREATED AS TEST
APP CREATED AS PROD
@takwas
takwas / forms.py
Last active December 9, 2015 09:02
Flask image uploading
class ProfileForm(Form):
img_fld = FileField('Upload a Profile Photo', \
validators=[FileAllowed(config['IMG_ALLOWED_EXTENSIONS'], 'Images only!')])
@takwas
takwas / actions.py
Created January 12, 2016 18:46
Python 'list' vs 'class' comparison
"""
List of actions:
do_help
do_about
"""
# standard library imports
import re
@takwas
takwas / snippet1.py
Created January 21, 2016 10:42
Which of these snippets uses a better approach?
###################
# FILE: commands.py
###################
cmds = {
'help' : Command(cmd='help', callback=actions.do_help,
help_text= \
"""
Usage:
@takwas
takwas / import_1.py
Created January 26, 2016 13:10
Should all imports in a function come at the top of the function?
def create_app(config_key):
"""
A factory function to create a Flask application instance and do
some initializations.
"""
# import `config_dict` dictionary from configuration module:
# `config.py`
from config import config_dict
configuration = config_dict.get(config_key)
class JobMedia(db.Model):
__tablename__ = 'jobs_media'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False, unique=True)
job_id = db.Column(db.Integer, db.ForeignKey('jobs.id'), nullable=False, unique=True)
job = db.relationship('Job', uselist=False,
backref=db.backref('media', uselist=False))
@takwas
takwas / controllers.py
Last active April 29, 2016 15:18
Flask-SQLAlchemy User Model
@user.route('/profile/<user_uid>')
@user.route('/profile/')
@user.route('/')
@login_required
def profile(user_uid=None):
if user_uid is None:
user = current_user
else:
user = db_ops.ret_val(db_ops.User, dict(uid=user_uid))