poyo icon indicating copy to clipboard operation
poyo copied to clipboard

Support to list of dicts?

Open timonwong opened this issue 8 years ago • 3 comments

It seems currently poyo doesn't support list of dicts, for example:

list_val:
  - key: key1
  - key: key2

Will be parsed intto:

{'list_val': ['key: key1', 'key: key2']}

And:

list_val:
  - key: key1
    value: value1
  - key: key2
    value: value2

Will just throw ValueError while parsing:

ValueError: Parent of ChildMixin instance needs to be a Container.

Is there any plan to support them?

I'm using poyo v0.4.0

timonwong avatar Jun 29 '16 06:06 timonwong

Hi @timonwong, thank you for raising this issue! 🙇

I am currently very busy with working on pytest related things, so I'm not sure if or when I'll get to this. I'd love to see a PR from you on this though. So please feel free to give it a go and let me know if you need help.

Raphael

hackebrot avatar Jun 29 '16 22:06 hackebrot

Also related, it does not support a list at the root of the document:

>>> parse_string('''
... - one
... - two
... - three
... ''')
Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/usr/local/lib/python3.5/dist-packages/poyo/parser.py", line 241, in parse_string
    return parser()
  File "/usr/local/lib/python3.5/dist-packages/poyo/parser.py", line 234, in __call__
    match = self.find_match()
  File "/usr/local/lib/python3.5/dist-packages/poyo/parser.py", line 224, in find_match
    ''.format(self.source[self.pos:])
poyo.exceptions.NoMatchException: None of the known patterns match for …

PyYaml:

>>> from yaml import safe_load
>>> safe_load('''
... - one
... - two
... - three
... ''')
['one', 'two', 'three']

mixmastamyk avatar Apr 12 '17 21:04 mixmastamyk

I know this is very late, but I'm interested in using a single-file 0-dependency version of this library (which I will happily contribute back). One of the only issues preventing me from using it is this issue where a list item cannot be a section.

I would appreciate any pointers you can provide to help allow this modification. I started by looking here:

_LIST_VALUE = (
     _BLANK
     + r"-"
     + _BLANK
     + r"('.*?'|\".*?\"|[^#\n]+?)"
     + _INLINE_COMMENT
     + _OPT_NEWLINE
 )
 _LIST_ITEM = _BLANK_LINE + r"|" + _COMMENT + r"|" + _LIST_VALUE

 _LIST = _SECTION + r"(?P<items>(?:" + _LIST_ITEM + r")*" + _LIST_VALUE + r")"

I believe that what I'll want to do is modify the _LIST_VALUE definition in some way, so I tried various iterations on:

     _BLANK
     + r"-"
     + _BLANK
     + _MULTILINE_SECTION
     #+ r"('.*?'|\".*?\"|[^#\n]+?)"
     #+ _INLINE_COMMENT
     + _OPT_NEWLINE

Using _MULTILINE_STR or _MULTILINE_SECTION but always running into duplicate group definition errors.

Do you have any quick and dirty ideas how I might capture this properly? Even if I can just get the entire representation after the dash and space as a single string, I would be able to just feed that back into parse_string and get that part of the dictionary, which I would be happy to do (even outside the initial parser run).

I will continue to tinker, but any assistance greatly appreciated.

tolidano avatar Jul 03 '23 15:07 tolidano