def test_json_hash(): # consistent output assert json_hash({'a': 'z', 'b': 'y', 'c': 'x'}) == 'f6117d60' # second parameter is size of string val = json_hash(['abc'], 35) assert len(val) == 35 # even OrderedDicts have the keys reordered assert json_hash(OrderedDict([ ('c', 'x'), ('b', 'y'), ('a', 'z'), ])) == json_hash(OrderedDict([ ('a', 'z'), ('b', 'y'), ('c', 'x'), ])) # identical objects match (==) assert json_hash({'d': 1, 'e': 2, 'f': 3}) == json_hash({'d': 1, 'e': 2, 'f': 3}) # types don't match (1 != '1') assert json_hash({'d': 1, 'e': 2, 'f': 3}) != json_hash({'d': '1', 'e': '2', 'f': '3'})
def autoname_fields__depr(surv_content): ''' Note: this method is deprecated but kept around to link prior deployments which don't have any names saved. ''' surv_list = surv_content.get('survey') kuid_names = {} for surv_row in surv_list: if not _has_name(surv_row): if _is_group_end(surv_row): continue if 'label' in surv_row: next_name = sluggify_valid_xml__depr(surv_row['label']) elif surv_row.get('type') == 'group': next_name = sluggify_valid_xml__depr('Grp') else: next_name = 'unnamable_row_{}'.format(json_hash(surv_row)) while next_name in kuid_names.values(): next_name = _increment(next_name) if 'kuid' not in surv_row: surv_row['kuid'] = _rand_id(8) if surv_row['kuid'] in kuid_names: raise Exception("Duplicate kuid: %s" % surv_row['kuid']) surv_row['name'] = next_name kuid_names[surv_row['kuid']] = next_name # kuid is unused, and can't be compared with replacement method for surv_row in surv_list: if 'kuid' in surv_row: del surv_row['kuid'] return surv_list
def _populate_report_styles(self): default = self.report_styles.get(DEFAULT_REPORTS_KEY, {}) specifieds = self.report_styles.get(SPECIFIC_REPORTS_KEY, {}) kuids_to_variable_names = self.report_styles.get('kuid_names', {}) for (index, row) in enumerate(self.content.get('survey', [])): if '$kuid' not in row: if 'name' in row: row['$kuid'] = json_hash([self.uid, row['name']]) else: row['$kuid'] = json_hash([self.uid, index, row]) _identifier = row.get('name', row['$kuid']) kuids_to_variable_names[_identifier] = row['$kuid'] if _identifier not in specifieds: specifieds[_identifier] = {} self.report_styles = { DEFAULT_REPORTS_KEY: default, SPECIFIC_REPORTS_KEY: specifieds, 'kuid_names': kuids_to_variable_names, }