Skip to content

Instantly share code, notes, and snippets.

View swanandp's full-sized avatar
🖥️

Swanand Pagnis swanandp

🖥️
View GitHub Profile
@swanandp
swanandp / swastika.py
Created March 27, 2024 06:56
Python code to generate the Hindu symbol Swastika of any given size
# generate a स्वस्तिक (swastika) of arm size n
# for n = 3
# * * * * *
# * *
# * *
# * * * * * * *
# * *
# * *
# * * * * *
@swanandp
swanandp / 1_recurstive_cte.sql
Last active November 29, 2023 17:23
A demonstration of recursive query in SQL
CREATE TABLE posts
(
id bigserial PRIMARY KEY,
content text NOT NULL,
reply_to_id bigint REFERENCES posts (id)
);
TRUNCATE posts RESTART IDENTITY; -- this deletes all rows and restarts the auto-increment ID
/*
@swanandp
swanandp / article_service.rb
Created March 29, 2023 15:12
Ruby Wrapper for Article Service HTTP API
class ArticlesService
include HTTParty
base_uri "https://api.example.com"
read_timeout 5 # always have timeouts!
# debug_output $stdout # for quick access during debugging
attr_reader :auth, :headers
def initialize
@swanandp
swanandp / fiends_not_a_typo.md
Created December 13, 2022 16:03
Modelling "Friendships" in PostgreSQL

For this particular problem, of modelling mutual friendship what we truly need is a "SET" data type. But Postgres doesn't really have a set datatype. However, I realised that tsvectors are sorted sets and can be used for this. So, here's a fun solution with a single row: tsvectors

Our schema:

create table friendships (
  id bigint primary key,
  friends tsvector 
);
@swanandp
swanandp / 0_schema.sql
Last active September 4, 2020 09:21
Fearless Joins Companion SQL
CREATE TABLE rumours
(
id BIGSERIAL PRIMARY KEY,
description TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE tidbits
(
id BIGSERIAL PRIMARY KEY,
@swanandp
swanandp / itunesicon.rb
Created December 3, 2019 08:08 — forked from ttscoff/itunesicon.rb
Retrieve a 512 x 512px icon for an iOS app
#!/usr/bin/ruby
# encoding: utf-8
#
# Updated 2017-10-25:
# - Defaults to large size (512)
# - If ImageMagick is installed:
# - rounds the corners (copped from @bradjasper, https://github.com/bradjasper/Download-iTunes-Icon/blob/master/itunesicon.rb)
# - replace original with rounded version, converting to png if necessary
#
# Retrieve an iOS app icon at the highest available resolution
@swanandp
swanandp / postgres_queries_and_commands.sql
Created December 7, 2017 07:15 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(query_start, clock_timestamp()), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(query_start, clock_timestamp()), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@swanandp
swanandp / posts_service.rb
Last active December 27, 2019 16:56
Interacting with a RESTful posts service using HTTParty
class PostsService
include HTTParty
base_uri "https://api.example.com"
read_timeout 5 # always have timeouts!
# debug_output $stdout # for quick access during debugging
attr_reader :auth, :headers
def initialize(user)
@swanandp
swanandp / And-dot-demo.rb
Created May 31, 2017 08:47
Ruby's &. operator is deceptive. It doesn't behave the same at foo && foo.something when foo is false.
irb(main):004:0> false&.foo
NoMethodError: undefined method `foo' for false:FalseClass
from (irb):4
from /Users/swanand/.rbenv/versions/2.3.1/bin/irb:11:in `<main>'
irb(main):005:0> false && false.foo
=> false
irb(main):006:0>
# Valid syntax
def do_stuff(arg1)
do_something(arg1)
rescue AnError => e
process_error(e)
end
# Syntax Error
do_stuff = lambda do |arg1|
do_something(arg1)