def has_property(propertyList, keyName): if not propertyList: return False for propertyItem in propertyList: if 'keyName' in propertyItem and propertyItem['keyName'] == keyName: return True return False def has_property_with_value(propertyList, keyName, dataValue): propertyItem = get_property(propertyList, keyName); if propertyItem and 'dataValue' in propertyItem and propertyItem['dataValue'] == dataValue: return True return False def get_property(propertyList, keyName): if not propertyList: return None for propertyItem in propertyList: if 'keyName' in propertyItem and propertyItem['keyName'] == keyName: return propertyItem return None def get_property_for_scenario(propertyList, keyName, scenarioName): if not propertyList: return None for propertyItem in propertyList: if 'keyName' in propertyItem and propertyItem['keyName'] == keyName: if 'scenarioName' in propertyItem and propertyItem['scenarioName'] == scenarioName: return propertyItem return None def _calc_tariff_comparison(tariffComparisonRequest): eligibilityProperties = ['isCARECustomer', 'isFERACustomer', 'hasElectricVehicle'] extraParams = {} for p in eligibilityProperties: if has_property_with_value(tariffComparisonRequest, p, True): # Include explicitly true. e.g. applicability=eq(isCARECustomer,true) if 'applicability' in extraParams: extraParams['applicability'].append("eq(%s,true)" % p) else: extraParams['applicability'] = ["eq(%s,true)" % p] else: # Include explicitly false or missing, e.g. filterByApplicability=true&applicabilityFilters[solarPvEligible]=false extraParams['filterByApplicability'] = True extraParams['applicabilityFilters[%s]' % p] = False print extraParams testPropertyList = [] print 'empty should be false %s' % has_property(testPropertyList, 'hello') print 'empty with value should be false %s' % has_property_with_value(testPropertyList, 'consumption', 120) testPropertyList.append({ 'keyName': 'consumption', 'dataValue': 100 }) print 'mismatch should be false %s' % has_property(testPropertyList, 'hello') print 'mismatch value should be false %s' % has_property_with_value(testPropertyList, 'hello', 'world') print 'match should be false %s' % has_property(testPropertyList, 'consumption') find = get_property(testPropertyList, 'consumption') print find testPropertyList.append({ 'keyName': 'masterTariffId', 'dataValue': 543, 'scenarioName': 'current' }) findS = get_property_for_scenario(testPropertyList, 'masterTariffId', 'current') print findS findN = get_property_for_scenario(testPropertyList, 'notInIt', 'current') print not findN print 'match value should be false %s' % has_property_with_value(testPropertyList, 'consumption', 120) print 'match value should be true %s' % has_property_with_value(testPropertyList, 'consumption', 100) _calc_tariff_comparison(testPropertyList) testPropertyList.append({ 'keyName': 'isFERACustomer', 'dataValue': True }) _calc_tariff_comparison(testPropertyList) testPropertyList.append({ 'keyName': 'hasElectricVehicle', 'dataValue': True }) _calc_tariff_comparison(testPropertyList)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question