schrodinger.infra.util module¶
General infrastructure level utilities.
Copyright Schrodinger, LLC. All rights reserved.
-
class
schrodinger.infra.util.
cached_property
(func)¶ Bases:
object
Decorator that converts a method with a single self argument into a property cached on the instance.
Use del to delete the currently cached value and force a recalculation on the next access. See the tests for examples.
This class is based on code that is Copyright (c) Django Software Foundation
-
__init__
(func)¶ Initialize self. See help(type(self)) for accurate signature.
-
__get__
(instance, type=None)¶
-
__class__
¶ alias of
builtins.type
-
__delattr__
¶ Implement delattr(self, name).
-
__dict__
= mappingproxy({'__module__': 'schrodinger.infra.util', '__doc__': '\n Decorator that converts a method with a single self argument into a\n property cached on the instance.\n\n Use del to delete the currently cached value and force a recalculation on\n the next access. See the tests for examples.\n\n This class is based on code that is Copyright (c) Django Software Foundation\n ', '__init__': <function cached_property.__init__>, '__get__': <function cached_property.__get__>, '__dict__': <attribute '__dict__' of 'cached_property' objects>, '__weakref__': <attribute '__weakref__' of 'cached_property' objects>})¶
-
__dir__
() → list¶ default dir() implementation
-
__eq__
¶ Return self==value.
-
__format__
()¶ default object formatter
-
__ge__
¶ Return self>=value.
-
__getattribute__
¶ Return getattr(self, name).
-
__gt__
¶ Return self>value.
-
__hash__
¶ Return hash(self).
-
__init_subclass__
()¶ This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
-
__le__
¶ Return self<=value.
-
__lt__
¶ Return self<value.
-
__module__
= 'schrodinger.infra.util'¶
-
__ne__
¶ Return self!=value.
-
__new__
()¶ Create and return a new object. See help(type) for accurate signature.
-
__reduce__
()¶ helper for pickle
-
__reduce_ex__
()¶ helper for pickle
-
__repr__
¶ Return repr(self).
-
__setattr__
¶ Implement setattr(self, name, value).
-
__sizeof__
() → int¶ size of object in memory, in bytes
-
__str__
¶ Return str(self).
-
__subclasshook__
()¶ Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
schrodinger.infra.util.
CreateWhenNeeded
(function, name=None, doc=None)¶ Bases:
object
This class can be used like property() (i.e., it is a descriptor; see section 3.3.2.2 in the python reference). It will hold off on creating the object until it is needed, but once it has been created it will return the object directly.
It’s best used for attributes that are expensive to calculate (as measured by profiling, of course) and not always used.
-
__init__
(function, name=None, doc=None)¶ Create the descriptor.
-
__get__
(obj, cls)¶ Calculate the value from the function, then set the attribute of the object to this result and return it.
-
__class__
¶ alias of
builtins.type
-
__delattr__
¶ Implement delattr(self, name).
-
__dict__
= mappingproxy({'__module__': 'schrodinger.infra.util', '__doc__': "\n This class can be used like property() (i.e., it is a descriptor; see\n section 3.3.2.2 in the python reference). It will hold off on creating the\n object until it is needed, but once it has been created it will return the\n object directly.\n\n It's best used for attributes that are expensive to calculate (as\n measured by profiling, of course) and not always used.\n\n ", '__init__': <function CreateWhenNeeded.__init__>, '__get__': <function CreateWhenNeeded.__get__>, '__dict__': <attribute '__dict__' of 'CreateWhenNeeded' objects>, '__weakref__': <attribute '__weakref__' of 'CreateWhenNeeded' objects>})¶
-
__dir__
() → list¶ default dir() implementation
-
__eq__
¶ Return self==value.
-
__format__
()¶ default object formatter
-
__ge__
¶ Return self>=value.
-
__getattribute__
¶ Return getattr(self, name).
-
__gt__
¶ Return self>value.
-
__hash__
¶ Return hash(self).
-
__init_subclass__
()¶ This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
-
__le__
¶ Return self<=value.
-
__lt__
¶ Return self<value.
-
__module__
= 'schrodinger.infra.util'¶
-
__ne__
¶ Return self!=value.
-
__new__
()¶ Create and return a new object. See help(type) for accurate signature.
-
__reduce__
()¶ helper for pickle
-
__reduce_ex__
()¶ helper for pickle
-
__repr__
¶ Return repr(self).
-
__setattr__
¶ Implement setattr(self, name, value).
-
__sizeof__
() → int¶ size of object in memory, in bytes
-
__str__
¶ Return str(self).
-
__subclasshook__
()¶ Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
-
__weakref__
¶ list of weak references to the object (if defined)
-
-
class
schrodinger.infra.util.
OneIndexedList
¶ Bases:
list
A list that starts at one instead of zero
-
__getitem__
(index)¶ x.__getitem__(y) <==> x[y]
-
__setitem__
(index, value)¶ Set self[key] to value.
-
__delitem__
(index)¶ Delete self[key].
-
__getslice__
(start, stop)¶
-
__setslice__
(start, stop, value)¶
-
__delslice__
(start, stop)¶
-
index
(value[, start[, stop]]) → integer -- return first index of value.¶ Raises ValueError if the value is not present.
-
insert
(i, x)¶ L.insert(index, object) – insert object before index
-
pop
([index]) → item -- remove and return item at index (default last).¶ Raises IndexError if list is empty or index is out of range.
-
__add__
(other)¶ Return self+value.
-
__mul__
(other)¶ Return self*value.n
-
__iadd__
(other)¶ Implement self+=value.
-
__imul__
(other)¶ Implement self*=value.
-
__class__
¶ alias of
builtins.type
-
__contains__
¶ Return key in self.
-
__delattr__
¶ Implement delattr(self, name).
-
__dict__
= mappingproxy({'__module__': 'schrodinger.infra.util', '__doc__': '\n A list that starts at one instead of zero\n ', '__getitem__': <function OneIndexedList.__getitem__>, '__setitem__': <function OneIndexedList.__setitem__>, '__delitem__': <function OneIndexedList.__delitem__>, '__getslice__': <function OneIndexedList.__getslice__>, '__setslice__': <function OneIndexedList.__setslice__>, '__delslice__': <function OneIndexedList.__delslice__>, 'index': <function OneIndexedList.index>, 'insert': <function OneIndexedList.insert>, 'pop': <function OneIndexedList.pop>, '_convertIndex': <function OneIndexedList._convertIndex>, '_convertScalarIndex': <function OneIndexedList._convertScalarIndex>, '__add__': <function OneIndexedList.__add__>, '__mul__': <function OneIndexedList.__mul__>, '__iadd__': <function OneIndexedList.__iadd__>, '__imul__': <function OneIndexedList.__imul__>, '__dict__': <attribute '__dict__' of 'OneIndexedList' objects>, '__weakref__': <attribute '__weakref__' of 'OneIndexedList' objects>})¶
-
__dir__
() → list¶ default dir() implementation
-
__eq__
¶ Return self==value.
-
__format__
()¶ default object formatter
-
__ge__
¶ Return self>=value.
-
__getattribute__
¶ Return getattr(self, name).
-
__gt__
¶ Return self>value.
-
__hash__
= None¶
-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
__init_subclass__
()¶ This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
-
__iter__
¶ Implement iter(self).
-
__le__
¶ Return self<=value.
-
__len__
¶ Return len(self).
-
__lt__
¶ Return self<value.
-
__module__
= 'schrodinger.infra.util'¶
-
__ne__
¶ Return self!=value.
-
__new__
()¶ Create and return a new object. See help(type) for accurate signature.
-
__reduce__
()¶ helper for pickle
-
__reduce_ex__
()¶ helper for pickle
-
__repr__
¶ Return repr(self).
-
__reversed__
()¶ L.__reversed__() – return a reverse iterator over the list
-
__rmul__
¶ Return self*value.
-
__setattr__
¶ Implement setattr(self, name, value).
-
__sizeof__
()¶ L.__sizeof__() – size of L in memory, in bytes
-
__str__
¶ Return str(self).
-
__subclasshook__
()¶ Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
-
__weakref__
¶ list of weak references to the object (if defined)
-
append
(object) → None -- append object to end¶
-
clear
() → None -- remove all items from L¶
-
copy
() → list -- a shallow copy of L¶
-
count
(value) → integer -- return number of occurrences of value¶
-
extend
(iterable) → None -- extend list by appending elements from the iterable¶
-
remove
(value) → None -- remove first occurrence of value.¶ Raises ValueError if the value is not present.
-
reverse
()¶ L.reverse() – reverse IN PLACE
-
sort
(key=None, reverse=False) → None -- stable sort *IN PLACE*¶
-
-
schrodinger.infra.util.
flag_context_manager
(attr_name, set_to=True)¶ Create a context manager that sets the specified class attribute to the given value and then restores the original value of the attribute when leaving the context.
Example:
- class Foo(object):
_includingStructure = util.flag_context_manager(“_including_structure”)
- def __init__(self):
- self._including_structure = False
- def includeStructure(proj_row):
- with self._includingStructure():
- proj_row.in_workspace = project.IN_WORKSPACE
See: Parameters: - attr_name (str) – The attribute to set to
set_to
- set_to (object) – The value to set
attr_name
to
-
schrodinger.infra.util.
skip_if
(attr_name, if_set_to=True)¶ Create a decorator that converts the decorated method to a no-op if class attribute
attr_name
equalsis_set_to
.Example:
skip_if_including_structure = util.skip_if("_including_structure") class Foo(object): _includingStructure = util.flag_context_manager("_including_structure") def __init__(self): self._including_structure = False def includeStructure(proj_row): with self._includingStructure(): proj_row.in_workspace = project.IN_WORKSPACE @skip_if_including_structure def skipped_method(self): print ("This method would have been skipped if we were the process " "of including a structure")
See: Parameters: - attr_name (str) – The attribute name to check
- if_set_to (object) – The decorated method will be skipped if
attr_name
equals this value.
-
schrodinger.infra.util.
enum_speedup
(enum_cls)¶ Add all enum members to a new class to speed up access.
Attribute access in enum classes is incredibly slow (see https://bugs.python.org/issue23486). Previously, it accounted for roughly 7.5% of the runtime of scrolling in the HPT. Use of this function reduced that time to nearly zero.
Parameters: enum_cls (enum.Enum) – The enum class to wrap. Returns: A new class that allows for faster access of the enum members. Return type: object Note: Enums have been significantly sped up in Python 3.5, but that speed up hasn’t yet been backported to the Python 2 enum backport. See the Python bug linked above. Note: Declaring a Qt signal with an argument type of a sped-up enum class will lead to a TypeError when you try to emit an enum member (since Qt doesn’t pay attention to
__instancecheck__
). Useobject
in the signal declaration instead. In other words, usemySignal = QtCore.pyqtSignal(object)
instead of
mySignal = QtCore.pyqtSignal(MySpedUpEnum)
-
class
schrodinger.infra.util.
WeakRefAttribute
¶ Bases:
object
A descriptor for an instance attribute that should be stored as a weakref. Unlike weakref.proxy, this descriptor allows the attribute to be hashed.
Note that the weakref is stored on the instance using the same name as the descriptor (which is stored on the class). Since this descriptor implements __set__, it will always take precedence over the value stored on the instance.
-
__set_name__
(owner, name)¶
-
__get__
(obj, objtype)¶
-
__set__
(obj, val)¶
-
__class__
¶ alias of
builtins.type
-
__delattr__
¶ Implement delattr(self, name).
-
__dict__
= mappingproxy({'__module__': 'schrodinger.infra.util', '__doc__': '\n A descriptor for an instance attribute that should be stored as a weakref.\n Unlike weakref.proxy, this descriptor allows the attribute to be hashed.\n\n Note that the weakref is stored on the instance using the same name as the\n descriptor (which is stored on the class). Since this descriptor implements\n __set__, it will always take precedence over the value stored on the\n instance.\n ', '__set_name__': <function WeakRefAttribute.__set_name__>, '__get__': <function WeakRefAttribute.__get__>, '__set__': <function WeakRefAttribute.__set__>, '__dict__': <attribute '__dict__' of 'WeakRefAttribute' objects>, '__weakref__': <attribute '__weakref__' of 'WeakRefAttribute' objects>})¶
-
__dir__
() → list¶ default dir() implementation
-
__eq__
¶ Return self==value.
-
__format__
()¶ default object formatter
-
__ge__
¶ Return self>=value.
-
__getattribute__
¶ Return getattr(self, name).
-
__gt__
¶ Return self>value.
-
__hash__
¶ Return hash(self).
-
__init__
¶ Initialize self. See help(type(self)) for accurate signature.
-
__init_subclass__
()¶ This method is called when a class is subclassed.
The default implementation does nothing. It may be overridden to extend subclasses.
-
__le__
¶ Return self<=value.
-
__lt__
¶ Return self<value.
-
__module__
= 'schrodinger.infra.util'¶
-
__ne__
¶ Return self!=value.
-
__new__
()¶ Create and return a new object. See help(type) for accurate signature.
-
__reduce__
()¶ helper for pickle
-
__reduce_ex__
()¶ helper for pickle
-
__repr__
¶ Return repr(self).
-
__setattr__
¶ Implement setattr(self, name, value).
-
__sizeof__
() → int¶ size of object in memory, in bytes
-
__str__
¶ Return str(self).
-
__subclasshook__
()¶ Abstract classes can override this to customize issubclass().
This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return True, False or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
-
__weakref__
¶ list of weak references to the object (if defined)
-