Skip to content

Commit

Permalink
add support for ItemRanges
Browse files Browse the repository at this point in the history
fix some of the indexing in the functions
  • Loading branch information
devowit committed May 5, 2020
1 parent 693737e commit c1bb209
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
78 changes: 53 additions & 25 deletions backend_code/parsing/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,57 @@ def __repr__(self):
return to_excel(self.col, self.row)+ " : "+str(self.value)


class RangeClass:
@property
def flattened(self):
for row in self.data:
for col in row:
yield col

def __eq__(self, comparator):
for i in self.flattened:
if i != comparator:
return False
return True

def __iter__(self):
for i in self.flattened:
yield i

def __getitem__(self, flat_index):
row=flat_index//self.row_length
col=flat_index%self.row_length
return self.area[row][col]



class Item(ReturnClass):
def __init__(self, col, row, context):
super().__init__(col, row)
item_table=bindings.item_table
self.value=item_table.get_item(self.col, self.row, context)

class ItemRange(RangeClass):
def __init__(self, col, row, context):
if isinstance(col, slice):
cols=[i for i in range(col.start, col.stop)]
else:
cols=[col]
if isinstance(row, slice):
rows=[i for i in range(row.start, row.stop)]
else:
rows=[row]

self.data=[]
for r in rows:
row_arr=[]
for c in cols:
row_arr.append(Item(c, r, context))
self.data.append(row_arr)

def __setitem__(self, flat_index, data):
raise ValueError("Should not be changing the value of Items")


class ItemExpression:
def __getitem__(self, args):
Expand All @@ -51,7 +96,9 @@ def __getitem__(self, args):
context='__NO_CONTEXT__'
col=index_converter(args[0])
row=index_converter(args[1])
return Item(col, row, context)
if isinstance(col, int) and isinstance(row, int):
return Item(col, row, context)
return ItemRange(col, row, context)

class Cell(ReturnClass):
def __init__(self, col, row):
Expand All @@ -60,7 +107,7 @@ def __init__(self, col, row):
self.value=data_sheet[row][col]


class CellRange:
class CellRange(RangeClass):
def __init__(self, col_args, row_args):
data_sheet=bindings.excel_sheet
self.col_args = col_args
Expand All @@ -74,37 +121,18 @@ def __init__(self, col_args, row_args):
new_row=row[col_args]
area.append(new_row)

self.area = area
self.data = area
self.row_length=len(self.area[0])

@property
def flattened_area(self):
for row in self.area:
for col in row:
yield col

def __eq__(self, comparator):
for i in self.flattened_area:
if i != comparator:
return False
return True


def __repr__(self):
return str(self.area)

def __iter__(self):
for i in self.flattened_area:
yield i


def __setitem__(self, flat_index, data):
row=flat_index//self.row_length
col=flat_index%self.row_length
self.area[row][col]=data


def __getitem__(self, flat_index):
row=flat_index//self.row_length
col=flat_index%self.row_length
return self.area[row][col]


class CellExpression:
Expand Down
21 changes: 15 additions & 6 deletions backend_code/parsing/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from etk.wikidata.utils import parse_datetime_string
from SPARQLWrapper import SPARQLWrapper, JSON
from backend_code.bindings import bindings
from backend_code.parsing.classes import ReturnClass, CellRange
from backend_code.parsing.classes import ReturnClass, RangeClass

def boolean_modifer(func):
def wrapper(input, *args, **kwargs):
if input: #if value is not None
if isinstance(input, CellRange): #handle ranges separately:
if isinstance(input, RangeClass): #handle ranges separately:
for i, val in enumerate(input):
if val:
flag=func(input[i], *args, **kwargs)
Expand Down Expand Up @@ -44,7 +44,7 @@ def instance_of_qnode(input, qnode):
def string_modifier(func):
def wrapper(input, *args, **kwargs):
if input: #if value is None, don't modify
if isinstance(input, CellRange): #handle ranges separately:
if isinstance(input, RangeClass): #handle ranges separately:
for i, val in enumerate(input):
if val:
input[i]=func(str(input[i]), *args, **kwargs)
Expand Down Expand Up @@ -90,7 +90,15 @@ def split_index(input, character, i):
def substring(input, start, end=None):
#1-based indexing
#substring("platypus", 3, 5) would be "aty" and substring(pirate, 2, -2) would return "irat"
return str(input)[start-1:end] #adjust start to 0-indexing

#adjust to 0-indexing
if start<0:
start+=1
else:
start-=1
if end<0:
end+=1
return str(input)[start-1:end]

@string_modifier
def extract_date(input, date_format):
Expand All @@ -99,13 +107,14 @@ def extract_date(input, date_format):
return date_str

@string_modifier
def regex(input, pattern, i=0):
def regex(input, pattern, i=1):
# extract a substring using a regex. The string is the regex and the result is the value of the first group in
# the regex. If the regex contains no group, it is the match of the regex.
#regex(value[], "regex") returns the first string that matches the whole regex
#regex(value[]. regex, i) returns the value of group i in the regex
#The reason for the group is that it allows more complex expressions. In our use case we could do a single expression as we cannot fetch more than one
matches=[x.group() for x in re.finditer(pattern, input)]
i=i-1 #1 indexing
try:
if matches[i]:
return matches[i]
Expand All @@ -123,7 +132,7 @@ def concat(*args):
args=args[:-1]
return_str=""
for arg in args:
if isinstance(arg, CellRange):
if isinstance(arg, RangeClass):
for thing in arg:
return_str+=str(thing)
return_str+=sep
Expand Down

0 comments on commit c1bb209

Please sign in to comment.