Skip to content

Instantly share code, notes, and snippets.

View zkan's full-sized avatar
🐻
Stay Hungry. Stay Foolish.

Kan Ouivirach zkan

🐻
Stay Hungry. Stay Foolish.
View GitHub Profile
from contextlib import ExitStack
import random
import unittest
from unittest.mock import patch
class TryMockUsingExitStackTest(unittest.TestCase):
def test_mock_random_twice(self):
with ExitStack() as stack:
mock_randrange = stack.enter_context(
import random
import unittest
from unittest.mock import patch
class TryMockUsingContextManagerTest(unittest.TestCase):
def test_mock_random_twice(self):
with patch('__main__.random.randrange') as mock_randrange:
with patch('__main__.random.randint') as mock_randint:
try_mock_using_context_manager()
@zkan
zkan / try_mock_using_decorator.py
Last active January 5, 2018 17:07
Try Mock using Decorator
import random
import unittest
from unittest.mock import patch
class TryMockUsingDecoratorTest(unittest.TestCase):
@patch('__main__.random.randrange')
@patch('__main__.random.randint')
def test_mock_random_twice(self, mock_randint, mock_randrange):
try_mock_using_decorator()

Effective Engineer - Notes

What's an Effective Engineer?

  • They are the people who get things done. Effective Engineers produce results.

Adopt the Right Mindsets

import csv
from faker import Faker
fake = Faker()
with open('transactions.csv', 'r') as file_:
with open('anonymized_transactions.csv', 'w') as new_file:
reader = csv.DictReader(file_)
writer = csv.DictWriter(new_file, fieldnames=reader.fieldnames)
@zkan
zkan / woocommerce-cart-count-shortcode.php
Created September 28, 2017 07:04
WooCommerce Cart Count Shortcode (quick patch) for "PHP error Call to a member function get_cart_contents_count()"
<?php
/*
* Plugin Name: WooCommerce Cart Count Shortcode
* Plugin URI: https://github.com/prontotools/woocommerce-cart-count-shortcode
* Description: Display a link to your shopping cart with the item count anywhere on your site with a customizable shortcode.
* Version: 1.0.4
* Author: Pronto Tools
* Author URI: http://www.prontotools.io
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
@zkan
zkan / config.ru
Created August 1, 2017 02:23
HTTP Basic Authentication for Jekyll on Heroku
require 'rack/jekyll'
require "rack/rewrite"
use Rack::Auth::Basic, "Restricted Area" do |username, password|
[username, password] == ['admin', 'admin'] or [username, password] == ['zkan', '123'] or [username, password] == ['cory', 'cool']
end
use Rack::Rewrite do
rewrite %r{/(.+)}, lambda { |match, rack_env|
if File.exists?('_site/' + match[1] + '.html')
@zkan
zkan / download_anonymous_icons_from_drive.rb
Created August 1, 2017 00:11 — forked from rockwotj/download_anonymous_icons_from_drive.rb
Downloads all of the icons of anonymous animals from Google Drive
#!/usr/bin/env ruby
icon_list = "alligator, anteater, armadillo, auroch, axolotl, badger, bat, beaver, buffalo, camel, chameleon, cheetah, chipmunk, chinchilla, chupacabra, cormorant, coyote, crow, dingo, dinosaur, dolphin, duck, elephant, ferret, fox, frog, giraffe, gopher, grizzly, hedgehog, hippo, hyena, jackal, ibex, ifrit, iguana, koala, kraken, lemur, leopard, liger, llama, manatee, mink, monkey, narwhal, nyan cat, orangutan, otter, panda, penguin, platypus, python, pumpkin, quagga, rabbit, raccoon, rhino, sheep, shrew, skunk, slow loris, squirrel, turtle, walrus, wolf, wolverine, wombat"
for icon in icon_list.split ', ' do
element = icon.sub ' ', ''
`curl https://ssl.gstatic.com/docs/common/profile/#{element}_lg.png -o icons/#{element}.png`
end
@zkan
zkan / wilson.py
Created July 16, 2017 14:55
Lower bound of Wilson score confidence interval (3 rating scale)
"""
How Not To Sort By Average Rating:
http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
Note: Lower bound of a 95% confidence interval
"""
import math
import unittest