https://yamlscript.org/posts/advent-2023/dec-21/ [yamlscript] * Blog * About YAML, Python and the Holy Graal by Ingy dot Net | 21 Dec 2023 | 4 min read Which has a greater airspeed velocity... an unladen swallow or Santa's sleigh? Well, that depends... are we talking about an African or European swallow? Huh? Welcome to Day 21 of the YAMLScript Advent Blog! So far we've been using the YAMLScript CLI ys to run (or load) our YAMLScript programs. YAML users are used to using a YAML framework module inside their programs. For example, in Python you might do: import yaml yaml_text = """ - 40 - 50 - 60 """ data = yaml.safe_load(yaml_text) print(data) # => [40, 50, 60] Wouldn't it be nice if we could do the same thing in YAMLScript? As of today, we can! import yamlscript yaml_text = """ - 40 - 50 - 60 """ data = yamlscript.load(yaml_text) print(data) # => [40, 50, 60] The only thing that changed was the name of the module. But this module has super powers. import yamlscript yaml_text = """ !yamlscript/v0 mapv \(% * 10): 4..6 """ data = yamlscript.load(yaml_text) print(data) # => [40, 50, 60] We can use YAMLScript functions in our YAML text to generate or manipulate data. That example was a bit contrived, but I just wanted to show how easy it is to load plain old YAML or super powered YAML with the new yamlscript Python module. Here's an example that might be more exciting. Say we have this normal YAML file with some data in it: # db.yaml cars: - make: Ford model: Mustang year: 1967 color: red - make: Dodge model: Charger year: 1969 color: orange - make: Chevrolet model: Camaro year: 1969 color: blue We could have another YAML file that uses YAMLScript: # racers.yaml !yamlscript/v0 db =: load("db.yaml") =>: ! - name: Ingy dot Net car: ! db.cars.0 - name: Santa Claus car: ! db.cars.1 - name: Sir Lancelot car: ! db.cars.2 Then we could load the data into Python and print it out: # race-report.py import yaml, yamlscript data = yamlscript.load('racers.yaml') print(yaml.dump(data)) And we get: - name: Ingy dot Net car: make: Ford model: Mustang year: 1967 color: red - name: Santa Claus car: make: Dodge model: Charger year: 1969 color: orange - name: Sir Lancelot car: make: Chevrolet model: Camaro year: 1969 color: blue Pretty cool, huh? There's no end to the things you can do with this. Today we're showing off the Python YAMLScript module but soon this module will be available in every language that has a need for it. Installing the yamlscript Python Module Normally to install a Python module you would do: pip install a-python-module With YAMLScript, you do the same: pip install yamlscript But you also need to install the libyamlscript shared library. You can do that with: curl -s https://yamlscript.org/install-libyamlscript | sudo bash That will install the latest version of libyamlscript for your platform in /usr/local/lib. If you want to install it somewhere else, you can set the PREFIX option: curl -s https://yamlscript.org/install-libyamlscript | PREFIX=~/ys bash But then you'll need to set the LD_LIBRARY_PATH environment variable to point to it: export LD_LIBRARY_PATH=~/ys/lib Eventually we may package the yamlscript.py module with wheels (binary assets) for libyamlscript, but for now you'll need to install it yourself. If you're a polyglot like me, at least you only have to install it once. :- ) The Holy Graal This magic is all possible because of the GraalVM project. Not only does GraalVM's native-image tool compile to binary executables, it also can compile to shared libraries. YAMLScript generates and publishes the libyamlscript shared library and then offers binding modules for it in many languages. --------------------------------------------------------------------- I hope you are starting to see the power of YAMLScript. Not only as a new programming language, but also as a new way to work with YAML files that you already have. Join me tomorrow for Day 22 of the YAMLScript Advent Blog! Santa's little secrets... In December 2023 YAMLScript is a work in progress. This is not to say that it's a toy proof of concept. It has a working compiler, runtime and CLI. It has unit tests that must pass for every commit pushed to the main branch. It's well thought out and has a clear direction that I continually vet with a small group of talented and trusted people. It has regular releases. But that in turn not to say that you should use it in production today. I encourage you to start playing with it, and I'm committed to backwards compatibility for stable releases. But we're not quite there yet. Caveat usor! YAMLScript is evolving fast. While I'm writing these posts, I'm also writing the code for new features and fixing bugs as I go. I've had the Advent plan for over a month now, and I had planned to be further along before December 1st, but such are the time estimates of hackers. I am attempting to make sure that the code examples in these posts are always using the implemented features, but sometimes I may fall a few hours behind. Often I need to write the examples using code patterns that actually work but are not the ones I preferred to best make a point. As I fix bugs, implement features and tweak the language, I will update the posts accordingly. If you see something that bugs you, try taking another look after a few days. It probably bugged me too! Be patient with me. If I had to guess, I'd say that YAMLScript v0 will be have a stable release sometime before March 2024. I'll continue blogging the progress as I go, so it shouldn't be hard for you to decide when to start using YAMLScript for real. Overall the whole thing is going extremely well, and I'm having a lot of fun. It's important to be transparent with you about where things are at. Stick with me... This is going to be awesome! -- Ingy dot Net #blog #advent-2023 - Godspeed 2023 Advent Index - * GitHub * RSS Powered by Eleventy. Theme: Eleventy Duo.