Skip to content

Instantly share code, notes, and snippets.

View camelcaseblog's full-sized avatar

camelcaseblog

View GitHub Profile
class Author(models.Model):
id = models.AutoField(primary_key=True, null=False)
name = models.TextField
class Post(models.Model):
id = models.AutoField(primary_key=True, null=False)
content = models.TextField(null=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE)
ids = [a.author.id for a in Post.objects.filter(content__contains="camelCase")] # 1
const { compareTwoStrings: sørensenDice } = require("string-similarity")
console.log(1 - sørensenDice("חצילים", "החציל"))
// 0.33333333333333337
console.log(1 - sørensenDice("פלפל ירוק ובצל", "פלפל ובצל ירוק"))
// 0.09090909090909094
const levenshtein = require("js-levenshtein")
const normalizedLevenshtein = (a, b) => levenshtein(a, b) / Math.max(a.length, b.length)
console.log(normalizedLevenshtein("headphones", "headph0nes"))
// 0.1
console.log(normalizedLevenshtein("לימון", "לימונים"))
// 0.42857142857142855
from ngram import NGram
print(NGram.compare('החציל', 'חצילים'))
# 0.15384615384615385
print(NGram.compare('פלפל ובצל ירוק', 'פלפל ירוק ובצל'))
# 0.6
create index ix1 on payment(payment_date);
EXPLAIN ANALYZE
SELECT *
FROM payment
WHERE payment_date = '2018-06-09'
ORDER BY amount;
EXPLAIN ANALYZE
SELECT *
FROM payment
WHERE payment_date = '2020-01-13'
ORDER BY amount;
CREATE TABLE payment (
id int primary key,
client_id int,
amount float,
payment_date date
);
INSERT INTO "payment" (id, client_id, amount, payment_date)
values(1, 250, 49.26, '2019-03-22'), (2, 143, 79.66, '2019-02-19'), (3, 87, 59.66, '2019-10-22'), (4, 159, 29.37, '2018-03-16'), (5, 213, 97.10, '2019-05-06'), (6, 225, 86.02, '2018-05-01'), (7, 85, 90.51, '2019-06-17'), (8, 58, 96.20, '2019-10-27'), (9, 200, 87.65, '2018-08-24'), (10, 238, 62.82, '2018-04-24'), (11, 21, 75.02, '2019-03-17'), (12, 239, 80.02, '2018-07-13'), (13, 43, 37.26, '2019-02-16'), (14, 105, 48.98, '2019-06-06'), (15, 232, 70.09, '2019-08-22'), (16, 237, 95.45, '2019-01-14'), (17, 171, 24.83, '2019-03-22'), (18, 195, 6.20, '2018-03-28'), (19, 20, 80.56, '2020-02-22'), (20, 142, 59.38, '2019-11-30'), (21, 211, 14.78, '2018-07-16'), (22, 58, 58.35, '2019-05-21'), (23, 120, 49.71, '2018-08-05'), (24, 202, 40.03, '2019-12-19'), (25, 225, 0.03, '2019-09-13'), (26, 296, 84.11, '2019-07-30'), (27, 5, 20.06, '2019-12-30'), (28, 94, 80.1
There should be one-- and preferably only one --obvious way to do it.
>>> import this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.