Skip to main content

Quora's core utility library

Project description

qcore is a library of common utility functions used at Quora. It is used to abstract out common functionality for other Quora libraries like asynq.

Its component modules are discussed below. See the docstrings in the code itself for more detail.


When a normal Python assert fails, it only indicates that there was a failure, not what the bad values were that caused the assert to fail. This module provides rich assertion helpers that automatically produce better error messages. For example:

>>> from qcore.asserts import assert_eq
>>> assert 5 == 2 * 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
>>> assert_eq(5, 2 * 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "qcore/", line 82, in assert_eq
    assert expected == actual, _assert_fail_message(message, expected, actual, '!=', extra)
AssertionError: 5 != 4

Similar methods are provided by the standard library’s unittest package, but those are tied to the TestCase class instead of being standalone functions.


This provides helpers for caching data. Some examples include:

from qcore.caching import cached_per_instance, lazy_constant

def some_function():
    # this will only be executed the first time some_function() is called;
    # afterwards it will be cached
    return expensive_computation()

class SomeClass(object):
    def some_method(self, a, b):
        # for any instance of SomeClass, this will only be executed once
        return expensive_computation(a, b)


This module provides some helpers useful for debugging Python. Among others, it includes the @qcore.debug.trace() decorator, which can be used to trace every time a function is called.


This module provides an abstraction for class-based decorators that supports transparently decorating functions, methods, classmethods, and staticmethods while also providing the option to add additional custom attributes. For example, it could be used to provide a caching decorator that adds a .dirty attribute to decorated functions to dirty their cache:

from qcore.decorators import DecoratorBase, DecoratorBinder, decorate

class CacheDecoratorBinder(DecoratorBinder):
    def dirty(self, *args):
        if self.instance is None:
            return self.decorator.dirty(*args)
            return self.decorator.dirty(self.instance, *args)

class CacheDecorator(DecoratorBase):
    binder_cls = CacheDecoratorBinder

    def __init__(self, *args):
        self._cache = {}

    def dirty(self, *args):
            del self._cache[args]
        except KeyError:

    def __call__(self, *args):
            return self._cache[args]
        except KeyError:
            value = self.fn(*args)
            self._cache[args] = value
            return value

cached = decorate(CacheDecorator)


This module provides an abstraction for defining enums. You can define an enum as follows:

from qcore.enum import Enum

class Color(Enum):
    red = 1
    green = 2
    blue = 3


This module provides some commonly useful exception classes and helpers for reraising exceptions from a different place.

This provides an abstraction for registering events and running callbacks. Example usage:

>>> from import EventHook
>>> event = EventHook()
>>> def callback():
...     print('callback called')
>>> event.subscribe(callback)
>>> event.trigger()
callback called


This provides a number of small helper functions.


This provides a base class that automatically provides hashing, equality checks, and a readable repr() result. Example usage:

>>> from qcore.inspectable_class import InspectableClass
>>> class Pair(InspectableClass):
...     def __init__(self, a, b):
...         self.a = a
...         self.b = b
>>> Pair(1, 2)
Pair(a=1, b=2)
>>> Pair(1, 2) == Pair(1, 2)


This provides functionality similar to the standard inspect module. Among others, it includes the get_original_fn function, which extracts the underlying function from a qcore.decorators-decorated object.


This includes helpers for dealing with time, represented as an integer number of microseconds since the Unix epoch.


This provides helpers to use in unit tests. Among others, it provides an Anything object that compares equal to any other Python object.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qcore-1.8.0.tar.gz (53.1 kB view hashes)

Uploaded source

Built Distributions

qcore-1.8.0-pp37-pypy37_pp73-win_amd64.whl (335.5 kB view hashes)

Uploaded pp37

qcore-1.8.0-cp39-cp39-win_amd64.whl (389.6 kB view hashes)

Uploaded cp39

qcore-1.8.0-cp39-cp39-win32.whl (347.0 kB view hashes)

Uploaded cp39

qcore-1.8.0-cp39-cp39-macosx_10_9_x86_64.whl (474.2 kB view hashes)

Uploaded cp39

qcore-1.8.0-cp38-cp38-win_amd64.whl (390.0 kB view hashes)

Uploaded cp38

qcore-1.8.0-cp38-cp38-win32.whl (347.5 kB view hashes)

Uploaded cp38

qcore-1.8.0-cp38-cp38-macosx_10_9_x86_64.whl (466.6 kB view hashes)

Uploaded cp38

qcore-1.8.0-cp37-cp37m-win_amd64.whl (380.0 kB view hashes)

Uploaded cp37

qcore-1.8.0-cp37-cp37m-win32.whl (341.1 kB view hashes)

Uploaded cp37

qcore-1.8.0-cp37-cp37m-macosx_10_9_x86_64.whl (458.1 kB view hashes)

Uploaded cp37

qcore-1.8.0-cp36-cp36m-win_amd64.whl (417.6 kB view hashes)

Uploaded cp36

qcore-1.8.0-cp36-cp36m-win32.whl (363.7 kB view hashes)

Uploaded cp36

qcore-1.8.0-cp36-cp36m-macosx_10_9_x86_64.whl (460.4 kB view hashes)

Uploaded cp36

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page