Skip to content

Commit

Permalink
access attributes through @Property
Browse files Browse the repository at this point in the history
  • Loading branch information
JunCEEE committed Oct 15, 2021
1 parent 5c9e0ab commit 9133ba4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
40 changes: 31 additions & 9 deletions libpyvinyl/Instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,47 @@ def __init__(self, name, calculators=None):
:param calculators: A collection of Calculator objects.
:type calculators: dict
"""
self.name = name
self.calculators = {}
self.parameters = InstrumentParameters()
self.__name = name
self.__calculators = {}
self.__parameters = InstrumentParameters()
if calculators is not None:
for calculator in calculators:
self.add_calculator(calculator)

def add_master_parameter(self, name, links, **kwargs):
self.parameters.add_master_parameter(name, links, **kwargs)

@property
def name(self):
"""The name of this instrument."""
return self.__name

@name.setter
def name(self, value: str):
self.__name = value

@property
def calculators(self):
"""The list of calculators. It's modified either when constructing the class instance
or using the `add_calculator` function.
"""
return self.__calculators

@property
def parameters(self):
"""The parameter collection of each calculator in the instrument. These parameters are links to the
exact parameters of each calculator."""
return self.__parameters

@property
def master(self):
return self.parameters.master

def set_base_path(self, base: str):
"""Set each calculator's output_path as 'base_path/calculator.name'.
Args:
base (str): The base path to be set.
:param base: The base path to be set.
:type base: str
"""
self.base_path = base
basePath = Path(base)
Expand All @@ -54,9 +76,9 @@ def list_parameters(self):
print(self.parameters)

def add_calculator(self, calculator):
self.calculators[calculator.name] = calculator
self.parameters.add(calculator.name, calculator.parameters)
self.__calculators[calculator.name] = calculator
self.__parameters.add(calculator.name, calculator.parameters)

def remove_calculator(self, calculator_name):
del self.calculators[calculator_name]
del self.parameters[calculator_name]
del self.__calculators[calculator_name]
del self.__parameters[calculator_name]
21 changes: 13 additions & 8 deletions tests/InstrumentTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class InstrumentTest(unittest.TestCase):
"""
Test class for the Detector class.
"""

@classmethod
def setUpClass(cls):
""" Setting up the test class. """
Expand Down Expand Up @@ -77,13 +76,14 @@ def testRemoveCalculator(self):
self.assertEqual(len(my_instrument.calculators), 2)
my_instrument.remove_calculator(self.calculator1.name)
self.assertEqual(len(my_instrument.calculators), 1)

def testEditCalculator(self):
""" Testing edit calculator """
my_instrument = Instrument('myInstrument')
my_instrument.add_calculator(self.calculator1)
my_instrument.parameters['test1']['photon_energy'] = 15
energy1 = my_instrument.calculators['test1'].parameters['photon_energy'].value
energy1 = my_instrument.calculators['test1'].parameters[
'photon_energy'].value
self.assertEqual(energy1, 15)

def testAddMaster(self):
Expand All @@ -95,8 +95,10 @@ def testAddMaster(self):
links = {'test1': 'photon_energy', 'test2': 'photon_energy'}
my_instrument.add_master_parameter('photon_energy', links)
my_instrument.master['photon_energy'] = 10
energy1 = my_instrument.calculators['test1'].parameters['photon_energy'].value
energy2 = my_instrument.calculators['test2'].parameters['photon_energy'].value
energy1 = my_instrument.calculators['test1'].parameters[
'photon_energy'].value
energy2 = my_instrument.calculators['test2'].parameters[
'photon_energy'].value
self.assertEqual(energy1, 10)
self.assertEqual(energy2, 10)

Expand All @@ -107,8 +109,11 @@ def testSetBasePath(self):
my_instrument.add_calculator(self.calculator1)
my_instrument.add_calculator(self.calculator2)
my_instrument.set_base_path('test')
self.assertEqual(my_instrument.calculators["test1"].output_path, 'test/test1')
self.assertEqual(my_instrument.calculators["test2"].output_path, 'test/test2')
self.assertEqual(my_instrument.calculators["test1"].output_path,
'test/test1')
self.assertEqual(my_instrument.calculators["test2"].output_path,
'test/test2')


if __name__ == '__main__':
unittest.main()

0 comments on commit 9133ba4

Please sign in to comment.