code stringlengths 52 7.75k | docs stringlengths 1 5.85k |
|---|---|
def is_img_id_valid(img_id):
t = re.sub(r'[^a-z0-9_:\-\.]', '', img_id, re.IGNORECASE)
t = re.sub(r'\.+', '.', t)
if img_id != t or img_id.count(':') != 1:
return False
profile, base_name = img_id.split(':', 1)
if not profile or not base_name:
return False
try:
get_p... | Checks if img_id is valid. |
def get_variant_label(v_conf):
if v_conf['MAX_SIZE'][0] is None:
return 'h{}'.format(v_conf['MAX_SIZE'][1])
if v_conf['MAX_SIZE'][1] is None:
return 'w{}'.format(v_conf['MAX_SIZE'][0])
return '{}x{}'.format(*v_conf['MAX_SIZE']) | Generates name for variant images based settings (by variants sizes). |
def remove_all_files_of_img_id(img_id):
files = get_files_by_img_id(img_id, check_hash=False)
if files:
os.remove(media_path(files['main']))
for fn in files['variants'].values():
os.remove(media_path(fn)) | Removes all img_id's files. |
def remove_tmp_prefix_from_filename(filename):
if not filename.startswith(dju_settings.DJU_IMG_UPLOAD_TMP_PREFIX):
raise RuntimeError(ERROR_MESSAGES['filename_hasnt_tmp_prefix'] % {'filename': filename})
return filename[len(dju_settings.DJU_IMG_UPLOAD_TMP_PREFIX):] | Remove tmp prefix from filename. |
def remove_tmp_prefix_from_file_path(file_path):
path, filename = os.path.split(file_path)
return os.path.join(path, remove_tmp_prefix_from_filename(filename)).replace('\\', '/') | Remove tmp prefix from file path or url. |
def make_permalink(img_id):
profile, filename = img_id.split(':', 1)
new_img_id = profile + ':' + remove_tmp_prefix_from_filename(filename)
urls = get_files_by_img_id(img_id)
if urls is None:
return urls
move_list = {(urls['main'], remove_tmp_prefix_from_file_path(urls['main']))}
fo... | Removes tmp prefix from filename and rename main and variant files.
Returns img_id without tmp prefix. |
def upload_from_fs(fn, profile=None, label=None):
if not os.path.isfile(fn):
raise ValueError('File is not exists: {}'.format(fn))
if profile is None:
profile = 'default'
conf = get_profile_configs(profile)
with open(fn, 'rb') as f:
if not is_image(f, types=conf['TYPES']):
... | Saves image from fn with TMP prefix and returns img_id. |
def upload_from_fileobject(f, profile=None, label=None):
if profile is None:
profile = 'default'
conf = get_profile_configs(profile)
f.seek(0)
if not is_image(f, types=conf['TYPES']):
msg = (('Format of uploaded file is not allowed. '
'Allowed formats is: %(formats)s... | Saves image from f with TMP prefix and returns img_id. |
def init_app(self, app):
app.extensions['invenio-documents'] = self
app.cli.add_command(cmd) | Flask application initialization. |
def request(self, method, url, **kwargs):
if "data" in kwargs:
kwargs["data"] = json.dumps(kwargs["data"])
kwargs["headers"] = {
'Content-Type': 'application/json',
'Authorization': 'token %s' % self.__token__,
}
req = make_request(
... | Unified method to make request to the Github API
:param method: HTTP Method to use
:param url: URL to reach
:param kwargs: dictionary of arguments (params for URL parameters, data for post/put data)
:return: Response |
def default_branch(self, file):
if isinstance(self.__default_branch__, str):
return self.__default_branch__
elif self.__default_branch__ == GithubProxy.DEFAULT_BRANCH.NO:
return self.master_upstream
else:
return file.sha[:8] | Decide the name of the default branch given the file and the configuration
:param file: File with informations about it
:return: Branch Name |
def init_app(self, app):
self.app = app
self.__blueprint__ = Blueprint(
self.__name__,
self.__name__,
url_prefix=self.__prefix__,
)
for url, name, methods in self.__urls__:
self.blueprint.add_url_rule(
url,
... | Initialize the application and register the blueprint
:param app: Flask Application
:return: Blueprint of the current nemo app
:rtype: flask.Blueprint |
def put(self, file):
input_ = {
"message": file.logs,
"author": file.author.dict(),
"content": file.base64,
"branch": file.branch
}
uri = "{api}/repos/{origin}/contents/{path}".format(
api=self.github_api_url,
origi... | Create a new file on github
:param file: File to create
:return: File or self.ProxyError |
def get(self, file):
uri = "{api}/repos/{origin}/contents/{path}".format(
api=self.github_api_url,
origin=self.origin,
path=file.path
)
params = {
"ref": file.branch
}
data = self.request("GET", uri, params=params)
... | Check on github if a file exists
:param file: File to check status of
:return: File with new information, including blob, or Error
:rtype: File or self.ProxyError |
def update(self, file):
params = {
"message": file.logs,
"author": file.author.dict(),
"content": file.base64,
"sha": file.blob,
"branch": file.branch
}
uri = "{api}/repos/{origin}/contents/{path}".format(
api=self.... | Make an update query on Github API for given file
:param file: File to update, with its content
:return: File with new information, including success (or Error) |
def pull_request(self, file):
uri = "{api}/repos/{upstream}/pulls".format(
api=self.github_api_url,
upstream=self.upstream,
path=file.path
)
params = {
"title": "[Proxy] {message}".format(message=file.logs),
"body": "",
"... | Create a pull request
:param file: File to push through pull request
:return: URL of the PullRequest or Proxy Error |
def get_ref(self, branch, origin=None):
if not origin:
origin = self.origin
uri = "{api}/repos/{origin}/git/refs/heads/{branch}".format(
api=self.github_api_url,
origin=origin,
branch=branch
)
data = self.request("GET", uri)
... | Check if a reference exists
:param branch: The branch to check if it exists
:return: Sha of the branch if it exists, False if it does not exist, self.ProxyError if it went wrong |
def make_ref(self, branch):
master_sha = self.get_ref(self.master_upstream)
if not isinstance(master_sha, str):
return self.ProxyError(
404,
"The default branch from which to checkout is either not available or does not exist",
step="m... | Make a branch on github
:param branch: Name of the branch to create
:return: Sha of the branch or self.ProxyError |
def check_sha(self, sha, content):
rightful_sha = sha256(bytes("{}{}".format(content, self.secret), "utf-8")).hexdigest()
return sha == rightful_sha | Check sent sha against the salted hash of the content
:param sha: SHA sent through fproxy-secure-hash header
:param content: Base 64 encoded Content
:return: Boolean indicating equality |
def patch_ref(self, sha):
uri = "{api}/repos/{origin}/git/refs/heads/{branch}".format(
api=self.github_api_url,
origin=self.origin,
branch=self.master_fork
)
data = {
"sha": sha,
"force": True
}
reply = self.req... | Patch reference on the origin master branch
:param sha: Sha to use for the branch
:return: Status of success
:rtype: str or self.ProxyError |
def r_update(self):
# Getting Master Branch
upstream = self.get_ref(self.master_upstream, origin=self.upstream)
if isinstance(upstream, bool):
return (ProxyError(
404, "Upstream Master branch '{0}' does not exist".format(self.master_upstream),
... | Updates a fork Master
- Check the ref of the origin repository
- Patch reference of fork repository
- Return status to Perseids
:return: JSON Response with status_code 201 if successful. |
def delete_where_user_id(cls, user_id):
result = cls.where_user_id(user_id)
if result is None:
return None
result.delete()
return True | delete by email |
def int_filter(text):
res = list()
for char in text:
if char.isdigit():
res.append(char)
return int("".join(res)) | Extract integer from text.
**中文文档**
摘除文本内的整数。 |
def float_filter(text):
res = list()
for char in text:
if (char.isdigit() or (char == ".")):
res.append(char)
return float("".join(res)) | Extract float from text.
**中文文档**
摘除文本内的小数。 |
def load(self, filename, offset):
try:
self.offset = offset
# self.fd = open(filename, 'rb')
# self.fd.close()
except IOError as e:
print(e) | Will eventually load information for Apple_Boot volume.
Not yet implemented |
def resolve(accessor: hexdi.core.clstype) -> __gentype__.T:
return hexdi.core.get_root_container().resolve(accessor=accessor) | shortcut for resolving from root container
:param accessor: accessor for resolving object
:return: resolved object of requested type |
def bind_type(type_to_bind: hexdi.core.restype, accessor: hexdi.core.clstype, lifetime_manager: hexdi.core.ltype):
hexdi.core.get_root_container().bind_type(type_to_bind, accessor, lifetime_manager) | shortcut for bind_type on root container
:param type_to_bind: type that will be resolved by accessor
:param accessor: accessor for resolving object
:param lifetime_manager: type of lifetime manager for this binding |
def bind_permanent(type_to_bind: hexdi.core.restype, accessor: hexdi.core.clstype):
hexdi.core.get_root_container().bind_type(type_to_bind, accessor, lifetime.PermanentLifeTimeManager) | shortcut for bind_type with PermanentLifeTimeManager on root container
:param type_to_bind: type that will be resolved by accessor
:param accessor: accessor for resolving object |
def bind_transient(type_to_bind: hexdi.core.restype, accessor: hexdi.core.clstype):
hexdi.core.get_root_container().bind_type(type_to_bind, accessor, lifetime.PerResolveLifeTimeManager) | shortcut for bind_type with PerResolveLifeTimeManager on root container
:param type_to_bind: type that will be resolved by accessor
:param accessor: accessor for resolving object |
def get_series(self, series):
if series == "acs1":
return self.census.acs1dp
elif series == "acs5":
return self.census.acs5
elif series == "sf1":
return self.census.sf1
elif series == "sf3":
return self.census.sf3
else:
... | Returns a census series API handler. |
def write_xml(self):
'''
:return: Xml for order by property
:rtype: lxml.etree.Element
'''
order_by_specs = etree.Element('order-by-property')
order_by_specs.attrib['type-id'] = self.typeid
order_by_specs.attrib['property-name'] = self.property_name
... | :return: Xml for order by property
:rtype: lxml.etree.Element |
def set(self, repo):
name = repo.name
if name in self.__repositories \
and self.__repositories[name].is_initialized:
raise ValueError('Can not replace repositories that have been '
'initialized.')
self.__repositories[name] = repo | Sets the given repository (by name). |
def new(self, repo_type, name=None, make_default=False,
repository_class=None, aggregate_class=None,
configuration=None):
if name == REPOSITORY_DOMAINS.ROOT:
# Unless explicitly configured differently, all root repositories
# join the transaction.
... | Creates a new repository of the given type. If the root repository
domain (see :class:`everest.repositories.constants.REPOSITORY_DOMAINS`)
is passed as a repository name, the type string is used as the name;
if no name is passed, a unique name is created automatically. |
def setup_system_repository(self, repository_type, reset_on_start,
repository_class=None):
# Set up the system entity repository (this does not join the
# transaction and is in autocommit mode).
cnf = dict(messaging_enable=True,
messagi... | Sets up the system repository with the given repository type.
:param str repository_type: Repository type to use for the SYSTEM
repository.
:param bool reset_on_start: Flag to indicate whether stored system
resources should be discarded on startup.
:param repository_class: c... |
def initialize_all(self):
for repo in itervalues_(self.__repositories):
if not repo.is_initialized:
repo.initialize() | Convenience method to initialize all repositories that have not been
initialized yet. |
def raw(body, status=200, headers=None,
content_type='application/octet-stream'):
'''
Returns response object without encoding the body.
:param body: Response data.
:param status: Response code.
:param headers: Custom Headers.
:param content_type: the content type (string) of the respons... | Returns response object without encoding the body.
:param body: Response data.
:param status: Response code.
:param headers: Custom Headers.
:param content_type: the content type (string) of the response. |
def html(body, status=200, headers=None):
'''
Returns response object with body in html format.
:param body: Response data to be encoded.
:param status: Response code.
:param headers: Custom Headers.
'''
return HTTPResponse(body, status=status, headers=headers,
conten... | Returns response object with body in html format.
:param body: Response data to be encoded.
:param status: Response code.
:param headers: Custom Headers. |
def redirect(to, headers=None, status=302,
content_type='text/html; charset=utf-8'):
'''Abort execution and cause a 302 redirect (by default).
:param to: path or fully qualified URL to redirect to
:param headers: optional dict of headers to include in the new request
:param status: status ... | Abort execution and cause a 302 redirect (by default).
:param to: path or fully qualified URL to redirect to
:param headers: optional dict of headers to include in the new request
:param status: status code (int) of the new request, defaults to 302
:param content_type: the content type (string) of the ... |
def get_message(self, more_content):
'''
http://channels.readthedocs.io/en/stable/asgi/www.html#response
'''
return {
'status': self.status,
'content': self.body,
'headers': self._parse_headers(),
'more_content': more_content
f get_... | http://channels.readthedocs.io/en/stable/asgi/www.html#response |
def get_bundles():
global _cached_bundles
if not _cached_bundles:
_cached_bundles = BundleManager()
for bundle_conf in bundles_settings.BUNDLES:
_cached_bundles[bundle_conf[0]] = Bundle(bundle_conf)
return _cached_bundles | Used to cache the bundle definitions rather than loading from config every time they're used |
def get_bundle_versions():
global _cached_versions
if not bundles_settings.BUNDLES_VERSION_FILE:
_cached_versions = {}
if _cached_versions is None:
locs = {}
try:
execfile(bundles_settings.BUNDLES_VERSION_FILE, locs)
_cached_versions = locs['BUNDLES_VERSI... | Used to cache the bundle versions rather than loading them from the bundle versions file every time they're used |
def get_url(self, version=None):
if self.fixed_bundle_url:
return self.fixed_bundle_url
return '%s.%s.%s' % (os.path.join(self.bundle_url_root, self.bundle_filename), version or self.get_version(), self.bundle_type) | Return the filename of the bundled bundle |
def get_file_urls(self):
if self.use_bundle:
return [self.get_url()]
return [bundle_file.file_url for bundle_file in self.files] | Return a list of file urls - will return a single item if settings.USE_BUNDLES is True |
def export_batch(self):
batch = self.batch_cls(
model=self.model, history_model=self.history_model, using=self.using
)
if batch.items:
try:
json_file = self.json_file_cls(batch=batch, path=self.path)
json_file.write()
e... | Returns a batch instance after exporting a batch of txs. |
def get(input_dict, environment_dict):
if environment_dict['currentkeyname'] is None or not seash_global_variables.keys[environment_dict['currentkeyname']]['privatekey']:
raise seash_exceptions.UserError("Error, must get as an identity with a private key")
# Activate secure mode if user did not specify the... | <Purpose>
Gets the specified vessels.
<Arguments>
input_dict: The commanddict representing the user's input.
environment_dict: The dictionary representing the current seash
environment.
<Side Effects>
Connects to the Clearinghouse and acquires vessels.
Adds the acquire... |
def release(input_dict, environment_dict):
# Activate secure mode if user did not specify the insecure keyword
allow_ssl_insecure = _get_user_argument(input_dict, 'insecure') is not None
# Get the group name to release
groupname = environment_dict['currenttarget']
nodelist = seash_global_variables.targets... | <Purpose>
Releases the specified vessels.
<Arguments>
input_dict: The commanddict representing the user's input.
environment_dict: The dictionary representing the current seash
environment.
<Side Effects>
Connects to the Clearinghouse and releases vessels.
Removes the ... |
def _update_targets(vesseldicts, environment_dict):
# Compile a list of the nodes that we need to check
nodelist = []
for vesseldict in vesseldicts:
nodeip_port = vesseldict['node_ip']+':'+str(vesseldict['node_port'])
if not nodeip_port in nodelist:
nodelist.append(nodeip_port)
# we'll output ... | <Purpose>
Connects to the nodes in the vesseldicts and adds them to the list
of valid targets.
<Arguments>
vesseldicts:
A list of vesseldicts obtained through
SeattleClearinghouseClient calls.
<Side Effects>
All valid targets that the user can access on the specified nodes
are ... |
def _get_clearinghouse_vessel_handle(vesselhandle):
host, portstring, vesselname = vesselhandle.split(':')
port = int(portstring)
# get information about the node's vessels
try:
nodehandle = nmclient.nmclient_createhandle(host, port,
timeout=seash_global_variables.globalseashtimeout)
except NMC... | <Purpose>
Acquires the unique vessel identifier for a given vesselhandle.
<Arguments>
vesselhandle:
A vessel handle expressed in the form node_ip:node_port:vesselname.
<Side Effects>
Opens a connection to the vessel to retrieve its nodekey.
<Exceptions>
None
<Returns>
A list of Cle... |
def _check_key(self, key):
self.setup_schema()
if key not in self._attrs and key not in self:
raise KeyError(key) | Ensure key is either in schema's attributes or already set on self. |
def similar(self):
if self._similar is None:
self._similar = [
Artist(artist['ArtistID'], artist['Name'], self._connection)
for artist in self._connection.request(
'artistGetSimilarArtists',
{'artistID': self.id},
... | iterator over similar artists as :class:`Artist` objects |
def songs(self):
if self._songs is None:
self._songs = [Song.from_response(song, self._connection) for song in
self._connection.request(
'artistGetArtistSongs',
{'artistID': self.id},
... | iterator over artist's songs as :class:`Song` objects |
def query_params(self, params, key, def_value, short_hand=None):
if key not in params and short_hand:
# value is associated with shorthand, move to key
params[key] = params.get(short_hand, def_value)
del params[short_hand]
elif key not in params and not short_hand:
params[ke... | updates params dict to use
:param params:
:param key:
:param def_value:
:param short_hand:
:return: |
def hirise_edr(self, pid, chunk_size=1024*1024):
productid = "{}*".format(pid)
query = {"target" : "mars",
"query" : "product",
"results" : "f",
"output" : "j",
"pt" : "EDR",
"iid" : "... | Download a HiRISE EDR set of .IMG files to the CWD
You must know the full id to specifiy the filter to use, ie:
PSP_XXXXXX_YYYY will download every EDR IMG file available
PSP_XXXXXX_YYYY_R will download every EDR RED filter IMG file
PSP_XXXXXX_YYYY_BG12_0 will download on... |
def get_meta(self, **kwargs):
query = kwargs
# filters
query = query_params(query, 'productid', None, short_hand='pid')
query = query_params(query, 'query', 'product')
query = query_params(query, 'results', 'm')
query = query_params(query, 'output', 'j')
... | Perform a mostly arbitrary meta_data query and dump to std out
:param kwargs:
:return: |
def suggest_accumulation_rate(chron):
# Follow's Bacon's method @ Bacon.R ln 30 - 44
# Suggested round vals.
sugg = np.tile([1, 2, 5], (4, 1)) * np.reshape(np.repeat([0.1, 1.0, 10, 100], 3), (4, 3))
# Get ballpark accumulation rates, uncalibrated dates.
ballpacc = stats.linregress(x=chron.depth... | From core age-depth data, suggest mean accumulation rate (cm/y) |
def detect(self, filename, offset, standalone=False):
r = RawStruct(
filename=filename,
offset=offset + SIG_OFFSET,
length=SIG_SIZE)
oem_id = r.data
if oem_id == b"NTFS ":
return True
return False | Verifies NTFS filesystem signature.
Returns:
bool: True if filesystem signature at offset 0x03 \
matches 'NTFS ', False otherwise. |
def load(cls, v):
if v is None:
return []
if isinstance(v, list):
return [ Action(s) for s in v ]
elif isinstance(v, str):
return [Action(v)]
else:
raise ParseError("Couldn't parse action: %r" % v) | Load the action from configuration |
def load_stream(cls, st):
y = yaml.load(st)
return [ Automaton(k, v) for k, v in y.iteritems() ] | Load Automatons from a stream |
def make_dot(self, filename_or_stream, auts):
if isinstance(filename_or_stream, str):
stream = file(filename_or_stream, 'w')
else:
stream = filename_or_stream
dot = DotFile(stream)
for aut in auts:
dot.start(aut.name)
... | Create a graphviz .dot representation of the automaton. |
def load(self, config):
self.config = config
if 'start' not in self.config:
raise ParseError('missing start entry')
if 'states' not in self.config:
raise ParseError('missing states entry')
if 'transitions' not in self.config:
raise Pa... | load the configuration |
def _parse_url(url=None):
if url is None:
return ('', '')
scheme, netloc, path, _, _ = parse.urlsplit(url)
if scheme != 's3':
raise InvalidURL(url, "URL scheme must be s3://")
if path and not netloc:
raise InvalidURL(url)
return netloc, path[1:] | Split the path up into useful parts: bucket, obj_key |
def get(self, url=None, delimiter="/"):
params = {'Delimiter': delimiter}
bucket, obj_key = _parse_url(url)
if bucket:
params['Bucket'] = bucket
else:
return self.call("ListBuckets", response_data_key="Buckets")
if obj_key:
params['P... | Path is an s3 url. Ommiting the path or providing "s3://" as the
path will return a list of all buckets. Otherwise, all subdirectories
and their contents will be shown. |
def create(self, url):
bucket, obj_key = _parse_url(url)
if not bucket:
raise InvalidURL(url,
"You must specify a bucket and (optional) path")
if obj_key:
target = "/".join((bucket, obj_key))
else:
target = bucke... | Create a bucket, directory, or empty file. |
def destroy(self, url, recursive=False):
bucket, obj_key = _parse_url(url)
if not bucket:
raise InvalidURL(url,
"You must specify a bucket and (optional) path")
if obj_key:
target = "/".join((bucket, obj_key))
else:
... | Destroy a bucket, directory, or file. Specifying recursive=True
recursively deletes all subdirectories and files. |
def upload(self, local_path, remote_url):
bucket, key = _parse_url(remote_url)
with open(local_path, 'rb') as fp:
return self.call("PutObject", bucket=bucket, key=key, body=fp) | Copy a local file to an S3 location. |
def download(self, remote_url, local_path, buffer_size=8 * 1024):
bucket, key = _parse_url(remote_url)
response_file = self.call("GetObject", bucket=bucket, key=key)['Body']
with open(local_path, 'wb') as fp:
buf = response_file.read(buffer_size)
while buf:
... | Copy S3 data to a local file. |
def copy(self, src_url, dst_url):
src_bucket, src_key = _parse_url(src_url)
dst_bucket, dst_key = _parse_url(dst_url)
if not dst_bucket:
dst_bucket = src_bucket
params = {
'copy_source': '/'.join((src_bucket, src_key)),
'bucket': dst_bucket,
... | Copy an S3 object to another S3 location. |
def move(self, src_url, dst_url):
self.copy(src_url, dst_url)
self.destroy(src_url) | Copy a single S3 object to another S3 location, then delete the
original object. |
def expand_file_names(path, files_root):
# For non-wildcards just return the path. This allows us to detect when
# explicitly listed files are missing.
if not any(wildcard in path for wildcard in '*?['):
return [path]
else:
dir_path, filename = os.path.split(path)
return [os... | Expands paths (e.g. css/*.css in files_root /actual/path/to/css/files/) |
def _init_redis_shards(self):
self._shards = {}
if self._sentinels is not None:
self.init_shards_from_sentinel()
elif self._masters is not None:
self.init_shards_from_masters()
else:
raise Exception("You must either specify sentinels or master... | init_redis_shards is used internally to connect to the Redis sentinels
and populate self.shards with the redis.StrictRedis instances. This
is a convenient method to override / stub out in unit tests. |
def get_shard_names(self):
results = []
for shard_num in range(0, self.num_shards()):
shard_name = self.get_shard_name(shard_num)
results.append(shard_name)
return results | get_shard_names returns an array containing the names of the shards
in the cluster. This is determined with num_shards and
shard_name_format |
def get_key(self, key_type, key_id):
return "{0}:{1}{2}{3}".format(key_type, self._hash_start, key_id,
self._hash_stop) | get_key constructs a key given a key type and a key id.
Keyword arguments:
key_type -- the type of key (e.g.: 'friend_request')
key_id -- the key id (e.g.: '12345')
returns a string representing the key
(e.g.: 'friend_request:{12345}') |
def get_shard_by_key(self, key):
key_id = self._get_key_id_from_key(key)
return self.get_shard_by_key_id(key_id) | get_shard_by_key returns the Redis shard given a key.
Keyword arguments:
key -- the key (e.g. 'friend_request:{12345}')
If the key contains curly braces as in the example, then portion inside
the curly braces will be used as the key id. Otherwise, the entire key
is the key id.
... |
def get_shard_num_by_key(self, key):
key_id = self._get_key_id_from_key(key)
return self.get_shard_num_by_key_id(key_id) | get_shard_num_by_key returns the Redis shard number givne a key.
Keyword arguments:
key -- the key (e.g. 'friend_request:{12345}')
See get_shard_by_key for more details as this method behaves the same
way. |
def get_shard_by_key_id(self, key_id):
shard_num = self.get_shard_num_by_key_id(key_id)
return self.get_shard_by_num(shard_num) | get_shard_by_key_id returns the Redis shard given a key id.
Keyword arguments:
key_id -- the key id (e.g. '12345')
This is similar to get_shard_by_key(key) except that it will not search
for a key id within the curly braces.
returns a redis.StrictRedis connection |
def get_shard_num_by_key_id(self, key_id):
# TODO: support other hash functions?
m = hashlib.md5(str(key_id).encode('ascii')).hexdigest()
# Below is borrowed from
# https://github.com/twitter/twemproxy/blob/master/src/hashkit/nc_md5.c
val = (int(m[0:2], 16) |
... | get_shard_num_by_key_id returns the Redis shard number (zero-indexed)
given a key id.
Keyword arguments:
key_id -- the key id (e.g. '12345' or 'anythingcangohere')
This method is critical in how the Redis cluster sharding works. We
emulate twemproxy's md5 distribution algorithm. |
def get_canonical_key(self, key_type, key_id):
canonical_key_id = self.get_canonical_key_id(key_id)
return self.get_key(key_type, canonical_key_id) | get_canonical_key returns the canonical form of a key given a key id.
For example, '12345' maps to shard 6. The canonical key at index 6
(say '12') is the canonical key id given the key id of '12345'. This
is useful for sets that need to exist on all shards. See
compute_canonical_key_ids... |
def get_canonical_key_id(self, key_id):
shard_num = self.get_shard_num_by_key_id(key_id)
return self._canonical_keys[shard_num] | get_canonical_key_id is used by get_canonical_key, see the comment
for that method for more explanation.
Keyword arguments:
key_id -- the key id (e.g. '12345')
returns the canonical key id (e.g. '12') |
def get_shard_by_num(self, shard_num):
if shard_num < 0 or shard_num >= self.num_shards():
raise ValueError("requested invalid shard# {0}".format(shard_num))
return self._shards[shard_num] | get_shard_by_num returns the shard at index shard_num.
Keyword arguments:
shard_num -- The shard index
Returns a redis.StrictRedis connection or raises a ValueError. |
def _get_key_id_from_key(self, key):
key_id = key
regex = '{0}([^{1}]*){2}'.format(self._hash_start, self._hash_stop,
self._hash_stop)
m = re.search(regex, key)
if m is not None:
# Use what's inside the hash tags as the key i... | _get_key_id_from_key returns the key id from a key, if found. otherwise
it just returns the key to be used as the key id.
Keyword arguments:
key -- The key to derive the ID from. If curly braces are found in the
key, then the contents of the curly braces are used as the
... |
def compute_canonical_key_ids(self, search_amplifier=100):
canonical_keys = {}
num_shards = self.num_shards()
# Guarantees enough to find all keys without running forever
num_iterations = (num_shards**2) * search_amplifier
for key_id in range(1, num_iterations):
... | A canonical key id is the lowest integer key id that maps to
a particular shard. The mapping to canonical key ids depends on the
number of shards.
Returns a dictionary mapping from shard number to canonical key id.
This method will throw an exception if it fails to compute all of
... |
def keys(self, args):
results = {}
# TODO: parallelize
for shard_num in range(0, self.num_shards()):
shard = self.get_shard_by_num(shard_num)
results[shard_num] = shard.keys(args)
return results | keys wrapper that queries every shard. This is an expensive
operation.
This method should be invoked on a TwemRedis instance as if it
were being invoked directly on a StrictRedis instance. |
def mget(self, args):
key_map = collections.defaultdict(list)
results = {}
for key in args:
shard_num = self.get_shard_num_by_key(key)
key_map[shard_num].append(key)
# TODO: parallelize
for shard_num in key_map.keys():
shard = self.ge... | mget wrapper that batches keys per shard and execute as few
mgets as necessary to fetch the keys from all the shards involved.
This method should be invoked on a TwemRedis instance as if it
were being invoked directly on a StrictRedis instance. |
def mset(self, args):
key_map = collections.defaultdict(dict)
result_count = 0
for key in args.keys():
value = args[key]
shard_num = self.get_shard_num_by_key(key)
key_map[shard_num][key] = value
# TODO: parallelize
for shard_num in k... | mset wrapper that batches keys per shard and execute as few
msets as necessary to set the keys in all the shards involved.
This method should be invoked on a TwemRedis instance as if it
were being invoked directly on a StrictRedis instance. |
def get_nested_attribute(obj, attribute):
parent, attr = resolve_nested_attribute(obj, attribute)
if not parent is None:
attr_value = getattr(parent, attr)
else:
attr_value = None
return attr_value | Returns the value of the given (possibly dotted) attribute for the given
object.
If any of the parents on the nested attribute's name path are `None`, the
value of the nested attribute is also assumed as `None`.
:raises AttributeError: If any attribute access along the attribute path
fails with ... |
def set_nested_attribute(obj, attribute, value):
parent, attr = resolve_nested_attribute(obj, attribute)
if parent is None:
raise AttributeError('Can not set attribute "%s" on None value.'
% attr)
setattr(parent, attr, value) | Sets the value of the given (possibly dotted) attribute for the given
object to the given value.
:raises AttributeError: If any of the parents on the nested attribute's
name path are `None`. |
def id_generator(start=0):
count = start
while True:
send_value = (yield count)
if not send_value is None:
if send_value < count:
raise ValueError('Values from ID generator must increase '
'monotonically (current value: %d; value ... | Generator for sequential numeric numbers. |
def get_filter_specification_visitor(name, registry=None):
if registry is None:
registry = get_current_registry()
return registry.getUtility(IFilterSpecificationVisitor, name=name) | Returns a the class registered as the filter specification
visitor utility under the given name (one of the
:const:`everest.querying.base.EXPRESSION_KINDS` constants).
:returns: class implementing
:class:`everest.interfaces.IFilterSpecificationVisitor` |
def get_order_specification_visitor(name, registry=None):
if registry is None:
registry = get_current_registry()
return registry.getUtility(IOrderSpecificationVisitor, name=name) | Returns the class registered as the order specification
visitor utility under the given name (one of the
:const:`everest.querying.base.EXPRESSION_KINDS` constants).
:returns: class implementing
:class:`everest.interfaces.IOrderSpecificationVisitor` |
def get_repository(name=None):
repo_mgr = get_repository_manager()
if name is None:
repo = repo_mgr.get_default()
else:
repo = repo_mgr.get(name)
return repo | Returns the repository with the given name or the default repository if
:param:`name` is `None`. |
def app_name_from_ini_file(ini_file_path):
parser = configparser.SafeConfigParser()
parser.read(ini_file_path)
return app_name_from_ini_parser(parser) | Returns the name of the main application from the given ini file. See
:function:`app_name_from_ini_parser` for details.
:param ini_file_path: Path to the .ini file to parse. |
def app_name_from_ini_parser(ini_parser):
app_names = [sect.split(':')[-1]
for sect in ini_parser.sections()
if sect[:4] == 'app:']
if len(app_names) == 1:
app_name = app_names[0]
else:
pp_sect_name = 'pipeline:main'
if ini_parser.has_section(pp... | Returns the name of the main application from the given ini file parser.
The name is found as follows:
* If the ini file contains only one app:<app name> section,
return this app name;
* Else, if the ini file contains a pipeline:main section, use
the name of the innermost app;
* Else ra... |
def generative(func):
def wrap(inst, *args, **kw):
clone = type(inst).__new__(type(inst))
clone.__dict__ = inst.__dict__.copy()
return func(clone, *args, **kw)
return update_wrapper(wrap, func) | Marks an instance method as generative. |
def truncate(message, limit=500):
if len(message) > limit:
trc_msg = ''.join([message[:limit // 2 - 2],
' .. ',
message[len(message) - limit // 2 + 2:]])
else:
trc_msg = message
return trc_msg | Truncates the message to the given limit length. The beginning and the
end of the message are left untouched. |
def remove_board(board_id):
log.debug('remove %s', board_id)
lines = boards_txt().lines()
lines = filter(lambda x: not x.strip().startswith(board_id + '.'), lines)
boards_txt().write_lines(lines) | remove board.
:param board_id: board id (e.g. 'diecimila')
:rtype: None |
def make_route(self, route) -> dict:
middleware = route['middleware'] if 'middleware' in route else None
# added to ALL requests to support xhr cross-site requests
route['methods'].append('OPTIONS')
return {
'url': route['url'],
'name': route['name... | Construct a route to be parsed into flask App |
def construct_routes(self):
modules = self.evernode_app.get_modules()
for module_name in modules:
with self.app.app_context():
module = importlib.import_module(
'modules.%s.routes' % (module_name))
for route in module.routes... | Gets modules routes.py and converts to module imports |
def diffusion_driver(self):
if self._diffusion_driver is None:
return self,
if isinstance(self._diffusion_driver, list):
return tuple(self._diffusion_driver)
if isinstance(self._diffusion_driver, tuple):
return self._diffusion_driver
return se... | diffusion driver are the underlying `dW` of each process `X` in a SDE like `dX = m dt + s dW`
:return list(StochasticProcess): |
def reset_codenames(self, dry_run=None, clear_existing=None):
self.created_codenames = []
self.updated_names = []
actions = ["add", "change", "delete", "view"]
if django.VERSION >= (2, 1):
actions.append("view")
for app in django_apps.get_app_configs():
... | Ensures all historical model codenames exist in Django's Permission
model. |
def login(self, username, password, disableautosave=True, print_response=True):
if type(username) != str:
return False, "Username must be string"
if type(password) != str:
return False, "Password must be string"
if type(disableautosave) != bool:
ret... | :param username:
:param password:
:param disableautosave: boolean
:param print_response: print log if required
:return: status code, response data |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.