-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_lookups.py
More file actions
179 lines (132 loc) · 3.64 KB
/
Copy pathtest_lookups.py
File metadata and controls
179 lines (132 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
""" Speed performance tests.
Copyright (c) 2014, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further information on copyrights,
or contact the author. All Rights Reserved.
License: MIT
"""
loops = range(1000)
### Variable lookups
global_a = 'global'
def variable_lookup_global():
for i in loops:
x = global_a
return x
assert variable_lookup_global() == global_a
def variable_lookup_local():
local_a = 'local'
for i in loops:
x = local_a
return x
assert variable_lookup_local() == 'local'
### Localized global variables
def variable_lookup_function_with_locals():
local_a = global_a
def f(a, b, c):
x = a
y = b
return y
for i in loops:
x = f(1, 2, 3)
return x
assert variable_lookup_function_with_locals() == 2
def variable_lookup_function_with_global():
local_a = global_a
def f(a, b, c):
x = local_a
y = local_a
return y
for i in loops:
x = f(1, 2, 3)
return x
assert variable_lookup_function_with_global() == global_a
def variable_lookup_function_with_kwargs_global():
def f(a, b, c, local_a=global_a):
x = local_a
y = local_a
return y
for i in loops:
x = f(1, 2, 3)
return x
assert variable_lookup_function_with_kwargs_global() == global_a
def variable_lookup_function_with_localized_global():
def f(a, b, c):
local_a = global_a
x = local_a
y = local_a
return y
for i in loops:
x = f(1, 2, 3)
return x
assert variable_lookup_function_with_localized_global() == global_a
### Attribute lookups
class AttributeClass:
class_attribute = 3
instance_attribute = None
def __init__(self):
self.instance_attribute = 4
attribute_class_instance = AttributeClass()
def attribute_lookup_instance():
o = attribute_class_instance
for i in loops:
x = o.instance_attribute
return x
assert attribute_lookup_instance() == 4
def attribute_lookup_class():
o = attribute_class_instance
for i in loops:
x = o.class_attribute
return x
assert attribute_lookup_class() == 3
### Attribute lookups on new style classes
class AttributeObject(object):
class_attribute = 3
instance_attribute = None
def __init__(self):
self.instance_attribute = 4
attribute_object_instance = AttributeObject()
def attribute_lookup_object_instance():
o = attribute_object_instance
for i in loops:
x = o.instance_attribute
return x
assert attribute_lookup_object_instance() == 4
def attribute_lookup_object_class():
o = attribute_object_instance
for i in loops:
x = o.class_attribute
return x
assert attribute_lookup_object_class() == 3
### Slot lookups
class SlotClass(object):
readonly_slot = 3 # read-only
#readwrite_slot read/write
__slots__ = ('readonly_slot', 'readwrite_slot')
def __init__(self):
self.readwrite_slot = 4
slot_class_instance = SlotClass()
try:
SlotClass().readonly_slot = 99
except AttributeError:
pass
else:
raise AssertionError('slot not read-only')
try:
SlotClass().readwrite_slot = 99
except TypeError:
raise AssertionError('slot not read-write')
def slot_lookup_readwrite():
o = slot_class_instance
for i in loops:
x = o.readwrite_slot
return x
assert slot_lookup_readwrite() == 4
def slot_lookup_readonly():
o = slot_class_instance
for i in loops:
x = o.readonly_slot
return x
assert slot_lookup_readonly() == 3
###
if __name__ == '__main__':
import perftools
perftools.time_functions(globals())