impressiver

  • Archive
  • RSS

JavaScript Console.lite™

Been thinking that it would be nice to leave console.log() messages in the front end JavaScript code, with a switch to turn them on and off, making for quick on-demand production debug sessions. It also acts as a failsafe for the occasional console message that slips out of the dev environment and into production, which can cause a surprising number of people to email support regarding the loading screen that won’t go away.

So I created a light window.console wrapper that routes console calls to noop functions if debug is not enabled, but back to the original console if you have DEBUG = true (or any non-falsey value) in localStorage.

Side note: Turns out localStorage.DEBUG allows direct access, without using localStorage.getItem(). And it works with any all-caps properties (at least in Chrome and Safari). Neat.

_js_
window.console = (function(w) {
  var wC = w.console, rC = {}, lS = w.localStorage || null,
      fs = ["log", "debug", "warn", "info"],
      isD = function() { return (!!(lS && lS.DEBUG)); };
  for (var i in fs) {
    rC[fs[i]] = function() {
      return (!!wC && isD()) ? wC[fs[i]].apply(wC, arguments) : 'ohkay!';
    };
  }
  return rC;
})(window);

I’m sure a few neck beards will blubber at such disregard for thorough deployment procedures. Yep, not going to argue, there are better ways to configure remote debug/logging, error handling, CI checks for silly mistakes, etc. But this took me 5 minutes to write, and now I can stop thinking about it until I have time for all that other stuff.

Here’ a link to the code in a Gist in case you want to improve on this and show me up.

    • #javascript
    • #console
    • #debug
  • 5 months ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Drop all database tables for a Django project without deleting and recreating the database.

During early development, db schema is all over the place. It’s annoying to make and undo migrations for every schema change while you’re still fleshing things out. But there’s no simple built-in way to reset a project back to ‘zero’ for all installed (and removed) apps. I needed a way to remove all of the database tables in a Django project so I could quickly (and completely) reset a development environment.

The easiest way I could come up with was to pipe drop database and then create database SQL commands through the Django management dbshell:

_sh_
$ echo "drop database $DATABASE_NAME; create database $DATABASE_NAME;" | ./manage.py dbshell

But if you can’t delete the database, don’t want to deal with re-adding permissions, or don’t know the database name (you could print settings.DATABASES['default']['NAME'] from the management shell, but things start to get creative at that point if you’re looking for a one-liner), here’s an alternative:

_sh_
$ for i in `(echo "show tables" | ./manage.py dbshell | awk '{ print $1}' | grep -v '^Tables')`; do echo "set foreign_key_checks = 0; drop table $i;" | ./manage.py dbshell; done;

This will completely drop all tables in the Django project database(s?) so you can re-run ./manage.py syncdb --migrate as if this were a fresh repository. So much hassle for a simple little thing, but it’s almost certainly for a reason — this would make it way too easy to shoot yourself in the foot.

For the record, this is probably a bad idea. Don’t ever leave this in your project after you get to the point where you’re keeping migrations around. But for the first few days of a project, this has been a real timesaver.

    • #Django
    • #Python
    • #MySQL
    • #dev
  • 5 months ago
  • 2
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

sprint.ly: Introducing Sprint.ly 1.0

sprintly:

A little more than a year ago we released Sprint.ly into private beta. In June we removed the invite code. Today we’re removing the beta tag. Over the last year we’ve been actively listening to our current (and former) customers’ major pains.

Those recurring and systemic pains could be summed up…

  • 6 months ago > sprintly
  • 26
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Deep Merge Multiple Python Dicts

I was looking for a simple way to merge Python dicts similar to how jQuery.extend works with the deep option. This is useful for situations where you need a nested set of default key-value pairs and want to allow those values to be overridden without losing any keys not specified in the overriding dicts. I couldn’t find anything built-in to Python that would preserve nested key-value pairs, and the closest I could Google was this great writeup for a similar merge function, so I wrote this little helper:

_py_
def dict_merge(target, *args):
  # Merge multiple dicts
  if len(args) > 1:
    for obj in args:
      dict_merge(target, obj)
    return target

  # Recursively merge dicts and set non-dict values
  obj = args[0]
  if not isinstance(obj, dict):
    return obj
  for k, v in obj.iteritems():
    if k in target and isinstance(target[k], dict):
      dict_merge(target[k], v)
    else:
      target[k] = deepcopy(v)
  return target

And here are a few examples of how it can be used:

_py_
import json
from utils import dict_merge

obj = {
  'one': 1,
  'two': 2,
  'three': {
    'sub1': 4,
    'sub2': 5,
    'sub3':6
  }
}

# Print the defaults
print json.dumps(obj, sort_keys=True, indent=2)
'''
{
  "one": 1, 
  "three": {
    "sub1": 4, 
    "sub2": 5, 
    "sub3": 6
  }, 
  "two": 2
}
'''

override1 = {
  'one': 'foo',
  'three': {
    'sub4': 'bar'
  }
}

# Print the object after merging in override1
dict_merge(obj, override1)
print json.dumps(obj, sort_keys=True, indent=2)
'''
{
  "one": "foo", 
  "three": {
    "sub1": 4, 
    "sub2": 5, 
    "sub3": 6, 
    "sub4": "bar"
  }, 
  "two": 2
}
'''

