Skip to content

Instantly share code, notes, and snippets.

@jm42
jm42 / Container.php
Created November 22, 2015 15:52
Container Builder Q&D
<?php
function camelize($name) {
return strtr(ucwords(strtr($name, array('_' => ' ', '-' => '_ ', '.' => '_ ', '\\' => '_ '))), array(' ' => ''));
}
class Container implements \ArrayAccess {
protected $container = [];
function offsetSet($key, $value) {
@jm42
jm42 / lamparitas.py
Created November 19, 2015 15:09
Bruteforce al problema de lamparitas
# Hay 1000 lamparitas numeradas del 1 al 1000, las que son numeros primos
# tienen un interruptor que cambia el estado de la lamparita de el y de
# todos sus multiplos.
# Cual es el maximo de lamparitas que pueden haber prendidas?
#def divisores(n):
# d = []
# for i in range(1, n + 1):
# if n % i == 0:
# d.append(i)
<?php
class ConflictException extends \Exception {}
class Configurator extends \SplPriorityQueue {
private $order = PHP_INT_MAX;
function insert($path, $key, callable $fn, $priority=0) {
parent::insert([$key, $fn], [$priority, $path, --$this->order]);
}
<?php
class Configurator extends \SplPriorityQueue {
private $order = PHP_INT_MAX;
function insert($path, $key, callable $fn, $priority=0) {
parent::insert([$key, $fn], [$priority, $path, --$this->order]);
}
// [...]
@jm42
jm42 / settings.php
Created November 18, 2015 03:18
Settings using Choclo
<?php
class Settings extends Configurator implements \ArrayAccess {
private $path;
private $values;
function __construct($path) {
$this->path = $path;
$this->values = [];
}
@jm42
jm42 / gendoc.php
Created October 29, 2015 21:20
Quick & Dirty (tm) PHP literate doc generator
#!/usr/bin/env php
<?php
/** Documentation Generator
*
* Command line little script that given a PHP file name will generate an HTML
* with it's documentation.
*/
function parse_doc($doc) {
$lines = preg_split('#(\r\n|\n)#', $doc);
@jm42
jm42 / play.py
Last active October 28, 2015 13:38
Play littlealchemy.com
"""Play Little Alchemy"""
from selenium.webdriver import Firefox
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import StaleElementReferenceException, \
MoveTargetOutOfBoundsException
from os.path import join, dirname, abspath
from time import sleep
<?php
function call_funcs(array $fns, array $args) {
switch (true) {
case empty($fns): return $args;
case count($fns) === 1: return call_user_func_array($fns[0], $args);
default: return call_user_func('call_funcs', array_slice($fns, 1), (array) call_user_func_array($fns[0], $args));
}
}
@jm42
jm42 / bootstrap.php
Created October 11, 2015 23:42
Bootstrap test
<?php
### LOADER ###################################################################
include 'https://gist.githubusercontent.com/jm42/0bafdc8792c90bdec3a7/raw/c420e913bffd54878be2b23cda1e7ade6843d6ff/loader.php';
class PHPLoader extends Loader {
public function load($resource) {
include $resource;
}
@jm42
jm42 / log.php
Created October 5, 2015 23:17
Simple Log Interface
<?php
interface Logger {
const CRITITAL = 50;
const ERROR = 40;
const WARNING = 30;
const INFO = 20;
const DEBUG = 10;
/** Logs a message with integer level $level on this logger.