Instantly share code, notes, and snippets.

# Matthew Planchardmplanchard

• Sort options
Created Mar 13, 2019
2D Array Problem
View 2d_array.py
 test_array = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], ] # [2][0]
Created Jan 3, 2019
Timing of Static Methods vs. Inline Functions
View time_inline_v_static.py
 """ ======================================================================== Timing Static Methods vs. Inline Functions ======================================================================== A simple test to compare the overhead of calling static functions vs. defining and calling inline functions. At least in this experiment, static methods are ~15-20% faster. This is of course not even considering their improvements in testability
Created Feb 18, 2018
pytest parametrize stack
View pytest_parametrize_stack.py
 import pytest @pytest.mark.parametrize('foo', ('a', 'b')) @pytest.mark.parametrize('bar', (True, False)) def test_multi(foo, bar): print(foo, bar)
Created Feb 18, 2018
pytest_parametrize_product
View pytest_parametrize_product.py
 import itertools import pytest @pytest.mark.parametrize( 'foo, bar', itertools.product(('a', 'b'), (True, False)) ) def test_multi(foo, bar): print(foo, bar)
Created Feb 18, 2018
pytest parametrize multi
View pytest_parametrize_multi.py
 import pytest @pytest.mark.parametrize('foo, bar', ( ('a', True), ('a', False), ('b', True), ('b', False) )) def test_multi(foo, bar): print(foo, bar)
Last active Feb 18, 2018
pytest parametrize basic
View pytest_parametrize_basic.py
 import pytest @pytest.mark.parametrize('foo', ('a', 'b')) def test_something(foo): print(foo)
Created Jan 23, 2018
Python 2/3 Compatible Version Checking without Third Party Packages
View version_check_2_3.py
 """This gist describes how to check which version of Python is running. This one is easy enough that there's really no reason to reach for six's PY2/PY3 attributes. """ from sys import version_info # verison_info acts like a named tuple of the form # (major, minor, micro, releaselevel, serial), so standard tuple
Created Jan 23, 2018
Python 2/3 Compatible Metaclasses Without Third Party Dependencies
View metaclasses_2_3.py
 """This example uses the ABCMeta class as its exemplar metaclass.""" from abc import ABCMeta # Create an instance of the metaclass. We can define __slots__ to be # an empty tuple, because we don't need this class to store any attributes. # This instance can be used multiple times, imported, etc., as desired. ABC = ABCMeta('ABC', (object,), {'__slots__': ()})
Created Nov 28, 2017
PyTest memory utilization test
View pytest_logtest.py
 from logging import basicConfig, getLogger import pytest basicConfig() log = getLogger() log.setLevel('DEBUG') def func_that_logs_a_lot(): for i in range(1000): log.info('foo output %s/1000', i)
Created Mar 1, 2017
Datetime New Obj vs Replace
View datetime.py
 from datetime import datetime from timeit import timeit iterations = int(1e6) def method_one(): now = datetime.now() hour = datetime(now.year, now.month, now.day, now.hour)
You can’t perform that action at this time.