Skip to content

Commit 6947ba4

Browse files
committed
Installed precommit
- Installed precommit and ran against all files. - Code black reformatted. - Added flake8 as a dev dependency since it is used in ci script.
1 parent 2055f57 commit 6947ba4

File tree

13 files changed

+149
-115
lines changed

13 files changed

+149
-115
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,4 @@
198198
distributed under the License is distributed on an "AS IS" BASIS,
199199
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200200
See the License for the specific language governing permissions and
201-
limitations under the License.
201+
limitations under the License.

examples/document.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
"Chapter 1": {}
1313
}
1414
}
15-
}
15+
}

examples/document.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,3 @@ A model of a paragraph
4040

4141

4242
</table>
43-

examples/document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import modelspec
32
from modelspec import field, instance_of, optional
43
from modelspec.base_types import Base
@@ -15,6 +14,7 @@ class Paragraph(Base):
1514
Args:
1615
contents: Paragraph contents, which make up the _Section_s.
1716
"""
17+
1818
contents: str = field(validator=instance_of(str))
1919

2020

@@ -27,6 +27,7 @@ class Section(Base):
2727
id: The id of the section
2828
paragraphs: The paragraphs
2929
"""
30+
3031
id: str = field(validator=instance_of(str))
3132
paragraphs: List[Paragraph] = field(factory=list)
3233

@@ -42,6 +43,7 @@ class Document(Base):
4243
ISBN: International Standard Book Number
4344
sections: The sections of the document
4445
"""
46+
4547
id: str = field(validator=instance_of(str))
4648
title: str = field(default=None, validator=optional(instance_of(str)))
4749
ISBN: int = field(default=None, validator=optional(instance_of(int)))

examples/document.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,3 @@ Allowed field Data Type Description
5454
=============== =========== =================================================
5555
**contents** str *Paragraph contents, which make up the Sections.*
5656
=============== =========== =================================================
57-

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ requires = [
66
"setuptools_scm>=4.1.2"
77
]
88

9-
build-backend = "setuptools.build_meta"
9+
build-backend = "setuptools.build_meta"

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@ ignore =
116116
CONTRIBUTING.md
117117
*.html
118118
src/modeci_mdf/version.py
119-
tests/.pytest_cache/**
119+
tests/.pytest_cache/**

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"pytorch-sphinx-theme==0.0.19",
2424
"sphinxcontrib-versioning",
2525
],
26-
"dev": [],
26+
"dev": ["flake8"],
2727
}
2828
extras["all"] = sum(extras.values(), [])
2929
extras["dev"] += extras["test"]
3030

31-
setup(extras_require=extras)
31+
setup(extras_require=extras)

src/modelspec/__init__.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
11
__version__ = "0.1.5"
22

3-
from .base_types import (
4-
Base,
5-
define,
6-
has,
7-
field,
8-
fields,
9-
optional,
10-
instance_of,
11-
in_
12-
)
13-
14-
__all__ = [
15-
"Base",
16-
"define",
17-
"has",
18-
"field",
19-
"fields",
20-
"optional",
21-
"instance_of",
22-
"in_"
23-
]
3+
from .base_types import Base, define, has, field, fields, optional, instance_of, in_
244

5+
__all__ = ["Base", "define", "has", "field", "fields", "optional", "instance_of", "in_"]

src/modelspec/base_types.py

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class EvaluableExpression(str):
5151
EvaluableExpression is a string that can be evaluated to a value during MDF execution. This class inherits from
5252
str, so it can be used as a string.
5353
"""
54+
5455
def __init__(self, expr):
5556
self.expr = expr
5657

@@ -68,7 +69,7 @@ def print_(text: str, print_it: bool = False):
6869
if not isinstance(text, str):
6970
text = ("%s" % text).decode("ascii")
7071
if print_it:
71-
print("%s%s" % (prefix, text.replace("\n", "\n" + prefix)))
72+
print("{}{}".format(prefix, text.replace("\n", "\n" + prefix)))
7273

7374

7475
def print_v(text: str):
@@ -372,7 +373,9 @@ def _is_child_field(cls, field_name: str) -> bool:
372373
try:
373374
f = attr.fields_dict(cls)[field_name]
374375
except KeyError:
375-
raise ValueError(f"Field '{field_name}' not found in modelspec class '{cls.__name__}'")
376+
raise ValueError(
377+
f"Field '{field_name}' not found in modelspec class '{cls.__name__}'"
378+
)
376379

377380
# Check if the type of the field is a list or dict
378381
collection_arg = None
@@ -430,13 +433,13 @@ def _is_evaluable_expression(value: Any) -> bool:
430433

431434
@classmethod
432435
def _is_base_type(
433-
cls,
434-
value,
435-
can_be_list=False,
436-
can_be_dict=False,
437-
can_be_ndarray=False,
438-
can_be_none=False,
439-
can_be_eval_expr=False,
436+
cls,
437+
value,
438+
can_be_list=False,
439+
can_be_dict=False,
440+
can_be_ndarray=False,
441+
can_be_none=False,
442+
can_be_eval_expr=False,
440443
):
441444

