File: example_data.py

package info (click to toggle)
python-can 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,372 kB
  • sloc: python: 25,840; makefile: 38; sh: 20
file content (199 lines) | stat: -rw-r--r-- 6,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python

"""
This module contains some example data, like messages of different
types and example comments with different challenges.
"""

import random
from operator import attrgetter

from can import Message

# make tests more reproducible
random.seed(13339115)


def sort_messages(messages):
    """
    Sorts the given messages by timestamps (ascending).

    :param Iterable[can.Message] messages: a sequence of messages to sort
    :rtype: list
    """
    return list(sorted(messages, key=attrgetter("timestamp")))


# some random number
TEST_TIME = 1483389946.197


# List of messages of different types that can be used in tests
TEST_MESSAGES_BASE = sort_messages(
    [
        Message(
            # empty
            timestamp=1e-4,
        ),
        Message(
            # only data
            timestamp=2e-4,
            data=[0x00, 0x42],
        ),
        Message(
            # no data
            timestamp=3e-4,
            arbitration_id=0xAB,
            is_extended_id=False,
        ),
        Message(
            # no data
            timestamp=4e-4,
            arbitration_id=0x42,
            is_extended_id=True,
        ),
        Message(
            # no data
            timestamp=5e-4,
            arbitration_id=0xABCDEF,
        ),
        Message(
            # empty data
            timestamp=6e-4,
            data=[],
        ),
        Message(
            # empty data
            timestamp=7e-4,
            data=[0xFF, 0xFE, 0xFD],
        ),
        Message(
            # with channel as integer
            timestamp=8e-4,
            channel=0,
        ),
        Message(
            # with channel as integer
            timestamp=9e-4,
            channel=42,
        ),
        Message(
            # with channel as string
            timestamp=10e-4,
            channel="vcan0",
        ),
        Message(
            # with channel as string
            timestamp=11e-4,
            channel="awesome_channel",
        ),
        Message(
            arbitration_id=0xABCDEF,
            is_extended_id=True,
            timestamp=TEST_TIME,
            data=[1, 2, 3, 4, 5, 6, 7, 8],
        ),
        Message(
            arbitration_id=0x123,
            is_extended_id=False,
            timestamp=TEST_TIME + 42.42,
            data=[0xFF, 0xFF],
        ),
        Message(
            arbitration_id=0xDADADA,
            is_extended_id=True,
            timestamp=TEST_TIME + 0.165,
            data=[1, 2, 3, 4, 5, 6, 7, 8],
        ),
        Message(
            arbitration_id=0x123,
            is_extended_id=False,
            timestamp=TEST_TIME + 0.365,
            data=[254, 255],
        ),
        Message(
            arbitration_id=0x768, is_extended_id=False, timestamp=TEST_TIME + 3.165
        ),
    ]
)


TEST_MESSAGES_CAN_FD = sort_messages(
    [
        Message(timestamp=12e-4, is_fd=True, data=range(64)),
        Message(timestamp=13e-4, is_fd=True, data=range(8)),
        Message(timestamp=14e-4, is_fd=True, data=range(8), bitrate_switch=True),
        Message(timestamp=15e-4, is_fd=True, data=range(8), error_state_indicator=True),
    ]
)


TEST_MESSAGES_REMOTE_FRAMES = sort_messages(
    [
        Message(
            arbitration_id=0xDADADA,
            is_extended_id=True,
            is_remote_frame=True,
            timestamp=TEST_TIME + 0.165,
        ),
        Message(
            arbitration_id=0x123,
            is_extended_id=False,
            is_remote_frame=True,
            timestamp=TEST_TIME + 0.365,
        ),
        Message(
            arbitration_id=0x768,
            is_extended_id=False,
            is_remote_frame=True,
            timestamp=TEST_TIME + 3.165,
        ),
        Message(
            arbitration_id=0xABCDEF,
            is_extended_id=True,
            is_remote_frame=True,
            timestamp=TEST_TIME + 7858.67,
        ),
    ]
)


TEST_MESSAGES_ERROR_FRAMES = sort_messages(
    [
        Message(is_error_frame=True),
        Message(is_error_frame=True, timestamp=TEST_TIME + 0.170),
        Message(is_error_frame=True, timestamp=TEST_TIME + 17.157),
    ]
)


TEST_ALL_MESSAGES = sort_messages(
    TEST_MESSAGES_BASE + TEST_MESSAGES_REMOTE_FRAMES + TEST_MESSAGES_ERROR_FRAMES
)


TEST_COMMENTS = [
    "This is the first comment",
    "",  # empty comment
    "This third comment contains some strange characters: 'ä\"§$%&/()=?__::_Öüßêè and ends here.",
    (
        "This fourth comment is quite long! "
        "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. "
        "Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. "
        "Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi."
    ),
]


def generate_message(arbitration_id):
    """
    Generates a new message with the given ID, some random data
    and a non-extended ID.
    """
    data = bytearray([random.randrange(0, 2**8 - 1) for _ in range(8)])
    return Message(
        arbitration_id=arbitration_id,
        data=data,
        is_extended_id=False,
        timestamp=TEST_TIME,
    )