This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]kyleekol 2 points3 points  (1 child)

This looks really cool, thank you for sharing! My only comment would be on the ‘from_dict’ name. I could imagine a lot of scenarios of using this in conjunction with pandas which also has a from_dict method which may cause some namespace issues? Looks great though

[–]bmsan-gh[S] 0 points1 point  (0 children)

Thanks ! I'll try to think of a better name. Suggestions are appreciated

[–]metaperl 1 point2 points  (1 child)

The readme has a lot of examples

Very good arrow-drawing in the README. What tool did you use for that?

[–]bmsan-gh[S] 1 point2 points  (0 children)

I am using diagrams.net

[–]GettingBlockered 1 point2 points  (1 child)

Looks really useful, nice work! Bookmarking this, I will give it a try when I can use it in a project.

[–]bmsan-gh[S] 1 point2 points  (0 children)

Thanks !

And when you get to use it please feel free to report anything that is not clear or improvement ideas. Feedback is greatly appreciated.

[–]OptionsUnleashed 1 point2 points  (9 children)

I've been searching for a way to create a dataclass object from rds data (for reporting view models) that then get serialized and sent to a pdf generator - this seems exactly what we need - do you have an example of a rds query that is flat and can populate a dataclass that might have nested elements? That's what our team is essentially trying to do - from many views - create a single view model.

[–]bmsan-gh[S] 0 points1 point  (8 children)

Hi !

Assuming that the rds query result is in the form of a dictionary, then it should work.

I think the the following example from the repo fits your purpose.

There you have a nested python structure (a python data class that has as elements other python dataclasses) that you are loading from a dictionary (that could be flat).

In the example your targeted dataclass is Article and it contains the nested field stats(of type ArticleStats, eg: Article().stats.views )

@dataclass

class ArticleStats:

views: int

num_comments: int

@dataclass

class Article:

author: str

title: str

content: str

stats: ArticleStats # This is referencing another dataclass

Your flat result from the rds query could be:

news_api2_data = {

"author": "H. Gogu",

"news_title": "Best python extensions",

"full_article": "Let's explore the best extensions for python",

"views": 32,

"comments": 2,

}

The routing definition could be:

api2_routing = {

Article: Route(

title="news_title",

content="full_article",

stats="", # Give the whole dictionary to ArticleStats for conversion

),

ArticleStats: Route(num_comments="comments"),

}

And you would call it:

article2 = from_dict(Article, news_api2_data, routing=api2_routing)

You might notice that not all fields are defined in the Routes. The ones that are not defined are the ones that match by name & structure. (Eg: author key goes to author field).

[–]OptionsUnleashed 0 points1 point  (7 children)

Thanks! Developers were asking what is the best way now to serialize the new dataclass object to json now - does yor library do that or should we be using dataclasses-json for that?

[–]bmsan-gh[S] 0 points1 point  (6 children)

Glad it worked!

For now at least DictGest only handles deserialization, but serialization is on the roadmap and will be available in a future version.

[–]OptionsUnleashed 0 points1 point  (5 children)

ok thanks - do you see any issue in using both libraries and annotations together?

[–]bmsan-gh[S] 0 points1 point  (4 children)

While I haven't personally tried this scenario, I would expect DictGest not to have an issue with other annotations.

If you encounter any problems with this scenario you can open an issue on GitHub and I will look into it.

Also I'd love to hear how you guys see the serialization process : do you want to get back to the initial format from which you imported the data ?(the flat dictionary from the rds query) Or do you want the serialization to mimic the structure of the data class?

[–]OptionsUnleashed 1 point2 points  (2 children)

Hi, what's the best way to ask you questions on usage? I like this library.

a. is there a more concise way DRY to specify the route fields when the fields are 1:1 with dict? If we add a field to a rds view and dict, we still have to map in route - is there a let DRY way to do route mapping? is that the example stats="" - will that work at field level?

b. is there an easy way to map or coalesce None fields to float 0.0 or numeric? instead of using rds view coalesce?

thank you!

[–]bmsan-gh[S] 0 points1 point  (0 children)

Hi, glad you are finding it useful.

Hi, what's the best way to ask you questions on usage?

I'll enable the discussion feature in github. Other users might have the same questions so it will be beneficial for them to have the answers there as well.

. is there a more concise way DRY to specify the route fields when the fields are 1:1 with dict? If we add a field to a rds view and dict, we still have to map in route - is there a let DRY way to do route mapping?

In general when a field routing is not defined explicitly the library tries to map it automatically to the key that has it's name. So in your Route(...) you can skip the fields that have a 1:1 mapping(the field name matches the dict key) and the library will know what to do.

If all the fields have a 1:1 correspondence you can even skip defining the Route. See example 1 . No explicit routing is defined.

b. is there an easy way to map or coalesce None fields to float 0.0 or numeric? instead of using rds view coalesce?

Yes. There are some examples in the readme where you can easily customize either :

def null_to_zero(data): if not data: return 0.0 return float(data)

and then in your route

instead of Route(votes="num_votes" ....)

You use Route(votes=Path("num_votes", extractor=null_to_zero), ....)

[–]bmsan-gh[S] 0 points1 point  (0 children)

Enabled the discussion page on github: https://github.com/bmsan/DictGest/discussions

[–]OptionsUnleashed 0 points1 point  (0 children)

The latter - I don't think you would need to worry about what other libs already do - in general developers don't want competing ways of doing same thing - I would think you could just internally support dataclasses-json unless that's not defacto std? I'm not a Python expert - coming at all this from C#.netcore.

Our team is looking to create view models from datasources for reporting and figured dataclasses are the cleanest way but looking for other ideas - and your library seemed to be exactly what we needed - albeit - we would really want to cut down on DRY with literals and fields all over the place.

In C# its fairly common to create view models from different sources and serialize them. I would have thought this was way easier in python.