override2 = {
  'four': 4,
  'three': {
    'baz': 'bat',
    'sub1': {
      'sub_sub1': 'wow'
    }
  }
}

# Print the object after merging with two dicts
merged = dict_merge(obj, override1, override2)
print json.dumps(obj, sort_keys=True, indent=2)
'''
{
  "four": 4, 
  "one": "foo", 
  "three": {
    "baz": "bat", 
    "sub1": {
      "sub_sub1": "wow"
    }, 
    "sub2": 5, 
    "sub3": 6, 
    "sub4": "bar"
  }, 
  "two": 2
}
'''

# Reset the original object
obj = {
  'one': 1,
  'two': 2,
  'three': {
    'sub1': 4,
    'sub2': 5,
    'sub3':6
  }
}

# Don't modify the original object
merged = dict_merge({}, obj, override1, override2)
print json.dumps(obj, sort_keys=True, indent=2)
'''
{
  "one": 1, 
  "three": {
    "sub1": 4, 
    "sub2": 5, 
    "sub3": 6
  }, 
  "two": 2
}
'''

print json.dumps(merged, sort_keys=True, indent=2)
'''
{
  "four": 4, 
  "one": "foo", 
  "three": {
    "baz": "bat", 
    "sub1": {
      "sub_sub1": "wow"
    }, 
    "sub2": 5, 
    "sub3": 6, 
    "sub4": "bar"
  }, 
  "two": 2
}
'''
    • #Python
    • #code
    • #snippet
    • #jQuery
  • 8 months ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Binge Coding

Throughout my geek years of messing around with computers and the internet, there have been more nights than I care to remember where I’ve set out to get some piece of software (or hardware) configured just the way I want, reading docs and following installation guides to the letter, only to run up on some tricky piece that just doesn’t work the way it’s described. It usually results in hours of hunting for fractured clues from various blog posts hoping to find just one person who’s tried to do what I’m doing, ran into the same problem, figured out how to fix it, and then bothered to blog about it.

It almost never ends up that way, though. The way it usually works out is that I find a handful of alternative, workaround, almost-as-good ways to accomplish what I’m trying to do. But, instead of spending five more minutes to go with something good-enough and then move on with my life, I have to figure out how to make it work exactly the way I had intended. So, I read more docs, sift through bug reports, patch code, and very un-scientifically begin to try every combination of suggestions from the dozen or so pages I’ve found around the nets discussing (sometimes loosely) related issues. I dig deeper into things I have no business messing with, all the while asking myself, “what the fuck are you doing? Give up already.” As the early AM hours roll on, I end up bashing on the keyboard in a desperate attempt to hit the one magical state that makes everything work. Trouble is, it inevitably does work, and I get things running the way I wanted just in time to see the black through the windows turn to dark blue, a reminder that morning will be here far too soon. By that point I am so disgusted with my ineptitude and wasted life that I don’t want to spend another second thinking about what just happened.

And then, six months later, when I need to do the same thing on a new computer, I can’t remember what the hell I did the first time around. What a waste.

OK, so that is an extreme example of a code binge, but spending an extra couple of hours to solve an issue that already has a simple workaround, just so I can understand why it even needs a workaround, happens all the time. Binge coding, binge hacking, binge tweaking, though the symtoms are slightly different, have all been responsible for a significant amount of sleep deprivation over the years.

You’ve heard the oft-bastardized saying, “if I’ve been able to help even one person…” At this point in my life I know that I’ll never be able to stop the occasional binge coding session, so I might as well try to help someone avoid the same fate. Even if that one person is myself, the next time I run into the same issue, then I’ve squeezed a tiny little bit of value out of those sleepless hours. Hopefully I won’t be the only person to Google my own post six months down the line, but I’m aware that it is a real possibility.

  • 8 months ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
http://instagr.am/p/G4A8aNPHK8/
Pop-upView Separately

http://instagr.am/p/G4A8aNPHK8/

    • #instagram
  • 1 year ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Neuroscience is fun: See the Blood Vessels In Your Eye [Video] http://t.co/JmS6VQxr

  • 1 year ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

My wife is passed out beside me, before we could sneak a gift, and I’m feeding our happy 7 week old daughter. Thanks Santa. Merry Christmas.

  • 1 year ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

The Lean Mean Green Machine, A Gas Powered Big Wheel: The Lean Mean Green Machine is a nine foot long big wheel… http://t.co/KAYnZgOg

  • 1 year ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+

Dropbox for Teams Offers a Terabyte of Space, Centralized Billing: If you have a crowd of people collaboratin… http://t.co/np2cpnQG

  • 1 year ago
  • Permalink
Share

Short URL

TwitterFacebookPinterestGoogle+
← Newer • Older →
Page 1 of 143

About

Avatar

Find me here

  • @impressiver on Twitter
  • haggisandkimchi on Flickr
  • Google
  • My Skype Info
  • Linkedin Profile
  • impressiver on github

Twitter

loading tweets…

Back to Top
  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Pixel Union Powered by Tumblr