This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Expr { | |
| def eval: Int = this match { | |
| case Number(n) => n | |
| case Sum(e1, e2) => e1.eval + e2.eval | |
| } | |
| } | |
| case class Number(n: Int) extends Expr | |
| case class Sum(e1: Expr, e2: Expr) extends Expr | |
| case class Prod(e1: Expr, e2: Expr) extends Expr | |
| case class Var(x: String) extends Expr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # 显示脚本的各种调用命令 | |
| set -x | |
| # 一但有任何一个语句返回非真的值,则退出bash | |
| set -e | |
| # 你使用未初始化的变量时,让bash自动退出 | |
| set -u | |
| SCRIPT_PATH="${BASH_SOURCE[0]}"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| # encoding: utf-8 | |
| from __future__ import print_function | |
| """ | |
| Data and non-data descriptors differ in how overrides are calculated with respect to entries in an instance’s dictionary. | |
| If an instance’s dictionary has an entry with the same name as a data descriptor, the data descriptor takes precedence. | |
| If an instance’s dictionary has an entry with the same name as a non-data descriptor, the dictionary entry takes precedence. | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| This module adds Redis Sentinel transport support to Celery. | |
| Current version of celery doesn't support Redis sentinel client, which is must have for automatic failover. | |
| To use it:: | |
| import register_celery_alias | |
| register_celery_alias("redis-sentinel") | |
| celery = Celery(..., broker="redis-sentinel://...", backend="redis-sentinel://...") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| * Generate classes like WhereNot automatically with class decorator? | |
| * Haskell style deconstruction, perhaps like: | |
| @pmatch | |
| def count(first_arg=Match(x=Head, *xs=Rest)): | |
| print x, xs | |
| Will require some nasty hacks like https://github.com/smcq/python-inject | |
| * Implement boolean operators for stuff like: | |
| @pmatch |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| # ^_^ encoding: utf-8 ^_^ | |
| import logging | |
| import sys | |
| logging.basicConfig( | |
| format='[%(asctime)s][%(levelname)s][%(process)d][%(thread)d]%(message)s', # no space before message | |
| filename='xxx.log', level=logging.DEBUG) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env python | |
| #^_^ encoding: utf-8 ^_^ | |
| # 函数式编程: http://coolshell.cn/articles/10822.html | |
| class Pipe(object): | |
| def __init__(self, func): | |
| self.func = func | |
| def __ror__(self, other): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| """ | |
| This module provides a class for cron-like scheduling systems, and | |
| exposes the function used to convert static cron expressions to Python | |
| sets. | |
| CronExpression objects are instantiated with a cron formatted string | |
| that represents the times when the trigger is active. When using | |
| expressions that contain periodic terms, an extension of cron created |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # based on Recipe 466302: Sorting big files the Python 2.4 way | |
| # by Nicolas Lehuen | |
| import os | |
| from tempfile import gettempdir | |
| from itertools import islice, cycle | |
| from collections import namedtuple | |
| import heapq | |
| Keyed = namedtuple("Keyed", ["key", "obj"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def sbv0(adict, reverse=False): | |
| return sorted(adict.iteritems(), key=lambda (k, v): (v, k), reverse=reverse) | |
| def sbv1(d, reverse=False): | |
| L = [(k, v) for (k, v) in d.iteritems()] | |
| return sorted(L, key=lambda x: x[1], reverse=reverse) | |
| def sbv2(d, reverse=False): |