Skip to content

Commit

Permalink
Clean up errors (#639)
Browse files Browse the repository at this point in the history
There were some errors that had been present a year or so. They are
gone, as are all the unused import statements.

I'm getting really frustrated by the formatter. Everything here was
formatted by `yapf` 0.40.2 but I must have merged something that wasn't
properly formatted because there is a large number of format changes.
Unfortunately, that was also true before I re-ran the formatter. I
wonder if maybe `yapf` isn't deterministic. (Edit: It seems to be
deterministic.)

There were two methods that didn't have a `self` parameter. There was a
`unicode` statement in a try-block to test for Python 2 vs 3. There were
two undefined vars -- one was misspelled ('query' should have been
'quiet') and the other was a missing `path` parameter to the method. I
think that is all the errors I fixed.

Added #640 to track the lack of unit test coverage.
  • Loading branch information
stevemessick authored Sep 26, 2024
1 parent da26449 commit 63244ad
Show file tree
Hide file tree
Showing 6 changed files with 546 additions and 580 deletions.
114 changes: 48 additions & 66 deletions kaggle/api/kaggle_api_extended.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#!/usr/bin/python
#
# Copyright 2024 Kaggle Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/python
#
# Copyright 2024 Kaggle Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/usr/bin/python
#
# Copyright 2019 Kaggle Inc
Expand All @@ -32,78 +32,64 @@

# coding=utf-8
from __future__ import print_function

import csv
from datetime import datetime
import io
import json
import os
from os.path import expanduser
from random import random
import sys
import shutil
import sys
import tarfile
import tempfile
import time
import zipfile
import tempfile
from os.path import expanduser
from random import random

import bleach
import requests
import urllib3.exceptions as urllib3_exceptions
from requests.adapters import HTTPAdapter
from slugify import slugify
from tqdm import tqdm
from urllib3.util.retry import Retry

from kaggle.configuration import Configuration
from kagglesdk import KaggleClient, KaggleEnv
from kagglesdk.competitions.types.competition_api_service import *
from kagglesdk.datasets.types.dataset_api_service import ApiListDatasetsRequest, ApiListDatasetFilesRequest, \
ApiGetDatasetStatusRequest, ApiDownloadDatasetRequest, ApiCreateDatasetRequest, ApiCreateDatasetVersionRequestBody, \
ApiCreateDatasetVersionByIdRequest, ApiCreateDatasetVersionRequest, ApiDatasetNewFile
from kagglesdk.datasets.types.dataset_enums import DatasetSelectionGroup, DatasetSortBy
from ..api_client import ApiClient
from kaggle.configuration import Configuration
from .kaggle_api import KaggleApi
from ..api_client import ApiClient
from ..models.api_blob_type import ApiBlobType
from ..models.collaborator import Collaborator
from ..models.create_inbox_file_request import CreateInboxFileRequest
from ..models.dataset_column import DatasetColumn
from ..models.dataset_new_request import DatasetNewRequest
from ..models.dataset_new_version_request import DatasetNewVersionRequest
from ..models.dataset_update_settings_request import DatasetUpdateSettingsRequest
from ..models.kaggle_models_extended import Competition
from ..models.kaggle_models_extended import Dataset
from ..models.kaggle_models_extended import DatasetNewResponse
from ..models.kaggle_models_extended import DatasetNewVersionResponse
from ..models.kaggle_models_extended import File
from ..models.kaggle_models_extended import Kernel
from ..models.kaggle_models_extended import KernelPushResponse
from ..models.kaggle_models_extended import LeaderboardEntry
from ..models.kaggle_models_extended import ListFilesResult
from ..models.kaggle_models_extended import Metadata
from ..models.kaggle_models_extended import Model
from ..models.kaggle_models_extended import ModelNewResponse
from ..models.kaggle_models_extended import ModelDeleteResponse
from ..models.kaggle_models_extended import ModelNewResponse
from ..models.kaggle_models_extended import ResumableUploadResult
from ..models.kaggle_models_extended import Submission
from ..models.kaggle_models_extended import SubmitResult
from ..models.kernel_push_request import KernelPushRequest
from ..models.license import License
from ..models.model_new_request import ModelNewRequest
from ..models.model_new_instance_request import ModelNewInstanceRequest
from ..models.model_instance_new_version_request import ModelInstanceNewVersionRequest
from ..models.model_update_request import ModelUpdateRequest
from ..models.model_instance_update_request import ModelInstanceUpdateRequest
from ..models.model_new_instance_request import ModelNewInstanceRequest
from ..models.model_new_request import ModelNewRequest
from ..models.model_update_request import ModelUpdateRequest
from ..models.start_blob_upload_request import StartBlobUploadRequest
from ..models.start_blob_upload_response import StartBlobUploadResponse
from ..models.upload_file import UploadFile
import requests
from requests.adapters import HTTPAdapter
import requests.packages.urllib3.exceptions as urllib3_exceptions
from requests.packages.urllib3.util.retry import Retry
from ..rest import ApiException
import six
from slugify import slugify
from tqdm import tqdm
import bleach
import time

