Skip to content

Commit b4e6b17

Browse files
committed
2 parents 0961efe + 7f8051b commit b4e6b17

File tree

81 files changed

+19830
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+19830
-0
lines changed

_downloads/CircularReferenceExample.ipynb

Lines changed: 879 additions & 0 deletions
Large diffs are not rendered by default.

_downloads/add_book_data.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
sample data for persistence/serializatiion examples
5+
6+
This version is nested, with more stucture
7+
- can be saved with pickle, JSON, xml...
8+
"""
9+
10+
AddressBook = [ {'first_name': "Chris",
11+
'last_name': "Barker",
12+
'address' : {'line_1':"835 NE 33rd St",
13+
'line_2' : "",
14+
'city' : "Seattle",
15+
'state': "WA",
16+
'zip': "96543"},
17+
'email' : "PythonCHB@gmail.com",
18+
'home_phone' : "206-555-1234",
19+
'office_phone' : "123-456-7890",
20+
'cell_phone' : "234-567-8901",
21+
},
22+
23+
{'first_name': "Fred",
24+
'last_name': "Jones",
25+
'address' : {'line_1':"123 SE 13th St",
26+
'line_2' : "Apt. 43",
27+
'city' : "Tacoma",
28+
'state': "WA",
29+
'zip': "93465"},
30+
'email' : "FredJones@some_company.com",
31+
'home_phone' : "510-555-1234",
32+
'office_phone' : "564-466-7990",
33+
'cell_phone' : "403-561-8911",
34+
},
35+
36+
{'first_name': "Nancy",
37+
'last_name': "Wilson",
38+
'address' : {'line_1':"8654 Walnut St",
39+
'line_2' : "Suite 567",
40+
'city' : "Pasadena",
41+
'state': "CA",
42+
'zip': "12345"},
43+
'email' : "Wilson.Nancy@gmail.com",
44+
'home_phone' : "423-321-9876",
45+
'office_phone' : "123-765-9877",
46+
'cell_phone' : "432-567-8466",
47+
},
48+
]
49+

_downloads/add_book_data_flat.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
sample data for persistence/serialization examples
5+
this version is flat for saving in CSV, ini, etc.
6+
"""
7+
8+
AddressBook = [ {'first_name': "Chris",
9+
'last_name': "Barker",
10+
'address_line_1':"835 NE 33rd St",
11+
'address_line_2' : "",
12+
'address_city' : "Seattle",
13+
'address_state': "WA",
14+
'address_zip': "96543",
15+
'email' : "PythonCHB@gmail.com",
16+
'home_phone' : "206-555-1234",
17+
'office_phone' : "123-456-7890",
18+
'cell_phone' : "234-567-8901",
19+
},
20+
21+
{'first_name': "Fred",
22+
'last_name': "Jones",
23+
'address_line_1':"123 SE 13th St",
24+
'address_line_2' : "Apt. 43",
25+
'address_city' : "Tacoma",
26+
'address_state': "WA",
27+
'address_zip': "93465",
28+
'email' : "FredJones@some_company.com",
29+
'home_phone' : "510-555-1234",
30+
'office_phone' : "564-466-7990",
31+
'cell_phone' : "403-561-8911",
32+
},
33+
34+
{'first_name': "Nancy",
35+
'last_name': "Wilson",
36+
'address_line_1':"8654 Walnut St",
37+
'address_line_2' : "Suite 567",
38+
'address_city' : "Pasadena",
39+
'address_state': "CA",
40+
'address_zip': "12345",
41+
'email' : "Wilson.Nancy@gmail.com",
42+
'home_phone' : "423-321-9876",
43+
'office_phone' : "123-765-9877",
44+
'cell_phone' : "432-567-8466",
45+
},
46+
]
47+

