dbfread icon indicating copy to clipboard operation
dbfread copied to clipboard

Support for dataclasses?

Open olemb opened this issue 4 years ago • 1 comments

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 avatar Dec 21 '20 02:12 olemb

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.

olemb avatar May 11 '21 09:05 olemb