442445
import numpy
@@ -452,16 +455,16 @@ def _is_base_type(
452455
value = get_origin(value)
453456

454457
return (
455-
value == int
456-
or value == str
457-
or value == bool
458-
or value == float
459-
or (can_be_list and value == list)
460-
or (can_be_dict and value == dict)
461-
or (can_be_ndarray and value == numpy.ndarray)
462-
or (can_be_none and value is type(None))
463-
or (can_be_eval_expr and cls._is_evaluable_expression(value))
464-
or value == Union
458+
value == int
459+
or value == str
460+
or value == bool
461+
or value == float
462+
or (can_be_list and value == list)
463+
or (can_be_dict and value == dict)
464+
or (can_be_ndarray and value == numpy.ndarray)
465+
or (can_be_none and value is type(None))
466+
or (can_be_eval_expr and cls._is_evaluable_expression(value))
467+
or value == Union
465468
)
466469

467470
@staticmethod
@@ -488,7 +491,11 @@ def _type_to_str(type_: Any) -> str:
488491
elif get_origin(type_) == dict and len(get_args(type_)) > 0:
489492
return Base._type_to_str(get_args(type_)[1])
490493
elif get_origin(type_) == Union and len(get_args(type_)) > 0:
491-
return "Union[" + ", ".join([Base._type_to_str(arg) for arg in get_args(type_)]) + "]"
494+
return (
495+
"Union["
496+
+ ", ".join([Base._type_to_str(arg) for arg in get_args(type_)])
497+
+ "]"
498+
)
492499

493500
# Fallback to returning just the string representation. Drop any occurrence of typing
494501
return str(type_).replace("typing.", "")
@@ -520,9 +527,9 @@ def _cls_generate_documentation(cls, format: str = MARKDOWN_FORMAT):
520527
allowed_fields = cls._parse_allowed_fields()
521528
allowed_children = cls._parse_allowed_children()
522529

523-
print(" - %s (%s)" % (cls.__name__, definition))
530+
print(f" - {cls.__name__} ({definition})")
524531

525-
rst_url_format = '`%s <%s>`_'
532+
rst_url_format = "`%s <%s>`_"
526533

527534
def insert_links(text, format=MARKDOWN_FORMAT):
528535
if not "_" in text:
@@ -534,12 +541,15 @@ def insert_links(text, format=MARKDOWN_FORMAT):
534541
for i in range(int(len(split) / 2.0)):
535542
pre = split[i * 2]
536543
type = split[i * 2 + 1]
537-
if format==MARKDOWN_FORMAT:
538-
text2 += '%s<a href="#%s">%s</a>' % (pre, type.lower(), type)
539-
elif format==RST_FORMAT:
540-
#text2 += ('%s'+rst_url_format) % (pre, type, '#'+type.lower # problem with handling links ending with s e.g. _Graph_s
541-
542-
text2 += ('%s%s') % (pre, type) # temp hack... problem with handling links ending with s e.g. _Graph_s
544+
if format == MARKDOWN_FORMAT:
545+
text2 += f'{pre}<a href="#{type.lower()}">{type}</a>'
546+
elif format == RST_FORMAT:
547+
# text2 += ('%s'+rst_url_format) % (pre, type, '#'+type.lower # problem with handling links ending with s e.g. _Graph_s
548+
549+
text2 += ("%s%s") % (
550+
pre,
551+
type,
552+
) # temp hack... problem with handling links ending with s e.g. _Graph_s
543553
if int(len(split) / 2.0) != len(split) / 2.0:
544554
text2 += split[-1]
545555
return text2
@@ -551,7 +561,7 @@ def insert_links(text, format=MARKDOWN_FORMAT):
551561
if definition is not None:
552562
doc_string += "%s\n\n" % insert_links(definition)
553563
elif format == RST_FORMAT:
554-
doc_string += "%s\n%s\n%s\n" % ("="*len(name), name, "="*len(name))
564+
doc_string += "{}\n{}\n{}\n".format("=" * len(name), name, "=" * len(name))
555565
if definition is not None:
556566
doc_string += "%s\n\n" % insert_links(definition, format=RST_FORMAT)
557567
elif format == DICT_FORMAT:
@@ -576,19 +586,17 @@ def insert_links(text, format=MARKDOWN_FORMAT):
576586
type_, can_be_eval_expr=True, can_be_dict=True
577587
)
578588
type_str = Base._type_to_str(type_)
579-
print(" Allowed parameter: %s %s" % (f, (description, type_str)))
589+
print(" Allowed parameter: {} {}".format(f, (description, type_str)))
580590

