Skip to content

Instantly share code, notes, and snippets.

#! /bin/bash
while getopts "i:o:" opt; do
case $opt in
i ) input="$OPTARG" ;;
o ) output="$OPTARG" ;;
esac
done
#字符串替换使用这样方式,可以方便的替换,也可以使用自带的${string/sub/replace}
#input=`echo "$input" | sed 's/\ /\\\ /g'`
#coding=utf8
import codecs
#正则表达式
import re
import math
'''
python
将x±y替换成x±y/sqrt(n)
正则表达式操作
文件操作
#The GUI interface is depend on python-tk package, so you should install it first
sudo apt-get install python-tk
#display the GUI interface
pydoc -g
import __builtin__
# list all the predefined names
print dir(__builtin__)
@zenloner
zenloner / global.py
Last active December 16, 2015 00:59
y, z = 1, 2
def all_global():
#global declaration
global x
x = y+z
all_global()
#you can get x here
print x
@zenloner
zenloner / nestedscopy.py
Created April 10, 2013 03:15
factory function implemented with nested scope
def maker(N):
'''
if you want to get different functions
when you pass different parameters into a maker,
you can use nested scope function,
it's also known as a factory function.
'''
def action(X):
return X**N
return action
@zenloner
zenloner / lambda.py
Created April 10, 2013 03:23
using lambda to implement factory function
def makeActions():
acts = []
for i in range(5):
acts.append(lambda x : i ** x) # all remember same last i
return acts
acts = makeActions()
acts[0](2) # print 16
acts[2](2) # print 16
L = [(lambda x: x**2),(lambda x: x**3),(lambda x: x**4)]
for f in L:
print f(2)
mydict = {'a':(lambda x: x**2),'b':(lambda x: x**3),'c':(lambda x: x**4) }
@zenloner
zenloner / lambda2.py
Created April 10, 2013 03:33
print something in a lambda
import sys
#use sys.stdout.write(x) to print x in a lambda
showall = (lambda x: map(sys.stdout.write, x))
# showall = lambda x:[sys.stdout.write(line) for line in x]
t = showall(['a\n', 'b\n', 'c\n']) # it will print a\nb\nc\n
@zenloner
zenloner / lambda3.py
Created April 10, 2013 03:37
if else statement in a lambda
if a:
b
else:
c
# equivaltent expression
b if a else c