Skip to content

Commit 8daebca

Browse files
committed
latest
1 parent 1602d56 commit 8daebca

12 files changed

Lines changed: 692 additions & 87 deletions

File tree

KopecBook-Excercise/FibonacciSeq

Whitespace-only changes.

Work/04_chap.py

Lines changed: 104 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ def __init__(self, x, y): # like c++ constructor
99
self.x = x
1010
self.y = y
1111
self.health = 100
12-
12+
'''
1313
def __init__(self): # check about default constructor
1414
self.x =0
1515
self.y=0
16+
'''
1617
def move(self, dx, dy): # self is the instance or similar to this pointer in c++
1718
self.x += dx
1819
self.y += dy
19-
20+
2021
def damage(self, pts):
2122
self.health -= pts
2223

@@ -59,6 +60,25 @@ def damage(self, pts):
5960
a = Stock('goo',11,22.22)
6061
b = Stock('uuu',66,77.23)
6162

63+
#-------------------------------------------
64+
# importing Tlogs module and printing....
65+
import Work.meslog as meslog
66+
dataDir = os.getcwd()
67+
dataDir += r'\work\Data\meslogs.txt'
68+
mylogs = meslog.ReadMesLogs(dataDir)
69+
70+
71+
for log in mylogs:
72+
#print(log.Id)
73+
#str(log)
74+
for sec in log.sections:
75+
#print(sec.z,sec.x,sec.y,sec.d)
76+
print(str(sec))
77+
print('---------------')
78+
79+
80+
#----------------------------------
81+
6282
stc = [a,b]
6383
stc
6484
for s in stc:
@@ -101,28 +121,94 @@ def damage(self, pts):
101121
#Exercise 4.4: Using your class
102122
# inside report.py
103123

104-
''' 4.2 inheritance
105-
class Parent:
106-
...
107124

108-
class Child(Parent):
109-
>>> method overriding can be done
110-
using "super" to call the base class method
111125

112-
class Stock:
126+
'''
127+
########### 4.2 inheritance
128+
class Parent:
129+
130+
131+
class Child(Parent):
132+
>>> with Inheritance
133+
Extending : With inheritance, you are taking an existing class and:
134+
Adding new methods
135+
Redefining some of the existing methods
136+
Adding new attributes to instances
137+
138+
>>> method overriding can be done
139+
using "super" to call the base class method
140+
141+
class Stock:
142+
...
143+
def cost(self):
144+
return self.shares * self.price
145+
...
146+
class MyStock(Stock):
147+
def cost(self):
148+
# Check the call to `super`
149+
actual_cost = super().cost() ## using super ...
150+
return 1.25 * actual_cost
151+
152+
>>> if __init__ is redifined in derived class, it is essential to initialize parent
153+
call the __init__() method on the super which is the way to call the base version as shown previously.
154+
>>> object as base class - if a class has no parent, object can be used as a base class. "object" is the parent of all objects in Python
155+
>>> Multiple inheritance
156+
class Mother:
113157
...
114-
def cost(self):
115-
return self.shares * self.price
158+
class Father:
116159
...
160+
class child (Mother, Father):
161+
'''
162+
163+
'''
164+
Exercise 4.5: An Extensibility Problem
165+
source file tableformat.py
166+
abstract class; only declaration for extensibility
167+
Exercise 4.7: Polymorphism in Action
168+
169+
'''
170+
171+
from stock import Stock,MyStock
172+
173+
ms = MyStock(20,120,2)
174+
print(ms.cost())
175+
176+
s2 = ms.cost
177+
s2
178+
s2()
179+
180+
'''
181+
4.3 Special Methods
182+
-------------------
183+
Classes may define special methods which are treated special way by the python interpreter.
184+
Special methods are always preceded by __; eg: __init(self):
185+
'''
186+
from datetime import date
187+
d = date(2024,11,12)
188+
print(d)
189+
190+
str("str")
191+
str(d)
192+
repr(d) # __repr__
117193

118-
class MyStock(Stock):
119-
def cost(self):
120-
# Check the call to `super`
121-
actual_cost = super().cost() ## using super ...
122-
return 1.25 * actual_cost
194+
#todo:: try using special function str and __len__; __getitem__()
123195

124-
>>> if __init__ is redifined in derived class, it is essential to initialize parent
125-
call the __init__() method on the super which is the way to call the previous version as shown previously.
196+
# lookup and bound methods
197+
lg1 = meslog.TSec(2,3,4,5)
198+
lg1
199+
lg1()
126200

201+
## Attribute Access
127202
'''
203+
property getter and setter
204+
'''
205+
206+
###### 4.4 Defining Exceptions
207+
208+
class NetworkError(Exception):
209+
pass
210+
211+
class AuthenticationError(NetworkError):
212+
pass
213+
128214

Work/0Scratch.py

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,16 @@
22
'''
33
take input from console upto 10 lines
44
'''
5-
print('test')
6-
5+
print("read a csv file and pring to another file")
76
import os
8-
workdir = os.getcwd()
9-
workdir += r'\work'
10-
datadir = workdir + r'\data'
11-
12-
with open(datadir+r'\1idata.txt','w+') as ifs:
13-
i=0
14-
lines=[]
15-
ifs.write('#header for the test file\n')
16-
while i <5:
17-
line =input(f'Enter line {i:2d}:')
18-
i+=1
19-
lines.append(line)
20-
ifs.write(line + '\n')
21-
ifs.write('# End header for the test file\n')
22-
23-
with open(datadir+r'\1idata.txt','rt') as ifs, open(datadir+r'\1odata.txt','w+') as ofs:
24-
for line in ifs:
25-
print(line)
26-
next(ifs)
27-
ofs.write(line)
28-
29-
print('done temp printing stuff')
307

31-
for line in lines:
32-
print(line)
8+
fpath = os.getcwd()
9+
fpath += r'\work\Data\foo.txt'
3310

34-
with open(datadir+r'\1odata.txt','w+') as ifs:
35-
for line in ifs:
11+
with open(fpath,'r') as f:
12+
data =f.read()
13+
print(data)
14+
f.seek(0)
15+
for line in f:
3616
print(line)
17+
print('---------------\n')

Work/Data/1idata.txt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#header for the test file
2-
void setString(std::string_view name, const String & value);
3-
String getString(std::string_view name) const;
4-
bool isChanged(std::string_view name) const;
5-
SettingsChanges changes() const;
6-
void applyChange(const SettingChange & change);
2+
with open(datadir+r'\1idata.txt','rt') as ifs, open(datadir+r'\1odata.txt','w+') as ofs:
3+
for line in ifs:
4+
print(line)
5+
next(ifs)
6+
ofs.write(line)
77
# End header for the test file

Work/Data/1odata.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
#header for the test file
2-
String getString(std::string_view name) const;
3-
SettingsChanges changes() const;

Work/Data/bar.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# this is foo.txt file
22

33
Why learning python seems so straightforward. Is this becuase I already know C based languages or is it the way python language is?
4-
I still wonder that till now, things have been quite simple and when I start writing long complex programs I may realize the issues.
4+
55
What about writing a very large application like simulation? What about integrating with OS based frameworks? What about rich UI design?
6-
What about platform SDK integration? What about code management?
6+
77
What about design patterns in python?
8-
What about working on a single file with other team members?
8+
99
Is there a proven application like C++, .Net in python which exists?
10-
------------end -----------
10+

0 commit comments

Comments
 (0)