Wednesday, January 29, 2014

OMG It Worked!

So what did I do wrong? :)  I'm half-way between the two points on this comic:



Here is part one of the second iteration of my word count script/mini program:

word_count = {}
File.open('test_input.txt', 'r') do |f1|
  while line = f1.gets
    words = line.split(" ")

    words.each do |word|
      word = word.downcase
      if !word_count.has_key?(word) #word not in hash?
        word_count[word] = 1        #add it and count it
      else
        word_count[word] += 1       #only increment count
      end                           #if it's there
    end
  end
end


Next up, I need to figure out how to feed it html pages. I could just feed it straight up html files, but I ultimately want to word count blogs and such. On to the next!

Sometime later, we rejoin our hero:

So I added a line to allow me to alternatively feed the wc any file I like via the command line:
File.open( ARGV[0]? ARGV[0] : 'default.txt', 'r' ) do
I'm using ARGV[0] ? to check to see if there are any arguments sent along with the request to run the script. If there are, we use them. If not, we use my creatively-named 'default.txt' so that we don't blow a gasket and throw an error for not having anything to work with. I snagged some HTML from a random website and fed it to the wc program.

Next up: parsing out the tags so that all we're left with is the actual content of the site. After that, I need to figure out how to get the generated HTML in the first place. I've heard of screen scrapers (and usually not in a positive way) but I think that's what I need to build here. Ultimately, I would like to give this little program the urls for two different websites and have it compare the two. I'm a long way from there, but it's nice to have a goal. :)

Tuesday, January 28, 2014

Because I Promised

Here is a very rough first run of the word_count script.

input_text = "The quick brown fox jumped over the lazy dog. And then the sun shone down on everyone and the little girl smiled. This is the end of this story, this is the end of this story."

words = input_text.split(" ")
word_count = {}

words.each do |word|
  if word_count.has_key?(word)
    word_count[word] += 1
  else
    word_count[word] = 1
  end  
end

puts word_count.inspect

My next steps will be to turn it into a separate method and open a file and count the contents. After that, I want to be able to strip out the text from and html document. I'll need to create tokens for tags to ignore things like <p>, etc. Or I can just count them and then ignore things at the end that have a < as the first letter. We'll see when we get there. I may also go back and refactor the logic so that the `if` is when the key does not already exist (it's a little more human-logical that way, I think.)

Tomorrow's a new day. Good night for now.

Thursday, January 23, 2014

Mind Your P's & Q's, or Rather, Your I's & You's

I have a theory about blogs. Based on nothing other than intuition and my own anecdotal and subjective reading experience, I suspect that blogs that are more author-centered (use "I" and "me" a lot) are less popular than reader-centered (use "you" more).

To test my theory, I'm planning a little word-count program that one can feed some texts and it will compare them. (A more advanced version will allow the user to feed the program a url, but I'm not there yet.)  Details will follow soon. :) 

The first post I will run through the word-count will be this post!

Sunday, January 19, 2014

In Confidence

Lately, I've been thinking about confidence. Partly because whomever curates the Elance Twitter feed seems to have the same theme running through their mind recently. Partly because it's simply a recurring theme in most articles and books about starting out in new fields. At my "day" job, however, I frequently come across a certain paradox about confidence.

For many people, someone with a lot of confidence inspires trust. They are speaking with someone providing (or wishing to provide) them a service and if their service provider is confident in their abilities and value, the customer will believe them.

Except when they don't. Perhaps this is getting confounded with power relationships (I have a very low-power role when dealing with my customers - "the customer is always right" reigns supreme in retail and there's no room for a Vancome Lady attitude!) Even when I am confident about an answer, I feel the need to hedge it and say things in the most wishy-washy "I believe" (instead of "I know") sort of way. Anything more confident is treated to a "well, check anyway!" anger-filled reaction.

There's a voice in my head that says to go with the confident response anyway, despite the anger that it will sometimes elicit. Practice makes permanent and I have successfully convinced myself that I am frequently uncertain and dis-confident about what I really do know. Perhaps the positive response I will get from a portion of my customers will be worth the intermittent nastiness.

Hmmm... I hear the winds of change. If there's rioting at a certain drug store in Juanita, you'll know who caused it ;).

Friday, January 17, 2014

Bug Catching

Today was a day spent almost entirely on smashing bugs. Several mundane and one so blindingly obvious I could smack myself. (See Twitter, in case you missed it.)

My main goal for today was to make the background color of the sfs page coarsely change color based on the time of day (morning, midday, etc.) The logic of that was mostly straighforward:

def time_of_day()
    t = Time.now
    if t.hour >= 4 and t.hour < 10
      return "morning"
    elsif t.hour >= 10 and t.hour < 16
      return "midday"
    elsif t.hour >= 16 or t.hour < 4
      return "bedtime"
    else
      return "late"
    end
  end

I then used the returned string to set the class for the background.

<div class="<%= time_of_day() %>">

After that, I was a little stumped because it had been so long since I started this project that I couldn't remember where exactly to put the Ruby code! (I know, I know, I'm edupunkn00b, remember?) At first I tried to load it like I would a JavaScript file and I tossed this in the header of my index:

<script src="/helpers.rb" type="ruby/text" />   #this doesn't work!

First, yes, I need to refactor my views (you can see them in all of their sopping wet glory over on Github, if you like watching train wrecks :) ). Refactoring was planned for the second part of my morning, but that was used up with bug smashing. Second, regrettably, that didn't work. After playing around for a long, long time (I dropped the code right into the body of the index page at one point, just to be sure it would run) Finally, remembered app.rb. (Sigh, I know.)

# my_app.#!/usr/bin/env ruby -wKU
require 'sinatra'

