def __init__(self, *args, **kwargs): """ Initializing mcstas parameter object Parameters ---------- Positional argument 1: type : str Type of the parameter, double, int or string Positional argument 2: name : str Name of input parameter Keyword arguments array : int length of array to be allocated, 0 if single value value : any sets initial value of parameter, can be a list with length matching array comment : str sets comment displayed next to declaration """ self.type = args[0] self.name = str(args[1]) par_name = self.name if "*" in par_name[0]: # Remove any number of prefixed *, indicating variable is a pointer par_name = par_name.split("*")[-1] elif "&" in par_name[0]: # Remove the first & indicating the variable is an address par_name = par_name[1:] if not is_legal_parameter(par_name): raise NameError("The given parameter name: \"" + self.name + "\" is not a legal c variable name, " + " and cannot be used in McStas.") self.value = "" if "value" in kwargs: self.value = kwargs["value"] self.vector = 0 if "array" in kwargs: self.vector = kwargs["array"] self.comment = "" if "comment" in kwargs: self.comment = " // " + kwargs["comment"]
def __init__(self, *args, **kwargs): """Initializing mcstas parameter object Parameters ---------- If giving a type: Positional argument 1: type: str Type of the parameter, double, int or string Positional argument 2: name: str Name of input parameter If not giving type Positional argument 1: name : str Name of input parameter Keyword arguments value : any sets default value of parameter comment : str sets comment displayed next to declaration """ if len(args) == 1: self.type = "" self.name = str(args[0]) if len(args) == 2: self.type = args[0] + " " self.name = str(args[1]) if not is_legal_parameter(self.name): raise NameError("The given parameter name: \"" + self.name + "\" is not a legal c variable name, " + " and cannot be used in McStas.") self.value = "" if "value" in kwargs: self.value = kwargs["value"] self.comment = "" if "comment" in kwargs: self.comment = "// " + kwargs["comment"]
def test_is_legal_parameter_reject_empty(self): """ An empty string should not be a legal name """ test_name = "" self.assertFalse(is_legal_parameter(test_name))
def test_is_legal_parameter_reject_first_number(self): """ The first character being a number is ilegal """ test_name = "2est_parameter_name" self.assertFalse(is_legal_parameter(test_name))
def test_is_legal_parameter_reject_space(self): """ A space should make the parameter name illegal """ test_name = "test_parameter name" self.assertFalse(is_legal_parameter(test_name))
def test_is_legal_parameter_simple(self): """ Check a legal parameter is legal """ test_name = "test_parameter_name1" self.assertTrue(is_legal_parameter(test_name))
def __init__(self, *args, **kwargs): """Initializing mcstas parameter object Examples -------- Creates a parameter with name wavelength and associated comment A = ParameterVariable("wavelength", comment="wavelength in [AA]") Creates a parameter with name A3 and default value A = ParameterVariable("A3", value=30, comment="A3 angle in [deg]") Creates a parameter with type string and name sample_name A = ParameterVariable("string", "sample_name") Parameters ---------- If giving a type: Positional argument 1: type: str Type of the parameter, double, int or string Positional argument 2: name: str Name of input parameter If not giving type Positional argument 1: name : str Name of input parameter Keyword arguments value : any sets default value of parameter comment : str sets comment displayed next to declaration """ if len(args) == 1: self.type = "" self.name = str(args[0]) if len(args) == 2: specified_type = args[0] allowed_types = {"double", "int", "string"} if specified_type not in allowed_types: raise RuntimeError("Tried to create parameter of type \"" + str(specified_type) + "\" which is not among the allowed types " + str(allowed_types) + ".") self.type = specified_type self.name = str(args[1]) if not is_legal_parameter(self.name): raise NameError("The given parameter name: \"" + self.name + "\" is not a legal c variable name, " + " and cannot be used in McStas.") self.value = "" if "value" in kwargs: if not isinstance(kwargs["value"], (str, int, float)): raise RuntimeError("Given value for parameter has to be of " + "type str, int or float.") self.value = kwargs["value"] self.comment = "" if "comment" in kwargs: self.comment = kwargs["comment"] if not isinstance(self.comment, str): raise RuntimeError("Tried to create a parameter with a " + "comment that was not a string.") self.comment = "// " + self.comment self.options = None if "options" in kwargs: self.options = kwargs["options"] if self.value != "": if (self.value not in self.options and self.value.strip("'") not in self.options and self.value.strip('"') not in self.options): raise RuntimeError("When giving both options and default, " "the value has to be an option.")
def __init__(self, type, name, **kwargs): """ Initializing mcstas parameter object Examples -------- Creates a variable with name A3 and default value A = DeclareVariable("double", "A3", value=30) Creates a variable with type integer and name sample_number A = DeclareVariable("int", "sample_number") Creates an array variable called m_values A = DeclareVariable("double", "m_values", array=3, value=[2, 2.5, 2]) Parameters ---------- type : str Type of the parameter, double, int or string name : str Name of input parameter Keyword arguments array : int length of array to be allocated, 0 if single value value : any sets initial value of parameter, can be a list with length matching array comment : str sets comment displayed next to declaration """ self.type = type if not isinstance(self.type, str): raise RuntimeError("Given type of DeclareVariable should be a " + "string.") self.name = str(name) par_name = self.name if "*" in par_name[0]: # Remove any number of prefixed *, indicating variable is a pointer par_name = par_name.split("*")[-1] elif "&" in par_name[0]: # Remove the first & indicating the variable is an address par_name = par_name[1:] if not is_legal_parameter(par_name): raise NameError("The given parameter name: \"" + self.name + "\" is not a legal c variable name, " + " and cannot be used in McStas.") self.value = "" if "value" in kwargs: self.value = kwargs["value"] self.vector = 0 if "array" in kwargs: self.vector = kwargs["array"] self.comment = "" if "comment" in kwargs: self.comment = " // " + kwargs["comment"]