Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill objects from CSV #8

Open
philippTheCat opened this issue Aug 27, 2014 · 5 comments
Open

Fill objects from CSV #8

philippTheCat opened this issue Aug 27, 2014 · 5 comments

Comments

@philippTheCat
Copy link

It would be nice to be able to give the parser an class and have it create and fill it in. The given example would create those, and return them as a list or set.

import smartcsv

COLUMNS_1 = [
    {'name': 'title', 'required': True},
    {'name': 'category', 'required': True},
    {'name': 'subcategory', 'required': False}
]

class Obj:
    def __init__(self):
        self.title = ""
        self.category = ""
        self.subcategory = ""


with open('my-csv.csv', 'r') as f:
    reader = smartcsv.reader(f, columns=COLUMNS_1,clazz=Obj)
    for obj in reader:
        print(obj)
@santiagobasulto
Copy link
Owner

+1 I'm currently working on this. The class would be the actual "model". The spec would be something like:

class ProductModel(object):
     def __init__(self, title, price, currency, url, image_url=None):
        self.title = title
        self.price = price
        self.currency = currency
        self.url = url
        self.image_url = image_url

with open('my-csv.csv', 'r') as f:
    reader = smartcsv.reader(f, columns=COLUMNS_1, model=ProductModel)

@philippTheCat
Copy link
Author

sweet.

what do you think about some hooks or signals?
for example I want to use this to import some data into a database. A hook where I could call the validation function of the model and commit it to the database would be usefull.

Im sure there are other usecases for hooks/signals.

@santiagobasulto
Copy link
Owner

I don't understand. How would it work? Can you do a quick code sample?

@philippTheCat
Copy link
Author

def commitToDb(model):
   db.session.add(model)
   db.session.commit()

with open('my-csv.csv', 'r') as f:
    reader = smartcsv.reader(f, columns=COLUMNS_1, model=ProductModel, post_create=commitToDb)

@santiagobasulto
Copy link
Owner

I'm currently working with a simplified version of it. It'll be out soon. Just to be clear, you could easily do what you say in different ways:

reader = smartcsv.reader(...)

# 1 way
for obj in reader:
    commitToDb(obj)

# 2 way (directly in the for)
for obj in reader:
   db.session.add(obj)
   db.session.commit()

# other way
[commitToDb(obj) for obj in reader]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants