Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello β I'm @olemoritz on Twitter.
Conditional if then, with math, less than, greater than
-
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)
This first line is subtracting a later datetime.date from an earlier datetime.date which results in a positive datetime.timedelta.
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.
http://www.editorial-workflows.com/workflow/5821858704261120/n5P57YobUeM
And
http://www.editorial-workflows.com/workflow/5880523125686272/mqcIZrzeGlE
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.splitThis works because the first line of your script converts the external variable DOB into the internal variable action_in. And because
datetime - datetime --> timedelta
whiledatetime - 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. -
I feel like I am learning Greek, but only to say... How old am I? To a computer, that doesn't natively speak Greek (I think my analogy breaks down somewhere, π). Maybe I mean learning Greek by talking to a computer that speaks Japanese and then tries to translate it back into Greek, which I don't really speak. (I give up on coming up with a proper analogy for right now)
I think I'm just putting things in the wrong places... I keep looking back at the two examples that you wrote that obviously work... To see if I can emulate that and find out where I'm going wrong, but not having much luck. (I did read through the link on string split syntax... But again, Greek?)
#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(int(year.strip()), int(month.strip()), int(day.strip())) workflow.set_variable('age') = int(delta_t.days / 365.2425) action_out = workflow.set_output('age')
-
Okay, the last two lines are the issue here, I think. I'll start with the second-to-last one:
workflow.set_variable('age') = int(delta_t.days / 365.2425)
That is not valid Python (you cannot assign something to a function call). I think what you're trying to do is:
workflow.set_variable('age', int(delta_t.days / 365.2425))
The function
workflow.set_variable
takes two arguments. The first one is the name of the variable, and the second one is what you want to put into the variable.And the last line:
action_out = workflow.set_output('age')
While that is valid Python, it probably doesn't do what you want. First of all,
workflow.set_output
doesn't return a result, so there's no need to assign it toaction_out
. You can simply write:workflow.set_output('age')
But that also doesn't do what you're expecting - it sets the text "age" as the workflow output, not what's stored in the workflow variable named "age". I think you're looking for this:
workflow.set_output(workflow.get_variable('age'))
This gets the content of the "age" workflow variable, and sets it as the output of the action.
-
#coding: utf-8 import datetime import workflow ymd = workflow.get_variable('DOB') year, month, day = ymd.split(',') delta_t = datetime.date.today() - datetime.date(int(year.strip()), int(month.strip()), int(day.strip())) workflow.set_variable('age', int(delta_t.days / 365.2425)) workflow.set_output(workflow.get_input()) # passthru
The last line just takes the text passed into this step and passes it back out unmodified to the next step. The real input / output is reading in the ymd string from 'DOB' and setting 'age' from that. Passing thru the text means that you can put the age calculation step in the middle of workflow without breaking the text that is flowing through it.
My strong recommendation is that you open up the Python console in Editorial, Pythonista, or even by typing
python
into the terminal program on your computer. You can then go into interactive mode:>>> ' a b c '.strip() >>> ' a b c '.split() >>> ' a b c '.partition('b') >>> ' a b c '.upper() >>> ' a b c '.strip().upper().split() >>> type(' a b c ') >>> type(' a b c '.split()) >>> int(' a b c ') >>> int(' 1') >>> int('1')
Going through https://docs.python.org/3/library/stdtypes.html#string-methods is a great way to start. It will build your confidence in a risk-free space.
-
I finally made it work. You both will understand better than I. (Have I mentioned I have literally nearly zero experience coding... outside of things like Editorial and the Workflow app and copy and pasting some HTML stuff).
That said. Someone throw a party!
#coding: utf-8 import datetime import workflow ymd = workflow.get_variable('DOB') year, month, day = ymd.split(',') delta_t = datetime.date.today() - datetime.date(int(year.strip()), int(month.strip()), int(day.strip())) age = int(delta_t.days / 365.2425) workflow.set_variable('age of client', str(age)) workflow.set_output(str(age))
-
πΎ π€ΈββοΈ π
-
You could make your script do double duty by adding the line:
workflow.set_variable('minor', 'minor' if age < 18 else '')