Package: configParsePy
configPy [ 🏗 Under Construction ]A tiny Configuration File Parser for Python Projects. Currently Supports JSON files only.InstallationInstall the Latest Stable Build usingpip install configParsePyUsageUse configPy to get your configurations imported to your python code from a configuration File.Import theconfigPymodule or its member methods to hadle different types of configuration files.from configPy import JSONConfigParserJSON Configuration FilesTo handle JSON Configuration Files using configPy use theJSONConfigParserobject from the configPy Module.from configPy import JSONConfigParserInitiate theJSONConfigParserby passing the JSON configuration file as an argument.configObject = JSONConfigParser(configFilePath="./sampleConfig.json")UsegetConfigurations()method to get the Imported Configuration.The getConfigurations()method returns the configurations as a Dictionary.configurations = configObject.getConfigurations()Use the configurations as adictobject.print("Module Name: ", configurations["module_name"])
print("Purpose: ", configurations["purpose"])Examplesfrom configPy import JSONConfigParser

importedConfigs = JSONConfigParser(configFilePath="./configFiles/sampleJSONConfig.json").getConfigurations()

print("Sample JSONConfigParser Test")

print("Module Name: ", importedConfigs["module_name"])
print("Purpose: ", importedConfigs["purpose"])The whole code for the above example can be foundhere.NDJSON Configuration FilesNDJSON or New-line Delimited JSON are files where each line is a valid JSON Object and each line is seperated by a newline\ncharacter.More about NDJSON can be found here.A sample NDJSON Structure can be{"env" : "dev" , "language" : "python" , "version" : 2.0}
{"env" : "stage" , "language" : "python" , "version" : 3.5}
{"env" : "prod" , "language" : "python" , "version" : 3.9}Use cases for NDJSON Config Files are listed in this Issue#11To handle NDJSON Configuration Files using configPy use theNDJSONConfigParserobject from the configPy Module.from configPy import NDJSONConfigParserInitiate theNDJSONConfigParserby passing the configuration file as an argument.configObject = NDJSONConfigParser(configFilePath="./sampleConfig.ndjson")UsegetConfigurations()method to get the Imported Configuration.The getConfigurations()method returns the configurations as a Dictionary.configurations = configObject.getConfigurations()Use the configurations as alistobject with each JSON Object as a member of the list and can be accessed using the index number.print("Dev Environment Python Version: ", importedConfigs[0]["version"])
print("Prod Environment Python Version: ", importedConfigs[2]["version"])mapKeysNDJSON(mappingKeys=[ ])While thegetCredentials()method returns a list of JSON Objects, a user might, at times, need a key-based access to the JSON Objects (Refer to Issue #11). In such a use case themapKeysNDJSON()provides the capability to map user-defined keys onto JSON Objects in the NDJSON Config files, that can be accessed using the key instead of index-number.ThemapKeysNDJSON()method takes an array of user definedkeys, of length same as that of the number lines of JSON Objects in the NDJSON File. Each Key is mapped to the JSON Object of same index.configObject = NDJSONConfigParser(configFilePath="./sampleNDJSONConfig.ndjson")
configurations = configObject.mapKeysNDJSON(mappingKeys=["dev","stage","prod"]).getConfigurations()Using thegetConfigurations()after using themapKeysNDJSON([])method returns adictobject instead of a list. The imported configurations can be used by their mapped keys.print("Dev Environment Python Version: ", importedConfigs["dev"]["version"])
print("Stage Environment Python Version: ", importedConfigs["stage"]["version"])Examplesfrom configPy import NDJSONConfigParser

importedConfigs = NDJSONConfigParser(configFilePath="./configFiles/sampleNDJSONConfig.ndjson").getConfigurations()

print("Sample NDJSONConfigParser Test")

print("Dev Environment Python Version: ", importedConfigs[0]["version"])
print("Prod Environment Python Version: ", importedConfigs[2]["version"])

importedConfigs = NDJSONConfigParser(configFilePath="./configFiles/sampleNDJSONConfig.ndjson").mapKeysNDJSON(mappingKeys=["dev","stage","prod"]).getConfigurations()

print("Sample NDJSONConfigParser with Key Mapping Test")

print("Dev Environment Python Version: ", importedConfigs["dev"]["version"])
print("Stage Environment Python Version: ", importedConfigs["stage"]["version"])The whole code for the above example can be foundhere.