Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2014/01/31

You Had One Job, MongoDBx::Class Documentation

So, after yesterday, I started back with the command line interface to MongoDB. I found I can do what I want with it, which of course is creating, retrieving, updating and deleting records. So, I try working with Perl, my go-to language, and I install MongoDBx::Class. The following code, especially the $db-&gtlinsert() is closely adapted from the CPAN page.


#!/usr/bin/perl

use feature qw{ say state } ;
use strict ;
use warnings ;
use MongoDBx::Class ;

my $dbx = MongoDBx::Class->new( namespace => 'MyApp::Model::DB' ) ;
my $conn = $dbx->connect(host => 'localhost', port => 27017 , safe => 1 );
my $db = $conn->get_database( 'test' ) ;

my @movies ;
push @movies , {
    title => 'Blade Runner' ,
    rating => 'R' ,
    releaseYear => '1982' ,
    hasCreditCookie => 1
    } ;
push @movies , {
    title => 'Thor' ,
    rating => 'PG-13' ,
    releaseYear => '2011' ,
    hasCreditCookie => 1
    } ;

for my $movie ( @movies ) {
    say qq{$movie->{ title } ($movie->{ releaseYear })};
    $db->insert($movie) ; # How the documentation says it should go
    }


2014/01/30

You Had ONE Job, Mongoose.js Documentation

Source
I'm trying to learn Node.js, MongoDB and Mongoose.js as an ORM connecting the two. I don't know what I'm doing yet, going through the early example code. I come to this as a reasonably experienced hand with Perl and MySQL. The first thing people write is a "Hello World" application, where you give it an input, commonly your name, and it gives you an output. In data, the four actions you want are Create, Read, Update and Delete, often used as CRUD. When trying out a data-storage system, you want to be able to Hello World your data.

The following is code adapted from the front page of Mongoosejs.com and the first link to documentation.

#!/usr/bin/js

/*
 * test_mongo.js
 *  adapted from mongoosejs.com
 *  
 * Getting up to speed with MongoDB, Mongoose as an ORM, ORMs in general,
 * and Node.js. 
*/

// Testing direct from http://mongoosejs.com/

// initiallizing connection to MongoDB 
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/cats');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
    console.log( "Yay" );
    });

// Schema - what comes from DB
var kittySchema = mongoose.Schema({ name: String }) ; 
kittySchema.methods.speak = function () {
    var greeting = this.name
        ? "Meow name is " + this.name
        : "I don't have a name" ;
    console.log(greeting);
    } ;
kittySchema.methods.save = function ( err , cat ) {
    console.log( cat ) ;
    if ( err ) {
        console.log(err) ;
        }
    cat.speak() ;
    } ;

// Model - what we work with
var Kitten = mongoose.model('Kitten', kittySchema) ;
Kitten.find(function (err, kittens) {
    if (err) // TODO handle err
    console.log(kittens)
    })

// Objects - instances of models
var silence = new Kitten({ name: 'Silence' }) ;
var morris = new Kitten({ name: 'Morris' }) ;
var fluffy = new Kitten({ name: 'Fluffy' });
var blank  = new Kitten({ });

console.log( '------------------------' ) ;
silence.speak() ;
morris.speak() ;
fluffy.speak() ;
blank.speak() ;
console.log( '------------------------' ) ;
silence.save() ;
morris.save() ;
fluffy.save() ;
blank.save() ;
console.log( '------------------------' ) ;

process.exit(0) ;


save() doesn't seem to save, or do anything, really. speak() works, but why shouldn't it? The reason I'm doing this is so that Mongoose can stand between me and MongoDB. Mongoose has one job. The documentation has one job: to show people me how to do just that.

One Job!

2014/01/29

Coming to Terms with Javascript's Not-Quite-Procedural Nature

I'm creating a web application. I've written HTML code on the server before and have decided templates are best, because whatever language, writing code that generates HTML is always worse than making a template that has holes you can fill with your data. This is why I now love Perl's Template Toolkit.

Funny thing is, making the code client-side doesn't make it much better. For now, I'm using Mustache.

