-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamedTuples.py
More file actions
executable file
·186 lines (132 loc) · 3.38 KB
/
NamedTuples.py
File metadata and controls
executable file
·186 lines (132 loc) · 3.38 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
180
181
182
183
184
185
186
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Mar 10 17:53:11 2021
@author: maherme
"""
#%%
class Point3D:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
from collections import namedtuple
Point2D = namedtuple('Point2D', ['x', 'y'])
pt1 = Point2D(10, 20)
print(pt1)
pt3d_1 = Point3D(10, 20, 30)
# As you will see pt3d_1 representation does not look like pt1, we need to
# add more code to the class Point3D to fix this.
print(pt3d_1)
#%%
# You can also create an instance giving the parameter names:
p = Point3D(x=10, y=20, z=30)
print(p)
print(isinstance(p, tuple))
p = Point2D(x=10, y=20)
print(p)
print(isinstance(p, tuple))
#%%
pt1 = Point2D(10, 20)
pt2 = Point2D(10, 20)
print(pt1 is pt2)
print(pt1 == pt2)
pt1 = Point3D(10, 20, 30)
pt2 = Point3D(10, 20, 30)
print(pt1 is pt2)
print(pt1 == pt2) # As Point3D is a class, the method eq need to be implemented
#%%
class Point3D:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return f'{self.__class__.__name__}(x={self.x}, y={self.y})'
def __eq__(self, other):
if isinstance(other, Point3D):
return self.x == other.x and self.y == other.y and self.z == other.z
else:
return False
#%%
pt1 = Point3D(10, 20, 30)
print(pt1)
pt2 = Point3D(10, 20, 30)
print(pt2)
print(pt1 is pt2)
print(pt1 == pt2)
#%%
# Let try to find the maximum coordinate:
pt1 = Point2D(10, 20)
pt2 = Point3D(10, 20, 30)
max(pt1)
max(pt2) # This will fail because is not iterable, is not a tuple.
#%%
# Let implement a dot product.
def dot_product_3d(a, b):
return a.x * b.x + a.y * b.y + a.z * b.z
pt1 = Point3D(1, 2, 3)
pt2 = Point3D(1, 1, 1)
print(dot_product_3d(pt1, pt2))
#%%
# Implement for 2 coordinates.
a = (1, 2)
b = (1, 1)
list(zip(a, b))
sum(e[0] * e[1] for e in zip(a, b))
#%%
# Implement using a function.
def dot_product(a, b):
return sum(e[0] * e[1] for e in zip(a, b))
print(dot_product(a, b))
pt1 = Point2D(1, 2)
pt2 = Point2D(1, 1)
print(dot_product(pt1, pt2))
#%%
Vector3D = namedtuple('Vector3D', 'x y z')
v1 = Vector3D(1, 2, 3)
v2 = Vector3D(1, 1, 1)
print(dot_product(v1, v2))
#%%
# v1 is a tuple indeed:
print(tuple(v1))
print(v1[0])
print(v1[0:2])
print(v1)
# This is the advantage of a named tuple, you can choose if get a component as
# a tuple or as a class
print(v1.x)
print(v1.y)
#%%
# You can use as many spaces as you want to create the named tuple:
Circle = namedtuple('Circle', 'center_x center_y radius')
c = Circle(0, 0, 10)
print(c)
print(c.radius)
#%%
# Let define a named tuple:
Stock = namedtuple('Stock', 'symbol year month day open high low close')
djia = Stock('DJIA', 2018, 1, 25, 26_323, 26_458, 26_260, 26_393)
# You can access as a class:
print(djia)
print(djia.close)
# You can iterate:
for item in djia:
print(item)
# You can unpack:
symbol, year, month, day, *_, close = djia
print(symbol, year, month, day, close)
#%%
# You can not use name starting with an underscore:
Person = namedtuple('Person', 'name age _ssn')
#%%
# You can fix this set rename = True:
Person = namedtuple('Person', 'name age _ssn', rename = True)
print(Person._fields) # Notice _ssn is replaced by _2
#%%
# You also can create a dictionary using:
d = djia._asdict()
print(d)
print(d['symbol'])
print(d['close'])
#%%