zilogic-systems/yamlcalc

If statement

Opened this issue · 4 comments

@bravegnu
Can i apply if statement in this yamlcalc ? any ideas for implementing.

I have come to know that yaml is just a data serialisation language therefore if /else is not supported.

See link http://stackoverflow.com/questions/23934213/conditional-check-in-yaml-file-to-show-the-proper-content
One way is to write a simple python module along with pyGTK extension and load the yaml file .

Any other inputs/ideas would be much helpful. thanks

I am not sure what your use case is, but you can use the Conditional Expressions (AKA Ternary Operator) https://docs.python.org/2/reference/expressions.html#conditional-expressions HTH

Thanks a lot . Actually i am bit confused with their following syntax in the above given link
Condition_expression = or_test ["if" or_test "else" expression] (syntax 1)

However, following this link https://docs.python.org/2.5/whatsnew/pep-308.html , the below syntax seems to be fine
x = (true_value if condition else false_value) (syntax 2)
though the bracket is not necessary.

If you could enlighten me on syntax 1, it will be much better.

I have worked out the syntax 2 and it does not seem to work . I found one external yaml pack which might holds the solution.
http://search.cpan.org/~mschilli/YAML-Logic-0.04/Logic.pm#Other_Comparators

update - it seems to work ! after correcting the self. however else is removed or for multiple conditions it still seems to generate the error.
This is my sample problem i am working on. developing a simple yaml file for alkalinity calculation
Stan:
VNa2CO3: 25
NNa2CO3: 0.01639
VH2SO4: 18
NH2SO4: = (self.VNa2CO3 * self.NNa2CO3) / self.VH2SO4
A: 0.5
B: 4.5
Vsample: 25
TA: =self.A + self.B
TotAlk: = (self.TA * self.NH2SO4 * 50000 )/ self.Vsample
PhenAlk: = (self.A * self.NH2SO4 * 50000) / self.Vsample
CarbALK: = 0 if self.PhenAlk < self.TotAlk else 1