There are many things you can easily do in JavasScript, but multi-line variables are not pretty in JavaScript, so it's far better to have external templates and import them. This is my current code to import a list of templates, which so far are div_request.m, div_sample.m and div_final.m.

var accordion_request = {} ;
accordion_request.m_path    = '/dev/dave' ;
accordion_request.templates = {} ;
accordion_request.template_fetch = []
accordion_request.template_fetch.push( 'div_request' ) ;
accordion_request.template_fetch.push( 'div_sample' ) ;
accordion_request.template_fetch.push( 'div_final' ) ;

accordion_request.get_templates = function ( ) {
    for ( var t in accordion_request.template_fetch ) {
        var template = accordion_request.template_fetch[t] ;
        var name = [ template , 'm' ].join( '.' )
        var url = [ accordion_request.m_path , name ].join('/') ;
        console.log( template ) ;
        console.log( name ) ;
        console.log( url ) ;
        $.get( url , function ( data ) {
            console.log( ': ' + url ) ;
            accordion_request.templates[template] = data ;
            }  ) ;
        }
    }

Do you see the problem?

JavaScript does not block. So, we convert div_request to div_request.m to /dev/dave/div_request.m, then we set the jQuery $.get() off to get that url, then we convert div_sample to div_sample.m to /dev/dave/div_sample.m and set that off to $.get(), then jump back to div_final.

By the time $.get() starts working, url is "/dev/dave/div_final.m", so we get a console log that looks like:
div_sample
div_sample.m
/dev/dave/div_sample.m
div_request
div_request.m
/dev/dave/div_request.m
div_final
div_final.m
/dev/dave/div_final.m
: /dev/dave/div_final.m
: /dev/dave/div_final.m
: /dev/dave/div_final.m

I now believe I need to write a function that takes url and gets and writes it, so that it has it's own scoped url rather than using the one in get_templates(). We will see.

2014/01/10

My Body Plans For 2014: January and February

I've covered my 2014 priorities and goals previously. Now, I'm starting to make plans, but I'm not going too far ahead. Just the first quarter. Will make further plans in support of my goals as the years go on.

It is important when planning behavior change to remember that willpower is finite, and so changing too many behaviors will doom a plan to failure. So, the plans will be set by month, not by task.

January

Sticking to strength training and diet at the beginning of the year, because running in snow sucks. 

Caffeine -- I have been trying to cut down of caffeine, and now follow these rules:
  • coffee only when I'm at work 
  • no coffee after work 
  • no more than two cups a day 
 I will be coming off a two-week vacation, so I'll be reset with caffeine. I could either see if I can code without coffee, or I can go down to one cup a day. I believe I'll go to one cup a day. 

Strength -- I have been going to the gym after work when I can, which tended to be Mondays and Thursdays. It's been ill-defined thing, with me picking up things as I figure them out. I know nothing about the gym -- the only thing I learned from gym class in school is to fear my fellow man -- but I've found some sources and now think I have a protocol to start with.

AreaExerciseCurrent Weight
Chest Dumbell Fly 15lbs
Dumbell Bench Press 25lbs
Back Dumbell Row 25lbs
Shoulder Dumbell Press --
Dumbell Shrug 25lbs
Arms Dumbell Curl 25lbs
Dumbell Extensions --
Glutes Dumbell Deadlift --
Quads Body Weight Squats
Core Planks
Sit-Ups


Once I get solid with Body Weight Squats, I might start doing them with weights. I'll work up how the circuit goes with the Quads and Core as I start to work through them. 

The source I read suggested that you do reps and sets such that they add up to 25. I put it to 30 to make all the math right, and came up with this.

Monday Wednesday Friday
  6 sets of 5 reps     3 sets of 10 reps     2 sets of 15 reps  

My plan is to do the circuit following this, going in before work on Monday Wednesday and Friday. That'll help get my body and mind going without coffee those days.

February
 

Feet -- Starting this month, I start to do range of motion exercizes to get my right foot's range of motion to more closely approximate the left. Will get into that later.

