Skip to content

Instantly share code, notes, and snippets.

@icejoywoo
icejoywoo / Expr.scala
Last active January 12, 2016 16:38 — forked from anonymous/Expr.scala
expression problem in week 4 (progfun-005 cousera)
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
@icejoywoo
icejoywoo / demo.sh
Last active August 1, 2018 02:12
bash snippets
#!/bin/bash
# 显示脚本的各种调用命令
set -x
# 一但有任何一个语句返回非真的值,则退出bash
set -e
# 你使用未初始化的变量时,让bash自动退出
set -u
SCRIPT_PATH="${BASH_SOURCE[0]}";
@icejoywoo
icejoywoo / descriptor_demo.py
Created July 10, 2015 17:03
descriptor look-up precedence
#!/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 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://...")
* 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
@icejoywoo
icejoywoo / logging_simple_wrapper.py
Created November 21, 2014 09:41
Sometimes you want to wrap the logging, or just some old code wrap the logging. If you want to get the filename, lineno and funcName using log format, logging will give you the wrapper function's filename, lineno and funcName. I just copy some code from logging to solve this problem.
#!/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)
#!/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):
#!/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
# 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"])
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):