I wondered how can I print the class contents of a particular index. I made a class that has all the values of a certain seismic movement and stores each in its own data type.
Here is the class:
import re
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def date(self):
return self._date
def time(self):
return self._time
def latit(self):
return self._latit
def long(self):
return self._long
def depth(self):
return self._depth
def md(self):
return self._md
def ml(self):
return self._ml
def mw(self):
return self._mw
def region(self):
return self._region
def method(self):
return self._method
# This does not work
def __str__(self):
return ('MAG: ' float(self.ml()) ' DEPTH: ' int(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' float(self.latit()) ' LON: ' float(self.long()))
result = [('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')]
print(Txt_data(result))
I was trying to print the data using str method but it doesn't work.
Here is the error:
Traceback (most recent call last):
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 73, in <module>
print(Txt_data(result))
File "/Users/seyfalsultanov/Documents/uni comp 100/comp100-2021f-ps5-seyfalku/main.py", line 60, in __str__
print('MAG: ' float(self.ml()) ' DEPTH: ' int(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' float(self.latit()) ' LON: ' float(self.long()))
AttributeError: Txt_data instance has no __call__ method
My question is how to print the string i tried to print using str method in the class. Thanks very much beforehand.
CodePudding user response:
Your immediate problem is that you shadowed all your methods with instance attributes, instead of using _
-prefixed names for the attributes that the method expect.
def __init__(self, result):
self._date = result[0]
self._time = result[1]
self._latit = result[2]
self._long = result[3]
self._depth = result[4]
self._md = result[5]
self._ml = result[6]
self._mw = result[7]
self._region = result[8]
self._method = result[9]
However, none of those getters are necessary; just use the instance attributes directly. In __str__
, use an f-string to perform any necessary type conversions (which you are currently doing wrong; non-str
values need to be converted to str
values, not the other way around).
class Txt_data:
def __init__(self, result):
self.date = result[0]
self.time = result[1]
self.latit = result[2]
self.long = result[3]
self.depth = result[4]
self.md = result[5]
self.ml = result[6]
self.mw = result[7]
self.region = result[8]
self.method = result[9]
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'
result = ('2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick')
print(Txt_data(result))
Finally, I would recommend making __init__
not be responsible for splitting up a list. Have it simply take 10 different arguments, and use a dedicated class method to parse a list in a fixed format.
class Txt_data:
def __init__(self, date, time, latitude, long, depth, md, ml, mw, region, method):
self.date = date
self.time = time
self.latit = latit
self.long = long
self.depth = depth
self.md = md
self.ml = ml
self.mw = mw
self.region = region
self.method = method
@classmethod
def from_list(cls, x):
if len(x) != 10:
raise ValueError("Wrong number of elements in list")
return cls(*x)
def __str__(self):
return f'MAG: {self.ml} DEPTH: {self.region} DATE/TIME: {self.date} {self.time} LAT: {self.latit} LON: {self.long}'
CodePudding user response:
I dont know why u created all these methods individualy but i found that your self variables have the exact same name as your functions does, so change your self variables name(also remeber to change the variables name where u have returned each of them and remove the dashes from the beginning), remove the '()' from the result list, also remove the 'float()' and 'int()' from the final return from the class and replace them with 'str()'
import re
class Txt_data:
def __init__(self, result):
self.date1 = result[0]
self.time1 = result[1]
self.latit1 = result[2]
self.long1 = result[3]
self.depth1 = result[4]
self.md1 = result[5]
self.ml1 = result[6]
self.mw1 = result[7]
self.region1 = result[8]
self.method1 = result[9]
def date(self):
return self.date1
def time(self):
return self.time1
def latit(self):
return self.latit1
def long(self):
return self.long1
def depth(self):
return self.depth1
def md(self):
return self.md1
def ml(self):
return self.ml1
def mw(self):
return self.mw1
def region(self):
return self.region1
def method(self):
return self.method1
def __str__(self):
return ('MAG: ' str(self.ml()) ' DEPTH: ' str(self.region()) ' DATE/TIME: ' str(self.date()) ' ' str(self.time()) ' LAT: ' str(self.latit()) ' LON: ' str(self.long()))
result = ['2021.12.02', '22:29:24', '36.9605', '28.1775', '13.0', '-.-', '1.5', '-.-', 'KARACA-MARMARIS (MUGLA)', ' Quick']
print(Txt_data(result))
I have corrected your code and this should be like this.