code
stringlengths
51
2.34k
docstring
stringlengths
11
171
def _cache_translation(translation, timeout=cache.default_timeout): if not appsettings.PARLER_ENABLE_CACHING: return if translation.master_id is None: raise ValueError("Can't cache unsaved translation") fields = translation.get_translated_fields() values = {'id': translation.id} for ...
Store a new translation in the cache.
def __edit_line(line, code, code_obj): try: result = eval(code_obj, globals(), locals()) except TypeError as ex: log.error("failed to execute %s: %s", code, ex) raise if result is None: log.error("cannot process line '%s' with %s", line, code) ...
Edit a line with one code object built in the ctor.
def worklog(accountable): worklog = accountable.issue_worklog() headers = ['author_name', 'comment', 'time_spent'] if worklog: rows = [[v for k, v in sorted(w.items()) if k in headers] for w in worklog] rows.insert(0, headers) print_table(SingleTable(rows)) else: ...
List all worklogs for a given issue key.
def send_username_changed_email(self, user): if not self.user_manager.USER_ENABLE_EMAIL: return if not self.user_manager.USER_SEND_USERNAME_CHANGED_EMAIL: return user_or_user_email_object = self.user_manager.db_manager.get_primary_user_email_object(user) email = user_or_user_email_object...
Send the 'username has changed' notification email.
def _calculate_sv_coverage_cnvkit(data, work_dir): from bcbio.variation import coverage from bcbio.structural import annotate out_target_file = os.path.join(work_dir, "%s-target-coverage.cnn" % dd.get_sample_name(data)) out_anti_file = os.path.join(work_dir, "%s-antitarget-coverage.cnn" % dd.get_sample_...
Calculate coverage in an CNVkit ready format using mosdepth.
def UnregisterMessageHandler(self, timeout=None): if self.handler_thread: self.handler_stop = True self.handler_thread.join(timeout) if self.handler_thread.isAlive(): raise RuntimeError("Message handler thread did not join in time.") self.handler_thread = None
Unregisters any registered message handler.
def socket_closed(self, sock): while True: try: if self._poller: with self._lock: self._poller.register(sock, _EVENT_MASK) try: rd = self._poller.poll(0) finally: ...
Return True if we know socket has been closed, False otherwise.
def log_message(self, kind, alert): if kind == 'ERROR': logging.error(alert) else: logging.warning(alert) return
Parses different event types and passes them to logging
def state_pop(self): super(Composite,self).state_pop() for gen in self.generators: gen.state_pop()
Pop the state of all generators
def flush(self): self.stages = [] self.stagenames = [] if not self.queue: return if self.print_statistics: print("LEN OF QUENE", len(self)) t = time() if self._chain._collect_compiler_artifacts: self._compile(debug=True, stages=self...
Force the queue of Primitives to compile, execute on the Controller, and fulfill promises with the data returned.
def validate(self, model_name, object): if model_name not in self.swagger_dict['definitions']: raise ValidationError("Swagger spec has no definition for model %s" % model_name) model_def = self.swagger_dict['definitions'][model_name] log.debug("Validating %s" % model_name) re...
Validate an object against its swagger model
def get(self, client_method, get_params, is_json=True): url = self._wa.apollo_url + self.CLIENT_BASE + client_method headers = {} response = requests.get(url, headers=headers, verify=self.__verify, params=get_params, **self._request...
Make a GET request
def reset_state(self): self.y = self.y0.copy() self.dy = np.zeros(self.dmps) self.ddy = np.zeros(self.dmps) self.cs.reset_state()
Reset the system state
def _trace_full (frame, event, arg): if event == "line": _trace_line(frame, event, arg) else: _trace(frame, event, arg) return _trace_full
Trace every executed line.
async def get(self, cid, coinid): if settings.SIGNATURE_VERIFICATION: super().verify() if coinid in settings.bridges.keys(): self.account.blockchain.setendpoint(settings.bridges[coinid]) reviews = await self.account.blockchain.getreviews(cid=cid) if isinstance(reviews, dict): if "error" in reviews: ...
Receives all contents reviews
def parse_ok(l1, l3): return { 'annee': l1.text.split(' : ')[1].split()[2], 'siren valide': ''.join( l1.text.split(' : ')[1].split(u'\xab')[0].split()[-4:-1]), 'categorie': ' '.join( l1.text.split(' : ')[1].split(u'\xab')[1].split()[:-1]), 'raison sociale': l3...
parse html when siren is ok
def select_unit(action, action_space, select_unit_act, select_unit_id): del action_space select = action.action_ui.multi_panel select.type = select_unit_act select.unit_index = select_unit_id
Select a specific unit from the multi-unit selection.
def _batchify(self, batch_data, batch_label, start=0): i = start batch_size = self.batch_size try: while i < batch_size: label, s = self.next_sample() data = self.imdecode(s) try: self.check_valid_image(data) ...
Helper function for batchifying data
def add_source(self, tier): tier.add_dest(self) self.sources.append(tier)
Schedule this tier to be called when another tier emits.
def create_device_from_category(self, plm, addr, cat, subcat, product_key=0x00): saved_device = self._saved_devices.get(Address(addr).id, {}) cat = saved_device.get('cat', cat) subcat = saved_device.get('subcat', subcat) product_key = saved_device.get(...
Create a new device from the cat, subcat and product_key data.
def removeGaps(self) : for i in range(1, len(self.children)) : if self.children[i].x1 > self.children[i-1].x2: aux_moveTree(self.children[i-1].x2-self.children[i].x1, self.children[i])
Remove all gaps between regions
def BROKER_URL(self): broker_url = get('BROKER_URL', None) if broker_url: log.info("Using BROKER_URL setting: {}".format(broker_url)) return broker_url redis_available = self._redis_available() broker_type = self.BROKER_TYPE if broker_type == 'redis' and n...
Sets BROKER_URL depending on redis or rabbitmq settings
def _expand_autotag(atag, container): if atag.tag != "auto": return if "names" in atag.attrib: i = -1 for name in re.split("[\s,]+", atag.attrib["names"]): if name[0] == '^': name = name[1::] insert = True i += 1 els...
Expands the contents of the specified auto tag within its parent container.
def interrupt(self): if self._database and self._databaseThreadId: try: self._database.interrupt(self._databaseThreadId) except AttributeError: pass self._database = None self._databaseThreadId = 0
Interrupts the current database from processing.
def _normal_map_callback(self, msg): try: self._cur_normal_map = self._bridge.imgmsg_to_cv2(msg) except: self._cur_normal_map = None
Callback for handling normal maps.
def draw_screen(self, surf): if (self._render_rgb and self._obs.observation.HasField("render_data") and self._obs.observation.render_data.HasField("map")): self.draw_rendered_map(surf) else: self.draw_base_map(surf) self.draw_units(surf) self.draw_selection(surf) self.draw_buil...
Draw the screen area.
def Remove(self, *descriptor_names): new_descriptor_map = self.descriptor_map.copy() for name in descriptor_names: new_descriptor_map.pop(name, None) new_descriptors = [ desc for desc in self.descriptors if desc in itervalues(new_descriptor_map) ] return TypeDescriptorSet(*new_...
Returns a copy of this set without elements with given names.
def publish(self): if self.config.publish: logger.info('Publish') self.execute(self.config.publish)
Publish the current release to PyPI
def ddtodms(self, dd): negative = dd < 0 dd = abs(dd) minutes,seconds = divmod(dd*3600,60) degrees,minutes = divmod(minutes,60) if negative: if degrees > 0: degrees = -degrees elif minutes > 0: minutes = -minutes ...
Take in dd string and convert to dms
def addRemoteCandidate(self, candidate): if not self._connection._remote_candidates_end: if candidate is None: self._connection.add_remote_candidate(None) else: self._connection.add_remote_candidate(candidate_to_aioice(candidate))
Add a remote candidate.
def download(self, sub_url): response = requests.get(sub_url, headers=self.headers).text soup = BS(response, 'lxml') downlink = self.base_url+soup.select('.download a')[0]['href'] data = requests.get(downlink, headers=self.headers) z = zipfile.ZipFile(cStringIO.StringIO(data.cont...
download and unzip subtitle archive to a temp location
def start(self): Global.LOGGER.info("starting the flow manager") self._start_actions() self._start_message_fetcher() Global.LOGGER.debug("flow manager started")
Start all the processes
def _is_match(self, response, answer): if self._only_generic_right_feedback(): return str(answer.genus_type) == str(RIGHT_ANSWER_GENUS) elif self._is_multiple_choice(): return MultiChoiceItemRecord._is_match(self, response, answer) elif self._is_image_sequence() or self._...
For MC, can call through to MultiChoice Item Record?
def SetRaw(self, name, value): if self.writeback is None: logging.warning("Attempting to modify a read only config object.") if name in self.constants: raise ConstModificationError( "Attempting to modify constant value %s" % name) self.writeback_data[name] = value self.FlushCache()
Set the raw string without verification or escaping.
def _altair_line_num_(self, xfield, yfield, opts, style, encode): try: c = self._altair_chart_num_("line", xfield, yfield, opts, style, encode) except Exception as e: self.err(e, "Can not draw a line num chart") return ...
Get a line + text number chart
def _set_choices(self, value): super(LazyChoicesMixin, self)._set_choices(value) self.widget.choices = value
Also update the widget's choices.
def addCircleAnnot(self, rect): CheckParent(self) val = _fitz.Page_addCircleAnnot(self, rect) if not val: return val.thisown = True val.parent = weakref.proxy(self) self._annot_refs[id(val)] = val return val
Add a 'Circle' annotation.
def _contiguous_slices(self): k = j = None for i in self._sorted(): if k is None: k = j = i if i - j > 1: yield slice(k, j + 1, 1) k = i j = i if k is not None: yield slice(k, j + 1, 1)
Internal iterator over contiguous slices in RangeSet.
async def set_playstate(self, state, pos=0): url = '{}/Sessions/{}/Playing/{}'.format( self.server.construct_url(API_URL), self.session_id, state) params = {'api_key': self.server.api_key} if state == 'seek': params['SeekPositionTicks'] = int(pos * 10000000) p...
Send media commands to server.
def insert_source_info(result): comment = result.get("comment") if comment: return source = result.get("source") job_name = result.get("job_name") run = result.get("run") source_list = [source, job_name, run] if not all(source_list): return source_note = "/".join(source_l...
Adds info about source of test result if available.
def save(self, async=False, callback=None, encrypted=True): if self._new_password and encrypted: self.password = Sha1.encrypt(self._new_password) controller = NURESTSession.get_current_session().login_controller controller.password = self._new_password controller.api_key = No...
Updates the user and perform the callback method
def acquisition_function_withGradients(self, x): f_acqu,df_acqu = self._compute_acq_withGradients(x) cost_x, cost_grad_x = self.cost_withGradients(x) f_acq_cost = f_acqu/cost_x df_acq_cost = (df_acqu*cost_x - f_acqu*cost_grad_x)/(cost_x**2) return -f_acq_cost*self.space.indicator...
Takes an acquisition and it gradient and weights it so the domain and cost are taken into account.
def change_access_key(self): method = 'POST' endpoint = '/rest/v1/users/{}/accesskey/change'.format( self.client.sauce_username) return self.client.request(method, endpoint)
Change access key of your account.
def to_str(cls, values, callback=None): if callback and callable(callback): if isinstance(values, dict): return callback(_es.to_str(values)) return [callback(_es.to_str(i)) for i in values] return _es.to_str(values)
Convert many records's values to str
def on_train_begin(self, **kwargs:Any)->None: "Prepare the master model." self.model_params, self.master_params = get_master(self.learn.layer_groups, self.flat_master) new_opt = self.learn.opt.new_with_params(self.master_params) if self.opt is not None: self.opt.lr,self.opt.w...
Prepare the master model.
def should_see_link_text(self, link_text, link_url): elements = ElementSelector( world.browser, str('//a[@href="%s"][./text()="%s"]' % (link_url, link_text)), filter_displayed=True, ) if not elements: raise AssertionError("Expected link not found.")
Assert a link with the provided text points to the provided URL.
def action(context, request, action=None, resource=None, uid=None): if action is None: action = request.get_header("HTTP_X_HTTP_METHOD_OVERRIDE", "CREATE").lower() func_name = "{}_items".format(action) action_func = getattr(api, func_name, None) if action_func is None: api.fail(500, "API...
Various HTTP POST actions
def readOne(stream, validate=False, transform=True, ignoreUnreadable=False, allowQP=False): return next(readComponents(stream, validate, transform, ignoreUnreadable, allowQP))
Return the first component from stream.
def items(self): return [ (c.name, getattr(self, c.name, None)) for c in self.__table__._columns ]
return list of pair of name and value of all declared columns.
def ListDirectory(self, pathspec, depth=0): if depth >= self.request.max_depth: return try: fd = vfs.VFSOpen(pathspec, progress_callback=self.Progress) files = fd.ListFiles(ext_attrs=self.request.collect_ext_attrs) except (IOError, OSError) as e: if depth == 0: self.SetStatus...
A recursive generator of files.
def logger_add (self, loggerclass): self.loggers[loggerclass.LoggerName] = loggerclass self[loggerclass.LoggerName] = {}
Add a new logger type to the known loggers.
def cyclic(self): "Returns True if the options cycle, otherwise False" return any(isinstance(val, Cycle) for val in self.kwargs.values())
Returns True if the options cycle, otherwise False
def update_bounds_boxes(self): if self.s not in list(self.Data.keys()): self.select_specimen(list(self.Data.keys())[0]) self.T_list = self.Data[self.s]['zijdblock_steps'] if self.current_fit: self.tmin_box.SetItems(self.T_list) self.tmax_box.SetItems(self.T_li...
updates bounds boxes with bounds of current specimen and fit
def _handle_fetch_response(self, request, send_time, response): fetch_offsets = {} for topic, partitions in request.topics: for partition_data in partitions: partition, offset = partition_data[:2] fetch_offsets[TopicPartition(topic, partition)] = offset ...
The callback for fetch completion
def gan_loss_from_func(loss_gen, loss_crit, weights_gen:Tuple[float,float]=None): "Define loss functions for a GAN from `loss_gen` and `loss_crit`." def _loss_G(fake_pred, output, target, weights_gen=weights_gen): ones = fake_pred.new_ones(fake_pred.shape[0]) weights_gen = ifnone(weights_gen, (1...
Define loss functions for a GAN from `loss_gen` and `loss_crit`.
def _fetch_access_token(self, url, data): logger.info('Fetching component access token') res = self._http.post( url=url, data=data ) try: res.raise_for_status() except requests.RequestException as reqe: raise WeChatClientException( ...
The real fetch access token
def dump_pickle(name, obj): with open(name,"wb") as f: pickle.dump(obj,f,2); pass;
quick pickle dump similar to np.save
def _configure_port_binding(self, is_provider_vlan, duplicate_type, is_native, switch_ip, vlan_id, intf_type, nexus_port, vni): if duplicate_type == const.DUPLICATE_PORT: return auto_create, auto_...
Conditionally calls vlan and port Nexus drivers.
def arch_size(self): if not self._ptr: raise BfdException("BFD not initialized") try: return _bfd.get_arch_size(self._ptr) except Exception, err: raise BfdException("Unable to determine architeure size.")
Return the architecure size in bits.
def stop_listening(cls, event, func): signal(event).disconnect(func, sender=cls)
Remove a callback for a signal against the class
def cleanup_nodes(doc): for node in doc.documentElement.childNodes: if node.nodeType == Node.TEXT_NODE and node.nodeValue.isspace(): doc.documentElement.removeChild(node) return doc
Remove text nodes containing only whitespace
def save(filename, html): try: out_file = open(filename, "w") out_file.write(html) out_file.close() except IOError as ex: parser.error("Could not write baked HTML to local file {name}. ({ex})".format(name=filename, ex=ex))
Creates a baked HTML file on the local system
def normalize_request(request): if isinstance(request, Request): return request for normalizer in REQUEST_NORMALIZERS: try: return normalizer(request) except TypeError: continue raise ValueError("Unable to normalize the provided request")
Given a request, normalize it to the internal Request class.
def commajoin_as_strings(iterable): return _(u',').join((six.text_type(i) for i in iterable))
Join the given iterable with ','
def associate_hosting_device_with_config_agent( self, client, config_agent_id, body): return client.post((ConfigAgentHandlingHostingDevice.resource_path + CFG_AGENT_HOSTING_DEVICES) % config_agent_id, body=body)
Associates a hosting_device with a config agent.
def _call(self, x, out=None): if out is None: out = self.range.zero() else: out.set_zero() x_arr = x.asarray() out_arr = out.asarray() tmp = np.empty(out.shape, out.dtype, order=out.space.default_order) ndim = self.domain.ndim dx = self.dom...
Calculate the spatial Laplacian of ``x``.
def prettyval(self, val): if len(val) == self.wordsize and val[-1:] in (b'\x00', b'\xff'): return "%x" % struct.unpack("<" + self.fmt, val) if len(val) == self.wordsize and re.search(b'[\x00-\x08\x0b\x0c\x0e-\x1f]', val, re.DOTALL): return "%x" % struct.unpack("<" + self.fmt,...
returns the value in a readable format.
def _scale_x_values_timestamps(self, values, max_width): first_timestamp = float(values[0][0]) last_timestamp = float(values[-1][0]) step_size = (last_timestamp - first_timestamp) / max_width values_by_column = [[] for i in range(max_width)] for timestamp, value in values: ...
Scale X values to new width based on timestamps
def icon(self): if self._icon is None: self._icon = QIcon(self.pm()) return self._icon
Get QIcon from wrapper
def ipython(image): from spython.main import get_client from spython.main.parse import ( DockerRecipe, SingularityRecipe ) client = get_client() client.load(image) client.DockerRecipe = DockerRecipe client.SingularityRecipe = SingularityRecipe from IPython import embed embed()
give the user an ipython shell
def resource_path(opts): resources = _load(opts.resources, opts.output_dir) if opts.resource_name not in resources: sys.stderr.write('Invalid resource name: {}\n'.format(opts.resource_name)) return 1 print(resources[opts.resource_name].destination)
Return the full path to a named resource.
def select_subreddit(self): name = self.get_selected_item()['name'] self.selected_page = self.open_subreddit_page(name)
Store the selected subreddit and return to the subreddit page
def scroll(self, delta_x, delta_y): if delta_x < 0: shift_x = 0 xend = self.width + delta_x dt_x = 1 else: shift_x = self.width - 1 xend = delta_x - 1 dt_x = -1 if delta_y < 0: y = 0 yend = self.heigh...
shifts framebuf in x and y direction
def tickmark(self, x, y, size=10, orientation=90): (x1, y1) = self.p2c([x, y]) x2 = x1 + size * math.cos(math.radians(orientation)) y2 = y1 - size * math.sin(math.radians(orientation)) self.create_line(x1, y1, x2, y2)
Draw a line of size and orientation at x,y
def device_selected(self, index): device = self.devicelist_model.itemFromIndex(index) print(device.device.addr) self.btnConnect.setEnabled(True)
Handler for selecting a device from the list in the UI
def visit_arg(self, node, parent): return self.visit_assignname(node, parent, node.arg)
visit an arg node by returning a fresh AssName instance
def _discretize_check(self, table, att, col): label = "'%s'" % col if table in self.discr_intervals and att in self.discr_intervals[table]: intervals = self.discr_intervals[table][att] n_intervals = len(intervals) prev_value = None for i, value in enumerat...
Replaces the value with an appropriate interval symbol, if available.
def _lint(): project_python_files = [filename for filename in get_project_files() if filename.endswith(b'.py')] retcode = subprocess.call( ['flake8', '--max-complexity=10'] + project_python_files) if retcode == 0: print_success_message('No style errors') retur...
Run lint and return an exit code.
def render_django_response(self, **kwargs): from django.http import HttpResponse return HttpResponse( self.render(**kwargs), content_type='image/svg+xml' )
Render the graph, and return a Django response
def create_checkbox(self, name, margin=10): chk_btn = Gtk.CheckButton(name) chk_btn.set_margin_right(margin) return chk_btn
Function creates a checkbox with his name
def _check_error_response(response, query): if "error" in response: http_error = ["HTTP request timed out.", "Pool queue is full"] geo_error = [ "Page coordinates unknown.", "One of the parameters gscoord, gspage, gsbbox is required", "Inva...
check for default error messages and throw correct exception
def generate(env): static_obj, shared_obj = SCons.Tool.createObjBuilders(env) for suffix in CSuffixes: static_obj.add_action(suffix, SCons.Defaults.CAction) shared_obj.add_action(suffix, SCons.Defaults.ShCAction) static_obj.add_emitter(suffix, SCons.Defaults.StaticObjectEmitter) ...
Add Builders and construction variables for C compilers to an Environment.
def _nodedev_event_lifecycle_cb(conn, dev, event, detail, opaque): _salt_send_event(opaque, conn, { 'nodedev': { 'name': dev.name() }, 'event': _get_libvirt_enum_string('VIR_NODE_DEVICE_EVENT_', event), 'detail': 'unknown' })
Node device lifecycle events handler
def which(x): for p in os.environ.get('PATH').split(os.pathsep): p = os.path.join(p, x) if os.path.exists(p): return os.path.abspath(p) return None
Same as which command on Linux
def save(self, fname: str): with open(fname, "wb") as fp: pickle.dump(self, fp)
Saves this training state to fname.
def update_project(config, task_presenter, results, long_description, tutorial, watch): if watch: res = _update_project_watch(config, task_presenter, results, long_description, tutorial) else: res = _update_project(config, task_presenter, re...
Update project templates and information.
def load(self, key=None): if key is None: key = '_pickle' obj = None if _compat_hooks: save = _compat_hooks[0]() try: self.__n += 1 s = self.__file.Get(key + ';{0:d}'.format(self.__n)) self.__io.setvalue(s.GetName()) ...
Read a pickled object representation from the open file.
def add_query_kwargs(kwargs, visitor, constraints, index): (query_const, filter_const) = constraints.remove_index(index) kwargs["key_condition_expr"] = query_const.build(visitor) if filter_const: kwargs["filter"] = filter_const.build(visitor) if index.name != "TABLE": kwargs["index"] = i...
Construct KeyConditionExpression and FilterExpression
def delete(self, *args): key = self.name_wid.text or self.name_wid.hint_text if not hasattr(self.store, key): return delattr(self.store, key) try: return min(kee for kee in dir(self.store) if kee > key) except ValueError: return '+'
Remove the currently selected item from my store
def check_cache(self, template): if template not in self.cached: self.cache_file(template) self.cached.append(template)
Cache a file only once
def _create_request_record(self, identifier, rtype, name, content, ttl, priority): record = collections.OrderedDict() if identifier is not None: record['id'] = identifier record['type'] = rtype if name is not None: record['name'] = self._relative_name(name) ...
Creates record for Subreg API calls
def GenerateCSRFToken(user_id, time): precondition.AssertType(user_id, Text) precondition.AssertOptionalType(time, int) time = time or rdfvalue.RDFDatetime.Now().AsMicrosecondsSinceEpoch() secret = config.CONFIG.Get("AdminUI.csrf_secret_key", None) if not secret: secret = config.CONFIG["AdminUI.django_sec...
Generates a CSRF token based on a secret key, id and time.
def _on_move(self, event): w = self.winfo_width() x = min(max(event.x, 0), w) self.coords('cursor', x, 0, x, self.winfo_height()) self._variable.set(round2((360. * x) / w))
Make selection cursor follow the cursor.
def set(self, folder: str, subscribed: bool) -> None: if subscribed: self.add(folder) else: self.remove(folder)
Set the subscribed status of a folder.
def by_sql(cls, sql, engine_or_session): ses, auto_close = ensure_session(engine_or_session) result = ses.query(cls).from_statement(sql).all() if auto_close: ses.close() return result
Query with sql statement or texture sql.
def check_status_mapping(self): self.verbose('checking status mapping...') if not self.status_mapping: self.message('no status mapping found') return for old_val, new_val in self.status_mapping.iteritems(): try: if isinstance(new_val, basestrin...
ensure status map does not contain status values which are not present in DB
def reverse(array): l = list(array) l.reverse() return _n.array(l)
returns a reversed numpy array
def partition(self): if self.urltype != 'partition': return None return self._bundle.library.partition(self.url)
For partition urltypes, return the partition specified by the ref
def notify_block_new(self, block): payload = block.SerializeToString() self._notify( "consensus_notifier_notify_block_new", payload, len(payload))
A new block was received and passed initial consensus validation
def options(self, parser, env=os.environ): "Add options to nosetests." parser.add_option("--%s-record" % self.name, action="store", metavar="FILE", dest="record_filename", help="Record actions to this...
Add options to nosetests.