Skip to content

Instantly share code, notes, and snippets.

Avatar

Sergei Lebedev superbobry

View GitHub Profile
@superbobry
superbobry / mangle.py
Created Jan 23, 2018
Python name mangling
View mangle.py
>>> class A:
... def __init__(self, __foo):
... print(__foo)
...
>>> dis.dis(A.__init__)
3 0 LOAD_GLOBAL 0 (print)
2 LOAD_FAST 1 (_A__foo)
4 CALL_FUNCTION 1
6 POP_TOP
8 LOAD_CONST 0 (None)
View issue12029.py
>>> Error(abc.ABC, Exception): pass
...
>>> EndOfTheWorld(Exception): pass
...
>>> Error.register(EndOfTheWorld)
<class '__main__.EndOfTheWorld'>
>>> e = EndOfTheWorld()
>>> isintance(e, Error)
True
>>> try:
@superbobry
superbobry / ImplicitTest.scala
Last active Sep 25, 2017
Specializing a method on a value class
View ImplicitTest.scala
import scala.{specialized => spec}
package object foo {
class ImplicitTest(val n: Nothing) extends AnyVal {
def foo[@spec(Double) T](size: Int)(f: Int => T): Array[T] = {
f(42)
???
}
}
}
@superbobry
superbobry / tdf.rst
Last active Mar 30, 2018
TDF format spec
View tdf.rst

TDF

TDF is a binary format developed by the IGV team at Broad Institute.

Overview

Concepts

@superbobry
superbobry / timing.py
Created Sep 23, 2015
Вспомогательный модуль для курса "Алгоритмы: теория и практика. Методы"
View timing.py
import time
from matplotlib import pyplot as plt
def timed(f, args, *, n_iter=100):
acc = float("inf")
for i in range(n_iter):
t0 = time.perf_counter()
f(*args)
@superbobry
superbobry / with-arguments.md
Last active Jan 30, 2020
Пояснение к лекции про декораторы в CS Центре
View with-arguments.md

Хочу ещё раз на примере декоратора trace пояснить, какие типы декораторов используются на практике и как они работают.

Декораторы без аргументов

Общая структура декоратора и пример использования:

def trace(func):
    def inner(*args, **kwargs):
View bbiRead.c
struct chromNameCallbackContext
/* Some stuff that the bPlusTree traverser needs for context. */
{
struct bbiChromInfo *list; /* The list we are building. */
boolean isSwapped; /* Need to byte-swap things? */
};
static void chromNameCallback(void *context, void *key, int keySize, void *val, int valSize)
/* Callback that captures chromInfo from bPlusTree. */
{
@superbobry
superbobry / example.py
Created Dec 22, 2014
Python 2/3 buffer protocol inconsistency
View example.py
# Python 2
>>> b = bytearray(8)
>>> v = memoryview(b)
>>> v[0] = 42
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' does not have the buffer interface
>>> b
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00')
@superbobry
superbobry / gist:44276d49244c381be649
Created Nov 11, 2014
Python generators and 'throw'
View gist:44276d49244c381be649
>>> def g():
... yield 42
...
>>> gen = g()
>>> next(gen)
42
>>> gen.throw(RuntimeError)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in g
@superbobry
superbobry / ChromHMM.java
Created Oct 13, 2014
Academic Java by example
View ChromHMM.java
/**
* ChromHMM - automating chromatin state discovery and characterization
* Copyright (C) 2008-2012 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
You can’t perform that action at this time.