Tools --  Will also develop tools to prepare for coming months. Specifically: 
  • check against Forecast.io to see if the day will be dry and warm. If so, have it tell me to run.  
  • additions to weight tools to 1) accept Google+, Facebook and Twitter login 2) allow multiple users 3) show progress on weight goals with sexy plots done with D3.js 
  • additions to FitBit tools 1) add to handled API calls 2) find best/worst days of the week 
  • obWeightTrainingApp work 
March
Endurance -- This month, I start preparing for the 5K, beginning the Couch to 5K. Adding HIIT Tabata sprints. Also, start finding and signing

2013/12/16

My Body Goals For 2014

I've established my priorities previously. This is to establish the goals that flow from those priorities. This is where the priorities interact.

 There's little in this that independently affects brain power (that I know of), so there's nothing here that I'm marking with (Brain), but otherwise, in order, here are my body goals for 2014:

  • Significantly greater range of motion for my right foot, less pain from both feet (Feet)
  • Weight of less than 200 lbs. Waist of 32 inches or less. (Weight/Body Fat) 
  • Meeting daily steps goal of 6000 daily. Raising to AHA-standard 10,000 steps and meeting that. (Feet,Stamina/Endurance) 
  • At least one 5K race completed in 40 minutes or less. (Stamina/Endurance) 
  • A set of ten standard push-ups (Strength) 
  • One successful pull-up or chin-up (Strength) 
You will notice no specific dietary or exercise regimines here. This is because I consider then as "plans", not "goals". You take on Paleo or Slow Carb or Atkins or whatever in order to take control over your weight, not as a goal in and of itself. You might become a Vegetarian for political/humanitarian reasons, but then "Stop participating in cruelty" is the goal and eating a Vegetarian diet is your plan to do fulfill that goal.

Significantly greater range of motion for my right foot, less pain from both feet (Feet)

As previously mentioned, my feet are weird. My left foot shows -- showed? It's been a while since I saw the doctor -- signs of pronation and bunions, but my right foot is far far worse.

It is my belief that it has always been worse and I just never noticed until 42 years in. It might be the case that I can't fully fix the problem without surgery, and any invasive surgery is problematic and likely to leave it in a continually compromised state. I intend to use stretching and other non-invasive means to get it worked out.

Related, though, is the fact that the mere fact of hitting the American Heart Association's recommendation of 10,000 steps leads me to believe that my years of neglect as a mostly-sitting student and computer professional has caused muscle atrophy in my feet and ankles, and an amount of physical therapy that'll lead me through pain is necessary before I get to where they're where they should be.

I'm finding that, while my ankle hurts when I start moving, once I'm going for a while, the pain subsides. I'm thinking that I have to move more all the time to get my ankles to a better point.

Weight of less than 200 lbs. Waist of 32 inches or less. (Weight/Body Fat)

I am on a downward trend, and the evidence tends to show that I have more to lose if I keep the way I am, but I'm into a time where many people hit a plateau due to the increased consumption around Christmas and the decreased activity due to Winter. I expect this, I hope to work against it, and I expect further loss come Spring. I suspect 2014's weight loss, unlike 2012 and 2013, will be something I have to work for.

I'm tossing in a Kickstarter-like stretch goal of getting and staying within 5 lbs of 180. I think much less than that and I'll look like a bean-pole, but we'll see.

Meeting daily steps goal of 6000 daily. Raising goal to AHA-standard 10,000 steps and meeting that. (Feet,Stamina/Endurance)

I dropped my daily steps goal in FitBit to 6000 steps because it seemed doable without heroic measures, and while I often have spike days above the goal -- even above the 10,000 step goal -- but weeks where my daily average is above 6000 are rare, and weeks when my worst days were above 6000 steps just do not exist.

Kinda goes with the first goal, and certainly relies on it. If my ankles and legs aren't happy, I won't be pushing myself to move.

At least one 5K race completed in 40 minutes or less. (Stamina/Endurance)

As mentioned before, I've "run" a 5K with a time of 45 minutes. I'm happy with it -- I came, I saw, I didn't stop, I got the t-shirt! -- but I believe I can do better. So, my goal here is to prove it.

