This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def default(cb, value=None): | |
| # noinspection PyBroadException | |
| try: | |
| return cb() | |
| except: | |
| return value | |
| o = object() | |
| print(default(lambda: o.does.noot.exist, 42)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import tzinfo, timedelta | |
| from math import floor | |
| from django.contrib.gis.db.models.fields import PolygonField | |
| from django.contrib.gis.db.models.manager import GeoManager | |
| from django.contrib.gis.geos.geometry import GEOSGeometry | |
| from django.db import models | |
| from osgeo import ogr | |
| from pytz import timezone | |
| from pytz.exceptions import UnknownTimeZoneError |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source = ogr.Open(path) | |
| if source is None: | |
| raise IOError('The source file {} is invalid or inexistant'.format(path)) | |
| layer = source.GetLayer(0) | |
| time_zones = [] | |
| for feature in layer: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TimeZone(models.Model): | |
| objects = TimeZoneManager() | |
| zone = PolygonField(spatial_index=True) | |
| tzid = models.CharField(max_length=100) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |