Skip to content

Instantly share code, notes, and snippets.

View ryanuber's full-sized avatar

Ryan Uber ryanuber

View GitHub Profile
@ryanuber
ryanuber / depth.py
Created July 26, 2013 23:44
Determine depth of data structure in Python. This recursive function will return the depth as an integer of the deepest-nested object.
def depth(data):
d = 1
if type(data) is dict:
for item in data.keys():
if type(data[item]) is dict or type(data[item]) is list:
d += depth(data[item])
elif type(data) is list:
for item in data:
if type(item) is dict or type(item) is list:
d += depth(item)
@ryanuber
ryanuber / app.php
Created September 28, 2013 03:35
PHP: "use" statement resolving both class and namespace in one statement. What if you have a namespace called `\fruit\apple`, and also a class called `\fruit\apple` ? If you do `use fruit\apple`, what does it "use" ? The answer is, both. It will now allow you to refer to both `apple` and `apple\...` as you please.
<?php
namespace fruit {
class apple {
public static $x = 'this is the apple class';
}
}
namespace fruit\apple {
class seed {
public static $x = 'this is the seed class';
}
@ryanuber
ryanuber / ruby 1.8.7
Last active January 2, 2016 22:09
Ruby 1.8.7 > 2.x SystemStackError
irb(main):001:0> def method_missing(*args) args.join(' ') end
=> nil
irb(main):002:0> hello world
=> "hello world"
@ryanuber
ryanuber / monthPlus29.php
Created February 5, 2014 05:53
Print a formatted date exactly 29 days from the 1st of every month
<?php
$result = array();
foreach (range(1, 12) as $m) {
array_push($result, date('Md', mktime(0,0,0,1,date('z', mktime(0,0,0,$m,1))+29)));
}
print implode(',', $result);
@ryanuber
ryanuber / gist:9014174
Created February 15, 2014 03:31
Vagrant re-import box from files on local system

If you find that vagrant box list somehow does not contain a box you previously had, you can sometimes add the box back if you still have the sources for it in ~/.vagrant.d/boxes.

$ cd ~/.vagrant.d/boxes/precise64/0/virtualbox/
$ tar czf ~/precise64.box .
$ vagrant box add precise64 ~/precise64.box 
Downloading box from URL: file:/Users/ryanuber/precise64.box
Extracting box...te: 417M/s, Estimated time remaining: --:--:--)
Successfully added box 'precise64' with provider 'virtualbox'!
@ryanuber
ryanuber / README.md
Last active August 29, 2015 13:57
Example Serf router-style handler

Serf handler router

When using @hashicorp's excellent Serf orchestration tool, you typically configure a handler for each type of event you expect to encounter (more about events and handlers here). To change handler configuration, one must update the Serf configuration and reload the agent with a SIGHUP or a restart.

Thanks to the flexibility and open-endedness Serf offers for configuring and executing handlers, we can achieve similar functionality by configuring only a single "router" handler, which we will never

@ryanuber
ryanuber / gist:9498296
Last active August 29, 2015 13:57
goyaml parsing JSON text
package main
import (
"fmt"
"log"
"github.com/go-yaml/yaml"
)
var json_data = `
{
@ryanuber
ryanuber / gist:7adb3babcc90cb9a6f40
Created August 23, 2014 06:53
terraform_example_panic
Refreshing Terraform state prior to plan...
Error running plan: unexpected EOF
panic: interface conversion: interface is nil, not string
2014/08/22 23:50:27 terraform-provider-aws:
2014/08/22 23:50:27 terraform-provider-aws: goroutine 8 [running]:
2014/08/22 23:50:27 terraform-provider-aws: runtime.panic(0x7d3040, 0xc2100a4700)
2014/08/22 23:50:27 terraform-provider-aws: /usr/local/go/src/pkg/runtime/panic.c:266 +0xb6
2014/08/22 23:50:27 terraform-provider-aws: github.com/hashicorp/terraform/builtin/providers/aws.func·003(0x0, 0x0, 0x9, 0x0)
$ consul keyring -init r+pUD3yH8bZVHxPrBwmI2g== -data-dir /tmp/consul
<start/join agents here>
$ consul keyring -list
==> Asking all WAN members for installed keys...
r+pUD3yH8bZVHxPrBwmI2g== [1/1]
==> Asking all LAN members for installed keys...
r+pUD3yH8bZVHxPrBwmI2g== [2/2]
$ consul keyring -list
==> Gathering installed encryption keys...
==> Done!
dc2 (LAN):
HjZbybeuVTvM5QRtuLl77g== [2/2]
dc1 (LAN):
HjZbybeuVTvM5QRtuLl77g== [2/2]