Skip to content

Implement better typing in EpicsName PandAName PviName #90

Open
@evvaaaa

Description

@evvaaaa

Currently we define types for each of the above names:

# EPICS format, i.e. ":" dividers
EpicsName = NewType("EpicsName", str)
# PandA format, i.e. "." dividers
PandAName = NewType("PandAName", str)
# No dividers and PascalCase
PviName = NewType("PviName", str)
def panda_to_epics_name(field_name: PandAName) -> EpicsName:
"""Convert PandA naming convention to EPICS convention. This module defaults to
EPICS names internally, only converting back to PandA names when necessary."""
return EpicsName(field_name.replace(".", ":"))
def epics_to_panda_name(field_name: EpicsName) -> PandAName:
"""Convert EPICS naming convention to PandA convention. This module defaults to
EPICS names internally, only converting back to PandA names when necessary."""
return PandAName(field_name.replace(":", "."))
def epics_to_pvi_name(field_name: EpicsName) -> PviName:
"""Converts EPICS naming convention to PVI naming convention.
For example PANDA:PCAP:TRIG_EDGE -> TrigEdge."""
relevant_section = field_name.split(":")[-1]
words = relevant_section.replace("-", "_").split("_")
capitalised_word = "".join(word.capitalize() for word in words)
# We don't want to allow any non-alphanumeric characters.
formatted_word = re.search(r"[A-Za-z0-9]+", capitalised_word)
assert formatted_word
return PviName(formatted_word.group())

But when used at runtime we don't have restrictions, and everything is a str.

We should turn these types into pydantic BaseModel and validate each against a regex schema on initialisation.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions