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

Consider adding alternate FormattedComponent using string.Template #840

Closed
klauer opened this issue Apr 15, 2020 · 6 comments
Closed

Consider adding alternate FormattedComponent using string.Template #840

klauer opened this issue Apr 15, 2020 · 6 comments

Comments

@klauer
Copy link
Member

klauer commented Apr 15, 2020

This would more closely mimic how epics-base does string interpolation.
https://docs.python.org/3/library/string.html#template-strings

Copying in the documentation relevant portions and example:

  • $identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" is restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter. The first non-identifier character after the $ character terminates this placeholder specification.

  • ${identifier} is equivalent to $identifier. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification".

  • $$ is an escape; it is replaced with a single $.

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

(Had I known it existed in 2015, I would've used it in the first place...)

@danielballan
Copy link
Member

Should we call it TemplatedComponent? Easy enough to explain that FormattedComponent uses str.format and TemplatedComponent uses string.Template.

@klauer
Copy link
Member Author

klauer commented Apr 16, 2020

TemplatedComponent would be the Python name
MacroComponent would be the EPICS name

I'll leave it at that 🤷‍♂️

@danielballan
Copy link
Member

How about TemproComponent? MacplateComponent?

@danielballan
Copy link
Member

^ PSA: This is a joke. :-D

@mrakitin
Copy link
Member

I thought it's a pair of names from the latest Star Wars 😄

@prjemian
Copy link
Contributor

prjemian commented Apr 16, 2020 via email

@klauer klauer closed this as not planned Won't fix, can't repro, duplicate, stale Nov 9, 2023
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

4 participants