Skip to content

Commit d2fb8a5

Browse files
wcukrysteboe
authored andcommitted
Valid types and making kernels init more friendly
1 parent 65f14b1 commit d2fb8a5

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

kaggle/api/kaggle_api_extended.py

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ class KaggleApi(KaggleApi):
8686
config_values = {}
8787
already_printed_version_warning = False
8888

89+
# Kernels valid types
90+
valid_push_kernel_types = ['script', 'notebook']
91+
valid_push_language_types = ['python', 'r', 'rmarkdown']
92+
valid_list_languages = ['all', 'python', 'r', 'sqlite', 'julia']
93+
valid_list_kernel_types = ['all', 'script', 'notebook']
94+
valid_list_output_types = ['all', 'visualization', 'data']
95+
valid_list_sort_by = ['hotness', 'commentCount', 'dateCreated', 'dateRun',
96+
'relevance', 'scoreAscending', 'scoreDescending', 'viewCount', 'voteCount']
97+
98+
# Competitoins valid types
99+
valid_competition_groups = ['general', 'entered', 'inClass']
100+
valid_competition_categories = [
101+
'all', 'featured', 'research', 'recruitment', 'gettingStarted', 'masters', 'playground']
102+
valid_competition_sort_by = ['grouped', 'prize', 'earliestDeadline',
103+
'latestDeadline', 'numberOfTeams', 'recentlyCreated']
104+
105+
# Datasets valid types
106+
valid_dataset_file_types = ['all', 'csv', 'sqlite', 'json', 'bigQuery']
107+
valid_dataset_license_names = ['all', 'cc', 'gpl', 'odb', 'other']
108+
valid_dataset_sizes = ['all', 'small', 'medium', 'large']
109+
valid_dataset_sort_bys = ['hottest', 'votes',
110+
'updated', 'active', 'published']
111+
89112
# Hack for https://github.com/Kaggle/kaggle-api/issues/22 / b/78194015
90113
if six.PY2:
91114
reload(sys)
@@ -380,30 +403,21 @@ def competitions_list(self,
380403
381404
page: the page to return (default is 1)
382405
search: a search term to use (default is empty string)
383-
sort_by: how to sort the result, see valid_sort_by for options
406+
sort_by: how to sort the result, see valid_competition_sort_by for options
384407
category: category to filter result to
385408
group: group to filter result to
386409
"""
387-
valid_groups = ['general', 'entered', 'inClass']
388-
if group and group not in valid_groups:
410+
if group and group not in self.valid_competition_groups:
389411
raise ValueError('Invalid group specified. Valid options are ' +
390-
str(valid_groups))
412+
str(self.valid_competition_groups))
391413

392-
valid_categories = [
393-
'all', 'featured', 'research', 'recruitment', 'gettingStarted',
394-
'masters', 'playground'
395-
]
396-
if category and category not in valid_categories:
414+
if category and category not in self.valid_competition_categories:
397415
raise ValueError('Invalid category specified. Valid options are ' +
398-
str(valid_categories))
416+
str(self.valid_competition_categories))
399417

400-
valid_sort_by = [
401-
'grouped', 'prize', 'earliestDeadline', 'latestDeadline',
402-
'numberOfTeams', 'recentlyCreated'
403-
]
404-
if sort_by and sort_by not in valid_sort_by:
418+
if sort_by and sort_by not in self.valid_competition_sort_by:
405419
raise ValueError('Invalid sort_by specified. Valid options are ' +
406-
str(valid_sort_by))
420+
str(self.valid_competition_sort_by))
407421

408422
competitions_list_result = self.process_response(
409423
self.competitions_list_with_http_info(
@@ -809,35 +823,31 @@ def dataset_list(self,
809823
810824
Parameters
811825
==========
812-
sort_by: how to sort the result, see valid_sort_bys for options
813-
size: the size of the dataset, see valid_sizes for string options
814-
file_type: the format, see valid_file_types for string options
815-
license_name: string descriptor for license, see valid_license_names
826+
sort_by: how to sort the result, see valid_dataset_sort_bys for options
827+
size: the size of the dataset, see valid_dataset_sizes for string options
828+
file_type: the format, see valid_dataset_file_types for string options
829+
license_name: string descriptor for license, see valid_dataset_license_names
816830
tag_ids: tag identifiers to filter the search
817831
search: a search term to use (default is empty string)
818832
user: username to filter the search to
819833
mine: boolean if True, group is changed to "my" to return personal
820834
page: the page to return (default is 1)
821835
"""
822-
valid_sort_bys = ['hottest', 'votes', 'updated', 'active', 'published']
823-
if sort_by and sort_by not in valid_sort_bys:
836+
if sort_by and sort_by not in self.valid_dataset_sort_bys:
824837
raise ValueError('Invalid sort by specified. Valid options are ' +
825-
str(valid_sort_bys))
838+
str(self.valid_dataset_sort_bys))
826839

