Comments (21)
as far as i understand ini files:
[section]
key=value
i guess the encoder doesn't need any changes?
or what will happen when you always use in the INISerializer
something like the following
pseudo-code
#encode() - write values into section of an ini
writeline(f"{key}={json.dumps(value)}"
#decode() - read lines/values from a section of an ini
section_value={}
for line in section:
key, value = split(line) # e.g. use regex r"^(?P<key>\w+)\=(?P<value>.*?$"
section_value[key] = json.loads(value)
from python-benedict.
i guess the encoder doesn't need any changes?
seems that encoder also must be changed
>>> json.dumps({'key':True})
'{"key": true}'
in the 'str' key's and values must be in double-quotes that json.loads
works. and True must be lower-case in the ini-file
so the encoder must be also modified
from python-benedict.
@d-chris the problem is that the ini
format doesn't support nested data structures, for this reason everything beyond the first level of depth gets stringified.
from python-benedict.
damn it, thought so.
i didn't inspect your package very thoroughly but i tried to use get_dict()
but it always returns the default
-value
print(readback.get_dict(path), type(readback.get_dict(path)))
output
{} <class 'benedict.dicts.benedict'>
maybe i can use the json.loads()
method somehow, to convert the str
back into a dict
from python-benedict.
@d-chris I could add a new option to the ini
serializer for serialize nested values in json format.
The only doubt about this approach is the decoding: how can we know if a string value is a json string that needs to be decoded or not?!
from python-benedict.
@d-chris I check if this is backward-compatible.
from python-benedict.
@d-chris I check if this is backward-compatible.
thx
for me your to_ini()
is ok, only from_ini()
needs to return a dict
ini-file
[avr]
setup = {'bitclock': '125kHz', 'chiperase': True}
fuses = {'bitclock': '1.8MHz', 'lock': '0xFC'}
[m8]
parent = avr
mcu = m8
setup = {'lfuse': '0xE4'}
python-dict
ini = benedict({
'avr': {
'setup': {'bitclock': '125kHz', 'chiperase': True},
'fuses': {'bitclock': '1.8MHz', 'lock': '0xFC'}
},
'm8': {
'parent': 'avr',
'mcu': 'm8',
'setup': {'lfuse': '0xE4'},
}
}
from python-benedict.
via inheritance it seems to work
import benedict as bd
import json
class benedict(bd.benedict):
def to_ini(self, *args, **kwargs):
def super_class(*vargs):
# return bd.benedict(*vargs)
return self.__class__.__bases__[0](*vargs)
data = dict()
for section, keys in self.dict().items():
data[section] = dict()
for key, value in keys.items():
data[section][key] = json.dumps(value)
return super_class(data).to_ini(*args, **kwargs)
@classmethod
def from_ini(cls, s, **kwargs):
ini = super().from_ini(s, **kwargs)
data = dict()
for section, keys in ini.dict().items():
data[section] = dict()
for key, value in keys.items():
data[section][key] = json.loads(value)
return cls(data)
if __name__ == '__main__':
original = benedict({
'avr': {
'setup': {'bitclock': '125kHz', 'chiperase': True},
'fuses': {'bitclock': '1.8MHz', 'lock': '0xFC'}
},
'm8': {
'parent': 'avr',
'mcu': 'm8',
'setup': {'lfuse': '0xE4'},
}
})
content = original.to_ini()
readback = benedict.from_ini(content)
print(original.avr.setup.chiperase, readback.avr.setup.chiperase)
from python-benedict.
I doubt it passes all tests, could you make a PR?
from python-benedict.
I doubt it passes all tests, could you make a PR?
PR? what do you mean?
for me i guess my problem is solved :)
from python-benedict.
PR? what do you mean?
Pull Request
from python-benedict.
i just played around... can try to fork your repo and contribute
don't want to mess up your benedict-class, what do you think about?
from benedict.__future__ import benedict
from python-benedict.
Do the changes in your fork, I'm not constrained to merge it, but I can see it if the tests pass.
from benedict.__future__ import benedict
What is this?
from python-benedict.
got it 😃
from python-benedict.
test for issue #43 seems the problem.
fixed with commit d203bfb
from python-benedict.
@d-chris you can upgrade to 0.30.1
version.
from python-benedict.
@d-chris you can upgrade to
0.30.1
version.
@fabiocaccamo i dont't know if i sould open a new issue, i tested output of to_ini()
from the following versions 0.30.0
and 0.30.1
long story short, output is not the same
python script
from benedict import benedict
from pathlib import Path
if __name__ == '__main__':
boolean_keys = ['_false_', '_true_', '_none_']
boolean_values = [False, True, None]
boolean_types = dict(zip(boolean_keys, boolean_values))
string_keys = ['_string_', '_null_string_']
string_values = ['fubar', '']
string_types = dict(zip(string_keys, string_values))
integer_keys = ['_neg_integer_', '_null_integer_', '_integer_']
integer_values = [-128, 0, 128]
integer_types = dict(zip(integer_keys, integer_values))
decimal_keys = ['_neg_float_', '_null_float_', '_float_']
decimal_values = [-25.6, 0.0, 25.6]
decimal_types = dict(zip(decimal_keys, decimal_values))
dtypes = boolean_types | string_types | integer_types | decimal_types
dtypes_values = list(dtypes.values())
dtypes['boolean'] = boolean_types | {'_list_': boolean_values, '_set_': set(
boolean_values), '_tuple_': tuple(boolean_values)}
dtypes['string'] = string_types | {'_list_': string_values, '_set_': set(
string_values), '_tuple_': tuple(string_values)}
dtypes['integer'] = integer_types | {'_list_': list(integer_values), '_set_': set(
integer_values), '_tuple_': tuple(integer_values)}
dtypes['decimal'] = decimal_types | {'_list_': list(decimal_values), '_set_': set(
decimal_values), '_tuple_': tuple(decimal_values)}
dtypes['_list_'] = list(dtypes_values)
dtypes['_set_'] = set(dtypes_values)
dtypes['_tuple_'] = tuple(dtypes_values)
Path('dtypes.ini').write_text(benedict(dtypes).to_ini())
0.30.0
[DEFAULT]
_false_ = False
_true_ = True
_none_ = None
_string_ = fubar
_null_string_ =
_neg_integer_ = -128
_null_integer_ = 0
_integer_ = 128
_neg_float_ = -25.6
_null_float_ = 0.0
_float_ = 25.6
_list_ = [False, True, None, 'fubar', '', -128, 0, 128, -25.6, 0.0, 25.6]
_set_ = {False, True, '', -128, 128, -25.6, None, 'fubar', 25.6}
_tuple_ = (False, True, None, 'fubar', '', -128, 0, 128, -25.6, 0.0, 25.6)
[boolean]
_false_ = False
_true_ = True
_none_ = None
_list_ = [False, True, None]
_set_ = {False, True, None}
_tuple_ = (False, True, None)
[string]
_string_ = fubar
_null_string_ =
_list_ = ['fubar', '']
_set_ = {'', 'fubar'}
_tuple_ = ('fubar', '')
[integer]
_neg_integer_ = -128
_null_integer_ = 0
_integer_ = 128
_list_ = [-128, 0, 128]
_set_ = {-128, 0, 128}
_tuple_ = (-128, 0, 128)
[decimal]
_neg_float_ = -25.6
_null_float_ = 0.0
_float_ = 25.6
_list_ = [-25.6, 0.0, 25.6]
_set_ = {0.0, 25.6, -25.6}
_tuple_ = (-25.6, 0.0, 25.6)
0.30.1
[DEFAULT]
_false_ = False
_true_ = True
_none_ = None
_string_ = fubar
_null_string_ =
_neg_integer_ = -128
_null_integer_ = 0
_integer_ = 128
_neg_float_ = -25.6
_null_float_ = 0.0
_float_ = 25.6
_list_ = [False, True, None, 'fubar', '', -128, 0, 128, -25.6, 0.0, 25.6]
_set_ = {False, True, '', -128, 128, -25.6, None, 'fubar', 25.6}
_tuple_ = (False, True, None, 'fubar', '', -128, 0, 128, -25.6, 0.0, 25.6)
[boolean]
_false_ = False
_true_ = True
_none_ = None
_list_ = [false, true, null]
_set_ = [false, true, null]
_tuple_ = [false, true, null]
[string]
_string_ = fubar
_null_string_ =
_list_ = ["fubar", ""]
_set_ = ["", "fubar"]
_tuple_ = ["fubar", ""]
[integer]
_neg_integer_ = -128
_null_integer_ = 0
_integer_ = 128
_list_ = [-128, 0, 128]
_set_ = [-128, 0, 128]
_tuple_ = [-128, 0, 128]
[decimal]
_neg_float_ = -25.6
_null_float_ = 0.0
_float_ = 25.6
_list_ = [-25.6, 0.0, 25.6]
_set_ = [0.0, 25.6, -25.6]
_tuple_ = [-25.6, 0.0, 25.6]
from python-benedict.
@d-chris actually all values that are collections (dict
, list
, tuple
, set
) are encoded to json.
from python-benedict.
i know, at the beginning it seemed easy...
but for some users the backwards compatibility may not guaranteed.
i would expect None
is always encoded as None
but json changes is to null
for the decoder the kwarg parse_constant
could be used, i guess encoding is a bigger issue
from python-benedict.
@d-chris I restricted json encoding only for dict values 391439e, I think that this is the best compromise between having dict support and backward-compatiblity.
from python-benedict.
@d-chris this has been fixed in 0.30.2
version.
from python-benedict.
Related Issues (20)
- Add possibility to access items using keys as attributes (dotted notation).
- Protected keys in JSON items after calling json.dumps on benedict HOT 7
- Missing dependency on pyyaml HOT 3
- keyattr dynamic or AttributeError HOT 5
- INI support for case sensitive keys HOT 6
- Support defining how input data is loaded (filepath, url or data-string) HOT 3
- Assigning benedict element to itself clears the element HOT 2
- Minimalize dependencies and define scope HOT 1
- Move some requirements to a new `parse` extra requirement.
- Exception when comparing with Protocol classes HOT 7
- Yaml serializer produces inconsistent results HOT 5
- Add `from_html` method. HOT 2
- iterating benedict_dictionary.items(), value is not benedict_type HOT 2
- Union with assignment operator produces inconsistent results compared to `dict`. HOT 2
- README Index links don't work on PyPI, only GitHub's markdown renderer HOT 2
- Bug: Passing `data_only=True` into `from_xls()` returns excel formulas instead of cell data values HOT 5
- RecursionError while initializing from existing dict HOT 3
- Optionally allow `keypath_separator` in input dict keys (`unflatten`). HOT 6
- Benedict with Realtime Sync accross Processes HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-benedict.