Skip to content

utils

plantimager.utils Link

A collection of various utilities.

guess_port Link

guess_port(info)

Guess the USB port using a given information.

Parameters:

Name Type Description Default
info str

The information to use to search ports.

required
Notes

Search for ports using a regular expression. Port name, description and hwid are searched (case insensitive). The function returns an iterable that contains the same data that comports() generates, but includes only those entries that match the regexp.

See Also

serial.tools.list_ports.grep

Examples:

>>> from plantimager.utils import guess_port
>>> guess_port("A")  # This should yield a "More than one serial device" error...
>>> guess_port("007")  # This should yield a "No serial device" error...
>>> guess_port("Arduino")  # This should return the port of the CNC (if using the X-Carve as the controller is based on an Arduino UNO)
Source code in plantimager/utils.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def guess_port(info):
    """Guess the USB port using a given information.

    Parameters
    ----------
    info : str
        The information to use to search ports.

    Notes
    -----
    Search for ports using a regular expression. Port ``name``, ``description`` and ``hwid`` are searched (case insensitive).
    The function returns an iterable that contains the same data that comports() generates, but includes only those entries that match the regexp.

    See Also
    --------
    serial.tools.list_ports.grep

    Examples
    --------
    >>> from plantimager.utils import guess_port
    >>> guess_port("A")  # This should yield a "More than one serial device" error...
    >>> guess_port("007")  # This should yield a "No serial device" error...
    >>> guess_port("Arduino")  # This should return the port of the CNC (if using the X-Carve as the controller is based on an Arduino UNO)

    """
    device = [p for p in list(list_ports.grep(info))]
    if len(device) > 1:
        logger.warning(f"More than one serial device has been found with '{info}'!")
        [logger.warning(f"  * {dev.device} - {dev.description} - {dev.hwid}") for dev in device]
        sys.exit("Non unique serial device ID!")
    elif len(device) == 0:
        logger.warning(f"No serial device has been found with '{info}'!")
        sys.exit("No serial device found!")
    else:
        device = device[0]

    return device.device

now Link

now(fmt='%Y-%m-%d %H:%M:%S')

Return datetime as a formatted string.

Parameters:

Name Type Description Default
fmt str

The date and time formating to use, default to '%y-%m-%d %H:%M:%S'.

'%Y-%m-%d %H:%M:%S'
References

https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

Examples:

>>> from plantimager.utils import now
>>> now()
Source code in plantimager/utils.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
def now(fmt="%Y-%m-%d %H:%M:%S"):
    """Return datetime as a formatted string.

    Parameters
    ----------
    fmt : str, optional
        The date and time formating to use, default to '%y-%m-%d %H:%M:%S'.

    References
    ----------
    https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

    Examples
    --------
    >>> from plantimager.utils import now
    >>> now()
    """
    return datetime.datetime.now().strftime(fmt)