Forum Archive

Help a beginner

Jozh

Can someone help my change this code. I would like to have the birthday list be set as a variable so I can use it later in a workflow. Also would like to be able to set the number of upcoming birthdays that the script outputs. Thanks

import contacts
from datetime import datetime
import operator

days_list = []
people = contacts.get_all_people()
now = datetime.now()
for p in people:
  b = p.birthday
  if b:
    next_birthday = datetime(now.year, b.month, b.day)
    if next_birthday < now:
      next_birthday = datetime(now.year + 1, b.month, b.day)
    days = (next_birthday - now).days
    days_list.append({'name': p.full_name, 'days': days})

if not days_list:
  print 'You don\'t have any birthdays in your address book.'
else:
  days_list.sort(key=operator.itemgetter('days'))
  print 'Upcoming Birthdays'
  print '=' * 40
  for item in days_list:
    print '* %s in %i days' % (item['name'], item['days'])

Discussion started with RV: Forums New Discussion workflow

zrzka

Two options ...

Variable

Add Set Variable action before you run your script and set it to empty value. Just to have this variable. And in your Python script do this:

import workflow
...
workflow.set_variable('my_var_name', 'my_var_value')

Without Set Variable action you're not able to set variable value from within your Python script. In other words, before you set it, it must exists.

Output

You can pass your value as an output in this way.

import workflow
...
workflow.set_output('my_var_value')

String

When you check set_output docs, you must pass str there. If you want to pass dict, list, ... or whatever, you can use json for example.

import workflow
import json

days_list = [ ... ]
workflow.set_output(json.dumps(days_list))

And later, in other script, you can get your list back with.

import workflow
import json

days_list = json.loads(workflow.get_input())
... or ...
days_list = json.loads(workflow.get_variable('my_var_name'))

But it really depends on what your goal is, workflow structure, ... These are just general comments how to ...

Jozh

Thanks for the help