Reading Text from a text file

If you have a question or need help, this is the place to be.
User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako » Tue Jan 12, 2016 4:11 pm

So maybe it is a problem between my chair and the keyboard :) .
You're right, according to ISO8601, now we have the second week of 2016.
I wrote "This week is the third in 2016", but in fact I had in mind "This week is the third in January". That's the difference.
I perhaps misunderstood your request and it follows that the program might work differently than you expect.
I have a fundamental question:
How are numbered weeks in February in your diary?
I had understood your request so that the weeks in every month are numbered starting from the first.
So, (in its current form) the program will work only if in the folder "February" will subfolders "Week 1" to "Week 5".
But you might have something else in mind. You might want to have in the folder "February" subfolders "Week 5" to "Week 9".
So maybe I'll be forced to redo the program.

Pako
You know flattr ? You can Image

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Tue Jan 12, 2016 10:58 pm

Hello Pako
Good evening, presently I did each month with sub folders week 1 to 4 January week 1 ,2,3,4
But I think making it read starting from week 1 ending on week 52 , would be much more straight forward, similar to the calendar i mention earlier.
Which ever of these method fits with your script,is just fine,but because this year a leap year, it maybe better idea to name the weeks from 1 to 52.
Thank you
Cheers

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako » Wed Jan 13, 2016 5:26 am

Very well.
I agree that the use of numbering weeks according to ISO 8601 is a much cleaner solution, and I'll do it. It is not difficult, and it is a relatively small intervention in the code. I'll do it today, when I have some time.

Edit:
Now, I realize that it is not so simple as it gets.
Some days the last week of the year abound until next year, hence the need to change the algorithm.

Pako
You know flattr ? You can Image

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Wed Jan 13, 2016 10:26 am

Hello
I too think that week 1-52 will be most suited for this project , thanks , did your last script include a repeat function or ,will a scheduler function needed ,what I men't is if there is a mention time in the file for example there is a 10:30 am Doctors appointment ,eventghost starts up at 8:30 am it would remind of the 10:30 , but when that 10:30 time is lapsed then you don't want to hear that again for the rest of day .
OK this may not be clear so let's give an instant my appointment was 10:30 which I kept because I was reminded earlier in the morning ,I had closed eventghost , at 15:30 pm I return home and restart eventghost I don't need to be told of an appointment which had already gone , is this possible in your present script or schedulghost had to be added
Thank you
Cheers

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako » Wed Jan 13, 2016 12:42 pm

Code: Select all

diary_root = "C:"
code_page = eg.systemEncoding

from datetime import date as date, datetime as dt
from os.path import isfile
import codecs
import re

WEEK = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
YEAR = (
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
)
ptrn = '((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|pm))?)'
rg = re.compile(ptrn,re.IGNORECASE|re.DOTALL)
am = {'12':'00','01':'01','02':'02','03':'03','04':'04','05':'05','06':'06',
	      '07':'07','08':'08','09':'09','10':'10','11':'11'}
pm = {'12':'12','01':'13','02':'14','03':'15','04':'16','05':'17','06':'18',
	      '07':'19','08':'20','09':'21','10':'22','11':'23'}
