Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python Code Cleanup #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
language: python
python:
- 3.5
- 3.8
- '3.8'
install:
- make travis.requirements
script:
- make test
after_script:
- coveralls
branches:
only:
- master
only:
- master
12 changes: 6 additions & 6 deletions grader_support/gradelib.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self, message=None):
Exception.__init__(self, message)


class Test(object):
class Test:
"""
A simple class to wrap a test function and its descriptions.

Expand Down Expand Up @@ -53,7 +53,7 @@ def compare_results(self, expected, actual):
return expected == actual


class Grader(object):
class Grader:
def __init__(self):
"""
Create an empty grader.
Expand Down Expand Up @@ -239,7 +239,7 @@ def _tokens(code):
"""
# Protect against pathological inputs: http://bugs.python.org/issue16152
code = code.rstrip() + "\n"
if isinstance(code, six.text_type):
if isinstance(code, str):
code = code.encode('utf8')
code = "# coding: utf8\n" + code
toks = tokenize.generate_tokens(six.BytesIO(code).readline)
Expand Down Expand Up @@ -481,7 +481,7 @@ def exec_wrapped_code(environment=None, post_process=None):
environment = {}
def test_fn(submission_module):
with capture_stdout() as stdout:
six.exec_(submission_module.submission_code, environment)
exec(submission_module.submission_code, environment)
stdout_text = stdout.getvalue()
if post_process:
stdout_text = post_process(stdout_text)
Expand All @@ -503,7 +503,7 @@ def exec_code_and_inspect_values(environment=None, vars_to_inspect=None, post_pr
environment = {}
def test_fn(submission_module):
with capture_stdout() as stdout:
six.exec_(submission_module.submission_code, environment)
exec(submission_module.submission_code, environment)

for var in vars_to_inspect:
print(var)
Expand All @@ -530,7 +530,7 @@ def invoke_student_function(fn_name, args, environment=None, output_writer=None)
"""
output_writer = output_writer or repr
def doit(submission_module):
for name, value in six.iteritems(environment or {}):
for name, value in (environment or {}).items():
setattr(submission_module, name, value)
fn = getattr(submission_module, fn_name)
print(output_writer(fn(*args)))
Expand Down
6 changes: 3 additions & 3 deletions grader_support/graderutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def captured_stdout():
sys.stdout = old_stdout


class ChangeDirectory(object):
class ChangeDirectory:
def __init__(self, new_dir):
self.old_dir = os.getcwd()
os.chdir(new_dir)
Expand All @@ -58,7 +58,7 @@ def change_directory(new_dir):
cd.clean_up()


class TempDirectory(object):
class TempDirectory:
def __init__(self, delete_when_done=True):
self.delete_when_done = delete_when_done
self.temp_dir = tempfile.mkdtemp(prefix="grader-")
Expand All @@ -84,7 +84,7 @@ def temp_directory(delete_when_done=True):
tmp.clean_up()


