Python Forum
Build class to make a Smart list - trying
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Build class to make a Smart list - trying
#1
Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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

1
2
3
4
5
# 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)

1
2
3
4
5
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?
Reply
#2
Add self. in front of both data in MySmartList
Note: when you have an error, please post the full error traceback in error tags.
Reply
#3
changed the data.append(k) to self.data.append(k)

1
2
3
4
5
6
7
8
9
10
11
12
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
Error:
Traceback (most recent call last): File "C:\Users\RTT\Documents\Visual Studio 2017\Projects\Python_Test_Project\Python_Test_Project\Test1.py", line 48, in <module> my.MySmartList.AddNewRec(myData,date1 = "05/01/1950",num1=100,str1='Test0') File "C:\Users\RTT\Documents\Visual Studio 2017\Projects\Python_Test_Project\Python_Test_Project\myClass.py", line 33, in AddNewRec self.data.append(k) AttributeError: 'list' object has no attribute 'data'
From prev post, what do you mean self infront of MySmartList??
Reply
#4
Make an instance of MySmartList
then call the instance's AddNewRec method
Don't pass in a list to MySmartListit has its own internal list
1
2
3
4
my_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')
Reply
#5
Getting there

my class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
1
2
3
4
5
6
7
8
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?
Reply
#6
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
1
for index, myData in enumerate(myData.data0):
Reply
#8
SUCCESS
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [split] How to ask Smart Questions (thread title expansion) darkuser 4 1,413 Nov-11-2024, 01:27 PM
Last Post: deanhystad
  Make a list learningpythonherenow 1 795 Oct-11-2024, 11:49 AM
Last Post: Pedroski55
  How to read module/class from list of strings? popular_dog 1 1,272 Oct-04-2023, 03:08 PM
Last Post: deanhystad
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 2,805 May-22-2023, 10:39 PM
Last Post: ICanIBB
  help me to make my password list in python >>> Oktay34riza 0 987 Dec-23-2022, 12:38 PM
Last Post: Oktay34riza
Question Keyword to build list from list of objects? pfdjhfuys 3 2,571 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Class-Aggregation and creating a list/dictionary IoannisDem 1 2,663 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Make Groups with the List Elements quest 2 2,631 Jul-11-2021, 09:58 AM
Last Post: perfringo
  Browse and build a list of folder paths using recursion Ultrainstinct_5 8 6,721 Apr-24-2021, 01:41 PM
Last Post: Ultrainstinct_5
  apendng to a list within a class gr3yali3n 4 3,123 Feb-16-2021, 06:30 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020