code
stringlengths
52
7.75k
docs
stringlengths
1
5.85k
def terminal_attribute_iterator(self, mapped_class=None, key=None): for attr in self._attribute_iterator(mapped_class, key): if attr.kind == RESOURCE_ATTRIBUTE_KINDS.TERMINAL: yield attr
Returns an iterator over all terminal mapped attributes for the given mapped class and attribute key. See :method:`get_attribute_map` for details.
def create_data_element(self, mapped_class=None): if not mapped_class is None and mapped_class != self.__mapped_cls: mp = self.__mp_reg.find_or_create_mapping(mapped_class) data_el = mp.create_data_element() else: data_el = self.__de_cls.create() retu...
Returns a new data element for the given mapped class. :returns: object implementing :class:`IResourceDataElement`.
def create_linked_data_element(self, url, kind, id=None, # pylint: disable=W0622 relation=None, title=None): mp = self.__mp_reg.find_or_create_mapping(Link) return mp.data_element_class.create(url, kind, id=id, relat...
Returns a new linked data element for the given url and kind. :param str url: URL to assign to the linked data element. :param str kind: kind of the resource that is linked. One of the constantes defined by :class:`everest.constants.RESOURCE_KINDS`. :returns: object implementing :clas...
def create_data_element_from_resource(self, resource): mp = self.__mp_reg.find_or_create_mapping(type(resource)) return mp.data_element_class.create_from_resource(resource)
Returns a new data element for the given resource object. :returns: object implementing :class:`IResourceDataElement`.
def create_linked_data_element_from_resource(self, resource): mp = self.__mp_reg.find_or_create_mapping(Link) return mp.data_element_class.create_from_resource(resource)
Returns a new linked data element for the given resource object. :returns: object implementing :class:`ILinkedDataElement`.
def map_to_resource(self, data_element, resource=None): if not IDataElement.providedBy(data_element): # pylint:disable=E1101 raise ValueError('Expected data element, got %s.' % data_element) if resource is None: coll = \ create_staging_collection(data_ele...
Maps the given data element to a new resource or updates the given resource. :raises ValueError: If :param:`data_element` does not provide :class:`everest.representers.interfaces.IDataElement`.
def map_to_data_element(self, resource): trv = ResourceTreeTraverser(resource, self.as_pruning()) visitor = DataElementBuilderResourceTreeVisitor(self) trv.run(visitor) return visitor.data_element
Maps the given resource to a data element tree.
def as_pruning(self): return PruningMapping(self.__mp_reg, self.__mapped_cls, self.__de_cls, self.__configurations[-1])
Returns a clone of this mapping with the `is_pruning` flag set to *True*.
def push_configuration(self, configuration): self.__mapped_attr_cache.clear() self.__configurations.append(configuration)
Pushes the given configuration object on the stack of configurations managed by this mapping and makes it the active configuration.
def pop_configuration(self): if len(self.__configurations) == 1: raise IndexError('Can not pop the last configuration from the ' 'stack of configurations.') self.__configurations.pop() self.__mapped_attr_cache.clear()
Pushes the currently active configuration from the stack of configurations managed by this mapping. :raises IndexError: If there is only one configuration in the stack.
def with_updated_configuration(self, options=None, attribute_options=None): new_cfg = self.__configurations[-1].copy() if not options is None: for o_name, o_value in iteritems_(options): new_cfg.set_option(o_name, o_value) i...
Returns a context in which this mapping is updated with the given options and attribute options.
def _attribute_iterator(self, mapped_class, key): for attr in \ itervalues_(self.__get_attribute_map(mapped_class, key, 0)): if self.is_pruning: do_ignore = attr.should_ignore(key) else: do_ignore = False if not do_ignore: ...
Returns an iterator over the attributes in this mapping for the given mapped class and attribute key. If this is a pruning mapping, attributes that are ignored because of a custom configuration or because of the default ignore rules are skipped.
def create_mapping(self, mapped_class, configuration=None): cfg = self.__configuration.copy() if not configuration is None: cfg.update(configuration) provided_ifcs = provided_by(object.__new__(mapped_class)) if IMemberResource in provided_ifcs: base_data_...
Creates a new mapping for the given mapped class and representer configuration. :param configuration: configuration for the new data element class. :type configuration: :class:`RepresenterConfiguration` :returns: newly created instance of :class:`Mapping`
def find_mapping(self, mapped_class): if not self.__is_initialized: self.__is_initialized = True self._initialize() mapping = None for base_cls in mapped_class.__mro__: try: mapping = self.__mappings[base_cls] except KeyErr...
Returns the mapping registered for the given mapped class or any of its base classes. Returns `None` if no mapping can be found. :param mapped_class: mapped type :type mapped_class: type :returns: instance of :class:`Mapping` or `None`
def find_or_create_mapping(self, mapped_class): mapping = self.find_mapping(mapped_class) if mapping is None: mapping = self.create_mapping(mapped_class) self.set_mapping(mapping) return mapping
First calls :meth:`find_mapping` to check if a mapping for the given mapped class or any of its base classes has been created. If not, a new one is created with a default configuration, registered automatically and returned.
def json_serial(obj): if isinstance(obj, LegipyModel): return obj.to_json() elif isinstance(obj, (datetime.date, datetime.datetime)): return obj.isoformat() raise TypeError("Type {0} not serializable".format(repr(type(obj))))
JSON serializer for objects not serializable by default json code
def eachMethod(decorator, methodFilter=lambda fName: True): if isinstance(methodFilter, basestring): # Is it a string? If it is, change it into a function that takes a string. prefix = methodFilter methodFilter = lambda fName: fName.startswith(prefix) ismethod = lambda fn: inspect....
Class decorator that wraps every single method in its own method decorator methodFilter: a function which accepts a function name and should return True if the method is one which we want to decorate, False if we want to leave this method alone. methodFilter can also be simply a string prefix. If it i...
def _sibpath(path, sibling): return os.path.join(os.path.dirname(os.path.abspath(path)), sibling)
Return the path to a sibling of a file in the filesystem. This is useful in conjunction with the special C{__file__} attribute that Python provides for modules, so modules can load associated resource files. (Stolen from twisted.python.util)
def parseDate(dateString, strict=True): if (not strict) and (not dateString): return None if not isinstance(dateString, basestring): raise TypeError('%r is not a string' % dateString) return parser.parse(dateString)
Return a datetime object, by parsing a string date/time With strict=False, dateString may be None or '', otherwise it must be a parseable string
def cache(cls, func): @functools.wraps(func) def func_wrapper(*args, **kwargs): func_key = cls.get_key(func) val_cache = cls.get_cache(func_key) lock = cls.get_cache_lock(func_key) return cls._get_value_from_cache( func, val_cach...
Global cache decorator :param func: the function to be decorated :return: the decorator
def instance_cache(cls, func): @functools.wraps(func) def func_wrapper(*args, **kwargs): if not args: raise ValueError('`self` is not available.') else: the_self = args[0] func_key = cls.get_key(func) val_cache = c...
Save the cache to `self` This decorator take it for granted that the decorated function is a method. The first argument of the function is `self`. :param func: function to decorate :return: the decorator
def clear_instance_cache(cls, func): @functools.wraps(func) def func_wrapper(*args, **kwargs): if not args: raise ValueError('`self` is not available.') else: the_self = args[0] cls.clear_self_cache(the_self) retu...
clear the instance cache Decorate a method of a class, the first parameter is supposed to be `self`. It clear all items cached by the `instance_cache` decorator. :param func: function to decorate
def persisted(cls, seconds=0, minutes=0, hours=0, days=0, weeks=0): days += weeks * 7 hours += days * 24 minutes += hours * 60 seconds += minutes * 60 if seconds == 0: # default to 1 day seconds = 24 * 60 * 60 def get_persisted_file(hash...
Cache the return of the function for given time. Default to 1 day. :param weeks: as name :param seconds: as name :param minutes: as name :param hours: as name :param days: as name :return: return of the function decorated
def getEvents(self, repo_user, repo_name, until_id=None): done = False page = 0 events = [] while not done: new_events = yield self.api.makeRequest( ['repos', repo_user, repo_name, 'events'], page) # terminate if w...
Get all repository events, following paging, until the end or until UNTIL_ID is seen. Returns a Deferred.
def getHook(self, repo_user, repo_name, hook_id): return self.api.makeRequest( ['repos', repo_user, repo_name, 'hooks', str(hook_id)], method='GET', )
GET /repos/:owner/:repo/hooks/:id Returns the Hook.
def editHook(self, repo_user, repo_name, hook_id, name, config, events=None, add_events=None, remove_events=None, active=None): post = dict( name=name, config=config, ) if events is not None: post['events'] = events ...
PATCH /repos/:owner/:repo/hooks/:id :param hook_id: Id of the hook. :param name: The name of the service that is being called. :param config: A Hash containing key/value pairs to provide settings for this hook.
def getStatuses(self, repo_user, repo_name, sha): return self.api.makeRequest( ['repos', repo_user, repo_name, 'statuses', sha], method='GET')
:param sha: Full sha to list the statuses from. :return: A defered with the result from GitHub.
def createStatus(self, repo_user, repo_name, sha, state, target_url=None, description=None, context=None): payload = {'state': state} if description is not None: payload['description'] = description if target_url is not None: payload['ta...
:param sha: Full sha to create the status for. :param state: one of the following 'pending', 'success', 'error' or 'failure'. :param target_url: Target url to associate with this status. :param description: Short description of the status. :return: A defered with th...
def edit(self, repo_user, repo_name, pull_number, title=None, body=None, state=None): if not any((title, body, state)): raise ValueError("must provide at least one of:" " title, body, state") post = {} if title is not None: ...
PATCH /repos/:owner/:repo/pulls/:number :param pull_number: The pull request's number :param title: The new title for the pull request :param body: The new top-level body for the pull request :param state: The new top-level body for the pull request
def create(self, repo_user, repo_name, issue_number, body): return self.api.makeRequest( ['repos', repo_user, repo_name, 'issues', issue_number, 'comments'], method='POST', post=dict(body=body))
PATCH /repos/:owner/:repo/issues/:number/comments :param issue_number: The issue's (or pull request's) number :param body: The body of this comment
def getPullRequestComments(self, repo_user, repo_name, pull_number): return self.api.makeRequestAllPages( ['repos', repo_user, repo_name, 'pulls', str(pull_number), 'comments'])
GET /repos/:owner/:repo/pulls/:number/comments :param pull_number: The pull request's number.
def getComment(self, repo_user, repo_name, comment_id): return self.api.makeRequest( ['repos', repo_user, repo_name, 'pulls', 'comments', str(comment_id)])
GET /repos/:owner/:repo/pull/comments/:number :param comment_id: The review comment's ID.
def createComment(self, repo_user, repo_name, pull_number, body, commit_id, path, position): return self.api.makeRequest( ["repos", repo_user, repo_name, "pulls", str(pull_number), "comments"], method="POST", data=dict(body=body, ...
POST /repos/:owner/:repo/pulls/:number/comments :param pull_number: The pull request's ID. :param body: The text of the comment. :param commit_id: The SHA of the commit to comment on. :param path: The relative path of the file to comment on. :param position: The line index in th...
def replyToComment(self, repo_user, repo_name, pull_number, body, in_reply_to): return self.api.makeRequest( ["repos", repo_user, repo_name, "pulls", str(pull_number), "comments"], method="POST", data=dict(body=body, ...
POST /repos/:owner/:repo/pulls/:number/comments Like create, but reply to an existing comment. :param body: The text of the comment. :param in_reply_to: The comment ID to reply to.
def editComment(self, repo_user, repo_name, comment_id, body): return self.api.makeRequest( ["repos", repo_user, repo_name, "pulls", "comments", str(comment_id)], method="POST", data=dict(body=body))
PATCH /repos/:owner/:repo/pulls/comments/:id :param comment_id: The ID of the comment to edit :param body: The new body of the comment.
def deleteComment(self, repo_user, repo_name, comment_id): return self.api.makeRequest( ["repos", repo_user, repo_name, "pulls", "comments", str(comment_id)], method="DELETE")
DELETE /repos/:owner/:repo/pulls/comments/:id :param comment_id: The ID of the comment to edit :param body: The new body of the comment.
def from_project_path(cls, path): path = vistir.compat.Path(path) if path.name == 'Pipfile': pipfile_path = path path = path.parent else: pipfile_path = path / 'Pipfile' pipfile_location = cls.normalize_path(pipfile_path) venv_path = p...
Utility for finding a virtualenv location based on a project path
def get_sys_path(cls, python_path): command = [python_path, "-c", "import json, sys; print(json.dumps(sys.path))"] c = vistir.misc.run(command, return_object=True, block=True, nospin=True) assert c.returncode == 0, "failed loading virtualenv path" sys_path = json.loads(c.out.st...
Get the :data:`sys.path` data for a given python executable. :param str python_path: Path to a specific python executable. :return: The system path information for that python runtime. :rtype: list
def get_setup_install_args(self, pkgname, setup_py, develop=False): headers = self.base_paths["headers"] headers = headers / "python{0}".format(self.python_version) / pkgname install_arg = "install" if not develop else "develop" return [ self.python, "-u", "-c", SET...
Get setup.py install args for installing the supplied package in the virtualenv :param str pkgname: The name of the package to install :param str setup_py: The path to the setup file of the package :param bool develop: Whether the package is in development mode :return: The installation...
def setuptools_install(self, chdir_to, pkg_name, setup_py_path=None, editable=False): install_options = ["--prefix={0}".format(self.prefix.as_posix()),] with vistir.contextmanagers.cd(chdir_to): c = self.run( self.get_setup_install_args(pkg_name, setup_py_path, deve...
Install an sdist or an editable package into the virtualenv :param str chdir_to: The location to change to :param str setup_py_path: The path to the setup.py, if applicable defaults to None :param bool editable: Whether the package is editable, defaults to False
def install(self, req, editable=False, sources=[]): try: packagebuilder = self.safe_import("packagebuilder") except ImportError: packagebuilder = None with self.activated(include_extras=False): if not packagebuilder: return 2 ...
Install a package into the virtualenv :param req: A requirement to install :type req: :class:`requirementslib.models.requirement.Requirement` :param bool editable: Whether the requirement is editable, defaults to False :param list sources: A list of pip sources to consult, defaults to [...
def run(self, cmd, cwd=os.curdir): c = None with self.activated(): script = vistir.cmdparse.Script.parse(cmd) c = vistir.misc.run(script._parts, return_object=True, nospin=True, cwd=cwd) return c
Run a command with :class:`~subprocess.Popen` in the context of the virtualenv :param cmd: A command to run in the virtual environment :type cmd: str or list :param str cwd: The working directory in which to execute the command, defaults to :data:`os.curdir` :return: A finished command ...
def get_monkeypatched_pathset(self): from pip_shims.shims import InstallRequirement # Determine the path to the uninstall module name based on the install module name uninstall_path = InstallRequirement.__module__.replace( "req_install", "req_uninstall" ) re...
Returns a monkeypatched `UninstallPathset` for using to uninstall packages from the virtualenv :return: A patched `UninstallPathset` which enables uninstallation of venv packages :rtype: :class:`pip._internal.req.req_uninstall.UninstallPathset`
def uninstall(self, pkgname, *args, **kwargs): auto_confirm = kwargs.pop("auto_confirm", True) verbose = kwargs.pop("verbose", False) with self.activated(): pathset_base = self.get_monkeypatched_pathset() dist = next( iter(filter(lambda d: d.proj...
A context manager which allows uninstallation of packages from the virtualenv :param str pkgname: The name of a package to uninstall >>> venv = VirtualEnv("/path/to/venv/root") >>> with venv.uninstall("pytz", auto_confirm=True, verbose=False) as uninstaller: cleaned = uninstall...
def remove_examples_all(): d = examples_all_dir() if d.exists(): log.debug('remove %s', d) d.rmtree() else: log.debug('nothing to remove: %s', d)
remove arduino/examples/all directory. :rtype: None
def getRootNode(nodes): '''Return the node with the most children''' max = 0 root = None for i in nodes: if len(i.children) > max: max = len(i.children) root = i return roof getRootNode(nodes): '''Return the node with the most children''' max = 0 root = No...
Return the node with the most children
def getNextNode(nodes,usednodes,parent): '''Get next node in a breadth-first traversal of nodes that have not been used yet''' for e in edges: if e.source==parent: if e.target in usednodes: x = e.target break elif e.target==parent: if e.sou...
Get next node in a breadth-first traversal of nodes that have not been used yet
def getCircles(rawnodes,rawedges): ''' Example input: rawnodes = [1,2,3,4,5,6] rawedges = [(1,2),(1,3),(1,4),(2,4),(1,5),(5,6)] Returns an array of Circle objects with attribute child arrays populated. ''' circles = [] for x in rawnodes: i = Circle(str(x)) for (p,q) in r...
Example input: rawnodes = [1,2,3,4,5,6] rawedges = [(1,2),(1,3),(1,4),(2,4),(1,5),(5,6)] Returns an array of Circle objects with attribute child arrays populated.
def calcPosition(self,parent_circle): ''' Position the circle tangent to the parent circle with the line connecting the centers of the two circles meeting the x axis at angle theta. ''' if r not in self: raise AttributeError("radius must be calculated before position.") if theta not ...
Position the circle tangent to the parent circle with the line connecting the centers of the two circles meeting the x axis at angle theta.
def upload_progress(request): if 'X-Progress-ID' in request.GET: progress_id = request.GET['X-Progress-ID'] elif 'X-Progress-ID' in request.META: progress_id = request.META['X-Progress-ID'] if progress_id: cache_key = "%s_%s" % (request.META['REMOTE_ADDR'], progress_id) ...
Used by Ajax calls Return the upload progress and total length values
def pre_save(self, model_instance, add): value = super(UserField, self).pre_save(model_instance, add) if not value and not add: # fall back to OS user if not accessing through browser # better than nothing ... value = self.get_os_username() setatt...
Updates username created on ADD only.
def sys_toolbox_dir(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), 'esri', 'toolboxes')
Returns this site-package esri toolbox directory.
def appdata_roaming_dir(): install = arcpy.GetInstallInfo('desktop') app_data = arcpy.GetSystemEnvironment("APPDATA") product_dir = ''.join((install['ProductName'], major_version())) return os.path.join(app_data, 'ESRI', product_dir)
Returns the roaming AppData directory for the installed ArcGIS Desktop.
def route(self, uri, methods=frozenset({'GET'}), host=None, strict_slashes=False, stream=False, websocket=False): '''Decorate a function to be registered as a route :param uri: path of the URL :param methods: list or tuple of methods allowed :param host: :param str...
Decorate a function to be registered as a route :param uri: path of the URL :param methods: list or tuple of methods allowed :param host: :param strict_slashes: :param stream: :return: decorated function
def middleware(self, middleware_or_request): '''Decorate and register middleware to be called before a request. Can either be called as @app.middleware or @app.middleware('request') ''' def register_middleware(middleware, attach_to='request'): if attach_to == 'request': ...
Decorate and register middleware to be called before a request. Can either be called as @app.middleware or @app.middleware('request')
def static(self, uri, file_or_directory, pattern=r'/?.+', use_modified_since=True, use_content_range=False): '''Register a root to serve files from. The input can either be a file or a directory. See ''' static_register(self, uri, file_or_directory, pattern, ...
Register a root to serve files from. The input can either be a file or a directory. See
def blueprint(self, blueprint, **options): '''Register a blueprint on the application. :param blueprint: Blueprint object :param options: option dictionary with blueprint defaults :return: Nothing ''' if blueprint.name in self.blueprints: assert self.blueprin...
Register a blueprint on the application. :param blueprint: Blueprint object :param options: option dictionary with blueprint defaults :return: Nothing
def i2osp(self, long_integer, block_size): 'Convert a long integer into an octet string.' hex_string = '%X' % long_integer if len(hex_string) > 2 * block_size: raise ValueError('integer %i too large to encode in %i octets' % (long_integer, block_size)) return a2b_hex(hex_stri...
Convert a long integer into an octet string.
def export_xlsx(wb, output, fn): wb.close() output.seek(0) response = HttpResponse(output.read(), content_type="application/vnd.ms-excel") cd = codecs.encode('attachment;filename=%s' % fn, 'utf-8') response['Content-Disposition'] = cd return response
export as excel wb: output: fn: file name
def xlsw_write_row(ws, row_idx, row, fmt=None): for col_idx in range(len(row)): ws.write(row_idx, col_idx, row[col_idx], fmt) row_idx += 1 return row_idx
ws: row_idx: row number row: a list, data to write fmt: format for cell
def simple_export2xlsx(filename, titles, qs, func_data): output = BytesIO() wb = xlwt.Workbook(output) ws = wb.add_worksheet(filename) header_fmt = wb.add_format() header_fmt.set_bg_color('#C4D89E') row_idx = 0 row_idx = xlsw_write_row(ws, row_idx, titles, header_fmt) for o in qs: ...
export as excel filename: file name titles: title for this table qs: queryset to export func_data: a function to format object to list. ex: `lambda o: [o.pk, o.name]`
def add(self, iterable): item1 = item2 = MarkovChain.START for item3 in iterable: self[(item1, item2)].add_side(item3) item1 = item2 item2 = item3 self[(item1, item2)].add_side(MarkovChain.END)
Insert an iterable (pattern) item into the markov chain. The order of the pattern will define more of the chain.
def random_output(self, max=100): output = [] item1 = item2 = MarkovChain.START for i in range(max-3): item3 = self[(item1, item2)].roll() if item3 is MarkovChain.END: break output.append(item3) item1 = item2 it...
Generate a list of elements from the markov chain. The `max` value is in place in order to prevent excessive iteration.
def start(self): if self._isRunning: return if self._cease.is_set(): self._cease.clear() # restart class Runner(threading.Thread): @classmethod def run(cls): nextRunAt = cls.setNextRun() while not self._...
Start the periodic runner
def useThis(self, *args, **kwargs): self._callback = functools.partial(self._callback, *args, **kwargs)
Change parameter of the callback function. :param *args, **kwargs: parameter(s) to use when executing the callback function.
def stop(self): self._cease.set() time.sleep(0.1) # let the thread closing correctly. self._isRunning = False
Stop the periodic runner
def force_slash(fn): @wraps(fn) def wrapped(*args, **kwargs): if request.environ['PATH_INFO'].endswith('/'): return fn(*args, **kwargs) else: redirect(request.environ['SCRIPT_NAME'] + request.environ['PATH_INFO'] + '/') return wrapped
Force Slash ----------- Wrap a bottle route with this decorator to force a trailing slash. This is useful for the root of your application or places where TrailingSlash doesn't work
def condition(self) -> bool: jwt = JWT() if jwt.verify_http_auth_token(): if not current_app.config['AUTH']['FAST_SESSIONS']: session = SessionModel.where_session_id( jwt.data['session_id']) if session is None: ...
check JWT, then check session for validity
def render_hidden(name, value): if isinstance(value, list): return MultipleHiddenInput().render(name, value) return HiddenInput().render(name, value)
render as hidden widget
def get_only_selected_choices(self, value): schoices = self.choices selected_choices = set([force_text(v) for v in value if v]) if isinstance(schoices, ModelChoiceIterator): schoices.queryset = schoices.queryset.filter(pk__in=selected_choices) else: schoi...
Return a list of optgroups for this widget.
def next_task(self, item, raise_exceptions=None, **kwargs): filename = os.path.basename(item) batch = self.get_batch(filename) tx_deserializer = self.tx_deserializer_cls( allow_self=self.allow_self, override_role=self.override_role ) try: tx_deser...
Deserializes all transactions for this batch and archives the file.
def get_batch(self, filename=None): try: history = self.history_model.objects.get(filename=filename) except self.history_model.DoesNotExist as e: raise TransactionsFileQueueError( f"Batch history not found for '{filename}'." ) from e i...
Returns a batch instance given the filename.
def get_items(self, html): captcha_patterns = [ "https://www.google.com/recaptcha/api.js", "I'm not a robot"] for captcha_pattern in captcha_patterns: if captcha_pattern in html: raise exc.CaptchaError("Found %r in html!" % captcha_pattern) data ...
Get state, county, zipcode, address code from lists page. Example: target url: http://www.zillow.com/browse/homes/md/ <<<<<<< HEAD data: ``[(href, name), ...]`` ======= data: [(href, name)] >>>>>>> 4507a26c6cc47e0affe1f7000f912e536c45212b
def __copy_tree(src_dir, dest_dir): if not os.path.exists(dest_dir): os.makedirs(dest_dir) shutil.copystat(src_dir, dest_dir) for entry in os.listdir(src_dir): from_path = os.path.join(src_dir, entry) to_path = os.path.join(dest_dir, entry) if os.path.isd...
The shutil.copytree() or distutils.dir_util.copy_tree() will happen to report error list below if we invoke it again and again ( at least in python 2.7.4 ): IOError: [Errno 2] No such file or directory: ... So we have to write our's copy_tree() for that purpose.
def get_time(self, loc4d=None): if loc4d is None: raise ValueError("Location4D object can not be None") if self.pattern == self.PATTERN_CYCLE: c = SunCycles.cycles(loc=loc4d) if self.cycle == self.CYCLE_SUNRISE: r = c[SunCycles.RISING] ...
Based on a Location4D object and this Diel object, calculate the time at which this Diel migration is actually happening
def make_relationship_aggregate(self, relationship): if not self._session.IS_MANAGING_BACKREFERENCES: relationship.direction &= ~RELATIONSHIP_DIRECTIONS.REVERSE return RelationshipAggregate(self, relationship)
Returns a new relationship aggregate for the given relationship. :param relationship: Instance of :class:`everest.entities.relationship.DomainRelationship`.
def get_request_body_chunk(self, content: bytes, closed: bool, more_content: bool) -> Dict[str, Any]: ''' http://channels.readthedocs.io/en/stable/asgi/www.html#request-body-chunk ''' return { 'content': content, 'closed': closed, ...
http://channels.readthedocs.io/en/stable/asgi/www.html#request-body-chunk
def snp_query(G, bim, Isnp): r bim_out = bim[Isnp].reset_index(drop=True) G_out = G[bim_out.i.values] bim_out.i = pd.Series(sp.arange(bim_out.shape[0]), index=bim_out.index) return G_out, bim_out
r""" Parameters ---------- G : (`n_snps`, `n_inds`) array Genetic data bim : pandas.DataFrame Variant annotation Isnp : bool array Variant filter Returns ------- G_out : (`n_snps`, `n_inds`) array filtered genetic data bim_out : datafram...
def is_in(bim, geno_range): r Ichrom = bim.chrom == geno_range[0] Ipos1 = bim.pos >= geno_range[1] Ipos2 = bim.pos < geno_range[2] return Ichrom & Ipos1 & Ipos2
r""" Parameters ---------- bim : pandas.DataFrame Variant annotation geno_range : tuple (chrom, pos_start, pos_end) Returns ------- Isnp : bool array Variant filter
def standardize_snps(G): r mean = G.mean(0) std = G.std(0) return (G - mean) / std
r""" Standardize variantes. Parameters ---------- G : (`n_inds`, `n_snps`) array Genetic data Returns ------- G_out : standardized array
def unique_variants(G): r _s = sp.dot(sp.rand(G.shape[0]), G) v, ix = sp.unique(_s, return_index=True) ix = sp.sort(ix) G_out = G[:, ix] return G_out, ix
r""" Filters out variants with the same genetic profile. Parameters ---------- G : (`n_inds`, `n_snps`) array Genetic data Returns ------- G_out : (`n_inds`, `n_unique_snps`) array filtered genetic data idxs : int array index...
def register_new(self, entity_class, entity): EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.NEW self.__entity_set_map[entity_class].add(entity)
Registers the given entity for the given class as NEW. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work.
def register_clean(self, entity_class, entity): EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.CLEAN self.__entity_set_map[entity_class].add(entity)
Registers the given entity for the given class as CLEAN. :returns: Cloned entity.
def register_deleted(self, entity_class, entity): EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.DELETED self.__entity_set_map[entity_class].add(entity)
Registers the given entity for the given class as DELETED. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work.
def unregister(self, entity_class, entity): EntityState.release(entity, self) self.__entity_set_map[entity_class].remove(entity)
Unregisters the given entity for the given class and discards its state information.
def is_marked_new(self, entity): try: result = EntityState.get_state(entity).status == ENTITY_STATUS.NEW except ValueError: result = False return result
Checks if the given entity is marked with status NEW. Returns `False` if the entity has no state information.
def is_marked_deleted(self, entity): try: result = EntityState.get_state(entity).status \ == ENTITY_STATUS.DELETED except ValueError: result = False return result
Checks if the given entity is marked with status DELETED. Returns `False` if the entity has no state information.
def mark_clean(self, entity): state = EntityState.get_state(entity) state.status = ENTITY_STATUS.CLEAN state.is_persisted = True
Marks the given entity as CLEAN. This is done when an entity is loaded fresh from the repository or after a commit.
def iterator(self): # FIXME: There is no dependency tracking; objects are iterated in # random order. for ent_cls in list(self.__entity_set_map.keys()): for ent in self.__entity_set_map[ent_cls]: yield EntityState.get_state(ent)
Returns an iterator over all entity states held by this Unit Of Work.
def reset(self): for ents in self.__entity_set_map.values(): for ent in ents: EntityState.release(ent, self) self.__entity_set_map.clear()
Releases all entities held by this Unit Of Work (i.e., removes state information from all registered entities and clears the entity map).
def add_field(self, name, default=None, required=False, error=None): if name is None: return self.field_arguments.append(dict( name=name, default=default, required=required, error=error))
Add a text/non-file field to parse for a value in the request
def add_file(self, name, required=False, error=None, extensions=None): if name is None: return self.file_arguments.append(dict( name=name, required=required, error=error, extensions=extensions))
Add a file field to parse on request (uploads)
def file_save(self, name, filename=None, folder="", keep_ext=True) -> bool: if name in self.files: file_object = self.files[name] clean_filename = secure_filename(file_object.filename) if filename is not None and keep_ext: clean_filename = filen...
Easy save of a file
def parse(self, fail_callback=None): # get text fields for field in self.field_arguments: self.values[field['name']] = self.__get_value(field['name']) if self.values[field['name']] is None and field['required']: if fail_callback is not None: ...
Parse text fields and file fields for values and files
def __get_value(self, field_name): value = request.values.get(field_name) if value is None: if self.json_form_data is None: value = None elif field_name in self.json_form_data: value = self.json_form_data[field_name] return...
Get request Json value by field name
def __get_file(self, file): file_object = None if file['name'] in request.files: file_object = request.files[file['name']] clean_filename = secure_filename(file_object.filename) if clean_filename == '': return file_object i...
Get request file and do a security check
def __allowed_extension(self, filename, extensions): allowed_extensions = current_app.config['UPLOADS']['EXTENSIONS'] if extensions is not None: allowed_extensions = extensions return '.' in filename and filename.rsplit('.', 1)[1].lower() in \ allowed_exten...
Check allowed file extensions
def __invalid_request(self, error): # TODO: make this modifiable error = { 'error': { 'message': error } } abort(JsonResponse(status_code=400, data=error))
Error response on failure
def get_field(self, offset, length, format): return struct.unpack(format, self.data[offset:offset + length])[0]
Returns unpacked Python struct array. Args: offset (int): offset to byte array within structure length (int): how many bytes to unpack format (str): Python struct format string for unpacking See Also: https://docs.python.org/2/library/struct.html#format-...
def get_string(self, offset, length): return struct.unpack(str(length) + "s", self.data[ offset:offset + length ])[0]
Returns string (length bytes) Args: offset (int): sring offset in byte array length (int): string length