581591
if format == DICT_FORMAT:
582592
doc_dict[name]["allowed_parameters"][f] = {}
583593
doc_dict[name]["allowed_parameters"][f]["type"] = type_str
584-
doc_dict[name]["allowed_parameters"][f][
585-
"description"
586-
] = description
594+
doc_dict[name]["allowed_parameters"][f]["description"] = description
587595

588596
elif format == MARKDOWN_FORMAT:
589-
doc_string += "<tr><td><b>%s</b></td><td>%s</td>" % (
597+
doc_string += "<tr><td><b>{}</b></td><td>{}</td>".format(
590598
f,
591-
'<a href="#%s">%s</a>' % (type_str.lower(), type_str)
599+
f'<a href="#{type_str.lower()}">{type_str}</a>'
592600
if referencable
593601
else type_str,
594602
)
@@ -598,14 +606,12 @@ def insert_links(text, format=MARKDOWN_FORMAT):
598606

599607
elif format == RST_FORMAT:
600608
n = "**%s**" % f
601-
t = "%s" % (
602-
rst_url_format % (type_, '#'+type_str.lower())
609+
t = "{}".format(
610+
rst_url_format % (type_, "#" + type_str.lower())
603611
if referencable
604612
else type_str,
605613
)
606-
d = "*%s*" % (
607-
insert_links(description, format=RST_FORMAT)
608-
)
614+
d = "*%s*" % (insert_links(description, format=RST_FORMAT))
609615
table_info.append([n, t, d])
610616

611617
if referencable:
@@ -615,7 +621,13 @@ def insert_links(text, format=MARKDOWN_FORMAT):
615621
if format == MARKDOWN_FORMAT:
616622
doc_string += "\n</table>\n\n"
617623
elif format == RST_FORMAT:
618-
doc_string += "%s\n\n" % (tabulate(table_info, ['Allowed field', 'Data Type', 'Description'], tablefmt="rst"))
624+
doc_string += "%s\n\n" % (
625+
tabulate(
626+
table_info,
627+
["Allowed field", "Data Type", "Description"],
628+
tablefmt="rst",
629+
)
630+
)
619631

620632
if len(allowed_children) > 0:
621633
if format == MARKDOWN_FORMAT:
@@ -629,23 +641,21 @@ def insert_links(text, format=MARKDOWN_FORMAT):
629641

630642
for c, (description, type_) in allowed_children.items():
631643
type_str = Base._type_to_str(type_)
632-
print(" Allowed child: %s %s" % (c, (description, type_str)))
644+
print(" Allowed child: {} {}".format(c, (description, type_str)))
633645

634-
referencable = not Base._is_base_type(
635-
type_, can_be_dict=True
636-
)
646+
referencable = not Base._is_base_type(type_, can_be_dict=True)
637647

638648
if format == DICT_FORMAT:
639649
doc_dict[name]["allowed_children"][c] = {}
640650
doc_dict[name]["allowed_children"][c]["type"] = type_str
641-
doc_dict[name]["allowed_children"][c][
642-
"description"
643-
] = allowed_children[c][0]
651+
doc_dict[name]["allowed_children"][c]["description"] = allowed_children[
652+
c
653+
][0]
644654

645655
elif format == MARKDOWN_FORMAT:
646-
doc_string += "<tr><td><b>%s</b></td><td>%s</td>" % (
656+
doc_string += "<tr><td><b>{}</b></td><td>{}</td>".format(
647657
c,
648-
'<a href="#%s">%s</a>' % (type_str.lower(), type_str)
658+
f'<a href="#{type_str.lower()}">{type_str}</a>'
649659
if referencable
650660
else type_str,
651661
)
@@ -655,14 +665,12 @@ def insert_links(text, format=MARKDOWN_FORMAT):
655665

656666
elif format == RST_FORMAT:
657667
n = "**%s**" % c
658-
t = "%s" % (
659-
rst_url_format % (type_str, '#'+type_str.lower())
668+
t = "{}".format(
669+
rst_url_format % (type_str, "#" + type_str.lower())
660670
if referencable
661671
else type_str,
662672
)
663-
d = "*%s*" % (
664-
insert_links(description, format=RST_FORMAT)
665-
)
673+
d = "*%s*" % (insert_links(description, format=RST_FORMAT))
666674
table_info.append([n, t, d])
667675

668676
# Get the contained type
@@ -677,7 +685,13 @@ def insert_links(text, format=MARKDOWN_FORMAT):
677685
if format == MARKDOWN_FORMAT:
678686
doc_string += "\n</table>\n\n"
679687
elif format == RST_FORMAT:
680-
doc_string += "%s\n\n"%(tabulate(table_info, ['Allowed child','Data Type','Description'], tablefmt="rst"))
688+
doc_string += "%s\n\n" % (
689+
tabulate(
690+
table_info,
691+
["Allowed child", "Data Type", "Description"],
692+
tablefmt="rst",
693+
)
694+
)
681695

682696
for r in referenced:
683697
if format in (MARKDOWN_FORMAT, RST_FORMAT):

0 commit comments

Comments
 (0)