_downloads/circular.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import sys
5+
import weakref
6+
import gc
7+
8+
deleted_object_messages = []
9+
10+
11+
class MyChild(object):
12+
def __init__(self, parent):
13+
self.parent = parent
14+
#self.parent = weakref.ref(parent)
15+
#self.parent = weakref.proxy(parent)
16+
17+
## store some data so it will use appreciable memory
18+
## multiply by 1234 to avoid interning
19+
self.data = [1234*i for i in range(100000)]
20+
21+
def __del__(self):
22+
deleted_object_messages.append( ('MyChild deleted', id(self)) )
23+
24+
25+
class MyParent(object):
26+
def __init__(self):
27+
self.children = []
28+
def addChild(self):
29+
child = MyChild(self)
30+
self.children.append(child)
31+
return child
32+
def __del__(self):
33+
deleted_object_messages.append( ('MyParent deleted', id(self)) )
34+
35+
if __name__ == "__main__":
36+
37+
p = MyParent()
38+
39+
print "refcount for p:", sys.getrefcount(p)
40+
assert sys.getrefcount(p) == 2
41+
42+
a = p.addChild()
43+
a2 = p.addChild()
44+
print "refcount for p after adding an two children:", sys.getrefcount(p)
45+
assert sys.getrefcount(p) == 2
46+
47+
print "p's children:", p.children
48+
assert len(p.children) == 2
49+
50+
print " a is:", a
51+
print "a's parent:", a.parent
52+
print "a's parent's children:", a.parent.children
53+
54+
assert a is a.parent.children[0]
55+
assert a2 is a.parent.children[1]
56+
57+
58+
print "a's refcount:", sys.getrefcount(a)
59+
assert sys.getrefcount(a) == 3
60+
61+
print "a2's refcount:", sys.getrefcount(a2)
62+
assert sys.getrefcount(a2) == 3
63+
64+
del p
65+
print "after deleting p:"
66+
67+
print "a's refcount:", sys.getrefcount(a)
68+
assert sys.getrefcount(a) == 2
69+
70+
print "a2's refcount:", sys.getrefcount(a2)
71+
assert sys.getrefcount(a2) == 2
72+
73+
print "deleting a:"
74+
id_a = id(a)
75+
del a
76+
print deleted_object_messages
77+
assert deleted_object_messages[-1][0] == 'MyChild deleted'
78+
assert deleted_object_messages[-1][1] == id_a
79+
80+
print "deleting a2:"
81+
id_a2 = id(a2)
82+
del a2
83+
print deleted_object_messages
84+
assert deleted_object_messages[-1][0] == 'MyChild deleted'
85+
assert deleted_object_messages[-1][1] == id_a2
86+
87+
88+
89+

