msl.equipment.utils module
Common functions.
- msl.equipment.utils.convert_to_enum(obj, enum, prefix=None, to_upper=False, strict=True)[source]
Convert obj to an
Enummember.- Parameters:
obj (
object) – Any object to be converted to the specified enum. Can be a value of member of the specified enum.enum (Type[
Enum]) – TheEnumobject that obj should be converted to.prefix (
str, optional) – If obj is astr, then ensures that prefix is included at the beginning of obj before converting obj to the enum.to_upper (
bool, optional) – If obj is astr, then whether to change obj to be upper case before converting obj to the enum.strict (
bool, optional) – Whether errors should be raised. IfFalseand obj cannot be converted to enum then obj is returned and the error is logged.
- Returns:
Enum– The enum member.- Raises:
ValueError – If obj is not in enum and strict is
True.
- msl.equipment.utils.convert_to_date(obj, fmt='%Y-%m-%d', strict=True)[source]
Convert an object to a
datetime.dateobject.- Parameters:
obj (
datetime.date,datetime.datetimeorstr) – Any object that can be converted to adatetime.dateobject.fmt (
str) – If obj is astrthen the format to use to convert obj to adatetime.date.strict (
bool, optional) – Whether errors should be raised. IfFalseand obj cannot be converted todatetime.datethendatetime.date(datetime.MINYEAR, 1, 1)is returned and the error is logged.
- Returns:
datetime.date– Adatetime.dateobject.
- msl.equipment.utils.convert_to_xml_string(element, indent=' ', encoding='utf-8', fix_newlines=True)[source]
Convert an XML
Elementin to a string with proper indentation.- Parameters:
- Returns:
str– The element as a pretty string. The returned value can be directly written to a file (i.e., it includes the XML declaration).
Examples
If the
Elementcontains unicode characters then you should use thecodecsmodule to create the file if you are using Python 2.7:import codecs with codecs.open('my_file.xml', mode='w', encoding='utf-8') as fp: fp.write(convert_to_xml_string(element))
otherwise you can use the builtin
open()function:with open('my_file.xml', mode='w', encoding='utf-8') as fp: fp.write(convert_to_xml_string(element))
- msl.equipment.utils.xml_element(tag, text=None, tail=None, **attributes)[source]
Create a new XML element.
- Parameters:
tag (
str) – The element’s name.text (
str, optional) – The text before the first sub-element. Can either be a string orNone.tail (
str, optional) – The text after this element’s end tag, but before the next sibling element’s start tag.attributes – All additional key-value pairs are included as XML attributes for the element. The value must be of type
str.
- Returns:
Element– The new XML element.
- msl.equipment.utils.to_bytes(iterable, fmt='ieee', dtype='<f')[source]
Convert an iterable of numbers into bytes.
- Parameters:
iterable – An object to convert to bytes. Must be a 1-dimensional sequence of elements (not a multidimensional array).
The format to use to convert iterable. Possible values are:
''(empty string orNone) – convert iterable to bytes without a header.None:
<byte><byte><byte>...'ascii'– comma-separated ASCII characters, see the <PROGRAM DATA SEPARATOR> standard that is defined in Section 7.4.2.2, IEEE 488.2-1992.ascii:
<string>,<string>,<string>,...'ieee'– arbitrary block data for SCPI messages, see the <DEFINITE LENGTH ARBITRARY BLOCK RESPONSE DATA> standard that is defined in Section 8.7.9, IEEE 488.2-1992.ieee:
#<length of num bytes value><num bytes><byte><byte><byte>...'hp'– the HP-IB data transfer standard, i.e., the FORM# command option. See the programming guide for an HP 8530A for more details.hp:
#A<num bytes as uint16><byte><byte><byte>...
dtype – The data type to use to convert each element in iterable to. If fmt is
'ascii'then dtype must be of typestrand it is used as the format_spec argument informat()to first convert each element in iterable to a string, and then it is encoded (e.g.,'.2e'converts each element to scientific notation with two digits after the decimal point). If dtype includes a byte-order character, it is ignored. For all other values of fmt, the dtype can be any object thatnumpy.dtypesupports (e.g.,'H','uint16'andnumpy.ushortare equivalent values to convert each element to an unsigned short). If a byte-order character is specified then it is used, otherwise the native byte order of the CPU architecture is used. See Format Strings for more details.
- Returns:
bytes– The iterable converted to bytes.
- msl.equipment.utils.from_bytes(buffer, fmt='ieee', dtype='<f')[source]
Convert bytes into an array.
- Parameters:
buffer (
bytes,bytearrayorstr) – A byte buffer. Can be an already-decoded buffer of typestr, but only if fmt equals'ascii'.fmt (
strorNone, optional) – The format that buffer is in. Seeto_bytes()for more details.dtype – The data type of each element in buffer. Can be any object that
numpy.dtypesupports. Seeto_bytes()for more details.
- Returns:
numpy.ndarray– The array.
- msl.equipment.utils.ipv4_addresses() set[str][source]
Get all IPv4 addresses on all network interfaces.