0

I have been looking into YAML and the Python parsing options with PyYAML. I kind of understand how it works but still have a question regarding the process:

Is it possible to directly update an item inside the YAML file without parsing the whole file, creating a dictionary for everything, operating on that dictionary and then dumping it back?

HOUSE:
 - white
APPLE:
 - red
BANANA:
- yellow

Let say that I want to make the APPLE "green", is that possible by only operating on the APPLE object, and not working on the whole dictionary?

Thanks.

mbilyanov
  • 2,315
  • 4
  • 29
  • 49
  • If you don't want to parse the entire file, you can't really have an APPLE object. You can read and write the file's text, but that would kind of defeat the purpose of using a configuration file. – Roman Levin Sep 08 '14 at 11:54
  • By the way, here is a great technique to have access to a Python dictionary as an object. Not sure if it has something to do with my question, but might be helpful to integrate a solution: http://stackoverflow.com/questions/1305532/convert-python-dict-to-object – mbilyanov Sep 08 '14 at 11:55

1 Answers1

0

Ok. I think Roman is right. I was asking this cause I was worried about the overhead of complex YAML objects. But, I guess, If things are becoming complex, then one should switch to a db solution, like MongoDB or a like. Once YAML is kept simple, the serialisation and de-serialisation overhead should bot be a huge issues.

mbilyanov
  • 2,315
  • 4
  • 29
  • 49
  • Jumping from YAML to MongoDB is a bit of a leap. The limiting factor here isn't complexity, it's performance. If you are using YAML as an object store, then yeah, that's not what YAML files are for - but you should check out Sqlite before you spring for a dedicated solution. Sqlite runs in the same process as your code and does not need any configuration outside of your application. As long as you're not reading and writing tens of megabytes every second, it should be more than enough. – Roman Levin Sep 08 '14 at 15:51