Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I query a YAML dataset in Python?

Similar to Is there a query language for JSON? and the more specific How can I filter a YAML dataset with an attribute value? - I would like to:

  • hand-edit small amounts data in YAML files
  • perform arbitrary queries on the complete dataset (probably in Python, open to other ideas)
  • work with the resulting subset in Python

It doesn't appear that PyYAML has a feature like this, and today I can't find the link I had to the YQuery language, which wasn't a mature project anyway (or maybe I dreamt it).

Is there a (Python) library that offers YAML queries? If not, is there a Pythonic way to "query" a set of objects other than just iterating over them?

like image 654
lofidevops Avatar asked Feb 10 '26 19:02

lofidevops


2 Answers

bootalchemy provides a means to do this via SQLAlchemy. First, define your schema in a SQLAlchemy model. Then load your YAML into a SQLAlchemy session using bootalchemy. Finally, perform queries on that session. (You don't have to commit the session to an actual database.)

Example from the PyPI page (assume model is already defined):

from bootalchemy.loader import Loader

# (this simulates loading the data from YAML)
data = [{'Genre':[{'name': "action",
                   'description':'Car chases, guns and violence.'
                  }
                 ]
        }
       ]

# load YAML data into session using pre-defined model
loader = Loader(model)
loader.from_list(session, data)

# query SQLAlchemy session
genres = session.query(Genre).all()

# print results
print [(genre.name, genre.description) for genre in genres]

Output:

[('action', 'Car chases, guns and violence.')]
like image 132
lofidevops Avatar answered Feb 13 '26 10:02

lofidevops


I don't thing there is a direct way to do it. But PyYAML reads yaml files into a dict representing everything in the file. Afterwards you can execute all dict related operations. The question python query keys in a dictionary based on values mentions some pythonic "query" styles.

like image 37
tea2code Avatar answered Feb 13 '26 09:02

tea2code



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!