Why I Blog

I've been asked recently why I blog. My answer is the same since I started blogging back in 2006. I blog for three reasons.

  1. I figured out a problem/issue I was having and want to share the knowledge. If I had the problem/issue, someone else will and hopefully google will link to my post and help them when they are searching. What I've also experienced is someone reads my post and comments with a better and/or more efficient solution. I then can link to their comment/post or update my own. I love that sense of community.
  2. I will use a different part of my brain to write the post and, therefore (hopefully), remember the solution better next time this particular problem/issue arises.
  3. I want to store the solution online, not only on my local computer (aka isolation), so that I, and others, can reference it in the future.

I DO NOT blog because I think it will make me popular or because I want to seem smarter than anyone else. I enjoy learning and sharing information.

When Git or SSH Protocols Blocked

    Tags : 

Recently I had issues connecting to Github because of some firewall restrictions outside my control. The firewall had outbound SSH (port 22) blocked. My co-worker Justin Hammond found a git config option that lets you force all git commands to use https:// (port 443) instead of git:// (port 22).

git config --global url."https://".insteadOf git://

Worked like a charm. Thanks Justin. Hope this helps someone else.

NB: Don't use --global if you don't want this applied to all repositories. Without it only sets for the current repository you're in.

Weechat keyboard shortcuts

I've switched from using irssi to Weechat since I rejoined the ranks as a Racker.

Here is my current weechat.conf file

Some of the ones I use often:

  • ctrl+h - window left
  • ctrl+j - window down
  • ctrl+k - window up
  • ctrl+l - window right
  • ctrl+z - window zoom (make current small window full screen)
  • ctrl+n - next buffer
  • ctrl+p - previous buffer
  • ctrl+y - clipboard paste
  • ctrl+r - search
  • ctrl+[j|m] - stop search

normal commands not included with my weechat.conf that I use:

  • /window resize - resizes window to that percentage
  • /buffer clear - clears the buffer, i use this often with the highlight monitor plugin
  • /buffer merge - merges two buffers into one (use ctrl+x to switch between them)
  • /buffer unmerge - unmerge the buffer you're currently on out to its own

Where I Go To Choose An Open Source License

I go to choosealicense.com to refresh my memory on open source licenses. MIT is usually my favorite.

A good resource, in my opinion

How to Get The SHA of The Last Commit on a Branch With Git

    Tags : 

I've been asked this a few times and have had to do it lately a few times.

To get the SHA of the last commit from a specific branch, use git rev-parse

git rev-parse origin/master (remote branch)

or

git rev-parse master (local branch)

How to Run a Single Test With Tox

I have to run a single python test once in a while and keep forgetting how to do it with Tox.

Here it is:

tox -e py27 -- project_name_here/tests/folder_name1_here/folder_name2_here/test_file_name_here.py:TestClassName.test_method_name

Hope this helps someone else.

Happy bit twiddling.

Personal VIM Cheatsheet

Will be adding to this post as I learn/remember shortcuts

  • . - repeat last command in normal mode
  • @: then @@ - repeat last command entered with : source
  • # - find all occurances of the word I'm currently on
  • dfc - delete everything including the letter 'c'
  • dtc - delete everything up to the letter 'c'
  • shift+h - go to top of screen
  • shift+m - go to middle of screen
  • shift+l - go to bottom of screen
  • ctrl+v [h|j|k|l]` - highlight indeividual items (great for highlighting column(s)
  • gUiw - capitalize the current word
  • gUU - capitalize current line
  • vU - capitalize current character
  • guiw - lowercase the current word
  • guu - lowercase the current line
  • vu - lowercase current character

keybindings I use all the time via mappgings in my .vimrc

  • ,v - create vertical split
  • ,h - create horizontal split
  • ctrl+j - go to split below/down
  • ctrl+h - go to split on left
  • ctrl+k - go to split above/up
  • ctrl+l - go to split on right

Using sed and regex to Remove Prefix on Filenames

I used wp2md recently to generate markdown files out of my wordpress posts so that I could migrate to a static blog generator (like Nikola, the one I chose to use on this blog). However, this exported the files with a prefixing datetime stamp (e.g., 20140224-blog-title.md)

I have used sed many times from my command line toolbox to rename files or content inside of files. Here is how I removed the prefixing datetime stamp from my filenames.

for f in *.md; do echo $f | sed -r 's/.*(\d*)-(.*\.md)/\2/'; done
  • the for loop iterates over just my mardown files (files with the md extension)
  • I echo out the filename into the sed command line tool via a pipe, you can send the results of one command to the input of another by piping them together (e.e., command1 | command2)
  • Then I setup the regex s/old_string/new_string/
    • I've learned when using group matching (the sections in the parenthses) and trying to replace the old_string with an entirely new string, you need to start the old_string with .*. This causes the entire line to be replaced with the contents of the new string
    • old_string regex
      • (\d*) - first group match - any digit, 1 or more times
      • "-" - a hyphen separating the match groups
      • (.*.md) - second group match - anything, ending in .md
    • new_string regex
      • \2 - replace the entire old string with the contents of the second group match

Example

20140224-test-driven-development.md becomes test-driven-development.md

NB: the ; do and ; done are just bash scripting loop notation when put onto one line. This script could have also been written as

for f in *.md; do
    echo $f | sed -r 's/.*(\d*)-(.*\.md)/\2/'
done

I am aware that Nikola has a wordpress importer, but it imports the files to .wp and .meta. I wanted markdown files (*.md)

NB: I also use Rubular.com or Pythex all the time to test out my regex expressions. You should check them out.

Consolidated My Blogs

I finally consolidate my blogs that were located at:

and unified them here. I also decided to move to Nikola since I'm coding in Python full-time now and it is a nice Python Static Site Generator. New posts and theme changes coming soon.

Using Default Scope and Unscoped in Rails

I recently had the need to add a deleted flag to a model in a Rails project. I am usually of the mindset “explicit over implicit” so whenever I needed to use a finder to get content for that model I would do something like:

User.where(deleted: false)

That does lean towards not having DRY (don’t repeat yourself) code. It’s a balancing act. I decided to give default_scope a chance this time. I quickly experienced the downside of that choice. I added the default_scope to my model:

class User < ActiveRecord::Base
  default_scope where(deleted: false)
...
end

Well, I needed to write a migration to add the column and then update any existing records to have a default value of false. I generated my migration and added the following:

class AddDeletedFlagToUsers < ActiveRecord::Migration
  def change
    add_column :users, :deleted, :boolean, :default =&gt; false
    User.update_all ["deleted = ?", false]
  end
end

However, when I looked at the database, none of the pre-existing records had false as their deleted flag. I went to the rails console and typed in the update_all command again:

User.update_all ["deleted = ?", false]

and that’s when I saw how default_scope can bite you in the butt:

~/code/project(master) &gt; rails c
Loading development environment (Rails 3.2.12)
>> User.update_all ["deleted = ?", false]
  SQL (35.9ms)  UPDATE "users" SET deleted = 'f' WHERE "users"."deleted" = 'f'
=> 0

Notice the WHERE clause. That is what the default_scope adds automatically. Duh!! So in order to bypass the default_scope I had to use unscoped:

User.unscoped.update_all ["deleted = ?", false]

in my migration. That then caused all existing records to be updated.

Yes, I’m aware this violates “Explicit over implicit” but it does keep my code “DRY (do not repeat yourself)”. There are pros and cons to all tools. Use them wisely.

Share