Skip to content

Commit dd17769

Browse files
committed
break out asserters into utils
1 parent 0577c64 commit dd17769

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

pypop/notebook_interface/wizard.py

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,7 @@
1313
from .tqdm_widget import tqdm_notebook_noauto
1414

1515
from pypop.utils.exceptions import ExtraePRVNoOnOffEventsError
16-
17-
class TypeAsserter:
18-
"""Parent class for type asserter objects
19-
20-
Asserter classes provide a callable which can be used to assert the validity of an
21-
object.
22-
23-
Subclasses should implement a check_type function returning true/false as needed.
24-
"""
25-
26-
_expected = None
27-
28-
def __call__(self, testobj):
29-
if not self.check_type(testobj):
30-
raise TypeError(
31-
"Expected {} not {}".format(self._expected, testobj.__class__.__name__)
32-
)
33-
return True
34-
35-
def check_type(self, testobj):
36-
"""Return true if object is valid
37-
38-
Parameters
39-
----------
40-
testobj: object
41-
Object to be validated.
42-
"""
43-
raise NotImplementedError()
44-
45-
46-
class SimpleAsserter(TypeAsserter):
47-
"""Assert that an object is of a given type
48-
49-
Provides a callable object which asserts the provided object is of the required type.
50-
"""
51-
52-
def __init__(self, assert_class):
53-
"""Define class to be validated
54-
55-
Parameters
56-
----------
57-
assert_class: class
58-
Reference class to be compared to.
59-
"""
60-
self._assert_cls = assert_class
61-
self._exepceted = assert_class.__name__
62-
63-
64-
def check_type(self, testobj):
65-
if isinstance(testobj, self._assert_cls):
66-
return True
67-
68-
69-
class ListofStrAsserter(TypeAsserter):
70-
"""Assert that an object is a list of strings
71-
72-
Provides a callable object which asserts a provided object is a list of strings.
73-
"""
74-
_expected = "list of str"
75-
76-
def check_type(self, testobj):
77-
if isinstance(testobj, list) and all(isinstance(x, str) for x in testobj):
78-
return True
16+
from pypop.utils.asserters import ListofStrAsserter, SimpleAsserter
7917

8018

8119
class AnalysisState:

pypop/utils/asserters.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python3
2+
# SPDX-License-Identifier: BSD-3-Clause-Clear
3+
# Copyright (c) 2019, The Numerical Algorithms Group, Ltd. All rights reserved.
4+
5+
__all__ = ["SimpleAsserter", "ListofStrAsserter"]
6+
7+
8+
class BaseAsserter:
9+
"""Parent class for type asserter objects
10+
11+
Asserter classes provide a callable which can be used to assert the validity of an
12+
object.
13+
14+
Subclasses should implement a check_type function returning true/false as needed.
15+
"""
16+
17+
_expected = None
18+
19+
def __call__(self, testobj):
20+
if not self.check_type(testobj):
21+
raise TypeError(
22+
"Expected {} not {}".format(self._expected, testobj.__class__.__name__)
23+
)
24+
return True
25+
26+
def check_type(self, testobj):
27+
"""Return true if object is valid
28+
29+
Parameters
30+
----------
31+
testobj: object
32+
Object to be validated.
33+
"""
34+
raise NotImplementedError()
35+
36+
37+
class SimpleAsserter(BaseAsserter):
38+
"""Assert that an object is of a given type
39+
40+
Provides a callable object which asserts the provided object is of the required type.
41+
"""
42+
43+
def __init__(self, assert_class):
44+
"""Define class to be validated
45+
46+
Parameters
47+
----------
48+
assert_class: class
49+
Reference class to be compared to.
50+
"""
51+
self._assert_cls = assert_class
52+
self._expected = assert_class.__name__
53+
54+
def check_type(self, testobj):
55+
if isinstance(testobj, self._assert_cls):
56+
return True
57+
58+
59+
class ListofStrAsserter(BaseAsserter):
60+
"""Assert that an object is a list of strings
61+
62+
Provides a callable object which asserts a provided object is a list of strings.
63+
"""
64+
65+
_expected = "list of str"
66+
67+
def check_type(self, testobj):
68+
if isinstance(testobj, list) and all(isinstance(x, str) for x in testobj):
69+
return True

0 commit comments

Comments
 (0)