Skip to content

Instantly share code, notes, and snippets.

Avatar
😇

Yuichi Goto yasaichi

😇
  • Tokyo, Japan
View GitHub Profile
@yasaichi
yasaichi / x_means.py
Last active Mar 2, 2022
Implementation of X-means clustering in Python
View x_means.py
"""
以下の論文で提案された改良x-means法の実装
クラスター数を自動決定するk-meansアルゴリズムの拡張について
http://www.rd.dnc.ac.jp/~tunenori/doc/xmeans_euc.pdf
"""
import numpy as np
from scipy import stats
from sklearn.cluster import KMeans
@yasaichi
yasaichi / index.rb
Created Mar 13, 2021
Hosting Rails application to Vercel
View index.rb
# NOTE: Put this file into `api` directory
require_relative "../config/environment"
class Handler < Rack::Handler::WEBrick
# Override
def initialize(server, *_options)
super(server, Rails.application)
end
end
@yasaichi
yasaichi / hellworld.s
Last active Aug 15, 2020
Hello World in Assembly
View hellworld.s
.intel_syntax noprefix
.global main
main:
mov rdx, 14 # message length
lea rsi, [rip + message]
mov rdi, 1 # file descriptor (stdout)
mov rax, 1 # system call number (sys_write)
syscall
View ootoya_auto_input.js
(()=>{const vals={nameSei:'山田',nameMei:'太郎',kanaSei:'やまだ',kanaMei:'たろう',telNo:'09001234567',mailAddress:'test@example.com'};for(let[key,val]of Object.entries(vals)){document.getElementsByName(`OrderCreationDelivery[${key}]`).item(0).value=val;}})();
@yasaichi
yasaichi / strict_composed_of.rb
Last active Feb 24, 2020
Strict mode of `composed_of` in Ruby on Rails
View strict_composed_of.rb
ApplicationRecord.singleton_class.prepend(Module.new do
def composed_of(part_id, options = {})
super(part_id, options.except(:strict))
return if !options[:strict] || !options[:mapping]
options[:mapping].flatten.each_slice(2) do |attr_name, _|
next if attr_name.to_sym == part_id.to_sym
define_method("#{attr_name}=") do |_val|
raise "Can't change the attributes mapped to `#{part_id}` directly."
View terminalapp-getafe.terminal
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ANSIBlackColor</key>
<data>
YnBsaXN0MDDUAQIDBAUGFRZYJHZlcnNpb25YJG9iamVjdHNZJGFyY2hpdmVyVCR0b3AS
AAGGoKMHCA9VJG51bGzTCQoLDA0OVU5TUkdCXE5TQ29sb3JTcGFjZVYkY2xhc3NPECcw
LjM1Mjk0MTE3NjUgMC40MzkyMTU2ODYzIDAuNTIxNTY4NjI3NQAQAYAC0hAREhNaJGNs
YXNzbmFtZVgkY2xhc3Nlc1dOU0NvbG9yohIUWE5TT2JqZWN0XxAPTlNLZXllZEFyY2hp
View journey_router_patch.rb
module AppName::JourneyRouterPatch
APP_NAME_GENERIC_ROUTING_INFO_KEY = "app_name.generic_routing_info".freeze
def call(env)
response_array = super
# NOTE: When Journey::Router#call can't resolve a request, it returns an array like:
# [404, {'X-Cascade' => 'pass'}, ['Not Found']]
# ref. https://github.com/rails/journey/blob/v1.0.4/lib/journey/router.rb#L80
if response_array[1]["X-Cascade"] != "pass" && env.key?(APP_NAME_GENERIC_ROUTING_INFO_KEY)
@yasaichi
yasaichi / okuribito.rb
Last active May 21, 2016
draft of okuribito
View okuribito.rb
require "yaml"
require "active_support"
require "active_support/core_ext"
config = YAML.load_file("okuribito.yml")
class Okuribito
CLASS_METHOD_SYMBOL = "."
INSTANCE_METHOD_SYMBOL = "#"
PATTERN = /\A(?<symbol>[#{CLASS_METHOD_SYMBOL}#{INSTANCE_METHOD_SYMBOL}])(?<method_name>.+)\z/
@yasaichi
yasaichi / attr_accessor_extension.rb
Last active Aug 29, 2015
Implementation "#to_h", returning a hash which includes the attribute name and its value
View attr_accessor_extension.rb
require 'set' # Railsの場合不要
module AttrAccessorExtension
def to_h
Hash[self.class.__send__(:attributes).sort.map { |name| [name, public_send(name)] }]
end
def self.included(klass)
klass.extend(ClassMethods)
end
@yasaichi
yasaichi / neighbor_records.rb
Created Apr 19, 2015
Instance methods for ActiveRecord to fetch the "neighbor" records
View neighbor_records.rb
module NeighborRecords
def previous_one(col_name = :id)
condition = self.class.arel_table[col_name].lt(public_send(col_name))
self.class.where(condition).order(col_name => :desc).first
end
def next_one(col_name = :id)
condition = self.class.arel_table[col_name].gt(public_send(col_name))
self.class.where(condition).order(col_name => :asc).first
end