csbot.util module

class csbot.util.User(raw)[source]

Bases: object

csbot.util.nick(user)[source]

Get nick from user string.

>>> nick('csyorkbot!~csbot@example.com')
'csyorkbot'
csbot.util.username(user)[source]

Get username from user string.

>>> username('csyorkbot!~csbot@example.com')
'csbot'
csbot.util.host(user)[source]

Get hostname from user string.

>>> host('csyorkbot!~csbot@example.com')
'example.com'
csbot.util.is_channel(channel)[source]

Check if channel is a channel or private chat.

>>> is_channel('#cs-york')
True
>>> is_channel('csyorkbot')
False
csbot.util.parse_arguments(raw)[source]

Parse raw into a list of arguments using shlex.

The shlex lexer is customised to be more appropriate for grouping natural language arguments by only treating " as a quote character. This allows ' to be used naturally. A ValueError will be raised if the string couldn’t be parsed.

>>> parse_arguments("a test string")
['a', 'test', 'string']
>>> parse_arguments("apostrophes aren't a problem")
['apostrophes', "aren't", 'a', 'problem']
>>> parse_arguments('"string grouping" is useful')
['string grouping', 'is', 'useful']
>>> parse_arguments('just remember to "match your quotes')
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ValueError: No closing quotation
csbot.util.simple_http_get(url, stream=False)[source]

A deliberately dumb wrapper around requests.get().

This should be used for the vast majority of HTTP GET requests. It turns off SSL certificate verification and sets a non-default User-Agent, thereby succeeding at most “just get the content” requests.

stream controls the “streaming mode” of the HTTP client, i.e. deferring the acquisition of the response body. Use this if you need to impose a maximum size or process a large response. The entire content must be consumed or ``response.close()`` must be called.

csbot.util.pairwise(iterable)[source]

Pairs elements of an iterable together, e.g. s -> (s0,s1), (s1,s2), (s2, s3), ...

csbot.util.cap_string(s, l)[source]

If a string is longer than a particular length, it gets truncated and has ‘...’ added to the end.

csbot.util.ordinal(value)[source]

Converts zero or a postive integer (or their string representations) to an ordinal value.

http://code.activestate.com/recipes/576888-format-a-number-as-an-ordinal/

>>> for i in range(1,13):
...     ordinal(i)
...
u'1st'
u'2nd'
u'3rd'
u'4th'
u'5th'
u'6th'
u'7th'
u'8th'
u'9th'
u'10th'
u'11th'
u'12th'
>>> for i in (100, '111', '112',1011):
...     ordinal(i)
...
u'100th'
u'111th'
u'112th'
u'1011th'
csbot.util.pluralize(n, singular, plural)[source]
csbot.util.is_ascii(s)[source]

Returns true if all characters in a string can be represented in ASCII.

class csbot.util.NamedObject(name)[source]

Bases: object

Make objects that have specific __repr__() text.

This is mostly useful for singleton objects that you want to give a useful description for auto-generated documentation purposes.

class csbot.util.StructMeta[source]

Bases: type

A metaclass for Struct to turn class attributes into fields.

class csbot.util.Struct(*args, **kwargs)[source]

Bases: object

A mutable alternative to collections.namedtuple().

To use this class, create a subclass of it. Any non-callable, non-“hidden” class attributes in the subclass will become struct fields. Setting of attribute values is limited to attributes recognised as fields. The class attribute value is effectively the field’s default value.

A struct constructor allows both positional arguments (based on field order) and keyword arguments (based on field name). If a field’s default value is REQUIRED, then an exception will be raised unless its value was set by the constructor.

Examples:

>>> class Foo(Struct):
...     a = Struct.REQUIRED
...     b = 12
...     c = None
...
>>> Foo()
Traceback (most recent call last):
    ...
ValueError: value required for attribute: a
>>> Foo(123)
Foo(a=123, b=12, c=None)
>>> Foo(123, c='Hello, world')
Foo(a=123, b=12, c='Hello, world')
>>> Foo(bar=False)
Traceback (most recent call last):
    ...
AttributeError: struct field does not exist: bar
>>> x = Foo(123)
>>> x.b
12
>>> x.b = 21
>>> x
Foo(a=123, b=21, c=None)
>>> x.bar = False
Traceback (most recent call last):
    ...
AttributeError: struct field does not exist: bar
REQUIRED = Struct.REQUIRED

Singleton object to signify an attribute that must be set