schrodinger.infra.util module

General infrastructure level utilities.

Copyright Schrodinger, LLC. All rights reserved.

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.

class schrodinger.infra.util.DecoratorAndContextManager

Bases: object

An abstract base class for context managers that can also be used to decorate functions and classes. When a function is decorated, it is equivalent to always running it inside the context manager. When a class is decorated, it is equivalent to decorating every method of the class.

This is tested in mmerr_test.

decorate(decoratee)

Decorate a function or class.

class schrodinger.infra.util.OneIndexedList

Bases: list

A list that starts at one instead of zero

index(x)
insert(i, x)
pop(i)
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

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.
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:

skip_if

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 equals is_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:

flag_context_manager

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.