Skip to content

Instantly share code, notes, and snippets.

View ssimeonov's full-sized avatar

Simeon Simeonov ssimeonov

View GitHub Profile
@ssimeonov
ssimeonov / google_ad_delivery_script.js
Created April 17, 2013 04:06
Anatomy of an online ad: Google ad delivery script
document.write('\x3c!-- Template Id \x3d 13,901 Template Name \x3d Banner Creative (Flash) - In Page Multiples - [DFA] --\x3e\n\x3c!-- Copyright 2006 DoubleClick Inc., All rights reserved. --\x3e\x3cscript src\x3d\x22http://s0.2mdn.net/879366/flashwrite_1_2.js\x22\x3e\x3c/script\x3e\n\x3cSCRIPT LANGUAGE\x3d\x22JavaScript\x22\x3e\n\x3c!--\nfunction DCFlash(id,pVM){\nvar swf \x3d \x22http://s0.2mdn.net/877848/CLB012__Hiking_728x90.swf\x22;\nvar gif \x3d \x22http://s0.2mdn.net/877848/Celebrex_728x90_Unbranded_Backup.3.11.13_1.gif\x22;\nvar minV \x3d 8;\nvar FWH \x3d \x27 width\x3d\x22728\x22 height\x3d\x2290\x22 \x27;\nvar url \x3d escape(\x22http://adclick.g.doubleclick.net/aclk?sa\x3dL\x26ai\x3dBkkTr1ABuUYnfKOz56AHh_4HQCbOj0YsDAAAAEAEgADgAUIC6g9b______wFYs4bGy0xgyYb2iISk7A-CARdjYS1wdWItNjc2MDQyMjg4NTEzMDEyMrIBGHd3dy5kY2xrLWRlZmF1bHQtcmVmLmNvbboBCWdmcF9pbWFnZcgBCdoBIGh0dHA6Ly93d3cuZGNsay1kZWZhdWx0LXJlZi5jb20vmAKIpAHAAgLgAgDqAi00Nzg4L2huLnVzLmhtbnloLmRpci54LngueC9DZWxlYnJleF9BcnRocml0aXP4AoHSHpAD4AOYA-ADqAMB4AQ
@ssimeonov
ssimeonov / array_validator.rb
Last active March 22, 2022 14:48
Enumerable and array validators for ActiveModel::Validations in Rails. Especially useful with document-oriented databases such as MongoDB (accessed via an ODM framework such as Mongoid).
# Syntax sugar
class ArrayValidator < EnumValidator
end
package wordle
/** Wordle solver, game runner & simulator
*
* Optimizes based on a combination of an allowed word list (from the Wordle source code or any
* other source), word frequency data and the move in the game.
*
* @note
* [[Wordle.Game]] is mutable to allow for play in an environment without easy STDIN input. Use
* [[Wordle.Game.nextMove()]]. All words are in lowercase. Patterns are entered as as strings of
@ssimeonov
ssimeonov / gist:4752473
Created February 11, 2013 03:57
I've found that test-driven development is the fastest way for me to learn a new language. There is no better way to bootstrap the learning process than by figuring out how to cobble together a minimal expectation-based testing framework in the new language. The following is the result of my first hour with Lua.
-- #t behaves strangely with hashtables & nil values
function table_length(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function Test(test, expectation)
return {
test = test,
@ssimeonov
ssimeonov / rscript.R
Created June 17, 2012 23:30
Rscript discovery & reloading
# Returns the stack of RScript files
rscript.stack <- function() {
Filter(Negate(is.null), lapply(sys.frames(), function(x) x$ofile))
}
# Returns the current RScript file path
rscript.current <- function() {
stack <- rscript.stack()
as.character(stack[length(stack)])
@ssimeonov
ssimeonov / memory_snapshot.rb
Created July 18, 2013 07:06
Analyze dyno memory use on Heroku with this memory footprint analyzer.
# Memory snapshot analyzer which parses the /proc file system on *nix
#
# Example (run in Heroku console):
#
# ms = MemorySnapshot.new
# 1.upto(10000).map { |i| Array.new(i) }; nil
# ms.snapshot!; nil
# ms.diff 10
# => {"lib/ld-2.11.1.so"=>156, "heap"=>2068, "all"=>2224}
#
# RSpec matcher to spec delegations.
# Forked from https://gist.github.com/txus/807456 with added bug fixes
#
# Usage:
#
# describe Post do
# it { should delegate(:name).to(:author).with_prefix } # post.author_name
# it { should delegate(:month).to(:created_at) }
# it { should delegate(:year).to(:created_at) }
# it { should delegate(:something).to(:'@instance_var') }
@ssimeonov
ssimeonov / a_shell_test.scala
Last active April 16, 2019 08:25
SPARK-9210 test: Spark SQL first() vs. first_value()
// This code is designed to be pasted in spark-shell in a *nix environment
// On Windows, replace sys.env("HOME") with a directory of your choice
import java.io.File
import java.io.PrintWriter
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.SaveMode
import org.apache.spark.sql.SaveMode
val ctx = sqlContext.asInstanceOf[HiveContext]
@ssimeonov
ssimeonov / databricks.scala
Created January 8, 2016 21:23
Some improvements to Databricks' Scala notebook capabilities.
val ctx = sqlContext
import ctx.implicits._
// With nested structs, sometimes JSON is a much more readable form than display()
def showall(df: DataFrame, num: Int): Unit = df.limit(num).toJSON.collect.foreach(println)
def showall(sql: String, num: Int = 100): Unit = showall(ctx.sql(sql), num)
def hivePath(name: String) = s"/user/hive/warehouse/$name"
// Bug workaround
@ssimeonov
ssimeonov / code.scala
Last active September 24, 2018 06:22
SPARK-9343: DROP IF EXISTS throws if a table is missing
import org.apache.spark.sql.hive.HiveContext
val ctx = sqlContext.asInstanceOf[HiveContext]
import ctx.implicits._
// Table test is not present
ctx.tableNames
// ERROR Hive: NoSuchObjectException(message:default.test table not found)
ctx.sql("drop table if exists test")