

class Ass(object):
	def __init__(self,w=40,h=25, fill=' ',ctx={}):
		self._w=w
		self._h=h
		self._ctx=ctx
		self._fill=fill

	def gloryhole(self,left=0,top=0,w=40,h=25):
		'''
		get a view of the context
		'''
		ctx = dict([
			((x-left,y-top),ch) 
			for ((x,y),ch) in self._ctx.items
			   ])
		return Ass(w,h,ctx)

	def plot(self,x,y,ch):
		'''
		add one char to context
		'''
		self._ctx[(x,y)] = ch

	def blit(self,left,top,posChLst):
		'''
		add a list of character
		'''
		for x,y,ch in posChLst:
			self.plot(left+x,top+y,ch)

	def fart(self):
		'''
		print content on screen
		'''
		print(''.join(['\n' if x == self._w
			       else self._fill if (x,y) not in self._ctx
			       else self._ctx[(x,y)] 
			       for y in range(self._h)
			       for x in range(self._w+1)]))

	def flush(self):
		'''
		clear screen
		'''
		self._ctx = {}
	
def rectum(left,top,w,h,ch):
	'''
	draw a big square rectum
	'''
	return ((x,y,ch)
		for x in range(left,left+w)
		for y in range(top,top+h)
		if x == left
		or x == left+w-1
		or y == top
		or y == top+h-1
	)

def turd(left,top,w,h,ch):
	'''
	draw a big square filled of shit
	'''
	return ((x,y,ch)
		for x in range(left,left+w)
		for y in range(top,top+h)
	)

def seatUp(left,top,length,ch):
	'''
	draw a vertical line
	'''
	return ((left,y,ch)
		for y in range(top,top+length)
	)

def seatDown(left,top,length,ch):
	'''
	draw horizontal line
	'''
	return ((x,top,ch)
		for x in range(left,left+length)
	)

def text(left,top,line):
	'''
	write a line
	'''
	return ((x,top,ch)
		for x,ch in zip(range(left,left+len(line)),line)
	)

def sprite(left,top,lineLst, ignore=' '):
	'''
	create a sprite from an array of line
	ex: [
		".'.",
		":o:",
		"'.'"
	]
	'''
        return((left+x,top+y,ch)
		for (y,line) in enumerate(lineLst)
	        for (x,ch) in enumerate(line)
        	if ch != ignore
	)

def load(filename):
	'''
	load sprite from file
	'''
	sprite = []
	with open(filename) as file:
		sprite.append(file.readline())
	return sprite

if __name__ == '__main__':
	ass = Ass(10,10)
	ass.plot(5,5,'!')
	ass.blit(2,2,rectum(0,0,6,6,'#'))
	ass.blit(1,1,text(0,0,'B==D'))
	ass.blit(5,5,sprite(-1,-1,[
		"[o_o]",
		" \_/ ",
                " d b "
       ]))
	ass.fart()




