schrodinger.infra.util module

General infrastructure level utilities.

Copyright Schrodinger, LLC. All rights reserved.

class schrodinger.infra.util.cached_property(func)[source]

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)[source]
class schrodinger.infra.util.CreateWhenNeeded(function, name=None, doc=None)[source]

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)[source]

Create the descriptor.

class schrodinger.infra.util.OneIndexedList(iterable=(), /)[source]

Bases: list

A list that starts at one instead of zero

index(x)[source]

Return first index of value.

Raises ValueError if the value is not present.

insert(i, x)[source]

Insert object before index.

pop(i)[source]

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

copy()[source]

Return a shallow copy of the list.

__contains__(key, /)

Return key in self.

__init__(*args, **kwargs)
__len__()

Return len(self).

append(object, /)

Append object to the end of the list.

clear()

Remove all items from list.

count(value, /)

Return number of occurrences of value.

extend(iterable, /)

Extend list by appending elements from the iterable.

remove(value, /)

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

sort(*, key=None, reverse=False)

Sort the list in ascending order and return None.

The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained).

If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values.

The reverse flag can be set to sort in descending order.

schrodinger.infra.util.flag_context_manager(attr_name, set_to=True)[source]

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)[source]

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.

schrodinger.infra.util.enum_speedup(enum_cls)[source]

Add all enum members to a new class to speed up access.

Attribute access in enum classes under Python 2.7 was 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. Enums were significantly sped up in Python 3.5, but attribute access is still measurably slower than in regular classes.

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

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__). Use object in the signal declaration instead.

In other words, use

mySignal = QtCore.pyqtSignal(object)

instead of

mySignal = QtCore.pyqtSignal(MySpedUpEnum)
class schrodinger.infra.util.WeakRefAttribute[source]

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.

schrodinger.infra.util.find_decorated_methods(cls_or_instance, method_attr_name)[source]

Find all class methods where the method object contains the specified attribute. This is normally used to find methods that have been decorated with a particular decorator. Note that this function will not cause properties to be executed so as to avoid any potential performance issues.

Parameters
  • cls_or_instance (type or object) – A class or instance if a class to find methods on

  • method_attr_name (str) – The attribute name to search for

Returns

A list of methods with the given attribute. The list is ordered the same as dir, i.e. alphabetically. This list will contain bound methods if cls_or_instance is an instance, and will contain functions (i.e. unbound methods) if cls_or_instance is a class.

Return type

list[FunctionType] or list[MethodType]

Example usage:

def my_decorator(func):
    func.is_special = True  # This can be any value
    return func

class Foo:
    @my_decorator
    def bar(self):
        pass

find_decorated_methods(Foo, "is_special")  # returns [Foo.bar]
foo = Foo()
find_decorated_methods(foo, "is_special")  # returns [foo.bar]