Skip to content

Instantly share code, notes, and snippets.

View jasonrhaas's full-sized avatar
🎯
Consulting @ Data North

Jason Haas jasonrhaas

🎯
Consulting @ Data North
View GitHub Profile
from github import Github
import os
import sys
# First create a Github instance: using an access token
access_token = os.getenv('GITHUB_ACCESS_TOKEN')
g = Github(access_token)
# Select your repo
CREATE OR REPLACE FUNCTION pseudo_encrypt(VALUE bigint) returns bigint AS $$
DECLARE
l1 bigint;
l2 bigint;
r1 bigint;
r2 bigint;
i int:=0;
BEGIN
l1:= (VALUE >> 32) & 4294967295::bigint;
r1:= VALUE & 4294967295;
@app.route('/<tiny_url>', methods=['GET'])
def get_long_url(tiny_url):
if request.method == 'GET':
try:
url_id = short_url.decode_url(tiny_url)
except Exception:
abort(500)
app.logger.debug(url_id)
url = Url.query.filter_by(id=url_id).first()
app.logger.debug(url)
@jasonrhaas
jasonrhaas / app.py
Last active January 30, 2019 22:35
get_tiny_url
@app.route('/', methods=['GET', 'POST'])
def get_tiny_url():
if request.method == 'POST' and request.is_json:
long_url = request.get_json().get('url')
if not long_url:
abort(400)
if not long_url.startswith('http'):
long_url = f'http://{long_url}'
h = blake2b(long_url.encode(), digest_size=20).hexdigest()
app.logger.debug(h)
@jasonrhaas
jasonrhaas / app.py
Created January 30, 2019 22:23
Url model
class Url(db.Model):
id = db.Column(db.Integer, primary_key=True)
hash = db.Column(db.String(80), unique=True, nullable=False)
long = db.Column(db.Text(), nullable=False)
hits = db.Column(db.Numeric(), nullable=False, default=0)
def __repr__(self):
return '<Url %r>' % self.id
@jasonrhaas
jasonrhaas / factory.py
Created January 30, 2019 19:31
Register Swagger Blueprint
swagger_bps = Swagger.blueprint(spec=self.spec, api_name=self.name)
restless_bps = Restless.blueprints(
# url_prefix='/{}'.format(self.name),
exclusions=self.exclusions,
app=self.app,
db=self.db,
models=self.models,
)
self.register_blueprints(welcome, restless_bps, swagger_bps, self.other_bps)
@jasonrhaas
jasonrhaas / blueprints.py
Created January 30, 2019 19:27
Swagger blueprint
from flask import Blueprint, jsonify
from connexion import Api
class Swagger:
def blueprint(spec, **yaml_kwargs):
api = Api(spec, swagger_ui=True, strict_validation=True, validate_responses=False, arguments=yaml_kwargs)
bp = api.blueprint
@bp.errorhandler(404)
@jasonrhaas
jasonrhaas / match_pattern.py
Created November 21, 2017 17:25
datadog interview question
# Python 3.6
import re
import logging
import unittest
from itertools import zip_longest
logging.basicConfig(level=logging.DEBUG)
in-slave03.nj.istresearch.com] out: 2015-11-06 11:25:23,434 - pykafka.simpleconsumer - INFO - Autocommitting consumer offset for consumer group scrapy-janitor and topic memex.crawled_firehose
[in-slave02.nj.istresearch.com] out: 2015-11-06 11:25:28,692 - kazoo.recipe.watchers - ERROR - timed out
[in-slave02.nj.istresearch.com] out: Traceback (most recent call last):
[in-slave02.nj.istresearch.com] out: File "/tmp/virtualenv/virtualenv_root/scrapy-janitor/lib/python2.7/site-packages/kazoo/recipe/watchers.py", line 333, in _get_children
[in-slave02.nj.istresearch.com] out: result = self._func(children)
[in-slave02.nj.istresearch.com] out: File "/tmp/virtualenv/virtualenv_root/scrapy-janitor/lib/python2.7/site-packages/pykafka/balancedconsumer.py", line 599, in _brokers_changed
[in-slave02.nj.istresearch.com] out: self._rebalance()
[in-slave02.nj.istresearch.com] out: File "/tmp/virtualenv/virtualenv_root/scrapy-janitor/lib/python2.7/site-packages/pykafka/balancedconsumer.py", line 454, in _rebalan
from pykafka import KafkaClient
import logging
logging.basicConfig(level=logging.INFO)
class KafkaConnect(object):
def __init__(self,
kafka_hosts='localhost:9092',
zk_hosts='localhost:2181',