Skip to content

Instantly share code, notes, and snippets.

@PiotrCzapla
Last active March 16, 2022 11:42
Show Gist options
  • Select an option

  • Save PiotrCzapla/074d8e73c6c12a66869918187e49b84d to your computer and use it in GitHub Desktop.

Select an option

Save PiotrCzapla/074d8e73c6c12a66869918187e49b84d to your computer and use it in GitHub Desktop.

Revisions

  1. PiotrCzapla revised this gist Mar 16, 2022. 1 changed file with 6 additions and 8 deletions.
    14 changes: 6 additions & 8 deletions exchange_rate.py
    Original file line number Diff line number Diff line change
    @@ -1,19 +1,17 @@
    # Code mostly written by github copilot with few modifications
    # license: mit

    import requests
    import pandas as pd
    from functools import lru_cache
    from datetime import datetime

    @lru_cache(maxsize=None)
    def get_nbp_exchange_rate(date:datetime, table='A'):
    """
    Get the exchange rate from the NBP API, and store it in cache folder
    >> get_nbp_exchange_rate(datetime(2022,3,8))['EUR']
    4.9121
    """
    url = 'http://api.nbp.pl/api/exchangerates/tables/{:s}/{:%Y-%m-%d}/?format=json'.format(table, date)
    r = requests.get(url)
    df = pd.DataFrame(r.json()[0]['rates']).set_index('code')
    return df

    get_nbp_exchange_rate(datetime.now())['mid']['EUR']
    if r.status_code != 200:
    raise RuntimeError("Unable to fetch exchange rate from NBP API")
    return {rate['code']:rate['mid'] for rate in r.json()[0]['rates']}
  2. PiotrCzapla created this gist Mar 16, 2022.
    19 changes: 19 additions & 0 deletions exchange_rate.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # Code mostly written by github copilot with few modifications
    # license: mit

    import requests
    import pandas as pd
    from functools import lru_cache
    from datetime import datetime

    @lru_cache(maxsize=None)
    def get_nbp_exchange_rate(date:datetime, table='A'):
    """
    Get the exchange rate from the NBP API, and store it in cache folder
    """
    url = 'http://api.nbp.pl/api/exchangerates/tables/{:s}/{:%Y-%m-%d}/?format=json'.format(table, date)
    r = requests.get(url)
    df = pd.DataFrame(r.json()[0]['rates']).set_index('code')
    return df

    get_nbp_exchange_rate(datetime.now())['mid']['EUR']