Skip to content

Instantly share code, notes, and snippets.

View arischow's full-sized avatar
🐈
Focusing with my kittens Loki, Kasumi and Miumiu

Aris Chow arischow

🐈
Focusing with my kittens Loki, Kasumi and Miumiu
View GitHub Profile
# -*- coding: utf-8 -*-
print('Hello, my Gist!')
# Py 文件所在目录
import os
print(__file__) # 包括文件名
print(os.path.dirname(os.path.abspath(__file__)))
# REDIS SET ADD
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
def opcount(fname):
with open(fname) as f:
for i, l in enumerate(f):
pass
return i + 1
@arischow
arischow / get_all_files.py
Created October 21, 2016 02:24
获取某目录下的所有同类型文件
# 获取某目录下的所有同类型文件
import os
import fnmatch
matches = []
for root, dirs, files in os.walk('/Users/arischow/Desktop/data'):
for file in fnmatch.filter(files, '*.txt'):
matches.append(os.path.join(root, file))
# Check http://stackoverflow.com/questions/17285826/flask-redirecting-multiple-routes for more details.
@arischow
arischow / get_query_string.js
Last active March 22, 2017 07:53
JavaScript
// Get all query string
var after_slash = window.location.search // "search?number=007&name=Aris&addr=PHP+is+the+best"
var urlParams = new URLSearchParams(window.location.search);
console.log(urlParams.get('number')); // "007";
console.log(urlParams.get('name')); // "Aris"
console.log(urlParams.get('addr')); // "PHP is the best";
console.log(urlParams.has('name')); // true
console.log(urlParams.append('language', 'zh_CN')); // "?number=007&name=Aris&addr=PHP+is+the+best&language=zh_CN"
# dropping a database via pymongo
from pymongo import Connection
client = MongoClient() # default host localhost, port 27017
client.drop_database('mydatabase')
# drop a collection via pymongo
from pymongo import Connection
client = MongoClient()
client['mydatabase'].drop_collection('mycollection')
@arischow
arischow / model.py
Last active April 9, 2019 03:20
[Django] abstract base model
# Based on https://medium.com/@adriennedomingus/soft-deletion-in-django-e4882581c340
# There’s one other thing that might be interesting to point out about the implementation of the soft-deletion: the unique together constraint.
# If you have a model with a unique together rule for fields ‘a’ and ‘b’, when an entry with the values ‘a=1’ and ‘b=2’ is soft-deleted, another one could not be created with the same values for those fields. So the ‘deleted_at’ has to be included on the constraint.
from django.db import models
from django.utils import timezone
class HasDeletedError(Exception):
pass
import random
# 生成5个0~9的不重复随机数
print random.sample(range(10), 5)
# [7, 9, 6, 2, 3]
# 从a~d中取出不重复的三个字母
print random.sample(['a', 'b', 'c', 'd'], 3)
# ['d', 'b', 'c']
@arischow
arischow / Dockerfile
Last active September 7, 2020 17:22
Dockerfile multi-stage build for Python
# build wheels
FROM python:3.7.9-buster as builder
ENV PYTHONFAULTHANDLER=1 \
PYTHONUNBUFFERED=1 \
PYTHONHASHSEED=random \
# pip:
PIP_NO_CACHE_DIR=yes \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100