3 ms·
I get it but I don't get it. I have seen cog before and respect ned so I assume there are sound uses, but the demo versions are trivial and Instruggle to see h
by lifeisstillgood 3y ago
I get it but I don't get it. I have seen cog before and respect ned so I assume there are sound uses, but the demo versions are trivial and Instruggle to see how I can use it for more complex cases.
My thoughts are the python code will be most effective if it can read config or access chunks of data outside the source (ie auto-generating fetter setter style stuff). I am trying to see how much is dangerous and how much is trivial or better handled pre-commit
- jmopp 3y agoFor my work I often run a lot of queries with a similar structure, but not repetitive enough that I can just have one query. What I normally do is paste the query template in a Python REPL and write some code to generate all my queries, which I then output to a file that I can call before ending work and leave running through the night. This would allow me to do the same thing, but in a more reproducible way that I can, for instance, share with my teammates.
- pseudony 3y agoHaving written something similar ( https://pypi.org/project/gcgen/ https://pypi.org/project/gcgen/ ), it is sometimes hard to provide examples without being overly specific. However, some "meta" examples: * If you have an authoritative source - a protobuf/openai/some other structured specification, then this is helpful (if there isn't a specific generator already). * Whenever you're knee-deep in meta-classes in Python, generating code from annotations in Java or similar - then this might actually be better - the code will likely be simpler and therefore easier for teammates to maintain as well.
- DriftRegion 3y ago> My thoughts are the python code will be most effective if it can read config or access chunks of data outside the source Agreed. I've used jinja2 quite a bit for templating C with structured data. It is very effective. Ned writes that he also considered templating engines but was not satisfied when trying to implement more complex code generation inline. I feel like I understand Ned's thought process here but I strongly disagree with his conclusion unless it's for personal use only. Cog is subject to the lisp curse.
- lifthrasiir 3y agoAs a concrete example, I would definitely use Cog for generating this kind of code: https://github.com/libjxl/libjxl/blob/main/lib/jxl/dct_scales.h https://github.com/libjxl/libjxl/blob/main/lib/jxl/dct_scale... Note that the current source code already contains a Python code used to generate them. The code is there for future uses, but it would be too much to have an additional build step and Python dependency just for that particular file, considering that this file will rarely change. Cog would be very useful in such situations.
- tjoff 3y agoIt would be to much to have an additional build step and Python dependency for a file that never changes, but not too much to add cog and python as dependencies and then slow down the build for all users?
- lifthrasiir 3y agoCog can be run on demand, unlike build steps. Only some subset of developers that would touch those files need to have Python installed.
- hprotagonist 3y agoI have a target docs: ${VENV_PYTHON} -m cog -r ./README.md and readme.md has cog statements in it which fully exercise my CLI’s help documentation, and every subcommand’s help, and prints them out as source blocks.