Closed plasticassius closed 1 year ago
The easy bit is the conversion of values going into SQLite, which you have covered.
For your needs of data coming out of SQLite, I think a better approach is to use the DataClassRowFactory. Inherit from that and override the get_dataclass method. (Make sure to @functools.lru_cache it!)
get_dataclass is called with the name and declared type of every result column. get_dataclass ultimately calls dataclasses.make_dataclass. There your choices will depend on your needs and include post init processing and descriptors.
I'm unlikely to add regex based typing because of the complexity and everyone's needs will be different and project dependent. However I am keen to make that as easy as possible via inheritance so you only have to write the small amount of code reflecting those needs.
Thanks for the suggestion. At first glance, TypesConverterCursorFactory seems closer to what I'd like to inherit from than DataClassRowFactory due to the use of dataclasses. But, I'll look into it more.
I take it you find using dataclasses less interesting. I'm expecting them to become the most pervasive structure of fields type in Python :) It is the first time the standard library has a data structure that hits all the sweet spots of being light weight, simple to use, has (optional) typing, comes out perfectly in documentation generators, trivial to convert to/from tuple/dict, simple to extend etc. We'll see.
If you are trying for the simplest shortest code possible, then you are welcome to take the code for TypesConverterCursorFactory and just write your own thing. The code I wrote has more blank lines and doc strings than actual code, and you almost certainly don't need all of it.
That said, especially for projects you expect to end up around for a long time and be bigger, then using the dataclass approach is probably better for data coming out of SQLite. It especially matters because you also want to use the column names as relevant information.
In any event, can this issue be closed? You may also want to post to the forum to see what other people think.
Dataclasses sound interesting, but the problem I'm looking at is about converting SQLite types to python types which makes TypesConverterCursorFactory more relevant.
As I've shown, I can make parameterized types by simply overriding convert_value. By breaking the arguments out of the type string and passing them to the python class, I can use Decimal from the standard library and other types that need arguments. I think that the regular expression is handy for breaking apart the type string, and can be easily replaced to customize the parsing.
To me it seems that dataclasses would be an enhancement on top of that, by adding complementary features to the data types. I'll think about how these things can be combined.
I'm interested in using parameters in conversion types in apsw.ext and I thought this might appeal to other users. I included an example of what I've been working with below, but I didn't open a pull request because it's still more of a proof of concept.
I included two parameterized types, both are int types in the database, but one is an enum that has named values, and the other has a fixed number of digits after the decimal point.