Skip to content

Instantly share code, notes, and snippets.

View hemalvarambhia's full-sized avatar
👨‍🔬

Hemal Varambhia hemalvarambhia

👨‍🔬
View GitHub Profile
@hemalvarambhia
hemalvarambhia / complex_number.rb
Created October 5, 2022 20:30
An Example of a Very Basic Class
class ComplexNumber
def initialize(real, imaginary)
@real = real
@imaginary = imaginary
end
def to_s
"#{@real} + #{@imaginary}i"
end
end
@hemalvarambhia
hemalvarambhia / employees.rb
Created October 5, 2022 20:28
An Example of a Collection Object
class Employees
include Enumerable
def initialize(employees)
@employees = employees
end
def each &block
@employees.each &block
end
@hemalvarambhia
hemalvarambhia / pca_tutorial_lindsay_smith.py
Created April 20, 2019 16:25
Using Scikit-Learn's PCA to reproduce the results of the Lindsay Smith PCA Tutorial Paper
import numpy as np
from numpy.linalg import inv
from scipy import linalg
from sklearn.decomposition import PCA
from sklearn import utils
X = np.array([[2.5, 2.4], [0.5, 0.7], [2.2, 2.9], [1.9, 2.2], [3.1, 3.0], [2.3, 2.7], [2, 1.6], [1, 1.1], [1.5, 1.6], [1.1, 0.9]])
mean_ = np.mean(X, axis=0)
X = X - mean_
import numpy as np
from numpy.linalg import inv
from scipy import linalg
from sklearn.decomposition import PCA
from sklearn import utils
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
mean_ = np.mean(X, axis=0)
X = X - mean_
@hemalvarambhia
hemalvarambhia / example_usage.rb
Created November 24, 2014 14:24
Example usage of the Nginx chefspec matchers
describe "configuration" do
before :each do
@nginx_configuration = chef_run.template(@server_nginx_config_file)
end
describe "server" do
describe "location" do
describe "/" do
it "whitelists requests from Abelson Info servers" do
expect(@nginx_configuration).to whitelist_requests_from(["92.165.78.7", "12.34.56", "24.68.12"]).to_url "/"
@hemalvarambhia
hemalvarambhia / matchers.rb
Created November 24, 2014 14:20
Factory methods that provide a fluent interface to the NginxPrivilegeMatcher
if defined?(ChefSpec)
def whitelist_requests_from(ip_addresses)
ChefSpec::Matchers::NginxPrivilegeMatcher.new(chef_run, ip_addresses, "allow")
end
def blacklist_requests_from(ip_addresses)
ChefSpec::Matchers::NginxPrivilegeMatcher.new(chef_run, ip_addresses, "deny")
end
end
@hemalvarambhia
hemalvarambhia / nginx_privilege_matcher.rb
Last active August 29, 2015 14:09
A ChefSpec nginx matcher that checks requests from a list of IP addresses have been whitelisted
if defined?(ChefSpec)
module ChefSpec::Matchers
class NginxPrivilegeMatcher
def initialize(runner, ip_addresses, permission="allow")
@runner = runner
@ip_addresses = ip_addresses
@permission = permission
end
@hemalvarambhia
hemalvarambhia / postgresql_matchers.rb
Created November 7, 2014 16:45
An RSpec matcher to check a user is allowed to connect to a database using TCP/IP connections unconditionally.
module PostgresqlMatchers
RSpec::Matchers.define :permit_user do |user|
match do |content|
!(content=~/host #{@database} #{user} 127\.0\.0\.1\/32 trust/).nil?
end
chain :to_connect_to do |database|
@database = database
end
@hemalvarambhia
hemalvarambhia / nginx_matcher_usage_example.rb
Last active August 29, 2015 14:08
An example of how the nginx rpsec matcher is used in serverspec. This was also shown to the London Chef Users Meetup on Tuesday.
describe "AI office" do
describe "/feed URL" do
its(:content) {
should permit(client_machines).access_to("/feed")
}
end
end