Skip to content

Commit

Permalink
Upgrade syntax for Python 3.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Oct 25, 2020
1 parent c161a9a commit f08c26f
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 69 deletions.
1 change: 0 additions & 1 deletion examples/coco/upload_coco2017.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def get_image_name(args, tag, id):
def load_dataset(args, tag):
with open(
os.path.join(args.dataset_path, f"annotations/instances_{tag}{args.year}.json"),
"r",
) as f:
instances = json.load(f)
# print(instances.keys())
Expand Down
6 changes: 3 additions & 3 deletions examples/fashion-mnist/train_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
super().__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
Expand Down Expand Up @@ -85,9 +85,9 @@ def main():
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=MOMENTUM)

for epoch in range(EPOCHS):
print("Starting Training Epoch {}".format(epoch))
print(f"Starting Training Epoch {epoch}")
train(model, train_loader, optimizer)
print("Training Epoch {} finished\n".format(epoch))
print(f"Training Epoch {epoch} finished\n")
test(model, test_loader)

# sanity check to see outputs of model
Expand Down
8 changes: 4 additions & 4 deletions examples/fashion-mnist/train_tf_gradient_tape.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def train(model, train_dataset, optimizer, loss_fn, train_acc_metric):
train_acc_metric.update_state(batch["labels"], pred)

train_acc = train_acc_metric.result()
print("Training acc: %.4f" % (float(train_acc),))
print("Training acc: {:.4f}".format(float(train_acc)))
train_acc_metric.reset_states()


Expand All @@ -43,7 +43,7 @@ def test(model, test_dataset, test_acc_metric):
test_acc_metric.update_state(batch["labels"], pred)

test_acc = test_acc_metric.result()
print("Test acc: %.4f" % (float(test_acc),))
print("Test acc: {:.4f}".format(float(test_acc)))
test_acc_metric.reset_states()


Expand Down Expand Up @@ -74,9 +74,9 @@ def main():
# model.summary()

for epoch in range(EPOCHS):
print("\nStarting Training Epoch {}".format(epoch))
print(f"\nStarting Training Epoch {epoch}")
train(model, train_dataset, optimizer, loss_fn, train_acc_metric)
print("Training Epoch {} finished\n".format(epoch))
print(f"Training Epoch {epoch} finished\n")
test(model, test_dataset, test_acc_metric)

# sanity check to see outputs of model
Expand Down
2 changes: 1 addition & 1 deletion hub/cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@click.option(
"-h",
"--host",
default="{}".format(config.HUB_REST_ENDPOINT),
default=f"{config.HUB_REST_ENDPOINT}",
help="Hub rest endpoint",
)
@click.option("-v", "--verbose", count=True, help="Devel debugging")
Expand Down
8 changes: 4 additions & 4 deletions hub/client/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class AuthClient(HubHttpClient):
"""

def __init__(self):
super(AuthClient, self).__init__()
super().__init__()

def check_token(self, access_token):
auth = "Bearer {}".format(access_token)
auth = f"Bearer {access_token}"
response = self.request(
"GET", config.CHECK_TOKEN_REST_SUFFIX, headers={"Authorization": auth}
)
Expand All @@ -22,7 +22,7 @@ def check_token(self, access_token):
response_dict = response.json()
is_valid = response_dict["is_valid"]
except Exception as e:
logger.error("Exception occured while validating token: {}.".format(e))
logger.error(f"Exception occured while validating token: {e}.")
raise HubException(
"Error while validating the token. \
Please try logging in using username ans password."
Expand All @@ -41,7 +41,7 @@ def get_access_token(self, username, password):
token_dict = response.json()
token = token_dict["token"]
except Exception as e:
logger.error("Exception occured while getting token: {}.".format(e))
logger.error(f"Exception occured while getting token: {e}.")
raise HubException(
"Error while loggin in. \
Please try logging in using access token."
Expand Down
6 changes: 3 additions & 3 deletions hub/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def urljoin(*args):
return "/".join(map(lambda x: str(x).strip("/"), args))


class HubHttpClient(object):
class HubHttpClient:
"""
Basic communication with Hub AI Controller rest API
"""
Expand Down Expand Up @@ -60,7 +60,7 @@ def request(
headers["Authorization"] = self.auth_header

try:
logger.debug("Sending: Headers {}, Json: {}".format(headers, json))
logger.debug(f"Sending: Headers {headers}, Json: {json}")
response = requests.request(
method,
request_url,
Expand Down Expand Up @@ -100,7 +100,7 @@ def check_response_status(self, response):
message = " "

logger.debug(
'Error received: status code: {}, message: "{}"'.format(code, message)
f'Error received: status code: {code}, message: "{message}"'
)
if code == 400:
raise BadRequestException(response)
Expand Down
4 changes: 2 additions & 2 deletions hub/client/hub_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HubControlClient(HubHttpClient):
"""

