olemb / dbfread

Read DBF Files with Python
MIT License
224 stars 91 forks source link

Support for dataclasses? #52

Open olemb opened 3 years ago

olemb commented 3 years ago

This is not something we need to support directly in the code, but it would be nice to have a documented way to create dataclasses from records.

I think it should be possible to create dataclasses dynamically be inspecting the DBF object, but perhaps it is best after all to create the dataclass manually.

olemb commented 3 years ago

That's something I'd love to add. How about this API?

@dataclass
class Customer:
    id: int
    name: str

dbf = dbfread.DBF('customers.dbf', recordfactory=Customer)    

This might already work. I haven't tested it.

We could also add support for dynamic dataclasses without adding any new options:

dbf = dbfread.DBF('customers.dbf', recordfactory=dataclass)

This should be fairly easy to implement.

Thanks for the suggestion! If I had created dbfread today I would have made dynamically created dataclasses the default instead of dictionaries.