def test_stackoverflow_question_58827592(self): """ https://stackoverflow.com/questions/58827592/is-there-a-way-to-convert-csv-columns-into-hierarchical-relationships """ from benedict import benedict as bdict data_source = """ RecordID,kingdom,phylum,class,order,family,genus,species 1,Animalia,Chordata,Mammalia,Primates,Hominidae,H**o,H**o sapiens 2,Animalia,Chordata,Mammalia,Carnivora,Canidae,Canis,Canis 3,Plantae,nan,Magnoliopsida,Brassicales,Brassicaceae,Arabidopsis,Arabidopsis thaliana 4,Plantae,nan,Magnoliopsida,Fabales,Fabaceae,Phaseoulus,Phaseolus vulgaris """ data_input = bdict.from_csv(data_source) data_output = bdict() ancestors_hierarchy = [ 'kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species' ] for value in data_input['values']: data_output['.'.join([ value[ancestor] for ancestor in ancestors_hierarchy ])] = bdict() # print(data_output.dump()) keypaths = sorted(data_output.keypaths(), key=lambda item: len(item.split('.')), reverse=True) data_output['children'] = [] def transform_data(d, key, value): if isinstance(value, dict): value.update({'name': key, 'children': []}) data_output.traverse(transform_data) for keypath in keypaths: target_keypath = '.'.join(keypath.split('.')[:-1] + ['children']) data_output[target_keypath].append(data_output.pop(keypath))
def test_stackoverflow_question_60066331(self): """ https://stackoverflow.com/questions/60066331/find-elements-in-python-dict """ from benedict import benedict as bdict d = bdict({ 'ResponseMetadata': {'NOT IMPORTANT'}, 'hasMoreResults': True, 'marker': '{"NOT IMPORTANT"}', 'pipelineIdList': [{ 'id': 'df-0001', 'name': 'Blue' }, { 'id': 'df-0002', 'name': 'Orange' }, { 'id': 'df-0003', 'name': 'Green' }, { 'id': 'df-0004', 'name': 'Red' }, { 'id': 'df-0005', 'name': 'Purple' }] }) results = d.search('red', in_keys=False, in_values=True, exact=True, case_sensitive=False) # for item, key, value in results: # print(item) # {'id': 'df-0004', 'name': 'Red'} # print(key) # 'name' # print(value) # 'Red' self.assertEqual(len(results), 1) self.assertEqual(results[0], ( { 'id': 'df-0004', 'name': 'Red' }, 'name', 'Red', ))
def test_stackoverflow_question_59176476(self): """ https://stackoverflow.com/questions/59176476/in-python-how-to-parse-a-multi-layered-json """ from benedict import benedict as bdict # data_source = 'http://legis.senado.leg.br/dadosabertos/materia/20050' # data = bdict.from_json('http://legis.senado.leg.br/dadosabertos/materia/20050') data_source = { 'DetalheMateria': { '@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', '@xsi:noNamespaceSchemaLocation': 'http://legis.senado.leg.br/dadosabertos/dados/DetalheMateriav5.xsd', 'Metadados': { 'Versao': '03/12/2019 21:36:18', 'VersaoServico': '5', 'DataVersaoServico': '2017-02-01', 'DescricaoDataSet': 'Dados básicos da matéria, incluindo autoria, norma gerada e outras informações. A partir de fevereiro de 2019, os requerimentos de comissões permanentes passam a ser numerados com a mesma sistemática dos requerimentos das comissões temporárias, seguindo, portanto, o padrão: REQ 99/2019 - SIGLA_COMISSÃO. Dessa forma, para se buscar um requerimento de determinada comissão (com ano a partir de 2019), deve-se informar .../materia/sigla/número/ano?comissao=sigla da comissão.' }, 'Materia': { 'IdentificacaoMateria': { 'CodigoMateria': '20050', 'SiglaCasaIdentificacaoMateria': 'SF', 'NomeCasaIdentificacaoMateria': 'Senado Federal', 'SiglaSubtipoMateria': 'PLC', 'DescricaoSubtipoMateria': 'PROJETO DE LEI DA CÂMARA', 'NumeroMateria': '00035', 'AnoMateria': '1988', 'DescricaoObjetivoProcesso': 'Revisora', 'DescricaoIdentificacaoMateria': 'PLC 35/1988', 'IndicadorTramitando': 'Não' }, 'DadosBasicosMateria': { 'EmentaMateria': "DECLARA FERIADO NACIONAL O DIA 20 DE NOVEMBRO, ANIVERSARIO DA MORTE \n DE ZUMBI DOS PALMARES, CONSAGRADO PELA COMUNIDADE AFRO-BRASILEIRA \n COMO 'DIA NACIONAL DA CONSCIENCIA NEGRA.' \n ", 'IndicadorComplementar': 'Não', 'DataApresentacao': '1987-12-01', 'DataLeitura': '1987-12-01' }, 'Autoria': { 'Autor': [{ 'NomeAutor': 'Câmara dos Deputados', 'SiglaTipoAutor': 'CAMARA', 'DescricaoTipoAutor': 'Câmara dos Deputados', 'NumOrdemAutor': '1', 'IndicadorOutrosAutores': 'Não' }] }, 'Iniciativa': { 'SiglaTipoIniciativa': 'DEPUTADO', 'DescricaoTipoIniciativa': 'Deputado', 'DescricaoIniciativa': 'BENEDITA DA SILVA' }, 'OrigemMateria': { 'SiglaCasaOrigem': 'CD', 'NomeCasaOrigem': 'Câmara dos Deputados' }, 'CasaIniciadoraNoLegislativo': { 'SiglaCasaIniciadora': 'SF', 'NomeCasaIniciadora': 'Senado Federal' }, 'OutrosNumerosDaMateria': { 'OutroNumeroDaMateria': { 'IdentificacaoMateria': { 'SiglaCasaIdentificacaoMateria': 'CD', 'NomeCasaIdentificacaoMateria': 'Câmara dos Deputados', 'SiglaSubtipoMateria': 'PL', 'DescricaoSubtipoMateria': 'PROJETO DE LEI', 'NumeroMateria': '00293', 'AnoMateria': '1987' }, 'DescricaoTipoNumeracao': 'CasaIniciadora' } }, 'SituacaoAtual': { 'Autuacoes': { 'Autuacao': [{ 'NumeroAutuacao': '1', 'Situacao': { 'DataSituacao': '1996-01-23', 'CodigoSituacao': '28', 'SiglaSituacao': 'ARQVD', 'DescricaoSituacao': 'ARQUIVADA AO FINAL DA LEGISLATURA' }, 'Local': { 'DataLocal': '1996-01-23', 'CodigoLocal': '206', 'TipoLocal': 'A', 'SiglaCasaLocal': 'SF', 'NomeCasaLocal': 'Senado Federal', 'SiglaLocal': 'SSEXP', 'NomeLocal': 'SUBSECRETARIA DE EXPEDIENTE' } }] } }, 'OutrasInformacoes': { 'Servico': [{ 'NomeServico': 'EmendaMateria', 'DescricaoServico': 'Emendas da matéria', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/materia/emendas/20050?v=6' }, { 'NomeServico': 'MovimentacaoMateria', 'DescricaoServico': 'Movimentações da matéria, como tramitações, despachos e prazos.\n Evoluções:\n 14/8/2019\n - criada nova Tag "DeliberacoesMPV" para as deliberações de MPV;\n - criada nova Tag "DescricaoItemCalendario" subord', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/materia/movimentacoes/20050?v=6' }, { 'NomeServico': 'RelatoriaMateria', 'DescricaoServico': 'Relatoria atual e relatorias encerradas (histórico) da matéria', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/materia/relatorias/20050?v=5' }, { 'NomeServico': 'TextoMateria', 'DescricaoServico': 'Textos da matéria. \n Evoluções da versão: \n 28/8/2019 - incluída a tag "AutoriaTexto", sob a tag "Texto".', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/materia/textos/20050?v=5' }, { 'NomeServico': 'VotacaoMateria', 'DescricaoServico': 'Votações da matéria', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/materia/votacoes/20050?v=5' }, { 'NomeServico': 'VotacoesComissao', 'UrlServico': 'http://legis.senado.leg.br/dadosabertos/votacaoComissao/materia/PLC/00035/1988?v=1' }] }, 'UrlGlossario': 'http://legis.senado.leg.br/dadosabertos/glossario/lista' } } } data = bdict(data_source) # once your url will return a valid json you could just do: # data = bdict.from_json('http://legis.senado.leg.br/dadosabertos/materia/20050') author = bdict(data[['DetalheMateria', 'Materia', 'Autoria', 'Autor']][0]) author_name = author['NomeAutor']
def test_stackoverflow_question_60276839(self): """ https://stackoverflow.com/questions/60276839/merge-list-of-nested-dictionaries """ from benedict import benedict as bdict data_input = [{ '9940542': { 'Invalid Adjust(Platform Fee)': '-1.00' } }, { '9940542': { 'Invalid Adjust(Media Fee)': '-2.00' } }, { '10315065': { 'Invalid Adjust(Platform Fee)': '-1.00' } }, { '10315065': { 'Invalid Adjust(Media Fee)': '-3.00' } }, { '11211744': { 'Invalid Adjust(Platform Fee)': '-290.00' } }, { '11211744': { 'Invalid Adjust(Media Fee)': '-3403.00' } }, { '11309685': { 'Invalid Adjust(Platform Fee)': '-57.00' } }, { '11309685': { 'Invalid Adjust(Media Fee)': '-670.00' } }, { '12103490': { 'Media Fee': '709472.00' } }, { '12103490': { 'Platform Fee': '22703.00' } }, { '12103490': { 'Invalid Adjust(Platform Fee)': '-30.00' } }, { '12103490': { 'TrueView Budget Adjust (Platofrm Fee)': '-301.00' } }, { '12103490': { 'Invalid Adjust(Media Fee)': '-348.00' } }, { '12103490': { 'TrueView Budget Adjust (Media Fee)': '-9376.00' } }, { '12160150': { 'Media Fee': '549173.00' } }, { '12160150': { 'Platform Fee': '17573.00' } }] data_output = bdict() data_output.merge(*data_input) # print(data_output.dump()) expected_data_output = { '10315065': { 'Invalid Adjust(Media Fee)': '-3.00', 'Invalid Adjust(Platform Fee)': '-1.00', }, '11211744': { 'Invalid Adjust(Media Fee)': '-3403.00', 'Invalid Adjust(Platform Fee)': '-290.00', }, '11309685': { 'Invalid Adjust(Media Fee)': '-670.00', 'Invalid Adjust(Platform Fee)': '-57.00', }, '12103490': { 'Invalid Adjust(Media Fee)': '-348.00', 'Invalid Adjust(Platform Fee)': '-30.00', 'Media Fee': '709472.00', 'Platform Fee': '22703.00', 'TrueView Budget Adjust (Media Fee)': '-9376.00', 'TrueView Budget Adjust (Platofrm Fee)': '-301.00', }, '12160150': { 'Media Fee': '549173.00', 'Platform Fee': '17573.00', }, '9940542': { 'Invalid Adjust(Media Fee)': '-2.00', 'Invalid Adjust(Platform Fee)': '-1.00', }, } self.assertEqual(data_output, expected_data_output)
def test_stackoverflow_question_60072709(self): """ https://stackoverflow.com/questions/60072709/i-want-to-convert-sample-json-data-into-nested-json-using-specific-key-value-in """ from benedict import benedict as bdict json_input = """ { "1": { "amount": 0, "breakdown": [ { "amount": 169857, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 8709, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 80862, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 51479, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 37204, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 13141, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 39591, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 41385, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 92801, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 77056, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 3412, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 5297, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 75020, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1217, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 4625, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 1, "name": "Project 1" }, "2": { "amount": 0, "breakdown": [ { "amount": 205822, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 12132, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 56235, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 72901, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 33750, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 10547, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 53946, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 38910, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 131750, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 74072, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 2585, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 9547, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 50087, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1463, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 4685, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 2, "name": "Project 2" }, "3": { "amount": 0, "breakdown": [ { "amount": 253894, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 11924, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 54651, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 82045, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 46000, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 9835, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 45660, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 25984, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 162184, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 91710, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 2596, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 9328, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 48171, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1550, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 4930, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 3, "name": "Project 3" }, "4": { "amount": 0, "breakdown": [ { "amount": 215166, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 11030, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 60883, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 82842, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 26713, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 13305, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 47650, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 41266, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 130015, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 85151, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 2341, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 8689, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 55320, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1399, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 4164, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 4, "name": "Project 4" }, "5": { "amount": 0, "breakdown": [ { "amount": 257678, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 11908, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 69707, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 80342, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 25483, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 12735, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 47972, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 39871, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 133534, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 124144, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 2083, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 9825, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 63413, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1437, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 4857, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 5, "name": "Project 5" }, "6": { "amount": 0, "breakdown": [ { "amount": 202257, "id": 4, "name": "Website Development", "parent_id": "1" }, { "amount": 11990, "id": 5, "name": "Mobile App Development", "parent_id": "1" }, { "amount": 67792, "id": 6, "name": "DevOps", "parent_id": "1" }, { "amount": 78702, "id": 7, "name": "Wireframing", "parent_id": "2" }, { "amount": 32707, "id": 8, "name": "UI Design", "parent_id": "2" }, { "amount": 10431, "id": 9, "name": "Brochure Design", "parent_id": "2" }, { "amount": 28636, "id": 10, "name": "Social Media Marketing", "parent_id": "3" }, { "amount": 43055, "id": 11, "name": "Print Media Marketing", "parent_id": "3" }, { "amount": 101200, "id": 12, "name": "Payment Gateway License", "parent_id": "4" }, { "amount": 101057, "id": 13, "name": "JavaScript Plugin License", "parent_id": "4" }, { "amount": 3081, "id": 14, "name": "Google Play Store Fees", "parent_id": "5" }, { "amount": 8909, "id": 15, "name": "Apple App Store Fees", "parent_id": "5" }, { "amount": 60790, "id": 16, "name": "Servers", "parent_id": "6" }, { "amount": 1612, "id": 17, "name": "Domain Name", "parent_id": "6" }, { "amount": 5390, "id": 18, "name": "SSL Certificate", "parent_id": "6" } ], "id": 6, "name": "Project 6" } } """ json_output = """ [ { "id": 1, "name": "Project 1", "amount": 442228, "breakdown": [ { "id": 1, "name": "Development", "amount": 259428, "breakdown": [ { "id": 4, "name": "Website Development", "amount": 169857, "breakdown": [ { "id": 12, "name": "Payment Gateway License", "amount": 92801, "breakdown": [] }, { "id": 13, "name": "JavaScript Plugin License", "amount": 77056, "breakdown": [] } ] }, { "id": 5, "name": "Mobile App Development", "amount": 8709, "breakdown": [ { "id": 14, "name": "Google Play Store Fees", "amount": 3412, "breakdown": [] }, { "id": 15, "name": "Apple App Store Fees", "amount": 5297, "breakdown": [] } ] }, { "id": 6, "name": "DevOps", "amount": 80862, "breakdown": [ { "id": 16, "name": "Servers", "amount": 75020, "breakdown": [] }, { "id": 17, "name": "Domain Name", "amount": 1217, "breakdown": [] }, { "id": 18, "name": "SSL Certificate", "amount": 4625, "breakdown": [] } ] } ] }, { "id": 2, "name": "Designing", "amount": 101824, "breakdown": [ { "id": 7, "name": "Wireframing", "amount": 51479, "breakdown": [] }, { "id": 8, "name": "UI Design", "amount": 37204, "breakdown": [] }, { "id": 9, "name": "Brochure Design", "amount": 13141, "breakdown": [] } ] }, { "id": 3, "name": "Marketing", "amount": 80976, "breakdown": [ { "id": 10, "name": "Social Media Marketing", "amount": 39591, "breakdown": [] }, { "id": 11, "name": "Print Media Marketing", "amount": 41385, "breakdown": [] } ] } ] } ] """ data = bdict.from_json(json_input) keys = list(data.keys()) items = [] for key in keys: item = bdict(data.get(key)) # move all items to the top level items += list(item['breakdown']) item['breakdown'] = [] items.append(item) break # convert all parent_id values to int to allow comparison for item in items: if 'parent_id' in item: item['parent_id'] = int(item['parent_id']) items_dict = bdict({'items': items}) items_dict['items_nested'] = items_dict.nest('items', id_key='id', parent_id_key='parent_id', children_key='breakdown')
def test_stackoverflow_question_58692636(self): """ https://stackoverflow.com/questions/58692636/python-script-fails-to-extract-data-from-xml/58695393#58695393 """ from benedict import benedict as bdict data_xml = """ <feed xml:base="http://data.treasury.gov/Feed.svc/"> <title type="text">DailyTreasuryYieldCurveRateData</title> <id> http://data.treasury.gov/feed.svc/DailyTreasuryYieldCurveRateData </id> <updated>2019-11-04T07:15:32Z</updated> <link rel="self" title="DailyTreasuryYieldCurveRateData" href="DailyTreasuryYieldCurveRateData"/> <entry> <id> http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7258) </id> <title type="text"/> <updated>2019-11-04T07:15:32Z</updated> <author> <name/> </author> <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7258)"/> <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> <content type="application/xml"> <m:properties> <d:Id m:type="Edm.Int32">7258</d:Id> <d:NEW_DATE m:type="Edm.DateTime">2019-01-02T00:00:00</d:NEW_DATE> <d:BC_1MONTH m:type="Edm.Double">2.4</d:BC_1MONTH> <d:BC_2MONTH m:type="Edm.Double">2.4</d:BC_2MONTH> <d:BC_3MONTH m:type="Edm.Double">2.42</d:BC_3MONTH> <d:BC_6MONTH m:type="Edm.Double">2.51</d:BC_6MONTH> <d:BC_1YEAR m:type="Edm.Double">2.6</d:BC_1YEAR> <d:BC_2YEAR m:type="Edm.Double">2.5</d:BC_2YEAR> <d:BC_3YEAR m:type="Edm.Double">2.47</d:BC_3YEAR> <d:BC_5YEAR m:type="Edm.Double">2.49</d:BC_5YEAR> <d:BC_7YEAR m:type="Edm.Double">2.56</d:BC_7YEAR> <d:BC_10YEAR m:type="Edm.Double">2.66</d:BC_10YEAR> <d:BC_20YEAR m:type="Edm.Double">2.83</d:BC_20YEAR> <d:BC_30YEAR m:type="Edm.Double">2.97</d:BC_30YEAR> <d:BC_30YEARDISPLAY m:type="Edm.Double">2.97</d:BC_30YEARDISPLAY> </m:properties> </content> </entry> <entry> <id> http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7259) </id> <title type="text"/> <updated>2019-11-04T07:15:32Z</updated> <author> <name/> </author> <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7259)"/> <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> <content type="application/xml"> <m:properties> <d:Id m:type="Edm.Int32">7259</d:Id> <d:NEW_DATE m:type="Edm.DateTime">2019-01-03T00:00:00</d:NEW_DATE> <d:BC_1MONTH m:type="Edm.Double">2.42</d:BC_1MONTH> <d:BC_2MONTH m:type="Edm.Double">2.42</d:BC_2MONTH> <d:BC_3MONTH m:type="Edm.Double">2.41</d:BC_3MONTH> <d:BC_6MONTH m:type="Edm.Double">2.47</d:BC_6MONTH> <d:BC_1YEAR m:type="Edm.Double">2.5</d:BC_1YEAR> <d:BC_2YEAR m:type="Edm.Double">2.39</d:BC_2YEAR> <d:BC_3YEAR m:type="Edm.Double">2.35</d:BC_3YEAR> <d:BC_5YEAR m:type="Edm.Double">2.37</d:BC_5YEAR> <d:BC_7YEAR m:type="Edm.Double">2.44</d:BC_7YEAR> <d:BC_10YEAR m:type="Edm.Double">2.56</d:BC_10YEAR> <d:BC_20YEAR m:type="Edm.Double">2.75</d:BC_20YEAR> <d:BC_30YEAR m:type="Edm.Double">2.92</d:BC_30YEAR> <d:BC_30YEARDISPLAY m:type="Edm.Double">2.92</d:BC_30YEARDISPLAY> </m:properties> </content> </entry> <entry> <id> http://data.treasury.gov/Feed.svc/DailyTreasuryYieldCurveRateData(7260) </id> <title type="text"/> <updated>2019-11-04T07:15:32Z</updated> <author> <name/> </author> <link rel="edit" title="DailyTreasuryYieldCurveRateDatum" href="DailyTreasuryYieldCurveRateData(7260)"/> <category term="TreasuryDataWarehouseModel.DailyTreasuryYieldCurveRateDatum" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> <content type="application/xml"> <m:properties> <d:Id m:type="Edm.Int32">7260</d:Id> <d:NEW_DATE m:type="Edm.DateTime">2019-01-04T00:00:00</d:NEW_DATE> <d:BC_1MONTH m:type="Edm.Double">2.4</d:BC_1MONTH> <d:BC_2MONTH m:type="Edm.Double">2.42</d:BC_2MONTH> <d:BC_3MONTH m:type="Edm.Double">2.42</d:BC_3MONTH> <d:BC_6MONTH m:type="Edm.Double">2.51</d:BC_6MONTH> <d:BC_1YEAR m:type="Edm.Double">2.57</d:BC_1YEAR> <d:BC_2YEAR m:type="Edm.Double">2.5</d:BC_2YEAR> <d:BC_3YEAR m:type="Edm.Double">2.47</d:BC_3YEAR> <d:BC_5YEAR m:type="Edm.Double">2.49</d:BC_5YEAR> <d:BC_7YEAR m:type="Edm.Double">2.56</d:BC_7YEAR> <d:BC_10YEAR m:type="Edm.Double">2.67</d:BC_10YEAR> <d:BC_20YEAR m:type="Edm.Double">2.83</d:BC_20YEAR> <d:BC_30YEAR m:type="Edm.Double">2.98</d:BC_30YEAR> <d:BC_30YEARDISPLAY m:type="Edm.Double">2.98</d:BC_30YEARDISPLAY> </m:properties> </content> </entry> </feed> """ data = bdict.from_xml(data_xml) # print(data.dump()) entries = data['feed.entry'] for entry in entries: props = bdict(bdict(entry)['content.m:properties']) # print(props.dump()) for key, value in props.items(): # print(key, value['#text']) pass
def test_stackoverflow_question_20528081(self): """ https://stackoverflow.com/questions/20528081/performance-of-calculations-on-large-flattened-dictionary-with-implied-hierarchy """ from benedict import benedict as bdict d = { 'guy1_arm_param1': 23.0, 'guy1_arm_param2_low': 2.0, 'guy1_arm_param2_high': 3.0, 'guy1_arm_param3': 20.0, 'guy1_leg_param1': 40.0, 'guy1_leg_param2_low': 2.0, 'guy1_leg_param2_high': 3.0, 'guy1_leg_param3': 20.0, 'guy2_arm_param1': 23.0, 'guy2_arm_param2_low': 2.0, 'guy2_arm_param2_high': 3.0, 'guy2_arm_param3': 20.0, 'guy2_leg_param1': 40.0, 'guy2_leg_param2_low': 2.0, 'guy2_leg_param2_high': 3.0, 'guy2_leg_param3': 20.0, 'another_guy_param1': 3.0, } b = bdict(d) u = b.unflatten() # print(u.dump()) r = { "another": { "guy": { "param1": 3.0, }, }, "guy1": { "arm": { "param1": 23.0, "param2": { "high": 3.0, "low": 2.0, }, "param3": 20.0, }, "leg": { "param1": 40.0, "param2": { "high": 3.0, "low": 2.0, }, "param3": 20.0, }, }, "guy2": { "arm": { "param1": 23.0, "param2": { "high": 3.0, "low": 2.0, }, "param3": 20.0, }, "leg": { "param1": 40.0, "param2": { "high": 3.0, "low": 2.0, }, "param3": 20.0, }, }, } self.assertEqual(u, r)