A set of ten standard push-ups (Strength) 
One successful pull-up or chin-up (Strength)

I am grouping these, as they are predicated on upper-body strength. There's a bunch of core in pushups, but that isn't my primary reason. I've been trying to keep body weight squats in my endurance work, like ten squats every lap, but I don't have a strong way to gauge that, nor a strong reason (pun intended) to work on it. (Did a 5K nature run followed by some Cross Fit including squats, and the squats made my thighs ache for the better part of a month. That is why I do squats.)

I also intend to get my form together for burpees (a perfectly sadistic exercise in it's own right, one where my form so far has been spastic and ill-disciplined), but that's that would be nice, not I will do this.

Up soon, at least before New Year, is my plans to achieve these. After that, progress reports as I knock these things down.

2013/12/10

My Body Priorities for 2014

I can see myself with a MacBook
This is not to take the form of a set of premature New Year's Resolutions. Resolutions are code for promises to yourself that you will not keep, and I have never wanted any part of that.

This is to take the shape of what I want to change about myself, which will then lead to behaviors I want to develop or cease. The scheme for behavior modification will come, but it will not be "Starting January 1, I'l stop drinking caffeine and stop eating sugar and run 5 miles every day and lift weights and read a book a night and get to sleep before 10pm and wake up at 5am..." Willpower is a finite resource, and you can burn through it this way.

So, my priorities, in order of importance:
  1. My Feet
  2. My Brain
  3. Weight/Body Fat
  4. Stamina and/or Endurance
  5. Strength
Two years ago, I stopped drinking colas and lost over 40 lbs, started sleeping better and felt good enough to start a Couch-to-5K process that lead to me finishing a race. Almost five months ago, I moved to a standing desk, and while that really sucked for quite a while, I'm happy with the results. So, I think I'm past all the outstandingly stupid things I was doing to myself, which means I'm out of shortcuts. Doesn't mean there aren't efficient and smart ways of doing things, just that the no-brainers are done and I'll have to start burning willpower.

My Feet: Two years ago, my youngest participated in a hike with his Cub Scout pack to commemorate the 200th anniversary of the Battle of Tippecanoe, and at the end of it, I noticed intense pain between the smaller toes and the outside of the ankle, which kept with me for several days after. I went through similar pains as a volunteer at an Independence Day 2012 celebration, and as part of my Couch-to-5K process. 

Eventually, the sharp pains in my foot lead me to a podiatrist, who told me had pronation in my left ankle and extreme pronation in my right. They're also pretty flat, to the point where I used to be able to make fart noises with my right foot coming out of the shower. Right now, my feet, mostly my right foot, are the limiting factor for most of my attempts at further fitness. It's one thing, for example, to keep running when you're sweaty, wheezing and tired, but it's another thing when each time you push off, it feels like someone's shoving a knife into your arch.

So, my goal here is to develop stretches and exercises that make my foot less flat, less pronated, and less painful. This is a primary things: I can't really do much with many of the later goals without making progress here.

My Brain: Exercise increases your brain power, and as a programmer, my brain is my most important feature. If I didn't believe that exercise helped my brain, I wouldn't be doing half of this or writing this. This article mentions benefits of both cardio and strength-based workouts, so more activity of whatever stripe is 

Weight/Body Fat: By memory, my top weight was 330-350 lbs. Top weight I recorded is 270 lbs, in Feb 2012. I am 217 lbs as of this morning. I no longer have a double chin. I wear a Large t-shirt (after 20+ years in XL) and 34x34 jeans.

But I still have a gut.

Teenage me could not imagine having six-pack abs. 30-year-old me was befuddled at how he could eat like he did and still be over 300 lbs, and couldn't even begin to imagine something like muscle tone or a lack of love handles. 43-year-old me is beginning to see it.

So, exercise plans will be chosen with an eye to fat burning as priority, but really, it seems to me that this is more a diet thing. Right now, I make breakfast and lunch decisions mostly based on economics and convenience, which means oatmeal for breakfast and microwave Banquet for lunch. My thought is to go with a Slow Carb diet, but I will need to work out how to make it work around my requirements.

A word on "diet": Popular usage has "diet" meaning a temporary change in food intake to achieve a certain outcome. This is not the correct meaning. When they say "A panda has a diet of shoots and leaves", they mean a panda only eats those things, all the time. When I say I'm considering the Slow Carb diet, I'm saying that I'll commit to it, the way I committed to not drinking sodas.

Stamina and/or Endurance: I mentioned running a 5K earlier. I'll add scare quotes and say "running", as I spent much of that race walking, giving me a 45-minute race time. My goal on this is to do at least one 5K this year, where I do a time of less than 40 minutes. I want to spend all that time running or at least jogging, with no energetic walking.

Endurance, as I understand it, relates to having enough to keep going through the race, while stamina relates to having enough to keep you going at maximum capacity. Endurance is for marathoners while stamina is for sprinters. I think what I have is neither and so could use a little bit of both.

Strength: At the previously-mentioned Independence Day celebration, one of my tasks was to move staging, including choir risers. These risers are built to stand the weight of several choir members, and are thus solidly built. I thought I had it in my to lift a riser to stage level, and I just didn't have it in me to do so. I had said that there was nothing in my life I didn't have the strength to handle, and this proved me wrong.

My goal is not bulk; I don't want to look like Mr. Universe. I just want to have reserves of strength when I need them, and I want that strength across my body, not just in my arms and upper body. This means squats and lunges in addition to curls and lifts.

My current objection to strength training is that it's hard to quantify like you can with endurance training.

From these priorities, I will develop plans, which I will put here and document my progress. Any ideas and suggestions, both on what to do and how to keep motivated in doing it, will be appreciated.

2013/12/09

Something That Makes Me Sad, But Probably Shouldn't


Saturday was a good and incredibly geeky day for me. I went to the Indy Quantified Self Meetup, held in one coworking space, spent much of the afternoon in Fry's, where I met someone and talked about a startup idea (planned meeting, not happenstance), then went to Indy Hackers' Holiday Social, held in another coworking space. I listened to tech podcasts all of the way down and most of the way back. There's geeky things I didn't do (converse with my good geek friend Patrick) and otherly-geeky things that aren't for this blog (went to two guitar stores).

It was a good day, but there's one thing that wasn't so good. Maybe.

Above is the entire technical book stock at Fry's Indy. The entire shelf. There's lots and lots of shelves, and once, they were filled with all sorts of technical books. It fills my heart with joy to see a wall of O'Reillys. For the longest time, my first-pass rule for judging a technical project or tool was "Is there an O'Reilly book for it?" The books for learning Perl are Learning Perl and Programming Perl, and I judged Python harshly because the book a Python person would suggest is not Learning Python.

These days, though, your first pass for a technical question is asking Shub-Internet and ending up with a discussion thread, IRC log, or hopefully a Stack Overflow answer. I have editions 1 through 4 of Programming Perl, and they are decreasingly dogeared and beverage-stained as time goes on. I haven't really dug into Programming Perl, Fourth Edition, in part because I know how to write Perl, but in part because, even when I don't know what I'm doing, dead trees are not my go-to.

And, even from the perspective of the writer, writing tech books is a losing game. Jeff Atwood of Coding Horror writes about it when pushing a book, ASP.NET 2.0 Anthology, that he contributed to. xkcd did a comic (which I can't find) which compares the staying power of different book types, grouping math and physics texts as books that can stay without revision for decades, sometimes centuries, while a book on a language or protocol will be out of date very soon, sometimes before publication.

So, it would make sense to have Knuth's Art of Computer Programming on your shelf, but less so Perl, much less Perl 5.18.1. So, it would make sense to publish Art of Computer Programming on paper, but to generate and regenerate language documentation into electronic form.

And really, at places like Fry's, you want as much floor space devoted to things that can only be atoms, like motherboards, cases, LEDs, cables, trackpads and MacBook Pros, and things that can be bits should be bits. Plus, of course, the fact that you can put a whole lot of bits into your tablets and newsreader.

So, I can rationalize it at length, but it still saddens me to know that the world I used to live in, where there were places where a wall of amazement and possibility was available to me. I guess it outs me as a geezery geezer who geezes.

2013/11/27

Why Yahoo is Sad for me

In a perfect world, I would be able to wrap up my tools in a little bundle. I can't. We've made a decision to have one lib directory and put everything in it. So, we have our blessed jQuery, jQuery-UI, Mustache.js, etc. And, we don't use every library in every web app. So, this means for one JS-heavy page, I'm calling those three, jQuery TableSorter, HighCharts and a theme, plus my library which calls that.

Well, really, I can and should pull out the HighCharts libs, as we no longer plot on that page. Which makes things even worse. I call seven (now 5) libraries to do this work which ySlow considers excessive. But because I don't always use jQuery-UI and Mustache, I can't just make and minify a std_lib.min.js with everything put together.

This is where I get a B.

Even worse, I don't run everything through GZIP. (F.) I also don't use a CDN. I am using SSL now, and it seems that SSL and CDNs are mutually exclusive, as everything that doesn't come from your https server is a possible security issue. Even then, almost everybody who is going to use this data is going to be in the same lab. Still, F.

And we're not going to get into how I'm not going to play with the Apache config to get Expires headers (F) and Entity Tags (F).

And we have our Lab Notebook, the place where we store all our work, in OddMuse. And OddMuse remembers who you are with a Cookie. Not having written OddMuse, I can't change that. Not having a good alternate choice, I can't change to another Wiki. So, there's one cookie. (D)

I know that Yahoo's problems are not my problems. Honestly, while I got a grade of 77 (C) on the page as a whole, I know it's one of my better websites, because it handles only a small chunk of data at a time. And really, I'd rather work with my unminified JS so if problems arrive, the JS console will give me a sane reading of where the problem lies.

So, I'll take that Charlie and proceed to the next problem.

2013/11/07

Counting Hashtag Usage with R

Found this on the Revolution Analytics blog: What does Barack Obama tweet about most? In essence, pull down all the tweets from the official Twitter feed for the President, grab the hashtags and create a bar graph.

I don't often use R interactively. I generally make a script, set it in crontab, and have it run automatically. So, I adapted it to run via Rscript.

2013/09/03

Bug in Perl? Or am I just doing it wrong?

Three modules: MyTools::Foo, MyTools::Bar and MyTools::Blee. Foo primarily exports foo(), which returns "foo". Bar, similarly, exports bar() which returns "bar". Same with Blee.

Foo also exports foo_bar(), which concatenates the output of foo() and bar(), and foo_blee(), which does the same with foo() and blee(). Bar has bar_foo() and bar_blee(), and Blee has blee_foo() and blee_bar().

Here, the library holding MyTools is identified using the PERL5LIB environment variable. I do this to model a problem shared by a suite of modules that have spaghetti-like tendencies to interconnect with each other. Previously, I had used use lib '/path/to/lib' to do this purpose, but the need to start using git and the like has pushed me toward using a means to identifying library paths without hardcoding them.

I haven't developed the test case such that, using use lib, it works, but my production uses lib all over the place, and I only noticed the problem when I copied it to a dev directory and pulled those lines.

The problem I'm seeing is that Perl starts to look for foo() in Bar or Blee, when foo() is in Foo. Clearly, It'd be better if Foo didn't include Bar and Blee while Bar included Foo and Blee, etc. It would be nice if I had separated the code better in the first place, but seeing that the code base where I started finding this problem is 19 modules with over 8,000 lines and is used across several systems by someone else, so every change has the potential to break my lab's workflow, I can't really disentangle it right now.

So, it strikes me that Perl is wrong, too. (I fully admit that my code is an unruly hairball. I'm starting to pay that technical debt right now.) I'm somewhat loathe to tag this as an error in Perl and start filing bug reports until someone with more direct experience looks at this and says either "That's odd" or "Dave, you're a dumbass." I would certainly accept either answer.

So, am I wrong?