code stringlengths 75 104k | docstring stringlengths 1 46.9k |
|---|---|
def _get_backend_router(self, locations, item):
"""Returns valid router options for ordering a dedicated host."""
mask = '''
id,
hostname
'''
cpu_count = item['capacity']
for capacity in item['bundleItems']:
for category in capacity['categories']:
if category['categoryCode'] == 'dedicated_host_ram':
mem_capacity = capacity['capacity']
if category['categoryCode'] == 'dedicated_host_disk':
disk_capacity = capacity['capacity']
for hardwareComponent in item['bundleItems']:
if hardwareComponent['keyName'].find("GPU") != -1:
hardwareComponentType = hardwareComponent['hardwareGenericComponentModel']['hardwareComponentType']
gpuComponents = [
{
'hardwareComponentModel': {
'hardwareGenericComponentModel': {
'id': hardwareComponent['hardwareGenericComponentModel']['id'],
'hardwareComponentType': {
'keyName': hardwareComponentType['keyName']
}
}
}
},
{
'hardwareComponentModel': {
'hardwareGenericComponentModel': {
'id': hardwareComponent['hardwareGenericComponentModel']['id'],
'hardwareComponentType': {
'keyName': hardwareComponentType['keyName']
}
}
}
}
]
if locations is not None:
for location in locations:
if location['locationId'] is not None:
loc_id = location['locationId']
host = {
'cpuCount': cpu_count,
'memoryCapacity': mem_capacity,
'diskCapacity': disk_capacity,
'datacenter': {
'id': loc_id
}
}
if item['keyName'].find("GPU") != -1:
host['pciDevices'] = gpuComponents
routers = self.host.getAvailableRouters(host, mask=mask)
return routers
raise SoftLayer.SoftLayerError("Could not find available routers") | Returns valid router options for ordering a dedicated host. |
def loads(cls, s):
"""Parse from a string representation (repr)"""
try:
currency, amount = s.strip().split(' ')
return cls(amount, currency)
except ValueError as err:
# RADAR: Python2
money.six.raise_from(ValueError("failed to parse string "
" '{}': {}".format(s, err)), None) | Parse from a string representation (repr) |
def det4D(m):
'''
det4D(array) yields the determinate of the given matrix array, which may have more than 2
dimensions, in which case the later dimensions are multiplied and added point-wise.
'''
# I just solved this in Mathematica, copy-pasted, and replaced the string '] m' with ']*m':
# Mathematica code: Det@Table[m[i][j], {i, 0, 3}, {j, 0, 3}]
return (m[0][3]*m[1][2]*m[2][1]*m[3][0] - m[0][2]*m[1][3]*m[2][1]*m[3][0] -
m[0][3]*m[1][1]*m[2][2]*m[3][0] + m[0][1]*m[1][3]*m[2][2]*m[3][0] +
m[0][2]*m[1][1]*m[2][3]*m[3][0] - m[0][1]*m[1][2]*m[2][3]*m[3][0] -
m[0][3]*m[1][2]*m[2][0]*m[3][1] + m[0][2]*m[1][3]*m[2][0]*m[3][1] +
m[0][3]*m[1][0]*m[2][2]*m[3][1] - m[0][0]*m[1][3]*m[2][2]*m[3][1] -
m[0][2]*m[1][0]*m[2][3]*m[3][1] + m[0][0]*m[1][2]*m[2][3]*m[3][1] +
m[0][3]*m[1][1]*m[2][0]*m[3][2] - m[0][1]*m[1][3]*m[2][0]*m[3][2] -
m[0][3]*m[1][0]*m[2][1]*m[3][2] + m[0][0]*m[1][3]*m[2][1]*m[3][2] +
m[0][1]*m[1][0]*m[2][3]*m[3][2] - m[0][0]*m[1][1]*m[2][3]*m[3][2] -
m[0][2]*m[1][1]*m[2][0]*m[3][3] + m[0][1]*m[1][2]*m[2][0]*m[3][3] +
m[0][2]*m[1][0]*m[2][1]*m[3][3] - m[0][0]*m[1][2]*m[2][1]*m[3][3] -
m[0][1]*m[1][0]*m[2][2]*m[3][3] + m[0][0]*m[1][1]*m[2][2]*m[3][3]) | det4D(array) yields the determinate of the given matrix array, which may have more than 2
dimensions, in which case the later dimensions are multiplied and added point-wise. |
def crossvalidate_model(self, classifier, data, num_folds, rnd, output=None):
"""
Crossvalidates the model using the specified data, number of folds and random number generator wrapper.
:param classifier: the classifier to cross-validate
:type classifier: Classifier
:param data: the data to evaluate on
:type data: Instances
:param num_folds: the number of folds
:type num_folds: int
:param rnd: the random number generator to use
:type rnd: Random
:param output: the output generator to use
:type output: PredictionOutput
"""
if output is None:
generator = []
else:
generator = [output.jobject]
javabridge.call(
self.jobject, "crossValidateModel",
"(Lweka/classifiers/Classifier;Lweka/core/Instances;ILjava/util/Random;[Ljava/lang/Object;)V",
classifier.jobject, data.jobject, num_folds, rnd.jobject, generator) | Crossvalidates the model using the specified data, number of folds and random number generator wrapper.
:param classifier: the classifier to cross-validate
:type classifier: Classifier
:param data: the data to evaluate on
:type data: Instances
:param num_folds: the number of folds
:type num_folds: int
:param rnd: the random number generator to use
:type rnd: Random
:param output: the output generator to use
:type output: PredictionOutput |
def merge(self, other):
"""
Merges two BoolCells
"""
other = BoolCell.coerce(other)
if self.is_equal(other):
# pick among dependencies
return self
elif other.is_entailed_by(self):
return self
elif self.is_entailed_by(other):
self.value = other.value
elif self.is_contradictory(other):
raise Contradiction("Cannot merge T and F")
else:
raise Exception
return self | Merges two BoolCells |
def get_region_products(self, region):
"""获得指定区域的产品信息
Args:
- region: 区域,如:"nq"
Returns:
返回该区域的产品信息,若失败则返回None
"""
regions, retInfo = self.list_regions()
if regions is None:
return None
for r in regions:
if r.get('name') == region:
return r.get('products') | 获得指定区域的产品信息
Args:
- region: 区域,如:"nq"
Returns:
返回该区域的产品信息,若失败则返回None |
def _set_fcoe(self, v, load=False):
"""
Setter method for fcoe, mapped from YANG variable /interface/fcoe (list)
If this variable is read-only (config: false) in the
source YANG file, then _set_fcoe is considered as a private
method. Backends looking to populate this variable should
do so via calling thisObj._set_fcoe() directly.
YANG Description: The list of FCoE interfaces. Each row contains FCoE
interface name and its status.
"""
if hasattr(v, "_utype"):
v = v._utype(v)
try:
t = YANGDynClass(v,base=YANGListType("fcoe_interface_name",fcoe.fcoe, yang_name="fcoe", rest_name="Fcoe", parent=self, is_container='list', user_ordered=False, path_helper=self._path_helper, yang_keys='fcoe-interface-name', extensions={u'tailf-common': {u'info': u'The list of FCoE Logical interfaces', u'cli-no-key-completion': None, u'alt-name': u'Fcoe', u'sort-priority': u'RUNNCFG_LEVEL_INTERFACE_TYPE_LOGICAL', u'cli-custom-range-actionpoint': u'FcoeRangeCliActionpoint', u'cli-custom-range-enumerator': u'FcoeRangeCliActionpoint', u'display-when': u'/vcsmode/vcs-mode = "true"', u'cli-full-command': None, u'callpoint': u'fcoe_interface_cp'}}), is_container='list', yang_name="fcoe", rest_name="Fcoe", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'The list of FCoE Logical interfaces', u'cli-no-key-completion': None, u'alt-name': u'Fcoe', u'sort-priority': u'RUNNCFG_LEVEL_INTERFACE_TYPE_LOGICAL', u'cli-custom-range-actionpoint': u'FcoeRangeCliActionpoint', u'cli-custom-range-enumerator': u'FcoeRangeCliActionpoint', u'display-when': u'/vcsmode/vcs-mode = "true"', u'cli-full-command': None, u'callpoint': u'fcoe_interface_cp'}}, namespace='urn:brocade.com:mgmt:brocade-fcoe', defining_module='brocade-fcoe', yang_type='list', is_config=True)
except (TypeError, ValueError):
raise ValueError({
'error-string': """fcoe must be of a type compatible with list""",
'defined-type': "list",
'generated-type': """YANGDynClass(base=YANGListType("fcoe_interface_name",fcoe.fcoe, yang_name="fcoe", rest_name="Fcoe", parent=self, is_container='list', user_ordered=False, path_helper=self._path_helper, yang_keys='fcoe-interface-name', extensions={u'tailf-common': {u'info': u'The list of FCoE Logical interfaces', u'cli-no-key-completion': None, u'alt-name': u'Fcoe', u'sort-priority': u'RUNNCFG_LEVEL_INTERFACE_TYPE_LOGICAL', u'cli-custom-range-actionpoint': u'FcoeRangeCliActionpoint', u'cli-custom-range-enumerator': u'FcoeRangeCliActionpoint', u'display-when': u'/vcsmode/vcs-mode = "true"', u'cli-full-command': None, u'callpoint': u'fcoe_interface_cp'}}), is_container='list', yang_name="fcoe", rest_name="Fcoe", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'The list of FCoE Logical interfaces', u'cli-no-key-completion': None, u'alt-name': u'Fcoe', u'sort-priority': u'RUNNCFG_LEVEL_INTERFACE_TYPE_LOGICAL', u'cli-custom-range-actionpoint': u'FcoeRangeCliActionpoint', u'cli-custom-range-enumerator': u'FcoeRangeCliActionpoint', u'display-when': u'/vcsmode/vcs-mode = "true"', u'cli-full-command': None, u'callpoint': u'fcoe_interface_cp'}}, namespace='urn:brocade.com:mgmt:brocade-fcoe', defining_module='brocade-fcoe', yang_type='list', is_config=True)""",
})
self.__fcoe = t
if hasattr(self, '_set'):
self._set() | Setter method for fcoe, mapped from YANG variable /interface/fcoe (list)
If this variable is read-only (config: false) in the
source YANG file, then _set_fcoe is considered as a private
method. Backends looking to populate this variable should
do so via calling thisObj._set_fcoe() directly.
YANG Description: The list of FCoE interfaces. Each row contains FCoE
interface name and its status. |
def focus(self, f):
"""
Get a new UI proxy copy with the given focus. Return a new UI proxy object as the UI proxy is immutable.
Args:
f (2-:obj:`tuple`/2-:obj:`list`/:obj:`str`): the focus point, it can be specified as 2-list/2-tuple
coordinates (x, y) in NormalizedCoordinate system or as 'center' or 'anchor'.
Returns:
:py:class:`UIObjectProxy <poco.proxy.UIObjectProxy>`: a new UI proxy object (copy)
"""
ret = copy.copy(self)
ret._focus = f
return ret | Get a new UI proxy copy with the given focus. Return a new UI proxy object as the UI proxy is immutable.
Args:
f (2-:obj:`tuple`/2-:obj:`list`/:obj:`str`): the focus point, it can be specified as 2-list/2-tuple
coordinates (x, y) in NormalizedCoordinate system or as 'center' or 'anchor'.
Returns:
:py:class:`UIObjectProxy <poco.proxy.UIObjectProxy>`: a new UI proxy object (copy) |
def create(dataset, label=None, features=None, distance=None, method='auto',
verbose=True, **kwargs):
"""
Create a nearest neighbor model, which can be searched efficiently and
quickly for the nearest neighbors of a query observation. If the `method`
argument is specified as `auto`, the type of model is chosen automatically
based on the type of data in `dataset`.
.. warning::
The 'dot_product' distance is deprecated and will be removed in future
versions of Turi Create. Please use 'transformed_dot_product'
distance instead, although note that this is more than a name change;
it is a *different* transformation of the dot product of two vectors.
Please see the distances module documentation for more details.
Parameters
----------
dataset : SFrame
Reference data. If the features for each observation are numeric, they
may be in separate columns of 'dataset' or a single column with lists
of values. The features may also be in the form of a column of sparse
vectors (i.e. dictionaries), with string keys and numeric values.
label : string, optional
Name of the SFrame column with row labels. If 'label' is not specified,
row numbers are used to identify reference dataset rows when the model
is queried.
features : list[string], optional
Name of the columns with features to use in computing distances between
observations and the query points. 'None' (the default) indicates that
all columns except the label should be used as features. Each column
can be one of the following types:
- *Numeric*: values of numeric type integer or float.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate variable in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values.
Each key indicates a separate variable in the model.
- *List*: list of integer or string values. Each element is treated as
a separate variable in the model.
- *String*: string values.
Please note: if a composite distance is also specified, this parameter
is ignored.
distance : string, function, or list[list], optional
Function to measure the distance between any two input data rows. This
may be one of three types:
- *String*: the name of a standard distance function. One of
'euclidean', 'squared_euclidean', 'manhattan', 'levenshtein',
'jaccard', 'weighted_jaccard', 'cosine', 'dot_product' (deprecated),
or 'transformed_dot_product'.
- *Function*: a function handle from the
:mod:`~turicreate.toolkits.distances` module.
- *Composite distance*: the weighted sum of several standard distance
functions applied to various features. This is specified as a list of
distance components, each of which is itself a list containing three
items:
1. list or tuple of feature names (strings)
2. standard distance name (string)
3. scaling factor (int or float)
For more information about Turi Create distance functions, please
see the :py:mod:`~turicreate.toolkits.distances` module.
If 'distance' is left unspecified or set to 'auto', a composite
distance is constructed automatically based on feature types.
method : {'auto', 'ball_tree', 'brute_force', 'lsh'}, optional
Method for computing nearest neighbors. The options are:
- *auto* (default): the method is chosen automatically, based on the
type of data and the distance. If the distance is 'manhattan' or
'euclidean' and the features are numeric or vectors of numeric
values, then the 'ball_tree' method is used. Otherwise, the
'brute_force' method is used.
- *ball_tree*: use a tree structure to find the k-closest neighbors to
each query point. The ball tree model is slower to construct than the
brute force model, but queries are faster than linear time. This
method is not applicable for the cosine and dot product distances.
See `Liu, et al (2004)
<http://papers.nips.cc/paper/2666-an-investigation-of-p
ractical-approximat e-nearest-neighbor-algorithms>`_ for
implementation details.
- *brute_force*: compute the distance from a query point to all
reference observations. There is no computation time for model
creation with the brute force method (although the reference data is
held in the model, but each query takes linear time.
- *lsh*: use Locality Sensitive Hashing (LSH) to find approximate
nearest neighbors efficiently. The LSH model supports 'euclidean',
'squared_euclidean', 'manhattan', 'cosine', 'jaccard', 'dot_product'
(deprecated), and 'transformed_dot_product' distances. Two options
are provided for LSH -- ``num_tables`` and
``num_projections_per_table``. See the notes below for details.
verbose: bool, optional
If True, print progress updates and model details.
**kwargs : optional
Options for the distance function and query method.
- *leaf_size*: for the ball tree method, the number of points in each
leaf of the tree. The default is to use the max of 1,000 and
n/(2^11), which ensures a maximum tree depth of 12.
- *num_tables*: For the LSH method, the number of hash tables
constructed. The default value is 20. We recommend choosing values
from 10 to 30.
- *num_projections_per_table*: For the LSH method, the number of
projections/hash functions for each hash table. The default value is
4 for 'jaccard' distance, 16 for 'cosine' distance and 8 for other
distances. We recommend using number 2 ~ 6 for 'jaccard' distance, 8
~ 20 for 'cosine' distance and 4 ~ 12 for other distances.
Returns
-------
out : NearestNeighborsModel
A structure for efficiently computing the nearest neighbors in 'dataset'
of new query points.
See Also
--------
NearestNeighborsModel.query, turicreate.toolkits.distances
Notes
-----
- Missing data is not allowed in the 'dataset' provided to this function.
Please use the :func:`turicreate.SFrame.fillna` and
:func:`turicreate.SFrame.dropna` utilities to handle missing data before
creating a nearest neighbors model.
- Missing keys in sparse vectors are assumed to have value 0.
- The `composite_params` parameter was removed as of Turi Create
version 1.5. The `distance` parameter now accepts either standard or
composite distances. Please see the :mod:`~turicreate.toolkits.distances`
module documentation for more information on composite distances.
- If the features should be weighted equally in the distance calculations
but are measured on different scales, it is important to standardize the
features. One way to do this is to subtract the mean of each column and
divide by the standard deviation.
**Locality Sensitive Hashing (LSH)**
There are several efficient nearest neighbors search algorithms that work
well for data with low dimensions :math:`d` (approximately 50). However,
most of the solutions suffer from either space or query time that is
exponential in :math:`d`. For large :math:`d`, they often provide little,
if any, improvement over the 'brute_force' method. This is a well-known
consequence of the phenomenon called `The Curse of Dimensionality`.
`Locality Sensitive Hashing (LSH)
<https://en.wikipedia.org/wiki/Locality-sensitive_hashing>`_ is an approach
that is designed to efficiently solve the *approximate* nearest neighbor
search problem for high dimensional data. The key idea of LSH is to hash
the data points using several hash functions, so that the probability of
collision is much higher for data points which are close to each other than
those which are far apart.
An LSH family is a family of functions :math:`h` which map points from the
metric space to a bucket, so that
- if :math:`d(p, q) \\leq R`, then :math:`h(p) = h(q)` with at least probability :math:`p_1`.
- if :math:`d(p, q) \\geq cR`, then :math:`h(p) = h(q)` with probability at most :math:`p_2`.
LSH for efficient approximate nearest neighbor search:
- We define a new family of hash functions :math:`g`, where each
function :math:`g` is obtained by concatenating :math:`k` functions
:math:`h_1, ..., h_k`, i.e., :math:`g(p)=[h_1(p),...,h_k(p)]`.
The algorithm constructs :math:`L` hash tables, each of which
corresponds to a different randomly chosen hash function :math:`g`.
There are :math:`k \\cdot L` hash functions used in total.
- In the preprocessing step, we hash all :math:`n` reference points
into each of the :math:`L` hash tables.
- Given a query point :math:`q`, the algorithm iterates over the
:math:`L` hash functions :math:`g`. For each :math:`g` considered, it
retrieves the data points that are hashed into the same bucket as q.
These data points from all the :math:`L` hash tables are considered as
candidates that are then re-ranked by their real distances with the query
data.
**Note** that the number of tables :math:`L` and the number of hash
functions per table :math:`k` are two main parameters. They can be set
using the options ``num_tables`` and ``num_projections_per_table``
respectively.
Hash functions for different distances:
- `euclidean` and `squared_euclidean`:
:math:`h(q) = \\lfloor \\frac{a \\cdot q + b}{w} \\rfloor` where
:math:`a` is a vector, of which the elements are independently
sampled from normal distribution, and :math:`b` is a number
uniformly sampled from :math:`[0, r]`. :math:`r` is a parameter for the
bucket width. We set :math:`r` using the average all-pair `euclidean`
distances from a small randomly sampled subset of the reference data.
- `manhattan`: The hash function of `manhattan` is similar with that of
`euclidean`. The only difference is that the elements of `a` are sampled
from Cauchy distribution, instead of normal distribution.
- `cosine`: Random Projection is designed to approximate the cosine
distance between vectors. The hash function is :math:`h(q) = sgn(a \\cdot
q)`, where :math:`a` is randomly sampled normal unit vector.
- `jaccard`: We use a recently proposed method one permutation hashing by
Shrivastava and Li. See the paper `[Shrivastava and Li, UAI 2014]
<http://www.auai.org/uai2014/proceedings/individuals/225.pdf>`_ for
details.
- `dot_product`: The reference data points are first transformed to
fixed-norm vectors, and then the minimum `dot_product` distance search
problem can be solved via finding the reference data with smallest
`cosine` distances. See the paper `[Neyshabur and Srebro, ICML 2015]
<http://proceedings.mlr.press/v37/neyshabur15.html>`_ for details.
References
----------
- `Wikipedia - nearest neighbor
search <http://en.wikipedia.org/wiki/Nearest_neighbor_search>`_
- `Wikipedia - ball tree <http://en.wikipedia.org/wiki/Ball_tree>`_
- Ball tree implementation: Liu, T., et al. (2004) `An Investigation of
Practical Approximate Nearest Neighbor Algorithms
<http://papers.nips.cc/paper/2666-an-investigation-of-p
ractical-approximat e-nearest-neighbor-algorithms>`_. Advances in Neural
Information Processing Systems pp. 825-832.
- `Wikipedia - Jaccard distance
<http://en.wikipedia.org/wiki/Jaccard_index>`_
- Weighted Jaccard distance: Chierichetti, F., et al. (2010) `Finding the
Jaccard Median
<http://theory.stanford.edu/~sergei/papers/soda10-jaccard.pdf>`_.
Proceedings of the Twenty-First Annual ACM-SIAM Symposium on Discrete
Algorithms. Society for Industrial and Applied Mathematics.
- `Wikipedia - Cosine distance
<http://en.wikipedia.org/wiki/Cosine_similarity>`_
- `Wikipedia - Levenshtein distance
<http://en.wikipedia.org/wiki/Levenshtein_distance>`_
- Locality Sensitive Hashing : Chapter 3 of the book `Mining Massive
Datasets <http://infolab.stanford.edu/~ullman/mmds/ch3.pdf>`_.
Examples
--------
Construct a nearest neighbors model with automatically determined method
and distance:
>>> sf = turicreate.SFrame({'X1': [0.98, 0.62, 0.11],
... 'X2': [0.69, 0.58, 0.36],
... 'str_feature': ['cat', 'dog', 'fossa']})
>>> model = turicreate.nearest_neighbors.create(sf, features=['X1', 'X2'])
For datasets with a large number of rows and up to about 100 variables, the
ball tree method often leads to much faster queries.
>>> model = turicreate.nearest_neighbors.create(sf, features=['X1', 'X2'],
... method='ball_tree')
Often the final determination of a neighbor is based on several distance
computations over different sets of features. Each part of this composite
distance may have a different relative weight.
>>> my_dist = [[['X1', 'X2'], 'euclidean', 2.],
... [['str_feature'], 'levenshtein', 3.]]
...
>>> model = turicreate.nearest_neighbors.create(sf, distance=my_dist)
"""
## Validate the 'dataset' input
_tkutl._raise_error_if_not_sframe(dataset, "dataset")
_tkutl._raise_error_if_sframe_empty(dataset, "dataset")
## Basic validation of the features input
if features is not None and not isinstance(features, list):
raise TypeError("If specified, input 'features' must be a list of " +
"strings.")
## Clean the method options and create the options dictionary
allowed_kwargs = ['leaf_size', 'num_tables', 'num_projections_per_table']
_method_options = {}
for k, v in kwargs.items():
if k in allowed_kwargs:
_method_options[k] = v
else:
raise _ToolkitError("'{}' is not a valid keyword argument".format(k) +
" for the nearest neighbors model. Please " +
"check for capitalization and other typos.")
## Exclude inappropriate combinations of method an distance
if method == 'ball_tree' and (distance == 'cosine'
or distance == _turicreate.distances.cosine
or distance == 'dot_product'
or distance == _turicreate.distances.dot_product
or distance == 'transformed_dot_product'
or distance == _turicreate.distances.transformed_dot_product):
raise TypeError("The ball tree method does not work with 'cosine' " +
"'dot_product', or 'transformed_dot_product' distance." +
"Please use the 'brute_force' method for these distances.")
if method == 'lsh' and ('num_projections_per_table' not in _method_options):
if distance == 'jaccard' or distance == _turicreate.distances.jaccard:
_method_options['num_projections_per_table'] = 4
elif distance == 'cosine' or distance == _turicreate.distances.cosine:
_method_options['num_projections_per_table'] = 16
else:
_method_options['num_projections_per_table'] = 8
## Initial validation and processing of the label
if label is None:
_label = _robust_column_name('__id', dataset.column_names())
_dataset = dataset.add_row_number(_label)
else:
_label = label
_dataset = _copy.copy(dataset)
col_type_map = {c:_dataset[c].dtype for c in _dataset.column_names()}
_validate_row_label(_label, col_type_map)
ref_labels = _dataset[_label]
## Determine the internal list of available feature names (may still include
# the row label name).
if features is None:
_features = _dataset.column_names()
else:
_features = _copy.deepcopy(features)
## Check if there's only one feature and it's the same as the row label.
# This would also be trapped by the composite distance validation, but the
# error message is not very informative for the user.
free_features = set(_features).difference([_label])
if len(free_features) < 1:
raise _ToolkitError("The only available feature is the same as the " +
"row label column. Please specify features " +
"that are not also row labels.")
### Validate and preprocess the distance function
### ---------------------------------------------
# - The form of the 'distance' controls how we interact with the 'features'
# parameter as well.
# - At this point, the row label 'label' may still be in the list(s) of
# features.
## Convert any distance function input into a single composite distance.
# distance is already a composite distance
if isinstance(distance, list):
distance = _copy.deepcopy(distance)
# distance is a single name (except 'auto') or function handle.
elif (hasattr(distance, '__call__') or
(isinstance(distance, str) and not distance == 'auto')):
distance = [[_features, distance, 1]]
# distance is unspecified and needs to be constructed.
elif distance is None or distance == 'auto':
sample = _dataset.head()
distance = _construct_auto_distance(_features,
_dataset.column_names(),
_dataset.column_types(),
sample)
else:
raise TypeError("Input 'distance' not understood. The 'distance' "
" argument must be a string, function handle, or " +
"composite distance.")
## Basic composite distance validation, remove the row label from all
# feature lists, and convert string distance names into distance functions.
distance = _scrub_composite_distance_features(distance, [_label])
distance = _convert_distance_names_to_functions(distance)
_validate_composite_distance(distance)
## Raise an error if any distances are used with non-lists
list_features_to_check = []
sparse_distances = ['jaccard', 'weighted_jaccard', 'cosine', 'dot_product', 'transformed_dot_product']
sparse_distances = [_turicreate.distances.__dict__[k] for k in sparse_distances]
for d in distance:
feature_names, dist, _ = d
list_features = [f for f in feature_names if _dataset[f].dtype == list]
for f in list_features:
if dist in sparse_distances:
list_features_to_check.append(f)
else:
raise TypeError("The chosen distance cannot currently be used " +
"on list-typed columns.")
for f in list_features_to_check:
only_str_lists = _validate_lists(_dataset[f], [str])
if not only_str_lists:
raise TypeError("Distances for sparse data, such as jaccard " +
"and weighted_jaccard, can only be used on " +
"lists containing only strings. Please modify " +
"any list features accordingly before creating " +
"the nearest neighbors model.")
## Raise an error if any component has string features are in single columns
for d in distance:
feature_names, dist, _ = d
if (len(feature_names) > 1) and (dist == _turicreate.distances.levenshtein):
raise ValueError("Levenshtein distance cannot be used with multiple " +
"columns. Please concatenate strings into a single " +
"column before creating the nearest neighbors model.")
## Get the union of feature names and make a clean dataset.
clean_features = _get_composite_distance_features(distance)
sf_clean = _tkutl._toolkits_select_columns(_dataset, clean_features)
## Decide which method to use
## - If more than one distance component (specified either directly or
# generated automatically because distance set to 'auto'), then do brute
# force.
if len(distance) > 1:
_method = 'brute_force'
if method != 'brute_force' and verbose is True:
print("Defaulting to brute force instead of ball tree because " +\
"there are multiple distance components.")
else:
if method == 'auto':
# get the total number of variables. Assume the number of elements in
# array type columns does not change
num_variables = sum([len(x) if hasattr(x, '__iter__') else 1
for x in _six.itervalues(sf_clean[0])])
# flag if all the features in the single composite are of numeric
# type.
numeric_type_flag = all([x in [int, float, list, array.array]
for x in sf_clean.column_types()])
## Conditions necessary for ball tree to work and be worth it
if ((distance[0][1] in ['euclidean',
'manhattan',
_turicreate.distances.euclidean,
_turicreate.distances.manhattan])
and numeric_type_flag is True
and num_variables <= 200):
_method = 'ball_tree'
else:
_method = 'brute_force'
else:
_method = method
## Pick the right model name for the method
if _method == 'ball_tree':
model_name = 'nearest_neighbors_ball_tree'
elif _method == 'brute_force':
model_name = 'nearest_neighbors_brute_force'
elif _method == 'lsh':
model_name = 'nearest_neighbors_lsh'
else:
raise ValueError("Method must be 'auto', 'ball_tree', 'brute_force', " +
"or 'lsh'.")
## Package the model options
opts = {}
opts.update(_method_options)
opts.update(
{'model_name': model_name,
'ref_labels': ref_labels,
'label': label,
'sf_features': sf_clean,
'composite_params': distance})
## Construct the nearest neighbors model
with QuietProgress(verbose):
result = _turicreate.extensions._nearest_neighbors.train(opts)
model_proxy = result['model']
model = NearestNeighborsModel(model_proxy)
return model | Create a nearest neighbor model, which can be searched efficiently and
quickly for the nearest neighbors of a query observation. If the `method`
argument is specified as `auto`, the type of model is chosen automatically
based on the type of data in `dataset`.
.. warning::
The 'dot_product' distance is deprecated and will be removed in future
versions of Turi Create. Please use 'transformed_dot_product'
distance instead, although note that this is more than a name change;
it is a *different* transformation of the dot product of two vectors.
Please see the distances module documentation for more details.
Parameters
----------
dataset : SFrame
Reference data. If the features for each observation are numeric, they
may be in separate columns of 'dataset' or a single column with lists
of values. The features may also be in the form of a column of sparse
vectors (i.e. dictionaries), with string keys and numeric values.
label : string, optional
Name of the SFrame column with row labels. If 'label' is not specified,
row numbers are used to identify reference dataset rows when the model
is queried.
features : list[string], optional
Name of the columns with features to use in computing distances between
observations and the query points. 'None' (the default) indicates that
all columns except the label should be used as features. Each column
can be one of the following types:
- *Numeric*: values of numeric type integer or float.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate variable in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values.
Each key indicates a separate variable in the model.
- *List*: list of integer or string values. Each element is treated as
a separate variable in the model.
- *String*: string values.
Please note: if a composite distance is also specified, this parameter
is ignored.
distance : string, function, or list[list], optional
Function to measure the distance between any two input data rows. This
may be one of three types:
- *String*: the name of a standard distance function. One of
'euclidean', 'squared_euclidean', 'manhattan', 'levenshtein',
'jaccard', 'weighted_jaccard', 'cosine', 'dot_product' (deprecated),
or 'transformed_dot_product'.
- *Function*: a function handle from the
:mod:`~turicreate.toolkits.distances` module.
- *Composite distance*: the weighted sum of several standard distance
functions applied to various features. This is specified as a list of
distance components, each of which is itself a list containing three
items:
1. list or tuple of feature names (strings)
2. standard distance name (string)
3. scaling factor (int or float)
For more information about Turi Create distance functions, please
see the :py:mod:`~turicreate.toolkits.distances` module.
If 'distance' is left unspecified or set to 'auto', a composite
distance is constructed automatically based on feature types.
method : {'auto', 'ball_tree', 'brute_force', 'lsh'}, optional
Method for computing nearest neighbors. The options are:
- *auto* (default): the method is chosen automatically, based on the
type of data and the distance. If the distance is 'manhattan' or
'euclidean' and the features are numeric or vectors of numeric
values, then the 'ball_tree' method is used. Otherwise, the
'brute_force' method is used.
- *ball_tree*: use a tree structure to find the k-closest neighbors to
each query point. The ball tree model is slower to construct than the
brute force model, but queries are faster than linear time. This
method is not applicable for the cosine and dot product distances.
See `Liu, et al (2004)
<http://papers.nips.cc/paper/2666-an-investigation-of-p
ractical-approximat e-nearest-neighbor-algorithms>`_ for
implementation details.
- *brute_force*: compute the distance from a query point to all
reference observations. There is no computation time for model
creation with the brute force method (although the reference data is
held in the model, but each query takes linear time.
- *lsh*: use Locality Sensitive Hashing (LSH) to find approximate
nearest neighbors efficiently. The LSH model supports 'euclidean',
'squared_euclidean', 'manhattan', 'cosine', 'jaccard', 'dot_product'
(deprecated), and 'transformed_dot_product' distances. Two options
are provided for LSH -- ``num_tables`` and
``num_projections_per_table``. See the notes below for details.
verbose: bool, optional
If True, print progress updates and model details.
**kwargs : optional
Options for the distance function and query method.
- *leaf_size*: for the ball tree method, the number of points in each
leaf of the tree. The default is to use the max of 1,000 and
n/(2^11), which ensures a maximum tree depth of 12.
- *num_tables*: For the LSH method, the number of hash tables
constructed. The default value is 20. We recommend choosing values
from 10 to 30.
- *num_projections_per_table*: For the LSH method, the number of
projections/hash functions for each hash table. The default value is
4 for 'jaccard' distance, 16 for 'cosine' distance and 8 for other
distances. We recommend using number 2 ~ 6 for 'jaccard' distance, 8
~ 20 for 'cosine' distance and 4 ~ 12 for other distances.
Returns
-------
out : NearestNeighborsModel
A structure for efficiently computing the nearest neighbors in 'dataset'
of new query points.
See Also
--------
NearestNeighborsModel.query, turicreate.toolkits.distances
Notes
-----
- Missing data is not allowed in the 'dataset' provided to this function.
Please use the :func:`turicreate.SFrame.fillna` and
:func:`turicreate.SFrame.dropna` utilities to handle missing data before
creating a nearest neighbors model.
- Missing keys in sparse vectors are assumed to have value 0.
- The `composite_params` parameter was removed as of Turi Create
version 1.5. The `distance` parameter now accepts either standard or
composite distances. Please see the :mod:`~turicreate.toolkits.distances`
module documentation for more information on composite distances.
- If the features should be weighted equally in the distance calculations
but are measured on different scales, it is important to standardize the
features. One way to do this is to subtract the mean of each column and
divide by the standard deviation.
**Locality Sensitive Hashing (LSH)**
There are several efficient nearest neighbors search algorithms that work
well for data with low dimensions :math:`d` (approximately 50). However,
most of the solutions suffer from either space or query time that is
exponential in :math:`d`. For large :math:`d`, they often provide little,
if any, improvement over the 'brute_force' method. This is a well-known
consequence of the phenomenon called `The Curse of Dimensionality`.
`Locality Sensitive Hashing (LSH)
<https://en.wikipedia.org/wiki/Locality-sensitive_hashing>`_ is an approach
that is designed to efficiently solve the *approximate* nearest neighbor
search problem for high dimensional data. The key idea of LSH is to hash
the data points using several hash functions, so that the probability of
collision is much higher for data points which are close to each other than
those which are far apart.
An LSH family is a family of functions :math:`h` which map points from the
metric space to a bucket, so that
- if :math:`d(p, q) \\leq R`, then :math:`h(p) = h(q)` with at least probability :math:`p_1`.
- if :math:`d(p, q) \\geq cR`, then :math:`h(p) = h(q)` with probability at most :math:`p_2`.
LSH for efficient approximate nearest neighbor search:
- We define a new family of hash functions :math:`g`, where each
function :math:`g` is obtained by concatenating :math:`k` functions
:math:`h_1, ..., h_k`, i.e., :math:`g(p)=[h_1(p),...,h_k(p)]`.
The algorithm constructs :math:`L` hash tables, each of which
corresponds to a different randomly chosen hash function :math:`g`.
There are :math:`k \\cdot L` hash functions used in total.
- In the preprocessing step, we hash all :math:`n` reference points
into each of the :math:`L` hash tables.
- Given a query point :math:`q`, the algorithm iterates over the
:math:`L` hash functions :math:`g`. For each :math:`g` considered, it
retrieves the data points that are hashed into the same bucket as q.
These data points from all the :math:`L` hash tables are considered as
candidates that are then re-ranked by their real distances with the query
data.
**Note** that the number of tables :math:`L` and the number of hash
functions per table :math:`k` are two main parameters. They can be set
using the options ``num_tables`` and ``num_projections_per_table``
respectively.
Hash functions for different distances:
- `euclidean` and `squared_euclidean`:
:math:`h(q) = \\lfloor \\frac{a \\cdot q + b}{w} \\rfloor` where
:math:`a` is a vector, of which the elements are independently
sampled from normal distribution, and :math:`b` is a number
uniformly sampled from :math:`[0, r]`. :math:`r` is a parameter for the
bucket width. We set :math:`r` using the average all-pair `euclidean`
distances from a small randomly sampled subset of the reference data.
- `manhattan`: The hash function of `manhattan` is similar with that of
`euclidean`. The only difference is that the elements of `a` are sampled
from Cauchy distribution, instead of normal distribution.
- `cosine`: Random Projection is designed to approximate the cosine
distance between vectors. The hash function is :math:`h(q) = sgn(a \\cdot
q)`, where :math:`a` is randomly sampled normal unit vector.
- `jaccard`: We use a recently proposed method one permutation hashing by
Shrivastava and Li. See the paper `[Shrivastava and Li, UAI 2014]
<http://www.auai.org/uai2014/proceedings/individuals/225.pdf>`_ for
details.
- `dot_product`: The reference data points are first transformed to
fixed-norm vectors, and then the minimum `dot_product` distance search
problem can be solved via finding the reference data with smallest
`cosine` distances. See the paper `[Neyshabur and Srebro, ICML 2015]
<http://proceedings.mlr.press/v37/neyshabur15.html>`_ for details.
References
----------
- `Wikipedia - nearest neighbor
search <http://en.wikipedia.org/wiki/Nearest_neighbor_search>`_
- `Wikipedia - ball tree <http://en.wikipedia.org/wiki/Ball_tree>`_
- Ball tree implementation: Liu, T., et al. (2004) `An Investigation of
Practical Approximate Nearest Neighbor Algorithms
<http://papers.nips.cc/paper/2666-an-investigation-of-p
ractical-approximat e-nearest-neighbor-algorithms>`_. Advances in Neural
Information Processing Systems pp. 825-832.
- `Wikipedia - Jaccard distance
<http://en.wikipedia.org/wiki/Jaccard_index>`_
- Weighted Jaccard distance: Chierichetti, F., et al. (2010) `Finding the
Jaccard Median
<http://theory.stanford.edu/~sergei/papers/soda10-jaccard.pdf>`_.
Proceedings of the Twenty-First Annual ACM-SIAM Symposium on Discrete
Algorithms. Society for Industrial and Applied Mathematics.
- `Wikipedia - Cosine distance
<http://en.wikipedia.org/wiki/Cosine_similarity>`_
- `Wikipedia - Levenshtein distance
<http://en.wikipedia.org/wiki/Levenshtein_distance>`_
- Locality Sensitive Hashing : Chapter 3 of the book `Mining Massive
Datasets <http://infolab.stanford.edu/~ullman/mmds/ch3.pdf>`_.
Examples
--------
Construct a nearest neighbors model with automatically determined method
and distance:
>>> sf = turicreate.SFrame({'X1': [0.98, 0.62, 0.11],
... 'X2': [0.69, 0.58, 0.36],
... 'str_feature': ['cat', 'dog', 'fossa']})
>>> model = turicreate.nearest_neighbors.create(sf, features=['X1', 'X2'])
For datasets with a large number of rows and up to about 100 variables, the
ball tree method often leads to much faster queries.
>>> model = turicreate.nearest_neighbors.create(sf, features=['X1', 'X2'],
... method='ball_tree')
Often the final determination of a neighbor is based on several distance
computations over different sets of features. Each part of this composite
distance may have a different relative weight.
>>> my_dist = [[['X1', 'X2'], 'euclidean', 2.],
... [['str_feature'], 'levenshtein', 3.]]
...
>>> model = turicreate.nearest_neighbors.create(sf, distance=my_dist) |
def watch_login(status_code=302, msg='',
get_username=utils.get_username_from_request):
"""
Used to decorate the django.contrib.admin.site.login method or
any other function you want to protect by brute forcing.
To make it work on normal functions just pass the status code that should
indicate a failure and/or a string that will be checked within the
response body.
"""
def decorated_login(func):
@functools.wraps(func)
def wrapper(request, *args, **kwargs):
# if the request is currently under lockout, do not proceed to the
# login function, go directly to lockout url, do not pass go,
# do not collect messages about this login attempt
if utils.is_already_locked(request):
return utils.lockout_response(request)
# call the login function
response = func(request, *args, **kwargs)
if request.method == 'POST':
# see if the login was successful
if status_code == 302: # standard Django login view
login_unsuccessful = (
response and
not response.has_header('location') and
response.status_code != status_code
)
else:
# If msg is not passed the last condition will be evaluated
# always to True so the first 2 will decide the result.
login_unsuccessful = (
response and response.status_code == status_code
and msg in response.content.decode('utf-8')
)
# ideally make this background task, but to keep simple,
# keeping it inline for now.
utils.add_login_attempt_to_db(request, not login_unsuccessful,
get_username)
if utils.check_request(request, login_unsuccessful,
get_username):
return response
return utils.lockout_response(request)
return response
return wrapper
return decorated_login | Used to decorate the django.contrib.admin.site.login method or
any other function you want to protect by brute forcing.
To make it work on normal functions just pass the status code that should
indicate a failure and/or a string that will be checked within the
response body. |
def clear_alert_destination(self, destination=0, channel=None):
"""Clear an alert destination
Remove the specified alert destination configuration.
:param destination: The destination to clear (defaults to 0)
"""
if channel is None:
channel = self.get_network_channel()
self.set_alert_destination(
'0.0.0.0', False, 0, 0, destination, channel) | Clear an alert destination
Remove the specified alert destination configuration.
:param destination: The destination to clear (defaults to 0) |
def add_lvl_to_ui(self, level, header):
"""Insert the level and header into the ui.
:param level: a newly created level
:type level: :class:`jukeboxcore.gui.widgets.browser.AbstractLevel`
:param header: a newly created header
:type header: QtCore.QWidget|None
:returns: None
:rtype: None
:raises: None
"""
lay = self.layout()
rc = lay.rowCount()
lay.addWidget(level, rc+1, 1)
if header is not None:
lay.addWidget(header, rc+1, 0)
lay.setColumnStretch(1,1) | Insert the level and header into the ui.
:param level: a newly created level
:type level: :class:`jukeboxcore.gui.widgets.browser.AbstractLevel`
:param header: a newly created header
:type header: QtCore.QWidget|None
:returns: None
:rtype: None
:raises: None |
def get_atom_feed_entry(self, feedentry_id):
"""
Get a specific feed entry
:param id: id of the feed entry to retrieve
:return: the feed entry
"""
return self.session.query(self.feedentry_model).filter(
self.feedentry_model.id == feedentry_id
).one() | Get a specific feed entry
:param id: id of the feed entry to retrieve
:return: the feed entry |
def create(source,
requirement_files=None,
force=False,
keep_wheels=False,
archive_destination_dir='.',
python_versions=None,
validate_archive=False,
wheel_args='',
archive_format='zip',
build_tag=''):
"""Create a Wagon archive and returns its path.
Package name and version are extracted from the setup.py file
of the `source` or from the PACKAGE_NAME==PACKAGE_VERSION if the source
is a PyPI package.
Supported `python_versions` must be in the format e.g [33, 27, 2, 3]..
`force` will remove any excess dirs or archives before creation.
`requirement_files` can be either a link/local path to a
requirements.txt file or just `.`, in which case requirement files
will be automatically extracted from either the GitHub archive URL
or the local path provided provided in `source`.
"""
if validate_archive:
_assert_virtualenv_is_installed()
logger.info('Creating archive for %s...', source)
processed_source = get_source(source)
if os.path.isdir(processed_source) and not \
os.path.isfile(os.path.join(processed_source, 'setup.py')):
raise WagonError(
'Source directory must contain a setup.py file')
package_name, package_version = get_source_name_and_version(
processed_source)
tempdir = tempfile.mkdtemp()
workdir = os.path.join(tempdir, package_name)
wheels_path = os.path.join(workdir, DEFAULT_WHEELS_PATH)
try:
wheels = wheel(
processed_source,
requirement_files,
wheels_path,
wheel_args)
finally:
if processed_source != source:
shutil.rmtree(processed_source, ignore_errors=True)
platform = _get_platform_for_set_of_wheels(wheels_path)
if is_verbose():
logger.debug('Platform is: %s', platform)
python_versions = _set_python_versions(python_versions)
if not os.path.isdir(archive_destination_dir):
os.makedirs(archive_destination_dir)
archive_name = _set_archive_name(
package_name, package_version, python_versions, platform, build_tag)
archive_path = os.path.join(archive_destination_dir, archive_name)
_handle_output_file(archive_path, force)
_generate_metadata_file(
workdir,
archive_name,
platform,
python_versions,
package_name,
package_version,
build_tag,
source,
wheels)
_create_wagon_archive(workdir, archive_path, archive_format)
if not keep_wheels:
logger.debug('Removing work directory...')
shutil.rmtree(tempdir, ignore_errors=True)
if validate_archive:
validate(archive_path)
logger.info('Wagon created successfully at: %s', archive_path)
return archive_path | Create a Wagon archive and returns its path.
Package name and version are extracted from the setup.py file
of the `source` or from the PACKAGE_NAME==PACKAGE_VERSION if the source
is a PyPI package.
Supported `python_versions` must be in the format e.g [33, 27, 2, 3]..
`force` will remove any excess dirs or archives before creation.
`requirement_files` can be either a link/local path to a
requirements.txt file or just `.`, in which case requirement files
will be automatically extracted from either the GitHub archive URL
or the local path provided provided in `source`. |
def generate_item_instances(cls, items, mediawiki_api_url='https://www.wikidata.org/w/api.php', login=None,
user_agent=config['USER_AGENT_DEFAULT']):
"""
A method which allows for retrieval of a list of Wikidata items or properties. The method generates a list of
tuples where the first value in the tuple is the QID or property ID, whereas the second is the new instance of
WDItemEngine containing all the data of the item. This is most useful for mass retrieval of WD items.
:param items: A list of QIDs or property IDs
:type items: list
:param mediawiki_api_url: The MediaWiki url which should be used
:type mediawiki_api_url: str
:param login: An object of type WDLogin, which holds the credentials/session cookies required for >50 item bulk
retrieval of items.
:type login: wdi_login.WDLogin
:return: A list of tuples, first value in the tuple is the QID or property ID string, second value is the
instance of WDItemEngine with the corresponding item data.
"""
assert type(items) == list
url = mediawiki_api_url
params = {
'action': 'wbgetentities',
'ids': '|'.join(items),
'format': 'json'
}
headers = {
'User-Agent': user_agent
}
if login:
reply = login.get_session().get(url, params=params, headers=headers)
else:
reply = requests.get(url, params=params)
item_instances = []
for qid, v in reply.json()['entities'].items():
ii = cls(wd_item_id=qid, item_data=v)
ii.mediawiki_api_url = mediawiki_api_url
item_instances.append((qid, ii))
return item_instances | A method which allows for retrieval of a list of Wikidata items or properties. The method generates a list of
tuples where the first value in the tuple is the QID or property ID, whereas the second is the new instance of
WDItemEngine containing all the data of the item. This is most useful for mass retrieval of WD items.
:param items: A list of QIDs or property IDs
:type items: list
:param mediawiki_api_url: The MediaWiki url which should be used
:type mediawiki_api_url: str
:param login: An object of type WDLogin, which holds the credentials/session cookies required for >50 item bulk
retrieval of items.
:type login: wdi_login.WDLogin
:return: A list of tuples, first value in the tuple is the QID or property ID string, second value is the
instance of WDItemEngine with the corresponding item data. |
def getColorMapAsDiscreetSLD(self, uniqueValues, nodata=-9999):
"""
Create the color map SLD format from a list of values.
:rtype: str
"""
colorMap = ET.Element('ColorMap', type='values')
# Add a line for the no-data values (nv)
ET.SubElement(colorMap, 'ColorMapEntry', color='#000000', quantity=str(nodata), label='NoData', opacity='0.0')
for value in uniqueValues:
red, green, blue = self.getColorForValue(value)
hexRGB = '#%02X%02X%02X' % (red,
green,
blue)
ET.SubElement(colorMap, 'ColorMapEntry', color=hexRGB, quantity=str(value), label=str(value), opacity=str(self.alpha))
return ET.tostring(colorMap) | Create the color map SLD format from a list of values.
:rtype: str |
def _populate_trie(self, values: List[str]) -> CharTrie:
"""Takes a list and inserts its elements into a new trie and returns it"""
if self._default_tokenizer:
return reduce(self._populate_trie_reducer, iter(values), CharTrie())
return reduce(self._populate_trie_reducer_regex, iter(values), CharTrie()) | Takes a list and inserts its elements into a new trie and returns it |
def get_bhavcopy_url(self, d):
"""take date and return bhavcopy url"""
d = parser.parse(d).date()
day_of_month = d.strftime("%d")
mon = d.strftime("%b").upper()
year = d.year
url = self.bhavcopy_base_url % (year, mon, day_of_month, mon, year)
return url | take date and return bhavcopy url |
def isRunActive(g):
"""
Polls the data server to see if a run is active
"""
if g.cpars['hcam_server_on']:
url = g.cpars['hipercam_server'] + 'summary'
response = urllib.request.urlopen(url, timeout=2)
rs = ReadServer(response.read(), status_msg=True)
if not rs.ok:
raise DriverError('isRunActive error: ' + str(rs.err))
if rs.state == 'idle':
return False
elif rs.state == 'active':
return True
else:
raise DriverError('isRunActive error, state = ' + rs.state)
else:
raise DriverError('isRunActive error: servers are not active') | Polls the data server to see if a run is active |
def on_connect(self, client, userdata, flags, rc):
'''
Callback for when the client receives a ``CONNACK`` response from the
broker.
Parameters
----------
client : paho.mqtt.client.Client
The client instance for this callback.
userdata : object
The private user data as set in :class:`paho.mqtt.client.Client`
constructor or :func:`paho.mqtt.client.Client.userdata_set`.
flags : dict
Response flags sent by the broker.
The flag ``flags['session present']`` is useful for clients that
are using clean session set to 0 only.
If a client with clean session=0, that reconnects to a broker that
it has previously connected to, this flag indicates whether the
broker still has the session information for the client.
If 1, the session still exists.
rc : int
The connection result.
The value of rc indicates success or not:
- 0: Connection successful
- 1: Connection refused - incorrect protocol version
- 2: Connection refused - invalid client identifier
- 3: Connection refused - server unavailable
- 4: Connection refused - bad username or password
- 5: Connection refused - not authorised
- 6-255: Currently unused.
Notes
-----
Subscriptions should be defined in this method to ensure subscriptions
will be renewed upon reconnecting after a loss of connection.
'''
super(SerialDeviceManager, self).on_connect(client, userdata, flags, rc)
if rc == 0:
self.mqtt_client.subscribe('serial_device/+/connect')
self.mqtt_client.subscribe('serial_device/+/send')
self.mqtt_client.subscribe('serial_device/+/close')
self.mqtt_client.subscribe('serial_device/refresh_comports')
self.refresh_comports() | Callback for when the client receives a ``CONNACK`` response from the
broker.
Parameters
----------
client : paho.mqtt.client.Client
The client instance for this callback.
userdata : object
The private user data as set in :class:`paho.mqtt.client.Client`
constructor or :func:`paho.mqtt.client.Client.userdata_set`.
flags : dict
Response flags sent by the broker.
The flag ``flags['session present']`` is useful for clients that
are using clean session set to 0 only.
If a client with clean session=0, that reconnects to a broker that
it has previously connected to, this flag indicates whether the
broker still has the session information for the client.
If 1, the session still exists.
rc : int
The connection result.
The value of rc indicates success or not:
- 0: Connection successful
- 1: Connection refused - incorrect protocol version
- 2: Connection refused - invalid client identifier
- 3: Connection refused - server unavailable
- 4: Connection refused - bad username or password
- 5: Connection refused - not authorised
- 6-255: Currently unused.
Notes
-----
Subscriptions should be defined in this method to ensure subscriptions
will be renewed upon reconnecting after a loss of connection. |
def zone(self, name, dns_name=None, description=None):
"""Construct a zone bound to this client.
:type name: str
:param name: Name of the zone.
:type dns_name: str
:param dns_name:
(Optional) DNS name of the zone. If not passed, then calls to
:meth:`zone.create` will fail.
:type description: str
:param description:
(Optional) the description for the zone. If not passed, defaults
to the value of 'dns_name'.
:rtype: :class:`google.cloud.dns.zone.ManagedZone`
:returns: a new ``ManagedZone`` instance.
"""
return ManagedZone(name, dns_name, client=self, description=description) | Construct a zone bound to this client.
:type name: str
:param name: Name of the zone.
:type dns_name: str
:param dns_name:
(Optional) DNS name of the zone. If not passed, then calls to
:meth:`zone.create` will fail.
:type description: str
:param description:
(Optional) the description for the zone. If not passed, defaults
to the value of 'dns_name'.
:rtype: :class:`google.cloud.dns.zone.ManagedZone`
:returns: a new ``ManagedZone`` instance. |
def insert_chain(cur, chain, encoded_data=None):
"""Insert a chain into the cache.
Args:
cur (:class:`sqlite3.Cursor`):
An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement.
chain (iterable):
A collection of nodes. Chains in embedding act as one node.
encoded_data (dict, optional):
If a dictionary is provided, it will be populated with the serialized data. This is
useful for preventing encoding the same information many times.
Notes:
This function assumes that the nodes in chain are index-labeled.
"""
if encoded_data is None:
encoded_data = {}
if 'nodes' not in encoded_data:
encoded_data['nodes'] = json.dumps(sorted(chain), separators=(',', ':'))
if 'chain_length' not in encoded_data:
encoded_data['chain_length'] = len(chain)
insert = "INSERT OR IGNORE INTO chain(chain_length, nodes) VALUES (:chain_length, :nodes);"
cur.execute(insert, encoded_data) | Insert a chain into the cache.
Args:
cur (:class:`sqlite3.Cursor`):
An sqlite3 cursor. This function is meant to be run within a :obj:`with` statement.
chain (iterable):
A collection of nodes. Chains in embedding act as one node.
encoded_data (dict, optional):
If a dictionary is provided, it will be populated with the serialized data. This is
useful for preventing encoding the same information many times.
Notes:
This function assumes that the nodes in chain are index-labeled. |
def current_bed_temp(self):
"""Return current bed temperature for in-progress session."""
try:
bedtemps = self.intervals[0]['timeseries']['tempBedC']
num_temps = len(bedtemps)
if num_temps == 0:
return None
bedtemp = bedtemps[num_temps-1][1]
except KeyError:
bedtemp = None
return bedtemp | Return current bed temperature for in-progress session. |
def set_start_date(self, date):
"""Sets the start date.
arg: date (osid.calendaring.DateTime): the new date
raise: InvalidArgument - ``date`` is invalid
raise: NoAccess - ``Metadata.isReadOnly()`` is ``true``
raise: NullArgument - ``date`` is ``null``
*compliance: mandatory -- This method must be implemented.*
"""
if self.get_start_date_metadata().is_read_only():
raise errors.NoAccess()
if not self._is_valid_date_time(date, self.get_start_date_metadata()):
raise errors.InvalidArgument()
# self._my_map['startDate'] = self._get_date_map(date)
self._my_map['startDate'] = date | Sets the start date.
arg: date (osid.calendaring.DateTime): the new date
raise: InvalidArgument - ``date`` is invalid
raise: NoAccess - ``Metadata.isReadOnly()`` is ``true``
raise: NullArgument - ``date`` is ``null``
*compliance: mandatory -- This method must be implemented.* |
def acceptEdit(self):
"""
Accepts the current edit for this label.
"""
if not self._lineEdit:
return
self.setText(self._lineEdit.text())
self._lineEdit.hide()
if not self.signalsBlocked():
self.editingFinished.emit(self._lineEdit.text()) | Accepts the current edit for this label. |
def init_widget(self):
""" Initialize the underlying widget.
"""
super(AndroidTextClock, self).init_widget()
d = self.declaration
if d.format_12_hour:
self.set_format_12_hour(d.format_12_hour)
if d.format_24_hour:
self.set_format_24_hour(d.format_24_hour)
if d.time_zone:
self.set_time_zone(d.time_zone) | Initialize the underlying widget. |
def do_before_loop(self):
"""Called before the main daemon loop.
:return: None
"""
logger.info("I am the arbiter: %s", self.link_to_myself.name)
# If I am a spare, I do not have anything to do here...
if not self.is_master:
logger.debug("Waiting for my master death...")
return
# Arbiter check if some daemons need to be started
if not self.daemons_start(run_daemons=True):
self.request_stop(message="Some Alignak daemons did not started correctly.",
exit_code=4)
if not self.daemons_check():
self.request_stop(message="Some Alignak daemons cannot be checked.",
exit_code=4)
# Make a pause to let our started daemons get ready...
pause = max(1, max(self.conf.daemons_start_timeout, len(self.my_daemons) * 0.5))
if pause:
logger.info("Pausing %.2f seconds...", pause)
time.sleep(pause)
# Prepare and dispatch the monitored configuration
self.configuration_dispatch()
# Now we can get all initial broks for our satellites
_t0 = time.time()
self.get_initial_broks_from_satellites()
statsmgr.timer('broks.get-initial', time.time() - _t0)
# Now create the external commands manager
# We are a dispatcher: our role is to dispatch commands to the schedulers
self.external_commands_manager = ExternalCommandManager(
self.conf, 'dispatcher', self, self.conf.accept_passive_unknown_check_results,
self.conf.log_external_commands) | Called before the main daemon loop.
:return: None |
def checkSanity(cls, trust_root_string):
"""str -> bool
is this a sane trust root?
"""
trust_root = cls.parse(trust_root_string)
if trust_root is None:
return False
else:
return trust_root.isSane() | str -> bool
is this a sane trust root? |
def add_host_to_segment(ipaddress, name, description, network_address, auth, url):
''' Function to abstract existing add_scope_ip_function. Allows for use of network address rather than forcing human
to learn the scope_id
:param ipaddress:
:param name: name of the owner of this host
:param description: Description of the host
:param: network_address: network address of the target scope in format x.x.x.x/yy where x.x.x.x representents the
network address and yy represents the length of the subnet mask. Example: 10.50.0.0 255.255.255.0 would be written
as 10.50.0.0/24
:param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class
:param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass
:return:
:rtype:
'''
scope_id = get_scope_id(network_address, auth, url)
add_scope_ip(ipaddress, name, description, scope_id, auth,url) | Function to abstract existing add_scope_ip_function. Allows for use of network address rather than forcing human
to learn the scope_id
:param ipaddress:
:param name: name of the owner of this host
:param description: Description of the host
:param: network_address: network address of the target scope in format x.x.x.x/yy where x.x.x.x representents the
network address and yy represents the length of the subnet mask. Example: 10.50.0.0 255.255.255.0 would be written
as 10.50.0.0/24
:param auth: requests auth object #usually auth.creds from auth pyhpeimc.auth.class
:param url: base url of IMC RS interface #usually auth.url from pyhpeimc.auth.authclass
:return:
:rtype: |
def is_object(brain_or_object):
"""Check if the passed in object is a supported portal content object
:param brain_or_object: A single catalog brain or content object
:type brain_or_object: Portal Object
:returns: True if the passed in object is a valid portal content
"""
if is_portal(brain_or_object):
return True
if is_at_content(brain_or_object):
return True
if is_dexterity_content(brain_or_object):
return True
if is_brain(brain_or_object):
return True
return False | Check if the passed in object is a supported portal content object
:param brain_or_object: A single catalog brain or content object
:type brain_or_object: Portal Object
:returns: True if the passed in object is a valid portal content |
def stop_instance(self, instance_id):
"""Stops the instance gracefully.
:param str instance_id: instance identifier
"""
instance = self._load_instance(instance_id)
instance.terminate()
del self._instances[instance_id] | Stops the instance gracefully.
:param str instance_id: instance identifier |
def get_validator(filter_data):
"""
ask every matcher whether it can serve such filter data
:param filter_data:
:return:
"""
for matcher_type, m in matchers.items():
if hasattr(m, 'can_handle') and m.can_handle(filter_data):
filter_data = m.handle(filter_data)
return filter_data | ask every matcher whether it can serve such filter data
:param filter_data:
:return: |
def db_create(cls, impl, working_dir):
"""
Create a sqlite3 db at the given path.
Create all the tables and indexes we need.
Returns a db connection on success
Raises an exception on error
"""
global VIRTUALCHAIN_DB_SCRIPT
log.debug("Setup chain state in {}".format(working_dir))
path = config.get_snapshots_filename(impl, working_dir)
if os.path.exists( path ):
raise Exception("Database {} already exists")
lines = [l + ";" for l in VIRTUALCHAIN_DB_SCRIPT.split(";")]
con = sqlite3.connect(path, isolation_level=None, timeout=2**30)
for line in lines:
con.execute(line)
con.row_factory = StateEngine.db_row_factory
return con | Create a sqlite3 db at the given path.
Create all the tables and indexes we need.
Returns a db connection on success
Raises an exception on error |
def change_sample(self, old_samp_name, new_samp_name, new_site_name=None,
new_er_data=None, new_pmag_data=None, replace_data=False):
"""
Find actual data objects for sample and site.
Then call Sample class change method to update sample name and data..
"""
sample = self.find_by_name(old_samp_name, self.samples)
if not sample:
print('-W- {} is not a currently existing sample, so it cannot be updated'.format(old_samp_name))
return False
if new_site_name:
new_site = self.find_by_name(new_site_name, self.sites)
if not new_site:
print("""-W- {} is not a currently existing site.
Adding site named: {}""".format(new_site_name, new_site_name))#sample.site or '*empty*', sample)
new_site = self.add_site(new_site_name)
else:
new_site = None
sample.change_sample(new_samp_name, new_site, new_er_data, new_pmag_data, replace_data)
return sample | Find actual data objects for sample and site.
Then call Sample class change method to update sample name and data.. |
def add(ctx, short_name, uri, interval, buffer):
"""Add a subscription with a given short_name for a given uri
This command can be used to create subscriptions to receive new pieces
of vehicle data on the stream channel on a periodic basis. By default,
subscriptions are buffered and have a 5 second interval:
\b
$ wva subscriptions add speed vehicle/data/VehicleSpeed
$ wva subscriptions show speed
{'buffer': 'queue', 'interval': 5, 'uri': 'vehicle/data/VehicleSpeed'}
These parameters can be modified by the use of optional arguments:
$ wva subscriptions add rpm vehicle/data/EngineSpeed --interval 1 --buffer discard
$ wva subscriptions show rpm
{'buffer': 'discard', 'interval': 1, 'uri': 'vehicle/data/EngineSpeed'}
To view the data coming in as a result of these subscriptions, one can use
either 'wva subscriptions listen' or 'wva subscriptions graph <name>'.
"""
wva = get_wva(ctx)
subscription = wva.get_subscription(short_name)
subscription.create(uri, buffer, interval) | Add a subscription with a given short_name for a given uri
This command can be used to create subscriptions to receive new pieces
of vehicle data on the stream channel on a periodic basis. By default,
subscriptions are buffered and have a 5 second interval:
\b
$ wva subscriptions add speed vehicle/data/VehicleSpeed
$ wva subscriptions show speed
{'buffer': 'queue', 'interval': 5, 'uri': 'vehicle/data/VehicleSpeed'}
These parameters can be modified by the use of optional arguments:
$ wva subscriptions add rpm vehicle/data/EngineSpeed --interval 1 --buffer discard
$ wva subscriptions show rpm
{'buffer': 'discard', 'interval': 1, 'uri': 'vehicle/data/EngineSpeed'}
To view the data coming in as a result of these subscriptions, one can use
either 'wva subscriptions listen' or 'wva subscriptions graph <name>'. |
def collate_data(in_dir, extension='.csv', out_dir=None):
"""
Copy all csvs in nested directroy to single directory.
Function to copy all csvs from a directory, and place
them in a new directory.
Parameters
----------
in_dir : str
Input directory containing csv files in subfolders
extension : str
The extension that identifies your data files.
Defaults to '.csv'.
out_dir : str
Destination directory
Returns
-------
None
"""
if out_dir is None:
out_dir = './' + re.search('^\.(.*)', extension).groups(0)[0]
if not os.path.isdir(out_dir):
os.mkdir(out_dir)
for p, d, fs in os.walk(in_dir):
for f in fs:
if extension in f:
shutil.copy(p + '/' + f, out_dir + '/' + f)
return | Copy all csvs in nested directroy to single directory.
Function to copy all csvs from a directory, and place
them in a new directory.
Parameters
----------
in_dir : str
Input directory containing csv files in subfolders
extension : str
The extension that identifies your data files.
Defaults to '.csv'.
out_dir : str
Destination directory
Returns
-------
None |
def run(self, backend_args, archive_args=None, resume=False):
"""Run the backend with the given parameters.
The method will run the backend assigned to this job,
storing the fetched items in a Redis queue. The ongoing
status of the job, can be accessed through the property
`result`. When `resume` is set, the job will start from
the last execution, overwriting 'from_date' and 'offset'
parameters, if needed.
Setting to `True` the parameter `fetch_from_archive`, items can
be fetched from the archive assigned to this job.
Any exception during the execution of the process will
be raised.
:param backend_args: parameters used to un the backend
:param archive_args: archive arguments
:param resume: fetch items starting where the last
execution stopped
"""
args = backend_args.copy()
if archive_args:
self.initialize_archive_manager(archive_args['archive_path'])
if not resume:
max_date = backend_args.get('from_date', None)
offset = backend_args.get('offset', None)
if max_date:
max_date = datetime_to_utc(max_date).timestamp()
self._result = JobResult(self.job_id, self.task_id, self.backend, self.category,
None, max_date, 0, offset=offset,
nresumed=0)
else:
if self.result.max_date:
args['from_date'] = unixtime_to_datetime(self.result.max_date)
if self.result.offset:
args['offset'] = self.result.offset
self._result.nresumed += 1
for item in self._execute(args, archive_args):
self.conn.rpush(self.qitems, pickle.dumps(item))
self._result.nitems += 1
self._result.last_uuid = item['uuid']
if not self.result.max_date or self.result.max_date < item['updated_on']:
self._result.max_date = item['updated_on']
if 'offset' in item:
self._result.offset = item['offset'] | Run the backend with the given parameters.
The method will run the backend assigned to this job,
storing the fetched items in a Redis queue. The ongoing
status of the job, can be accessed through the property
`result`. When `resume` is set, the job will start from
the last execution, overwriting 'from_date' and 'offset'
parameters, if needed.
Setting to `True` the parameter `fetch_from_archive`, items can
be fetched from the archive assigned to this job.
Any exception during the execution of the process will
be raised.
:param backend_args: parameters used to un the backend
:param archive_args: archive arguments
:param resume: fetch items starting where the last
execution stopped |
def _set_nd_basic_indexing(self, key, value):
"""This function is called by __setitem__ when key is a basic index, i.e.
an integer, or a slice, or a tuple of integers and slices. No restrictions
on the values of slices' steps."""
shape = self.shape
if isinstance(key, integer_types):
if key < 0:
key += shape[0]
if key < 0 or key >= shape[0]:
if key < 0:
key -= shape[0]
raise IndexError('index %d is out of bounds for axis 0 with size %d'
% (key, shape[0]))
key = py_slice(key, key+1) # key must be >= 0 here
if isinstance(key, py_slice):
assign_to_self = key.step is None or key.step == 1
assign_to_self &= key.start is None or key.start == 0
assign_to_self &= key.stop is None or key.stop == shape[0]
if assign_to_self: # trivial case, assign value to self
if isinstance(value, NDArray):
if value.handle is not self.handle:
if value.shape != shape:
value = value.broadcast_to(shape)
value.copyto(self)
elif isinstance(value, numeric_types):
_internal._full(shape=shape, ctx=self.context,
dtype=self.dtype, value=float(value), out=self)
elif isinstance(value, (np.ndarray, np.generic)):
if isinstance(value, np.generic) or value.shape != shape:
value = np.broadcast_to(value, shape)
self._sync_copyfrom(value)
else: # value might be a list or a tuple
value_nd = self._prepare_value_nd(value, shape)
value_nd.copyto(self)
return
else: # non-trivial case, use _slice_assign or _slice_assign_scalar
key = (key,)
assert isinstance(key, tuple), "key=%s must be a tuple of slices and integers" % str(key)
assert len(key) <= len(shape), "Indexing dimensions exceed array dimensions, %d vs %d"\
% (len(key), len(shape))
begin = []
end = []
steps = []
oshape = [] # output shape of slice using key
vshape = [] # value shape of data[key]
for i, slice_i in enumerate(key):
dim_size = 1
if isinstance(slice_i, py_slice):
begin.append(slice_i.start)
end.append(slice_i.stop)
steps.append(slice_i.step)
start, stop, step = _get_index_range(slice_i.start, slice_i.stop,
shape[i], slice_i.step)
dim_size = _get_dim_size(start, stop, step)
vshape.append(dim_size)
elif isinstance(slice_i, integer_types):
begin.append(slice_i)
end.append(slice_i+1 if slice_i != -1 else self.shape[i])
steps.append(1)
else:
raise ValueError("basic indexing does not support index=%s of type=%s"
% (str(slice_i), str(type(slice_i))))
oshape.append(dim_size)
oshape.extend(shape[len(key):])
vshape.extend(shape[len(key):])
# if key contains all integers, vshape should be (1,)
if len(vshape) == 0:
vshape.append(1)
oshape = tuple(oshape)
vshape = tuple(vshape)
if isinstance(value, numeric_types):
_internal._slice_assign_scalar(self, out=self, begin=begin, end=end,
step=steps, scalar=float(value))
else:
value_nd = self._prepare_value_nd(value, vshape)
if vshape != oshape:
value_nd = value_nd.reshape(oshape)
_internal._slice_assign(self, value_nd, begin, end, steps, out=self) | This function is called by __setitem__ when key is a basic index, i.e.
an integer, or a slice, or a tuple of integers and slices. No restrictions
on the values of slices' steps. |
def resolve_font(name):
"""Turns font names into absolute filenames
This is case sensitive. The extension should be omitted.
For example::
>>> path = resolve_font('NotoSans-Bold')
>>> fontdir = os.path.join(os.path.dirname(__file__), 'fonts')
>>> noto_path = os.path.join(fontdir, 'NotoSans-Bold.ttf')
>>> noto_path = os.path.abspath(noto_path)
>>> assert path == noto_path
Absolute paths are allowed::
>>> resolve_font(noto_path) == noto_path
True
Raises :exc:`FontNotFound` on failure::
>>> try:
... resolve_font('blahahaha')
... assert False
... except FontNotFound:
... pass
"""
if os.path.exists(name):
return os.path.abspath(name)
fonts = get_font_files()
if name in fonts:
return fonts[name]
raise FontNotFound("Can't find %r :'( Try adding it to ~/.fonts" % name) | Turns font names into absolute filenames
This is case sensitive. The extension should be omitted.
For example::
>>> path = resolve_font('NotoSans-Bold')
>>> fontdir = os.path.join(os.path.dirname(__file__), 'fonts')
>>> noto_path = os.path.join(fontdir, 'NotoSans-Bold.ttf')
>>> noto_path = os.path.abspath(noto_path)
>>> assert path == noto_path
Absolute paths are allowed::
>>> resolve_font(noto_path) == noto_path
True
Raises :exc:`FontNotFound` on failure::
>>> try:
... resolve_font('blahahaha')
... assert False
... except FontNotFound:
... pass |
def compute_fov(self, x, y, fov='PERMISSIVE', radius=None,
light_walls=True, sphere=True, cumulative=False):
"""Compute the field-of-view of this Map and return an iterator of the
points touched.
Args:
x (int): Point of view, x-coordinate.
y (int): Point of view, y-coordinate.
fov (Text): The type of field-of-view to be used.
Available types are:
'BASIC', 'DIAMOND', 'SHADOW', 'RESTRICTIVE', 'PERMISSIVE',
'PERMISSIVE0', 'PERMISSIVE1', ..., 'PERMISSIVE8'
radius (Optional[int]): Maximum view distance from the point of
view.
A value of 0 will give an infinite distance.
light_walls (bool): Light up walls, or only the floor.
sphere (bool): If True the lit area will be round instead of
square.
cumulative (bool): If True the lit cells will accumulate instead
of being cleared before the computation.
Returns:
Iterator[Tuple[int, int]]: An iterator of (x, y) points of tiles
touched by the field-of-view.
"""
# refresh cdata
if radius is None: # infinite radius
radius = 0
if cumulative:
fov_copy = self.fov.copy()
lib.TCOD_map_compute_fov(
self.map_c, x, y, radius, light_walls, _get_fov_type(fov))
if cumulative:
self.fov[:] |= fov_copy
return zip(*np.where(self.fov)) | Compute the field-of-view of this Map and return an iterator of the
points touched.
Args:
x (int): Point of view, x-coordinate.
y (int): Point of view, y-coordinate.
fov (Text): The type of field-of-view to be used.
Available types are:
'BASIC', 'DIAMOND', 'SHADOW', 'RESTRICTIVE', 'PERMISSIVE',
'PERMISSIVE0', 'PERMISSIVE1', ..., 'PERMISSIVE8'
radius (Optional[int]): Maximum view distance from the point of
view.
A value of 0 will give an infinite distance.
light_walls (bool): Light up walls, or only the floor.
sphere (bool): If True the lit area will be round instead of
square.
cumulative (bool): If True the lit cells will accumulate instead
of being cleared before the computation.
Returns:
Iterator[Tuple[int, int]]: An iterator of (x, y) points of tiles
touched by the field-of-view. |
def split(self, sequence):
""" Split into subsequences according to `sequence`."""
major_idx = sequence.idx
idx2 = 0
for start, end in zip(major_idx[:-1], major_idx[1:]):
idx1 = self.idx.index(start, idx2)
idx2 = self.idx.index(end, idx2)
seq = Sequence(self.text[start:end])
seq.idx = [x-start for x in self.idx[idx1:idx2]]
yield seq | Split into subsequences according to `sequence`. |
def transform_to_length(nndata, length):
"""
Given NNData, transforms data to the specified fingerprint length
Args:
nndata: (NNData)
length: (int) desired length of NNData
"""
if length is None:
return nndata
if length:
for cn in range(length):
if cn not in nndata.cn_weights:
nndata.cn_weights[cn] = 0
nndata.cn_nninfo[cn] = []
return nndata | Given NNData, transforms data to the specified fingerprint length
Args:
nndata: (NNData)
length: (int) desired length of NNData |
def getEstablishments(self, city_id, **kwargs):
"""
:param city_id: id of the city for which collections are needed
:param lat: latitude
:param lon: longitude
Get a list of restaurant types in a city. The location/City input can be provided in the following ways
- Using Zomato City ID
- Using coordinates of any location within a city
List of all restaurants categorized under a particular restaurant type can obtained using
/Search API with Establishment ID and location details as inputs
"""
params = {"city_id": city_id}
optional_params = ["lat", "lon"]
for key in optional_params:
if key in kwargs:
params[key] = kwargs[key]
establishments = self.api.get("/establishments", params)
return establishments | :param city_id: id of the city for which collections are needed
:param lat: latitude
:param lon: longitude
Get a list of restaurant types in a city. The location/City input can be provided in the following ways
- Using Zomato City ID
- Using coordinates of any location within a city
List of all restaurants categorized under a particular restaurant type can obtained using
/Search API with Establishment ID and location details as inputs |
def parse_date(value):
"""Attempts to parse `value` into an instance of ``datetime.date``. If
`value` is ``None``, this function will return ``None``.
Args:
value: A timestamp. This can be a string, datetime.date, or
datetime.datetime value.
"""
if not value:
return None
if isinstance(value, datetime.date):
return value
return parse_datetime(value).date() | Attempts to parse `value` into an instance of ``datetime.date``. If
`value` is ``None``, this function will return ``None``.
Args:
value: A timestamp. This can be a string, datetime.date, or
datetime.datetime value. |
def parse_frequencies(variant, transcripts):
"""Add the frequencies to a variant
Frequencies are parsed either directly from keys in info fieds or from the
transcripts is they are annotated there.
Args:
variant(cyvcf2.Variant): A parsed vcf variant
transcripts(iterable(dict)): Parsed transcripts
Returns:
frequencies(dict): A dictionary with the relevant frequencies
"""
frequencies = {}
# These lists could be extended...
thousand_genomes_keys = ['1000GAF']
thousand_genomes_max_keys = ['1000G_MAX_AF']
exac_keys = ['EXACAF']
exac_max_keys = ['ExAC_MAX_AF', 'EXAC_MAX_AF']
gnomad_keys = ['GNOMADAF', 'GNOMAD_AF']
gnomad_max_keys = ['GNOMADAF_POPMAX', 'GNOMADAF_MAX']
for test_key in thousand_genomes_keys:
thousand_g = parse_frequency(variant, test_key)
if thousand_g:
frequencies['thousand_g'] = thousand_g
break
for test_key in thousand_genomes_max_keys:
thousand_g_max = parse_frequency(variant, test_key)
if thousand_g_max:
frequencies['thousand_g_max'] = thousand_g_max
break
for test_key in exac_keys:
exac = parse_frequency(variant, test_key)
if exac:
frequencies['exac'] = exac
break
for test_key in exac_max_keys:
exac_max = parse_frequency(variant, test_key)
if exac_max:
frequencies['exac_max'] = exac_max
break
for test_key in gnomad_keys:
gnomad = parse_frequency(variant, test_key)
if gnomad:
frequencies['gnomad'] = gnomad
break
for test_key in gnomad_max_keys:
gnomad_max = parse_frequency(variant, test_key)
if gnomad_max:
frequencies['gnomad_max'] = gnomad_max
break
# Search transcripts if not found in VCF
if not frequencies:
for transcript in transcripts:
exac = transcript.get('exac_maf')
exac_max = transcript.get('exac_max')
thousand_g = transcript.get('thousand_g_maf')
thousandg_max = transcript.get('thousandg_max')
gnomad = transcript.get('gnomad_maf')
gnomad_max = transcript.get('gnomad_max')
if exac:
frequencies['exac'] = exac
if exac_max:
frequencies['exac_max'] = exac_max
if thousand_g:
frequencies['thousand_g'] = thousand_g
if thousandg_max:
frequencies['thousand_g_max'] = thousandg_max
if gnomad:
frequencies['gnomad'] = gnomad
if gnomad_max:
frequencies['gnomad_max'] = gnomad_max
#These are SV-specific frequencies
thousand_g_left = parse_frequency(variant, 'left_1000GAF')
if thousand_g_left:
frequencies['thousand_g_left'] = thousand_g_left
thousand_g_right = parse_frequency(variant, 'right_1000GAF')
if thousand_g_right:
frequencies['thousand_g_right'] = thousand_g_right
return frequencies | Add the frequencies to a variant
Frequencies are parsed either directly from keys in info fieds or from the
transcripts is they are annotated there.
Args:
variant(cyvcf2.Variant): A parsed vcf variant
transcripts(iterable(dict)): Parsed transcripts
Returns:
frequencies(dict): A dictionary with the relevant frequencies |
def determine_coords(list_of_variable_dicts):
# type: (List[Dict]) -> Tuple[Set, Set]
"""Given a list of dicts with xarray object values, identify coordinates.
Parameters
----------
list_of_variable_dicts : list of dict or Dataset objects
Of the same form as the arguments to expand_variable_dicts.
Returns
-------
coord_names : set of variable names
noncoord_names : set of variable names
All variable found in the input should appear in either the set of
coordinate or non-coordinate names.
"""
from .dataarray import DataArray
from .dataset import Dataset
coord_names = set() # type: set
noncoord_names = set() # type: set
for variables in list_of_variable_dicts:
if isinstance(variables, Dataset):
coord_names.update(variables.coords)
noncoord_names.update(variables.data_vars)
else:
for name, var in variables.items():
if isinstance(var, DataArray):
coords = set(var._coords) # use private API for speed
# explicitly overwritten variables should take precedence
coords.discard(name)
coord_names.update(coords)
return coord_names, noncoord_names | Given a list of dicts with xarray object values, identify coordinates.
Parameters
----------
list_of_variable_dicts : list of dict or Dataset objects
Of the same form as the arguments to expand_variable_dicts.
Returns
-------
coord_names : set of variable names
noncoord_names : set of variable names
All variable found in the input should appear in either the set of
coordinate or non-coordinate names. |
def register_plugin(self, name):
"""Load and register a plugin given its package name."""
logger.info("Registering plugin: " + name)
module = importlib.import_module(name)
module.register_plugin(self) | Load and register a plugin given its package name. |
def _pages_to_generate(self):
'''Return list of slugs that correspond to pages to generate.'''
# right now it gets all the files. In theory, It should only
# get what's changed... but the program is not doing that yet.
all_pages = self.get_page_names()
# keep only those whose status is published
ptg = []
for slug in all_pages:
p = s2page.Page(self, slug, isslug=True)
if p.published:
ptg.append({'slug': p.slug, 'title':p.title, 'date': p.creation_date })
# sort the ptg array in reverse chronological order of its entries.
sptg = sorted(ptg, key=lambda x : x['date'],reverse=True)
res = [ pinfo['slug'] for pinfo in sptg]
return res | Return list of slugs that correspond to pages to generate. |
def pinfo(self,obj,oname='',formatter=None,info=None,detail_level=0):
"""Show detailed information about an object.
Optional arguments:
- oname: name of the variable pointing to the object.
- formatter: special formatter for docstrings (see pdoc)
- info: a structure with some information fields which may have been
precomputed already.
- detail_level: if set to 1, more information is given.
"""
info = self.info(obj, oname=oname, formatter=formatter,
info=info, detail_level=detail_level)
displayfields = []
def add_fields(fields):
for title, key in fields:
field = info[key]
if field is not None:
displayfields.append((title, field.rstrip()))
add_fields(self.pinfo_fields1)
# Base class for old-style instances
if (not py3compat.PY3) and isinstance(obj, types.InstanceType) and info['base_class']:
displayfields.append(("Base Class", info['base_class'].rstrip()))
add_fields(self.pinfo_fields2)
# Namespace
if info['namespace'] != 'Interactive':
displayfields.append(("Namespace", info['namespace'].rstrip()))
add_fields(self.pinfo_fields3)
# Source or docstring, depending on detail level and whether
# source found.
if detail_level > 0 and info['source'] is not None:
displayfields.append(("Source", self.format(py3compat.cast_bytes_py2(info['source']))))
elif info['docstring'] is not None:
displayfields.append(("Docstring", info["docstring"]))
# Constructor info for classes
if info['isclass']:
if info['init_definition'] or info['init_docstring']:
displayfields.append(("Constructor information", ""))
if info['init_definition'] is not None:
displayfields.append((" Definition",
info['init_definition'].rstrip()))
if info['init_docstring'] is not None:
displayfields.append((" Docstring",
indent(info['init_docstring'])))
# Info for objects:
else:
add_fields(self.pinfo_fields_obj)
# Finally send to printer/pager:
if displayfields:
page.page(self._format_fields(displayfields)) | Show detailed information about an object.
Optional arguments:
- oname: name of the variable pointing to the object.
- formatter: special formatter for docstrings (see pdoc)
- info: a structure with some information fields which may have been
precomputed already.
- detail_level: if set to 1, more information is given. |
def connect(token, protocol=RtmProtocol, factory=WebSocketClientFactory, factory_kwargs=None, api_url=None, debug=False):
"""
Creates a new connection to the Slack Real-Time API.
Returns (connection) which represents this connection to the API server.
"""
if factory_kwargs is None:
factory_kwargs = dict()
metadata = request_session(token, api_url)
wsfactory = factory(metadata.url, **factory_kwargs)
if debug:
warnings.warn('debug=True has been deprecated in autobahn 0.14.0')
wsfactory.protocol = lambda *a,**k: protocol(*a,**k)._seedMetadata(metadata)
connection = connectWS(wsfactory)
return connection | Creates a new connection to the Slack Real-Time API.
Returns (connection) which represents this connection to the API server. |
def _updown(self, direction):
"""Provides curses scroll functionality.
"""
if direction == "up" and self.top_line != 0:
self.top_line -= 1
elif direction == "down" and \
self.screen.getmaxyx()[0] + self.top_line\
<= self.content_lines + 3:
self.top_line += 1 | Provides curses scroll functionality. |
def get_user_information():
"""
Returns the user's information
:rtype: (str, int, str)
"""
try:
import pwd
_username = pwd.getpwuid(os.getuid())[0]
_userid = os.getuid()
_uname = os.uname()[1]
except ImportError:
import getpass
_username = getpass.getuser()
_userid = 0
import platform
_uname = platform.node()
return _username, _userid, _uname | Returns the user's information
:rtype: (str, int, str) |
def make_backup_files(*,
mongodump=MONGODB_DEFAULT_MONGODUMP,
hosts={},
host_defaults={},
dry_run=False,
**kwargs):
"""
Backup all specified databases into a gzipped tarball via mongodump
:param mongodump(str, optional): Path to mongodump executable
:param hosts(dict, optional): A dict containing hosts info to be backed up
:param host_defaults(dict, optional): Default values applied to each host
:param dry_run(bool, optional): Whether to activate dry run mode
:param \*\*kwargs: arbitrary keyword arguments
:raises TypeError: if an argument in kwargs does not have the type expected
:raises ValueError: if an argument within kwargs has an invalid value
"""
# Output directory
output_dir = fs.get_output_dir()
# Type checks
utils.chkstr(mongodump, 'mongodump')
# List of mongodb hosts holding databases
mongodb_hosts = hosts
if len(mongodb_hosts) == 0:
raise ValueError("No mongodb 'hosts' specified!")
# Default values applied to each host if necessary,
# this setting takes effect on a 'cascade' fashion.
mongodb_defaults = host_defaults
# Collected list of files generated by _make_backup_file
mongodump_files = {}
# Backup cycle for each mongodb host
for mongodb_host_name, mongodb_host in mongodb_hosts.items():
if type(mongodb_host) != dict:
raise TypeError("mongodb_host '{name}' must be dict"
.format(name=mongodb_host_name))
# For each host, its 'address' is essential
if 'address' not in mongodb_host:
raise KeyError("No 'address' specified!")
utils.chkstr(mongodb_host['address'], 'address')
"""
For these keys, the value is going to be set in
in a 'cascade' fashion, if the value is not already set,
its value is going to be picked from the 'defaults'(mongodb_defaults)
sub-section within 'mongodb', as a last resort, its hardcoded default
value is going to be picked.
"""
# TODO: there has to be something better than this
_set_mongodb_host_val('user_name', MONGODB_DEFAULT_USER,
mongodb_host, mongodb_defaults)
_set_mongodb_host_val('password', MONGODB_DEFAULT_PWD,
mongodb_host, mongodb_defaults)
_set_mongodb_host_val('port', MONGODB_DEFAULT_PORT,
mongodb_host, mongodb_defaults)
_set_mongodb_host_val('auth_db', MONGODB_DEFAULT_AUTH,
mongodb_host, mongodb_defaults)
"""Merge dbs list with that of the host_defaults section (if any)"""
if 'dbs' in mongodb_defaults:
if 'dbs' in mongodb_host:
mongodb_host['dbs'] = _merge_dbs(mongodb_defaults['dbs'],
mongodb_host['dbs'])
else:
mongodb_host['dbs'] = mongodb_defaults['dbs']
# Add the file name to the list to be returned
mongodump_files[mongodb_host_name] = _make_backup_file(dry_run=dry_run, mongodump=mongodump,
output_dir=output_dir, name=mongodb_host_name,
**mongodb_host)
# .. and finally, give it
return mongodump_files | Backup all specified databases into a gzipped tarball via mongodump
:param mongodump(str, optional): Path to mongodump executable
:param hosts(dict, optional): A dict containing hosts info to be backed up
:param host_defaults(dict, optional): Default values applied to each host
:param dry_run(bool, optional): Whether to activate dry run mode
:param \*\*kwargs: arbitrary keyword arguments
:raises TypeError: if an argument in kwargs does not have the type expected
:raises ValueError: if an argument within kwargs has an invalid value |
def _handle_continue(self, node, scope, ctxt, stream):
"""Handle continue node
:node: TODO
:scope: TODO
:ctxt: TODO
:stream: TODO
:returns: TODO
"""
self._dlog("handling continue")
raise errors.InterpContinue() | Handle continue node
:node: TODO
:scope: TODO
:ctxt: TODO
:stream: TODO
:returns: TODO |
def load(self, orbit=None):
"""Load a particular orbit into .data for loaded day.
Parameters
----------
orbit : int
orbit number, 1 indexed
Note
----
A day of data must be loaded before this routine functions properly.
If the last orbit of the day is requested, it will automatically be
padded with data from the next day. The orbit counter will be
reset to 1.
"""
if not self.sat.empty: # ensure data exists
# set up orbit metadata
self._calcOrbits()
# ensure user supplied an orbit
if orbit is not None:
# pull out requested orbit
if orbit < 0:
# negative indexing consistent with numpy, -1 last,
# -2 second to last, etc.
orbit = self.num + 1 + orbit
if orbit == 1:
# change from orig copied from _core, didn't look correct.
# self._getBasicOrbit(orbit=2)
try:
true_date = self.sat.date # .copy()
self.sat.prev()
# if and else added becuase of CINDI turn off
# 6/5/2013, turn on 10/22/2014
# crashed when starting on 10/22/2014
# prev returned empty data
if not self.sat.empty:
self.load(orbit=-1)
else:
self.sat.next()
self._getBasicOrbit(orbit=1)
# check that this orbit should end on the current day
delta = true_date - self.sat.data.index[0]
# print 'checking if first orbit should land on requested day'
# print self.sat.date, self.sat.data.index[0], delta, delta >= self.orbit_period
if delta >= self.orbit_period:
# the orbit loaded isn't close enough to date
# to be the first orbit of the day, move forward
self.next()
except StopIteration:
# print 'going for basic orbit'
self._getBasicOrbit(orbit=1)
# includes hack to appear to be zero indexed
print('Loaded Orbit:%i' % (self._current - 1))
# check if the first orbit is also the last orbit
elif orbit == self.num:
# we get here if user asks for last orbit
# make sure that orbit data goes across daybreak as needed
# load previous orbit
if self.num != 1:
self._getBasicOrbit(self.num - 1)
self.next()
else:
self._getBasicOrbit(orbit=-1)
elif orbit < self.num:
# load orbit data into data
self._getBasicOrbit(orbit)
# includes hack to appear to be zero indexed
print('Loaded Orbit:%i' % (self._current - 1))
else:
# gone too far
self.sat.data = DataFrame()
raise Exception('Requested an orbit past total orbits for day')
else:
raise Exception('Must set an orbit')
else:
print('No data loaded in instrument object to determine orbits.') | Load a particular orbit into .data for loaded day.
Parameters
----------
orbit : int
orbit number, 1 indexed
Note
----
A day of data must be loaded before this routine functions properly.
If the last orbit of the day is requested, it will automatically be
padded with data from the next day. The orbit counter will be
reset to 1. |
def load_sanitizers(self, config_data):
"""
Loads sanitizers possibly defined in the configuration under dictionary
called "strategy", which should contain mapping of database tables with
column names mapped into sanitizer function names.
:param config_data: Already parsed configuration data, as dictionary.
:type config_data: dict[str,any]
"""
section_strategy = config_data.get("strategy")
if not isinstance(section_strategy, dict):
if section_strategy is None:
return
raise ConfigurationError(
"'strategy' is %s instead of dict" % (
type(section_strategy),
),
)
for table_name, column_data in six.iteritems(section_strategy):
if not isinstance(column_data, dict):
if column_data is None:
continue
raise ConfigurationError(
"'strategy.%s' is %s instead of dict" % (
table_name,
type(column_data),
),
)
for column_name, sanitizer_name in six.iteritems(column_data):
if sanitizer_name is None:
continue
if not isinstance(sanitizer_name, six.text_type):
raise ConfigurationError(
"'strategy.%s.%s' is %s instead of string" % (
table_name,
column_name,
type(sanitizer_name),
),
)
sanitizer_callback = self.find_sanitizer(sanitizer_name)
sanitizer_key = "%s.%s" % (table_name, column_name)
self.sanitizers[sanitizer_key] = sanitizer_callback | Loads sanitizers possibly defined in the configuration under dictionary
called "strategy", which should contain mapping of database tables with
column names mapped into sanitizer function names.
:param config_data: Already parsed configuration data, as dictionary.
:type config_data: dict[str,any] |
def get_client(quiet=False, debug=False):
'''
get the client and perform imports not on init, in case there are any
initialization or import errors.
Parameters
==========
quiet: if True, suppress most output about the client
debug: turn on debugging mode
'''
from spython.utils import get_singularity_version
from .base import Client
Client.quiet = quiet
Client.debug = debug
# Do imports here, can be customized
from .apps import apps
from .build import build
from .execute import execute
from .help import help
from .inspect import inspect
from .instances import ( instances, stopall ) # global instance commands
from .run import run
from .pull import pull
# Actions
Client.apps = apps
Client.build = build
Client.execute = execute
Client.help = help
Client.inspect = inspect
Client.instances = instances
Client.run = run
Client.pull = pull
# Command Groups, Images
from spython.image.cmd import generate_image_commands # deprecated
Client.image = generate_image_commands()
# Commands Groups, Instances
from spython.instance.cmd import generate_instance_commands # instance level commands
Client.instance = generate_instance_commands()
Client.instance_stopall = stopall
Client.instance.version = Client.version
# Commands Groups, OCI (Singularity version 3 and up)
if "version 3" in get_singularity_version():
from spython.oci.cmd import generate_oci_commands
Client.oci = generate_oci_commands()() # first () runs function, second
# initializes OciImage class
Client.oci.debug = Client.debug
Client.oci.quiet = Client.quiet
Client.oci.OciImage.quiet = Client.quiet
Client.oci.OciImage.debug = Client.debug
# Initialize
cli = Client()
# Pass on verbosity
cli.image.debug = cli.debug
cli.image.quiet = cli.quiet
cli.instance.debug = cli.debug
cli.instance.quiet = cli.quiet
return cli | get the client and perform imports not on init, in case there are any
initialization or import errors.
Parameters
==========
quiet: if True, suppress most output about the client
debug: turn on debugging mode |
def clinvar_export(store, institute_id, case_name, variant_id):
"""Gather the required data for creating the clinvar submission form
Args:
store(scout.adapter.MongoAdapter)
institute_id(str): Institute ID
case_name(str): case ID
variant_id(str): variant._id
Returns:
a dictionary with all the required data (case and variant level) to pre-fill in fields in the clinvar submission form
"""
institute_obj, case_obj = institute_and_case(store, institute_id, case_name)
pinned = [store.variant(variant_id) or variant_id for variant_id in
case_obj.get('suspects', [])]
variant_obj = store.variant(variant_id)
return dict(
today = str(date.today()),
institute=institute_obj,
case=case_obj,
variant=variant_obj,
pinned_vars=pinned
) | Gather the required data for creating the clinvar submission form
Args:
store(scout.adapter.MongoAdapter)
institute_id(str): Institute ID
case_name(str): case ID
variant_id(str): variant._id
Returns:
a dictionary with all the required data (case and variant level) to pre-fill in fields in the clinvar submission form |
def sam_list(sam):
"""
get a list of mapped reads
"""
list = []
for file in sam:
for line in file:
if line.startswith('@') is False:
line = line.strip().split()
id, map = line[0], int(line[1])
if map != 4 and map != 8:
list.append(id)
return set(list) | get a list of mapped reads |
def assert_conditions(self):
"""Handles various HTTP conditions and raises HTTP exceptions to
abort the request.
- Content-MD5 request header must match the MD5 hash of the full
input (:func:`assert_condition_md5`).
- If-Match and If-None-Match etags are checked against the ETag of
this resource (:func:`assert_condition_etag`).
- If-Modified-Since and If-Unmodified-Since are checked against
the modification date of this resource
(:func:`assert_condition_last_modified`).
.. todo:: Return a 501 exception when any Content-* headers have been
set in the request. (See :rfc:`2616`, section 9.6)
"""
self.assert_condition_md5()
etag = self.clean_etag(self.call_method('get_etag'))
self.response.last_modified = self.call_method('get_last_modified')
self.assert_condition_etag()
self.assert_condition_last_modified() | Handles various HTTP conditions and raises HTTP exceptions to
abort the request.
- Content-MD5 request header must match the MD5 hash of the full
input (:func:`assert_condition_md5`).
- If-Match and If-None-Match etags are checked against the ETag of
this resource (:func:`assert_condition_etag`).
- If-Modified-Since and If-Unmodified-Since are checked against
the modification date of this resource
(:func:`assert_condition_last_modified`).
.. todo:: Return a 501 exception when any Content-* headers have been
set in the request. (See :rfc:`2616`, section 9.6) |
def validate_init_args_statically(distribution, batch_shape):
"""Helper to __init__ which makes or raises assertions."""
if tensorshape_util.rank(batch_shape.shape) is not None:
if tensorshape_util.rank(batch_shape.shape) != 1:
raise ValueError("`batch_shape` must be a vector "
"(saw rank: {}).".format(
tensorshape_util.rank(batch_shape.shape)))
batch_shape_static = tensorshape_util.constant_value_as_shape(batch_shape)
batch_size_static = tensorshape_util.num_elements(batch_shape_static)
dist_batch_size_static = tensorshape_util.num_elements(
distribution.batch_shape)
if batch_size_static is not None and dist_batch_size_static is not None:
if batch_size_static != dist_batch_size_static:
raise ValueError("`batch_shape` size ({}) must match "
"`distribution.batch_shape` size ({}).".format(
batch_size_static, dist_batch_size_static))
if tensorshape_util.dims(batch_shape_static) is not None:
if any(
tf.compat.dimension_value(dim) is not None and
tf.compat.dimension_value(dim) < 1 for dim in batch_shape_static):
raise ValueError("`batch_shape` elements must be >=-1.") | Helper to __init__ which makes or raises assertions. |
def Deserialize(self, reader):
"""
Deserialize full object.
Args:
reader (neo.IO.BinaryReader):
"""
self.HashStart = reader.ReadSerializableArray('neocore.UInt256.UInt256')
self.HashStop = reader.ReadUInt256() | Deserialize full object.
Args:
reader (neo.IO.BinaryReader): |
def get_tools_paths(self):
"""Get tools' paths."""
if settings.DEBUG or is_testing():
return list(get_apps_tools().values())
else:
tools_root = settings.FLOW_TOOLS_ROOT
subdirs = next(os.walk(tools_root))[1]
return [os.path.join(tools_root, sdir) for sdir in subdirs] | Get tools' paths. |
def maps_get_default_rules_output_rules_rbridgeid(self, **kwargs):
"""Auto Generated Code
"""
config = ET.Element("config")
maps_get_default_rules = ET.Element("maps_get_default_rules")
config = maps_get_default_rules
output = ET.SubElement(maps_get_default_rules, "output")
rules = ET.SubElement(output, "rules")
rbridgeid = ET.SubElement(rules, "rbridgeid")
rbridgeid.text = kwargs.pop('rbridgeid')
callback = kwargs.pop('callback', self._callback)
return callback(config) | Auto Generated Code |
def makeCredBearerTokenLoginMethod(username,
password,
stsUrl,
stsCert=None):
'''Return a function that will call the vim.SessionManager.LoginByToken()
after obtaining a Bearer token from the STS. The result of this function
can be passed as the "loginMethod" to a SessionOrientedStub constructor.
@param username: username of the user/service registered with STS.
@param password: password of the user/service registered with STS.
@param stsUrl: URL of the SAML Token issueing service. (i.e. SSO server).
@param stsCert: public key of the STS service.
'''
assert(username)
assert(password)
assert(stsUrl)
def _doLogin(soapStub):
from . import sso
cert = soapStub.schemeArgs['cert_file']
key = soapStub.schemeArgs['key_file']
authenticator = sso.SsoAuthenticator(sts_url=stsUrl,
sts_cert=stsCert)
samlAssertion = authenticator.get_bearer_saml_assertion(username,
password,
cert,
key)
si = vim.ServiceInstance("ServiceInstance", soapStub)
sm = si.content.sessionManager
if not sm.currentSession:
try:
soapStub.samlToken = samlAssertion
si.content.sessionManager.LoginByToken()
finally:
soapStub.samlToken = None
return _doLogin | Return a function that will call the vim.SessionManager.LoginByToken()
after obtaining a Bearer token from the STS. The result of this function
can be passed as the "loginMethod" to a SessionOrientedStub constructor.
@param username: username of the user/service registered with STS.
@param password: password of the user/service registered with STS.
@param stsUrl: URL of the SAML Token issueing service. (i.e. SSO server).
@param stsCert: public key of the STS service. |
def backwards(apps, schema_editor):
"""
Delete sample events, including derivative repeat and variation events.
"""
titles = [
'Daily Event',
'Weekday Event',
'Weekend Event',
'Weekly Event',
'Monthly Event',
'Yearly Event',
]
samples = EventBase.objects.filter(title__in=titles)
samples.delete() | Delete sample events, including derivative repeat and variation events. |
def new_dxfile(mode=None, write_buffer_size=dxfile.DEFAULT_BUFFER_SIZE, expected_file_size=None, file_is_mmapd=False,
**kwargs):
'''
:param mode: One of "w" or "a" for write and append modes, respectively
:type mode: string
:rtype: :class:`~dxpy.bindings.dxfile.DXFile`
Additional optional parameters not listed: all those under
:func:`dxpy.bindings.DXDataObject.new`.
Creates a new remote file object that is ready to be written to;
returns a :class:`~dxpy.bindings.dxfile.DXFile` object that is a
writable file-like object.
Example::
with new_dxfile(media_type="application/json") as fd:
fd.write("foo\\n")
Note that this is shorthand for::
dxFile = DXFile()
dxFile.new(**kwargs)
'''
dx_file = DXFile(mode=mode, write_buffer_size=write_buffer_size, expected_file_size=expected_file_size,
file_is_mmapd=file_is_mmapd)
dx_file.new(**kwargs)
return dx_file | :param mode: One of "w" or "a" for write and append modes, respectively
:type mode: string
:rtype: :class:`~dxpy.bindings.dxfile.DXFile`
Additional optional parameters not listed: all those under
:func:`dxpy.bindings.DXDataObject.new`.
Creates a new remote file object that is ready to be written to;
returns a :class:`~dxpy.bindings.dxfile.DXFile` object that is a
writable file-like object.
Example::
with new_dxfile(media_type="application/json") as fd:
fd.write("foo\\n")
Note that this is shorthand for::
dxFile = DXFile()
dxFile.new(**kwargs) |
def _rec_get_names(args, names=None):
"""return a list of all argument names"""
if names is None:
names = []
for arg in args:
if isinstance(arg, node_classes.Tuple):
_rec_get_names(arg.elts, names)
else:
names.append(arg.name)
return names | return a list of all argument names |
def psffunc(self, *args, **kwargs):
"""Calculates a linescan psf"""
if self.polychromatic:
func = psfcalc.calculate_polychrome_linescan_psf
else:
func = psfcalc.calculate_linescan_psf
return func(*args, **kwargs) | Calculates a linescan psf |
def add_result(self, scan_id, result_type, host='', name='', value='',
port='', test_id='', severity='', qod=''):
""" Add a result to a scan in the table. """
assert scan_id
assert len(name) or len(value)
result = dict()
result['type'] = result_type
result['name'] = name
result['severity'] = severity
result['test_id'] = test_id
result['value'] = value
result['host'] = host
result['port'] = port
result['qod'] = qod
results = self.scans_table[scan_id]['results']
results.append(result)
# Set scan_info's results to propagate results to parent process.
self.scans_table[scan_id]['results'] = results | Add a result to a scan in the table. |
def _pop_letters(char_list):
"""Pop consecutive letters from the front of a list and return them
Pops any and all consecutive letters from the start of the provided
character list and returns them as a list of characters. Operates
on (and possibly alters) the passed list
:param list char_list: a list of characters
:return: a list of characters
:rtype: list
"""
logger.debug('_pop_letters(%s)', char_list)
letters = []
while len(char_list) != 0 and char_list[0].isalpha():
letters.append(char_list.pop(0))
logger.debug('got letters: %s', letters)
logger.debug('updated char list: %s', char_list)
return letters | Pop consecutive letters from the front of a list and return them
Pops any and all consecutive letters from the start of the provided
character list and returns them as a list of characters. Operates
on (and possibly alters) the passed list
:param list char_list: a list of characters
:return: a list of characters
:rtype: list |
def uninstall(cls):
"""Remove the package manager from the system."""
if os.path.exists(cls.home):
shutil.rmtree(cls.home) | Remove the package manager from the system. |
def list_users(ctx, search, uuid, active):
"""List all locally known users"""
users = ctx.obj['db'].objectmodels['user']
for found_user in users.find():
if not search or (search and search in found_user.name):
# TODO: Not 2.x compatible
print(found_user.name, end=' ' if active or uuid else '\n')
if uuid:
print(found_user.uuid, end=' ' if active else '\n')
if active:
print(found_user.active)
log("Done") | List all locally known users |
def get_default_sess_config(mem_fraction=0.99):
"""
Return a tf.ConfigProto to use as default session config.
You can modify the returned config to fit your needs.
Args:
mem_fraction(float): see the `per_process_gpu_memory_fraction` option
in TensorFlow's GPUOptions protobuf:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto
Returns:
tf.ConfigProto: the config to use.
"""
conf = tfv1.ConfigProto()
conf.allow_soft_placement = True
# conf.log_device_placement = True
conf.intra_op_parallelism_threads = 1
conf.inter_op_parallelism_threads = 0
# TF benchmark use cpu_count() - gpu_thread_count(), e.g. 80 - 8 * 2
# Didn't see much difference.
conf.gpu_options.per_process_gpu_memory_fraction = mem_fraction
# This hurt performance of large data pipeline:
# https://github.com/tensorflow/benchmarks/commit/1528c46499cdcff669b5d7c006b7b971884ad0e6
# conf.gpu_options.force_gpu_compatible = True
conf.gpu_options.allow_growth = True
# from tensorflow.core.protobuf import rewriter_config_pb2 as rwc
# conf.graph_options.rewrite_options.memory_optimization = \
# rwc.RewriterConfig.HEURISTICS
# May hurt performance?
# conf.graph_options.optimizer_options.global_jit_level = tf.OptimizerOptions.ON_1
# conf.graph_options.place_pruned_graph = True
return conf | Return a tf.ConfigProto to use as default session config.
You can modify the returned config to fit your needs.
Args:
mem_fraction(float): see the `per_process_gpu_memory_fraction` option
in TensorFlow's GPUOptions protobuf:
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/core/protobuf/config.proto
Returns:
tf.ConfigProto: the config to use. |
def _duplicateLayer(self, layerName, newLayerName):
"""
This is the environment implementation of :meth:`BaseFont.duplicateLayer`.
**layerName** will be a :ref:`type-string` representing a valid layer name.
The value will have been normalized with :func:`normalizers.normalizeLayerName`
and **layerName** will be a layer that exists in the font. **newLayerName**
will be a :ref:`type-string` representing a valid layer name. The value will
have been normalized with :func:`normalizers.normalizeLayerName` and
**newLayerName** will have been tested to make sure that no layer with
the same name exists in the font. This must return an instance of a
:class:`BaseLayer` subclass.
Subclasses may override this method.
"""
newLayer = self.getLayer(layerName).copy()
return self.insertLayer(newLayer, newLayerName) | This is the environment implementation of :meth:`BaseFont.duplicateLayer`.
**layerName** will be a :ref:`type-string` representing a valid layer name.
The value will have been normalized with :func:`normalizers.normalizeLayerName`
and **layerName** will be a layer that exists in the font. **newLayerName**
will be a :ref:`type-string` representing a valid layer name. The value will
have been normalized with :func:`normalizers.normalizeLayerName` and
**newLayerName** will have been tested to make sure that no layer with
the same name exists in the font. This must return an instance of a
:class:`BaseLayer` subclass.
Subclasses may override this method. |
def evaluate(self, instance, step, extra):
"""Evaluate the current definition and fill its attributes.
Uses attributes definition in the following order:
- values defined when defining the ParameteredAttribute
- additional values defined when instantiating the containing factory
Args:
instance (builder.Resolver): The object holding currently computed
attributes
step: a factory.builder.BuildStep
extra (dict): additional, call-time added kwargs
for the step.
"""
defaults = dict(self.defaults)
if extra:
defaults.update(extra)
return self.generate(step, defaults) | Evaluate the current definition and fill its attributes.
Uses attributes definition in the following order:
- values defined when defining the ParameteredAttribute
- additional values defined when instantiating the containing factory
Args:
instance (builder.Resolver): The object holding currently computed
attributes
step: a factory.builder.BuildStep
extra (dict): additional, call-time added kwargs
for the step. |
def data_struct(*args, **kw):
'''
data_struct(args...) collapses all arguments (which must be maps) and keyword arguments
right-to-left into a single mapping and uses this mapping to create a DataStruct object.
'''
m = pimms.merge(*args, **kw)
return DataStruct(**m) | data_struct(args...) collapses all arguments (which must be maps) and keyword arguments
right-to-left into a single mapping and uses this mapping to create a DataStruct object. |
def currentGrouping( self ):
"""
Returns the current grouping for this widget.
:return [<str> group level, ..]
"""
groupBy = self.groupBy()
if ( groupBy == XOrbBrowserWidget.GroupByAdvancedKey ):
return self.advancedGrouping()
else:
table = self.tableType()
if ( not table ):
return []
for column in table.schema().columns():
if ( column.displayName() == groupBy ):
return [column.name()]
return [] | Returns the current grouping for this widget.
:return [<str> group level, ..] |
def _get_event_request_header(self):
"""Return EventRequestHeader for conversation."""
otr_status = (hangouts_pb2.OFF_THE_RECORD_STATUS_OFF_THE_RECORD
if self.is_off_the_record else
hangouts_pb2.OFF_THE_RECORD_STATUS_ON_THE_RECORD)
return hangouts_pb2.EventRequestHeader(
conversation_id=hangouts_pb2.ConversationId(id=self.id_),
client_generated_id=self._client.get_client_generated_id(),
expected_otr=otr_status,
delivery_medium=self._get_default_delivery_medium(),
) | Return EventRequestHeader for conversation. |
def _check_initialized(self):
"""Internal helper to check for uninitialized properties.
Raises:
BadValueError if it finds any.
"""
baddies = self._find_uninitialized()
if baddies:
raise datastore_errors.BadValueError(
'Entity has uninitialized properties: %s' % ', '.join(baddies)) | Internal helper to check for uninitialized properties.
Raises:
BadValueError if it finds any. |
def __discoverPlugins():
""" Discover the plugin classes contained in Python files, given a
list of directory names to scan. Return a list of plugin classes.
"""
for app in settings.INSTALLED_APPS:
if not app.startswith('django'):
module = __import__(app)
moduledir = path.Path(module.__file__).parent
plugin = moduledir / 'frog_plugin.py'
if plugin.exists():
file_, fpath, desc = imp.find_module('frog_plugin', [moduledir])
if file_:
imp.load_module('frog_plugin', file_, fpath, desc)
return FrogPluginRegistry.plugins | Discover the plugin classes contained in Python files, given a
list of directory names to scan. Return a list of plugin classes. |
def _clean(self, magic):
""" Given a magic string, remove the output tag designator. """
if magic.lower() == 'o':
self.magic = ''
elif magic[:2].lower() == 'o:':
self.magic = magic[2:]
elif magic[:2].lower() == 'o.':
self.ext = magic[1:] | Given a magic string, remove the output tag designator. |
def save_devices(self):
"""
save devices that have been obtained from LaMetric cloud
to a local file
"""
log.debug("saving devices to ''...".format(self._devices_filename))
if self._devices != []:
with codecs.open(self._devices_filename, "wb", "utf-8") as f:
json.dump(self._devices, f) | save devices that have been obtained from LaMetric cloud
to a local file |
def similar(names=None, ids=None, start=0, results=15, buckets=None, limit=False, max_familiarity=None, min_familiarity=None,
max_hotttnesss=None, min_hotttnesss=None, seed_catalog=None,artist_start_year_before=None, \
artist_start_year_after=None,artist_end_year_before=None,artist_end_year_after=None):
"""Return similar artists to this one
Args:
Kwargs:
ids (str/list): An artist id or list of ids
names (str/list): An artist name or list of names
results (int): An integer number of results to return
buckets (list): A list of strings specifying which buckets to retrieve
limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
start (int): An integer starting value for the result set
max_familiarity (float): A float specifying the max familiarity of artists to search for
min_familiarity (float): A float specifying the min familiarity of artists to search for
max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
seed_catalog (str): A string specifying the catalog similar artists are restricted to
Returns:
A list of similar Artist objects
Example:
>>> some_dudes = [artist.Artist('weezer'), artist.Artist('radiohead')]
>>> some_dudes
[<artist - Weezer>, <artist - Radiohead>]
>>> sims = artist.similar(ids=[art.id for art in some_dudes], results=5)
>>> sims
[<artist - The Smashing Pumpkins>, <artist - Biffy Clyro>, <artist - Death Cab for Cutie>, <artist - Jimmy Eat World>, <artist - Nerf Herder>]
>>>
"""
buckets = buckets or []
kwargs = {}
if ids:
if not isinstance(ids, list):
ids = [ids]
kwargs['id'] = ids
if names:
if not isinstance(names, list):
names = [names]
kwargs['name'] = names
if max_familiarity is not None:
kwargs['max_familiarity'] = max_familiarity
if min_familiarity is not None:
kwargs['min_familiarity'] = min_familiarity
if max_hotttnesss is not None:
kwargs['max_hotttnesss'] = max_hotttnesss
if min_hotttnesss is not None:
kwargs['min_hotttnesss'] = min_hotttnesss
if seed_catalog is not None:
kwargs['seed_catalog'] = seed_catalog
if start:
kwargs['start'] = start
if results:
kwargs['results'] = results
if buckets:
kwargs['bucket'] = buckets
if limit:
kwargs['limit'] = 'true'
if artist_start_year_before:
kwargs['artist_start_year_before'] = artist_start_year_before
if artist_start_year_after:
kwargs['artist_start_year_after'] = artist_start_year_after
if artist_end_year_before:
kwargs['artist_end_year_before'] = artist_end_year_before
if artist_end_year_after:
kwargs['artist_end_year_after'] = artist_end_year_after
result = util.callm("%s/%s" % ('artist', 'similar'), kwargs)
return [Artist(**util.fix(a_dict)) for a_dict in result['response']['artists']] | Return similar artists to this one
Args:
Kwargs:
ids (str/list): An artist id or list of ids
names (str/list): An artist name or list of names
results (int): An integer number of results to return
buckets (list): A list of strings specifying which buckets to retrieve
limit (bool): A boolean indicating whether or not to limit the results to one of the id spaces specified in buckets
start (int): An integer starting value for the result set
max_familiarity (float): A float specifying the max familiarity of artists to search for
min_familiarity (float): A float specifying the min familiarity of artists to search for
max_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
min_hotttnesss (float): A float specifying the max hotttnesss of artists to search for
seed_catalog (str): A string specifying the catalog similar artists are restricted to
Returns:
A list of similar Artist objects
Example:
>>> some_dudes = [artist.Artist('weezer'), artist.Artist('radiohead')]
>>> some_dudes
[<artist - Weezer>, <artist - Radiohead>]
>>> sims = artist.similar(ids=[art.id for art in some_dudes], results=5)
>>> sims
[<artist - The Smashing Pumpkins>, <artist - Biffy Clyro>, <artist - Death Cab for Cutie>, <artist - Jimmy Eat World>, <artist - Nerf Herder>]
>>> |
def create_comment_browser(self, layout):
"""Create a comment browser and insert it into the given layout
:param layout: the layout to insert the browser into
:type layout: QLayout
:returns: the created browser
:rtype: :class:`jukeboxcore.gui.widgets.browser.ListBrowser`
:raises: None
"""
brws = CommentBrowser(1, headers=['Comments:'])
layout.insertWidget(1, brws)
return brws | Create a comment browser and insert it into the given layout
:param layout: the layout to insert the browser into
:type layout: QLayout
:returns: the created browser
:rtype: :class:`jukeboxcore.gui.widgets.browser.ListBrowser`
:raises: None |
def by_user_and_perm(cls, user_id, perm_name, db_session=None):
"""
return by user and permission name
:param user_id:
:param perm_name:
:param db_session:
:return:
"""
db_session = get_db_session(db_session)
query = db_session.query(cls.model).filter(cls.model.user_id == user_id)
query = query.filter(cls.model.perm_name == perm_name)
return query.first() | return by user and permission name
:param user_id:
:param perm_name:
:param db_session:
:return: |
def expected_h(nvals, fit="RANSAC"):
"""
Uses expected_rs to calculate the expected value for the Hurst exponent h
based on the values of n used for the calculation.
Args:
nvals (iterable of int):
the values of n used to calculate the individual (R/S)_n
KWargs:
fit (str):
the fitting method to use for the line fit, either 'poly' for normal
least squares polynomial fitting or 'RANSAC' for RANSAC-fitting which
is more robust to outliers
Returns:
float:
expected h for white noise
"""
rsvals = [expected_rs(n) for n in nvals]
poly = poly_fit(np.log(nvals), np.log(rsvals), 1, fit=fit)
return poly[0] | Uses expected_rs to calculate the expected value for the Hurst exponent h
based on the values of n used for the calculation.
Args:
nvals (iterable of int):
the values of n used to calculate the individual (R/S)_n
KWargs:
fit (str):
the fitting method to use for the line fit, either 'poly' for normal
least squares polynomial fitting or 'RANSAC' for RANSAC-fitting which
is more robust to outliers
Returns:
float:
expected h for white noise |
def _recv_sf(self, data):
"""Process a received 'Single Frame' frame"""
self.rx_timer.cancel()
if self.rx_state != ISOTP_IDLE:
warning("RX state was reset because single frame was received")
self.rx_state = ISOTP_IDLE
length = six.indexbytes(data, 0) & 0xf
if len(data) - 1 < length:
return 1
msg = data[1:1 + length]
self.rx_queue.put(msg)
for cb in self.rx_callbacks:
cb(msg)
self.call_release()
return 0 | Process a received 'Single Frame' frame |
def preorder(self):
"""Return the nodes in the binary tree using pre-order_ traversal.
A pre-order_ traversal visits root, left subtree, then right subtree.
.. _pre-order: https://en.wikipedia.org/wiki/Tree_traversal
:return: List of nodes.
:rtype: [binarytree.Node]
**Example**:
.. doctest::
>>> from binarytree import Node
>>>
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.left = Node(4)
>>> root.left.right = Node(5)
>>>
>>> print(root)
<BLANKLINE>
__1
/ \\
2 3
/ \\
4 5
<BLANKLINE>
>>> root.preorder
[Node(1), Node(2), Node(4), Node(5), Node(3)]
"""
node_stack = [self]
result = []
while len(node_stack) > 0:
node = node_stack.pop()
result.append(node)
if node.right is not None:
node_stack.append(node.right)
if node.left is not None:
node_stack.append(node.left)
return result | Return the nodes in the binary tree using pre-order_ traversal.
A pre-order_ traversal visits root, left subtree, then right subtree.
.. _pre-order: https://en.wikipedia.org/wiki/Tree_traversal
:return: List of nodes.
:rtype: [binarytree.Node]
**Example**:
.. doctest::
>>> from binarytree import Node
>>>
>>> root = Node(1)
>>> root.left = Node(2)
>>> root.right = Node(3)
>>> root.left.left = Node(4)
>>> root.left.right = Node(5)
>>>
>>> print(root)
<BLANKLINE>
__1
/ \\
2 3
/ \\
4 5
<BLANKLINE>
>>> root.preorder
[Node(1), Node(2), Node(4), Node(5), Node(3)] |
def load_py(stream, filepath=None):
"""Load python-formatted data from a stream.
Args:
stream (file-like object).
Returns:
dict.
"""
with add_sys_paths(config.package_definition_build_python_paths):
return _load_py(stream, filepath=filepath) | Load python-formatted data from a stream.
Args:
stream (file-like object).
Returns:
dict. |
def run():
""" Module level test.
"""
logging.basicConfig(level=logging.DEBUG)
load_config.ConfigLoader().load()
config.debug = True
print(repr(config.engine.item(sys.argv[1]))) | Module level test. |
def conf_budget(self, budget):
"""
Set limit on the number of conflicts.
"""
if self.maplesat:
pysolvers.maplechrono_cbudget(self.maplesat, budget) | Set limit on the number of conflicts. |
def validate_one(func_name):
"""
Validate the docstring for the given func_name
Parameters
----------
func_name : function
Function whose docstring will be evaluated (e.g. pandas.read_csv).
Returns
-------
dict
A dictionary containing all the information obtained from validating
the docstring.
"""
doc = Docstring(func_name)
errs, wrns, examples_errs = get_validation_data(doc)
return {'type': doc.type,
'docstring': doc.clean_doc,
'deprecated': doc.deprecated,
'file': doc.source_file_name,
'file_line': doc.source_file_def_line,
'github_link': doc.github_url,
'errors': errs,
'warnings': wrns,
'examples_errors': examples_errs} | Validate the docstring for the given func_name
Parameters
----------
func_name : function
Function whose docstring will be evaluated (e.g. pandas.read_csv).
Returns
-------
dict
A dictionary containing all the information obtained from validating
the docstring. |
def count(self, source, target):
"""
The 'count' relationship is used for listing endpoints where a specific attribute
might hold the value to the number of instances of another attribute.
"""
try:
source_value = self._response_holder[source]
except KeyError:
# Source value hasn't been determined yet, we need
# to generate the source value first
raw = self.fake_response[source]
source_value = self._parse_syntax(raw)
if isinstance(source_value, str):
source_value = int(source_value)
target = self.fake_response[target]
values = []
for _ in range(source_value):
self._is_empty = False # Remote state for re.sub to switch in case it hits a None value
mock_value = self._parse_syntax(target)
mock_value = str(mock_value) # Treat the value as a string regardless of its actual data type
_target = mock_value[1:-1] # Remove extra quotation
_target = _target.replace("'", '"')
try:
mock_value = json.loads(_target)
except:
mock_value = _target
# If uniqueness is specified and this mock value isn't
# in the store yet, then we can append it to the results
if not self._is_empty:
values.append(mock_value)
return values | The 'count' relationship is used for listing endpoints where a specific attribute
might hold the value to the number of instances of another attribute. |
def scale(self, scalex, scaley=None, center=(0, 0)):
"""
Scale this object.
Parameters
----------
scalex : number
Scaling factor along the first axis.
scaley : number or ``None``
Scaling factor along the second axis. If ``None``, same as
``scalex``.
center : array-like[2]
Center point for the scaling operation.
Returns
-------
out : ``PolygonSet``
This object.
"""
c0 = numpy.array(center)
s = scalex if scaley is None else numpy.array((scalex, scaley))
self.polygons = [(points - c0) * s + c0 for points in self.polygons]
return self | Scale this object.
Parameters
----------
scalex : number
Scaling factor along the first axis.
scaley : number or ``None``
Scaling factor along the second axis. If ``None``, same as
``scalex``.
center : array-like[2]
Center point for the scaling operation.
Returns
-------
out : ``PolygonSet``
This object. |
def reservations(self):
"""get nodes of every reservations"""
command = [SINFO, '--reservation']
output = subprocess.check_output(command, env=SINFO_ENV)
output = output.decode()
it = iter(output.splitlines())
next(it)
for line in it:
rsv = Reservation.from_sinfo(line)
yield rsv.name, rsv | get nodes of every reservations |
def tagged(self, tag):
"""Returns a new PostCollection containing the subset of posts that are tagged with *tag*."""
return PostCollection([p for p in self if unicode(tag) in p.tags]) | Returns a new PostCollection containing the subset of posts that are tagged with *tag*. |
def _set_service(self, v, load=False):
"""
Setter method for service, mapped from YANG variable /service (container)
If this variable is read-only (config: false) in the
source YANG file, then _set_service is considered as a private
method. Backends looking to populate this variable should
do so via calling thisObj._set_service() directly.
"""
if hasattr(v, "_utype"):
v = v._utype(v)
try:
t = YANGDynClass(v,base=service.service, is_container='container', presence=False, yang_name="service", rest_name="service", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'Perform services', u'sort-priority': u'18'}}, namespace='urn:brocade.com:mgmt:brocade-aaa', defining_module='brocade-aaa', yang_type='container', is_config=True)
except (TypeError, ValueError):
raise ValueError({
'error-string': """service must be of a type compatible with container""",
'defined-type': "container",
'generated-type': """YANGDynClass(base=service.service, is_container='container', presence=False, yang_name="service", rest_name="service", parent=self, path_helper=self._path_helper, extmethods=self._extmethods, register_paths=True, extensions={u'tailf-common': {u'info': u'Perform services', u'sort-priority': u'18'}}, namespace='urn:brocade.com:mgmt:brocade-aaa', defining_module='brocade-aaa', yang_type='container', is_config=True)""",
})
self.__service = t
if hasattr(self, '_set'):
self._set() | Setter method for service, mapped from YANG variable /service (container)
If this variable is read-only (config: false) in the
source YANG file, then _set_service is considered as a private
method. Backends looking to populate this variable should
do so via calling thisObj._set_service() directly. |
def note(name, source=None, contents=None, **kwargs):
'''
Add content to a document generated using `highstate_doc.render`.
This state does not preform any tasks on the host. It only is used in highstate_doc lowstate proccessers
to include extra documents.
.. code-block:: yaml
{{sls}} example note:
highstate_doc.note:
- name: example note
- require_in:
- pkg: somepackage
- contents: |
example `highstate_doc.note`
------------------
This state does not do anything to the system! It is only used by a `proccesser`
you can use `requisites` and `order` to move your docs around the rendered file.
.. this message appare aboce the `pkg: somepackage` state.
- source: salt://{{tpldir}}/also_include_a_file.md
{{sls}} extra help:
highstate_doc.note:
- name: example
- order: 0
- source: salt://{{tpldir}}/HELP.md
'''
comment = ''
if source:
comment += 'include file: {0}\n'.format(source)
if contents and len(contents) < 200:
comment += contents
return {'name': name, 'result': True, 'comment': comment, 'changes': {}} | Add content to a document generated using `highstate_doc.render`.
This state does not preform any tasks on the host. It only is used in highstate_doc lowstate proccessers
to include extra documents.
.. code-block:: yaml
{{sls}} example note:
highstate_doc.note:
- name: example note
- require_in:
- pkg: somepackage
- contents: |
example `highstate_doc.note`
------------------
This state does not do anything to the system! It is only used by a `proccesser`
you can use `requisites` and `order` to move your docs around the rendered file.
.. this message appare aboce the `pkg: somepackage` state.
- source: salt://{{tpldir}}/also_include_a_file.md
{{sls}} extra help:
highstate_doc.note:
- name: example
- order: 0
- source: salt://{{tpldir}}/HELP.md |
def get_gcp_client(**kwargs):
"""Public GCP client builder."""
return _gcp_client(project=kwargs['project'], mod_name=kwargs['mod_name'],
pkg_name=kwargs.get('pkg_name', 'google.cloud'),
key_file=kwargs.get('key_file', None),
http_auth=kwargs.get('http', None),
user_agent=kwargs.get('user_agent', None)) | Public GCP client builder. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.