Skip to content

Instantly share code, notes, and snippets.

View MerleLiuKun's full-sized avatar
🎯
Focusing

klein MerleLiuKun

🎯
Focusing
View GitHub Profile
@MerleLiuKun
MerleLiuKun / abstract_model_demo.py
Created October 19, 2017 04:58
django abstract model demo.
from django.db import models
class AbstractTimeModel(models.Model):
"""
Simple abstract base class.
provide the general created time, update time fields.
可以将 公用的字段抽取出来.
"""
created_at = models.DateTimeField(
@MerleLiuKun
MerleLiuKun / md5_for_large_file.py
Last active March 6, 2018 11:47
python encrypt large file with md5
def md5_for_file(path):
path = path.encode('utf-8')
md = hashlib.md5()
block_size = 128 * md.block_size
with open(path, 'rb') as f:
for chunk in iter(lambda: f.read(block_size), b''):
md.update(chunk)
return md.hexdigest()
@MerleLiuKun
MerleLiuKun / flask_models.py
Created May 15, 2018 10:02
Flask-SQLAlchemy use abstract
class BasicModel(db.Model):
__abstract__ = True
created_at = db.Column(db.DateTime, default=db.func.now())
updated_at = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())
class User(BasicModel):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
@MerleLiuKun
MerleLiuKun / retry_request
Created July 18, 2018 12:43
TwitterApi request retry
# coding=utf-8
from app.ext.token_gainer import twitter_api
from TwitterAPI.TwitterError import TwitterConnectionError, TwitterRequestError
def call_request(url, params, retries=3, api=None):
"""
:param url: twitter api
:param params: api param conmmon dict(t)
@MerleLiuKun
MerleLiuKun / cut_frame_by_cv2.py
Created October 31, 2018 02:12
Get Online video's frame pic
import cv2
video_url = 'https://scontent.xx.fbcdn.net/v/t50.2886-16/39483744_2088298591194407_5257741205319450624_n.mp4?_nc_cat=109&oh=807b4ef98367893cacb969323f47048b&oe=5C4D7231'
vidcap = cv2.VideoCapture(video_url)
vidcap.set(cv2.CAP_PROP_POS_MSEC, 1000)
success, image = vidcap.read()
if success:
cv2.imwrite('frame.jpg', image)
@MerleLiuKun
MerleLiuKun / convert_pdf_to_png.py
Created December 14, 2018 10:05
pdf file to image by page.
"""
pip install PyMuPDF==1.14.3
"""
import fitz
def convert_pdf_to_png(filename):
doc = fitz.open(filename=filename)
for pg in range(doc.pageCount):
@MerleLiuKun
MerleLiuKun / binary_conversion.py
Created March 19, 2019 10:56
simple binary conversion for numbers and letters
# coding=utf-8
CONSTANT_BEGIN = 201416920417
CONSTANT_CHAR = 'JTXD9hz6SBqpINCrx5Memk1LO2ylcPYjFnwbd4tv3VW0aiRK8oGE7fuQgsAUHZ'
CONSTANT_LENGTH = len(CONSTANT_CHAR)
def encode_b64(num):
if not isinstance(num, (int, long)):
raise TypeError('Must int num')
if num <= 0:
@MerleLiuKun
MerleLiuKun / git_fork_update.sh
Last active March 27, 2019 11:39
git fork then follow the source repositories update
# add remote upstream source
git remote add upstream https://github.com/encode/requests-async.git
# check the remote status
git remote -v
# fetch the upstream lastest update
git fetch upstream
# change current branch to master
filename = './xxx.pdf'
with open(GUIDO, 'rb') as g_f:
guido = MIMEApplication(g_f.read(), _subtype='pdf')
guido.add_header('Content-Disposition', 'attachment', filename=filename)
msg.attach(guido)
@MerleLiuKun
MerleLiuKun / mongo_server.sh
Created April 18, 2019 09:12
mongo db docker
# run a simple mongo server
docker run --rm --name mymongo -p 27017:27017 -d mongo
# run mongo web
docker run --rm --name mymongo-express --link mymongo:mongo -p 8081:8081 -d mongo-express