parsing¶
Parsing module Qiskit Metal.
The main function in this module is parse_value, and it explains what and how it is handled. Some basic arithmetic can be handled as well, such as ‘-2 * 1e5 nm’ will yield float(-0.2) when the default units are set to mm.
Example parsing values test:¶
from qiskit_metal.toolbox_metal.parsing import * def test(val, _vars): res = parse_value(val, _vars) print( f'{type(val).__name__:<6} |{val:>12} >> {str(res):<20} | {type(res).__name__:<6}') def test2(val, _vars): res = parse_value(val, _vars) print( f'{type(val).__name__:<6} |{str(val):>38} >> {str(res):<47} | {type(res).__name__:<6}') vars_ = Dict({'x':5.0, 'y':'5um', 'cpw_width':'10um'}) print('------------------------------------------------') print('String: Basics') test(1, vars_) test(1., vars_) test('1', vars_) test('1.', vars_) test('+1.', vars_) test('-1.', vars_) test('1.0', vars_) test('1mm', vars_) test(' 1 mm ', vars_) test('100mm', vars_) test('1.mm', vars_) test('1.0mm', vars_) test('1um', vars_) test('+1um', vars_) test('-1um', vars_) test('-0.1um', vars_) test('.1um', vars_) test(' 0.1 m', vars_) test('-1E6 nm', vars_) test('-1e6 nm', vars_) test('.1e6 nm', vars_) test(' - .1e6nm ', vars_) test(' - .1e6 nm ', vars_) test(' - 1e6 nm ', vars_) test('- 1e6 nm ', vars_) test(' - 1. ', vars_) test(' + 1. ', vars_) test('1 .', vars_) print('------------------------------------------------') print('String: Arithmetic') test('2*1', vars_) test('2*10mm', vars_) test('-2 * 1e5 nm', vars_) print('------------------------------------------------') print('String: Variable') test('x', vars_) test('y', vars_) test('z', vars_) test('x1', vars_) test('2*y', vars_) print('------------------------------------------------') print('String: convert list and dict') test2(' [1,2,3.,4., "5um", " -0.1e6 nm" ] ', vars_) test2(' {3:2, 4: " -0.1e6 nm" } ', vars_) print('') print('------------------------------------------------') print('Dict: convert list and dict') my_dict = Dict( string1 = '1m', string2 = '1mm', string3 = '1um', string4 = '1nm', variable1 = 'cpw_width', list1 = "['1m', '5um', 'cpw_width', -1, False, 'a string']", dict1 = "{'key1':'4e-6mm', '2mm':'100um'}" ) #test2(my_dict, vars_) display(parse_value(my_dict, vars_))
Returns:¶
------------------------------------------------ String: Basics int | 1 >> 1 | int float | 1.0 >> 1.0 | float str | 1 >> 1.0 | float str | 1. >> 1.0 | float str | +1. >> 1.0 | float str | -1. >> -1.0 | float str | 1.0 >> 1.0 | float str | 1mm >> 1 | int str | 1 mm >> 1 | int str | 100mm >> 100 | int str | 1.mm >> 1.0 | float str | 1.0mm >> 1.0 | float str | 1um >> 0.001 | float str | +1um >> 0.001 | float str | -1um >> -0.001 | float str | -0.1um >> -0.0001 | float str | .1um >> 0.0001 | float str | 0.1 m >> 100.0 | float str | -1E6 nm >> -1.0000000000000002 | float str | -1e6 nm >> -1.0000000000000002 | float str | .1e6 nm >> 0.10000000000000002 | float str | - .1e6nm >> -0.10000000000000002 | float str | - .1e6 nm >> -0.10000000000000002 | float str | - 1e6 nm >> - 1e6 nm | str str | - 1e6 nm >> - 1e6 nm | str str | - 1. >> - 1. | str str | + 1. >> + 1. | str str | 1 . >> 1 . | str ------------------------------------------------ String: Arithmetic str | 2*1 >> 2*1 | str str | 2*10mm >> 20 | int str | -2 * 1e5 nm >> -0.20000000000000004 | float ------------------------------------------------ String: Variable str | x >> 5.0 | float str | y >> 0.005 | float str | z >> z | str str | x1 >> x1 | str str | 2*y >> 2*y | str ------------------------------------------------ String: convert list and dict str | [1,2,3.,4., "5um", " -0.1e6 nm" ] >> [1, 2, 3.0, 4.0, 0.005, -0.10000000000000002] | list str | {3:2, 4: " -0.1e6 nm" } >> {3: 2, 4: -0.10000000000000002} | Dict ------------------------------------------------ Dict: convert list and dict {'string1': 1000.0, 'string2': 1, 'string3': 0.001, 'string4': 1.0000000000000002e-06, 'variable1': 0.01, 'list1': [1000.0, 0.005, 0.01, -1, False, 'a string'], 'dict1': {'key1': 4e-06, '2mm': 0.1}}
Functions
|
|
|
Convert all numbers to string and append the assumed units if needed. |
|
Is the test string a valid list of dict string, such as "[1, 2]", that can be evaluated by ast eval. |
|
Is the test string a valid possible numerical with /or w/o units. |
|
Check if a value is true or not. |
|
Is the test string a valid name for a variable or not? |
|
Should take a list of tuple of list. |
|
Calls parse_value to extract from a dictionary a small subset of values. |
|
Convert number, string, and lists/arrays/tuples to numbers scaled in HFSS units. |
|
Parse a string, mappable (dict, Dict), iterable (list, tuple) to account for units conversion, some basic arithmetic, and design variables. |
- is_for_ast_eval(test_str: str)[source]¶
Is the test string a valid list of dict string, such as “[1, 2]”, that can be evaluated by ast eval.
- Parameters:
test_str (str) – Test string
- Returns:
Is test_str a valid list of dict strings
- Return type:
bool
- is_numeric_possible(test_str: str)[source]¶
Is the test string a valid possible numerical with /or w/o units.
- Parameters:
test_str (str) – Test string
- Returns:
Is the test string a valid possible numerical
- Return type:
bool
- is_true(value: str | int | bool | float) bool [source]¶
Check if a value is true or not.
- Parameters:
value (str) – Value to check
- Returns:
Is the string a true
- Return type:
bool
- is_variable_name(test_str: str)[source]¶
Is the test string a valid name for a variable or not?
- Parameters:
test_str (str) – Test string
- Returns:
Is str a variable name
- Return type:
bool
- parse_options(params: dict, parse_names: str, variable_dict=None)[source]¶
Calls parse_value to extract from a dictionary a small subset of values. You can specify parse_names = ‘x,y,z,cpw_width’.
- Parameters:
params (dict) – Dictionary of params
parse_names (str) – Name to parse
variable_dict (dict) – Dictionary of variables. Defaults to None.
- parse_value(value: str, variable_dict: dict)[source]¶
Parse a string, mappable (dict, Dict), iterable (list, tuple) to account for units conversion, some basic arithmetic, and design variables. This is the main parsing function of Qiskit Metal.
Handled Inputs:
- Strings of numbers, numbers with units; e.g., ‘1’, ‘1nm’, ‘1 um’
Converts to int or float. Some basic arithmetic is possible, see below.
- Strings of variables ‘variable1’.
Variable interpretation will use string method isidentifier ‘variable1’.isidentifier()
- Strings of Dictionaries:
Returns ordered Dict with same key-value mappings, where the values have been subjected to parse_value.
- Strings of Iterables(list, tuple, …):
Returns same kind and calls itself parse_value on each elemnt.
- Numbers:
Returns the number as is. Int to int, etc.
- Arithmetic:
Some basic arithmetic 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
- Parameters:
value (str) – String to parse
variable_dict (dict) – dict pointer of variables
- Returns:
Parsed value
- Return type:
str, float, list, tuple, or ast eval