Common utilities for BridgeDB.
configureLogging
(cfg)[source]¶Set up Python’s logging subsystem based on the configuration.
Parameters: | cfg (Conf ) – The current configuration, including any in-memory settings. |
---|
levenshteinDistance
(s1, s2, len1=None, len2=None, offset1=0, offset2=0, memo=None)[source]¶Compute the Levenstein Distance between two strings.
The Levenshtein String Distance Algorithm <https://en.wikipedia.org/wiki/Levenshtein_distance> efficiently computes the number of characters which must be changed in s1 to make it identical to s2.
>>> from bridgedb.util import levenshteinDistance
>>> levenshteinDistance('cat', 'cat')
0
>>> levenshteinDistance('cat', 'hat')
1
>>> levenshteinDistance('arma', 'armadillo')
5
Parameters: |
---|
isascii
(s)[source]¶Return True if there are no non-ASCII characters in s, False otherwise.
Note that this function differs from the str.is* methods in that it returns True for the empty string, rather than False.
>>> from bridgedb.util import isascii >>> isascii('') False >>> isascii('foo bar
baz ‘)
True >>> isascii(‘foo bar’) True
param str s: The string to check for non-ASCII characters.
isascii_noncontrol
(s)[source]¶s, False otherwise.
Note that this function differs from the str.is* methods in that it returns True for the empty string, rather than False.
>>> from bridgedb.util import isascii_noncontrol
>>> isascii_noncontrol('')
False
>>> isascii_noncontrol('foo bar
baz ‘)
False >>> isascii_noncontrol(‘foo bar’) True
param str s: The string to check for non-ASCII or control characters.
replaceControlChars
(text, replacement=None, encoding='utf-8')[source]¶Remove ASCII control characters [0-31, 92, 127].
>>> from bridgedb.util import replaceControlChars >>> replaceControlChars('foo
- barbaz
- quux
‘foo bar baz quux’ >>> replaceControlChars(“I wonder if I’m outside the quotes now”) “I wonder if I’m outside the quotes now”
param str text: | Some text to remove ASCII control characters from. |
---|---|
param int replacement: | |
If given, the replacement should be an integer
representing the decimal representation of the byte to replace
occurences of ASCII control characters with. For example, if they
should be replaced with the character 'a' , then 97 should be
used as the replacement, because ord('a') == 97 . |
|
param str encoding: | |
The encoding of the text. | |
rtype: | str |
returns: | The sanitized text. |
registerAdapter
(adapter, adapted, interface)[source]¶Register a Zope interface adapter for global use.
See twisted.python.components.registerAdapter and the Twisted Matrix Labs `howto documentation for components`_.
JustifiedLogFormatter
(logThreads=False, logTrace=False, datefmt='%H:%M:%s')[source]¶Bases: logging.Formatter
A logging formatter which pretty prints thread and calling function information, in addition to the normal timestamp, log level, and log message.
Variables: | width (int) – The width of the column for the calling function information, if the latter is to be included. |
---|
If logTrace is True
, the line number, module name, and
function name where the logger was called will be included in the
message, and the width of this information will always equal width
.
Parameters: |
---|
width
= 30¶format
(record)[source]¶Reformat this log record to neatly print thread and function traces, if configured to do so.
Parameters: | record (logging.LogRecord ) – A record of an event created by calling a logger. |
---|
mixin
[source]¶Bases: object
Subclasses of me can be used as a mixin class by registering another
class, ClassA
, which should be mixed with the mixin
subclass, in
order to provide simple, less error-prone, multiple inheritance models:
>>> from __future__ import print_function
>>> from bridgedb.util import mixin
>>>
>>> class ClassA(object):
... def sayWhich(self): ... print(“ClassA.sayWhich() called.”) ... def doSuperThing(self): ... print(“%s” % super(ClassA, self)) ... def doThing(self): ... print(“ClassA is doing a thing.”) ... >>> class ClassB(ClassA): ... def sayWhich(self): ... print(“ClassB.sayWhich() called.”) ... def doSuperThing(self): ... print(“%s” % super(ClassB, self)) ... def doOtherThing(self): ... print(“ClassB is doing something else.”) ... >>> class ClassM(mixin): ... def sayWhich(self): ... print(“ClassM.sayWhich() called.”) ... >>> ClassM.register(ClassA) >>> >>> class ClassC(ClassM, ClassB): ... def sayWhich(self): ... super(ClassC, self).sayWhich() ... >>> c = ClassC() >>> c.sayWhich() ClassM.sayWhich() called. >>> c.doSuperThing() <super: <class ‘ClassB’>, <ClassC object>> >>> c.doThing() ClassA is doing a thing. >>> c.doOtherThing() ClassB is doing something else.