In this very simple example, Python program will just print a set of market and fixings data from CSV files, based on a given set of configurations. In order to keep this example program short and sweet, our JSON configuration file has only two configurations: directory addresses for market and fixings data CSV files, as follows.
{ "MARKETDATA":"/home/mikejuniperhill/Market.csv", "FIXINGSDATA":"/home/mikejuniperhill/Fixings.csv" }
Directory address of this configuration file will be given as a command line argument for the program. Based on this given configuration, program will then read configured data from files to be used in program.
Example program is shown below. In the first stage, program will create configurations object. Technically, this object is just a wrapper for dictionary data structure. Any specific configuration can be accessed by using Python version of index operator overloading. After this, program reads data from configured CSV files into DataFrame objects and prints their contents to terminal.
import json import sys import pandas # class for hosting configurations class Configurations: inner = {} # read JSON configuration file to dictionary def __init__(self, filePathName): self.inner = json.load(open(filePathName)) self.inner = {k.upper(): v for k, v in self.inner.items()} # return value for a given configuration key # 'overload' indexing operator def __getitem__(self, key): return self.inner[key.upper()] # configuration file string is command line argument configurationsFilePathName = sys.argv[1] # create configurations object config = Configurations(configurationsFilePathName) # create market data based on configuration market = pandas.read_csv(config['MarketData']) print('EUR swap curve:') print(market.head()) # create fixings data based on configuration fixings = pandas.read_csv(config['FixingsData']) print('6M Euribor fixings:') print(fixings.head())
Handy tool for constructing and testing syntactic correctness of any JSON file can be found in here. Finally, thanks for reading.
-Mike
No comments:
Post a Comment