Forum Archive

Editorial Reminders

dendrites

Hello, I see that there’s a Reminders module in Editorial now and I’ve also found and tried the 15 minutes Remind Me workflow.

For us non-programmers, is there/will there be a workflow that parses Taskmator-style Taskpaper entries, e.g. @alarm(2015-12-07, 20:00) and passes them over to Reminders.app? :)

Cethric

Assuming the alarm format is:
name of alarm @alarm(yyyy-mm-dd, HH:MM)
Then create a new workflow with the two items.
First One is the get document text element
Second one is to run the python code:

#coding: utf-8
import re
import editor
import dialogs
import datetime
import workflow
import reminders

action_in = workflow.get_input()
for line in action_in.split('\n'):
    for name, s_time in re.findall(r'(.*)@alarm\((.*)\)', line):
        date, time = s_time.split(', ')
        d_yyyy, d_mm, d_dd = [int(x) for x in date.split('-')]
        t_hh, t_mm = [int(x) for x in time.split(':')]
        rem = reminders.Reminder()
        rem.title = name
        due = datetime.datetime(d_yyyy, d_mm, d_dd, t_hh, t_mm)
        rem.due_date = due
        a = reminders.Alarm()
        a.date = due
        rem.alarms = [a]
        try:
            res = dialogs.alert(
                                                            'The Reminder Was Set',
                                                            'Name: {name}\n{date} {time}'.format(
                                                                                                                                                    name=name,
                                                                                                                                                    date=date,
                                                                                                                                                    time=time),
                                                            'Ok')
            rem.save()
        except KeyboardInterrupt:
            print "User Cancled Input"


action_out = action_in

workflow.set_output(action_out)

Workflow can be found here

dendrites

Wow, thank you so much! :D Works like a charm.

Phuket2

@Cethric , sorry I am going to ask if this a better approach without knowing anything about Editorial. But I would have thought better to break your dissecting of the alarm code into a function. Just so it's clearer as well as for re usability. But I have no idea of the limitations in workflows in Editorial.

def extract_alarm_info(alarm_text):
    for name, s_time in re.findall(r'(.*)@alarm\((.*)\)', alarm_text):
        date, time = s_time.split(', ') 
        d_yyyy, d_mm, d_dd = [int(x) for x in date.split('-')]
        t_hh, t_mm = [int(x) for x in time.split(':')]

    return {
                'year'      : d_yyyy,
                'month'     : d_mm,
                'day'           : d_dd,
                'hour'      : t_hh,
                'min'           : t_mm,
                'date_str'  : date,
                'time_str'  : s_time,
            }

the_alarm = '@alarm(2015-12-10, 22:05)'
print extract_alarm_info(the_alarm)
ccc

The standard library provides you three different versions of the string parsing function strptime().

import datetime
the_alarm = '@alarm(2015-12-10, 22:05)'
alarm_datetime = datetime.datetime.strptime(the_alarm, '@alarm(%Y-%m-%d, %H:%M)')
print(alarm_datetime, str(alarm_datetime))
# (datetime.datetime(2015, 12, 10, 22, 5), '2015-12-10 22:05:00')
Cethric

@Phuket2 in most situations yes it would however it was just an extra layer of unnecessary code to the task required.
@ccc thank for pointing that out. I had looked into using the datetime module in that way but did not realise it would work like that so ended up doing it how I did.

Phuket2

@Cethric , understand. As I say, I have not done a workflow before. Normally I am not seeing Editorial posts, but now as I am viewing the unread in the Forum I see them. I was also thinking it would be useful for me to learn editorial. I use it like a scratch pad when writing longer posts. However, I have a few friends that are writers, which Editorial might be a better solution from what they use now.
I was looking for a sub to using re, only because re is still black magic to me. All the posts I answers I found on stackflow also using re.

ccc

"Some people, when confronted with a problem, think
“I know, I'll use regular expressions.” Now they have two problems.
" http://regex.info/blog/2006-09-15/247

"Regular expressions - particularly non trivial ones - are difficult to code, understand and maintain. You only have to look at the number of questions on Stack Overflow tagged [regex] where the questioner has assumed that the answer is a regex and has got stuck. In a lot of cases the problem can (and perhaps should) be solved a different way.". http://programmers.stackexchange.com/questions/223634/what-is-meant-by-now-you-have-two-problems

ccc

I was listening to a podcast today where someone was raving about dateutil's ability to parse datetimes without format string. Given that dateutil is one of the Pythonista extra modules, I thought I would give it a whirl...

import dateutil  # a builtin in the current Beta

# returns the substring that is inside of two delimiters
def str_inside(s, delimiters='()'):  # 'this (is a) test.' --> 'is a'
    return (s.partition(delimiters[0])[2] or s).partition(delimiters[1])[0]

the_alarm = '@alarm(2015-12-10, 22:05)'
alarm_datetime = dateutil.parser.parse(str_inside(the_alarm))
print(alarm_datetime, str(alarm_datetime))

dateutil.parser.parse() does not require a format string and is much better than strptime() at figuring out what datetime the user meant:

for s in '2/13/70', '8:00 on 13 Feb 70', "8am on 13th Feb 1970", 'July 4th', '7:30am', '7:30pm', 'Monday':
    print(dateutil.parser.parse(s), s)
MartinPacker

@ccc What podcast series would give you that?