PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > errors, trying to edit and write some text files
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
errors, trying to edit and write some text files

Réponse
 
LinkBack Outils de la discussion
Vieux 15/09/2007, 15h07   #1 (permalink)
Nathan Thickett-menghini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut errors, trying to edit and write some text files

Hi, I'm a total ruby noob but i've obtained this script from another
forum over at Tracks. I'm trying to extract some info from a plain text
feed of my upcoming calendar events and write specific info to three
different text files (today, tomorrow, and next 7 days). I'm on a Mac.

The script is here: I've already made edits specific to my computer (the
address of my text feed for example):

#!/usr/bin/ruby
#ÂGenerateÂ3ÂseperateÂtextÂfilesÂfromÂTrack sÂTXTÂfeeds
#ÂlistingÂactionsÂdueÂtodayÂ(orÂoverdue),Ât omorrowÂor
#ÂinÂtheÂnextÂ7Âdays.
#ÂYouÂcanÂthenÂpullÂtheseÂtextÂfilesÂinto GeekToolÂandÂcolour
#ÂthemÂseparately.

#Â========ÂConstantsÂtoÂsetÂwithÂyourÂvalue sÂ===============
#
#ÂURLÂforÂtheÂbaseÂfeed:Âhttp://yourdomain.tld/feed/text/[token]
URLÂ=Â"http://0.0.0.0:3000/feed/text/Nathan/b0b2a7c81680d1a1920b199ecb6db0ad86e2e984"
#ÂPathÂtoÂtemporaryÂdirectory
TEMP_DIRÂ=Â"/Users/"
#
#Â=============================================
#ÂGrabÂeachÂfeed,ÂsplitÂintoÂlines,ÂthenÂg etÂridÂofÂanyÂnon-actionÂlines
todayÂ=Â`curlÂ-sÂ"#{URL}"?due=0`.split("\n").select{|line|Âline Â=~Â/\s+\[Due:/ÂorÂlineÂ=~Â/^\w/}
tomorrowÂ=Â`curlÂ-sÂ"#{URL}"?due=1`.split("\n").select{|line|Âline Â=~Â/\s+\[Due:/ÂorÂlineÂ=~Â/^\w/}
this_weekÂ=Â`curlÂ-sÂ"#{URL}"?due=6`.split("\n").select{|line|Âline Â=~Â/\s+\[Due:/ÂorÂlineÂ=~Â/^\w/}

#ÂGetÂridÂofÂrepeatedÂactionsÂinÂtomorrow andÂthis_week
tomorrowÂ=ÂtomorrowÂ-Âtoday
today_or_tomorrowÂ=ÂtodayÂ+Âtomorrow
this_weekÂ=Âthis_weekÂ-Âtoday_or_tomorrow

#ÂPrintÂtheÂinfoÂtoÂthreeÂfiles,ÂstoredÂin ÂTEMP_DIR
fileÂ=ÂFile.new("#{TEMP_DIR}/today.txt",Â"w")
ÂÂfile.printÂ"\nToday:\n"Â+Âtoday.join("\n")
file.close

fileÂ=ÂFile.new("#{TEMP_DIR}/tomorrow.txt",Â"w")
ÂÂfile.printÂ"\nTomorrow:\n"Â+Âtomorrow.join( "\n")
file.close

fileÂ=ÂFile.new("#{TEMP_DIR}/later.txt",Â"w")
ÂÂfile.printÂ"\nLater:\n"Â+Âthis_week.join("\ n")
file.close

When I run this from BBEdit I get about 50 lines of this:

untitled text:11: Invalid char `\302' in expression
untitled text:11: Invalid char `\240' in expression
untitled text:11: Invalid char `\302' in expression
untitled text:11: Invalid char `\240' in expression
untitled text:13: Invalid char `\302' in expression
untitled text:13: Invalid char `\240' in expression
untitled text:13: Invalid char `\302' in expression
untitled text:13: Invalid char `\240' in expression
untitled text:17: Invalid char `\302' in expression
untitled text:17: Invalid char `\240' in expression
untitled text:17: Invalid char `\302' in expression
untitled text:17: Invalid char `\240' in expression


Any ideas? Sorry if this appears obvious. Many thanks.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 15/09/2007, 16h18   #2 (permalink)
matt neuburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Nathan Thickett-menghini <somerandom_bloke@hotmail.com> wrote:

> When I run this from BBEdit I get about 50 lines of this:
>
> untitled text:11: Invalid char `\302' in expression
> untitled text:11: Invalid char `\240' in expression
> untitled text:11: Invalid char `\302' in expression
> untitled text:11: Invalid char `\240' in expression
> untitled text:13: Invalid char `\302' in expression
> untitled text:13: Invalid char `\240' in expression
> untitled text:13: Invalid char `\302' in expression
> untitled text:13: Invalid char `\240' in expression
> untitled text:17: Invalid char `\302' in expression
> untitled text:17: Invalid char `\240' in expression
> untitled text:17: Invalid char `\302' in expression
> untitled text:17: Invalid char `\240' in expression


Even in my newsreader program I had trouble with your message. The
problem seems to be that something went wrong when you did the cut and
paste from the Tracks forum. Your code is full of invisible characters
between the normal characters. BBEdit should in fact be able to you
track this down and fix the problem. Feel free to email me the BBEdit
file privately and I'll remove the gremlins for you if you don't know
how to do it. m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
  Réponse avec citation
Vieux 17/09/2007, 14h54   #3 (permalink)
Nathan Thickett-menghini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Many thanks, I had some success by typing out the script myself and this
eliminated the troublesome characters. The script now runs and produces
my three text files. I still can't get it to work as desired - it seems
to omit the lines of the calendar that I most need, i.e. the events
themselves! I don't understand enough about the script to be able to fix
it but I think the error comes where the script splits the text feeds
into separate lines - it only seems to include the first line of the
feed and never more. If you fancy taking a look then by all means do but
I appreciate you fixing my first problem.

For example, the feed for 'today' is as follows in its entirety (in the
web browser, before ruby has touched it):


CAR:
[17/09/2007] Car to Garage at 8.30am


But the ruby script returns only:


Today:
CAR:




Any ideas?

Attachments:
http://www.ruby-forum.com/attachment...red_actions.rb

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 17/09/2007, 21h52   #4 (permalink)
matt neuburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Nathan Thickett-menghini <somerandom_bloke@hotmail.com> wrote:

> Many thanks, I had some success by typing out the script myself and this
> eliminated the troublesome characters. The script now runs and produces
> my three text files. I still can't get it to work as desired - it seems
> to omit the lines of the calendar that I most need, i.e. the events
> themselves! I don't understand enough about the script to be able to fix
> it but I think the error comes where the script splits the text feeds
> into separate lines - it only seems to include the first line of the
> feed and never more. If you fancy taking a look then by all means do but
> I appreciate you fixing my first problem.
>
> For example, the feed for 'today' is as follows in its entirety (in the
> web browser, before ruby has touched it):
>
>
> CAR:
> [17/09/2007] Car to Garage at 8.30am
>
>
> But the ruby script returns only:
>
>
> Today:
> CAR:
>
>
>
>
> Any ideas?
>
> Attachments:
> http://www.ruby-forum.com/attachment...red_actions.rb


Well, the script is doing exactly what you're telling it to do. This
expression:

select{|line|line =~ /\s+\[Due:/ or line =~ /^\w/}

....means:

Keep only those lines that begin with the word "[Due:" (possibly after
some whitespace), or that begin with a word character (e.g. "a" or "b"
or "c"...).

Well, the first line does begin with a word character ("C" for "CAR:"),
so it's kept. But the second line does not - it begins with whitespace,
and the whitespace is not followed by the word "[Due:", so it's thrown
away.

So it seems as if the script you've adopted is inappropriate to the
format of the actual data you wish to parse.

m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
  Réponse avec citation
Vieux 17/09/2007, 22h03   #5 (permalink)
Nathan Thickett-menghini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

That's brilliant thanks! So it seems to me that the Tracks Developers
have changed the format of the feeds since the script was written,
omitting the word 'Due:'. How can I get it to include all lines that
begin with white space? Many thanks, you've been fantastic!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 18/09/2007, 02h43   #6 (permalink)
matt neuburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Nathan Thickett-menghini <somerandom_bloke@hotmail.com> wrote:

> That's brilliant thanks! So it seems to me that the Tracks Developers
> have changed the format of the feeds since the script was written,
> omitting the word 'Due:'. How can I get it to include all lines that
> begin with white space?


It's hard to say, because I don't know the format of the feeds. You
could change the script you have, just deleting the six character phrase
"\[Due:" wherever you see it. But that is just a guess, not knowing what
the feeds look like. It would be better if you'd show us a multi-line
feed output so we can see what the desirable and undesirable lines look
like. In the example you gave, *all* lines were desirable, which made
some of the program wasteful. But perhaps in a fuller example, there are
some undesirable lines. The change I'm suggesting might not eliminate
them; it depends what they look like.

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
  Réponse avec citation
Vieux 18/09/2007, 17h36   #7 (permalink)
Nathan Thickett-menghini
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Many thanks - you're ing me hugely. I've gone through the script and
made the changes you suggested and it has worked in the most part. The
part of the script that parses the lines is now as follows:

today = `curl -s "#{URL}"?due=0`.split("\n").select{|line| line =~
/\s+/}
tomorrow = `curl -s "#{URL}"?due=1`.split("\n").select{|line| line =~
/\s+/}
this_week = `curl -s "#{URL}"?due=6`.split("\n").select{|line| line =~
/\s+/}
later = `curl -s "#{URL}"?due=365`.split("\n").select{|line| line =~
/\s+/}

I'm sure this is not perfect but it seems to do the trick of including
the lines that begin with blank space. Let me show you the feed as you
requested.


HOME:
[18/09/2007] Parcelforce coming after 1pm
[19/09/2007] be a bender
[21/09/2007] Sort important documents etc

WORK:
[24/09/2007] Learn NATS Stuff

CAR:
[30/09/2007] nob

So you see there are lines that begin with letters (these are the
contexts, Home, Work, etc) lines with a small gap followed by events,
and lines that are entirely blank.

You'll have noticed that part of the script deletes duplicate lines -
so that you don't get an event that is on today's feed repeated on the
weekly feed. This works mostly but it also deletes repeated CONTEXTS,
i.e. the context 'HOME:' will appear on today's feed but not on any
others. I've tried to get round this by totally abandoning all contexts,
i.e. changing the part that parses the lines to NOT INCLUDE lines that
begin with word characters. This works, but it now still includes the
totally empty lines, which puts gaps in my text files. Could you add a
tiny part to the following line to show me how to NOT include empty
lines? Is this possible? I know it seems like a lot of effort for a
small problem, but I'd really appreciate it, it will make me more
productive to see my events neatly on my desktop.

today = `curl -s "#{URL}"?due=0`.split("\n").select{|line| line =~
/\s+/}
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 19/09/2007, 18h44   #8 (permalink)
matt neuburg
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: errors, trying to edit and write some text files

Nathan Thickett-menghini <somerandom_bloke@hotmail.com> wrote:

> HOME:
> [18/09/2007] Parcelforce coming after 1pm
> [19/09/2007] be a bender
> [21/09/2007] Sort important documents etc
>
> WORK:
> [24/09/2007] Learn NATS Stuff
>
> CAR:
> [30/09/2007] nob
>
> So you see there are lines that begin with letters (these are the
> contexts, Home, Work, etc) lines with a small gap followed by events,
> and lines that are entirely blank.
>
> You'll have noticed that part of the script deletes duplicate lines -
> so that you don't get an event that is on today's feed repeated on the
> weekly feed. This works mostly but it also deletes repeated CONTEXTS,
> i.e. the context 'HOME:' will appear on today's feed but not on any
> others. I've tried to get round this by totally abandoning all contexts,
> i.e. changing the part that parses the lines to NOT INCLUDE lines that
> begin with word characters. This works, but it now still includes the
> totally empty lines, which puts gaps in my text files. Could you add a


In my view the entire original script is misconceived. You can't just
look at individual lines. You need to cycle thru the lines in order,
gathering info as categorized. Something, perhaps, like this:

cats = {}
today = `curl -s "#{URL}"?due=0`
today.split("\n").inject do |memo, line|
line.lstrip!
if line =~ /^\w*:/
cats[line] = []
line
else
cats[memo] |= line.to_a
memo
end
end

Now "cats" holds a nice hash where the keys are the categories and each
category's value is an array of its events. Do that again for "tomorrow"
and the others, using the same "cats"; no duplicate values will be
inserted, because of the use of the |= operator. When you're all
finished, output "cats" in whatever format you like, such as:

cats.each do |cat, events|
puts cat
events.each do |event|
puts " " + event
end
end

If you wanted to get really neat, you could sort stuff before
outputting.

m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h27.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17544 seconds with 16 queries