Utils

restless.utils

class restless.utils.MoreTypesJSONEncoder(skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, encoding='utf-8', default=None)

A JSON encoder that allows for more common Python data types.

In addition to the defaults handled by json, this also supports:

  • datetime.datetime
  • datetime.date
  • datetime.time
  • decimal.Decimal
default(data)
restless.utils.lookup_data(lookup, data)

Given a lookup string, attempts to descend through nested data looking for the value.

Can work with either dictionary-alikes or objects (or any combination of those).

Lookups should be a string. If it is a dotted path, it will be split on . & it will traverse through to find the final value. If not, it will simply attempt to find either a key or attribute of that name & return it.

Example:

>>> data = {
...     'type': 'message',
...     'greeting': {
...         'en': 'hello',
...         'fr': 'bonjour',
...         'es': 'hola',
...     },
...     'person': Person(
...         name='daniel'
...     )
... }
>>> lookup_data('type', data)
'message'
>>> lookup_data('greeting.en', data)
'hello'
>>> lookup_data('person.name', data)
'daniel'

Table Of Contents

Previous topic

Resources

Next topic

restless v1.2.0

This Page