text_prompt stringlengths 157 13.1k | code_prompt stringlengths 7 19.8k ⌀ |
|---|---|
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def delete_host_template(resource_root, name, cluster_name):
""" Delete a host template identified by name in the specified cluster. @param resource_root: The root Resource object. @param name: Host template name. @param cluster_name: Cluster name. @return: The deleted ApiHostTemplate object. @since: API v3 """ |
return call(resource_root.delete,
HOST_TEMPLATE_PATH % (cluster_name, name),
ApiHostTemplate, api_version=3) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def update_host_template(resource_root, name, cluster_name, api_host_template):
""" Update a host template identified by name in the specified cluster. @param resource_root: The root Resource object. @param name: Host template name. @param cluster_name: Cluster name. @param api_host_template: The updated host template. @return: The updated ApiHostTemplate. @since: API v3 """ |
return call(resource_root.put,
HOST_TEMPLATE_PATH % (cluster_name, name),
ApiHostTemplate, data=api_host_template, api_version=3) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def rename(self, new_name):
""" Rename a host template. @param new_name: New host template name. @return: An ApiHostTemplate object. """ |
update = copy.copy(self)
update.name = new_name
return self._do_update(update) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def set_role_config_groups(self, role_config_group_refs):
""" Updates the role config groups in a host template. @param role_config_group_refs: List of role config group refs. @return: An ApiHostTemplate object. """ |
update = copy.copy(self)
update.roleConfigGroupRefs = role_config_group_refs
return self._do_update(update) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def list_supported_categories():
""" Prints a list of supported external account category names. For example, "AWS" is a supported external account category name. """ |
categories = get_supported_categories(api)
category_names = [category.name for category in categories]
print ("Supported account categories by name: {0}".format(
COMMA_WITH_SPACE.join(map(str, category_names)))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def list_supported_types(category_name):
""" Prints a list of supported external account type names for the given category_name. For example, "AWS_ACCESS_KEY_AUTH" is a supported external account type name for external account category "AWS". """ |
types = get_supported_types(api, category_name)
type_names = [type.name for type in types]
print ("Supported account types by name for '{0}': [{1}]".format(
category_name, COMMA_WITH_SPACE.join(map(str, type_names)))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def list_credentials_by_name(type_name):
""" Prints a list of available credential names for the given type_name. """ |
accounts = get_all_external_accounts(api, type_name)
account_names = [account.name for account in accounts]
print ("List of credential names for '{0}': [{1}]".format(
type_name, COMMA_WITH_SPACE.join(map(str, account_names)))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def call_s3guard_prune(credential_name):
""" Runs S3Guard prune command on external account associated with the given credential_name. """ | # Get the AWS credential account associated with the credential
account = get_external_account(api, credential_name)
# Invoke the prune command for the account by its name
cmd = account.external_account_cmd_by_name('S3GuardPrune')
print ("Issued '{0}' command with id '{1}'".format(cmd.name, cmd.id))
print ("Waiting for command {0} to finish...".format(cmd.id))
cmd = cmd.wait()
print ("Command succeeded: {0}".format(cmd.success)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def initialize_api(args):
""" Initializes the global API instance using the given arguments. @param args: arguments provided to the script. """ |
global api
api = ApiResource(server_host=args.hostname, server_port=args.port,
username=args.username, password=args.password,
version=args.api_version, use_tls=args.use_tls) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def validate_api_compatibility(args):
""" Validates the API version. @param args: arguments provided to the script. """ |
if args.api_version and args.api_version < MINIMUM_SUPPORTED_API_VERSION:
print("ERROR: Given API version: {0}. Minimum supported API version: {1}"
.format(args.api_version, MINIMUM_SUPPORTED_API_VERSION)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_login_credentials(args):
""" Gets the login credentials from the user, if not specified while invoking the script. @param args: arguments provided to the script. """ |
if not args.username:
args.username = raw_input("Enter Username: ")
if not args.password:
args.password = getpass.getpass("Enter Password: ") |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def main():
""" The "main" entry that controls the flow of the script based on the provided arguments. """ |
setup_logging(logging.INFO)
# Parse arguments
parser = argparse.ArgumentParser(
description="A utility to interact with AWS using Cloudera Manager.")
parser.add_argument('-H', '--hostname', action='store', dest='hostname',
required=True,
help='The hostname of the Cloudera Manager server.')
parser.add_argument('-p', action='store', dest='port', type=int,
help='The port of the Cloudera Manager server. Defaults '
'to 7180 (http) or 7183 (https).')
parser.add_argument('-u', '--username', action='store', dest='username',
help='Login name.')
parser.add_argument('--password', action='store', dest='password',
help='Login password.')
parser.add_argument('--api-version', action='store', dest='api_version',
type=int,
default=MINIMUM_SUPPORTED_API_VERSION,
help='API version to be used. Defaults to {0}.'.format(
MINIMUM_SUPPORTED_API_VERSION))
parser.add_argument('--tls', action='store_const', dest='use_tls',
const=True, default=False,
help='Whether to use tls (https).')
parser.add_argument('-c', '--show-categories', action='store_true',
default=False, dest='show_categories',
help='Prints a list of supported external account '
'category names. For example, "AWS" is a supported '
'external account category name.')
parser.add_argument('-t', '--show-types', action='store',
dest='category_name',
help='Prints a list of supported external account type '
'names for the given CATEGORY_NAME. For example, '
'"AWS_ACCESS_KEY_AUTH" is a supported external '
'account type name for external account category '
'"AWS".')
parser.add_argument('-n', '--show-credentials', action='store',
dest='type_name',
help='Prints a list of available credential names for '
'the given TYPE_NAME.')
parser.add_argument('--prune', action='store', dest='credential_name',
help='Runs S3Guard prune command on external account '
'associated with the given CREDENTIAL_NAME.')
parser.add_argument('--version', action='version', version='%(prog)s 1.0')
args = parser.parse_args()
# Use the default port if required.
if not args.port:
if args.use_tls:
args.port = DEFAULT_HTTPS_PORT
else:
args.port = DEFAULT_HTTP_PORT
validate_api_compatibility(args)
get_login_credentials(args)
initialize_api(args)
# Perform the AWS operation based on the input arguments.
if args.show_categories:
list_supported_categories()
elif args.category_name:
list_supported_types(args.category_name)
elif args.type_name:
list_credentials_by_name(args.type_name)
elif args.credential_name:
call_s3guard_prune(args.credential_name)
else:
print ("ERROR: No arguments given to perform any AWS operation.")
parser.print_help()
sys.exit(1) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_root_resource(server_host, server_port=None, username="admin", password="admin", use_tls=False, version=API_CURRENT_VERSION):
""" See ApiResource. """ |
return ApiResource(server_host, server_port, username, password, use_tls,
version) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def create_cluster(self, name, version=None, fullVersion=None):
""" Create a new cluster. @param name: Cluster name. @param version: Cluster major CDH version, e.g. 'CDH5'. Ignored if fullVersion is specified. @param fullVersion: Complete CDH version, e.g. '5.1.2'. Overrides major version if both specified. @return: The created cluster. """ |
return clusters.create_cluster(self, name, version, fullVersion) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def create_host(self, host_id, name, ipaddr, rack_id = None):
""" Create a host. @param host_id: The host id. @param name: Host name @param ipaddr: IP address @param rack_id: Rack id. Default None. @return: An ApiHost object """ |
return hosts.create_host(self, host_id, name, ipaddr, rack_id) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_metrics(self, path, from_time, to_time, metrics, view, params=None):
""" Generic function for querying metrics. @param from_time: A datetime; start of the period to query (optional). @param to_time: A datetime; end of the period to query (default = now). @param metrics: List of metrics to query (default = all). @param view: View to materialize ('full' or 'summary') @param params: Other query parameters. @return: List of metrics and their readings. """ |
if not params:
params = { }
if from_time:
params['from'] = from_time.isoformat()
if to_time:
params['to'] = to_time.isoformat()
if metrics:
params['metrics'] = metrics
if view:
params['view'] = view
resp = self.get(path, params=params)
return types.ApiList.from_json_dict(resp, self, types.ApiMetric) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def query_timeseries(self, query, from_time=None, to_time=None, by_post=False):
""" Query time series. @param query: Query string. @param from_time: Start of the period to query (optional). @param to_time: End of the period to query (default = now). @return: A list of ApiTimeSeriesResponse. """ |
return timeseries.query_timeseries(self, query, from_time, to_time, by_post=by_post) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def echo(root_resource, message):
"""Have the server echo our message back.""" |
params = dict(message=message)
return root_resource.get(ECHO_PATH, params) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def echo_error(root_resource, message):
"""Generate an error, but we get to set the error message.""" |
params = dict(message=message)
return root_resource.get(ECHO_ERROR_PATH, params) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def service_action(self, service, action):
"Perform given action on service for the selected cluster"
try:
service = api.get_cluster(self.cluster).get_service(service)
except ApiException:
print("Service not found")
return None
if action == "start":
service.start()
if action == "restart":
service.restart()
if action == "stop":
service.stop()
return True |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def cluster_autocomplete(self, text, line, start_index, end_index):
"autocomplete for the use command, obtain list of clusters first"
if not self.CACHED_CLUSTERS:
clusters = [cluster.name for cluster in api.get_all_clusters()]
self.CACHED_CLUSTERS = clusters
if text:
return [cluster for cluster in self.CACHED_CLUSTERS if cluster.startswith(text)]
else:
return self.CACHED_CLUSTERS |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def roles_autocomplete(self, text, line, start_index, end_index):
"Return full list of roles"
if '-' not in line:
# Append a dash to each service, makes for faster autocompletion of
# roles
return [s + '-' for s in self.services_autocomplete(text, line, start_index, end_index)]
else:
key, role = line.split()[1].split('-', 1)
if key not in self.CACHED_ROLES:
service = api.get_cluster(self.cluster).get_service(key)
roles = []
for t in service.get_role_types():
for r in service.get_roles_by_type(t):
roles.append(r.name)
self.CACHED_ROLES[key] = roles
if not role:
return self.CACHED_ROLES[key]
else:
return [r for r in self.CACHED_ROLES[key] if r.startswith(line.split()[1])] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def query_events(resource_root, query_str=None):
""" Search for events. @param query_str: Query string. @return: A list of ApiEvent. """ |
params = None
if query_str:
params = dict(query=query_str)
return call(resource_root.get, EVENTS_PATH, ApiEventQueryResult,
params=params) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def configure(config=None, bind_in_runtime=True):
"""Create an injector with a callable config or raise an exception when already configured.""" |
global _INJECTOR
with _INJECTOR_LOCK:
if _INJECTOR:
raise InjectorException('Injector is already configured')
_INJECTOR = Injector(config, bind_in_runtime=bind_in_runtime)
logger.debug('Created and configured an injector, config=%s', config)
return _INJECTOR |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def configure_once(config=None, bind_in_runtime=True):
"""Create an injector with a callable config if not present, otherwise, do nothing.""" |
with _INJECTOR_LOCK:
if _INJECTOR:
return _INJECTOR
return configure(config, bind_in_runtime=bind_in_runtime) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def clear_and_configure(config=None, bind_in_runtime=True):
"""Clear an existing injector and create another one with a callable config.""" |
with _INJECTOR_LOCK:
clear()
return configure(config, bind_in_runtime=bind_in_runtime) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def autoparams(*selected_args):
"""Return a decorator that will inject args into a function using type annotations, Python >= 3.5 only. For example:: @inject.autoparams() def refresh_cache(cache: RedisCache, db: DbInterface):
pass There is an option to specify which arguments we want to inject without attempts of injecting everything: For example:: @inject.autoparams('cache', 'db') def sign_up(name, email, cache, db):
pass """ |
def autoparams_decorator(func):
if sys.version_info[:2] < (3, 5):
raise InjectorException('autoparams are supported from Python 3.5 onwards')
full_args_spec = inspect.getfullargspec(func)
annotations_items = full_args_spec.annotations.items()
all_arg_names = frozenset(full_args_spec.args + full_args_spec.kwonlyargs)
args_to_check = frozenset(selected_args) or all_arg_names
args_annotated_types = {
arg_name: annotated_type for arg_name, annotated_type in annotations_items
if arg_name in args_to_check
}
return _ParametersInjection(**args_annotated_types)(func)
return autoparams_decorator |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bind(self, cls, instance):
"""Bind a class to an instance.""" |
self._check_class(cls)
self._bindings[cls] = lambda: instance
logger.debug('Bound %s to an instance %s', cls, instance)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bind_to_constructor(self, cls, constructor):
"""Bind a class to a callable singleton constructor.""" |
self._check_class(cls)
if constructor is None:
raise InjectorException('Constructor cannot be None, key=%s' % cls)
self._bindings[cls] = _ConstructorBinding(constructor)
logger.debug('Bound %s to a constructor %s', cls, constructor)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def bind_to_provider(self, cls, provider):
"""Bind a class to a callable instance provider executed for each injection.""" |
self._check_class(cls)
if provider is None:
raise InjectorException('Provider cannot be None, key=%s' % cls)
self._bindings[cls] = provider
logger.debug('Bound %s to a provider %s', cls, provider)
return self |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_instance(self, cls):
"""Return an instance for a class.""" |
binding = self._bindings.get(cls)
if binding:
return binding()
# Try to create a runtime binding.
with _BINDING_LOCK:
binding = self._bindings.get(cls)
if binding:
return binding()
if not self._bind_in_runtime:
raise InjectorException('No binding was found for key=%s' % cls)
if not callable(cls):
raise InjectorException(
'Cannot create a runtime binding, the key is not callable, key=%s' % cls)
instance = cls()
self._bindings[cls] = lambda: instance
logger.debug('Created a runtime binding for key=%s, instance=%s', cls, instance)
return instance |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_csv( filename: Union[PathLike, Iterator[str]], delimiter: Optional[str]=',', first_column_names: Optional[bool]=None, dtype: str='float32', ) -> AnnData: """Read ``.csv`` file. Same as :func:`~anndata.read_text` but with default delimiter ``','``. Parameters filename Data file. delimiter Delimiter that separates data within text file. If ``None``, will split at arbitrary number of white spaces, which is different from enforcing splitting at single white space ``' '``. first_column_names Assume the first column stores row names. dtype Numpy data type. """ |
return read_text(filename, delimiter, first_column_names, dtype) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_umi_tools(filename: PathLike, dtype: str='float32') -> AnnData: """Read a gzipped condensed count matrix from umi_tools. Parameters filename File name to read from. """ |
# import pandas for conversion of a dict of dicts into a matrix
# import gzip to read a gzipped file :-)
import gzip
from pandas import DataFrame
dod = {} # this will contain basically everything
fh = gzip.open(fspath(filename))
header = fh.readline() # read the first line
for line in fh:
t = line.decode('ascii').split('\t') # gzip read bytes, hence the decoding
try:
dod[t[1]].update({t[0]:int(t[2])})
except KeyError:
dod[t[1]] = {t[0]:int(t[2])}
df = DataFrame.from_dict(dod, orient='index') # build the matrix
df.fillna(value=0., inplace=True) # many NaN, replace with zeros
return AnnData(np.array(df), {'obs_names': df.index}, {'var_names': df.columns}, dtype=dtype) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_loom(filename: PathLike, sparse: bool = True, cleanup: bool = False, X_name: str = 'spliced', obs_names: str = 'CellID', var_names: str = 'Gene', dtype: str='float32', **kwargs) -> AnnData: """Read ``.loom``-formatted hdf5 file. This reads the whole file into memory. Beware that you have to explicitly state when you want to read the file as sparse data. Parameters filename The filename. sparse Whether to read the data matrix as sparse. cleanup: Whether to remove all obs/var keys that do not store more than one unique value. X_name: Loompy key where the data matrix is stored. obs_names: Loompy key where the observation/cell names are stored. var_names: Loompy key where the variable/gene names are stored. **kwargs: Arguments to loompy.connect """ |
filename = fspath(filename) # allow passing pathlib.Path objects
from loompy import connect
with connect(filename, 'r', **kwargs) as lc:
if X_name not in lc.layers.keys(): X_name = ''
X = lc.layers[X_name].sparse().T.tocsr() if sparse else lc.layers[X_name][()].T
layers = OrderedDict()
if X_name != '': layers['matrix'] = lc.layers[''].sparse().T.tocsr() if sparse else lc.layers[''][()].T
for key in lc.layers.keys():
if key != '': layers[key] = lc.layers[key].sparse().T.tocsr() if sparse else lc.layers[key][()].T
obs = dict(lc.col_attrs)
if obs_names in obs.keys(): obs['obs_names'] = obs.pop(obs_names)
obsm_attrs = [k for k, v in obs.items() if v.ndim > 1 and v.shape[1] > 1]
obsm = {}
for key in obsm_attrs:
obsm[key] = obs.pop(key)
var = dict(lc.row_attrs)
if var_names in var.keys(): var['var_names'] = var.pop(var_names)
varm_attrs = [k for k, v in var.items() if v.ndim > 1 and v.shape[1] > 1]
varm = {}
for key in varm_attrs:
varm[key] = var.pop(key)
if cleanup:
for key in list(obs.keys()):
if len(set(obs[key])) == 1:
del obs[key]
for key in list(var.keys()):
if len(set(var[key])) == 1:
del var[key]
adata = AnnData(
X,
obs=obs, # not ideal: make the generator a dict...
var=var,
layers=layers,
obsm=obsm if obsm else None,
varm=varm if varm else None,
dtype=dtype)
return adata |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_mtx(filename: PathLike, dtype: str='float32') -> AnnData: """Read ``.mtx`` file. Parameters filename The filename. dtype Numpy data type. """ |
from scipy.io import mmread
# could be rewritten accounting for dtype to be more performant
X = mmread(fspath(filename)).astype(dtype)
from scipy.sparse import csr_matrix
X = csr_matrix(X)
return AnnData(X, dtype=dtype) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def iter_lines(file_like: Iterable[str]) -> Generator[str, None, None]: """ Helper for iterating only nonempty lines without line breaks""" |
for line in file_like:
line = line.rstrip('\r\n')
if line:
yield line |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_zarr(store):
"""Read from a hierarchical Zarr array store. Parameters store The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class. """ |
if isinstance(store, Path):
store = str(store)
import zarr
f = zarr.open(store, mode='r')
d = {}
for key in f.keys():
_read_key_value_from_zarr(f, d, key)
return AnnData(*AnnData._args_from_dict(d)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def read_h5ad(filename, backed: Optional[str] = None, chunk_size: int = 6000):
"""Read ``.h5ad``-formatted hdf5 file. Parameters filename File name of data file. backed : {``None``, ``'r'``, ``'r+'``} If ``'r'``, load :class:`~anndata.AnnData` in ``backed`` mode instead of fully loading it into memory (`memory` mode). If you want to modify backed attributes of the AnnData object, you need to choose ``'r+'``. chunk_size Used only when loading sparse dataset that is stored as dense. Loading iterates through chunks of the dataset of this row size until it reads the whole dataset. Higher size means higher memory consumption and higher loading speed. """ |
if isinstance(backed, bool):
# We pass `None`s through to h5py.File, and its default is “a”
# (=“r+”, but create the file if it doesn’t exist)
backed = 'r+' if backed else None
warnings.warn(
"In a future version, read_h5ad will no longer explicitly support "
"boolean arguments. Specify the read mode, or leave `backed=None`.",
DeprecationWarning,
)
if backed:
# open in backed-mode
return AnnData(filename=filename, filemode=backed)
else:
# load everything into memory
constructor_args = _read_args_from_h5ad(filename=filename, chunk_size=chunk_size)
X = constructor_args[0]
dtype = None
if X is not None:
dtype = X.dtype.name # maintain dtype, since 0.7
return AnnData(*_read_args_from_h5ad(filename=filename, chunk_size=chunk_size), dtype=dtype) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _read_args_from_h5ad( adata: AnnData = None, filename: Optional[PathLike] = None, mode: Optional[str] = None, chunk_size: int = 6000 ):
"""Return a tuple with the parameters for initializing AnnData. Parameters filename Defaults to the objects filename if ``None``. """ |
if filename is None and (adata is None or adata.filename is None):
raise ValueError('Need either a filename or an AnnData object with file backing')
# we need to be able to call the function without reference to self when
# not reading in backed mode
backed = mode is not None
if filename is None and not backed:
filename = adata.filename
d = {}
if backed:
f = adata.file._file
else:
f = h5py.File(filename, 'r')
for key in f.keys():
if backed and key in AnnData._BACKED_ATTRS:
d[key] = None
else:
_read_key_value_from_h5(f, d, key, chunk_size=chunk_size)
# backwards compat: save X with the correct name
if 'X' not in d:
if backed == 'r+':
for key in AnnData._H5_ALIASES['X']:
if key in d:
del f[key]
f.create_dataset('X', data=d[key])
break
# backwards compat: store sparse matrices properly
csr_keys = [key.replace('_csr_data', '')
for key in d if '_csr_data' in key]
for key in csr_keys:
d = load_sparse_csr(d, key=key)
if not backed:
f.close()
return AnnData._args_from_dict(d) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def make_index_unique(index: pd.Index, join: str = '-'):
"""Makes the index unique by appending '1', '2', etc. The first occurance of a non-unique value is ignored. Parameters join The connecting string between name and integer. Examples -------- Index(['a', 'b', 'c', 'd', 'b', 'b'], dtype='object') Index(['a', 'b', 'c', 'd', 'b-1', 'b-2'], dtype='object') """ |
if index.is_unique:
return index
from collections import defaultdict
values = index.values
indices_dup = index.duplicated(keep='first')
values_dup = values[indices_dup]
counter = defaultdict(lambda: 0)
for i, v in enumerate(values_dup):
counter[v] += 1
values_dup[i] += join + str(counter[v])
values[indices_dup] = values_dup
index = pd.Index(values)
return index |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _find_corresponding_multicol_key(key, keys_multicol):
"""Find the corresponding multicolumn key.""" |
for mk in keys_multicol:
if key.startswith(mk) and 'of' in key:
return mk
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _gen_keys_from_multicol_key(key_multicol, n_keys):
"""Generates single-column keys from multicolumn key.""" |
keys = [('{}{:03}of{:03}')
.format(key_multicol, i+1, n_keys) for i in range(n_keys)]
return keys |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _check_2d_shape(X):
"""Check shape of array or sparse matrix. Assure that X is always 2D: Unlike numpy we always deal with 2D arrays. """ |
if X.dtype.names is None and len(X.shape) != 2:
raise ValueError('X needs to be 2-dimensional, not '
'{}-dimensional.'.format(len(X.shape))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def to_df(self) -> pd.DataFrame: """Convert to pandas dataframe.""" |
df = pd.DataFrame(index=RangeIndex(0, self.shape[0], name=None))
for key in self.keys():
value = self[key]
for icolumn, column in enumerate(value.T):
df['{}{}'.format(key, icolumn+1)] = column
return df |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def isopen(self) -> bool: """State of backing file.""" |
if self._file is None:
return False
# try accessing the id attribute to see if the file is open
return bool(self._file.id) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def transpose(self) -> 'AnnData': """Transpose whole object. Data matrix is transposed, observations and variables are interchanged. """ |
if not self.isbacked: X = self._X
else: X = self.file['X']
if self.isview:
raise ValueError(
'You\'re trying to transpose a view of an `AnnData`, which is currently not implemented. '
'Call `.copy()` before transposing.')
layers = {k:(v.T.tocsr() if sparse.isspmatrix_csr(v) else v.T) for (k, v) in self.layers.items(copy=False)}
if sparse.isspmatrix_csr(X):
return AnnData(X.T.tocsr(), self._var, self._obs, self._uns,
self._varm.flipped(), self._obsm.flipped(),
filename=self.filename, layers=layers, dtype=self.X.dtype.name)
return AnnData(X.T, self._var, self._obs, self._uns,
self._varm.flipped(), self._obsm.flipped(),
filename=self.filename, layers=layers, dtype=self.X.dtype.name) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def copy(self, filename: Optional[PathLike] = None) -> 'AnnData': """Full copy, optionally on disk.""" |
if not self.isbacked:
return AnnData(self._X.copy() if self._X is not None else None,
self._obs.copy(),
self._var.copy(),
# deepcopy on DictView does not work and is unnecessary
# as uns was copied already before
self._uns.copy() if isinstance(self._uns, DictView) else deepcopy(self._uns),
self._obsm.copy(), self._varm.copy(),
raw=None if self._raw is None else self._raw.copy(),
layers=self.layers.as_dict(),
dtype=self._X.dtype.name if self._X is not None else 'float32')
else:
if filename is None:
raise ValueError(
'To copy an AnnData object in backed mode, '
'pass a filename: `.copy(filename=\'myfilename.h5ad\')`.')
if self.isview:
self.write(filename)
else:
from shutil import copyfile
copyfile(self.filename, filename)
return AnnData(filename=filename) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write_h5ad( self, filename: Optional[PathLike] = None, compression: Optional[str] = None, compression_opts: Union[int, Any] = None, force_dense: Optional[bool] = None ):
"""Write ``.h5ad``-formatted hdf5 file. .. note:: Setting compression to ``'gzip'`` can save disk space but will slow down writing and subsequent reading. Prior to v0.6.16, this was the default for parameter ``compression``. Generally, if you have sparse data that are stored as a dense matrix, you can dramatically improve performance and reduce disk space by converting to a :class:`~scipy.sparse.csr_matrix`:: from scipy.sparse import csr_matrix adata.X = csr_matrix(adata.X) Parameters filename Filename of data file. Defaults to backing file. compression : ``None``, {``'gzip'``, ``'lzf'``} (default: ``None``) See the h5py :ref:`dataset_compression`. compression_opts See the h5py :ref:`dataset_compression`. force_dense Write sparse data as a dense matrix. Defaults to ``True`` if object is backed, otherwise to ``False``. """ |
from .readwrite.write import _write_h5ad
if filename is None and not self.isbacked:
raise ValueError('Provide a filename!')
if filename is None:
filename = self.filename
if force_dense is None:
force_dense = self.isbacked
_write_h5ad(filename, self, compression=compression,
compression_opts=compression_opts, force_dense=force_dense)
if self.isbacked:
self.file.close() |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write_csvs(self, dirname: PathLike, skip_data: bool = True, sep: str = ','):
"""Write annotation to ``.csv`` files. It is not possible to recover the full :class:`~anndata.AnnData` from the output of this function. Use :meth:`~anndata.AnnData.write` for this. Parameters dirname Name of directory to which to export. skip_data Skip the data matrix :attr:`X`. sep Separator for the data. """ |
from .readwrite.write import write_csvs
write_csvs(dirname, self, skip_data=skip_data, sep=sep) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write_loom(self, filename: PathLike, write_obsm_varm: bool = False):
"""Write ``.loom``-formatted hdf5 file. Parameters filename The filename. """ |
from .readwrite.write import write_loom
write_loom(filename, self, write_obsm_varm = write_obsm_varm) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def write_zarr( self, store: Union[MutableMapping, PathLike], ):
"""Write a hierarchical Zarr array store. Parameters store The filename, a :class:`~typing.MutableMapping`, or a Zarr storage class. chunks Chunk shape. """ |
from .readwrite.write import write_zarr
write_zarr(store, self, chunks=chunks) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _to_dict_fixed_width_arrays(self, var_len_str=True):
"""A dict of arrays that stores data and annotation. It is sufficient for reconstructing the object. """ |
self.strings_to_categoricals()
obs_rec, uns_obs = df_to_records_fixed_width(self._obs, var_len_str)
var_rec, uns_var = df_to_records_fixed_width(self._var, var_len_str)
layers = self.layers.as_dict()
d = {
'X': self._X,
'obs': obs_rec,
'var': var_rec,
'obsm': self._obsm,
'varm': self._varm,
'layers': layers,
# add the categories to the unstructured annotation
'uns': {**self._uns, **uns_obs, **uns_var}}
if self.raw is not None:
self.strings_to_categoricals(self.raw._var)
var_rec, uns_var = df_to_records_fixed_width(self.raw._var, var_len_str)
d['raw.X'] = self.raw.X
d['raw.var'] = var_rec
d['raw.varm'] = self.raw.varm
d['raw.cat'] = uns_var
return d |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _expose_rule_functions(self, expose_all_rules=False):
"""add parse functions for public grammar rules Defines a function for each public grammar rule, based on introspecting the grammar. For example, the `c_interval` rule is exposed as a method `parse_c_interval` and used like this:: """ |
def make_parse_rule_function(rule_name):
"builds a wrapper function that parses a string with the specified rule"
def rule_fxn(s):
try:
return self._grammar(s).__getattr__(rule_name)()
except ometa.runtime.ParseError as exc:
raise HGVSParseError("{s}: char {exc.position}: {reason}".format(
s=s, exc=exc, reason=exc.formatReason()))
rule_fxn.__doc__ = "parse string s using `%s' rule" % rule_name
return rule_fxn
exposed_rule_re = re.compile(r"hgvs_(variant|position)|(c|g|m|n|p|r)"
r"_(edit|hgvs_position|interval|pos|posedit|variant)")
exposed_rules = [
m.replace("rule_", "") for m in dir(self._grammar._grammarClass)
if m.startswith("rule_")
]
if not expose_all_rules:
exposed_rules = [
rule_name for rule_name in exposed_rules if exposed_rule_re.match(rule_name)
]
for rule_name in exposed_rules:
att_name = "parse_" + rule_name
rule_fxn = make_parse_rule_function(rule_name)
self.__setattr__(att_name, rule_fxn)
self._logger.debug("Exposed {n} rules ({rules})".format(
n=len(exposed_rules), rules=", ".join(exposed_rules))) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def format_sequence(seq, start=None, end=None, group_size=3):
"""print seq from [start, end) in groups of size 3 6 9 12 15 | | | | | 2001 AAA BBB CCC DDD EEE """ |
width = 100
loc_width = 9
sep = " "
body_sep = " : "
start = start or 0
end = end or len(seq)
bw = width - loc_width - len(body_sep)
assert group_size <= bw, "group size must be less than available line width"
gpl = int((bw + len(sep)) / (group_size + len(sep))) # groups per line
gpl = int(gpl / 5) * 5 if gpl > 20 else gpl
rpl = group_size * gpl
line_fmt = "{{l:>{lw}s}}{body_sep}{{body}}".format(lw=loc_width, body_sep=body_sep)
ge_fmt = "{{ge:>{gs}}}".format(gs=group_size)
blocks = []
for ls in range(start, end, rpl):
le = ls + rpl
groups = [
ge_fmt.format(ge=str(gs + group_size)[-group_size + 1:])
for gs in range(ls, le, group_size)
]
blocks += [line_fmt.format(l="", body=sep.join(groups)) + "\n"]
groups = [seq[gs:min(gs + group_size, end)] for gs in range(ls, le, group_size)]
blocks += [line_fmt.format(l=str(ls + 1), body=sep.join(groups)) + "\n"]
blocks += ["\n"]
return blocks |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _stage_from_version(version):
"""return "prd", "stg", or "dev" for the given version string. A value is always returned""" |
if version:
m = re.match(r"^(?P<xyz>\d+\.\d+\.\d+)(?P<extra>.*)", version)
if m:
return "stg" if m.group("extra") else "prd"
return "dev" |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_ncbi_db_url():
"""returns NCBI DB URL based on environment variables and code version * if NCBI_DB_URL is set, use that * Otherwise, if _NCBI_URL_KEY is set, use that as the name of a config file entry and use the corresponding URL * Otherwise, """ |
if "NCBI_DB_URL" in os.environ:
return os.environ["NCBI_DB_URL"]
if "_NCBI_URL_KEY" in os.environ:
url_key = os.environ["_NCBI_URL_KEY"]
else:
sdlc = _stage_from_version(hgvs.__version__)
url_key = "public_{sdlc}".format(sdlc=sdlc)
return hgvs.global_config['NCBI'][url_key] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_cursor(self, n_retries=1):
"""Returns a context manager for obtained from a single or pooled connection, and sets the PostgreSQL search_path to the schema specified in the connection URL. Although *connections* are threadsafe, *cursors* are bound to connections and are *not* threadsafe. Do not share cursors across threads. Use this funciton like this:: with hdp._get_cursor() as cur: # your code Do not call this function outside a contextmanager. """ |
n_tries_rem = n_retries + 1
while n_tries_rem > 0:
try:
conn = self._pool.getconn() if self.pooling else self._conn
# autocommit=True obviates closing explicitly
conn.autocommit = True
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
cur.execute("set search_path = {self.url.schema};".format(self=self))
yield cur
# contextmanager executes these when context exits
cur.close()
if self.pooling:
self._pool.putconn(conn)
break
except psycopg2.OperationalError:
_logger.warning(
"Lost connection to {url}; attempting reconnect".format(url=self.url))
if self.pooling:
self._pool.closeall()
self._connect()
_logger.warning("Reconnected to {url}".format(url=self.url))
n_tries_rem -= 1
else:
# N.B. Probably never reached
raise HGVSError("Permanently lost connection to {url} ({n} retries)".format(
url=self.url, n=n_retries)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def project_interval_forward(self, c_interval):
""" project c_interval on the source transcript to the destination transcript :param c_interval: an :class:`hgvs.interval.Interval` object on the source transcript :returns: c_interval: an :class:`hgvs.interval.Interval` object on the destination transcript """ |
return self.dst_tm.g_to_c(self.src_tm.c_to_g(c_interval)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def project_interval_backward(self, c_interval):
""" project c_interval on the destination transcript to the source transcript :param c_interval: an :class:`hgvs.interval.Interval` object on the destination transcript :returns: c_interval: an :class:`hgvs.interval.Interval` object on the source transcript """ |
return self.src_tm.g_to_c(self.dst_tm.c_to_g(c_interval)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _convert_edit_check_strand(strand, edit_in):
""" Convert an edit from one type to another, based on the stand and type """ |
if isinstance(edit_in, hgvs.edit.NARefAlt):
if strand == 1:
edit_out = copy.deepcopy(edit_in)
else:
try:
# if smells like an int, do nothing
# TODO: should use ref_n, right?
int(edit_in.ref)
ref = edit_in.ref
except (ValueError, TypeError):
ref = reverse_complement(edit_in.ref)
edit_out = hgvs.edit.NARefAlt(
ref=ref,
alt=reverse_complement(edit_in.alt),
)
elif isinstance(edit_in, hgvs.edit.Dup):
if strand == 1:
edit_out = copy.deepcopy(edit_in)
else:
edit_out = hgvs.edit.Dup(ref=reverse_complement(edit_in.ref))
elif isinstance(edit_in, hgvs.edit.Inv):
if strand == 1:
edit_out = copy.deepcopy(edit_in)
else:
try:
int(edit_in.ref)
ref = edit_in.ref
except (ValueError, TypeError):
ref = reverse_complement(edit_in.ref)
edit_out = hgvs.edit.Inv(ref=ref)
else:
raise NotImplementedError("Only NARefAlt/Dup/Inv types are currently implemented")
return edit_out |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def t_to_p(self, var_t):
"""Return a protein variant, or "non-coding" for non-coding variant types CAUTION: Unlike other x_to_y methods that always return SequenceVariant instances, this method returns a string when the variant type is ``n``. This is intended as a convenience, particularly when looping over ``relevant_transcripts``, projecting with ``g_to_t``, then desiring a protein representation for coding transcripts. """ |
if var_t.type == "n":
return "non-coding"
if var_t.type == "c":
return self.c_to_p(var_t)
raise HGVSInvalidVariantError("Expected a coding (c.) or non-coding (n.) variant; got " +
str(var_t)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _fetch_AlignmentMapper(self, tx_ac, alt_ac=None, alt_aln_method=None):
"""convenience version of VariantMapper._fetch_AlignmentMapper that derives alt_ac from transcript, assembly, and alt_aln_method used to instantiate the AssemblyMapper instance """ |
if alt_ac is None:
alt_ac = self._alt_ac_for_tx_ac(tx_ac)
if alt_aln_method is None:
alt_aln_method = self.alt_aln_method
return super(AssemblyMapper, self)._fetch_AlignmentMapper(tx_ac, alt_ac, alt_aln_method) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _maybe_normalize(self, var):
"""normalize variant if requested, and ignore HGVSUnsupportedOperationError This is better than checking whether the variant is intronic because future UTAs will support LRG, which will enable checking intronic variants. """ |
if self.normalize:
try:
return self._norm.normalize(var)
except HGVSUnsupportedOperationError as e:
_logger.warning(str(e) + "; returning unnormalized variant")
# fall through to return unnormalized variant
return var |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _parse_cigar(self, cigar):
"""For a given CIGAR string, return the start positions of each aligned segment in ref and tgt, and a list of CIGAR operators. """ |
ces = [m.groupdict() for m in cigar_re.finditer(cigar)]
ref_pos = [None] * len(ces)
tgt_pos = [None] * len(ces)
cigar_op = [None] * len(ces)
ref_cur = tgt_cur = 0
for i, ce in enumerate(ces):
ref_pos[i] = ref_cur
tgt_pos[i] = tgt_cur
cigar_op[i] = ce["op"]
step = int(ce["len"])
if ce["op"] in "=MINX":
ref_cur += step
if ce["op"] in "=MDX":
tgt_cur += step
ref_pos.append(ref_cur)
tgt_pos.append(tgt_cur)
return ref_pos, tgt_pos, cigar_op |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _map(self, from_pos, to_pos, pos, base):
"""Map position between aligned sequences Positions in this function are 0-based. """ |
pos_i = -1
while pos_i < len(self.cigar_op) and pos >= from_pos[pos_i + 1]:
pos_i += 1
if pos_i == -1 or pos_i == len(self.cigar_op):
raise HGVSInvalidIntervalError("Position is beyond the bounds of transcript record")
if self.cigar_op[pos_i] in "=MX":
mapped_pos = to_pos[pos_i] + (pos - from_pos[pos_i])
mapped_pos_offset = 0
elif self.cigar_op[pos_i] in "DI":
if base == "start":
mapped_pos = to_pos[pos_i] - 1
elif base == "end":
mapped_pos = to_pos[pos_i]
mapped_pos_offset = 0
elif self.cigar_op[pos_i] == "N":
if pos - from_pos[pos_i] + 1 <= from_pos[pos_i + 1] - pos:
mapped_pos = to_pos[pos_i] - 1
mapped_pos_offset = pos - from_pos[pos_i] + 1
else:
mapped_pos = to_pos[pos_i]
mapped_pos_offset = -(from_pos[pos_i + 1] - pos)
return mapped_pos, mapped_pos_offset, self.cigar_op[pos_i] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def build_tx_cigar(exons, strand):
"""builds a single CIGAR string representing an alignment of the transcript sequence to a reference sequence, including introns. The input exons are expected to be in transcript order, and the resulting CIGAR is also in transcript order. True """ |
cigarelem_re = re.compile(r"\d+[=DIMNX]")
def _reverse_cigar(c):
return ''.join(reversed(cigarelem_re.findall(c)))
if len(exons) == 0:
return None
# flip orientation of all CIGARs if on - strand
if strand == -1:
cigars = [_reverse_cigar(e["cigar"]) for e in exons]
else:
cigars = [e["cigar"] for e in exons]
tx_cigar = [cigars[0]] # exon 1
for i in range(1, len(cigars)): # and intron + exon pairs thereafter
intron = str(exons[i]["alt_start_i"] - exons[i - 1]["alt_end_i"]) + "N"
tx_cigar += [intron, cigars[i]]
tx_cigar_str = "".join(tx_cigar)
return tx_cigar_str |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _check_if_ins_is_dup(self, start, insertion):
"""Helper to identify an insertion as a duplicate :param start: 1-based insertion start :type start: int :param insertion: sequence :type insertion: str :return (is duplicate, variant start) :rtype (bool, int) """ |
is_dup = False # assume no
variant_start = None
dup_candidate_start = start - len(insertion) - 1
dup_candidate = self._ref_seq[dup_candidate_start:dup_candidate_start + len(insertion)]
if insertion == dup_candidate:
is_dup = True
variant_start = dup_candidate_start + 1
return is_dup, variant_start |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _create_variant(self, start, end, ref, alt, fsext_len=None, is_dup=False, acc=None, is_ambiguous=False, is_sub=False, is_ext=False, is_no_protein=False, is_init_met=False):
"""Creates a SequenceVariant object""" |
if is_init_met:
posedit = AARefAlt(ref=ref, alt=alt, init_met=True)
elif is_ambiguous:
posedit = None
else:
interval = Interval(start=start, end=end)
# Note - order matters
if is_no_protein:
edit = '0'
elif is_sub:
edit = AASub(ref=ref, alt=alt)
elif is_ext:
edit = AAExt(ref=ref, alt=alt, aaterm='*', length=fsext_len)
elif self._is_frameshift:
edit = AAFs(ref=ref, alt=alt, length=fsext_len)
elif is_dup:
edit = Dup()
elif ref == alt == '':
edit = AARefAlt(ref='', alt='')
else:
edit = AARefAlt(ref=ref, alt=alt)
posedit = PosEdit(
pos=interval,
edit=edit,
uncertain=hgvs.global_config.mapping.inferred_p_is_uncertain)
var_p = hgvs.sequencevariant.SequenceVariant(acc, 'p', posedit)
return var_p |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def connect(db_url=None, pooling=hgvs.global_config.uta.pooling, application_name=None, mode=None, cache=None):
"""Connect to a UTA database instance and return a UTA interface instance. :param db_url: URL for database connection :type db_url: string :param pooling: whether to use connection pooling (postgresql only) :type pooling: bool :param application_name: log application name in connection (useful for debugging; PostgreSQL only) :type application_name: str When called with an explicit db_url argument, that db_url is used for connecting. When called without an explicit argument, the function default is determined by the environment variable UTA_DB_URL if it exists, or hgvs.datainterface.uta.public_db_url otherwise. '1.1' The format of the db_url is driver://user:pass@host/database/schema (the same as that used by SQLAlchemy). Examples: A remote public postgresql database: postgresql://anonymous:anonymous@uta.biocommons.org/uta/uta_20170707' A local postgresql database: postgresql://localhost/uta_dev/uta_20170707 For postgresql db_urls, pooling=True causes connect to use a psycopg2.pool.ThreadedConnectionPool. """ |
_logger.debug('connecting to ' + str(db_url) + '...')
if db_url is None:
db_url = _get_uta_db_url()
url = _parse_url(db_url)
if url.scheme == 'sqlite':
conn = UTA_sqlite(url, mode, cache)
elif url.scheme == 'postgresql':
conn = UTA_postgresql(
url=url, pooling=pooling, application_name=application_name, mode=mode, cache=cache)
else:
# fell through connection scheme cases
raise RuntimeError("{url.scheme} in {url} is not currently supported".format(url=url))
_logger.info('connected to ' + str(db_url) + '...')
return conn |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_tx_for_region(self, alt_ac, alt_aln_method, start_i, end_i):
""" return transcripts that overlap given region :param str alt_ac: reference sequence (e.g., NC_000007.13) :param str alt_aln_method: alignment method (e.g., splign) :param int start_i: 5' bound of region :param int end_i: 3' bound of region """ |
return self._fetchall(self._queries['tx_for_region'],
[alt_ac, alt_aln_method, start_i, end_i]) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_tx_identity_info(self, tx_ac):
"""returns features associated with a single transcript. :param tx_ac: transcript accession with version (e.g., 'NM_199425.2') :type tx_ac: str # database output tx_ac | NM_199425.2 alt_ac | NM_199425.2 alt_aln_method | transcript cds_start_i | 283 cds_end_i | 1003 lengths | {707,79,410} hgnc | VSX1 """ |
rows = self._fetchall(self._queries['tx_identity_info'], [tx_ac])
if len(rows) == 0:
raise HGVSDataNotAvailableError(
"No transcript definition for (tx_ac={tx_ac})".format(tx_ac=tx_ac))
return rows[0] |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_similar_transcripts(self, tx_ac):
"""Return a list of transcripts that are similar to the given transcript, with relevant similarity criteria. >> sim_tx = hdp.get_similar_transcripts('NM_001285829.1') >> dict(sim_tx[0]) { 'cds_eq': False, 'cds_es_fp_eq': False, 'es_fp_eq': True, 'tx_ac1': 'NM_001285829.1', 'tx_ac2': 'ENST00000498907' } where: * cds_eq means that the CDS sequences are identical * es_fp_eq means that the full exon structures are identical (i.e., incl. UTR) * cds_es_fp_eq means that the cds-clipped portions of the exon structures are identical (i.e., ecluding. UTR) * Hint: "es" = "exon set", "fp" = "fingerprint", "eq" = "equal" "exon structure" refers to the start and end coordinates on a specified reference sequence. Thus, having the same exon structure means that the transcripts are defined on the same reference sequence and have the same exon spans on that sequence. """ |
rows = self._fetchall(self._queries['tx_similar'], [tx_ac])
return rows |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
| def _make_key(func,
args,
kwds,
typed,
kwd_mark=(object(), ),
fasttypes={int, str, frozenset, type(None)},
sorted=sorted,
tuple=tuple,
type=type,
len=len):
'Make a cache key from optionally typed positional and keyword arguments'
key = args
key += kwd_mark
key += ('__func__', func)
if kwds:
sorted_items = sorted(kwds.items())
for item in sorted_items:
key += item
if typed:
key += tuple(type(v) for v in args)
if kwds:
key += tuple(type(v) for k, v in sorted_items)
elif len(key) == 1 and type(key[0]) in fasttypes:
return key[0]
return _HashedSeq(key) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_boundary(self, var):
"""Get the position of exon-intron boundary for current variant """ |
if var.type == "r" or var.type == "n":
if self.cross_boundaries:
return 0, float("inf")
else:
# Get genomic sequence access number for this transcript
map_info = self.hdp.get_tx_mapping_options(var.ac)
if not map_info:
raise HGVSDataNotAvailableError(
"No mapping info available for {ac}".format(ac=var.ac))
map_info = [
item for item in map_info if item["alt_aln_method"] == self.alt_aln_method
]
alt_ac = map_info[0]["alt_ac"]
# Get tx info
tx_info = self.hdp.get_tx_info(var.ac, alt_ac, self.alt_aln_method)
cds_start = tx_info["cds_start_i"]
cds_end = tx_info["cds_end_i"]
# Get exon info
exon_info = self.hdp.get_tx_exons(var.ac, alt_ac, self.alt_aln_method)
exon_starts = [exon["tx_start_i"] for exon in exon_info]
exon_ends = [exon["tx_end_i"] for exon in exon_info]
exon_starts.sort()
exon_ends.sort()
exon_starts.append(exon_ends[-1])
exon_ends.append(float("inf"))
# Find the end pos of the exon where the var locates
left = 0
right = float("inf")
# TODO: #242: implement methods to find tx regions
for i, _ in enumerate(exon_starts):
if (var.posedit.pos.start.base - 1 >= exon_starts[i]
and var.posedit.pos.start.base - 1 < exon_ends[i]):
break
for j, _ in enumerate(exon_starts):
if (var.posedit.pos.end.base - 1 >= exon_starts[j]
and var.posedit.pos.end.base - 1 < exon_ends[j]):
break
if i != j:
raise HGVSUnsupportedOperationError(
"Unsupported normalization of variants spanning the exon-intron boundary ({var})"
.format(var=var))
left = exon_starts[i]
right = exon_ends[i]
if cds_start is None:
pass
elif var.posedit.pos.end.base - 1 < cds_start:
right = min(right, cds_start)
elif var.posedit.pos.start.base - 1 >= cds_start:
left = max(left, cds_start)
else:
raise HGVSUnsupportedOperationError(
"Unsupported normalization of variants spanning the UTR-exon boundary ({var})"
.format(var=var))
if cds_end is None:
pass
elif var.posedit.pos.start.base - 1 >= cds_end:
left = max(left, cds_end)
elif var.posedit.pos.end.base - 1 < cds_end:
right = min(right, cds_end)
else:
raise HGVSUnsupportedOperationError(
"Unsupported normalization of variants spanning the exon-UTR boundary ({var})"
.format(var=var))
return left, right
else:
# For variant type of g and m etc.
return 0, float("inf") |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_tgt_length(self, var):
"""Get the total length of the whole reference sequence """ |
if var.type == "g" or var.type == "m":
return float("inf")
else:
# Get genomic sequence access number for this transcript
identity_info = self.hdp.get_tx_identity_info(var.ac)
if not identity_info:
raise HGVSDataNotAvailableError(
"No identity info available for {ac}".format(ac=var.ac))
tgt_len = sum(identity_info["lengths"])
return tgt_len |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _fetch_bounded_seq(self, var, start, end, window_size, boundary):
"""Fetch reference sequence from hgvs data provider. The start position is 0 and the interval is half open """ |
var_len = end - start - window_size
start = start if start >= boundary[0] else boundary[0]
end = end if end <= boundary[1] else boundary[1]
if start >= end:
return ""
seq = self.hdp.get_seq(var.ac, start, end)
if len(seq) < end - start and len(seq) < var_len:
raise HGVSInvalidVariantError(
"Variant span is outside sequence bounds ({var})".format(var=var))
return seq |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_ref_alt(self, var, boundary):
"""Get reference allele and alternative allele of the variant """ |
# Get reference allele
if var.posedit.edit.type == "ins" or var.posedit.edit.type == "dup":
ref = ""
else:
# For NARefAlt and Inv
if var.posedit.edit.ref_s is None or var.posedit.edit.ref == "":
ref = self._fetch_bounded_seq(var, var.posedit.pos.start.base - 1,
var.posedit.pos.end.base, 0, boundary)
else:
ref = var.posedit.edit.ref
# Get alternative allele
if var.posedit.edit.type == "sub" or var.posedit.edit.type == "delins" or var.posedit.edit.type == "ins":
alt = var.posedit.edit.alt
elif var.posedit.edit.type == "del":
alt = ""
elif var.posedit.edit.type == "dup":
alt = var.posedit.edit.ref or self._fetch_bounded_seq(
var, var.posedit.pos.start.base - 1, var.posedit.pos.end.base, 0, boundary)
elif var.posedit.edit.type == "inv":
alt = reverse_complement(ref)
elif var.posedit.edit.type == "identity":
alt = ref
return ref, alt |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def trim_common_suffixes(strs, min_len=0):
""" trim common suffixes (0, 'A') """ |
if len(strs) < 2:
return 0, strs
rev_strs = [s[::-1] for s in strs]
trimmed, rev_strs = trim_common_prefixes(rev_strs, min_len)
if trimmed:
strs = [s[::-1] for s in rev_strs]
return trimmed, strs |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def trim_common_prefixes(strs, min_len=0):
"""trim common prefixes""" |
trimmed = 0
if len(strs) > 1:
s1 = min(strs)
s2 = max(strs)
for i in range(len(s1) - min_len):
if s1[i] != s2[i]:
break
trimmed = i + 1
if trimmed > 0:
strs = [s[trimmed:] for s in strs]
return trimmed, strs |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def normalize_alleles_left(ref, start, stop, alleles, bound, ref_step, shuffle=True):
""" Normalize loci by removing extraneous reference padding shuffled_alleles(start=1, stop=2, alleles='A') """ |
normalized_alleles = namedtuple('shuffled_alleles', 'start stop alleles')
if len(alleles) < 2:
return normalized_alleles(start, stop, alleles)
# STEP 1: Trim common suffix
trimmed, alleles = trim_common_suffixes(alleles)
stop -= trimmed
# STEP 2: Trim common prefix
trimmed, alleles = trim_common_prefixes(alleles)
start += trimmed
# assert bound <= start,'start={:d}, left bound={:d}'.format(start, bound)
# STEP 3: While a null allele exists, left shuffle by prepending alleles
# with reference and trimming common suffixes
while shuffle and '' in alleles and start > bound:
step = min(ref_step, start - bound)
r = ref[start - step:start].upper()
new_alleles = [r + a for a in alleles]
trimmed, new_alleles = trim_common_suffixes(new_alleles)
if not trimmed:
break
start -= trimmed
stop -= trimmed
if trimmed == step:
alleles = new_alleles
else:
left = step - trimmed
alleles = [a[left:] for a in new_alleles]
break
return normalized_alleles(start, stop, tuple(alleles)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def validate_type_ac_pair(type, ac):
"""validate that accession is correct for variant type AND that accession is fully specified. """ |
assert type in valid_pairs, "Unknown variant type " + type
if valid_pairs[type].match(ac):
return (ValidationLevel.VALID,
"Accession ({ac}) is compatible with variant type {type}".format(ac=ac, type=type))
elif invalid_pairs[type].match(ac):
return (ValidationLevel.ERROR,
"Accession ({ac}) is not compatible with variant type {type}".format(
ac=ac, type=type))
else:
return (ValidationLevel.WARNING,
"Accession ({ac}) is not known to be compatible with variant type {type}".format(
ac=ac, type=type)) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def build_altseq(self):
"""given a variant and a sequence, incorporate the variant and return the new sequence Data structure returned is analogous to the data structure used to return the variant sequence, but with an additional parameter denoting the start of a frameshift that should affect all bases downstream. :returns variant sequence data :rtype list of dictionaries """ |
NOT_CDS = "not_cds_variant"
WHOLE_GENE_DELETED = "whole_gene_deleted"
type_map = {
NARefAlt: self._incorporate_delins,
Dup: self._incorporate_dup,
Inv: self._incorporate_inv,
Repeat: self._incorporate_repeat,
NOT_CDS: self._create_alt_equals_ref_noncds,
WHOLE_GENE_DELETED: self._create_no_protein
}
# should loop over each allele rather than assume only 1 variant; return a list for now
alt_data = []
variant_location = self._get_variant_region()
if variant_location == self.EXON:
edit_type = type(self._var_c.posedit.edit)
elif variant_location == self.INTRON:
edit_type = NOT_CDS
elif variant_location == self.T_UTR:
edit_type = NOT_CDS
elif variant_location == self.F_UTR:
# TODO: handle case where variant introduces a Met (new start)
edit_type = NOT_CDS
elif variant_location == self.WHOLE_GENE:
if self._var_c.posedit.edit.type == "del":
edit_type = WHOLE_GENE_DELETED
elif self._var_c.posedit.edit.type == "dup":
_logger.warning(
"Whole-gene duplication; consequence assumed to not affect protein product")
edit_type = NOT_CDS
elif self._var_c.posedit.edit.type == "inv":
_logger.warning(
"Whole-gene inversion; consequence assumed to not affect protein product")
edit_type = NOT_CDS
else:
edit_type = NOT_CDS
else: # should never get here
raise ValueError("value_location = {}".format(variant_location))
try:
this_alt_data = type_map[edit_type]()
except KeyError:
raise NotImplementedError("c to p translation unsupported for {} type {}".format(
self._var_c, edit_type))
# get the start of the "terminal" frameshift (i.e. one never "cancelled out")
this_alt_data = self._get_frameshift_start(this_alt_data)
alt_data.append(this_alt_data)
if DBG:
print(this_alt_data.transcript_sequence)
return alt_data |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _incorporate_dup(self):
"""Incorporate dup into sequence""" |
seq, cds_start, cds_stop, start, end = self._setup_incorporate()
dup_seq = seq[start:end]
seq[end:end] = dup_seq
is_frameshift = len(dup_seq) % 3 != 0
variant_start_aa = int(math.ceil((self._var_c.posedit.pos.end.base + 1) / 3.0))
alt_data = AltTranscriptData(
seq,
cds_start,
cds_stop,
is_frameshift,
variant_start_aa,
self._transcript_data.protein_accession,
is_ambiguous=self._ref_has_multiple_stops)
return alt_data |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _incorporate_inv(self):
"""Incorporate inv into sequence""" |
seq, cds_start, cds_stop, start, end = self._setup_incorporate()
seq[start:end] = list(reverse_complement(''.join(seq[start:end])))
is_frameshift = False
variant_start_aa = max(int(math.ceil((self._var_c.posedit.pos.start.base) / 3.0)), 1)
alt_data = AltTranscriptData(
seq,
cds_start,
cds_stop,
is_frameshift,
variant_start_aa,
self._transcript_data.protein_accession,
is_ambiguous=self._ref_has_multiple_stops)
return alt_data |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _create_no_protein(self):
"""Create a no-protein result""" |
alt_data = AltTranscriptData([],
None,
None,
False,
None,
self._transcript_data.protein_accession,
is_ambiguous=False)
return alt_data |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def prepare_headers(self, headers, metadata, queue_derive=True):
"""Convert a dictionary of metadata into S3 compatible HTTP headers, and append headers to ``headers``. :type metadata: dict :param metadata: Metadata to be converted into S3 HTTP Headers and appended to ``headers``. :type headers: dict :param headers: (optional) S3 compatible HTTP headers. """ |
if not metadata.get('scanner'):
scanner = 'Internet Archive Python library {0}'.format(__version__)
metadata['scanner'] = scanner
prepared_metadata = prepare_metadata(metadata)
headers['x-archive-auto-make-bucket'] = '1'
if queue_derive is False:
headers['x-archive-queue-derive'] = '0'
else:
headers['x-archive-queue-derive'] = '1'
for meta_key, meta_value in prepared_metadata.items():
# Encode arrays into JSON strings because Archive.org does not
# yet support complex metadata structures in
# <identifier>_meta.xml.
if isinstance(meta_value, dict):
meta_value = json.dumps(meta_value)
# Convert the metadata value into a list if it is not already
# iterable.
if (isinstance(meta_value, six.string_types) or
not hasattr(meta_value, '__iter__')):
meta_value = [meta_value]
# Convert metadata items into HTTP headers and add to
# ``headers`` dict.
for i, value in enumerate(meta_value):
if not value:
continue
header_key = 'x-archive-meta{0:02d}-{1}'.format(i, meta_key)
if (isinstance(value, six.string_types) and needs_quote(value)):
if six.PY2 and isinstance(value, six.text_type):
value = value.encode('utf-8')
value = 'uri({0})'.format(urllib.parse.quote(value))
# because rfc822 http headers disallow _ in names, IA-S3 will
# translate two hyphens in a row (--) into an underscore (_).
header_key = header_key.replace('_', '--')
headers[header_key] = value
super(S3PreparedRequest, self).prepare_headers(headers) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def load_ia_module(cmd):
"""Dynamically import ia module.""" |
try:
if cmd in list(cmd_aliases.keys()) + list(cmd_aliases.values()):
_module = 'internetarchive.cli.ia_{0}'.format(cmd)
return __import__(_module, fromlist=['internetarchive.cli'])
else:
_module = 'ia_{0}'.format(cmd)
for ep in iter_entry_points('internetarchive.cli.plugins'):
if ep.name == _module:
return ep.load()
raise ImportError
except (ImportError, DistributionNotFound):
print("error: '{0}' is not an ia command! See 'ia help'".format(cmd),
file=sys.stderr)
matches = '\t'.join(difflib.get_close_matches(cmd, cmd_aliases.values()))
if matches:
print('\nDid you mean one of these?\n\t{0}'.format(matches))
sys.exit(127) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def main():
"""This is the CLI driver for ia-wrapper.""" |
args = docopt(__doc__, version=__version__, options_first=True)
# Validate args.
s = Schema({
six.text_type: bool,
'--config-file': Or(None, str),
'<args>': list,
'<command>': Or(str, lambda _: 'help'),
})
try:
args = s.validate(args)
except SchemaError as exc:
print('{0}\n{1}'.format(str(exc), printable_usage(__doc__)), file=sys.stderr)
sys.exit(1)
# Get subcommand.
cmd = args['<command>']
if cmd in cmd_aliases:
cmd = cmd_aliases[cmd]
if (cmd == 'help') or (not cmd):
if not args['<args>']:
sys.exit(print(__doc__.strip(), file=sys.stderr))
else:
ia_module = load_ia_module(args['<args>'][0])
sys.exit(print(ia_module.__doc__.strip(), file=sys.stderr))
if cmd != 'configure' and args['--config-file']:
if not os.path.isfile(args['--config-file']):
print('--config-file should be a readable file.\n{0}'.format(
printable_usage(__doc__)), file=sys.stderr)
sys.exit(1)
argv = [cmd] + args['<args>']
config = dict()
if args['--log']:
config['logging'] = {'level': 'INFO'}
elif args['--debug']:
config['logging'] = {'level': 'DEBUG'}
if args['--insecure']:
config['general'] = dict(secure=False)
session = get_session(config_file=args['--config-file'],
config=config,
debug=args['--debug'])
ia_module = load_ia_module(cmd)
try:
sys.exit(ia_module.main(argv, session))
except IOError as e:
# Handle Broken Pipe errors.
if e.errno == errno.EPIPE:
sys.stderr.close()
sys.stdout.close()
sys.exit(0)
else:
raise |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def suppress_keyboard_interrupt_message():
"""Register a new excepthook to suppress KeyboardInterrupt exception messages, and exit with status code 130. """ |
old_excepthook = sys.excepthook
def new_hook(type, value, traceback):
if type != KeyboardInterrupt:
old_excepthook(type, value, traceback)
else:
sys.exit(130)
sys.excepthook = new_hook |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def recursive_file_count(files, item=None, checksum=False):
"""Given a filepath or list of filepaths, return the total number of files.""" |
if not isinstance(files, (list, set)):
files = [files]
total_files = 0
if checksum is True:
md5s = [f.get('md5') for f in item.files]
else:
md5s = list()
if isinstance(files, dict):
# make sure to use local filenames.
_files = files.values()
else:
if isinstance(files[0], tuple):
_files = dict(files).values()
else:
_files = files
for f in _files:
try:
is_dir = os.path.isdir(f)
except TypeError:
try:
f = f[0]
is_dir = os.path.isdir(f)
except (AttributeError, TypeError):
is_dir = False
if is_dir:
for x, _ in iter_directory(f):
lmd5 = get_md5(open(x, 'rb'))
if lmd5 in md5s:
continue
else:
total_files += 1
else:
try:
lmd5 = get_md5(open(f, 'rb'))
except TypeError:
# Support file-like objects.
lmd5 = get_md5(f)
if lmd5 in md5s:
continue
else:
total_files += 1
return total_files |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def reraise_modify(caught_exc, append_msg, prepend=False):
"""Append message to exception while preserving attributes. Preserves exception class, and exception traceback. Note: This function needs to be called inside an except because `sys.exc_info()` requires the exception context. Args: caught_exc(Exception):
The caught exception object append_msg(str):
The message to append to the caught exception prepend(bool):
If True prepend the message to args instead of appending Returns: None Side Effects: Re-raises the exception with the preserved data / trace but modified message """ |
ExceptClass = type(caught_exc)
# Keep old traceback
traceback = sys.exc_info()[2]
if not caught_exc.args:
# If no args, create our own tuple
arg_list = [append_msg]
else:
# Take the last arg
# If it is a string
# append your message.
# Otherwise append it to the
# arg list(Not as pretty)
arg_list = list(caught_exc.args[:-1])
last_arg = caught_exc.args[-1]
if isinstance(last_arg, str):
if prepend:
arg_list.append(append_msg + last_arg)
else:
arg_list.append(last_arg + append_msg)
else:
arg_list += [last_arg, append_msg]
caught_exc.args = tuple(arg_list)
six.reraise(ExceptClass,
caught_exc,
traceback) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def configure(username=None, password=None, config_file=None):
"""Configure internetarchive with your Archive.org credentials. :type username: str :param username: The email address associated with your Archive.org account. :type password: str :param password: Your Archive.org password. Usage: """ |
username = input('Email address: ') if not username else username
password = getpass('Password: ') if not password else password
config_file_path = config_module.write_config_file(username, password, config_file)
return config_file_path |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_user_info(access_key, secret_key):
"""Returns details about an Archive.org user given an IA-S3 key pair. :type access_key: str :param access_key: IA-S3 access_key to use when making the given request. :type secret_key: str :param secret_key: IA-S3 secret_key to use when making the given request. """ |
u = 'https://s3.us.archive.org'
p = dict(check_auth=1)
r = requests.get(u, params=p, auth=auth.S3Auth(access_key, secret_key))
r.raise_for_status()
j = r.json()
if j.get('error'):
raise AuthenticationError(j.get('error'))
else:
return j |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def task_log(self):
"""Get task log. :rtype: str :returns: The task log as a string. """ |
if self.task_id is None:
raise ValueError('task_id is None')
return self.get_task_log(self.task_id, self.session, self.request_kwargs) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def get_task_log(task_id, session, request_kwargs=None):
"""Static method for getting a task log, given a task_id. This method exists so a task log can be retrieved without retrieving the items task history first. :type task_id: str or int :param task_id: The task id for the task log you'd like to fetch. :type archive_session: :class:`ArchiveSession <ArchiveSession>` :type request_kwargs: dict :param request_kwargs: (optional) Keyword arguments that :py:class:`requests.Request` takes. :rtype: str :returns: The task log as a string. """ |
request_kwargs = request_kwargs if request_kwargs else dict()
url = '{0}//catalogd.archive.org/log/{1}'.format(session.protocol, task_id)
p = dict(full=1)
r = session.get(url, params=p, **request_kwargs)
r.raise_for_status()
return r.content.decode('utf-8') |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def _get_user_agent_string(self):
"""Generate a User-Agent string to be sent with every request.""" |
uname = platform.uname()
try:
lang = locale.getlocale()[0][:2]
except:
lang = ''
py_version = '{0}.{1}.{2}'.format(*sys.version_info)
return 'internetarchive/{0} ({1} {2}; N; {3}; {4}) Python/{5}'.format(
__version__, uname[0], uname[-1], lang, self.access_key, py_version) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def rebuild_auth(self, prepared_request, response):
"""Never rebuild auth for archive.org URLs. """ |
u = urlparse(prepared_request.url)
if u.netloc.endswith('archive.org'):
return
super(ArchiveSession, self).rebuild_auth(prepared_request, response) |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def email_to(self):
""" Return the value entered for the first field of type EmailField. """ |
for field in self.form_fields:
if field.is_a(fields.EMAIL):
return self.cleaned_data[field.slug]
return None |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def unique_slug(manager, slug_field, slug):
""" Ensure slug is unique for the given manager, appending a digit if it isn't. """ |
max_length = manager.model._meta.get_field(slug_field).max_length
slug = slug[:max_length]
i = 0
while True:
if i > 0:
if i > 1:
slug = slug.rsplit("-", 1)[0]
# We need to keep the slug length under the slug fields max length. We need to
# account for the length that is added by adding a random integer and `-`.
slug = "%s-%s" % (slug[:max_length - len(str(i)) - 1], i)
if not manager.filter(**{slug_field: slug}):
break
i += 1
return slug |
<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
def import_attr(path):
""" Given a a Python dotted path to a variable in a module, imports the module and returns the variable in it. """ |
module_path, attr_name = path.rsplit(".", 1)
return getattr(import_module(module_path), attr_name) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.