from fito import as_operation, SpecField, PrimitiveField, Operation
from time import sleep

class DatabaseConnection(Operation):
    host = PrimitiveField(pos=0)
    
    def __repr__(self): return "connection(db://{})".format(self.host)

@as_operation(database=SpecField, experiment_config=SpecField)
def run_experiment(database, experiment_config):
    sleep(0.25)

@as_operation()
def experiment_1(alpha=0.5, beta=10):
    return alpha + beta / 2
    

"""
Explanation: The flexibility that can be seen below can be exploited on these contexts:

Running some experiments from config files in yaml/json
Automatize them and use the json representation to send them as messages
Attach metadata to the experiment (use data stores)
End of explanation
"""


run = run_experiment(DatabaseConnection('localhost'), experiment_1(alpha=10))
print run

"""
Explanation: It's __repr__ is descent
End of explanation
"""


print run.json.dumps()

"""
Explanation: Json serializable:
End of explanation
"""


print run.yaml.dumps()

"""
Explanation: My beloved yaml-serializable
End of explanation
"""


print Operation.from_yaml(run.yaml.dumps()).yaml.dumps()

"""
Explanation: And of course, parseable :)
End of explanation
"""


