schrodinger.utils.scollections module

schrodinger.utils.scollections.split_list(input_list, num_chunks)

Split a list into N equal chunks.

Note: function is similar to numpy.split_array.

Parameters:
  • input_list (list) – The list to split
  • num_chunks (int) – The desired number of chunks
class schrodinger.utils.scollections.IdSet(initial_set=None)

Bases: collections.abc.MutableSet, set

An id set is a set that uses the id of an object as the key instead of the hash. This means that two objects that compare equal but are different instances will be stored separately since id(obj1) != id(obj2).

NOTE: Using this set with python’s builtin immutable datatypes is /strongly/ discouraged (e.g. str and int are not guaranteed to have different ids for separate instances)

__init__(initial_set=None)

Initialize self. See help(type(self)) for accurate signature.

__contains__(obj)

x.__contains__(y) <==> y in x.

__len__()

Return len(self).

copy()

Return a shallow copy of a set.

classmethod fromIterable(iterable)
isdisjoint(other)

Return True if two sets have a null intersection.

issubset(other)

Report whether another set contains this set.

issuperset(other)

Report whether this set contains another set.

union(*other_sets)

Return the union of sets as a new set.

(i.e. all elements that are in either set.)

intersection(*other_sets)

Return the intersection of two sets as a new set.

(i.e. all elements that are in both sets.)

difference(*other_sets)

Return the difference of two or more sets as a new set.

(i.e. all elements that are in this set but not the others.)

symmetric_difference(*other_sets)

Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)

update(*other_sets)

Update a set with the union of itself and others.

intersection_update(*other_sets)

Update a set with the intersection of itself and another.

difference_update(*other_sets)

Remove all elements of another set from this set.

symmetric_difference_update(*other_sets)

Update a set with the symmetric difference of itself and another.

add(obj)

Add an element.

discard(obj)

Remove an element. Do not raise an exception if absent.

clear()

This is slow (creates N new iterators!) but effective.

pop()

Return the popped value. Raise KeyError if empty.

remove(value)

Remove an element. If not a member, raise a KeyError.

class schrodinger.utils.scollections.IdItemsView(id_dict, id_map)

Bases: collections.abc.ItemsView

__init__(id_dict, id_map)

Initialize self. See help(type(self)) for accurate signature.

__contains__(item)
__len__()
isdisjoint(other)

Return True if two sets have a null intersection.

class schrodinger.utils.scollections.IdDict(initial_dict=None)

Bases: collections.abc.MutableMapping, dict

An id dict is a dictionary that uses the id of an object as the key instead of the hash. This means that two objects that compare equal but are different instances will be stored separately since id(obj1) != id(obj2).

NOTE: Using this dict with python’s builtin immutable datatypes is /strongly/ discouraged (e.g. str and int are not guaranteed to have different ids for separate instances)

__init__(initial_dict=None)

Initialize self. See help(type(self)) for accurate signature.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
__contains__(obj)

True if D has a key k, else False.

__len__()

Return len(self).

items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
has_key(obj)
update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

updateFromIterable(iterable)
classmethod fromIterable(iterable)
clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

values() → an object providing a view on D's values
class schrodinger.utils.scollections.DefaultIdDict(default_factory)

Bases: schrodinger.utils.scollections.IdDict

A dict that is both an id dict and a defaultdict.

__init__(default_factory)

Initialize self. See help(type(self)) for accurate signature.

classmethod fromIterable(iterable)
setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
__contains__(obj)

True if D has a key k, else False.

__len__()

Return len(self).

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
has_key(obj)
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() → (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

update([E, ]**F) → None. Update D from mapping/iterable E and F.

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

updateFromIterable(iterable)
values() → an object providing a view on D's values
class schrodinger.utils.scollections.DefaultFactoryDictMixin(factory_func, *args, **kwargs)

Bases: object

A mixin to use with dict’s that allows the dict to use a factory function similar to defaultdict. The key distinction here is that the factory function will be passed the key itself instead of called without any arguments.

Note

Despite the name, this mixin works with classes as well. When passed a class, the constructor will be called and passed the keys.

Warning

This mixin will not work with factory functions that expect only one tuple as an argument. This is due to the way __getitem__ packages up all keys in a single call into one tuple.

__init__(factory_func, *args, **kwargs)
Parameters:factory_fun (callable) – A callable to create a value from.
class schrodinger.utils.scollections.DefaultFactoryDict(factory_func, *args, **kwargs)

Bases: schrodinger.utils.scollections.DefaultFactoryDictMixin, dict

A basic dict using the DefaultFactoryDictMixin. This is separated from the mixin to allow other dict subclasses to easily subclass DefaultFactoryDictMixin.

Example usage::
stringified_objs = DefaultFactoryDict(str) assert 1 not in stringified_objs print(stringified_objs[1]) # ‘1’
__contains__()

True if D has a key k, else False.

__init__(factory_func, *args, **kwargs)
Parameters:factory_fun (callable) – A callable to create a value from.
__len__

Return len(self).

clear() → None. Remove all items from D.
copy() → a shallow copy of D
fromkeys()

Returns a new dict with keys from iterable and values equal to value.

get(k[, d]) → D[k] if k in D, else d. d defaults to None.
items() → a set-like object providing a view on D's items
keys() → a set-like object providing a view on D's keys
pop(k[, d]) → v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem() → (k, v), remove and return some (key, value) pair as a

2-tuple; but raise KeyError if D is empty.

setdefault(k[, d]) → D.get(k,d), also set D[k]=d if k not in D
update([E, ]**F) → None. Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() → an object providing a view on D's values