Skip to content

Instantly share code, notes, and snippets.

@mplanchard
mplanchard / req_install.py
Created November 4, 2015 17:45
Pip Uninstall Entry Points
if dist.has_metadata('entry_points.txt'):
config = configparser.SafeConfigParser()
config.readfp(
FakeFile(dist.get_metadata_lines('entry_points.txt'))
)
if config.has_section('console_scripts'):
for name, value in config.items('console_scripts'):
if dist_in_usersite(dist):
bin_dir = bin_user
else:
@mplanchard
mplanchard / jados_itertools.py
Created April 21, 2016 22:06
Quick and dirty permutations script
from itertools import permutations
def get_combinations(layer):
possibles = (-layer, -layer + 1, 0, layer - 1, layer)
combinations = permutations(possibles, 2)
return_combinations = []
for combination in combinations:
if layer in combination or -layer in combination:
return_combinations.append(combination)
return return_combinations
- name: Some play
hosts: some_hosts
roles:
- role: some_role
some_variable: variable value
another_variable: another value
@mplanchard
mplanchard / ansible_sysfile.yaml
Last active February 5, 2017 01:01
ansible_sysfile
# Permission denied error
- name: ensure transparent huge pages are disabled
copy:
content: never
dest: "{{ celery_thp_file }}"
owner: root
group: root
mode: 0644
notify: restart redis-server service
become: yes
@mplanchard
mplanchard / datetime.py
Created March 1, 2017 17:51
Datetime New Obj vs Replace
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)
@mplanchard
mplanchard / pytest_logtest.py
Created November 28, 2017 22:02
PyTest memory utilization test
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)
@mplanchard
mplanchard / metaclasses_2_3.py
Created January 23, 2018 14:35
Python 2/3 Compatible Metaclasses Without Third Party Dependencies
"""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__': ()})
@mplanchard
mplanchard / version_check_2_3.py
Created January 23, 2018 14:47
Python 2/3 Compatible Version Checking without Third Party Packages
"""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
@mplanchard
mplanchard / pytest_parametrize_basic.py
Last active February 18, 2018 03:46
pytest parametrize basic
import pytest
@pytest.mark.parametrize('foo', ('a', 'b'))
def test_something(foo):
print(foo)
@mplanchard
mplanchard / pytest_parametrize_multi.py
Created February 18, 2018 03:48
pytest parametrize multi
import pytest
@pytest.mark.parametrize('foo, bar', (
('a', True),
('a', False),
('b', True),
('b', False)
))
def test_multi(foo, bar):
print(foo, bar)