Skip to content

Commit 9371204

Browse files
authored
Refactor: remove Python 2 leftovers (#71)
* fix: new style classes * fix: use logger.warning * style: run ruff formatter
1 parent 860d05a commit 9371204

File tree

15 files changed

+33
-52
lines changed

15 files changed

+33
-52
lines changed

docs/conf.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
#
32
# python-hl7 documentation build configuration file, created by
43
# sphinx-quickstart on Tue Jul 12 10:57:30 2011.
@@ -43,8 +42,8 @@
4342
master_doc = "index"
4443

4544
# General information about the project.
46-
project = u"python-hl7"
47-
copyright = u"2011-{}, John Paulett".format(date.today().year)
45+
project = "python-hl7"
46+
copyright = "2011-{}, John Paulett".format(date.today().year)
4847

4948
# The version info for the project you're documenting, acts as replacement for
5049
# |version| and |release|, also used in various other places throughout the
@@ -189,7 +188,7 @@
189188
# Grouping the document tree into LaTeX files. List of tuples
190189
# (source start file, target name, title, author, documentclass [howto/manual]).
191190
latex_documents = [
192-
("index", "python-hl7.tex", u"python-hl7 Documentation", u"John Paulett", "manual"),
191+
("index", "python-hl7.tex", "python-hl7 Documentation", "John Paulett", "manual"),
193192
]
194193

195194
# The name of an image file (relative to this directory) to place at the top of
@@ -220,16 +219,16 @@
220219

221220
# One entry per manual page. List of tuples
222221
# (source start file, name, description, authors, manual section).
223-
man_pages = [("mllp_send", "mllp_send", "MLLP network client", [u"John Paulett"], 1)]
222+
man_pages = [("mllp_send", "mllp_send", "MLLP network client", ["John Paulett"], 1)]
224223

225224

226225
# -- Options for Epub output ---------------------------------------------------
227226

228227
# Bibliographic Dublin Core info.
229-
epub_title = u"python-hl7"
230-
epub_author = u"John Paulett"
231-
epub_publisher = u"John Paulett"
232-
epub_copyright = u"2011, John Paulett"
228+
epub_title = "python-hl7"
229+
epub_author = "John Paulett"
230+
epub_publisher = "John Paulett"
231+
epub_copyright = "2011, John Paulett"
233232

234233
# The language of the text. It defaults to the language option
235234
# or en if the language is not set.

hl7/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""python-hl7 is a simple library for parsing messages of Health Level 7
32
(HL7) version 2.x into Python objects.
43

hl7/accessor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from collections import namedtuple
32

43

@@ -27,7 +26,7 @@ def __new__(
2726
subcomponent_num=None,
2827
):
2928
"""Create a new instance of Accessor for *segment*. Index numbers start from 1."""
30-
return super(Accessor, cls).__new__(
29+
return super().__new__(
3130
cls,
3231
segment,
3332
segment_num,

hl7/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class MLLPException(Exception):
1919
pass
2020

2121

22-
class MLLPClient(object):
22+
class MLLPClient:
2323
"""
2424
A basic, blocking, HL7 MLLP client based upon :py:mod:`socket`.
2525

hl7/containers.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import datetime
32
import logging
43

@@ -53,7 +52,7 @@ def __init__(
5352
# Initialize the list object, optionally passing in the
5453
# sequence. Since list([]) == [], using the default
5554
# parameter will not cause any issues.
56-
super(Container, self).__init__(sequence)
55+
super().__init__(sequence)
5756
self.separator = separator
5857
self.esc = esc
5958
self.separators = separators
@@ -125,7 +124,7 @@ def create_component(self, seq):
125124
def __getitem__(self, item):
126125
# Python slice operator was returning a regular list, not a
127126
# Container subclass
128-
sequence = super(Container, self).__getitem__(item)
127+
sequence = super().__getitem__(item)
129128
if isinstance(item, slice):
130129
return self.__class__(
131130
self.separator,
@@ -136,11 +135,6 @@ def __getitem__(self, item):
136135
)
137136
return sequence
138137

139-
def __getslice__(self, i, j):
140-
# Python 2.x compatibility. __getslice__ is deprecated, and
141-
# we want to wrap the logic from __getitem__ when handling slices
142-
return self.__getitem__(slice(i, j))
143-
144138
def __str__(self):
145139
return self.separator.join((str(x) for x in self))
146140

@@ -158,7 +152,7 @@ def __init__(
158152
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
159153
):
160154
assert not separator or separator == separators[0]
161-
super(File, self).__init__(
155+
super().__init__(
162156
separator=separators[0],
163157
sequence=sequence,
164158
esc=esc,
@@ -226,11 +220,11 @@ def __str__(self):
226220
"Either both header and trailer must be present or neither"
227221
)
228222
return (
229-
super(File, self).__str__()
223+
super().__str__()
230224
if not self.header
231225
else str(self.header)
232226
+ self.separator
233-
+ super(File, self).__str__()
227+
+ super().__str__()
234228
+ str(self.trailer)
235229
+ self.separator
236230
)
@@ -249,7 +243,7 @@ def __init__(
249243
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
250244
):
251245
assert not separator or separator == separators[0]
252-
super(Batch, self).__init__(
246+
super().__init__(
253247
separator=separators[0],
254248
sequence=sequence,
255249
esc=esc,
@@ -317,11 +311,11 @@ def __str__(self):
317311
"Either both header and trailer must be present or neither"
318312
)
319313
return (
320-
super(Batch, self).__str__()
314+
super().__str__()
321315
if not self.header
322316
else str(self.header)
323317
+ self.separator
324-
+ super(Batch, self).__str__()
318+
+ super().__str__()
325319
+ str(self.trailer)
326320
+ self.separator
327321
)
@@ -332,7 +326,7 @@ def __init__(
332326
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
333327
):
334328
assert not separator or separator == separators[0]
335-
super(Message, self).__init__(
329+
super().__init__(
336330
separator=separators[0],
337331
sequence=sequence,
338332
esc=esc,
@@ -373,7 +367,7 @@ def __getitem__(self, key):
373367
return self.extract_field(*Accessor.parse_key(key))
374368
elif isinstance(key, Accessor):
375369
return self.extract_field(*key)
376-
return super(Message, self).__getitem__(key)
370+
return super().__getitem__(key)
377371

378372
def __setitem__(self, key, value):
379373
"""Index or accessor assignment.
@@ -396,7 +390,7 @@ def __setitem__(self, key, value):
396390
return self.assign_field(value, *Accessor.parse_key(key))
397391
elif isinstance(key, Accessor):
398392
return self.assign_field(value, *key)
399-
return super(Message, self).__setitem__(key, value)
393+
return super().__setitem__(key, value)
400394

401395
def segment(self, segment_id):
402396
"""Gets the first segment with the *segment_id* from the parsed
@@ -607,15 +601,15 @@ def __str__(self):
607601
"""
608602
# Per spec, Message Construction Rules, Section 2.6 (v2.8), Message ends
609603
# with the carriage return
610-
return super(Message, self).__str__() + self.separator
604+
return super().__str__() + self.separator
611605

612606

613607
class Segment(Container):
614608
def __init__(
615609
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
616610
):
617611
assert not separator or separator == separators[1]
618-
super(Segment, self).__init__(
612+
super().__init__(
619613
separator=separators[1],
620614
sequence=sequence,
621615
esc=esc,
@@ -774,15 +768,15 @@ def __str__(self):
774768
+ str(self[1])
775769
+ self.separator.join((str(x) for x in self[3:]))
776770
)
777-
return super(Segment, self).__str__()
771+
return super().__str__()
778772

779773

780774
class Field(Container):
781775
def __init__(
782776
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
783777
):
784778
assert not separator or separator == separators[2]
785-
super(Field, self).__init__(
779+
super().__init__(
786780
separator=separators[2],
787781
sequence=sequence,
788782
esc=esc,
@@ -801,7 +795,7 @@ def __init__(
801795
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
802796
):
803797
assert not separator or separator == separators[3]
804-
super(Repetition, self).__init__(
798+
super().__init__(
805799
separator=separators[3],
806800
sequence=sequence,
807801
esc=esc,
@@ -819,7 +813,7 @@ def __init__(
819813
self, separator=None, sequence=[], esc="\\", separators="\r|~^&", factory=None
820814
):
821815
assert not separator or separator == separators[4]
822-
super(Component, self).__init__(
816+
super().__init__(
823817
separator=separators[4],
824818
sequence=sequence,
825819
esc=esc,
@@ -832,7 +826,7 @@ def __init__(
832826
"""
833827

834828

835-
class Factory(object):
829+
class Factory:
836830
"""Factory used to create each type of Container.
837831
838832
A subclass can be used to create specialized subclasses of each container.

hl7/datatypes.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import datetime
32
import math
43
import re

hl7/parser.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from string import whitespace
32

43
from .containers import Factory
@@ -380,7 +379,7 @@ def create_parse_plan(strmsg, factory=Factory):
380379
return _ParsePlan(separators[0], separators, containers, esc, factory)
381380

382381

383-
class _ParsePlan(object):
382+
class _ParsePlan:
384383
"""Details on how to parse an HL7 message. Typically this object
385384
should be created via :func:`hl7.create_parse_plan`
386385
"""

hl7/util.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
import datetime
32
import logging
43
import random
@@ -194,7 +193,7 @@ def unescape(container, field, app_map=None): # noqa: C901
194193
value = "".join(collecting)
195194
collecting = []
196195
if not value:
197-
logger.warn(
196+
logger.warning(
198197
"Error unescaping value [%s], empty sequence found at %d",
199198
field,
200199
offset,
@@ -219,15 +218,15 @@ def unescape(container, field, app_map=None): # noqa: C901
219218
value[0] == "C"
220219
): # Convert to new Single Byte character set : 2.10.2
221220
# Two HEX values, first value chooses the character set (ISO-IR), second gives the value
222-
logger.warn(
221+
logger.warning(
223222
"Error inline character sets [%s] not implemented, field [%s], offset [%s]",
224223
value,
225224
field,
226225
offset,
227226
)
228227
elif value[0] == "M": # Switch to new Multi Byte character set : 2.10.2
229228
# Three HEX values, first value chooses the character set (ISO-IR), rest give the value
230-
logger.warn(
229+
logger.warning(
231230
"Error inline character sets [%s] not implemented, field [%s], offset [%s]",
232231
value,
233232
field,

tests/samples.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# -*- coding: utf-8 -*-
2-
31
# Sample message from HL7 Normative Edition
42
# http://healthinfo.med.dal.ca/hl7intro/CDA_R2_normativewebedition/help/v3guide/v3guide.htm#v3gexamples
53
sample_hl7 = "\r".join(

tests/test_accessor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
from unittest import TestCase
32

43
from hl7 import Accessor, Field, Message, Segment

0 commit comments

Comments
 (0)