Advertisement
LaurentG

Untitled

Aug 25th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. from ladon.ladonizer import ladonize
  2. from ladon.types.ladontype import LadonType
  3.  
  4. class Complex(LadonType):
  5.   r = float
  6.   i = float
  7.  
  8.   def __init__(self,r=None, i=None, **kw):
  9.     # ASSIGNEMTS
  10.     # Class-local assignments
  11.     self.r = r
  12.     self.i = i
  13.     # prime_dict based assignments
  14.     LadonType.__init__(self,**kw)
  15.  
  16.   def __repr__(self):
  17.     return "(%g+%gj)" % (self.r, self.i)
  18.  
  19.  
  20. class Calculator(object):
  21.   """
  22.  This service does the math, and serves as example for new potential Ladon users.
  23.  """
  24.  
  25.   @ladonize(int,int,rtype=int)
  26.   def add(self,a,b):
  27.     """
  28.    Add two integers together and return the result.
  29.  
  30.    @param a: 1st integer.
  31.    @param b: 2nd integer.
  32.    @rtype: The result of the addition
  33.    """
  34.     return a+b
  35.  
  36.   @ladonize([int],rtype=int)
  37.   def add_many(self, args):
  38.     """
  39.    Add integers together and return the result
  40.  
  41.    @param args: integer
  42.    @rtype: The result of the addition
  43.    """
  44.     return reduce(lambda x, y: x+y, args)
  45.  
  46.   @ladonize(Complex, Complex, rtype=Complex)
  47.   def add_complex(self, a, b):
  48.     """ Add two complex numbers and return the result
  49.    @param a: 1st complex number
  50.    @param b: 2nd complex number
  51.    @rtype: The result of the addition
  52.    """
  53.     #ret = complex(a.r, a.i) + complex(b.r, b.i)
  54.     #return Complex(ret.real, ret.imag)
  55.     return a
  56.  
  57.  
  58. if __name__ == "__main__":
  59.   c = Calculator()
  60.   a = Complex(0, 1)
  61.   b = Complex(1, 0)
  62.   print c.add_complex(a, b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement