Skip to content

Instantly share code, notes, and snippets.

@Xowap
Xowap / anonymize_email.py
Created July 3, 2017 08:34
A script to anonymize emails in a SQL dump
#!/usr/bin/env python3
# coding: utf-8
import argparse
import re
from uuid import uuid4
from hashlib import sha256
SALT = str(uuid4()).encode()
import re
import json
import asyncio
from textwrap import dedent
from urllib.parse import urljoin
from aiohttp import web
async def index(_):
return web.Response(text="""
@Xowap
Xowap / patch_idea_env.py
Last active April 10, 2017 10:42
Patch IDEA run configurations with environment variables from a file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import argparse
import re
import os
import subprocess
@Xowap
Xowap / romanize_numbers.sql
Created March 30, 2017 13:30
Romanize numbers inside a string (turn "4" to "IV", and so on)
create or replace
function romanize_numbers(s text)
returns text
as $$
select
string_agg(
m [1] ||
case when char_length(m [2]) between 1 and 3
then trim(to_char(m [2] :: int, 'RN'))
else m [2]
@Xowap
Xowap / upsert.py
Created March 29, 2017 16:22
Django Upsert
from collections import namedtuple
def auto_dict(val):
# type: (Union[Dict, NamedTuple]) -> Dict
"""
Transforms named tuples into dict. Does nothing if `val` is already a dict.
:param val: a NamedTuple instance or a dict
:return: dict version of val
"""
@Xowap
Xowap / slack_purge.py
Created March 22, 2017 08:51
A script to purge slack files
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 :
import requests
import time
import os
token = os.getenv('SLACK_TOKEN')
# Delete files older than this:
ts_to = int(time.time()) - (3600 * 24 * 30)
@Xowap
Xowap / ftf.js
Created September 9, 2016 10:21
Find which DOM item makes your container go too big.
/*vim: fileencoding=utf8 tw=100 expandtab ts=4 sw=4 */
/*jslint indent: 4, maxlen: 100, browser: true */
/*globals console, Element*/
(function (exports) {
'use strict';
function findTheFucker(root, width, restoreAll) {
var fucker;
# vim: fileencoding=utf-8 tw=100 expandtab ts=4 sw=4 :
import json
import hmac
from hashlib import sha256
from uuid import uuid4
from time import time
def force_bytes(s):
point = geometry.centroid
if point.srid is not None:
point.transform(4326)
try:
tzid = self.filter(zone__contains=point)[0].tzid
return tzid, point
except IndexError:
return None, point
class TimeZone(models.Model):
objects = TimeZoneManager()
zone = PolygonField(spatial_index=True)
tzid = models.CharField(max_length=100)