Skip to content

Commit e9e7f74

Browse files
committed
Code clean-up and more test cases
1 parent 28b649e commit e9e7f74

File tree

6 files changed

+67
-23
lines changed

6 files changed

+67
-23
lines changed

boltstub/parsing.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ def parse_jolt(self, jolt_package):
232232
self,
233233
"message fields failed JOLT parser"
234234
) from e
235-
decoded = self._jolt_to_struct(decoded, jolt_package)
235+
decoded = self._jolt_to_struct(decoded)
236236
jolt_fields.append(decoded)
237237
self.jolt_parsed = self.parsed[0], jolt_fields
238238
return self.jolt_parsed
239239

240-
def _jolt_to_struct(self, decoded, jolt_package):
240+
def _jolt_to_struct(self, decoded):
241241
if isinstance(decoded, JoltWildcard):
242242
if not self.allow_jolt_wildcard:
243243
raise LineError(
@@ -249,11 +249,9 @@ def _jolt_to_struct(self, decoded, jolt_package):
249249
if isinstance(decoded, JoltType):
250250
return Structure.from_jolt_type(decoded)
251251
if isinstance(decoded, (list, tuple)):
252-
return type(decoded)(self._jolt_to_struct(d, jolt_package)
253-
for d in decoded)
252+
return type(decoded)(self._jolt_to_struct(d) for d in decoded)
254253
if isinstance(decoded, dict):
255-
return {k: self._jolt_to_struct(v, jolt_package)
256-
for k, v in decoded.items()}
254+
return {k: self._jolt_to_struct(v) for k, v in decoded.items()}
257255
return decoded
258256

259257

boltstub/simple_jolt/v1/types.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import datetime
22
import re
3-
from typing import (
4-
Optional,
5-
Union,
6-
)
3+
from typing import Union
74

85
import pytz
96

@@ -122,7 +119,7 @@ def __init__(self, value: str, allow_timezone_id: bool = False):
122119
+ hours * 3600000000000)
123120

124121
@classmethod
125-
def new(cls, nanoseconds: int, utc_offset_seconds: Optional[int] = None):
122+
def new(cls, nanoseconds: int, utc_offset_seconds: int):
126123
if nanoseconds < 0:
127124
raise ValueError("nanoseconds must be >= 0")
128125
hours, nanoseconds = divmod(nanoseconds, 3600000000000)
@@ -136,11 +133,10 @@ def new(cls, nanoseconds: int, utc_offset_seconds: Optional[int] = None):
136133
if nanoseconds:
137134
seconds_str += "." + re.sub(r"0+$", "", "%09i" % nanoseconds)
138135
s = "%02i:%02i:%s" % (hours, minutes, seconds_str)
139-
if utc_offset_seconds is not None:
140-
if utc_offset_seconds >= 0:
141-
s += "+%02i%02i" % (offset_hours, offset_minutes)
142-
else:
143-
s += "-%02i%02i" % (-offset_hours - 1, 60 - offset_minutes)
136+
if utc_offset_seconds >= 0:
137+
s += "+%02i%02i" % (offset_hours, offset_minutes)
138+
else:
139+
s += "-%02i%02i" % (-offset_hours - 1, 60 - offset_minutes)
144140
return cls(s)
145141

146142
def __eq__(self, other):
@@ -335,6 +331,7 @@ def _format_dt(cls, dt, buffered_ns):
335331

336332
@classmethod
337333
def _format_s_ns_tz_info(cls, seconds: int, nanoseconds: int, tz_info):
334+
# seconds, nanoseconds since local unix epoch
338335
microseconds, buffered_ns = divmod(nanoseconds, 1000)
339336
dt = datetime.datetime(1970, 1, 1) # zone_id local unix epoch
340337
dt += datetime.timedelta(seconds=seconds, microseconds=microseconds)
@@ -347,9 +344,6 @@ def _new_zone_id(cls, seconds: int, nanoseconds: int, zone_id: str):
347344
tz_info = pytz.timezone(zone_id)
348345
return cls(cls._format_s_ns_tz_info(seconds, nanoseconds, tz_info))
349346

350-
# TODO:
351-
# - make sure there are tests for the representation converters
352-
353347
@classmethod
354348
def _new_fixed_offset(cls, seconds: int, nanoseconds: int,
355349
utc_offset_seconds: int):

boltstub/simple_jolt/v2/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def seconds_nanoseconds(self):
3434

3535
@classmethod
3636
def _format_s_ns_tz_info(cls, seconds: int, nanoseconds: int, tz_info):
37-
# zone_id UTC unix epoch
37+
# seconds, nanoseconds since UTC unix epoch
3838
dt = datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
3939

4040
microseconds, buffered_ns = divmod(nanoseconds, 1000)

