Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

indirect-vs-inline-python #23

Open
dreftymac opened this issue Mar 11, 2023 · 0 comments
Open

indirect-vs-inline-python #23

dreftymac opened this issue Mar 11, 2023 · 0 comments

Comments

@dreftymac
Copy link

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 with cog.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

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
    • Step002: change the default cog_markup_syntax with the existing --marker option
      • this allows [[[cog ]]] [[[end]]] to become <cogbeg coguri= <cogend>
    • 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" becomes cog.out(MyPyOutputEngine.unique_id_here())
    • Step007: invoke cog
    • Step008: muck around with the cog_enabled_src_file so that cog.out(..) changes back to coggid="..."

See also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant