I have been teaching myself Python 3. I watched a video on YouTube entitled "stop writing classes". The Gtable class doesn't even have an init function, so I can't help but wonder if I should have done something different. Also as someone teaching themselves a language for the first time I would really appreciate any critique on how to write the code more efficiently, as well as any anti-idioms unintentionally used. Specifically I feel like there was probably a better way to generate the dictionaries, but especially with the first one I was having trouble writing a comprehension.
##input file text
##|HAAS T|TARGET T|XGEOM|ZGEOM
##$T0101$T0101$X22.960$Z20.0$
##$T0202$T0606$X32.425$Z7.376$
##$T0303$T0404$X30.588$Z15.620$
##$T0404$T0202$X22.367$Z18.549$
##$T0505$T0808$X33.291$Z8.197$
class GeometryTable(dict):
"create a geometry table from a file for use in regexes"
def __init__(self):
self.d={}
def create(self,location):
f=open(location,'r')
tablestring=f.read()
f.close()
tablelist=[]
tablelist=tablestring.split('$')
#delete unecessary data from list
tablelist.pop(0)
tablelist=[item for item in tablelist if item!='\n']
#create a dictionary
count=0
keylist=[]
valuelist=[]
while count<len(tablelist):
keylist.append(tablelist[count])
valuelist.append([tablelist[count+1],tablelist[count+2],tablelist[count+3]])
count+=4
self.d=dict(zip(keylist,valuelist))
def xval(self,string):
if string in self.d:
return (self.d[string][1])
else:return(r"X20.0")
def zval(self,string):
if string in self.d:
return (self.d[string][2])
else:return(r"Z20.0")
def tval(self,string):
if string in self.d:
return (self.d[string][0])
else:return(r"T2020")
def report(self,string):
if string in self.d:
return (self.d[string])
else:return(r"(no value)")
class GTable(GeometryTable):
"""for replacing geometries that were given
default values before this program was created"""
def load(self,location):
GeometryTable.load(self,location)
b=self.__dict__["d"].values()
redict={x[0]:x for x in b}
self.d=redict
##output dictionaries for regular expressions
##>>> test.d
##{'T0101': ['T0101', 'X22.960', 'Z20.0'],
## 'T0202': ['T0606', 'X32.425', 'Z7.376'],
## 'T0303': ['T0404', 'X30.588', 'Z15.620'],
## 'T0404': ['T0202', 'X22.367', 'Z18.549'],
## 'T0505': ['T0808', 'X33.291', 'Z8.197']}