Hacked By Imam

Hacked By Imam with Love

Advice on Acronyms

This showed up on Facebook today:
Screenshot from 2015-02-03 15:02:00

First, it should be noted that you specify the acronym after the definition, so you’d have this:

ear, nose and throat (ENT) specialist

Further, and what peeves me–what’s the point? Why define an acronym in a twitter-sized Facebook post? The word is not re-used, so no typing or space is saved. In fact, it’s wasted. The post should read:

I need recommendations for a good ears, nose, and throat doctor for a child.

Rails 4 Enums

Rails 4 added an enum helper for models. Here’s how to use it.

Generate your model
rails g model title:string attribute_type:integer

Edit the migration to include a default
create_table :staff_attributes do |t|
t.string :title
t.integer :attr_type, default: 0 #set a default here.

class StaffAttribute < ActiveRecord::Base enum attr_type: [ :education, :award, :membership ] end

Since the enum maps to an integer, it's not a true (database-level) enum. The list corresponds to integers, so in this case our default of 0 will equate to :education.

Rails creates methods like: StaffAttribute.last.education? #=> true

Read more here: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

Postgres On Ubuntu

Because I’m tired of looking up syntax!

sudo apt-get install postgresql
sudo -u postgres createuser -P -s -e new_username
(enter password)
sudo -u postgres psql
>CREATE DATABASE database_name;
>GRANT ALL ON DATABASE database_name TO new_username;

Here’s a pretty nice postgres reference, if this isn’t enough.

Python Dictionaries In Django Templates

Selecting a value using a variable key in Django is not straightforward.

For instance, today I was counting active contacts on a User model, which are referenced by a foreign key. So I’ve got

{% User.contact_set.count %}

But I realized I was counting all contacts, when I really want to do .filter(active=True)

Since you can’t (and really don’t want to) run a filter in the template, you have two options:
1) Prepare the counts in the view, or
2) Write a method in your model to count for you

Method One

I chose solution one, and used a dictionary comprehension:

users = User.objects.all()
active_contacts = {user:Contact.objects.filter(owner=user, active=True).count() for user in users}

But in the template, now we’ve got a variable key. You can’t use a variable key in dot notation in the template. This won’t work:

{% for user in users %}
{{ active_contacts.user }}
{% endfor %}

That tries to do active_contacts["user"] instead of active_contacts[user], and so you get no value returned.

Instead, I had to do:
{% for user in users %}
{% for key, value in active_contacts.items %}
{% if key == user %}
{{ value}}
{% endif %}
{% endfor %}
{% endfor %}

Method Two

The alternative, as it turns out, is probably nicer. I don’t like polluting my models with methods that are very specific to one place, but it would make my template code a lot cleaner. It would be something like this:

class User(models.Model):
def active_contacts(self):
return Contact.objects.filter(owner=self, active=True).count()

And then, in the template, we could simply do

{{ user.active_contacts }}

Note, this code is untested.

So, dictionaries with dynamic keys aren’t awesome in Django, but they’re definitely workable.

General Westerly Direction

I think the way to use these big ideas is not to try to identify a precise point in the future and then ask yourself how to get from here to there, like the popular image of a visionary. You’ll be better off if you operate like Columbus and just head in a general westerly direction.

Paul Graham

2013 Resolutions

I’m not big on resolutions.

That is to say–I make tons of resolutions, and I accomplish most of them. It’s just that I don’t need the motivation of a fresh year to set a goal–I set them as soon as one comes to my brain that I decide to accomplish.

But it’s a convenient time, and all the cool kids are doing it, right?

In 2012 I had a pretty short list. I’m not sure what all was on it, however I do remember what I considered the most important: “Say No.” I did pretty well at saying no–mostly in the form of turning down request that I did not want to fulfill (“favors”), or letting people or my conscience manipulate me into doing things I did not want to do. I quit working at my own bar because it was the best thing for me. I broke an agreement I had made several years prior, that had turned into the most restrictive thing in my life. I moved back to Florida from Arkansas–intending for it to be a stop–and landed in a phenomenal job.

I also failed on some counts–middle-of-the-night rides for friends, moving furniture in my truck, and loaning money to people who haven’t paid up but have plenty to spend on themselves.

Anyhow, on with it:


 2013 resolutions

  • Build lots of small things I love
  • Build something that generates moderate income
  • Try to build both in one

I’m a programmer, so building things isn’t always difficult. Last year I built a script to watch the craigslist free section for new items, a daily inspirational quote site, messed around a lot with screen scraping, and started a whole slew of unfinished Django projects. I’ve spent probably the most time thinking about, researching, and coding a rough version of a video training site–like CardRunners.com, but I plan to target video games.

As usual, though, my steam has run out for most of those. I don’t craigslist much anymore, I frequently quit updating the quote site, and I still haven’t shipped a MVP for the video training site–probably because I don’t want to “run the business” for it.

I’m quite ready to build some kind of app or SaaS product that is fun, has a lot of areas to explore programatically, and is built on a profit model. I just haven’t figured out what it is yet!

For good measure, I’ll add a few more:

  • Turn off distractions
  • Buy new jeans
  • Write 500 words per day
  • Polish
  • Meet people
  • Compliment people who deserve it, strangers or otherwise

“Polish” probably could use some explaining. I want to “polish” my little projects. Instead of a craigslist script, I should have a little craigslist program with a GUI and some options, and I want my web-apps to look nice–even if that means hiring a designer.

And as for “meet people,” it goes without saying that I will meet some people, however I’d like to make a conscious, concerted effort to expand my “people” list a little.

Prediction: 2013 is going to be a year I look back on with pride. No unfathomable mistakes, less slacking, more enjoying, better people.

Pointing Dreamhost DNS to Heroku

I had a domain ( WitDaily.com ) hosted at Dreamhost that I decided to move to Heroku. (I still think Dreamhost is great, though!)

It took me a while to figure out what to do with my DNS records. Heroku has a walkthrough for Custom Domains on their service. It’s pretty straightforward, but I ran into a small hang-up trying to set my DNS over at Dreamhost: they had “non-editable” records for a ton of stuff–a bunch of subdomains, plus an A-record.

The fix? Go to “Edit” in the domain management / hosting area, and go from something like “fully hosted” to “DNS only.” At that point, all the records are editable.

June 7th

You can’t just say you’re something–you have to be the something to actually claim it. Me–I’m going to quit telling people what I am.

Happy birthday, Mom.

Rain Is Just an Excuse

Out for a run today, I could tell it was going to rain as I finished the 1.2 mile loop. My goal is to run a little farther each run than I did the run before it. On my last run I had gone about 1.6 miles.

But the rain…

And then I realized the rain was just an excuse.

And ran 2.4 miles instead. And then biked 2.4 miles. All in the rain.

There’s an excuse to quit or not do basically everything you’ll ever have an opportunity at. Just do it.