Skip to content

Instantly share code, notes, and snippets.

@o3o3o
o3o3o / utils.py
Created August 8, 2019 02:26 — forked from Alir3z4/utils.py
Create a hash of a file on upload time and save it for Django FileField/ImageField
def hash_file(file, block_size=65536):
hasher = hashlib.md5()
for buf in iter(partial(file.read, block_size), b''):
hasher.update(buf)
return hasher.hexdigest()
def upload_to(instance, filename):
"""
@o3o3o
o3o3o / graphene.py
Created July 22, 2019 02:54 — forked from mixxorz/graphene.py
Get requested fields from ResolveInfo. Graphene python.
"""
MIT License
Copyright (c) 2018 Mitchel Cabuloy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@o3o3o
o3o3o / graphene.py
Created July 22, 2019 02:54 — forked from mixxorz/graphene.py
Get requested fields from ResolveInfo. Graphene python.
"""
MIT License
Copyright (c) 2018 Mitchel Cabuloy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@o3o3o
o3o3o / jwt_test_example_with_session.py
Last active July 9, 2019 02:11
django graphql jwt test with sessionMiddleware
from django.test import TestCase
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from graphql_jwt.testcases import JSONWebTokenClient
class MYJSONWebTokenClient(JSONWebTokenClient):
def execute(self, query, variables=None, **extra):
extra.update(self._credentials)
context = self.post("/", **extra)
@o3o3o
o3o3o / timeit.py
Last active June 14, 2019 09:16
log the duration of calling function
import time
from functools import wraps
def timeit(method):
@wraps(method)
def timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
https://press.one/p/v?s=91071199849ea0254aef566bcecdcf9686b0128162ae0dc2dcb4b4ddaabbe4d21068cbf83a6874a14c1e4416bd12b42a8c77a9d5e5725b39388883f3260c1f9b1&h=caf41557e46af6293b0f2d028200e32158b472a0255b2f4eb45884b29d31b9ba&a=4cf4d161036bf303e7fe92ff38b43eaab73f15e8&f=P1&v=3
@o3o3o
o3o3o / recurse_ fib.py
Last active December 26, 2018 07:40
tail recurse exercise
"""
fun fib(n) =
if n=1 then 1
else if n=2 then 1
else fib(n-1) + fib(n-2)
"""
def fib_loop(n):
a = 1
b = 2
@o3o3o
o3o3o / reverse_integers.py
Created December 26, 2018 07:37
reverse integers with tail recurse
# Execice for tail recurse
# Reverse the integr, strip 0
# if 0 is the first num
#
# e.g: 123450 --> 54321
def reverse(n):
s=list(str(n))
length = len(s)
@o3o3o
o3o3o / mount_phone_with_ftps.sh
Created July 7, 2018 03:19
mount phone strorage into pc with_ftps
curlftpfs ftp://192.168.0.105:3721 /tmp/phone
@o3o3o
o3o3o / log_middleware.py
Created July 7, 2018 03:17
log middleware
# duration: api 耗时,按秒计
# duration_ms: api 耗时,按毫秒计
# level: api 耗时档次,分为
# LEVEL-A: x < 1 sec
# LEVEL-B: 1 sec < x < 5 sec
# LEVEL-C: 5 sec < x < 10 sec
# LEVEL-D: x > 10 sec
# method: http 请求方法
# status_code: 请求响应状态
# user_agent: HTTP_USER_AGENT