You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

89 lines
3.0 KiB

class PortObject:
CLS_EQ = 1
CLS_RANGE = 2
CLS_LT = 3
CLS_GT = 4
CLS_NEQ = 5
tpe = {'eq': CLS_EQ, 'range': CLS_RANGE, 'lt': CLS_LT, 'gt': CLS_LT, 'neq': CLS_NEQ}
def __init__(self, eq=None, range_=None, lt=None, gt=None, neq=None):
po = dict()
if eq:
if not isinstance(eq, (list, tuple)):
raise ValueError("Unsupported type for eq parameter!")
po['eq'] = eq
if range_:
if not isinstance(range_, (list, tuple)):
raise ValueError("Unsupported type for range_ parameter!")
if len(range_) != 2:
raise ValueError("range_ should define range, so only 2 values accepted in tuple or list")
po['range'] = range_
if lt:
if not isinstance(lt, int):
raise ValueError("Unsupported type for lt parameter!")
po['lt'] = lt
if gt:
if not isinstance(gt, int):
raise ValueError("Unsupported type for gt parameter!")
po['gt'] = gt
if neq:
if not isinstance(neq, int):
raise ValueError("Unsupported type for neq parameter!")
po['neq'] = neq
if len(po) == 0:
raise ValueError("Cannot create object without parameters!")
elif len(po) > 1:
raise ValueError("Unable to determine type of PortObject. Please specify only one type of port definition!")
self.type = [x for x in po][0]
self._range_s = 0
self._range_e = 0
self._eq = list()
self._f_neq = False
if self.tpe[self.type] in [self.CLS_LT, self.CLS_GT, self.CLS_RANGE]:
if self.tpe[self.type] == self.CLS_LT:
self._range_s = 1
self._range_e = po['lt'] - 1
elif self.tpe[self.type] == self.CLS_GT:
self._range_s = po['gt'] + 1
self._range_e = 65535
elif self.tpe[self.type] == self.CLS_RANGE:
self._range_s = po['range'][0]
self._range_e = po['range'][1]
elif self.tpe[self.type] == self.CLS_EQ:
if isinstance(po['eq'], (tuple, list)):
self._eq = list(po['eq'])
else:
self._eq = [po['eq']]
elif self.tpe[self.type] == self.CLS_NEQ:
self._eq = [po['neq']]
self._f_neq = True
print(self._range_s, self._range_e, self._eq, self._f_neq)
def match(self, port):
if self.tpe[self.type] in [self.CLS_LT, self.CLS_GT, self.CLS_RANGE]:
return self._range_s <= port <= self._range_e
elif self.tpe[self.type] == self.CLS_EQ:
return port in self._eq
elif self.tpe[self.type] == self.CLS_NEQ:
return port not in self._eq
if __name__ == "__main__":
po1 = PortObject(range_=(5,10))
print(po1.type)
po2 = PortObject(eq=[1024, 3389, 8080])
print(po2.type)
print(po2.match(4500))
po3 = PortObject(neq=25)
print(po3.type)
print(po3.match(4500))