提交 28cbc819 authored 作者: Steven Pigeon's avatar Steven Pigeon

fixed pep8 issues

上级 e43983b8
...@@ -31,17 +31,17 @@ if necessary): ...@@ -31,17 +31,17 @@ if necessary):
import sys import sys
def show_sizeof(x,level=0): def show_sizeof(x, level=0):
print "\t"*level,x.__class__, sys.getsizeof(x), x print "\t"*level, x.__class__, sys.getsizeof(x), x
if hasattr(x,'__iter__'): if hasattr(x, '__iter__'):
if hasattr(x,'items'): if hasattr(x, 'items'):
for xx in x.items(): for xx in x.items():
show_sizeof(xx,level+1) show_sizeof(xx, level + 1)
else: else:
for xx in x: for xx in x:
show_sizeof(xx,level+1) show_sizeof(xx, level + 1)
We can now use the function to inspect the sizes of the different basic We can now use the function to inspect the sizes of the different basic
data types: data types:
...@@ -139,7 +139,7 @@ heterogeneous. Let us look at our sizes: ...@@ -139,7 +139,7 @@ heterogeneous. Let us look at our sizes:
:: ::
show_sizeof([]) show_sizeof([])
show_sizeof([4,"toaster",230.1,]) show_sizeof([4, "toaster", 230.1])
outputs outputs
...@@ -162,7 +162,7 @@ about tuples? (and dictionaries?): ...@@ -162,7 +162,7 @@ about tuples? (and dictionaries?):
:: ::
show_sizeof({}) show_sizeof({})
show_sizeof({'a':213,'b':2131}) show_sizeof({'a':213, 'b':2131})
outputs, on a 32-bits box outputs, on a 32-bits box
...@@ -288,11 +288,11 @@ program (it makes my point entirely): ...@@ -288,11 +288,11 @@ program (it makes my point entirely):
:: ::
port copy, memory_profiler import copy, memory_profiler
@profile @profile
def function(): def function():
x=range(1000000) # allocate a big list x=range(1000000) # allocate a big list
y=copy.deepcopy(x) y=copy.deepcopy(x)
del x del x
return y return y
...@@ -428,23 +428,23 @@ A naïve implementation would give: ...@@ -428,23 +428,23 @@ A naïve implementation would give:
import memory_profiler, random, pickle import memory_profiler, random, pickle
def random_string(): def random_string():
return "".join([ chr(64+random.randint(0,25)) for _ in xrange(20) ]) return "".join([ chr(64+random.randint(0, 25)) for _ in xrange(20) ])
@profile @profile
def create_file(): def create_file():
x=[ (random.random(), x=[ (random.random(),
random_string(), random_string(),
random.randint(0,2**64)) random.randint(0, 2**64))
for _ in xrange(1000000) ] for _ in xrange(1000000) ]
f=open('machin.flat','w') f=open('machin.flat', 'w')
for xx in x: for xx in x:
print >>f, xx print >>f, xx
@profile @profile
def load_file(): def load_file():
y=[] y=[]
f=open('machin.flat','r') f=open('machin.flat', 'r')
for line in f: for line in f:
y.append(eval(line)) y.append(eval(line))
return y return y
...@@ -465,10 +465,10 @@ Creating the file: ...@@ -465,10 +465,10 @@ Creating the file:
9 9.19 MB 0.00 MB def create_file(): 9 9.19 MB 0.00 MB def create_file():
10 9.34 MB 0.15 MB x=[ (random.random(), 10 9.34 MB 0.15 MB x=[ (random.random(),
11 random_string(), 11 random_string(),
12 random.randint(0,2**64)) 12 random.randint(0, 2**64))
13 246.09 MB 236.75 MB for _ in xrange(1000000) ] 13 246.09 MB 236.75 MB for _ in xrange(1000000) ]
14 14
15 246.09 MB 0.00 MB f=open('machin.flat','w') 15 246.09 MB 0.00 MB f=open('machin.flat', 'w')
16 308.27 MB 62.18 MB for xx in x: 16 308.27 MB 62.18 MB for xx in x:
17 print >>f, xx 17 print >>f, xx
...@@ -483,7 +483,7 @@ and reading the file back: ...@@ -483,7 +483,7 @@ and reading the file back:
20 @profile 20 @profile
21 9.19 MB 0.00 MB def load_file(): 21 9.19 MB 0.00 MB def load_file():
22 9.34 MB 0.15 MB y=[] 22 9.34 MB 0.15 MB y=[]
23 9.34 MB 0.00 MB f=open('machin.flat','r') 23 9.34 MB 0.00 MB f=open('machin.flat', 'r')
24 300.99 MB 291.66 MB for line in f: 24 300.99 MB 291.66 MB for line in f:
25 300.99 MB 0.00 MB y.append(eval(line)) 25 300.99 MB 0.00 MB y.append(eval(line))
26 301.00 MB 0.00 MB return y 26 301.00 MB 0.00 MB return y
...@@ -501,8 +501,9 @@ whole data. Using pickle, you would allocate the whole data (at least) ...@@ -501,8 +501,9 @@ whole data. Using pickle, you would allocate the whole data (at least)
twice: once by pickle, and once through Numpy. twice: once by pickle, and once through Numpy.
Or even better yet: use Numpy (or PyTables) arrays. But that's a different Or even better yet: use Numpy (or PyTables) arrays. But that's a different
topic that is discussed in 'loading and saving' another tutorial in the topic. In the mean time, you can have a look at `loading and saving
Theano/doc/tutorial directory. <https://github.com/Theano/Theano/blob/master/doc/tutorial/loading_and_saving.txt>`_
another tutorial in the Theano/doc/tutorial directory.
\* \*
\* \* \* \*
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论