Skip to content

Commit

Permalink
adding a custom __dir__() method to the PrettyStr... classes to restr…
Browse files Browse the repository at this point in the history
…ict the options to signals and the read method
  • Loading branch information
awalter-bnl committed Jun 19, 2024
1 parent 9f3acd1 commit ed6e3e9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 19 deletions.
Binary file modified src/ari_sxn_common/__pycache__/common_ophyd.cpython-312.pyc
Binary file not shown.
88 changes: 69 additions & 19 deletions src/ari_sxn_common/common_ophyd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,35 @@

class PrettyStrForDevices():
"""
A class that provides a better string when using `print(obj)`
A class that provides a better print and tab-to-complete functionality`
This class has a custom `__str__()` method that returns a formatted string
that includes the device name as well as child signals grouped by the
`signal._ophyd_labels_` list.
Parameters
----------
None
This class also has a custom `__dir__()` method that returns a list of
attribute names giving the required options when using tab-to-complete. This
list contains all of the signals found in `self._signals.keys()` as well as
the `read()` method.
Methods
-------
__str__() :
Returns a formatted string indicating it's name and all of the child
signals grouped by their `_ophyd_labels_`.
__dir__() :
Returns a list of attribute name strings to be used to define what
options are available when doing tab-to-complete.
"""
def __str__(self):
"""
Updates the __str__() method to provide the formatted string described
in the class definition.
Returns
-------
output : str
A formatted string that should be printed when using print(self)
"""
signals = defaultdict(list)
if hasattr(self, '_signals'):
Expand Down Expand Up @@ -58,31 +67,56 @@ def __str__(self):

return output

def __dir__(self):
"""
Used to limit the number of options when using tab to complete.
This method is used to give the list of options when using pythons tab
to complete process. It gives all of the signal attributes (motors and
detectors) as well as the 'read' method.
Returns
-------
attribute_list : list[str]
A list of attribute names to be included when using tab-to-complete
"""
attribute_list = ['read']
attribute_list.extend([key for key in self._signals.keys()])

return attribute_list


class PrettyStrForSignal():
"""
A class that provides a better string when using `print(PrettyStr)`
A class that provides a better string when using `print(PrettyStr)`
This class has a custom `__str__()` method that returns a formatted
string that includes the device name as well as the
`signal._ophyd_labels_` list. It is designed for use with the
`PrettyStrForDevices` class but on hte lowest level signals that should
be accessed by users.
This class has a custom `__str__()` method that returns a formatted string
that includes the device name as well as the `signal._ophyd_labels_` list.
It is designed for use with the `PrettyStrForDevices` class but on the
lowest level signals that should be accessed by users.
Parameters
----------
None
This class also has a custom `__dir__()` method that returns a list of
attribute names giving the required options when using tab-to-complete. This
list contains only the `read()` method.
Methods
-------
__str__() :
Returns a formatted string indicating it's name and it's
`_ophyd_labels_`.
"""
Methods
-------
__str__() :
Returns a formatted string indicating it's name and it's
`_ophyd_labels_`.
__dir__() :
Returns a list of attribute name strings to be used to define what
options are available when doing tab-to-complete.
"""

def __str__(self):
"""
Updating the __str__ function to return 'name (label)'
Returns
-------
output : str
A formatted string that should be printed when using print(self)
"""

try:
Expand All @@ -91,6 +125,22 @@ def __str__(self):
self_label = {'unknown', }
return f'{self.name} ({str(self_label)[1:-1]})'

def __dir__(self):
"""
Used to limit the number of options when using tab to complete.
This method is used to give the list of options when using pythons tab
to complete process. It gives only the 'read' method.
Returns
-------
attribute_list : list[str]
A list of attribute names to be included when using tab-to-complete
"""
attribute_list = ['read']

return attribute_list


class ID29EpicsMotor(PrettyStrForSignal, EpicsMotor):
"""
Expand Down

0 comments on commit ed6e3e9

Please sign in to comment.