Skip to content

Commit

Permalink
Fix Python < 3.6, add __slots__
Browse files Browse the repository at this point in the history
  • Loading branch information
henryiii committed Apr 5, 2019
1 parent a025ff2 commit 1458457
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
14 changes: 14 additions & 0 deletions decaylanguage/dec/dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ class DecFileParser(object):
>>> parser = DecFileParser('my-decay-file.dec')
>>> parser.parse()
"""

__slots__ = ("_grammar",
"_grammar_info",
"_decay_file",
"_parsed_dec_file",
"_parsed_decays")

def __init__(self, decay_file):
"""
Parser constructor.
Expand All @@ -36,6 +43,9 @@ def __init__(self, decay_file):
decay_file: str
Input .dec decay file.
"""
# Conversion to handle pathlib on Python < 3.6:
decay_file = str(decay_file)

# Check input file
if not os.path.exists(decay_file):
raise FileNotFoundError("'{0}'!".format(decay_file))
Expand Down Expand Up @@ -99,11 +109,15 @@ def load_grammar(self, filename=None, parser='lalr', lexer='standard', **options
See Lark's Lark class for a description of available options
for parser, lexer and options.
"""

if filename is None:
filename = 'decfile.lark'
with data.open_text(data, filename) as f:
self._grammar = f.read()
else:
# Conversion to handle pathlib on Python < 3.6:
filename = str(filename)

self._grammar = open(filename).read()

self._grammar_info = dict(lark_file=filename, parser=parser, lexer=lexer, **options)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def proc_readme(text):
'pathlib2>=2.3; python_version<"3.5"',
'enum34>=1.1; python_version<"3.4"',
'importlib_resources>=1.0; python_version<"3.7"',
'particle'
'particle>=0.4.1'
],
extras_require=extras,
setup_requires=['pytest-runner'],
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from decaylanguage.dec.dec import DecFileParser
from decaylanguage.dec.dec import DecFileNotParsed, DecayNotFound

# New in Python 3
try:
FileNotFoundError
except NameError:
FileNotFoundError = IOError


DIR = Path(__file__).parent.resolve()

Expand Down

0 comments on commit 1458457

Please sign in to comment.