Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
7c44101
use is_extended_id instead of id_type for message objects
felixdivo Sep 6, 2018
b3fd52f
better documentation
felixdivo Sep 6, 2018
00903f8
document check option
felixdivo Sep 6, 2018
6204cda
fixes for the Message class
felixdivo Sep 6, 2018
965bbbb
run test on CI for logformats_test.py
felixdivo Sep 6, 2018
1797e43
added channel to docs
felixdivo Sep 6, 2018
e76f017
add example messages with channel attribute set
felixdivo Sep 6, 2018
4e23808
test codecov
felixdivo Sep 8, 2018
f71fd56
change logformat tests
felixdivo Sep 10, 2018
a20f4f7
nicer __repr__
felixdivo Sep 10, 2018
5217880
add __slots__, correct __hash__. deprecate id_type
felixdivo Sep 10, 2018
edb2b6e
make timestamp comparisoun a bit more forgiving
felixdivo Sep 10, 2018
ec9f9ed
use extended_id in constructor
felixdivo Sep 10, 2018
1a76644
fix TestBlfFileFormat test
felixdivo Sep 10, 2018
26b2406
implemented Message._check
felixdivo Sep 10, 2018
52ab605
adjust logformat test
felixdivo Sep 10, 2018
a724a92
Merge branch 'develop' into change-messages
felixdivo Sep 10, 2018
5d88662
try to add warning & compat __dict__
felixdivo Sep 15, 2018
c0fe8a7
Merge branch 'change-messages' of github.com:hardbyte/python-can into…
felixdivo Sep 15, 2018
5fb1775
properly implement warning
felixdivo Sep 16, 2018
eee819f
update deprecation notes
felixdivo Sep 16, 2018
00f6b92
docs & remove undocumented setting of flags in kvaser backend
felixdivo Sep 16, 2018
da6d180
docs
felixdivo Sep 16, 2018
9791761
compare by identity; add _dict to __hash__()
felixdivo Sep 23, 2018
95cc0b5
address PR comments
felixdivo Sep 23, 2018
f4c532e
remove leftover code
felixdivo Sep 23, 2018
360cfdf
Merge branch 'develop' into change-messages
felixdivo Sep 23, 2018
43586e9
resolve bad merge commit
felixdivo Sep 23, 2018
afe9cf6
added missing comma
felixdivo Sep 23, 2018
58a31f3
add __copy__ and __deepcopy__
felixdivo Sep 23, 2018
e840d36
docs
felixdivo Sep 23, 2018
e27fafb
fix bad test case
felixdivo Sep 27, 2018
196cd89
add Message.equals(), and correctly check for equality in failing tests
felixdivo Sep 27, 2018
2499c95
Merge branch 'develop' into change-messages
felixdivo Sep 27, 2018
fddef63
Merge branch 'develop' into change-messages
felixdivo Sep 27, 2018
c28a106
Remove message hash implementation
hardbyte Sep 28, 2018
5eb4fa4
Merge branch 'develop' into change-messages
hardbyte Sep 28, 2018
68d8e45
Fix missing space in setup
hardbyte Sep 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
document check option
  • Loading branch information
felixdivo committed Sep 6, 2018
commit 00903f8fc98755dcdf174700651ad659f6ae6cd3
59 changes: 39 additions & 20 deletions can/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,43 @@
This module contains the implementation of :class:`can.Message`.

.. note::
Could also use `@dataclass <https://docs.python.org/3.7/library/dataclasses.html>`__
Could use `@dataclass <https://docs.python.org/3.7/library/dataclasses.html>`__
starting with Python 3.7.
"""

import logging
logger = logging.getLogger(__name__)


class Message(object):
"""
The :class:`~can.Message` object is used to represent CAN messages for
sending, receiving and other purposes like converting between.
sending, receiving and other purposes like converting between different
logging formats.

Messages can use extended identifiers, be remote or error frames, contain
data and can be associated to a channel.

When testing for equality of the messages, the timestamp and the channel
is not used for comparing.

.. note::

This class does not strictly check the input. Thus, the caller must
prevent the creation of invalid messages. Possible problems include
the `dlc` field not matching the length of `data` or creating a message
with both `is_remote_frame` and `is_error_frame` set to True.

When testing for equality of messages, the timestamp and the channel
are not used for comparing.
"""

def __init__(self, timestamp=0.0, arbitration_id=0, extended_id=True,
is_remote_frame=False, is_error_frame=False, channel=None,
dlc=None, data=None,
is_fd=False, bitrate_switch=False, error_state_indicator=False,):
is_fd=False, bitrate_switch=False, error_state_indicator=False,
check=False):
"""
To create a message object, simply provide any of the below attributes
toghether with additional parameters as keyword arguments to the constructor.

:param bool check: By default, the constructor of this class does not strictly check the input.
Thus, the caller must prevent the creation of invalid messages or
set this parameter to `True`, to raise an Error on invalid inputs.
Possible problems include the `dlc` field not matching the length of `data`
or creating a message with both `is_remote_frame` and `is_error_frame` set to `True`.

:raises ValueError: iff `check` is set to `True` and one or more arguments were invalid
"""

self.timestamp = timestamp
self.arbitration_id = arbitration_id
Expand Down Expand Up @@ -67,11 +72,8 @@ def __init__(self, timestamp=0.0, arbitration_id=0, extended_id=True,
else:
self.dlc = dlc

# TODO: this shall only be checked when asked for
if is_fd and self.dlc > 64:
logger.warning("data link count was %d but it should be less than or equal to 64", self.dlc)
if not is_fd and self.dlc > 8:
logger.warning("data link count was %d but it should be less than or equal to 8", self.dlc)
if check:
self._check()

def __str__(self):
field_strings = ["Timestamp: {0:>15.6f}".format(self.timestamp)]
Expand Down Expand Up @@ -171,4 +173,21 @@ def __hash__(self):
))

def __format__(self, format_spec):
return self.__str__()
if not format_spec:
return self.__str__()
else:
raise ValueError('non empty format_specs are not supported')

def __bytes__(self):
return bytes(self.data)

def _check(self):
"""Checks if the message parameters are valid.

:raises ValueError: iff one or more attributes are invalid
"""
# TODO add checks
if is_fd and self.dlc > 64:
logger.warning("data link count was %d but it should be less than or equal to 64", self.dlc)
if not is_fd and self.dlc > 8:
logger.warning("data link count was %d but it should be less than or equal to 8", self.dlc)