Skip to content

Commit 4ca0d93

Browse files
committed
little change
1 parent f9afbb0 commit 4ca0d93

3 files changed

Lines changed: 89 additions & 6 deletions

File tree

Work/05_chap.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,60 @@
3232

3333
s1 = stock.Stock('gg',12,45)
3434

35-
s1.__dict__
35+
s1.__dict__
36+
37+
'''
38+
#### 5.2 Classes and Encapsulation
39+
'''
40+
# public vs private variable
41+
# any attribute with leading _ is private in python but only as style. In python everything is visible.
42+
class person:
43+
def __init__(self) -> None:
44+
self._name=0
45+
46+
# one can defie accessor methods as like c++, C#; and raise a typeerror exception...
47+
48+
## defining Properties @property
49+
class XStock:
50+
def __init__(self,nm,sh,pr):
51+
self.name=nm
52+
self.shares=sh
53+
self.price = pr
54+
@property
55+
def shares(self):
56+
return self._shares # note -- property uses private name with _
57+
@shares.setter
58+
def shares(self,value):
59+
if not isinstance(value,int):
60+
raise TypeError('expected int')
61+
self.shares = value
62+
63+
64+
s1 = XStock('gg',12,343.1)
65+
s1.name
66+
s1.shares
67+
s1.gox =20
68+
s1.gox
69+
70+
## using __slots__ to fix set of attributes belonging to an object; without this, attribute can be added to an object at run time
71+
72+
class YStock:
73+
__slots__ =('name','_shares','price')
74+
def __init__(self,nm,sh,pr):
75+
self.name=nm
76+
self._shares=sh
77+
self.price = pr
78+
@property
79+
def shares(self):
80+
return self._shares # note -- property uses private name with _
81+
@shares.setter
82+
def shares(self,value):
83+
if not isinstance(value,int):
84+
raise TypeError('expected int')
85+
self.shares = value
86+
87+
s2 = YStock('gg',12,343.1)
88+
s2.name
89+
s2.shares
90+
#s2.gox =20 # uncomment - it should fail bocz of slots
91+
s2.gox

Work/06_chap.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
6. Generators
3+
'''
4+
## 6.1 Iteration Protocol
5+
6+
a = 'hello'
7+
for c in a: # Loop over characters in a
8+
print(c)
9+
10+
b = { 'name': 'Dave', 'password':'foo'}
11+
for k in b: # Loop over keys in dictionary
12+
print(k)
13+
for k,v in b.items(): # Loop over keys and values in dictionary
14+
print(k,v)
15+
16+
17+
## using hash function
18+
#https://stackoverflow.com/questions/4567089/hash-function-that-produces-short-hashes
19+
import hashlib
20+
import base64
21+
plaintext1 ="long_namexyz_1234567892"
22+
hash_result1 = hashlib.sha256(plaintext1.encode()).digest()[:16]
23+
hash_result1
24+
hash_result2 = hashlib.shake_256(plaintext1.encode()).hexdigest(4)
25+
hash_result2
26+
encoded_result = base64.b64decode(hash_result1).decode('UTF-8') #base64.b64encode(hash_result1).decode("utf-8")
27+
encoded_result
28+
29+
encoded_result1 = encoded_result.decode("UTF-8")
30+

Work/meslog.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ def ReadMesLogs(mesfile):
3232
for line in f:
3333
#hdr = f.readline()
3434
strs = line.split()
35-
if len(strs) >=4 and strs[0] != '.' and strs[1] != '.' and strs[2] != '.' and strs[3] != '.':
36-
id = strs[0]
37-
seg =int(strs[1])
38-
nsec =int(strs[2])
39-
wgt = float(strs[3])
35+
if len(strs) >=4 and strs[0] != '.' and strs[1] != '.' and strs[2] != '.' and strs[3] != '.':
36+
id,seg,nsec,wgt = strs[0],int(strs[1]),int(strs[2]),float(strs[3])
4037
i=0
4138
secs = []
4239
while i < nsec: # read all section for the log

0 commit comments

Comments
 (0)