Skip to content

Instantly share code, notes, and snippets.

View alebian's full-sized avatar
🌊

Alejandro Bezdjian alebian

🌊
View GitHub Profile
@alebian
alebian / model_to_csv.rb
Created April 17, 2016 21:26
Model records to CSV
require 'csv'
CSV.open("path/to/file.csv", "wb") do |csv|
csv << Model.attribute_names
Model.find_each do |model|
csv << model.attributes.values
end
end
module JsonSerializer
module_function
#
# Usage:
# JsonSerializer.find_by_id(User, 1, only: [:id, :email, :address], except: [:address])
#
def find_by_id(klass, id, options = {})
sql = "SELECT row_to_json(results) FROM (
SELECT #{selected_attributes(klass, options)}
@alebian
alebian / memoizer.js
Created January 18, 2017 18:37
Javascript
// Example taken from Javascript The Good Parts
var memoizer = function(memo, formula) {
var recur = function(n) {
var result = memo[n];
if (typeof result !== 'number') {
result = formula(recur, n);
memo[n] = result;
}
return result;
@alebian
alebian / interactor_mixin.rb
Last active September 26, 2017 18:09
Mixin for Interactor objects. Dependencies: interactor, dry-types
require 'interactor'
require 'dry-types'
# Author:: Alejandro Bezdjian
# Copyright:: Copyright (c) 2017 Alejandro Bezdjian
# License:: Apache 2.0
class InteractorMixin
include Interactor
require 'pathname'
require 'json'
################################################################################
# USAGE: #
# Execute this script from console like #
# $> ruby variable_checker.rb ./repositories_to_check_example.json #
# #
# Create your own repositories.json file following the example #
################################################################################
@alebian
alebian / git_commits_since_release.rb
Created March 16, 2017 13:53
This script lets you easily see what commits were made after the last release of your project. Usefull to track changes between releases.
new_tag_version = ARGV[0]
GIT_REPOS = []
class GitRepository
GIT_MERGE_COMMITS = /(Merge pull request)|(Merge branch)/
GIT_HASH_LENGTH = 8
attr_reader :name
@alebian
alebian / spy.rb
Last active April 12, 2017 15:48
class Spy
def self.spy(klass, method)
define_counter(klass, method)
klass.singleton_class.class_eval do
define_method(method) do |*args, &block|
instance_variable_set("@spied_#{method}_counter", instance_variable_get("@spied_#{method}_counter") + 1)
super(*args, &block)
end
end
end
import numpy as np
class SimplePerceptron:
def __init__(self, input_layer_size, output_layer_size, coeff = 0.05):
self._bias = -1
self._coeff = coeff
self._input_layer_size = input_layer_size
self._output_layer_size = output_layer_size
# Weights is a matrix in which each column represents the weights from every input
# to each output and has the bias in each column
class JSHash
def initialize(hash)
@original_hash = hash
@hash = build_recursively(hash)
end
def method_missing(m, *args, &block)
response = @hash.send(:[], m.to_s, *args, &block)
return response if response
response = @hash.send(:[], m.to_sym, *args, &block)
module Configurable
def self.with(*attrs)
not_provided = Object.new
config_class = Class.new do
attrs.each do |attr|
define_method attr do |value = not_provided, &block|
if value === not_provided && block.nil?
result = instance_variable_get("@#{attr}")
result.is_a?(Proc) ? instance_eval(&result) : result