Skip to content

Commit 91cd196

Browse files
authored
Merge pull request #97 from MrClock8163/change/keyword-arguments
Update function signatures to expect keywords where appropriate
2 parents d5a4b3b + 7765af7 commit 91cd196

File tree

7 files changed

+78
-26
lines changed

7 files changed

+78
-26
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ The project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
1515

1616
- Updated GeoCOM response parsing to raise exception when the number of
1717
received paramters does not match the number of parsers specified
18+
- Updated `GeoCom` init signature to require keyword arguments
19+
- Updated `GsiOnlineDNA` init signature to require keyword arguments
20+
- Updated `Angle` init signature to require keyword arguments
21+
- Updated `GsiWord` and subclass init signatures to require keyword arguments
22+
- Updated `GsiBlock` `.parse` and `.serialize` signatures to require keyword
23+
arguments
24+
- Updated `parse_gsi_word` signature to require keyword arguments
25+
- Updated `parse_gsi_blocks_from_file` signature to require keyword arguments
26+
- Updated `write_gsi_blocks_to_file` signature to require keyword arguments
1827

1928
## v0.13.0 (2025-09-29)
2029

src/geocompy/data.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ def __init__(
275275
self,
276276
value: float,
277277
unit: _AngleUnit = 'rad',
278-
/,
278+
*,
279279
normalize: bool = False,
280280
positive: bool = False
281281
):
@@ -504,7 +504,12 @@ def normalized(self, positive: bool = True) -> Self:
504504
Angle
505505
New `Angle` with normalized value.
506506
"""
507-
return type(self)(self._value, 'rad', True, positive)
507+
return type(self)(
508+
self._value,
509+
'rad',
510+
normalize=True,
511+
positive=positive
512+
)
508513

509514
def relative_to(self, other: SupportsFloat) -> Self:
510515
"""
@@ -872,6 +877,7 @@ def cross(self, other: Vector) -> Self:
872877
def _swizzle_component(
873878
self,
874879
component: Literal['x', 'y', 'z', '0'],
880+
*,
875881
flip: bool = False
876882
) -> float:
877883
"""
@@ -953,9 +959,9 @@ def swizzle(
953959
954960
"""
955961
return type(self)(
956-
self._swizzle_component(x, flip_x),
957-
self._swizzle_component(y, flip_y),
958-
self._swizzle_component(z, flip_z)
962+
self._swizzle_component(x, flip=flip_x),
963+
self._swizzle_component(y, flip=flip_y),
964+
self._swizzle_component(z, flip=flip_z)
959965
)
960966

961967

