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

Passing parameters containing commas #36

Open
psychemedia opened this issue Mar 7, 2018 · 1 comment
Open

Passing parameters containing commas #36

psychemedia opened this issue Mar 7, 2018 · 1 comment

Comments

@psychemedia
Copy link

If I pass a sentence containing commas in to a modifier, the params are split on simple commas:

rules={
    'origin': '''here's my sentence: #test.uppercase.echo(this is, my sentence, "with a comma, ok?")#''',
    'test': 'Ping'
}

def myecho(*params):
    return '::'.join(params)

grammar_test= {
    'echo': myecho
}

grammar = tracery.Grammar(rules)
grammar.add_modifiers(base_english)
grammar.add_modifiers(grammar_test)
print(grammar.flatten("#origin#"))

here's my sentence: PING::this is:: my sentence:: "with a comma:: ok?"

It would be useful to be able to pass in strings containing commas:

e.g.

import shlex

line='This is, a line, "with a comma, ok?"'
shlex.split(line)
['This', 'is,', 'a', 'line,', 'with a comma, ok?']

rather than:

line.split(',')
['This is', ' a line', ' "with a comma', ' ok?"']
@psychemedia
Copy link
Author

psychemedia commented May 6, 2018

Here's a better approach...

import csv

def parse_params(txt):
    return [i for i in csv.reader([txt], skipinitialspace=True)][0]

(or maybe i.strip() for i...?)

Then in Node.expand() use parse_params(matches[0]) rather than matches[0].split(",") and in NodeAction.activate() use parse_params(self.rule)rather than self.rule.split(",") ?

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