def __init__(self):
super(HubControlClient, self).__init__()
super().__init__()
self.details = self.get_config()

def get_dataset_path(self, tag):
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_config(self, reset=False):
if not os.path.isfile(config.STORE_CONFIG_PATH) or self.auth_header is None:
self.get_credentials()

with open(config.STORE_CONFIG_PATH, "r") as file:
with open(config.STORE_CONFIG_PATH) as file:
details = file.readlines()
details = json.loads("".join(details))

Expand Down
10 changes: 5 additions & 5 deletions hub/client/token_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from hub.log import logger


class TokenManager(object):
class TokenManager:
""" manages access tokens """

@classmethod
Expand All @@ -14,7 +14,7 @@ def is_authenticated(cls):
@classmethod
def set_token(cls, token):
logger.debug(
"Putting the key {} into {}.".format(token, config.TOKEN_FILE_PATH)
f"Putting the key {token} into {config.TOKEN_FILE_PATH}."
)
path = Path(config.TOKEN_FILE_PATH)
os.makedirs(path.parent, exist_ok=True)
Expand All @@ -26,17 +26,17 @@ def get_token(cls):
logger.debug("Getting token...")
if not os.path.exists(config.TOKEN_FILE_PATH):
return None
with open(config.TOKEN_FILE_PATH, "r") as f:
with open(config.TOKEN_FILE_PATH) as f:
token = f.read()
logger.debug("Got the key {} from {}.".format(token, config.TOKEN_FILE_PATH))
logger.debug(f"Got the key {token} from {config.TOKEN_FILE_PATH}.")
return token

@classmethod
def get_auth_header(cls):
logger.debug("Constructing auth header...")
token = cls.get_token()
if token:
return "Bearer {}".format(token)
return f"Bearer {token}"
return None

@classmethod
Expand Down
26 changes: 13 additions & 13 deletions hub/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class S3CredsParseException(Exception):

class HubException(ClickException):
def __init__(self, message=None, code=None):
super(HubException, self).__init__(message)
super().__init__(message)


class AuthenticationException(HubException):
def __init__(self, message="Authentication failed. Please login again."):
super(AuthenticationException, self).__init__(message=message)
super().__init__(message=message)


class AuthorizationException(HubException):
Expand All @@ -83,15 +83,15 @@ def __init__(self, response):
message = response.json()["message"]
except (KeyError, AttributeError):
message = "You are not authorized to access this resource on Snark AI."
super(AuthorizationException, self).__init__(message=message)
super().__init__(message=message)


class NotFoundException(HubException):
def __init__(
self,
message="The resource you are looking for was not found. Check if the name or id is correct.",
):
super(NotFoundException, self).__init__(message=message)
super().__init__(message=message)


class BadRequestException(HubException):
Expand All @@ -104,49 +104,49 @@ def __init__(self, response):
message = "One or more request parameters is incorrect, %s" % str(
response.content
)
super(BadRequestException, self).__init__(message=message)
super().__init__(message=message)


