Skip to content

Instantly share code, notes, and snippets.

@misja
misja / singleton.py
Last active January 3, 2022 00:12
Python None is an object (and a singleton)
# None is of type NoneType
print(f"{None} is of type", type(None))
# Each object contains a reference to its class,
# let's use this to create a new instance of NoneType
NewNone = None.__class__()
# NoneType is a *singleton*, so should be equal
assert NewNone is None
@misja
misja / launch.json
Created February 13, 2021 20:41
VSCode debug launch configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
// "type": "pwa-firefox",
// "type": "pwa-edge",
"request": "launch",
"name": "Launch Workspace",
"file": "${workspaceFolder}"

Keybase proof

I hereby claim:

  • I am misja on github.
  • I am misja (https://keybase.io/misja) on keybase.
  • I have a public key ASBXBkMToKKYtW9ne9alJgepmgdh9DWtNJbPQD-BjPn2zgo

To claim this, I am signing this object:

import nsq
import asyncio
import tornado
import tornado.ioloop
import tornado.platform.asyncio
queue = asyncio.Queue()
async def go():
def enqueue_message(message):
import nsq
import time
import asyncio
import tornado
import tornado.ioloop
import tornado.platform.asyncio
def pub_callback(conn, data):
print(data)
@misja
misja / gist:d9b88ea849b9ab86af95
Last active August 29, 2015 14:21
A data cleaning cerberus validator (remove unknown fields)
import copy
import collections
from cerberus import Validator
class CleaningValidator(Validator):
def validate_clean(self, document, *args, **kwargs):
self.validate(document, *args, **kwargs)
if self.errors and self.allow_unknown == False:
self._clean()
return len(self.errors) == 0
@misja
misja / country_detect.py
Created March 16, 2014 11:28
Get a country for a longitude / latitude point. World Borders Dataset to be found at http://thematicmapping.org/downloads/world_borders.php
import rtree
import cPickle
import osgeo.ogr
import shapely.wkb
import shapely.speedups
shapely.speedups.enable()
class FastRtree(rtree.Rtree):
def dumps(self, obj):
@misja
misja / nltk_langdetect.py
Created April 27, 2013 11:22
Language detection with NLTK. Snippet I had up on my posterous.
from nltk.util import trigrams as nltk_trigrams
from nltk.tokenize import word_tokenize as nltk_word_tokenize
from nltk.probability import FreqDist
from nltk.corpus.util import LazyCorpusLoader
from nltk.corpus.reader.api import CorpusReader
from nltk.corpus.reader.util import StreamBackedCorpusView, concat
class LangIdCorpusReader(CorpusReader):
'''
LangID corpus reader
@misja
misja / google_spreadsheet.py
Last active October 7, 2015 05:18
Read a Google spreadsheet, with worksheets
import gdata.spreadsheet.service
class DocumentIterator(object):
index = 0
entries = []
def next(self):
if self.index >= self.count:
self.index = 0
raise StopIteration
@misja
misja / opengraph.py
Last active December 20, 2021 08:30
Quick way to extract opengraph and twitter card attributes from a page in python
from lxml import html
from urllib import request
from collections import defaultdict
url = "http://www.youtube.com/watch?v=IyLZ6RXJ8Eg"
doc = html.parse(request.urlopen(url))
data = defaultdict(dict)
props = doc.xpath('//meta[re:test(@name|@property, "^twitter|og:.*$", "i")]',
namespaces={"re": "http://exslt.org/regular-expressions"})