Conditional if then, with math, less than, greater than
Is there a simple way to use conditional statements with less than, greater than in editorial Itself? It's listed in the "math" documentation, but states that it's not supported... (It will do a text based "equal to", but I can't figure out if there is even away to list multiple values in the if/then dialog)
I think I'd have to do it in Python... Which is outside of my skillset to at the moment. I've checked in the workflows directory and didn't find anything there either. Ideas?
I did some playing around, and strangely there doesn't seem to be an easy way to compare numbers. To check whether two numbers are equal (in value, not an exact text match) you can pass each number into a "Math" action that just outputs the number (as far as I can tell, this removes whitespace and extra zeroes), and compare the results. That way you can be sure that two numbers with the same value compare equal even if they are written differently in text form.
As for relational operators (less than, greater than, etc.), if you try to use them in a "Math" action, it shows a custom error message saying that "< and > are not supported operators", which is a bit strange since they are listed in the documentation... The
absfunction exists though, so you can emulate the relational operators the hard way -
num1 > num2, and
num1 < num2. (If
num1 == num2, you get a division by zero and the result is
NaN.) This isn't exactly simple though.
Python feels a bit overkill for comparing some numbers, but it seems to be the only reasonable solution here. At least it has the advantage that you can easily write conditional blocks with multiple alternatives, an
elsechain is much shorter than multiple "conditional block" actions.
I think you're, right... I understand the idea behind if...elif...else, but, well... this is a little more complicated than I'm thinking I'm ready for.
I was actually just trying to look through different Python documentation to see if I could learn just a little code to make this possible. (I currently know Ø code. Lots about therapy and counseling and nothing about coding.)
In this case I want to compare clients age to 18, to have it determine if client is a minor to include a statement about the parents or guardians. I could write 14 conditional blocks... To include the range of clients I might see. But yes. Overkill.
Hm, in that case I think you can use the condition "8 9 10 11 12 13 14 15 16 17" "contains" "<age>" (where <age> is the variable containing the age). This is not very reliable, for example "2 1" is detected as under 18, and " 7" (with a space in front) is not. You might need to add a dummy "math" action to "clean up" the age number first.
You could also change the condition to "<age>" "matches regular expression" "
^([0-9]|1[0-7])$" - that should be a bit more reliable, but you still need a dummy "math" action in front to get rid of whitespace and leading zeroes.
What about a simple
Is A Minorworkflow step that looks at the
agevariable returns 'true', 'false', or 'unknown' ?
The abs equation worked. I set up three if then workflows.... One for 1, one for -1, and one for error. It worked fine.
Is there a way to put multiple values in the if then workflow itself? I tried that with commas, semicolons, backslash a in between... I tried using contains... It didn't seem to have a way to evaluate multiple text valjesy, but needed a separate workflows for each. Fun times.
Thanks so much for your help! I'd spent quite a few hours trying to figure this out on my own.
@NikkiSchwartzVB Do you mean something like "If [value] is A, B, or C, do this"? Or rather, "if [value] is A, do this, if value is B, do that..."?
I was trying to figure out a similar way to automatically adjust grammar for "an 8/18/80 year old" vs everyone else "a 3/5/12/99 year old"
One possible solution seemed to be, if age is 8; 18; 80; 81; 82... use an
If age is not 8; 18; 80... use a
I tried to use if age contains "8" at all, but that didn't work like I wanted anyways for 18 and if it had, it would've been a problem for clients aged 28, 38, 48, etc.
And as long as we're on the subject... I haven't figured out how to use conditional statements in the math workflow action... I've searched, it doesn't seem possible at this point?
@dgelessus that's clever. I didn't think of doing it in the reverse.
Could you separate them by semicolon?
You can use any character you want as the separator, but you need to keep in mind that it's still just a text search. For example, if you write "8;18;80" "contains" "<age>", the condition will also detect age 1 (because the text "1" is contained in the text "18"). The semicolon means nothing special to the "contains" condition.
This is another case where a regular expression match is more reliable.
^(8|18|8[0-9])$should match 8, 18, and anything from 80 to 89.
@ccc that works great! I wish that I had the time to dedicate to learning python. I've been looking at the age from birthdate script you wrote into your example of a note from python and trying to figure out how to pull that out... but all of the necessary pieces seem unclear to me.
From http://www.editorial-workflows.com/workflow/5821858704261120/n5P57YobUeM we get the two lines:
delta_t = datetime.date.today() - patient_info['birth_date'] patient_info['age'] = int(delta_t.days / 365.2425)
The second line is taking the timedelta's number of days between the dates and dividing it by the number of days in a year which results in the number of years between the dates (i.e. age in years). With leap days and leap seconds, the number of days in a year works out to be ~365.2425. We convert all of that into an integer because someone who is 17.925 years old is a minor while someone who is 18.000001 is not.
I hope that helps.
@ccc that is fantastic, I just saw this... after I mentioned in a different thread. Sorry for the redundancy.
I saw this in the script, which makes sense... I think the problem is that I don't understand python well enough to know how to make it produce output back to editorial workflows. (Which is obviously not your problem, right ;)
To clarify, I don't mind asking for help... but I don't want to be that chick who shows up and says, hey guys, can you build me x, y, and z, because I'm so much busier and more important than you to learn it myself. Thanks peace.
Ya know? Don't want to be that.
I tried combining these two lines (before you answered) with the pieces of the script in Am I a Minor... but being rather ignorant Of python... it didn't work.
Hmm... just found the editorial and python documentation... how to use them together. Maybe I can make this work.
I lied. Not working. :(
Don't worry, you are NOT being the annoying "write all the code for me" person. :) You're clearly putting your own effort into this, and that's what matters. There's absolutely nothing wrong with being new to programming and asking questions, all of us have been there at some point.
If you are getting errors from your Python code, could you copy and paste the code into your post, and also include the error message? It's not always possible to find the problem based on a description of the code, especially with syntax errors. (You could also upload your workflow, that will automatically include the Python code. But that might not be an option if you have personal data in there that you don't want to post in public.)
Here's what I tried, I'm know I'm hopelessly lost.
#coding: utf-8 import datetime import workflow action_in = workflow.get_variable('DOB') delta_t = datetime.date.today() - int(['DOB']) workflow.set_variable('age') = int(delta_t.days / 365.2425) action_out = workflow.set_output('age')
I'm essentially trying to combine two short scripts that @ccc wrote.
Here's what I'm trying to do...
As I'm filling out the difference to variables for this report, I want it to self-populate and automate as much as possible. I do these for every client.
I want to put in the client's Birthdate while running a workflow... Then have python calculate age in the background and spit that back out as a variable.
I'm just not making sense of all the pieces that need to go into the python script, itself. I understand the math behind getting the age, just not how to put it all in and bake a cake with it.
Awesome... A few minor changes and I believe that we will have Nikki's first Python action!
Just put in the line:
year, month, day = action_in.split(',') # and then change: int(['DOB']) # to datetime.date(int(year.strip()), int(month.strip()), int(day.strip()))
str.split()is described in https://docs.python.org/3/library/stdtypes.html#str.split
This works because the first line of your script converts the external variable DOB into the internal variable action_in. And because
datetime - datetime --> timedeltawhile
datetime - int --> TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'.
The third line of your script converts an internal value into the external variable age.
I do believe that you were born to code...
Here's what I tried. No Bueno.
#coding: utf-8 import datetime import workflow action_in = workflow.get_variable('DOB') year, month, day = action_in.split(',') delta_t = datetime.date.today() - datetime.date(year, month, day) workflow.set_variable('age') = int(delta_t.days / 365.2425) action_out = workflow.set_output('age')```
Sorry. We need to convert the strings year, month, and day into ints. We do this with the syntax
int(year.strip()). See the post above.