# HG changeset patch
# User Sebastien Labbe <slabqc at gmail.com>
# Date 1351769970 3600
# Node ID 00f84c26354a61c7b0c9b0a7427d5ccc8021f640
# Parent a17e9a2ea2afd8852ca94694c3620f4a65a8dd12
Improve __repr__ and __str__ methods of WordMorphism
diff git a/sage/combinat/words/morphism.py b/sage/combinat/words/morphism.py
a

b

class WordMorphism(SageObject): 
450  450  
451  451  """ 
452  452  return not self.__eq__(other) 
453   
 453  
454  454  def __repr__(self): 
455  455  r""" 
456   Returns the morphism in str (for display). 
 456  Returns the string representation of the morphism. 
457  457  
458  458  EXAMPLES:: 
459  459  
460  460  sage: WordMorphism('a>ab,b>ba') 
461   Morphism from Words over Ordered Alphabet ['a', 'b'] to Words over Ordered Alphabet ['a', 'b'] 
462   sage: d = {0:[0,1],1:[1,0]} 
463   sage: WordMorphism(d) 
464   Morphism from Words over Ordered Alphabet [0, 1] to Words over Ordered Alphabet [0, 1] 
 461  WordMorphism: a>ab, b>ba 
 462  sage: WordMorphism({0:[0,1],1:[1,0]}) 
 463  WordMorphism: 0>01, 1>10 
 464  
 465  TESTS:: 
 466  
 467  sage: s = WordMorphism('a>ab,b>ba') 
 468  sage: repr(s) 
 469  'WordMorphism: a>ab, b>ba' 
465  470  """ 
466   return "Morphism from %s to %s" %(self.domain(),self.codomain()) 
467   
 471  return "WordMorphism: %s" % str(self) 
 472  
468  473  def __str__(self): 
469  474  r""" 
470   Returns the morphism in str (for display). 
471   
 475  Returns the morphism in str. 
 476  
472  477  EXAMPLES:: 
473  478  
474  479  sage: print WordMorphism('a>ab,b>ba') 
475   WordMorphism: a>ab, b>ba 
476   sage: print WordMorphism('b>ba,a>ab') 
477   WordMorphism: a>ab, b>ba 
478   sage: d = {0:[0,1],1:[1,0]} 
479   sage: print WordMorphism(d) 
480   WordMorphism: 0>01, 1>10 
 480  a>ab, b>ba 
 481  sage: print WordMorphism({0:[0,1],1:[1,0]}) 
 482  0>01, 1>10 
 483  
 484  The output is sorted to make it unique:: 
 485  
 486  sage: print WordMorphism('b>ba,a>ab') 
 487  a>ab, b>ba 
 488  
 489  The str method is used for string formatting:: 
 490  
 491  sage: s = WordMorphism('a>ab,b>ba') 
 492  sage: "Here is a map : %s" % s 
 493  'Here is a map : a>ab, b>ba' 
 494  
 495  :: 
 496  
 497  sage: s = WordMorphism({1:[1,2],2:[1]}) 
 498  sage: s.dual_map() 
 499  E_1^*(1>12, 2>1) 
 500  
 501  TESTS:: 
 502  
 503  sage: s = WordMorphism('a>ab,b>ba') 
 504  sage: str(s) 
 505  'a>ab, b>ba' 
481  506  """ 
482   l = [str(lettre) + '>' + image.string_rep() for lettre,image in self._morph.iteritems()] 
 507  L = [str(lettre) + '>' + image.string_rep() for lettre,image in self._morph.iteritems()] 
 508  return ', '.join(sorted(L)) 
483  509  
484   return "WordMorphism: %s" % ', '.join(sorted(l)) 
485   
486  510  def __call__(self, w, order=1, datatype='iter'): 
487  511  r""" 
488  512  Returns the image of ``w`` under self to the given order. 