From 0491b3a14fc4e87ab6d8101b1680252d27a4ecc9 Mon Sep 17 00:00:00 2001 From: Amit <43564590+amitvadhel@users.noreply.github.com> Date: Fri, 10 May 2019 16:00:44 +0300 Subject: [PATCH] INCR-170: Run python-modernize on common/lib [safe_lxml, sandbox-packages] (#20508) --- common/lib/safe_lxml/safe_lxml/__init__.py | 1 + common/lib/safe_lxml/safe_lxml/etree.py | 2 ++ common/lib/safe_lxml/safe_lxml/tests.py | 2 ++ common/lib/safe_lxml/setup.py | 1 + common/lib/sandbox-packages/eia.py | 2 +- .../sandbox-packages/loncapa/loncapa_check.py | 3 ++- common/lib/sandbox-packages/setup.py | 2 ++ .../sandbox-packages/verifiers/draganddrop.py | 21 +++++++++++-------- .../verifiers/tests_draganddrop.py | 4 +++- 9 files changed, 26 insertions(+), 12 deletions(-) diff --git a/common/lib/safe_lxml/safe_lxml/__init__.py b/common/lib/safe_lxml/safe_lxml/__init__.py index b17efb15cc..7623398ec1 100644 --- a/common/lib/safe_lxml/safe_lxml/__init__.py +++ b/common/lib/safe_lxml/safe_lxml/__init__.py @@ -1,6 +1,7 @@ """ Defuse vulnerabilities in XML packages. """ +from __future__ import absolute_import def defuse_xml_libs(): diff --git a/common/lib/safe_lxml/safe_lxml/etree.py b/common/lib/safe_lxml/safe_lxml/etree.py index 4fbb6b9473..94bdb1e518 100644 --- a/common/lib/safe_lxml/safe_lxml/etree.py +++ b/common/lib/safe_lxml/safe_lxml/etree.py @@ -11,6 +11,8 @@ For processing xml always prefer this over using lxml.etree directly. # lxml.etree. The names are not used here, so disable the pylint warning. # pylint: disable=unused-import, wildcard-import, unused-wildcard-import +from __future__ import absolute_import + from lxml.etree import XMLParser as _XMLParser from lxml.etree import * from lxml.etree import _Element, _ElementTree diff --git a/common/lib/safe_lxml/safe_lxml/tests.py b/common/lib/safe_lxml/safe_lxml/tests.py index 1a7fbb6e42..99b574aa87 100644 --- a/common/lib/safe_lxml/safe_lxml/tests.py +++ b/common/lib/safe_lxml/safe_lxml/tests.py @@ -1,5 +1,7 @@ """Test that we have defused XML.""" +from __future__ import absolute_import + import defusedxml from lxml import etree diff --git a/common/lib/safe_lxml/setup.py b/common/lib/safe_lxml/setup.py index 8b01267b75..ac1b880190 100644 --- a/common/lib/safe_lxml/setup.py +++ b/common/lib/safe_lxml/setup.py @@ -1,6 +1,7 @@ """ Setup.py for safe_lxml. """ +from __future__ import absolute_import from setuptools import setup diff --git a/common/lib/sandbox-packages/eia.py b/common/lib/sandbox-packages/eia.py index 45f6ea32a5..766960156c 100644 --- a/common/lib/sandbox-packages/eia.py +++ b/common/lib/sandbox-packages/eia.py @@ -7,7 +7,7 @@ values in parameterized problems. For details, see: http://en.wikipedia.org/wiki/Electronic_color_code """ -from __future__ import print_function +from __future__ import absolute_import, print_function # pylint: disable=invalid-name # r is standard name for a resistor. We would like to use it as such. diff --git a/common/lib/sandbox-packages/loncapa/loncapa_check.py b/common/lib/sandbox-packages/loncapa/loncapa_check.py index 7bde74e3eb..1fbc96301d 100644 --- a/common/lib/sandbox-packages/loncapa/loncapa_check.py +++ b/common/lib/sandbox-packages/loncapa/loncapa_check.py @@ -5,10 +5,11 @@ # Python functions which duplicate the standard comparison functions available to LON-CAPA problems. # Used in translating LON-CAPA problems to i4x problem specification language. -from __future__ import division +from __future__ import absolute_import, division import math import random +from six.moves import range def lc_random(lower, upper, stepsize): diff --git a/common/lib/sandbox-packages/setup.py b/common/lib/sandbox-packages/setup.py index c435114987..66232d606c 100644 --- a/common/lib/sandbox-packages/setup.py +++ b/common/lib/sandbox-packages/setup.py @@ -1,3 +1,5 @@ +from __future__ import absolute_import + from setuptools import setup setup( diff --git a/common/lib/sandbox-packages/verifiers/draganddrop.py b/common/lib/sandbox-packages/verifiers/draganddrop.py index bce588ac42..fe6a15e70d 100644 --- a/common/lib/sandbox-packages/verifiers/draganddrop.py +++ b/common/lib/sandbox-packages/verifiers/draganddrop.py @@ -23,8 +23,11 @@ or: } values are (x, y) coordinates of centers of dragged images. """ +from __future__ import absolute_import import json +import six +from six.moves import zip def flat_user_answer(user_answer): @@ -39,8 +42,8 @@ def flat_user_answer(user_answer): """ def parse_user_answer(answer): - key = answer.keys()[0] - value = answer.values()[0] + key = list(answer.keys())[0] + value = list(answer.values())[0] if isinstance(value, dict): # Make complex value: @@ -49,8 +52,8 @@ def flat_user_answer(user_answer): complex_value_list = [] v_value = value while isinstance(v_value, dict): - v_key = v_value.keys()[0] - v_value = v_value.values()[0] + v_key = list(v_value.keys())[0] + v_value = list(v_value.values())[0] complex_value_list.append(v_key) complex_value = '{0}'.format(v_value) @@ -101,8 +104,8 @@ class PositionsCompare(list): isinstance(other[0], (list, int, float))): return self.coordinate_positions_compare(other) - elif (isinstance(self[0], (unicode, str)) and - isinstance(other[0], (unicode, str))): + elif (isinstance(self[0], (six.text_type, str)) and + isinstance(other[0], (six.text_type, str))): return ''.join(self) == ''.join(other) else: # improper argument types: no (float / int or lists of list #and float / int pair) or two string / unicode lists pair @@ -165,7 +168,7 @@ class DragAndDrop(object): # {'1': [u'2', u'2', u'2'], '0': [u'1', u'1'], '2': [u'3']} # if '+number' is in rule - do not remove duplicates and strip # '+number' from rule - current_rule = self.correct_positions[index].keys()[0] + current_rule = list(self.correct_positions[index].keys())[0] if 'number' in current_rule: rule_values = self.correct_positions[index][current_rule] # clean rule, do not do clean duplicate items @@ -353,7 +356,7 @@ class DragAndDrop(object): # correct_answer entry, the value is False. # default to consider every user answer excess until proven otherwise. self.excess_draggables = dict( - (users_draggable.keys()[0], True) + (list(users_draggable.keys())[0], True) for users_draggable in user_answer ) @@ -366,7 +369,7 @@ class DragAndDrop(object): user_positions_data = [] for draggable_dict in user_answer: # Draggable_dict is 1-to-1 {draggable_name: position}. - draggable_name = draggable_dict.keys()[0] + draggable_name = list(draggable_dict.keys())[0] if draggable_name in answer['draggables']: user_groups_data.append(draggable_name) user_positions_data.append( diff --git a/common/lib/sandbox-packages/verifiers/tests_draganddrop.py b/common/lib/sandbox-packages/verifiers/tests_draganddrop.py index 79abe37fc2..d4107b5cad 100644 --- a/common/lib/sandbox-packages/verifiers/tests_draganddrop.py +++ b/common/lib/sandbox-packages/verifiers/tests_draganddrop.py @@ -1,7 +1,9 @@ +from __future__ import absolute_import + import json import unittest -import draganddrop +from . import draganddrop from .draganddrop import PositionsCompare