src/geocompy/geo/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class GeoCom(GeoComType):
133133
def __init__(
134134
self,
135135
connection: Connection,
136+
*,
136137
logger: Logger | None = None,
137138
retry: int = 2,
138139
checksum: bool = False

src/geocompy/gsi/dna/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class BEEPTYPE(IntEnum):
106106
def __init__(
107107
self,
108108
connection: Connection,
109+
*,
109110
logger: Logger | None = None,
110111
retry: int = 2
111112
):
@@ -282,7 +283,7 @@ def putrequest(
282283
Success of the change.
283284
"""
284285
asterisk = "*" if self.is_client_gsi16 else ""
285-
cmd = f"PUT/{asterisk}{word.serialize(self.is_client_gsi16):s}"
286+
cmd = f"PUT/{asterisk}{word.serialize(gsi16=self.is_client_gsi16):s}"
286287
comment = ""
287288
try:
288289
answer = self._conn.exchange(cmd)

src/geocompy/gsi/gsiformat.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,15 @@ def wi(self) -> int:
211211
@abstractmethod
212212
def serialize(
213213
self,
214+
*,
214215
gsi16: bool = False,
215216
angleunit: GsiUnit | None = None,
216217
distunit: GsiUnit | None = None
217218
) -> str:
218219
raise NotImplementedError()
219220

220221
def __str__(self) -> str:
221-
return self.serialize(True)
222+
return self.serialize(gsi16=True)
222223

223224
def __eq__(self, other: Any) -> bool:
224225
if not isinstance(other, GsiWord):
@@ -331,6 +332,7 @@ def parse(cls, value: str) -> GsiUnknownWord:
331332

332333
def serialize(
333334
self,
335+
*,
334336
gsi16: bool = False,
335337
angleunit: GsiUnit | None = None,
336338
distunit: GsiUnit | None = None
@@ -397,6 +399,7 @@ def parse(cls, value: str) -> Self:
397399

398400
def serialize(
399401
self,
402+
*,
400403
gsi16: bool = False,
401404
angleunit: GsiUnit | None = None,
402405
distunit: GsiUnit | None = None
@@ -461,6 +464,7 @@ def parse(cls, value: str) -> Self:
461464

462465
def serialize(
463466
self,
467+
*,
464468
gsi16: bool = False,
465469
angleunit: GsiUnit | None = None,
466470
distunit: GsiUnit | None = None
@@ -599,6 +603,7 @@ def parse(cls, value: str) -> Self:
599603

600604
def serialize(
601605
self,
606+
*,
602607
gsi16: bool = False,
603608
angleunit: GsiUnit | None = None,
604609
distunit: GsiUnit | None = None
@@ -673,6 +678,7 @@ def parse(cls, value: str) -> Self:
673678

674679
def serialize(
675680
self,
681+
*,
676682
gsi16: bool = False,
677683
angleunit: GsiUnit | None = None,
678684
distunit: GsiUnit | None = None
@@ -783,6 +789,7 @@ def parse(cls, value: str) -> Self:
783789

784790
def serialize(
785791
self,
792+
*,
786793
gsi16: bool = False,
787794
angleunit: GsiUnit | None = GsiUnit.DEG,
788795
distunit: GsiUnit | None = None
@@ -925,6 +932,7 @@ def parse(cls, value: str) -> Self:
925932

926933
def serialize(
927934
self,
935+
*,
928936
gsi16: bool = False,
929937
angleunit: GsiUnit | None = None,
930938
distunit: GsiUnit | None = GsiUnit.CENTIMILLI
@@ -1054,6 +1062,7 @@ def parse(cls, value: str) -> Self:
10541062

10551063
def serialize(
10561064
self,
1065+
*,
10571066
gsi16: bool = False,
10581067
angleunit: GsiUnit | None = None,
10591068
distunit: GsiUnit | None = None
@@ -1214,6 +1223,7 @@ def parse(
12141223

12151224
def serialize(
12161225
self,
1226+
*,
12171227
gsi16: bool = False,
12181228
angleunit: GsiUnit | None = None,
12191229
distunit: GsiUnit | None = None
@@ -1505,6 +1515,7 @@ def parse(cls, value: str) -> Self:
15051515

15061516
def serialize(
15071517
self,
1518+
*,
15081519
gsi16: bool = False,
15091520
angleunit: GsiUnit | None = None,
15101521
distunit: GsiUnit | None = None
@@ -1562,6 +1573,7 @@ def parse(cls, value: str) -> Self:
15621573

15631574
def serialize(
15641575
self,
1576+
*,
15651577
gsi16: bool = False,
15661578
angleunit: GsiUnit | None = None,
15671579
distunit: GsiUnit | None = None
@@ -1619,6 +1631,7 @@ def parse(cls, value: str) -> Self:
16191631

16201632
def serialize(
16211633
self,
1634+
*,
16221635
gsi16: bool = False,
16231636
angleunit: GsiUnit | None = None,
16241637
distunit: GsiUnit | None = None
@@ -1749,6 +1762,7 @@ def parse(cls, value: str) -> Self:
17491762

17501763
def serialize(
17511764
self,
1765+
*,
17521766
gsi16: bool = False,
17531767
angleunit: GsiUnit | None = None,
17541768
distunit: GsiUnit | None = None
@@ -2013,6 +2027,7 @@ def WI(cls) -> int:
20132027

20142028
def parse_gsi_word(
20152029
value: str,
2030+
*,
20162031
dna: bool = False,
20172032
strict: bool = False
20182033
) -> GsiWord:
@@ -2148,6 +2163,7 @@ def address(self) -> int | None:
21482163
def parse(
21492164
cls,
21502165
data: str,
2166+
*,
21512167
dna: bool = False,
21522168
keep_unknowns: bool = False
21532169
) -> Self:
@@ -2227,7 +2243,11 @@ def parse(
22272243
words: list[GsiWord] = []
22282244
for i in range(wordsize, len(data), wordsize):
22292245
wordstring = data[i:i+wordsize]
2230-
word = parse_gsi_word(wordstring, dna, False)
2246+
word = parse_gsi_word(
2247+
wordstring,
2248+
dna=dna,
2249+
strict=False
2250+
)
22312251
if isinstance(word, GsiUnknownWord) and not keep_unknowns:
22322252
continue
22332253

@@ -2265,6 +2285,7 @@ def get_word(self, wordtype: type[_T]) -> _T | None:
22652285

22662286
def serialize(
22672287
self,
2288+
*,
22682289
address: int | None = None,
22692290
gsi16: bool = False,
22702291
endl: bool = True,
@@ -2301,11 +2322,11 @@ def serialize(
23012322
"""
23022323
match self.blocktype:
23032324
case "measurement":
2304-
header = GsiPointNameWord(self.value).serialize(gsi16)
2325+
header = GsiPointNameWord(self.value).serialize(gsi16=gsi16)
23052326
case "code":
2306-
header = GsiCodeWord(self.value, False).serialize(gsi16)
2327+
header = GsiCodeWord(self.value, False).serialize(gsi16=gsi16)
23072328
case "specialcode":
2308-
header = GsiCodeWord(self.value, True).serialize(gsi16)
2329+
header = GsiCodeWord(self.value, True).serialize(gsi16=gsi16)
23092330
case _:
23102331
raise ValueError(f"Unknown block type: '{self.blocktype}'")
23112332

@@ -2317,7 +2338,11 @@ def serialize(
23172338

23182339
output = header + "".join(
23192340
[
2320-
w.serialize(gsi16, angleunit, distunit)
2341+
w.serialize(
2342+
gsi16=gsi16,
2343+
angleunit=angleunit,
2344+
distunit=distunit
2345+
)
23212346
for w in self
23222347
]
23232348
)
@@ -2348,6 +2373,7 @@ def without_unknowns(self) -> Self:
23482373

23492374
def parse_gsi_blocks_from_file(
23502375
file: TextIO,
2376+
*,
23512377
dna: bool = False,
23522378
keep_unknowns: bool = False,
23532379
strict: bool = False
@@ -2375,7 +2401,11 @@ def parse_gsi_blocks_from_file(
23752401
if not line.strip():
23762402
continue
23772403
try:
2378-
block = GsiBlock.parse(line.strip("\n"), dna, keep_unknowns)
2404+
block = GsiBlock.parse(
2405+
line.strip("\n"),
2406+
dna=dna,
2407+
keep_unknowns=keep_unknowns
2408+
)
23792409
except Exception as e:
23802410
if strict:
23812411
raise e
@@ -2388,6 +2418,7 @@ def parse_gsi_blocks_from_file(
23882418
def write_gsi_blocks_to_file(
23892419
blocks: Iterable[GsiBlock],
23902420
file: TextIO,
2421+
*,
23912422
gsi16: bool = False,
23922423
angleunit: GsiUnit = GsiUnit.DEG,
23932424
distunit: GsiUnit = GsiUnit.MILLI,
@@ -2431,10 +2462,10 @@ def get_addresser(
24312462
for block in blocks:
24322463
file.write(
24332464
block.serialize(
2434-
next(addresser),
2435-
gsi16,
2436-
True,
2437-
angleunit,
2438-
distunit
2465+
address=next(addresser),
2466+
gsi16=gsi16,
2467+
endl=True,
2468+
angleunit=angleunit,
2469+
distunit=distunit
24392470
)
24402471
)

tests/helpers_gsionline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def exchange(self, cmd: str) -> str:
4242
return "?"
4343
elif self._GET.match(cmd):
4444
if cmd == "GET/I/WI71":
45-
return gsi.GsiRemark2Word("1").serialize(self._gsi16)
45+
return gsi.GsiRemark2Word("1").serialize(gsi16=self._gsi16)
4646
elif cmd != "GET/I/WI0":
4747
wi = int(cmd.split("/")[-1].removeprefix("WI"))
4848

tests/test_gsiformat.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def test_serialization(self) -> None:
124124
word = wordtype(args)
125125

126126
assert word.serialize(
127-
gsi16,
128-
angleunit,
129-
distunit
127+
gsi16=gsi16,
128+
angleunit=angleunit,
129+
distunit=distunit
130130
) == expected
131131

132132

@@ -138,7 +138,11 @@ def run_parsing_test(
138138
dna: bool = False
139139
) -> None:
140140
with open(filepath, "rt", encoding="utf8") as file:
141-
blocks = parse_gsi_blocks_from_file(file, dna, True)
141+
blocks = parse_gsi_blocks_from_file(
142+
file,
143+
dna=dna,
144+
strict=True
145+
)
142146

143147
assert len(blocks) == count
144148

@@ -160,7 +164,7 @@ def test_serialization(self) -> None:
160164
GsiBenchmarkHeightWord(123.456),
161165
address=0
162166
)
163-
text = b1.serialize(1, distunit=GsiUnit.CENTIMILLI, endl=False)
167+
text = b1.serialize(address=1, distunit=GsiUnit.CENTIMILLI, endl=False)
164168
assert text == "110001+000000P1 83...8+12345600 "
165169

166170
b2 = GsiBlock(
@@ -169,7 +173,7 @@ def test_serialization(self) -> None:
169173
GsiInfo1Word("STN"),
170174
address=2
171175
)
172-
text = b2.serialize(None, True, endl=False)
176+
text = b2.serialize(address=None, gsi16=True, endl=False)
173177
assert text == "*410002+?..............2 42....+0000000000000STN "
174178

175179
b3 = GsiBlock(
@@ -178,5 +182,5 @@ def test_serialization(self) -> None:
178182
GsiInfo1Word("STN"),
179183
address=3
180184
)
181-
text = b3.serialize(None, True, endl=False)
185+
text = b3.serialize(address=None, gsi16=True, endl=False)
182186
assert text == "*410003+0000000000000002 42....+0000000000000STN "

0 commit comments

Comments
 (0)