Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert two dictionary to influx writing measurement fields in python

I have a dictionary like

{'BTC': {'030323': 562.7,
  '170323': 9.900000000000002,
  '250223': 64.60000000000004,
  '290923': 17.6,
  '100323': 53.90000000000002,
  '310323': 27.4,
  '260223': 15.499999999999996,
  '300623': 33.7,
  '280423': 30.0,
  '291223': 28.6,
  '260523': 1.0999999999999999},
 'ETH': {'030323': 562.7,
  '170323': 9.900000000000002,
  '250223': 64.60000000000004,
  '290923': 17.6,
  '100323': 53.90000000000002,
  '310323': 27.4,
  '260223': 15.499999999999996,
  '300623': 33.7,
  '280423': 30.0,
  '291223': 28.6,
  '260523': 1.0999999999999999}}

and other dictionary like

{'250223': ['daily'],
 '260223': ['bidaily'],
 '030323': ['weekly'],
 '100323': ['biweekly'],
 '170323': ['triweekly'],
 '310323': ['monthly', 'quarterly'],
 '280423': ['bimonthly'],
 '260523': ['trimonthly'],
 '300623': ['biquarterly'],
 '290923': ['triquarterly'],
 '291223': ['yearly']}

I need data structure to insert it into influx db.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Example:

{'measurement': 'measurement_name',
        'tags': {
                'asset': 'BTC', 
                'maturity_date': 030323, 
                'Expiry' - 'weekly'
            },
        'fields': {
            'Amount' - '562.7' 
            },
        'time': int(time.time()*1000),
        }

Confused with the data format on how to do it for all the values, is there any mapping that would be helpful.

>Solution :

Yes, you can use the following code to change the two dictionaries into the format that InfluxDB needs:

import time

def convert_to_influx_format(asset_dict, expiry_dict):
    result = []
    for asset, amounts in asset_dict.items():
        for maturity_date, amount in amounts.items():
            if maturity_date in expiry_dict:
                for expiry in expiry_dict[maturity_date]:
                    result.append({
                        'measurement': 'measurement_name',
                        'tags': {
                            'asset': asset,
                            'maturity_date': maturity_date,
                            'Expiry': expiry
                        },
                        'fields': {
                            'Amount': amount
                        },
                        'time': int(time.time()*1000)
                    })
    return result

result = convert_to_influx_format({'BTC': {'030323': 562.7,
  '170323': 9.900000000000002,
  '250223': 64.60000000000004,
  '290923': 17.6,
  '100323': 53.90000000000002,
  '310323': 27.4,
  '260223': 15.499999999999996,
  '300623': 33.7,
  '280423': 30.0,
  '291223': 28.6,
  '260523': 1.0999999999999999},
 'ETH': {'030323': 562.7,
  '170323': 9.900000000000002,
  '250223': 64.60000000000004,
  '290923': 17.6,
  '100323': 53.90000000000002,
  '310323': 27.4,
  '260223': 15.499999999999996,
  '300623': 33.7,
  '280423': 30.0,
  '291223': 28.6,
  '260523': 1.0999999999999999}},
{'250223': ['daily'],
 '260223': ['bidaily'],
 '030323': ['weekly'],
 '100323': ['biweekly'],
 '170323': ['triweekly'],
 '310323': ['monthly', 'quarterly'],
 '280423': ['bimonthly'],
 '260523': ['trimonthly'],
 '300623': ['biquarterly'],
 '290923': ['triquarterly'],
 '291223': ['yearly']})

print(result)

VERSION 2 WITH COMPRESHENSION LIST (ARRAY)

import time

def convert_to_influx_format(asset_dict, expiry_dict):
    result = [{'measurement': 'measurement_name',
               'tags': {'asset': asset, 'maturity_date': maturity_date, 'Expiry': expiry},
               'fields': {'Amount': amount},
               'time': int(time.time() * 1000)}
              for asset, amounts in asset_dict.items()
              for maturity_date, amount in amounts.items()
              if maturity_date in expiry_dict
              for expiry in expiry_dict[maturity_date]]

    return result

result = convert_to_influx_format({'BTC': {'030323': 562.7, '170323': 9.9, '250223': 64.6,
                                           '290923': 17.6, '100323': 53.9, '310323': 27.4, '260223': 15.5,
                                           '300623': 33.7, '280423': 30.0, '291223': 28.6, '260523': 1.1},
                                   'ETH': {'030323': 562.7, '170323': 9.9, '250223': 64.6, '290923': 17.6,
                                           '100323': 53.9, '310323': 27.4, '260223': 15.5, '300623': 33.7,
                                           '280423': 30.0, '291223': 28.6, '260523': 1.1}},
                                  {'250223': ['daily'], '260223': ['bidaily'], '030323': ['weekly'],
                                   '100323': ['biweekly'], '170323': ['triweekly'], '310323': ['monthly', 'quarterly'],
                                   '280423': ['bimonthly'], '260523': ['trimonthly'], '300623': ['biquarterly'],
                                   '290923': ['triquarterly'], '291223': ['yearly']})

print(result)

VERSION 3 TO PREVENT REPETITION

import time

def convert_to_influx_format(asset_dict, expiry_dict):
    result = []
    for asset, amounts in asset_dict.items():
        for maturity_date, amount in amounts.items():
            if maturity_date in expiry_dict:
                expiries = expiry_dict[maturity_date]
                result.append({'measurement': 'measurement_name',
                               'tags': {'asset': asset, 'maturity_date': maturity_date, 'Expiry': ','.join(expiries)},
                               'fields': {'Amount': amount},
                               'time': int(time.time() * 1000)})

    return result

result = convert_to_influx_format({'BTC': {'030323': 562.7, '170323': 9.9, '250223': 64.6,
                                           '290923': 17.6, '100323': 53.9, '310323': 27.4, '260223': 15.5,
                                           '300623': 33.7, '280423': 30.0, '291223': 28.6, '260523': 1.1},
                                   'ETH': {'030323': 562.7, '170323': 9.9, '250223': 64.6, '290923': 17.6,
                                           '100323': 53.9, '310323': 27.4, '260223': 15.5, '300623': 33.7,
                                           '280423': 30.0, '291223': 28.6, '260523': 1.1}},
                                  {'250223': ['daily'], '260223': ['bidaily'], '030323': ['weekly'],
                                   '100323': ['biweekly'], '170323': ['triweekly'], '310323': ['monthly', 'quarterly'],
                                   '280423': ['bimonthly'], '260523': ['trimonthly'], '300623': ['biquarterly'],
                                   '290923': ['triquarterly'], '291223': ['yearly']})

print(result)

In the end, you’ll have a single dictionary for each asset and maturity date. The Expiry field will have a string of all the expiry values separated by commas.

I have made now 3 examples you can use all of these to your project or play with it

Hope this helps you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading