Created
September 11, 2017 08:08
-
-
Save systemcatch/256b06fca69921c605f0c04a16435255 to your computer and use it in GitHub Desktop.
Idea for production validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate(data, **kwargs): | |
""" | |
Validates a production datapoint based on given constraints. | |
By default this will change type values that are negative to None. | |
If the datapoint is found to be invalid then the production key | |
is changed to the following. | |
data['production'] = None | |
Optional Arguments: | |
-- | |
missing (list) | |
Generation types that must be present. | |
If any of these types are None the datapoint will be invalidated. | |
For example ['gas', 'hydro'] | |
Defaults to an empty list. | |
-- | |
allow_negative (bool) | |
Determines if negative generation values are allowed. | |
Defaults to False. | |
-- | |
reference (list) | |
Checks production total against expected range. | |
List is in form [reference value, low %, high %]. | |
If the total generation is outside this range the datapoint will be invalidated. | |
For example [12000, 0.1, 1.4] | |
Defaults to None. | |
""" | |
missing = kwargs.pop('missing', []) | |
allow_negative = kwargs.pop('allow_negative', False) | |
reference = kwargs.pop('reference', None) | |
if kwargs: | |
raise TypeError('Unexpected **kwargs: %r' % kwargs) | |
generation = data['production'] | |
if missing: | |
for item in missing: | |
if generation[item] == None: | |
data['production'] = None | |
return data | |
if allow_negative == True: | |
pass | |
else: | |
for k,v in generation.iteritems(): | |
if v < 0.0: | |
generation[k] = None | |
if reference: | |
low = reference[0]*reference[1] | |
high = reference[0]*reference[2] | |
total = sum(v for k, v in generation.iteritems() if v != None) | |
if low <= total <= high: | |
pass | |
else: | |
generation = None | |
data['production'] = generation | |
return data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment