Skip to content

Instantly share code, notes, and snippets.

View chrisguitarguy's full-sized avatar

Christopher Davis chrisguitarguy

View GitHub Profile
@chrisguitarguy
chrisguitarguy / ioc.py
Last active August 29, 2015 14:06
An IoC container in Python3 using function annotations
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import inspect
import functools
class InvalidObjectError(ValueError):
"""
Raised when the service locator cannot create a class
@chrisguitarguy
chrisguitarguy / readtags.go
Created September 28, 2014 14:18
How to read struct tags in Go.
package main
import (
"errors"
"fmt"
"reflect"
)
var NotAStruct = errors.New("Object is Not a Struct")
@chrisguitarguy
chrisguitarguy / composer.json
Last active August 29, 2015 14:10
A silex app with a repeated field. Try submitting mis-matched values -- `Form::isValid` happily accepts them.
{
"require": {
"silex/silex": "~1.2",
"twig/twig": "~1.16",
"symfony/form": "~2.6",
"symfony/validator": "~2.6",
"symfony/config": "~2.6",
"symfony/translation": "~2.6",
"symfony/twig-bridge": "~2.6"
}
@chrisguitarguy
chrisguitarguy / wat.php
Last active August 29, 2015 14:12
Can't throw in generator functions? You can, but they don't throw until iteration starts.
<?php
function gen($x)
{
if (!is_int($x)) {
throw new \InvalidArgumentException;
}
foreach (range(0, $x) as $i) {
yield $i;
use std::io;
use std::rand;
use std::cmp::Ordering;
fn main() {
println!("Guess the number!");
let secret_number: u32 = (rand::random::<u32>() % 100) + 1;
loop {
@chrisguitarguy
chrisguitarguy / functools.py
Last active August 29, 2015 14:17
Example of partial "function" application on python class using lambdas or functools
import http.server as httpserver
import functools
class Handler(httpserver.BaseHTTPRequestHandler):
_id = 'default'
_secret = 'default'
def __init__(self, request, client_addy, server, app_id, app_secret):
# super().__init__(...) kicks of request handler so set up
# before calling it
final class CountableMemorySpool extends \Swift_MemorySpool implements \Countable
{
public function count()
{
return count($this->messages);
}
public function getMessages()
{
return $this->messages;
@chrisguitarguy
chrisguitarguy / CustomerMeta.php
Last active August 29, 2015 14:18
Some example code for a blog post on PMG.co
<?php
namespace PMG\ExampleCustomer;
/**
* An API class for fetching custom metadata for a given customer post type.
*/
final class CustomerMeta
{
const WEBSITE_LINK = '_pmg_examplecustomer_websitelink';
@chrisguitarguy
chrisguitarguy / with.php
Created April 13, 2015 01:26
A *with* statement in PHP?
<?php
/*
Ideally this would be part of the language:
with (createAnObject() as $object) {
$object->doStuff();
}
This is just sugar for:
@chrisguitarguy
chrisguitarguy / bad_traversable.php
Last active August 29, 2015 14:19
Some example code for a blog post on https://www.pmg.com/blog/
<?php
interface Spam extends \Traversable
{
// ...
}
final class Ham implements Spam, \IteratorAggregate
{
public function getIterator()