Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View camelcaseblog's full-sized avatar

camelcaseblog

View GitHub Profile
import itertools
import requests
def check_password(password):
data = {'username': '0501234567', 'password': password}
res = requests.post('http://localhost:8080/login', json=data)
if res.status_code == 200:
return True
return False
import hashlib
from bottle import run, post, request, HTTPResponse
def check_password(username, password):
hashed_password = hashlib.md5(password.encode('utf-8')).hexdigest()
return username == "0501234567" and hashed_password == '24fdf987d78b1f6d7c6008e7ecffeefb': # md5 of 01011980
@post('/login')
def login():
if check_password(request.json['username'], request.json['password']):
@camelcaseblog
camelcaseblog / 02 orm_data_model_example.py
Last active January 29, 2019 07:33
example of query with joins that affect performance
class Reporter(models.Model):
id = models.AutoField(primary_key=True, null=False)
name = models.TextField
class Article(models.Model):
id = models.AutoField(primary_key=True, null=False)
content = models.TextField(null=True)
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
ids = [a.reporter.id for a in Article.objects.filter(content__contains="camelCase")] # 1
select * from table where a=b and b=c
select * from table where a=b and b=c and a=c
>>> 0.1 + 0.2
0.30000000000000004
>>> round(0.1 + 0.2, 10) == 0.3
True
>>> from fractions import Fraction
>>> a = Fraction(1, 10)
>>> b = Fraction(2, 10)
>>> float(a + b)
0.3
>>> a + b == Fraction(3, 10)
True
>>> 0.1 + 0.2 == 0.3
False
>>> def compare(a, b, epsilon=0.00001):
... return abs(a - b) < epsilon
...
>>> compare(0.1 + 0.2, 0.3)
True
>>> class Reporter(models.Model):
... id = models.AutoField(primary_key=True, null=False)
... name = models.TextField
...
>>> qs = Reporter.objects.filter(name=None) # 1
>>> print(qs.query)
SELECT *
FROM reporter
WHERE name = NULL
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Base = declarative_base()