Skip to content

Commit f89e05a

Browse files
committed
3.10 adjustment: f-strings
1 parent 8d1fcad commit f89e05a

File tree

11 files changed

+31
-39
lines changed

11 files changed

+31
-39
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interchange = Interchange.from_str(
4949
)
5050
for message in interchange.get_messages():
5151
for segment in message.segments:
52-
print("Segment tag: {}, content: {}".format(segment.tag, segment.elements))
52+
print(f"Segment tag: {segment.tag}, content: {segment.elements}")
5353
```
5454

5555
You may also want to iterate directly on segments :
@@ -68,7 +68,7 @@ interchange = Interchange.from_str(
6868
)
6969

7070
for segment in interchange.segments:
71-
print("Segment tag: {}, content: {}".format(segment.tag, segment.elements))
71+
print(f"Segment tag: {segment.tags}, content: {segment.elements}")
7272
```
7373

7474
Or you can create an EDI interchange on the fly:
@@ -100,7 +100,7 @@ from pydifact.segmentcollection import RawSegmentCollection
100100
collection = RawSegmentCollection.from_str("UNH+1+ORDERS:D:96A:UN:EAN008'")
101101

102102
for segment in collection.segments:
103-
print("Segment tag: {}, content: {}".format(segment.tag, segment.elements))
103+
print(f"Segment tag: {segment.tags}, content: {segment.elements}")
104104
```
105105

106106

@@ -124,7 +124,7 @@ To develop pydifact, clone the repository and install the dev requirements:
124124

125125
```bash
126126
make dev
127-
# or
127+
# or
128128
# pip install -e .[dev]
129129
```
130130

TODO.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,4 @@
22

33
* Translation of following files:
44
* Parser, ParserTest
5-
* Message, MessageTest
6-
* When Python 3.10 is adapted:
7-
* replace Union[x,y] with x|y and Optional[x] with x|None in annotations
8-
* use f-strings instead of .format-strings
5+
* Message, MessageTest

pydifact/control/characters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def with_control_character(self, cc_type: str, char: str):
8282
# set the attribute dynamically.
8383
if not hasattr(self, cc_type):
8484
raise AttributeError(
85-
"{} doesn't have an attribute with the name '{}'".format(self, cc_type)
85+
f"{self} doesn't have an attribute with the name '{cc_type}'"
8686
)
8787

8888
other = copy(self)
@@ -114,10 +114,10 @@ def __str__(self) -> str:
114114
)
115115

116116
def __repr__(self):
117-
return "'{}'".format(self.__str__())
117+
return f"'{self.__str__()}'"
118118

119119
def __eq__(self, other):
120-
if type(other) == str:
120+
if isinstance(other, str):
121121
other = Characters.from_str(other)
122122
return (
123123
(self.component_separator == other.component_separator)

pydifact/segmentcollection.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def version(self) -> str:
313313
314314
:return: message version, parsable by pkg_resources.parse_version()
315315
"""
316-
return "{}.{}".format(self.identifier[1], self.identifier[2])
316+
return f"{self.identifier[1]}.{self.identifier[2]}"
317317

318318
def get_header_segment(self) -> Segment:
319319
return Segment(
@@ -382,7 +382,7 @@ def get_header_segment(self) -> Segment:
382382
[self.syntax_identifier[0], str(self.syntax_identifier[1])],
383383
self.sender,
384384
self.recipient,
385-
["{:%y%m%d}".format(self.timestamp), "{:%H%M}".format(self.timestamp)],
385+
[f"{self.timestamp:%y%m%d}", f"{self.timestamp:%H%M}"],
386386
self.control_reference,
387387
*self.extra_header_elements,
388388
)
@@ -422,7 +422,7 @@ def get_messages(self) -> Iterator[Message]:
422422
last_segment = segment
423423
else:
424424
raise EDISyntaxError(
425-
"Missing UNT segment before new UNH: {}".format(segment)
425+
f"Missing UNT segment before new UNH: segment{segment}"
426426
)
427427
elif segment.tag == "UNT":
428428
if message:
@@ -431,7 +431,7 @@ def get_messages(self) -> Iterator[Message]:
431431
last_segment = segment
432432
else:
433433
raise EDISyntaxError(
434-
'UNT segment without matching UNH: "{}"'.format(segment)
434+
f'UNT segment without matching UNH: "{segment}"'
435435
)
436436
else:
437437
if message:

pydifact/segments.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ def __init__(self, tag: str, *elements: Element) -> None:
6868

6969
def __str__(self) -> str:
7070
"""Returns the Segment in Python list printout"""
71-
return "'{tag}' EDI segment: {elements}".format(
72-
tag=self.tag, elements=str(self.elements)
73-
)
71+
return f"'{self.tag}' EDI segment: {self.elements}"
7472

7573
def __repr__(self) -> str:
76-
return "{} segment: {}".format(self.tag, str(self.elements))
74+
return f"{self.tag} segment: {str(self.elements)}"
7775

7876
def __eq__(self, other) -> bool:
7977
# FIXME the other way round too? isinstance(other, type(self))?
@@ -125,16 +123,12 @@ def create_segment(name: str, *elements: Element, validate: bool = True) -> Segm
125123

126124
if not isinstance(name, str):
127125
raise EDISyntaxError(
128-
"The tag name of a segment must be a str, but is a {}: {}".format(
129-
type(name), name
130-
)
126+
f"The tag name of a segment must be a str, but is a {type(name)}: {name}"
131127
)
132128

133129
if not name.isalnum():
134130
raise EDISyntaxError(
135-
"Tag '{}': A tag name must only contain alphanumeric characters.".format(
136-
name
137-
)
131+
f"Tag '{name}': A tag name must only contain alphanumeric characters."
138132
)
139133

140134
for Plugin in SegmentProvider.plugins:
@@ -149,7 +143,7 @@ def create_segment(name: str, *elements: Element, validate: bool = True) -> Segm
149143
if validate:
150144
if not s.validate():
151145
raise EDISyntaxError(
152-
"could not create '{}' Segment. Validation failed.".format(name)
146+
f"could not create '{name}' Segment. Validation failed."
153147
)
154148

155149
return s

pydifact/serializer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def escape(self, string: str | None) -> str:
101101

102102
if string is None:
103103
return ""
104-
assert isinstance(string, str), "%s is not a str, it is %s" % (string, type(string))
104+
assert isinstance(string, str), "%s is not a str, it is %s" % (
105+
string,
106+
type(string),
107+
)
105108

106109
return self.regexp.sub(lambda match: self.replace_map[match.group(0)], string)

pydifact/token.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ def __init__(self, token_type: Type, value: str):
4545
self.value = value
4646

4747
def __str__(self) -> str:
48-
return "{name} Token: '{value}'".format(name=self.type.name, value=self.value)
48+
return f"{self.type.name} Token: '{self.value}'"
4949

5050
def __repr__(self) -> str:
51-
return "<{name} Token object '{value}' at {address}>".format(
52-
name=self.type.name, value=self.value, address=hex(id(self))
53-
)
51+
return f"<{self.type.name} Token object '{self.value}' at {hex(id(self))}>"
5452

5553
def __eq__(self, other) -> bool:
5654
return self.type == other.type and self.value == other.value

tests/test_characters.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_with_separator_identity():
2222
one = Characters()
2323
other = Characters()
2424
# a copy of a characters object must be equal, but not the same
25-
assert one == other, 'Objects differ: "{}", "{}"'.format(one, other)
25+
assert one == other, f'Objects differ: "{one}", "{other}"'
2626
assert one is not other
2727

2828

tests/test_input_output.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323

2424
def test_wikipedia_file():
25-
message = Interchange.from_file("{}/wikipedia.edi".format(path))
25+
message = Interchange.from_file(f"{path}/wikipedia.edi")
2626
# make some checks
2727
assert message.get_header_segment() == Segment(
2828
"UNB", ["IATB", "1"], "6XPPC", "LHPPC", ["940101", "0950"], "1"
@@ -34,7 +34,7 @@ def test_wikipedia_file():
3434

3535

3636
def test_invoice_file():
37-
message = Interchange.from_file("{}/invoice1.edi".format(path))
37+
message = Interchange.from_file(f"{path}/invoice1.edi")
3838
# make some checks
3939
assert message.get_header_segment() == Segment(
4040
"UNB",
@@ -52,8 +52,8 @@ def test_invoice_file():
5252

5353

5454
# def test_order_file():
55-
# _test_file_read("{}/order.edi".format(path))
55+
# _test_file_read(f"{path}/order.edi")
5656
#
5757
#
5858
# def test_patient1_file():
59-
# _test_file_read("{}/patient1.edi".format(path))
59+
# _test_file_read(f"{path}/patient1.edi")

tests/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ def _assert_segments(parser, default_una_segment, collection: str, segments: lis
9595

9696
input_str = "UNA:+,? '\n" + collection + "'\n"
9797
result = list(parser.parse(input_str))
98-
# print("input segments: {}".format(segments[0]))
99-
# print("parser result: {}".format(result[0]))
98+
# print(f"input segments: {segments[0]}")
99+
# print(f"parser result: {result[0]}")
100100
assert segments == result
101101

102102

0 commit comments

Comments
 (0)