-
Notifications
You must be signed in to change notification settings - Fork 8
/
exceptions.py
169 lines (110 loc) · 4.23 KB
/
exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Custom exception types."""
# --- import --------------------------------------------------------------------------------------
import os
import pathlib
import warnings
# --- custom exceptions ---------------------------------------------------------------------------
class WrightToolsException(Exception):
"""WrightTools Base Exception."""
pass
class DimensionalityError(WrightToolsException):
"""DimensionalityError."""
def __init__(self, expected, recieved):
"""Dimensionality error.
Parameters
----------
expected : object
Expected dimensionalit(ies).
recieved : object
Recieved dimensionality.
"""
message = "dimensionality must be {0} (recieved {1})".format(expected, recieved)
super().__init__(self, message)
class NameNotUniqueError(WrightToolsException):
"""NameNotUniqueError."""
def __init__(self, name=None):
"""Format a Name Not Unique Error.
Parameters
----------
name : string
Name of an attribute which causes a duplication.
"""
if name is not None:
message = "Name {} results in a duplicate".format(name)
else:
message = "Names must be unique"
super().__init__(self, message)
class MultidimensionalAxisError(WrightToolsException):
"""Error for when operation does not support Multidimensional Axes."""
def __init__(self, axis, operation):
"""Multidimesional Axis error.
Parameters
----------
axis : str
Name of axis which causes the error.
operation : str
Name of operation which cannot handle multidimensional axes.
"""
message = "{} can not handle multidimensional axis: {}".format(operation, axis)
super().__init__(self, message)
class ValueError(ValueError, WrightToolsException):
"""Raised when an argument has the right type but an inappropriate value."""
pass
class FileExistsError(FileExistsError, WrightToolsException):
"""Raised when trying to create a file or directory which already exists.
Corresponds to errno ``EEXIST``.
"""
pass
class TypeError(TypeError, WrightToolsException):
"""Raised when an operation or function is applied to an object of inappropriate type.
The associated value is a string giving details about the type mismatch.
"""
pass
class UnitsError(WrightToolsException):
"""Units Error."""
def __init__(self, expected, recieved):
"""Units error.
Parameters
----------
expected : object
Expected units.
recieved : object
Recieved units.
"""
message = "expected units of {0} (recieved {1})".format(expected, recieved)
super().__init__(self, message)
# --- custom warnings -----------------------------------------------------------------------------
class WrightToolsWarning(Warning):
"""WrightTools Base Warning."""
pass
class EntireDatasetInMemoryWarning(WrightToolsWarning):
"""Warn when an entire dataset is taken into memory at once.
Such operations may lead to memory overflow errors for large datasets.
Warning ignored by default.
"""
pass
warnings.simplefilter("ignore", category=EntireDatasetInMemoryWarning) # ignore by default
class VisibleDeprecationWarning(WrightToolsWarning):
"""VisibleDepreciationWarning."""
pass
class WrongFileTypeWarning(WrightToolsWarning):
"""WrongFileTypeWarning."""
def warn(filepath, expected):
"""Raise warning.
Parameters
----------
filepath : path-like
Given filepath.
expected : string
Expected file suffix.
"""
filepath = pathlib.Path(filepath)
message = "file {0} has type {1} (expected {2})".format(
filepath, filepath.suffix, expected
)
warnings.warn(message, WrongFileTypeWarning)
class ObjectExistsWarning(WrightToolsWarning):
"""Warn that an HDF5 object already exists when a new one is requested."""
def warn(name):
message = "object '{0}' already exists, returning existing copy".format(name)
warnings.warn(message, ObjectExistsWarning)