Jaymon / prom

A PostgreSQL or SQLite orm for Python
MIT License
22 stars 4 forks source link

CSVOrm #99

Open Jaymon opened 4 years ago

Jaymon commented 4 years ago

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 https://github.com/Jaymon/prom/issues/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.