Utilities and commonly reused generics¶
Many helper functions used throughout the rest of the library.
-
class
glypy.utils.base.
ClassPropertyDescriptor
(fget, fset=None)[source]¶ Bases:
object
Standard Class Property Descriptor Implementation
-
glypy.utils.base.
classproperty
(func)[source]¶ Applies ClassPropertyDescriptor as you would a normal @property descriptor
-
glypy.utils.base.
cyclewarning
()[source]¶ Used to warn users about the presence of cylcical glycans, which are harder to reason about for crossring_cleavages.
-
glypy.utils.base.
make_counter
(start=1)[source]¶ Create a functor whose only internal piece of data is a mutable container with a reference to an integer,
start
. When the functor is called, it returns currentint
value ofstart
and increments the mutable value by one.Parameters: start: int, optional
The number to start counting from. Defaults to
1
.Returns: int:
The next number in the count progression.
-
glypy.utils.base.
make_struct
(name, fields, debug=False)[source]¶ A convenience function for defining plain-old-data (POD) objects that are optimized for named accessor lookup, unlike
namedtuple
. If the named container does not require any special logic and won’t be extended, the resulting structure is best for storing and accessing the data.Parameters: name: str
The name of the new class structure
fields: iterable of str
-
glypy.utils.base.
opener
(obj, mode='r')[source]¶ Try to use
obj
to access a file-object. Ifobj
is a string, assume it denotes a path to a file, and open that file in the specified mode. Ifobj
has an attributeread
, assume it itself is a file-like object and return it.Parameters: obj: basestring or file-like object
If
obj
is a base string it is treated like a file path, else if it supports the file-like operationread
, return the object unchanged.mode: str, optional
The mode, if any, to open
obj
with if it is a file path. Defaults to ‘r’,read
-
glypy.utils.base.
root
(structure)[source]¶ The
root
protocol function.Creates a generic method for obtaining the root of a structure representing or containing a glycan graph with a single distinct root.
Parameters: structure : any
An object that implements the
root
protocol, containing a tree structure somewhere inside it.Returns: Monosaccharide : The root of the
Glycan
tree
-
glypy.utils.base.
tree
(structure)[source]¶ The
tree
protocol function.Creates a generic method for obtaining the
Glycan
of a structure representing or containing a glycan graph.Parameters: structure : any
An object that implements the
root
protocol, containing a tree structure somewhere inside it.Returns: Glycan
-
class
glypy.utils.enum.
Enum
[source]¶ Bases:
object
A simple class implementing
EnumMeta
. Useful base type for other enumerated types.
-
class
glypy.utils.enum.
EnumMeta
[source]¶ Bases:
type
A metaclass for types hosting enumerated members. Class attributes are automatically translated into
EnumValue
objects with the specified value and a name string matching the specified attribute name. Newly assigned attributes are wrapped dynamically.The class itself can be treated like a dictionary for looking up these attributes by name or value, and these values can be iterated over.
- ..note::
Why is this necessary? It’s probably not. I chose to write it initially for compatibility with a previous iteration of the library, but later decided it was worth keeping for two reasons:
- Avoids
stringly-typing
the library. Comparison of strings is a slippery slope to raw magic strings littering the codebase. - Richer comparison behavior allows these same names to be used in different modes with the same symbol.
- Namespacing of EnumValue objects makes it easier to avoid accidental name collisions when comparing EnumValues instead of magic strings.
- Avoids
-
__call__
(k)¶ Attempt to translate the input object
k
into a data member of the Enum.First try to find an element of
self
by hashing it against member names.Then try to find an element of
self
by searching for a member in self that is value-equal tok
Otherwise throw a
KeyError
Parameters: k: object
The value to be translated.
Returns:
-
__setattr__
(k, v)[source]¶ Intercept attribute assignment, wrapping values in
EnumValue
Parameters: k : str
Name to be set
v : object
The value to assign. If it is not of type
EnumValue
it will be wrapped likeEnumValue(self, name=k, value=v)
-
translate
(k)[source]¶ Attempt to translate the input object
k
into a data member of the Enum.First try to find an element of
self
by hashing it against member names.Then try to find an element of
self
by searching for a member in self that is value-equal tok
Otherwise throw a
KeyError
Parameters: k: object
The value to be translated.
Returns:
-
class
glypy.utils.enum.
EnumValue
(group, name, value, other_names=None)[source]¶ Bases:
object
Represents a wrapper around an value with a name to identify it and more rich comparison logic. A value of an enumerated type
-
class
glypy.utils.multimap.
MultiMap
(**kwargs)[source]¶ Bases:
object
Implements a simple MultiMap data structure on top of a dictionary of lists
-
items
()[source]¶ Returns an iterator over the items of
contents
. Each item takes the form of(key, value)
.
-
-
class
glypy.utils.multimap.
OrderedMultiMap
(**kwargs)[source]¶ Bases:
glypy.utils.multimap.MultiMap
Implements a simple MultiMap data structure on top of a dictionary of lists that remembers the order keys were first inserted in.
-
keys
()¶ Alias of
__iter__()
-