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