def test(txt):
    m = rg.search(txt)
    if m:
        time1 = m.group(1)
    else:
        return (True, "No timestamp found !")
    t_lower = time1.strip().lower()
    a = time1.split(':')
    res = ""
    hr = a[0].zfill(2)
    if t_lower.endswith("am"):
        if hr in am:
            res = am[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    elif t_lower.endswith("pm"):
        if hr in pm:
            res = pm[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    if res == "":
        #res = time1
        return (True, time1 + " (wrong format)")     # When an error occurs, it is preferable to return True
    if len(a) == 3:
        res += ":" + a[2][:2]
    else:
        res += ":00"
    now = dt.now()
    ts = dt.now()
    slots = res.split(":")
    ts = ts.replace(hour = int(slots[0]),minute = int(slots[1]),second = int(slots[2]))
    return (now < ts, ts)

today = date.today()
iso = today.isocalendar()
month = 12 if today.year > iso[0] else today.month
today_file = r"%s\%i Diary\%s\Week %i\%s.txt" % (diary_root, iso[0], YEAR[month-1], iso[1], WEEK[iso[2]-1])

if not isfile(today_file):
    eg.PrintError('File "%s" not found !' % today_file)
    eg.Exit()
eg.PrintNotice("File valid for today: " + today_file)

input = codecs.open(today_file, 'r', code_page, 'ignore')
data = input.readlines()
input.close()
speakData = ""
for line in data:
    flag, ts = test(line)
    if flag:
        print "Upcoming event: ",str(ts)
        speakData += line
    else:
        print "Missed event:   ",str(ts)
if speakData:
    eg.TriggerEvent("Speak", prefix = "Diary", payload = speakData)
I did, but I'm not too excited about.
For me, such a calendar is rather confusing.
Take for example the day January 1, 2017.
It is Sunday and according to ISO calendars belong to the 52nd week of 2016.
It follows that the plan for this Sunday has to be written to the file "C:\2016 Diary\December\Week 52\Sunday.txt" !!!
It can not be the file "C:\2017 Diary\December\Week 52\Sunday.txt" because it is Sunday 31 December 2017.
If I wanted to use this "talking diary," I would definitely stay with the status quo, where weeks are numbered separately each month (for example, in January 2016, there are weeks 1-5). For me it would be much clearer.
But I'm certainly not going to use (one of the reasons is that the Czech Windows can speak English only).
allalone747 wrote:... did your last script include a repeat function or ,will a scheduler function needed ...
No, repeater feature is not built.
You must run the script (for example using Schedulghost) whenever you need it.
The function is exactly as I described it on Monday, 11th of January.
This happens every time you run the script.

Pako
You know flattr ? You can Image

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Wed Jan 13, 2016 9:05 pm

Hello Pako
Good evening, thanks for your valuable and your great knowledge with scripting and also your good advice helping putting this talking diary That should be the Name of this Project.
Looking on that Calendar with numbered weeks,1-52 what i think when editing weeks of each month ,it should begin with Monday therefore , the first full week in February would be the 01/02/2016, the fifth week in February would begin on the 29/02/2016 the first full week in March would begin on the 07/03/2016, therefore the first full week in December 2016 would be on the 05/12/2016 and the last week in December would begin on the 26/12/2016.
This is how i would edit each month base on 52 weeks method

week 9 would begin on the 29/02/2016
week 10 would start on 07/03/2016
therefore week 52 would start on the 26/12/2016

Your script send on the 11 January works good only reading week 3 when it should be week 2 is this one a fix/ update or is it base on the 52 week method
Thanks
Cheers

User avatar
Pako
Plugin Developer
Posts: 2294
Joined: Sat Nov 11, 2006 1:31 pm
Location: Czech Republic
Contact:

Re: Reading Text from a text file

Post by Pako » Thu Jan 14, 2016 7:48 am

The new script works as well as it is described in the post dated 11 January, in part, "It works as follows:". This happens every time you run the script.
There, nothing is said about the numbering of weeks.
In the new script, only the numbering of weeks is changed (this corresponds to your description - "52 weeks method").
Pako wrote:It follows that the plan for this Sunday has to be written to the file "C:\2016 Diary\December\Week 52\Sunday.txt" !!!
It can not be the file "C:\2017 Diary\December\Week 52\Sunday.txt" because it is Sunday 31 December 2017.
But I think I made a mistake after all.
I think plan for Sunday 1. January 2017 properly should be entered in the file "C:\2017 Diary\January\Week 52\Sunday.txt". This is definitely better than "C:\2016 Diary\December\Week 52\Sunday.txt" (less confusing).
Here's a modified script that works this way:

Code: Select all

diary_root = "C:"
code_page = eg.systemEncoding

from datetime import date as date, datetime as dt
from os.path import isfile
import codecs
import re

WEEK = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
YEAR = (
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
)
ptrn = '((?:(?:[0-1][0-9])|(?:[2][0-3])|(?:[0-9])):(?:[0-5][0-9])(?::[0-5][0-9])?(?:\\s?(?:am|pm))?)'
rg = re.compile(ptrn,re.IGNORECASE|re.DOTALL)
am = {'12':'00','01':'01','02':'02','03':'03','04':'04','05':'05','06':'06',
	      '07':'07','08':'08','09':'09','10':'10','11':'11'}
pm = {'12':'12','01':'13','02':'14','03':'15','04':'16','05':'17','06':'18',
	      '07':'19','08':'20','09':'21','10':'22','11':'23'}
def test(txt):
    m = rg.search(txt)
    if m:
        time1 = m.group(1)
    else:
        return (True, "No timestamp found !")
    t_lower = time1.strip().lower()
    a = time1.split(':')
    res = ""
    hr = a[0].zfill(2)
    if t_lower.endswith("am"):
        if hr in am:
            res = am[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    elif t_lower.endswith("pm"):
        if hr in pm:
            res = pm[hr] + ":" + a[1][:2]
        else:
            return (True, time1 + " (wrong format)") # When an error occurs, it is preferable to return True
    if res == "":
        #res = time1
        return (True, time1 + " (wrong format)")     # When an error occurs, it is preferable to return True
    if len(a) == 3:
        res += ":" + a[2][:2]
    else:
        res += ":00"
    now = dt.now()
    ts = dt.now()
    slots = res.split(":")
    ts = ts.replace(hour = int(slots[0]),minute = int(slots[1]),second = int(slots[2]))
    return (now < ts, ts)

today = date.today()
#today = date(2017, 1, 1) ##FOR TESTING (comment line 63)
iso = today.isocalendar()
today_file = r"%s\%i Diary\%s\Week %i\%s.txt" % (diary_root, today.year, YEAR[today.month-1], iso[1], WEEK[iso[2]-1])

if not isfile(today_file):
    eg.PrintError('File "%s" not found !' % today_file)
    eg.Exit()
eg.PrintNotice("File valid for today: " + today_file)

input = codecs.open(today_file, 'r', code_page, 'ignore')
data = input.readlines()
input.close()
speakData = ""
for line in data:
    flag, ts = test(line)
    if flag:
        print "Upcoming event: ",str(ts)
        speakData += line
    else:
        print "Missed event:   ",str(ts)
if speakData:
    eg.TriggerEvent("Speak", prefix = "Diary", payload = speakData)
Pako
You know flattr ? You can Image

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Thu Jan 14, 2016 11:29 am

Hello Pako
A very good day to you, thank you for explaining things to me , i have two options script to try out, therefore i will create two xml files and use each of your script,to see the different methods, brilliant work
Cheers, I will keep you informed, and hope that others love it they will use it
Thank you Pako you are the one, i will keep you informed :)
Cheers

shaggy79
Experienced User
Posts: 129
Joined: Sun Jul 13, 2014 4:57 pm

Re: Reading Text from a text file

Post by shaggy79 » Wed May 25, 2016 3:10 am

Hi all, 8)
I have followed the tutorial for the "Talking Diary".
viewtopic.php?f=13&t=8046&p=38990&hilit=Diary#p38990

I am a bit confused,
I have set it up as in the tutorial but my diary seems to be a week behind :?: .......
It is now 25/05/2016 which is Week 22. But my talking diary is saying it's week 21 :?: I have no idea what I have done wrong :oops:
Any help would be great.
Thanks,
Paul.

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Wed May 25, 2016 7:11 am

shaggy79 wrote:Hi all, 8)
I have followed the tutorial for the "Talking Diary".
viewtopic.php?f=13&t=8046&p=38990&hilit=Diary#p38990

