Package pology :: Module colors :: Class ColorString

Class ColorString

object --+        
         |        
basestring --+    
             |    
       unicode --+
                 |
                ColorString

Class for strings with color markup.

This class provides automatic resolution of color XML markup in strings for various output formats. It automatically escapes any raw strings combined with it (e.g. when using the % or + operators) and returns objects of its own type from methods (e.g. from split() or strip()). Otherwise it should behave like a normal string.

Note that usage of this class is expensive, given that arguments are constantly checked and strings escaped. It should be used only for user-visible output, i.e. where human reaction time is the limiting factor.

Instance Methods
 
__add__(self, other)
x+y
 
__radd__(self, other)
 
__mod__(self, args)
x%y
 
__repr__(self)
repr(x)
unicode
join(self, strings)
Return a string which is the concatenation of the strings in the iterable.
string
resolve(self, ctype=None, dest=None)
Resolve color markup according to given type and destination.
string, int
visual_segment(self, pos)
Get visual representation of raw segment starting from position.
 
__getitem__(self, *args, **kwargs)
x[y]
 
__getslice__(self, *args, **kwargs)
x[i:j]
 
__mul__(self, *args, **kwargs)
x*n
 
__rmul__(self, *args, **kwargs)
n*x
unicode
capitalize(self, *args, **kwargs)
Return a capitalized version of S, i.e.
unicode
center(self, *args, **kwargs)
Return S centered in a Unicode string of length width.
unicode
expandtabs(self, *args, **kwargs)
Return a copy of S where all tab characters are expanded using spaces.
int
ljust(self, *args, **kwargs)
Return S left-justified in a Unicode string of length width.
unicode
lower(self, *args, **kwargs)
Return a copy of the string S converted to lowercase.
unicode
lstrip(self, *args, **kwargs)
Return a copy of the string S with leading whitespace removed.
unicode
replace(self, *args, **kwargs)
Return a copy of S with all occurrences of substring old replaced by new.
unicode
rjust(self, *args, **kwargs)
Return S right-justified in a Unicode string of length width.
list of strings
rsplit(self, *args, **kwargs)
Return a list of the words in S, using sep as the delimiter string, starting at the end of the string and working to the front.
unicode
rstrip(self, *args, **kwargs)
Return a copy of the string S with trailing whitespace removed.
list of strings
split(self, *args, **kwargs)
Return a list of the words in S, using sep as the delimiter string.
unicode
strip(self, *args, **kwargs)
Return a copy of the string S with leading and trailing whitespace removed.
unicode
swapcase(self, *args, **kwargs)
Return a copy of S with uppercase characters converted to lowercase and vice versa.
unicode
title(self, *args, **kwargs)
Return a titlecased version of S, i.e.
unicode
translate(self, *args, **kwargs)
Return a copy of the string S, where all characters have been mapped through the given translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None.
unicode
upper(self, *args, **kwargs)
Return a copy of S converted to uppercase.
unicode
zfill(self, *args, **kwargs)
Pad a numeric string S with zeros on the left, to fill a field of the specified width.

Inherited from unicode: __contains__, __eq__, __format__, __ge__, __getattribute__, __getnewargs__, __gt__, __hash__, __le__, __len__, __lt__, __ne__, __new__, __rmod__, __sizeof__, __str__, count, decode, encode, endswith, find, format, index, isalnum, isalpha, isdecimal, isdigit, islower, isnumeric, isspace, istitle, isupper, partition, rfind, rindex, rpartition, splitlines, startswith

Inherited from object: __delattr__, __init__, __reduce__, __reduce_ex__, __setattr__, __subclasshook__

Properties

Inherited from object: __class__

Method Details

__add__(self, other)
(Addition operator)

 

x+y

Overrides: unicode.__add__
(inherited documentation)

__mod__(self, args)

 

x%y

Overrides: unicode.__mod__
(inherited documentation)

__repr__(self)
(Representation operator)

 

repr(x)

Overrides: object.__repr__
(inherited documentation)

join(self, strings)

 

Return a string which is the concatenation of the strings in the iterable. The separator between elements is S.

Returns: unicode
Overrides: unicode.join
(inherited documentation)

resolve(self, ctype=None, dest=None)

 

Resolve color markup according to given type and destination.

Currently available coloring types (values of ctype parameter):

  • "none": no coloring
  • "term": ANSI color escape sequences (for terminal output)
  • "html": HTML markup (for integration into HTML pages)

