Skip to content

Commit

Permalink
Update kagglesdk and make tests work
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemessick committed Aug 23, 2024
1 parent 89ebc86 commit ad35d17
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changelog
====
### 1.6.18
* Fix KeyError when "Last-Modified" is not in the response.
* The header for the second column of the output from "kaggle competitions files ..." changed from "size" to "totalBytes"

### 1.6.17

Expand Down
24 changes: 11 additions & 13 deletions kaggle/api/kaggle_api_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ def build_kaggle_client(self):
return KaggleClient(env=env, verbose=verbose)

def camel_to_snake(self, name):
""" TODO Remove this and rewrite field lists using snake case.
"""
:param name: field in camel case
:return: field in snake case
"""
Expand Down Expand Up @@ -775,11 +775,9 @@ def competitions_list_cli(self,
]
if competitions:
if csv_display:
self.print_csv(competitions,
[self.camel_to_snake(f) for f in fields])
self.print_csv(competitions, fields)
else:
self.print_table(competitions,
[self.camel_to_snake(f) for f in fields])
self.print_table(competitions, fields)
else:
print('No competitions found')

Expand Down Expand Up @@ -914,8 +912,8 @@ def competition_submissions_cli(self,
submissions = self.competition_submissions(competition, page_token=page_token,
page_size=page_size)
fields = [
'file_name', 'date', 'description', 'status', 'public_score', # Breaking change: column headers
'private_score'
'fileName', 'date', 'description', 'status', 'publicScore',
'privateScore'
]
if submissions:
if csv_display:
Expand Down Expand Up @@ -976,7 +974,7 @@ def competition_list_files_cli(self,
next_page_token = result.next_page_token
if next_page_token:
print('Next Page Token = {}'.format(next_page_token))
fields = ['name', 'total_bytes', 'creation_date'] # Breaking change: column header
fields = ['name', 'totalBytes', 'creationDate'] # Breaking change: column header
if result:
if csv_display:
self.print_csv(result.files, fields)
Expand Down Expand Up @@ -1160,7 +1158,7 @@ def competition_leaderboard_cli(self,

if view:
results = self.competition_leaderboard_view(competition)
fields = ['team_id', 'team_name', 'submission_date', 'score'] # Breaking change: column headers
fields = ['teamId', 'teamName', 'submissionDate', 'score']
if results:
if csv_display:
self.print_csv(results, fields)
Expand Down Expand Up @@ -3835,17 +3833,17 @@ def print_table(self, items, fields):
return
for f in fields:
length = max(len(f),
max([len(self.string(getattr(i, f))) for i in items]))
max([len(self.string(getattr(i, self.camel_to_snake(f)))) for i in items]))
justify = '>' if isinstance(getattr(
items[0], f), int) or f == 'size' or f == 'reward' else '<'
items[0], self.camel_to_snake(f)), int) or f == 'size' or f == 'reward' else '<'
formats.append('{:' + justify + self.string(length + 2) + '}')
borders.append('-' * length + ' ')
row_format = u''.join(formats)
headers = [f + ' ' for f in fields]
print(row_format.format(*headers))
print(row_format.format(*borders))
for i in items:
i_fields = [self.string(getattr(i, f)) + ' ' for f in fields]
i_fields = [self.string(getattr(i, self.camel_to_snake(f))) + ' ' for f in fields]
try:
print(row_format.format(*i_fields))
except UnicodeEncodeError:
Expand All @@ -3862,7 +3860,7 @@ def print_csv(self, items, fields):
writer = csv.writer(sys.stdout)
writer.writerow(fields)
for i in items:
i_fields = [self.string(getattr(i, f)) for f in fields]
i_fields = [self.string(getattr(i, self.camel_to_snake(f))) for f in fields]
writer.writerow(i_fields)

def string(self, item):
Expand Down
4 changes: 4 additions & 0 deletions kagglesdk/common/types/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def content_length(self, content_length: int):
self._content_length = content_length


@classmethod
def prepare_from(cls, http_response):
return http_response

FileDownload._fields = [
FieldMetadata("contentType", "content_type", "_content_type", str, "", PredefinedSerializer()),
FieldMetadata("fileName", "file_name", "_file_name", str, "", PredefinedSerializer()),
Expand Down
4 changes: 4 additions & 0 deletions kagglesdk/kaggle_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ class KaggleEnv(Enum):
STAGING = 1 # staging.kaggle.com
ADMIN = 2 # admin.kaggle.com
QA = 3 # qa.kaggle.com
# Direct prod access is not allowed to have IAP protection during testing.
# PROD = 3 # www.kaggle.com


_env_to_endpoint = {
KaggleEnv.LOCAL: 'http://localhost',
KaggleEnv.STAGING: 'https://staging.kaggle.com',
KaggleEnv.ADMIN: 'https://admin.kaggle.com',
KaggleEnv.QA: 'https://qa.kaggle.com',
# See the comment above in KaggleEnv enum.
# KaggleEnv.PROD: "http://www.kaggle.com",
}


Expand Down
10 changes: 9 additions & 1 deletion kagglesdk/kaggle_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ def _from_str(cls, v):
# return enum_items[v[ix_start:]]
#
# return enum_items[f'{enum_prefix}_{v}']
return cls[v]
try:
return cls[v]
except KeyError:
dct = vars(cls)
n = v.lower()
for key in dct.keys():
if key.lower() == n:
return dct[key]
raise


class ListSerializer(ObjectSerializer):
Expand Down

0 comments on commit ad35d17

Please sign in to comment.