Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Created January 18, 2012 00:04
Show Gist options
  • Save shimizukawa/1629911 to your computer and use it in GitHub Desktop.
Save shimizukawa/1629911 to your computer and use it in GitHub Desktop.
エキPy読書会2nd #3 スクリーン
>>> a = range(10)
>>> import itertools
>>> b, c = itertools.tee(a)
>>> a2 = iter(a)
>>> b, c = itertools.tee(a2)
>>> next(b)
0
>>> next(b)
1
>>> next(b)
2
>>> next(b)
3
>>> next(b)
4
>>> next(b)
5
>>> next(c)
0
>>> next(c)
1
>>> next(c)
2
>>> d = c
>>> next(d)
3
>>> next(d)
4
>>> next(d)
5
>>> next(c)
6
>>> next(a2)
7
>>> id(a2)
43650320
>>> id(b)
43721512
>>> id(c)
43624928
>>> itertools.tee(iter(a),10)
(<itertools.tee object at 0x0299B260>, <itertools.tee object at 0x0299B2D8>, <itertools.tee object at 0x0299B1E8>, <itertools.tee object at 0x0299B3C8>, <itertools.tee object at 0x0299B418>, <itertools.tee object at 0x0299B238>, <itertools.tee object at 0x0299B300>, <itertools.tee object at 0x0299B3F0>, <itertools.tee
object at 0x0299B3A0>, <itertools.tee object at 0x0299B4B8>)
>>>
>>>
>>> s = 'abcddddefg'
>>> itertools.groupby(s)
<itertools.groupby object at 0x029AF9F0>
>>> g = _
>>> list(g)
[('a', <itertools._grouper object at 0x029A0FD0>), ('b', <itertools._grouper object at 0x029A0FF0>), ('c', <itertools._grouper object at 0x029A3070>), ('d', <itertools._grouper object at 0x029A30B0>), ('e', <itertools._grouper object at 0x029A3050>), ('f', <itertools._grouper object at 0x029A30D0>), ('g', <itertools._grouper object at 0x029A30F0>)]
>>> list(g)
[]
>>> s = 'aaabcddddefg'
>>> g = itertools.groupby(s)
>>> k,v = next(g)
>>> k
'a'
>>> v
<itertools._grouper object at 0x029A05F0>
>>> list(v)
['a', 'a', 'a']
>>>
>>> s = 'aabbca'
>>> g = itertools.groupby(s)
>>> k,v = next(g)
>>> k
'a'
>>> list(v)
['a', 'a']
>>> next(g)
('b', <itertools._grouper object at 0x02715D50>)
>>> next(g)
('c', <itertools._grouper object at 0x029A0E10>)
>>> next(g)
('a', <itertools._grouper object at 0x029A00B0>)
>>> g2 = _
>>> next(g)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> g2
('a', <itertools._grouper object at 0x029A00B0>)
>>> list(g2[1])
['a']
>>>
>>>
>>>
>>>
>>>
>>> def egg(x):
... print x
...
>>> egg(10)
10
>>> def egg(x):
... print 'egg:', x
...
>>> egg(10)
egg: 10
>>>
>>>
>>> print 'before call'
before call
>>> egg(10)
egg: 10
>>> print 'post call'
post call
>>>
>>> @print_decorator
... def egg(x):
... print 'egg:', x
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'print_decorator' is not defined
>>>
>>>
>>>
>>>
>>> egg(10)
egg: 10
>>>
>>> @egg
... def foo():
... pass
...
egg: <function foo at 0x0298F830>
>>> def foo():
... passs
...
>>> egg(foo)
egg: <function foo at 0x0298F7B0>
>>> a = egg(10)
egg: 10
>>> a
>>> a is None
True
>>>
>>>
>>>
>>>
>>>
>>> egg(10)
egg: 10
>>>
>>> def print_function(func):
... def wrapper(*args, **kw):
... print 'before'
... res = func(*args, **kw)
... print 'after'
... return res
... return wrapper
...
>>> @print_function
... def ham(x):
... print 'ham:',x
...
>>> ham(10)
before
ham: 10
after
>>> def p():
... @print_function
... def ham(x):
... print 'ham:',x
...
>>> import pdb
>>> pdb.runcall
<function runcall at 0x0298E8F0>
>>> pdb.runcall(p)
> <stdin>(2)p()
(Pdb) s
--Call--
> <stdin>(1)print_function()
(Pdb) list
[EOF]
(Pdb) s
> <stdin>(2)print_function()
(Pdb)
> <stdin>(7)print_function()
(Pdb)
--Return--
> <stdin>(7)print_function()-><functio...0298F930>
(Pdb)
--Return--
> <stdin>(2)p()->None
(Pdb)
>>>
>>>
>>>
[c:\project]
$ d:
[D:\]
$ dir
ドライブ D のボリューム ラベルは BFRD-DRIVE です
ボリューム シリアル番号は 1234-5678 です
D:\ のディレクトリ
2012/01/17 20:11 243 test.py
1 個のファイル 243 バイト
0 個のディレクトリ 534,933,504 バイトの空き領域
[D:\]
$ cat test.py
def print_function(func):
def wrapper(*args, **kw):
print 'before'
res = func(*args, **kw)
print 'after'
return res
return wrapper
@print_function
def ham(x):
print 'ham:',x
ham(10)
[D:\]
$ python -m pdb test.py
> d:\test.py(1)<module>()
-> def print_function(func):
(Pdb) n
> d:\test.py(9)<module>()
-> @print_function
(Pdb)
> d:\test.py(13)<module>()
-> ham(10)
(Pdb) c
before
ham: 10
after
The program finished and will be restarted
> d:\test.py(1)<module>()
-> def print_function(func):
(Pdb) c
before
ham: 10
after
The program finished and will be restarted
> d:\test.py(1)<module>()
-> def print_function(func):
(Pdb) q
[D:\]
$ python -m pdb test.py
> d:\test.py(1)<module>()
-> def print_function(func):
(Pdb) n
> d:\test.py(9)<module>()
-> @print_function
(Pdb) list
4 res = func(*args, **kw)
5 print 'after'
6 return res
7 return wrapper
8
9 -> @print_function
10 def ham(x):
11 print 'ham:',x
12
13 ham(10)
14
(Pdb) s
--Call--
> d:\test.py(1)print_function()
-> def print_function(func):
(Pdb) n
> d:\test.py(2)print_function()
-> def wrapper(*args, **kw):
(Pdb) list
1 def print_function(func):
2 -> def wrapper(*args, **kw):
3 print 'before'
4 res = func(*args, **kw)
5 print 'after'
6 return res
7 return wrapper
8
9 @print_function
10 def ham(x):
11 print 'ham:',x
(Pdb) func
<function ham at 0x028CACF0>
(Pdb) n
> d:\test.py(7)print_function()
-> return wrapper
(Pdb) n
--Return--
> d:\test.py(7)print_function()-><functio...028CAEF0>
-> return wrapper
(Pdb) n
> d:\test.py(13)<module>()
-> ham(10)
(Pdb) list
8
9 @print_function
10 def ham(x):
11 print 'ham:',x
12
13 -> ham(10)
14
[EOF]
(Pdb) list 0
1 def print_function(func):
2 def wrapper(*args, **kw):
3 print 'before'
4 res = func(*args, **kw)
5 print 'after'
6 return res
7 return wrapper
8
9 @print_function
10 def ham(x):
11 print 'ham:',x
(Pdb)
12
13 -> ham(10)
14
[EOF]
(Pdb) s
--Call--
> d:\test.py(2)wrapper()
-> def wrapper(*args, **kw):
(Pdb) n
> d:\test.py(3)wrapper()
-> print 'before'
(Pdb) p args
(10,)
(Pdb) p kw
{}
(Pdb) n
before
> d:\test.py(4)wrapper()
-> res = func(*args, **kw)
(Pdb) list
1 def print_function(func):
2 def wrapper(*args, **kw):
3 print 'before'
4 -> res = func(*args, **kw)
5 print 'after'
6 return res
7 return wrapper
8
9 @print_function
10 def ham(x):
11 print 'ham:',x
(Pdb) s
--Call--
> d:\test.py(9)ham()
-> @print_function
(Pdb) n
> d:\test.py(11)ham()
-> print 'ham:',x
(Pdb) list
6 return res
7 return wrapper
8
9 @print_function
10 def ham(x):
11 -> print 'ham:',x
12
13 ham(10)
14
[EOF]
(Pdb) n
ham: 10
--Return--
> d:\test.py(11)ham()->None
-> print 'ham:',x
(Pdb) n
> d:\test.py(5)wrapper()
-> print 'after'
(Pdb) list
1 def print_function(func):
2 def wrapper(*args, **kw):
3 print 'before'
4 res = func(*args, **kw)
5 -> print 'after'
6 return res
7 return wrapper
8
9 @print_function
10 def ham(x):
11 print 'ham:',x
(Pdb) n
after
> d:\test.py(6)wrapper()
-> return res
(Pdb) res is None
True
(Pdb) n
--Return--
> d:\test.py(6)wrapper()->None
-> return res
(Pdb) n
--Return--
> d:\test.py(13)<module>()->None
-> ham(10)
(Pdb) c
The program finished and will be restarted
> d:\test.py(1)<module>()
-> def print_function(func):
(Pdb) q
[D:\]
$ cat test.py
def print_function(func):
def wrapper(*args, **kw):
print 'before'
res = func(*args, **kw)
print 'after'
return res
return wrapper
@print_function
def ham(x):
print 'ham:',x
ham(10)
[D:\]
$ python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> def ham(x):
... print 'ham:',x
...
>>> ham.__name__
'ham'
>>>
>>>
>>>
>>> class Foo(object):
... def x(self):
... return 10
...
>>> foo = Foo(
... )
>>> foo.x
<bound method Foo.x of <__main__.Foo object at 0x027D5D50>>
>>> foo.x()
10
>>> class Foo2(object):
... def x(self):
... return 10
... x = property(x)
...
>>> foo2 = Foo2()
>>> foo2.x
10
>>> class Foo3(object):
... @property
... def x(self):
... return 10
...
>>>
>>> foo3 = Foo3()
>>> foo3.x
10
def print_function(func):
def wrapper(*args, **kw):
print 'before'
res = func(*args, **kw)
print 'after'
return res
return wrapper
@print_function
def ham(x):
print 'ham:',x
ham(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment