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

CSVOrm #99

Open
Jaymon opened this issue Mar 5, 2020 · 0 comments
Open

CSVOrm #99

Jaymon opened this issue Mar 5, 2020 · 0 comments

Comments

@Jaymon
Copy link
Owner

Jaymon commented Mar 5, 2020

I was messing around with this for a project, we ended up not using it but it might be handy in an addon:

from prom import Orm, Schema, Field
import os
import re
class CSVOrm(Orm):

    _id = _created = _updated = pk = None

    @classmethod
    def create_schema(cls, csvpath):
        c = CSV(csvpath)
        fields = {}

        table_name = os.path.splitext(os.path.basename(csvpath))[0]

        for row in c:
            for k, v in row.items():
                if k not in fields:
                    fields[k] = Field(str, name=k)
                    #fields = {k: Field(str) for k in row.keys()}

                if v:
                    if v.lower() in set(["true", "false", "t", "f", "1", "0", "y", "n"]):
                        if fields[k]._type is str:
                            fields[k]._type = bool

                    elif v.isdigit():
                        fields[k]._type = int

                    elif re.match("^\d+\.\d+$", v):
                        fields[k]._type = float

                    else:
                        fields[k]._type = str

                else:
                    fields[k].required = False

        return Schema(table_name, **fields)

It builds upon #95

The CSV class is a small wrapper around the csv.DictReader module that can read utf-8 csv files, so it would be easily to switch that code out.

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

1 participant