This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain | |
l = [[0, 1], [2, 3]] | |
l = list(chain.from_iterable(l)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt | |
bars = [3, 6, 2, 4] | |
plt.figure(figsize=(10, 5)) | |
plt.bar(range(len(bars)), bars, align='center') | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
counts = [10, 8, 6, 14] | |
weights = np.ones_like(counts) / float(len(counts)) | |
plt.figure(figsize=(10, 5)) | |
plt.hist(counts, bins=range(1, max(counts)+2), align='left', weights=weights) | |
plt.xticks(range(1, max(counts)+1)) | |
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy import create_engine | |
from sqlalchemy.engine.url import URL | |
from sqlalchemy.orm import sessionmaker | |
from sqlalchemy.sql.expression import text as Text | |
def db_connect(db): | |
return create_engine(URL(**db)) # db should be a dict with connection params | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import itemgetter | |
sorted_d = sorted(d.items(), key=itemgetter(1), reverse=True) | |
# sorted_dict is a list of tuples (key, value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L = [1, 2, 3, 1] | |
[i for i, x in enumerate(L) if x == value] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import request | |
# here we want to get the value of user (i.e. ?user=value) | |
user = request.args.get('user') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cProfile | |
import StringIO | |
import pstats | |
import contextlib | |
@contextlib.contextmanager | |
def profiled(): | |
pr = cProfile.Profile() | |
pr.enable() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import wraps | |
from flask import request, abort | |
def validate_post(validator, status_code): | |
"""Validate the POST request with the given validator function. | |
Abort with the given status_code upon validation failure. | |
""" | |
def decorator(func): | |
@wraps(func) | |
def wrapper(*args, **kwargs): |
OlderNewer