class MyApp < Sinatra::Base
  # This is where your code will go

  def time_of_day()
    t = Time.now
    if t.hour > 4 and t.hour < 10
      return "morning"
    elsif t.hour >= 10 and t.hour < 16
      return "midday"
    elsif t.hour >= 16 and t.hour < 7
      return "bedtime"
    else
      return "late"
    end
  end
  
  get "/" do
    erb :index  
  .
  .
  .

The little "this is where your code should go" comment was a nice little dig. Maybe I should have named the file "this_is_where_your_code_goes_dummy.rb"

After all that, it worked! I got my little test <h2> tag that showed the logic. I created some CSS rules and ....

Nada. I tried F12 and Chrome had just drawn a big ugly red line through my CSS and called it invalid. What was going on? I tried renaming the colors (I once had a bug because I used 7 digits instead of 6 in a hex code.) Nope, that didn't help. I tried different selectors. Nope, the selectors were working just fine. Finally I decided to just try to change the background color and that's when I caught it. I had written the rule as:

body div.midday {
  background-color: #000000,  # <-- right there!
  color: #FFFFFF;
}

SMH, FML, STFU, D'OH and every other acronym you can think of. Do you see it? I stared at that code for almost two hours (well, not two hours straight, but you get the idea) before I caught it. The comma. Perfect if you're making a list or want to describe the dietary requirements of the giant panda, but very, very bad when trying to describe how you want your class to look in CSS. I fixed that bug and thought, great! Time for the Heroky Pokey and... it didn't work.

There seems to be a recurring theme here. It worked locally on Chrome, so I pulled it up in Firefox.

Nope.

OK. Time for coding with the world's most popular language: Google. It seemed to be a rather common problem. Try it - I got over 1.5 million hits. I felt a little better after that. After much digging and rechecking (nope, Firefox and IE were loading the CSS file, so I wasn't blowing it there), but neither browser would recognize them as CSS. Then I looked a little closer at what I had typed:

<link type="css/text" ... />  # wrong!

and not

<link type="text/css" ... />  # correct!

A little more typing (and even less swearing) later, I had it fixed and running: http://schatz-family-splash.herokuapp.com. It's ugly and, well, ugly. But it works. Kinda. Tomorrow's always another day. :D



Thursday, January 16, 2014

Blood From A Stone

Each day, we all perform a careful balancing act, deciding (either mindfully or not) how we will use the 1,440 minutes we have for the day. From multitasking to single-tasking, waking at 4am to burning the midnight oil and only crawling beneath the sheets when dawn arrives, everyone has their own solution.

For some of us, our work and school schedules dictate for us where our "free" time will land on the face of the clock. When that time comes in oddly-sized chunks (that 35 minutes you might have between one appointment and another) or at off-kilter and unpredictable times (swing shift workers unite! Oh, wait a minute, that's another topic.) Those are the times when making the time for our (unpaid) priorities can feel like we're wringing blood from a stone.

The solution is
.
.
.
.
.
Hey, I'll share when I find one if you promise to do the same!

Wednesday, January 15, 2014

A Post About Nothing

Tipping my hat to Mr. Jerry Seinfeld, whose productivity hack I am stealing :). (While I love the hack, I still hate the show. Sorry, Jerry.)  And now, in Seinfeld style, a post about nothing.

The hack is "Don't Break the Chain," as featured here, here and here. You get a big, year-long calendar and put a big bright 'X' on everyday that you work. (You get to pick what's work - for a comic, it's writing.) As you probably deduced, the hack is not "break the chain." You want a big, long streak of 'X's on your calendar. This is designed to keep you working, even when you don't really feel like it. The joy of keeping the chain going can help keep you motivated even on days when you're not.

This is somewhat related to a recent post from my on-again-off-again favorite blogger, Leo Babauta. Favorite blogger because he seems to have such joy and, more than many other writers I follow, really seems to have found his bliss. I have now unsubscribed from his updates five or six times now, though. There are times when it seems like I won't ever be in a similar place and I lose hope and click 'unsubscribe.'

Back to his post, though. His (second) most recent post (as of this writing) is all about motivation and how to get moving when you really don't feel like it (or feel like there's a point to it, anyway.) He describes waking up in a funk and thinking
"Should I just give up what I do, because I’m not as good at it as I thought I was?"

And all I could think was "how did he get in my head?" This, in turn reminded me of Scott Hanselman's now-famous post on being a phony. In both instances, I thought that if these people who I respect and try to emulate can feel like they're not good enough to work sometimes, maybe I should just suck it up and push harder at my own work. And that I'm not (in Kirstie Alley's great moment) too stupid to live.

Which brings me back to the title of my post. If I'm too afraid to do anything, if I think that my effort won't even be worth it, then I will have nothing to show at the end of the day. Nothing. It's taken years for me to start to think that it's better to have a failed project at the end of the day than to have just filled my head with more information, quotes and other people's ideas. As Seth Godin says, to have been merely a spectator of my day. So, instead of nothing, I will have something. It may not be great. It may not even really work all the way, but it will be something. It's better than nothing.

P.S. And here it is: http://schatz-family-splash.herokuapp.com/

Monday, January 6, 2014

It's Been a Long December...

And you know the rest of that line. In fact, I think I may have used that post title before. Working in retail, December becomes a lost month. Massive staffing problems kept me away from the big Railsbridge event in Seattle last month.

I'm back in the saddle, warming up on Railsbridge intro-level stuff and then I'm going to tackle to rest of Michael Hartl's Rails Tutorial starting tomorrow. I'm putting out feelers, trying to find some sort of apprentice-type gig, hopefully part time so I can make money at the same time. This was my last Christmas in retail, one way or another. :)

More will follow soon.