-
Notifications
You must be signed in to change notification settings - Fork 32
Open
Description
indirect-vs-inline-python
Overview
- indirect-vs-inline-python
- using cog without having to include any python in the cog_enabled_src_file
Context
- python nedbat cog
- most recent stable version as of 2023-03-11
Scenario
- developer MurrayStella wants to invoke cog on a cog_enabled_src_file
- MurrayStella wants to remove the inline python from the cog_enabled_src_file and use indirect python instead
Example
- this simple example illustrates the desired outcome
- MurrayStella wants to convert BEFORE into AFTER
BEFORE
## Simple demo just to show what we want
* just print a hello world message
* a random number of times for variability
* include a random integer for more variability
##@ [[[cog cog.out('Hello {vxx:03d} World!\n'.format(vxx=random.randint(0,999))*random.randint(1,3)) ]]]
Hello 009 World!
Hello 009 World!
##@ [[[end]]]
##@ [[[cog cog.out('Hello {vxx:03d} World!\n'.format(vxx=random.randint(0,999))*random.randint(1,3)) ]]]
Hello 371 World!
##@ [[[end]]]
AFTER
## Simple demo just to show what we want
* just print a hello world message
* a random number of times for variability
* include a random integer for more variability
##@ <cogbeg coggid="uu190hello1678463240" coguri="py003010" >
Hello 772 World!
Hello 772 World!
Hello 772 World!
##@ <cogend>
##@ <cogbeg coggid="uu195hello1678463240" coguri="py003010" >
Hello 371 World!
##@ <cogend>
Feature request
- MurrayStella would like to use the syntax style in the AFTER example instead of BEFORE
- This could be doable with a new cmd_line flag such as
--idmarker=
or--outputmarker=
- this flag is similar to the
--marker
cmd_line flag - instead of changing the cog_markup_syntax as
--marker
does, it would instead remove the need to specify inline python withcog.out("PythonCodeHere")
- it would allow the user to specify any arbitrary prefix and suffix to delimit a cog_code_id
- the cog_code_id could simply reference a variable specified with the
-D name=val
cmd_line flag - this would tell cog to get the auto-generated code from the variable instead of inline python code
- it would also allow arbitrary syntax in the cog_enabled_src_file that does not have to involve any visible python
- this flag is similar to the
Workaround
- MurrayStella has already implemented a hacky sadness-inducing (but functional) workaround to provide this functionality
- Workaround Steps:
- Step001: invoke cog from within python instead of from the cmd_line
- this allows us to do our hacky sadness-inducing postproc and preproc
- capt="seealso" ;; href="Generate multiple output files with cog #19 (comment)"
- Step002: change the default cog_markup_syntax with the existing
--marker
option- this allows
[[[cog ]]] [[[end]]]
to become<cogbeg coguri= <cogend>
- this allows
- Step003: extract out all the coggid unique_ids in the cog_enabled_src_file
- Step004: use those unique_ids to create cog-visible variables using the
-D name=val
cmd_line flag - Step005: assign python-generated output to each of those unique_ids
- this causes sadness because it is brittle
- the
-D name=val
cmd_line flag is not friendly to python-generated output that spans more than one line - there are hacky workarounds for this, but they are less desirable compared to a full-fledged cogscript API
- Step006: muck around with the cog_enabled_src_file so that
coggid="unique_id_here"
becomescog.out(MyPyOutputEngine.unique_id_here())
- Step007: invoke cog
- Step008: muck around with the cog_enabled_src_file so that
cog.out(..)
changes back tocoggid="..."
- Step001: invoke cog from within python instead of from the cmd_line
See also
Metadata
Metadata
Assignees
Labels
No labels