1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] is CommentedMap.keys() in ruamel.yaml keeping the insertion order?

Discussão em 'Python' iniciado por Stack, Junho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to check some internal yaml files, and "fix" them. For that I need to preserve the comments, so I decided to use raumel.yaml library.

    The thing is that one of the checks is that some "keys", if present, are in a certain order. So for example:

    settings:
    # foo
    key2:
    prop: value
    # bar
    key1:
    prop: value
    ...


    I need to "fix" it, like this:

    settings:
    # bar
    key1:
    prop: value
    # foo
    key2:
    prop: value
    ...


    but in order to check first the correct "order", I am wondering if I do the following:

    yaml = YAML()
    yaml.preserve_quotes = True
    file: CommentedMap = yaml.load(path)
    settings: Any = file.get("settings", default=[])
    if not isinstance(linters, CommentedMap):
    return
    keys: list[str] = list(settings.keys())
    # are keys keeping the insertion order?
    ...


    keys will keep the insertion order.

    Thanks.

    Continue reading...

Compartilhe esta Página