def test_trait_utils2(self): trait = traits.CTrait(0) trait.handler = traits.Float() trait.ouptut = True trait.optional = False manhelp = get_trait_desc("float_trait", trait, 5) self.assertEqual( manhelp[0], "float_trait: a float (['Float'] - mandatory, default value: 5)") self.assertEqual(manhelp[1], " No description.")
def test_trait_utils4(self): trait = traits.Either(traits.Int(47), traits.Str("vovo")).as_ctrait() trait.ouptut = False trait.optional = False manhelp = get_trait_desc("choice", trait, None) desc = ' '.join([x.strip() for x in manhelp[:-1]]) self.assertTrue( desc in ("choice: an integer (int or long) or a string " "(['Int', 'Str'] - mandatory)", "choice: an integer or a string " "(['Int', 'Str'] - mandatory)")) self.assertEqual(manhelp[-1], " No description.")
def test_trait_utils1(self): """ Method to test if we can build a string description for a trait. """ trait = traits.CTrait(0) trait.handler = traits.Float() trait.ouptut = False trait.optional = True trait.desc = "bla" manhelp = get_trait_desc("float_trait", trait, 5) self.assertEqual( manhelp[0], "float_trait: a float (['Float'] - optional, default value: 5)") self.assertEqual(manhelp[1], " bla")
def test_trait_string_description(self): """ Method to test if we can build a string description for a trait. """ trait = CTrait(0) trait.handler = Float() trait.ouptut = False trait.optional = True trait.desc = "bla" manhelp = get_trait_desc("float_trait", trait, 5) self.assertEqual( manhelp[0], "float_trait: a float (['Float'] - optional, default value: 5)") self.assertEqual(manhelp[1], " bla")
def test_trait_utils3(self): class Blop(object): pass trait = traits.CTrait(0) trait.handler = traits.Instance(Blop()) trait.ouptut = False trait.optional = False manhelp = get_trait_desc("blop", trait, None) desc = ' '.join([x.strip() for x in manhelp[:-1]]) self.assertEqual( desc, "blop: a Blop or None (['Instance_%s.Blop'] - mandatory)" % Blop.__module__) self.assertEqual(manhelp[-1], " No description.")
def get_output_help(self, rst_formating=False): """ Generate description for process output parameters. Parameters ---------- rst_formating: bool (optional, default False) if True generate a rst table witht the input descriptions. Returns ------- helpstr: str the trait output help descriptions """ # Generate an output section helpstr = ["Outputs", "~" * 7, ""] # Get all the process output traits items = self.traits(output=True) # If we have no output trait, return no string description if not items: return [""] # If we have some outputs, get the corresponding string # descriptions data = [] for trait_name, trait in six.iteritems(items): data.append( get_trait_desc(trait_name, trait)) # If we want to format the output nicely (rst) if data != []: if rst_formating: helpstr += self._rst_table(data) # Otherwise else: helpstr += functools.reduce(operator.add, data) return helpstr
def get_output_help(self, rst_formating=False): """ Generate description for process output parameters. Parameters ---------- rst_formating: bool (optional, default False) if True generate a rst table witht the input descriptions. Returns ------- helpstr: str the trait output help descriptions """ # Generate an output section helpstr = ["Outputs", "~" * 7, ""] # Get all the process output traits items = self.traits(output=True) # If we have no output trait, return no string description if not items: return [""] # If we have some outputs, get the corresponding string # descriptions data = [] for trait_name, trait in items.iteritems(): data.append( get_trait_desc(trait_name, trait)) # If we want to format the output nicely (rst) if data != []: if rst_formating: helpstr += self._rst_table(data) # Otherwise else: helpstr += reduce(operator.add, data) return helpstr
def get_input_help(self, rst_formating=False): """ Generate description for process input parameters. Parameters ---------- rst_formating: bool (optional, default False) if True generate a rst table witht the input descriptions. Returns ------- helpstr: str the class input traits help """ # Generate an input section helpstr = ["Inputs", "~" * 6, ""] # Markup to separate mandatory inputs manhelpstr = ["[Mandatory]", ""] # Get all the mandatory input traits mandatory_items = dict([ x for x in six.iteritems(self.user_traits()) if not x[1].output and not x[1].optional ]) mandatory_items.update(self.traits(output=None, optional=False)) # If we have mandatory inputs, get the corresponding string # descriptions data = [] if mandatory_items: for trait_name, trait in six.iteritems(mandatory_items): trait_desc = get_trait_desc(trait_name, trait) data.append(trait_desc) # If we want to format the output nicely (rst) if data != []: if rst_formating: manhelpstr += self._rst_table(data) # Otherwise else: manhelpstr += functools.reduce(operator.add, data) # Markup to separate optional inputs opthelpstr = ["", "[Optional]", ""] # Get all optional input traits optional_items = self.traits(output=False, optional=True) optional_items.update(self.traits(output=None, optional=True)) # If we have optional inputs, get the corresponding string # descriptions data = [] if optional_items: for trait_name, trait in six.iteritems(optional_items): data.append(get_trait_desc(trait_name, trait)) # If we want to format the output nicely (rst) if data != []: if rst_formating: opthelpstr += self._rst_table(data) # Otherwise else: opthelpstr += functools.reduce(operator.add, data) # Add the mandatry and optional input string description if necessary if mandatory_items: helpstr += manhelpstr if optional_items: helpstr += opthelpstr return helpstr
def test_controller4(self): class Driver(Controller): head = traits.Str() arms = traits.Str() legs = traits.Str() class Car(Controller): wheels = traits.Str() engine = traits.Str() driver = ControllerTrait(Driver(), desc='the guy who would better take a ' 'bus') problems = ControllerTrait(OpenKeyController(traits.Str())) my_car = Car() my_car.wheels = 'flat' my_car.engine = 'wind-broken' my_car.driver.head = 'empty' # ! modify class trait ! my_car.driver.arms = 'heavy' my_car.driver.legs = 'short' my_car.problems = {'exhaust': 'smoking', 'windshield': 'cracked'} d = my_car.export_to_dict() self.assertEqual( d, { 'wheels': 'flat', 'engine': 'wind-broken', 'driver': { 'head': 'empty', 'arms': 'heavy', 'legs': 'short' }, 'problems': { 'exhaust': 'smoking', 'windshield': 'cracked' } }) self.assertTrue(isinstance(my_car.driver, Driver)) self.assertTrue(isinstance(my_car.problems, OpenKeyController)) my_car.driver = {'head': 'smiling', 'legs': 'strong'} d = my_car.export_to_dict() self.assertEqual( d, { 'wheels': 'flat', 'engine': 'wind-broken', 'driver': { 'head': 'smiling', 'arms': '', 'legs': 'strong' }, 'problems': { 'exhaust': 'smoking', 'windshield': 'cracked' } }) other_car = my_car.copy(with_values=True) self.assertEqual(other_car.export_to_dict(), d) other_car = my_car.copy(with_values=False) self.assertEqual( other_car.export_to_dict(), { 'wheels': '', 'engine': '', 'driver': { 'head': 'empty', 'arms': 'heavy', 'legs': 'short' }, 'problems': {} }) self.assertRaises(traits.TraitError, setattr, my_car.problems, 'fuel', 3.5) del my_car.problems.fuel self.assertEqual(sorted(my_car.problems.user_traits().keys()), ['exhaust', 'windshield']) manhelp = get_trait_desc('driver', my_car.trait('driver')) self.assertEqual( manhelp[0], "driver: a legal value (['ControllerTrait'] - mandatory)") self.assertEqual(manhelp[1], " the guy who would better take a bus")
def get_input_help(self, rst_formating=False): """ Generate description for process input parameters. Parameters ---------- rst_formating: bool (optional, default False) if True generate a rst table witht the input descriptions. Returns ------- helpstr: str the class input traits help """ # Generate an input section helpstr = ["Inputs", "~" * 6, ""] # Markup to separate mandatory inputs manhelpstr = ["[Mandatory]", ""] # Get all the mandatory input traits mandatory_items = dict([x for x in self.user_traits().iteritems() if not x[1].output and not x[1].optional]) mandatory_items.update(self.traits(output=None, optional=False)) # If we have mandatory inputs, get the corresponding string # descriptions data = [] if mandatory_items: for trait_name, trait in mandatory_items.iteritems(): trait_desc = get_trait_desc(trait_name, trait) data.append(trait_desc) # If we want to format the output nicely (rst) if data != []: if rst_formating: manhelpstr += self._rst_table(data) # Otherwise else: manhelpstr += reduce(operator.add, data) # Markup to separate optional inputs opthelpstr = ["", "[Optional]", ""] # Get all optional input traits optional_items = self.traits(output=False, optional=True) optional_items.update(self.traits(output=None, optional=True)) # If we have optional inputs, get the corresponding string # descriptions data = [] if optional_items: for trait_name, trait in optional_items.iteritems(): data.append( get_trait_desc(trait_name, trait)) # If we want to format the output nicely (rst) if data != []: if rst_formating: opthelpstr += self._rst_table(data) # Otherwise else: opthelpstr += reduce(operator.add, data) # Add the mandatry and optional input string description if necessary if mandatory_items: helpstr += manhelpstr if optional_items: helpstr += opthelpstr return helpstr