-2

I need to be able to build my buildObject using data extracted from csv file columns

class BuildObject(ObjectID):

    def __init__(self, ObjectID, ObjectName, ObjectPrice, ObjectLocation, ObjectColour, ObjectAge, ObjectTag):
        self.ObjectID= ObjectID
        self.ObjectName= ObjectName


    def main():
         with open(filename1, "r") as csv1, open(filename2, "r") as csv2:  
            csvReader1 = csv.DictReader(csv1)
            csvReader2 = csv.DictReader(csv2)

            csvList = []
            for row1, row2 in zip(csvReader1, csvReader2):  
                csvList.append((row2["ObjectName"], row1["ObjectId"], row1["ObjectPrice"]))

            return csvList
  • Please try to rephrase your question. Especially the second one... Which data do you get from the database with SQL? And what are you doing with this data? Do the two csv files have the same columns? – zypro Oct 12 '18 at 09:32
  • Pleae reduce your Question to **one** Topic. As it's possible to make any `item` a `class attribute` i did not recommend this. Why don't you want to use `list` or `dict`? – stovfl Oct 12 '18 at 10:10
  • [Edit] your Question and give examples data for *"every unique itemID"*. – stovfl Oct 12 '18 at 10:26
  • Read [What's the pythonic way to use getters and setters?](https://stackoverflow.com/a/2627034/7414759) – stovfl Oct 12 '18 at 10:53

1 Answers1

0

Comment: My concern is with this answer that it will work fine provided the csv files have the exact same objectID and in the same order - but will happen if a objectID/Object is missing only in one of the csv files?

Therefore, you can't use zip(csvReader1, csvReader2), you need random access to a Date_Record using the ObjectID as key/index.
As you mentinioned large amounts of data I would recommend go with SQL.
If you want to do it using Python objects change the following:

def __init__(self):
    self._data_store = {}

@data_store.setter
def data_store(self, data):
    ...
    self._data_store[record['ObjectID'] = record

Question: The one topic would be the create a BuildObject for every unique itemID using the data from the csv files and sql query


Checking your code, got the following Error:

    class BuildObject(ObjectID):
NameError: name 'ObjectID' is not defined

Why do you inherit from ObjectID?
Where are these class defined?


Consider the following:

class Data_Record():
    """
      This class object hold all data for ONE Record
    """
    def __init__(self, ObjectID, ObjectName):
        self.ObjectID= ObjectID
        self.ObjectName= ObjectName
        # ... (omitted for brevity)

class Data_Store():
    """
      This class object handels Data_Record, reading from csv or sql or anywhere
    """

    def __init__(self):
        # List to hold all Data_Record objects
        self._data_store = []

    # Access read only the Data_Record objects
    @property
    def data_store(self):
        return self._data_store

    # Add ONE Data_Record from either csv or sql or anywhere
    @data_store.setter
    def data_store(self, data):
        # Condition type(data)
        if isinstance(data, dict):
            record = Data_Record(**data)
        elif isinstance(data, list):
            record = Data_Record(**tuple(data))
        else:
            raise(ValueError, "Data of type({}) are not supported!".format(type(data)))

        self._data_store.append(record)

    # Method to read from csv
    def read_csv(self, fname1, fname2):
        # ... (omitted for brevity)
        csvReader1, csvReader2 = ([], [])
        for csv1, csv2 in zip(csvReader1, csvReader2):
            self.data_store = (csv2["ObjectName"], csv1["ObjectId"])

    # Method to read from sql
    def read_sql(self, sql, query):
        result = sql.query(query)
        for record in result:
            self.data_store = record

Alternative: Without @property/getter/setter.

Here the read(... functions have to know how to add a new Date_Record object to self.data_store. Note: self.data_store is now a public attribute.
If you decide, later on, to store not in memory, you have to rewrite both read(... functions.

class Data_Record():
    def __init__(self, data=None):
        # Condition type(data)
        if isinstance(data, dict):
            self.ObjectID = data['ObjectID']
            self.ObjectName = data['ObjectName']

        elif isinstance(data, list):
            # List have to be in predefined order
            # e.g ObjectID == Index 0 ObjectName == Index 1 etc.
            self.ObjectID = data[0]
            self.ObjectName = data[1]
        else:
            self.ObjectID = None
            self.ObjectName = None

class Data_Store():
    def __init__(self):
        self.data_store = []

def read_csv(self, fname1, fname2):
    for csv1, csv2 in zip(csvReader1, csvReader2):
        self.data_store.append(Data_Record((csv2["ObjectName"], csv1["ObjectId"])))

def read_sql(self, query):
    for record in SQL.query(query):
        self.data_store.append(Data_Record(record))
stovfl
  • 14,998
  • 7
  • 24
  • 51
  • The csv and sql files hold thousands of records, would this be suitable for holding large amounts of data – Salted_coder Oct 12 '18 at 13:00
  • @dick_bob1: This depends how large are your **memory**, using `list` has the lowest footprint. Whatever, you can simple change from **list** to write each record to **a new sql db** or **csv file**. Therefore you have only implement this in `def date_store(...`. – stovfl Oct 12 '18 at 14:32
  • @dick_bob1: Updated my Answer – stovfl Oct 12 '18 at 16:59