boltstub/tests/_common.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def get_jolt_package(version):
184184
[Structure(b"\x66", 25666200, 0,
185185
"America/New_York", packstream_version=1)]
186186
),
187-
( # wow! the same encoding \o/
187+
( # Wow! The same encoding \o/ [/sarcasm]
188188
'{"T": "1970-10-25T01:30-05:00[America/New_York]"}',
189189
[Structure(b"\x66", 25666200, 0,
190190
"America/New_York", packstream_version=1)]
@@ -315,12 +315,12 @@ def get_jolt_package(version):
315315
[Structure(b"\x69", 90061, 123400000, "Europe/Stockholm",
316316
packstream_version=2)]
317317
),
318-
( # packstream v1 bug (non-unique times) fixed:
318+
( # packstream v1 bug (non-unique times) fixed
319319
'{"T": "1970-10-25T01:30-04:00[America/New_York]"}',
320320
[Structure(b"\x69", 25680600, 0,
321321
"America/New_York", packstream_version=2)]
322322
),
323-
( # wow! different times have different representations :O
323+
( # Wow! Different times have different representations :O
324324
'{"T": "1970-10-25T01:30-05:00[America/New_York]"}',
325325
[Structure(b"\x69", 25684200, 0,
326326
"America/New_York", packstream_version=2)]
@@ -512,6 +512,8 @@ def get_jolt_package(version):
512512
[Structure(b"\x74", 60000000001, packstream_version=1)],
513513
[Structure(b"\x74", 3601000000000, packstream_version=1)],
514514
[Structure(b"\x46", 90061, 123400000, 3600, packstream_version=1)],
515+
[Structure(b"\x66", 90061, 123400000, "Europe/Stockholm",
516+
packstream_version=1)],
515517
[Structure(b"\x64", 90061, 1234, packstream_version=1)],
516518
[Structure(b"\x45", 25, 2, 1080, 100000000, packstream_version=1)],
517519
)),
@@ -564,6 +566,8 @@ def get_jolt_package(version):
564566
[Structure(b"\x74", 60000000001, packstream_version=2)],
565567
[Structure(b"\x74", 3601000000000, packstream_version=2)],
566568
[Structure(b"\x49", 90061, 123400000, 3600, packstream_version=2)],
569+
[Structure(b"\x69", 90061, 123400000, "Europe/Stockholm",
570+
packstream_version=2)],
567571
[Structure(b"\x64", 90061, 1234, packstream_version=2)],
568572
[Structure(b"\x45", 25, 2, 1080, 100000000, packstream_version=2)],
569573
)),

boltstub/tests/simple_jolt/v1/parse_data.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,34 @@
124124
('{"T": "12:00"}', JoltLocalTime("12:00")),
125125

126126
# date time - full
127+
(
128+
'{"T": "2020-01-02T12:13:01+01"}',
129+
JoltDateTime("2020-01-02T12:13:01+01")
130+
),
127131
(
128132
'{"T": "2020-01-02T12:13:01.1234+01"}',
129133
JoltDateTime("2020-01-02T12:13:01.1234+01")
130134
),
135+
(
136+
'{"T": "2020-01-02T12:13:01.1234+0100"}',
137+
JoltDateTime("2020-01-02T12:13:01.1234+01")
138+
),
139+
(
140+
'{"T": "2020-01-02T12:13:01.1234+01:00"}',
141+
JoltDateTime("2020-01-02T12:13:01.1234+01")
142+
),
143+
(
144+
'{"T": "2020-01-02T12:13:01.1234+01[Europe/Stockholm]"}',
145+
JoltDateTime("2020-01-02T12:13:01.1234+01[Europe/Stockholm]")
146+
),
147+
(
148+
'{"T": "2020-01-02T12:13:01.1234+0100[Europe/Stockholm]"}',
149+
JoltDateTime("2020-01-02T12:13:01.1234+0100[Europe/Stockholm]")
150+
),
151+
(
152+
'{"T": "2020-01-02T12:13:01.1234+01:00[Europe/Stockholm]"}',
153+
JoltDateTime("2020-01-02T12:13:01.1234+01:00[Europe/Stockholm]")
154+
),
131155

132156
# local date time - full
133157
(

boltstub/tests/simple_jolt/v2/parse_data.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,34 @@
124124
('{"T": "12:00"}', JoltLocalTime("12:00")),
125125

126126
# date time - full
127+
(
128+
'{"T": "2020-01-02T12:13:01+01"}',
129+
JoltDateTime("2020-01-02T12:13:01+01")
130+
),
127131
(
128132
'{"T": "2020-01-02T12:13:01.1234+01"}',
129133
JoltDateTime("2020-01-02T12:13:01.1234+01")
130134
),
135+
(
136+
'{"T": "2020-01-02T12:13:01.1234+0100"}',
137+
JoltDateTime("2020-01-02T12:13:01.1234+01")
138+
),
139+
(
140+
'{"T": "2020-01-02T12:13:01.1234+01:00"}',
141+
JoltDateTime("2020-01-02T12:13:01.1234+01")
142+
),
143+
(
144+
'{"T": "2020-01-02T12:13:01.1234+01[Europe/Stockholm]"}',
145+
JoltDateTime("2020-01-02T12:13:01.1234+01[Europe/Stockholm]")
146+
),
147+
(
148+
'{"T": "2020-01-02T12:13:01.1234+0100[Europe/Stockholm]"}',
149+
JoltDateTime("2020-01-02T12:13:01.1234+0100[Europe/Stockholm]")
150+
),
151+
(
152+
'{"T": "2020-01-02T12:13:01.1234+01:00[Europe/Stockholm]"}',
153+
JoltDateTime("2020-01-02T12:13:01.1234+01:00[Europe/Stockholm]")
154+
),
131155

132156
# local date time - full
133157
(

0 commit comments

Comments
 (0)