Coverage for d7a/alp/operations/operation.py: 81%
16 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
1#
2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
3#
4# This file is part of pyd7a.
5# See https://github.com/Sub-IoT/pyd7a for further info.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
20# author: Christophe VG <contact@christophe.vg>
21# abstract base-class for operations
23from abc import ABCMeta
25from d7a.support.schema import Validatable
27class Operation(Validatable, metaclass=ABCMeta):
28 def __init__(self, operand=None):
29 if self.operand_class is None and operand is not None:
30 raise ValueError("{0} doesn't require an operand.".format(
31 self.__class__.__name__
32 ))
33 if (operand is None and self.operand_class is not None) or \
34 (operand is not None and not isinstance(operand, self.operand_class)):
35 raise ValueError("{0} requires a {1} operand".format(
36 self.__class__.__name__,
37 self.operand_class.__name__
38 ))
39 self.operand = operand
41 def __iter__(self):
42 if self.operand:
43 for byte in self.operand: yield byte
45 def __str__(self):
46 s = self.__class__.__name__
47 s += ": " + str(self.operand)
48 return s