code stringlengths 51 2.38k | docstring stringlengths 4 15.2k |
|---|---|
def enable_tracing(self, thread_trace_func=None):
if self.frame_eval_func is not None:
self.frame_eval_func()
pydevd_tracing.SetTrace(self.dummy_trace_dispatch)
return
if thread_trace_func is None:
thread_trace_func = self.get_thread_local_trace_func()
... | Enables tracing.
If in regular mode (tracing), will set the tracing function to the tracing
function for this thread -- by default it's `PyDB.trace_dispatch`, but after
`PyDB.enable_tracing` is called with a `thread_trace_func`, the given function will
be the default for the given threa... |
def _CreateFeedMapping(client, feed_details):
feed_mapping_service = client.GetService('FeedMappingService',
version='v201809')
operation = {
'operand': {
'criterionType': DSA_PAGE_FEED_CRITERION_TYPE,
'feedId': feed_details.feed_id,
'at... | Creates the feed mapping for DSA page feeds.
Args:
client: an AdWordsClient instance.
feed_details: a _DSAFeedDetails instance. |
def add_to_package_numpy(self, root, ndarray, node_path, target, source_path, transform, custom_meta):
filehash = self.save_numpy(ndarray)
metahash = self.save_metadata(custom_meta)
self._add_to_package_contents(root, node_path, [filehash], target, source_path, transform, metahash) | Save a Numpy array to the store. |
def _reconnect(self):
if self.idle or self.closed:
LOGGER.debug('Attempting RabbitMQ reconnect in %s seconds',
self.reconnect_delay)
self.io_loop.call_later(self.reconnect_delay, self.connect)
return
LOGGER.warning('Reconnect called while %s',... | Schedule the next connection attempt if the class is not currently
closing. |
def workspace_backup_add(ctx):
backup_manager = WorkspaceBackupManager(Workspace(ctx.resolver, directory=ctx.directory, mets_basename=ctx.mets_basename, automatic_backup=ctx.automatic_backup))
backup_manager.add() | Create a new backup |
def _atomicModification(func):
def wrapper(*args, **kwargs):
self = args[0]
with self._qpart:
func(*args, **kwargs)
return wrapper | Decorator
Make document modification atomic |
def dt_avg(self, print_output=True, output_file="dt_query.csv"):
avg = self.dt.mean(axis=2)
if print_output:
np.savetxt(output_file, avg, delimiter=",")
return avg | Compute average document-topic matrix,
and print to file if print_output=True. |
def get_wrong_answer_ids(self):
id_list = []
for answer in self.get_wrong_answers():
id_list.append(answer.get_id())
return IdList(id_list) | provide this method to return only wrong answer ids |
def parse_inline(self, text):
element_list = self._build_inline_element_list()
return inline_parser.parse(
text, element_list, fallback=self.inline_elements['RawText']
) | Parses text into inline elements.
RawText is not considered in parsing but created as a wrapper of holes
that don't match any other elements.
:param text: the text to be parsed.
:returns: a list of inline elements. |
def _serialize_rules(rules):
result = [(rule_name, str(rule)) for rule_name, rule in rules.items()]
return sorted(result, key=lambda rule: rule[0]) | Serialize all the Rule object as string.
New string is used to compare the rules list. |
def add_selected(self, ):
browser = self.shot_browser if self.browser_tabw.currentIndex() == 1 else self.asset_browser
selelements = browser.selected_indexes(2)
if not selelements:
return
seltypes = browser.selected_indexes(3)
if not seltypes:
return
... | Create a new reftrack with the selected element and type and add it to the root.
:returns: None
:rtype: None
:raises: NotImplementedError |
def with_more_selectors(self, selectors):
if self.headers and self.headers[-1].is_selector:
new_selectors = extend_unique(
self.headers[-1].selectors,
selectors)
new_headers = self.headers[:-1] + (
BlockSelectorHeader(new_selectors),)
... | Return a new ancestry that also matches the given selectors. No
nesting is done. |
def ensure_on():
if get_status() == 'not-running':
if config.dbserver.multi_user:
sys.exit('Please start the DbServer: '
'see the documentation for details')
subprocess.Popen([sys.executable, '-m', 'openquake.server.dbserver',
'-l', 'INFO'])... | Start the DbServer if it is off |
def nested_dict_to_list(path, dic, exclusion=None):
result = []
exclusion = ['__self'] if exclusion is None else exclusion
for key, value in dic.items():
if not any([exclude in key for exclude in exclusion]):
if isinstance(value, dict):
aux = path + key + "/"
... | Transform nested dict to list |
def build_annotation_dict_any_filter(annotations: Mapping[str, Iterable[str]]) -> EdgePredicate:
if not annotations:
return keep_edge_permissive
@edge_predicate
def annotation_dict_any_filter(edge_data: EdgeData) -> bool:
return _annotation_dict_any_filter(edge_data, query=annotations)
r... | Build an edge predicate that passes for edges whose data dictionaries match the given dictionary.
If the given dictionary is empty, will always evaluate to true.
:param annotations: The annotation query dict to match |
def print_file_details_as_csv(self, fname, col_headers):
line = ''
qu = '"'
d = ','
for fld in col_headers:
if fld == "fullfilename":
line = line + qu + fname + qu + d
if fld == "name":
line = line + qu + os.path.basename(fn... | saves as csv format |
def contracts_version_expects_deposit_limits(contracts_version: Optional[str]) -> bool:
if contracts_version is None:
return True
if contracts_version == '0.3._':
return False
return compare(contracts_version, '0.9.0') > -1 | Answers whether TokenNetworkRegistry of the contracts_vesion needs deposit limits |
def _reference_rmvs(self, removes):
print("")
self.msg.template(78)
msg_pkg = "package"
if len(removes) > 1:
msg_pkg = "packages"
print("| Total {0} {1} removed".format(len(removes), msg_pkg))
self.msg.template(78)
for pkg in removes:
if no... | Prints all removed packages |
def iter_dialogs(
self, limit=None, *, offset_date=None, offset_id=0,
offset_peer=types.InputPeerEmpty(), ignore_migrated=False
):
return _DialogsIter(
self,
limit,
offset_date=offset_date,
offset_id=offset_id,
offset_peer=o... | Returns an iterator over the dialogs, yielding 'limit' at most.
Dialogs are the open "chats" or conversations with other people,
groups you have joined, or channels you are subscribed to.
Args:
limit (`int` | `None`):
How many dialogs to be retrieved as maximum. Can ... |
def get_generic_subseq_3D(protein, cutoff, prop, condition):
if not protein.representative_structure:
log.error('{}: no representative structure, cannot search for subseq'.format(protein.id))
return {'subseq_len': 0, 'subseq': None, 'subseq_resnums': []}
subseq, subseq_resnums = ... | Get a subsequence from REPSEQ based on a property stored in REPSTRUCT.REPCHAIN.letter_annotations |
def visit_ListComp(self, node: ast.ListComp) -> None:
if node in self._recomputed_values:
value = self._recomputed_values[node]
text = self._atok.get_text(node)
self.reprs[text] = value
self.generic_visit(node=node) | Represent the list comprehension by dumping its source code. |
def parse_iptables_rule(line):
bits = line.split()
definition = {}
key = None
args = []
not_arg = False
def add_args():
arg_string = ' '.join(args)
if key in IPTABLES_ARGS:
definition_key = (
'not_{0}'.format(IPTABLES_ARGS[key])
if not_... | Parse one iptables rule. Returns a dict where each iptables code argument
is mapped to a name using IPTABLES_ARGS. |
def renderer_doc(*args):
renderers_ = salt.loader.render(__opts__, [])
docs = {}
if not args:
for func in six.iterkeys(renderers_):
docs[func] = renderers_[func].__doc__
return _strip_rst(docs)
for module in args:
if '*' in module or '.' in module:
for fun... | Return the docstrings for all renderers. Optionally, specify a renderer or a
function to narrow the selection.
The strings are aggregated into a single document on the master for easy
reading.
Multiple renderers can be specified.
.. versionadded:: 2015.5.0
CLI Example:
.. code-block:: b... |
def _create_binary_trigger(trigger):
ops = {
0: ">",
1: "<",
2: ">=",
3: "<=",
4: "==",
5: 'always'
}
op_codes = {y: x for x, y in ops.items()}
source = 0
if isinstance(trigger, TrueTrigger):
op_code = op_codes['always']
elif isinstance(tri... | Create an 8-bit binary trigger from an InputTrigger, TrueTrigger, FalseTrigger. |
def match(self, p_todo):
operand1 = self.value
operand2 = p_todo.priority() or 'ZZ'
return self.compare_operands(operand1, operand2) | Performs a match on a priority in the todo.
It gets priority from p_todo and compares it with user-entered
expression based on the given operator (default ==). It does that however
in reversed order to obtain more intuitive result. Example: (>B) will
match todos with priority (A).
... |
def _validate_file_format(self, file_format):
if file_format not in self.valid_file_formats:
raise InvalidFileFormatError(
"{} is not a valid file format".format(file_format)
)
return file_format | Validates file format, raising error if invalid. |
def from_json(cls, data, json_schema_class=None):
schema = cls.json_schema if json_schema_class is None else json_schema_class()
return schema.load(data) | JSON deserialization method that retrieves a genome instance from its json representation
If specific json schema is provided, it is utilized, and if not, a class specific is used |
def upload_content(self, synchronous=True, **kwargs):
kwargs = kwargs.copy()
kwargs.update(self._server_config.get_client_kwargs())
response = client.post(self.path('upload_content'), **kwargs)
json = _handle_response(response, self._server_config, synchronous)
if json['status'] ... | Upload a file or files to the current repository.
Here is an example of how to upload content::
with open('my_content.rpm') as content:
repo.upload_content(files={'content': content})
This method accepts the same keyword arguments as Requests. As a
result, the foll... |
def copy(self, deep=True):
from copy import copy, deepcopy
if deep:
return deepcopy(self)
else:
return copy(self) | Make a copy of this object
Parameters
----------
deep : boolean, default True
Make a deep copy, i.e. also copy data
Returns
-------
copy : type of caller |
def _parse_file():
file_name = path.join(
path.abspath(path.dirname(path.dirname(__file__))), 'config.json'
)
logger.info('loading configuration from file: %r', file_name)
try:
data = _read_file(file_name)
except FileNotFoundError:
logger.error('no configuration available, se... | Parse the config from a file.
Note:
Assumes any value that ``"$LOOKS_LIKE_THIS"`` in a service
definition refers to an environment variable, and attempts to get
it accordingly. |
def tmp(p_queue, host=None):
if host is not None:
return _path(_c.FSQ_TMP, root=_path(host, root=hosts(p_queue)))
return _path(p_queue, _c.FSQ_TMP) | Construct a path to the tmp dir for a queue |
def _get_request_fields_from_parent(self):
if not self.parent:
return None
if not getattr(self.parent, 'request_fields'):
return None
if not isinstance(self.parent.request_fields, dict):
return None
return self.parent.request_fields.get(self.field_name... | Get request fields from the parent serializer. |
def release_subnet(self, cidr, direc):
if direc == 'in':
self.service_in_ip.release_subnet(cidr)
else:
self.service_out_ip.release_subnet(cidr) | Routine to release a subnet from the DB. |
def list(self, request):
max_items_per_page = getattr(self, 'max_per_page',
getattr(settings, 'AJAX_MAX_PER_PAGE', 100))
requested_items_per_page = request.POST.get("items_per_page", 20)
items_per_page = min(max_items_per_page, requested_items_per_page)
... | List objects of a model. By default will show page 1 with 20 objects on it.
**Usage**::
params = {"items_per_page":10,"page":2} //all params are optional
$.post("/ajax/{app}/{model}/list.json"),params) |
def parse_skypos(ra, dec):
rval = make_val_float(ra)
dval = make_val_float(dec)
if rval is None:
rval, dval = radec_hmstodd(ra, dec)
return rval, dval | Function to parse RA and Dec input values and turn them into decimal
degrees
Input formats could be:
["nn","nn","nn.nn"]
"nn nn nn.nnn"
"nn:nn:nn.nn"
"nnH nnM nn.nnS" or "nnD nnM nn.nnS"
nn.nnnnnnnn
"nn.nnnnnnn" |
def mfpt(totflux, pi, qminus):
r
return dense.tpt.mfpt(totflux, pi, qminus) | r"""Mean first passage time for reaction A to B.
Parameters
----------
totflux : float
The total flux between reactant and product
pi : (M,) ndarray
Stationary distribution
qminus : (M,) ndarray
Backward comittor
Returns
-------
tAB : float
The mean firs... |
def is_supported():
on_supported_platform = False
if salt.utils.platform.is_sunos():
on_supported_platform = True
elif salt.utils.platform.is_freebsd() and _check_retcode('kldstat -q -m zfs'):
on_supported_platform = True
elif salt.utils.platform.is_linux() and os.path.exists('/sys/modul... | Check the system for ZFS support |
def write_temp_file(self, content, filename=None, mode='w'):
if filename is None:
filename = str(uuid.uuid4())
fqpn = os.path.join(self.tcex.default_args.tc_temp_path, filename)
with open(fqpn, mode) as fh:
fh.write(content)
return fqpn | Write content to a temporary file.
Args:
content (bytes|str): The file content. If passing binary data the mode needs to be set
to 'wb'.
filename (str, optional): The filename to use when writing the file.
mode (str, optional): The file write mode which could... |
def url_view(url_pattern, name=None, priority=None):
def meta_wrapper(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.urljects_view = True
wrapper.url = url_pattern
wrapper.url_name = name or func.__name__
w... | Decorator for registering functional views.
Meta decorator syntax has to be used in order to accept arguments.
This decorator does not really do anything that magical:
This:
>>> from urljects import U, url_view
>>> @url_view(U / 'my_view')
... def my_view(request)
... pass
is equi... |
def _get_meaning(value_pb, is_list=False):
meaning = None
if is_list:
if len(value_pb.array_value.values) == 0:
return None
all_meanings = [
_get_meaning(sub_value_pb) for sub_value_pb in value_pb.array_value.values
]
unique_meanings = set(all_meanings)
... | Get the meaning from a protobuf value.
:type value_pb: :class:`.entity_pb2.Value`
:param value_pb: The protobuf value to be checked for an
associated meaning.
:type is_list: bool
:param is_list: Boolean indicating if the ``value_pb`` contains
a list value.
... |
def find_element(driver, elem_path, by=CSS, timeout=TIMEOUT, poll_frequency=0.5):
wait = WebDriverWait(driver, timeout, poll_frequency)
return wait.until(EC.presence_of_element_located((by, elem_path))) | Find and return an element once located
find_element locates an element on the page, waiting
for up to timeout seconds. The element, when located,
is returned. If not located, a TimeoutException is raised.
Args:
driver (selenium webdriver or element): A driver or element
elem_path (str... |
def exit_statistics(hostname, start_time, count_sent, count_received, min_time, avg_time, max_time, deviation):
end_time = datetime.datetime.now()
duration = end_time - start_time
duration_sec = float(duration.seconds * 1000)
duration_ms = float(duration.microseconds / 1000)
duration = duration_sec ... | Print ping exit statistics |
def _get_subelements(self, node):
items = node.find('rdf:Alt', self.NS)
if items is not None:
try:
return items[0].text
except IndexError:
return ''
for xmlcontainer, container, insertfn in XMP_CONTAINERS:
items = node.find('rdf... | Gather the sub-elements attached to a node
Gather rdf:Bag and and rdf:Seq into set and list respectively. For
alternate languages values, take the first language only for
simplicity. |
def _sigma_pi_midE(self, Tp):
m_p = self._m_p
Qp = (Tp - self._Tth) / m_p
multip = -6e-3 + 0.237 * Qp - 0.023 * Qp ** 2
return self._sigma_inel(Tp) * multip | Geant 4.10.0 model for 2 GeV < Tp < 5 GeV |
def ts_stats_significance(ts, ts_stat_func, null_ts_func, B=1000, permute_fast=False):
stats_ts = ts_stat_func(ts)
if permute_fast:
null_ts = map(np.random.permutation, np.array([ts, ] * B))
else:
null_ts = np.vstack([null_ts_func(ts) for i in np.arange(0, B)])
stats_null_ts = np.vstack(... | Compute the statistical significance of a test statistic at each point
of the time series. |
def breathe_identifier(self):
if self.kind == "function":
return "{name}({parameters})".format(
name=self.name,
parameters=", ".join(self.parameters)
)
return self.name | The unique identifier for breathe directives.
.. note::
This method is currently assumed to only be called for nodes that are
in :data:`exhale.utils.LEAF_LIKE_KINDS` (see also
:func:`exhale.graph.ExhaleRoot.generateSingleNodeRST` where it is used).
**Return**
... |
def table(name=None, mode='create', use_cache=True, priority='interactive',
allow_large_results=False):
output = QueryOutput()
output._output_type = 'table'
output._table_name = name
output._table_mode = mode
output._use_cache = use_cache
output._priority = priority
output._allow... | Construct a query output object where the result is a table
Args:
name: the result table name as a string or TableName; if None (the default), then a
temporary table will be used.
table_mode: one of 'create', 'overwrite' or 'append'. If 'create' (the default), the request
will fail ... |
def get_version(filename):
with open(filename) as in_fh:
for line in in_fh:
if line.startswith('__version__'):
return line.split('=')[1].strip()[1:-1]
raise ValueError("Cannot extract version from %s" % filename) | Extract the package version |
def load_notebook(fullname: str):
shell = InteractiveShell.instance()
path = fullname
with open(path, 'r', encoding='utf-8') as f:
notebook = read(f, 4)
mod = types.ModuleType(fullname)
mod.__file__ = path
mod.__dict__['get_ipython'] = get_ipython
sys.modules[fullname] = mod
save... | Import a notebook as a module. |
def display_element_selected(self, f):
self._display_element_selected_func = f
@wraps(f)
def wrapper(*args, **kw):
self._flask_view_func(*args, **kw)
return f | Decorator routes Alexa Display.ElementSelected request to the wrapped view function.
@ask.display_element_selected
def eval_element():
return "", 200
The wrapped function is registered as the display_element_selected view function
and renders the response for requests.
... |
def file_w_create_directories(filepath):
dirname = os.path.dirname(filepath)
if dirname and dirname != os.path.curdir and not os.path.isdir(dirname):
os.makedirs(dirname)
return open(filepath, 'w') | Recursively create some directories if needed so that the directory where
@filepath must be written exists, then open it in "w" mode and return the
file object. |
def get_start_time(self):
start_time = c_double()
self.library.get_start_time.argtypes = [POINTER(c_double)]
self.library.get_start_time.restype = None
self.library.get_start_time(byref(start_time))
return start_time.value | returns start time |
def duration(self):
self._duration = self.lib.iperf_get_test_duration(self._test)
return self._duration | The test duration in seconds. |
def sync_and_deploy_gateway(collector):
configuration = collector.configuration
aws_syncr = configuration['aws_syncr']
find_gateway(aws_syncr, configuration)
artifact = aws_syncr.artifact
aws_syncr.artifact = ""
sync(collector)
aws_syncr.artifact = artifact
deploy_gateway(collector) | Do a sync followed by deploying the gateway |
def generate_key(block_size=32):
random_seq = os.urandom(block_size)
random_key = base64.b64encode(random_seq)
return random_key | Generate random key for ope cipher.
Parameters
----------
block_size : int, optional
Length of random bytes.
Returns
-------
random_key : str
A random key for encryption.
Notes:
------
Implementation follows https://githu... |
def save_proficiency(self, proficiency_form, *args, **kwargs):
if proficiency_form.is_for_update():
return self.update_proficiency(proficiency_form, *args, **kwargs)
else:
return self.create_proficiency(proficiency_form, *args, **kwargs) | Pass through to provider ProficiencyAdminSession.update_proficiency |
def sort_resources(cls, request, resources, fail_enum, header_proto=None):
if not request.sorting:
return resources
value_handlers = cls._get_handler_set(request, fail_enum, header_proto)
def sorter(resource_a, resource_b):
for handler in value_handlers:
v... | Sorts a list of resources based on a list of sort controls
Args:
request (object): The parsed protobuf request object
resources (list of objects): The resources to be sorted
fail_enum (int, enum): The enum status to raise with invalid keys
header_proto(class): Cl... |
def generate_token(self):
response = self._make_request()
self.auth = response
self.token = response['token'] | Make request in API to generate a token. |
def on_interesting_rts_change(self, new_global_rts, removed_global_rts):
if new_global_rts:
LOG.debug(
'Sending route_refresh to all neighbors that'
' did not negotiate RTC capability.'
)
pm = self._core_service.peer_manager
pm.sche... | Update global tables as interested RTs changed.
Adds `new_rts` and removes `removed_rts` rt nlris. Does not check if
`new_rts` or `removed_rts` are already present. Schedules refresh
request to peers that do not participate in RTC address-family. |
def circular_pores(target, pore_diameter='pore.diameter',
throat_diameter='throat.diameter',
throat_centroid='throat.centroid'):
r
return spherical_pores(target=target, pore_diameter=pore_diameter,
throat_diameter=throat_diameter) | r"""
Calculate the coordinates of throat endpoints, assuming circular pores.
This model accounts for the overlapping lens between pores and throats.
Parameters
----------
target : OpenPNM Object
The object which this model is associated with. This controls the
length of the calculat... |
def do_diff(self, params):
count = 0
for count, (diff, path) in enumerate(self._zk.diff(params.path_a, params.path_b), 1):
if diff == -1:
self.show_output("-- %s", path)
elif diff == 0:
self.show_output("-+ %s", path)
elif diff == 1:
... | \x1b[1mNAME\x1b[0m
diff - Display the differences between two paths
\x1b[1mSYNOPSIS\x1b[0m
diff <src> <dst>
\x1b[1mDESCRIPTION\x1b[0m
The output is interpreted as:
-- means the znode is missing in /new-configs
++ means the znode is new in /new-configs
+- means the... |
def config_keys(self, sortkey = False):
if sortkey:
items = sorted(self.items())
else:
items = self.items()
for k,v in items:
if isinstance(v, ConfigTree):
for k2 in v.config_keys(sortkey):
yield k + '.' + k2
els... | Return all configuration keys in this node, including configurations on children nodes. |
def validate(data):
text = data.get('text')
if not isinstance(text, _string_types) or len(text) == 0:
raise ValueError('text field is required and should not be empty')
if 'markdown' in data and not type(data['markdown']) is bool:
raise ValueError('markdown field should be bool')
if 'att... | Validates incoming data
Args:
data(dict): the incoming data
Returns:
True if the data is valid
Raises:
ValueError: the data is not valid |
def surface_measure(self, param):
scalar_out = (np.shape(param) == ())
param = np.array(param, dtype=float, copy=False, ndmin=1)
if self.check_bounds and not is_inside_bounds(param, self.params):
raise ValueError('`param` {} not in the valid range '
'{}'.... | Return the arc length measure at ``param``.
This is a constant function evaluating to `radius` everywhere.
Parameters
----------
param : float or `array-like`
Parameter value(s) at which to evaluate.
Returns
-------
measure : float or `numpy.ndarray... |
def upload_path(instance, filename):
filename = filename.replace(" ", "_")
filename = unicodedata.normalize('NFKD', filename).lower()
return os.path.join(str(timezone.now().date().isoformat()), filename) | Sanitize the user-provided file name, add timestamp for uniqness. |
def get_session(username, password, cookie_path=COOKIE_PATH, cache=True,
cache_expiry=300, cache_path=CACHE_PATH, driver='phantomjs'):
class USPSAuth(AuthBase):
def __init__(self, username, password, cookie_path, driver):
self.username = username
self.password = passw... | Get session, existing or new. |
def _identity(self, *args, **kwargs):
LOCAL = 'local accounts'
EXT = 'external accounts'
data = dict()
data[LOCAL] = self._get_local_users(disabled=kwargs.get('disabled'))
data[EXT] = self._get_external_accounts(data[LOCAL].keys()) or 'N/A'
data['local groups'] = self._ge... | Local users and groups.
accounts
Can be either 'local', 'remote' or 'all' (equal to "local,remote").
Remote accounts cannot be resolved on all systems, but only
those, which supports 'passwd -S -a'.
disabled
True (or False, default) to return only disabl... |
def bulk_create(self, *args, **kwargs):
ret_val = super(ManagerUtilsQuerySet, self).bulk_create(*args, **kwargs)
post_bulk_operation.send(sender=self.model, model=self.model)
return ret_val | Overrides Django's bulk_create function to emit a post_bulk_operation signal when bulk_create
is finished. |
def get_actions(self, request):
actions = super(CertificateMixin, self).get_actions(request)
actions.pop('delete_selected', '')
return actions | Disable the "delete selected" admin action.
Otherwise the action is present even though has_delete_permission is False, it just doesn't
work. |
def square_off(series, time_delta=None, transition_seconds=1):
if time_delta:
if isinstance(time_delta, (int, float)):
time_delta = datetime.timedelta(0, time_delta)
new_times = series.index + time_delta
else:
diff = np.diff(series.index)
time_delta = np.append(diff, ... | Insert samples in regularly sampled data to produce stairsteps from ramps when plotted.
New samples are 1 second (1e9 ns) before each existing samples, to facilitate plotting and sorting
>>> square_off(pd.Series(range(3), index=pd.date_range('2014-01-01', periods=3, freq='15m')),
... time_delta... |
def write(text, delay=0, restore_state_after=True, exact=None):
if exact is None:
exact = _platform.system() == 'Windows'
state = stash_state()
if exact:
for letter in text:
if letter in '\n\b':
send(letter)
else:
_os_keyboard.type_unic... | Sends artificial keyboard events to the OS, simulating the typing of a given
text. Characters not available on the keyboard are typed as explicit unicode
characters using OS-specific functionality, such as alt+codepoint.
To ensure text integrity, all currently pressed keys are released before
the text ... |
def shutdown(opts):
log.debug('dummy proxy shutdown() called...')
DETAILS = _load_state()
if 'filename' in DETAILS:
os.unlink(DETAILS['filename']) | For this proxy shutdown is a no-op |
def _diff_interface_lists(old, new):
diff = _diff_lists(old, new, _nics_equal)
macs = [nic.find('mac').get('address') for nic in diff['unchanged']]
for nic in diff['new']:
mac = nic.find('mac')
if mac.get('address') in macs:
nic.remove(mac)
return diff | Compare network interface definitions to extract the changes
:param old: list of ElementTree nodes representing the old interfaces
:param new: list of ElementTree nodes representing the new interfaces |
def _project_eigenvectors(self):
self._p_eigenvectors = []
for vecs_q in self._eigenvectors:
p_vecs_q = []
for vecs in vecs_q.T:
p_vecs_q.append(np.dot(vecs.reshape(-1, 3),
self._projection_direction))
self._p_eig... | Eigenvectors are projected along Cartesian direction |
def setMAC(self, xEUI):
print '%s call setMAC' % self.port
address64 = ''
try:
if not xEUI:
address64 = self.mac
if not isinstance(xEUI, str):
address64 = self.__convertLongToString(xEUI)
if len(address64) < 16:
... | set the extended addresss of Thread device
Args:
xEUI: extended address in hex format
Returns:
True: successful to set the extended address
False: fail to set the extended address |
def _to_json_type(obj, classkey=None):
if isinstance(obj, dict):
data = {}
for (k, v) in obj.items():
data[k] = _to_json_type(v, classkey)
return data
elif hasattr(obj, "_ast"):
return _to_json_type(obj._ast())
elif hasattr(obj, "__iter__"):
return [_to_js... | Recursively convert the object instance into a valid JSON type. |
def _costfcn(self, x):
f = self._f(x)
df = self._df(x)
d2f = self._d2f(x)
return f, df, d2f | Evaluates the objective function, gradient and Hessian for OPF. |
def label(self, input_grid):
marked = self.find_local_maxima(input_grid)
marked = np.where(marked >= 0, 1, 0)
markers = splabel(marked)[0]
return markers | Labels input grid using enhanced watershed algorithm.
Args:
input_grid (numpy.ndarray): Grid to be labeled.
Returns:
Array of labeled pixels |
def check_input_files(self,
return_found=True,
return_missing=True):
all_input_files = self.files.chain_input_files + self.sub_files.chain_input_files
return check_files(all_input_files, self._file_stage,
return_found, return... | Check if input files exist.
Parameters
----------
return_found : list
A list with the paths of the files that were found.
return_missing : list
A list with the paths of the files that were missing.
Returns
-------
found : list
... |
def _post_activity(self, activity, unserialize=True):
feed_url = "{proto}://{server}/api/user/{username}/feed".format(
proto=self._pump.protocol,
server=self._pump.client.server,
username=self._pump.client.nickname
)
data = self._pump.request(feed_url, method=... | Posts a activity to feed |
def __getAvatar(self, web):
try:
self.avatar = web.find("img", {"class": "avatar"})['src'][:-10]
except IndexError as error:
print("There was an error with the user " + self.name)
print(error)
except AttributeError as error:
print("There was an err... | Scrap the avatar from a GitHub profile.
:param web: parsed web.
:type web: BeautifulSoup node. |
def get_output_margin(self, status=None):
margin = self.get_reserved_space() + self.get_prompt(self.prompt).count('\n') + 1
if special.is_timing_enabled():
margin += 1
if status:
margin += 1 + status.count('\n')
return margin | Get the output margin (number of rows for the prompt, footer and
timing message. |
def _save(self, url, path, data):
worker = self._workers[url]
path = self._paths[url]
if len(data):
try:
with open(path, 'wb') as f:
f.write(data)
except Exception:
logger.error((url, path))
worker.finished = Tru... | Save `data` of downloaded `url` in `path`. |
def etree_to_string(tree):
buff = BytesIO()
tree.write(buff, xml_declaration=True, encoding='UTF-8')
return buff.getvalue() | Creates string from lxml.etree.ElementTree with XML declaration and UTF-8 encoding.
:param tree: the instance of ElementTree
:return: the string of XML. |
def is_newer_file(a, b):
if not (op.exists(a) and op.exists(b)):
return False
am = os.stat(a).st_mtime
bm = os.stat(b).st_mtime
return am > bm | Check if the file a is newer than file b |
def main():
try:
device = AlarmDecoder(SerialDevice(interface=SERIAL_DEVICE))
device.on_zone_fault += handle_zone_fault
device.on_zone_restore += handle_zone_restore
with device.open(baudrate=BAUDRATE):
last_update = time.time()
while True:
if ... | Example application that periodically faults a virtual zone and then
restores it.
This is an advanced feature that allows you to emulate a virtual zone. When
the AlarmDecoder is configured to emulate a zone expander we can fault and
restore those zones programmatically at will. These events can also b... |
def alarm_on_segfault(self, alarm):
self.register_alarm(alarm)
for alarm in listify(alarm):
self._set('alarm-segfault', alarm.alias, multi=True)
return self._section | Raise the specified alarm when the segmentation fault handler is executed.
Sends a backtrace.
:param AlarmType|list[AlarmType] alarm: Alarm. |
def add_host(self, host_id=None, host='localhost', port=6379,
unix_socket_path=None, db=0, password=None,
ssl=False, ssl_options=None):
if host_id is None:
raise RuntimeError('Host ID is required')
elif not isinstance(host_id, (int, long)):
raise... | Adds a new host to the cluster. This is only really useful for
unittests as normally hosts are added through the constructor and
changes after the cluster has been used for the first time are
unlikely to make sense. |
async def tile(tile_number):
try:
tile = get_tile(tile_number)
except TileOutOfBoundsError:
abort(404)
buf = BytesIO(tile.tobytes())
tile.save(buf, 'JPEG')
content = buf.getvalue()
response = await make_response(content)
response.headers['Content-Type'] = 'image/jpg'
resp... | Handles GET requests for a tile number.
:param int tile_number: Number of the tile between 0 and `max_tiles`^2.
:raises HTTPError: 404 if tile exceeds `max_tiles`^2. |
def check_fieldsets(*args, **kwargs):
if hasattr(settings, "CONFIG_FIELDSETS") and settings.CONFIG_FIELDSETS:
inconsistent_fieldnames = get_inconsistent_fieldnames()
if inconsistent_fieldnames:
return [
checks.Warning(
_(
"CONST... | A Django system check to make sure that, if defined, CONFIG_FIELDSETS accounts for
every entry in settings.CONFIG. |
def load_fixture(self, body, attachment_bodies={}):
doc = json.loads(body)
self._documents[doc['_id']] = doc
self._attachments[doc['_id']] = dict()
for name in doc.get('_attachments', list()):
attachment_body = attachment_bodies.get(name, 'stub')
self._attachments... | Loads the document into the database from json string. Fakes the
attachments if necessary. |
def operator_complexity(self):
return sum([level.A.nnz for level in self.levels]) /\
float(self.levels[0].A.nnz) | Operator complexity of this multigrid hierarchy.
Defined as:
Number of nonzeros in the matrix on all levels /
Number of nonzeros in the matrix on the finest level |
def _set_implied_name(self):
"Allow the name of this handler to default to the function name."
if getattr(self, 'name', None) is None:
self.name = self.func.__name__
self.name = self.name.lower() | Allow the name of this handler to default to the function name. |
def validate_number(self, number):
if not isinstance(number, str):
raise ElksException('Recipient phone number may not be empty')
if number[0] == '+' and len(number) > 2 and len(number) < 16:
return True
else:
raise ElksException("Phone number must be of forma... | Checks if a number looks somewhat like a E.164 number. Not an
exhaustive check, as the API takes care of that |
def cmServiceRequest(PriorityLevel_presence=0):
a = TpPd(pd=0x5)
b = MessageType(mesType=0x24)
c = CmServiceTypeAndCiphKeySeqNr()
e = MobileStationClassmark2()
f = MobileId()
packet = a / b / c / e / f
if PriorityLevel_presence is 1:
g = PriorityLevelHdr(ieiPL=0x8, eightBitPL=0x0)
... | CM SERVICE REQUEST Section 9.2.9 |
def secp256k1():
GFp = FiniteField(2 ** 256 - 2 ** 32 - 977)
ec = EllipticCurve(GFp, 0, 7)
return ECDSA(ec, ec.point(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8), 2 ** 256 - 4324203865656596568524208663949... | create the secp256k1 curve |
def StartElement(self, name, attributes):
if name == 'hierarchy':
pass
elif name == 'node':
attributes['uniqueId'] = 'id/no_id/%d' % self.idCount
bounds = re.split('[\][,]', attributes['bounds'])
attributes['bounds'] = ((int(bounds[1]), int(bounds[2])), (i... | Expat start element event handler |
def _transform(xsl_filename, xml, **kwargs):
xslt = _make_xsl(xsl_filename)
xml = xslt(xml, **kwargs)
return xml | Transforms the xml using the specifiec xsl file. |
def diff(candidate, running, *models):
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... |
def normalize_name(self, name):
name = name.lower()
if name in self.aliases:
name = self.aliases[name]
return name | Normalize a field or level name.
:param name: The field or level name (a string).
:returns: The normalized name (a string).
Transforms all strings to lowercase and resolves level name aliases
(refer to :func:`find_level_aliases()`) to their canonical name:
>>> from coloredlogs... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.