from kagglesdk import KaggleClient, KaggleEnv
from kagglesdk.competitions.types.competition_api_service import *

try:
unicode # Python 2
except NameError:
unicode = str # Python 3


class DirectoryArchive(object):
Expand Down Expand Up @@ -375,11 +361,6 @@ class KaggleApi(KaggleApi):
]
dataset_file_fields = ['name', 'size', 'creationDate']

# Hack for https://github.com/Kaggle/kaggle-api/issues/22 / b/78194015
if six.PY2:
reload(sys)
sys.setdefaultencoding('latin1')

def _is_retriable(self, e):
return issubclass(type(e), ConnectionError) or \
issubclass(type(e), urllib3_exceptions.ConnectionError) or \
Expand Down Expand Up @@ -1770,12 +1751,12 @@ def dataset_create_version(self,
keywords = self.get_or_default(meta_data, 'keywords', [])

body = ApiCreateDatasetVersionRequestBody()
body.version_notes=version_notes
body.subtitle=subtitle
body.description=description
body.files=[]
body.category_ids=keywords
body.delete_old_versions=delete_old_versions
body.version_notes = version_notes
body.subtitle = subtitle
body.description = description
body.files = []
body.category_ids = keywords
body.delete_old_versions = delete_old_versions

with self.build_kaggle_client() as kaggle:
if id_no:
Expand Down Expand Up @@ -3930,7 +3911,7 @@ def print_csv(self, items, fields, labels=None):
writer.writerow(i_fields)

def string(self, item):
return item if isinstance(item, unicode) else str(item)
return item if isinstance(item, str) else str(item)

def get_or_fail(self, data, key):
if key in data:
Expand Down Expand Up @@ -4168,7 +4149,8 @@ def upload_complete(self, path, url, quiet, resume=False):

try:
if resume:
resumable_upload_result = self._resume_upload(url, file_size, quiet)
resumable_upload_result = self._resume_upload(path, url, file_size,
quiet)
if resumable_upload_result.result != ResumableUploadResult.INCOMPLETE:
return resumable_upload_result.result

Expand Down Expand Up @@ -4209,7 +4191,7 @@ def upload_complete(self, path, url, quiet, resume=False):
# in case it works on the next try.
return ResumableUploadResult.INCOMPLETE

def _resume_upload(self, url, content_length, quiet):
def _resume_upload(self, path, url, content_length, quiet):
# Documentation: https://developers.google.com/drive/api/guides/manage-uploads#resume-upload
session = requests.Session()
session.headers.update({
Expand All @@ -4223,7 +4205,7 @@ def _resume_upload(self, url, content_length, quiet):
return ResumableUploadResult.Complete()
if response.status_code == 404:
# Upload expired so need to start from scratch.
if not query:
if not quiet:
print('Upload of %s expired. Please try again.' % path)
return ResumableUploadResult.Failed()
if response.status_code == 308: # Resume Incomplete
Expand Down
7 changes: 5 additions & 2 deletions kaggle/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@

# coding=utf-8
from __future__ import print_function

import argparse
import json
from kaggle import api

import six

from kaggle import KaggleApi
from kaggle import api
from .rest import ApiException
import six


def main():
Expand Down
6 changes: 2 additions & 4 deletions kaggle/models/kaggle_models_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
# limitations under the License.

# coding=utf-8
import os
import time
from datetime import datetime


Expand Down Expand Up @@ -280,10 +278,10 @@ def __init__(self, result, bytes_uploaded=None):
self.bytes_uploaded = bytes_uploaded
self.start_at = 0 if bytes_uploaded is None else bytes_uploaded + 1

def Complete():
def Complete(self):
return ResumableUploadResult(ResumableUploadResult.COMPLETE)

def Failed():
def Failed(self):
return ResumableUploadResult(ResumableUploadResult.FAILED)

def Incomplete(bytes_uploaded=None):
Expand Down
Loading

0 comments on commit 63244ad

Please sign in to comment.