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
-
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 '')