-
Notifications
You must be signed in to change notification settings - Fork 8
Add support for setting values and writing the config #9
Comments
Thanks for letting me know. To be honest I was thinking about adding write functionality at the time of implementing the library but that would have introduced a few problems and the correct solution requires a lot of extra work. E.g.: retaining comments, perhaps formatting. This is why I decided create reader-only library which does the job for 90+% of projects that don't want to manipulate the config programmatically. Despite this I think write is a good idea. However in that case I'd structure the project differently. If write would be supported (correctly - e.g.: optionally retaining comments) then I'd implement the whole thing as a general purpose extended json library that consisted of the current On top of the previously described How As you see this is quite a lot of work and I can't allocate free time for this in the near future. For this reason I'd keep json-cfg as it is. I thought people who want to modify their config and are fine reverting the cfg = jsoncfg.load('my.cfg')
py_obj_tree = cfg()
py_obj_tree['whatever'] = ['woof', 'meow']
json.dumps(py_obj_tree) |
In my implementation (https://github.com/Synerty/json-cfg-rw), i do use json.dumps. However, i've made changes to json-cfg nodes so that they auto create objects and populate them with the default values when they are missing. This way i can start reading empty config files, use the default configuration like normal, and write the default config back out. Usign json-cfg to write my default config files programatically. This also works when I want to add new properties to the file. Having a JSONWriter as you say would be very nice. |
This is a strategy that is very specific to your application. It mixes read and write into a relatively complex operation that isn't intuitive for the user: someone using the library (or a newcomer who reads the existing config loader code) probably doesn't expect the config data to be altered as a result of a query. Another problem with this solution is that it works with dicts but I can easily imagine scenarios where it doesn't work in with lists (fixed length, variable length, etc...). Those situations can't be solved with a compound "query_or_set_default" operation - they require custom code (if config exists then do this, else do that). Unfortunately for the above reasons I would be hesitant to add your solution to the core - I think it isn't simple and/or general purpose. Supporting assignment would be relatively easy to add (at least with dicts, lists could cause problem here too) but I haven't added it due to the lack of json write support. Lists are problematic because if someone says |
|
A bit off topic, but another thing that isn't solid is that the |
I've chosen to use the with block for convenience, the user can choose this or they can call "save_config" them selves. Remembering that I use it to update the config file as well, I want to ensure that the data given to the user matches what is in the config file, so reading defaults always writes it back. Here is how I use it. |
When people read code they expect People look at the dog, expect it to bark, and then they are surprised when the dog says 'meow'...... To be honest I can't complain with json-cfg: In
As you see I've paid a price: I've modified the "popular" behaviour of some operators in unusual ways (that is a bad-bad thing). But at least in exchange for relatively valuable things. I still don't think it to be an extremely good tradeoff, but it might be an OK one. |
You're very correct. I'll keep this in mind, and thank you for the clarity. |
I understand your motives. As a programmer it is very tempting to hack a hackable language. I've hacked the sh*t out of python, C++ operator overriding, template metaprogramming, etc... It took me more than a decade to realise that in case of large codebases it is much easier to navigate and read the code if I don't have to dig deep to find out how some deeply hidden magic works. It is a best case scenario when you can at least find out by reading the code that there is hidden magic. The much worse scenario is when you can't even see it by reading the code. E.g.: your I've also learned that abstractions are super dangerous. If you aren't convinced that an abstraction is useful then don't introduce it. Writing a bit bloaty code or copy-pasted code is less expensive than introducing the wrong kind of abstraction (that will be very difficult to eliminate/cleanup/refactored compared to bloat or copy-paste). An abstraction first has to be understood, then people have to realise that it is the wrong abstraction, then a strategy is needed to remove it... In case of bloat and copy-paste: it is bloody obvious how to deal with it even if its ugly. |
This works for me.
Synerty@7214f33
The text was updated successfully, but these errors were encountered: