coapy.util

Utility classes and functions used within CoAPy.

copyright:Copyright 2013, Peter A. Bigot
license:Apache-2.0
class coapy.util.ReadOnlyMeta[source]

Metaclass for supporting read-only values in classes.

When used as a metaclass, this inserts an intermediary type that prevents assignment to certain attributes at both the instance and class levels. Any attribute in the class that is initialized in the class body with a value of type ClassReadOnly is made read-only.

See example at ClassReadOnly.

class coapy.util.ClassReadOnly(value)[source]

A marker to indicate an attribute of a class should be read-only within the class as well as instances of the class.

Effective only if the metaclass is (or is derived from) ReadOnlyMeta.

Example:

class C(Object):
    __metaclass__ = ReadOnlyMeta

    Zero = ClassReadOnly(0)

instance = C()
assert 0 == C.Zero
assert 0 == instance.Zero

# This will raise an exception:
C.Zero = 4
# As will this:
instance.Zero = 4
class coapy.util.TimeDueOrdinal(*args, **kw)[source]

Base class for elements that are sorted by time.

The intent is that information related to an activity that should occur at or after a particular time be held in a subclass of TimeDueOrdinal. The priority queue of upcoming activity is implemented using a sorted list, as instances of (subclasses of) TimeDueOrdinal are ordered by increasing value of time_due using the features of bisect. Insertion, removal, and repositioning of elements in the priority queue may be accomplished using queue_insert(), queue_remove(), and queue_reposition().

time_due as a keyword parameter initializes time_due and is removed from kw. Any positional parameters and remaining keyword parameters are passed to the next superclass.

queue_insert(queue)[source]

Insert this entry into queue.

static queue_ready_prefix(queue, now=None)[source]

Return the elements of queue that are due.

queue is a sorted list of TimeDueOrdinal instances. now is the timestamp, and defaults to coapy.clock(). Elements are due when time_due <= now.

queue_remove(queue)[source]

Remove this entry from queue.

queue_reposition(queue)[source]

Reposition this entry within queue.

self must already be in the queue; only its position changes (if necessary).

time_due = None

The time at which the subclass instance becomes relevant.

This is a value in the ordinal space defined by coapy.clock().

coapy.util.to_display_text(data)[source]

Return data as human-readable text.

This is intended for diagnostic messages for values like tokens and payloads that are sometimes text, and sometimes raw data. If data is bytes but all its characters are printable return it as text, otherwise return it as hex-encoded data (wrapped in square brackets to distinguish the encoding, e.g.: [01020304] for b'\x01\x02\x03\x04').

Non-bytes data is simply converted to Unicode and returned in that format. (If data is already text, even if it’s Unicode, we assume it’s displayable. If it isn’t, select a better terminal configuration.)

coapy.util.to_net_unicode(text)[source]

Convert text to Net-Unicode (RFC 5198) data.

This normalizes text to ensure all characters are their own canonical equivalent in the NFC form (section 3 of RFC 5198). The result is encoded in UTF-8 and returned as data.

The operation currently does not handle newline normalization (section 2 item 2), since its use in CoAP is currently limited to values of options with format coapy.option.format_string and diagnostic payloads.

coapy.util.url_quote(text, safe=u'/')[source]

Perform URL percent encoding on text.

If text is Unicode, it is first converted to Net-Unicode. text may also be data.

Unsafe characters are percent-escaped, and the result is returned as text containing only ASCII characters.

safe is as in urllib.parse.

Encapsulated because in Python 3 urllib.parse.quote works directly on Unicode strings, while in Python 2 the corresponding urllib.quote() does not tolerate Unicode characters and does not like safe to be a Unicode string as it is since we use unicode_literals).

coapy.util.url_unquote(quoted)[source]

Perform URL percent decoding on quoted.

Encapsulated because in Python 3 urllib.parse.unquote works directly on Unicode strings, while in Python 2 the corresponding urllib.unquote() does not tolerate Unicode characters.

coapy.util.format_time(tval=None, format=u'iso')[source]

Convert a date/time value to a standard representation and validity duration.

The return value (rep, vsec) provides the representation of tval using style format. Representation rep is expected to be unchanged for vsec seconds after the represented time.

tval is a datetime.datetime, time.struct_time, or POSIX ordinal as from time.time(). It is interpreted as being a universal time (i.e., conversions do not account for time zone).

format is one of the following:

Format rep vsec Description
iso 2013-10-11T10:46:23 0 ISO 8601 combined date and time
ord 2013-284 47617 ISO 8601 ordinal date
pgd 735152 47617 Proleptic Gregorian Ordinal Day
jd 2456576.94888 0 Julian Date
mjd 56576.4488773 0 Modified Julian Date
tjd 16576.4488773 0 Truncated Julian Date
jdn 2456576 4417 Julian Day Number
doy 284 47617 Day-of-year
dow 5 47617 Day-of-week (ISO: Mon=1 Sun=7)
mod 646 37 Minute-of-day
posix 1381488383 0 Seconds since POSIX epoch 1970-01-01T00:00:00

Previous topic

coapy.endpoint

Next topic

coapy.httputil

This Page