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

Remove Python 2 support and modernize required libraries. #248

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ eval*
.pytest_cache
.tox
docs_api
.idea
venv
Binary file removed dump.txt.gz
Binary file not shown.
2 changes: 0 additions & 2 deletions examples/advanced.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from koala.ExcelCompiler import ExcelCompiler
from koala.Spreadsheet import Spreadsheet
from koala.excellib import xsum
Expand Down
2 changes: 0 additions & 2 deletions examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from koala.ExcelCompiler import ExcelCompiler
from koala.Spreadsheet import Spreadsheet

Expand Down
10 changes: 1 addition & 9 deletions koala/Cell.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# cython: profile=True

from __future__ import absolute_import, division

from koala.CellBase import CellBase
from koala.Range import RangeCore
from koala.utils import *

from openpyxl.compat import unicode


class Cell(CellBase):
ctr = 0
Expand Down Expand Up @@ -62,11 +58,7 @@ def __init__(
self.__row = None
self.__col_idx = None

# `unicode` != `str` in Python2. See `from openpyxl.compat import unicode`
if type(formula) == str and str != unicode:
self.__formula = unicode(formula, 'utf-8') if formula else None
else:
self.__formula = formula if formula else None
self.__formula = formula if formula else None

self.__value = value
self.python_expression = None
Expand Down
10 changes: 0 additions & 10 deletions koala/ExcelCompiler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
from __future__ import print_function
# cython: profile=True

import os.path

import networkx

from koala.reader import read_archive, read_named_ranges, read_cells
from koala.utils import *
from koala.ast import graph_from_seeds, shunting_yard, build_ast, prepare_pointer
from koala.Cell import Cell
from koala.Range import RangeFactory
from koala.Spreadsheet import Spreadsheet
import warnings

Expand Down
17 changes: 6 additions & 11 deletions koala/Range.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
from __future__ import absolute_import, division, print_function

from koala.CellBase import CellBase
from koala.ExcelError import ErrorCodes, ExcelError
from koala.utils import *

from openpyxl.compat import unicode


# WARNING: Range should never be imported directly. Import Range from excelutils instead.

### Range Utils ###
Expand Down Expand Up @@ -58,7 +53,7 @@ def check_value(a):
return ExcelError(a)

try: # This is to avoid None or Exception returned by Range operations
if isinstance(a, (unicode, str)):
if isinstance(a, str):
return a
elif float(a):
return a
Expand Down Expand Up @@ -570,7 +565,7 @@ def multiply(a, b):
@staticmethod
def divide(a, b):
try:
return old_div(float(check_value(a)), float(check_value(b)))
return float(check_value(a)) / float(check_value(b))
except Exception as e:
return ExcelError('#DIV/0!', e)

Expand All @@ -584,9 +579,9 @@ def power(a, b):
@staticmethod
def is_equal(a, b):
try:
if not isinstance(a, (str, unicode)):
if not isinstance(a, str):
a = check_value(a)
if not isinstance(b, (str, unicode)):
if not isinstance(b, str):
b = check_value(b)

return is_almost_equal(a, b, precision=0.00001)
Expand All @@ -596,9 +591,9 @@ def is_equal(a, b):
@staticmethod
def is_not_equal(a, b):
try:
if not isinstance(a, (str, unicode)):
if not isinstance(a, str):
a = check_value(a)
if not isinstance(a, (str, unicode)):
if not isinstance(a, str):
b = check_value(b)

return a != b
Expand Down
8 changes: 2 additions & 6 deletions koala/Spreadsheet.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import absolute_import, print_function
# cython: profile=True

from koala.Range import get_cell_address, parse_cell_address
Expand All @@ -17,9 +16,6 @@
import networkx
from networkx.readwrite import json_graph

from openpyxl.compat import unicode


class Spreadsheet(object):
def __init__(self, file=None, ignore_sheets=[], ignore_hidden=False, debug=False):
# print("___### Initializing Excel Compiler ###___")
Expand Down Expand Up @@ -56,7 +52,7 @@ def __init__(self, file=None, ignore_sheets=[], ignore_hidden=False, debug=False
else: # assume file path
archive = read_archive(os.path.abspath(file))
# Parse cells
self.cells = read_cells(archive, ignore_sheets, ignore_hidden)
self.cells, self.sheets = read_cells(archive, ignore_sheets, ignore_hidden)
# Parse named_range { name (ExampleName) -> address (Sheet!A1:A10)}
self.named_ranges = read_named_ranges(archive)
self.range = RangeFactory(self.cells)
Expand Down Expand Up @@ -306,7 +302,7 @@ def cell_set_formula(self, address, formula):
for index, c in enumerate(cell.range.cells): # for each cell of the range, translate the formula
if index == 0:
c.formula = formula
translator = Translator(unicode('=' + formula), c.address().split('!')[1]) # the Translator needs a reference without sheet
translator = Translator('=' + formula, c.address().split('!')[1]) # the Translator needs a reference without sheet
else:
translated = translator.translate_formula(c.address().split('!')[1]) # the Translator needs a reference without sheet
c.formula = translated[1:] # to get rid of the '='
Expand Down
2 changes: 0 additions & 2 deletions koala/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import absolute_import

from openpyxl import *
from .ast import *
from .Cell import *
Expand Down
15 changes: 5 additions & 10 deletions koala/ast/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import absolute_import
# cython: profile=True

import collections
import six

import networkx
from networkx.classes.digraph import DiGraph
from openpyxl.compat import unicode

from koala.utils import uniqueify, flatten, max_dimension, col2num, resolve_range
from koala.Cell import Cell
Expand Down Expand Up @@ -118,7 +115,7 @@ def shunting_yard(expression, named_ranges, ref = None, tokenize_range = False):
for index, token in enumerate(tokens):
new_tokens.append(token)

if type(token.tvalue) == str or type(token.tvalue) == unicode:
if type(token.tvalue) == str:

if token.tvalue.startswith(':'): # example -> :OFFSET( or simply :A10
depth = 0
Expand All @@ -139,7 +136,7 @@ def shunting_yard(expression, named_ranges, ref = None, tokenize_range = False):
if depth == 0:
new_tokens.pop() # these 2 lines are needed to remove INDEX()
new_tokens.pop()
expr = six.next(rev).tvalue + expr
expr = next(rev).tvalue + expr
break

expr += token.tvalue
Expand Down Expand Up @@ -377,13 +374,11 @@ def cell2code(cell, named_ranges):

else:
ast = None
if isinstance(cell.value, unicode):
code = u'u"' + cell.value.replace(u'"', u'\\"') + u'"'
elif isinstance(cell.value, str):
raise RuntimeError("Got unexpected non-unicode str")
if isinstance(cell.value, str):
code = '"' + cell.value.replace('"', r'\"') + '"'
else:
code = str(cell.value)
return code,ast
return code, ast


def prepare_pointer(code, names, ref_cell = None):
Expand Down
14 changes: 5 additions & 9 deletions koala/ast/astnodes.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
from __future__ import print_function
# cython: profile=True

from networkx import NetworkXError

from openpyxl.compat import unicode

from koala.excellib import FUNCTION_MAP, IND_FUN
from koala.utils import is_range, split_range, split_address, resolve_range
from koala.ExcelError import *


def to_str(my_string):
# `unicode` != `str` in Python2. See `from openpyxl.compat import unicode`
if type(my_string) == str and str != unicode:
return unicode(my_string, 'utf-8')
elif type(my_string) == unicode:
if isinstance(my_string, bytes):
return my_string.decode("utf-8")
elif isinstance(my_string, str):
return my_string
else:
try:
return str(my_string)
except:
except Exception:
print('Couldnt parse as string', type(my_string))
return my_string
# elif isinstance(my_string, (int, float, tuple, Ra):
Expand All @@ -45,7 +41,7 @@ def __getattr__(self, name):
def children(self, ast):
try:
args = ast.predecessors(self)
args = sorted(args, key=lambda x: ast.node[x]['pos'])
args = sorted(args, key=lambda x: ast.nodes[x]['pos'])
except NetworkXError:
args = ''
return args
Expand Down
9 changes: 3 additions & 6 deletions koala/excellib.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

# source: https://github.com/dgorissen/pycel/blob/master/src/pycel/excellib.py

from __future__ import absolute_import, division

import itertools
import numpy as np
import numpy_financial as npf
import scipy.optimize
import datetime
import random
Expand All @@ -18,8 +17,6 @@
from calendar import monthrange
from dateutil.relativedelta import relativedelta

from openpyxl.compat import unicode

from koala.utils import *
from koala.Range import RangeCore as Range
from koala.ExcelError import *
Expand Down Expand Up @@ -427,7 +424,7 @@ def irr(values, guess = None):
raise ValueError('guess value for excellib.irr() is %s and not 0' % guess)
else:
try:
return np.irr(values)
return npf.irr(values)
except Exception as e:
return ExcelError('#NUM!', e)

Expand Down Expand Up @@ -729,7 +726,7 @@ def randbetween(bottom, top):

def right(text,n):
#TODO: hack to deal with naca section numbers
if isinstance(text, unicode) or isinstance(text,str):
if isinstance(text, str):
return text[-n:]
else:
# TODO: get rid of the decimal
Expand Down
9 changes: 6 additions & 3 deletions koala/reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import print_function

from io import BytesIO
import re
import os
Expand Down Expand Up @@ -108,6 +106,8 @@ def read_cells(archive, ignore_sheets = [], ignore_hidden = False):

cells = {}

sheets = []

functions = set()

cts = dict(read_content_types(archive))
Expand All @@ -127,6 +127,9 @@ def read_cells(archive, ignore_sheets = [], ignore_hidden = False):

if sheet_name in ignore_sheets: continue

if sheet_name not in sheets:
sheets.append(sheet_name)

root = fromstring(archive.read(sheet['path'])) # it is necessary to use cElementTree from xml module, otherwise root.findall doesn't work as it should

hidden_cols = False
Expand Down Expand Up @@ -225,7 +228,7 @@ def read_cells(archive, ignore_sheets = [], ignore_hidden = False):
# if f not in existing:
# print('== Missing function: %s' % f)

return cells
return cells, sheets


def read_rels(archive):
Expand Down
40 changes: 5 additions & 35 deletions koala/serializer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from __future__ import absolute_import, print_function

import json
import gzip
import networkx

from networkx.classes.digraph import DiGraph
from networkx.readwrite import json_graph
from networkx.drawing.nx_pydot import write_dot
from openpyxl.compat import unicode

from koala.Cell import Cell
from koala.Range import RangeCore, RangeFactory
Expand Down Expand Up @@ -51,10 +47,10 @@ def parse_cell_info(cell):
for cell in simple_cells:
parse_cell_info(cell)
value = cell.value
if isinstance(value, unicode):
if isinstance(value, str):
outfile.write(cell.value.encode('utf-8') + b"\n")
else:
outfile.write((str(cell.value) + u"\n").encode('utf-8'))
outfile.write((str(cell.value) + "\n").encode('utf-8'))
outfile.write(b"====" + b"\n")

outfile.write(b"-----" + b"\n")
Expand Down Expand Up @@ -205,41 +201,15 @@ def dump_json(self, fname):

def load_json(fname):

def _decode_list(data):
rv = []
for item in data:
if isinstance(item, unicode) and unicode != str:
item = item.encode('utf-8')
elif isinstance(item, list) and unicode != str:
item = _decode_list(item)
elif isinstance(item, dict):
item = _decode_dict(item)
rv.append(item)
return rv

def _decode_dict(data):
rv = {}
for key, value in data.items():
if isinstance(key, unicode) and unicode != str:
key = key.encode('utf-8')
if isinstance(value, unicode) and unicode != str:
value = value.encode('utf-8')
elif isinstance(value, list):
value = _decode_list(value)
elif isinstance(value, dict):
value = _decode_dict(value)
rv[key] = value
return rv

with gzip.GzipFile(fname, 'r') as infile:
data = json.loads(infile.read().decode('utf-8'), object_hook=_decode_dict)
data = json.loads(infile.read().decode('utf-8'))

return data


########### based on dot #################
def export_to_dot(self,fname):
write_dot(self.G,fname)
def export_to_dot(self, fname):
write_dot(self.G, fname)


########### plotting #################
Expand Down
Loading