Skip to content

Instantly share code, notes, and snippets.

View Tuhaj's full-sized avatar
🎯
Focusing

Piotr Zientara Tuhaj

🎯
Focusing
View GitHub Profile
# Example INSERT INTO with ON CONFLICT behavior
SAVED_FIELDS="state, name, type, instance_id, public_ip, launch_time, region, profile, publicdnsname"
row='{"state":"running","name":"important_key_name","type":"t2.medium","instance_id":"i-0a1b2c3d4e556677f","public_ip":"11.111.1.111","launch_time":"2019-01-30T08:00:00.000Z","region":"eu-central-1","profile":"","publicdnsname":"ec2-xx-xxx-x-x.eu-central-1.compute.amazonaws.com"}'
DATABASE="aws_instances"
TABLE_NAME="aws_ec2"
psql -c "INSERT INTO $TABLE_NAME($SAVED_FIELDS) SELECT $SAVED_FIELDS from json_populate_record(NULL::$TABLE_NAME, '${row}')
ON CONFLICT (instance_id)
DO UPDATE
function randomString() {
return Math.floor((1 + Math.random()) * 0x10000).toString(32)
}
const randomId = () => (`${randomString()}-${randomString()}-${randomString()}`)
@Tuhaj
Tuhaj / binomial.rb
Created July 12, 2016 11:42
simple method to count binomial coefficients https://en.wikipedia.org/wiki/Binomial_coefficient
module Probability
def binomial(n, k)
return 1 if k == 0 || n == k
if n < 0 || k < 0
raise ArgumentError, 'both arguments must be greater than 0'
elsif k >= n
raise ArgumentError, 'second argument can\'t be greater than first one'
end
# Sonic Pi Diggy diggy hole chorus based on https://musescore.com/user/1121936/scores/1022696
# :Ab, :Bb, :Db, :Eb
define :play_note do |note|
play note[0], release: note[1]
sleep note[1]
end
loop do
[
# For mac XOS
# dependent from SetFile
# uses it like: SetFile -d '12/31/1999 23:59:59' photo.jpg in order to change
# the displayed creation date of a file
# usage: enter directory with all directories with names, which are date-like
# yyyymmdd - so y - year; m - month; d - day
# 1. put file on the desktop
# 2. enter irb
# 3. write in console: load '~/Desktop/change_exifs.rb'
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'activerecord', '4.2.1'
gem 'state_machines'
gem 'sqlite3'
gem 'minitest'
GEMFILE
system 'bundle'