Guest User

Laba2

a guest
Dec 5th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. from functools import singledispatch, update_wrapper
  2.  
  3.  
  4. def methdispatch(func):
  5.     dispatcher = singledispatch(func)
  6.     def wrapper(*args, **kw):
  7.         return dispatcher.dispatch(args[1].__class__)(*args, **kw)
  8.     wrapper.register = dispatcher.register
  9.     update_wrapper(wrapper, func)
  10.     return wrapper
  11.  
  12. class Foo(object):
  13.     @methdispatch
  14.     def Subtraction(self, a, b):
  15.         raise NotImplementedError('Unsupported type')
  16.  
  17.  
  18.     @Subtraction.register(bool)
  19.     def _(self, p, q):
  20.         if (p == True) and (q == True) : return False
  21.         if (p == False) and (q == True): return True
  22.         if (p == True) and (q == False): return False
  23.         if (p == False) and (q == True): return False
  24.  
  25.  
  26.     @Subtraction.register(list)
  27.     def _(self, a, b):
  28.         if ( a[0] < b[0] <= a[1] ) and (b[0] <= a[1] < b[1]) : return [b[0], a[1]]
  29.         if (a[0] < b[0] <= b[1]) and (b[0] <= b[1] < a[1]): return [b[0], b[1]]
  30.         if (b[0] < a[0] <= b[1]) and (a[0] <= b[1] < a[1]): return [a[0], b[1]]
  31.         if (b[0] <= a[0] < a[1]) and (a[0] < a[1] < b[1]): return [a[0], a[1]]
  32.         if (a[1] < b[0] < b[1]) : return [0, 0]
  33.         if (b[1] < a[0] < a[1]) : return [0, 0]
  34.  
  35.  
  36.  
  37. q = Foo()
  38.  
  39. print(q.Subtraction(True, False))
Add Comment
Please, Sign In to add comment