I am a bit confused,
I have set it up as in the tutorial but my diary seems to be a week behind :?: .......
It is now 25/05/2016 which is Week 22. But my talking diary is saying it's week 21 :?: I have no idea what I have done wrong :oops:
Any help would be great.
Thanks,
Paul.

Hi there , remember that you are doing each week that is week 1 to week 52 , have a look at today's date in May you see that the week commencing on the 30th but ends in June therefore in MAY week commencing 2/5/2016 should be week 19 and so on week commencing 23 /5/2016 should be week 22
WEEK 18 starts in APRIL BUT FINISH IN MAY
I hope this help ; it's good to see some one trying out the tutorial ; put your questions in the tutorial section ; we need more interest on the good work from our more skilled script and programming friends on the forum NOT MYSELF I am learning from them
Cheers man

shaggy79
Experienced User
Posts: 129
Joined: Sun Jul 13, 2014 4:57 pm

Re: Reading Text from a text file

Post by shaggy79 » Wed May 25, 2016 4:21 pm

Thanks for the reply allalone747 8)

Now I'm even more confused lol :oops:

I sort of understand what you are saying :shock:
week 22 started on Monday 23/5/16 but because of the overlapping months it says week 21 :?:

I have also copied my Question to the "Talking Diary" tutorial as any answers may help others in the future..

One last thing.....
I have added a screen shot of my Diary
2016 Diary.png
Left hand side is in the 'MAY' folder, Right hand side is in 'Week 23' folder as you can see the days on the right hand side are mixed up.
Is there away to sort it,
Like this...:?:
  • Monday.txt
    Tuesday.txt
    Wednesday.txt
    Thursday.txt
    Friday.txt
    Saturday.txt
    Sunday.txt

allalone747
Experienced User
Posts: 81
Joined: Sun May 03, 2015 2:17 pm

Re: Reading Text from a text file

Post by allalone747 » Wed May 25, 2016 5:24 pm

shaggy79 wrote:Thanks for the reply allalone747 8)

Now I'm even more confused lol :oops:

I sort of understand what you are saying :shock:
week 22 started on Monday 23/5/16 but because of the overlapping months it says week 21 :?:

I have also copied my Question to the "Talking Diary" tutorial as any answers may help others in the future..

One last thing.....
I have added a screen shot of my Diary
2016 Diary.png
Left hand side is in the 'MAY' folder, Right hand side is in 'Week 23' folder as you can see the days on the right hand side are mixed up.
Is there away to sort it,
Like this...:?:
  • Monday.txt
    Tuesday.txt
    Wednesday.txt
    Thursday.txt
    Friday.txt
    Saturday.txt
    Sunday.txt

have a look at this calender see how the weeks are entered, do the same and it should work

http://week-number.net/calendar-with-we ... -2016.html

shaggy79
Experienced User
Posts: 129
Joined: Sun Jul 13, 2014 4:57 pm

Re: Reading Text from a text file

Post by shaggy79 » Wed May 25, 2016 8:03 pm

Thanks allalone747,
I get it now, :idea:
I have a 'Year Planner' on my wall (A Big calendar like in offices).
It also has week numbers on it but in a different way to the calendar you posted, so it gets a bit confusing using both of them as me & my family use the one on my wall I might just stick to that one :cry:

Post Reply