Skip to content

Instantly share code, notes, and snippets.

View toaco's full-sized avatar
🎯
Focusing

toaco toaco

🎯
Focusing
View GitHub Profile
@toaco
toaco / demo.py
Created January 30, 2018 02:31
导入某个路径下的所有模块
import importlib
import pkgutil
for loader, name, is_pkg in pkgutil.walk_packages(__path__):
importlib.import_module('.{}'.format(name), 'models')
@toaco
toaco / draft.py
Last active February 5, 2018 03:49
年阶梯气价计算方式
# encoding=utf8
from decimal import Decimal
def create_ladders(price_name, last_reading, current_reading, price):
""" 目前不考虑优惠,但是可以合计金额里面体现出来
ladder['price_name'] = price_name
ladder['last_reading'] = last_reading
ladder['current_reading'] = current_reading
ladder['gas'] = current_reading - last_reading
@toaco
toaco / demo.py
Created February 8, 2018 01:45
计算某天所属区间
import datetime
import arrow
def diff_month(d1, d2):
return (d1.year - d2.year) * 12 + d1.month - d2.month
def calc_current_period(start_date, period, now=None, unit='months'):
@toaco
toaco / README.md
Last active April 4, 2018 07:30
rubric设计

功能

  • 对象转换器,提供load_xxx/dump_xxx方法
  • 对象验证器,定义一个模式,判断一个Python对象是否符合该模式. validate
  • 对象生成器,generate
  • 提供其他库的插件:
    • flask/django等Web框架的表单验证插件
    • pymongo 扩展

对象验证

目标在于提供Pythonic的对象验证方式,如:

@toaco
toaco / solution.py
Last active February 15, 2019 01:36
Flight Ticket Booking Problem
# -*- coding: utf-8 -*-
import datetime
from collections import defaultdict
FLIGHTS = {
"chengdu_to_xian": {
"reward": [
{'name': 'GD2501', 'time': '08:00', 'price': 500, 'at_weekends': True},
{'name': 'GD2501', 'time': '08:00', 'price': 800, 'at_weekends': False},
{'name': 'GD2606', 'time': '12:25', 'price': 500, 'at_weekends': True},
@toaco
toaco / caculator.js
Last active March 28, 2020 15:28
A simple four arithmetic operations implementation
const Decimal = require('decimal.js');
// 计算表达式的值
function calculate(expression) {
const words = splitExpression(expression);
const outputs = [];
const signs = [];
while (words.length) {
const word = words.shift();