Skip to content

Instantly share code, notes, and snippets.

View hakunin's full-sized avatar

Michal Hantl hakunin

  • Ostrava, Czech Republic
  • 20:46 (UTC -12:00)
View GitHub Profile
@hakunin
hakunin / python_better_trace_printer.py
Last active April 27, 2024 13:40
Better trace printer for Python/Django
import logging
import traceback
import os
from colorama import Fore, Back, Style, init as colorama_init
colorama_init()
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class AdvancedTraceFormatter(logging.Formatter):
@hakunin
hakunin / AuthController.ex
Last active May 15, 2017 18:47
Coinbase Oauth2 strategy for elixir / phoenix.
defmodule Myapp.Web.AuthController do
use BlockViz.Web, :controller
def connect(conn, _params) do
conn |> redirect(external: BlockViz.Oauth2.Strategies.Coinbase.authorize_url!)
end
def callback(conn, params) do
IO.inspect params
client = BlockViz.Oauth2.Strategies.Coinbase.get_token!(code: params["code"])
@hakunin
hakunin / mutlibyte_upcase_first.rb
Created February 18, 2017 13:50
Multi byte upcase_first polyfill
class String
# polyfill until Rails 5, with support for multi-byte chars
# https://glaucocustodio.github.io/2016/05/19/rails-5-new-upcase-first-method/
# https://coderwall.com/p/b74yda/rails-upcase-downcase-for-non-ascii-strings
unless method_defined? :upcase_first
def upcase_first
return self if length == 0
self.mb_chars[0].upcase.mb_chars.concat(self[1..-1]).to_s
end
end
@hakunin
hakunin / approach_01_random_ids.rb
Last active November 16, 2016 11:01
Prevent different records with matching IDs from falsely passing tests
# put this inside spec/support/random_ids.rb
# NOTE: this works, but will break tests whee something like Article.last is used
# a more clever approach is used in 02 below
# Some tests may pass when they should have failed
# due to Rails using sequential ids. This allows us
# to fix that by having random ids.
class ActiveRecord::Base
before_create :generate_random_id
/**
* Copyright 2015, Yahoo Inc.
* Copyrights licensed under the MIT License. See the accompanying LICENSE file for terms.
*/
'use strict';
import invariant from 'invariant';
var ignore = false

So since the API really desn't work as you'd expect it to, I am documenting this here at least.

I created a testing board with cards A, B, C, ... , M so I can test on it.

First, grabbing all cards works as expected.

api.find(:boards, '56af532ae7a74a5fcac32e66').cards.map { |c| [c.name, c.id] }

=> [
 ["A", "56af53367f15b163c0e345fd"],

["B", "56af533783d4fccc1cce3941"],

@hakunin
hakunin / Level 5.rb
Last active October 24, 2015 14:34
Ruby warrior
class Player
def play_turn(warrior)
@state ||= :init
@health ||= 0
@init ||= true
@got_hit = (warrior.health < @health)
@hakunin
hakunin / activerecord.rb
Last active August 29, 2015 14:18
Work around Errno::ECONNREFUSED when SOLR is not running
=begin
1) include following ruby file from your config/application.rb like so:
require File.expand_path('../../lib/ext/active_record', __FILE__)
2) add auto_commit_after_request: false to your config/sunspot.yml
development:
solr:
# A sample Guardfile
# More info at https://github.com/guard/guard#readme
# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rsspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separetly)
# adapted from https://gist.github.com/johdax/771943
# modified to work without creating new String methods
class StringDistance
def self.damerau_levenshtein(str1, str2)
d = Array.new(str1.size+1){Array.new(str2.size+1)}
for i in (0..str1.size)
d[i][0] = i
end
for j in (0..str2.size)