repo
stringlengths 7
55
| path
stringlengths 4
223
| func_name
stringlengths 1
134
| original_string
stringlengths 75
104k
| language
stringclasses 1
value | code
stringlengths 75
104k
| code_tokens
listlengths 19
28.4k
| docstring
stringlengths 1
46.9k
| docstring_tokens
listlengths 1
1.97k
| sha
stringlengths 40
40
| url
stringlengths 87
315
| partition
stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.update_in_hdx
|
def update_in_hdx(self, update_resources=True, update_resources_by_name=True,
remove_additional_resources=False, create_default_views=True, hxl_update=True):
# type: (bool, bool, bool, bool, bool) -> None
"""Check if dataset exists in HDX and if so, update it
Args:
update_resources (bool): Whether to update resources. Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset. Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views. Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
"""
loaded = False
if 'id' in self.data:
self._check_existing_object('dataset', 'id')
if self._dataset_load_from_hdx(self.data['id']):
loaded = True
else:
logger.warning('Failed to load dataset with id %s' % self.data['id'])
if not loaded:
self._check_existing_object('dataset', 'name')
if not self._dataset_load_from_hdx(self.data['name']):
raise HDXError('No existing dataset to update!')
self._dataset_merge_hdx_update(update_resources=update_resources,
update_resources_by_name=update_resources_by_name,
remove_additional_resources=remove_additional_resources,
create_default_views=create_default_views,
hxl_update=hxl_update)
|
python
|
def update_in_hdx(self, update_resources=True, update_resources_by_name=True,
remove_additional_resources=False, create_default_views=True, hxl_update=True):
# type: (bool, bool, bool, bool, bool) -> None
"""Check if dataset exists in HDX and if so, update it
Args:
update_resources (bool): Whether to update resources. Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset. Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views. Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
"""
loaded = False
if 'id' in self.data:
self._check_existing_object('dataset', 'id')
if self._dataset_load_from_hdx(self.data['id']):
loaded = True
else:
logger.warning('Failed to load dataset with id %s' % self.data['id'])
if not loaded:
self._check_existing_object('dataset', 'name')
if not self._dataset_load_from_hdx(self.data['name']):
raise HDXError('No existing dataset to update!')
self._dataset_merge_hdx_update(update_resources=update_resources,
update_resources_by_name=update_resources_by_name,
remove_additional_resources=remove_additional_resources,
create_default_views=create_default_views,
hxl_update=hxl_update)
|
[
"def",
"update_in_hdx",
"(",
"self",
",",
"update_resources",
"=",
"True",
",",
"update_resources_by_name",
"=",
"True",
",",
"remove_additional_resources",
"=",
"False",
",",
"create_default_views",
"=",
"True",
",",
"hxl_update",
"=",
"True",
")",
":",
"# type: (bool, bool, bool, bool, bool) -> None",
"loaded",
"=",
"False",
"if",
"'id'",
"in",
"self",
".",
"data",
":",
"self",
".",
"_check_existing_object",
"(",
"'dataset'",
",",
"'id'",
")",
"if",
"self",
".",
"_dataset_load_from_hdx",
"(",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
":",
"loaded",
"=",
"True",
"else",
":",
"logger",
".",
"warning",
"(",
"'Failed to load dataset with id %s'",
"%",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
"if",
"not",
"loaded",
":",
"self",
".",
"_check_existing_object",
"(",
"'dataset'",
",",
"'name'",
")",
"if",
"not",
"self",
".",
"_dataset_load_from_hdx",
"(",
"self",
".",
"data",
"[",
"'name'",
"]",
")",
":",
"raise",
"HDXError",
"(",
"'No existing dataset to update!'",
")",
"self",
".",
"_dataset_merge_hdx_update",
"(",
"update_resources",
"=",
"update_resources",
",",
"update_resources_by_name",
"=",
"update_resources_by_name",
",",
"remove_additional_resources",
"=",
"remove_additional_resources",
",",
"create_default_views",
"=",
"create_default_views",
",",
"hxl_update",
"=",
"hxl_update",
")"
] |
Check if dataset exists in HDX and if so, update it
Args:
update_resources (bool): Whether to update resources. Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset. Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views. Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
|
[
"Check",
"if",
"dataset",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"it"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L500-L530
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.create_in_hdx
|
def create_in_hdx(self, allow_no_resources=False, update_resources=True, update_resources_by_name=True,
remove_additional_resources=False, create_default_views=True, hxl_update=True):
# type: (bool, bool, bool, bool, bool, bool) -> None
"""Check if dataset exists in HDX and if so, update it, otherwise create it
Args:
allow_no_resources (bool): Whether to allow no resources. Defaults to False.
update_resources (bool): Whether to update resources (if updating). Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset (if updating). Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views (if updating). Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
"""
self.check_required_fields(allow_no_resources=allow_no_resources)
loadedid = None
if 'id' in self.data:
if self._dataset_load_from_hdx(self.data['id']):
loadedid = self.data['id']
else:
logger.warning('Failed to load dataset with id %s' % self.data['id'])
if not loadedid:
if self._dataset_load_from_hdx(self.data['name']):
loadedid = self.data['name']
if loadedid:
logger.warning('Dataset exists. Updating %s' % loadedid)
self._dataset_merge_hdx_update(update_resources=update_resources,
update_resources_by_name=update_resources_by_name,
remove_additional_resources=remove_additional_resources,
create_default_views=create_default_views,
hxl_update=hxl_update)
return
filestore_resources = list()
if self.resources:
ignore_fields = ['package_id']
for resource in self.resources:
resource.check_required_fields(ignore_fields=ignore_fields)
if resource.get_file_to_upload():
filestore_resources.append(resource)
resource['url'] = Dataset.temporary_url
self.data['resources'] = self._convert_hdxobjects(self.resources)
self._save_to_hdx('create', 'name')
self._add_filestore_resources(filestore_resources, False, hxl_update)
|
python
|
def create_in_hdx(self, allow_no_resources=False, update_resources=True, update_resources_by_name=True,
remove_additional_resources=False, create_default_views=True, hxl_update=True):
# type: (bool, bool, bool, bool, bool, bool) -> None
"""Check if dataset exists in HDX and if so, update it, otherwise create it
Args:
allow_no_resources (bool): Whether to allow no resources. Defaults to False.
update_resources (bool): Whether to update resources (if updating). Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset (if updating). Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views (if updating). Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
"""
self.check_required_fields(allow_no_resources=allow_no_resources)
loadedid = None
if 'id' in self.data:
if self._dataset_load_from_hdx(self.data['id']):
loadedid = self.data['id']
else:
logger.warning('Failed to load dataset with id %s' % self.data['id'])
if not loadedid:
if self._dataset_load_from_hdx(self.data['name']):
loadedid = self.data['name']
if loadedid:
logger.warning('Dataset exists. Updating %s' % loadedid)
self._dataset_merge_hdx_update(update_resources=update_resources,
update_resources_by_name=update_resources_by_name,
remove_additional_resources=remove_additional_resources,
create_default_views=create_default_views,
hxl_update=hxl_update)
return
filestore_resources = list()
if self.resources:
ignore_fields = ['package_id']
for resource in self.resources:
resource.check_required_fields(ignore_fields=ignore_fields)
if resource.get_file_to_upload():
filestore_resources.append(resource)
resource['url'] = Dataset.temporary_url
self.data['resources'] = self._convert_hdxobjects(self.resources)
self._save_to_hdx('create', 'name')
self._add_filestore_resources(filestore_resources, False, hxl_update)
|
[
"def",
"create_in_hdx",
"(",
"self",
",",
"allow_no_resources",
"=",
"False",
",",
"update_resources",
"=",
"True",
",",
"update_resources_by_name",
"=",
"True",
",",
"remove_additional_resources",
"=",
"False",
",",
"create_default_views",
"=",
"True",
",",
"hxl_update",
"=",
"True",
")",
":",
"# type: (bool, bool, bool, bool, bool, bool) -> None",
"self",
".",
"check_required_fields",
"(",
"allow_no_resources",
"=",
"allow_no_resources",
")",
"loadedid",
"=",
"None",
"if",
"'id'",
"in",
"self",
".",
"data",
":",
"if",
"self",
".",
"_dataset_load_from_hdx",
"(",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
":",
"loadedid",
"=",
"self",
".",
"data",
"[",
"'id'",
"]",
"else",
":",
"logger",
".",
"warning",
"(",
"'Failed to load dataset with id %s'",
"%",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
"if",
"not",
"loadedid",
":",
"if",
"self",
".",
"_dataset_load_from_hdx",
"(",
"self",
".",
"data",
"[",
"'name'",
"]",
")",
":",
"loadedid",
"=",
"self",
".",
"data",
"[",
"'name'",
"]",
"if",
"loadedid",
":",
"logger",
".",
"warning",
"(",
"'Dataset exists. Updating %s'",
"%",
"loadedid",
")",
"self",
".",
"_dataset_merge_hdx_update",
"(",
"update_resources",
"=",
"update_resources",
",",
"update_resources_by_name",
"=",
"update_resources_by_name",
",",
"remove_additional_resources",
"=",
"remove_additional_resources",
",",
"create_default_views",
"=",
"create_default_views",
",",
"hxl_update",
"=",
"hxl_update",
")",
"return",
"filestore_resources",
"=",
"list",
"(",
")",
"if",
"self",
".",
"resources",
":",
"ignore_fields",
"=",
"[",
"'package_id'",
"]",
"for",
"resource",
"in",
"self",
".",
"resources",
":",
"resource",
".",
"check_required_fields",
"(",
"ignore_fields",
"=",
"ignore_fields",
")",
"if",
"resource",
".",
"get_file_to_upload",
"(",
")",
":",
"filestore_resources",
".",
"append",
"(",
"resource",
")",
"resource",
"[",
"'url'",
"]",
"=",
"Dataset",
".",
"temporary_url",
"self",
".",
"data",
"[",
"'resources'",
"]",
"=",
"self",
".",
"_convert_hdxobjects",
"(",
"self",
".",
"resources",
")",
"self",
".",
"_save_to_hdx",
"(",
"'create'",
",",
"'name'",
")",
"self",
".",
"_add_filestore_resources",
"(",
"filestore_resources",
",",
"False",
",",
"hxl_update",
")"
] |
Check if dataset exists in HDX and if so, update it, otherwise create it
Args:
allow_no_resources (bool): Whether to allow no resources. Defaults to False.
update_resources (bool): Whether to update resources (if updating). Defaults to True.
update_resources_by_name (bool): Compare resource names rather than position in list. Defaults to True.
remove_additional_resources (bool): Remove additional resources found in dataset (if updating). Defaults to False.
create_default_views (bool): Whether to call package_create_default_resource_views (if updating). Defaults to True.
hxl_update (bool): Whether to call package_hxl_update. Defaults to True.
Returns:
None
|
[
"Check",
"if",
"dataset",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"it",
"otherwise",
"create",
"it"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L532-L577
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.search_in_hdx
|
def search_in_hdx(cls, query='*:*', configuration=None, page_size=1000, **kwargs):
# type: (Optional[str], Optional[Configuration], int, Any) -> List['Dataset']
"""Searches for datasets in HDX
Args:
query (Optional[str]): Query (in Solr format). Defaults to '*:*'.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
**kwargs: See below
fq (string): Any filter queries to apply
sort (string): Sorting of the search results. Defaults to 'relevance asc, metadata_modified desc'.
rows (int): Number of matching rows to return. Defaults to all datasets (sys.maxsize).
start (int): Offset in the complete result for where the set of returned datasets should begin
facet (string): Whether to enable faceted results. Default to True.
facet.mincount (int): Minimum counts for facet fields should be included in the results
facet.limit (int): Maximum number of values the facet fields return (- = unlimited). Defaults to 50.
facet.field (List[str]): Fields to facet upon. Default is empty.
use_default_schema (bool): Use default package schema instead of custom schema. Defaults to False.
Returns:
List[Dataset]: list of datasets resulting from query
"""
dataset = Dataset(configuration=configuration)
total_rows = kwargs.get('rows', cls.max_int)
start = kwargs.get('start', 0)
all_datasets = None
attempts = 0
while attempts < cls.max_attempts and all_datasets is None: # if the count values vary for multiple calls, then must redo query
all_datasets = list()
counts = set()
for page in range(total_rows // page_size + 1):
pagetimespagesize = page * page_size
kwargs['start'] = start + pagetimespagesize
rows_left = total_rows - pagetimespagesize
rows = min(rows_left, page_size)
kwargs['rows'] = rows
_, result = dataset._read_from_hdx('dataset', query, 'q', Dataset.actions()['search'], **kwargs)
datasets = list()
if result:
count = result.get('count', None)
if count:
counts.add(count)
no_results = len(result['results'])
for datasetdict in result['results']:
dataset = Dataset(configuration=configuration)
dataset.old_data = dict()
dataset.data = datasetdict
dataset._dataset_create_resources()
datasets.append(dataset)
all_datasets += datasets
if no_results < rows:
break
else:
break
else:
logger.debug(result)
if all_datasets and len(counts) != 1: # Make sure counts are all same for multiple calls to HDX
all_datasets = None
attempts += 1
else:
ids = [dataset['id'] for dataset in all_datasets] # check for duplicates (shouldn't happen)
if len(ids) != len(set(ids)):
all_datasets = None
attempts += 1
if attempts == cls.max_attempts and all_datasets is None:
raise HDXError('Maximum attempts reached for searching for datasets!')
return all_datasets
|
python
|
def search_in_hdx(cls, query='*:*', configuration=None, page_size=1000, **kwargs):
# type: (Optional[str], Optional[Configuration], int, Any) -> List['Dataset']
"""Searches for datasets in HDX
Args:
query (Optional[str]): Query (in Solr format). Defaults to '*:*'.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
**kwargs: See below
fq (string): Any filter queries to apply
sort (string): Sorting of the search results. Defaults to 'relevance asc, metadata_modified desc'.
rows (int): Number of matching rows to return. Defaults to all datasets (sys.maxsize).
start (int): Offset in the complete result for where the set of returned datasets should begin
facet (string): Whether to enable faceted results. Default to True.
facet.mincount (int): Minimum counts for facet fields should be included in the results
facet.limit (int): Maximum number of values the facet fields return (- = unlimited). Defaults to 50.
facet.field (List[str]): Fields to facet upon. Default is empty.
use_default_schema (bool): Use default package schema instead of custom schema. Defaults to False.
Returns:
List[Dataset]: list of datasets resulting from query
"""
dataset = Dataset(configuration=configuration)
total_rows = kwargs.get('rows', cls.max_int)
start = kwargs.get('start', 0)
all_datasets = None
attempts = 0
while attempts < cls.max_attempts and all_datasets is None: # if the count values vary for multiple calls, then must redo query
all_datasets = list()
counts = set()
for page in range(total_rows // page_size + 1):
pagetimespagesize = page * page_size
kwargs['start'] = start + pagetimespagesize
rows_left = total_rows - pagetimespagesize
rows = min(rows_left, page_size)
kwargs['rows'] = rows
_, result = dataset._read_from_hdx('dataset', query, 'q', Dataset.actions()['search'], **kwargs)
datasets = list()
if result:
count = result.get('count', None)
if count:
counts.add(count)
no_results = len(result['results'])
for datasetdict in result['results']:
dataset = Dataset(configuration=configuration)
dataset.old_data = dict()
dataset.data = datasetdict
dataset._dataset_create_resources()
datasets.append(dataset)
all_datasets += datasets
if no_results < rows:
break
else:
break
else:
logger.debug(result)
if all_datasets and len(counts) != 1: # Make sure counts are all same for multiple calls to HDX
all_datasets = None
attempts += 1
else:
ids = [dataset['id'] for dataset in all_datasets] # check for duplicates (shouldn't happen)
if len(ids) != len(set(ids)):
all_datasets = None
attempts += 1
if attempts == cls.max_attempts and all_datasets is None:
raise HDXError('Maximum attempts reached for searching for datasets!')
return all_datasets
|
[
"def",
"search_in_hdx",
"(",
"cls",
",",
"query",
"=",
"'*:*'",
",",
"configuration",
"=",
"None",
",",
"page_size",
"=",
"1000",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional[str], Optional[Configuration], int, Any) -> List['Dataset']",
"dataset",
"=",
"Dataset",
"(",
"configuration",
"=",
"configuration",
")",
"total_rows",
"=",
"kwargs",
".",
"get",
"(",
"'rows'",
",",
"cls",
".",
"max_int",
")",
"start",
"=",
"kwargs",
".",
"get",
"(",
"'start'",
",",
"0",
")",
"all_datasets",
"=",
"None",
"attempts",
"=",
"0",
"while",
"attempts",
"<",
"cls",
".",
"max_attempts",
"and",
"all_datasets",
"is",
"None",
":",
"# if the count values vary for multiple calls, then must redo query",
"all_datasets",
"=",
"list",
"(",
")",
"counts",
"=",
"set",
"(",
")",
"for",
"page",
"in",
"range",
"(",
"total_rows",
"//",
"page_size",
"+",
"1",
")",
":",
"pagetimespagesize",
"=",
"page",
"*",
"page_size",
"kwargs",
"[",
"'start'",
"]",
"=",
"start",
"+",
"pagetimespagesize",
"rows_left",
"=",
"total_rows",
"-",
"pagetimespagesize",
"rows",
"=",
"min",
"(",
"rows_left",
",",
"page_size",
")",
"kwargs",
"[",
"'rows'",
"]",
"=",
"rows",
"_",
",",
"result",
"=",
"dataset",
".",
"_read_from_hdx",
"(",
"'dataset'",
",",
"query",
",",
"'q'",
",",
"Dataset",
".",
"actions",
"(",
")",
"[",
"'search'",
"]",
",",
"*",
"*",
"kwargs",
")",
"datasets",
"=",
"list",
"(",
")",
"if",
"result",
":",
"count",
"=",
"result",
".",
"get",
"(",
"'count'",
",",
"None",
")",
"if",
"count",
":",
"counts",
".",
"add",
"(",
"count",
")",
"no_results",
"=",
"len",
"(",
"result",
"[",
"'results'",
"]",
")",
"for",
"datasetdict",
"in",
"result",
"[",
"'results'",
"]",
":",
"dataset",
"=",
"Dataset",
"(",
"configuration",
"=",
"configuration",
")",
"dataset",
".",
"old_data",
"=",
"dict",
"(",
")",
"dataset",
".",
"data",
"=",
"datasetdict",
"dataset",
".",
"_dataset_create_resources",
"(",
")",
"datasets",
".",
"append",
"(",
"dataset",
")",
"all_datasets",
"+=",
"datasets",
"if",
"no_results",
"<",
"rows",
":",
"break",
"else",
":",
"break",
"else",
":",
"logger",
".",
"debug",
"(",
"result",
")",
"if",
"all_datasets",
"and",
"len",
"(",
"counts",
")",
"!=",
"1",
":",
"# Make sure counts are all same for multiple calls to HDX",
"all_datasets",
"=",
"None",
"attempts",
"+=",
"1",
"else",
":",
"ids",
"=",
"[",
"dataset",
"[",
"'id'",
"]",
"for",
"dataset",
"in",
"all_datasets",
"]",
"# check for duplicates (shouldn't happen)",
"if",
"len",
"(",
"ids",
")",
"!=",
"len",
"(",
"set",
"(",
"ids",
")",
")",
":",
"all_datasets",
"=",
"None",
"attempts",
"+=",
"1",
"if",
"attempts",
"==",
"cls",
".",
"max_attempts",
"and",
"all_datasets",
"is",
"None",
":",
"raise",
"HDXError",
"(",
"'Maximum attempts reached for searching for datasets!'",
")",
"return",
"all_datasets"
] |
Searches for datasets in HDX
Args:
query (Optional[str]): Query (in Solr format). Defaults to '*:*'.
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
**kwargs: See below
fq (string): Any filter queries to apply
sort (string): Sorting of the search results. Defaults to 'relevance asc, metadata_modified desc'.
rows (int): Number of matching rows to return. Defaults to all datasets (sys.maxsize).
start (int): Offset in the complete result for where the set of returned datasets should begin
facet (string): Whether to enable faceted results. Default to True.
facet.mincount (int): Minimum counts for facet fields should be included in the results
facet.limit (int): Maximum number of values the facet fields return (- = unlimited). Defaults to 50.
facet.field (List[str]): Fields to facet upon. Default is empty.
use_default_schema (bool): Use default package schema instead of custom schema. Defaults to False.
Returns:
List[Dataset]: list of datasets resulting from query
|
[
"Searches",
"for",
"datasets",
"in",
"HDX"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L598-L665
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_all_dataset_names
|
def get_all_dataset_names(configuration=None, **kwargs):
# type: (Optional[Configuration], Any) -> List[str]
"""Get all dataset names in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all dataset names.
offset (int): Offset in the complete result for where the set of returned dataset names should begin
Returns:
List[str]: list of all dataset names in HDX
"""
dataset = Dataset(configuration=configuration)
dataset['id'] = 'all dataset names' # only for error message if produced
return dataset._write_to_hdx('list', kwargs, 'id')
|
python
|
def get_all_dataset_names(configuration=None, **kwargs):
# type: (Optional[Configuration], Any) -> List[str]
"""Get all dataset names in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all dataset names.
offset (int): Offset in the complete result for where the set of returned dataset names should begin
Returns:
List[str]: list of all dataset names in HDX
"""
dataset = Dataset(configuration=configuration)
dataset['id'] = 'all dataset names' # only for error message if produced
return dataset._write_to_hdx('list', kwargs, 'id')
|
[
"def",
"get_all_dataset_names",
"(",
"configuration",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional[Configuration], Any) -> List[str]",
"dataset",
"=",
"Dataset",
"(",
"configuration",
"=",
"configuration",
")",
"dataset",
"[",
"'id'",
"]",
"=",
"'all dataset names'",
"# only for error message if produced",
"return",
"dataset",
".",
"_write_to_hdx",
"(",
"'list'",
",",
"kwargs",
",",
"'id'",
")"
] |
Get all dataset names in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all dataset names.
offset (int): Offset in the complete result for where the set of returned dataset names should begin
Returns:
List[str]: list of all dataset names in HDX
|
[
"Get",
"all",
"dataset",
"names",
"in",
"HDX"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L668-L683
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_all_datasets
|
def get_all_datasets(cls, configuration=None, page_size=1000, check_duplicates=True, **kwargs):
# type: (Optional[Configuration], int, bool, Any) -> List['Dataset']
"""Get all datasets in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
check_duplicates (bool): Whether to check for duplicate datasets. Defaults to True.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all datasets (sys.maxsize)
offset (int): Offset in the complete result for where the set of returned datasets should begin
Returns:
List[Dataset]: list of all datasets in HDX
"""
dataset = Dataset(configuration=configuration)
dataset['id'] = 'all datasets' # only for error message if produced
total_rows = kwargs.get('limit', cls.max_int)
start = kwargs.get('offset', 0)
all_datasets = None
attempts = 0
while attempts < cls.max_attempts and all_datasets is None: # if the dataset names vary for multiple calls, then must redo query
all_datasets = list()
for page in range(total_rows // page_size + 1):
pagetimespagesize = page * page_size
kwargs['offset'] = start + pagetimespagesize
rows_left = total_rows - pagetimespagesize
rows = min(rows_left, page_size)
kwargs['limit'] = rows
result = dataset._write_to_hdx('all', kwargs, 'id')
datasets = list()
if isinstance(result, list):
no_results = len(result)
if no_results == 0 and page == 0:
all_datasets = None
break
for datasetdict in result:
dataset = Dataset(configuration=configuration)
dataset.old_data = dict()
dataset.data = datasetdict
dataset._dataset_create_resources()
datasets.append(dataset)
all_datasets += datasets
if no_results < rows:
break
else:
logger.debug(result)
if all_datasets is None:
attempts += 1
elif check_duplicates:
names_list = [dataset['name'] for dataset in all_datasets]
names = set(names_list)
if len(names_list) != len(names): # check for duplicates (shouldn't happen)
all_datasets = None
attempts += 1
# This check is no longer valid because of showcases being returned by package_list!
# elif total_rows == max_int:
# all_names = set(Dataset.get_all_dataset_names()) # check dataset names match package_list
# if names != all_names:
# all_datasets = None
# attempts += 1
if attempts == cls.max_attempts and all_datasets is None:
raise HDXError('Maximum attempts reached for getting all datasets!')
return all_datasets
|
python
|
def get_all_datasets(cls, configuration=None, page_size=1000, check_duplicates=True, **kwargs):
# type: (Optional[Configuration], int, bool, Any) -> List['Dataset']
"""Get all datasets in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
check_duplicates (bool): Whether to check for duplicate datasets. Defaults to True.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all datasets (sys.maxsize)
offset (int): Offset in the complete result for where the set of returned datasets should begin
Returns:
List[Dataset]: list of all datasets in HDX
"""
dataset = Dataset(configuration=configuration)
dataset['id'] = 'all datasets' # only for error message if produced
total_rows = kwargs.get('limit', cls.max_int)
start = kwargs.get('offset', 0)
all_datasets = None
attempts = 0
while attempts < cls.max_attempts and all_datasets is None: # if the dataset names vary for multiple calls, then must redo query
all_datasets = list()
for page in range(total_rows // page_size + 1):
pagetimespagesize = page * page_size
kwargs['offset'] = start + pagetimespagesize
rows_left = total_rows - pagetimespagesize
rows = min(rows_left, page_size)
kwargs['limit'] = rows
result = dataset._write_to_hdx('all', kwargs, 'id')
datasets = list()
if isinstance(result, list):
no_results = len(result)
if no_results == 0 and page == 0:
all_datasets = None
break
for datasetdict in result:
dataset = Dataset(configuration=configuration)
dataset.old_data = dict()
dataset.data = datasetdict
dataset._dataset_create_resources()
datasets.append(dataset)
all_datasets += datasets
if no_results < rows:
break
else:
logger.debug(result)
if all_datasets is None:
attempts += 1
elif check_duplicates:
names_list = [dataset['name'] for dataset in all_datasets]
names = set(names_list)
if len(names_list) != len(names): # check for duplicates (shouldn't happen)
all_datasets = None
attempts += 1
# This check is no longer valid because of showcases being returned by package_list!
# elif total_rows == max_int:
# all_names = set(Dataset.get_all_dataset_names()) # check dataset names match package_list
# if names != all_names:
# all_datasets = None
# attempts += 1
if attempts == cls.max_attempts and all_datasets is None:
raise HDXError('Maximum attempts reached for getting all datasets!')
return all_datasets
|
[
"def",
"get_all_datasets",
"(",
"cls",
",",
"configuration",
"=",
"None",
",",
"page_size",
"=",
"1000",
",",
"check_duplicates",
"=",
"True",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional[Configuration], int, bool, Any) -> List['Dataset']",
"dataset",
"=",
"Dataset",
"(",
"configuration",
"=",
"configuration",
")",
"dataset",
"[",
"'id'",
"]",
"=",
"'all datasets'",
"# only for error message if produced",
"total_rows",
"=",
"kwargs",
".",
"get",
"(",
"'limit'",
",",
"cls",
".",
"max_int",
")",
"start",
"=",
"kwargs",
".",
"get",
"(",
"'offset'",
",",
"0",
")",
"all_datasets",
"=",
"None",
"attempts",
"=",
"0",
"while",
"attempts",
"<",
"cls",
".",
"max_attempts",
"and",
"all_datasets",
"is",
"None",
":",
"# if the dataset names vary for multiple calls, then must redo query",
"all_datasets",
"=",
"list",
"(",
")",
"for",
"page",
"in",
"range",
"(",
"total_rows",
"//",
"page_size",
"+",
"1",
")",
":",
"pagetimespagesize",
"=",
"page",
"*",
"page_size",
"kwargs",
"[",
"'offset'",
"]",
"=",
"start",
"+",
"pagetimespagesize",
"rows_left",
"=",
"total_rows",
"-",
"pagetimespagesize",
"rows",
"=",
"min",
"(",
"rows_left",
",",
"page_size",
")",
"kwargs",
"[",
"'limit'",
"]",
"=",
"rows",
"result",
"=",
"dataset",
".",
"_write_to_hdx",
"(",
"'all'",
",",
"kwargs",
",",
"'id'",
")",
"datasets",
"=",
"list",
"(",
")",
"if",
"isinstance",
"(",
"result",
",",
"list",
")",
":",
"no_results",
"=",
"len",
"(",
"result",
")",
"if",
"no_results",
"==",
"0",
"and",
"page",
"==",
"0",
":",
"all_datasets",
"=",
"None",
"break",
"for",
"datasetdict",
"in",
"result",
":",
"dataset",
"=",
"Dataset",
"(",
"configuration",
"=",
"configuration",
")",
"dataset",
".",
"old_data",
"=",
"dict",
"(",
")",
"dataset",
".",
"data",
"=",
"datasetdict",
"dataset",
".",
"_dataset_create_resources",
"(",
")",
"datasets",
".",
"append",
"(",
"dataset",
")",
"all_datasets",
"+=",
"datasets",
"if",
"no_results",
"<",
"rows",
":",
"break",
"else",
":",
"logger",
".",
"debug",
"(",
"result",
")",
"if",
"all_datasets",
"is",
"None",
":",
"attempts",
"+=",
"1",
"elif",
"check_duplicates",
":",
"names_list",
"=",
"[",
"dataset",
"[",
"'name'",
"]",
"for",
"dataset",
"in",
"all_datasets",
"]",
"names",
"=",
"set",
"(",
"names_list",
")",
"if",
"len",
"(",
"names_list",
")",
"!=",
"len",
"(",
"names",
")",
":",
"# check for duplicates (shouldn't happen)",
"all_datasets",
"=",
"None",
"attempts",
"+=",
"1",
"# This check is no longer valid because of showcases being returned by package_list!",
"# elif total_rows == max_int:",
"# all_names = set(Dataset.get_all_dataset_names()) # check dataset names match package_list",
"# if names != all_names:",
"# all_datasets = None",
"# attempts += 1",
"if",
"attempts",
"==",
"cls",
".",
"max_attempts",
"and",
"all_datasets",
"is",
"None",
":",
"raise",
"HDXError",
"(",
"'Maximum attempts reached for getting all datasets!'",
")",
"return",
"all_datasets"
] |
Get all datasets in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
page_size (int): Size of page to return. Defaults to 1000.
check_duplicates (bool): Whether to check for duplicate datasets. Defaults to True.
**kwargs: See below
limit (int): Number of rows to return. Defaults to all datasets (sys.maxsize)
offset (int): Offset in the complete result for where the set of returned datasets should begin
Returns:
List[Dataset]: list of all datasets in HDX
|
[
"Get",
"all",
"datasets",
"in",
"HDX"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L686-L750
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_all_resources
|
def get_all_resources(datasets):
# type: (List['Dataset']) -> List[hdx.data.resource.Resource]
"""Get all resources from a list of datasets (such as returned by search)
Args:
datasets (List[Dataset]): list of datasets
Returns:
List[hdx.data.resource.Resource]: list of resources within those datasets
"""
resources = []
for dataset in datasets:
for resource in dataset.get_resources():
resources.append(resource)
return resources
|
python
|
def get_all_resources(datasets):
# type: (List['Dataset']) -> List[hdx.data.resource.Resource]
"""Get all resources from a list of datasets (such as returned by search)
Args:
datasets (List[Dataset]): list of datasets
Returns:
List[hdx.data.resource.Resource]: list of resources within those datasets
"""
resources = []
for dataset in datasets:
for resource in dataset.get_resources():
resources.append(resource)
return resources
|
[
"def",
"get_all_resources",
"(",
"datasets",
")",
":",
"# type: (List['Dataset']) -> List[hdx.data.resource.Resource]",
"resources",
"=",
"[",
"]",
"for",
"dataset",
"in",
"datasets",
":",
"for",
"resource",
"in",
"dataset",
".",
"get_resources",
"(",
")",
":",
"resources",
".",
"append",
"(",
"resource",
")",
"return",
"resources"
] |
Get all resources from a list of datasets (such as returned by search)
Args:
datasets (List[Dataset]): list of datasets
Returns:
List[hdx.data.resource.Resource]: list of resources within those datasets
|
[
"Get",
"all",
"resources",
"from",
"a",
"list",
"of",
"datasets",
"(",
"such",
"as",
"returned",
"by",
"search",
")"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L753-L767
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_dataset_date_as_datetime
|
def get_dataset_date_as_datetime(self):
# type: () -> Optional[datetime]
"""Get dataset date as datetime.datetime object. For range returns start date.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
"""
dataset_date = self.data.get('dataset_date', None)
if dataset_date:
if '-' in dataset_date:
dataset_date = dataset_date.split('-')[0]
return datetime.strptime(dataset_date, '%m/%d/%Y')
else:
return None
|
python
|
def get_dataset_date_as_datetime(self):
# type: () -> Optional[datetime]
"""Get dataset date as datetime.datetime object. For range returns start date.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
"""
dataset_date = self.data.get('dataset_date', None)
if dataset_date:
if '-' in dataset_date:
dataset_date = dataset_date.split('-')[0]
return datetime.strptime(dataset_date, '%m/%d/%Y')
else:
return None
|
[
"def",
"get_dataset_date_as_datetime",
"(",
"self",
")",
":",
"# type: () -> Optional[datetime]",
"dataset_date",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'dataset_date'",
",",
"None",
")",
"if",
"dataset_date",
":",
"if",
"'-'",
"in",
"dataset_date",
":",
"dataset_date",
"=",
"dataset_date",
".",
"split",
"(",
"'-'",
")",
"[",
"0",
"]",
"return",
"datetime",
".",
"strptime",
"(",
"dataset_date",
",",
"'%m/%d/%Y'",
")",
"else",
":",
"return",
"None"
] |
Get dataset date as datetime.datetime object. For range returns start date.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
|
[
"Get",
"dataset",
"date",
"as",
"datetime",
".",
"datetime",
"object",
".",
"For",
"range",
"returns",
"start",
"date",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L785-L798
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_dataset_end_date_as_datetime
|
def get_dataset_end_date_as_datetime(self):
# type: () -> Optional[datetime]
"""Get dataset end date as datetime.datetime object.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
"""
dataset_date = self.data.get('dataset_date', None)
if dataset_date:
if '-' in dataset_date:
dataset_date = dataset_date.split('-')[1]
return datetime.strptime(dataset_date, '%m/%d/%Y')
return None
|
python
|
def get_dataset_end_date_as_datetime(self):
# type: () -> Optional[datetime]
"""Get dataset end date as datetime.datetime object.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
"""
dataset_date = self.data.get('dataset_date', None)
if dataset_date:
if '-' in dataset_date:
dataset_date = dataset_date.split('-')[1]
return datetime.strptime(dataset_date, '%m/%d/%Y')
return None
|
[
"def",
"get_dataset_end_date_as_datetime",
"(",
"self",
")",
":",
"# type: () -> Optional[datetime]",
"dataset_date",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'dataset_date'",
",",
"None",
")",
"if",
"dataset_date",
":",
"if",
"'-'",
"in",
"dataset_date",
":",
"dataset_date",
"=",
"dataset_date",
".",
"split",
"(",
"'-'",
")",
"[",
"1",
"]",
"return",
"datetime",
".",
"strptime",
"(",
"dataset_date",
",",
"'%m/%d/%Y'",
")",
"return",
"None"
] |
Get dataset end date as datetime.datetime object.
Returns:
Optional[datetime.datetime]: Dataset date in datetime object or None if no date is set
|
[
"Get",
"dataset",
"end",
"date",
"as",
"datetime",
".",
"datetime",
"object",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L800-L812
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset._get_formatted_date
|
def _get_formatted_date(dataset_date, date_format=None):
# type: (Optional[datetime], Optional[str]) -> Optional[str]
"""Get supplied dataset date as string in specified format.
If no format is supplied, an ISO 8601 string is returned.
Args:
dataset_date (Optional[datetime.datetime]): dataset date in datetime.datetime format
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
if dataset_date:
if date_format:
return dataset_date.strftime(date_format)
else:
return dataset_date.date().isoformat()
else:
return None
|
python
|
def _get_formatted_date(dataset_date, date_format=None):
# type: (Optional[datetime], Optional[str]) -> Optional[str]
"""Get supplied dataset date as string in specified format.
If no format is supplied, an ISO 8601 string is returned.
Args:
dataset_date (Optional[datetime.datetime]): dataset date in datetime.datetime format
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
if dataset_date:
if date_format:
return dataset_date.strftime(date_format)
else:
return dataset_date.date().isoformat()
else:
return None
|
[
"def",
"_get_formatted_date",
"(",
"dataset_date",
",",
"date_format",
"=",
"None",
")",
":",
"# type: (Optional[datetime], Optional[str]) -> Optional[str]",
"if",
"dataset_date",
":",
"if",
"date_format",
":",
"return",
"dataset_date",
".",
"strftime",
"(",
"date_format",
")",
"else",
":",
"return",
"dataset_date",
".",
"date",
"(",
")",
".",
"isoformat",
"(",
")",
"else",
":",
"return",
"None"
] |
Get supplied dataset date as string in specified format.
If no format is supplied, an ISO 8601 string is returned.
Args:
dataset_date (Optional[datetime.datetime]): dataset date in datetime.datetime format
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
|
[
"Get",
"supplied",
"dataset",
"date",
"as",
"string",
"in",
"specified",
"format",
".",
"If",
"no",
"format",
"is",
"supplied",
"an",
"ISO",
"8601",
"string",
"is",
"returned",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L815-L833
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_dataset_date
|
def get_dataset_date(self, date_format=None):
# type: (Optional[str]) -> Optional[str]
"""Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
dataset_date = self.get_dataset_date_as_datetime()
return self._get_formatted_date(dataset_date, date_format)
|
python
|
def get_dataset_date(self, date_format=None):
# type: (Optional[str]) -> Optional[str]
"""Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
dataset_date = self.get_dataset_date_as_datetime()
return self._get_formatted_date(dataset_date, date_format)
|
[
"def",
"get_dataset_date",
"(",
"self",
",",
"date_format",
"=",
"None",
")",
":",
"# type: (Optional[str]) -> Optional[str]",
"dataset_date",
"=",
"self",
".",
"get_dataset_date_as_datetime",
"(",
")",
"return",
"self",
".",
"_get_formatted_date",
"(",
"dataset_date",
",",
"date_format",
")"
] |
Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
|
[
"Get",
"dataset",
"date",
"as",
"string",
"in",
"specified",
"format",
".",
"For",
"range",
"returns",
"start",
"date",
".",
"If",
"no",
"format",
"is",
"supplied",
"an",
"ISO",
"8601",
"string",
"is",
"returned",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L835-L847
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_dataset_end_date
|
def get_dataset_end_date(self, date_format=None):
# type: (Optional[str]) -> Optional[str]
"""Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
dataset_date = self.get_dataset_end_date_as_datetime()
return self._get_formatted_date(dataset_date, date_format)
|
python
|
def get_dataset_end_date(self, date_format=None):
# type: (Optional[str]) -> Optional[str]
"""Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
"""
dataset_date = self.get_dataset_end_date_as_datetime()
return self._get_formatted_date(dataset_date, date_format)
|
[
"def",
"get_dataset_end_date",
"(",
"self",
",",
"date_format",
"=",
"None",
")",
":",
"# type: (Optional[str]) -> Optional[str]",
"dataset_date",
"=",
"self",
".",
"get_dataset_end_date_as_datetime",
"(",
")",
"return",
"self",
".",
"_get_formatted_date",
"(",
"dataset_date",
",",
"date_format",
")"
] |
Get dataset date as string in specified format. For range returns start date.
If no format is supplied, an ISO 8601 string is returned.
Args:
date_format (Optional[str]): Date format. None is taken to be ISO 8601. Defaults to None.
Returns:
Optional[str]: Dataset date string or None if no date is set
|
[
"Get",
"dataset",
"date",
"as",
"string",
"in",
"specified",
"format",
".",
"For",
"range",
"returns",
"start",
"date",
".",
"If",
"no",
"format",
"is",
"supplied",
"an",
"ISO",
"8601",
"string",
"is",
"returned",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L849-L861
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_dataset_date_from_datetime
|
def set_dataset_date_from_datetime(self, dataset_date, dataset_end_date=None):
# type: (datetime, Optional[datetime]) -> None
"""Set dataset date from datetime.datetime object
Args:
dataset_date (datetime.datetime): Dataset date
dataset_end_date (Optional[datetime.datetime]): Dataset end date
Returns:
None
"""
start_date = dataset_date.strftime('%m/%d/%Y')
if dataset_end_date is None:
self.data['dataset_date'] = start_date
else:
end_date = dataset_end_date.strftime('%m/%d/%Y')
self.data['dataset_date'] = '%s-%s' % (start_date, end_date)
|
python
|
def set_dataset_date_from_datetime(self, dataset_date, dataset_end_date=None):
# type: (datetime, Optional[datetime]) -> None
"""Set dataset date from datetime.datetime object
Args:
dataset_date (datetime.datetime): Dataset date
dataset_end_date (Optional[datetime.datetime]): Dataset end date
Returns:
None
"""
start_date = dataset_date.strftime('%m/%d/%Y')
if dataset_end_date is None:
self.data['dataset_date'] = start_date
else:
end_date = dataset_end_date.strftime('%m/%d/%Y')
self.data['dataset_date'] = '%s-%s' % (start_date, end_date)
|
[
"def",
"set_dataset_date_from_datetime",
"(",
"self",
",",
"dataset_date",
",",
"dataset_end_date",
"=",
"None",
")",
":",
"# type: (datetime, Optional[datetime]) -> None",
"start_date",
"=",
"dataset_date",
".",
"strftime",
"(",
"'%m/%d/%Y'",
")",
"if",
"dataset_end_date",
"is",
"None",
":",
"self",
".",
"data",
"[",
"'dataset_date'",
"]",
"=",
"start_date",
"else",
":",
"end_date",
"=",
"dataset_end_date",
".",
"strftime",
"(",
"'%m/%d/%Y'",
")",
"self",
".",
"data",
"[",
"'dataset_date'",
"]",
"=",
"'%s-%s'",
"%",
"(",
"start_date",
",",
"end_date",
")"
] |
Set dataset date from datetime.datetime object
Args:
dataset_date (datetime.datetime): Dataset date
dataset_end_date (Optional[datetime.datetime]): Dataset end date
Returns:
None
|
[
"Set",
"dataset",
"date",
"from",
"datetime",
".",
"datetime",
"object"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L863-L879
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset._parse_date
|
def _parse_date(dataset_date, date_format):
# type: (str, Optional[str]) -> datetime
"""Parse dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
datetime.datetime
"""
if date_format is None:
try:
return parser.parse(dataset_date)
except (ValueError, OverflowError) as e:
raisefrom(HDXError, 'Invalid dataset date!', e)
else:
try:
return datetime.strptime(dataset_date, date_format)
except ValueError as e:
raisefrom(HDXError, 'Invalid dataset date!', e)
|
python
|
def _parse_date(dataset_date, date_format):
# type: (str, Optional[str]) -> datetime
"""Parse dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
datetime.datetime
"""
if date_format is None:
try:
return parser.parse(dataset_date)
except (ValueError, OverflowError) as e:
raisefrom(HDXError, 'Invalid dataset date!', e)
else:
try:
return datetime.strptime(dataset_date, date_format)
except ValueError as e:
raisefrom(HDXError, 'Invalid dataset date!', e)
|
[
"def",
"_parse_date",
"(",
"dataset_date",
",",
"date_format",
")",
":",
"# type: (str, Optional[str]) -> datetime",
"if",
"date_format",
"is",
"None",
":",
"try",
":",
"return",
"parser",
".",
"parse",
"(",
"dataset_date",
")",
"except",
"(",
"ValueError",
",",
"OverflowError",
")",
"as",
"e",
":",
"raisefrom",
"(",
"HDXError",
",",
"'Invalid dataset date!'",
",",
"e",
")",
"else",
":",
"try",
":",
"return",
"datetime",
".",
"strptime",
"(",
"dataset_date",
",",
"date_format",
")",
"except",
"ValueError",
"as",
"e",
":",
"raisefrom",
"(",
"HDXError",
",",
"'Invalid dataset date!'",
",",
"e",
")"
] |
Parse dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
datetime.datetime
|
[
"Parse",
"dataset",
"date",
"from",
"string",
"using",
"specified",
"format",
".",
"If",
"no",
"format",
"is",
"supplied",
"the",
"function",
"will",
"guess",
".",
"For",
"unambiguous",
"formats",
"this",
"should",
"be",
"fine",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L882-L903
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_dataset_date
|
def set_dataset_date(self, dataset_date, dataset_end_date=None, date_format=None):
# type: (str, Optional[str], Optional[str]) -> None
"""Set dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
dataset_end_date (Optional[str]): Dataset end date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
None
"""
parsed_date = self._parse_date(dataset_date, date_format)
if dataset_end_date is None:
self.set_dataset_date_from_datetime(parsed_date)
else:
parsed_end_date = self._parse_date(dataset_end_date, date_format)
self.set_dataset_date_from_datetime(parsed_date, parsed_end_date)
|
python
|
def set_dataset_date(self, dataset_date, dataset_end_date=None, date_format=None):
# type: (str, Optional[str], Optional[str]) -> None
"""Set dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
dataset_end_date (Optional[str]): Dataset end date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
None
"""
parsed_date = self._parse_date(dataset_date, date_format)
if dataset_end_date is None:
self.set_dataset_date_from_datetime(parsed_date)
else:
parsed_end_date = self._parse_date(dataset_end_date, date_format)
self.set_dataset_date_from_datetime(parsed_date, parsed_end_date)
|
[
"def",
"set_dataset_date",
"(",
"self",
",",
"dataset_date",
",",
"dataset_end_date",
"=",
"None",
",",
"date_format",
"=",
"None",
")",
":",
"# type: (str, Optional[str], Optional[str]) -> None",
"parsed_date",
"=",
"self",
".",
"_parse_date",
"(",
"dataset_date",
",",
"date_format",
")",
"if",
"dataset_end_date",
"is",
"None",
":",
"self",
".",
"set_dataset_date_from_datetime",
"(",
"parsed_date",
")",
"else",
":",
"parsed_end_date",
"=",
"self",
".",
"_parse_date",
"(",
"dataset_end_date",
",",
"date_format",
")",
"self",
".",
"set_dataset_date_from_datetime",
"(",
"parsed_date",
",",
"parsed_end_date",
")"
] |
Set dataset date from string using specified format. If no format is supplied, the function will guess.
For unambiguous formats, this should be fine.
Args:
dataset_date (str): Dataset date string
dataset_end_date (Optional[str]): Dataset end date string
date_format (Optional[str]): Date format. If None is given, will attempt to guess. Defaults to None.
Returns:
None
|
[
"Set",
"dataset",
"date",
"from",
"string",
"using",
"specified",
"format",
".",
"If",
"no",
"format",
"is",
"supplied",
"the",
"function",
"will",
"guess",
".",
"For",
"unambiguous",
"formats",
"this",
"should",
"be",
"fine",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L905-L923
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_dataset_year_range
|
def set_dataset_year_range(self, dataset_year, dataset_end_year=None):
# type: (Union[str, int], Optional[Union[str, int]]) -> None
"""Set dataset date as a range from year or start and end year.
Args:
dataset_year (Union[str, int]): Dataset year given as string or int
dataset_end_year (Optional[Union[str, int]]): Dataset end year given as string or int
Returns:
None
"""
if isinstance(dataset_year, int):
dataset_date = '01/01/%d' % dataset_year
elif isinstance(dataset_year, str):
dataset_date = '01/01/%s' % dataset_year
else:
raise hdx.data.hdxobject.HDXError('dataset_year has type %s which is not supported!' % type(dataset_year).__name__)
if dataset_end_year is None:
dataset_end_year = dataset_year
if isinstance(dataset_end_year, int):
dataset_end_date = '31/12/%d' % dataset_end_year
elif isinstance(dataset_end_year, str):
dataset_end_date = '31/12/%s' % dataset_end_year
else:
raise hdx.data.hdxobject.HDXError('dataset_end_year has type %s which is not supported!' % type(dataset_end_year).__name__)
self.set_dataset_date(dataset_date, dataset_end_date)
|
python
|
def set_dataset_year_range(self, dataset_year, dataset_end_year=None):
# type: (Union[str, int], Optional[Union[str, int]]) -> None
"""Set dataset date as a range from year or start and end year.
Args:
dataset_year (Union[str, int]): Dataset year given as string or int
dataset_end_year (Optional[Union[str, int]]): Dataset end year given as string or int
Returns:
None
"""
if isinstance(dataset_year, int):
dataset_date = '01/01/%d' % dataset_year
elif isinstance(dataset_year, str):
dataset_date = '01/01/%s' % dataset_year
else:
raise hdx.data.hdxobject.HDXError('dataset_year has type %s which is not supported!' % type(dataset_year).__name__)
if dataset_end_year is None:
dataset_end_year = dataset_year
if isinstance(dataset_end_year, int):
dataset_end_date = '31/12/%d' % dataset_end_year
elif isinstance(dataset_end_year, str):
dataset_end_date = '31/12/%s' % dataset_end_year
else:
raise hdx.data.hdxobject.HDXError('dataset_end_year has type %s which is not supported!' % type(dataset_end_year).__name__)
self.set_dataset_date(dataset_date, dataset_end_date)
|
[
"def",
"set_dataset_year_range",
"(",
"self",
",",
"dataset_year",
",",
"dataset_end_year",
"=",
"None",
")",
":",
"# type: (Union[str, int], Optional[Union[str, int]]) -> None",
"if",
"isinstance",
"(",
"dataset_year",
",",
"int",
")",
":",
"dataset_date",
"=",
"'01/01/%d'",
"%",
"dataset_year",
"elif",
"isinstance",
"(",
"dataset_year",
",",
"str",
")",
":",
"dataset_date",
"=",
"'01/01/%s'",
"%",
"dataset_year",
"else",
":",
"raise",
"hdx",
".",
"data",
".",
"hdxobject",
".",
"HDXError",
"(",
"'dataset_year has type %s which is not supported!'",
"%",
"type",
"(",
"dataset_year",
")",
".",
"__name__",
")",
"if",
"dataset_end_year",
"is",
"None",
":",
"dataset_end_year",
"=",
"dataset_year",
"if",
"isinstance",
"(",
"dataset_end_year",
",",
"int",
")",
":",
"dataset_end_date",
"=",
"'31/12/%d'",
"%",
"dataset_end_year",
"elif",
"isinstance",
"(",
"dataset_end_year",
",",
"str",
")",
":",
"dataset_end_date",
"=",
"'31/12/%s'",
"%",
"dataset_end_year",
"else",
":",
"raise",
"hdx",
".",
"data",
".",
"hdxobject",
".",
"HDXError",
"(",
"'dataset_end_year has type %s which is not supported!'",
"%",
"type",
"(",
"dataset_end_year",
")",
".",
"__name__",
")",
"self",
".",
"set_dataset_date",
"(",
"dataset_date",
",",
"dataset_end_date",
")"
] |
Set dataset date as a range from year or start and end year.
Args:
dataset_year (Union[str, int]): Dataset year given as string or int
dataset_end_year (Optional[Union[str, int]]): Dataset end year given as string or int
Returns:
None
|
[
"Set",
"dataset",
"date",
"as",
"a",
"range",
"from",
"year",
"or",
"start",
"and",
"end",
"year",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L925-L950
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_expected_update_frequency
|
def get_expected_update_frequency(self):
# type: () -> Optional[str]
"""Get expected update frequency (in textual rather than numeric form)
Returns:
Optional[str]: Update frequency in textual form or None if the update frequency doesn't exist or is blank.
"""
days = self.data.get('data_update_frequency', None)
if days:
return Dataset.transform_update_frequency(days)
else:
return None
|
python
|
def get_expected_update_frequency(self):
# type: () -> Optional[str]
"""Get expected update frequency (in textual rather than numeric form)
Returns:
Optional[str]: Update frequency in textual form or None if the update frequency doesn't exist or is blank.
"""
days = self.data.get('data_update_frequency', None)
if days:
return Dataset.transform_update_frequency(days)
else:
return None
|
[
"def",
"get_expected_update_frequency",
"(",
"self",
")",
":",
"# type: () -> Optional[str]",
"days",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'data_update_frequency'",
",",
"None",
")",
"if",
"days",
":",
"return",
"Dataset",
".",
"transform_update_frequency",
"(",
"days",
")",
"else",
":",
"return",
"None"
] |
Get expected update frequency (in textual rather than numeric form)
Returns:
Optional[str]: Update frequency in textual form or None if the update frequency doesn't exist or is blank.
|
[
"Get",
"expected",
"update",
"frequency",
"(",
"in",
"textual",
"rather",
"than",
"numeric",
"form",
")"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L966-L977
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_expected_update_frequency
|
def set_expected_update_frequency(self, update_frequency):
# type: (str) -> None
"""Set expected update frequency
Args:
update_frequency (str): Update frequency
Returns:
None
"""
try:
int(update_frequency)
except ValueError:
update_frequency = Dataset.transform_update_frequency(update_frequency)
if not update_frequency:
raise HDXError('Invalid update frequency supplied!')
self.data['data_update_frequency'] = update_frequency
|
python
|
def set_expected_update_frequency(self, update_frequency):
# type: (str) -> None
"""Set expected update frequency
Args:
update_frequency (str): Update frequency
Returns:
None
"""
try:
int(update_frequency)
except ValueError:
update_frequency = Dataset.transform_update_frequency(update_frequency)
if not update_frequency:
raise HDXError('Invalid update frequency supplied!')
self.data['data_update_frequency'] = update_frequency
|
[
"def",
"set_expected_update_frequency",
"(",
"self",
",",
"update_frequency",
")",
":",
"# type: (str) -> None",
"try",
":",
"int",
"(",
"update_frequency",
")",
"except",
"ValueError",
":",
"update_frequency",
"=",
"Dataset",
".",
"transform_update_frequency",
"(",
"update_frequency",
")",
"if",
"not",
"update_frequency",
":",
"raise",
"HDXError",
"(",
"'Invalid update frequency supplied!'",
")",
"self",
".",
"data",
"[",
"'data_update_frequency'",
"]",
"=",
"update_frequency"
] |
Set expected update frequency
Args:
update_frequency (str): Update frequency
Returns:
None
|
[
"Set",
"expected",
"update",
"frequency"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L979-L995
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.remove_tag
|
def remove_tag(self, tag):
# type: (str) -> bool
"""Remove a tag
Args:
tag (str): Tag to remove
Returns:
bool: True if tag removed or False if not
"""
return self._remove_hdxobject(self.data.get('tags'), tag, matchon='name')
|
python
|
def remove_tag(self, tag):
# type: (str) -> bool
"""Remove a tag
Args:
tag (str): Tag to remove
Returns:
bool: True if tag removed or False if not
"""
return self._remove_hdxobject(self.data.get('tags'), tag, matchon='name')
|
[
"def",
"remove_tag",
"(",
"self",
",",
"tag",
")",
":",
"# type: (str) -> bool",
"return",
"self",
".",
"_remove_hdxobject",
"(",
"self",
".",
"data",
".",
"get",
"(",
"'tags'",
")",
",",
"tag",
",",
"matchon",
"=",
"'name'",
")"
] |
Remove a tag
Args:
tag (str): Tag to remove
Returns:
bool: True if tag removed or False if not
|
[
"Remove",
"a",
"tag"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1030-L1040
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_location
|
def get_location(self, locations=None):
# type: (Optional[List[str]]) -> List[str]
"""Return the dataset's location
Args:
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
List[str]: list of locations or [] if there are none
"""
countries = self.data.get('groups', None)
if not countries:
return list()
return [Locations.get_location_from_HDX_code(x['name'], locations=locations,
configuration=self.configuration) for x in countries]
|
python
|
def get_location(self, locations=None):
# type: (Optional[List[str]]) -> List[str]
"""Return the dataset's location
Args:
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
List[str]: list of locations or [] if there are none
"""
countries = self.data.get('groups', None)
if not countries:
return list()
return [Locations.get_location_from_HDX_code(x['name'], locations=locations,
configuration=self.configuration) for x in countries]
|
[
"def",
"get_location",
"(",
"self",
",",
"locations",
"=",
"None",
")",
":",
"# type: (Optional[List[str]]) -> List[str]",
"countries",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'groups'",
",",
"None",
")",
"if",
"not",
"countries",
":",
"return",
"list",
"(",
")",
"return",
"[",
"Locations",
".",
"get_location_from_HDX_code",
"(",
"x",
"[",
"'name'",
"]",
",",
"locations",
"=",
"locations",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"for",
"x",
"in",
"countries",
"]"
] |
Return the dataset's location
Args:
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
List[str]: list of locations or [] if there are none
|
[
"Return",
"the",
"dataset",
"s",
"location"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1066-L1080
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_country_location
|
def add_country_location(self, country, exact=True, locations=None, use_live=True):
# type: (str, bool, Optional[List[str]], bool) -> bool
"""Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
converted to an iso 3 code. If the country is already added, it is ignored.
Args:
country (str): Country to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if country added or False if country already present
"""
iso3, match = Country.get_iso3_country_code_fuzzy(country, use_live=use_live)
if iso3 is None:
raise HDXError('Country: %s - cannot find iso3 code!' % country)
return self.add_other_location(iso3, exact=exact,
alterror='Country: %s with iso3: %s could not be found in HDX list!' %
(country, iso3),
locations=locations)
|
python
|
def add_country_location(self, country, exact=True, locations=None, use_live=True):
# type: (str, bool, Optional[List[str]], bool) -> bool
"""Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
converted to an iso 3 code. If the country is already added, it is ignored.
Args:
country (str): Country to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if country added or False if country already present
"""
iso3, match = Country.get_iso3_country_code_fuzzy(country, use_live=use_live)
if iso3 is None:
raise HDXError('Country: %s - cannot find iso3 code!' % country)
return self.add_other_location(iso3, exact=exact,
alterror='Country: %s with iso3: %s could not be found in HDX list!' %
(country, iso3),
locations=locations)
|
[
"def",
"add_country_location",
"(",
"self",
",",
"country",
",",
"exact",
"=",
"True",
",",
"locations",
"=",
"None",
",",
"use_live",
"=",
"True",
")",
":",
"# type: (str, bool, Optional[List[str]], bool) -> bool",
"iso3",
",",
"match",
"=",
"Country",
".",
"get_iso3_country_code_fuzzy",
"(",
"country",
",",
"use_live",
"=",
"use_live",
")",
"if",
"iso3",
"is",
"None",
":",
"raise",
"HDXError",
"(",
"'Country: %s - cannot find iso3 code!'",
"%",
"country",
")",
"return",
"self",
".",
"add_other_location",
"(",
"iso3",
",",
"exact",
"=",
"exact",
",",
"alterror",
"=",
"'Country: %s with iso3: %s could not be found in HDX list!'",
"%",
"(",
"country",
",",
"iso3",
")",
",",
"locations",
"=",
"locations",
")"
] |
Add a country. If an iso 3 code is not provided, value is parsed and if it is a valid country name,
converted to an iso 3 code. If the country is already added, it is ignored.
Args:
country (str): Country to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if country added or False if country already present
|
[
"Add",
"a",
"country",
".",
"If",
"an",
"iso",
"3",
"code",
"is",
"not",
"provided",
"value",
"is",
"parsed",
"and",
"if",
"it",
"is",
"a",
"valid",
"country",
"name",
"converted",
"to",
"an",
"iso",
"3",
"code",
".",
"If",
"the",
"country",
"is",
"already",
"added",
"it",
"is",
"ignored",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1082-L1102
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_country_locations
|
def add_country_locations(self, countries, locations=None, use_live=True):
# type: (List[str], Optional[List[str]], bool) -> bool
"""Add a list of countries. If iso 3 codes are not provided, values are parsed and where they are valid country
names, converted to iso 3 codes. If any country is already added, it is ignored.
Args:
countries (List[str]): list of countries to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries added or False if any already present.
"""
allcountriesadded = True
for country in countries:
if not self.add_country_location(country, locations=locations, use_live=use_live):
allcountriesadded = False
return allcountriesadded
|
python
|
def add_country_locations(self, countries, locations=None, use_live=True):
# type: (List[str], Optional[List[str]], bool) -> bool
"""Add a list of countries. If iso 3 codes are not provided, values are parsed and where they are valid country
names, converted to iso 3 codes. If any country is already added, it is ignored.
Args:
countries (List[str]): list of countries to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries added or False if any already present.
"""
allcountriesadded = True
for country in countries:
if not self.add_country_location(country, locations=locations, use_live=use_live):
allcountriesadded = False
return allcountriesadded
|
[
"def",
"add_country_locations",
"(",
"self",
",",
"countries",
",",
"locations",
"=",
"None",
",",
"use_live",
"=",
"True",
")",
":",
"# type: (List[str], Optional[List[str]], bool) -> bool",
"allcountriesadded",
"=",
"True",
"for",
"country",
"in",
"countries",
":",
"if",
"not",
"self",
".",
"add_country_location",
"(",
"country",
",",
"locations",
"=",
"locations",
",",
"use_live",
"=",
"use_live",
")",
":",
"allcountriesadded",
"=",
"False",
"return",
"allcountriesadded"
] |
Add a list of countries. If iso 3 codes are not provided, values are parsed and where they are valid country
names, converted to iso 3 codes. If any country is already added, it is ignored.
Args:
countries (List[str]): list of countries to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries added or False if any already present.
|
[
"Add",
"a",
"list",
"of",
"countries",
".",
"If",
"iso",
"3",
"codes",
"are",
"not",
"provided",
"values",
"are",
"parsed",
"and",
"where",
"they",
"are",
"valid",
"country",
"names",
"converted",
"to",
"iso",
"3",
"codes",
".",
"If",
"any",
"country",
"is",
"already",
"added",
"it",
"is",
"ignored",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1104-L1121
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_region_location
|
def add_region_location(self, region, locations=None, use_live=True):
# type: (str, Optional[List[str]], bool) -> bool
"""Add all countries in a region. If a 3 digit UNStats M49 region code is not provided, value is parsed as a
region name. If any country is already added, it is ignored.
Args:
region (str): M49 region, intermediate region or subregion to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries in region added or False if any already present.
"""
return self.add_country_locations(Country.get_countries_in_region(region, exception=HDXError,
use_live=use_live), locations=locations)
|
python
|
def add_region_location(self, region, locations=None, use_live=True):
# type: (str, Optional[List[str]], bool) -> bool
"""Add all countries in a region. If a 3 digit UNStats M49 region code is not provided, value is parsed as a
region name. If any country is already added, it is ignored.
Args:
region (str): M49 region, intermediate region or subregion to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries in region added or False if any already present.
"""
return self.add_country_locations(Country.get_countries_in_region(region, exception=HDXError,
use_live=use_live), locations=locations)
|
[
"def",
"add_region_location",
"(",
"self",
",",
"region",
",",
"locations",
"=",
"None",
",",
"use_live",
"=",
"True",
")",
":",
"# type: (str, Optional[List[str]], bool) -> bool",
"return",
"self",
".",
"add_country_locations",
"(",
"Country",
".",
"get_countries_in_region",
"(",
"region",
",",
"exception",
"=",
"HDXError",
",",
"use_live",
"=",
"use_live",
")",
",",
"locations",
"=",
"locations",
")"
] |
Add all countries in a region. If a 3 digit UNStats M49 region code is not provided, value is parsed as a
region name. If any country is already added, it is ignored.
Args:
region (str): M49 region, intermediate region or subregion to add
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
use_live (bool): Try to get use latest country data from web rather than file in package. Defaults to True.
Returns:
bool: True if all countries in region added or False if any already present.
|
[
"Add",
"all",
"countries",
"in",
"a",
"region",
".",
"If",
"a",
"3",
"digit",
"UNStats",
"M49",
"region",
"code",
"is",
"not",
"provided",
"value",
"is",
"parsed",
"as",
"a",
"region",
"name",
".",
"If",
"any",
"country",
"is",
"already",
"added",
"it",
"is",
"ignored",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1123-L1137
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_other_location
|
def add_other_location(self, location, exact=True, alterror=None, locations=None):
# type: (str, bool, Optional[str], Optional[List[str]]) -> bool
"""Add a location which is not a country or region. Value is parsed and compared to existing locations in
HDX. If the location is already added, it is ignored.
Args:
location (str): Location to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
alterror (Optional[str]): Alternative error message to builtin if location not found. Defaults to None.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
bool: True if location added or False if location already present
"""
hdx_code, match = Locations.get_HDX_code_from_location_partial(location, locations=locations,
configuration=self.configuration)
if hdx_code is None or (exact is True and match is False):
if alterror is None:
raise HDXError('Location: %s - cannot find in HDX!' % location)
else:
raise HDXError(alterror)
groups = self.data.get('groups', None)
hdx_code = hdx_code.lower()
if groups:
if hdx_code in [x['name'] for x in groups]:
return False
else:
groups = list()
groups.append({'name': hdx_code})
self.data['groups'] = groups
return True
|
python
|
def add_other_location(self, location, exact=True, alterror=None, locations=None):
# type: (str, bool, Optional[str], Optional[List[str]]) -> bool
"""Add a location which is not a country or region. Value is parsed and compared to existing locations in
HDX. If the location is already added, it is ignored.
Args:
location (str): Location to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
alterror (Optional[str]): Alternative error message to builtin if location not found. Defaults to None.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
bool: True if location added or False if location already present
"""
hdx_code, match = Locations.get_HDX_code_from_location_partial(location, locations=locations,
configuration=self.configuration)
if hdx_code is None or (exact is True and match is False):
if alterror is None:
raise HDXError('Location: %s - cannot find in HDX!' % location)
else:
raise HDXError(alterror)
groups = self.data.get('groups', None)
hdx_code = hdx_code.lower()
if groups:
if hdx_code in [x['name'] for x in groups]:
return False
else:
groups = list()
groups.append({'name': hdx_code})
self.data['groups'] = groups
return True
|
[
"def",
"add_other_location",
"(",
"self",
",",
"location",
",",
"exact",
"=",
"True",
",",
"alterror",
"=",
"None",
",",
"locations",
"=",
"None",
")",
":",
"# type: (str, bool, Optional[str], Optional[List[str]]) -> bool",
"hdx_code",
",",
"match",
"=",
"Locations",
".",
"get_HDX_code_from_location_partial",
"(",
"location",
",",
"locations",
"=",
"locations",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"if",
"hdx_code",
"is",
"None",
"or",
"(",
"exact",
"is",
"True",
"and",
"match",
"is",
"False",
")",
":",
"if",
"alterror",
"is",
"None",
":",
"raise",
"HDXError",
"(",
"'Location: %s - cannot find in HDX!'",
"%",
"location",
")",
"else",
":",
"raise",
"HDXError",
"(",
"alterror",
")",
"groups",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'groups'",
",",
"None",
")",
"hdx_code",
"=",
"hdx_code",
".",
"lower",
"(",
")",
"if",
"groups",
":",
"if",
"hdx_code",
"in",
"[",
"x",
"[",
"'name'",
"]",
"for",
"x",
"in",
"groups",
"]",
":",
"return",
"False",
"else",
":",
"groups",
"=",
"list",
"(",
")",
"groups",
".",
"append",
"(",
"{",
"'name'",
":",
"hdx_code",
"}",
")",
"self",
".",
"data",
"[",
"'groups'",
"]",
"=",
"groups",
"return",
"True"
] |
Add a location which is not a country or region. Value is parsed and compared to existing locations in
HDX. If the location is already added, it is ignored.
Args:
location (str): Location to add
exact (bool): True for exact matching or False to allow fuzzy matching. Defaults to True.
alterror (Optional[str]): Alternative error message to builtin if location not found. Defaults to None.
locations (Optional[List[str]]): Valid locations list. Defaults to list downloaded from HDX.
Returns:
bool: True if location added or False if location already present
|
[
"Add",
"a",
"location",
"which",
"is",
"not",
"a",
"country",
"or",
"region",
".",
"Value",
"is",
"parsed",
"and",
"compared",
"to",
"existing",
"locations",
"in",
"HDX",
".",
"If",
"the",
"location",
"is",
"already",
"added",
"it",
"is",
"ignored",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1139-L1169
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.remove_location
|
def remove_location(self, location):
# type: (str) -> bool
"""Remove a location. If the location is already added, it is ignored.
Args:
location (str): Location to remove
Returns:
bool: True if location removed or False if not
"""
res = self._remove_hdxobject(self.data.get('groups'), location, matchon='name')
if not res:
res = self._remove_hdxobject(self.data.get('groups'), location.upper(), matchon='name')
if not res:
res = self._remove_hdxobject(self.data.get('groups'), location.lower(), matchon='name')
return res
|
python
|
def remove_location(self, location):
# type: (str) -> bool
"""Remove a location. If the location is already added, it is ignored.
Args:
location (str): Location to remove
Returns:
bool: True if location removed or False if not
"""
res = self._remove_hdxobject(self.data.get('groups'), location, matchon='name')
if not res:
res = self._remove_hdxobject(self.data.get('groups'), location.upper(), matchon='name')
if not res:
res = self._remove_hdxobject(self.data.get('groups'), location.lower(), matchon='name')
return res
|
[
"def",
"remove_location",
"(",
"self",
",",
"location",
")",
":",
"# type: (str) -> bool",
"res",
"=",
"self",
".",
"_remove_hdxobject",
"(",
"self",
".",
"data",
".",
"get",
"(",
"'groups'",
")",
",",
"location",
",",
"matchon",
"=",
"'name'",
")",
"if",
"not",
"res",
":",
"res",
"=",
"self",
".",
"_remove_hdxobject",
"(",
"self",
".",
"data",
".",
"get",
"(",
"'groups'",
")",
",",
"location",
".",
"upper",
"(",
")",
",",
"matchon",
"=",
"'name'",
")",
"if",
"not",
"res",
":",
"res",
"=",
"self",
".",
"_remove_hdxobject",
"(",
"self",
".",
"data",
".",
"get",
"(",
"'groups'",
")",
",",
"location",
".",
"lower",
"(",
")",
",",
"matchon",
"=",
"'name'",
")",
"return",
"res"
] |
Remove a location. If the location is already added, it is ignored.
Args:
location (str): Location to remove
Returns:
bool: True if location removed or False if not
|
[
"Remove",
"a",
"location",
".",
"If",
"the",
"location",
"is",
"already",
"added",
"it",
"is",
"ignored",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1171-L1186
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_maintainer
|
def get_maintainer(self):
# type: () -> hdx.data.user.User
"""Get the dataset's maintainer.
Returns:
User: Dataset's maintainer
"""
return hdx.data.user.User.read_from_hdx(self.data['maintainer'], configuration=self.configuration)
|
python
|
def get_maintainer(self):
# type: () -> hdx.data.user.User
"""Get the dataset's maintainer.
Returns:
User: Dataset's maintainer
"""
return hdx.data.user.User.read_from_hdx(self.data['maintainer'], configuration=self.configuration)
|
[
"def",
"get_maintainer",
"(",
"self",
")",
":",
"# type: () -> hdx.data.user.User",
"return",
"hdx",
".",
"data",
".",
"user",
".",
"User",
".",
"read_from_hdx",
"(",
"self",
".",
"data",
"[",
"'maintainer'",
"]",
",",
"configuration",
"=",
"self",
".",
"configuration",
")"
] |
Get the dataset's maintainer.
Returns:
User: Dataset's maintainer
|
[
"Get",
"the",
"dataset",
"s",
"maintainer",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1188-L1195
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_maintainer
|
def set_maintainer(self, maintainer):
# type: (Union[hdx.data.user.User,Dict,str]) -> None
"""Set the dataset's maintainer.
Args:
maintainer (Union[User,Dict,str]): Either a user id or User metadata from a User object or dictionary.
Returns:
None
"""
if isinstance(maintainer, hdx.data.user.User) or isinstance(maintainer, dict):
if 'id' not in maintainer:
maintainer = hdx.data.user.User.read_from_hdx(maintainer['name'], configuration=self.configuration)
maintainer = maintainer['id']
elif not isinstance(maintainer, str):
raise HDXError('Type %s cannot be added as a maintainer!' % type(maintainer).__name__)
if is_valid_uuid(maintainer) is False:
raise HDXError('%s is not a valid user id for a maintainer!' % maintainer)
self.data['maintainer'] = maintainer
|
python
|
def set_maintainer(self, maintainer):
# type: (Union[hdx.data.user.User,Dict,str]) -> None
"""Set the dataset's maintainer.
Args:
maintainer (Union[User,Dict,str]): Either a user id or User metadata from a User object or dictionary.
Returns:
None
"""
if isinstance(maintainer, hdx.data.user.User) or isinstance(maintainer, dict):
if 'id' not in maintainer:
maintainer = hdx.data.user.User.read_from_hdx(maintainer['name'], configuration=self.configuration)
maintainer = maintainer['id']
elif not isinstance(maintainer, str):
raise HDXError('Type %s cannot be added as a maintainer!' % type(maintainer).__name__)
if is_valid_uuid(maintainer) is False:
raise HDXError('%s is not a valid user id for a maintainer!' % maintainer)
self.data['maintainer'] = maintainer
|
[
"def",
"set_maintainer",
"(",
"self",
",",
"maintainer",
")",
":",
"# type: (Union[hdx.data.user.User,Dict,str]) -> None",
"if",
"isinstance",
"(",
"maintainer",
",",
"hdx",
".",
"data",
".",
"user",
".",
"User",
")",
"or",
"isinstance",
"(",
"maintainer",
",",
"dict",
")",
":",
"if",
"'id'",
"not",
"in",
"maintainer",
":",
"maintainer",
"=",
"hdx",
".",
"data",
".",
"user",
".",
"User",
".",
"read_from_hdx",
"(",
"maintainer",
"[",
"'name'",
"]",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"maintainer",
"=",
"maintainer",
"[",
"'id'",
"]",
"elif",
"not",
"isinstance",
"(",
"maintainer",
",",
"str",
")",
":",
"raise",
"HDXError",
"(",
"'Type %s cannot be added as a maintainer!'",
"%",
"type",
"(",
"maintainer",
")",
".",
"__name__",
")",
"if",
"is_valid_uuid",
"(",
"maintainer",
")",
"is",
"False",
":",
"raise",
"HDXError",
"(",
"'%s is not a valid user id for a maintainer!'",
"%",
"maintainer",
")",
"self",
".",
"data",
"[",
"'maintainer'",
"]",
"=",
"maintainer"
] |
Set the dataset's maintainer.
Args:
maintainer (Union[User,Dict,str]): Either a user id or User metadata from a User object or dictionary.
Returns:
None
|
[
"Set",
"the",
"dataset",
"s",
"maintainer",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1197-L1214
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_organization
|
def get_organization(self):
# type: () -> hdx.data.organization.Organization
"""Get the dataset's organization.
Returns:
Organization: Dataset's organization
"""
return hdx.data.organization.Organization.read_from_hdx(self.data['owner_org'], configuration=self.configuration)
|
python
|
def get_organization(self):
# type: () -> hdx.data.organization.Organization
"""Get the dataset's organization.
Returns:
Organization: Dataset's organization
"""
return hdx.data.organization.Organization.read_from_hdx(self.data['owner_org'], configuration=self.configuration)
|
[
"def",
"get_organization",
"(",
"self",
")",
":",
"# type: () -> hdx.data.organization.Organization",
"return",
"hdx",
".",
"data",
".",
"organization",
".",
"Organization",
".",
"read_from_hdx",
"(",
"self",
".",
"data",
"[",
"'owner_org'",
"]",
",",
"configuration",
"=",
"self",
".",
"configuration",
")"
] |
Get the dataset's organization.
Returns:
Organization: Dataset's organization
|
[
"Get",
"the",
"dataset",
"s",
"organization",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1216-L1223
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_organization
|
def set_organization(self, organization):
# type: (Union[hdx.data.organization.Organization,Dict,str]) -> None
"""Set the dataset's organization.
Args:
organization (Union[Organization,Dict,str]): Either an Organization id or Organization metadata from an Organization object or dictionary.
Returns:
None
"""
if isinstance(organization, hdx.data.organization.Organization) or isinstance(organization, dict):
if 'id' not in organization:
organization = hdx.data.organization.Organization.read_from_hdx(organization['name'], configuration=self.configuration)
organization = organization['id']
elif not isinstance(organization, str):
raise HDXError('Type %s cannot be added as a organization!' % type(organization).__name__)
if is_valid_uuid(organization) is False and organization != 'hdx':
raise HDXError('%s is not a valid organization id!' % organization)
self.data['owner_org'] = organization
|
python
|
def set_organization(self, organization):
# type: (Union[hdx.data.organization.Organization,Dict,str]) -> None
"""Set the dataset's organization.
Args:
organization (Union[Organization,Dict,str]): Either an Organization id or Organization metadata from an Organization object or dictionary.
Returns:
None
"""
if isinstance(organization, hdx.data.organization.Organization) or isinstance(organization, dict):
if 'id' not in organization:
organization = hdx.data.organization.Organization.read_from_hdx(organization['name'], configuration=self.configuration)
organization = organization['id']
elif not isinstance(organization, str):
raise HDXError('Type %s cannot be added as a organization!' % type(organization).__name__)
if is_valid_uuid(organization) is False and organization != 'hdx':
raise HDXError('%s is not a valid organization id!' % organization)
self.data['owner_org'] = organization
|
[
"def",
"set_organization",
"(",
"self",
",",
"organization",
")",
":",
"# type: (Union[hdx.data.organization.Organization,Dict,str]) -> None",
"if",
"isinstance",
"(",
"organization",
",",
"hdx",
".",
"data",
".",
"organization",
".",
"Organization",
")",
"or",
"isinstance",
"(",
"organization",
",",
"dict",
")",
":",
"if",
"'id'",
"not",
"in",
"organization",
":",
"organization",
"=",
"hdx",
".",
"data",
".",
"organization",
".",
"Organization",
".",
"read_from_hdx",
"(",
"organization",
"[",
"'name'",
"]",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"organization",
"=",
"organization",
"[",
"'id'",
"]",
"elif",
"not",
"isinstance",
"(",
"organization",
",",
"str",
")",
":",
"raise",
"HDXError",
"(",
"'Type %s cannot be added as a organization!'",
"%",
"type",
"(",
"organization",
")",
".",
"__name__",
")",
"if",
"is_valid_uuid",
"(",
"organization",
")",
"is",
"False",
"and",
"organization",
"!=",
"'hdx'",
":",
"raise",
"HDXError",
"(",
"'%s is not a valid organization id!'",
"%",
"organization",
")",
"self",
".",
"data",
"[",
"'owner_org'",
"]",
"=",
"organization"
] |
Set the dataset's organization.
Args:
organization (Union[Organization,Dict,str]): Either an Organization id or Organization metadata from an Organization object or dictionary.
Returns:
None
|
[
"Set",
"the",
"dataset",
"s",
"organization",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1225-L1242
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_showcases
|
def get_showcases(self):
# type: () -> List[hdx.data.showcase.Showcase]
"""Get any showcases the dataset is in
Returns:
List[Showcase]: list of showcases
"""
assoc_result, showcases_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='package_id',
action=hdx.data.showcase.Showcase.actions()['list_showcases'])
showcases = list()
if assoc_result:
for showcase_dict in showcases_dicts:
showcase = hdx.data.showcase.Showcase(showcase_dict, configuration=self.configuration)
showcases.append(showcase)
return showcases
|
python
|
def get_showcases(self):
# type: () -> List[hdx.data.showcase.Showcase]
"""Get any showcases the dataset is in
Returns:
List[Showcase]: list of showcases
"""
assoc_result, showcases_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='package_id',
action=hdx.data.showcase.Showcase.actions()['list_showcases'])
showcases = list()
if assoc_result:
for showcase_dict in showcases_dicts:
showcase = hdx.data.showcase.Showcase(showcase_dict, configuration=self.configuration)
showcases.append(showcase)
return showcases
|
[
"def",
"get_showcases",
"(",
"self",
")",
":",
"# type: () -> List[hdx.data.showcase.Showcase]",
"assoc_result",
",",
"showcases_dicts",
"=",
"self",
".",
"_read_from_hdx",
"(",
"'showcase'",
",",
"self",
".",
"data",
"[",
"'id'",
"]",
",",
"fieldname",
"=",
"'package_id'",
",",
"action",
"=",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
".",
"actions",
"(",
")",
"[",
"'list_showcases'",
"]",
")",
"showcases",
"=",
"list",
"(",
")",
"if",
"assoc_result",
":",
"for",
"showcase_dict",
"in",
"showcases_dicts",
":",
"showcase",
"=",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
"(",
"showcase_dict",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"showcases",
".",
"append",
"(",
"showcase",
")",
"return",
"showcases"
] |
Get any showcases the dataset is in
Returns:
List[Showcase]: list of showcases
|
[
"Get",
"any",
"showcases",
"the",
"dataset",
"is",
"in"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1244-L1258
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset._get_dataset_showcase_dict
|
def _get_dataset_showcase_dict(self, showcase):
# type: (Union[hdx.data.showcase.Showcase, Dict,str]) -> Dict
"""Get dataset showcase dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
dict: dataset showcase dict
"""
if isinstance(showcase, hdx.data.showcase.Showcase) or isinstance(showcase, dict):
if 'id' not in showcase:
showcase = hdx.data.showcase.Showcase.read_from_hdx(showcase['name'])
showcase = showcase['id']
elif not isinstance(showcase, str):
raise HDXError('Type %s cannot be added as a showcase!' % type(showcase).__name__)
if is_valid_uuid(showcase) is False:
raise HDXError('%s is not a valid showcase id!' % showcase)
return {'package_id': self.data['id'], 'showcase_id': showcase}
|
python
|
def _get_dataset_showcase_dict(self, showcase):
# type: (Union[hdx.data.showcase.Showcase, Dict,str]) -> Dict
"""Get dataset showcase dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
dict: dataset showcase dict
"""
if isinstance(showcase, hdx.data.showcase.Showcase) or isinstance(showcase, dict):
if 'id' not in showcase:
showcase = hdx.data.showcase.Showcase.read_from_hdx(showcase['name'])
showcase = showcase['id']
elif not isinstance(showcase, str):
raise HDXError('Type %s cannot be added as a showcase!' % type(showcase).__name__)
if is_valid_uuid(showcase) is False:
raise HDXError('%s is not a valid showcase id!' % showcase)
return {'package_id': self.data['id'], 'showcase_id': showcase}
|
[
"def",
"_get_dataset_showcase_dict",
"(",
"self",
",",
"showcase",
")",
":",
"# type: (Union[hdx.data.showcase.Showcase, Dict,str]) -> Dict",
"if",
"isinstance",
"(",
"showcase",
",",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
")",
"or",
"isinstance",
"(",
"showcase",
",",
"dict",
")",
":",
"if",
"'id'",
"not",
"in",
"showcase",
":",
"showcase",
"=",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
".",
"read_from_hdx",
"(",
"showcase",
"[",
"'name'",
"]",
")",
"showcase",
"=",
"showcase",
"[",
"'id'",
"]",
"elif",
"not",
"isinstance",
"(",
"showcase",
",",
"str",
")",
":",
"raise",
"HDXError",
"(",
"'Type %s cannot be added as a showcase!'",
"%",
"type",
"(",
"showcase",
")",
".",
"__name__",
")",
"if",
"is_valid_uuid",
"(",
"showcase",
")",
"is",
"False",
":",
"raise",
"HDXError",
"(",
"'%s is not a valid showcase id!'",
"%",
"showcase",
")",
"return",
"{",
"'package_id'",
":",
"self",
".",
"data",
"[",
"'id'",
"]",
",",
"'showcase_id'",
":",
"showcase",
"}"
] |
Get dataset showcase dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
dict: dataset showcase dict
|
[
"Get",
"dataset",
"showcase",
"dict"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1260-L1278
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_showcase
|
def add_showcase(self, showcase, showcases_to_check=None):
# type: (Union[hdx.data.showcase.Showcase,Dict,str], List[hdx.data.showcase.Showcase]) -> bool
"""Add dataset to showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or showcase metadata from a Showcase object or dictionary
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if the showcase was added, False if already present
"""
dataset_showcase = self._get_dataset_showcase_dict(showcase)
if showcases_to_check is None:
showcases_to_check = self.get_showcases()
for showcase in showcases_to_check:
if dataset_showcase['showcase_id'] == showcase['id']:
return False
showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)
showcase._write_to_hdx('associate', dataset_showcase, 'package_id')
return True
|
python
|
def add_showcase(self, showcase, showcases_to_check=None):
# type: (Union[hdx.data.showcase.Showcase,Dict,str], List[hdx.data.showcase.Showcase]) -> bool
"""Add dataset to showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or showcase metadata from a Showcase object or dictionary
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if the showcase was added, False if already present
"""
dataset_showcase = self._get_dataset_showcase_dict(showcase)
if showcases_to_check is None:
showcases_to_check = self.get_showcases()
for showcase in showcases_to_check:
if dataset_showcase['showcase_id'] == showcase['id']:
return False
showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)
showcase._write_to_hdx('associate', dataset_showcase, 'package_id')
return True
|
[
"def",
"add_showcase",
"(",
"self",
",",
"showcase",
",",
"showcases_to_check",
"=",
"None",
")",
":",
"# type: (Union[hdx.data.showcase.Showcase,Dict,str], List[hdx.data.showcase.Showcase]) -> bool",
"dataset_showcase",
"=",
"self",
".",
"_get_dataset_showcase_dict",
"(",
"showcase",
")",
"if",
"showcases_to_check",
"is",
"None",
":",
"showcases_to_check",
"=",
"self",
".",
"get_showcases",
"(",
")",
"for",
"showcase",
"in",
"showcases_to_check",
":",
"if",
"dataset_showcase",
"[",
"'showcase_id'",
"]",
"==",
"showcase",
"[",
"'id'",
"]",
":",
"return",
"False",
"showcase",
"=",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
"(",
"{",
"'id'",
":",
"dataset_showcase",
"[",
"'showcase_id'",
"]",
"}",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"showcase",
".",
"_write_to_hdx",
"(",
"'associate'",
",",
"dataset_showcase",
",",
"'package_id'",
")",
"return",
"True"
] |
Add dataset to showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or showcase metadata from a Showcase object or dictionary
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if the showcase was added, False if already present
|
[
"Add",
"dataset",
"to",
"showcase"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1280-L1299
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.add_showcases
|
def add_showcases(self, showcases, showcases_to_check=None):
# type: (List[Union[hdx.data.showcase.Showcase,Dict,str]], List[hdx.data.showcase.Showcase]) -> bool
"""Add dataset to multiple showcases
Args:
showcases (List[Union[Showcase,Dict,str]]): A list of either showcase ids or showcase metadata from Showcase objects or dictionaries
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if all showcases added or False if any already present
"""
if showcases_to_check is None:
showcases_to_check = self.get_showcases()
allshowcasesadded = True
for showcase in showcases:
if not self.add_showcase(showcase, showcases_to_check=showcases_to_check):
allshowcasesadded = False
return allshowcasesadded
|
python
|
def add_showcases(self, showcases, showcases_to_check=None):
# type: (List[Union[hdx.data.showcase.Showcase,Dict,str]], List[hdx.data.showcase.Showcase]) -> bool
"""Add dataset to multiple showcases
Args:
showcases (List[Union[Showcase,Dict,str]]): A list of either showcase ids or showcase metadata from Showcase objects or dictionaries
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if all showcases added or False if any already present
"""
if showcases_to_check is None:
showcases_to_check = self.get_showcases()
allshowcasesadded = True
for showcase in showcases:
if not self.add_showcase(showcase, showcases_to_check=showcases_to_check):
allshowcasesadded = False
return allshowcasesadded
|
[
"def",
"add_showcases",
"(",
"self",
",",
"showcases",
",",
"showcases_to_check",
"=",
"None",
")",
":",
"# type: (List[Union[hdx.data.showcase.Showcase,Dict,str]], List[hdx.data.showcase.Showcase]) -> bool",
"if",
"showcases_to_check",
"is",
"None",
":",
"showcases_to_check",
"=",
"self",
".",
"get_showcases",
"(",
")",
"allshowcasesadded",
"=",
"True",
"for",
"showcase",
"in",
"showcases",
":",
"if",
"not",
"self",
".",
"add_showcase",
"(",
"showcase",
",",
"showcases_to_check",
"=",
"showcases_to_check",
")",
":",
"allshowcasesadded",
"=",
"False",
"return",
"allshowcasesadded"
] |
Add dataset to multiple showcases
Args:
showcases (List[Union[Showcase,Dict,str]]): A list of either showcase ids or showcase metadata from Showcase objects or dictionaries
showcases_to_check (List[Showcase]): list of showcases against which to check existence of showcase. Defaults to showcases containing dataset.
Returns:
bool: True if all showcases added or False if any already present
|
[
"Add",
"dataset",
"to",
"multiple",
"showcases"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1301-L1318
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.remove_showcase
|
def remove_showcase(self, showcase):
# type: (Union[hdx.data.showcase.Showcase,Dict,str]) -> None
"""Remove dataset from showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id string or showcase metadata from a Showcase object or dictionary
Returns:
None
"""
dataset_showcase = self._get_dataset_showcase_dict(showcase)
showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)
showcase._write_to_hdx('disassociate', dataset_showcase, 'package_id')
|
python
|
def remove_showcase(self, showcase):
# type: (Union[hdx.data.showcase.Showcase,Dict,str]) -> None
"""Remove dataset from showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id string or showcase metadata from a Showcase object or dictionary
Returns:
None
"""
dataset_showcase = self._get_dataset_showcase_dict(showcase)
showcase = hdx.data.showcase.Showcase({'id': dataset_showcase['showcase_id']}, configuration=self.configuration)
showcase._write_to_hdx('disassociate', dataset_showcase, 'package_id')
|
[
"def",
"remove_showcase",
"(",
"self",
",",
"showcase",
")",
":",
"# type: (Union[hdx.data.showcase.Showcase,Dict,str]) -> None",
"dataset_showcase",
"=",
"self",
".",
"_get_dataset_showcase_dict",
"(",
"showcase",
")",
"showcase",
"=",
"hdx",
".",
"data",
".",
"showcase",
".",
"Showcase",
"(",
"{",
"'id'",
":",
"dataset_showcase",
"[",
"'showcase_id'",
"]",
"}",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"showcase",
".",
"_write_to_hdx",
"(",
"'disassociate'",
",",
"dataset_showcase",
",",
"'package_id'",
")"
] |
Remove dataset from showcase
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id string or showcase metadata from a Showcase object or dictionary
Returns:
None
|
[
"Remove",
"dataset",
"from",
"showcase"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1320-L1332
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_requestable
|
def set_requestable(self, requestable=True):
# type: (bool) -> None
"""Set the dataset to be of type requestable or not
Args:
requestable (bool): Set whether dataset is requestable. Defaults to True.
Returns:
None
"""
self.data['is_requestdata_type'] = requestable
if requestable:
self.data['private'] = False
|
python
|
def set_requestable(self, requestable=True):
# type: (bool) -> None
"""Set the dataset to be of type requestable or not
Args:
requestable (bool): Set whether dataset is requestable. Defaults to True.
Returns:
None
"""
self.data['is_requestdata_type'] = requestable
if requestable:
self.data['private'] = False
|
[
"def",
"set_requestable",
"(",
"self",
",",
"requestable",
"=",
"True",
")",
":",
"# type: (bool) -> None",
"self",
".",
"data",
"[",
"'is_requestdata_type'",
"]",
"=",
"requestable",
"if",
"requestable",
":",
"self",
".",
"data",
"[",
"'private'",
"]",
"=",
"False"
] |
Set the dataset to be of type requestable or not
Args:
requestable (bool): Set whether dataset is requestable. Defaults to True.
Returns:
None
|
[
"Set",
"the",
"dataset",
"to",
"be",
"of",
"type",
"requestable",
"or",
"not"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1343-L1355
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.get_filetypes
|
def get_filetypes(self):
# type: () -> List[str]
"""Return list of filetypes in your data
Returns:
List[str]: List of filetypes
"""
if not self.is_requestable():
return [resource.get_file_type() for resource in self.get_resources()]
return self._get_stringlist_from_commastring('file_types')
|
python
|
def get_filetypes(self):
# type: () -> List[str]
"""Return list of filetypes in your data
Returns:
List[str]: List of filetypes
"""
if not self.is_requestable():
return [resource.get_file_type() for resource in self.get_resources()]
return self._get_stringlist_from_commastring('file_types')
|
[
"def",
"get_filetypes",
"(",
"self",
")",
":",
"# type: () -> List[str]",
"if",
"not",
"self",
".",
"is_requestable",
"(",
")",
":",
"return",
"[",
"resource",
".",
"get_file_type",
"(",
")",
"for",
"resource",
"in",
"self",
".",
"get_resources",
"(",
")",
"]",
"return",
"self",
".",
"_get_stringlist_from_commastring",
"(",
"'file_types'",
")"
] |
Return list of filetypes in your data
Returns:
List[str]: List of filetypes
|
[
"Return",
"list",
"of",
"filetypes",
"in",
"your",
"data"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1410-L1419
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.clean_dataset_tags
|
def clean_dataset_tags(self):
# type: () -> Tuple[bool, bool]
"""Clean dataset tags according to tags cleanup spreadsheet and return if any changes occurred
Returns:
Tuple[bool, bool]: Returns (True if tags changed or False if not, True if error or False if not)
"""
tags_dict, wildcard_tags = Tags.tagscleanupdicts()
def delete_tag(tag):
logger.info('%s - Deleting tag %s!' % (self.data['name'], tag))
return self.remove_tag(tag), False
def update_tag(tag, final_tags, wording, remove_existing=True):
text = '%s - %s: %s -> ' % (self.data['name'], wording, tag)
if not final_tags:
logger.error('%snothing!' % text)
return False, True
tags_lower_five = final_tags[:5].lower()
if tags_lower_five == 'merge' or tags_lower_five == 'split' or (
';' not in final_tags and len(final_tags) > 50):
logger.error('%s%s - Invalid final tag!' % (text, final_tags))
return False, True
if remove_existing:
self.remove_tag(tag)
tags = ', '.join(self.get_tags())
if self.add_tags(final_tags.split(';')):
logger.info('%s%s! Dataset tags: %s' % (text, final_tags, tags))
else:
logger.warning(
'%s%s - At least one of the tags already exists! Dataset tags: %s' % (text, final_tags, tags))
return True, False
def do_action(tag, tags_dict_key):
whattodo = tags_dict[tags_dict_key]
action = whattodo[u'action']
final_tags = whattodo[u'final tags (semicolon separated)']
if action == u'Delete':
changed, error = delete_tag(tag)
elif action == u'Merge':
changed, error = update_tag(tag, final_tags, 'Merging')
elif action == u'Fix spelling':
changed, error = update_tag(tag, final_tags, 'Fixing spelling')
elif action == u'Non English':
changed, error = update_tag(tag, final_tags, 'Anglicising', remove_existing=False)
else:
changed = False
error = False
return changed, error
def process_tag(tag):
changed = False
error = False
if tag in tags_dict.keys():
changed, error = do_action(tag, tag)
else:
for wildcard_tag in wildcard_tags:
if fnmatch.fnmatch(tag, wildcard_tag):
changed, error = do_action(tag, wildcard_tag)
break
return changed, error
anychange = False
anyerror = False
for tag in self.get_tags():
changed, error = process_tag(tag)
if changed:
anychange = True
if error:
anyerror = True
return anychange, anyerror
|
python
|
def clean_dataset_tags(self):
# type: () -> Tuple[bool, bool]
"""Clean dataset tags according to tags cleanup spreadsheet and return if any changes occurred
Returns:
Tuple[bool, bool]: Returns (True if tags changed or False if not, True if error or False if not)
"""
tags_dict, wildcard_tags = Tags.tagscleanupdicts()
def delete_tag(tag):
logger.info('%s - Deleting tag %s!' % (self.data['name'], tag))
return self.remove_tag(tag), False
def update_tag(tag, final_tags, wording, remove_existing=True):
text = '%s - %s: %s -> ' % (self.data['name'], wording, tag)
if not final_tags:
logger.error('%snothing!' % text)
return False, True
tags_lower_five = final_tags[:5].lower()
if tags_lower_five == 'merge' or tags_lower_five == 'split' or (
';' not in final_tags and len(final_tags) > 50):
logger.error('%s%s - Invalid final tag!' % (text, final_tags))
return False, True
if remove_existing:
self.remove_tag(tag)
tags = ', '.join(self.get_tags())
if self.add_tags(final_tags.split(';')):
logger.info('%s%s! Dataset tags: %s' % (text, final_tags, tags))
else:
logger.warning(
'%s%s - At least one of the tags already exists! Dataset tags: %s' % (text, final_tags, tags))
return True, False
def do_action(tag, tags_dict_key):
whattodo = tags_dict[tags_dict_key]
action = whattodo[u'action']
final_tags = whattodo[u'final tags (semicolon separated)']
if action == u'Delete':
changed, error = delete_tag(tag)
elif action == u'Merge':
changed, error = update_tag(tag, final_tags, 'Merging')
elif action == u'Fix spelling':
changed, error = update_tag(tag, final_tags, 'Fixing spelling')
elif action == u'Non English':
changed, error = update_tag(tag, final_tags, 'Anglicising', remove_existing=False)
else:
changed = False
error = False
return changed, error
def process_tag(tag):
changed = False
error = False
if tag in tags_dict.keys():
changed, error = do_action(tag, tag)
else:
for wildcard_tag in wildcard_tags:
if fnmatch.fnmatch(tag, wildcard_tag):
changed, error = do_action(tag, wildcard_tag)
break
return changed, error
anychange = False
anyerror = False
for tag in self.get_tags():
changed, error = process_tag(tag)
if changed:
anychange = True
if error:
anyerror = True
return anychange, anyerror
|
[
"def",
"clean_dataset_tags",
"(",
"self",
")",
":",
"# type: () -> Tuple[bool, bool]",
"tags_dict",
",",
"wildcard_tags",
"=",
"Tags",
".",
"tagscleanupdicts",
"(",
")",
"def",
"delete_tag",
"(",
"tag",
")",
":",
"logger",
".",
"info",
"(",
"'%s - Deleting tag %s!'",
"%",
"(",
"self",
".",
"data",
"[",
"'name'",
"]",
",",
"tag",
")",
")",
"return",
"self",
".",
"remove_tag",
"(",
"tag",
")",
",",
"False",
"def",
"update_tag",
"(",
"tag",
",",
"final_tags",
",",
"wording",
",",
"remove_existing",
"=",
"True",
")",
":",
"text",
"=",
"'%s - %s: %s -> '",
"%",
"(",
"self",
".",
"data",
"[",
"'name'",
"]",
",",
"wording",
",",
"tag",
")",
"if",
"not",
"final_tags",
":",
"logger",
".",
"error",
"(",
"'%snothing!'",
"%",
"text",
")",
"return",
"False",
",",
"True",
"tags_lower_five",
"=",
"final_tags",
"[",
":",
"5",
"]",
".",
"lower",
"(",
")",
"if",
"tags_lower_five",
"==",
"'merge'",
"or",
"tags_lower_five",
"==",
"'split'",
"or",
"(",
"';'",
"not",
"in",
"final_tags",
"and",
"len",
"(",
"final_tags",
")",
">",
"50",
")",
":",
"logger",
".",
"error",
"(",
"'%s%s - Invalid final tag!'",
"%",
"(",
"text",
",",
"final_tags",
")",
")",
"return",
"False",
",",
"True",
"if",
"remove_existing",
":",
"self",
".",
"remove_tag",
"(",
"tag",
")",
"tags",
"=",
"', '",
".",
"join",
"(",
"self",
".",
"get_tags",
"(",
")",
")",
"if",
"self",
".",
"add_tags",
"(",
"final_tags",
".",
"split",
"(",
"';'",
")",
")",
":",
"logger",
".",
"info",
"(",
"'%s%s! Dataset tags: %s'",
"%",
"(",
"text",
",",
"final_tags",
",",
"tags",
")",
")",
"else",
":",
"logger",
".",
"warning",
"(",
"'%s%s - At least one of the tags already exists! Dataset tags: %s'",
"%",
"(",
"text",
",",
"final_tags",
",",
"tags",
")",
")",
"return",
"True",
",",
"False",
"def",
"do_action",
"(",
"tag",
",",
"tags_dict_key",
")",
":",
"whattodo",
"=",
"tags_dict",
"[",
"tags_dict_key",
"]",
"action",
"=",
"whattodo",
"[",
"u'action'",
"]",
"final_tags",
"=",
"whattodo",
"[",
"u'final tags (semicolon separated)'",
"]",
"if",
"action",
"==",
"u'Delete'",
":",
"changed",
",",
"error",
"=",
"delete_tag",
"(",
"tag",
")",
"elif",
"action",
"==",
"u'Merge'",
":",
"changed",
",",
"error",
"=",
"update_tag",
"(",
"tag",
",",
"final_tags",
",",
"'Merging'",
")",
"elif",
"action",
"==",
"u'Fix spelling'",
":",
"changed",
",",
"error",
"=",
"update_tag",
"(",
"tag",
",",
"final_tags",
",",
"'Fixing spelling'",
")",
"elif",
"action",
"==",
"u'Non English'",
":",
"changed",
",",
"error",
"=",
"update_tag",
"(",
"tag",
",",
"final_tags",
",",
"'Anglicising'",
",",
"remove_existing",
"=",
"False",
")",
"else",
":",
"changed",
"=",
"False",
"error",
"=",
"False",
"return",
"changed",
",",
"error",
"def",
"process_tag",
"(",
"tag",
")",
":",
"changed",
"=",
"False",
"error",
"=",
"False",
"if",
"tag",
"in",
"tags_dict",
".",
"keys",
"(",
")",
":",
"changed",
",",
"error",
"=",
"do_action",
"(",
"tag",
",",
"tag",
")",
"else",
":",
"for",
"wildcard_tag",
"in",
"wildcard_tags",
":",
"if",
"fnmatch",
".",
"fnmatch",
"(",
"tag",
",",
"wildcard_tag",
")",
":",
"changed",
",",
"error",
"=",
"do_action",
"(",
"tag",
",",
"wildcard_tag",
")",
"break",
"return",
"changed",
",",
"error",
"anychange",
"=",
"False",
"anyerror",
"=",
"False",
"for",
"tag",
"in",
"self",
".",
"get_tags",
"(",
")",
":",
"changed",
",",
"error",
"=",
"process_tag",
"(",
"tag",
")",
"if",
"changed",
":",
"anychange",
"=",
"True",
"if",
"error",
":",
"anyerror",
"=",
"True",
"return",
"anychange",
",",
"anyerror"
] |
Clean dataset tags according to tags cleanup spreadsheet and return if any changes occurred
Returns:
Tuple[bool, bool]: Returns (True if tags changed or False if not, True if error or False if not)
|
[
"Clean",
"dataset",
"tags",
"according",
"to",
"tags",
"cleanup",
"spreadsheet",
"and",
"return",
"if",
"any",
"changes",
"occurred"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1463-L1534
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.set_quickchart_resource
|
def set_quickchart_resource(self, resource):
# type: (Union[hdx.data.resource.Resource,Dict,str,int]) -> bool
"""Set the resource that will be used for displaying QuickCharts in dataset preview
Args:
resource (Union[hdx.data.resource.Resource,Dict,str,int]): Either resource id or name, resource metadata from a Resource object or a dictionary or position
Returns:
bool: Returns True if resource for QuickCharts in dataset preview set or False if not
"""
if isinstance(resource, int) and not isinstance(resource, bool):
resource = self.get_resources()[resource]
if isinstance(resource, hdx.data.resource.Resource) or isinstance(resource, dict):
res = resource.get('id')
if res is None:
resource = resource['name']
else:
resource = res
elif not isinstance(resource, str):
raise hdx.data.hdxobject.HDXError('Resource id cannot be found in type %s!' % type(resource).__name__)
if is_valid_uuid(resource) is True:
search = 'id'
else:
search = 'name'
changed = False
for dataset_resource in self.resources:
if dataset_resource[search] == resource:
dataset_resource.enable_dataset_preview()
self.preview_resource()
changed = True
else:
dataset_resource.disable_dataset_preview()
return changed
|
python
|
def set_quickchart_resource(self, resource):
# type: (Union[hdx.data.resource.Resource,Dict,str,int]) -> bool
"""Set the resource that will be used for displaying QuickCharts in dataset preview
Args:
resource (Union[hdx.data.resource.Resource,Dict,str,int]): Either resource id or name, resource metadata from a Resource object or a dictionary or position
Returns:
bool: Returns True if resource for QuickCharts in dataset preview set or False if not
"""
if isinstance(resource, int) and not isinstance(resource, bool):
resource = self.get_resources()[resource]
if isinstance(resource, hdx.data.resource.Resource) or isinstance(resource, dict):
res = resource.get('id')
if res is None:
resource = resource['name']
else:
resource = res
elif not isinstance(resource, str):
raise hdx.data.hdxobject.HDXError('Resource id cannot be found in type %s!' % type(resource).__name__)
if is_valid_uuid(resource) is True:
search = 'id'
else:
search = 'name'
changed = False
for dataset_resource in self.resources:
if dataset_resource[search] == resource:
dataset_resource.enable_dataset_preview()
self.preview_resource()
changed = True
else:
dataset_resource.disable_dataset_preview()
return changed
|
[
"def",
"set_quickchart_resource",
"(",
"self",
",",
"resource",
")",
":",
"# type: (Union[hdx.data.resource.Resource,Dict,str,int]) -> bool",
"if",
"isinstance",
"(",
"resource",
",",
"int",
")",
"and",
"not",
"isinstance",
"(",
"resource",
",",
"bool",
")",
":",
"resource",
"=",
"self",
".",
"get_resources",
"(",
")",
"[",
"resource",
"]",
"if",
"isinstance",
"(",
"resource",
",",
"hdx",
".",
"data",
".",
"resource",
".",
"Resource",
")",
"or",
"isinstance",
"(",
"resource",
",",
"dict",
")",
":",
"res",
"=",
"resource",
".",
"get",
"(",
"'id'",
")",
"if",
"res",
"is",
"None",
":",
"resource",
"=",
"resource",
"[",
"'name'",
"]",
"else",
":",
"resource",
"=",
"res",
"elif",
"not",
"isinstance",
"(",
"resource",
",",
"str",
")",
":",
"raise",
"hdx",
".",
"data",
".",
"hdxobject",
".",
"HDXError",
"(",
"'Resource id cannot be found in type %s!'",
"%",
"type",
"(",
"resource",
")",
".",
"__name__",
")",
"if",
"is_valid_uuid",
"(",
"resource",
")",
"is",
"True",
":",
"search",
"=",
"'id'",
"else",
":",
"search",
"=",
"'name'",
"changed",
"=",
"False",
"for",
"dataset_resource",
"in",
"self",
".",
"resources",
":",
"if",
"dataset_resource",
"[",
"search",
"]",
"==",
"resource",
":",
"dataset_resource",
".",
"enable_dataset_preview",
"(",
")",
"self",
".",
"preview_resource",
"(",
")",
"changed",
"=",
"True",
"else",
":",
"dataset_resource",
".",
"disable_dataset_preview",
"(",
")",
"return",
"changed"
] |
Set the resource that will be used for displaying QuickCharts in dataset preview
Args:
resource (Union[hdx.data.resource.Resource,Dict,str,int]): Either resource id or name, resource metadata from a Resource object or a dictionary or position
Returns:
bool: Returns True if resource for QuickCharts in dataset preview set or False if not
|
[
"Set",
"the",
"resource",
"that",
"will",
"be",
"used",
"for",
"displaying",
"QuickCharts",
"in",
"dataset",
"preview"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1556-L1588
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/dataset.py
|
Dataset.create_default_views
|
def create_default_views(self, create_datastore_views=False):
# type: (bool) -> None
"""Create default resource views for all resources in dataset
Args:
create_datastore_views (bool): Whether to try to create resource views that point to the datastore
Returns:
None
"""
package = deepcopy(self.data)
if self.resources:
package['resources'] = self._convert_hdxobjects(self.resources)
data = {'package': package, 'create_datastore_views': create_datastore_views}
self._write_to_hdx('create_default_views', data, 'package')
|
python
|
def create_default_views(self, create_datastore_views=False):
# type: (bool) -> None
"""Create default resource views for all resources in dataset
Args:
create_datastore_views (bool): Whether to try to create resource views that point to the datastore
Returns:
None
"""
package = deepcopy(self.data)
if self.resources:
package['resources'] = self._convert_hdxobjects(self.resources)
data = {'package': package, 'create_datastore_views': create_datastore_views}
self._write_to_hdx('create_default_views', data, 'package')
|
[
"def",
"create_default_views",
"(",
"self",
",",
"create_datastore_views",
"=",
"False",
")",
":",
"# type: (bool) -> None",
"package",
"=",
"deepcopy",
"(",
"self",
".",
"data",
")",
"if",
"self",
".",
"resources",
":",
"package",
"[",
"'resources'",
"]",
"=",
"self",
".",
"_convert_hdxobjects",
"(",
"self",
".",
"resources",
")",
"data",
"=",
"{",
"'package'",
":",
"package",
",",
"'create_datastore_views'",
":",
"create_datastore_views",
"}",
"self",
".",
"_write_to_hdx",
"(",
"'create_default_views'",
",",
"data",
",",
"'package'",
")"
] |
Create default resource views for all resources in dataset
Args:
create_datastore_views (bool): Whether to try to create resource views that point to the datastore
Returns:
None
|
[
"Create",
"default",
"resource",
"views",
"for",
"all",
"resources",
"in",
"dataset"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/dataset.py#L1590-L1605
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration._get_credentials
|
def _get_credentials(self):
# type: () -> Optional[Tuple[str, str]]
"""
Return HDX site username and password
Returns:
Optional[Tuple[str, str]]: HDX site username and password or None
"""
site = self.data[self.hdx_site]
username = site.get('username')
if username:
return b64decode(username).decode('utf-8'), b64decode(site['password']).decode('utf-8')
else:
return None
|
python
|
def _get_credentials(self):
# type: () -> Optional[Tuple[str, str]]
"""
Return HDX site username and password
Returns:
Optional[Tuple[str, str]]: HDX site username and password or None
"""
site = self.data[self.hdx_site]
username = site.get('username')
if username:
return b64decode(username).decode('utf-8'), b64decode(site['password']).decode('utf-8')
else:
return None
|
[
"def",
"_get_credentials",
"(",
"self",
")",
":",
"# type: () -> Optional[Tuple[str, str]]",
"site",
"=",
"self",
".",
"data",
"[",
"self",
".",
"hdx_site",
"]",
"username",
"=",
"site",
".",
"get",
"(",
"'username'",
")",
"if",
"username",
":",
"return",
"b64decode",
"(",
"username",
")",
".",
"decode",
"(",
"'utf-8'",
")",
",",
"b64decode",
"(",
"site",
"[",
"'password'",
"]",
")",
".",
"decode",
"(",
"'utf-8'",
")",
"else",
":",
"return",
"None"
] |
Return HDX site username and password
Returns:
Optional[Tuple[str, str]]: HDX site username and password or None
|
[
"Return",
"HDX",
"site",
"username",
"and",
"password"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L244-L258
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration.call_remoteckan
|
def call_remoteckan(self, *args, **kwargs):
# type: (Any, Any) -> Dict
"""
Calls the remote CKAN
Args:
*args: Arguments to pass to remote CKAN call_action method
**kwargs: Keyword arguments to pass to remote CKAN call_action method
Returns:
Dict: The response from the remote CKAN call_action method
"""
requests_kwargs = kwargs.get('requests_kwargs', dict())
credentials = self._get_credentials()
if credentials:
requests_kwargs['auth'] = credentials
kwargs['requests_kwargs'] = requests_kwargs
apikey = kwargs.get('apikey', self.get_api_key())
kwargs['apikey'] = apikey
return self.remoteckan().call_action(*args, **kwargs)
|
python
|
def call_remoteckan(self, *args, **kwargs):
# type: (Any, Any) -> Dict
"""
Calls the remote CKAN
Args:
*args: Arguments to pass to remote CKAN call_action method
**kwargs: Keyword arguments to pass to remote CKAN call_action method
Returns:
Dict: The response from the remote CKAN call_action method
"""
requests_kwargs = kwargs.get('requests_kwargs', dict())
credentials = self._get_credentials()
if credentials:
requests_kwargs['auth'] = credentials
kwargs['requests_kwargs'] = requests_kwargs
apikey = kwargs.get('apikey', self.get_api_key())
kwargs['apikey'] = apikey
return self.remoteckan().call_action(*args, **kwargs)
|
[
"def",
"call_remoteckan",
"(",
"self",
",",
"*",
"args",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Any, Any) -> Dict",
"requests_kwargs",
"=",
"kwargs",
".",
"get",
"(",
"'requests_kwargs'",
",",
"dict",
"(",
")",
")",
"credentials",
"=",
"self",
".",
"_get_credentials",
"(",
")",
"if",
"credentials",
":",
"requests_kwargs",
"[",
"'auth'",
"]",
"=",
"credentials",
"kwargs",
"[",
"'requests_kwargs'",
"]",
"=",
"requests_kwargs",
"apikey",
"=",
"kwargs",
".",
"get",
"(",
"'apikey'",
",",
"self",
".",
"get_api_key",
"(",
")",
")",
"kwargs",
"[",
"'apikey'",
"]",
"=",
"apikey",
"return",
"self",
".",
"remoteckan",
"(",
")",
".",
"call_action",
"(",
"*",
"args",
",",
"*",
"*",
"kwargs",
")"
] |
Calls the remote CKAN
Args:
*args: Arguments to pass to remote CKAN call_action method
**kwargs: Keyword arguments to pass to remote CKAN call_action method
Returns:
Dict: The response from the remote CKAN call_action method
|
[
"Calls",
"the",
"remote",
"CKAN"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L286-L306
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration._environment_variables
|
def _environment_variables(**kwargs):
# type: (Any) -> Any
"""
Overwrite keyword arguments with environment variables
Args:
**kwargs: See below
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test. Defaults to test.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
Returns:
kwargs: Changed keyword arguments
"""
hdx_key = os.getenv('HDX_KEY')
if hdx_key is not None:
kwargs['hdx_key'] = hdx_key
hdx_url = os.getenv('HDX_URL')
if hdx_url is not None:
kwargs['hdx_url'] = hdx_url
else:
hdx_site = os.getenv('HDX_SITE')
if hdx_site is not None:
kwargs['hdx_site'] = hdx_site
return kwargs
|
python
|
def _environment_variables(**kwargs):
# type: (Any) -> Any
"""
Overwrite keyword arguments with environment variables
Args:
**kwargs: See below
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test. Defaults to test.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
Returns:
kwargs: Changed keyword arguments
"""
hdx_key = os.getenv('HDX_KEY')
if hdx_key is not None:
kwargs['hdx_key'] = hdx_key
hdx_url = os.getenv('HDX_URL')
if hdx_url is not None:
kwargs['hdx_url'] = hdx_url
else:
hdx_site = os.getenv('HDX_SITE')
if hdx_site is not None:
kwargs['hdx_site'] = hdx_site
return kwargs
|
[
"def",
"_environment_variables",
"(",
"*",
"*",
"kwargs",
")",
":",
"# type: (Any) -> Any",
"hdx_key",
"=",
"os",
".",
"getenv",
"(",
"'HDX_KEY'",
")",
"if",
"hdx_key",
"is",
"not",
"None",
":",
"kwargs",
"[",
"'hdx_key'",
"]",
"=",
"hdx_key",
"hdx_url",
"=",
"os",
".",
"getenv",
"(",
"'HDX_URL'",
")",
"if",
"hdx_url",
"is",
"not",
"None",
":",
"kwargs",
"[",
"'hdx_url'",
"]",
"=",
"hdx_url",
"else",
":",
"hdx_site",
"=",
"os",
".",
"getenv",
"(",
"'HDX_SITE'",
")",
"if",
"hdx_site",
"is",
"not",
"None",
":",
"kwargs",
"[",
"'hdx_site'",
"]",
"=",
"hdx_site",
"return",
"kwargs"
] |
Overwrite keyword arguments with environment variables
Args:
**kwargs: See below
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test. Defaults to test.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
Returns:
kwargs: Changed keyword arguments
|
[
"Overwrite",
"keyword",
"arguments",
"with",
"environment",
"variables"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L309-L335
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration.create_remoteckan
|
def create_remoteckan(cls, site_url, user_agent=None, user_agent_config_yaml=None, user_agent_lookup=None,
session=None, **kwargs):
# type: (str, Optional[str], Optional[str], Optional[str], requests.Session, Any) -> ckanapi.RemoteCKAN
"""
Create remote CKAN instance from configuration
Args:
site_url (str): Site url.
user_agent (Optional[str]): User agent string. HDXPythonLibrary/X.X.X- is prefixed.
user_agent_config_yaml (Optional[str]): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (Optional[str]): Lookup key for YAML. Ignored if user_agent supplied.
session (requests.Session): requests Session object to use. Defaults to calling hdx.utilities.session.get_session()
Returns:
ckanapi.RemoteCKAN: Remote CKAN instance
"""
if not session:
session = get_session(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,
method_whitelist=frozenset(['HEAD', 'TRACE', 'GET', 'POST', 'PUT',
'OPTIONS', 'DELETE']), **kwargs)
ua = session.headers['User-Agent']
else:
ua = kwargs.get('full_agent')
if not ua:
ua = UserAgent.get(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,
**kwargs)
return ckanapi.RemoteCKAN(site_url, user_agent=ua, session=session)
|
python
|
def create_remoteckan(cls, site_url, user_agent=None, user_agent_config_yaml=None, user_agent_lookup=None,
session=None, **kwargs):
# type: (str, Optional[str], Optional[str], Optional[str], requests.Session, Any) -> ckanapi.RemoteCKAN
"""
Create remote CKAN instance from configuration
Args:
site_url (str): Site url.
user_agent (Optional[str]): User agent string. HDXPythonLibrary/X.X.X- is prefixed.
user_agent_config_yaml (Optional[str]): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (Optional[str]): Lookup key for YAML. Ignored if user_agent supplied.
session (requests.Session): requests Session object to use. Defaults to calling hdx.utilities.session.get_session()
Returns:
ckanapi.RemoteCKAN: Remote CKAN instance
"""
if not session:
session = get_session(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,
method_whitelist=frozenset(['HEAD', 'TRACE', 'GET', 'POST', 'PUT',
'OPTIONS', 'DELETE']), **kwargs)
ua = session.headers['User-Agent']
else:
ua = kwargs.get('full_agent')
if not ua:
ua = UserAgent.get(user_agent, user_agent_config_yaml, user_agent_lookup, prefix=Configuration.prefix,
**kwargs)
return ckanapi.RemoteCKAN(site_url, user_agent=ua, session=session)
|
[
"def",
"create_remoteckan",
"(",
"cls",
",",
"site_url",
",",
"user_agent",
"=",
"None",
",",
"user_agent_config_yaml",
"=",
"None",
",",
"user_agent_lookup",
"=",
"None",
",",
"session",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (str, Optional[str], Optional[str], Optional[str], requests.Session, Any) -> ckanapi.RemoteCKAN",
"if",
"not",
"session",
":",
"session",
"=",
"get_session",
"(",
"user_agent",
",",
"user_agent_config_yaml",
",",
"user_agent_lookup",
",",
"prefix",
"=",
"Configuration",
".",
"prefix",
",",
"method_whitelist",
"=",
"frozenset",
"(",
"[",
"'HEAD'",
",",
"'TRACE'",
",",
"'GET'",
",",
"'POST'",
",",
"'PUT'",
",",
"'OPTIONS'",
",",
"'DELETE'",
"]",
")",
",",
"*",
"*",
"kwargs",
")",
"ua",
"=",
"session",
".",
"headers",
"[",
"'User-Agent'",
"]",
"else",
":",
"ua",
"=",
"kwargs",
".",
"get",
"(",
"'full_agent'",
")",
"if",
"not",
"ua",
":",
"ua",
"=",
"UserAgent",
".",
"get",
"(",
"user_agent",
",",
"user_agent_config_yaml",
",",
"user_agent_lookup",
",",
"prefix",
"=",
"Configuration",
".",
"prefix",
",",
"*",
"*",
"kwargs",
")",
"return",
"ckanapi",
".",
"RemoteCKAN",
"(",
"site_url",
",",
"user_agent",
"=",
"ua",
",",
"session",
"=",
"session",
")"
] |
Create remote CKAN instance from configuration
Args:
site_url (str): Site url.
user_agent (Optional[str]): User agent string. HDXPythonLibrary/X.X.X- is prefixed.
user_agent_config_yaml (Optional[str]): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (Optional[str]): Lookup key for YAML. Ignored if user_agent supplied.
session (requests.Session): requests Session object to use. Defaults to calling hdx.utilities.session.get_session()
Returns:
ckanapi.RemoteCKAN: Remote CKAN instance
|
[
"Create",
"remote",
"CKAN",
"instance",
"from",
"configuration"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L338-L365
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration.setup_remoteckan
|
def setup_remoteckan(self, remoteckan=None, **kwargs):
# type: (Optional[ckanapi.RemoteCKAN], Any) -> None
"""
Set up remote CKAN from provided CKAN or by creating from configuration
Args:
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
Returns:
None
"""
if remoteckan is None:
self._remoteckan = self.create_remoteckan(self.get_hdx_site_url(), full_agent=self.get_user_agent(),
**kwargs)
else:
self._remoteckan = remoteckan
|
python
|
def setup_remoteckan(self, remoteckan=None, **kwargs):
# type: (Optional[ckanapi.RemoteCKAN], Any) -> None
"""
Set up remote CKAN from provided CKAN or by creating from configuration
Args:
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
Returns:
None
"""
if remoteckan is None:
self._remoteckan = self.create_remoteckan(self.get_hdx_site_url(), full_agent=self.get_user_agent(),
**kwargs)
else:
self._remoteckan = remoteckan
|
[
"def",
"setup_remoteckan",
"(",
"self",
",",
"remoteckan",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional[ckanapi.RemoteCKAN], Any) -> None",
"if",
"remoteckan",
"is",
"None",
":",
"self",
".",
"_remoteckan",
"=",
"self",
".",
"create_remoteckan",
"(",
"self",
".",
"get_hdx_site_url",
"(",
")",
",",
"full_agent",
"=",
"self",
".",
"get_user_agent",
"(",
")",
",",
"*",
"*",
"kwargs",
")",
"else",
":",
"self",
".",
"_remoteckan",
"=",
"remoteckan"
] |
Set up remote CKAN from provided CKAN or by creating from configuration
Args:
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
Returns:
None
|
[
"Set",
"up",
"remote",
"CKAN",
"from",
"provided",
"CKAN",
"or",
"by",
"creating",
"from",
"configuration"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L367-L383
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration.setup
|
def setup(cls, configuration=None, **kwargs):
# type: (Optional['Configuration'], Any) -> None
"""
Set up the HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
None
"""
if configuration is None:
cls._configuration = Configuration(**kwargs)
else:
cls._configuration = configuration
|
python
|
def setup(cls, configuration=None, **kwargs):
# type: (Optional['Configuration'], Any) -> None
"""
Set up the HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
None
"""
if configuration is None:
cls._configuration = Configuration(**kwargs)
else:
cls._configuration = configuration
|
[
"def",
"setup",
"(",
"cls",
",",
"configuration",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional['Configuration'], Any) -> None",
"if",
"configuration",
"is",
"None",
":",
"cls",
".",
"_configuration",
"=",
"Configuration",
"(",
"*",
"*",
"kwargs",
")",
"else",
":",
"cls",
".",
"_configuration",
"=",
"configuration"
] |
Set up the HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
None
|
[
"Set",
"up",
"the",
"HDX",
"configuration"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L436-L468
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration._create
|
def _create(cls, configuration=None, remoteckan=None, **kwargs):
# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str
"""
Create HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
"""
kwargs = cls._environment_variables(**kwargs)
cls.setup(configuration, **kwargs)
cls._configuration.setup_remoteckan(remoteckan, **kwargs)
return cls._configuration.get_hdx_site_url()
|
python
|
def _create(cls, configuration=None, remoteckan=None, **kwargs):
# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str
"""
Create HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
"""
kwargs = cls._environment_variables(**kwargs)
cls.setup(configuration, **kwargs)
cls._configuration.setup_remoteckan(remoteckan, **kwargs)
return cls._configuration.get_hdx_site_url()
|
[
"def",
"_create",
"(",
"cls",
",",
"configuration",
"=",
"None",
",",
"remoteckan",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str",
"kwargs",
"=",
"cls",
".",
"_environment_variables",
"(",
"*",
"*",
"kwargs",
")",
"cls",
".",
"setup",
"(",
"configuration",
",",
"*",
"*",
"kwargs",
")",
"cls",
".",
"_configuration",
".",
"setup_remoteckan",
"(",
"remoteckan",
",",
"*",
"*",
"kwargs",
")",
"return",
"cls",
".",
"_configuration",
".",
"get_hdx_site_url",
"(",
")"
] |
Create HDX configuration
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
|
[
"Create",
"HDX",
"configuration"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L471-L504
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_configuration.py
|
Configuration.create
|
def create(cls, configuration=None, remoteckan=None, **kwargs):
# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str
"""
Create HDX configuration. Can only be called once (will raise an error if called more than once).
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
"""
if cls._configuration is not None:
raise ConfigurationError('Configuration already created!')
return cls._create(configuration=configuration, remoteckan=remoteckan, **kwargs)
|
python
|
def create(cls, configuration=None, remoteckan=None, **kwargs):
# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str
"""
Create HDX configuration. Can only be called once (will raise an error if called more than once).
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
"""
if cls._configuration is not None:
raise ConfigurationError('Configuration already created!')
return cls._create(configuration=configuration, remoteckan=remoteckan, **kwargs)
|
[
"def",
"create",
"(",
"cls",
",",
"configuration",
"=",
"None",
",",
"remoteckan",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional['Configuration'], Optional[ckanapi.RemoteCKAN], Any) -> str",
"if",
"cls",
".",
"_configuration",
"is",
"not",
"None",
":",
"raise",
"ConfigurationError",
"(",
"'Configuration already created!'",
")",
"return",
"cls",
".",
"_create",
"(",
"configuration",
"=",
"configuration",
",",
"remoteckan",
"=",
"remoteckan",
",",
"*",
"*",
"kwargs",
")"
] |
Create HDX configuration. Can only be called once (will raise an error if called more than once).
Args:
configuration (Optional[Configuration]): Configuration instance. Defaults to setting one up from passed arguments.
remoteckan (Optional[ckanapi.RemoteCKAN]): CKAN instance. Defaults to setting one up from configuration.
**kwargs: See below
user_agent (str): User agent string. HDXPythonLibrary/X.X.X- is prefixed. Must be supplied if remoteckan is not.
user_agent_config_yaml (str): Path to YAML user agent configuration. Ignored if user_agent supplied. Defaults to ~/.useragent.yml.
user_agent_lookup (str): Lookup key for YAML. Ignored if user_agent supplied.
hdx_url (str): HDX url to use. Overrides hdx_site.
hdx_site (str): HDX site to use eg. prod, test.
hdx_read_only (bool): Whether to access HDX in read only mode. Defaults to False.
hdx_key (str): Your HDX key. Ignored if hdx_read_only = True.
hdx_config_dict (dict): HDX configuration dictionary to use instead of above 3 parameters OR
hdx_config_json (str): Path to JSON HDX configuration OR
hdx_config_yaml (str): Path to YAML HDX configuration
project_config_dict (dict): Project configuration dictionary OR
project_config_json (str): Path to JSON Project configuration OR
project_config_yaml (str): Path to YAML Project configuration
hdx_base_config_dict (dict): HDX base configuration dictionary OR
hdx_base_config_json (str): Path to JSON HDX base configuration OR
hdx_base_config_yaml (str): Path to YAML HDX base configuration. Defaults to library's internal hdx_base_configuration.yml.
Returns:
str: HDX site url
|
[
"Create",
"HDX",
"configuration",
".",
"Can",
"only",
"be",
"called",
"once",
"(",
"will",
"raise",
"an",
"error",
"if",
"called",
"more",
"than",
"once",
")",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_configuration.py#L507-L539
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
kwargs_to_variable_assignment
|
def kwargs_to_variable_assignment(kwargs: dict, value_representation=repr,
assignment_operator: str = ' = ',
statement_separator: str = '\n',
statement_per_line: bool = False) -> str:
"""
Convert a dictionary into a string with assignments
Each assignment is constructed based on:
key assignment_operator value_representation(value) statement_separator,
where key and value are the key and value of the dictionary.
Moreover one can seprate the assignment statements by new lines.
Parameters
----------
kwargs : dict
assignment_operator: str, optional:
Assignment operator (" = " in python)
value_representation: str, optinal
How to represent the value in the assignments (repr function in python)
statement_separator : str, optional:
Statement separator (new line in python)
statement_per_line: bool, optional
Insert each statement on a different line
Returns
-------
str
All the assignemnts.
>>> kwargs_to_variable_assignment({'a': 2, 'b': "abc"})
"a = 2\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a':2 ,'b': "abc"}, statement_per_line=True)
"a = 2\\n\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a': 2})
'a = 2\\n'
>>> kwargs_to_variable_assignment({'a': 2}, statement_per_line=True)
'a = 2\\n'
"""
code = []
join_str = '\n' if statement_per_line else ''
for key, value in kwargs.items():
code.append(key + assignment_operator +
value_representation(value)+statement_separator)
return join_str.join(code)
|
python
|
def kwargs_to_variable_assignment(kwargs: dict, value_representation=repr,
assignment_operator: str = ' = ',
statement_separator: str = '\n',
statement_per_line: bool = False) -> str:
"""
Convert a dictionary into a string with assignments
Each assignment is constructed based on:
key assignment_operator value_representation(value) statement_separator,
where key and value are the key and value of the dictionary.
Moreover one can seprate the assignment statements by new lines.
Parameters
----------
kwargs : dict
assignment_operator: str, optional:
Assignment operator (" = " in python)
value_representation: str, optinal
How to represent the value in the assignments (repr function in python)
statement_separator : str, optional:
Statement separator (new line in python)
statement_per_line: bool, optional
Insert each statement on a different line
Returns
-------
str
All the assignemnts.
>>> kwargs_to_variable_assignment({'a': 2, 'b': "abc"})
"a = 2\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a':2 ,'b': "abc"}, statement_per_line=True)
"a = 2\\n\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a': 2})
'a = 2\\n'
>>> kwargs_to_variable_assignment({'a': 2}, statement_per_line=True)
'a = 2\\n'
"""
code = []
join_str = '\n' if statement_per_line else ''
for key, value in kwargs.items():
code.append(key + assignment_operator +
value_representation(value)+statement_separator)
return join_str.join(code)
|
[
"def",
"kwargs_to_variable_assignment",
"(",
"kwargs",
":",
"dict",
",",
"value_representation",
"=",
"repr",
",",
"assignment_operator",
":",
"str",
"=",
"' = '",
",",
"statement_separator",
":",
"str",
"=",
"'\\n'",
",",
"statement_per_line",
":",
"bool",
"=",
"False",
")",
"->",
"str",
":",
"code",
"=",
"[",
"]",
"join_str",
"=",
"'\\n'",
"if",
"statement_per_line",
"else",
"''",
"for",
"key",
",",
"value",
"in",
"kwargs",
".",
"items",
"(",
")",
":",
"code",
".",
"append",
"(",
"key",
"+",
"assignment_operator",
"+",
"value_representation",
"(",
"value",
")",
"+",
"statement_separator",
")",
"return",
"join_str",
".",
"join",
"(",
"code",
")"
] |
Convert a dictionary into a string with assignments
Each assignment is constructed based on:
key assignment_operator value_representation(value) statement_separator,
where key and value are the key and value of the dictionary.
Moreover one can seprate the assignment statements by new lines.
Parameters
----------
kwargs : dict
assignment_operator: str, optional:
Assignment operator (" = " in python)
value_representation: str, optinal
How to represent the value in the assignments (repr function in python)
statement_separator : str, optional:
Statement separator (new line in python)
statement_per_line: bool, optional
Insert each statement on a different line
Returns
-------
str
All the assignemnts.
>>> kwargs_to_variable_assignment({'a': 2, 'b': "abc"})
"a = 2\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a':2 ,'b': "abc"}, statement_per_line=True)
"a = 2\\n\\nb = 'abc'\\n"
>>> kwargs_to_variable_assignment({'a': 2})
'a = 2\\n'
>>> kwargs_to_variable_assignment({'a': 2}, statement_per_line=True)
'a = 2\\n'
|
[
"Convert",
"a",
"dictionary",
"into",
"a",
"string",
"with",
"assignments"
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L12-L56
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
decode_json
|
def decode_json(json_input: Union[str, None] = None):
"""
Simple wrapper of json.load and json.loads.
If json_input is None the output is an empty dictionary.
If the input is a string that ends in .json it is decoded using json.load.
Otherwise it is decoded using json.loads.
Parameters
----------
json_input : str, None, optional
input json object
Returns
-------
Decoded json object
>>> decode_json()
{}
>>> decode_json('{"flag":true}')
{'flag': True}
>>> decode_json('{"value":null}')
{'value': None}
"""
if json_input is None:
return {}
else:
if isinstance(json_input, str) is False:
raise TypeError()
elif json_input[-5:] == ".json":
with open(json_input) as f:
decoded_json = json.load(f)
else:
decoded_json = json.loads(json_input)
return decoded_json
|
python
|
def decode_json(json_input: Union[str, None] = None):
"""
Simple wrapper of json.load and json.loads.
If json_input is None the output is an empty dictionary.
If the input is a string that ends in .json it is decoded using json.load.
Otherwise it is decoded using json.loads.
Parameters
----------
json_input : str, None, optional
input json object
Returns
-------
Decoded json object
>>> decode_json()
{}
>>> decode_json('{"flag":true}')
{'flag': True}
>>> decode_json('{"value":null}')
{'value': None}
"""
if json_input is None:
return {}
else:
if isinstance(json_input, str) is False:
raise TypeError()
elif json_input[-5:] == ".json":
with open(json_input) as f:
decoded_json = json.load(f)
else:
decoded_json = json.loads(json_input)
return decoded_json
|
[
"def",
"decode_json",
"(",
"json_input",
":",
"Union",
"[",
"str",
",",
"None",
"]",
"=",
"None",
")",
":",
"if",
"json_input",
"is",
"None",
":",
"return",
"{",
"}",
"else",
":",
"if",
"isinstance",
"(",
"json_input",
",",
"str",
")",
"is",
"False",
":",
"raise",
"TypeError",
"(",
")",
"elif",
"json_input",
"[",
"-",
"5",
":",
"]",
"==",
"\".json\"",
":",
"with",
"open",
"(",
"json_input",
")",
"as",
"f",
":",
"decoded_json",
"=",
"json",
".",
"load",
"(",
"f",
")",
"else",
":",
"decoded_json",
"=",
"json",
".",
"loads",
"(",
"json_input",
")",
"return",
"decoded_json"
] |
Simple wrapper of json.load and json.loads.
If json_input is None the output is an empty dictionary.
If the input is a string that ends in .json it is decoded using json.load.
Otherwise it is decoded using json.loads.
Parameters
----------
json_input : str, None, optional
input json object
Returns
-------
Decoded json object
>>> decode_json()
{}
>>> decode_json('{"flag":true}')
{'flag': True}
>>> decode_json('{"value":null}')
{'value': None}
|
[
"Simple",
"wrapper",
"of",
"json",
".",
"load",
"and",
"json",
".",
"loads",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L69-L104
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
is_jsonable
|
def is_jsonable(obj) -> bool:
"""
Check if an object is jsonable.
An object is jsonable if it is json serialisable and by loading its json representation the same object is recovered.
Parameters
----------
obj :
Python object
Returns
-------
bool
>>> is_jsonable([1,2,3])
True
>>> is_jsonable((1,2,3))
False
>>> is_jsonable({'a':True,'b':1,'c':None})
True
"""
try:
return obj==json.loads(json.dumps(obj))
except TypeError:
return False
except:
raise
|
python
|
def is_jsonable(obj) -> bool:
"""
Check if an object is jsonable.
An object is jsonable if it is json serialisable and by loading its json representation the same object is recovered.
Parameters
----------
obj :
Python object
Returns
-------
bool
>>> is_jsonable([1,2,3])
True
>>> is_jsonable((1,2,3))
False
>>> is_jsonable({'a':True,'b':1,'c':None})
True
"""
try:
return obj==json.loads(json.dumps(obj))
except TypeError:
return False
except:
raise
|
[
"def",
"is_jsonable",
"(",
"obj",
")",
"->",
"bool",
":",
"try",
":",
"return",
"obj",
"==",
"json",
".",
"loads",
"(",
"json",
".",
"dumps",
"(",
"obj",
")",
")",
"except",
"TypeError",
":",
"return",
"False",
"except",
":",
"raise"
] |
Check if an object is jsonable.
An object is jsonable if it is json serialisable and by loading its json representation the same object is recovered.
Parameters
----------
obj :
Python object
Returns
-------
bool
>>> is_jsonable([1,2,3])
True
>>> is_jsonable((1,2,3))
False
>>> is_jsonable({'a':True,'b':1,'c':None})
True
|
[
"Check",
"if",
"an",
"object",
"is",
"jsonable",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L107-L133
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
is_literal_eval
|
def is_literal_eval(node_or_string) -> tuple:
"""
Check if an expresion can be literal_eval.
----------
node_or_string :
Input
Returns
-------
tuple
(bool,python object)
If it can be literal_eval the python object is returned. Otherwise None it is returned.
>>> is_literal_eval('[1,2,3]')
(True, [1, 2, 3])
>>> is_literal_eval('a')
(False, None)
"""
try:
obj=ast.literal_eval(node_or_string)
return (True, obj)
except:
return (False, None)
|
python
|
def is_literal_eval(node_or_string) -> tuple:
"""
Check if an expresion can be literal_eval.
----------
node_or_string :
Input
Returns
-------
tuple
(bool,python object)
If it can be literal_eval the python object is returned. Otherwise None it is returned.
>>> is_literal_eval('[1,2,3]')
(True, [1, 2, 3])
>>> is_literal_eval('a')
(False, None)
"""
try:
obj=ast.literal_eval(node_or_string)
return (True, obj)
except:
return (False, None)
|
[
"def",
"is_literal_eval",
"(",
"node_or_string",
")",
"->",
"tuple",
":",
"try",
":",
"obj",
"=",
"ast",
".",
"literal_eval",
"(",
"node_or_string",
")",
"return",
"(",
"True",
",",
"obj",
")",
"except",
":",
"return",
"(",
"False",
",",
"None",
")"
] |
Check if an expresion can be literal_eval.
----------
node_or_string :
Input
Returns
-------
tuple
(bool,python object)
If it can be literal_eval the python object is returned. Otherwise None it is returned.
>>> is_literal_eval('[1,2,3]')
(True, [1, 2, 3])
>>> is_literal_eval('a')
(False, None)
|
[
"Check",
"if",
"an",
"expresion",
"can",
"be",
"literal_eval",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L136-L161
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
find_duplicates
|
def find_duplicates(l: list) -> set:
"""
Return the duplicates in a list.
The function relies on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
l : list
Name
Returns
-------
set
Duplicated values
>>> find_duplicates([1,2,3])
set()
>>> find_duplicates([1,2,1])
{1}
"""
return set([x for x in l if l.count(x) > 1])
|
python
|
def find_duplicates(l: list) -> set:
"""
Return the duplicates in a list.
The function relies on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
l : list
Name
Returns
-------
set
Duplicated values
>>> find_duplicates([1,2,3])
set()
>>> find_duplicates([1,2,1])
{1}
"""
return set([x for x in l if l.count(x) > 1])
|
[
"def",
"find_duplicates",
"(",
"l",
":",
"list",
")",
"->",
"set",
":",
"return",
"set",
"(",
"[",
"x",
"for",
"x",
"in",
"l",
"if",
"l",
".",
"count",
"(",
"x",
")",
">",
"1",
"]",
")"
] |
Return the duplicates in a list.
The function relies on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
l : list
Name
Returns
-------
set
Duplicated values
>>> find_duplicates([1,2,3])
set()
>>> find_duplicates([1,2,1])
{1}
|
[
"Return",
"the",
"duplicates",
"in",
"a",
"list",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L164-L185
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
sort_dict
|
def sort_dict(d: dict, by: str = 'key',
allow_duplicates: bool = True) -> collections.OrderedDict:
"""
Sort a dictionary by key or value.
The function relies on
https://docs.python.org/3/library/collections.html#collections.OrderedDict .
The dulicated are determined based on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
d : dict
Input dictionary
by : ['key','value'], optional
By what to sort the input dictionary
allow_duplicates : bool, optional
Flag to indicate if the duplicates are allowed.
Returns
-------
collections.OrderedDict
Sorted dictionary.
>>> sort_dict({2: 3, 1: 2, 3: 1})
OrderedDict([(1, 2), (2, 3), (3, 1)])
>>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')
OrderedDict([(3, 1), (1, 2), (2, 3)])
>>> sort_dict({'2': 3, '1': 2}, by='value')
OrderedDict([('1', 2), ('2', 3)])
>>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)
Traceback (most recent call last):
...
ValueError: There are duplicates in the values: {1}
>>> sort_dict({1:1,2:3},by=True)
Traceback (most recent call last):
...
ValueError: by can be 'key' or 'value'.
"""
if by == 'key':
i = 0
elif by == 'value':
values = list(d.values())
if len(values) != len(set(values)) and not allow_duplicates:
duplicates = find_duplicates(values)
raise ValueError("There are duplicates in the values: {}".format(duplicates))
i = 1
else:
raise ValueError("by can be 'key' or 'value'.")
return collections.OrderedDict(sorted(d.items(), key=lambda t: t[i]))
|
python
|
def sort_dict(d: dict, by: str = 'key',
allow_duplicates: bool = True) -> collections.OrderedDict:
"""
Sort a dictionary by key or value.
The function relies on
https://docs.python.org/3/library/collections.html#collections.OrderedDict .
The dulicated are determined based on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
d : dict
Input dictionary
by : ['key','value'], optional
By what to sort the input dictionary
allow_duplicates : bool, optional
Flag to indicate if the duplicates are allowed.
Returns
-------
collections.OrderedDict
Sorted dictionary.
>>> sort_dict({2: 3, 1: 2, 3: 1})
OrderedDict([(1, 2), (2, 3), (3, 1)])
>>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')
OrderedDict([(3, 1), (1, 2), (2, 3)])
>>> sort_dict({'2': 3, '1': 2}, by='value')
OrderedDict([('1', 2), ('2', 3)])
>>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)
Traceback (most recent call last):
...
ValueError: There are duplicates in the values: {1}
>>> sort_dict({1:1,2:3},by=True)
Traceback (most recent call last):
...
ValueError: by can be 'key' or 'value'.
"""
if by == 'key':
i = 0
elif by == 'value':
values = list(d.values())
if len(values) != len(set(values)) and not allow_duplicates:
duplicates = find_duplicates(values)
raise ValueError("There are duplicates in the values: {}".format(duplicates))
i = 1
else:
raise ValueError("by can be 'key' or 'value'.")
return collections.OrderedDict(sorted(d.items(), key=lambda t: t[i]))
|
[
"def",
"sort_dict",
"(",
"d",
":",
"dict",
",",
"by",
":",
"str",
"=",
"'key'",
",",
"allow_duplicates",
":",
"bool",
"=",
"True",
")",
"->",
"collections",
".",
"OrderedDict",
":",
"if",
"by",
"==",
"'key'",
":",
"i",
"=",
"0",
"elif",
"by",
"==",
"'value'",
":",
"values",
"=",
"list",
"(",
"d",
".",
"values",
"(",
")",
")",
"if",
"len",
"(",
"values",
")",
"!=",
"len",
"(",
"set",
"(",
"values",
")",
")",
"and",
"not",
"allow_duplicates",
":",
"duplicates",
"=",
"find_duplicates",
"(",
"values",
")",
"raise",
"ValueError",
"(",
"\"There are duplicates in the values: {}\"",
".",
"format",
"(",
"duplicates",
")",
")",
"i",
"=",
"1",
"else",
":",
"raise",
"ValueError",
"(",
"\"by can be 'key' or 'value'.\"",
")",
"return",
"collections",
".",
"OrderedDict",
"(",
"sorted",
"(",
"d",
".",
"items",
"(",
")",
",",
"key",
"=",
"lambda",
"t",
":",
"t",
"[",
"i",
"]",
")",
")"
] |
Sort a dictionary by key or value.
The function relies on
https://docs.python.org/3/library/collections.html#collections.OrderedDict .
The dulicated are determined based on
https://stackoverflow.com/questions/9835762/find-and-list-duplicates-in-a-list .
Parameters
----------
d : dict
Input dictionary
by : ['key','value'], optional
By what to sort the input dictionary
allow_duplicates : bool, optional
Flag to indicate if the duplicates are allowed.
Returns
-------
collections.OrderedDict
Sorted dictionary.
>>> sort_dict({2: 3, 1: 2, 3: 1})
OrderedDict([(1, 2), (2, 3), (3, 1)])
>>> sort_dict({2: 3, 1: 2, 3: 1}, by='value')
OrderedDict([(3, 1), (1, 2), (2, 3)])
>>> sort_dict({'2': 3, '1': 2}, by='value')
OrderedDict([('1', 2), ('2', 3)])
>>> sort_dict({2: 1, 1: 2, 3: 1}, by='value', allow_duplicates=False)
Traceback (most recent call last):
...
ValueError: There are duplicates in the values: {1}
>>> sort_dict({1:1,2:3},by=True)
Traceback (most recent call last):
...
ValueError: by can be 'key' or 'value'.
|
[
"Sort",
"a",
"dictionary",
"by",
"key",
"or",
"value",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L188-L236
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
group_dict_by_value
|
def group_dict_by_value(d: dict) -> dict:
"""
Group a dictionary by values.
Parameters
----------
d : dict
Input dictionary
Returns
-------
dict
Output dictionary. The keys are the values of the initial dictionary
and the values ae given by a list of keys corresponding to the value.
>>> group_dict_by_value({2: 3, 1: 2, 3: 1})
{3: [2], 2: [1], 1: [3]}
>>> group_dict_by_value({2: 3, 1: 2, 3: 1, 10:1, 12: 3})
{3: [2, 12], 2: [1], 1: [3, 10]}
"""
d_out = {}
for k, v in d.items():
if v in d_out:
d_out[v].append(k)
else:
d_out[v] = [k]
return d_out
|
python
|
def group_dict_by_value(d: dict) -> dict:
"""
Group a dictionary by values.
Parameters
----------
d : dict
Input dictionary
Returns
-------
dict
Output dictionary. The keys are the values of the initial dictionary
and the values ae given by a list of keys corresponding to the value.
>>> group_dict_by_value({2: 3, 1: 2, 3: 1})
{3: [2], 2: [1], 1: [3]}
>>> group_dict_by_value({2: 3, 1: 2, 3: 1, 10:1, 12: 3})
{3: [2, 12], 2: [1], 1: [3, 10]}
"""
d_out = {}
for k, v in d.items():
if v in d_out:
d_out[v].append(k)
else:
d_out[v] = [k]
return d_out
|
[
"def",
"group_dict_by_value",
"(",
"d",
":",
"dict",
")",
"->",
"dict",
":",
"d_out",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"d",
".",
"items",
"(",
")",
":",
"if",
"v",
"in",
"d_out",
":",
"d_out",
"[",
"v",
"]",
".",
"append",
"(",
"k",
")",
"else",
":",
"d_out",
"[",
"v",
"]",
"=",
"[",
"k",
"]",
"return",
"d_out"
] |
Group a dictionary by values.
Parameters
----------
d : dict
Input dictionary
Returns
-------
dict
Output dictionary. The keys are the values of the initial dictionary
and the values ae given by a list of keys corresponding to the value.
>>> group_dict_by_value({2: 3, 1: 2, 3: 1})
{3: [2], 2: [1], 1: [3]}
>>> group_dict_by_value({2: 3, 1: 2, 3: 1, 10:1, 12: 3})
{3: [2, 12], 2: [1], 1: [3, 10]}
|
[
"Group",
"a",
"dictionary",
"by",
"values",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L239-L266
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
variable_status
|
def variable_status(code: str,
exclude_variable: Union[set, None] = None,
jsonable_parameter: bool = True) -> tuple:
"""
Find the possible parameters and "global" variables from a python code.
This is achieved by parsing the abstract syntax tree.
Parameters
----------
code : str
Input code as string.
exclude_variable : set, None, optional
Variable to exclude.
jsonable_parameter: bool, True, optional
Consider only jsonable parameter
Returns
-------
tuple
(a set of possible parameter, a set of parameter to exclude, a dictionary of possible parameter )
A variable is a possible parameter if 1) it is not in the input exclude_variable,
2) the code contains only assignments, and 3) it is used only to bound objects.
The set of parameter to exclude is the union of the input exclude_variable and all names that looks like a global variable.
The dictionary of possible parameter {parameter name, parameter value} is available only if jsonable_parameter is True.
>>> variable_status("a=3")
({'a'}, {'a'}, {'a': 3})
>>> variable_status("a=3",jsonable_parameter=False)
({'a'}, {'a'}, {})
>>> variable_status("a += 1")
(set(), {'a'}, {})
>>> variable_status("def f(x,y=3):\\n\\t pass")
(set(), {'f'}, {})
>>> variable_status("class C(A):\\n\\t pass")
(set(), {'C'}, {})
>>> variable_status("import f")
(set(), {'f'}, {})
>>> variable_status("import f as g")
(set(), {'g'}, {})
>>> variable_status("from X import f")
(set(), {'f'}, {})
>>> variable_status("from X import f as g")
(set(), {'g'}, {})
"""
if exclude_variable is None:
exclude_variable = set()
else:
exclude_variable = copy.deepcopy(exclude_variable)
root = ast.parse(code)
store_variable_name = set()
assign_only = True
dict_parameter={}
for node in ast.iter_child_nodes(root):
if isinstance(node, ast.Assign):
for assign_node in ast.walk(node):
if isinstance(assign_node, ast.Name):
if isinstance(assign_node.ctx, ast.Store):
if jsonable_parameter is False:
store_variable_name |= {assign_node.id}
else:
exclude_variable |= {assign_node.id}
_is_literal_eval,_value=is_literal_eval(node.value)
if jsonable_parameter is True:
for assign_node in ast.iter_child_nodes(node):
if isinstance(assign_node, ast.Tuple):
i=0
for assign_tuple_node in ast.iter_child_nodes(assign_node):
if isinstance(assign_tuple_node, ast.Name):
if isinstance(_value,(collections.Iterable)) and is_jsonable(_value[i]) and _is_literal_eval:
dict_parameter[assign_tuple_node.id]=_value[i]
store_variable_name |= {assign_tuple_node.id}
else:
exclude_variable |= {assign_tuple_node.id}
i += 1
else:
if isinstance(assign_node, ast.Name):
if is_jsonable(_value) and _is_literal_eval:
dict_parameter[assign_node.id]=_value
store_variable_name |= {assign_node.id}
else:
exclude_variable |= {assign_node.id}
elif isinstance(node, ast.AugAssign):
for assign_node in ast.walk(node):
if isinstance(assign_node, ast.Name):
exclude_variable |= {assign_node.id}
# class and function
elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):
assign_only = False
exclude_variable |= {node.name}
# import
elif isinstance(node, ast.Import):
assign_only = False
for node1 in ast.iter_child_nodes(node):
if node1.asname is not None:
exclude_variable |= {node1.asname}
else:
exclude_variable |= {node1.name}
# import from
elif isinstance(node, ast.ImportFrom):
assign_only = False
for node1 in ast.iter_child_nodes(node):
if node1.asname is not None:
exclude_variable |= {node1.asname}
else:
exclude_variable |= {node1.name}
else:
assign_only = False
if assign_only is True:
possible_parameter = store_variable_name-exclude_variable
if jsonable_parameter is True:
dict_parameter = {k:dict_parameter[k] for k in possible_parameter}
return (possible_parameter, store_variable_name | exclude_variable, dict_parameter)
return set(), store_variable_name | exclude_variable, {}
|
python
|
def variable_status(code: str,
exclude_variable: Union[set, None] = None,
jsonable_parameter: bool = True) -> tuple:
"""
Find the possible parameters and "global" variables from a python code.
This is achieved by parsing the abstract syntax tree.
Parameters
----------
code : str
Input code as string.
exclude_variable : set, None, optional
Variable to exclude.
jsonable_parameter: bool, True, optional
Consider only jsonable parameter
Returns
-------
tuple
(a set of possible parameter, a set of parameter to exclude, a dictionary of possible parameter )
A variable is a possible parameter if 1) it is not in the input exclude_variable,
2) the code contains only assignments, and 3) it is used only to bound objects.
The set of parameter to exclude is the union of the input exclude_variable and all names that looks like a global variable.
The dictionary of possible parameter {parameter name, parameter value} is available only if jsonable_parameter is True.
>>> variable_status("a=3")
({'a'}, {'a'}, {'a': 3})
>>> variable_status("a=3",jsonable_parameter=False)
({'a'}, {'a'}, {})
>>> variable_status("a += 1")
(set(), {'a'}, {})
>>> variable_status("def f(x,y=3):\\n\\t pass")
(set(), {'f'}, {})
>>> variable_status("class C(A):\\n\\t pass")
(set(), {'C'}, {})
>>> variable_status("import f")
(set(), {'f'}, {})
>>> variable_status("import f as g")
(set(), {'g'}, {})
>>> variable_status("from X import f")
(set(), {'f'}, {})
>>> variable_status("from X import f as g")
(set(), {'g'}, {})
"""
if exclude_variable is None:
exclude_variable = set()
else:
exclude_variable = copy.deepcopy(exclude_variable)
root = ast.parse(code)
store_variable_name = set()
assign_only = True
dict_parameter={}
for node in ast.iter_child_nodes(root):
if isinstance(node, ast.Assign):
for assign_node in ast.walk(node):
if isinstance(assign_node, ast.Name):
if isinstance(assign_node.ctx, ast.Store):
if jsonable_parameter is False:
store_variable_name |= {assign_node.id}
else:
exclude_variable |= {assign_node.id}
_is_literal_eval,_value=is_literal_eval(node.value)
if jsonable_parameter is True:
for assign_node in ast.iter_child_nodes(node):
if isinstance(assign_node, ast.Tuple):
i=0
for assign_tuple_node in ast.iter_child_nodes(assign_node):
if isinstance(assign_tuple_node, ast.Name):
if isinstance(_value,(collections.Iterable)) and is_jsonable(_value[i]) and _is_literal_eval:
dict_parameter[assign_tuple_node.id]=_value[i]
store_variable_name |= {assign_tuple_node.id}
else:
exclude_variable |= {assign_tuple_node.id}
i += 1
else:
if isinstance(assign_node, ast.Name):
if is_jsonable(_value) and _is_literal_eval:
dict_parameter[assign_node.id]=_value
store_variable_name |= {assign_node.id}
else:
exclude_variable |= {assign_node.id}
elif isinstance(node, ast.AugAssign):
for assign_node in ast.walk(node):
if isinstance(assign_node, ast.Name):
exclude_variable |= {assign_node.id}
# class and function
elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):
assign_only = False
exclude_variable |= {node.name}
# import
elif isinstance(node, ast.Import):
assign_only = False
for node1 in ast.iter_child_nodes(node):
if node1.asname is not None:
exclude_variable |= {node1.asname}
else:
exclude_variable |= {node1.name}
# import from
elif isinstance(node, ast.ImportFrom):
assign_only = False
for node1 in ast.iter_child_nodes(node):
if node1.asname is not None:
exclude_variable |= {node1.asname}
else:
exclude_variable |= {node1.name}
else:
assign_only = False
if assign_only is True:
possible_parameter = store_variable_name-exclude_variable
if jsonable_parameter is True:
dict_parameter = {k:dict_parameter[k] for k in possible_parameter}
return (possible_parameter, store_variable_name | exclude_variable, dict_parameter)
return set(), store_variable_name | exclude_variable, {}
|
[
"def",
"variable_status",
"(",
"code",
":",
"str",
",",
"exclude_variable",
":",
"Union",
"[",
"set",
",",
"None",
"]",
"=",
"None",
",",
"jsonable_parameter",
":",
"bool",
"=",
"True",
")",
"->",
"tuple",
":",
"if",
"exclude_variable",
"is",
"None",
":",
"exclude_variable",
"=",
"set",
"(",
")",
"else",
":",
"exclude_variable",
"=",
"copy",
".",
"deepcopy",
"(",
"exclude_variable",
")",
"root",
"=",
"ast",
".",
"parse",
"(",
"code",
")",
"store_variable_name",
"=",
"set",
"(",
")",
"assign_only",
"=",
"True",
"dict_parameter",
"=",
"{",
"}",
"for",
"node",
"in",
"ast",
".",
"iter_child_nodes",
"(",
"root",
")",
":",
"if",
"isinstance",
"(",
"node",
",",
"ast",
".",
"Assign",
")",
":",
"for",
"assign_node",
"in",
"ast",
".",
"walk",
"(",
"node",
")",
":",
"if",
"isinstance",
"(",
"assign_node",
",",
"ast",
".",
"Name",
")",
":",
"if",
"isinstance",
"(",
"assign_node",
".",
"ctx",
",",
"ast",
".",
"Store",
")",
":",
"if",
"jsonable_parameter",
"is",
"False",
":",
"store_variable_name",
"|=",
"{",
"assign_node",
".",
"id",
"}",
"else",
":",
"exclude_variable",
"|=",
"{",
"assign_node",
".",
"id",
"}",
"_is_literal_eval",
",",
"_value",
"=",
"is_literal_eval",
"(",
"node",
".",
"value",
")",
"if",
"jsonable_parameter",
"is",
"True",
":",
"for",
"assign_node",
"in",
"ast",
".",
"iter_child_nodes",
"(",
"node",
")",
":",
"if",
"isinstance",
"(",
"assign_node",
",",
"ast",
".",
"Tuple",
")",
":",
"i",
"=",
"0",
"for",
"assign_tuple_node",
"in",
"ast",
".",
"iter_child_nodes",
"(",
"assign_node",
")",
":",
"if",
"isinstance",
"(",
"assign_tuple_node",
",",
"ast",
".",
"Name",
")",
":",
"if",
"isinstance",
"(",
"_value",
",",
"(",
"collections",
".",
"Iterable",
")",
")",
"and",
"is_jsonable",
"(",
"_value",
"[",
"i",
"]",
")",
"and",
"_is_literal_eval",
":",
"dict_parameter",
"[",
"assign_tuple_node",
".",
"id",
"]",
"=",
"_value",
"[",
"i",
"]",
"store_variable_name",
"|=",
"{",
"assign_tuple_node",
".",
"id",
"}",
"else",
":",
"exclude_variable",
"|=",
"{",
"assign_tuple_node",
".",
"id",
"}",
"i",
"+=",
"1",
"else",
":",
"if",
"isinstance",
"(",
"assign_node",
",",
"ast",
".",
"Name",
")",
":",
"if",
"is_jsonable",
"(",
"_value",
")",
"and",
"_is_literal_eval",
":",
"dict_parameter",
"[",
"assign_node",
".",
"id",
"]",
"=",
"_value",
"store_variable_name",
"|=",
"{",
"assign_node",
".",
"id",
"}",
"else",
":",
"exclude_variable",
"|=",
"{",
"assign_node",
".",
"id",
"}",
"elif",
"isinstance",
"(",
"node",
",",
"ast",
".",
"AugAssign",
")",
":",
"for",
"assign_node",
"in",
"ast",
".",
"walk",
"(",
"node",
")",
":",
"if",
"isinstance",
"(",
"assign_node",
",",
"ast",
".",
"Name",
")",
":",
"exclude_variable",
"|=",
"{",
"assign_node",
".",
"id",
"}",
"# class and function",
"elif",
"isinstance",
"(",
"node",
",",
"(",
"ast",
".",
"FunctionDef",
",",
"ast",
".",
"ClassDef",
")",
")",
":",
"assign_only",
"=",
"False",
"exclude_variable",
"|=",
"{",
"node",
".",
"name",
"}",
"# import",
"elif",
"isinstance",
"(",
"node",
",",
"ast",
".",
"Import",
")",
":",
"assign_only",
"=",
"False",
"for",
"node1",
"in",
"ast",
".",
"iter_child_nodes",
"(",
"node",
")",
":",
"if",
"node1",
".",
"asname",
"is",
"not",
"None",
":",
"exclude_variable",
"|=",
"{",
"node1",
".",
"asname",
"}",
"else",
":",
"exclude_variable",
"|=",
"{",
"node1",
".",
"name",
"}",
"# import from",
"elif",
"isinstance",
"(",
"node",
",",
"ast",
".",
"ImportFrom",
")",
":",
"assign_only",
"=",
"False",
"for",
"node1",
"in",
"ast",
".",
"iter_child_nodes",
"(",
"node",
")",
":",
"if",
"node1",
".",
"asname",
"is",
"not",
"None",
":",
"exclude_variable",
"|=",
"{",
"node1",
".",
"asname",
"}",
"else",
":",
"exclude_variable",
"|=",
"{",
"node1",
".",
"name",
"}",
"else",
":",
"assign_only",
"=",
"False",
"if",
"assign_only",
"is",
"True",
":",
"possible_parameter",
"=",
"store_variable_name",
"-",
"exclude_variable",
"if",
"jsonable_parameter",
"is",
"True",
":",
"dict_parameter",
"=",
"{",
"k",
":",
"dict_parameter",
"[",
"k",
"]",
"for",
"k",
"in",
"possible_parameter",
"}",
"return",
"(",
"possible_parameter",
",",
"store_variable_name",
"|",
"exclude_variable",
",",
"dict_parameter",
")",
"return",
"set",
"(",
")",
",",
"store_variable_name",
"|",
"exclude_variable",
",",
"{",
"}"
] |
Find the possible parameters and "global" variables from a python code.
This is achieved by parsing the abstract syntax tree.
Parameters
----------
code : str
Input code as string.
exclude_variable : set, None, optional
Variable to exclude.
jsonable_parameter: bool, True, optional
Consider only jsonable parameter
Returns
-------
tuple
(a set of possible parameter, a set of parameter to exclude, a dictionary of possible parameter )
A variable is a possible parameter if 1) it is not in the input exclude_variable,
2) the code contains only assignments, and 3) it is used only to bound objects.
The set of parameter to exclude is the union of the input exclude_variable and all names that looks like a global variable.
The dictionary of possible parameter {parameter name, parameter value} is available only if jsonable_parameter is True.
>>> variable_status("a=3")
({'a'}, {'a'}, {'a': 3})
>>> variable_status("a=3",jsonable_parameter=False)
({'a'}, {'a'}, {})
>>> variable_status("a += 1")
(set(), {'a'}, {})
>>> variable_status("def f(x,y=3):\\n\\t pass")
(set(), {'f'}, {})
>>> variable_status("class C(A):\\n\\t pass")
(set(), {'C'}, {})
>>> variable_status("import f")
(set(), {'f'}, {})
>>> variable_status("import f as g")
(set(), {'g'}, {})
>>> variable_status("from X import f")
(set(), {'f'}, {})
>>> variable_status("from X import f as g")
(set(), {'g'}, {})
|
[
"Find",
"the",
"possible",
"parameters",
"and",
"global",
"variables",
"from",
"a",
"python",
"code",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L279-L403
|
train
|
hz-inova/run_jnb
|
run_jnb/util.py
|
increment_name
|
def increment_name(name: str, start_marker: str = " (",
end_marker: str = ")") -> str:
"""
Increment the name where the incremental part is given by parameters.
Parameters
----------
name : str, nbformat.notebooknode.NotebookNode
Name
start_marker : str
The marker used before the incremental
end_marker : str
The marker after the incrementa
Returns
-------
str
Incremented name.
>>> increment_name('abc')
'abc (1)'
>>> increment_name('abc(1)')
'abc(1) (1)'
>>> increment_name('abc (123)')
'abc (124)'
>>> increment_name('abc-1',start_marker='-',end_marker='')
'abc-2'
>>> increment_name('abc[2]',start_marker='[',end_marker=']')
'abc[3]'
>>> increment_name('abc1',start_marker='',end_marker='')
Traceback (most recent call last):
...
ValueError: start_marker can not be the empty string.
"""
if start_marker == '':
raise ValueError("start_marker can not be the empty string.")
a = name
start = len(a)-a[::-1].find(start_marker[::-1])
if (a[len(a)-len(end_marker):len(a)] == end_marker
and start < (len(a)-len(end_marker))
and a[start-len(start_marker):start] == start_marker
and a[start:len(a)-len(end_marker)].isdigit()):
old_int = int(a[start:len(a)-len(end_marker)])
new_int = old_int+1
new_name = a[:start]+str(new_int)+end_marker
else:
new_name = a+start_marker+'1'+end_marker
return new_name
|
python
|
def increment_name(name: str, start_marker: str = " (",
end_marker: str = ")") -> str:
"""
Increment the name where the incremental part is given by parameters.
Parameters
----------
name : str, nbformat.notebooknode.NotebookNode
Name
start_marker : str
The marker used before the incremental
end_marker : str
The marker after the incrementa
Returns
-------
str
Incremented name.
>>> increment_name('abc')
'abc (1)'
>>> increment_name('abc(1)')
'abc(1) (1)'
>>> increment_name('abc (123)')
'abc (124)'
>>> increment_name('abc-1',start_marker='-',end_marker='')
'abc-2'
>>> increment_name('abc[2]',start_marker='[',end_marker=']')
'abc[3]'
>>> increment_name('abc1',start_marker='',end_marker='')
Traceback (most recent call last):
...
ValueError: start_marker can not be the empty string.
"""
if start_marker == '':
raise ValueError("start_marker can not be the empty string.")
a = name
start = len(a)-a[::-1].find(start_marker[::-1])
if (a[len(a)-len(end_marker):len(a)] == end_marker
and start < (len(a)-len(end_marker))
and a[start-len(start_marker):start] == start_marker
and a[start:len(a)-len(end_marker)].isdigit()):
old_int = int(a[start:len(a)-len(end_marker)])
new_int = old_int+1
new_name = a[:start]+str(new_int)+end_marker
else:
new_name = a+start_marker+'1'+end_marker
return new_name
|
[
"def",
"increment_name",
"(",
"name",
":",
"str",
",",
"start_marker",
":",
"str",
"=",
"\" (\"",
",",
"end_marker",
":",
"str",
"=",
"\")\"",
")",
"->",
"str",
":",
"if",
"start_marker",
"==",
"''",
":",
"raise",
"ValueError",
"(",
"\"start_marker can not be the empty string.\"",
")",
"a",
"=",
"name",
"start",
"=",
"len",
"(",
"a",
")",
"-",
"a",
"[",
":",
":",
"-",
"1",
"]",
".",
"find",
"(",
"start_marker",
"[",
":",
":",
"-",
"1",
"]",
")",
"if",
"(",
"a",
"[",
"len",
"(",
"a",
")",
"-",
"len",
"(",
"end_marker",
")",
":",
"len",
"(",
"a",
")",
"]",
"==",
"end_marker",
"and",
"start",
"<",
"(",
"len",
"(",
"a",
")",
"-",
"len",
"(",
"end_marker",
")",
")",
"and",
"a",
"[",
"start",
"-",
"len",
"(",
"start_marker",
")",
":",
"start",
"]",
"==",
"start_marker",
"and",
"a",
"[",
"start",
":",
"len",
"(",
"a",
")",
"-",
"len",
"(",
"end_marker",
")",
"]",
".",
"isdigit",
"(",
")",
")",
":",
"old_int",
"=",
"int",
"(",
"a",
"[",
"start",
":",
"len",
"(",
"a",
")",
"-",
"len",
"(",
"end_marker",
")",
"]",
")",
"new_int",
"=",
"old_int",
"+",
"1",
"new_name",
"=",
"a",
"[",
":",
"start",
"]",
"+",
"str",
"(",
"new_int",
")",
"+",
"end_marker",
"else",
":",
"new_name",
"=",
"a",
"+",
"start_marker",
"+",
"'1'",
"+",
"end_marker",
"return",
"new_name"
] |
Increment the name where the incremental part is given by parameters.
Parameters
----------
name : str, nbformat.notebooknode.NotebookNode
Name
start_marker : str
The marker used before the incremental
end_marker : str
The marker after the incrementa
Returns
-------
str
Incremented name.
>>> increment_name('abc')
'abc (1)'
>>> increment_name('abc(1)')
'abc(1) (1)'
>>> increment_name('abc (123)')
'abc (124)'
>>> increment_name('abc-1',start_marker='-',end_marker='')
'abc-2'
>>> increment_name('abc[2]',start_marker='[',end_marker=']')
'abc[3]'
>>> increment_name('abc1',start_marker='',end_marker='')
Traceback (most recent call last):
...
ValueError: start_marker can not be the empty string.
|
[
"Increment",
"the",
"name",
"where",
"the",
"incremental",
"part",
"is",
"given",
"by",
"parameters",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/util.py#L406-L455
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/resource_view.py
|
ResourceView.read_from_hdx
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['ResourceView']
"""Reads the resource view given by identifier from HDX and returns ResourceView object
Args:
identifier (str): Identifier of resource view
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[ResourceView]: ResourceView object if successful read, None if not
"""
resourceview = ResourceView(configuration=configuration)
result = resourceview._load_from_hdx('resource view', identifier)
if result:
return resourceview
return None
|
python
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['ResourceView']
"""Reads the resource view given by identifier from HDX and returns ResourceView object
Args:
identifier (str): Identifier of resource view
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[ResourceView]: ResourceView object if successful read, None if not
"""
resourceview = ResourceView(configuration=configuration)
result = resourceview._load_from_hdx('resource view', identifier)
if result:
return resourceview
return None
|
[
"def",
"read_from_hdx",
"(",
"identifier",
",",
"configuration",
"=",
"None",
")",
":",
"# type: (str, Optional[Configuration]) -> Optional['ResourceView']",
"resourceview",
"=",
"ResourceView",
"(",
"configuration",
"=",
"configuration",
")",
"result",
"=",
"resourceview",
".",
"_load_from_hdx",
"(",
"'resource view'",
",",
"identifier",
")",
"if",
"result",
":",
"return",
"resourceview",
"return",
"None"
] |
Reads the resource view given by identifier from HDX and returns ResourceView object
Args:
identifier (str): Identifier of resource view
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[ResourceView]: ResourceView object if successful read, None if not
|
[
"Reads",
"the",
"resource",
"view",
"given",
"by",
"identifier",
"from",
"HDX",
"and",
"returns",
"ResourceView",
"object"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/resource_view.py#L71-L87
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/resource_view.py
|
ResourceView.get_all_for_resource
|
def get_all_for_resource(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> List['ResourceView']
"""Read all resource views for a resource given by identifier from HDX and returns list of ResourceView objects
Args:
identifier (str): Identifier of resource
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
List[ResourceView]: List of ResourceView objects
"""
resourceview = ResourceView(configuration=configuration)
success, result = resourceview._read_from_hdx('resource view', identifier, 'id', ResourceView.actions()['list'])
resourceviews = list()
if success:
for resourceviewdict in result:
resourceview = ResourceView(resourceviewdict, configuration=configuration)
resourceviews.append(resourceview)
return resourceviews
|
python
|
def get_all_for_resource(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> List['ResourceView']
"""Read all resource views for a resource given by identifier from HDX and returns list of ResourceView objects
Args:
identifier (str): Identifier of resource
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
List[ResourceView]: List of ResourceView objects
"""
resourceview = ResourceView(configuration=configuration)
success, result = resourceview._read_from_hdx('resource view', identifier, 'id', ResourceView.actions()['list'])
resourceviews = list()
if success:
for resourceviewdict in result:
resourceview = ResourceView(resourceviewdict, configuration=configuration)
resourceviews.append(resourceview)
return resourceviews
|
[
"def",
"get_all_for_resource",
"(",
"identifier",
",",
"configuration",
"=",
"None",
")",
":",
"# type: (str, Optional[Configuration]) -> List['ResourceView']",
"resourceview",
"=",
"ResourceView",
"(",
"configuration",
"=",
"configuration",
")",
"success",
",",
"result",
"=",
"resourceview",
".",
"_read_from_hdx",
"(",
"'resource view'",
",",
"identifier",
",",
"'id'",
",",
"ResourceView",
".",
"actions",
"(",
")",
"[",
"'list'",
"]",
")",
"resourceviews",
"=",
"list",
"(",
")",
"if",
"success",
":",
"for",
"resourceviewdict",
"in",
"result",
":",
"resourceview",
"=",
"ResourceView",
"(",
"resourceviewdict",
",",
"configuration",
"=",
"configuration",
")",
"resourceviews",
".",
"append",
"(",
"resourceview",
")",
"return",
"resourceviews"
] |
Read all resource views for a resource given by identifier from HDX and returns list of ResourceView objects
Args:
identifier (str): Identifier of resource
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
List[ResourceView]: List of ResourceView objects
|
[
"Read",
"all",
"resource",
"views",
"for",
"a",
"resource",
"given",
"by",
"identifier",
"from",
"HDX",
"and",
"returns",
"list",
"of",
"ResourceView",
"objects"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/resource_view.py#L90-L109
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/resource_view.py
|
ResourceView._update_resource_view
|
def _update_resource_view(self, log=False):
# type: () -> bool
"""Check if resource view exists in HDX and if so, update resource view
Returns:
bool: True if updated and False if not
"""
update = False
if 'id' in self.data and self._load_from_hdx('resource view', self.data['id']):
update = True
else:
if 'resource_id' in self.data:
resource_views = self.get_all_for_resource(self.data['resource_id'])
for resource_view in resource_views:
if self.data['title'] == resource_view['title']:
self.old_data = self.data
self.data = resource_view.data
update = True
break
if update:
if log:
logger.warning('resource view exists. Updating %s' % self.data['id'])
self._merge_hdx_update('resource view', 'id')
return update
|
python
|
def _update_resource_view(self, log=False):
# type: () -> bool
"""Check if resource view exists in HDX and if so, update resource view
Returns:
bool: True if updated and False if not
"""
update = False
if 'id' in self.data and self._load_from_hdx('resource view', self.data['id']):
update = True
else:
if 'resource_id' in self.data:
resource_views = self.get_all_for_resource(self.data['resource_id'])
for resource_view in resource_views:
if self.data['title'] == resource_view['title']:
self.old_data = self.data
self.data = resource_view.data
update = True
break
if update:
if log:
logger.warning('resource view exists. Updating %s' % self.data['id'])
self._merge_hdx_update('resource view', 'id')
return update
|
[
"def",
"_update_resource_view",
"(",
"self",
",",
"log",
"=",
"False",
")",
":",
"# type: () -> bool",
"update",
"=",
"False",
"if",
"'id'",
"in",
"self",
".",
"data",
"and",
"self",
".",
"_load_from_hdx",
"(",
"'resource view'",
",",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
":",
"update",
"=",
"True",
"else",
":",
"if",
"'resource_id'",
"in",
"self",
".",
"data",
":",
"resource_views",
"=",
"self",
".",
"get_all_for_resource",
"(",
"self",
".",
"data",
"[",
"'resource_id'",
"]",
")",
"for",
"resource_view",
"in",
"resource_views",
":",
"if",
"self",
".",
"data",
"[",
"'title'",
"]",
"==",
"resource_view",
"[",
"'title'",
"]",
":",
"self",
".",
"old_data",
"=",
"self",
".",
"data",
"self",
".",
"data",
"=",
"resource_view",
".",
"data",
"update",
"=",
"True",
"break",
"if",
"update",
":",
"if",
"log",
":",
"logger",
".",
"warning",
"(",
"'resource view exists. Updating %s'",
"%",
"self",
".",
"data",
"[",
"'id'",
"]",
")",
"self",
".",
"_merge_hdx_update",
"(",
"'resource view'",
",",
"'id'",
")",
"return",
"update"
] |
Check if resource view exists in HDX and if so, update resource view
Returns:
bool: True if updated and False if not
|
[
"Check",
"if",
"resource",
"view",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"resource",
"view"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/resource_view.py#L124-L147
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/resource_view.py
|
ResourceView.create_in_hdx
|
def create_in_hdx(self):
# type: () -> None
"""Check if resource view exists in HDX and if so, update it, otherwise create resource view
Returns:
None
"""
self.check_required_fields()
if not self._update_resource_view(log=True):
self._save_to_hdx('create', 'title')
|
python
|
def create_in_hdx(self):
# type: () -> None
"""Check if resource view exists in HDX and if so, update it, otherwise create resource view
Returns:
None
"""
self.check_required_fields()
if not self._update_resource_view(log=True):
self._save_to_hdx('create', 'title')
|
[
"def",
"create_in_hdx",
"(",
"self",
")",
":",
"# type: () -> None",
"self",
".",
"check_required_fields",
"(",
")",
"if",
"not",
"self",
".",
"_update_resource_view",
"(",
"log",
"=",
"True",
")",
":",
"self",
".",
"_save_to_hdx",
"(",
"'create'",
",",
"'title'",
")"
] |
Check if resource view exists in HDX and if so, update it, otherwise create resource view
Returns:
None
|
[
"Check",
"if",
"resource",
"view",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"it",
"otherwise",
"create",
"resource",
"view"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/resource_view.py#L159-L168
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/resource_view.py
|
ResourceView.copy
|
def copy(self, resource_view):
# type: (Union[ResourceView,Dict,str]) -> None
"""Copies all fields except id, resource_id and package_id from another resource view.
Args:
resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary
Returns:
None
"""
if isinstance(resource_view, str):
if is_valid_uuid(resource_view) is False:
raise HDXError('%s is not a valid resource view id!' % resource_view)
resource_view = ResourceView.read_from_hdx(resource_view)
if not isinstance(resource_view, dict) and not isinstance(resource_view, ResourceView):
raise HDXError('%s is not a valid resource view!' % resource_view)
for key in resource_view:
if key not in ('id', 'resource_id', 'package_id'):
self.data[key] = resource_view[key]
|
python
|
def copy(self, resource_view):
# type: (Union[ResourceView,Dict,str]) -> None
"""Copies all fields except id, resource_id and package_id from another resource view.
Args:
resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary
Returns:
None
"""
if isinstance(resource_view, str):
if is_valid_uuid(resource_view) is False:
raise HDXError('%s is not a valid resource view id!' % resource_view)
resource_view = ResourceView.read_from_hdx(resource_view)
if not isinstance(resource_view, dict) and not isinstance(resource_view, ResourceView):
raise HDXError('%s is not a valid resource view!' % resource_view)
for key in resource_view:
if key not in ('id', 'resource_id', 'package_id'):
self.data[key] = resource_view[key]
|
[
"def",
"copy",
"(",
"self",
",",
"resource_view",
")",
":",
"# type: (Union[ResourceView,Dict,str]) -> None",
"if",
"isinstance",
"(",
"resource_view",
",",
"str",
")",
":",
"if",
"is_valid_uuid",
"(",
"resource_view",
")",
"is",
"False",
":",
"raise",
"HDXError",
"(",
"'%s is not a valid resource view id!'",
"%",
"resource_view",
")",
"resource_view",
"=",
"ResourceView",
".",
"read_from_hdx",
"(",
"resource_view",
")",
"if",
"not",
"isinstance",
"(",
"resource_view",
",",
"dict",
")",
"and",
"not",
"isinstance",
"(",
"resource_view",
",",
"ResourceView",
")",
":",
"raise",
"HDXError",
"(",
"'%s is not a valid resource view!'",
"%",
"resource_view",
")",
"for",
"key",
"in",
"resource_view",
":",
"if",
"key",
"not",
"in",
"(",
"'id'",
",",
"'resource_id'",
",",
"'package_id'",
")",
":",
"self",
".",
"data",
"[",
"key",
"]",
"=",
"resource_view",
"[",
"key",
"]"
] |
Copies all fields except id, resource_id and package_id from another resource view.
Args:
resource_view (Union[ResourceView,Dict,str]): Either a resource view id or resource view metadata either from a ResourceView object or a dictionary
Returns:
None
|
[
"Copies",
"all",
"fields",
"except",
"id",
"resource_id",
"and",
"package_id",
"from",
"another",
"resource",
"view",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/resource_view.py#L179-L197
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/hdx_tagscleanup.py
|
Tags.tagscleanupdicts
|
def tagscleanupdicts(configuration=None, url=None, keycolumn=5, failchained=True):
# type: (Optional[Configuration], Optional[str], int, bool) -> Tuple[Dict,List]
"""
Get tags cleanup dictionaries
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
url (Optional[str]): Url of tags cleanup spreadsheet. Defaults to None (internal configuration parameter).
keycolumn (int): Column number of tag column in spreadsheet. Defaults to 5.
failchained (bool): Fail if chained rules found. Defaults to True.
Returns:
Tuple[Dict,List]: Returns (Tags dictionary, Wildcard tags list)
"""
if not Tags._tags_dict:
if configuration is None:
configuration = Configuration.read()
with Download(full_agent=configuration.get_user_agent()) as downloader:
if url is None:
url = configuration['tags_cleanup_url']
Tags._tags_dict = downloader.download_tabular_rows_as_dicts(url, keycolumn=keycolumn)
keys = Tags._tags_dict.keys()
chainerror = False
for i, tag in enumerate(keys):
whattodo = Tags._tags_dict[tag]
action = whattodo[u'action']
final_tags = whattodo[u'final tags (semicolon separated)']
for final_tag in final_tags.split(';'):
if final_tag in keys:
index = list(keys).index(final_tag)
if index != i:
whattodo2 = Tags._tags_dict[final_tag]
action2 = whattodo2[u'action']
if action2 != 'OK' and action2 != 'Other':
final_tags2 = whattodo2[u'final tags (semicolon separated)']
if final_tag not in final_tags2.split(';'):
chainerror = True
if failchained:
logger.error('Chained rules: %s (%s -> %s) | %s (%s -> %s)' %
(action, tag, final_tags, action2, final_tag, final_tags2))
if failchained and chainerror:
raise ChainRuleError('Chained rules for tags detected!')
Tags._wildcard_tags = list()
for tag in Tags._tags_dict:
if '*' in tag:
Tags._wildcard_tags.append(tag)
return Tags._tags_dict, Tags._wildcard_tags
|
python
|
def tagscleanupdicts(configuration=None, url=None, keycolumn=5, failchained=True):
# type: (Optional[Configuration], Optional[str], int, bool) -> Tuple[Dict,List]
"""
Get tags cleanup dictionaries
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
url (Optional[str]): Url of tags cleanup spreadsheet. Defaults to None (internal configuration parameter).
keycolumn (int): Column number of tag column in spreadsheet. Defaults to 5.
failchained (bool): Fail if chained rules found. Defaults to True.
Returns:
Tuple[Dict,List]: Returns (Tags dictionary, Wildcard tags list)
"""
if not Tags._tags_dict:
if configuration is None:
configuration = Configuration.read()
with Download(full_agent=configuration.get_user_agent()) as downloader:
if url is None:
url = configuration['tags_cleanup_url']
Tags._tags_dict = downloader.download_tabular_rows_as_dicts(url, keycolumn=keycolumn)
keys = Tags._tags_dict.keys()
chainerror = False
for i, tag in enumerate(keys):
whattodo = Tags._tags_dict[tag]
action = whattodo[u'action']
final_tags = whattodo[u'final tags (semicolon separated)']
for final_tag in final_tags.split(';'):
if final_tag in keys:
index = list(keys).index(final_tag)
if index != i:
whattodo2 = Tags._tags_dict[final_tag]
action2 = whattodo2[u'action']
if action2 != 'OK' and action2 != 'Other':
final_tags2 = whattodo2[u'final tags (semicolon separated)']
if final_tag not in final_tags2.split(';'):
chainerror = True
if failchained:
logger.error('Chained rules: %s (%s -> %s) | %s (%s -> %s)' %
(action, tag, final_tags, action2, final_tag, final_tags2))
if failchained and chainerror:
raise ChainRuleError('Chained rules for tags detected!')
Tags._wildcard_tags = list()
for tag in Tags._tags_dict:
if '*' in tag:
Tags._wildcard_tags.append(tag)
return Tags._tags_dict, Tags._wildcard_tags
|
[
"def",
"tagscleanupdicts",
"(",
"configuration",
"=",
"None",
",",
"url",
"=",
"None",
",",
"keycolumn",
"=",
"5",
",",
"failchained",
"=",
"True",
")",
":",
"# type: (Optional[Configuration], Optional[str], int, bool) -> Tuple[Dict,List]",
"if",
"not",
"Tags",
".",
"_tags_dict",
":",
"if",
"configuration",
"is",
"None",
":",
"configuration",
"=",
"Configuration",
".",
"read",
"(",
")",
"with",
"Download",
"(",
"full_agent",
"=",
"configuration",
".",
"get_user_agent",
"(",
")",
")",
"as",
"downloader",
":",
"if",
"url",
"is",
"None",
":",
"url",
"=",
"configuration",
"[",
"'tags_cleanup_url'",
"]",
"Tags",
".",
"_tags_dict",
"=",
"downloader",
".",
"download_tabular_rows_as_dicts",
"(",
"url",
",",
"keycolumn",
"=",
"keycolumn",
")",
"keys",
"=",
"Tags",
".",
"_tags_dict",
".",
"keys",
"(",
")",
"chainerror",
"=",
"False",
"for",
"i",
",",
"tag",
"in",
"enumerate",
"(",
"keys",
")",
":",
"whattodo",
"=",
"Tags",
".",
"_tags_dict",
"[",
"tag",
"]",
"action",
"=",
"whattodo",
"[",
"u'action'",
"]",
"final_tags",
"=",
"whattodo",
"[",
"u'final tags (semicolon separated)'",
"]",
"for",
"final_tag",
"in",
"final_tags",
".",
"split",
"(",
"';'",
")",
":",
"if",
"final_tag",
"in",
"keys",
":",
"index",
"=",
"list",
"(",
"keys",
")",
".",
"index",
"(",
"final_tag",
")",
"if",
"index",
"!=",
"i",
":",
"whattodo2",
"=",
"Tags",
".",
"_tags_dict",
"[",
"final_tag",
"]",
"action2",
"=",
"whattodo2",
"[",
"u'action'",
"]",
"if",
"action2",
"!=",
"'OK'",
"and",
"action2",
"!=",
"'Other'",
":",
"final_tags2",
"=",
"whattodo2",
"[",
"u'final tags (semicolon separated)'",
"]",
"if",
"final_tag",
"not",
"in",
"final_tags2",
".",
"split",
"(",
"';'",
")",
":",
"chainerror",
"=",
"True",
"if",
"failchained",
":",
"logger",
".",
"error",
"(",
"'Chained rules: %s (%s -> %s) | %s (%s -> %s)'",
"%",
"(",
"action",
",",
"tag",
",",
"final_tags",
",",
"action2",
",",
"final_tag",
",",
"final_tags2",
")",
")",
"if",
"failchained",
"and",
"chainerror",
":",
"raise",
"ChainRuleError",
"(",
"'Chained rules for tags detected!'",
")",
"Tags",
".",
"_wildcard_tags",
"=",
"list",
"(",
")",
"for",
"tag",
"in",
"Tags",
".",
"_tags_dict",
":",
"if",
"'*'",
"in",
"tag",
":",
"Tags",
".",
"_wildcard_tags",
".",
"append",
"(",
"tag",
")",
"return",
"Tags",
".",
"_tags_dict",
",",
"Tags",
".",
"_wildcard_tags"
] |
Get tags cleanup dictionaries
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
url (Optional[str]): Url of tags cleanup spreadsheet. Defaults to None (internal configuration parameter).
keycolumn (int): Column number of tag column in spreadsheet. Defaults to 5.
failchained (bool): Fail if chained rules found. Defaults to True.
Returns:
Tuple[Dict,List]: Returns (Tags dictionary, Wildcard tags list)
|
[
"Get",
"tags",
"cleanup",
"dictionaries"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/hdx_tagscleanup.py#L24-L71
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.read_from_hdx
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['User']
"""Reads the user given by identifier from HDX and returns User object
Args:
identifier (str): Identifier of user
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[User]: User object if successful read, None if not
"""
user = User(configuration=configuration)
result = user._load_from_hdx('user', identifier)
if result:
return user
return None
|
python
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['User']
"""Reads the user given by identifier from HDX and returns User object
Args:
identifier (str): Identifier of user
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[User]: User object if successful read, None if not
"""
user = User(configuration=configuration)
result = user._load_from_hdx('user', identifier)
if result:
return user
return None
|
[
"def",
"read_from_hdx",
"(",
"identifier",
",",
"configuration",
"=",
"None",
")",
":",
"# type: (str, Optional[Configuration]) -> Optional['User']",
"user",
"=",
"User",
"(",
"configuration",
"=",
"configuration",
")",
"result",
"=",
"user",
".",
"_load_from_hdx",
"(",
"'user'",
",",
"identifier",
")",
"if",
"result",
":",
"return",
"user",
"return",
"None"
] |
Reads the user given by identifier from HDX and returns User object
Args:
identifier (str): Identifier of user
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[User]: User object if successful read, None if not
|
[
"Reads",
"the",
"user",
"given",
"by",
"identifier",
"from",
"HDX",
"and",
"returns",
"User",
"object"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L70-L86
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.update_in_hdx
|
def update_in_hdx(self):
# type: () -> None
"""Check if user exists in HDX and if so, update user
Returns:
None
"""
capacity = self.data.get('capacity')
if capacity is not None:
del self.data['capacity'] # remove capacity (which comes from users from Organization)
self._update_in_hdx('user', 'id')
if capacity is not None:
self.data['capacity'] = capacity
|
python
|
def update_in_hdx(self):
# type: () -> None
"""Check if user exists in HDX and if so, update user
Returns:
None
"""
capacity = self.data.get('capacity')
if capacity is not None:
del self.data['capacity'] # remove capacity (which comes from users from Organization)
self._update_in_hdx('user', 'id')
if capacity is not None:
self.data['capacity'] = capacity
|
[
"def",
"update_in_hdx",
"(",
"self",
")",
":",
"# type: () -> None",
"capacity",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'capacity'",
")",
"if",
"capacity",
"is",
"not",
"None",
":",
"del",
"self",
".",
"data",
"[",
"'capacity'",
"]",
"# remove capacity (which comes from users from Organization)",
"self",
".",
"_update_in_hdx",
"(",
"'user'",
",",
"'id'",
")",
"if",
"capacity",
"is",
"not",
"None",
":",
"self",
".",
"data",
"[",
"'capacity'",
"]",
"=",
"capacity"
] |
Check if user exists in HDX and if so, update user
Returns:
None
|
[
"Check",
"if",
"user",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"user"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L101-L113
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.create_in_hdx
|
def create_in_hdx(self):
# type: () -> None
"""Check if user exists in HDX and if so, update it, otherwise create user
Returns:
None
"""
capacity = self.data.get('capacity')
if capacity is not None:
del self.data['capacity']
self._create_in_hdx('user', 'id', 'name')
if capacity is not None:
self.data['capacity'] = capacity
|
python
|
def create_in_hdx(self):
# type: () -> None
"""Check if user exists in HDX and if so, update it, otherwise create user
Returns:
None
"""
capacity = self.data.get('capacity')
if capacity is not None:
del self.data['capacity']
self._create_in_hdx('user', 'id', 'name')
if capacity is not None:
self.data['capacity'] = capacity
|
[
"def",
"create_in_hdx",
"(",
"self",
")",
":",
"# type: () -> None",
"capacity",
"=",
"self",
".",
"data",
".",
"get",
"(",
"'capacity'",
")",
"if",
"capacity",
"is",
"not",
"None",
":",
"del",
"self",
".",
"data",
"[",
"'capacity'",
"]",
"self",
".",
"_create_in_hdx",
"(",
"'user'",
",",
"'id'",
",",
"'name'",
")",
"if",
"capacity",
"is",
"not",
"None",
":",
"self",
".",
"data",
"[",
"'capacity'",
"]",
"=",
"capacity"
] |
Check if user exists in HDX and if so, update it, otherwise create user
Returns:
None
|
[
"Check",
"if",
"user",
"exists",
"in",
"HDX",
"and",
"if",
"so",
"update",
"it",
"otherwise",
"create",
"user"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L115-L127
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.email
|
def email(self, subject, text_body, html_body=None, sender=None, **kwargs):
# type: (str, str, Optional[str], Optional[str], Any) -> None
"""Emails a user.
Args:
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
"""
self.configuration.emailer().send([self.data['email']], subject, text_body, html_body=html_body, sender=sender,
**kwargs)
|
python
|
def email(self, subject, text_body, html_body=None, sender=None, **kwargs):
# type: (str, str, Optional[str], Optional[str], Any) -> None
"""Emails a user.
Args:
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
"""
self.configuration.emailer().send([self.data['email']], subject, text_body, html_body=html_body, sender=sender,
**kwargs)
|
[
"def",
"email",
"(",
"self",
",",
"subject",
",",
"text_body",
",",
"html_body",
"=",
"None",
",",
"sender",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (str, str, Optional[str], Optional[str], Any) -> None",
"self",
".",
"configuration",
".",
"emailer",
"(",
")",
".",
"send",
"(",
"[",
"self",
".",
"data",
"[",
"'email'",
"]",
"]",
",",
"subject",
",",
"text_body",
",",
"html_body",
"=",
"html_body",
",",
"sender",
"=",
"sender",
",",
"*",
"*",
"kwargs",
")"
] |
Emails a user.
Args:
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
|
[
"Emails",
"a",
"user",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L138-L155
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.get_all_users
|
def get_all_users(configuration=None, **kwargs):
# type: (Optional[Configuration], Any) -> List['User']
"""Get all users in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
q (str): Restrict to names containing a string. Defaults to all users.
order_by (str): Field by which to sort - any user field or edits (number_of_edits). Defaults to 'name'.
Returns:
List[User]: List of all users in HDX
"""
user = User(configuration=configuration)
user['id'] = 'all users' # only for error message if produced
result = user._write_to_hdx('list', kwargs, 'id')
users = list()
if result:
for userdict in result:
user = User(userdict, configuration=configuration)
users.append(user)
else:
logger.debug(result)
return users
|
python
|
def get_all_users(configuration=None, **kwargs):
# type: (Optional[Configuration], Any) -> List['User']
"""Get all users in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
q (str): Restrict to names containing a string. Defaults to all users.
order_by (str): Field by which to sort - any user field or edits (number_of_edits). Defaults to 'name'.
Returns:
List[User]: List of all users in HDX
"""
user = User(configuration=configuration)
user['id'] = 'all users' # only for error message if produced
result = user._write_to_hdx('list', kwargs, 'id')
users = list()
if result:
for userdict in result:
user = User(userdict, configuration=configuration)
users.append(user)
else:
logger.debug(result)
return users
|
[
"def",
"get_all_users",
"(",
"configuration",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (Optional[Configuration], Any) -> List['User']",
"user",
"=",
"User",
"(",
"configuration",
"=",
"configuration",
")",
"user",
"[",
"'id'",
"]",
"=",
"'all users'",
"# only for error message if produced",
"result",
"=",
"user",
".",
"_write_to_hdx",
"(",
"'list'",
",",
"kwargs",
",",
"'id'",
")",
"users",
"=",
"list",
"(",
")",
"if",
"result",
":",
"for",
"userdict",
"in",
"result",
":",
"user",
"=",
"User",
"(",
"userdict",
",",
"configuration",
"=",
"configuration",
")",
"users",
".",
"append",
"(",
"user",
")",
"else",
":",
"logger",
".",
"debug",
"(",
"result",
")",
"return",
"users"
] |
Get all users in HDX
Args:
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
**kwargs: See below
q (str): Restrict to names containing a string. Defaults to all users.
order_by (str): Field by which to sort - any user field or edits (number_of_edits). Defaults to 'name'.
Returns:
List[User]: List of all users in HDX
|
[
"Get",
"all",
"users",
"in",
"HDX"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L158-L181
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.email_users
|
def email_users(users, subject, text_body, html_body=None, sender=None, configuration=None, **kwargs):
# type: (List['User'], str, str, Optional[str], Optional[str], Optional[Configuration], Any) -> None
"""Email a list of users
Args:
users (List[User]): List of users
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
configuration (Optional[Configuration]): HDX configuration. Defaults to configuration of first user in list.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
"""
if not users:
raise ValueError('No users supplied')
recipients = list()
for user in users:
recipients.append(user.data['email'])
if configuration is None:
configuration = users[0].configuration
configuration.emailer().send(recipients, subject, text_body, html_body=html_body, sender=sender, **kwargs)
|
python
|
def email_users(users, subject, text_body, html_body=None, sender=None, configuration=None, **kwargs):
# type: (List['User'], str, str, Optional[str], Optional[str], Optional[Configuration], Any) -> None
"""Email a list of users
Args:
users (List[User]): List of users
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
configuration (Optional[Configuration]): HDX configuration. Defaults to configuration of first user in list.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
"""
if not users:
raise ValueError('No users supplied')
recipients = list()
for user in users:
recipients.append(user.data['email'])
if configuration is None:
configuration = users[0].configuration
configuration.emailer().send(recipients, subject, text_body, html_body=html_body, sender=sender, **kwargs)
|
[
"def",
"email_users",
"(",
"users",
",",
"subject",
",",
"text_body",
",",
"html_body",
"=",
"None",
",",
"sender",
"=",
"None",
",",
"configuration",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"# type: (List['User'], str, str, Optional[str], Optional[str], Optional[Configuration], Any) -> None",
"if",
"not",
"users",
":",
"raise",
"ValueError",
"(",
"'No users supplied'",
")",
"recipients",
"=",
"list",
"(",
")",
"for",
"user",
"in",
"users",
":",
"recipients",
".",
"append",
"(",
"user",
".",
"data",
"[",
"'email'",
"]",
")",
"if",
"configuration",
"is",
"None",
":",
"configuration",
"=",
"users",
"[",
"0",
"]",
".",
"configuration",
"configuration",
".",
"emailer",
"(",
")",
".",
"send",
"(",
"recipients",
",",
"subject",
",",
"text_body",
",",
"html_body",
"=",
"html_body",
",",
"sender",
"=",
"sender",
",",
"*",
"*",
"kwargs",
")"
] |
Email a list of users
Args:
users (List[User]): List of users
subject (str): Email subject
text_body (str): Plain text email body
html_body (str): HTML email body
sender (Optional[str]): Email sender. Defaults to SMTP username.
configuration (Optional[Configuration]): HDX configuration. Defaults to configuration of first user in list.
**kwargs: See below
mail_options (List): Mail options (see smtplib documentation)
rcpt_options (List): Recipient options (see smtplib documentation)
Returns:
None
|
[
"Email",
"a",
"list",
"of",
"users"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L184-L209
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/user.py
|
User.get_organizations
|
def get_organizations(self, permission='read'):
# type: (str) -> List['Organization']
"""Get organizations in HDX that this user is a member of.
Args:
permission (str): Permission to check for. Defaults to 'read'.
Returns:
List[Organization]: List of organizations in HDX that this user is a member of
"""
success, result = self._read_from_hdx('user', self.data['name'], 'id', self.actions()['listorgs'],
permission=permission)
organizations = list()
if success:
for organizationdict in result:
organization = hdx.data.organization.Organization.read_from_hdx(organizationdict['id'])
organizations.append(organization)
return organizations
|
python
|
def get_organizations(self, permission='read'):
# type: (str) -> List['Organization']
"""Get organizations in HDX that this user is a member of.
Args:
permission (str): Permission to check for. Defaults to 'read'.
Returns:
List[Organization]: List of organizations in HDX that this user is a member of
"""
success, result = self._read_from_hdx('user', self.data['name'], 'id', self.actions()['listorgs'],
permission=permission)
organizations = list()
if success:
for organizationdict in result:
organization = hdx.data.organization.Organization.read_from_hdx(organizationdict['id'])
organizations.append(organization)
return organizations
|
[
"def",
"get_organizations",
"(",
"self",
",",
"permission",
"=",
"'read'",
")",
":",
"# type: (str) -> List['Organization']",
"success",
",",
"result",
"=",
"self",
".",
"_read_from_hdx",
"(",
"'user'",
",",
"self",
".",
"data",
"[",
"'name'",
"]",
",",
"'id'",
",",
"self",
".",
"actions",
"(",
")",
"[",
"'listorgs'",
"]",
",",
"permission",
"=",
"permission",
")",
"organizations",
"=",
"list",
"(",
")",
"if",
"success",
":",
"for",
"organizationdict",
"in",
"result",
":",
"organization",
"=",
"hdx",
".",
"data",
".",
"organization",
".",
"Organization",
".",
"read_from_hdx",
"(",
"organizationdict",
"[",
"'id'",
"]",
")",
"organizations",
".",
"append",
"(",
"organization",
")",
"return",
"organizations"
] |
Get organizations in HDX that this user is a member of.
Args:
permission (str): Permission to check for. Defaults to 'read'.
Returns:
List[Organization]: List of organizations in HDX that this user is a member of
|
[
"Get",
"organizations",
"in",
"HDX",
"that",
"this",
"user",
"is",
"a",
"member",
"of",
"."
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/user.py#L211-L228
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/facades/simple.py
|
facade
|
def facade(projectmainfn, **kwargs):
# (Callable[[None], None], Any) -> None
"""Facade to simplify project setup that calls project main function
Args:
projectmainfn ((None) -> None): main function of project
**kwargs: configuration parameters to pass to HDX Configuration class
Returns:
None
"""
#
# Setting up configuration
#
site_url = Configuration._create(**kwargs)
logger.info('--------------------------------------------------')
logger.info('> Using HDX Python API Library %s' % Configuration.apiversion)
logger.info('> HDX Site: %s' % site_url)
UserAgent.user_agent = Configuration.read().user_agent
projectmainfn()
|
python
|
def facade(projectmainfn, **kwargs):
# (Callable[[None], None], Any) -> None
"""Facade to simplify project setup that calls project main function
Args:
projectmainfn ((None) -> None): main function of project
**kwargs: configuration parameters to pass to HDX Configuration class
Returns:
None
"""
#
# Setting up configuration
#
site_url = Configuration._create(**kwargs)
logger.info('--------------------------------------------------')
logger.info('> Using HDX Python API Library %s' % Configuration.apiversion)
logger.info('> HDX Site: %s' % site_url)
UserAgent.user_agent = Configuration.read().user_agent
projectmainfn()
|
[
"def",
"facade",
"(",
"projectmainfn",
",",
"*",
"*",
"kwargs",
")",
":",
"# (Callable[[None], None], Any) -> None",
"#",
"# Setting up configuration",
"#",
"site_url",
"=",
"Configuration",
".",
"_create",
"(",
"*",
"*",
"kwargs",
")",
"logger",
".",
"info",
"(",
"'--------------------------------------------------'",
")",
"logger",
".",
"info",
"(",
"'> Using HDX Python API Library %s'",
"%",
"Configuration",
".",
"apiversion",
")",
"logger",
".",
"info",
"(",
"'> HDX Site: %s'",
"%",
"site_url",
")",
"UserAgent",
".",
"user_agent",
"=",
"Configuration",
".",
"read",
"(",
")",
".",
"user_agent",
"projectmainfn",
"(",
")"
] |
Facade to simplify project setup that calls project main function
Args:
projectmainfn ((None) -> None): main function of project
**kwargs: configuration parameters to pass to HDX Configuration class
Returns:
None
|
[
"Facade",
"to",
"simplify",
"project",
"setup",
"that",
"calls",
"project",
"main",
"function"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/facades/simple.py#L15-L38
|
train
|
jorisroovers/pymarkdownlint
|
pymarkdownlint/cli.py
|
get_lint_config
|
def get_lint_config(config_path=None):
""" Tries loading the config from the given path. If no path is specified, the default config path
is tried, and if that is not specified, we the default config is returned. """
# config path specified
if config_path:
config = LintConfig.load_from_file(config_path)
click.echo("Using config from {0}".format(config_path))
# default config path
elif os.path.exists(DEFAULT_CONFIG_FILE):
config = LintConfig.load_from_file(DEFAULT_CONFIG_FILE)
click.echo("Using config from {0}".format(DEFAULT_CONFIG_FILE))
# no config file
else:
config = LintConfig()
return config
|
python
|
def get_lint_config(config_path=None):
""" Tries loading the config from the given path. If no path is specified, the default config path
is tried, and if that is not specified, we the default config is returned. """
# config path specified
if config_path:
config = LintConfig.load_from_file(config_path)
click.echo("Using config from {0}".format(config_path))
# default config path
elif os.path.exists(DEFAULT_CONFIG_FILE):
config = LintConfig.load_from_file(DEFAULT_CONFIG_FILE)
click.echo("Using config from {0}".format(DEFAULT_CONFIG_FILE))
# no config file
else:
config = LintConfig()
return config
|
[
"def",
"get_lint_config",
"(",
"config_path",
"=",
"None",
")",
":",
"# config path specified",
"if",
"config_path",
":",
"config",
"=",
"LintConfig",
".",
"load_from_file",
"(",
"config_path",
")",
"click",
".",
"echo",
"(",
"\"Using config from {0}\"",
".",
"format",
"(",
"config_path",
")",
")",
"# default config path",
"elif",
"os",
".",
"path",
".",
"exists",
"(",
"DEFAULT_CONFIG_FILE",
")",
":",
"config",
"=",
"LintConfig",
".",
"load_from_file",
"(",
"DEFAULT_CONFIG_FILE",
")",
"click",
".",
"echo",
"(",
"\"Using config from {0}\"",
".",
"format",
"(",
"DEFAULT_CONFIG_FILE",
")",
")",
"# no config file",
"else",
":",
"config",
"=",
"LintConfig",
"(",
")",
"return",
"config"
] |
Tries loading the config from the given path. If no path is specified, the default config path
is tried, and if that is not specified, we the default config is returned.
|
[
"Tries",
"loading",
"the",
"config",
"from",
"the",
"given",
"path",
".",
"If",
"no",
"path",
"is",
"specified",
"the",
"default",
"config",
"path",
"is",
"tried",
"and",
"if",
"that",
"is",
"not",
"specified",
"we",
"the",
"default",
"config",
"is",
"returned",
"."
] |
c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65
|
https://github.com/jorisroovers/pymarkdownlint/blob/c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65/pymarkdownlint/cli.py#L17-L32
|
train
|
jorisroovers/pymarkdownlint
|
pymarkdownlint/cli.py
|
cli
|
def cli(list_files, config, ignore, path):
""" Markdown lint tool, checks your markdown for styling issues """
files = MarkdownFileFinder.find_files(path)
if list_files:
echo_files(files)
lint_config = get_lint_config(config)
lint_config.apply_on_csv_string(ignore, lint_config.disable_rule)
linter = MarkdownLinter(lint_config)
error_count = linter.lint_files(files)
exit(error_count)
|
python
|
def cli(list_files, config, ignore, path):
""" Markdown lint tool, checks your markdown for styling issues """
files = MarkdownFileFinder.find_files(path)
if list_files:
echo_files(files)
lint_config = get_lint_config(config)
lint_config.apply_on_csv_string(ignore, lint_config.disable_rule)
linter = MarkdownLinter(lint_config)
error_count = linter.lint_files(files)
exit(error_count)
|
[
"def",
"cli",
"(",
"list_files",
",",
"config",
",",
"ignore",
",",
"path",
")",
":",
"files",
"=",
"MarkdownFileFinder",
".",
"find_files",
"(",
"path",
")",
"if",
"list_files",
":",
"echo_files",
"(",
"files",
")",
"lint_config",
"=",
"get_lint_config",
"(",
"config",
")",
"lint_config",
".",
"apply_on_csv_string",
"(",
"ignore",
",",
"lint_config",
".",
"disable_rule",
")",
"linter",
"=",
"MarkdownLinter",
"(",
"lint_config",
")",
"error_count",
"=",
"linter",
".",
"lint_files",
"(",
"files",
")",
"exit",
"(",
"error_count",
")"
] |
Markdown lint tool, checks your markdown for styling issues
|
[
"Markdown",
"lint",
"tool",
"checks",
"your",
"markdown",
"for",
"styling",
"issues"
] |
c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65
|
https://github.com/jorisroovers/pymarkdownlint/blob/c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65/pymarkdownlint/cli.py#L42-L53
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/__init__.py
|
main
|
def main():
""" Main function """
# Read configuration from the config file if present, else fall back to
# command line options
if args.config:
config = config_file_parser.get_configuration(args.config)
access_key_id = config['access-key-id']
secret_access_key = config['secret-access-key']
region = config['region']
else:
access_key_id = args.access_key_id
secret_access_key = args.secret_access_key
region = args.region
if args.daemon:
pid_file = '/tmp/automatic-ebs-snapshots.pid'
daemon = AutoEBSDaemon(pid_file)
if args.daemon == 'start':
daemon.start()
elif args.daemon == 'stop':
daemon.stop()
sys.exit(0)
elif args.daemon == 'restart':
daemon.restart()
elif args.daemon in ['foreground', 'fg']:
daemon.run()
else:
print 'Valid options for --daemon are start, stop and restart'
sys.exit(1)
# Connect to AWS
connection = connection_manager.connect_to_ec2(
region, access_key_id, secret_access_key)
if args.watch:
volume_manager.watch(
connection,
args.watch,
args.interval,
args.retention)
if args.unwatch:
volume_manager.unwatch(connection, args.unwatch)
if args.watch_file:
volume_manager.watch_from_file(connection, args.watch_file)
if args.unwatch_file:
volume_manager.unwatch_from_file(connection, args.unwatch_file)
if args.snapshots:
volume_manager.list_snapshots(connection, args.snapshots)
if args.list:
volume_manager.list(connection)
if args.run:
snapshot_manager.run(connection)
|
python
|
def main():
""" Main function """
# Read configuration from the config file if present, else fall back to
# command line options
if args.config:
config = config_file_parser.get_configuration(args.config)
access_key_id = config['access-key-id']
secret_access_key = config['secret-access-key']
region = config['region']
else:
access_key_id = args.access_key_id
secret_access_key = args.secret_access_key
region = args.region
if args.daemon:
pid_file = '/tmp/automatic-ebs-snapshots.pid'
daemon = AutoEBSDaemon(pid_file)
if args.daemon == 'start':
daemon.start()
elif args.daemon == 'stop':
daemon.stop()
sys.exit(0)
elif args.daemon == 'restart':
daemon.restart()
elif args.daemon in ['foreground', 'fg']:
daemon.run()
else:
print 'Valid options for --daemon are start, stop and restart'
sys.exit(1)
# Connect to AWS
connection = connection_manager.connect_to_ec2(
region, access_key_id, secret_access_key)
if args.watch:
volume_manager.watch(
connection,
args.watch,
args.interval,
args.retention)
if args.unwatch:
volume_manager.unwatch(connection, args.unwatch)
if args.watch_file:
volume_manager.watch_from_file(connection, args.watch_file)
if args.unwatch_file:
volume_manager.unwatch_from_file(connection, args.unwatch_file)
if args.snapshots:
volume_manager.list_snapshots(connection, args.snapshots)
if args.list:
volume_manager.list(connection)
if args.run:
snapshot_manager.run(connection)
|
[
"def",
"main",
"(",
")",
":",
"# Read configuration from the config file if present, else fall back to",
"# command line options",
"if",
"args",
".",
"config",
":",
"config",
"=",
"config_file_parser",
".",
"get_configuration",
"(",
"args",
".",
"config",
")",
"access_key_id",
"=",
"config",
"[",
"'access-key-id'",
"]",
"secret_access_key",
"=",
"config",
"[",
"'secret-access-key'",
"]",
"region",
"=",
"config",
"[",
"'region'",
"]",
"else",
":",
"access_key_id",
"=",
"args",
".",
"access_key_id",
"secret_access_key",
"=",
"args",
".",
"secret_access_key",
"region",
"=",
"args",
".",
"region",
"if",
"args",
".",
"daemon",
":",
"pid_file",
"=",
"'/tmp/automatic-ebs-snapshots.pid'",
"daemon",
"=",
"AutoEBSDaemon",
"(",
"pid_file",
")",
"if",
"args",
".",
"daemon",
"==",
"'start'",
":",
"daemon",
".",
"start",
"(",
")",
"elif",
"args",
".",
"daemon",
"==",
"'stop'",
":",
"daemon",
".",
"stop",
"(",
")",
"sys",
".",
"exit",
"(",
"0",
")",
"elif",
"args",
".",
"daemon",
"==",
"'restart'",
":",
"daemon",
".",
"restart",
"(",
")",
"elif",
"args",
".",
"daemon",
"in",
"[",
"'foreground'",
",",
"'fg'",
"]",
":",
"daemon",
".",
"run",
"(",
")",
"else",
":",
"print",
"'Valid options for --daemon are start, stop and restart'",
"sys",
".",
"exit",
"(",
"1",
")",
"# Connect to AWS",
"connection",
"=",
"connection_manager",
".",
"connect_to_ec2",
"(",
"region",
",",
"access_key_id",
",",
"secret_access_key",
")",
"if",
"args",
".",
"watch",
":",
"volume_manager",
".",
"watch",
"(",
"connection",
",",
"args",
".",
"watch",
",",
"args",
".",
"interval",
",",
"args",
".",
"retention",
")",
"if",
"args",
".",
"unwatch",
":",
"volume_manager",
".",
"unwatch",
"(",
"connection",
",",
"args",
".",
"unwatch",
")",
"if",
"args",
".",
"watch_file",
":",
"volume_manager",
".",
"watch_from_file",
"(",
"connection",
",",
"args",
".",
"watch_file",
")",
"if",
"args",
".",
"unwatch_file",
":",
"volume_manager",
".",
"unwatch_from_file",
"(",
"connection",
",",
"args",
".",
"unwatch_file",
")",
"if",
"args",
".",
"snapshots",
":",
"volume_manager",
".",
"list_snapshots",
"(",
"connection",
",",
"args",
".",
"snapshots",
")",
"if",
"args",
".",
"list",
":",
"volume_manager",
".",
"list",
"(",
"connection",
")",
"if",
"args",
".",
"run",
":",
"snapshot_manager",
".",
"run",
"(",
"connection",
")"
] |
Main function
|
[
"Main",
"function"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/__init__.py#L109-L171
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/__init__.py
|
AutoEBSDaemon.run
|
def run(self, check_interval=300):
""" Run the daemon
:type check_interval: int
:param check_interval: Delay in seconds between checks
"""
while True:
# Read configuration from the config file if present, else fall
# back to command line options
if args.config:
config = config_file_parser.get_configuration(args.config)
access_key_id = config['access-key-id']
secret_access_key = config['secret-access-key']
region = config['region']
else:
access_key_id = args.access_key_id
secret_access_key = args.secret_access_key
region = args.region
# Connect to AWS
connection = connection_manager.connect_to_ec2(
region, access_key_id, secret_access_key)
snapshot_manager.run(connection)
logger.info('Sleeping {} seconds until next check'.format(
check_interval))
time.sleep(check_interval)
|
python
|
def run(self, check_interval=300):
""" Run the daemon
:type check_interval: int
:param check_interval: Delay in seconds between checks
"""
while True:
# Read configuration from the config file if present, else fall
# back to command line options
if args.config:
config = config_file_parser.get_configuration(args.config)
access_key_id = config['access-key-id']
secret_access_key = config['secret-access-key']
region = config['region']
else:
access_key_id = args.access_key_id
secret_access_key = args.secret_access_key
region = args.region
# Connect to AWS
connection = connection_manager.connect_to_ec2(
region, access_key_id, secret_access_key)
snapshot_manager.run(connection)
logger.info('Sleeping {} seconds until next check'.format(
check_interval))
time.sleep(check_interval)
|
[
"def",
"run",
"(",
"self",
",",
"check_interval",
"=",
"300",
")",
":",
"while",
"True",
":",
"# Read configuration from the config file if present, else fall",
"# back to command line options",
"if",
"args",
".",
"config",
":",
"config",
"=",
"config_file_parser",
".",
"get_configuration",
"(",
"args",
".",
"config",
")",
"access_key_id",
"=",
"config",
"[",
"'access-key-id'",
"]",
"secret_access_key",
"=",
"config",
"[",
"'secret-access-key'",
"]",
"region",
"=",
"config",
"[",
"'region'",
"]",
"else",
":",
"access_key_id",
"=",
"args",
".",
"access_key_id",
"secret_access_key",
"=",
"args",
".",
"secret_access_key",
"region",
"=",
"args",
".",
"region",
"# Connect to AWS",
"connection",
"=",
"connection_manager",
".",
"connect_to_ec2",
"(",
"region",
",",
"access_key_id",
",",
"secret_access_key",
")",
"snapshot_manager",
".",
"run",
"(",
"connection",
")",
"logger",
".",
"info",
"(",
"'Sleeping {} seconds until next check'",
".",
"format",
"(",
"check_interval",
")",
")",
"time",
".",
"sleep",
"(",
"check_interval",
")"
] |
Run the daemon
:type check_interval: int
:param check_interval: Delay in seconds between checks
|
[
"Run",
"the",
"daemon"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/__init__.py#L79-L106
|
train
|
jorisroovers/pymarkdownlint
|
pymarkdownlint/lint.py
|
MarkdownLinter._apply_line_rules
|
def _apply_line_rules(self, markdown_string):
""" Iterates over the lines in a given markdown string and applies all the enabled line rules to each line """
all_violations = []
lines = markdown_string.split("\n")
line_rules = self.line_rules
line_nr = 1
ignoring = False
for line in lines:
if ignoring:
if line.strip() == '<!-- markdownlint:enable -->':
ignoring = False
else:
if line.strip() == '<!-- markdownlint:disable -->':
ignoring = True
continue
for rule in line_rules:
violation = rule.validate(line)
if violation:
violation.line_nr = line_nr
all_violations.append(violation)
line_nr += 1
return all_violations
|
python
|
def _apply_line_rules(self, markdown_string):
""" Iterates over the lines in a given markdown string and applies all the enabled line rules to each line """
all_violations = []
lines = markdown_string.split("\n")
line_rules = self.line_rules
line_nr = 1
ignoring = False
for line in lines:
if ignoring:
if line.strip() == '<!-- markdownlint:enable -->':
ignoring = False
else:
if line.strip() == '<!-- markdownlint:disable -->':
ignoring = True
continue
for rule in line_rules:
violation = rule.validate(line)
if violation:
violation.line_nr = line_nr
all_violations.append(violation)
line_nr += 1
return all_violations
|
[
"def",
"_apply_line_rules",
"(",
"self",
",",
"markdown_string",
")",
":",
"all_violations",
"=",
"[",
"]",
"lines",
"=",
"markdown_string",
".",
"split",
"(",
"\"\\n\"",
")",
"line_rules",
"=",
"self",
".",
"line_rules",
"line_nr",
"=",
"1",
"ignoring",
"=",
"False",
"for",
"line",
"in",
"lines",
":",
"if",
"ignoring",
":",
"if",
"line",
".",
"strip",
"(",
")",
"==",
"'<!-- markdownlint:enable -->'",
":",
"ignoring",
"=",
"False",
"else",
":",
"if",
"line",
".",
"strip",
"(",
")",
"==",
"'<!-- markdownlint:disable -->'",
":",
"ignoring",
"=",
"True",
"continue",
"for",
"rule",
"in",
"line_rules",
":",
"violation",
"=",
"rule",
".",
"validate",
"(",
"line",
")",
"if",
"violation",
":",
"violation",
".",
"line_nr",
"=",
"line_nr",
"all_violations",
".",
"append",
"(",
"violation",
")",
"line_nr",
"+=",
"1",
"return",
"all_violations"
] |
Iterates over the lines in a given markdown string and applies all the enabled line rules to each line
|
[
"Iterates",
"over",
"the",
"lines",
"in",
"a",
"given",
"markdown",
"string",
"and",
"applies",
"all",
"the",
"enabled",
"line",
"rules",
"to",
"each",
"line"
] |
c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65
|
https://github.com/jorisroovers/pymarkdownlint/blob/c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65/pymarkdownlint/lint.py#L13-L35
|
train
|
jorisroovers/pymarkdownlint
|
pymarkdownlint/lint.py
|
MarkdownLinter.lint_files
|
def lint_files(self, files):
""" Lints a list of files.
:param files: list of files to lint
:return: a list of violations found in the files
"""
all_violations = []
for filename in files:
with open(filename, 'r') as f:
content = f.read()
violations = self.lint(content)
all_violations.extend(violations)
for e in violations:
print("{0}:{1}: {2} {3}".format(filename, e.line_nr, e.rule_id, e.message))
return len(all_violations)
|
python
|
def lint_files(self, files):
""" Lints a list of files.
:param files: list of files to lint
:return: a list of violations found in the files
"""
all_violations = []
for filename in files:
with open(filename, 'r') as f:
content = f.read()
violations = self.lint(content)
all_violations.extend(violations)
for e in violations:
print("{0}:{1}: {2} {3}".format(filename, e.line_nr, e.rule_id, e.message))
return len(all_violations)
|
[
"def",
"lint_files",
"(",
"self",
",",
"files",
")",
":",
"all_violations",
"=",
"[",
"]",
"for",
"filename",
"in",
"files",
":",
"with",
"open",
"(",
"filename",
",",
"'r'",
")",
"as",
"f",
":",
"content",
"=",
"f",
".",
"read",
"(",
")",
"violations",
"=",
"self",
".",
"lint",
"(",
"content",
")",
"all_violations",
".",
"extend",
"(",
"violations",
")",
"for",
"e",
"in",
"violations",
":",
"print",
"(",
"\"{0}:{1}: {2} {3}\"",
".",
"format",
"(",
"filename",
",",
"e",
".",
"line_nr",
",",
"e",
".",
"rule_id",
",",
"e",
".",
"message",
")",
")",
"return",
"len",
"(",
"all_violations",
")"
] |
Lints a list of files.
:param files: list of files to lint
:return: a list of violations found in the files
|
[
"Lints",
"a",
"list",
"of",
"files",
".",
":",
"param",
"files",
":",
"list",
"of",
"files",
"to",
"lint",
":",
"return",
":",
"a",
"list",
"of",
"violations",
"found",
"in",
"the",
"files"
] |
c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65
|
https://github.com/jorisroovers/pymarkdownlint/blob/c1044e25e18afd78b3fda8fd9b00a4f67cfbbc65/pymarkdownlint/lint.py#L42-L55
|
train
|
libyal/dtfabric
|
dtfabric/runtime/byte_operations.py
|
StructOperation.ReadFrom
|
def ReadFrom(self, byte_stream):
"""Read values from a byte stream.
Args:
byte_stream (bytes): byte stream.
Returns:
tuple[object, ...]: values copies from the byte stream.
Raises:
IOError: if byte stream cannot be read.
OSError: if byte stream cannot be read.
"""
try:
return self._struct.unpack_from(byte_stream)
except (TypeError, struct.error) as exception:
raise IOError('Unable to read byte stream with error: {0!s}'.format(
exception))
|
python
|
def ReadFrom(self, byte_stream):
"""Read values from a byte stream.
Args:
byte_stream (bytes): byte stream.
Returns:
tuple[object, ...]: values copies from the byte stream.
Raises:
IOError: if byte stream cannot be read.
OSError: if byte stream cannot be read.
"""
try:
return self._struct.unpack_from(byte_stream)
except (TypeError, struct.error) as exception:
raise IOError('Unable to read byte stream with error: {0!s}'.format(
exception))
|
[
"def",
"ReadFrom",
"(",
"self",
",",
"byte_stream",
")",
":",
"try",
":",
"return",
"self",
".",
"_struct",
".",
"unpack_from",
"(",
"byte_stream",
")",
"except",
"(",
"TypeError",
",",
"struct",
".",
"error",
")",
"as",
"exception",
":",
"raise",
"IOError",
"(",
"'Unable to read byte stream with error: {0!s}'",
".",
"format",
"(",
"exception",
")",
")"
] |
Read values from a byte stream.
Args:
byte_stream (bytes): byte stream.
Returns:
tuple[object, ...]: values copies from the byte stream.
Raises:
IOError: if byte stream cannot be read.
OSError: if byte stream cannot be read.
|
[
"Read",
"values",
"from",
"a",
"byte",
"stream",
"."
] |
0d2b5719fa257f6e5c661a406737ebcf8c8db266
|
https://github.com/libyal/dtfabric/blob/0d2b5719fa257f6e5c661a406737ebcf8c8db266/dtfabric/runtime/byte_operations.py#L65-L82
|
train
|
libyal/dtfabric
|
dtfabric/runtime/byte_operations.py
|
StructOperation.WriteTo
|
def WriteTo(self, values):
"""Writes values to a byte stream.
Args:
values (tuple[object, ...]): values to copy to the byte stream.
Returns:
bytes: byte stream.
Raises:
IOError: if byte stream cannot be written.
OSError: if byte stream cannot be read.
"""
try:
return self._struct.pack(*values)
except (TypeError, struct.error) as exception:
raise IOError('Unable to write stream with error: {0!s}'.format(
exception))
|
python
|
def WriteTo(self, values):
"""Writes values to a byte stream.
Args:
values (tuple[object, ...]): values to copy to the byte stream.
Returns:
bytes: byte stream.
Raises:
IOError: if byte stream cannot be written.
OSError: if byte stream cannot be read.
"""
try:
return self._struct.pack(*values)
except (TypeError, struct.error) as exception:
raise IOError('Unable to write stream with error: {0!s}'.format(
exception))
|
[
"def",
"WriteTo",
"(",
"self",
",",
"values",
")",
":",
"try",
":",
"return",
"self",
".",
"_struct",
".",
"pack",
"(",
"*",
"values",
")",
"except",
"(",
"TypeError",
",",
"struct",
".",
"error",
")",
"as",
"exception",
":",
"raise",
"IOError",
"(",
"'Unable to write stream with error: {0!s}'",
".",
"format",
"(",
"exception",
")",
")"
] |
Writes values to a byte stream.
Args:
values (tuple[object, ...]): values to copy to the byte stream.
Returns:
bytes: byte stream.
Raises:
IOError: if byte stream cannot be written.
OSError: if byte stream cannot be read.
|
[
"Writes",
"values",
"to",
"a",
"byte",
"stream",
"."
] |
0d2b5719fa257f6e5c661a406737ebcf8c8db266
|
https://github.com/libyal/dtfabric/blob/0d2b5719fa257f6e5c661a406737ebcf8c8db266/dtfabric/runtime/byte_operations.py#L84-L101
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/snapshot_manager.py
|
run
|
def run(connection):
""" Ensure that we have snapshots for a given volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
"""
volumes = volume_manager.get_watched_volumes(connection)
for volume in volumes:
_ensure_snapshot(connection, volume)
_remove_old_snapshots(connection, volume)
|
python
|
def run(connection):
""" Ensure that we have snapshots for a given volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
"""
volumes = volume_manager.get_watched_volumes(connection)
for volume in volumes:
_ensure_snapshot(connection, volume)
_remove_old_snapshots(connection, volume)
|
[
"def",
"run",
"(",
"connection",
")",
":",
"volumes",
"=",
"volume_manager",
".",
"get_watched_volumes",
"(",
"connection",
")",
"for",
"volume",
"in",
"volumes",
":",
"_ensure_snapshot",
"(",
"connection",
",",
"volume",
")",
"_remove_old_snapshots",
"(",
"connection",
",",
"volume",
")"
] |
Ensure that we have snapshots for a given volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
|
[
"Ensure",
"that",
"we",
"have",
"snapshots",
"for",
"a",
"given",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/snapshot_manager.py#L13-L24
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/snapshot_manager.py
|
_create_snapshot
|
def _create_snapshot(volume):
""" Create a new snapshot
:type volume: boto.ec2.volume.Volume
:param volume: Volume to snapshot
:returns: boto.ec2.snapshot.Snapshot -- The new snapshot
"""
logger.info('Creating new snapshot for {}'.format(volume.id))
snapshot = volume.create_snapshot(
description="Automatic snapshot by Automated EBS Snapshots")
logger.info('Created snapshot {} for volume {}'.format(
snapshot.id, volume.id))
return snapshot
|
python
|
def _create_snapshot(volume):
""" Create a new snapshot
:type volume: boto.ec2.volume.Volume
:param volume: Volume to snapshot
:returns: boto.ec2.snapshot.Snapshot -- The new snapshot
"""
logger.info('Creating new snapshot for {}'.format(volume.id))
snapshot = volume.create_snapshot(
description="Automatic snapshot by Automated EBS Snapshots")
logger.info('Created snapshot {} for volume {}'.format(
snapshot.id, volume.id))
return snapshot
|
[
"def",
"_create_snapshot",
"(",
"volume",
")",
":",
"logger",
".",
"info",
"(",
"'Creating new snapshot for {}'",
".",
"format",
"(",
"volume",
".",
"id",
")",
")",
"snapshot",
"=",
"volume",
".",
"create_snapshot",
"(",
"description",
"=",
"\"Automatic snapshot by Automated EBS Snapshots\"",
")",
"logger",
".",
"info",
"(",
"'Created snapshot {} for volume {}'",
".",
"format",
"(",
"snapshot",
".",
"id",
",",
"volume",
".",
"id",
")",
")",
"return",
"snapshot"
] |
Create a new snapshot
:type volume: boto.ec2.volume.Volume
:param volume: Volume to snapshot
:returns: boto.ec2.snapshot.Snapshot -- The new snapshot
|
[
"Create",
"a",
"new",
"snapshot"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/snapshot_manager.py#L27-L40
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/snapshot_manager.py
|
_ensure_snapshot
|
def _ensure_snapshot(connection, volume):
""" Ensure that a given volume has an appropriate snapshot
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
"""
if 'AutomatedEBSSnapshots' not in volume.tags:
logger.warning(
'Missing tag AutomatedEBSSnapshots for volume {}'.format(
volume.id))
return
interval = volume.tags['AutomatedEBSSnapshots']
if volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:
logger.warning(
'"{}" is not a valid snapshotting interval for volume {}'.format(
interval, volume.id))
return
snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})
# Create a snapshot if we don't have any
if not snapshots:
_create_snapshot(volume)
return
min_delta = 3600*24*365*10 # 10 years :)
for snapshot in snapshots:
timestamp = datetime.datetime.strptime(
snapshot.start_time,
'%Y-%m-%dT%H:%M:%S.000Z')
delta_seconds = int(
(datetime.datetime.utcnow() - timestamp).total_seconds())
if delta_seconds < min_delta:
min_delta = delta_seconds
logger.info('The newest snapshot for {} is {} seconds old'.format(
volume.id, min_delta))
if interval == 'hourly' and min_delta > 3600:
_create_snapshot(volume)
elif interval == 'daily' and min_delta > 3600*24:
_create_snapshot(volume)
elif interval == 'weekly' and min_delta > 3600*24*7:
_create_snapshot(volume)
elif interval == 'monthly' and min_delta > 3600*24*30:
_create_snapshot(volume)
elif interval == 'yearly' and min_delta > 3600*24*365:
_create_snapshot(volume)
else:
logger.info('No need for a new snapshot of {}'.format(volume.id))
|
python
|
def _ensure_snapshot(connection, volume):
""" Ensure that a given volume has an appropriate snapshot
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
"""
if 'AutomatedEBSSnapshots' not in volume.tags:
logger.warning(
'Missing tag AutomatedEBSSnapshots for volume {}'.format(
volume.id))
return
interval = volume.tags['AutomatedEBSSnapshots']
if volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:
logger.warning(
'"{}" is not a valid snapshotting interval for volume {}'.format(
interval, volume.id))
return
snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})
# Create a snapshot if we don't have any
if not snapshots:
_create_snapshot(volume)
return
min_delta = 3600*24*365*10 # 10 years :)
for snapshot in snapshots:
timestamp = datetime.datetime.strptime(
snapshot.start_time,
'%Y-%m-%dT%H:%M:%S.000Z')
delta_seconds = int(
(datetime.datetime.utcnow() - timestamp).total_seconds())
if delta_seconds < min_delta:
min_delta = delta_seconds
logger.info('The newest snapshot for {} is {} seconds old'.format(
volume.id, min_delta))
if interval == 'hourly' and min_delta > 3600:
_create_snapshot(volume)
elif interval == 'daily' and min_delta > 3600*24:
_create_snapshot(volume)
elif interval == 'weekly' and min_delta > 3600*24*7:
_create_snapshot(volume)
elif interval == 'monthly' and min_delta > 3600*24*30:
_create_snapshot(volume)
elif interval == 'yearly' and min_delta > 3600*24*365:
_create_snapshot(volume)
else:
logger.info('No need for a new snapshot of {}'.format(volume.id))
|
[
"def",
"_ensure_snapshot",
"(",
"connection",
",",
"volume",
")",
":",
"if",
"'AutomatedEBSSnapshots'",
"not",
"in",
"volume",
".",
"tags",
":",
"logger",
".",
"warning",
"(",
"'Missing tag AutomatedEBSSnapshots for volume {}'",
".",
"format",
"(",
"volume",
".",
"id",
")",
")",
"return",
"interval",
"=",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshots'",
"]",
"if",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshots'",
"]",
"not",
"in",
"VALID_INTERVALS",
":",
"logger",
".",
"warning",
"(",
"'\"{}\" is not a valid snapshotting interval for volume {}'",
".",
"format",
"(",
"interval",
",",
"volume",
".",
"id",
")",
")",
"return",
"snapshots",
"=",
"connection",
".",
"get_all_snapshots",
"(",
"filters",
"=",
"{",
"'volume-id'",
":",
"volume",
".",
"id",
"}",
")",
"# Create a snapshot if we don't have any",
"if",
"not",
"snapshots",
":",
"_create_snapshot",
"(",
"volume",
")",
"return",
"min_delta",
"=",
"3600",
"*",
"24",
"*",
"365",
"*",
"10",
"# 10 years :)",
"for",
"snapshot",
"in",
"snapshots",
":",
"timestamp",
"=",
"datetime",
".",
"datetime",
".",
"strptime",
"(",
"snapshot",
".",
"start_time",
",",
"'%Y-%m-%dT%H:%M:%S.000Z'",
")",
"delta_seconds",
"=",
"int",
"(",
"(",
"datetime",
".",
"datetime",
".",
"utcnow",
"(",
")",
"-",
"timestamp",
")",
".",
"total_seconds",
"(",
")",
")",
"if",
"delta_seconds",
"<",
"min_delta",
":",
"min_delta",
"=",
"delta_seconds",
"logger",
".",
"info",
"(",
"'The newest snapshot for {} is {} seconds old'",
".",
"format",
"(",
"volume",
".",
"id",
",",
"min_delta",
")",
")",
"if",
"interval",
"==",
"'hourly'",
"and",
"min_delta",
">",
"3600",
":",
"_create_snapshot",
"(",
"volume",
")",
"elif",
"interval",
"==",
"'daily'",
"and",
"min_delta",
">",
"3600",
"*",
"24",
":",
"_create_snapshot",
"(",
"volume",
")",
"elif",
"interval",
"==",
"'weekly'",
"and",
"min_delta",
">",
"3600",
"*",
"24",
"*",
"7",
":",
"_create_snapshot",
"(",
"volume",
")",
"elif",
"interval",
"==",
"'monthly'",
"and",
"min_delta",
">",
"3600",
"*",
"24",
"*",
"30",
":",
"_create_snapshot",
"(",
"volume",
")",
"elif",
"interval",
"==",
"'yearly'",
"and",
"min_delta",
">",
"3600",
"*",
"24",
"*",
"365",
":",
"_create_snapshot",
"(",
"volume",
")",
"else",
":",
"logger",
".",
"info",
"(",
"'No need for a new snapshot of {}'",
".",
"format",
"(",
"volume",
".",
"id",
")",
")"
] |
Ensure that a given volume has an appropriate snapshot
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
|
[
"Ensure",
"that",
"a",
"given",
"volume",
"has",
"an",
"appropriate",
"snapshot"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/snapshot_manager.py#L43-L96
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/snapshot_manager.py
|
_remove_old_snapshots
|
def _remove_old_snapshots(connection, volume):
""" Remove old snapshots
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
"""
if 'AutomatedEBSSnapshotsRetention' not in volume.tags:
logger.warning(
'Missing tag AutomatedEBSSnapshotsRetention for volume {}'.format(
volume.id))
return
retention = int(volume.tags['AutomatedEBSSnapshotsRetention'])
snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})
# Sort the list based on the start time
snapshots.sort(key=lambda x: x.start_time)
# Remove snapshots we want to keep
snapshots = snapshots[:-int(retention)]
if not snapshots:
logger.info('No old snapshots to remove')
return
for snapshot in snapshots:
logger.info('Deleting snapshot {}'.format(snapshot.id))
try:
snapshot.delete()
except EC2ResponseError as error:
logger.warning('Could not remove snapshot: {}'.format(
error.message))
logger.info('Done deleting snapshots')
|
python
|
def _remove_old_snapshots(connection, volume):
""" Remove old snapshots
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
"""
if 'AutomatedEBSSnapshotsRetention' not in volume.tags:
logger.warning(
'Missing tag AutomatedEBSSnapshotsRetention for volume {}'.format(
volume.id))
return
retention = int(volume.tags['AutomatedEBSSnapshotsRetention'])
snapshots = connection.get_all_snapshots(filters={'volume-id': volume.id})
# Sort the list based on the start time
snapshots.sort(key=lambda x: x.start_time)
# Remove snapshots we want to keep
snapshots = snapshots[:-int(retention)]
if not snapshots:
logger.info('No old snapshots to remove')
return
for snapshot in snapshots:
logger.info('Deleting snapshot {}'.format(snapshot.id))
try:
snapshot.delete()
except EC2ResponseError as error:
logger.warning('Could not remove snapshot: {}'.format(
error.message))
logger.info('Done deleting snapshots')
|
[
"def",
"_remove_old_snapshots",
"(",
"connection",
",",
"volume",
")",
":",
"if",
"'AutomatedEBSSnapshotsRetention'",
"not",
"in",
"volume",
".",
"tags",
":",
"logger",
".",
"warning",
"(",
"'Missing tag AutomatedEBSSnapshotsRetention for volume {}'",
".",
"format",
"(",
"volume",
".",
"id",
")",
")",
"return",
"retention",
"=",
"int",
"(",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshotsRetention'",
"]",
")",
"snapshots",
"=",
"connection",
".",
"get_all_snapshots",
"(",
"filters",
"=",
"{",
"'volume-id'",
":",
"volume",
".",
"id",
"}",
")",
"# Sort the list based on the start time",
"snapshots",
".",
"sort",
"(",
"key",
"=",
"lambda",
"x",
":",
"x",
".",
"start_time",
")",
"# Remove snapshots we want to keep",
"snapshots",
"=",
"snapshots",
"[",
":",
"-",
"int",
"(",
"retention",
")",
"]",
"if",
"not",
"snapshots",
":",
"logger",
".",
"info",
"(",
"'No old snapshots to remove'",
")",
"return",
"for",
"snapshot",
"in",
"snapshots",
":",
"logger",
".",
"info",
"(",
"'Deleting snapshot {}'",
".",
"format",
"(",
"snapshot",
".",
"id",
")",
")",
"try",
":",
"snapshot",
".",
"delete",
"(",
")",
"except",
"EC2ResponseError",
"as",
"error",
":",
"logger",
".",
"warning",
"(",
"'Could not remove snapshot: {}'",
".",
"format",
"(",
"error",
".",
"message",
")",
")",
"logger",
".",
"info",
"(",
"'Done deleting snapshots'",
")"
] |
Remove old snapshots
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: boto.ec2.volume.Volume
:param volume: Volume to check
:returns: None
|
[
"Remove",
"old",
"snapshots"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/snapshot_manager.py#L99-L135
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
list
|
def list(connection):
""" List watched EBS volumes
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
"""
volumes = get_watched_volumes(connection)
if not volumes:
logger.info('No watched volumes found')
return
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
logger.info(
'| {volume:<21} '
'| {volume_name:<20.20} '
'| {interval:<12} '
'| {retention:<10} |'.format(
volume='Volume ID',
volume_name='Volume name',
interval='Interval',
retention='Retention'))
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
for volume in volumes:
if 'AutomatedEBSSnapshots' not in volume.tags:
interval = 'Interval tag not found'
elif volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:
interval = 'Invalid interval'
else:
interval = volume.tags['AutomatedEBSSnapshots']
if 'AutomatedEBSSnapshotsRetention' not in volume.tags:
retention = 0
else:
retention = volume.tags['AutomatedEBSSnapshotsRetention']
# Get the volume name
try:
volume_name = volume.tags['Name']
except KeyError:
volume_name = ''
logger.info(
'| {volume_id:<14} '
'| {volume_name:<20.20} '
'| {interval:<12} '
'| {retention:<10} |'.format(
volume_id=volume.id,
volume_name=volume_name,
interval=interval,
retention=retention))
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
|
python
|
def list(connection):
""" List watched EBS volumes
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
"""
volumes = get_watched_volumes(connection)
if not volumes:
logger.info('No watched volumes found')
return
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
logger.info(
'| {volume:<21} '
'| {volume_name:<20.20} '
'| {interval:<12} '
'| {retention:<10} |'.format(
volume='Volume ID',
volume_name='Volume name',
interval='Interval',
retention='Retention'))
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
for volume in volumes:
if 'AutomatedEBSSnapshots' not in volume.tags:
interval = 'Interval tag not found'
elif volume.tags['AutomatedEBSSnapshots'] not in VALID_INTERVALS:
interval = 'Invalid interval'
else:
interval = volume.tags['AutomatedEBSSnapshots']
if 'AutomatedEBSSnapshotsRetention' not in volume.tags:
retention = 0
else:
retention = volume.tags['AutomatedEBSSnapshotsRetention']
# Get the volume name
try:
volume_name = volume.tags['Name']
except KeyError:
volume_name = ''
logger.info(
'| {volume_id:<14} '
'| {volume_name:<20.20} '
'| {interval:<12} '
'| {retention:<10} |'.format(
volume_id=volume.id,
volume_name=volume_name,
interval=interval,
retention=retention))
logger.info(
'+-----------------------'
'+----------------------'
'+--------------'
'+------------+')
|
[
"def",
"list",
"(",
"connection",
")",
":",
"volumes",
"=",
"get_watched_volumes",
"(",
"connection",
")",
"if",
"not",
"volumes",
":",
"logger",
".",
"info",
"(",
"'No watched volumes found'",
")",
"return",
"logger",
".",
"info",
"(",
"'+-----------------------'",
"'+----------------------'",
"'+--------------'",
"'+------------+'",
")",
"logger",
".",
"info",
"(",
"'| {volume:<21} '",
"'| {volume_name:<20.20} '",
"'| {interval:<12} '",
"'| {retention:<10} |'",
".",
"format",
"(",
"volume",
"=",
"'Volume ID'",
",",
"volume_name",
"=",
"'Volume name'",
",",
"interval",
"=",
"'Interval'",
",",
"retention",
"=",
"'Retention'",
")",
")",
"logger",
".",
"info",
"(",
"'+-----------------------'",
"'+----------------------'",
"'+--------------'",
"'+------------+'",
")",
"for",
"volume",
"in",
"volumes",
":",
"if",
"'AutomatedEBSSnapshots'",
"not",
"in",
"volume",
".",
"tags",
":",
"interval",
"=",
"'Interval tag not found'",
"elif",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshots'",
"]",
"not",
"in",
"VALID_INTERVALS",
":",
"interval",
"=",
"'Invalid interval'",
"else",
":",
"interval",
"=",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshots'",
"]",
"if",
"'AutomatedEBSSnapshotsRetention'",
"not",
"in",
"volume",
".",
"tags",
":",
"retention",
"=",
"0",
"else",
":",
"retention",
"=",
"volume",
".",
"tags",
"[",
"'AutomatedEBSSnapshotsRetention'",
"]",
"# Get the volume name",
"try",
":",
"volume_name",
"=",
"volume",
".",
"tags",
"[",
"'Name'",
"]",
"except",
"KeyError",
":",
"volume_name",
"=",
"''",
"logger",
".",
"info",
"(",
"'| {volume_id:<14} '",
"'| {volume_name:<20.20} '",
"'| {interval:<12} '",
"'| {retention:<10} |'",
".",
"format",
"(",
"volume_id",
"=",
"volume",
".",
"id",
",",
"volume_name",
"=",
"volume_name",
",",
"interval",
"=",
"interval",
",",
"retention",
"=",
"retention",
")",
")",
"logger",
".",
"info",
"(",
"'+-----------------------'",
"'+----------------------'",
"'+--------------'",
"'+------------+'",
")"
] |
List watched EBS volumes
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:returns: None
|
[
"List",
"watched",
"EBS",
"volumes"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L24-L90
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
unwatch
|
def unwatch(connection, volume_id):
""" Remove watching of a volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
volume.remove_tag('AutomatedEBSSnapshots')
except EC2ResponseError:
pass
logger.info('Removed {} from the watchlist'.format(volume_id))
return True
|
python
|
def unwatch(connection, volume_id):
""" Remove watching of a volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
volume.remove_tag('AutomatedEBSSnapshots')
except EC2ResponseError:
pass
logger.info('Removed {} from the watchlist'.format(volume_id))
return True
|
[
"def",
"unwatch",
"(",
"connection",
",",
"volume_id",
")",
":",
"try",
":",
"volume",
"=",
"connection",
".",
"get_all_volumes",
"(",
"volume_ids",
"=",
"[",
"volume_id",
"]",
")",
"[",
"0",
"]",
"volume",
".",
"remove_tag",
"(",
"'AutomatedEBSSnapshots'",
")",
"except",
"EC2ResponseError",
":",
"pass",
"logger",
".",
"info",
"(",
"'Removed {} from the watchlist'",
".",
"format",
"(",
"volume_id",
")",
")",
"return",
"True"
] |
Remove watching of a volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:returns: bool - True if the watch was successful
|
[
"Remove",
"watching",
"of",
"a",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L93-L110
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
watch
|
def watch(connection, volume_id, interval='daily', retention=0):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:type interval: str
:param interval: Backup interval [hourly|daily|weekly|monthly|yearly]
:type retention: int
:param retention: Number of snapshots to keep. 0 == keep all
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume_id))
return False
if interval not in VALID_INTERVALS:
logger.warning(
'{} is not a valid interval. Valid intervals are {}'.format(
interval, ', '.join(VALID_INTERVALS)))
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshots')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshots', value=interval)
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshotsRetention')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshotsRetention', value=int(retention))
logger.info('Updated the rotation interval to {} for {}'.format(
interval, volume_id))
return True
|
python
|
def watch(connection, volume_id, interval='daily', retention=0):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:type interval: str
:param interval: Backup interval [hourly|daily|weekly|monthly|yearly]
:type retention: int
:param retention: Number of snapshots to keep. 0 == keep all
:returns: bool - True if the watch was successful
"""
try:
volume = connection.get_all_volumes(volume_ids=[volume_id])[0]
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume_id))
return False
if interval not in VALID_INTERVALS:
logger.warning(
'{} is not a valid interval. Valid intervals are {}'.format(
interval, ', '.join(VALID_INTERVALS)))
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshots')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshots', value=interval)
# Remove the tag first
volume.remove_tag('AutomatedEBSSnapshotsRetention')
# Re-add the tag
volume.add_tag('AutomatedEBSSnapshotsRetention', value=int(retention))
logger.info('Updated the rotation interval to {} for {}'.format(
interval, volume_id))
return True
|
[
"def",
"watch",
"(",
"connection",
",",
"volume_id",
",",
"interval",
"=",
"'daily'",
",",
"retention",
"=",
"0",
")",
":",
"try",
":",
"volume",
"=",
"connection",
".",
"get_all_volumes",
"(",
"volume_ids",
"=",
"[",
"volume_id",
"]",
")",
"[",
"0",
"]",
"except",
"EC2ResponseError",
":",
"logger",
".",
"warning",
"(",
"'Volume {} not found'",
".",
"format",
"(",
"volume_id",
")",
")",
"return",
"False",
"if",
"interval",
"not",
"in",
"VALID_INTERVALS",
":",
"logger",
".",
"warning",
"(",
"'{} is not a valid interval. Valid intervals are {}'",
".",
"format",
"(",
"interval",
",",
"', '",
".",
"join",
"(",
"VALID_INTERVALS",
")",
")",
")",
"# Remove the tag first",
"volume",
".",
"remove_tag",
"(",
"'AutomatedEBSSnapshots'",
")",
"# Re-add the tag",
"volume",
".",
"add_tag",
"(",
"'AutomatedEBSSnapshots'",
",",
"value",
"=",
"interval",
")",
"# Remove the tag first",
"volume",
".",
"remove_tag",
"(",
"'AutomatedEBSSnapshotsRetention'",
")",
"# Re-add the tag",
"volume",
".",
"add_tag",
"(",
"'AutomatedEBSSnapshotsRetention'",
",",
"value",
"=",
"int",
"(",
"retention",
")",
")",
"logger",
".",
"info",
"(",
"'Updated the rotation interval to {} for {}'",
".",
"format",
"(",
"interval",
",",
"volume_id",
")",
")",
"return",
"True"
] |
Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume_id: str
:param volume_id: VolumeID to add to the watchlist
:type interval: str
:param interval: Backup interval [hourly|daily|weekly|monthly|yearly]
:type retention: int
:param retention: Number of snapshots to keep. 0 == keep all
:returns: bool - True if the watch was successful
|
[
"Start",
"watching",
"a",
"new",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L113-L152
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
get_volume_id
|
def get_volume_id(connection, volume):
"""
Get Volume ID from the given volume. Input can be volume id
or its Name tag.
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: Volume ID or None if the given volume does not exist
"""
# Regular expression to check whether input is a volume id
volume_id_pattern = re.compile('vol-\w{8}')
if volume_id_pattern.match(volume):
# input is volume id
try:
# Check whether it exists
connection.get_all_volumes(volume_ids=[volume])
volume_id = volume
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume))
return None
else:
# input is volume name
name_filter = {'tag-key': 'Name', 'tag-value': volume}
volumes = connection.get_all_volumes(filters=name_filter)
if not volumes:
logger.warning('Volume {} not found'.format(volume))
return None
if len(volumes) > 1:
logger.warning('Volume {} not unique'.format(volume))
volume_id = volumes[0].id
return volume_id
|
python
|
def get_volume_id(connection, volume):
"""
Get Volume ID from the given volume. Input can be volume id
or its Name tag.
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: Volume ID or None if the given volume does not exist
"""
# Regular expression to check whether input is a volume id
volume_id_pattern = re.compile('vol-\w{8}')
if volume_id_pattern.match(volume):
# input is volume id
try:
# Check whether it exists
connection.get_all_volumes(volume_ids=[volume])
volume_id = volume
except EC2ResponseError:
logger.warning('Volume {} not found'.format(volume))
return None
else:
# input is volume name
name_filter = {'tag-key': 'Name', 'tag-value': volume}
volumes = connection.get_all_volumes(filters=name_filter)
if not volumes:
logger.warning('Volume {} not found'.format(volume))
return None
if len(volumes) > 1:
logger.warning('Volume {} not unique'.format(volume))
volume_id = volumes[0].id
return volume_id
|
[
"def",
"get_volume_id",
"(",
"connection",
",",
"volume",
")",
":",
"# Regular expression to check whether input is a volume id",
"volume_id_pattern",
"=",
"re",
".",
"compile",
"(",
"'vol-\\w{8}'",
")",
"if",
"volume_id_pattern",
".",
"match",
"(",
"volume",
")",
":",
"# input is volume id",
"try",
":",
"# Check whether it exists",
"connection",
".",
"get_all_volumes",
"(",
"volume_ids",
"=",
"[",
"volume",
"]",
")",
"volume_id",
"=",
"volume",
"except",
"EC2ResponseError",
":",
"logger",
".",
"warning",
"(",
"'Volume {} not found'",
".",
"format",
"(",
"volume",
")",
")",
"return",
"None",
"else",
":",
"# input is volume name",
"name_filter",
"=",
"{",
"'tag-key'",
":",
"'Name'",
",",
"'tag-value'",
":",
"volume",
"}",
"volumes",
"=",
"connection",
".",
"get_all_volumes",
"(",
"filters",
"=",
"name_filter",
")",
"if",
"not",
"volumes",
":",
"logger",
".",
"warning",
"(",
"'Volume {} not found'",
".",
"format",
"(",
"volume",
")",
")",
"return",
"None",
"if",
"len",
"(",
"volumes",
")",
">",
"1",
":",
"logger",
".",
"warning",
"(",
"'Volume {} not unique'",
".",
"format",
"(",
"volume",
")",
")",
"volume_id",
"=",
"volumes",
"[",
"0",
"]",
".",
"id",
"return",
"volume_id"
] |
Get Volume ID from the given volume. Input can be volume id
or its Name tag.
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: Volume ID or None if the given volume does not exist
|
[
"Get",
"Volume",
"ID",
"from",
"the",
"given",
"volume",
".",
"Input",
"can",
"be",
"volume",
"id",
"or",
"its",
"Name",
"tag",
"."
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L155-L189
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
watch_from_file
|
def watch_from_file(connection, file_name):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
"""
with open(file_name, 'r') as filehandle:
for line in filehandle.xreadlines():
volume, interval, retention = line.rstrip().split(',')
watch(
connection,
get_volume_id(connection, volume),
interval, retention)
|
python
|
def watch_from_file(connection, file_name):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
"""
with open(file_name, 'r') as filehandle:
for line in filehandle.xreadlines():
volume, interval, retention = line.rstrip().split(',')
watch(
connection,
get_volume_id(connection, volume),
interval, retention)
|
[
"def",
"watch_from_file",
"(",
"connection",
",",
"file_name",
")",
":",
"with",
"open",
"(",
"file_name",
",",
"'r'",
")",
"as",
"filehandle",
":",
"for",
"line",
"in",
"filehandle",
".",
"xreadlines",
"(",
")",
":",
"volume",
",",
"interval",
",",
"retention",
"=",
"line",
".",
"rstrip",
"(",
")",
".",
"split",
"(",
"','",
")",
"watch",
"(",
"connection",
",",
"get_volume_id",
"(",
"connection",
",",
"volume",
")",
",",
"interval",
",",
"retention",
")"
] |
Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
|
[
"Start",
"watching",
"a",
"new",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L192-L207
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
unwatch_from_file
|
def unwatch_from_file(connection, file_name):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
"""
with open(file_name, 'r') as filehandle:
for line in filehandle.xreadlines():
volume, interval, retention = line.rstrip().split(',')
unwatch(connection, get_volume_id(connection, volume))
|
python
|
def unwatch_from_file(connection, file_name):
""" Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
"""
with open(file_name, 'r') as filehandle:
for line in filehandle.xreadlines():
volume, interval, retention = line.rstrip().split(',')
unwatch(connection, get_volume_id(connection, volume))
|
[
"def",
"unwatch_from_file",
"(",
"connection",
",",
"file_name",
")",
":",
"with",
"open",
"(",
"file_name",
",",
"'r'",
")",
"as",
"filehandle",
":",
"for",
"line",
"in",
"filehandle",
".",
"xreadlines",
"(",
")",
":",
"volume",
",",
"interval",
",",
"retention",
"=",
"line",
".",
"rstrip",
"(",
")",
".",
"split",
"(",
"','",
")",
"unwatch",
"(",
"connection",
",",
"get_volume_id",
"(",
"connection",
",",
"volume",
")",
")"
] |
Start watching a new volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type file_name: str
:param file_name: path to config file
:returns: None
|
[
"Start",
"watching",
"a",
"new",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L210-L222
|
train
|
skymill/automated-ebs-snapshots
|
automated_ebs_snapshots/volume_manager.py
|
list_snapshots
|
def list_snapshots(connection, volume):
""" List all snapshots for the volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: None
"""
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
logger.info(
'| {snapshot:<14} '
'| {snapshot_name:<20.20} '
'| {created:<25} |'.format(
snapshot='Snapshot ID',
snapshot_name='Snapshot name',
created='Created'))
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
vid = get_volume_id(connection, volume)
if vid:
vol = connection.get_all_volumes(volume_ids=[vid])[0]
for snap in vol.snapshots():
logger.info(
'| {snapshot:<14} '
'| {snapshot_name:<20.20} '
'| {created:<25} |'.format(
snapshot=snap.id,
snapshot_name=snap.tags.get('Name', ''),
created=snap.start_time))
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
|
python
|
def list_snapshots(connection, volume):
""" List all snapshots for the volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: None
"""
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
logger.info(
'| {snapshot:<14} '
'| {snapshot_name:<20.20} '
'| {created:<25} |'.format(
snapshot='Snapshot ID',
snapshot_name='Snapshot name',
created='Created'))
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
vid = get_volume_id(connection, volume)
if vid:
vol = connection.get_all_volumes(volume_ids=[vid])[0]
for snap in vol.snapshots():
logger.info(
'| {snapshot:<14} '
'| {snapshot_name:<20.20} '
'| {created:<25} |'.format(
snapshot=snap.id,
snapshot_name=snap.tags.get('Name', ''),
created=snap.start_time))
logger.info(
'+----------------'
'+----------------------'
'+---------------------------+')
|
[
"def",
"list_snapshots",
"(",
"connection",
",",
"volume",
")",
":",
"logger",
".",
"info",
"(",
"'+----------------'",
"'+----------------------'",
"'+---------------------------+'",
")",
"logger",
".",
"info",
"(",
"'| {snapshot:<14} '",
"'| {snapshot_name:<20.20} '",
"'| {created:<25} |'",
".",
"format",
"(",
"snapshot",
"=",
"'Snapshot ID'",
",",
"snapshot_name",
"=",
"'Snapshot name'",
",",
"created",
"=",
"'Created'",
")",
")",
"logger",
".",
"info",
"(",
"'+----------------'",
"'+----------------------'",
"'+---------------------------+'",
")",
"vid",
"=",
"get_volume_id",
"(",
"connection",
",",
"volume",
")",
"if",
"vid",
":",
"vol",
"=",
"connection",
".",
"get_all_volumes",
"(",
"volume_ids",
"=",
"[",
"vid",
"]",
")",
"[",
"0",
"]",
"for",
"snap",
"in",
"vol",
".",
"snapshots",
"(",
")",
":",
"logger",
".",
"info",
"(",
"'| {snapshot:<14} '",
"'| {snapshot_name:<20.20} '",
"'| {created:<25} |'",
".",
"format",
"(",
"snapshot",
"=",
"snap",
".",
"id",
",",
"snapshot_name",
"=",
"snap",
".",
"tags",
".",
"get",
"(",
"'Name'",
",",
"''",
")",
",",
"created",
"=",
"snap",
".",
"start_time",
")",
")",
"logger",
".",
"info",
"(",
"'+----------------'",
"'+----------------------'",
"'+---------------------------+'",
")"
] |
List all snapshots for the volume
:type connection: boto.ec2.connection.EC2Connection
:param connection: EC2 connection object
:type volume: str
:param volume: Volume ID or Volume Name
:returns: None
|
[
"List",
"all",
"snapshots",
"for",
"the",
"volume"
] |
9595bc49d458f6ffb93430722757d2284e878fab
|
https://github.com/skymill/automated-ebs-snapshots/blob/9595bc49d458f6ffb93430722757d2284e878fab/automated_ebs_snapshots/volume_manager.py#L225-L266
|
train
|
dmirecki/pyMorfologik
|
pymorfologik/morfologik.py
|
Morfologik.stem
|
def stem(self, words, parser, **kwargs):
"""
Get stems for the words using a given parser
Example:
from .parsing import ListParser
parser = ListParser()
stemmer = Morfologik()
stemmer.stem(['ja tańczę a ona śpi], parser)
[
('ja': ['ja']),
('tańczę': ['tańczyć']),
('a': ['a']),
('ona': ['on']),
('śpi': ['spać'])
]
"""
output = self._run_morfologik(words)
return parser.parse(output, **kwargs)
|
python
|
def stem(self, words, parser, **kwargs):
"""
Get stems for the words using a given parser
Example:
from .parsing import ListParser
parser = ListParser()
stemmer = Morfologik()
stemmer.stem(['ja tańczę a ona śpi], parser)
[
('ja': ['ja']),
('tańczę': ['tańczyć']),
('a': ['a']),
('ona': ['on']),
('śpi': ['spać'])
]
"""
output = self._run_morfologik(words)
return parser.parse(output, **kwargs)
|
[
"def",
"stem",
"(",
"self",
",",
"words",
",",
"parser",
",",
"*",
"*",
"kwargs",
")",
":",
"output",
"=",
"self",
".",
"_run_morfologik",
"(",
"words",
")",
"return",
"parser",
".",
"parse",
"(",
"output",
",",
"*",
"*",
"kwargs",
")"
] |
Get stems for the words using a given parser
Example:
from .parsing import ListParser
parser = ListParser()
stemmer = Morfologik()
stemmer.stem(['ja tańczę a ona śpi], parser)
[
('ja': ['ja']),
('tańczę': ['tańczyć']),
('a': ['a']),
('ona': ['on']),
('śpi': ['spać'])
]
|
[
"Get",
"stems",
"for",
"the",
"words",
"using",
"a",
"given",
"parser"
] |
e4d93a82e8b4c7a108f01e0456fbeb8024df0259
|
https://github.com/dmirecki/pyMorfologik/blob/e4d93a82e8b4c7a108f01e0456fbeb8024df0259/pymorfologik/morfologik.py#L29-L49
|
train
|
dmirecki/pyMorfologik
|
pymorfologik/morfologik.py
|
Morfologik._run_morfologik
|
def _run_morfologik(self, words):
"""
Runs morfologik java jar and assumes that input and output is
UTF-8 encoded.
"""
p = subprocess.Popen(
['java', '-jar', self.jar_path, 'plstem',
'-ie', 'UTF-8',
'-oe', 'UTF-8'],
bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, _ = p.communicate(input=bytes("\n".join(words), "utf-8"))
return decode(out, 'utf-8')
|
python
|
def _run_morfologik(self, words):
"""
Runs morfologik java jar and assumes that input and output is
UTF-8 encoded.
"""
p = subprocess.Popen(
['java', '-jar', self.jar_path, 'plstem',
'-ie', 'UTF-8',
'-oe', 'UTF-8'],
bufsize=-1,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
out, _ = p.communicate(input=bytes("\n".join(words), "utf-8"))
return decode(out, 'utf-8')
|
[
"def",
"_run_morfologik",
"(",
"self",
",",
"words",
")",
":",
"p",
"=",
"subprocess",
".",
"Popen",
"(",
"[",
"'java'",
",",
"'-jar'",
",",
"self",
".",
"jar_path",
",",
"'plstem'",
",",
"'-ie'",
",",
"'UTF-8'",
",",
"'-oe'",
",",
"'UTF-8'",
"]",
",",
"bufsize",
"=",
"-",
"1",
",",
"stdin",
"=",
"subprocess",
".",
"PIPE",
",",
"stdout",
"=",
"subprocess",
".",
"PIPE",
",",
"stderr",
"=",
"subprocess",
".",
"STDOUT",
")",
"out",
",",
"_",
"=",
"p",
".",
"communicate",
"(",
"input",
"=",
"bytes",
"(",
"\"\\n\"",
".",
"join",
"(",
"words",
")",
",",
"\"utf-8\"",
")",
")",
"return",
"decode",
"(",
"out",
",",
"'utf-8'",
")"
] |
Runs morfologik java jar and assumes that input and output is
UTF-8 encoded.
|
[
"Runs",
"morfologik",
"java",
"jar",
"and",
"assumes",
"that",
"input",
"and",
"output",
"is",
"UTF",
"-",
"8",
"encoded",
"."
] |
e4d93a82e8b4c7a108f01e0456fbeb8024df0259
|
https://github.com/dmirecki/pyMorfologik/blob/e4d93a82e8b4c7a108f01e0456fbeb8024df0259/pymorfologik/morfologik.py#L52-L66
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/showcase.py
|
Showcase.read_from_hdx
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['Showcase']
"""Reads the showcase given by identifier from HDX and returns Showcase object
Args:
identifier (str): Identifier of showcase
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[Showcase]: Showcase object if successful read, None if not
"""
showcase = Showcase(configuration=configuration)
result = showcase._load_from_hdx('showcase', identifier)
if result:
return showcase
return None
|
python
|
def read_from_hdx(identifier, configuration=None):
# type: (str, Optional[Configuration]) -> Optional['Showcase']
"""Reads the showcase given by identifier from HDX and returns Showcase object
Args:
identifier (str): Identifier of showcase
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[Showcase]: Showcase object if successful read, None if not
"""
showcase = Showcase(configuration=configuration)
result = showcase._load_from_hdx('showcase', identifier)
if result:
return showcase
return None
|
[
"def",
"read_from_hdx",
"(",
"identifier",
",",
"configuration",
"=",
"None",
")",
":",
"# type: (str, Optional[Configuration]) -> Optional['Showcase']",
"showcase",
"=",
"Showcase",
"(",
"configuration",
"=",
"configuration",
")",
"result",
"=",
"showcase",
".",
"_load_from_hdx",
"(",
"'showcase'",
",",
"identifier",
")",
"if",
"result",
":",
"return",
"showcase",
"return",
"None"
] |
Reads the showcase given by identifier from HDX and returns Showcase object
Args:
identifier (str): Identifier of showcase
configuration (Optional[Configuration]): HDX configuration. Defaults to global configuration.
Returns:
Optional[Showcase]: Showcase object if successful read, None if not
|
[
"Reads",
"the",
"showcase",
"given",
"by",
"identifier",
"from",
"HDX",
"and",
"returns",
"Showcase",
"object"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/showcase.py#L76-L92
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/showcase.py
|
Showcase.get_datasets
|
def get_datasets(self):
# type: () -> List[hdx.data.dataset.Dataset]
"""Get any datasets in the showcase
Returns:
List[Dataset]: List of datasets
"""
assoc_result, datasets_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='showcase_id',
action=self.actions()['list_datasets'])
datasets = list()
if assoc_result:
for dataset_dict in datasets_dicts:
dataset = hdx.data.dataset.Dataset(dataset_dict, configuration=self.configuration)
datasets.append(dataset)
return datasets
|
python
|
def get_datasets(self):
# type: () -> List[hdx.data.dataset.Dataset]
"""Get any datasets in the showcase
Returns:
List[Dataset]: List of datasets
"""
assoc_result, datasets_dicts = self._read_from_hdx('showcase', self.data['id'], fieldname='showcase_id',
action=self.actions()['list_datasets'])
datasets = list()
if assoc_result:
for dataset_dict in datasets_dicts:
dataset = hdx.data.dataset.Dataset(dataset_dict, configuration=self.configuration)
datasets.append(dataset)
return datasets
|
[
"def",
"get_datasets",
"(",
"self",
")",
":",
"# type: () -> List[hdx.data.dataset.Dataset]",
"assoc_result",
",",
"datasets_dicts",
"=",
"self",
".",
"_read_from_hdx",
"(",
"'showcase'",
",",
"self",
".",
"data",
"[",
"'id'",
"]",
",",
"fieldname",
"=",
"'showcase_id'",
",",
"action",
"=",
"self",
".",
"actions",
"(",
")",
"[",
"'list_datasets'",
"]",
")",
"datasets",
"=",
"list",
"(",
")",
"if",
"assoc_result",
":",
"for",
"dataset_dict",
"in",
"datasets_dicts",
":",
"dataset",
"=",
"hdx",
".",
"data",
".",
"dataset",
".",
"Dataset",
"(",
"dataset_dict",
",",
"configuration",
"=",
"self",
".",
"configuration",
")",
"datasets",
".",
"append",
"(",
"dataset",
")",
"return",
"datasets"
] |
Get any datasets in the showcase
Returns:
List[Dataset]: List of datasets
|
[
"Get",
"any",
"datasets",
"in",
"the",
"showcase"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/showcase.py#L179-L193
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/showcase.py
|
Showcase._get_showcase_dataset_dict
|
def _get_showcase_dataset_dict(self, dataset):
# type: (Union[hdx.data.dataset.Dataset,Dict,str]) -> Dict
"""Get showcase dataset dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
Dict: showcase dataset dict
"""
if isinstance(dataset, hdx.data.dataset.Dataset) or isinstance(dataset, dict):
if 'id' not in dataset:
dataset = hdx.data.dataset.Dataset.read_from_hdx(dataset['name'])
dataset = dataset['id']
elif not isinstance(dataset, str):
raise hdx.data.hdxobject.HDXError('Type %s cannot be added as a dataset!' % type(dataset).__name__)
if is_valid_uuid(dataset) is False:
raise hdx.data.hdxobject.HDXError('%s is not a valid dataset id!' % dataset)
return {'showcase_id': self.data['id'], 'package_id': dataset}
|
python
|
def _get_showcase_dataset_dict(self, dataset):
# type: (Union[hdx.data.dataset.Dataset,Dict,str]) -> Dict
"""Get showcase dataset dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
Dict: showcase dataset dict
"""
if isinstance(dataset, hdx.data.dataset.Dataset) or isinstance(dataset, dict):
if 'id' not in dataset:
dataset = hdx.data.dataset.Dataset.read_from_hdx(dataset['name'])
dataset = dataset['id']
elif not isinstance(dataset, str):
raise hdx.data.hdxobject.HDXError('Type %s cannot be added as a dataset!' % type(dataset).__name__)
if is_valid_uuid(dataset) is False:
raise hdx.data.hdxobject.HDXError('%s is not a valid dataset id!' % dataset)
return {'showcase_id': self.data['id'], 'package_id': dataset}
|
[
"def",
"_get_showcase_dataset_dict",
"(",
"self",
",",
"dataset",
")",
":",
"# type: (Union[hdx.data.dataset.Dataset,Dict,str]) -> Dict",
"if",
"isinstance",
"(",
"dataset",
",",
"hdx",
".",
"data",
".",
"dataset",
".",
"Dataset",
")",
"or",
"isinstance",
"(",
"dataset",
",",
"dict",
")",
":",
"if",
"'id'",
"not",
"in",
"dataset",
":",
"dataset",
"=",
"hdx",
".",
"data",
".",
"dataset",
".",
"Dataset",
".",
"read_from_hdx",
"(",
"dataset",
"[",
"'name'",
"]",
")",
"dataset",
"=",
"dataset",
"[",
"'id'",
"]",
"elif",
"not",
"isinstance",
"(",
"dataset",
",",
"str",
")",
":",
"raise",
"hdx",
".",
"data",
".",
"hdxobject",
".",
"HDXError",
"(",
"'Type %s cannot be added as a dataset!'",
"%",
"type",
"(",
"dataset",
")",
".",
"__name__",
")",
"if",
"is_valid_uuid",
"(",
"dataset",
")",
"is",
"False",
":",
"raise",
"hdx",
".",
"data",
".",
"hdxobject",
".",
"HDXError",
"(",
"'%s is not a valid dataset id!'",
"%",
"dataset",
")",
"return",
"{",
"'showcase_id'",
":",
"self",
".",
"data",
"[",
"'id'",
"]",
",",
"'package_id'",
":",
"dataset",
"}"
] |
Get showcase dataset dict
Args:
showcase (Union[Showcase,Dict,str]): Either a showcase id or Showcase metadata from a Showcase object or dictionary
Returns:
Dict: showcase dataset dict
|
[
"Get",
"showcase",
"dataset",
"dict"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/showcase.py#L195-L213
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/showcase.py
|
Showcase.add_dataset
|
def add_dataset(self, dataset, datasets_to_check=None):
# type: (Union[hdx.data.dataset.Dataset,Dict,str], List[hdx.data.dataset.Dataset]) -> bool
"""Add a dataset
Args:
dataset (Union[Dataset,Dict,str]): Either a dataset id or dataset metadata either from a Dataset object or a dictionary
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if the dataset was added, False if already present
"""
showcase_dataset = self._get_showcase_dataset_dict(dataset)
if datasets_to_check is None:
datasets_to_check = self.get_datasets()
for dataset in datasets_to_check:
if showcase_dataset['package_id'] == dataset['id']:
return False
self._write_to_hdx('associate', showcase_dataset, 'package_id')
return True
|
python
|
def add_dataset(self, dataset, datasets_to_check=None):
# type: (Union[hdx.data.dataset.Dataset,Dict,str], List[hdx.data.dataset.Dataset]) -> bool
"""Add a dataset
Args:
dataset (Union[Dataset,Dict,str]): Either a dataset id or dataset metadata either from a Dataset object or a dictionary
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if the dataset was added, False if already present
"""
showcase_dataset = self._get_showcase_dataset_dict(dataset)
if datasets_to_check is None:
datasets_to_check = self.get_datasets()
for dataset in datasets_to_check:
if showcase_dataset['package_id'] == dataset['id']:
return False
self._write_to_hdx('associate', showcase_dataset, 'package_id')
return True
|
[
"def",
"add_dataset",
"(",
"self",
",",
"dataset",
",",
"datasets_to_check",
"=",
"None",
")",
":",
"# type: (Union[hdx.data.dataset.Dataset,Dict,str], List[hdx.data.dataset.Dataset]) -> bool",
"showcase_dataset",
"=",
"self",
".",
"_get_showcase_dataset_dict",
"(",
"dataset",
")",
"if",
"datasets_to_check",
"is",
"None",
":",
"datasets_to_check",
"=",
"self",
".",
"get_datasets",
"(",
")",
"for",
"dataset",
"in",
"datasets_to_check",
":",
"if",
"showcase_dataset",
"[",
"'package_id'",
"]",
"==",
"dataset",
"[",
"'id'",
"]",
":",
"return",
"False",
"self",
".",
"_write_to_hdx",
"(",
"'associate'",
",",
"showcase_dataset",
",",
"'package_id'",
")",
"return",
"True"
] |
Add a dataset
Args:
dataset (Union[Dataset,Dict,str]): Either a dataset id or dataset metadata either from a Dataset object or a dictionary
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if the dataset was added, False if already present
|
[
"Add",
"a",
"dataset"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/showcase.py#L215-L233
|
train
|
OCHA-DAP/hdx-python-api
|
src/hdx/data/showcase.py
|
Showcase.add_datasets
|
def add_datasets(self, datasets, datasets_to_check=None):
# type: (List[Union[hdx.data.dataset.Dataset,Dict,str]], List[hdx.data.dataset.Dataset]) -> bool
"""Add multiple datasets
Args:
datasets (List[Union[Dataset,Dict,str]]): A list of either dataset ids or dataset metadata from Dataset objects or dictionaries
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if all datasets added or False if any already present
"""
if datasets_to_check is None:
datasets_to_check = self.get_datasets()
alldatasetsadded = True
for dataset in datasets:
if not self.add_dataset(dataset, datasets_to_check=datasets_to_check):
alldatasetsadded = False
return alldatasetsadded
|
python
|
def add_datasets(self, datasets, datasets_to_check=None):
# type: (List[Union[hdx.data.dataset.Dataset,Dict,str]], List[hdx.data.dataset.Dataset]) -> bool
"""Add multiple datasets
Args:
datasets (List[Union[Dataset,Dict,str]]): A list of either dataset ids or dataset metadata from Dataset objects or dictionaries
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if all datasets added or False if any already present
"""
if datasets_to_check is None:
datasets_to_check = self.get_datasets()
alldatasetsadded = True
for dataset in datasets:
if not self.add_dataset(dataset, datasets_to_check=datasets_to_check):
alldatasetsadded = False
return alldatasetsadded
|
[
"def",
"add_datasets",
"(",
"self",
",",
"datasets",
",",
"datasets_to_check",
"=",
"None",
")",
":",
"# type: (List[Union[hdx.data.dataset.Dataset,Dict,str]], List[hdx.data.dataset.Dataset]) -> bool",
"if",
"datasets_to_check",
"is",
"None",
":",
"datasets_to_check",
"=",
"self",
".",
"get_datasets",
"(",
")",
"alldatasetsadded",
"=",
"True",
"for",
"dataset",
"in",
"datasets",
":",
"if",
"not",
"self",
".",
"add_dataset",
"(",
"dataset",
",",
"datasets_to_check",
"=",
"datasets_to_check",
")",
":",
"alldatasetsadded",
"=",
"False",
"return",
"alldatasetsadded"
] |
Add multiple datasets
Args:
datasets (List[Union[Dataset,Dict,str]]): A list of either dataset ids or dataset metadata from Dataset objects or dictionaries
datasets_to_check (List[Dataset]): List of datasets against which to check existence of dataset. Defaults to datasets in showcase.
Returns:
bool: True if all datasets added or False if any already present
|
[
"Add",
"multiple",
"datasets"
] |
212440f54f73805826a16db77dbcb6033b18a313
|
https://github.com/OCHA-DAP/hdx-python-api/blob/212440f54f73805826a16db77dbcb6033b18a313/src/hdx/data/showcase.py#L235-L252
|
train
|
hz-inova/run_jnb
|
run_jnb/core.py
|
possible_parameter
|
def possible_parameter(nb, jsonable_parameter=True, end_cell_index=None):
"""
Find the possible parameters from a jupyter notebook (python3 only).
The possible parameters are obtained by parsing the abstract syntax tree of
the python code generated from the jupyter notebook.
For a jupuyter notebook, a variable can be a possible parameter if:
- it is defined in a cell that contains only comments or assignments,
- its name is not used in the current cell beside the assignment nor previously.
Parameters
----------
nb : str, nbformat.notebooknode.NotebookNode
Jupyter notebook path or its content as a NotebookNode object.
jsonable_parameter: bool, optional
Consider only jsonable parameters.
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
Returns
-------
list[collections.namedtuple]
If jsonable_parameter is true the fields are ('name','value','cell_index'), otherwise ('name', 'cell_index').
The list is ordered by the name of the parameters.
"""
jh = _JupyterNotebookHelper(nb, jsonable_parameter, end_cell_index)
if jsonable_parameter is True:
PossibleParameter=collections.namedtuple('PossibleParameter',['name','value','cell_index'])
else:
PossibleParameter=collections.namedtuple('PossibleParameter',['name', 'cell_index'])
res=[]
for name, cell_index in jh.param_cell_index.items():
if jsonable_parameter is True:
res.append(PossibleParameter(name=name,value=jh.param_value[name],cell_index=cell_index))
else:
res.append(PossibleParameter(name=name,cell_index=cell_index))
return sorted(res, key = lambda x: (x.name))
|
python
|
def possible_parameter(nb, jsonable_parameter=True, end_cell_index=None):
"""
Find the possible parameters from a jupyter notebook (python3 only).
The possible parameters are obtained by parsing the abstract syntax tree of
the python code generated from the jupyter notebook.
For a jupuyter notebook, a variable can be a possible parameter if:
- it is defined in a cell that contains only comments or assignments,
- its name is not used in the current cell beside the assignment nor previously.
Parameters
----------
nb : str, nbformat.notebooknode.NotebookNode
Jupyter notebook path or its content as a NotebookNode object.
jsonable_parameter: bool, optional
Consider only jsonable parameters.
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
Returns
-------
list[collections.namedtuple]
If jsonable_parameter is true the fields are ('name','value','cell_index'), otherwise ('name', 'cell_index').
The list is ordered by the name of the parameters.
"""
jh = _JupyterNotebookHelper(nb, jsonable_parameter, end_cell_index)
if jsonable_parameter is True:
PossibleParameter=collections.namedtuple('PossibleParameter',['name','value','cell_index'])
else:
PossibleParameter=collections.namedtuple('PossibleParameter',['name', 'cell_index'])
res=[]
for name, cell_index in jh.param_cell_index.items():
if jsonable_parameter is True:
res.append(PossibleParameter(name=name,value=jh.param_value[name],cell_index=cell_index))
else:
res.append(PossibleParameter(name=name,cell_index=cell_index))
return sorted(res, key = lambda x: (x.name))
|
[
"def",
"possible_parameter",
"(",
"nb",
",",
"jsonable_parameter",
"=",
"True",
",",
"end_cell_index",
"=",
"None",
")",
":",
"jh",
"=",
"_JupyterNotebookHelper",
"(",
"nb",
",",
"jsonable_parameter",
",",
"end_cell_index",
")",
"if",
"jsonable_parameter",
"is",
"True",
":",
"PossibleParameter",
"=",
"collections",
".",
"namedtuple",
"(",
"'PossibleParameter'",
",",
"[",
"'name'",
",",
"'value'",
",",
"'cell_index'",
"]",
")",
"else",
":",
"PossibleParameter",
"=",
"collections",
".",
"namedtuple",
"(",
"'PossibleParameter'",
",",
"[",
"'name'",
",",
"'cell_index'",
"]",
")",
"res",
"=",
"[",
"]",
"for",
"name",
",",
"cell_index",
"in",
"jh",
".",
"param_cell_index",
".",
"items",
"(",
")",
":",
"if",
"jsonable_parameter",
"is",
"True",
":",
"res",
".",
"append",
"(",
"PossibleParameter",
"(",
"name",
"=",
"name",
",",
"value",
"=",
"jh",
".",
"param_value",
"[",
"name",
"]",
",",
"cell_index",
"=",
"cell_index",
")",
")",
"else",
":",
"res",
".",
"append",
"(",
"PossibleParameter",
"(",
"name",
"=",
"name",
",",
"cell_index",
"=",
"cell_index",
")",
")",
"return",
"sorted",
"(",
"res",
",",
"key",
"=",
"lambda",
"x",
":",
"(",
"x",
".",
"name",
")",
")"
] |
Find the possible parameters from a jupyter notebook (python3 only).
The possible parameters are obtained by parsing the abstract syntax tree of
the python code generated from the jupyter notebook.
For a jupuyter notebook, a variable can be a possible parameter if:
- it is defined in a cell that contains only comments or assignments,
- its name is not used in the current cell beside the assignment nor previously.
Parameters
----------
nb : str, nbformat.notebooknode.NotebookNode
Jupyter notebook path or its content as a NotebookNode object.
jsonable_parameter: bool, optional
Consider only jsonable parameters.
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
Returns
-------
list[collections.namedtuple]
If jsonable_parameter is true the fields are ('name','value','cell_index'), otherwise ('name', 'cell_index').
The list is ordered by the name of the parameters.
|
[
"Find",
"the",
"possible",
"parameters",
"from",
"a",
"jupyter",
"notebook",
"(",
"python3",
"only",
")",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/core.py#L16-L57
|
train
|
hz-inova/run_jnb
|
run_jnb/core.py
|
run_jnb
|
def run_jnb(input_path, output_path=r"///_run_jnb/*-output",
execution_path=r'///input',
return_mode='except',
overwrite=False,
timeout=ExecutePreprocessor.timeout.default_value,
kernel_name=ExecutePreprocessor.kernel_name.default_value,
ep_kwargs=None, jsonable_parameter=True, end_cell_index=None, arg=None, **kwargs):
"""
Run an input jupyter notebook file and optionally (python3 only)
parametrise it.
One can pass arguments as keyword arguments or in a json format (file or string).
For safety reasons, in order to avoid any code injection,
only json serialisable keywords arguments are available. The keyword
arguments are firstly encoded in json format using the standard json encoder.
The json content is decoded into python objects using the standard json decoder
and it is mapped to a variable assignment by unpacking it.
The assignments are appended at the end of the cell where they are initially defined.
Parameters
----------
input_path : str
Path of the input jupyter notebook.
output_path : str, optional
Path of the output jupyter notebook.
One can use the input_path location as relative path by starting with "///" .
* can be used once in the beggining or end (excluding the ".ipynb" extension) as a wildcard of the input_path filename.
"///_run_jnb/*-output" is the default value and states that the output
is in the run_jnb folder with respect to the input_path directory and "-output" is appended to the input name.
execution_path : str, optional
The path of the folder where to execute the notebook.
r'///input' or r'///output' can be used to denote the input / output folder.
return_mode : ['parametrised_only','except',True, False], optional
Flag to write the generated notebook to the output_path: "parametrised_only" writes the generated notebook without executing it, "except" writes in case of an exception, True writes always, False writes never.
overwrite : bool, optional
Flag to overwrite or not the output_path. If the parameter is False
the used output_path will be incremented until a valid one is found.
timeout : int, optional
ExecutePreprocessor.timeout
kernel_name : str, optional
ExecutePreprocessor.kernel_name
ep_kwargs : dict, optional
Other kwargs accepted by nbconvert.preprocessors.ExecutePreprocessor
jsonable_parameter: bool, optional
Parametrise only jsonable parameters
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
arg : str
Path of a json file (it should end in ".json") or json formatted string used to parametrise the jupyter notebook.
It should containt json objects. It is decoded into python objects following https://docs.python.org/3.6/library/json.html#json-to-py-table .
kwargs:
json serialsable keyword arguments used to parametrise the jupyter notebook.
Returns
-------
collections.namedtuple
The fields are ('output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback').
If the generated file is written the output path is returned otherwise None.
If an error is catched the details are return otherwise None.
"""
if os.path.splitext(input_path)[1] != '.ipynb':
raise ValueError("The extension of input_path = '{}' is not '.ipynb'".format(input_path))
if os.path.basename(input_path) == '*':
raise ValueError("The filename ={} can not start with *".format(input_path))
input_path_dir, input_path_base = os.path.split(input_path)
if output_path.startswith(r'///'):
input_rel = True
output_path = output_path[3:]
else:
input_rel = False
output_path_dir, output_path_base = os.path.split(output_path)
if input_rel:
output_path_dir = os.path.join(input_path_dir, output_path_dir)
output_path_dir = os.path.normpath(output_path_dir)
if os.path.exists(output_path_dir) is False:
os.makedirs(output_path_dir)
if output_path_base.endswith('.ipynb'):
pass
elif output_path_base.startswith("*"):
output_path_base = input_path_base[:-6]+output_path_base[1:]+'.ipynb'
elif output_path_base.endswith("*"):
output_path_base = output_path_base[:-1]+input_path_base[:-6]+'.ipynb'
else:
raise ValueError("Invalid output_path")
output_path = os.path.abspath(os.path.join(output_path_dir,
output_path_base))
if output_path is not None and os.path.splitext(output_path)[1] != '.ipynb':
raise ValueError("The extension of output_path = '{}' is not '.ipynb'".format(output_path))
if execution_path.startswith(r'///input'):
execution_path = os.path.join(input_path_dir, execution_path[8:])
elif execution_path.startswith(r'///output'):
execution_path = os.path.join(input_path_dir, execution_path[8:])
execution_path = os.path.normpath(execution_path)
if os.path.exists(execution_path) is False:
os.makedirs(execution_path)
if ep_kwargs is None:
ep_kwargs = {}
if return_mode not in ['parametrised_only', 'except', True, False]:
raise TypeError("return mode is not valid!")
kwarg_to_json = json.dumps(kwargs)
kwarg_as_kwarg = decode_json(kwarg_to_json)
arg_as_kwarg = decode_json(arg)
multiple_kwarg = set(arg_as_kwarg.keys()) & set(kwarg_as_kwarg.keys())
if multiple_kwarg != set():
raise ValueError('Multiple values for keyword argument {}'.format(multiple_kwarg))
jupyter_kwargs = {**arg_as_kwarg, **kwarg_as_kwarg}
nb = _read_nb(input_path)
# clean notebook
for i, cell in enumerate(nb['cells']):
if cell['cell_type'] == 'code':
nb['cells'][i]['outputs'] = []
nb['cells'][i]['execution_count'] = None
if jupyter_kwargs != {}:
params_of_interest = {}
jnh = _JupyterNotebookHelper(nb, jsonable_parameter ,end_cell_index)
for el in jupyter_kwargs.keys():
if el not in jnh.param_cell_index.keys():
raise ValueError(repr(el)+' is not a possible parameter {}.'.format(list(jnh.param_cell_index.keys())))
else:
params_of_interest[el] = jnh.param_cell_index[el]
params_of_interest = sort_dict(params_of_interest, by='value')
cell_index_param = group_dict_by_value(params_of_interest)
for key, value in cell_index_param.items():
cell_param = {k: jupyter_kwargs[k] for k in value}
cell_code = kwargs_to_variable_assignment(cell_param)
marked_code = _mark_auto_generated_code(cell_code)
nb['cells'][key]['source'] += marked_code
if return_mode != 'parametrised_only':
ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel_name,
**ep_kwargs)
catch_except = False
error = (None, None, None, None)
try:
if return_mode != 'parametrised_only':
ep.preprocess(nb, {'metadata': {'path': execution_path}})
except CellExecutionError:
catch_except = True
for cell in nb['cells'][::-1]:
if cell['cell_type'] == 'code' and cell.get('outputs') != []:
for output in cell['outputs']:
if output.get('output_type') == 'error':
error = (cell['execution_count'],
output.get('ename'), output.get('evalue'),
output.get('traceback'))
break
if error[0] is not None:
break
else:
raise ValueError('Cell expected to have an error.')
except:
raise
if return_mode == 'except':
if catch_except is True:
nb_return = True
else:
nb_return = None
elif return_mode is True or return_mode == 'parametrised_only':
nb_return = True
elif return_mode is False:
nb_return = None
if nb_return is not None:
if overwrite is False:
while os.path.exists(output_path):
dirname, basename = os.path.split(output_path)
root, ext = os.path.splitext(basename)
new_root = increment_name(root)
output_path = os.path.join(dirname, new_root+ext)
nb_return = output_path # update the output_path
_write_nb(nb, output_path)
Output=collections.namedtuple('Output',['output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback'])
res = Output(output_nb_path=nb_return,error_prompt_number=error[0],
error_type=error[1],error_value=error[2],error_traceback=error[3])
return res
|
python
|
def run_jnb(input_path, output_path=r"///_run_jnb/*-output",
execution_path=r'///input',
return_mode='except',
overwrite=False,
timeout=ExecutePreprocessor.timeout.default_value,
kernel_name=ExecutePreprocessor.kernel_name.default_value,
ep_kwargs=None, jsonable_parameter=True, end_cell_index=None, arg=None, **kwargs):
"""
Run an input jupyter notebook file and optionally (python3 only)
parametrise it.
One can pass arguments as keyword arguments or in a json format (file or string).
For safety reasons, in order to avoid any code injection,
only json serialisable keywords arguments are available. The keyword
arguments are firstly encoded in json format using the standard json encoder.
The json content is decoded into python objects using the standard json decoder
and it is mapped to a variable assignment by unpacking it.
The assignments are appended at the end of the cell where they are initially defined.
Parameters
----------
input_path : str
Path of the input jupyter notebook.
output_path : str, optional
Path of the output jupyter notebook.
One can use the input_path location as relative path by starting with "///" .
* can be used once in the beggining or end (excluding the ".ipynb" extension) as a wildcard of the input_path filename.
"///_run_jnb/*-output" is the default value and states that the output
is in the run_jnb folder with respect to the input_path directory and "-output" is appended to the input name.
execution_path : str, optional
The path of the folder where to execute the notebook.
r'///input' or r'///output' can be used to denote the input / output folder.
return_mode : ['parametrised_only','except',True, False], optional
Flag to write the generated notebook to the output_path: "parametrised_only" writes the generated notebook without executing it, "except" writes in case of an exception, True writes always, False writes never.
overwrite : bool, optional
Flag to overwrite or not the output_path. If the parameter is False
the used output_path will be incremented until a valid one is found.
timeout : int, optional
ExecutePreprocessor.timeout
kernel_name : str, optional
ExecutePreprocessor.kernel_name
ep_kwargs : dict, optional
Other kwargs accepted by nbconvert.preprocessors.ExecutePreprocessor
jsonable_parameter: bool, optional
Parametrise only jsonable parameters
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
arg : str
Path of a json file (it should end in ".json") or json formatted string used to parametrise the jupyter notebook.
It should containt json objects. It is decoded into python objects following https://docs.python.org/3.6/library/json.html#json-to-py-table .
kwargs:
json serialsable keyword arguments used to parametrise the jupyter notebook.
Returns
-------
collections.namedtuple
The fields are ('output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback').
If the generated file is written the output path is returned otherwise None.
If an error is catched the details are return otherwise None.
"""
if os.path.splitext(input_path)[1] != '.ipynb':
raise ValueError("The extension of input_path = '{}' is not '.ipynb'".format(input_path))
if os.path.basename(input_path) == '*':
raise ValueError("The filename ={} can not start with *".format(input_path))
input_path_dir, input_path_base = os.path.split(input_path)
if output_path.startswith(r'///'):
input_rel = True
output_path = output_path[3:]
else:
input_rel = False
output_path_dir, output_path_base = os.path.split(output_path)
if input_rel:
output_path_dir = os.path.join(input_path_dir, output_path_dir)
output_path_dir = os.path.normpath(output_path_dir)
if os.path.exists(output_path_dir) is False:
os.makedirs(output_path_dir)
if output_path_base.endswith('.ipynb'):
pass
elif output_path_base.startswith("*"):
output_path_base = input_path_base[:-6]+output_path_base[1:]+'.ipynb'
elif output_path_base.endswith("*"):
output_path_base = output_path_base[:-1]+input_path_base[:-6]+'.ipynb'
else:
raise ValueError("Invalid output_path")
output_path = os.path.abspath(os.path.join(output_path_dir,
output_path_base))
if output_path is not None and os.path.splitext(output_path)[1] != '.ipynb':
raise ValueError("The extension of output_path = '{}' is not '.ipynb'".format(output_path))
if execution_path.startswith(r'///input'):
execution_path = os.path.join(input_path_dir, execution_path[8:])
elif execution_path.startswith(r'///output'):
execution_path = os.path.join(input_path_dir, execution_path[8:])
execution_path = os.path.normpath(execution_path)
if os.path.exists(execution_path) is False:
os.makedirs(execution_path)
if ep_kwargs is None:
ep_kwargs = {}
if return_mode not in ['parametrised_only', 'except', True, False]:
raise TypeError("return mode is not valid!")
kwarg_to_json = json.dumps(kwargs)
kwarg_as_kwarg = decode_json(kwarg_to_json)
arg_as_kwarg = decode_json(arg)
multiple_kwarg = set(arg_as_kwarg.keys()) & set(kwarg_as_kwarg.keys())
if multiple_kwarg != set():
raise ValueError('Multiple values for keyword argument {}'.format(multiple_kwarg))
jupyter_kwargs = {**arg_as_kwarg, **kwarg_as_kwarg}
nb = _read_nb(input_path)
# clean notebook
for i, cell in enumerate(nb['cells']):
if cell['cell_type'] == 'code':
nb['cells'][i]['outputs'] = []
nb['cells'][i]['execution_count'] = None
if jupyter_kwargs != {}:
params_of_interest = {}
jnh = _JupyterNotebookHelper(nb, jsonable_parameter ,end_cell_index)
for el in jupyter_kwargs.keys():
if el not in jnh.param_cell_index.keys():
raise ValueError(repr(el)+' is not a possible parameter {}.'.format(list(jnh.param_cell_index.keys())))
else:
params_of_interest[el] = jnh.param_cell_index[el]
params_of_interest = sort_dict(params_of_interest, by='value')
cell_index_param = group_dict_by_value(params_of_interest)
for key, value in cell_index_param.items():
cell_param = {k: jupyter_kwargs[k] for k in value}
cell_code = kwargs_to_variable_assignment(cell_param)
marked_code = _mark_auto_generated_code(cell_code)
nb['cells'][key]['source'] += marked_code
if return_mode != 'parametrised_only':
ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel_name,
**ep_kwargs)
catch_except = False
error = (None, None, None, None)
try:
if return_mode != 'parametrised_only':
ep.preprocess(nb, {'metadata': {'path': execution_path}})
except CellExecutionError:
catch_except = True
for cell in nb['cells'][::-1]:
if cell['cell_type'] == 'code' and cell.get('outputs') != []:
for output in cell['outputs']:
if output.get('output_type') == 'error':
error = (cell['execution_count'],
output.get('ename'), output.get('evalue'),
output.get('traceback'))
break
if error[0] is not None:
break
else:
raise ValueError('Cell expected to have an error.')
except:
raise
if return_mode == 'except':
if catch_except is True:
nb_return = True
else:
nb_return = None
elif return_mode is True or return_mode == 'parametrised_only':
nb_return = True
elif return_mode is False:
nb_return = None
if nb_return is not None:
if overwrite is False:
while os.path.exists(output_path):
dirname, basename = os.path.split(output_path)
root, ext = os.path.splitext(basename)
new_root = increment_name(root)
output_path = os.path.join(dirname, new_root+ext)
nb_return = output_path # update the output_path
_write_nb(nb, output_path)
Output=collections.namedtuple('Output',['output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback'])
res = Output(output_nb_path=nb_return,error_prompt_number=error[0],
error_type=error[1],error_value=error[2],error_traceback=error[3])
return res
|
[
"def",
"run_jnb",
"(",
"input_path",
",",
"output_path",
"=",
"r\"///_run_jnb/*-output\"",
",",
"execution_path",
"=",
"r'///input'",
",",
"return_mode",
"=",
"'except'",
",",
"overwrite",
"=",
"False",
",",
"timeout",
"=",
"ExecutePreprocessor",
".",
"timeout",
".",
"default_value",
",",
"kernel_name",
"=",
"ExecutePreprocessor",
".",
"kernel_name",
".",
"default_value",
",",
"ep_kwargs",
"=",
"None",
",",
"jsonable_parameter",
"=",
"True",
",",
"end_cell_index",
"=",
"None",
",",
"arg",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"if",
"os",
".",
"path",
".",
"splitext",
"(",
"input_path",
")",
"[",
"1",
"]",
"!=",
"'.ipynb'",
":",
"raise",
"ValueError",
"(",
"\"The extension of input_path = '{}' is not '.ipynb'\"",
".",
"format",
"(",
"input_path",
")",
")",
"if",
"os",
".",
"path",
".",
"basename",
"(",
"input_path",
")",
"==",
"'*'",
":",
"raise",
"ValueError",
"(",
"\"The filename ={} can not start with *\"",
".",
"format",
"(",
"input_path",
")",
")",
"input_path_dir",
",",
"input_path_base",
"=",
"os",
".",
"path",
".",
"split",
"(",
"input_path",
")",
"if",
"output_path",
".",
"startswith",
"(",
"r'///'",
")",
":",
"input_rel",
"=",
"True",
"output_path",
"=",
"output_path",
"[",
"3",
":",
"]",
"else",
":",
"input_rel",
"=",
"False",
"output_path_dir",
",",
"output_path_base",
"=",
"os",
".",
"path",
".",
"split",
"(",
"output_path",
")",
"if",
"input_rel",
":",
"output_path_dir",
"=",
"os",
".",
"path",
".",
"join",
"(",
"input_path_dir",
",",
"output_path_dir",
")",
"output_path_dir",
"=",
"os",
".",
"path",
".",
"normpath",
"(",
"output_path_dir",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"output_path_dir",
")",
"is",
"False",
":",
"os",
".",
"makedirs",
"(",
"output_path_dir",
")",
"if",
"output_path_base",
".",
"endswith",
"(",
"'.ipynb'",
")",
":",
"pass",
"elif",
"output_path_base",
".",
"startswith",
"(",
"\"*\"",
")",
":",
"output_path_base",
"=",
"input_path_base",
"[",
":",
"-",
"6",
"]",
"+",
"output_path_base",
"[",
"1",
":",
"]",
"+",
"'.ipynb'",
"elif",
"output_path_base",
".",
"endswith",
"(",
"\"*\"",
")",
":",
"output_path_base",
"=",
"output_path_base",
"[",
":",
"-",
"1",
"]",
"+",
"input_path_base",
"[",
":",
"-",
"6",
"]",
"+",
"'.ipynb'",
"else",
":",
"raise",
"ValueError",
"(",
"\"Invalid output_path\"",
")",
"output_path",
"=",
"os",
".",
"path",
".",
"abspath",
"(",
"os",
".",
"path",
".",
"join",
"(",
"output_path_dir",
",",
"output_path_base",
")",
")",
"if",
"output_path",
"is",
"not",
"None",
"and",
"os",
".",
"path",
".",
"splitext",
"(",
"output_path",
")",
"[",
"1",
"]",
"!=",
"'.ipynb'",
":",
"raise",
"ValueError",
"(",
"\"The extension of output_path = '{}' is not '.ipynb'\"",
".",
"format",
"(",
"output_path",
")",
")",
"if",
"execution_path",
".",
"startswith",
"(",
"r'///input'",
")",
":",
"execution_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"input_path_dir",
",",
"execution_path",
"[",
"8",
":",
"]",
")",
"elif",
"execution_path",
".",
"startswith",
"(",
"r'///output'",
")",
":",
"execution_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"input_path_dir",
",",
"execution_path",
"[",
"8",
":",
"]",
")",
"execution_path",
"=",
"os",
".",
"path",
".",
"normpath",
"(",
"execution_path",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"execution_path",
")",
"is",
"False",
":",
"os",
".",
"makedirs",
"(",
"execution_path",
")",
"if",
"ep_kwargs",
"is",
"None",
":",
"ep_kwargs",
"=",
"{",
"}",
"if",
"return_mode",
"not",
"in",
"[",
"'parametrised_only'",
",",
"'except'",
",",
"True",
",",
"False",
"]",
":",
"raise",
"TypeError",
"(",
"\"return mode is not valid!\"",
")",
"kwarg_to_json",
"=",
"json",
".",
"dumps",
"(",
"kwargs",
")",
"kwarg_as_kwarg",
"=",
"decode_json",
"(",
"kwarg_to_json",
")",
"arg_as_kwarg",
"=",
"decode_json",
"(",
"arg",
")",
"multiple_kwarg",
"=",
"set",
"(",
"arg_as_kwarg",
".",
"keys",
"(",
")",
")",
"&",
"set",
"(",
"kwarg_as_kwarg",
".",
"keys",
"(",
")",
")",
"if",
"multiple_kwarg",
"!=",
"set",
"(",
")",
":",
"raise",
"ValueError",
"(",
"'Multiple values for keyword argument {}'",
".",
"format",
"(",
"multiple_kwarg",
")",
")",
"jupyter_kwargs",
"=",
"{",
"*",
"*",
"arg_as_kwarg",
",",
"*",
"*",
"kwarg_as_kwarg",
"}",
"nb",
"=",
"_read_nb",
"(",
"input_path",
")",
"# clean notebook",
"for",
"i",
",",
"cell",
"in",
"enumerate",
"(",
"nb",
"[",
"'cells'",
"]",
")",
":",
"if",
"cell",
"[",
"'cell_type'",
"]",
"==",
"'code'",
":",
"nb",
"[",
"'cells'",
"]",
"[",
"i",
"]",
"[",
"'outputs'",
"]",
"=",
"[",
"]",
"nb",
"[",
"'cells'",
"]",
"[",
"i",
"]",
"[",
"'execution_count'",
"]",
"=",
"None",
"if",
"jupyter_kwargs",
"!=",
"{",
"}",
":",
"params_of_interest",
"=",
"{",
"}",
"jnh",
"=",
"_JupyterNotebookHelper",
"(",
"nb",
",",
"jsonable_parameter",
",",
"end_cell_index",
")",
"for",
"el",
"in",
"jupyter_kwargs",
".",
"keys",
"(",
")",
":",
"if",
"el",
"not",
"in",
"jnh",
".",
"param_cell_index",
".",
"keys",
"(",
")",
":",
"raise",
"ValueError",
"(",
"repr",
"(",
"el",
")",
"+",
"' is not a possible parameter {}.'",
".",
"format",
"(",
"list",
"(",
"jnh",
".",
"param_cell_index",
".",
"keys",
"(",
")",
")",
")",
")",
"else",
":",
"params_of_interest",
"[",
"el",
"]",
"=",
"jnh",
".",
"param_cell_index",
"[",
"el",
"]",
"params_of_interest",
"=",
"sort_dict",
"(",
"params_of_interest",
",",
"by",
"=",
"'value'",
")",
"cell_index_param",
"=",
"group_dict_by_value",
"(",
"params_of_interest",
")",
"for",
"key",
",",
"value",
"in",
"cell_index_param",
".",
"items",
"(",
")",
":",
"cell_param",
"=",
"{",
"k",
":",
"jupyter_kwargs",
"[",
"k",
"]",
"for",
"k",
"in",
"value",
"}",
"cell_code",
"=",
"kwargs_to_variable_assignment",
"(",
"cell_param",
")",
"marked_code",
"=",
"_mark_auto_generated_code",
"(",
"cell_code",
")",
"nb",
"[",
"'cells'",
"]",
"[",
"key",
"]",
"[",
"'source'",
"]",
"+=",
"marked_code",
"if",
"return_mode",
"!=",
"'parametrised_only'",
":",
"ep",
"=",
"ExecutePreprocessor",
"(",
"timeout",
"=",
"timeout",
",",
"kernel_name",
"=",
"kernel_name",
",",
"*",
"*",
"ep_kwargs",
")",
"catch_except",
"=",
"False",
"error",
"=",
"(",
"None",
",",
"None",
",",
"None",
",",
"None",
")",
"try",
":",
"if",
"return_mode",
"!=",
"'parametrised_only'",
":",
"ep",
".",
"preprocess",
"(",
"nb",
",",
"{",
"'metadata'",
":",
"{",
"'path'",
":",
"execution_path",
"}",
"}",
")",
"except",
"CellExecutionError",
":",
"catch_except",
"=",
"True",
"for",
"cell",
"in",
"nb",
"[",
"'cells'",
"]",
"[",
":",
":",
"-",
"1",
"]",
":",
"if",
"cell",
"[",
"'cell_type'",
"]",
"==",
"'code'",
"and",
"cell",
".",
"get",
"(",
"'outputs'",
")",
"!=",
"[",
"]",
":",
"for",
"output",
"in",
"cell",
"[",
"'outputs'",
"]",
":",
"if",
"output",
".",
"get",
"(",
"'output_type'",
")",
"==",
"'error'",
":",
"error",
"=",
"(",
"cell",
"[",
"'execution_count'",
"]",
",",
"output",
".",
"get",
"(",
"'ename'",
")",
",",
"output",
".",
"get",
"(",
"'evalue'",
")",
",",
"output",
".",
"get",
"(",
"'traceback'",
")",
")",
"break",
"if",
"error",
"[",
"0",
"]",
"is",
"not",
"None",
":",
"break",
"else",
":",
"raise",
"ValueError",
"(",
"'Cell expected to have an error.'",
")",
"except",
":",
"raise",
"if",
"return_mode",
"==",
"'except'",
":",
"if",
"catch_except",
"is",
"True",
":",
"nb_return",
"=",
"True",
"else",
":",
"nb_return",
"=",
"None",
"elif",
"return_mode",
"is",
"True",
"or",
"return_mode",
"==",
"'parametrised_only'",
":",
"nb_return",
"=",
"True",
"elif",
"return_mode",
"is",
"False",
":",
"nb_return",
"=",
"None",
"if",
"nb_return",
"is",
"not",
"None",
":",
"if",
"overwrite",
"is",
"False",
":",
"while",
"os",
".",
"path",
".",
"exists",
"(",
"output_path",
")",
":",
"dirname",
",",
"basename",
"=",
"os",
".",
"path",
".",
"split",
"(",
"output_path",
")",
"root",
",",
"ext",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"basename",
")",
"new_root",
"=",
"increment_name",
"(",
"root",
")",
"output_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"dirname",
",",
"new_root",
"+",
"ext",
")",
"nb_return",
"=",
"output_path",
"# update the output_path",
"_write_nb",
"(",
"nb",
",",
"output_path",
")",
"Output",
"=",
"collections",
".",
"namedtuple",
"(",
"'Output'",
",",
"[",
"'output_nb_path'",
",",
"'error_prompt_number'",
",",
"'error_type'",
",",
"'error_value'",
",",
"'error_traceback'",
"]",
")",
"res",
"=",
"Output",
"(",
"output_nb_path",
"=",
"nb_return",
",",
"error_prompt_number",
"=",
"error",
"[",
"0",
"]",
",",
"error_type",
"=",
"error",
"[",
"1",
"]",
",",
"error_value",
"=",
"error",
"[",
"2",
"]",
",",
"error_traceback",
"=",
"error",
"[",
"3",
"]",
")",
"return",
"res"
] |
Run an input jupyter notebook file and optionally (python3 only)
parametrise it.
One can pass arguments as keyword arguments or in a json format (file or string).
For safety reasons, in order to avoid any code injection,
only json serialisable keywords arguments are available. The keyword
arguments are firstly encoded in json format using the standard json encoder.
The json content is decoded into python objects using the standard json decoder
and it is mapped to a variable assignment by unpacking it.
The assignments are appended at the end of the cell where they are initially defined.
Parameters
----------
input_path : str
Path of the input jupyter notebook.
output_path : str, optional
Path of the output jupyter notebook.
One can use the input_path location as relative path by starting with "///" .
* can be used once in the beggining or end (excluding the ".ipynb" extension) as a wildcard of the input_path filename.
"///_run_jnb/*-output" is the default value and states that the output
is in the run_jnb folder with respect to the input_path directory and "-output" is appended to the input name.
execution_path : str, optional
The path of the folder where to execute the notebook.
r'///input' or r'///output' can be used to denote the input / output folder.
return_mode : ['parametrised_only','except',True, False], optional
Flag to write the generated notebook to the output_path: "parametrised_only" writes the generated notebook without executing it, "except" writes in case of an exception, True writes always, False writes never.
overwrite : bool, optional
Flag to overwrite or not the output_path. If the parameter is False
the used output_path will be incremented until a valid one is found.
timeout : int, optional
ExecutePreprocessor.timeout
kernel_name : str, optional
ExecutePreprocessor.kernel_name
ep_kwargs : dict, optional
Other kwargs accepted by nbconvert.preprocessors.ExecutePreprocessor
jsonable_parameter: bool, optional
Parametrise only jsonable parameters
end_cell_index : int, optional
End cell index used to slice the notebook in finding the possible parameters.
arg : str
Path of a json file (it should end in ".json") or json formatted string used to parametrise the jupyter notebook.
It should containt json objects. It is decoded into python objects following https://docs.python.org/3.6/library/json.html#json-to-py-table .
kwargs:
json serialsable keyword arguments used to parametrise the jupyter notebook.
Returns
-------
collections.namedtuple
The fields are ('output_nb_path', 'error_prompt_number','error_type','error_value','error_traceback').
If the generated file is written the output path is returned otherwise None.
If an error is catched the details are return otherwise None.
|
[
"Run",
"an",
"input",
"jupyter",
"notebook",
"file",
"and",
"optionally",
"(",
"python3",
"only",
")",
"parametrise",
"it",
"."
] |
55e63de33b0643161948864d2c1ec998f02815bf
|
https://github.com/hz-inova/run_jnb/blob/55e63de33b0643161948864d2c1ec998f02815bf/run_jnb/core.py#L60-L256
|
train
|
Tygs/ww
|
src/ww/wrappers/lists.py
|
ListWrapper.join
|
def join(self, joiner, formatter=lambda s, t: t.format(s),
template="{}"):
"""Join values and convert to string
Example:
>>> from ww import l
>>> lst = l('012')
>>> lst.join(',')
u'0,1,2'
>>> lst.join(',', template="{}#")
u'0#,1#,2#'
>>> string = lst.join(',',\
formatter = lambda x, y: str(int(x) ** 2))
>>> string
u'0,1,4'
"""
return ww.s(joiner).join(self, formatter, template)
|
python
|
def join(self, joiner, formatter=lambda s, t: t.format(s),
template="{}"):
"""Join values and convert to string
Example:
>>> from ww import l
>>> lst = l('012')
>>> lst.join(',')
u'0,1,2'
>>> lst.join(',', template="{}#")
u'0#,1#,2#'
>>> string = lst.join(',',\
formatter = lambda x, y: str(int(x) ** 2))
>>> string
u'0,1,4'
"""
return ww.s(joiner).join(self, formatter, template)
|
[
"def",
"join",
"(",
"self",
",",
"joiner",
",",
"formatter",
"=",
"lambda",
"s",
",",
"t",
":",
"t",
".",
"format",
"(",
"s",
")",
",",
"template",
"=",
"\"{}\"",
")",
":",
"return",
"ww",
".",
"s",
"(",
"joiner",
")",
".",
"join",
"(",
"self",
",",
"formatter",
",",
"template",
")"
] |
Join values and convert to string
Example:
>>> from ww import l
>>> lst = l('012')
>>> lst.join(',')
u'0,1,2'
>>> lst.join(',', template="{}#")
u'0#,1#,2#'
>>> string = lst.join(',',\
formatter = lambda x, y: str(int(x) ** 2))
>>> string
u'0,1,4'
|
[
"Join",
"values",
"and",
"convert",
"to",
"string"
] |
6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4
|
https://github.com/Tygs/ww/blob/6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4/src/ww/wrappers/lists.py#L33-L51
|
train
|
Tygs/ww
|
src/ww/wrappers/lists.py
|
ListWrapper.append
|
def append(self, *values):
"""Append values at the end of the list
Allow chaining.
Args:
values: values to be appened at the end.
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.append(1)
[1]
>>> lst
[1]
>>> lst.append(2, 3).append(4,5)
[1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5]
"""
for value in values:
list.append(self, value)
return self
|
python
|
def append(self, *values):
"""Append values at the end of the list
Allow chaining.
Args:
values: values to be appened at the end.
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.append(1)
[1]
>>> lst
[1]
>>> lst.append(2, 3).append(4,5)
[1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5]
"""
for value in values:
list.append(self, value)
return self
|
[
"def",
"append",
"(",
"self",
",",
"*",
"values",
")",
":",
"for",
"value",
"in",
"values",
":",
"list",
".",
"append",
"(",
"self",
",",
"value",
")",
"return",
"self"
] |
Append values at the end of the list
Allow chaining.
Args:
values: values to be appened at the end.
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.append(1)
[1]
>>> lst
[1]
>>> lst.append(2, 3).append(4,5)
[1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5]
|
[
"Append",
"values",
"at",
"the",
"end",
"of",
"the",
"list"
] |
6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4
|
https://github.com/Tygs/ww/blob/6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4/src/ww/wrappers/lists.py#L53-L77
|
train
|
Tygs/ww
|
src/ww/wrappers/lists.py
|
ListWrapper.extend
|
def extend(self, *iterables):
"""Add all values of all iterables at the end of the list
Args:
iterables: iterable which content to add at the end
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.extend([1, 2])
[1, 2]
>>> lst
[1, 2]
>>> lst.extend([3, 4]).extend([5, 6])
[1, 2, 3, 4, 5, 6]
>>> lst
[1, 2, 3, 4, 5, 6]
"""
for value in iterables:
list.extend(self, value)
return self
|
python
|
def extend(self, *iterables):
"""Add all values of all iterables at the end of the list
Args:
iterables: iterable which content to add at the end
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.extend([1, 2])
[1, 2]
>>> lst
[1, 2]
>>> lst.extend([3, 4]).extend([5, 6])
[1, 2, 3, 4, 5, 6]
>>> lst
[1, 2, 3, 4, 5, 6]
"""
for value in iterables:
list.extend(self, value)
return self
|
[
"def",
"extend",
"(",
"self",
",",
"*",
"iterables",
")",
":",
"for",
"value",
"in",
"iterables",
":",
"list",
".",
"extend",
"(",
"self",
",",
"value",
")",
"return",
"self"
] |
Add all values of all iterables at the end of the list
Args:
iterables: iterable which content to add at the end
Example:
>>> from ww import l
>>> lst = l([])
>>> lst.extend([1, 2])
[1, 2]
>>> lst
[1, 2]
>>> lst.extend([3, 4]).extend([5, 6])
[1, 2, 3, 4, 5, 6]
>>> lst
[1, 2, 3, 4, 5, 6]
|
[
"Add",
"all",
"values",
"of",
"all",
"iterables",
"at",
"the",
"end",
"of",
"the",
"list"
] |
6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4
|
https://github.com/Tygs/ww/blob/6a4b85141c9b74026abe8f3fa9bc7021f3c99fd4/src/ww/wrappers/lists.py#L79-L101
|
train
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.