Python Forum

Full Version: Custom data structure
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
New to python, from .Net. I know python does not support arrays.

This class

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
I can do this ONCE
# Class Instance
res = my.myStructure()
res.str1 = "Yes"
res.num1 = 50
res.date1 = my.dt.datetime.strptime("05/01/1950","%m/%d/%Y")
Question: How can I use myStructure to store many records of data (like an array), or may be I cant?
Can I convert my class structure into a dictionary or something ??

Please advise
I'm not sure what exactly your looking for. You could do a list of your object:

data = []
data.append(my.myStructure(dt.datetime(1950, 5, 1), 50, 'Yes'))
data.append(my.myStructure(dt.datetime(1970, 2, 25), 0, 'Up'))
data.append(my.myStructure(dt.datetime(1980, 8, 10), 108, 'Eno'))
Now you have a list of three instances of your class. Or you could implement a list in myStructure:

class myStructure(object):

    def __init__(self):
        data = []

    def append(self, date=dt.datetime(2000,12,31), num=0, text="Nothing"):
        data.append((date, num, text))
Now you have an object that has a list of tuples, with the data previously stored in myStructure. You could then override the other list methods, and magic methods like __getitem__, to make it act more like a list. Or you could sub-class one of the classes in collections.abc, which would probably be easier.
Great that works, make the class the tuples( or array type). That acts like a structured array found in .Net