diff --git a/README.md b/README.md index 1fa2ec4..d4218a1 100755 --- a/README.md +++ b/README.md @@ -1 +1,4 @@ -Using CLI slide +Expert python slide +---- + +PS: I used a custom webfonts. please note the flow: Font Size 23M diff --git a/code/async.py b/code/async.py new file mode 100644 index 0000000..ded2941 --- /dev/null +++ b/code/async.py @@ -0,0 +1,16 @@ +def framework(logic): + try: + it = logic() + s = next(it) + print "[FX] logic: ", s + print "[FX] do something" + it.send("async:" + s) + except StopIteration: + pass + +def logic(): + s = "mylogic" + r = yield s + print r + +framework(logic) diff --git a/code/blocking.py b/code/blocking.py new file mode 100644 index 0000000..95aa8ac --- /dev/null +++ b/code/blocking.py @@ -0,0 +1,14 @@ +def framework(logic, callback): + s = logic() + print "[FX] logic: ", s + print "[FX] do something" + callback("async:" + s) + +def logic(): + s = "mylogic" + return s + +def callback(s): + print s + +framework(logic, callback) diff --git a/code/borg.py b/code/borg.py new file mode 100644 index 0000000..a051746 --- /dev/null +++ b/code/borg.py @@ -0,0 +1,13 @@ +def borg(cls): + cls._state = {} + orig_init = cls.__init__ + def new_init(self, *args, **kwargs): + self.__dict__ = cls._state + orig_init(self, *args, **kwargs) + cls.__init__ = new_init + return cls + +@borg +class A(object): + def common(self): + print hex(id(self)) diff --git a/code/contextmanager.py b/code/contextmanager.py new file mode 100644 index 0000000..8219b07 --- /dev/null +++ b/code/contextmanager.py @@ -0,0 +1,42 @@ +# coding=utf-8 +import threading +from contextlib import contextmanager + +lock = threading.Lock() + + +@contextmanager +def openlock(): + print('Acquire') + lock.acquire() + yield + print('Releasing') + lock.release() + +with openlock(): + print('Lock is locked: {}'.format(lock.locked())) + print 'Do some stuff' + + +@contextmanager +def openlock2(): + print('Acquire') + with lock: # threading.Lock其实就是个with的上下文管理器. + # __enter__ = acquire + yield + print('Releasing') + +with openlock2(): + print('Lock is locked: {}'.format(lock.locked())) + print 'Do some stuff' + +import pymongo + +@contextmanager +def operation(database, host='localhost', port=27017): + db = pymongo.MongoClient(host, port)[database] + yield db + db.connection.disconnect() + +with operation('test') as db: + print(db.test.find_one()) diff --git a/code/custom_defaultdict.py b/code/custom_defaultdict.py new file mode 100644 index 0000000..dd9efa2 --- /dev/null +++ b/code/custom_defaultdict.py @@ -0,0 +1,16 @@ +import bisect +from collections import defaultdict + + +class Mytype(object): + def __init__(self): + self._arrs = [] + def insort(self, arr): + bisect.insort_left(self._arrs, arr) + + +d = defaultdict(Mytype) +d['l'].insort(1) +d['l'].insort(9) +d['l'].insort(4) +print d['l']._arrs diff --git a/code/decorator_class.py b/code/decorator_class.py new file mode 100644 index 0000000..83dda4e --- /dev/null +++ b/code/decorator_class.py @@ -0,0 +1,15 @@ +class Common(object): + def __init__(self, func): + self.func = func + def __call__(self, *args, **kwargs): + print 'args:', args + return self.func(*args, **kwargs) + + +@Common +def test(p): + print p + + +test +test(1) diff --git a/code/decorator_common.py b/code/decorator_common.py new file mode 100644 index 0000000..4a85d6e --- /dev/null +++ b/code/decorator_common.py @@ -0,0 +1,13 @@ +def common(func): + def _deco(*args, **kwargs): + print 'args:', args + return func(*args, **kwargs) + return _deco + +@common +def test(p): + print p + + +test +test(1) diff --git a/code/decorator_for_class.py b/code/decorator_for_class.py new file mode 100644 index 0000000..76e3efa --- /dev/null +++ b/code/decorator_for_class.py @@ -0,0 +1,19 @@ +def singleton(cls): + class deco(cls): + def __new__(cls, *args, **kwargs): + o = getattr(cls, "__instance__", None) + if o is None: + o = object.__new__(cls) + cls.__instance__ = o + return o + return deco + +@singleton +class A(object): + def common(self): + print hex(id(self)) + + +a, b = A(), A() +a is b +a.common() diff --git a/code/decorator_with_args.py b/code/decorator_with_args.py new file mode 100644 index 0000000..74e28ca --- /dev/null +++ b/code/decorator_with_args.py @@ -0,0 +1,16 @@ +def common(*args, **kw): + a = args + def _common(func): + def _deco(*args, **kwargs): + print 'args:', args, a + return func(*args, **kwargs) + return _deco + return _common + +@common('c') +def test(p): + print p + + +test +test(1) diff --git a/code/encoding.py b/code/encoding.py new file mode 100644 index 0000000..54d8ec7 --- /dev/null +++ b/code/encoding.py @@ -0,0 +1,6 @@ +# encoding: utf-8 + +from __future__ import unicode_literals +import encoding_example as a +b = 'helló wörld from this' +print b + a.name.decode('utf-8') diff --git a/code/encoding_example.py b/code/encoding_example.py new file mode 100644 index 0000000..e417f99 --- /dev/null +++ b/code/encoding_example.py @@ -0,0 +1,2 @@ +# encoding: utf-8 +name = 'helló wörld from example' diff --git a/code/end.py b/code/end.py new file mode 100644 index 0000000..85f90fc --- /dev/null +++ b/code/end.py @@ -0,0 +1,19 @@ +__builtins__.end = None + +def test(x): + if x > 0: + print "a" + else: + print "b" + end +end + + +def main(): + test(1) + print('I can use end!') +end + + +if __name__ == "__main__": + main() diff --git a/code/fibonacci.py b/code/fibonacci.py new file mode 100644 index 0000000..e69de29 diff --git a/code/for_absolute_import/__init__.py b/code/for_absolute_import/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/code/for_absolute_import/main.py b/code/for_absolute_import/main.py new file mode 100644 index 0000000..ae7e3e2 --- /dev/null +++ b/code/for_absolute_import/main.py @@ -0,0 +1,3 @@ +from __future__ import absolute_import +from .string import a +from for_absolute_import.string import a diff --git a/code/for_absolute_import/string.py b/code/for_absolute_import/string.py new file mode 100644 index 0000000..1337a53 --- /dev/null +++ b/code/for_absolute_import/string.py @@ -0,0 +1 @@ +a = 1 diff --git a/code/generatoe-sample.py b/code/generatoe-sample.py new file mode 100644 index 0000000..f211bca --- /dev/null +++ b/code/generatoe-sample.py @@ -0,0 +1,10 @@ +class Data(object): + def __init__(self, *args): + self._data = list(args) + def __iter__(self): + for x in self._data: + yield x + +d = Data(1, 2, 3) +for x in d: + print(x) diff --git a/code/iter-sample.py b/code/iter-sample.py new file mode 100644 index 0000000..b929824 --- /dev/null +++ b/code/iter-sample.py @@ -0,0 +1,40 @@ +class Data(object): + def __init__(self): + self._data = [] + + def add(self, x): + self._data.append(x) + + def data(self): + return iter(self._data) + +d = Data() +d.add(1) +d.add(2) +d.add(3) +for x in d.data(): + print(x) + +class Data(object): + def __init__(self, *args): + self._data = list(args) + self._index = 0 + + def __iter__(self): + return self + + def next(self): + if self._index >= len(self._data): + raise StopIteration() + d = self._data[self._index] + self._index += 1 + return d + +d = Data(1, 2, 3) +for x in d: + print(x) + +d = Data(1, 2, 3) +it = iter(d) +next(it) +next(it) diff --git a/code/metaclass.py b/code/metaclass.py new file mode 100644 index 0000000..0a5ea98 --- /dev/null +++ b/code/metaclass.py @@ -0,0 +1,52 @@ +# coding=utf-8 +class Hello(object): + + def __init__(self, func): + self.func = func + + def hello(self): + print 'hello world' + + +def hello(cls): + print 'hello world' + + +def __init__(cls, func): + cls.func = func + + +class HelloMeta(type): # 注意继承至type + + def __new__(cls, name, bases, attrs): # 这个就是前面说的4个必要属性 + def __init__(cls, func): + cls.func = func + + def hello(cls): + print 'hello world' + t = type.__new__(cls, name, bases, dict( + attrs.viewitems() | [('hello', hello), ('__init__', __init__)])) + return t # 要return创建的类哦 + + +class New_Hello(object): + __metaclass__ = HelloMeta + + +hellometa = lambda name, parents, attrs: type( + name, + parents, + dict(attrs.items() + [ + ('__new__', classmethod( + lambda cls, *args, **kargs: + super(type(cls), cls).__new__( + *args, **kargs) + )), + ('hello', hello), + ('__init__', __init__) + ]) +) + + +class New_Hello2(object): + __metaclass__ = hellometa diff --git a/code/property.py b/code/property.py new file mode 100644 index 0000000..ca177dc --- /dev/null +++ b/code/property.py @@ -0,0 +1,26 @@ +class C(object): + def __init__(self): + self._x = None + def getx(self): + print 'get x' + return self._x + def setx(self, value): + print 'set x' + self._x = value + def delx(self): + print 'del x' + del self._x + x = property(getx, setx, delx, "I'm the 'x' property.") + + +class Parrot(object): + def __init__(self): + self._voltage = 100000 + @property + def voltage(self): + """Get the current voltage.""" + return self._voltage + @voltage.setter + def voltage(self, value): + """Set to current voltage.""" + self._voltage = value diff --git a/code/with.py b/code/with.py new file mode 100644 index 0000000..8dfa7a5 --- /dev/null +++ b/code/with.py @@ -0,0 +1,15 @@ +import pymongo + + +class Operation(object): + def __init__(self, database, host='localhost', port=27017): + self._db = pymongo.MongoClient(host, port)[database] + + def __enter__(self): + return self._db + + def __exit__(self, *args): + self._db.connection.disconnect() + +with Operation(database='test') as db: + print db.test.find_one() diff --git a/css/default.css b/css/default.css index 9cc807e..f7d720b 100755 --- a/css/default.css +++ b/css/default.css @@ -1,9 +1,16 @@ +@font-face { + font-family: 'Wawa'; + src: url('../fonts/wawa.eot'); /* IE9 Compat Modes */ + src: url('../fonts/wawa.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */ + url('../fonts/wawa.woff') format('woff'), /* Modern Browsers */ + url('../fonts/wawa.ttf') format('truetype'); /* Safari, Android, iOS */ +} body { - /*font-family: 'WenQuanYi Micro Hei';*/ + font-family: 'Wawa'; font-size: 20px; } h1, h2, h3 { - /*font-family: 'WenQuanYi Micro Hei';*/ + font-family: 'Wawa'; font-weight: 400; margin-bottom: 0; } diff --git a/fonts/wawa.eot b/fonts/wawa.eot new file mode 100644 index 0000000..498208f Binary files /dev/null and b/fonts/wawa.eot differ diff --git a/fonts/wawa.ttf b/fonts/wawa.ttf new file mode 100644 index 0000000..b68c810 Binary files /dev/null and b/fonts/wawa.ttf differ diff --git a/fonts/wawa.woff b/fonts/wawa.woff new file mode 100644 index 0000000..f842681 --- /dev/null +++ b/fonts/wawa.woff @@ -0,0 +1 @@ +Error converting the font diff --git a/img/hist-word.png b/img/hist-word.png deleted file mode 100644 index c46fe66..0000000 Binary files a/img/hist-word.png and /dev/null differ diff --git a/img/hist.png b/img/hist.png deleted file mode 100644 index fccf917..0000000 Binary files a/img/hist.png and /dev/null differ diff --git a/img/pypa.png b/img/pypa.png new file mode 100644 index 0000000..76a0f79 Binary files /dev/null and b/img/pypa.png differ diff --git a/img/terminal.png b/img/terminal.png deleted file mode 100644 index 979d485..0000000 Binary files a/img/terminal.png and /dev/null differ diff --git a/img/unix-magic.jpg b/img/unix-magic.jpg deleted file mode 100644 index 1e7818b..0000000 Binary files a/img/unix-magic.jpg and /dev/null differ diff --git a/index.html b/index.html index 0a0fa98..de9ed32 100755 --- a/index.html +++ b/index.html @@ -2,831 +2,1466 @@ - 像黑客一样使用 Linux 命令行 - + python高级编程