Skip to content

Instantly share code, notes, and snippets.

@chaoflow
chaoflow / model.py
Created April 20, 2017 10:26
sqlite WITHOUT ROWID with (flask-)sqlalchemy
import flask_sqlalchemy
from sqlalchemy import schema
from sqlalchemy.ext.compiler import compiles
db = flask_sqlalchemy.SQLAlchemy()
class Tag(db.Model):
__table_args__ = {'info': {'without_rowid': True}}
text = db.Column(db.String, primary_key=True)

Most downloaded projects

SELECT
  file.project,
  COUNT(*) as total_downloads,
FROM
  TABLE_DATE_RANGE(
    [the-psf:pypi.downloads],
    TIMESTAMP("20160114"),
@Xion
Xion / sqla_regex.py
Last active May 13, 2021 05:16
Regular expression filters in SQLAlchemy
"""
Module implementing an enhanced string column type for SQLAlchemy
with a support for regular expression operators in Postgres and SQLite.
"""
import re
from sqlalchemy import String as _String, event, exc
from sqlalchemy.engine import Engine
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import BinaryExpression, func, literal
@zed
zed / url2filename.py
Last active September 3, 2022 04:42
Extract filename from an url
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import posixpath
try:
from urlparse import urlsplit
from urllib import unquote
except ImportError: # Python 3
from urllib.parse import urlsplit, unquote
"""
floyd_warshall_fastest() is a fast Python+NumPy implementation of the Floyd-Warshall algorithm
for finding the shortest path distances between all nodes of a weighted Graph. For more details see
http://en.wikipedia.org/wiki/Floyd-Warshall_algorithm
Tests and time comparisons to slower versions are provided.
Result of test_floyd_warshall_compatibility_on_large_matrix():
Matrix size: 100
Slow algorithm (with allocations): 0.726 seconds elapsed
@gromgull
gromgull / unmatchedgroup.py
Created October 20, 2012 06:01
Python "unmatched group" regex replace workaround
import re
def re_sub(pattern, replacement, string):
def _r(m):
# Now this is ugly.
# Python has a "feature" where unmatched groups return None
# then re.sub chokes on this.
# see http://bugs.python.org/issue1519638
# this works around and hooks into the internal of the re module...
@masak
masak / explanation.md
Last active April 11, 2024 02:50
How is git commit sha1 formed

Ok, I geeked out, and this is probably more information than you need. But it completely answers the question. Sorry. ☺

Locally, I'm at this commit:

$ git show
commit d6cd1e2bd19e03a81132a23b2025920577f84e37
Author: jnthn <jnthn@jnthn.net>
Date:   Sun Apr 15 16:35:03 2012 +0200

When I added FIRST/NEXT/LAST, it was idiomatic but not quite so fast. This makes it faster. Another little bit of masak++'s program.

@norman
norman / earthdistance.rb
Last active November 3, 2022 21:20
Geographic Searches With Postgres's Earthdistance and Cube Extensions
#!/usr/bin/env ruby
=begin
= Geographic Searches With Postgres's Earthdistance and Cube Extensions
This program shows how to easily create a Postgres database that uses the Cube
and Earthdistance extensions to perform fast queries on geographic data.
Briefly, the problem this code solves is "show me all places within 50
kilometers of New York City."