# -*- coding: utf_8 -*-
[docs]
class CorrData:
""" Class to manipulate the CorrDB-class'es data dict structure.
"""
def __init__(self):
""" Instantiates the CorrData class """
# init
self.errormsg=""
[docs]
def rangeList(self,rfrom,rto,asStr=False) -> list:
""" Create a list of values in a given range, either as integers or strings.
Args:
rfrom (int): Start of range to create list values of.
rto (int): End of range to create list values of. Values will be created including this value.
asStr (bool): Defines if returned list will have string type values or not. Optional. Defaults
to False and values will be integers.
Returns:
list: List of values that are in the range requested by input parameters.
"""
if asStr:
return [str(i) for i in range(rfrom,rto+1)]
else:
return [i for i in range(rfrom,rto+1)]
[docs]
def getFieldUniqueValues(self,data,field,omitblanks=True,encode=False) -> list:
""" Get all unique values for a given data field.
Args:
data (dict): CorrDB dict-structure to search for unique values.
field (str): Which field to search for unique values in the CorrDB dict-structure.
omitblanks (bool): Omit field values that are blank and do not add them as unique. Optional.
Defaults to True. If set to False, the blank value will be returned as
unique value: "***N/A (Other)***".
Returns:
list: List of unique values found in the CorrDB data structure.
"""
uvalues={}
for rowno in range(1,len(data.keys())+1):
# find unique values and add to uvalues
# can only exist once in dict, so multiple adds is ok
# it is quicker than checking if values already there
if field in data[rowno]["cols"]:
# check if blanks are to be omitted
value=str(data[rowno]["cols"][field]["value"]).strip()
if encode: value=str(value).encode("utf-8")
if value == "" and omitblanks:
continue
elif value == "": value="***N/A (Other)***"
# this value is to be added
uvalues[value]=1
# return dict keys as list - they are the unique values
return list(uvalues.keys())
[docs]
def getFieldCount(self,data={},field="dummy",values=[],omitblanks=True,ignorecase=False,dependent="",depvalue=[]) -> int:
""" Get number of occurences of a given value in a given field of the CorrDB data-structure.
Args:
data (dict): The CorrDB data-structure to go through and count occurences.
field (str): Name of the field to search for the given value.
values (list): String or list of value(s) to search for and count.
omitblanks (bool): Specifies if the search for values are to omit blanks. Optional.
Defaults to True. If omitblanks is False it will search for
the value "***N/A (Other)***" (see the getFieldUniqueValues()-method)
ignorecase (bool): Specifies if the search is to ignore case. Optional. Default to False.
dependent (str): Specifies if the counting is dependent upon given field has a certain
value or not. Optional. Default to blank, which means False. If non-blank
it specified the field that holds the value(s) that the search are dependent upon.
depvalue (list): String or list of value(s) that must be matched (one instance of) before
counting is performed on a given row. Optional. Defaults to empty list.
Returns:
int: The number of matches of the given conditions set.
"""
count=0
# go through each row and count the number of occurrences of
# given field
for rowno in range(1,len(data.keys())+1):
# check if counting is dependent upon a field and value
if str(dependent).strip() != "":
# we are dependent upon a field and value in the row to do the counting
# get dependent value
dval=depvalue
# we must check and potentially uppercase a string different than a list
if type(dval) == list:
dval=dval if not ignorecase else [str(v).upper().strip() for v in dval]
else: dval=str(dval).strip() if not ignorecase else str(dval).upper().strip()
# get the value from the row
value=data[rowno]["cols"][dependent]["value"] if dependent in data[rowno]["cols"] else ""
# uppercase value if needed
value=str(value) if not ignorecase else str(value).upper()
# check if dependent requirement is fulfilled or not?
# if depdenent value not in row, skip it
if value not in dval: continue
# check if the given field we are searching for has a
# value that matches anything in the value array
value=data[rowno]["cols"][field]["value"]
if str(value).strip == "" and not omitblanks:
value="***N/A (Other)***"
# check if we are to do case-insensitive matching or not?
cmpvalue=str(value) if not ignorecase else str(value).upper()
# we have to be careful to convert to uppercase another way with a list
# than with a single value
if type(values) == list:
cmpvalues=values if not ignorecase else [str(v).upper() for v in values]
else: cmpvalues=values if not ignorecase else str(values).upper()
# check if value matches or not
if str(value).strip() != "" and cmpvalue in cmpvalues:
count=count+1
# return the count
return count
[docs]
def getRowCount(self,data={}) -> int:
""" Returns the number of rows in a CorrDB data dict-structure
Args:
data (dict): The CorrDB data dict-structure to count rows of.
Returns:
int: Returns the number of rows in a CorrDB data dict-structure
"""
return len(data.keys())