code
stringlengths 75
104k
| docstring
stringlengths 1
46.9k
|
|---|---|
def search_value(self, xpath, default=None, single_value=True):
""" Try to find a value in the result
:param str xpath: a xpath filter see https://github.com/kennknowles/python-jsonpath-rw#jsonpath-syntax
:param any default: default value if not found
:param bool single_value: is the result is multivalued
:return: the value found or None
"""
matches = [match.value for match in parse(xpath).find(self.retval)]
if len(matches) == 0:
return default
return matches[0] if single_value is True else matches
|
Try to find a value in the result
:param str xpath: a xpath filter see https://github.com/kennknowles/python-jsonpath-rw#jsonpath-syntax
:param any default: default value if not found
:param bool single_value: is the result is multivalued
:return: the value found or None
|
def visit_Call(self, node):
"""
Transform call site to have normal function call.
Examples
--------
For methods:
>> a = [1, 2, 3]
>> a.append(1)
Becomes
>> __list__.append(a, 1)
For functions:
>> __builtin__.dict.fromkeys([1, 2, 3])
Becomes
>> __builtin__.__dict__.fromkeys([1, 2, 3])
"""
node = self.generic_visit(node)
# Only attributes function can be Pythonic and should be normalized
if isinstance(node.func, ast.Attribute):
if node.func.attr in methods:
# Get object targeted by methods
obj = lhs = node.func.value
# Get the most left identifier to check if it is not an
# imported module
while isinstance(obj, ast.Attribute):
obj = obj.value
is_not_module = (not isinstance(obj, ast.Name) or
obj.id not in self.imports)
if is_not_module:
self.update = True
# As it was a methods call, push targeted object as first
# arguments and add correct module prefix
node.args.insert(0, lhs)
mod = methods[node.func.attr][0]
# Submodules import full module
self.to_import.add(mangle(mod[0]))
node.func = reduce(
lambda v, o: ast.Attribute(v, o, ast.Load()),
mod[1:] + (node.func.attr,),
ast.Name(mangle(mod[0]), ast.Load(), None)
)
# else methods have been called using function syntax
if node.func.attr in methods or node.func.attr in functions:
# Now, methods and function have both function syntax
def rec(path, cur_module):
"""
Recursively rename path content looking in matching module.
Prefers __module__ to module if it exists.
This recursion is done as modules are visited top->bottom
while attributes have to be visited bottom->top.
"""
err = "Function path is chained attributes and name"
assert isinstance(path, (ast.Name, ast.Attribute)), err
if isinstance(path, ast.Attribute):
new_node, cur_module = rec(path.value, cur_module)
new_id, mname = self.renamer(path.attr, cur_module)
return (ast.Attribute(new_node, new_id, ast.Load()),
cur_module[mname])
else:
new_id, mname = self.renamer(path.id, cur_module)
if mname not in cur_module:
raise PythranSyntaxError(
"Unbound identifier '{}'".format(mname), node)
return (ast.Name(new_id, ast.Load(), None),
cur_module[mname])
# Rename module path to avoid naming issue.
node.func.value, _ = rec(node.func.value, MODULES)
self.update = True
return node
|
Transform call site to have normal function call.
Examples
--------
For methods:
>> a = [1, 2, 3]
>> a.append(1)
Becomes
>> __list__.append(a, 1)
For functions:
>> __builtin__.dict.fromkeys([1, 2, 3])
Becomes
>> __builtin__.__dict__.fromkeys([1, 2, 3])
|
def removeXmlElement(name, directory, file_pattern, logger=None):
""" Recursively walk a directory and remove XML elements """
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, file_pattern):
filepath = os.path.join(path, filename)
remove_xml_element_file(name, filepath)
|
Recursively walk a directory and remove XML elements
|
def get_exchange_group_info(self, symprec=1e-2, angle_tolerance=5.0):
"""
Returns the information on the symmetry of the Hamiltonian
describing the exchange energy of the system, taking into
account relative direction of magnetic moments but not their
absolute direction.
This is not strictly accurate (e.g. some/many atoms will
have zero magnetic moments), but defining symmetry this
way is a useful way of keeping track of distinct magnetic
orderings within pymatgen.
:param symprec: same as SpacegroupAnalyzer
:param angle_tolerance: same as SpacegroupAnalyzer
:return: spacegroup_symbol, international_number
"""
structure = self.get_structure_with_spin()
return structure.get_space_group_info(
symprec=symprec, angle_tolerance=angle_tolerance
)
|
Returns the information on the symmetry of the Hamiltonian
describing the exchange energy of the system, taking into
account relative direction of magnetic moments but not their
absolute direction.
This is not strictly accurate (e.g. some/many atoms will
have zero magnetic moments), but defining symmetry this
way is a useful way of keeping track of distinct magnetic
orderings within pymatgen.
:param symprec: same as SpacegroupAnalyzer
:param angle_tolerance: same as SpacegroupAnalyzer
:return: spacegroup_symbol, international_number
|
def _validate_slices_form_uniform_grid(slice_datasets):
'''
Perform various data checks to ensure that the list of slices form a
evenly-spaced grid of data.
Some of these checks are probably not required if the data follows the
DICOM specification, however it seems pertinent to check anyway.
'''
invariant_properties = [
'Modality',
'SOPClassUID',
'SeriesInstanceUID',
'Rows',
'Columns',
'PixelSpacing',
'PixelRepresentation',
'BitsAllocated',
'BitsStored',
'HighBit',
]
for property_name in invariant_properties:
_slice_attribute_equal(slice_datasets, property_name)
_validate_image_orientation(slice_datasets[0].ImageOrientationPatient)
_slice_ndarray_attribute_almost_equal(slice_datasets, 'ImageOrientationPatient', 1e-5)
slice_positions = _slice_positions(slice_datasets)
_check_for_missing_slices(slice_positions)
|
Perform various data checks to ensure that the list of slices form a
evenly-spaced grid of data.
Some of these checks are probably not required if the data follows the
DICOM specification, however it seems pertinent to check anyway.
|
def CWDE(cpu):
"""
Converts word to doubleword.
::
DX = sign-extend of AX.
:param cpu: current CPU.
"""
bit = Operators.EXTRACT(cpu.AX, 15, 1)
cpu.EAX = Operators.SEXTEND(cpu.AX, 16, 32)
cpu.EDX = Operators.SEXTEND(bit, 1, 32)
|
Converts word to doubleword.
::
DX = sign-extend of AX.
:param cpu: current CPU.
|
def make_multisig_segwit_wallet( m, n ):
"""
Create a bundle of information
that can be used to generate an
m-of-n multisig witness script.
"""
pks = []
for i in xrange(0, n):
pk = BitcoinPrivateKey(compressed=True).to_wif()
pks.append(pk)
return make_multisig_segwit_info(m, pks)
|
Create a bundle of information
that can be used to generate an
m-of-n multisig witness script.
|
def diff(candidate, running, *models):
'''
Returns the difference between two configuration entities structured
according to the YANG model.
.. note::
This function is recommended to be used mostly as a state helper.
candidate
First model to compare.
running
Second model to compare.
models
A list of models to be used when comparing.
CLI Example:
.. code-block:: bash
salt '*' napalm_yang.diff {} {} models.openconfig_interfaces
Output Example:
.. code-block:: python
{
"interfaces": {
"interface": {
"both": {
"Port-Channel1": {
"config": {
"mtu": {
"first": "0",
"second": "9000"
}
}
}
},
"first_only": [
"Loopback0"
],
"second_only": [
"Loopback1"
]
}
}
}
'''
if isinstance(models, tuple) and isinstance(models[0], list):
models = models[0]
first = _get_root_object(models)
first.load_dict(candidate)
second = _get_root_object(models)
second.load_dict(running)
return napalm_yang.utils.diff(first, second)
|
Returns the difference between two configuration entities structured
according to the YANG model.
.. note::
This function is recommended to be used mostly as a state helper.
candidate
First model to compare.
running
Second model to compare.
models
A list of models to be used when comparing.
CLI Example:
.. code-block:: bash
salt '*' napalm_yang.diff {} {} models.openconfig_interfaces
Output Example:
.. code-block:: python
{
"interfaces": {
"interface": {
"both": {
"Port-Channel1": {
"config": {
"mtu": {
"first": "0",
"second": "9000"
}
}
}
},
"first_only": [
"Loopback0"
],
"second_only": [
"Loopback1"
]
}
}
}
|
def read_stb(library, session):
"""Reads a status byte of the service request.
Corresponds to viReadSTB function of the VISA library.
:param library: the visa library wrapped by ctypes.
:param session: Unique logical identifier to a session.
:return: Service request status byte, return value of the library call.
:rtype: int, :class:`pyvisa.constants.StatusCode`
"""
status = ViUInt16()
ret = library.viReadSTB(session, byref(status))
return status.value, ret
|
Reads a status byte of the service request.
Corresponds to viReadSTB function of the VISA library.
:param library: the visa library wrapped by ctypes.
:param session: Unique logical identifier to a session.
:return: Service request status byte, return value of the library call.
:rtype: int, :class:`pyvisa.constants.StatusCode`
|
def transform(self, X):
"""Transform a sequence of documents to a document-term matrix.
Transformation is done in parallel, and correctly handles dask
collections.
Parameters
----------
X : dask.Bag of raw text documents, length = n_samples
Samples. Each sample must be a text document (either bytes or
unicode strings, file name or file object depending on the
constructor argument) which will be tokenized and hashed.
Returns
-------
X : dask.array.Array, shape = (n_samples, self.n_features)
Document-term matrix. Each block of the array is a scipy sparse
matrix.
Notes
-----
The returned dask Array is composed scipy sparse matricies. If you need
to compute on the result immediately, you may need to convert the individual
blocks to ndarrays or pydata/sparse matricies.
>>> import sparse
>>> X.map_blocks(sparse.COO.from_scipy_sparse, dtype=X.dtype) # doctest: +SKIP
See the :doc:`examples/text-vectorization` for more.
"""
msg = "'X' should be a 1-dimensional array with length 'num_samples'."
if not dask.is_dask_collection(X):
return super(HashingVectorizer, self).transform(X)
if isinstance(X, db.Bag):
bag2 = X.map_partitions(_transform, estimator=self)
objs = bag2.to_delayed()
arrs = [
da.from_delayed(obj, (np.nan, self.n_features), self.dtype)
for obj in objs
]
result = da.concatenate(arrs, axis=0)
elif isinstance(X, dd.Series):
result = X.map_partitions(_transform, self)
elif isinstance(X, da.Array):
# dask.Array
chunks = ((np.nan,) * X.numblocks[0], (self.n_features,))
if X.ndim == 1:
result = X.map_blocks(
_transform, estimator=self, dtype="f8", chunks=chunks, new_axis=1
)
else:
raise ValueError(msg)
else:
raise ValueError(msg)
return result
|
Transform a sequence of documents to a document-term matrix.
Transformation is done in parallel, and correctly handles dask
collections.
Parameters
----------
X : dask.Bag of raw text documents, length = n_samples
Samples. Each sample must be a text document (either bytes or
unicode strings, file name or file object depending on the
constructor argument) which will be tokenized and hashed.
Returns
-------
X : dask.array.Array, shape = (n_samples, self.n_features)
Document-term matrix. Each block of the array is a scipy sparse
matrix.
Notes
-----
The returned dask Array is composed scipy sparse matricies. If you need
to compute on the result immediately, you may need to convert the individual
blocks to ndarrays or pydata/sparse matricies.
>>> import sparse
>>> X.map_blocks(sparse.COO.from_scipy_sparse, dtype=X.dtype) # doctest: +SKIP
See the :doc:`examples/text-vectorization` for more.
|
def get_runs_by_id(self, config_id):
"""
returns a list of runs for a given config id
The runs are sorted by ascending budget, so '-1' will give
the longest run for this config.
"""
d = self.data[config_id]
runs = []
for b in d.results.keys():
try:
err_logs = d.exceptions.get(b, None)
if d.results[b] is None:
r = Run(config_id, b, None, None , d.time_stamps[b], err_logs)
else:
r = Run(config_id, b, d.results[b]['loss'], d.results[b]['info'] , d.time_stamps[b], err_logs)
runs.append(r)
except:
raise
runs.sort(key=lambda r: r.budget)
return(runs)
|
returns a list of runs for a given config id
The runs are sorted by ascending budget, so '-1' will give
the longest run for this config.
|
def target_query(plugin, port, location):
"""
prepared ReQL for target
"""
return ((r.row[PLUGIN_NAME_KEY] == plugin) &
(r.row[PORT_FIELD] == port) &
(r.row[LOCATION_FIELD] == location))
|
prepared ReQL for target
|
def _verify_signed_jwt_with_certs(
jwt, time_now, cache,
cert_uri=_DEFAULT_CERT_URI):
"""Verify a JWT against public certs.
See http://self-issued.info/docs/draft-jones-json-web-token.html.
The PyCrypto library included with Google App Engine is severely limited and
so you have to use it very carefully to verify JWT signatures. The first
issue is that the library can't read X.509 files, so we make a call to a
special URI that has the public cert in modulus/exponent form in JSON.
The second issue is that the RSA.verify method doesn't work, at least for
how the JWT tokens are signed, so we have to manually verify the signature
of the JWT, which means hashing the signed part of the JWT and comparing
that to the signature that's been encrypted with the public key.
Args:
jwt: string, A JWT.
time_now: The current time, as a long (eg. long(time.time())).
cache: Cache to use (eg. the memcache module).
cert_uri: string, URI to get cert modulus and exponent in JSON format.
Returns:
dict, The deserialized JSON payload in the JWT.
Raises:
_AppIdentityError: if any checks are failed.
"""
segments = jwt.split('.')
if len(segments) != 3:
# Note that anywhere we print the jwt or its json body, we need to use
# %r instead of %s, so that non-printable characters are escaped safely.
raise _AppIdentityError('Token is not an id_token (Wrong number of '
'segments)')
signed = '%s.%s' % (segments[0], segments[1])
signature = _urlsafe_b64decode(segments[2])
# pycrypto only deals in integers, so we have to convert the string of bytes
# into a long.
lsignature = long(signature.encode('hex'), 16)
# Verify expected header.
header_body = _urlsafe_b64decode(segments[0])
try:
header = json.loads(header_body)
except:
raise _AppIdentityError("Can't parse header")
if header.get('alg') != 'RS256':
raise _AppIdentityError('Unexpected encryption algorithm: %r' %
header.get('alg'))
# Formerly we would parse the token body here.
# However, it's not safe to do that without first checking the signature.
certs = _get_cached_certs(cert_uri, cache)
if certs is None:
raise _AppIdentityError(
'Unable to retrieve certs needed to verify the signed JWT')
# Verify that we were able to load the Crypto libraries, before we try
# to use them.
if not _CRYPTO_LOADED:
raise _AppIdentityError('Unable to load pycrypto library. Can\'t verify '
'id_token signature. See http://www.pycrypto.org '
'for more information on pycrypto.')
# SHA256 hash of the already 'signed' segment from the JWT. Since a SHA256
# hash, will always have length 64.
local_hash = SHA256.new(signed).hexdigest()
# Check signature.
verified = False
for keyvalue in certs['keyvalues']:
try:
modulus = _b64_to_long(keyvalue['modulus'])
exponent = _b64_to_long(keyvalue['exponent'])
key = RSA.construct((modulus, exponent))
# Encrypt, and convert to a hex string.
hexsig = '%064x' % key.encrypt(lsignature, '')[0]
# Make sure we have only last 64 base64 chars
hexsig = hexsig[-64:]
# Check the signature on 'signed' by encrypting 'signature' with the
# public key and confirming the result matches the SHA256 hash of
# 'signed'. hmac.compare_digest(a, b) is used to avoid timing attacks.
verified = hmac.compare_digest(hexsig, local_hash)
if verified:
break
except Exception, e: # pylint: disable=broad-except
# Log the exception for debugging purpose.
_logger.debug(
'Signature verification error: %s; continuing with the next cert.', e)
continue
if not verified:
raise _AppIdentityError('Invalid token signature')
# Parse token.
json_body = _urlsafe_b64decode(segments[1])
try:
parsed = json.loads(json_body)
except:
raise _AppIdentityError("Can't parse token body")
# Check creation timestamp.
iat = parsed.get('iat')
if iat is None:
raise _AppIdentityError('No iat field in token')
earliest = iat - _CLOCK_SKEW_SECS
# Check expiration timestamp.
exp = parsed.get('exp')
if exp is None:
raise _AppIdentityError('No exp field in token')
if exp >= time_now + _MAX_TOKEN_LIFETIME_SECS:
raise _AppIdentityError('exp field too far in future')
latest = exp + _CLOCK_SKEW_SECS
if time_now < earliest:
raise _AppIdentityError('Token used too early, %d < %d' %
(time_now, earliest))
if time_now > latest:
raise _AppIdentityError('Token used too late, %d > %d' %
(time_now, latest))
return parsed
|
Verify a JWT against public certs.
See http://self-issued.info/docs/draft-jones-json-web-token.html.
The PyCrypto library included with Google App Engine is severely limited and
so you have to use it very carefully to verify JWT signatures. The first
issue is that the library can't read X.509 files, so we make a call to a
special URI that has the public cert in modulus/exponent form in JSON.
The second issue is that the RSA.verify method doesn't work, at least for
how the JWT tokens are signed, so we have to manually verify the signature
of the JWT, which means hashing the signed part of the JWT and comparing
that to the signature that's been encrypted with the public key.
Args:
jwt: string, A JWT.
time_now: The current time, as a long (eg. long(time.time())).
cache: Cache to use (eg. the memcache module).
cert_uri: string, URI to get cert modulus and exponent in JSON format.
Returns:
dict, The deserialized JSON payload in the JWT.
Raises:
_AppIdentityError: if any checks are failed.
|
def dump(destination, xs, model=None, properties=False, indent=True, **kwargs):
"""
Serialize Xmrs (or subclass) objects to PENMAN and write to a file.
Args:
destination: filename or file object
xs: iterator of :class:`~delphin.mrs.xmrs.Xmrs` objects to
serialize
model: Xmrs subclass used to get triples
properties: if `True`, encode variable properties
indent: if `True`, adaptively indent; if `False` or `None`,
don't indent; if a non-negative integer N, indent N spaces
per level
"""
text = dumps(
xs, model=model, properties=properties, indent=indent, **kwargs
)
if hasattr(destination, 'write'):
print(text, file=destination)
else:
with open(destination, 'w') as fh:
print(text, file=fh)
|
Serialize Xmrs (or subclass) objects to PENMAN and write to a file.
Args:
destination: filename or file object
xs: iterator of :class:`~delphin.mrs.xmrs.Xmrs` objects to
serialize
model: Xmrs subclass used to get triples
properties: if `True`, encode variable properties
indent: if `True`, adaptively indent; if `False` or `None`,
don't indent; if a non-negative integer N, indent N spaces
per level
|
def addChild(self,item):
"""
When you add a child to a Node, you are adding yourself as a parent to the child
You cannot have the same node as a child more than once.
If you add a Node, it is used. If you add a non-node, a new child Node is created. Thus: You cannot
add a child as an item which is a Node. (You can, however, construct such a node, and add it as a child)
"""
if not isinstance(item,Node):
item = Node(item)
if item in self.children:
return item
self.children.append(item)
item.parents.add(self)
return item
|
When you add a child to a Node, you are adding yourself as a parent to the child
You cannot have the same node as a child more than once.
If you add a Node, it is used. If you add a non-node, a new child Node is created. Thus: You cannot
add a child as an item which is a Node. (You can, however, construct such a node, and add it as a child)
|
def length(self):
"""Total surveyed cave length, not including splays."""
return sum([shot.length for shot in self.shots if not shot.is_splay])
|
Total surveyed cave length, not including splays.
|
def iter(self, match="*", count=1000):
""" Iterates the set of keys in :prop:key_prefix in :prop:_client
@match: #str pattern to match after the :prop:key_prefix
@count: the user specified the amount of work that should be done
at every call in order to retrieve elements from the collection
-> yields redis keys within this instance
"""
replace_this = self.key_prefix+":"
for key in self._client.scan_iter(
match="{}:{}".format(self.key_prefix, match), count=count):
yield self._decode(key).replace(replace_this, "", 1)
|
Iterates the set of keys in :prop:key_prefix in :prop:_client
@match: #str pattern to match after the :prop:key_prefix
@count: the user specified the amount of work that should be done
at every call in order to retrieve elements from the collection
-> yields redis keys within this instance
|
def _prepare_data_payload(data):
"""
Make a copy of the `data` object, preparing it to be sent to the server.
The data will be sent via x-www-form-urlencoded or multipart/form-data mechanisms. Both of them work with
plain lists of key/value pairs, so this method converts the data into such format.
"""
if not data: return None
res = {}
for key, value in viewitems(data):
if value is None: continue # don't send args set to None so backend defaults take precedence
if isinstance(value, list):
value = stringify_list(value)
elif isinstance(value, dict):
if "__meta" in value and value["__meta"]["schema_name"].endswith("KeyV3"):
value = value["name"]
else:
value = stringify_dict(value)
else:
value = str(value)
res[key] = value
return res
|
Make a copy of the `data` object, preparing it to be sent to the server.
The data will be sent via x-www-form-urlencoded or multipart/form-data mechanisms. Both of them work with
plain lists of key/value pairs, so this method converts the data into such format.
|
def get_banks_by_assessment_taken(self, assessment_taken_id):
"""Gets the list of ``Banks`` mapped to an ``AssessmentTaken``.
arg: assessment_taken_id (osid.id.Id): ``Id`` of an
``AssessmentTaken``
return: (osid.assessment.BankList) - list of banks
raise: NotFound - ``assessment_taken_id`` is not found
raise: NullArgument - ``assessment_taken_id`` is ``null``
raise: OperationFailed - unable to complete request
raise: PermissionDenied - authorization failure occurred
*compliance: mandatory -- This method must be implemented.*
"""
# Implemented from template for
# osid.resource.ResourceBinSession.get_bins_by_resource
mgr = self._get_provider_manager('ASSESSMENT', local=True)
lookup_session = mgr.get_bank_lookup_session(proxy=self._proxy)
return lookup_session.get_banks_by_ids(
self.get_bank_ids_by_assessment_taken(assessment_taken_id))
|
Gets the list of ``Banks`` mapped to an ``AssessmentTaken``.
arg: assessment_taken_id (osid.id.Id): ``Id`` of an
``AssessmentTaken``
return: (osid.assessment.BankList) - list of banks
raise: NotFound - ``assessment_taken_id`` is not found
raise: NullArgument - ``assessment_taken_id`` is ``null``
raise: OperationFailed - unable to complete request
raise: PermissionDenied - authorization failure occurred
*compliance: mandatory -- This method must be implemented.*
|
def collect_phrases (sent, ranks, spacy_nlp):
"""
iterator for collecting the noun phrases
"""
tail = 0
last_idx = sent[0].idx - 1
phrase = []
while tail < len(sent):
w = sent[tail]
if (w.word_id > 0) and (w.root in ranks) and ((w.idx - last_idx) == 1):
# keep collecting...
rl = RankedLexeme(text=w.raw.lower(), rank=ranks[w.root], ids=w.word_id, pos=w.pos.lower(), count=1)
phrase.append(rl)
else:
# just hit a phrase boundary
for text, p in enumerate_chunks(phrase, spacy_nlp):
if p:
id_list = [rl.ids for rl in p]
rank_list = [rl.rank for rl in p]
np_rl = RankedLexeme(text=text, rank=rank_list, ids=id_list, pos="np", count=1)
if DEBUG:
print(np_rl)
yield np_rl
phrase = []
last_idx = w.idx
tail += 1
|
iterator for collecting the noun phrases
|
def mem(args, opts):
"""
%prog mem database.fasta read1.fq [read2.fq]
Wrapper for `bwa mem`. Output will be read1.sam.
"""
dbfile, read1file = args[:2]
readtype = opts.readtype
pl = readtype or "illumina"
pf = op.basename(read1file).split(".")[0]
rg = opts.rg or r"@RG\tID:{0}\tSM:sm\tLB:lb\tPL:{1}".format(pf, pl)
dbfile = check_index(dbfile)
args[0] = dbfile
samfile, _, unmapped = get_samfile(read1file, dbfile,
bam=opts.bam, unmapped=opts.unmapped)
if not need_update(read1file, samfile):
logging.error("`{0}` exists. `bwa mem` already run.".format(samfile))
return "", samfile
cmd = "{} mem".format(opts.bwa)
'''
-M Mark shorter split hits as secondary (for Picard compatibility).
'''
cmd += " -M -t {0}".format(opts.cpus)
cmd += ' -R "{0}"'.format(rg)
if readtype:
cmd += " -x {0}".format(readtype)
cmd += " " + opts.extra
cmd += " ".join(args)
return cmd, samfile
|
%prog mem database.fasta read1.fq [read2.fq]
Wrapper for `bwa mem`. Output will be read1.sam.
|
def setmode(mode):
"""
You must call this method prior to using all other calls.
:param mode: the mode, one of :py:attr:`GPIO.BOARD`, :py:attr:`GPIO.BCM`,
:py:attr:`GPIO.SUNXI`, or a `dict` or `object` representing a custom
pin mapping.
"""
if hasattr(mode, '__getitem__'):
set_custom_pin_mappings(mode)
mode = CUSTOM
assert mode in [BCM, BOARD, SUNXI, CUSTOM]
global _mode
_mode = mode
|
You must call this method prior to using all other calls.
:param mode: the mode, one of :py:attr:`GPIO.BOARD`, :py:attr:`GPIO.BCM`,
:py:attr:`GPIO.SUNXI`, or a `dict` or `object` representing a custom
pin mapping.
|
def placeholders(cls,dic):
"""Placeholders for fields names and value binds"""
keys = [str(x) for x in dic]
entete = ",".join(keys)
placeholders = ",".join(cls.named_style.format(x) for x in keys)
entete = f"({entete})"
placeholders = f"({placeholders})"
return entete, placeholders
|
Placeholders for fields names and value binds
|
def _function(self):
"""
Waits until stopped to keep script live. Gui must handle calling of Toggle_NV function on mouse click.
"""
start_time = datetime.datetime.now()
# calculate stop time
if self.settings['wait_mode'] == 'absolute':
stop_time = start_time + datetime.timedelta(seconds= self.settings['wait_time'])
elif self.settings['wait_mode'] == 'loop_interval':
if self.last_execution is None:
stop_time = start_time
else:
loop_time = start_time - self.last_execution
wait_time = datetime.timedelta(seconds= self.settings['wait_time'])
if wait_time.total_seconds() <0:
stop_time = start_time
else:
stop_time = start_time + wait_time
else:
TypeError('unknown wait_mode')
current_time = start_time
while current_time<stop_time:
if self._abort:
break
current_time = datetime.datetime.now()
time.sleep(1)
self.progress = 100.*(current_time- start_time).total_seconds() / (stop_time - start_time).total_seconds()
self.updateProgress.emit(int(self.progress))
if self.settings['wait_mode'] == 'absolute':
self.last_execution = None
else:
self.last_execution = start_time
|
Waits until stopped to keep script live. Gui must handle calling of Toggle_NV function on mouse click.
|
def requires_lock(function):
"""
Decorator to check if the user owns the required lock.
The first argument must be the filename.
"""
def new_lock_requiring_function(self, filename, *args, **kwargs):
if self.owns_lock(filename):
return function(self, filename, *args, **kwargs)
else:
raise RequiresLockException()
return new_lock_requiring_function
|
Decorator to check if the user owns the required lock.
The first argument must be the filename.
|
def set_window_pos_callback(window, cbfun):
"""
Sets the position callback for the specified window.
Wrapper for:
GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* window, GLFWwindowposfun cbfun);
"""
window_addr = ctypes.cast(ctypes.pointer(window),
ctypes.POINTER(ctypes.c_long)).contents.value
if window_addr in _window_pos_callback_repository:
previous_callback = _window_pos_callback_repository[window_addr]
else:
previous_callback = None
if cbfun is None:
cbfun = 0
c_cbfun = _GLFWwindowposfun(cbfun)
_window_pos_callback_repository[window_addr] = (cbfun, c_cbfun)
cbfun = c_cbfun
_glfw.glfwSetWindowPosCallback(window, cbfun)
if previous_callback is not None and previous_callback[0] != 0:
return previous_callback[0]
|
Sets the position callback for the specified window.
Wrapper for:
GLFWwindowposfun glfwSetWindowPosCallback(GLFWwindow* window, GLFWwindowposfun cbfun);
|
def compute_gaussian_krnl(M):
"""Creates a gaussian kernel following Foote's paper."""
g = signal.gaussian(M, M // 3., sym=True)
G = np.dot(g.reshape(-1, 1), g.reshape(1, -1))
G[M // 2:, :M // 2] = -G[M // 2:, :M // 2]
G[:M // 2, M // 2:] = -G[:M // 2, M // 2:]
return G
|
Creates a gaussian kernel following Foote's paper.
|
def getsockopt(self, level, optname, *args, **kwargs):
"""get the value of a given socket option
the values for ``level`` and ``optname`` will usually come from
constants in the standard library ``socket`` module. consult the unix
manpage ``getsockopt(2)`` for more information.
:param level: the level of the requested socket option
:type level: int
:param optname: the specific socket option requested
:type optname: int
:param buflen:
the length of the buffer to use to collect the raw value of the
socket option. if provided, the buffer is returned as a string and
it is not parsed.
:type buflen: int
:returns: a string of the socket option's value
"""
return self._sock.getsockopt(level, optname, *args, **kwargs)
|
get the value of a given socket option
the values for ``level`` and ``optname`` will usually come from
constants in the standard library ``socket`` module. consult the unix
manpage ``getsockopt(2)`` for more information.
:param level: the level of the requested socket option
:type level: int
:param optname: the specific socket option requested
:type optname: int
:param buflen:
the length of the buffer to use to collect the raw value of the
socket option. if provided, the buffer is returned as a string and
it is not parsed.
:type buflen: int
:returns: a string of the socket option's value
|
def essl(lw):
"""ESS (Effective sample size) computed from log-weights.
Parameters
----------
lw: (N,) ndarray
log-weights
Returns
-------
float
the ESS of weights w = exp(lw), i.e. the quantity
sum(w**2) / (sum(w))**2
Note
----
The ESS is a popular criterion to determine how *uneven* are the weights.
Its value is in the range [1, N], it equals N when weights are constant,
and 1 if all weights but one are zero.
"""
w = np.exp(lw - lw.max())
return (w.sum())**2 / np.sum(w**2)
|
ESS (Effective sample size) computed from log-weights.
Parameters
----------
lw: (N,) ndarray
log-weights
Returns
-------
float
the ESS of weights w = exp(lw), i.e. the quantity
sum(w**2) / (sum(w))**2
Note
----
The ESS is a popular criterion to determine how *uneven* are the weights.
Its value is in the range [1, N], it equals N when weights are constant,
and 1 if all weights but one are zero.
|
def dskmi2(vrtces, plates, finscl, corscl, worksz, voxpsz, voxlsz, makvtl, spxisz):
"""
Make spatial index for a DSK type 2 segment. The index is returned
as a pair of arrays, one of type int and one of type
float. These arrays are suitable for use with the DSK type 2
writer dskw02.
http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/dskmi2_c.html
:param vrtces: Vertices
:type vrtces: NxM-Element Array of floats
:param plates: Plates
:type plates: NxM-Element Array of ints
:param finscl: Fine voxel scale
:type finscl: float
:param corscl: Coarse voxel scale
:type corscl: int
:param worksz: Workspace size
:type worksz: int
:param voxpsz: Voxel plate pointer array size
:type voxpsz: int
:param voxlsz: Voxel plate list array size
:type voxlsz: int
:param makvtl: Vertex plate list flag
:type makvtl: bool
:param spxisz: Spatial index integer component size
:type spxisz: int
:return: double precision and integer components of the spatial index of the segment.
:rtype: tuple
"""
nv = ctypes.c_int(len(vrtces))
vrtces = stypes.toDoubleMatrix(vrtces)
np = ctypes.c_int(len(plates))
plates = stypes.toIntMatrix(plates)
finscl = ctypes.c_double(finscl)
corscl = ctypes.c_int(corscl)
worksz = ctypes.c_int(worksz)
voxpsz = ctypes.c_int(voxpsz)
voxlsz = ctypes.c_int(voxlsz)
makvtl = ctypes.c_int(makvtl)
spxisz = ctypes.c_int(spxisz)
work = stypes.emptyIntMatrix(2, worksz)
spaixd = stypes.emptyDoubleVector(10) # SPICE_DSK02_SPADSZ
spaixi = stypes.emptyIntVector(spxisz)
libspice.dskmi2_c(nv, vrtces, np, plates, finscl, corscl, worksz, voxpsz, voxlsz, makvtl, spxisz, work, spaixd, spaixi)
return stypes.cVectorToPython(spaixd), stypes.cVectorToPython(spaixi)
|
Make spatial index for a DSK type 2 segment. The index is returned
as a pair of arrays, one of type int and one of type
float. These arrays are suitable for use with the DSK type 2
writer dskw02.
http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/cspice/dskmi2_c.html
:param vrtces: Vertices
:type vrtces: NxM-Element Array of floats
:param plates: Plates
:type plates: NxM-Element Array of ints
:param finscl: Fine voxel scale
:type finscl: float
:param corscl: Coarse voxel scale
:type corscl: int
:param worksz: Workspace size
:type worksz: int
:param voxpsz: Voxel plate pointer array size
:type voxpsz: int
:param voxlsz: Voxel plate list array size
:type voxlsz: int
:param makvtl: Vertex plate list flag
:type makvtl: bool
:param spxisz: Spatial index integer component size
:type spxisz: int
:return: double precision and integer components of the spatial index of the segment.
:rtype: tuple
|
def main():
"""This is the mainline.
It first invokes the pipermail archiver to add the message to the archive,
then calls the function above to do whatever with the archived message
after it's URL and path are known.
"""
listname = sys.argv[2]
hostname = sys.argv[1]
# We must get the list unlocked here because it is already locked in
# ArchRunner. This is safe because we aren't actually changing our list
# object. ArchRunner's lock plus pipermail's archive lock will prevent
# any race conditions.
mlist = MailList.MailList(listname, lock=False)
# We need a seekable file for processUnixMailbox()
f = StringIO(sys.stdin.read())
# If we don't need a Message.Message instance, we can skip the next and
# the imports of email and Message above.
msg = email.message_from_file(f, Message.Message)
h = HyperArch.HyperArchive(mlist)
# Get the message number for the next message
sequence = h.sequence
# and add the message.
h.processUnixMailbox(f)
f.close()
# Get the archive name, etc.
archive = h.archive
msgno = '%06d' % sequence
filename = msgno + '.html'
filepath = os.path.join(h.basedir, archive, filename)
h.close()
url = '%s%s/%s' % (mlist.GetBaseArchiveURL(), archive, filename)
ext_process(listname, hostname, url, filepath, msg)
|
This is the mainline.
It first invokes the pipermail archiver to add the message to the archive,
then calls the function above to do whatever with the archived message
after it's URL and path are known.
|
def folderitem(self, obj, item, index):
"""Service triggered each time an item is iterated in folderitems.
The use of this service prevents the extra-loops in child objects.
:obj: the instance of the class to be foldered
:item: dict containing the properties of the object to be used by
the template
:index: current index of the item
"""
cat = obj.getCategoryTitle()
cat_order = self.an_cats_order.get(cat)
if self.do_cats:
# category groups entries
item["category"] = cat
if (cat, cat_order) not in self.categories:
self.categories.append((cat, cat_order))
# Category
category = obj.getCategory()
if category:
title = category.Title()
url = category.absolute_url()
item["Category"] = title
item["replace"]["Category"] = get_link(url, value=title)
# Calculation
calculation = obj.getCalculation()
if calculation:
title = calculation.Title()
url = calculation.absolute_url()
item["Calculation"] = title
item["replace"]["Calculation"] = get_link(url, value=title)
# Methods
methods = obj.getMethods()
if methods:
links = map(
lambda m: get_link(
m.absolute_url(), value=m.Title(), css_class="link"),
methods)
item["replace"]["Methods"] = ", ".join(links)
# Max time allowed
maxtime = obj.MaxTimeAllowed
if maxtime:
item["MaxTimeAllowed"] = self.format_maxtime(maxtime)
# Price
item["Price"] = self.format_price(obj.Price)
# Duplicate Variation
dup_variation = obj.DuplicateVariation
if dup_variation:
item["DuplicateVariation"] = self.format_duplication_variation(
dup_variation)
# Icons
after_icons = ""
if obj.getAccredited():
after_icons += get_image(
"accredited.png", title=_("Accredited"))
if obj.getAttachmentOption() == "r":
after_icons += get_image(
"attach_reqd.png", title=_("Attachment required"))
if obj.getAttachmentOption() == "n":
after_icons += get_image(
"attach_no.png", title=_("Attachment not permitted"))
if after_icons:
item["after"]["Title"] = after_icons
return item
|
Service triggered each time an item is iterated in folderitems.
The use of this service prevents the extra-loops in child objects.
:obj: the instance of the class to be foldered
:item: dict containing the properties of the object to be used by
the template
:index: current index of the item
|
def update_ff(self, ff, mol2=False, force_ff_assign=False):
"""Manages assigning the force field parameters.
The aim of this method is to avoid unnecessary assignment of the
force field.
Parameters
----------
ff: BuffForceField
The force field to be used for scoring.
mol2: bool, optional
If true, mol2 style labels will also be used.
force_ff_assign: bool, optional
If true, the force field will be completely reassigned, ignoring the
cached parameters.
"""
aff = False
if force_ff_assign:
aff = True
elif 'assigned_ff' not in self.tags:
aff = True
elif not self.tags['assigned_ff']:
aff = True
if aff:
self.assign_force_field(ff, mol2=mol2)
return
|
Manages assigning the force field parameters.
The aim of this method is to avoid unnecessary assignment of the
force field.
Parameters
----------
ff: BuffForceField
The force field to be used for scoring.
mol2: bool, optional
If true, mol2 style labels will also be used.
force_ff_assign: bool, optional
If true, the force field will be completely reassigned, ignoring the
cached parameters.
|
def from_json_and_lambdas(cls, file: str, lambdas):
"""Builds a GrFN from a JSON object.
Args:
cls: The class variable for object creation.
file: Filename of a GrFN JSON file.
Returns:
type: A GroundedFunctionNetwork object.
"""
with open(file, "r") as f:
data = json.load(f)
return cls.from_dict(data, lambdas)
|
Builds a GrFN from a JSON object.
Args:
cls: The class variable for object creation.
file: Filename of a GrFN JSON file.
Returns:
type: A GroundedFunctionNetwork object.
|
def get_ec_names(ecfile, fasta_names):
"""
convert equivalence classes to their set of transcripts
"""
df = pd.read_table(ecfile, header=None, names=["ec", "transcripts"])
transcript_groups = [x.split(",") for x in df["transcripts"]]
transcripts = []
for group in transcript_groups:
transcripts.append(":".join([fasta_names[int(x)] for x in group]))
return transcripts
|
convert equivalence classes to their set of transcripts
|
def pprint(self, imports=None, prefix="\n ",unknown_value='<?>',
qualify=False, separator=""):
"""
Same as Parameterized.pprint, except that X.classname(Y
is replaced with X.classname.instance(Y
"""
r = Parameterized.pprint(self,imports,prefix,
unknown_value=unknown_value,
qualify=qualify,separator=separator)
classname=self.__class__.__name__
return r.replace(".%s("%classname,".%s.instance("%classname)
|
Same as Parameterized.pprint, except that X.classname(Y
is replaced with X.classname.instance(Y
|
def assemble(experiments,
backend=None,
qobj_id=None, qobj_header=None, # common run options
shots=1024, memory=False, max_credits=None, seed_simulator=None,
default_qubit_los=None, default_meas_los=None, # schedule run options
schedule_los=None, meas_level=2, meas_return='avg',
memory_slots=None, memory_slot_size=100, rep_time=None, parameter_binds=None,
config=None, seed=None, # deprecated
**run_config):
"""Assemble a list of circuits or pulse schedules into a Qobj.
This function serializes the payloads, which could be either circuits or schedules,
to create Qobj "experiments". It further annotates the experiment payload with
header and configurations.
Args:
experiments (QuantumCircuit or list[QuantumCircuit] or Schedule or list[Schedule]):
Circuit(s) or pulse schedule(s) to execute
backend (BaseBackend):
If set, some runtime options are automatically grabbed from
backend.configuration() and backend.defaults().
If any other option is explicitly set (e.g. rep_rate), it
will override the backend's.
If any other options is set in the run_config, it will
also override the backend's.
qobj_id (str):
String identifier to annotate the Qobj
qobj_header (QobjHeader or dict):
User input that will be inserted in Qobj header, and will also be
copied to the corresponding Result header. Headers do not affect the run.
shots (int):
Number of repetitions of each circuit, for sampling. Default: 2014
memory (bool):
If True, per-shot measurement bitstrings are returned as well
(provided the backend supports it). For OpenPulse jobs, only
measurement level 2 supports this option. Default: False
max_credits (int):
Maximum credits to spend on job. Default: 10
seed_simulator (int):
Random seed to control sampling, for when backend is a simulator
default_qubit_los (list):
List of default qubit lo frequencies
default_meas_los (list):
List of default meas lo frequencies
schedule_los (None or list[Union[Dict[PulseChannel, float], LoConfig]] or
Union[Dict[PulseChannel, float], LoConfig]):
Experiment LO configurations
meas_level (int):
Set the appropriate level of the measurement output for pulse experiments.
meas_return (str):
Level of measurement data for the backend to return
For `meas_level` 0 and 1:
"single" returns information from every shot.
"avg" returns average measurement output (averaged over number of shots).
memory_slots (int):
Number of classical memory slots used in this job.
memory_slot_size (int):
Size of each memory slot if the output is Level 0.
rep_time (int): repetition time of the experiment in μs.
The delay between experiments will be rep_time.
Must be from the list provided by the device.
parameter_binds (list[dict{Parameter: Value}]):
List of Parameter bindings over which the set of experiments will be
executed. Each list element (bind) should be of the form
{Parameter1: value1, Parameter2: value2, ...}. All binds will be
executed across all experiments, e.g. if parameter_binds is a
length-n list, and there are m experiments, a total of m x n
experiments will be run (one for each experiment/bind pair).
seed (int):
DEPRECATED in 0.8: use ``seed_simulator`` kwarg instead
config (dict):
DEPRECATED in 0.8: use run_config instead
run_config (dict):
extra arguments used to configure the run (e.g. for Aer configurable backends)
Refer to the backend documentation for details on these arguments
Returns:
Qobj: a qobj which can be run on a backend. Depending on the type of input,
this will be either a QasmQobj or a PulseQobj.
Raises:
QiskitError: if the input cannot be interpreted as either circuits or schedules
"""
# deprecation matter
if config:
warnings.warn('config is not used anymore. Set all configs in '
'run_config.', DeprecationWarning)
run_config = run_config or config
if seed:
warnings.warn('seed is deprecated in favor of seed_simulator.', DeprecationWarning)
seed_simulator = seed_simulator or seed
# Get RunConfig(s) that will be inserted in Qobj to configure the run
experiments = experiments if isinstance(experiments, list) else [experiments]
qobj_id, qobj_header, run_config = _parse_run_args(backend, qobj_id, qobj_header,
shots, memory, max_credits, seed_simulator,
default_qubit_los, default_meas_los,
schedule_los, meas_level, meas_return,
memory_slots, memory_slot_size, rep_time,
parameter_binds, **run_config)
# assemble either circuits or schedules
if all(isinstance(exp, QuantumCircuit) for exp in experiments):
# If circuits are parameterized, bind parameters and remove from run_config
bound_experiments, run_config = _expand_parameters(circuits=experiments,
run_config=run_config)
return assemble_circuits(circuits=bound_experiments, qobj_id=qobj_id,
qobj_header=qobj_header, run_config=run_config)
elif all(isinstance(exp, Schedule) for exp in experiments):
return assemble_schedules(schedules=experiments, qobj_id=qobj_id,
qobj_header=qobj_header, run_config=run_config)
else:
raise QiskitError("bad input to assemble() function; "
"must be either circuits or schedules")
|
Assemble a list of circuits or pulse schedules into a Qobj.
This function serializes the payloads, which could be either circuits or schedules,
to create Qobj "experiments". It further annotates the experiment payload with
header and configurations.
Args:
experiments (QuantumCircuit or list[QuantumCircuit] or Schedule or list[Schedule]):
Circuit(s) or pulse schedule(s) to execute
backend (BaseBackend):
If set, some runtime options are automatically grabbed from
backend.configuration() and backend.defaults().
If any other option is explicitly set (e.g. rep_rate), it
will override the backend's.
If any other options is set in the run_config, it will
also override the backend's.
qobj_id (str):
String identifier to annotate the Qobj
qobj_header (QobjHeader or dict):
User input that will be inserted in Qobj header, and will also be
copied to the corresponding Result header. Headers do not affect the run.
shots (int):
Number of repetitions of each circuit, for sampling. Default: 2014
memory (bool):
If True, per-shot measurement bitstrings are returned as well
(provided the backend supports it). For OpenPulse jobs, only
measurement level 2 supports this option. Default: False
max_credits (int):
Maximum credits to spend on job. Default: 10
seed_simulator (int):
Random seed to control sampling, for when backend is a simulator
default_qubit_los (list):
List of default qubit lo frequencies
default_meas_los (list):
List of default meas lo frequencies
schedule_los (None or list[Union[Dict[PulseChannel, float], LoConfig]] or
Union[Dict[PulseChannel, float], LoConfig]):
Experiment LO configurations
meas_level (int):
Set the appropriate level of the measurement output for pulse experiments.
meas_return (str):
Level of measurement data for the backend to return
For `meas_level` 0 and 1:
"single" returns information from every shot.
"avg" returns average measurement output (averaged over number of shots).
memory_slots (int):
Number of classical memory slots used in this job.
memory_slot_size (int):
Size of each memory slot if the output is Level 0.
rep_time (int): repetition time of the experiment in μs.
The delay between experiments will be rep_time.
Must be from the list provided by the device.
parameter_binds (list[dict{Parameter: Value}]):
List of Parameter bindings over which the set of experiments will be
executed. Each list element (bind) should be of the form
{Parameter1: value1, Parameter2: value2, ...}. All binds will be
executed across all experiments, e.g. if parameter_binds is a
length-n list, and there are m experiments, a total of m x n
experiments will be run (one for each experiment/bind pair).
seed (int):
DEPRECATED in 0.8: use ``seed_simulator`` kwarg instead
config (dict):
DEPRECATED in 0.8: use run_config instead
run_config (dict):
extra arguments used to configure the run (e.g. for Aer configurable backends)
Refer to the backend documentation for details on these arguments
Returns:
Qobj: a qobj which can be run on a backend. Depending on the type of input,
this will be either a QasmQobj or a PulseQobj.
Raises:
QiskitError: if the input cannot be interpreted as either circuits or schedules
|
def format_subpages(self, page, subpages):
"""Banana banana
"""
if not subpages:
return None
try:
template = self.get_template('subpages.html')
except IOError:
return None
ret = etree.XML(template.render({'page': page,
'subpages': subpages}))
assets = ret.xpath('.//*[@src]')
for asset in assets:
self.__lookup_asset(asset, self.extension.project, page)
return ret
|
Banana banana
|
def Q(self):
"""
The quality factor of the scale, or, the ratio of center frequencies
to bandwidths
"""
return np.array(list(self.center_frequencies)) \
/ np.array(list(self.bandwidths))
|
The quality factor of the scale, or, the ratio of center frequencies
to bandwidths
|
def recursive_build_tree(self, intervals):
"""
recursively builds a BST based on the elementary intervals.
each node is an array: [interval value, left descendent nodes, right descendent nodes, [ids]].
nodes with no descendents have a -1 value in left/right descendent positions.
for example, a node with two empty descendents:
[500, interval value
[-1,-1,-1,['id5','id6']], left descendent
[-1,-1,-1,['id4']], right descendent
['id1',id2',id3']] data values
"""
center = int(round(len(intervals) / 2))
left = intervals[:center]
right = intervals[center + 1:]
node = intervals[center]
if len(left) > 1:
left = self.recursive_build_tree(left)
elif len(left) == 1:
left = [left[0],[-1,-1,-1,[]],[-1,-1,-1,[]],[]]
else:
left = [-1,-1,-1,[]]
if len(right) > 1:
right = self.recursive_build_tree(right)
elif len(right) == 1:
right = [right[0],[-1,-1,-1,[]],[-1,-1,-1,[]],[]]
else:
right = [-1,-1,-1,[]]
return [node, left, right, []]
|
recursively builds a BST based on the elementary intervals.
each node is an array: [interval value, left descendent nodes, right descendent nodes, [ids]].
nodes with no descendents have a -1 value in left/right descendent positions.
for example, a node with two empty descendents:
[500, interval value
[-1,-1,-1,['id5','id6']], left descendent
[-1,-1,-1,['id4']], right descendent
['id1',id2',id3']] data values
|
def destroy(self, force=False):
"""
Like shutdown(), but also removes all accounts, hosts, etc., and
does not restart the queue. In other words, the queue can no longer
be used after calling this method.
:type force: bool
:param force: Whether to wait until all jobs were processed.
"""
try:
if not force:
self.join()
finally:
self._dbg(2, 'Destroying queue...')
self.workqueue.destroy()
self.account_manager.reset()
self.completed = 0
self.total = 0
self.failed = 0
self.status_bar_length = 0
self._dbg(2, 'Queue destroyed.')
self._del_status_bar()
|
Like shutdown(), but also removes all accounts, hosts, etc., and
does not restart the queue. In other words, the queue can no longer
be used after calling this method.
:type force: bool
:param force: Whether to wait until all jobs were processed.
|
def lease(self, lease_time, num_tasks, group_by_tag=False, tag=None, client=None):
""" Acquires a lease on the topmost N unowned tasks in the specified queue.
:type lease_time: int
:param lease_time: How long to lease this task, in seconds.
:type num_tasks: int
:param num_tasks: The number of tasks to lease.
:type group_by_tag: bool
:param group_by_tag: Optional. When True, returns tasks of the same tag. Specify which tag by using the
tag parameter. If tag is not specified, returns tasks of the same tag as the oldest task in the queue.
:type tag: string
:param tag: Optional. Only specify tag if groupByTag is true. If groupByTag is true and tag is not specified,
the tag is assumed to be that of the oldest task by ETA. I.e., the first available tag.
:type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the task's taskqueue.
:rtype: :class:`_TaskIterator`.
:returns: An iterator of tasks.
"""
client = self._require_client(client)
if group_by_tag:
query_params = {"leaseSecs": lease_time, "numTasks": num_tasks, "groupByTag": group_by_tag, "tag": tag}
else:
query_params = {"leaseSecs": lease_time, "numTasks": num_tasks}
response = client.connection.api_request(method='POST', path=self.path + "/tasks/lease",
query_params=query_params)
for item in response.get('items', []):
id = item.get('id')
task = Task(id, taskqueue=self)
task._set_properties(item)
yield task
|
Acquires a lease on the topmost N unowned tasks in the specified queue.
:type lease_time: int
:param lease_time: How long to lease this task, in seconds.
:type num_tasks: int
:param num_tasks: The number of tasks to lease.
:type group_by_tag: bool
:param group_by_tag: Optional. When True, returns tasks of the same tag. Specify which tag by using the
tag parameter. If tag is not specified, returns tasks of the same tag as the oldest task in the queue.
:type tag: string
:param tag: Optional. Only specify tag if groupByTag is true. If groupByTag is true and tag is not specified,
the tag is assumed to be that of the oldest task by ETA. I.e., the first available tag.
:type client: :class:`gcloud.taskqueue.client.Client` or ``NoneType``
:param client: Optional. The client to use. If not passed, falls back
to the ``client`` stored on the task's taskqueue.
:rtype: :class:`_TaskIterator`.
:returns: An iterator of tasks.
|
def compare(args):
"""
Compare the extant river with the given name to the passed JSON. The
command will exit with a return code of 0 if the named river is configured
as specified, and 1 otherwise.
"""
data = json.load(sys.stdin)
m = RiverManager(args.hosts)
if m.compare(args.name, data):
sys.exit(0)
else:
sys.exit(1)
|
Compare the extant river with the given name to the passed JSON. The
command will exit with a return code of 0 if the named river is configured
as specified, and 1 otherwise.
|
def adjustSizeConstraint(self):
"""
Adjusts the min/max size based on the current tab.
"""
widget = self.currentWidget()
if not widget:
return
offw = 4
offh = 4
#if self.tabBar().isVisible():
# offh += 20 # tab bar height
minw = min(widget.minimumWidth() + offw, MAX_INT)
minh = min(widget.minimumHeight() + offh, MAX_INT)
maxw = min(widget.maximumWidth() + offw, MAX_INT)
maxh = min(widget.maximumHeight() + offh, MAX_INT)
self.setMinimumSize(minw, minh)
self.setMaximumSize(maxw, maxh)
self.setSizePolicy(widget.sizePolicy())
|
Adjusts the min/max size based on the current tab.
|
def delete_last_line(self, file_path='', date=str(datetime.date.today())):
"""
The following code was modified from
http://stackoverflow.com/a/10289740 &
http://stackoverflow.com/a/17309010
It essentially will check if the total for the current date already
exists in total.csv. If it does, it just removes the last line.
This is so the script could be run more than once a day and not
create many entries in the total.csv file for the same date.
"""
deleted_line = False
if os.path.isfile(file_path):
with open(file_path, 'r+') as file:
reader = csv.reader(file, delimiter=',')
for row in reader:
if date == row[0]:
file.seek(0, os.SEEK_END)
pos = file.tell() - 1
while pos > 0 and file.read(1) != "\n":
pos -= 1
file.seek(pos, os.SEEK_SET)
if pos > 0:
file.seek(pos, os.SEEK_SET)
file.truncate()
deleted_line = True
break
if deleted_line: file.write('\n')
file.close()
|
The following code was modified from
http://stackoverflow.com/a/10289740 &
http://stackoverflow.com/a/17309010
It essentially will check if the total for the current date already
exists in total.csv. If it does, it just removes the last line.
This is so the script could be run more than once a day and not
create many entries in the total.csv file for the same date.
|
def get(data_label=None, destination_dir="."):
"""
Download sample data by data label. Labels can be listed by sample_data.data_urls.keys()
:param data_label: label of data. If it is set to None, all data are downloaded
:param destination_dir: output dir for data
:return:
"""
try:
os.mkdir(destination_dir)
except:
pass
if data_label is None:
data_label=data_urls.keys()
if type(data_label) == str:
data_label = [data_label]
for label in data_label:
# make all data:url have length 3
data_url = data_urls[label]
if type(data_url) == str:
# back compatibility
data_url = [data_url]
data_url.extend([None, None])
data_url = data_url[:3]
url, expected_hash, hash_path = data_url
if hash_path is None:
hash_path = label
try:
computed_hash = checksum(os.path.join(destination_dir, hash_path))
except:
# there is probably no checksumdir module
logger.warning("problem with sample_data.checksum()")
computed_hash = None
logger.info("dataset '" + label + "'")
logger.info("expected hash: '" + str(expected_hash) + "'")
logger.info("computed hash: '" + str(computed_hash) + "'")
if (computed_hash is not None) and (expected_hash == computed_hash):
logger.info("match ok - no download needed")
else:
logger.info("downloading")
downzip(url, destination=destination_dir)
logger.info("finished")
downloaded_hash = checksum(os.path.join(destination_dir, hash_path))
logger.info("downloaded hash: '" + str(downloaded_hash) + "'")
if downloaded_hash != expected_hash:
logger.warning("downloaded hash is different from expected hash\n" + \
"expected hash: '" + str(expected_hash) + "'\n" + \
"downloaded hash: '" + str(downloaded_hash) + "'\n")
|
Download sample data by data label. Labels can be listed by sample_data.data_urls.keys()
:param data_label: label of data. If it is set to None, all data are downloaded
:param destination_dir: output dir for data
:return:
|
def are_domains_equal(domain1, domain2):
"""Compare two International Domain Names.
:Parameters:
- `domain1`: domains name to compare
- `domain2`: domains name to compare
:Types:
- `domain1`: `unicode`
- `domain2`: `unicode`
:return: True `domain1` and `domain2` are equal as domain names."""
domain1 = domain1.encode("idna")
domain2 = domain2.encode("idna")
return domain1.lower() == domain2.lower()
|
Compare two International Domain Names.
:Parameters:
- `domain1`: domains name to compare
- `domain2`: domains name to compare
:Types:
- `domain1`: `unicode`
- `domain2`: `unicode`
:return: True `domain1` and `domain2` are equal as domain names.
|
def authorized(resp, remote):
"""Authorized callback handler for GitHub.
:param resp: The response.
:param remote: The remote application.
"""
if resp and 'error' in resp:
if resp['error'] == 'bad_verification_code':
# See https://developer.github.com/v3/oauth/#bad-verification-code
# which recommends starting auth flow again.
return redirect(url_for('invenio_oauthclient.login',
remote_app='github'))
elif resp['error'] in ['incorrect_client_credentials',
'redirect_uri_mismatch']:
raise OAuthResponseError(
'Application mis-configuration in GitHub', remote, resp
)
return authorized_signup_handler(resp, remote)
|
Authorized callback handler for GitHub.
:param resp: The response.
:param remote: The remote application.
|
def formatday(self, day, weekday):
"""Return a day as a table cell."""
super(MiniEventCalendar, self).formatday(day, weekday)
now = get_now()
self.day = day
if day == 0:
return '<td class="noday"> </td>' # day outside month
elif now.month == self.mo and now.year == self.yr and day == now.day:
if day in self.count:
self.popover_helper()
return self.wkday_today + self.anch + self.cal_event + self.end
else:
return self.wkday_today + self.anch + self.end
elif day in self.count:
self.popover_helper()
return self.wkday_not_today + self.anch + self.cal_event + self.end
else:
return self.wkday_not_today + self.anch + self.end
|
Return a day as a table cell.
|
def load(self):
"""
Extract tabular data as |TableData| instances from a CSV text object.
|load_source_desc_text|
:return:
Loaded table data.
|load_table_name_desc|
=================== ========================================
Format specifier Value after the replacement
=================== ========================================
``%(filename)s`` ``""``
``%(format_name)s`` ``"csv"``
``%(format_id)s`` |format_id_desc|
``%(global_id)s`` |global_id|
=================== ========================================
:rtype: |TableData| iterator
:raises pytablereader.DataError:
If the CSV data is invalid.
.. seealso::
:py:func:`csv.reader`
"""
self._validate()
self._logger.logging_load()
self._csv_reader = csv.reader(
six.StringIO(self.source.strip()),
delimiter=self.delimiter,
quotechar=self.quotechar,
strict=True,
skipinitialspace=True,
)
formatter = CsvTableFormatter(self._to_data_matrix())
formatter.accept(self)
return formatter.to_table_data()
|
Extract tabular data as |TableData| instances from a CSV text object.
|load_source_desc_text|
:return:
Loaded table data.
|load_table_name_desc|
=================== ========================================
Format specifier Value after the replacement
=================== ========================================
``%(filename)s`` ``""``
``%(format_name)s`` ``"csv"``
``%(format_id)s`` |format_id_desc|
``%(global_id)s`` |global_id|
=================== ========================================
:rtype: |TableData| iterator
:raises pytablereader.DataError:
If the CSV data is invalid.
.. seealso::
:py:func:`csv.reader`
|
def objects_reachable_from(obj):
"""
Return graph of objects reachable from *obj* via ``gc.get_referrers``.
Returns an :class:`~refcycle.object_graph.ObjectGraph` object holding all
objects reachable from the given one by following the output of
``gc.get_referrers``. Note that unlike the
:func:`~refcycle.creators.snapshot` function, the output graph may
include non-gc-tracked objects.
"""
# Depth-first search.
found = ObjectGraph.vertex_set()
to_process = [obj]
while to_process:
obj = to_process.pop()
found.add(obj)
for referent in gc.get_referents(obj):
if referent not in found:
to_process.append(referent)
return ObjectGraph(found)
|
Return graph of objects reachable from *obj* via ``gc.get_referrers``.
Returns an :class:`~refcycle.object_graph.ObjectGraph` object holding all
objects reachable from the given one by following the output of
``gc.get_referrers``. Note that unlike the
:func:`~refcycle.creators.snapshot` function, the output graph may
include non-gc-tracked objects.
|
def buy_open_order_quantity(self):
"""
[int] 买方向挂单量
"""
return sum(order.unfilled_quantity for order in self.open_orders if
order.side == SIDE.BUY and order.position_effect == POSITION_EFFECT.OPEN)
|
[int] 买方向挂单量
|
def emailclients(self, tag=None, fromdate=None, todate=None):
"""
Gets an overview of the email clients used to open your emails.
This is only recorded when open tracking is enabled for that email.
"""
return self.call("GET", "/stats/outbound/opens/emailclients", tag=tag, fromdate=fromdate, todate=todate)
|
Gets an overview of the email clients used to open your emails.
This is only recorded when open tracking is enabled for that email.
|
def start(self, **kwargs):
"""
Start this container. Similar to the ``docker start`` command, but
doesn't support attach options.
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
"""
return self.client.api.start(self.id, **kwargs)
|
Start this container. Similar to the ``docker start`` command, but
doesn't support attach options.
Raises:
:py:class:`docker.errors.APIError`
If the server returns an error.
|
def get_hacr_channels(db=None, gps=None, connection=None,
**conectkwargs):
"""Return the names of all channels present in the given HACR database
"""
# connect if needed
if connection is None:
if gps is None:
gps = from_gps('now')
if db is None:
db = get_database_names(gps, gps)[0]
connection = connect(db=db, **conectkwargs)
# query
out = query("select channel from job where monitorName = 'chacr'")
return [r[0] for r in out]
|
Return the names of all channels present in the given HACR database
|
def get(self, cls, id_field, id_val):
"""
Retrieve an object which `id_field` matches `id_val`. If it exists in
the cache, it will be fetched from Redis. If not, it will be fetched
via the `fetch` method and cached in Redis (unless the cache flag got
invalidated in the meantime).
"""
cache_key, flag_key = self.get_keys(cls, id_field, id_val)
result = self.get_cached_or_set_flag(keys=(cache_key, flag_key))
# in Lua, arrays cannot hold nil values, so e.g. if [1, nil] is returned,
# we'll only get [1] here. That's why we need to append None ourselves.
if len(result) == 1:
result.append(None)
previous_flag, cached_data = result
# if cached data was found, deserialize and return it
if cached_data is not None:
deserialized = self.deserialize(cls, cached_data)
# verify that the cached object matches our expectations
# if not, return from the persistant storage instead.
if self.verify(cls, id_field, id_val, deserialized):
return deserialized
else:
# invalidate the cache if it didn't pass verification
self.invalidate(cls, id_field, id_val)
obj = self.fetch(cls, id_field, id_val)
# If the flag wasn't previously set, then we set it and we're responsible
# for putting the item in the cache. Do this unless the cache got
# invalidated and the flag was removed.
if not previous_flag:
obj_serialized = self.serialize(obj)
self.cache(keys=(cache_key, flag_key), args=(obj_serialized,))
return obj
|
Retrieve an object which `id_field` matches `id_val`. If it exists in
the cache, it will be fetched from Redis. If not, it will be fetched
via the `fetch` method and cached in Redis (unless the cache flag got
invalidated in the meantime).
|
def disconnect_container_from_network(container, network_id):
'''
.. versionadded:: 2015.8.3
Disconnect container from network
container
Container name or ID
network_id
Network name or ID
CLI Examples:
.. code-block:: bash
salt myminion docker.disconnect_container_from_network web-1 mynet
salt myminion docker.disconnect_container_from_network web-1 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
'''
log.debug(
'Disconnecting container \'%s\' from network \'%s\'',
container, network_id
)
response = _client_wrapper('disconnect_container_from_network',
container,
network_id)
log.debug(
'Successfully disconnected container \'%s\' from network \'%s\'',
container, network_id
)
_clear_context()
return True
|
.. versionadded:: 2015.8.3
Disconnect container from network
container
Container name or ID
network_id
Network name or ID
CLI Examples:
.. code-block:: bash
salt myminion docker.disconnect_container_from_network web-1 mynet
salt myminion docker.disconnect_container_from_network web-1 1f9d2454d0872b68dd9e8744c6e7a4c66b86f10abaccc21e14f7f014f729b2bc
|
def format_property(name, value):
"""Format the name and value (both unicode) of a property as a string."""
result = b''
utf8_name = utf8_bytes_string(name)
result = b'property ' + utf8_name
if value is not None:
utf8_value = utf8_bytes_string(value)
result += b' ' + ('%d' % len(utf8_value)).encode('ascii') + b' ' + utf8_value
return result
|
Format the name and value (both unicode) of a property as a string.
|
def get_astrom(official='%',provisional='%'):
"""Query the measure table for all measurements of a particular object.
Default is to return all the astrometry in the measure table,
sorted by mjdate"""
sql= "SELECT m.* FROM measure m "
sql+="LEFT JOIN object o ON m.provisional LIKE o.provisional "
if not official:
sql+="WHERE o.official IS NULL"
else:
sql+="WHERE o.official LIKE '%s' " % ( official, )
sql+=" AND m.provisional LIKE '%s' " % ( provisional, )
cfeps.execute(sql)
return mk_dict(cfeps.fetchall(), cfeps.description)
|
Query the measure table for all measurements of a particular object.
Default is to return all the astrometry in the measure table,
sorted by mjdate
|
def get_realms_and_credentials(self, uri, http_method='GET', body=None,
headers=None):
"""Fetch realms and credentials for the presented request token.
:param uri: The full URI of the token request.
:param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
:param body: The request body as a string.
:param headers: The request headers as a dict.
:returns: A tuple of 2 elements.
1. A list of request realms.
2. A dict of credentials which may be useful in creating the
authorization form.
"""
request = self._create_request(uri, http_method=http_method, body=body,
headers=headers)
if not self.request_validator.verify_request_token(
request.resource_owner_key, request):
raise errors.InvalidClientError()
realms = self.request_validator.get_realms(
request.resource_owner_key, request)
return realms, {'resource_owner_key': request.resource_owner_key}
|
Fetch realms and credentials for the presented request token.
:param uri: The full URI of the token request.
:param http_method: A valid HTTP verb, i.e. GET, POST, PUT, HEAD, etc.
:param body: The request body as a string.
:param headers: The request headers as a dict.
:returns: A tuple of 2 elements.
1. A list of request realms.
2. A dict of credentials which may be useful in creating the
authorization form.
|
def _removeSegment(self, segment, preserveCurve, **kwargs):
"""
segment will be a valid segment index.
preserveCurve will be a boolean.
Subclasses may override this method.
"""
segment = self.segments[segment]
for point in segment.points:
self.removePoint(point, preserveCurve)
|
segment will be a valid segment index.
preserveCurve will be a boolean.
Subclasses may override this method.
|
def try_utf8_decode(value):
"""Try to decode an object.
:param value:
:return:
"""
if not value or not is_string(value):
return value
elif PYTHON3 and not isinstance(value, bytes):
return value
elif not PYTHON3 and not isinstance(value, unicode):
return value
try:
return value.decode('utf-8')
except UnicodeDecodeError:
pass
return value
|
Try to decode an object.
:param value:
:return:
|
def read_bitpacked_deprecated(file_obj, byte_count, count, width, debug_logging):
"""Read `count` values from `fo` using the deprecated bitpacking encoding."""
raw_bytes = array.array(ARRAY_BYTE_STR, file_obj.read(byte_count)).tolist()
mask = _mask_for_bits(width)
index = 0
res = []
word = 0
bits_in_word = 0
while len(res) < count and index <= len(raw_bytes):
if debug_logging:
logger.debug("index = %d", index)
logger.debug("bits in word = %d", bits_in_word)
logger.debug("word = %s", bin(word))
if bits_in_word >= width:
# how many bits over the value is stored
offset = (bits_in_word - width)
# figure out the value
value = (word & (mask << offset)) >> offset
if debug_logging:
logger.debug("offset = %d", offset)
logger.debug("value = %d (%s)", value, bin(value))
res.append(value)
bits_in_word -= width
else:
word = (word << 8) | raw_bytes[index]
index += 1
bits_in_word += 8
return res
|
Read `count` values from `fo` using the deprecated bitpacking encoding.
|
def do_cspvarica(self, varfit='ensemble', random_state=None):
""" Perform CSPVARICA
Perform CSPVARICA source decomposition and VAR model fitting.
Parameters
----------
varfit : string
Determines how to calculate the residuals for source decomposition.
'ensemble' (default) fits one model to the whole data set,
'class' fits a different model for each class, and
'trial' fits a different model for each individual trial.
Returns
-------
self : Workspace
The Workspace object.
Raises
------
RuntimeError
If the :class:`Workspace` instance does not contain data.
See Also
--------
:func:`cspvarica` : CSPVARICA implementation
"""
if self.data_ is None:
raise RuntimeError("CSPVARICA requires data to be set")
try:
sorted(self.cl_)
for c in self.cl_:
assert(c is not None)
except (TypeError, AssertionError):
raise RuntimeError("CSPVARICA requires orderable and hashable class labels that are not None")
result = cspvarica(x=self.data_, var=self.var_, cl=self.cl_,
reducedim=self.reducedim_, backend=self.backend_,
varfit=varfit, random_state=random_state)
self.mixing_ = result.mixing
self.unmixing_ = result.unmixing
self.var_ = result.b
self.connectivity_ = Connectivity(self.var_.coef, self.var_.rescov, self.nfft_)
self.activations_ = dot_special(self.unmixing_.T, self.data_)
self.mixmaps_ = []
self.unmixmaps_ = []
return self
|
Perform CSPVARICA
Perform CSPVARICA source decomposition and VAR model fitting.
Parameters
----------
varfit : string
Determines how to calculate the residuals for source decomposition.
'ensemble' (default) fits one model to the whole data set,
'class' fits a different model for each class, and
'trial' fits a different model for each individual trial.
Returns
-------
self : Workspace
The Workspace object.
Raises
------
RuntimeError
If the :class:`Workspace` instance does not contain data.
See Also
--------
:func:`cspvarica` : CSPVARICA implementation
|
def timezone(self):
"""The name of the time zone for the location.
A list of time zone names can be obtained from pytz. For example.
>>> from pytz import all_timezones
>>> for timezone in all_timezones:
... print(timezone)
"""
if not self._timezone_group and not self._timezone_location:
return None
if self._timezone_location != "":
return "%s/%s" % (self._timezone_group, self._timezone_location)
else:
return self._timezone_group
|
The name of the time zone for the location.
A list of time zone names can be obtained from pytz. For example.
>>> from pytz import all_timezones
>>> for timezone in all_timezones:
... print(timezone)
|
def ancestral_states(self, n):
"""
Generate ancestral sequence states from the equilibrium frequencies
"""
anc = np.empty(n, dtype=np.intc)
_weighted_choices(self.state_indices, self.freqs, anc)
return anc
|
Generate ancestral sequence states from the equilibrium frequencies
|
def asset_view_prj(self, ):
"""View the project of the current asset
:returns: None
:rtype: None
:raises: None
"""
if not self.cur_asset:
return
prj = self.cur_asset.project
self.view_prj(prj)
|
View the project of the current asset
:returns: None
:rtype: None
:raises: None
|
def image_plane_pix_grid_from_regular_grid(self, regular_grid):
"""Calculate the image-plane pixelization from a regular-grid of coordinates (and its mask).
See *grid_stacks.SparseToRegularGrid* for details on how this grid is calculated.
Parameters
-----------
regular_grid : grids.RegularGrid
The grid of (y,x) arc-second coordinates at the centre of every image value (e.g. image-pixels).
"""
pixel_scale = regular_grid.mask.pixel_scale
pixel_scales = ((regular_grid.masked_shape_arcsec[0] + pixel_scale) / (self.shape[0]),
(regular_grid.masked_shape_arcsec[1] + pixel_scale) / (self.shape[1]))
return grids.SparseToRegularGrid(unmasked_sparse_grid_shape=self.shape, pixel_scales=pixel_scales,
regular_grid=regular_grid, origin=regular_grid.mask.centre)
|
Calculate the image-plane pixelization from a regular-grid of coordinates (and its mask).
See *grid_stacks.SparseToRegularGrid* for details on how this grid is calculated.
Parameters
-----------
regular_grid : grids.RegularGrid
The grid of (y,x) arc-second coordinates at the centre of every image value (e.g. image-pixels).
|
def accept_ws(buf, pos):
"""Skip whitespace at the current buffer position."""
match = re_ws.match(buf, pos)
if not match:
return None, pos
return buf[match.start(0):match.end(0)], match.end(0)
|
Skip whitespace at the current buffer position.
|
def should_recover(self):
"""Returns whether the trial qualifies for restoring.
This is if a checkpoint frequency is set and has not failed more than
max_failures. This may return true even when there may not yet
be a checkpoint.
"""
return (self.checkpoint_freq > 0
and (self.num_failures < self.max_failures
or self.max_failures < 0))
|
Returns whether the trial qualifies for restoring.
This is if a checkpoint frequency is set and has not failed more than
max_failures. This may return true even when there may not yet
be a checkpoint.
|
def use_pickle():
"""Revert to using stdlib pickle.
Reverts custom serialization enabled by use_dill|cloudpickle.
"""
from . import serialize
serialize.pickle = serialize._stdlib_pickle
# restore special function handling
can_map[FunctionType] = _original_can_map[FunctionType]
|
Revert to using stdlib pickle.
Reverts custom serialization enabled by use_dill|cloudpickle.
|
def wind_series(self):
"""Returns the wind speed time series relative to the
meteostation, in the form of a list of tuples, each one containing the
couple timestamp-value
:returns: a list of tuples
"""
return [(timestamp, \
self._station_history.get_measurements()[timestamp]['wind']) \
for timestamp in self._station_history.get_measurements()]
|
Returns the wind speed time series relative to the
meteostation, in the form of a list of tuples, each one containing the
couple timestamp-value
:returns: a list of tuples
|
def fromdict(dict):
"""Takes a dictionary as an argument and returns a new Challenge
object from the dictionary.
:param dict: the dictionary to convert
"""
seed = hb_decode(dict['seed'])
index = dict['index']
return Challenge(seed, index)
|
Takes a dictionary as an argument and returns a new Challenge
object from the dictionary.
:param dict: the dictionary to convert
|
def session_to_hour(timestamp):
""":param timestamp: as string in YYYYMMDDHHmmSS format
:return string in YYYYMMDDHH format"""
t = datetime.strptime(timestamp, SYNERGY_SESSION_PATTERN)
return t.strftime(SYNERGY_HOURLY_PATTERN)
|
:param timestamp: as string in YYYYMMDDHHmmSS format
:return string in YYYYMMDDHH format
|
def _http_headers(self):
"""Return dictionary of http headers necessary for making an http
connection to the endpoint of this Connection.
:return: Dictionary of headers
"""
if not self.usertag:
return {}
creds = u'{}:{}'.format(
self.usertag,
self.password or ''
)
token = base64.b64encode(creds.encode())
return {
'Authorization': 'Basic {}'.format(token.decode())
}
|
Return dictionary of http headers necessary for making an http
connection to the endpoint of this Connection.
:return: Dictionary of headers
|
def CheckAccess(self, token):
"""Enforce a dual approver policy for access."""
namespace, _ = self.urn.Split(2)
if namespace != "ACL":
raise access_control.UnauthorizedAccess(
"Approval object has invalid urn %s." % self.urn,
subject=self.urn,
requested_access=token.requested_access)
user, subject_urn = self.InferUserAndSubjectFromUrn()
if user != token.username:
raise access_control.UnauthorizedAccess(
"Approval object is not for user %s." % token.username,
subject=self.urn,
requested_access=token.requested_access)
now = rdfvalue.RDFDatetime.Now()
# Is this an emergency access?
break_glass = self.Get(self.Schema.BREAK_GLASS)
if break_glass and now < break_glass:
# This tags the token as an emergency token.
token.is_emergency = True
return True
# Check that there are enough approvers.
approvers = self.GetNonExpiredApprovers()
approvers_required = config.CONFIG["ACL.approvers_required"]
if len(approvers) < approvers_required:
missing = approvers_required - len(approvers)
msg = ("Need at least %d additional approver%s for access." %
(missing, "s" if missing > 1 else ""))
raise access_control.UnauthorizedAccess(
msg, subject=subject_urn, requested_access=token.requested_access)
# Check User labels
if self.checked_approvers_label:
approvers_with_label = []
# We need to check labels with high privilege since normal users can
# inspect other user's labels.
for approver in approvers:
try:
user = aff4.FACTORY.Open(
"aff4:/users/%s" % approver,
aff4_type=aff4_users.GRRUser,
token=token.SetUID())
if self.checked_approvers_label in user.GetLabelsNames():
approvers_with_label.append(approver)
except IOError:
pass
if len(approvers_with_label) < self.min_approvers_with_label:
missing = self.min_approvers_with_label - len(approvers_with_label)
raise access_control.UnauthorizedAccess(
"Need at least 1 admin approver for access.",
subject=subject_urn,
requested_access=token.requested_access)
return True
|
Enforce a dual approver policy for access.
|
def combine_sample_regions(*samples):
"""Create batch-level sets of callable regions for multi-sample calling.
Intersects all non-callable (nblock) regions from all samples in a batch,
producing a global set of callable regions.
"""
samples = utils.unpack_worlds(samples)
samples = cwlutils.unpack_tarballs(samples, samples[0])
# back compatibility -- global file for entire sample set
global_analysis_file = os.path.join(samples[0]["dirs"]["work"], "analysis_blocks.bed")
if utils.file_exists(global_analysis_file) and not _needs_region_update(global_analysis_file, samples):
global_no_analysis_file = os.path.join(os.path.dirname(global_analysis_file), "noanalysis_blocks.bed")
else:
global_analysis_file = None
out = []
analysis_files = []
batches = []
with shared.bedtools_tmpdir(samples[0]):
for batch, items in vmulti.group_by_batch(samples, require_bam=False).items():
batches.append(items)
if global_analysis_file:
analysis_file, no_analysis_file = global_analysis_file, global_no_analysis_file
else:
analysis_file, no_analysis_file = _combine_sample_regions_batch(batch, items)
for data in items:
vr_file = dd.get_variant_regions(data)
if analysis_file:
analysis_files.append(analysis_file)
data["config"]["algorithm"]["callable_regions"] = analysis_file
data["config"]["algorithm"]["non_callable_regions"] = no_analysis_file
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(analysis_file).count()
elif vr_file:
data["config"]["algorithm"]["callable_count"] = pybedtools.BedTool(vr_file).count()
# attach a representative sample for calculating callable region
if not data.get("work_bam"):
for x in items:
if x.get("work_bam"):
data["work_bam_callable"] = x["work_bam"]
out.append([data])
# Ensure output order matches input order, consistency for CWL-based runs
assert len(out) == len(samples)
sample_indexes = {dd.get_sample_name(d): i for i, d in enumerate(samples)}
def by_input_index(xs):
return sample_indexes[dd.get_sample_name(xs[0])]
out.sort(key=by_input_index)
if len(analysis_files) > 0:
final_regions = pybedtools.BedTool(analysis_files[0])
_analysis_block_stats(final_regions, batches[0])
return out
|
Create batch-level sets of callable regions for multi-sample calling.
Intersects all non-callable (nblock) regions from all samples in a batch,
producing a global set of callable regions.
|
def power_off(env, identifier, hard):
"""Power off an active virtual server."""
virtual_guest = env.client['Virtual_Guest']
vsi = SoftLayer.VSManager(env.client)
vs_id = helpers.resolve_id(vsi.resolve_ids, identifier, 'VS')
if not (env.skip_confirmations or
formatting.confirm('This will power off the VS with id %s. '
'Continue?' % vs_id)):
raise exceptions.CLIAbort('Aborted.')
if hard:
virtual_guest.powerOff(id=vs_id)
else:
virtual_guest.powerOffSoft(id=vs_id)
|
Power off an active virtual server.
|
def _convert(cls, record):
"""
Core method of the converter. Converts a single dictionary into another dictionary.
"""
if not record:
return {}
converted_dict = {}
for field in cls.conversion:
key = field[0]
if len(field) >= 2 and field[1]:
converted_key = field[1]
else:
converted_key = key
if len(field) >= 3 and field[2]:
conversion_method = field[2]
else:
conversion_method = cls.default_conversion_method
if len(field) >= 4:
converter = field[3]
else:
converter = None
try:
value = conversion_method(record[key])
except KeyError:
continue
if converter:
value = converter._convert_internal(value)
if converted_key is APPEND:
if isinstance(value, list):
for v in value:
converted_dict.update(v)
else:
converted_dict.update(value)
else:
converted_dict[converted_key] = value
return converted_dict
|
Core method of the converter. Converts a single dictionary into another dictionary.
|
def on_redraw(self):
"""
Called when the Layer should be redrawn.
If a subclass uses the :py:meth:`initialize()` Method, it is very important to also call the Super Class Method to prevent crashes.
"""
super(WidgetLayer,self).on_redraw()
if not self._initialized:
self.initialize()
self._initialized = True
|
Called when the Layer should be redrawn.
If a subclass uses the :py:meth:`initialize()` Method, it is very important to also call the Super Class Method to prevent crashes.
|
def Wp(self):
"""Total energy in protons
"""
Wp = trapz_loglog(self._Ep * self._J, self._Ep) * u.GeV
return Wp.to("erg")
|
Total energy in protons
|
def skip(self, num_bytes):
"""Jump the ahead the specified bytes in the buffer."""
if num_bytes is None:
self._offset = len(self._data)
else:
self._offset += num_bytes
|
Jump the ahead the specified bytes in the buffer.
|
def _add_child(self, child, logical_block_size, allow_duplicate, check_overflow):
# type: (DirectoryRecord, int, bool, bool) -> bool
'''
An internal method to add a child to this object. Note that this is called both
during parsing and when adding a new object to the system, so it
it shouldn't have any functionality that is not appropriate for both.
Parameters:
child - The child directory record object to add.
logical_block_size - The size of a logical block for this volume descriptor.
allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed.
check_overflow - Whether to check for overflow; if we are parsing, we don't want to do this.
Returns:
True if adding this child caused the directory to overflow into another
extent, False otherwise.
'''
if not self.isdir:
raise pycdlibexception.PyCdlibInvalidInput('Trying to add a child to a record that is not a directory')
# First ensure that this is not a duplicate. For speed purposes, we
# recognize that bisect_left will always choose an index to the *left*
# of a duplicate child. Thus, to check for duplicates we only need to
# see if the child to be added is a duplicate with the entry that
# bisect_left returned.
index = bisect.bisect_left(self.children, child)
if index != len(self.children) and self.children[index].file_ident == child.file_ident:
if not self.children[index].is_associated_file() and not child.is_associated_file():
if not (self.rock_ridge is not None and self.file_identifier() == b'RR_MOVED'):
if not allow_duplicate:
raise pycdlibexception.PyCdlibInvalidInput('Failed adding duplicate name to parent')
else:
self.children[index].data_continuation = child
index += 1
self.children.insert(index, child)
if child.rock_ridge is not None and not child.is_dot() and not child.is_dotdot():
lo = 0
hi = len(self.rr_children)
while lo < hi:
mid = (lo + hi) // 2
rr = self.rr_children[mid].rock_ridge
if rr is not None:
if rr.name() < child.rock_ridge.name():
lo = mid + 1
else:
hi = mid
else:
raise pycdlibexception.PyCdlibInternalError('Expected all children to have Rock Ridge, but one did not')
rr_index = lo
self.rr_children.insert(rr_index, child)
# We now have to check if we need to add another logical block.
# We have to iterate over the entire list again, because where we
# placed this last entry may rearrange the empty spaces in the blocks
# that we've already allocated.
num_extents, offset_unused = self._recalculate_extents_and_offsets(index,
logical_block_size)
overflowed = False
if check_overflow and (num_extents * logical_block_size > self.data_length):
overflowed = True
# When we overflow our data length, we always add a full block.
self.data_length += logical_block_size
# We also have to make sure to update the length of the dot child,
# as that should always reflect the length.
self.children[0].data_length = self.data_length
# We also have to update all of the dotdot entries. If this is
# the root directory record (no parent), we first update the root
# dotdot entry. In all cases, we update the dotdot entry of all
# children that are directories.
if self.parent is None:
self.children[1].data_length = self.data_length
for c in self.children:
if not c.is_dir():
continue
if len(c.children) > 1:
c.children[1].data_length = self.data_length
return overflowed
|
An internal method to add a child to this object. Note that this is called both
during parsing and when adding a new object to the system, so it
it shouldn't have any functionality that is not appropriate for both.
Parameters:
child - The child directory record object to add.
logical_block_size - The size of a logical block for this volume descriptor.
allow_duplicate - Whether to allow duplicate names, as there are situations where duplicate children are allowed.
check_overflow - Whether to check for overflow; if we are parsing, we don't want to do this.
Returns:
True if adding this child caused the directory to overflow into another
extent, False otherwise.
|
def prepare_inputseries(self, ramflag: bool = True) -> None:
"""Call method |Element.prepare_inputseries| of all handled
|Element| objects."""
for element in printtools.progressbar(self):
element.prepare_inputseries(ramflag)
|
Call method |Element.prepare_inputseries| of all handled
|Element| objects.
|
def lvdisplay(lvname='', quiet=False):
'''
Return information about the logical volume(s)
lvname
logical device name
quiet
if the logical volume is not present, do not show any error
CLI Examples:
.. code-block:: bash
salt '*' lvm.lvdisplay
salt '*' lvm.lvdisplay /dev/vg_myserver/root
'''
ret = {}
cmd = ['lvdisplay', '-c']
if lvname:
cmd.append(lvname)
cmd_ret = __salt__['cmd.run_all'](cmd, python_shell=False,
ignore_retcode=quiet)
if cmd_ret['retcode'] != 0:
return {}
out = cmd_ret['stdout'].splitlines()
for line in out:
comps = line.strip().split(':')
ret[comps[0]] = {
'Logical Volume Name': comps[0],
'Volume Group Name': comps[1],
'Logical Volume Access': comps[2],
'Logical Volume Status': comps[3],
'Internal Logical Volume Number': comps[4],
'Open Logical Volumes': comps[5],
'Logical Volume Size': comps[6],
'Current Logical Extents Associated': comps[7],
'Allocated Logical Extents': comps[8],
'Allocation Policy': comps[9],
'Read Ahead Sectors': comps[10],
'Major Device Number': comps[11],
'Minor Device Number': comps[12],
}
return ret
|
Return information about the logical volume(s)
lvname
logical device name
quiet
if the logical volume is not present, do not show any error
CLI Examples:
.. code-block:: bash
salt '*' lvm.lvdisplay
salt '*' lvm.lvdisplay /dev/vg_myserver/root
|
def _process_field_queries(field_dictionary):
"""
We have a field_dictionary - we want to match the values for an elasticsearch "match" query
This is only potentially useful when trying to tune certain search operations
"""
def field_item(field):
""" format field match as "match" item for elasticsearch query """
return {
"match": {
field: field_dictionary[field]
}
}
return [field_item(field) for field in field_dictionary]
|
We have a field_dictionary - we want to match the values for an elasticsearch "match" query
This is only potentially useful when trying to tune certain search operations
|
def assemble(asmcode, pc=0, fork=DEFAULT_FORK):
""" Assemble an EVM program
:param asmcode: an evm assembler program
:type asmcode: str
:param pc: program counter of the first instruction(optional)
:type pc: int
:param fork: fork name (optional)
:type fork: str
:return: the hex representation of the bytecode
:rtype: str
Example use::
>>> assemble('''PUSH1 0x60\n \
BLOCKHASH\n \
MSTORE\n \
PUSH1 0x2\n \
PUSH2 0x100\n \
''')
...
b"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00"
"""
return b''.join(x.bytes for x in assemble_all(asmcode, pc=pc, fork=fork))
|
Assemble an EVM program
:param asmcode: an evm assembler program
:type asmcode: str
:param pc: program counter of the first instruction(optional)
:type pc: int
:param fork: fork name (optional)
:type fork: str
:return: the hex representation of the bytecode
:rtype: str
Example use::
>>> assemble('''PUSH1 0x60\n \
BLOCKHASH\n \
MSTORE\n \
PUSH1 0x2\n \
PUSH2 0x100\n \
''')
...
b"\x60\x60\x60\x40\x52\x60\x02\x61\x01\x00"
|
def read_mail(window):
"""
Reads late emails from IMAP server and displays them in the Window
:param window: window to display emails in
:return:
"""
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
(retcode, capabilities) = mail.login(LOGIN_EMAIL, LOGIN_PASSWORD)
mail.list()
typ, data = mail.select('Inbox')
n = 0
now = datetime.now()
# get messages from today
search_string = '(SENTON {}-{}-{})'.format(now.day, calendar.month_abbr[now.month], now.year)
(retcode, messages) = mail.search(None, search_string)
if retcode == 'OK':
msg_list = messages[0].split() # message numbers are separated by spaces, turn into list
msg_list.sort(reverse=True) # sort messages descending
for n, message in enumerate(msg_list):
if n >= MAX_EMAILS:
break
from_elem = window.FindElement('{}from'.format(n))
date_elem = window.FindElement('{}date'.format(n))
from_elem.Update('') # erase them so you know they're changing
date_elem.Update('')
window.Refresh()
typ, data = mail.fetch(message, '(RFC822)')
for response_part in data:
if isinstance(response_part, tuple):
original = email.message_from_bytes(response_part[1])
date_str = original['Date'][:22]
from_elem.Update(original['From'])
date_elem.Update(date_str)
window.Refresh()
|
Reads late emails from IMAP server and displays them in the Window
:param window: window to display emails in
:return:
|
def download_unzip(names=None, normalize_filenames=False, verbose=True):
r""" Download CSV or HTML tables listed in `names`, unzip and to DATA_PATH/`names`.csv .txt etc
TODO: move to web or data_utils or futils
Also normalizes file name extensions (.bin.gz -> .w2v.bin.gz).
Uses table in data_info.csv (internal DATA_INFO) to determine URL or file path from dataset name.
Also looks
If names or [names] is a valid URL then download it and create a name
from the url in BIG_URLS (not yet pushed to data_info.csv)
"""
names = [names] if isinstance(names, (str, basestring)) else names
# names = names or list(BIG_URLS.keys()) # download them all, if none specified!
file_paths = {}
for name in names:
created = create_big_url(name)
name = (created or name).lower().strip()
if name in BIG_URLS:
filepath = download_name(name, verbose=verbose)
if not filepath:
continue
file_paths[name] = normalize_ext_rename(filepath)
logger.debug('downloaded name={} to filepath={}'.format(name, file_paths[name]))
fplower = file_paths[name].lower()
if fplower.endswith('.tar.gz'):
logger.info('Extracting {}'.format(file_paths[name]))
file_paths[name] = untar(file_paths[name], verbose=verbose)
logger.debug('download_untar.filepaths=' + str(file_paths))
elif file_paths[name].lower().endswith('.zip'):
file_paths[name] = unzip(file_paths[name], verbose=verbose)
logger.debug('download_unzip.filepaths=' + str(file_paths))
else:
df = pd.read_html(DATA_INFO['url'][name], **DATA_INFO['downloader_kwargs'][name])[-1]
df.columns = clean_columns(df.columns)
file_paths[name] = os.path.join(DATA_PATH, name + '.csv')
df.to_csv(file_paths[name])
file_paths[name] = normalize_ext_rename(file_paths[name])
return file_paths
|
r""" Download CSV or HTML tables listed in `names`, unzip and to DATA_PATH/`names`.csv .txt etc
TODO: move to web or data_utils or futils
Also normalizes file name extensions (.bin.gz -> .w2v.bin.gz).
Uses table in data_info.csv (internal DATA_INFO) to determine URL or file path from dataset name.
Also looks
If names or [names] is a valid URL then download it and create a name
from the url in BIG_URLS (not yet pushed to data_info.csv)
|
def convert_to_string(data, headers, **_):
"""Convert all *data* and *headers* to strings.
Binary data that cannot be decoded is converted to a hexadecimal
representation via :func:`binascii.hexlify`.
:param iterable data: An :term:`iterable` (e.g. list) of rows.
:param iterable headers: The column headers.
:return: The processed data and headers.
:rtype: tuple
"""
return (([utils.to_string(v) for v in row] for row in data),
[utils.to_string(h) for h in headers])
|
Convert all *data* and *headers* to strings.
Binary data that cannot be decoded is converted to a hexadecimal
representation via :func:`binascii.hexlify`.
:param iterable data: An :term:`iterable` (e.g. list) of rows.
:param iterable headers: The column headers.
:return: The processed data and headers.
:rtype: tuple
|
def add_actors(self, doc:Document, event: str, cameo_code: int) -> List[Document]:
"""
Each event has two actors. The relationship of the event to the actors depends
on the cameo code and is defined by the mapping.
Args:
doc: the document containing the evence
event: one of "event1", "event2", or "event3"
cameo_code:
Returns: the documents created for each actor
"""
# Actor1
actor1_cdr = {
"ActorName": doc.cdr_document[self.attribute("Actor1Name")],
"ActorCountryCode": doc.cdr_document[self.attribute("Actor1CountryCode")],
"ActorKnownGroupCode": doc.cdr_document[self.attribute("Actor1KnownGroupCode")],
"ActorEthnicCode": doc.cdr_document[self.attribute("Actor1EthnicCode")],
"ActorReligion1Code": doc.cdr_document[self.attribute("Actor1Religion1Code")],
"ActorReligion2Code": doc.cdr_document[self.attribute("Actor1Religion2Code")],
"ActorType1Code": doc.cdr_document[self.attribute("Actor1Type1Code")],
"ActorType2Code": doc.cdr_document[self.attribute("Actor1Type2Code")],
"ActorType3Code": doc.cdr_document[self.attribute("Actor1Type3Code")],
"ActorGeo_Type": doc.cdr_document[self.attribute("Actor1Geo_Type")],
"ActorGeo_FullName": doc.cdr_document[self.attribute("Actor1Geo_FullName")],
"ActorGeo_CountryCode": doc.cdr_document[self.attribute("Actor1Geo_CountryCode")],
"ActorGeo_ADM1Code": doc.cdr_document[self.attribute("Actor1Geo_ADM1Code")],
"ActorGeo_Lat": doc.cdr_document[self.attribute("Actor1Geo_Lat")],
"ActorGeo_Long": doc.cdr_document[self.attribute("Actor1Geo_Long")],
"ActorGeo_FeatureID": doc.cdr_document[self.attribute("Actor1Geo_FeatureID")],
"dataset": "gdelt-actor"
}
actor1 = etk.create_document(actor1_cdr)
actor1.doc_id = doc.doc_id + "-actor1"
# Link actor1 to the event
actor_field = "participant"
actor_prop = self.mapping.actor_property(event, "actor1", cameo_code)
if actor_prop and self.actor_role.get(actor_prop):
actor_field = self.actor_role.get(actor_prop)
doc.kg.add_value(actor_field, actor1.doc_id)
# Actor2
actor2_cdr = {
"ActorName": doc.cdr_document[self.attribute("Actor2Name")],
"ActorCountryCode": doc.cdr_document[self.attribute("Actor2CountryCode")],
"ActorKnownGroupCode": doc.cdr_document[self.attribute("Actor2KnownGroupCode")],
"ActorEthnicCode": doc.cdr_document[self.attribute("Actor2EthnicCode")],
"ActorReligion1Code": doc.cdr_document[self.attribute("Actor2Religion1Code")],
"ActorReligion2Code": doc.cdr_document[self.attribute("Actor2Religion2Code")],
"ActorType1Code": doc.cdr_document[self.attribute("Actor2Type1Code")],
"ActorType2Code": doc.cdr_document[self.attribute("Actor2Type2Code")],
"ActorType3Code": doc.cdr_document[self.attribute("Actor2Type3Code")],
"ActorGeo_Type": doc.cdr_document[self.attribute("Actor2Geo_Type")],
"ActorGeo_FullName": doc.cdr_document[self.attribute("Actor2Geo_FullName")],
"ActorGeo_CountryCode": doc.cdr_document[self.attribute("Actor2Geo_CountryCode")],
"ActorGeo_ADM1Code": doc.cdr_document[self.attribute("Actor2Geo_ADM1Code")],
"ActorGeo_Lat": doc.cdr_document[self.attribute("Actor2Geo_Lat")],
"ActorGeo_Long": doc.cdr_document[self.attribute("Actor2Geo_Long")],
"ActorGeo_FeatureID": doc.cdr_document[self.attribute("Actor2Geo_FeatureID")],
"dataset": "gdelt-actor"
}
actor2 = etk.create_document(actor2_cdr)
actor2.doc_id = doc.doc_id + "-actor2"
# Link actor2 to the event
actor_field = "participant"
actor_prop = self.mapping.actor_property(event, "actor2", cameo_code)
if actor_prop and self.actor_role.get(actor_prop):
actor_field = self.actor_role.get(actor_prop)
doc.kg.add_value(actor_field, actor2.doc_id)
return [actor1, actor2]
|
Each event has two actors. The relationship of the event to the actors depends
on the cameo code and is defined by the mapping.
Args:
doc: the document containing the evence
event: one of "event1", "event2", or "event3"
cameo_code:
Returns: the documents created for each actor
|
def createNotification(self, ulOverlayHandle, ulUserValue, type_, pchText, style):
"""
Create a notification and enqueue it to be shown to the user.
An overlay handle is required to create a notification, as otherwise it would be impossible for a user to act on it.
To create a two-line notification, use a line break ('\n') to split the text into two lines.
The pImage argument may be NULL, in which case the specified overlay's icon will be used instead.
"""
fn = self.function_table.createNotification
pImage = NotificationBitmap_t()
pNotificationId = VRNotificationId()
result = fn(ulOverlayHandle, ulUserValue, type_, pchText, style, byref(pImage), byref(pNotificationId))
return result, pImage, pNotificationId
|
Create a notification and enqueue it to be shown to the user.
An overlay handle is required to create a notification, as otherwise it would be impossible for a user to act on it.
To create a two-line notification, use a line break ('\n') to split the text into two lines.
The pImage argument may be NULL, in which case the specified overlay's icon will be used instead.
|
def is_ancestor_of_repository(self, id_, repository_id):
"""Tests if an ``Id`` is an ancestor of a repository.
arg: id (osid.id.Id): an ``Id``
arg: repository_id (osid.id.Id): the Id of a repository
return: (boolean) - ``true`` if this ``id`` is an ancestor of
``repository_id,`` ``false`` otherwise
raise: NotFound - ``repository_id`` not found
raise: NullArgument - ``repository_id`` is ``null``
raise: OperationFailed - unable to complete request
raise: PermissionDenied - authorization failure
*compliance: mandatory -- This method must be implemented.*
*implementation notes*: If ``id`` not found return ``false``.
"""
# Implemented from template for
# osid.resource.BinHierarchySession.is_ancestor_of_bin
if self._catalog_session is not None:
return self._catalog_session.is_ancestor_of_catalog(id_=id_, catalog_id=repository_id)
return self._hierarchy_session.is_ancestor(id_=id_, ancestor_id=repository_id)
|
Tests if an ``Id`` is an ancestor of a repository.
arg: id (osid.id.Id): an ``Id``
arg: repository_id (osid.id.Id): the Id of a repository
return: (boolean) - ``true`` if this ``id`` is an ancestor of
``repository_id,`` ``false`` otherwise
raise: NotFound - ``repository_id`` not found
raise: NullArgument - ``repository_id`` is ``null``
raise: OperationFailed - unable to complete request
raise: PermissionDenied - authorization failure
*compliance: mandatory -- This method must be implemented.*
*implementation notes*: If ``id`` not found return ``false``.
|
def parse_personalities(personalities_line):
"""Parse the "personalities" line of ``/proc/mdstat``.
Lines are expected to be like:
Personalities : [linear] [raid0] [raid1] [raid5] [raid4] [raid6]
If they do not have this format, an error will be raised since it
would be considered an unexpected parsing error.
Parameters
----------
personalities_line : str
A single "Personalities" line from an ``/proc/mdstat`` files.
Returns
-------
A list of raid "personalities" listed on the line.
"""
tokens = personalities_line.split()
assert tokens.pop(0) == "Personalities"
assert tokens.pop(0) == ":"
personalities = []
for token in tokens:
assert token.startswith('[') and token.endswith(']')
personalities.append(token.strip('[]'))
return personalities
|
Parse the "personalities" line of ``/proc/mdstat``.
Lines are expected to be like:
Personalities : [linear] [raid0] [raid1] [raid5] [raid4] [raid6]
If they do not have this format, an error will be raised since it
would be considered an unexpected parsing error.
Parameters
----------
personalities_line : str
A single "Personalities" line from an ``/proc/mdstat`` files.
Returns
-------
A list of raid "personalities" listed on the line.
|
def noise_despike(sig, win=3, nlim=24., maxiter=4):
"""
Apply standard deviation filter to remove anomalous values.
Parameters
----------
win : int
The window used to calculate rolling statistics.
nlim : float
The number of standard deviations above the rolling
mean above which data are considered outliers.
Returns
-------
None
"""
if win % 2 != 1:
win += 1 # win must be odd
kernel = np.ones(win) / win # make convolution kernel
over = np.ones(len(sig), dtype=bool) # initialize bool array
# pad edges to avoid edge-effects
npad = int((win - 1) / 2)
over[:npad] = False
over[-npad:] = False
# set up monitoring
nloops = 0
# do the despiking
while any(over) and (nloops < maxiter):
rmean = np.convolve(sig, kernel, 'valid') # mean by convolution
rstd = rmean**0.5 # std = sqrt(signal), because count statistics
# identify where signal > mean + std * nlim (OR signa < mean - std *
# nlim)
# | (sig[npad:-npad] < rmean - nlim * rstd)
over[npad:-npad] = (sig[npad:-npad] > rmean + nlim * rstd)
# if any are over, replace them with mean of neighbours
if any(over):
# replace with values either side
# sig[over] = sig[np.roll(over, -1) | np.roll(over, 1)].reshape((sum(over), 2)).mean(1)
# replace with mean
sig[npad:-npad][over[npad:-npad]] = rmean[over[npad:-npad]]
nloops += 1
# repeat until no more removed.
return sig
|
Apply standard deviation filter to remove anomalous values.
Parameters
----------
win : int
The window used to calculate rolling statistics.
nlim : float
The number of standard deviations above the rolling
mean above which data are considered outliers.
Returns
-------
None
|
def from_dict(cls, serialized):
'''Create an error from a JSON-deserialized object
@param serialized the object holding the serialized error {dict}
'''
# Some servers return lower case field names for message and code.
# The Go client is tolerant of this, so be similarly tolerant here.
def field(name):
return serialized.get(name) or serialized.get(name.lower())
return Error(
code=field('Code'),
message=field('Message'),
info=ErrorInfo.from_dict(field('Info')),
version=bakery.LATEST_VERSION,
)
|
Create an error from a JSON-deserialized object
@param serialized the object holding the serialized error {dict}
|
def store_memory_object(self, mo, overwrite=True):
"""
This function optimizes a large store by storing a single reference to the :class:`SimMemoryObject` instead of
one for each byte.
:param memory_object: the memory object to store
"""
for p in self._containing_pages_mo(mo):
self._apply_object_to_page(p, mo, overwrite=overwrite)
self._update_range_mappings(mo.base, mo.object, mo.length)
|
This function optimizes a large store by storing a single reference to the :class:`SimMemoryObject` instead of
one for each byte.
:param memory_object: the memory object to store
|
def remove_shard(self, shard, drop_buffered_records=False):
"""Remove a Shard from the Coordinator. Drops all buffered records from the Shard.
If the Shard is active or a root, it is removed and any children promoted to those roles.
:param shard: The shard to remove
:type shard: :class:`~bloop.stream.shard.Shard`
:param bool drop_buffered_records:
Whether records from this shard should be removed.
Default is False.
"""
try:
self.roots.remove(shard)
except ValueError:
# Wasn't a root Shard
pass
else:
self.roots.extend(shard.children)
try:
self.active.remove(shard)
except ValueError:
# Wasn't an active Shard
pass
else:
self.active.extend(shard.children)
if drop_buffered_records:
# TODO can this be improved? Gets expensive for high-volume streams with large buffers
heap = self.buffer.heap
# Clear buffered records from the shard. Each record is (ordering, record, shard)
to_remove = [x for x in heap if x[2] is shard]
for x in to_remove:
heap.remove(x)
|
Remove a Shard from the Coordinator. Drops all buffered records from the Shard.
If the Shard is active or a root, it is removed and any children promoted to those roles.
:param shard: The shard to remove
:type shard: :class:`~bloop.stream.shard.Shard`
:param bool drop_buffered_records:
Whether records from this shard should be removed.
Default is False.
|
def _sigfigs(n, sigfigs=3):
'helper function to round a number to significant figures'
n = float(n)
if n == 0 or math.isnan(n): # avoid math domain errors
return n
return round(n, -int(math.floor(math.log10(abs(n))) - sigfigs + 1))
|
helper function to round a number to significant figures
|
def p_referenceInitializer(p):
"""referenceInitializer : objectHandle
| aliasIdentifier
"""
if p[1][0] == '$':
try:
p[0] = p.parser.aliases[p[1]]
except KeyError:
ce = CIMError(
CIM_ERR_FAILED,
_format("Unknown alias: {0!A}", p[1]))
ce.file_line = (p.parser.file, p.lexer.lineno)
raise ce
else:
p[0] = p[1]
|
referenceInitializer : objectHandle
| aliasIdentifier
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.