def _build_ordered_columns(cls, survey_element, ordered_columns, is_repeating_section=False): """ Build a flat ordered dict of column groups is_repeating_section ensures that child questions of repeating sections are not considered columns """ for child in survey_element.children: # child_xpath = child.get_abbreviated_xpath() if isinstance(child, Section): child_is_repeating = False if isinstance(child, RepeatingSection): ordered_columns[child.get_abbreviated_xpath()] = [] child_is_repeating = True cls._build_ordered_columns(child, ordered_columns, child_is_repeating) elif isinstance(child, Question) and not \ question_types_to_exclude(child.type) and not\ is_repeating_section: # if is_repeating_section, # its parent already initiliased an empty list # so we dont add it to our list of columns, # the repeating columns list will be # generated when we reindex ordered_columns[child.get_abbreviated_xpath()] = None
def get_ordered_repeat_value(key, item, index): """ Return Ordered Dict of repeats in the order in which they appear in the XForm. """ children = xform.get_child_elements(key, split_select_multiples=False) item_list = OrderedDict() data = {} for elem in children: if not question_types_to_exclude(elem.type): new_xpath = elem.get_abbreviated_xpath() item_list[new_xpath] = item.get(new_xpath, DEFAULT_NA_REP) # Loop through repeat data and flatten it # given the key "children/details" and nested_key/ # abbreviated xpath "children/details/immunization/polio_1", # generate ["children", index, "immunization/polio_1"] for (nested_key, nested_val) in item_list.items(): qstn_type = xform.get_element(nested_key).type xpaths = get_xpath(key, nested_key) if qstn_type == MULTIPLE_SELECT_TYPE: data = get_updated_data_dict(xpaths, nested_val, data) elif qstn_type == REPEAT_SELECT_TYPE: data = get_updated_data_dict(xpaths, nested_val, data) else: data[xpaths] = nested_val return data
def get_ordered_repeat_value(key, item, index): """ Return Ordered Dict of repeats in the order in which they appear in the XForm. """ index_tags = DEFAULT_INDEX_TAGS children = xform.get_child_elements(key, split_select_multiples=False) item_list = OrderedDict() data = {} for elem in children: if not question_types_to_exclude(elem.type): new_xpath = elem.get_abbreviated_xpath() item_list[new_xpath] = item.get(new_xpath, DEFAULT_NA_REP) # Loop through repeat data and flatten it # given the key "children/details" and nested_key/ # abbreviated xpath "children/details/immunization/polio_1", # generate ["children", index, "immunization/polio_1"] for (nested_key, nested_val) in item_list.items(): xpaths = [ '{key}{open_tag}{index}{close_tag}'.format( key=nested_key.split('/')[0], open_tag=index_tags[0], index=index, close_tag=index_tags[1])] + [ nested_key.split('/')[1]] xpaths = "/".join(xpaths) data[xpaths] = nested_val return data
def _add_sheets(self): for e in self._data_dictionary.get_survey_elements(): if isinstance(e, Section): sheet_name = e.name self.add_sheet(sheet_name) for f in e.children: if isinstance(f, Question) and\ not question_types_to_exclude(f.type): self.add_column(sheet_name, f.name)
def get_ordered_repeat_value(xpath, repeat_value): children = data_dictionary.get_child_elements( xpath, split_select_multiples) item = OrderedDict() for elem in children: if not question_types_to_exclude(elem.type): xp = elem.get_abbreviated_xpath() item[xp] = repeat_value.get(xp, DEFAULT_NA_REP) return item
def get_ordered_repeat_value(xpath, repeat_value): """ Return OrderedDict of repeats in the order in which they appear in the XForm. """ children = data_dictionary.get_child_elements( xpath, split_select_multiples) item = OrderedDict() for elem in children: if not question_types_to_exclude(elem.type): xp = elem.get_abbreviated_xpath() item[xp] = repeat_value.get(xp, DEFAULT_NA_REP) return item