So tonight I rediscovered Hy. I had seen Hy before a while ago but never really sat down and tried it. Tonight just must have been one of those days to try something new.
So Hy is a dialect of Lisp but embedded in Python which means you can use any Python library will using a Lisp dialect. Pretty nifty.
My next thought was, how would this look using the QGIS libraries. So lets give it a try.
First we need to install Hy:
pip install Hy
Now just create a .hy file and add some code
(importqgis)(import[qgis.core[QgsVectorLayer]])(import[qgis.core.contextmanagers[qgisapp]])(setvlayers[])(defnload-layer[filename](setvlayer(QgsVectorLayerfilename"ogr"))(.appendlayerslayer))(defnprint-layer[layer](print"Layer Name:"(.namelayer))(print"Valid:"(.isValidlayer))(print"Extents:"(.toString(.extentlayer))))(defnmain[app](load-layerr"F:\gis_data\test.shp""test")(for[layerlayers](print-layerlayer)))(with[[app(applyqgisapp[]{"guienabled"False})]](print"Loading QGIS")(mainapp))
run it in our shell and bingo.
F:\dev\hy-qgis>hyqgistest.hyLoadingQGISLayerName:testValid:TrueExtents:392515.3457026787800714,6461581.2076761415228248:392683.3794420150225051,6461705.1012571481987834
Sweet.
Just for reference the Python version of the above would be:
importqgisfromqgis.coreimportQgsVectorLayerfromqgis.core.contextmanagersimportqgisapplayers=[]defload_layer(file,name):layer=QgsVectorLayer(file,name,"ogr")layers.append(layer)defprint_layer(layer):print"Layer Name:",layer.name()print"Valid:",layer.isValid()print"Extents:",layer.extent().toString()defmain(app):load_layer(r"F:\gis_data\test.shp","test")forlayerinlayers:print_layer(layer)withqgisappl(guienabled=False)asapp:main(app)
More readable? No doubt, that is why I love Python, however the strange thing is the first time I looked at Lisp, including Hy, I thought "whoa all those parentheses back it up!1!" but strangely after using it for a while (read: not even a few hours) they don't seem to be an issue, or not much of one anyway. YMMV.
The cool thing with using Hy is you can still use all the libraries you are used to as the example above shows, PyQt, QGIS, anything.
The other interesting, and pretty funky, thing is that you are able to import .hy files like normal Python files. If you create a file winning.hy
you can just import winning
into any Python application and it works.
Why bother? Mainly because learning something new is never a bad thing, and you never know what you might pick up.
Check out the Hy for more info on what you can do and how it works
I have also created a hy-qgis GitHub repo for some experiments.
Enjoy!