Package: qcore
qcoreis a library of common utility functions used at Quora. It is used to
abstract out common functionality for other Quora libraries likeasynq.Its component modules are discussed below. See the docstrings in the code
itself for more detail.qcore.assertsWhen 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:>>>fromqcore.assertsimportassert_eq>>>assert5==2*2Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>AssertionError>>>assert_eq(5,2*2)Traceback(mostrecentcalllast):File"<stdin>",line1,in<module>File"qcore/asserts.py",line82,inassert_eqassertexpected==actual,_assert_fail_message(message,expected,actual,'!=',extra)AssertionError:5!=4Similar methods are provided by the standard library’sunittestpackage,
but those are tied to theTestCaseclass instead of being standalone
functions.qcore.cachingThis provides helpers for caching data. Some examples include:fromqcore.cachingimportcached_per_instance,lazy_constant@lazy_constantdefsome_function():# this will only be executed the first time some_function() is called;# afterwards it will be cachedreturnexpensive_computation()classSomeClass:@cached_per_instance()defsome_method(self,a,b):# for any instance of SomeClass, this will only be executed oncereturnexpensive_computation(a,b)qcore.debugThis 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.qcore.decoratorsThis 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.dirtyattribute to decorated functions to dirty their cache:fromqcore.decoratorsimportDecoratorBase,DecoratorBinder,decorateclassCacheDecoratorBinder(DecoratorBinder):defdirty(self,*args):ifself.instanceisNone:returnself.decorator.dirty(*args)else:returnself.decorator.dirty(self.instance,*args)classCacheDecorator(DecoratorBase):binder_cls=CacheDecoratorBinderdef__init__(self,*args):super().__init__(*args)self._cache={}defdirty(self,*args):try:delself._cache[args]exceptKeyError:passdef__call__(self,*args):try:returnself._cache[args]exceptKeyError:value=self.fn(*args)self._cache[args]=valuereturnvaluecached=decorate(CacheDecorator)qcore.enumThis module provides an abstraction for defining enums. You can define an enum
as follows:fromqcore.enumimportEnumclassColor(Enum):red=1green=2blue=3qcore.errorsThis module provides some commonly useful exception classes and helpers for
reraising exceptions from a different place.qcore.eventsThis provides an abstraction for registering events and running callbacks.
Example usage:>>>fromqcore.eventsimportEventHook>>>event=EventHook()>>>defcallback():...print('callback called')...>>>event.subscribe(callback)>>>event.trigger()callbackcalledqcore.helpersThis provides a number of small helper functions.qcore.inspectable_classThis provides a base class that automatically provides hashing, equality
checks, and a readablerepr()result. Example usage:>>>fromqcore.inspectable_classimportInspectableClass>>>classPair(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)Trueqcore.inspectionThis provides functionality similar to the standardinspectmodule. Among
others, it includes theget_original_fnfunction, which extracts the
underlying function from aqcore.decorators-decorated object.qcore.microtimeThis includes helpers for dealing with time, represented as an integer number
of microseconds since the Unix epoch.qcore.testingThis provides helpers to use in unit tests. Among others, it provides anAnythingobject that compares equal to any other Python object.