All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Classes | Public Member Functions | Public Attributes | List of all members
SortModuleTimes.TabularAlignmentClass Class Reference

Classes

class  CatchAllLines
 
class  FormatNotSupported
 
class  LineIdentifierClass
 
class  LineNo
 

Public Member Functions

def __init__
 
def ParseFormatSpec
 
def SetRowFormats
 
def SetDefaultFormats
 
def AddData
 
def AddRow
 
def SelectFormat
 
def FormatTable
 
def ToStrings
 
def Print
 

Public Attributes

 tabledata
 
 formats
 

Detailed Description

Formats list of data in a table

Definition at line 691 of file SortModuleTimes.py.

Constructor & Destructor Documentation

def SortModuleTimes.TabularAlignmentClass.__init__ (   self,
  specs = [ None 
)
Each format specification applies to one item in each row.
If no format specification is supplied for an item, the last used format
is applied. By default, that is a plain conversion to string.

Definition at line 693 of file SortModuleTimes.py.

694  def __init__(self, specs = [ None, ]):
695  """
696  Each format specification applies to one item in each row.
697  If no format specification is supplied for an item, the last used format
698  is applied. By default, that is a plain conversion to string.
699  """
700  self.tabledata = []
701  self.formats = {}
if specs: self.SetDefaultFormats(specs)

Member Function Documentation

def SortModuleTimes.TabularAlignmentClass.AddData (   self,
  data 
)

Definition at line 765 of file SortModuleTimes.py.

def AddData(self, data): self.tabledata.extend(data)
def SortModuleTimes.TabularAlignmentClass.AddRow (   self,
  row_data 
)

Definition at line 766 of file SortModuleTimes.py.

767  def AddRow(self, *row_data): self.tabledata.append(row_data)
768 
def SortModuleTimes.TabularAlignmentClass.FormatTable (   self)

Definition at line 783 of file SortModuleTimes.py.

784  def FormatTable(self):
785  # select the formats for all lines
786  AllFormats \
787  = [ self.SelectFormat(iRow) for iRow in xrange(len(self.tabledata)) ]
788 
789  # format all the items
790  ItemLengths = MaxItemLengthsClass()
791  TableContent = []
792  for iRow, rowdata in enumerate(self.tabledata):
793  RowFormats = AllFormats[iRow]
794  LineContent = []
795  LastSpec = None
796  for iItem, itemdata in enumerate(rowdata):
797  try:
798  Spec = RowFormats[iItem]
799  LastSpec = Spec
800  except IndexError: Spec = LastSpec
801 
802  Formatter = Spec['format']
803  if isinstance(Formatter, basestring):
804  ItemContent = Formatter % itemdata
805  elif callable(Formatter):
806  ItemContent = Formatter(itemdata)
807  else:
808  raise RuntimeError("Formatter %r (#%d) not supported."
809  % (Formatter, iItem))
810  # if ... else
811  LineContent.append(ItemContent)
812  # for items
813  ItemLengths.add(LineContent)
814  TableContent.append(LineContent)
815  # for rows
816 
817  # pad the objects
818  for iRow, rowdata in enumerate(TableContent):
819  RowFormats = AllFormats[iRow]
820  Spec = AllFormats[iRow]
821  for iItem, item in enumerate(rowdata):
822  try:
823  Spec = RowFormats[iItem]
824  LastSpec = Spec
825  except IndexError: Spec = LastSpec
826 
827  fieldWidth = ItemLengths[iItem]
828  alignment = Spec.get('align', 'left')
829  if alignment == 'right':
830  alignedItem = RightString(item, fieldWidth)
831  elif alignment == 'justified':
832  alignedItem = JustifyString(item, fieldWidth)
833  elif alignment == 'center':
834  alignedItem = CenterString(item, fieldWidth)
835  else: # if alignment == 'left':
836  alignedItem = LeftString(item, fieldWidth)
837  if Spec.get('truncate', True): alignedItem = alignedItem[:fieldWidth]
838 
839  rowdata[iItem] = alignedItem
840  # for items
841  # for rows
return TableContent
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def SortModuleTimes.TabularAlignmentClass.ParseFormatSpec (   self,
  spec 
)

Definition at line 738 of file SortModuleTimes.py.

739  def ParseFormatSpec(self, spec):
740  SpecData = {}
741  if spec is None: SpecData['format'] = str
742  elif isinstance(spec, basestring): SpecData['format'] = spec
743  elif isinstance(spec, dict):
744  SpecData = spec
745  SpecData.setdefault('format', str)
return SpecData
def SortModuleTimes.TabularAlignmentClass.Print (   self,
  stream = sys.stdout 
)

Definition at line 847 of file SortModuleTimes.py.

848  def Print(self, stream = sys.stdout):
849  print "\n".join(self.ToStrings())
850 
851 # class TabularAlignmentClass
852 
S join(S const &sep, Coll const &s)
Returns a concatenation of strings in s separated by sep.
def SortModuleTimes.TabularAlignmentClass.SelectFormat (   self,
  iLine 
)

Definition at line 769 of file SortModuleTimes.py.

770  def SelectFormat(self, iLine):
771  rowdata = self.tabledata[iLine]
772  success = None
773  bestFormat = None
774  for lineMatcher, format_ in self.formats.items():
775  match_success = lineMatcher(iLine, self.tabledata)
776  if match_success <= success: continue
777  bestFormat = format_
778  success = match_success
779  # for
return bestFormat
def SortModuleTimes.TabularAlignmentClass.SetDefaultFormats (   self,
  specs 
)

Definition at line 762 of file SortModuleTimes.py.

763  def SetDefaultFormats(self, specs):
def SortModuleTimes.TabularAlignmentClass.SetRowFormats (   self,
  rowSelector,
  specs 
)

Definition at line 749 of file SortModuleTimes.py.

750  def SetRowFormats(self, rowSelector, specs):
751  # parse the format specifications
752  formats = []
753  for iSpec, spec in enumerate(specs):
754  try:
755  formats.append(self.ParseFormatSpec(spec))
757  raise RuntimeError("Format specification %r (#%d) not supported."
758  % (str(e), iSpec))
759  # for specifications
self.formats[rowSelector] = formats
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
def SortModuleTimes.TabularAlignmentClass.ToStrings (   self,
  separator = " " 
)

Definition at line 844 of file SortModuleTimes.py.

845  def ToStrings(self, separator = " "):
846  return [ separator.join(RowContent) for RowContent in self.FormatTable() ]

Member Data Documentation

SortModuleTimes.TabularAlignmentClass.formats

Definition at line 700 of file SortModuleTimes.py.

SortModuleTimes.TabularAlignmentClass.tabledata

Definition at line 699 of file SortModuleTimes.py.


The documentation for this class was generated from the following file: