###########################################################
########################## lec 14 #########################
###########################################################

import pluscount

pluscount.ctr

pluscount.reset

pluscount.next()
pluscount.next()
pluscount.next()


import minuscount 

minuscount.ctr

minuscount.reset

minuscount.next()
minuscount.next()
minuscount.next()
minuscount.next()



def doCount(c,n=10):
  c.reset()
  for i in range(n):
    print c.next()



doCount(pluscount)

doCount(minuscount,20)

import scount

scount.reset("words")

doCount(scount,40)

#----------------------------------------------------------

class Point:
  x = 0
  y = 0


Point.x

Point.y




p = Point()

p.x

p.y

p.x = 7


q = Point()

q.x

q.x = 10

p.x 



#-----------------------------------------------------------

class Point:
  x = 0
  y = 0
  def move(self,dx,dy):
    self.x = self.x + dx
    self.y = self.y + dy





    




Point.__dict__.keys()

Point.x
Point.y
Point.move




p = Point()

q = Point()


p.move 


p.move(10,100)





#bind instance methods to other names
(p.x,p.y)

f = p.move

for i in range(10):
  f(100,100)

(p.x,p.y)

#-----------------------------------------------------------

class Point:
  x = 0
  y = 0

  def move(self,dx,dy):
    self.x = self.x + dx
    self.y = self.y + dy

  def jump(self,x,y):
    self.x = x
    self.y = y



p = Point()

(p.x,p.y)


p.jump(10,15)

(p.x,p.y)



#"default" methods

class Point:
  count = 0
  x = 0
  y = 0

  def move(self,dx,dy):
    self.x = self.x + dx
    self.y = self.y + dy

  def jump(self,x,y):
    self.x = x
    self.y = y

  def __init__(self,x,y):
    Point.count = Point.count + 1
    self.jump(x,y)
    
  def __repr__(self): 
    return "Point(" + str(self.x) + "," + str(self.y) + ")"
  def __str__(self):
    return "x = " + str(self.x) + " y = " + str(self.y)
    
p = Point(100,100)

p.move(10,20)

p

#-----------------------------------------------------------

class Count:
  def __init__(self,orig):
    self.ctr = orig
    self.__orig__ = orig
  def next(self):
    self.ctr = self.ctr + 1
    return self.ctr
  def reset(self):
    self.__init__(self.__orig__)

c = Count(10)

d = Count(100)

c.next()
c.next()

d.next()

for i in range(10):
  print c.next(),d.next()
  
c.reset()

for i in range(10):
  print c.next(),d.next()

#-----------------------------------------------------------

class PlusCount:
  def __init__(self):
    self.ctr = 0

  def reset(self):
    self.ctr = 0

  def next(self):
    self.ctr = self.ctr + 1
    return self.ctr
  
  def __repr__(self):
    return repr(self.ctr)


#-----------------------------------------------------------

c1 = PlusCount()
c2 = PlusCount()

doCount(c1)
doCount(c2)
doCount(c1)

#-----------------------------------------------------------

class MinusCount:
  def __init__(self):
    self.ctr = 0

  def reset(self):
    self.ctr = 0

  def next(self):
    self.ctr = self.ctr - 1
    return self.ctr

  def __repr__(self):
    return repr(self.ctr)


#-----------------------------------------------------------

c3 = MinusCount()
doCount(c3)
doCount(c2)
doCount(c3)








#-----------------------------------------------------------

class Count:
  def __init__(self,init,nextf):
    self.ctr = init
    self.__orig__ = init
    self.__next__ = nextf
    
  def reset(self):
    self.ctr = self.__orig__
  
  def next(self):
    self.ctr = self.__next__(self.ctr)
    return self.ctr

  def __repr__(self):
    return repr(self.ctr)

#-----------------------------------------------------------

c4 = Count(0, lambda x : x + x)
c5 = Count("foo ", lambda x : x + x)

doCount(c4)
doCount(c5)
