Skip to content

Instantly share code, notes, and snippets.

View umayrh's full-sized avatar

Umayr Hassan umayrh

View GitHub Profile
@rgreenjr
rgreenjr / postgres_queries_and_commands.sql
Last active July 31, 2026 07:52
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), 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(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@felixyz
felixyz / Intro to Common Table Expressions.md
Last active February 5, 2025 15:29
Introduction to transitive closure / Common Table Expressions / iterative queries in SQL

Teh Social Netswork!

CREATE TABLE users (
 id SERIAL PRIMARY KEY,
 name VARCHAR(10) NOT NULL
);

INSERT into users VALUES

@shuxiang
shuxiang / sqla.py
Last active August 7, 2025 08:19
sqlalchemy get model by name and get model by tablename
from flask.ext.sqlalchemy import SQLAlchemy
def get_model(self, name):
return self.Model._decl_class_registry.get(name, None)
SQLAlchemy.get_model = get_model
def get_model_by_tablename(self, tablename):
for c in self.Model._decl_class_registry.values():
if hasattr(c, '__tablename__') and c.__tablename__ == tablename:
return c
@kissgyorgy
kissgyorgy / sqlalchemy_conftest.py
Last active January 18, 2026 14:35
Python: py.test fixture for SQLAlchemy test in a transaction, create tables only once!
from sqlalchemy import create_engine
from sqlalchemy.orm import Session
from myapp.models import BaseModel
import pytest
@pytest.fixture(scope="session")
def engine():
return create_engine("postgresql://localhost/test_database")
@tachyondecay
tachyondecay / datetime-local.py
Last active March 8, 2024 09:18
DateTime WTForms field that assumes input is in local time and converts to UTC for storage
# Requires Arrow package
class DateTimeWidget:
"""Widget for DateTimeFields using separate date and time inputs."""
def __call__(self, field, **kwargs):
id = kwargs.pop('id', field.id)
date = time = ''
if field.data:
dt = arrow.get(field.data).to(current_app.config['TIMEZONE'])
date = dt.format('YYYY-MM-DD')
@SanMuHe
SanMuHe / first_day_of_week.py
Created December 30, 2015 05:59
Python: Given a date, get the first day of its corresponding week
# -*- coding: utf-8 -*-
import datetime
def first_day_of_week(date):
return date + datetime.timedelta(days = -date.weekday())
@jamescarr
jamescarr / shell.py
Last active July 22, 2025 01:09
Test jinja2 out in a console quickly. If you have jinja2 installed locally just open a python REPL and do this!
from jinja2 import Template
text = """
hi {{ name|default(other_name) }}
"""
template = Template(text)
template.render(name="foo") # passing variables here to the text template if needed
template.render(other_name="bar")
@DaniSancas
DaniSancas / neo4j_cypher_cheatsheet.md
Created June 14, 2016 23:52
Neo4j's Cypher queries cheatsheet

Neo4j Tutorial

Fundamentals

Store any kind of data using the following graph concepts:

  • Node: Graph data records
  • Relationship: Connect nodes (has direction and a type)
  • Property: Stores data in key-value pair in nodes and relationships
  • Label: Groups nodes and relationships (optional)
@tzmartin
tzmartin / embedded-file-viewer.md
Last active June 29, 2026 19:42
Embedded File Viewer: Google Drive, OneDrive

Office Web Apps Viewer

('.ppt' '.pptx' '.doc', '.docx', '.xls', '.xlsx')

http://view.officeapps.live.com/op/view.aspx?src=[OFFICE_FILE_URL]

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=[OFFICE_FILE_URL]' width='px' height='px' frameborder='0'>
</iframe>

OneDrive Embed Links

@eguven
eguven / brew-list.sh
Last active July 26, 2026 13:53
List all packages installed using Homebrew and their sizes
# this original one uses values returned from 'brew info'
brew list --formula | xargs -n1 -P8 -I {} \
sh -c "brew info {} | egrep '[0-9]* files, ' | sed 's/^.*[0-9]* files, \(.*\)).*$/{} \1/'" | \
sort -h -r -k2 - | column -t
# faster alternative using 'du'
du -sch $(brew --cellar)/*/* | sed "s|$(brew --cellar)/\([^/]*\)/.*|\1|" | sort -k1h