File size: 12,913 Bytes
079c32c |
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
import operator
from abc import abstractmethod
from functools import wraps
from typing import Callable, Any
from .base import ILoaderClass
def _callable_to_norm(func: Callable[[Any], Any]) -> 'INormClass':
"""
Overview:
Convert callable to norm.
Arguments:
- func (:obj:`Callable[[Any], Any]`): The callable to be converted.
"""
class _Norm(INormClass):
def _call(self, value):
return func(value)
return _Norm()
def norm(value) -> 'INormClass':
"""
Overview:
Convert value to norm.
Arguments:
- value (:obj:`Any`): The value to be converted.
"""
if isinstance(value, INormClass):
return value
elif isinstance(value, ILoaderClass):
return _callable_to_norm(value)
else:
return _callable_to_norm(lambda v: value)
def normfunc(func):
"""
Overview:
Convert function to norm function.
Arguments:
- func (:obj:`Callable[[Any], Any]`): The function to be converted.
"""
@wraps(func)
def _new_func(*args_norm, **kwargs_norm):
args_norm = [norm(item) for item in args_norm]
kwargs_norm = {key: norm(value) for key, value in kwargs_norm.items()}
def _callable(v):
args = [item(v) for item in args_norm]
kwargs = {key: value(v) for key, value in kwargs_norm.items()}
return func(*args, **kwargs)
return _callable_to_norm(_callable)
return _new_func
UNARY_FUNC = Callable[[Any], Any]
BINARY_FUNC = Callable[[Any, Any], Any]
def _unary(a: 'INormClass', func: UNARY_FUNC) -> 'INormClass':
"""
Overview:
Create a unary norm.
Arguments:
- a (:obj:`INormClass`): The norm.
- func (:obj:`UNARY_FUNC`): The function.
"""
return _callable_to_norm(lambda v: func(a(v)))
def _binary(a: 'INormClass', b: 'INormClass', func: BINARY_FUNC) -> 'INormClass':
"""
Overview:
Create a binary norm.
Arguments:
- a (:obj:`INormClass`): The first norm.
- b (:obj:`INormClass`): The second norm.
- func (:obj:`BINARY_FUNC`): The function.
"""
return _callable_to_norm(lambda v: func(a(v), b(v)))
def _binary_reducing(func: BINARY_FUNC, zero):
"""
Overview:
Create a binary reducing norm.
Arguments:
- func (:obj:`BINARY_FUNC`): The function.
- zero (:obj:`Any`): The zero value.
"""
@wraps(func)
def _new_func(*args) -> 'INormClass':
_sum = norm(zero)
for item in args:
_sum = _binary(_sum, norm(item), func)
return _sum
return _new_func
class INormClass:
"""
Overview:
The norm class.
Interfaces:
``__call__``, ``__add__``, ``__radd__``, ``__sub__``, ``__rsub__``, ``__mul__``, ``__rmul__``, ``__matmul__``,
``__rmatmul__``, ``__truediv__``, ``__rtruediv__``, ``__floordiv__``, ``__rfloordiv__``, ``__mod__``,
``__rmod__``, ``__pow__``, ``__rpow__``, ``__lshift__``, ``__rlshift__``, ``__rshift__``, ``__rrshift__``,
``__and__``, ``__rand__``, ``__or__``, ``__ror__``, ``__xor__``, ``__rxor__``, ``__invert__``, ``__pos__``,
``__neg__``, ``__eq__``, ``__ne__``, ``__lt__``, ``__le__``, ``__gt__``, ``__ge__``
"""
@abstractmethod
def _call(self, value):
"""
Overview:
Call the norm.
Arguments:
- value (:obj:`Any`): The value to be normalized.
"""
raise NotImplementedError
def __call__(self, value):
"""
Overview:
Call the norm.
Arguments:
- value (:obj:`Any`): The value to be normalized.
"""
return self._call(value)
def __add__(self, other):
"""
Overview:
Add the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__add__)
def __radd__(self, other):
"""
Overview:
Add the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) + self
def __sub__(self, other):
"""
Overview:
Subtract the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__sub__)
def __rsub__(self, other):
"""
Overview:
Subtract the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) - self
def __mul__(self, other):
"""
Overview:
Multiply the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__mul__)
def __rmul__(self, other):
"""
Overview:
Multiply the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) * self
def __matmul__(self, other):
"""
Overview:
Matrix multiply the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__matmul__)
def __rmatmul__(self, other):
"""
Overview:
Matrix multiply the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) @ self
def __truediv__(self, other):
"""
Overview:
Divide the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__truediv__)
def __rtruediv__(self, other):
"""
Overview:
Divide the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) / self
def __floordiv__(self, other):
"""
Overview:
Floor divide the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__floordiv__)
def __rfloordiv__(self, other):
"""
Overview:
Floor divide the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) // self
def __mod__(self, other):
"""
Overview:
Mod the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__mod__)
def __rmod__(self, other):
"""
Overview:
Mod the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) % self
def __pow__(self, power, modulo=None):
"""
Overview:
Power the norm.
Arguments:
- power (:obj:`Any`): The power.
- modulo (:obj:`Any`): The modulo.
"""
return _binary(self, norm(power), operator.__pow__)
def __rpow__(self, other):
"""
Overview:
Power the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) ** self
def __lshift__(self, other):
"""
Overview:
Lshift the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__lshift__)
def __rlshift__(self, other):
"""
Overview:
Lshift the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) << self
def __rshift__(self, other):
"""
Overview:
Rshift the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__rshift__)
def __rrshift__(self, other):
"""
Overview:
Rshift the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) >> self
def __and__(self, other):
"""
Overview:
And operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__and__)
def __rand__(self, other):
"""
Overview:
And operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) & self
def __or__(self, other):
"""
Overview:
Or operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__or__)
def __ror__(self, other):
"""
Overview:
Or operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) | self
def __xor__(self, other):
"""
Overview:
Xor operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__xor__)
def __rxor__(self, other):
"""
Overview:
Xor operation the norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return norm(other) ^ self
def __invert__(self):
"""
Overview:
Invert the norm.
"""
return _unary(self, operator.__invert__)
def __pos__(self):
"""
Overview:
Positive the norm.
"""
return _unary(self, operator.__pos__)
def __neg__(self):
"""
Overview:
Negative the norm.
"""
return _unary(self, operator.__neg__)
# Attention: DO NOT USE LINKING COMPARE OPERATORS, IT WILL CAUSE ERROR.
def __eq__(self, other):
"""
Overview:
Compare the norm if they are equal.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__eq__)
def __ne__(self, other):
"""
Overview:
Compare the norm if they are not equal.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__ne__)
def __lt__(self, other):
"""
Overview:
Compare the norm if it is less than the other norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__lt__)
def __le__(self, other):
"""
Overview:
Compare the norm if it is less than or equal to the other norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__le__)
def __gt__(self, other):
"""
Overview:
Compare the norm if it is greater than the other norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__gt__)
def __ge__(self, other):
"""
Overview:
Compare the norm if it is greater than or equal to the other norm.
Arguments:
- other (:obj:`Any`): The other norm.
"""
return _binary(self, norm(other), operator.__ge__)
lnot = normfunc(lambda x: not x)
land = _binary_reducing(lambda x, y: x and y, True)
lor = _binary_reducing(lambda x, y: x or y, True)
lin = normfunc(operator.__contains__)
lis = normfunc(operator.is_)
lisnot = normfunc(operator.is_not)
lsum = _binary_reducing(lambda x, y: x + y, 0)
_COMPARE_OPERATORS = {
'!=': operator.__ne__,
'==': operator.__eq__,
'<': operator.__lt__,
'<=': operator.__le__,
'>': operator.__gt__,
'>=': operator.__ge__,
}
@normfunc
def lcmp(first, *items):
"""
Overview:
Compare the items.
Arguments:
- first (:obj:`Any`): The first item.
- items (:obj:`Any`): The other items.
"""
if len(items) % 2 == 1:
raise ValueError('Count of items should be odd number but {number} found.'.format(number=len(items) + 1))
ops, items = items[0::2], items[1::2]
for op in ops:
if op not in _COMPARE_OPERATORS.keys():
raise KeyError('Invalid compare operator - {op}.'.format(op=repr(op)))
_last = first
for op, item in zip(ops, items):
if not _COMPARE_OPERATORS[op](_last, item):
return False
_last = item
return True
|