code stringlengths 51 2.38k | docstring stringlengths 4 15.2k |
|---|---|
def _runargs(argstring):
import shlex
parser = cli.make_arg_parser()
args = parser.parse_args(shlex.split(argstring))
run(args) | Entrypoint for debugging |
def get_token(self, url):
parsed_url = urlparse.urlsplit(url)
parsed_url = parsed_url._replace(path='/authorization/api')
self.url = urlparse.urlunsplit(parsed_url)
response = self.request(method='GET', url='/v1/token?url=' + url)
return response.result.text | Retrieves a temporary access token |
def fetch(cls, id, api_key=None, endpoint=None, add_headers=None,
**kwargs):
if endpoint is None:
endpoint = cls.get_endpoint()
inst = cls(api_key=api_key)
parse_key = cls.sanitize_ep(endpoint).split("/")[-1]
endpoint = '/'.join((endpoint, id))
data = cl... | Fetch a single entity from the API endpoint.
Used when you know the exact ID that must be queried. |
def has_stack(self, s):
for t in self.transitions:
if t.lhs[s].position != 0:
return False
if t.rhs[s].position != 0:
return False
return True | Tests whether store `s` is a stack, that is, it never moves from
position 0. |
def housecode_to_index(housecode):
match = re.search(r'^([A-P])(\d{1,2})$', housecode.upper())
if match:
house_index = int(match.group(2))
if 1 <= house_index <= 16:
return (ord(match.group(1)) - ord('A')) * 16 + house_index - 1
raise ValueError("Invalid X10 housecode: %s" % hous... | Convert a X10 housecode to a zero-based index |
def remove(self, w):
self.wpoints.remove(w)
self.last_change = time.time()
self.reindex() | remove a waypoint |
def _validate_options(options, service_name, add_error):
if options is None:
return
if not isdict(options):
add_error('service {} has malformed options'.format(service_name)) | Lazily validate the options, ensuring that they are a dict.
Use the given add_error callable to register validation error. |
def load(self, filename=None):
assert not self.__flag_loaded, "File can be loaded only once"
if filename is None:
filename = self.default_filename
assert filename is not None, \
"{0!s} class has no default filename".format(self.__class__.__name__)
size = os.path.g... | Loads file and registers filename as attribute. |
def _build_tree(self):
if not self.nn_ready:
self.kdtree = scipy.spatial.cKDTree(self.data)
self.nn_ready = True | Build the KDTree for the observed data |
def topoff(cls, amount):
for user in get_user_model().objects.all():
cls.topoff_user(user, amount) | Ensure all users have a minimum number of invites. |
def with_port(self, port):
if port is not None and not isinstance(port, int):
raise TypeError("port should be int or None, got {}".format(type(port)))
if not self.is_absolute():
raise ValueError("port replacement is not allowed " "for relative URLs")
val = self._val
... | Return a new URL with port replaced.
Clear port to default if None is passed. |
def _compile_dimension_size(self, base_index, array,
property, sized_elements):
sort_index = base_index + 2
sized_elements.sort(key=lambda x: x[sort_index])
for element_data in sized_elements:
start, end = element_data[base_index], element_data[sort_in... | Build one set of col widths or row heights. |
def create(cls, parent, child, relation_type, index=None):
try:
with db.session.begin_nested():
obj = cls(parent_id=parent.id,
child_id=child.id,
relation_type=relation_type,
index=index)
db... | Create a PID relation for given parent and child. |
def possible_moves(self, position):
for move in itertools.chain(*[self.add(fn, position) for fn in self.cardinal_directions]):
yield move
for move in self.add_castle(position):
yield move | Generates list of possible moves
:type: position: Board
:rtype: list |
def put_file(self, key, file):
if isinstance(file, str):
return self._put_filename(key, file)
else:
return self._put_file(key, file) | Store into key from file on disk
Stores data from a source into key. *file* can either be a string,
which will be interpretet as a filename, or an object with a *read()*
method.
If the passed object has a *fileno()* method, it may be used to speed
up the operation.
The... |
def _compose_func(func, args_func=lambda req_info: [req_info.index]):
return FuncInfo(func=func, args_func=args_func) | Compose function used to compose arguments to function.
Arguments for the functions are composed from the :class:`.RequestInfo`
object from the ZODB. |
def make_spiral_texture(spirals=6.0, ccw=False, offset=0.0, resolution=1000):
dist = np.sqrt(np.linspace(0., 1., resolution))
if ccw:
direction = 1.
else:
direction = -1.
angle = dist * spirals * np.pi * 2. * direction
spiral_texture = (
(np.cos(angle) * dist / 2.) + 0.5,
... | Makes a texture consisting of a spiral from the origin.
Args:
spirals (float): the number of rotations to make
ccw (bool): make spirals counter-clockwise (default is clockwise)
offset (float): if non-zero, spirals start offset by this amount
resolution (int): number of midpoints alo... |
def localCheckpoint(self, eager=True):
jdf = self._jdf.localCheckpoint(eager)
return DataFrame(jdf, self.sql_ctx) | Returns a locally checkpointed version of this Dataset. Checkpointing can be used to
truncate the logical plan of this DataFrame, which is especially useful in iterative
algorithms where the plan may grow exponentially. Local checkpoints are stored in the
executors using the caching subsystem an... |
def identify_modules(*args, **kwargs):
if len(args) == 1:
path_template = "%(file)s"
error_template = "Module '%(mod)s' not found (%(error)s)"
else:
path_template = "%(mod)s: %(file)s"
error_template = "%(mod)s: not found (%(error)s)"
for modulename in args:
try:
... | Find the disk locations of the given named modules, printing the
discovered paths to stdout and errors discovering paths to stderr.
Any provided keyword arguments are passed to `identify_filepath()`. |
def comments(self, ticket, include_inline_images=False):
return self._query_zendesk(self.endpoint.comments, 'comment', id=ticket, include_inline_images=repr(include_inline_images).lower()) | Retrieve the comments for a ticket.
:param ticket: Ticket object or id
:param include_inline_images: Boolean. If `True`, inline image attachments will be
returned in each comments' `attachments` field alongside non-inline attachments |
def get_status(self):
return {
"host": self.__hostid,
"status": self._service_status_announced,
"statustext": CommonService.human_readable_state.get(
self._service_status_announced
),
"service": self._service_name,
"servicec... | Returns a dictionary containing all relevant status information to be
broadcast across the network. |
def move(self, bearing, distance):
lat = self.pkt['I105']['Lat']['val']
lon = self.pkt['I105']['Lon']['val']
(lat, lon) = mp_util.gps_newpos(lat, lon, bearing, distance)
self.setpos(lat, lon) | move position by bearing and distance |
def parse_group_address(addr):
if addr is None:
raise KNXException("No address given")
res = None
if re.match('[0-9]+$', addr):
res = int(addr)
match = re.match("([0-9]+)/([0-9]+)$", addr)
if match:
main = match.group(1)
sub = match.group(2)
res = int(main) * ... | Parse KNX group addresses and return the address as an integer.
This allows to convert x/x/x and x/x address syntax to a numeric
KNX group address |
def validate(self, csdl, service='facebook'):
return self.request.post('validate', data=dict(csdl=csdl)) | Validate the given CSDL
:param csdl: The CSDL to be validated for analysis
:type csdl: str
:param service: The service for this API call (facebook, etc)
:type service: str
:return: dict of REST API output with headers attached
:rtype: :class:`~dat... |
def epanechnikovKernel(x,ref_x,h=1.0):
u = (x-ref_x)/h
these = np.abs(u) <= 1.0
out = np.zeros_like(x)
out[these] = 0.75*(1.0-u[these]**2.0)
return out | The Epanechnikov kernel.
Parameters
----------
x : np.array
Values at which to evaluate the kernel
x_ref : float
The reference point
h : float
Kernel bandwidth
Returns
-------
out : np.array
Kernel values at each value of x |
def trigger_modified(self, filepath):
mod_time = self._get_modified_time(filepath)
if mod_time > self._watched_files.get(filepath, 0):
self._trigger('modified', filepath)
self._watched_files[filepath] = mod_time | Triggers modified event if the given filepath mod time is newer. |
def background_at_centroid(self):
from scipy.ndimage import map_coordinates
if self._background is not None:
if (self._is_completely_masked or
np.any(~np.isfinite(self.centroid))):
return np.nan * self._background_unit
else:
val... | The value of the ``background`` at the position of the source
centroid.
The background value at fractional position values are
determined using bilinear interpolation. |
def update(self, key_vals=None, overwrite=True):
if not key_vals:
return
write_items = self._update(key_vals, overwrite)
self._root._root_set(self._path, write_items)
self._root._write(commit=True) | Locked keys will be overwritten unless overwrite=False.
Otherwise, written keys will be added to the "locked" list. |
def _locate_settings(settings=''):
"Return the path to the DJANGO_SETTINGS_MODULE"
import imp
import sys
sys.path.append(os.getcwd())
settings = settings or os.getenv('DJANGO_SETTINGS_MODULE')
if settings:
parts = settings.split('.')
f = imp.find_module(parts[0])[1]
args ... | Return the path to the DJANGO_SETTINGS_MODULE |
def notify(
self,
method_name: str,
*args: Any,
trim_log_values: Optional[bool] = None,
validate_against_schema: Optional[bool] = None,
**kwargs: Any
) -> Response:
return self.send(
Notification(method_name, *args, **kwargs),
trim_log_... | Send a JSON-RPC request, without expecting a response.
Args:
method_name: The remote procedure's method name.
args: Positional arguments passed to the remote procedure.
kwargs: Keyword arguments passed to the remote procedure.
trim_log_values: Abbreviate the log ... |
def filter_yn(string, default=None):
if string.startswith(('Y', 'y')):
return True
elif string.startswith(('N', 'n')):
return False
elif not string and default is not None:
return True if default else False
raise InvalidInputError | Return True if yes, False if no, or the default. |
def scan (data, clamconf):
try:
scanner = ClamdScanner(clamconf)
except socket.error:
errmsg = _("Could not connect to ClamAV daemon.")
return ([], [errmsg])
try:
scanner.scan(data)
finally:
scanner.close()
return scanner.infected, scanner.errors | Scan data for viruses.
@return (infection msgs, errors)
@rtype ([], []) |
def start_child_span(operation_name, tracer=None, parent=None, tags=None):
tracer = tracer or opentracing.tracer
return tracer.start_span(
operation_name=operation_name,
child_of=parent.context if parent else None,
tags=tags
) | Start a new span as a child of parent_span. If parent_span is None,
start a new root span.
:param operation_name: operation name
:param tracer: Tracer or None (defaults to opentracing.tracer)
:param parent: parent Span or None
:param tags: optional tags
:return: new span |
async def fire(self, name, payload=None, *,
dc=None, node=None, service=None, tag=None):
params = {
"dc": dc,
"node": extract_pattern(node),
"service": extract_pattern(service),
"tag": extract_pattern(tag)
}
payload = encode_valu... | Fires a new event
Parameters:
name (str): Event name
payload (Payload): Opaque data
node (Filter): Regular expression to filter by node name
service (Filter): Regular expression to filter by service
tag (Filter): Regular expression to filter by servic... |
def __get_cfg_pkgs_rpm(self):
out, err = self._syscall('rpm', None, None, '-qa', '--configfiles',
'--queryformat', '%{name}-%{version}-%{release}\\n')
data = dict()
pkg_name = None
pkg_configs = []
out = salt.utils.stringutils.to_str(out)
... | Get packages with configuration files on RPM systems. |
def clean_before_output(kw_matches):
filtered_kw_matches = {}
for kw_match, info in iteritems(kw_matches):
if not kw_match.nostandalone:
filtered_kw_matches[kw_match] = info
return filtered_kw_matches | Return a clean copy of the keywords data structure.
Stripped off the standalone and other unwanted elements. |
def remove_go(self, target):
with self.lock:
if not self._go:
try:
self.job_queue.remove(target)
except ValueError:
pass | FOR SAVING MEMORY |
def get_evcodes_all(self, inc_set=None, exc_set=None):
codes = self._get_grps_n_codes(inc_set) if inc_set else set(self.code2nt)
if exc_set:
codes.difference_update(self._get_grps_n_codes(exc_set))
return codes | Get set of evidence codes given include set and exclude set |
def __add_watch(self, path, mask, proc_fun, auto_add, exclude_filter):
path = self.__format_path(path)
if auto_add and not mask & IN_CREATE:
mask |= IN_CREATE
wd = self._inotify_wrapper.inotify_add_watch(self._fd, path, mask)
if wd < 0:
return wd
watch = W... | Add a watch on path, build a Watch object and insert it in the
watch manager dictionary. Return the wd value. |
def _trace_filename(self):
dir_stub = ''
if self.output_directory is not None:
dir_stub = self.output_directory
if self.each_time:
filename = '{0}_{1}.json'.format(
self.output_file_name, self.counter)
else:
filename = '{0}.json'.format... | Creates trace filename. |
def get_nodes(code, desired_type, path="__main__", mode="exec", tree=None):
return _GetVisitor(parse(code, path, mode, tree), desired_type).result | Find all nodes of a given type
Arguments:
code -- code text
desired_type -- ast Node or tuple
Keyword Arguments:
path -- code path
mode -- execution mode (exec, eval, single)
tree -- current tree, if it was optimized |
def unsubscribe_url(self):
server_relative = ('%s?s=%s' % (reverse('tidings.unsubscribe',
args=[self.pk]),
self.secret))
return 'https://%s%s' % (Site.objects.get_current().domain,
se... | Return the absolute URL to visit to delete me. |
def redef(obj, key, value, **kwargs):
return Redef(obj, key, value=value, **kwargs) | A static constructor helper function |
def _qr_factor_full(a, dtype=np.float):
n, m = a.shape
packed, pmut, rdiag, acnorm = \
_manual_qr_factor_packed(a, dtype)
r = np.zeros((n, m))
for i in range(n):
r[i,:i] = packed[i,:i]
r[i,i] = rdiag[i]
q = np.eye(m)
v = np.empty(m)
for i in range(n):
v[:] = p... | Compute the QR factorization of a matrix, with pivoting.
Parameters:
a - An n-by-m arraylike, m >= n.
dtype - (optional) The data type to use for computations.
Default is np.float.
Returns:
q - An m-by-m orthogonal matrix (q q^T = ident)
r - An n-by-m upper triangular matrix
pmut - An n-element perm... |
def deep_merge_dict(base, priority):
if not isinstance(base, dict) or not isinstance(priority, dict):
return priority
result = copy.deepcopy(base)
for key in priority.keys():
if key in base:
result[key] = deep_merge_dict(base[key], priority[key])
else:
result[... | Recursively merges the two given dicts into a single dict.
Treating base as the the initial point of the resulting merged dict,
and considering the nested dictionaries as trees, they are merged os:
1. Every path to every leaf in priority would be represented in the result.
2. Subtrees of base are overw... |
def generate_css(self, output_file):
if self.CSS_TEMPLATE_NAME is not None:
template = TEMPLATE_ENV.get_template(self.CSS_TEMPLATE_NAME)
style = template.render(self._context())
if isinstance(style, six.string_types):
style = style.encode('utf-8')
output_file.... | Generate an external style sheet file.
output_file must be a file handler that takes in bytes! |
def get_data_size(self, sport, plan, from_day, from_month, from_year, to_day, to_month, to_year, event_id=None,
event_name=None, market_types_collection=None, countries_collection=None,
file_type_collection=None, session=None):
params = clean_locals(locals())
... | Returns a dictionary of file count and combines size files.
:param sport: sport to filter data for.
:param plan: plan type to filter for, Basic Plan, Advanced Plan or Pro Plan.
:param from_day: day of month to start data from.
:param from_month: month to start data from.
:param ... |
def Get(self):
args = vfs_pb2.ApiGetFileDetailsArgs(
client_id=self.client_id, file_path=self.path)
data = self._context.SendRequest("GetFileDetails", args).file
return File(client_id=self.client_id, data=data, context=self._context) | Fetch file's data and return proper File object. |
def load_builtin_plugins() -> int:
plugin_dir = os.path.join(os.path.dirname(__file__), 'plugins')
return load_plugins(plugin_dir, 'nonebot.plugins') | Load built-in plugins distributed along with "nonebot" package. |
def digest(self):
if self._digest is None:
if self._buf:
self._add_block(self._buf)
self._buf = EMPTY
ctx = self._blake2s(0, 1, True)
for t in self._thread:
ctx.update(t.digest())
self._digest = ctx.digest()
... | Return final digest value. |
def has_logs(self):
found_files = []
if self.logpath is None:
return found_files
if os.path.exists(self.logpath):
for root, _, files in os.walk(os.path.abspath(self.logpath)):
for fil in files:
found_files.append(os.path.join(root, fil)... | Check if log files are available and return file names if they exist.
:return: list |
def prepare_connection():
elasticsearch_host = getattr(settings, 'ELASTICSEARCH_HOST', 'localhost')
elasticsearch_port = getattr(settings, 'ELASTICSEARCH_PORT', 9200)
connections.create_connection(hosts=['{}:{}'.format(elasticsearch_host, elasticsearch_port)]) | Set dafault connection for ElasticSearch.
.. warning::
In case of using multiprocessing/multithreading, connection will
be probably initialized in the main process/thread and the same
connection (socket) will be used in all processes/threads. This
will cause some unexpected timeout... |
def cmdloop(self):
while True:
cmdline = input(self.prompt)
tokens = shlex.split(cmdline)
if not tokens:
if self.last_cmd:
tokens = self.last_cmd
else:
print('No previous command.')
co... | Start CLI REPL. |
def search(self):
logger.debug("Grafana search... %s", cherrypy.request.method)
if cherrypy.request.method == 'OPTIONS':
cherrypy.response.headers['Access-Control-Allow-Methods'] = 'GET,POST,PATCH,PUT,DELETE'
cherrypy.response.headers['Access-Control-Allow-Headers'] = 'Content-Ty... | Request available queries
Posted data: {u'target': u''}
Return the list of available target queries
:return: See upper comment
:rtype: list |
def reset(self):
self._count = 0
self._exception_count = 0
self._stat_start_time = None
self._time_sum = float(0)
self._time_min = float('inf')
self._time_max = float(0)
self._server_time_sum = float(0)
self._server_time_min = float('inf')
self._se... | Reset the statistics data for this object. |
def set_defaults(self, default_values, recursive = False):
result = Parameters()
if recursive:
RecursiveObjectWriter.copy_properties(result, default_values)
RecursiveObjectWriter.copy_properties(result, self)
else:
ObjectWriter.set_properties(result, default_v... | Set default values from specified Parameters and returns a new Parameters object.
:param default_values: Parameters with default parameter values.
:param recursive: (optional) true to perform deep copy, and false for shallow copy. Default: false
:return: a new Parameters object. |
def profile(self):
with self._mutex:
if not self._profile:
profile = self._obj.get_profile()
self._profile = utils.nvlist_to_dict(profile.properties)
return self._profile | The manager's profile. |
def solve(self):
result = Formula(path_actions(a_star_search(
({f: self.cube[f] for f in "LUFDRB"},
self.cube.select_type("edge") & self.cube.has_colour(self.cube["D"].colour)),
self.cross_successors,
self.cross_state_value,
self.cross_goal,
... | Solve the cross. |
def mode(keys, axis=semantics.axis_default, weights=None, return_indices=False):
index = as_index(keys, axis)
if weights is None:
unique, weights = count(index)
else:
unique, weights = group_by(index).sum(weights)
bin = np.argmax(weights)
_mode = unique[bin]
if return_indices:
... | compute the mode, or most frequent occuring key in a set
Parameters
----------
keys : ndarray, [n_keys, ...]
input array. elements of 'keys' can have arbitrary shape or dtype
weights : ndarray, [n_keys], optional
if given, the contribution of each key to the mode is weighted by the give... |
def get_module_names(package_path, pattern="lazy_*.py*"):
package_contents = glob(os.path.join(package_path[0], pattern))
relative_path_names = (os.path.split(name)[1] for name in package_contents)
no_ext_names = (os.path.splitext(name)[0] for name in relative_path_names)
return sorted(set(no_ext_names)) | All names in the package directory that matches the given glob, without
their extension. Repeated names should appear only once. |
def check_extensions(extensions: Set[str], allow_multifile: bool = False):
check_var(extensions, var_types=set, var_name='extensions')
for ext in extensions:
check_extension(ext, allow_multifile=allow_multifile) | Utility method to check that all extensions in the provided set are valid
:param extensions:
:param allow_multifile:
:return: |
def DeleteAttachment(self, attachment_link, options=None):
if options is None:
options = {}
path = base.GetPathFromLink(attachment_link)
attachment_id = base.GetResourceIdOrFullNameFromLink(attachment_link)
return self.DeleteResource(path,
'... | Deletes an attachment.
:param str attachment_link:
The link to the attachment.
:param dict options:
The request options for the request.
:return:
The deleted Attachment.
:rtype:
dict |
def _sample_stratum(self, pmf=None, replace=True):
if pmf is None:
pmf = self.weights_
if not replace:
empty = (self._n_sampled >= self.sizes_)
if np.any(empty):
pmf = copy.copy(pmf)
pmf[empty] = 0
if np.sum(pmf) == 0:
... | Sample a stratum
Parameters
----------
pmf : array-like, shape=(n_strata,), optional, default None
probability distribution to use when sampling from the strata. If
not given, use the stratum weights.
replace : bool, optional, default True
whether to... |
def capture_sale(self, transaction_id, capture_amount, message=None):
request_data = {
"amount": self.base.convert_decimal_to_hundreds(capture_amount),
"currency": self.currency,
"message": message
}
url = "%s%s%s/capture" % (self.api_endpoint, constants.TRANS... | Capture existing preauth.
:param transaction_id:
:param capture_amount:
:param message:
:return: status code |
def _disbatch_runner_async(self, chunk):
pub_data = self.saltclients['runner'](chunk)
raise tornado.gen.Return(pub_data) | Disbatch runner client_async commands |
def write_intro (self):
self.writeln(configuration.AppInfo)
self.writeln(configuration.Freeware)
self.writeln(_("Get the newest version at %(url)s") %
{'url': configuration.Url})
self.writeln(_("Write comments and bugs to %(url)s") %
{'url': conf... | Log introduction text. |
def ellplot (mjr, mnr, pa):
_ellcheck (mjr, mnr, pa)
import omega as om
th = np.linspace (0, 2 * np.pi, 200)
x, y = ellpoint (mjr, mnr, pa, th)
return om.quickXY (x, y, 'mjr=%f mnr=%f pa=%f' %
(mjr, mnr, pa * 180 / np.pi)) | Utility for debugging. |
def create_writer_of_type(type_name):
writers = available_writers()
if type_name not in writers.keys():
raise UnknownWriterException('Unknown writer: %s' % (type_name,))
return writers[type_name]() | Create an instance of the writer with the given name.
Args:
type_name: The name of a writer.
Returns:
An instance of the writer with the given type. |
def parse(self, nodes):
self.last_node_type = self.initial_node_type
for node_number, node in enumerate(nodes):
try:
self.step(node)
except Exception as ex:
raise Exception("An error occurred on node {}".format(node_number)) from ex | Given a stream of node data, try to parse the nodes according to the machine's graph. |
def get_custom_annotations_recursive(data_type):
data_types_seen = set()
def recurse(data_type):
if data_type in data_types_seen:
return
data_types_seen.add(data_type)
dt, _, _ = unwrap(data_type)
if is_struct_type(dt) or is_union_type(dt):
for field in dt... | Given a Stone data type, returns all custom annotations applied to any of
its memebers, as well as submembers, ..., to an arbitrary depth. |
def fit(self, X, y=None):
X = self._check_array(X)
solver_kwargs = self._get_solver_kwargs()
self._coef = algorithms._solvers[self.solver](X, y, **solver_kwargs)
if self.fit_intercept:
self.coef_ = self._coef[:-1]
self.intercept_ = self._coef[-1]
else:
... | Fit the model on the training data
Parameters
----------
X: array-like, shape (n_samples, n_features)
y : array-like, shape (n_samples,)
Returns
-------
self : objectj |
def process_nxml_file(fname, output_fmt='json', outbuf=None, cleanup=True,
**kwargs):
sp = None
out_fname = None
try:
out_fname = run_sparser(fname, output_fmt, outbuf, **kwargs)
sp = process_sparser_output(out_fname, output_fmt)
except Exception as e:
logge... | Return processor with Statements extracted by reading an NXML file.
Parameters
----------
fname : str
The path to the NXML file to be read.
output_fmt: Optional[str]
The output format to obtain from Sparser, with the two options being
'json' and 'xml'. Default: 'json'
outbuf... |
def get_tokens(condition):
try:
ast_tokens = list(ast.walk(ast.parse(condition.strip())))
except SyntaxError as exception:
Logger.get_logger(__name__).error("Syntax error: %s", exception)
ast_tokens = []
return ast_tokens | Get AST tokens for Python condition.
Returns:
list: list of AST tokens |
def checkout_dirs(self):
directories = [os.path.join(self.base_directory, d)
for d in os.listdir(self.base_directory)]
return [d for d in directories if os.path.isdir(d)] | Return directories inside the base directory. |
def drag_and_drop(self, source_selector, destination_selector, **kwargs):
self.info_log(
"Drag and drop: source (%s); destination (%s)" %
(source_selector, destination_selector)
)
use_javascript_dnd = kwargs.get(
"use_javascript_dnd",
"proxy_driver... | Drag and drop
Args:
source_selector: (str)
destination_selector: (str)
Kwargs:
use_javascript_dnd: bool; default:
config proxy_driver:use_javascript_dnd |
def close_filenos(preserve):
maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
if maxfd == resource.RLIM_INFINITY:
maxfd = 4096
for fileno in range(maxfd):
if fileno not in preserve:
try:
os.close(fileno)
except OSError as err:
if n... | Close unprotected file descriptors
Close all open file descriptors that are not in preserve.
If ulimit -nofile is "unlimited", all is defined filenos <= 4096,
else all is <= the output of resource.getrlimit().
:param preserve: set with protected files
:type preserve: set
:return: None |
def _create_field_mapping_action(self):
icon = resources_path('img', 'icons', 'show-mapping-tool.svg')
self.action_field_mapping = QAction(
QIcon(icon),
self.tr('InaSAFE Field Mapping Tool'),
self.iface.mainWindow())
self.action_field_mapping.setStatusTip(self... | Create action for showing field mapping dialog. |
def OnCardRightClick(self, event):
item = event.GetItem()
if item:
itemdata = self.readertreepanel.cardtreectrl.GetItemPyData(item)
if isinstance(itemdata, smartcard.Card.Card):
self.selectedcard = itemdata
if not hasattr(self, "connectID"):
... | Called when user right-clicks a node in the card tree control. |
def remove_column(self, column_name, inplace=False):
if column_name not in self.column_names():
raise KeyError('Cannot find column %s' % column_name)
if inplace:
self.__is_dirty__ = True
try:
with cython_context():
if self._is_verte... | Removes the column with the given name from the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
... |
def _two_qubit_accumulate_into_scratch(args: Dict[str, Any]):
index0, index1 = args['indices']
half_turns = args['half_turns']
scratch = _scratch_shard(args)
projector = _one_projector(args, index0) * _one_projector(args, index1)
scratch += 2 * half_turns * projector | Accumulates two qubit phase gates into the scratch shards. |
def inserir(self, id_brand, name):
model_map = dict()
model_map['name'] = name
model_map['id_brand'] = id_brand
code, xml = self.submit({'model': model_map}, 'POST', 'model/')
return self.response(code, xml) | Inserts a new Model and returns its identifier
:param id_brand: Identifier of the Brand. Integer value and greater than zero.
:param name: Model name. String with a minimum 3 and maximum of 100 characters
:return: Dictionary with the following structure:
::
{'model': {'id... |
def to_array(self, variables):
arr = np.zeros(len(variables), np.int8)
dc = dict(self)
for i, var in enumerate(variables):
arr[i] = dc.get(var, arr[i])
return arr | Converts the clamping to a 1-D array with respect to the given variables
Parameters
----------
variables : list[str]
List of variables names
Returns
-------
`numpy.ndarray`_
1-D array where position `i` correspond to the sign of the clamped vari... |
def compare(left: Union[str, pathlib.Path, _Entity],
right: Union[str, pathlib.Path, _Entity]) -> Comparison:
def normalise(param: Union[str, pathlib.Path, _Entity]) -> _Entity:
if isinstance(param, str):
param = pathlib.Path(param)
if isinstance(param, pathlib.Path):
... | Compare two paths.
:param left: The left side or "before" entity.
:param right: The right side or "after" entity.
:return: A comparison details what has changed from the left side to the
right side. |
def check_move(self, move_type, move_x, move_y):
if move_type not in self.move_types:
raise ValueError("This is not a valid move!")
if move_x < 0 or move_x >= self.board_width:
raise ValueError("This is not a valid X position of the move!")
if move_y < 0 or move_y >= self... | Check if a move is valid.
If the move is not valid, then shut the game.
If the move is valid, then setup a dictionary for the game,
and update move counter.
TODO: maybe instead of shut the game, can end the game or turn it into
a valid move?
Parameters
--------... |
def defaults(cls, *options, **kwargs):
if kwargs and len(kwargs) != 1 and list(kwargs.keys())[0] != 'backend':
raise Exception('opts.defaults only accepts "backend" keyword argument')
cls._linemagic(cls._expand_options(merge_options_to_dict(options)), backend=kwargs.get('backend')) | Set default options for a session.
Set default options for a session. whether in a Python script or
a Jupyter notebook.
Args:
*options: Option objects used to specify the defaults.
backend: The plotting extension the options apply to |
def ancestor_of(self, name, ancestor, visited=None):
if visited is None:
visited = set()
node = self._nodes.get(name)
if node is None or name not in self._nodes:
return False
stack = list(node.parents)
while stack:
current = stack.pop()
... | Check whether a node has another node as an ancestor.
name: The name of the node being checked.
ancestor: The name of the (possible) ancestor node.
visited: (optional, None) If given, a set of nodes that have
already been traversed. NOTE: The set will be updated with
any... |
def has_gap_in_elf_shndx(self):
if not self._ptr:
raise BfdException("BFD not initialized")
return _bfd.get_bfd_attribute(
self._ptr, BfdAttributes.HAS_GAP_IN_ELF_SHNDX) | Return the has gap in elf shndx attribute of the BFD file being
processed. |
def dependencies(project_name):
log = logging.getLogger('ciu')
log.info('Locating dependencies for {}'.format(project_name))
located = distlib.locators.locate(project_name, prereleases=True)
if not located:
log.warning('{0} not found'.format(project_name))
return None
return {packagi... | Get the dependencies for a project. |
def get_auth_url(self, app_id, canvas_url, perms=None, **kwargs):
url = "{0}{1}/{2}".format(
FACEBOOK_WWW_URL, self.version, FACEBOOK_OAUTH_DIALOG_PATH
)
args = {"client_id": app_id, "redirect_uri": canvas_url}
if perms:
args["scope"] = ",".join(perms)
arg... | Build a URL to create an OAuth dialog. |
def format_platforms(cls, platforms):
lines = []
if platforms:
lines.append('This DAP is only supported on the following platforms:')
lines.extend([' * ' + platform for platform in platforms])
return lines | Formats supported platforms in human readable form |
def influx_query_(self, q):
if self.influx_cli is None:
self.err(
self.influx_query_,
"No database connected. Please initialize a connection")
return
try:
return self.influx_cli.query(q)
except Exception as e:
self.e... | Runs an Influx db query |
def register_parser(parser, subparsers_action=None, categories=('other', ), add_help=True):
name = re.sub('^dx ', '', parser.prog)
if subparsers_action is None:
subparsers_action = subparsers
if isinstance(categories, basestring):
categories = (categories, )
parser_map[name] = parser
... | Attaches `parser` to the global ``parser_map``. If `add_help` is truthy,
then adds the helpstring of `parser` into the output of ``dx help...``, for
each category in `categories`.
:param subparsers_action: A special action object that is returned by
``ArgumentParser.add_subparsers(...)``, or None.
... |
def validate_driver_or_none(option, value):
if value is None:
return value
if not isinstance(value, DriverInfo):
raise TypeError("%s must be an instance of DriverInfo" % (option,))
return value | Validate the driver keyword arg. |
def gpg_key(value):
try:
return crypto.get_key(value)
except GPGProblem as e:
raise ValidateError(str(e)) | test if value points to a known gpg key
and return that key as a gpg key object. |
def get_data(n_samples=100):
X, y = make_classification(
n_samples=n_samples,
n_features=N_FEATURES,
n_classes=N_CLASSES,
random_state=0,
)
X = X.astype(np.float32)
return X, y | Get synthetic classification data with n_samples samples. |
def normalize(body_part_tup,):
return '\n\n'.join(
[
'{}\n\n{}'.format(
str(p.headers[b'Content-Disposition'], p.encoding), p.text
)
for p in sorted(
body_part_tup, key=lambda p: p.headers[b'Content-Disposition']
)
]
... | Normalize a tuple of BodyPart objects to a string.
Normalization is done by sorting the body_parts by the Content- Disposition headers,
which is typically on the form, ``form-data; name="name_of_part``. |
def disable_insecure_request_warning():
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning) | Suppress warning about untrusted SSL certificate. |
def capture_url_missing_namespace(self, node):
for arg in node.args:
if not(isinstance(arg, ast.Call) and isinstance(arg.func, ast.Name)):
continue
if arg.func.id != 'include':
continue
for keyword in arg.keywords:
if keyword.ar... | Capture missing namespace in url include. |
def wrap_callback(function):
@wraps(function)
def wrapped(task):
task._callback_result = function(task)
return task._callback_result
return wrapped | Set the callback's result as self._callback_result. |
def add_standard_attention_hparams(hparams):
hparams.add_hparam("num_heads", 8)
hparams.add_hparam("attention_key_channels", 0)
hparams.add_hparam("attention_value_channels", 0)
hparams.add_hparam("attention_dropout", 0.0)
hparams.add_hparam("attention_loc_block_length", 256)
hparams.add_hparam("attention_l... | Adds the hparams used by get_standardized_layers. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.