Module vopu
[show private | hide private]
[frames | no frames]

Module vopu

VoPU -- Volker's Python Utilities.

This module contains various functions and classes which are very useful for my daily work with Python.

http://www.profv.de/vopu/


Version:

1.9

Author:

Volker Grabsch <vog@notjusthosting.com>

License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Requires:

Python 2.4 or higher

Classes
InitAttributes Base class for objects whose constructor initializes all attributes.
OrderedByCreation Base class for objects which are ordered by their creation time.
StringStream Stream which writes into a byte string.

Function Summary
  camelcase(ustr, maxlen)
Convert a unicode string into CamelCase.
  escape_latex(s)
Escape a unicode string for LaTeX.
  readlines(obj, encoding)
Return an iterator that steps through the lines of obj.
  split_labeled_uri(labeleduri, default)
Split a labeled URI into its URI and its label.
  UnicodeStream(stream, encoding)
Stream wrapper which automatically encodes and decodes.
  _test()
Run all doc tests of this module.

Variable Summary
dict _latex_special_chars = {u'`': u'{}`', u'#': u'\\#', u'"'...

Function Details

camelcase(ustr, maxlen=None)

Convert a unicode string into CamelCase.

>>> camelcase(u"Abc")
u'Abc'
>>> camelcase(u"abc")
u'Abc'
>>> camelcase(u"ABC")
u'Abc'
>>> camelcase(u"This is a text")
u'ThisIsAText'
>>> camelcase(u"This is a text", None)
u'ThisIsAText'

When maxlen is not None, each word is truncated to the length maxlen.

>>> camelcase(u"This is a text", 4)
u'ThisIsAText'
>>> camelcase(u"This is a text", 3)
u'ThiIsATex'
>>> camelcase(u"This is a text", 2)
u'ThIsATe'
>>> camelcase(u"This is a text", 1)
u'TIAT'
>>> camelcase(u"This is a text", 0)
u''
>>> camelcase(u"")
u''
Parameters:
ustr -

unicode string to convert

maxlen -

maximum length of each word (default: None)

escape_latex(s)

Escape a unicode string for LaTeX.

>>> s = u'\\"{}_&%a$b#\nc"\\'
>>> escape_latex(s)
u"\\textbackslash{}{''}\\{\\}\\_\\&\\%a\\$b\\#\\\\c{''}\\textbackslash{}"
>>> print s
\"{}_&%a$b#
c"\
>>> print escape_latex(s)
\textbackslash{}{''}\{\}\_\&\%a\$b\#\\c{''}\textbackslash{}
Parameters:
s -

unicode object to escape for LaTeX

Warning:

The source string must not contain empty lines such as:
  • u"n..." -- empty first line
  • u"...nn..." -- empty line in between
  • u"...n" -- empty last line

readlines(obj, encoding=u'utf8')

Return an iterator that steps through the lines of obj.

Line endings are preserved. The iterator returns unicode objects.

>>> obj = u"Line1\nLine2\nLine3"
>>> for line in readlines(obj):
...     print repr(line)
u'Line1\n'
u'Line2\n'
u'Line3'
>>> obj = u"Line1\nLine2\nLine3\n"
>>> for line in readlines(obj):
...     print repr(line)
u'Line1\n'
u'Line2\n'
u'Line3\n'
>>> obj = "Line1\nLine2\nLine3\n"
>>> for line in readlines(obj):
...     print repr(line)
u'Line1\n'
u'Line2\n'
u'Line3\n'
>>> import os
>>> stream = os.tmpfile()
>>> stream.write("Line1\nLine2\nLine3\n")
>>> stream.seek(0)
>>> for line in readlines(obj):
...     print repr(line)
u'Line1\n'
u'Line2\n'
u'Line3\n'
Parameters:
obj -

byte string, byte stream or unicode object to read from

encoding -

encoding of obj (default: UTF-8)

split_labeled_uri(labeleduri, default=u'')

Split a labeled URI into its URI and its label.

If the labeled URI doesn't contain a label, return the default label.

>>> split_labeled_uri(u"http://www.google.com/ This is Google.")
(u'http://www.google.com/', u'This is Google.')
>>> split_labeled_uri(u"http://www.google.com/  \t surrounding spaces ")
(u'http://www.google.com/', u'surrounding spaces')
>>> split_labeled_uri(u"http://www.google.com/ given label", u"default label")
(u'http://www.google.com/', u'given label')
>>> split_labeled_uri(u"http://www.google.com/", u"default label")
(u'http://www.google.com/', u'default label')
Parameters:
labeleduri -

unicode string containing the labeled URI

default -

fallback label (default: u"")

UnicodeStream(stream, encoding=u'utf8')

Stream wrapper which automatically encodes and decodes.

Simpler interface to the codecs package.

Return a stream whose methods read(), write(), ... take and return only unicode objects. These are encoded to and decoded from the given stream using the given encoding.

>>> import sys
>>> ustream = UnicodeStream(sys.stdout)
>>> ustream.write(u"abc")
abc
>>> import os
>>> stream = os.tmpfile()
>>> stream.write("abc")
>>> stream.seek(0)
>>> ustream = UnicodeStream(stream)
>>> ustream.read()
u'abc'
Parameters:
stream -

byte stream to wrap

encoding -

encoding of stream (default: UTF-8)

_test()

Run all doc tests of this module.


Variable Details

_latex_special_chars

Type:
dict
Value:
{u'\n': u'\\\\',
 u'"': u"{''}",
 u'#': u'\\#',
 u'$': u'\\$',
 u'%': u'\\%',
 u'&': u'\\&',
 u'<': u'\\textless{}',
 u'>': u'\\textgreater{}',
...                                                                    

Generated by Epydoc 2.1 on Sun Aug 31 15:41:30 2008 http://epydoc.sf.net