ophyd icon indicating copy to clipboard operation
ophyd copied to clipboard

Consider adding alternate FormattedComponent using string.Template

Open klauer opened this issue 4 years ago • 6 comments

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...)

klauer avatar Apr 15 '20 16:04 klauer

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

danielballan avatar Apr 16 '20 13:04 danielballan

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

I'll leave it at that 🤷‍♂️

klauer avatar Apr 16 '20 16:04 klauer

How about TemproComponent? MacplateComponent?

danielballan avatar Apr 16 '20 18:04 danielballan

^ PSA: This is a joke. :-D

danielballan avatar Apr 16 '20 18:04 danielballan

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

mrakitin avatar Apr 16 '20 19:04 mrakitin

A musical term, as in: senza sordino TemproComponent ma non troppo

Maksim Rakitin wrote:

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

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/bluesky/ophyd/issues/840#issuecomment-614841766, or unsubscribe https://github.com/notifications/unsubscribe-auth/AARMUMCBFNLSKCQBYE5SPHTRM5JTPANCNFSM4MIW3RLQ.

prjemian avatar Apr 16 '20 19:04 prjemian