Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Python Code Cleanup (#70)
Browse files Browse the repository at this point in the history
* Python code cleanup by the cleanup-python-code Jenkins job.

This pull request was generated by the cleanup-python-code Jenkins job, which ran
```
modernize_travis;modernize_tox;make upgrade;find . -type f -name '*.py' | while read fname; do pyupgrade --exit-zero-even-if-changed --keep-percent-format --py3-plus --py36-plus --py38-plus "$fname"; done
```

The following packages were installed:
`pyupgrade,git+https://github.com/edx/repo-tools.git@c05453b733602ba7a2224ffbbf58a8d0903b62d8`

* Fix test failures

Co-authored-by: Zulqarnain <[email protected]>
  • Loading branch information
edx-requirements-bot and Zulqarnain authored Jan 18, 2021
1 parent c212171 commit 4df92ce
Show file tree
Hide file tree
Showing 34 changed files with 233 additions and 217 deletions.
22 changes: 8 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
language: python
python:
- 3.5
- 3.8
- '3.8'
env:
- TOXENV=quality
matrix:
include:
- python: 3.5
env: TOXENV=py35
- python: 3.8
env: TOXENV=py38
- TOXENV=py38
cache: pip
sudo: false
branches:
only:
- master
- /^\d+\.\d+(\.\d+)?(-\S*)?$/
install:
- pip install coveralls
- pip install -r requirements/tox.txt
- pip install coveralls
- pip install -r requirements/tox.txt
script: tox
after_success:
- coveralls
- bash ./scripts/build-stats-to-datadog.sh
- coveralls
- bash ./scripts/build-stats-to-datadog.sh
deploy:
provider: pypi
user: edx
distributions: sdist bdist_wheel
on:
tags: true
python: 3.5
condition: "$TOXENV = quality"
python: 3.8
condition: $TOXENV = quality
password:
secure: niAkz7MPmeGxu+dgicFGm8SydYPy0PYZI8jn/Rcs+B9ASPt8oJ0sFxGLtYLl7b0Rg9UcRrHHfoLOMWYK/0n+cdgGPJEmtXHnxBgzSdliYl5kO16A3omK4s7a8794qJ+4DM/LraFc2jU3Fg1t2PJZp4BuNMVIpSch3iZ3bvWhh/s=
2 changes: 1 addition & 1 deletion analyticsclient/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.16.1'
__version__ = '0.17.0'
2 changes: 0 additions & 2 deletions analyticsclient/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


from analyticsclient.constants import data_formats, http_methods


Expand Down
26 changes: 12 additions & 14 deletions analyticsclient/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import logging

import requests
Expand Down Expand Up @@ -95,10 +93,10 @@ def request(self, method, resource, data=None, timeout=None, data_format=data_fo

try:
return response.json()
except ValueError:
except ValueError as exception:
message = 'Unable to decode JSON response'
log.exception(message)
raise ClientError(message)
raise ClientError(message) from exception

def has_resource(self, resource, timeout=None):
"""
Expand Down Expand Up @@ -138,7 +136,7 @@ def _request(self, method, resource, data=None, timeout=None, data_format=data_f
headers['Authorization'] = 'Token ' + self.auth_token

try:
uri = '{0}/{1}'.format(self.base_url, resource)
uri = f'{self.base_url}/{resource}'

if method == http_methods.GET:
params = self._data_to_get_params(data or {})
Expand All @@ -147,7 +145,7 @@ def _request(self, method, resource, data=None, timeout=None, data_format=data_f
response = requests.post(uri, data=(data or {}), headers=headers, timeout=timeout)
else:
raise ValueError(
'Invalid \'method\' argument: expected {0} or {1}, got {2}'.format(
'Invalid \'method\' argument: expected {} or {}, got {}'.format(
http_methods.GET,
http_methods.POST,
method,
Expand All @@ -156,30 +154,30 @@ def _request(self, method, resource, data=None, timeout=None, data_format=data_f

status = response.status_code
if status != requests.codes.ok:
message = 'Resource "{0}" returned status code {1}'.format(resource, status)
message = f'Resource "{resource}" returned status code {status}'
error_class = ClientError

if status == requests.codes.bad_request:
message = 'The request to {0} was invalid.'.format(uri)
message = f'The request to {uri} was invalid.'
error_class = InvalidRequestError
elif status == requests.codes.not_found:
message = 'Resource {0} was not found on the API server.'.format(uri)
message = f'Resource {uri} was not found on the API server.'
error_class = NotFoundError

log.error(message)
raise error_class(message)

return response

except requests.exceptions.Timeout:
message = "Response from {0} exceeded timeout of {1}s.".format(resource, timeout)
except requests.exceptions.Timeout as exception:
message = f"Response from {resource} exceeded timeout of {timeout}s."
log.exception(message)
raise TimeoutError(message)
raise TimeoutError(message) from exception

except requests.exceptions.RequestException:
except requests.exceptions.RequestException as exception:
message = 'Unable to retrieve resource'
log.exception(message)
raise ClientError('{0} "{1}"'.format(message, resource))
raise ClientError(f'{message} "{resource}"') from exception

@staticmethod
def _data_to_get_params(data):
Expand Down
2 changes: 1 addition & 1 deletion analyticsclient/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
UNKNOWN_COUNTRY_CODE = u'UNKNOWN'
UNKNOWN_COUNTRY_CODE = 'UNKNOWN'
8 changes: 4 additions & 4 deletions analyticsclient/constants/activity_types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Course activity types."""

ANY = u'any'
ATTEMPTED_PROBLEM = u'attempted_problem'
PLAYED_VIDEO = u'played_video'
POSTED_FORUM = u'posted_forum'
ANY = 'any'
ATTEMPTED_PROBLEM = 'attempted_problem'
PLAYED_VIDEO = 'played_video'
POSTED_FORUM = 'posted_forum'
8 changes: 4 additions & 4 deletions analyticsclient/constants/demographics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Course demographics."""

BIRTH_YEAR = u'birth_year'
EDUCATION = u'education'
GENDER = u'gender'
LOCATION = u'location'
BIRTH_YEAR = 'birth_year'
EDUCATION = 'education'
GENDER = 'gender'
LOCATION = 'location'
18 changes: 9 additions & 9 deletions analyticsclient/constants/education_levels.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NONE = u'none'
OTHER = u'other'
PRIMARY = u'primary'
JUNIOR_SECONDARY = u'junior_secondary'
SECONDARY = u'secondary'
ASSOCIATES = u'associates'
BACHELORS = u'bachelors'
MASTERS = u'masters'
DOCTORATE = u'doctorate'
NONE = 'none'
OTHER = 'other'
PRIMARY = 'primary'
JUNIOR_SECONDARY = 'junior_secondary'
SECONDARY = 'secondary'
ASSOCIATES = 'associates'
BACHELORS = 'bachelors'
MASTERS = 'masters'
DOCTORATE = 'doctorate'
12 changes: 6 additions & 6 deletions analyticsclient/constants/enrollment_modes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
AUDIT = u'audit'
CREDIT = u'credit'
HONOR = u'honor'
PROFESSIONAL = u'professional'
VERIFIED = u'verified'
MASTERS = u'masters'
AUDIT = 'audit'
CREDIT = 'credit'
HONOR = 'honor'
PROFESSIONAL = 'professional'
VERIFIED = 'verified'
MASTERS = 'masters'

ALL = [AUDIT, CREDIT, HONOR, PROFESSIONAL, VERIFIED, MASTERS]
8 changes: 4 additions & 4 deletions analyticsclient/constants/genders.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FEMALE = u'female'
MALE = u'male'
OTHER = u'other'
UNKNOWN = u'unknown'
FEMALE = 'female'
MALE = 'male'
OTHER = 'other'
UNKNOWN = 'unknown'
18 changes: 9 additions & 9 deletions analyticsclient/constants/http_methods.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
GET = u'GET'
HEAD = u'HEAD'
POST = u'POST'
PUT = u'PUT'
DELETE = u'DELETE'
CONNECT = u'CONNECT'
OPTIONS = u'OPTIONS'
TRACE = u'TRACE'
PATCH = u'PATCH'
GET = 'GET'
HEAD = 'HEAD'
POST = 'POST'
PUT = 'PUT'
DELETE = 'DELETE'
CONNECT = 'CONNECT'
OPTIONS = 'OPTIONS'
TRACE = 'TRACE'
PATCH = 'PATCH'
24 changes: 11 additions & 13 deletions analyticsclient/course.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import warnings
from urllib.parse import urlencode

Expand All @@ -21,7 +19,7 @@ def __init__(self, client, course_id):
course_id (str): String identifying the course (e.g. edX/DemoX/Demo_Course)
"""
super(Course, self).__init__(client)
super().__init__(client)
self.course_id = str(course_id)

def enrollment(self, demographic=None, start_date=None, end_date=None, data_format=data_formats.JSON):
Expand All @@ -41,9 +39,9 @@ def enrollment(self, demographic=None, start_date=None, end_date=None, data_form
end_date (str): Maximum date for returned enrollment data
data_format (str): Format in which data should be returned
"""
path = 'courses/{0}/enrollment/'.format(self.course_id)
path = f'courses/{self.course_id}/enrollment/'
if demographic:
path += '{0}/'.format(demographic)
path += f'{demographic}/'

params = {}
if start_date:
Expand All @@ -54,7 +52,7 @@ def enrollment(self, demographic=None, start_date=None, end_date=None, data_form

querystring = urlencode(params)
if querystring:
path += '?{0}'.format(querystring)
path += f'?{querystring}'

return self.client.get(path, data_format=data_format)

Expand All @@ -79,9 +77,9 @@ def activity(self, activity_type=activity_types.ANY, start_date=None, end_date=N
if end_date:
params['end_date'] = end_date

path = 'courses/{0}/activity/'.format(self.course_id)
path = f'courses/{self.course_id}/activity/'
querystring = urlencode(params)
path += '?{0}'.format(querystring)
path += f'?{querystring}'

return self.client.get(path, data_format=data_format)

Expand All @@ -95,7 +93,7 @@ def recent_activity(self, activity_type=activity_types.ANY, data_format=data_for
"""
warnings.warn('recent_activity has been deprecated! Use activity instead.', DeprecationWarning)

path = 'courses/{0}/recent_activity/?activity_type={1}'.format(self.course_id, activity_type)
path = f'courses/{self.course_id}/recent_activity/?activity_type={activity_type}'
return self.client.get(path, data_format=data_format)

def problems(self, data_format=data_formats.JSON):
Expand All @@ -105,7 +103,7 @@ def problems(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which data should be returned
"""
path = 'courses/{0}/problems/'.format(self.course_id)
path = f'courses/{self.course_id}/problems/'
return self.client.get(path, data_format=data_format)

def problems_and_tags(self, data_format=data_formats.JSON):
Expand All @@ -115,7 +113,7 @@ def problems_and_tags(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which data should be returned
"""
path = 'courses/{0}/problems_and_tags/'.format(self.course_id)
path = f'courses/{self.course_id}/problems_and_tags/'
return self.client.get(path, data_format=data_format)

def reports(self, report_name, data_format=data_formats.JSON):
Expand All @@ -125,7 +123,7 @@ def reports(self, report_name, data_format=data_formats.JSON):
Arguments:
report_name (str): Report name, e.g. "problem_response"
"""
path = 'courses/{0}/reports/{1}/'.format(self.course_id, report_name)
path = f'courses/{self.course_id}/reports/{report_name}/'
return self.client.get(path, data_format=data_format)

def videos(self, data_format=data_formats.JSON):
Expand All @@ -135,5 +133,5 @@ def videos(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which data should be returned
"""
path = 'courses/{0}/videos/'.format(self.course_id)
path = f'courses/{self.course_id}/videos/'
return self.client.get(path, data_format=data_format)
2 changes: 0 additions & 2 deletions analyticsclient/course_summaries.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


import datetime

from analyticsclient.base import PostableCourseIDsEndpoint
Expand Down
2 changes: 0 additions & 2 deletions analyticsclient/course_totals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


from analyticsclient.base import PostableCourseIDsEndpoint
from analyticsclient.constants import data_formats

Expand Down
5 changes: 2 additions & 3 deletions analyticsclient/engagement_timeline.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

from urllib.parse import urlencode

from analyticsclient.base import PostableCourseIDsEndpoint
Expand All @@ -19,13 +18,13 @@ def __init__(self, client, username, course_id):
course_id (str): String identifying the course (e.g. edX/DemoX/Demo_Course)
"""
super(EngagementTimeline, self).__init__(client)
super().__init__(client)

self.username = str(username)
self.course_id = str(course_id)

def get(self):
"""Get a particular learner's engagement timeline for a particular course."""
querystring = urlencode({'course_id': self.course_id})
path = 'engagement_timelines/{username}/?{querystring}'.format(username=self.username, querystring=querystring)
path = f'engagement_timelines/{self.username}/?{querystring}'
return self.client.get(path, data_format=data_formats.JSON)
12 changes: 5 additions & 7 deletions analyticsclient/module.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


from analyticsclient.base import BaseEndpoint
from analyticsclient.constants import data_formats

Expand All @@ -16,7 +14,7 @@ def __init__(self, client, course_id, module_id):
course_id (str): String identifying the course
module_id (str): String identifying the module
"""
super(Module, self).__init__(client)
super().__init__(client)
self.course_id = str(course_id)
self.module_id = str(module_id)

Expand All @@ -27,7 +25,7 @@ def answer_distribution(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which to return data (default is JSON)
"""
path = 'problems/{0}/answer_distribution/'.format(self.module_id)
path = f'problems/{self.module_id}/answer_distribution/'

return self.client.get(path, data_format=data_format)

Expand All @@ -38,7 +36,7 @@ def grade_distribution(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which to return data (default is JSON)
"""
path = 'problems/{0}/grade_distribution/'.format(self.module_id)
path = f'problems/{self.module_id}/grade_distribution/'

return self.client.get(path, data_format=data_format)

Expand All @@ -49,7 +47,7 @@ def sequential_open_distribution(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which to return data (default is JSON)
"""
path = 'problems/{0}/sequential_open_distribution/'.format(self.module_id)
path = f'problems/{self.module_id}/sequential_open_distribution/'

return self.client.get(path, data_format=data_format)

Expand All @@ -60,6 +58,6 @@ def video_timeline(self, data_format=data_formats.JSON):
Arguments:
data_format (str): Format in which to return data (default is JSON)
"""
path = 'videos/{0}/timeline/'.format(self.module_id)
path = f'videos/{self.module_id}/timeline/'

return self.client.get(path, data_format=data_format)
2 changes: 1 addition & 1 deletion analyticsclient/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ def programs(self, program_ids=None, fields=None, exclude=None, data_format=data
path = 'programs/'
querystring = urlencode(query_params)
if querystring:
path += '?{0}'.format(querystring)
path += f'?{querystring}'

return self.client.get(path, data_format=data_format)
Loading

0 comments on commit 4df92ce

Please sign in to comment.