Build class to make a Smart list - trying - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Build class to make a Smart list - trying (/thread-17093.html) |
Build class to make a Smart list - trying - icm63 - Mar-28-2019 Class import datetime as dt class Defaults: """Storage of Default Values""" dbConStr = "pass:Name:Database;" baseDate = dt.datetime(2000,12,31) baseVal = int(100) class myStructure: """ My custom data structure for arrays """ def __init__(self, date1=dt.datetime(2000,12,31), num1=0, str1="Nothing"): self.date1 = date1 self.num1 = num1 self.str1 = str1 class MySmartList(): """ Smart list with field data IDs """ def __init__(self): data = [] def AddNewRec(self, date1="05/01/1950", num1=0, str1="Nothing"): #Accepted date format 05/01/1950 zDate = dt.datetime.strptime(date1,"%m/%d/%Y") k = myStructure(zDate, num1, str1) data.append(k)trying it out # Smart List myData =[] my.MySmartList.AddNewRec(myData,date1 = "05/01/1950",num1=100,str1='Test0') my.MySmartList.AddNewRec(myData,date1 = "05/02/1950",num1=101,str1='Test1') my.MySmartList.AddNewRec(myData,date1 = "05/03/1950",num1=102,str1='Test2')Get error on bold line 5 >> data.append(k) def AddNewRec(self, date1="05/01/1950", num1=0, str1="Nothing"): #Accepted date format 05/01/1950 zDate = dt.datetime.strptime(date1,"%m/%d/%Y") k = myStructure(zDate, num1, str1) data.append(k)Newbie here, what am i doing wrong? RE: Build class to make a Smart list - trying - Yoriz - Mar-28-2019 Add self. in front of both data in MySmartList Note: when you have an error, please post the full error traceback in error tags. RE: Build class to make a Smart list - trying - icm63 - Mar-28-2019 changed the data.append(k) to self.data.append(k) class MySmartList(): """ Smart list with field data IDs """ def __init__(self): self.data = [] def AddNewRec(self, date1="05/01/1950", num1=0, str1="Nothing"): #Accepted date format 05/01/1950 zDate = dt.datetime.strptime(date1,"%m/%d/%Y") k = myStructure(zDate, num1, str1) self.data.append(k)My error now is From prev post, what do you mean self infront of MySmartList??
RE: Build class to make a Smart list - trying - Yoriz - Mar-28-2019 Make an instance of MySmartList then call the instance's AddNewRec methodDon't pass in a list to MySmartList it has its own internal listmy_smart_list = my.MySmartList() my_smart_list.AddNewRec(date1 = "05/01/1950",num1=100,str1='Test0') my_smart_list.AddNewRec(date1 = "05/02/1950",num1=101,str1='Test1') my_smart_list.AddNewRec(date1 = "05/03/1950",num1=102,str1='Test2') RE: Build class to make a Smart list - trying - icm63 - Mar-28-2019 Getting there my class import datetime as dt class Defaults: """Exaample of Storage of Default Values""" dbConStr = "pass:Name:Database;" baseDate = dt.datetime(2000,12,31) baseVal = int(100) class myStructure: """ My custom data structure for arrays """ def __init__(self, date1=dt.datetime(2000,12,31), num1=0, str1="Nothing"): self.date1 = date1 self.num1 = num1 self.str1 = str1 class MySmartList(): """ Smart list with field data IDs """ def __init__(self): self.data0 =[] #Add new records to data0 list via data structure object def AddNewRec(self,date1="05/01/1950", num1=0, str1="Nothing"): #Accepted date format 05/01/1950 zDate = dt.datetime.strptime(date1,"%m/%d/%Y") self.data0.append(myStructure(zDate, num1, str1))My smart list with a structure myData=my.MySmartList() myData.AddNewRec(date1 = "05/01/1950",num1=100,str1='Test0') myData.AddNewRec(date1 = "05/02/1950",num1=101,str1='Test1') myData.AddNewRec(date1 = "05/03/1950",num1=102,str1='Test2') for index, myData in enumerate(myData): print(index, myData.str1 + ' -- ' + str(myData.num1) + ' -- ' + str(myData.date1))Error on this line: for index, myData in enumerate(myData): Traceback (most recent call last): File "C:\Users\RTT\Documents\Visual Studio 2017\Projects\Python_Test_Project\Python_Test_Project\Test1.py", line 54, in <module> for index, myData in enumerate(myData): TypeError: 'MySmartList' object is not iterable any ideas? RE: Build class to make a Smart list - trying - ichabod801 - Mar-28-2019 You need enumerate(myData.data) . Your class isn't a list, it just contains one. This is what I was saying in the other thread about overriding the other magic methods that sequences use (in this case __iter__) or sub-classing from a class that already has those methods.
RE: Build class to make a Smart list - trying - Yoriz - Mar-28-2019 for index, myData in enumerate(myData.data0): RE: Build class to make a Smart list - trying - icm63 - Mar-28-2019 SUCCESS |