Skip to content

File¤

Suppose you have a variable named file (which is an instance of File) that represents the following information in an equipment register for data that is stored in a Spreadsheet

<file comment="FEL T647">
  <url sheet="2024-May" cells="A1:C11">tests\resources\irradiance.xlsx</url>
  <sha256>7a91267cfb529388a99762b891ee4b7a12463e83b5d55809f76a0c8e76c71886</sha256>
</file>

You can access sha256 and comment as attributes of file

>>> file.sha256
'7a91267cfb529388a99762b891ee4b7a12463e83b5d55809f76a0c8e76c71886'
>>> file.comment
'FEL T647'

The url and attributes attributes of file can be used with the read_table function of msl-io to read the Spreadsheet data

>>> from msl.io import read_table
>>> table = read_table(file.url, **file.attributes)
>>> print(table.metadata.header)
['Wavelength' 'Irradiance' 'u(Irradiance)']
>>> table
<Dataset 'irradiance.xlsx' shape=(10, 3) dtype='<f8' (1 metadata)>
>>> print(table)
array([[2.500000e+02, 1.818000e-02, 2.033000e-02],
       [3.000000e+02, 1.847800e-01, 1.755000e-02],
       [3.500000e+02, 8.084500e-01, 1.606000e-02],
       [4.000000e+02, 2.213550e+00, 1.405000e-02],
       [4.500000e+02, 4.490040e+00, 1.250000e-02],
       [5.000000e+02, 7.451350e+00, 1.200000e-02],
       [5.500000e+02, 1.075753e+01, 1.152000e-02],
       [6.000000e+02, 1.403809e+01, 1.102000e-02],
       [6.500000e+02, 1.699469e+01, 1.103000e-02],
       [7.000000e+02, 1.944093e+01, 1.077000e-02]])

Note

Passing **file.attributes to read_table works as expected provided that the XML attributes of the <url> element are valid keyword arguments to read_table. See Read a table for more examples from msl-io, in particular, specifying dtype="header" will return a structured dataset which would behave similar to the Table example in msl-equipment (i.e., accessing columns by header name).

File dataclass ¤

File(
    url: str,
    sha256: str,
    attributes: dict[str, str] = dict(),
    comment: str = "",
)

Represents the file element in an equipment register.

Parameters:

Name Type Description Default
url str

The location of the file. The syntax follows RFC 1738 scheme:scheme-specific-part. If scheme: is not specified, it is assumed to be file:.

required
sha256 str

The SHA-256 checksum of the file.

required
attributes dict[str, str]

XML attributes associated with the <url> element.

dict()
comment str

A comment to associate with the file.

''

attributes class-attribute instance-attribute ¤

attributes: dict[str, str] = field(default_factory=dict)

XML attributes associated with the <url> element.

comment class-attribute instance-attribute ¤

comment: str = ''

A comment associated with the file.

scheme property ¤

scheme: str

Returns the scheme component that is specified in the url (see RFC 1738 for more details).

If a <scheme> is not specified, an empty string is returned (which shall be treated as the file scheme). Drive letters on Windows are not considered as a scheme.

sha256 instance-attribute ¤

sha256: str

The SHA-256 checksum of the file.

url instance-attribute ¤

url: str

The location of the file.

The syntax follows RFC 1738 <scheme>:<scheme-specific-part>. If <scheme> is not specified, it shall be treated as the file scheme (see also scheme).

from_xml classmethod ¤

from_xml(element: Element[str]) -> File

Convert an XML element into a File instance.

Parameters:

Name Type Description Default
element Element[str]

A file XML element from an equipment register.

required

Returns:

Type Description
File

The File instance.

Source code in src/msl/equipment/schema.py
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
@classmethod
def from_xml(cls, element: Element[str]) -> File:
    """Convert an XML element into a [File][msl.equipment.schema.File] instance.

    Args:
        element: A [file][type_file] XML element from an equipment register.

    Returns:
        The [File][msl.equipment.schema.File] instance.
    """
    # Schema forces order
    return cls(
        url=element[0].text or "",
        sha256=element[1].text or "",
        attributes=element[0].attrib,
        comment=element.attrib.get("comment", ""),
    )

to_xml ¤

to_xml() -> Element[str]

Convert the File class into an XML element.

Returns:

Type Description
Element[str]

The File as an XML element.

Source code in src/msl/equipment/schema.py
872
873
874
875
876
877
878
879
880
881
882
883
884
def to_xml(self) -> Element[str]:
    """Convert the [File][msl.equipment.schema.File] class into an XML element.

    Returns:
        The [File][msl.equipment.schema.File] as an XML element.
    """
    attrib = {"comment": self.comment} if self.comment else {}
    e = Element("file", attrib=attrib)
    url = SubElement(e, "url", attrib=self.attributes)
    url.text = self.url
    sha256 = SubElement(e, "sha256")
    sha256.text = self.sha256
    return e