예제 #1
0
    def parse_value(self, value: Union[Any, List, Dict, Iterable]) -> Any:
        """
        Main parsing function.
        Parse a string, mappable (dict, Dict), iterrable (list, tuple) to account for
        units conversion, some basic arithmetic, and design variables.

        Arguments:
            value (str): String to parse *or*
            variable_dict (dict): dict pointer of variables

        Return:
            str, float, list, tuple, or ast eval: Parsed value

        Handled Inputs:

            Strings:
                Strings of numbers, numbers with units; e.g., '1', '1nm', '1 um'
                    Converts to int or float.
                    Some basic arithmatic is possible, see below.
                Strings of variables 'variable1'.
                    Variable interpertation will use string method
                    isidentifier 'variable1'.isidentifier()

            Dictionaries:
                Returns ordered `Dict` with same key-value mappings, where the values have
                been subjected to parse_value.

            Itterables(list, tuple, ...):
                Returns same kind and calls itself `parse_value` on each elemnt.

            Numbers:
                Returns the number as is. Int to int, etc.


        Arithemetic:
            Some basic arithemetic can be handled as well, such as `'-2 * 1e5 nm'`
            will yield float(-0.2) when the default units are set to `mm`.

        Default units:
            User units can be set in the design. The design will set config.DEFAULT.units

        Examples:
            See the docstring for this module.
                qiskit_metal.toolbox_metal.parsing
        """
        return parse_value(value, self.variables)
예제 #2
0
    def test_toolbox_metal_parse_value(self):
        """Test parse_value in toolbox_metal.py."""
        var_dict = {'data_a': '4 miles', 'data_b': '1um'}

        self.assertAlmostEqualRel(parsing.parse_value('1 meter', var_dict),
                                  1000.0,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('2 um', var_dict),
                                  0.002,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('3 miles', var_dict),
                                  4828032.0,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('4 feet', var_dict),
                                  1219.1999999999998,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('5 km', var_dict),
                                  5000000.0,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('6 pm', var_dict),
                                  6.000000000000001e-09,
                                  rel_tol=1e-10)
        self.assertAlmostEqualRel(parsing.parse_value('-12 inches', var_dict),
                                  -304.79999999999995,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('data_a', var_dict),
                                  6437376.0,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('data_b', var_dict),
                                  0.001,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('1 um', None),
                                  0.001,
                                  rel_tol=1e-3)
        self.assertAlmostEqualRel(parsing.parse_value('1 um', 'None'),
                                  0.001,
                                  rel_tol=1e-3)