Norbert Wesolowski
Norbert Wesolowski

Reputation: 75

Making an object from each row of an array

I have the following section of code:

class Product(Cylinder): ### Creates a child class of Cylinder called Product
        def __init__(self, height, radius, name): ### new class paramenter, we now have name
            super().__init__(height, radius) ### inherits the previous initial viables, no need to rewrite.
            self.name = name ### name is now a new parameter.

        def objectName(self): ### new functions to return the name of the object
            return self.name

#### The bit about csv spreadsheets
import csv ### imports the csv module
with open('shopping.csv') as csv_file: ### opens the csv file
    reader = csv.reader(csv_file) ### defines the variable containing the information
    next(csv_file) ### goes to the next line, ignoring the title
    products = ["name","radius","hieght"] ### create's a base array for the information to be written to
    for row in reader: ### for each row
        products = np.vstack((products,row)); ### writes each row of the imported file to the array

    products = np.delete(products, (0), axis=0) ### removes the non-useful information (row 0)
    products[:,[0, 2]] = products[:,[2, 0]] ### switches first and third row to match our class and function inputs
    print(products)

as seen, there are three columns, which by the end of the code are: height, radius, name. I need to be able to change each row of the array I've created into objects which I can use later in the code. I have numpy imported at the start of the code. If possible I'd like to avoid importing any extra modules.

The contents of the array at the moment of print:

[['30.0' '4.0' 'Pringles ']
 ['12.2' '3.3' 'Coke can']
 ['7.5' '4.1' "Cadbury's Hot Chocolate"]
 ['8.2' '3.2' 'Green Giant Sweetcorn']
 ['8.8' '11.8' 'Celebrations Tub']
 ['15.0' '0.8' 'Rowntrees Fruit Pastilles']
 ['13.0' '6.0' 'M&S Best Ginger Nuts Tub']
 ['17.0' '3.3' 'Monster Energy Drink']
 ['10.9' '3.8' 'Heinz Baked Beans']]

I need a way to make each row an object, for example:

pringles = Product(30.0,4.0,'Pringles')
cokeCan = Product(12.2,3.3,'Coke Can')

The name of the object doesn't have to be the name of the actual product, it can be the row number or whatever is easiest to use in this code. any help is very much appreciated :)

Upvotes: 2

Views: 581

Answers (2)

blhsing
blhsing

Reputation: 106768

You can use itertools.starmap to map the sequence of tuples as arguments to the constructor of Product:

from itertools import starmap
with open('shopping.csv') as csv_file:
    reader = csv.reader(csv_file)
    next(reader)
    products = list(starmap(Product, reader))

Upvotes: 0

LeoE
LeoE

Reputation: 2083

You need some sort of datastructure in which you store your objects, probably a list seems to be a good idea, so you could simply do:

list_of_products = [Product(h, r, n) for (h, r, n) in original_array]

This results in a list of Products for later use

Upvotes: 2

Related Questions