This file contains 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
_setattr_cached = object.__setattr__ | |
class A: | |
__slots__ = ('x', 'y', 'z', 'a', 'b', 'c') | |
def __init__(self, x, y, z, a, b, c): | |
_setattr = _setattr_cached.__get__(self, A) | |
_setattr('x', x) | |
_setattr('y', y) | |
_setattr('z', z) |
This file contains 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
import attr | |
import pickle | |
@attr.s(frozen=True) | |
class A(object): | |
x = attr.ib() | |
@attr.s(slots=True) | |
class B(object): |
This file contains 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 hypothesis.stateful import RuleBasedStateMachine, run_state_machine_as_test, rule, precondition | |
def test_t(): | |
class StatefulTest(RuleBasedStateMachine): | |
def __init__(self): | |
print("init") | |
super().__init__() | |
self.a = False | |
self.b = False | |
self.c = False |
This file contains 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
>>> msgpack.loads(msgpack.dumps(b"TEST", encoding='utf8', use_bin_type=True), encoding='utf8') | |
b'TEST' | |
>>> msgpack.loads(msgpack.dumps("TEST", encoding='utf8', use_bin_type=True), encoding='utf8') | |
'TEST' |
This file contains 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 cpython.object cimport descrgetfunc, Py_TYPE | |
cdef class FrozenSlotDescriptor(object): | |
""" | |
Wraps the normal Python slot descriptor, disabling write access. | |
""" | |
cdef descrgetfunc get | |
cdef object descriptor | |
def __cinit__(self, orig): |
This file contains 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
import pyximport; pyximport.install() | |
from frozen import FrozenSlotDescriptor | |
class A(object): | |
__slots__ = ('a',) | |
A._old = A.a | |
A.a = FrozenSlotDescriptor(A.a) |
This file contains 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 attr._make import _attrs_to_tuple as attrs_to_tuple | |
class A: | |
def __init__(self, a): | |
self.a = a | |
def __eq__(self, other): | |
""" | |
Automatically created by attrs. | |
""" | |
if other.__class__ is self.__class__: |
This file contains 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
========================================================================================================================== FAILURES =========================================================================================================================== | |
_________________________________________________________________________________________________________________________ test_lists __________________________________________________________________________________________________________________________ | |
def test_lists(): | |
> assert [i for i in range(20)] == [i for i in range(21)] | |
E assert [0, 1, 2, 3, 4, 5, ...] == [0, 1, 2, 3, 4, 5, ...] | |
E Right contains more items, first extra item: 20 | |
E Full diff: | |
E - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] | |
E + [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] |
This file contains 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
> pyperf timeit -g -s "i = [(e, 1) for e in range(1000)]" "[e[0] for e in i]" | |
..................... | |
39.5 us: 1 ########### | |
39.6 us: 2 ####################### | |
39.7 us: 4 ############################################# | |
39.9 us: 6 #################################################################### | |
40.0 us: 6 #################################################################### | |
40.1 us: 7 ############################################################################### | |
40.2 us: 6 #################################################################### | |
40.3 us: 6 #################################################################### |
This file contains 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
import attr | |
@attr.s | |
class C: | |
a = attr.ib() | |
__hash__ = attr.hash_by_values |