class ModuleIsolation(object):
class ModuleIsolation:
"""
Manage changes to sys.modules so that we can roll back imported modules.

Expand Down
6 changes: 1 addition & 5 deletions grader_support/run.py
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Run a set of tests on a submission, printing the outputs to stdout as a json
string.
Expand Down Expand Up @@ -31,10 +30,7 @@
fallback=True,
languages=[graderutil.LANGUAGE]
)
if six.PY2:
trans.install(names=None, unicode=True)
else:
trans.install(names=None)
trans.install(names=None)


def run(grader_name, submission_name, seed=1):
Expand Down
2 changes: 1 addition & 1 deletion load_test/mock_xqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_submission():
response = {
'return_code': 0,
'content': flask.json.dumps({
'xqueue_header': '{}.{}'.format(counter.next(), idx),
'xqueue_header': f'{counter.next()}.{idx}',
'xqueue_body': flask.json.dumps({
'student_response': submission,
'grader_payload': flask.json.dumps(payload)
Expand Down
22 changes: 8 additions & 14 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
# make upgrade
#
-e git+https://github.com/edx/codejail.git@4127fc4bd5775cc72aee8d7f0a70e31405e22439#egg=codejail # via -r requirements/base.in
backports.os==0.1.1 # via path.py
certifi==2020.6.20 # via requests
chardet==3.0.4 # via requests
configparser==4.0.2 # via importlib-metadata
contextlib2==0.6.0.post1 # via importlib-metadata, zipp
certifi==2020.12.5 # via requests
chardet==4.0.0 # via requests
dogstatsd-python==0.5.6 # via -r requirements/base.in
future==0.18.2 # via backports.os
idna==2.10 # via requests
importlib-metadata==1.7.0 # via path.py
newrelic==5.14.1.144 # via -r requirements/base.in
importlib-metadata==3.4.0 # via path.py
newrelic==6.0.1.155 # via -r requirements/base.in
path.py==11.5.2 # via -c requirements/constraints.txt, -r requirements/base.in
pathlib2==2.3.5 # via importlib-metadata
requests==2.24.0 # via -r requirements/base.in
scandir==1.10.0 # via pathlib2
six==1.15.0 # via -r requirements/base.in, pathlib2
urllib3==1.25.9 # via requests
zipp==1.2.0 # via importlib-metadata
requests==2.25.1 # via -r requirements/base.in
six==1.15.0 # via -r requirements/base.in
urllib3==1.26.3 # via requests
zipp==3.4.0 # via importlib-metadata
3 changes: 3 additions & 0 deletions requirements/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
# pin when possible. Writing an issue against the offending project and
# linking to it here is good.

# Common constraints for edx repos
-c https://raw.githubusercontent.com/edx/edx-lint/master/edx_lint/files/common_constraints.txt

# After this version drops support for python 2.7
path.py<=11.5.2
3 changes: 1 addition & 2 deletions requirements/pip_tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
# make upgrade
#
click==7.1.2 # via pip-tools
pip-tools==5.2.1 # via -r requirements/pip_tools.in
six==1.15.0 # via pip-tools
pip-tools==5.5.0 # via -r requirements/pip_tools.in

# The following packages are considered to be unsafe in a requirements file:
# pip
22 changes: 8 additions & 14 deletions requirements/production.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@
# make upgrade
#
-e git+https://github.com/edx/codejail.git@4127fc4bd5775cc72aee8d7f0a70e31405e22439#egg=codejail # via -r requirements/base.txt
backports.os==0.1.1 # via -r requirements/base.txt, path.py
certifi==2020.6.20 # via -r requirements/base.txt, requests
chardet==3.0.4 # via -r requirements/base.txt, requests
configparser==4.0.2 # via -r requirements/base.txt, importlib-metadata
contextlib2==0.6.0.post1 # via -r requirements/base.txt, importlib-metadata, zipp
certifi==2020.12.5 # via -r requirements/base.txt, requests
chardet==4.0.0 # via -r requirements/base.txt, requests
dogstatsd-python==0.5.6 # via -r requirements/base.txt
future==0.18.2 # via -r requirements/base.txt, backports.os
idna==2.10 # via -r requirements/base.txt, requests
importlib-metadata==1.7.0 # via -r requirements/base.txt, path.py
newrelic==5.14.1.144 # via -r requirements/base.txt
importlib-metadata==3.4.0 # via -r requirements/base.txt, path.py
newrelic==6.0.1.155 # via -r requirements/base.txt
path.py==11.5.2 # via -c requirements/constraints.txt, -r requirements/base.txt
pathlib2==2.3.5 # via -r requirements/base.txt, importlib-metadata
requests==2.24.0 # via -r requirements/base.txt
scandir==1.10.0 # via -r requirements/base.txt, pathlib2
six==1.15.0 # via -r requirements/base.txt, pathlib2
urllib3==1.25.9 # via -r requirements/base.txt, requests
zipp==1.2.0 # via -r requirements/base.txt, importlib-metadata
requests==2.25.1 # via -r requirements/base.txt
six==1.15.0 # via -r requirements/base.txt
urllib3==1.26.3 # via -r requirements/base.txt, requests
zipp==3.4.0 # via -r requirements/base.txt, importlib-metadata
43 changes: 17 additions & 26 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,25 @@
# make upgrade
#
-e git+https://github.com/edx/codejail.git@4127fc4bd5775cc72aee8d7f0a70e31405e22439#egg=codejail # via -r requirements/production.txt
atomicwrites==1.4.0 # via pytest
attrs==19.3.0 # via pytest
backports.functools-lru-cache==1.6.1 # via wcwidth
backports.os==0.1.1 # via -r requirements/production.txt, path.py
certifi==2020.6.20 # via -r requirements/production.txt, requests
chardet==3.0.4 # via -r requirements/production.txt, requests
configparser==4.0.2 # via -r requirements/production.txt, importlib-metadata
contextlib2==0.6.0.post1 # via -r requirements/production.txt, importlib-metadata, zipp
coverage==5.1 # via pytest-cov
attrs==20.3.0 # via pytest
certifi==2020.12.5 # via -r requirements/production.txt, requests
chardet==4.0.0 # via -r requirements/production.txt, requests
coverage==5.4 # via pytest-cov
dogstatsd-python==0.5.6 # via -r requirements/production.txt
funcsigs==1.0.2 # via mock, pytest
future==0.18.2 # via -r requirements/production.txt, backports.os
idna==2.10 # via -r requirements/production.txt, requests
importlib-metadata==1.7.0 # via -r requirements/production.txt, path.py, pluggy, pytest
mock==3.0.5 # via -r requirements/test.in
more-itertools==5.0.0 # via pytest
newrelic==5.14.1.144 # via -r requirements/production.txt
packaging==20.4 # via pytest
importlib-metadata==3.4.0 # via -r requirements/production.txt, path.py
iniconfig==1.1.1 # via pytest
mock==4.0.3 # via -r requirements/test.in
newrelic==6.0.1.155 # via -r requirements/production.txt
packaging==20.8 # via pytest
path.py==11.5.2 # via -c requirements/constraints.txt, -r requirements/production.txt
pathlib2==2.3.5 # via -r requirements/production.txt, importlib-metadata, pytest
pluggy==0.13.1 # via pytest
py==1.9.0 # via pytest
py==1.10.0 # via pytest
pyparsing==2.4.7 # via packaging
pytest-cov==2.10.0 # via -r requirements/test.in
pytest==4.6.11 # via pytest-cov
requests==2.24.0 # via -r requirements/production.txt
scandir==1.10.0 # via -r requirements/production.txt, pathlib2
six==1.15.0 # via -r requirements/production.txt, mock, more-itertools, packaging, pathlib2, pytest
urllib3==1.25.9 # via -r requirements/production.txt, requests
wcwidth==0.2.5 # via pytest
zipp==1.2.0 # via -r requirements/production.txt, importlib-metadata
pytest-cov==2.11.1 # via -r requirements/test.in
pytest==6.2.2 # via pytest-cov
requests==2.25.1 # via -r requirements/production.txt
six==1.15.0 # via -r requirements/production.txt
toml==0.10.2 # via pytest
urllib3==1.26.3 # via -r requirements/production.txt, requests
zipp==3.4.0 # via -r requirements/production.txt, importlib-metadata
43 changes: 17 additions & 26 deletions requirements/travis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,25 @@
# make upgrade
#
-e git+https://github.com/edx/codejail.git@4127fc4bd5775cc72aee8d7f0a70e31405e22439#egg=codejail # via -r requirements/test.txt
atomicwrites==1.4.0 # via -r requirements/test.txt, pytest
attrs==19.3.0 # via -r requirements/test.txt, pytest
backports.functools-lru-cache==1.6.1 # via -r requirements/test.txt, wcwidth
backports.os==0.1.1 # via -r requirements/test.txt, path.py
certifi==2020.6.20 # via -r requirements/test.txt, requests
chardet==3.0.4 # via -r requirements/test.txt, requests
configparser==4.0.2 # via -r requirements/test.txt, importlib-metadata
contextlib2==0.6.0.post1 # via -r requirements/test.txt, importlib-metadata, zipp
coverage==5.1 # via -r requirements/test.txt, -r requirements/travis.in, pytest-cov
attrs==20.3.0 # via -r requirements/test.txt, pytest
certifi==2020.12.5 # via -r requirements/test.txt, requests
chardet==4.0.0 # via -r requirements/test.txt, requests
coverage==5.4 # via -r requirements/test.txt, -r requirements/travis.in, pytest-cov
dogstatsd-python==0.5.6 # via -r requirements/test.txt
funcsigs==1.0.2 # via -r requirements/test.txt, mock, pytest
future==0.18.2 # via -r requirements/test.txt, backports.os
idna==2.10 # via -r requirements/test.txt, requests
importlib-metadata==1.7.0 # via -r requirements/test.txt, path.py, pluggy, pytest
mock==3.0.5 # via -r requirements/test.txt
more-itertools==5.0.0 # via -r requirements/test.txt, pytest
newrelic==5.14.1.144 # via -r requirements/test.txt
packaging==20.4 # via -r requirements/test.txt, pytest
importlib-metadata==3.4.0 # via -r requirements/test.txt, path.py
iniconfig==1.1.1 # via -r requirements/test.txt, pytest
mock==4.0.3 # via -r requirements/test.txt
newrelic==6.0.1.155 # via -r requirements/test.txt
packaging==20.8 # via -r requirements/test.txt, pytest
path.py==11.5.2 # via -c requirements/constraints.txt, -r requirements/test.txt
pathlib2==2.3.5 # via -r requirements/test.txt, importlib-metadata, pytest
pluggy==0.13.1 # via -r requirements/test.txt, pytest
py==1.9.0 # via -r requirements/test.txt, pytest
py==1.10.0 # via -r requirements/test.txt, pytest
pyparsing==2.4.7 # via -r requirements/test.txt, packaging
pytest-cov==2.10.0 # via -r requirements/test.txt
pytest==4.6.11 # via -r requirements/test.txt, pytest-cov
requests==2.24.0 # via -r requirements/test.txt
scandir==1.10.0 # via -r requirements/test.txt, pathlib2
six==1.15.0 # via -r requirements/test.txt, mock, more-itertools, packaging, pathlib2, pytest
urllib3==1.25.9 # via -r requirements/test.txt, requests
wcwidth==0.2.5 # via -r requirements/test.txt, pytest
zipp==1.2.0 # via -r requirements/test.txt, importlib-metadata
pytest-cov==2.11.1 # via -r requirements/test.txt
pytest==6.2.2 # via -r requirements/test.txt, pytest-cov
requests==2.25.1 # via -r requirements/test.txt
six==1.15.0 # via -r requirements/test.txt
toml==0.10.2 # via -r requirements/test.txt, pytest
urllib3==1.26.3 # via -r requirements/test.txt, requests
zipp==3.4.0 # via -r requirements/test.txt, importlib-metadata
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='xqueue_watcher',
version='0.2',
version='1.0',
description='XQueue Pull Grader',
packages=[
'xqueue_watcher',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_grader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import mock
from unittest import mock
import json
import sys
from path import Path
Expand Down
2 changes: 1 addition & 1 deletion tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
from path import Path
import json
from mock import Mock
from unittest.mock import Mock
import time
import sys

Expand Down
2 changes: 1 addition & 1 deletion tests/test_xqueue_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import mock
from unittest import mock
import json
import collections
import requests
Expand Down
10 changes: 5 additions & 5 deletions xqueue_watcher/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
log = logging.getLogger(__name__)


class XQueueClient(object):
class XQueueClient:
def __init__(self,
queue_name,
xqueue_server='http://localhost:18040',
Expand All @@ -20,7 +20,7 @@ def __init__(self,
poll_interval=MANAGER_CONFIG_DEFAULTS['POLL_INTERVAL'],
login_poll_interval=MANAGER_CONFIG_DEFAULTS['LOGIN_POLL_INTERVAL'],
follow_client_redirects=MANAGER_CONFIG_DEFAULTS['FOLLOW_CLIENT_REDIRECTS']):
super(XQueueClient, self).__init__()
super().__init__()
self.session = requests.session()
self.xqueue_server = xqueue_server
self.queue_name = queue_name
Expand All @@ -41,7 +41,7 @@ def __init__(self,
self.processing = False

def __repr__(self):
return '{}({})'.format(self.__class__.__name__, self.queue_name)
return f'{self.__class__.__name__}({self.queue_name})'

def _parse_response(self, response, is_reply=True):
if response.status_code not in [200]:
Expand Down Expand Up @@ -97,7 +97,7 @@ def _request(self, method, uri, **kwargs):
else:
return (False, "Could not log in")
else:
message = "Received un expected response status code, {0}, calling {1}.".format(
message = "Received un expected response status code, {}, calling {}.".format(
r.status_code,url)
log.error(message)
return (False, message)
Expand All @@ -106,7 +106,7 @@ def _login(self):
if self.username is None:
return True
url = self.xqueue_server + '/xqueue/login/'
log.debug("Trying to login to {0} with user: {1} and pass {2}".format(url, self.username, self.password))
log.debug(f"Trying to login to {url} with user: {self.username} and pass {self.password}")
response = self.session.request('post', url, auth=self.http_basic_auth, data={
'username': self.username,
'password': self.password,
Expand Down
Loading