class OverLimitException(HubException):
def __init__(
self,
message="You are over the allowed limits for this operation. Consider upgrading your account.",
):
super(OverLimitException, self).__init__(message=message)
super().__init__(message=message)


class ServerException(HubException):
def __init__(self, message="Internal Snark AI server error."):
super(ServerException, self).__init__(message=message)
super().__init__(message=message)


class BadGatewayException(HubException):
def __init__(self, message="Invalid response from Snark AI server."):
super(BadGatewayException, self).__init__(message=message)
super().__init__(message=message)


class GatewayTimeoutException(HubException):
def __init__(self, message="Snark AI server took too long to respond."):
super(GatewayTimeoutException, self).__init__(message=message)
super().__init__(message=message)


class WaitTimeoutException(HubException):
def __init__(self, message="Timeout waiting for server state update."):
super(WaitTimeoutException, self).__init__(message=message)
super().__init__(message=message)


class LockedException(HubException):
def __init__(self, message="Resource locked."):
super(LockedException, self).__init__(message=message)
super().__init__(message=message)


class DatasetNotFound(HubException):
def __init__(self, response):
message = f"The dataset with tag {response} was not found"
super(DatasetNotFound, self).__init__(message=message)
super().__init__(message=message)


class PermissionException(HubException):
def __init__(self, response):
message = f"No permision to store the dataset at {response}"
super(PermissionException, self).__init__(message=message)
super().__init__(message=message)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
with open(os.path.join(this_directory, "README.md")) as f:
long_description = f.read()

with open(os.path.join(this_directory, "requirements.txt"), "r") as f:
with open(os.path.join(this_directory, "requirements.txt")) as f:
requirements = f.readlines()

setup(
Expand Down
16 changes: 8 additions & 8 deletions test/benchmark/old/baseline.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def main_worker(gpu, ngpus_per_node, args):
args.gpu = gpu

if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
print(f"Use GPU: {args.gpu} for training")

if args.distributed:
if args.dist_url == "env://" and args.rank == -1:
Expand All @@ -130,10 +130,10 @@ def main_worker(gpu, ngpus_per_node, args):
world_size=args.world_size, rank=args.rank)
# create model
if args.pretrained:
print("=> using pre-trained model '{}'".format(args.arch))
print(f"=> using pre-trained model '{args.arch}'")
model = models.__dict__[args.arch](pretrained=True)
else:
print("=> creating model '{}'".format(args.arch))
print(f"=> creating model '{args.arch}'")
model = models.__dict__[args.arch]()

if args.distributed:
Expand Down Expand Up @@ -175,7 +175,7 @@ def main_worker(gpu, ngpus_per_node, args):
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
print(f"=> loading checkpoint '{args.resume}'")
checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch']
best_acc1 = checkpoint['best_acc1']
Expand All @@ -187,7 +187,7 @@ def main_worker(gpu, ngpus_per_node, args):
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.resume))
print(f"=> no checkpoint found at '{args.resume}'")

cudnn.benchmark = True

Expand Down Expand Up @@ -264,7 +264,7 @@ def train(train_loader, model, criterion, optimizer, epoch, args):
progress = ProgressMeter(
len(train_loader),
[batch_time, data_time, losses, top1, top5],
prefix="Epoch: [{}]".format(epoch))
prefix=f"Epoch: [{epoch}]")

# switch to train mode
model.train()
Expand Down Expand Up @@ -351,7 +351,7 @@ def save_checkpoint(state, is_best, filename='checkpoint.pth.tar'):
shutil.copyfile(filename, 'model_best.pth.tar')


class AverageMeter(object):
class AverageMeter:
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
Expand All @@ -375,7 +375,7 @@ def __str__(self):
return fmtstr.format(**self.__dict__)


class ProgressMeter(object):
class ProgressMeter:
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
Expand Down
Loading

0 comments on commit f08c26f

Please sign in to comment.