If ctype is None, it is taken from global coloring options.

Some coloring types may be applied conditionally, based on whether the intended output destination is a file or terminal. If this is desired, the file descriptor of the destination can be given by the dest parameter.

Parameters:
  • ctype (string) - type of coloring
  • dest (file) - destination file descriptor
Returns: string
plain string with resolved markup

visual_segment(self, pos)

 

Get visual representation of raw segment starting from position.

This function checks whether the segment of the string starting at given position has the raw or another visual value, accounting for markup. If the visual and raw segments differ, the visual representation and length of the raw segment are returned. Otherwise, empty string and zero length are returned.

Parameters:
  • pos (int) - position where to check for visual segment
Returns: string, int
visual segment and length of underlying raw segment

__getitem__(self, *args, **kwargs)
(Indexing operator)

 

x[y]

Overrides: unicode.__getitem__
(inherited documentation)

__getslice__(self, *args, **kwargs)
(Slicling operator)

 

x[i:j]

Use of negative indices is not supported.

Overrides: unicode.__getslice__
(inherited documentation)

__mul__(self, *args, **kwargs)

 

x*n

Overrides: unicode.__mul__
(inherited documentation)

__rmul__(self, *args, **kwargs)

 

n*x

Overrides: unicode.__rmul__
(inherited documentation)

capitalize(self, *args, **kwargs)

 

Return a capitalized version of S, i.e. make the first character have upper case and the rest lower case.

Returns: unicode
Overrides: unicode.capitalize
(inherited documentation)

center(self, *args, **kwargs)

 

Return S centered in a Unicode string of length width. Padding is done using the specified fill character (default is a space)

Returns: unicode
Overrides: unicode.center
(inherited documentation)

expandtabs(self, *args, **kwargs)

 

Return a copy of S where all tab characters are expanded using spaces. If tabsize is not given, a tab size of 8 characters is assumed.

Returns: unicode
Overrides: unicode.expandtabs
(inherited documentation)

ljust(self, *args, **kwargs)

 

Return S left-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space).

Returns: int
Overrides: unicode.ljust
(inherited documentation)

lower(self, *args, **kwargs)

 

Return a copy of the string S converted to lowercase.

Returns: unicode
Overrides: unicode.lower
(inherited documentation)

lstrip(self, *args, **kwargs)

 

Return a copy of the string S with leading whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is a str, it will be converted to unicode before stripping

Returns: unicode
Overrides: unicode.lstrip
(inherited documentation)

replace(self, *args, **kwargs)

 

Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Returns: unicode
Overrides: unicode.replace
(inherited documentation)

rjust(self, *args, **kwargs)

 

Return S right-justified in a Unicode string of length width. Padding is done using the specified fill character (default is a space).

Returns: unicode
Overrides: unicode.rjust
(inherited documentation)

rsplit(self, *args, **kwargs)

 

Return a list of the words in S, using sep as the delimiter string, starting at the end of the string and working to the front. If maxsplit is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator.

Returns: list of strings
Overrides: unicode.rsplit
(inherited documentation)

rstrip(self, *args, **kwargs)

 

Return a copy of the string S with trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is a str, it will be converted to unicode before stripping

Returns: unicode
Overrides: unicode.rstrip
(inherited documentation)

split(self, *args, **kwargs)

 

Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

Returns: list of strings
Overrides: unicode.split
(inherited documentation)

strip(self, *args, **kwargs)

 

Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is a str, it will be converted to unicode before stripping

Returns: unicode
Overrides: unicode.strip
(inherited documentation)

swapcase(self, *args, **kwargs)

 

Return a copy of S with uppercase characters converted to lowercase and vice versa.

Returns: unicode
Overrides: unicode.swapcase
(inherited documentation)

title(self, *args, **kwargs)

 

Return a titlecased version of S, i.e. words start with title case characters, all remaining cased characters have lower case.

Returns: unicode
Overrides: unicode.title
(inherited documentation)

translate(self, *args, **kwargs)

 

Return a copy of the string S, where all characters have been mapped through the given translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None. Unmapped characters are left untouched. Characters mapped to None are deleted.

Returns: unicode
Overrides: unicode.translate
(inherited documentation)

upper(self, *args, **kwargs)

 

Return a copy of S converted to uppercase.

Returns: unicode
Overrides: unicode.upper
(inherited documentation)

zfill(self, *args, **kwargs)

 

Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated.

Returns: unicode
Overrides: unicode.zfill
(inherited documentation)