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 Enum member.

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]) – The Enum object that obj should be converted to.

  • prefix (str, optional) – If obj is a str, then ensures that prefix is included at the beginning of obj before converting obj to the enum.

  • to_upper (bool, optional) – If obj is a str, then whether to change obj to be upper case before converting obj to the enum.

  • strict (bool, optional) – Whether errors should be raised. If False and 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_primitive(text)[source]

Convert text into a primitive value.

Parameters:

text (str or bytes) – The text to convert.

Returns:

  • The text as a None, bool, int,

  • float or complex object. Returns the

  • original text if it cannot be converted to any of these types.

  • The text 0 and 1 get converted to an integer not a boolean.

msl.equipment.utils.convert_to_date(obj, fmt='%Y-%m-%d', strict=True)[source]

Convert an object to a datetime.date object.

Parameters:
Returns:

datetime.date – A datetime.date object.

msl.equipment.utils.convert_to_xml_string(element, indent='  ', encoding='utf-8', fix_newlines=True)[source]

Convert an XML Element in to a string with proper indentation.

Parameters:
  • element (Element) – The element to convert.

  • indent (str, optional) – The value to use for the indentation.

  • encoding (str, optional) – The encoding to use.

  • fix_newlines (bool, optional) – Whether to remove newlines inside text nodes.

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 Element contains unicode characters then you should use the codecs module 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 or None.

  • 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.xml_comment(text)[source]

Create a new XML comment element.

Parameters:

text (str) – The comment.

Returns:

Comment() – A special element that is an XML comment.

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).

  • fmt (str or None, optional) –

    The format to use to convert iterable. Possible values are:

    • '' (empty string or None) – 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 type str and it is used as the format_spec argument in format() 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 that numpy.dtype supports (e.g., 'H', 'uint16' and numpy.ushort are 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, bytearray or str) – A byte buffer. Can be an already-decoded buffer of type str, but only if fmt equals 'ascii'.

  • fmt (str or None, optional) – The format that buffer is in. See to_bytes() for more details.

  • dtype – The data type of each element in buffer. Can be any object that numpy.dtype supports. See to_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.

msl.equipment.utils.parse_lxi_webserver(host, port=80, timeout=1)[source]

Get the information about an LXI device from the device’s webserver.

Parameters:
  • host (str) – The IP address or hostname of the LXI device.

  • port (int, optional) – The port number of the device’s webservice.

  • timeout (float, optional) – The maximum number of seconds to wait for a reply.

Returns:

dict – The information about the LXI device.