_downloads/mem_check.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
mem_check.py
3+
4+
functions for getting memoroy use of the current python process
5+
6+
Windows and *nix versions
7+
8+
USAGE:
9+
10+
amount = get_mem_use(units='MB') # options are KB, MB, GB
11+
12+
"""
13+
14+
import sys
15+
16+
div = {'GB': 1024*1024*1024,
17+
'MB': 1024*1024,
18+
'KB': 1024,
19+
}
20+
21+
if sys.platform.startswith('win'):
22+
23+
"""
24+
25+
Functions for getting memory usage of Windows processes.
26+
27+
from:
28+
29+
http://code.activestate.com/recipes/578513-get-memory-usage-of-windows-processes-using-getpro/
30+
31+
get_mem_use(units='MB') is the one to get memory use for the current process.
32+
33+
34+
"""
35+
import ctypes
36+
from ctypes import wintypes
37+
38+
GetCurrentProcess = ctypes.windll.kernel32.GetCurrentProcess
39+
GetCurrentProcess.argtypes = []
40+
GetCurrentProcess.restype = wintypes.HANDLE
41+
42+
SIZE_T = ctypes.c_size_t
43+
44+
class PROCESS_MEMORY_COUNTERS_EX(ctypes.Structure):
45+
_fields_ = [
46+
('cb', wintypes.DWORD),
47+
('PageFaultCount', wintypes.DWORD),
48+
('PeakWorkingSetSize', SIZE_T),
49+
('WorkingSetSize', SIZE_T),
50+
('QuotaPeakPagedPoolUsage', SIZE_T),
51+
('QuotaPagedPoolUsage', SIZE_T),
52+
('QuotaPeakNonPagedPoolUsage', SIZE_T),
53+
('QuotaNonPagedPoolUsage', SIZE_T),
54+
('PagefileUsage', SIZE_T),
55+
('PeakPagefileUsage', SIZE_T),
56+
('PrivateUsage', SIZE_T),
57+
]
58+
59+
GetProcessMemoryInfo = ctypes.windll.psapi.GetProcessMemoryInfo
60+
GetProcessMemoryInfo.argtypes = [
61+
wintypes.HANDLE,
62+
ctypes.POINTER(PROCESS_MEMORY_COUNTERS_EX),
63+
wintypes.DWORD,
64+
]
65+
GetProcessMemoryInfo.restype = wintypes.BOOL
66+
67+
def get_current_process():
68+
"""Return handle to current process."""
69+
return GetCurrentProcess()
70+
71+
def get_memory_info(process=None):
72+
"""Return Win32 process memory counters structure as a dict."""
73+
if process is None:
74+
process = get_current_process()
75+
counters = PROCESS_MEMORY_COUNTERS_EX()
76+
ret = GetProcessMemoryInfo(process, ctypes.byref(counters),
77+
ctypes.sizeof(counters))
78+
if not ret:
79+
raise ctypes.WinError()
80+
info = dict((name, getattr(counters, name))
81+
for name, _ in counters._fields_)
82+
return info
83+
84+
def get_mem_use(units='MB'):
85+
"""
86+
returns the total memory use of the current python process
87+
88+
:param units='MB': the units you want the reslut in. Options are:
89+
'GB', 'MB', 'KB'
90+
"""
91+
info = get_memory_info()
92+
return info['PrivateUsage'] / float(div[units])
93+
94+
95+
else: # for posix systems only tested on OS-X for now
96+
def get_mem_use(units='MB'):
97+
"""
98+
returns the total memory use of the current python process
99+
100+
:param units='MB': the units you want the reslut in. Options are:
101+
'GB', 'MB', 'KB'
102+
"""
103+
import resource
104+
#useage = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
105+
div = {'GB': 1024*1024*1024,
106+
'MB': 1024*1024,
107+
'KB': 1024,
108+
}
109+
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / float(div[units])

_downloads/memcount.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
a simple test of a circular reference that gc.collect() will not clean up.
6+
7+
And watch the memory grow!
8+
9+
"""
10+
11+
12+
import sys
13+
import gc
14+
import weakref
15+
16+
import mem_check
17+
18+
19+
class MyChild(object):
20+
def __init__(self, parent):
21+
#self.parent = parent
22+
# if a weak ref is used, then no memory leak.
23+
self.parent = weakref.proxy(parent)
24+
25+
## store some data so it will use appreciable memory
26+
## multiply by 1234 to reduce interning
27+
self.data = [1234*i for i in range(100000)]
28+
29+
def __del__(self):
30+
""" __del__ defined to defeat GC"""
31+
print 'MyChild deleted', id(self)
32+
33+
34+
class MyParent(object):
35+
def __init__(self):
36+
self.children = []
37+
def addChild(self):
38+
child = MyChild(self)
39+
self.children.append(child)
40+
return child
41+
def __del__(self):
42+
""" __del__ defined to defeat GC"""
43+
print 'MyParent deleted', id(self)
44+
45+
46+
if __name__ == "__main__":
47+
48+
# create a bunch in a loop:
49+
for i in range(50):
50+
print "iteration:", i
51+
p = MyParent()
52+
p.addChild()
53+
p.addChild()
54+
p.addChild()
55+
print "ref count:", sys.getrefcount(p)
56+
print "mem_use: %f MB"%mem_check.get_mem_use()
57+
del p
58+
print "collected", gc.collect()

_downloads/simple_circular.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Simple contrived example of circular reference counting
5+
"""
6+
7+
import sys
8+
9+
l1 = [1,]
10+
l2 = [2,]
11+
12+
l1_id = id(l1)
13+
l2_id = id(l2)
14+
15+
print "initial ref counts:"
16+
print "l1:", sys.getrefcount(l1)
17+
print "l2:", sys.getrefcount(l2)
18+
19+
print "now add the circular references:"
20+
l1.append(l2)
21+
l2.append(l1)
22+
23+
print "ref counts after the circular reference is added"
24+
print "l1:", l1, "refcount:", sys.getrefcount(l1)
25+
print "l2:", l2, "refcount:", sys.getrefcount(l2)
26+
27+
print "delete one"
28+
del l1
29+
print "l2:", sys.getrefcount(l2)
30+
31+
print "delete the other"
32+
del l2
33+
34+
print "but are they still there???"
35+
36+
37+
38+
39+

0 commit comments

Comments
 (0)