Merge pull request #19628 from cclauss/modernize-common-lib-chem

INCR-29: Run python-modernize on common/lib/chem
This commit is contained in:
Jeremy Bowman
2019-02-20 08:21:28 -05:00
committed by GitHub
5 changed files with 22 additions and 13 deletions

View File

@@ -1,11 +1,14 @@
from __future__ import division
from __future__ import absolute_import, division
from fractions import Fraction
import markupsafe
import nltk
from nltk.tree import Tree
from pyparsing import Literal, OneOrMore, ParseException, StringEnd
from six.moves import map
from six.moves import range
from six.moves import zip
from functools import reduce
ARROWS = ('<->', '->')
@@ -22,10 +25,10 @@ elements = ['Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be',
'Ru', 'S', 'Sb', 'Sc', 'Se', 'Sg', 'Si', 'Sm', 'Sn', 'Sr', 'Ta',
'Tb', 'Tc', 'Te', 'Th', 'Ti', 'Tl', 'Tm', 'U', 'Uuo', 'Uup',
'Uus', 'Uut', 'V', 'W', 'Xe', 'Y', 'Yb', 'Zn', 'Zr']
digits = map(str, range(10))
digits = list(map(str, list(range(10))))
symbols = list("[](){}^+-/")
phases = ["(s)", "(l)", "(g)", "(aq)"]
tokens = reduce(lambda a, b: a ^ b, map(Literal, elements + digits + symbols + phases))
tokens = reduce(lambda a, b: a ^ b, list(map(Literal, elements + digits + symbols + phases)))
tokenizer = OneOrMore(tokens) + StringEnd()
# HTML, Text are temporarily copied from openedx.core.djangolib.markup
@@ -263,7 +266,7 @@ def _get_final_tree(s):
try:
tokenized = tokenizer.parseString(s)
parsed = parser.parse(tokenized)
merged = _merge_children(parsed.next(), {'S', 'group'})
merged = _merge_children(next(parsed), {'S', 'group'})
final = _clean_parse_tree(merged)
return final
except StopIteration:
@@ -354,11 +357,11 @@ def divide_chemical_expression(s1, s2, ignore_state=False):
# order of factors and phases must mirror the order of multimolecules,
# use 'decorate, sort, undecorate' pattern
treedic['1 cleaned_mm_list'], treedic['1 factors'], treedic['1 phases'] = zip(
*sorted(zip(treedic['1 cleaned_mm_list'], treedic['1 factors'], treedic['1 phases'])))
treedic['1 cleaned_mm_list'], treedic['1 factors'], treedic['1 phases'] = list(zip(
*sorted(zip(treedic['1 cleaned_mm_list'], treedic['1 factors'], treedic['1 phases']))))
treedic['2 cleaned_mm_list'], treedic['2 factors'], treedic['2 phases'] = zip(
*sorted(zip(treedic['2 cleaned_mm_list'], treedic['2 factors'], treedic['2 phases'])))
treedic['2 cleaned_mm_list'], treedic['2 factors'], treedic['2 phases'] = list(zip(
*sorted(zip(treedic['2 cleaned_mm_list'], treedic['2 factors'], treedic['2 phases']))))
# check if expressions are correct without factors
if not _check_equality(treedic['1 cleaned_mm_list'], treedic['2 cleaned_mm_list']):

View File

@@ -3,6 +3,7 @@
Also, may be this module is the place for other chemistry-related grade functions. TODO: discuss it.
"""
from __future__ import absolute_import
import itertools
import json
import unittest

View File

@@ -1,11 +1,15 @@
""" Calculation of Miller indices """
from __future__ import absolute_import
import decimal
import fractions as fr
import json
import math
import numpy as np
from six.moves import map
from six.moves import range
from functools import reduce
def lcm(a, b):
@@ -101,7 +105,7 @@ def sub_miller(segments):
fract.numerator * math.fabs(common_denominator) / fract.denominator
for fract in fracts
])
return'(' + ','.join(map(str, map(decimal.Decimal, miller_indices))) + ')'
return'(' + ','.join(map(str, list(map(decimal.Decimal, miller_indices)))) + ')'
def miller(points):
@@ -147,7 +151,7 @@ def miller(points):
N = np.cross(points[1] - points[0], points[2] - points[0])
O = np.array([0, 0, 0])
P = points[0] # point of plane
Ccs = map(np.array, [[1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]])
Ccs = list(map(np.array, [[1.0, 0, 0], [0, 1.0, 0], [0, 0, 1.0]]))
segments = ([
np.dot(P - O, N) / np.dot(ort, N) if np.dot(ort, N) != 0
else np.nan for ort in Ccs
@@ -256,7 +260,7 @@ def grade(user_input, correct_answer):
if user_answer['lattice'] != correct_answer['lattice']:
return False
points = [map(float, p) for p in user_answer['points']]
points = [list(map(float, p)) for p in user_answer['points']]
if len(points) < 3:
return False

View File

@@ -1,4 +1,4 @@
from __future__ import print_function
from __future__ import absolute_import, print_function
import codecs
import unittest
from fractions import Fraction

View File

@@ -1,3 +1,4 @@
from __future__ import absolute_import
from setuptools import setup
setup(