827-
valid_sizes = ['all', 'small', 'medium', 'large']
828-
if size and size not in valid_sizes:
840+
if size and size not in self.valid_dataset_sizes:
829841
raise ValueError('Invalid size specified. Valid options are ' +
830-
str(valid_sizes))
842+
str(self.valid_dataset_sizes))
831843

832-
valid_file_types = ['all', 'csv', 'sqlite', 'json', 'bigQuery']
833-
if file_type and file_type not in valid_file_types:
844+
if file_type and file_type not in self.valid_dataset_file_types:
834845
raise ValueError('Invalid file type specified. Valid options are '
835-
+ str(valid_file_types))
846+
+ str(self.valid_dataset_file_types))
836847

837-
valid_license_names = ['all', 'cc', 'gpl', 'odb', 'other']
838-
if license_name and license_name not in valid_license_names:
848+
if license_name and license_name not in self.valid_dataset_license_names:
839849
raise ValueError('Invalid license specified. Valid options are ' +
840-
str(valid_license_names))
850+
str(self.valid_dataset_license_names))
841851

842852
if int(page) <= 0:
843853
raise ValueError('Page number must be >= 1')
@@ -879,10 +889,10 @@ def dataset_list_cli(self,
879889
880890
Parameters
881891
==========
882-
sort_by: how to sort the result, see valid_sort_bys for options
883-
size: the size of the dataset, see valid_sizes for string options
884-
file_type: the format, see valid_file_types for string options
885-
license_name: string descriptor for license, see valid_license_names
892+
sort_by: how to sort the result, see valid_dataset_sort_bys for options
893+
size: the size of the dataset, see valid_dataset_sizes for string options
894+
file_type: the format, see valid_dataset_file_types for string options
895+
license_name: string descriptor for license, see valid_dataset_license_names
886896
tag_ids: tag identifiers to filter the search
887897
search: a search term to use (default is empty string)
888898
user: username to filter the search to
@@ -1525,9 +1535,9 @@ def kernels_list(self,
15251535
mine: if true, group is specified as "my" to return personal kernels
15261536
user: filter results to a specific user
15271537
language: the programming language of the kernel
1528-
kernel_type: the type of kernel, one of valid_kernel_types (str)
1529-
output_type: the output type, one of valid_output_types (str)
1530-
sort_by: if defined, sort results by this string (valid_sort_by)
1538+
kernel_type: the type of kernel, one of valid_list_kernel_types (str)
1539+
output_type: the output type, one of valid_list_output_types (str)
1540+
sort_by: if defined, sort results by this string (valid_list_sort_by)
15311541
"""
15321542
if int(page) <= 0:
15331543
raise ValueError('Page number must be >= 1')
@@ -1538,31 +1548,25 @@ def kernels_list(self,
15381548
if page_size > 100:
15391549
page_size = 100
15401550

1541-
valid_languages = ['all', 'python', 'r', 'sqlite', 'julia']
1542-
if language and language not in valid_languages:
1551+
if language and language not in self.valid_list_languages:
15431552
raise ValueError('Invalid language specified. Valid options are ' +
1544-
str(valid_languages))
1553+
str(self.valid_list_languages))
15451554

1546-
valid_kernel_types = ['all', 'script', 'notebook']
1547-
if kernel_type and kernel_type not in valid_kernel_types:
1555+
if kernel_type and kernel_type not in self.valid_list_kernel_types:
15481556
raise ValueError(
15491557
'Invalid kernel type specified. Valid options are ' +
1550-
str(valid_kernel_types))
1558+
str(self.valid_list_kernel_types))
15511559

1552-
valid_output_types = ['all', 'visualization', 'data']
1553-
if output_type and output_type not in valid_output_types:
1560+
if output_type and output_type not in self.valid_list_output_types:
15541561
raise ValueError(
15551562
'Invalid output type specified. Valid options are ' +
1556-
str(valid_output_types))
1557-
1558-
valid_sort_by = [
1559-
'hotness', 'commentCount', 'dateCreated', 'dateRun', 'relevance',
1560-
'scoreAscending', 'scoreDescending', 'viewCount', 'voteCount'
1561-
]
1562-
if sort_by and sort_by not in valid_sort_by:
1563+
str(self.valid_list_output_types))
1564+
1565+
if sort_by and sort_by not in self.valid_list_sort_by:
15631566
raise ValueError(
15641567
'Invalid sort by type specified. Valid options are ' +
1565-
str(valid_sort_by))
1568+
str(self.valid_list_sort_by))
1569+
15661570
if sort_by == 'relevance' and search == '':
15671571
raise ValueError('Cannot sort by relevance without a search term.')
15681572

@@ -1650,8 +1654,8 @@ def kernels_initialize(self, folder):
16501654
'id': username + '/INSERT_KERNEL_SLUG_HERE',
16511655
'title': 'INSERT_TITLE_HERE',
16521656
'code_file': 'INSERT_CODE_FILE_PATH_HERE',
1653-
'language': 'INSERT_LANGUAGE_HERE',
1654-
'kernel_type': 'INSERT_KERNEL_TYPE_HERE',
1657+
'language': 'Pick one of: {' + ','.join(x for x in self.valid_push_language_types) + '}',
1658+
'kernel_type': 'Pick one of: {' + ','.join(x for x in self.valid_push_kernel_types) + '}',
16551659
'is_private': 'true',
16561660
'enable_gpu': 'false',
16571661
'enable_internet': 'false',
@@ -1728,19 +1732,17 @@ def kernels_push(self, folder):
17281732
'how slugs are determined.' %
17291733
'https://en.wikipedia.org/wiki/Clean_URL#Slug')
17301734

1731-
valid_languages = ['python', 'r', 'rmarkdown']
17321735
language = self.get_or_default(meta_data, 'language', '')
1733-
if language not in valid_languages:
1736+
if language not in self.valid_push_language_types:
17341737
raise ValueError(
17351738
'A valid language must be specified in the metadata. Valid '
1736-
'options are ' + str(valid_languages))
1739+
'options are ' + str(self.valid_push_language_types))
17371740

1738-
valid_kernel_types = ['script', 'notebook']
17391741
kernel_type = self.get_or_default(meta_data, 'kernel_type', '')
1740-
if kernel_type not in valid_kernel_types:
1742+
if kernel_type not in self.valid_push_kernel_types:
17411743
raise ValueError(
17421744
'A valid kernel type must be specified in the metadata. Valid '
1743-
'options are ' + str(valid_kernel_types))
1745+
'options are ' + str(self.valid_push_kernel_types))
17441746

17451747
if kernel_type == 'notebook' and language == 'rmarkdown':
17461748
language = 'r'

0 commit comments

Comments
 (0)