Skip to content

Commit

Permalink
Re #10836 Added method to access DirectEnergyConversion properties
Browse files Browse the repository at this point in the history
directly, without accessing _propMan explicitly
  • Loading branch information
abuts committed Jan 12, 2015
1 parent 2195368 commit 07fb9c1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
49 changes: 45 additions & 4 deletions Code/Mantid/scripts/Inelastic/Direct/DirectEnergyConversion.py
Expand Up @@ -9,7 +9,8 @@

import CommonFunctions as common
import diagnostics
from PropertyManager import PropertyManager;
from PropertyManager import PropertyManager
from ReductionHelpers import extract_non_system_names


def setup_reducer(inst_name,reload_instrument=False):
Expand Down Expand Up @@ -1298,14 +1299,28 @@ def copy_spectrum2monitors(wsName,monWSName,spectraID):
@property
def prop_man(self):
""" Return property manager containing DirectEnergyConversion parameters """
return self._propMan;
return self._propMan
@prop_man.setter
def prop_man(self,value):
""" Assign new instance of direct property manager to provide DirectEnergyConversion parameters """
if isinstance(value,PropertyManager):
self._propMan = value;
self._propMan = value
else:
raise KeyError("Property manager can be initialized by an instance of ProperyManager only")
@property
def spectra_masks(self):
""" check if spectra masks are defined """
if hasattr(self,'_spectra_masks'):
return self._spectra_masks
else:
return None

@spectra_masks.setter
def spectra_masks(self,value):
""" set up spectra masks """
self._spectra_masks = value


#---------------------------------------------------------------------------
# Behind the scenes stuff
#---------------------------------------------------------------------------
Expand All @@ -1314,16 +1329,42 @@ def __init__(self, instr_name=None,reload_instrument=False):
"""
Constructor
"""
all_methods = dir(self)
# define list of all existing properties, which have descriptors
object.__setattr__(self,'_descriptors',extract_non_system_names(all_methods))

if instr_name:
self.initialise(instr_name,reload_instrument);
else:
self._propMan = None;
#
self._keep_wb_workspace = False;
self._do_ISIS_normalization = True;
self.spectra_masks = None;
self._spectra_masks = None;
#end

def __getattr__(selv,attr_name):
""" overloaded to return values of properties non-existing in the class dictionary from the property manager class except this
property already have descriptor
"""
if attr_name in self._descriptors:
return object.__getattr__(self,attr_name)
else:
return getattr(self._propMan,attr_name)

def __setattr__(self,attr_name,attr_value):
""" overloaded to prohibit adding non-starting with _properties to the class instance
and add all other properties to property manager except this property already have descriptor
"""
if attr_name[0] == '_':
object.__setattr__(self,attr_name,attr_value)
else:
if attr_name in self._descriptors:
object.__setattr__(self,attr_name,attr_value)
else:
setattr(self._propMan,attr_name,attr_value)



def initialise(self, instr,reload_instrument=False):
"""
Expand Down
12 changes: 7 additions & 5 deletions Code/Mantid/scripts/Inelastic/Direct/PropertyManager.py
Expand Up @@ -35,16 +35,18 @@ class PropertyManager(NonIDF_Properties):
1) Simple properties from IDF are stored in class dictionary in the form __dict__[property name]=property value
2) Complex properties from IDF implemented as instances of ReductionHelpers.ComplexProperty class and are stored
in class dictionary in the form __dict__[_property name] = ReductionHelpers.ComplexProperty([dependent properties list])
2) Complex properties from IDF are generated as instances of ReductionHelpers.ComplexProperty class and stored
in class dictionary in the form __dict__[_property_name] = ReductionHelpers.ComplexProperty([dependent properties list])
(note underscore in front of property name)
__getattr__ and __setattr__ are overloaded to understand such calls
__getattr__ and __setattr__ are overloaded to understand such calls. The property_name itself is naturally not placed into
system dictionary.
3) Descriptors with the name present in IDF do not store their values and names in __dict__
(the name is removed during IDF parsing) but keep their information in the descriptor.
This is not considered a problem as only one instance of property manager is expected.
This is not considered a problem as only one instance of property manager is expected. If this need to be changed,
adding property values to the __dict__ as values of _property_name keys should be safe.
4) __getattr__ (and __setattr__ ) method is overloaded to provide call to descriptor before the search in the system dictionary.
4) __getattr__ (and __setattr__ ) method are overloaded to provide call to a descriptor before the search in the system dictionary.
Custom __getattr__ naturally works only if Python does not find a property name in the __dict__ or __class__.__dict__ (and mro()),
e.g. in case when an descriptor is called through one of its synonym name.
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/scripts/test/ReductionWrapperTest.py
@@ -1,5 +1,5 @@
import os,sys
#os.environ["PATH"] = r"c:/Mantid/Code/builds/br_10803/bin/Release;"+os.environ["PATH"]
os.environ["PATH"] = r"c:/Mantid/Code/builds/br_10803/bin/Release;"+os.environ["PATH"]

from mantid.simpleapi import *
from mantid import api
Expand Down

0 comments on commit 07fb9c1

Please sign in to comment.