Skip to content

Instantly share code, notes, and snippets.

View Tjorriemorrie's full-sized avatar
💭
most repos are private

Tjorriemorrie Tjorriemorrie

💭
most repos are private
  • South Africa
  • 02:52 (UTC +02:00)
View GitHub Profile
@Tjorriemorrie
Tjorriemorrie / pytest-fixture-modularization.md
Created January 8, 2019 00:53 — forked from peterhurford/pytest-fixture-modularization.md
How to modularize your py.test fixtures

Using py.test is great and the support for test fixtures is pretty awesome. However, in order to share your fixtures across your entire module, py.test suggests you define all your fixtures within one single conftest.py file. This is impractical if you have a large quantity of fixtures -- for better organization and readibility, you would much rather define your fixtures across multiple, well-named files. But how do you do that? ...No one on the internet seemed to know.

Turns out, however, you can define fixtures in individual files like this:

tests/fixtures/add.py

import pytest

@pytest.fixture
@Tjorriemorrie
Tjorriemorrie / memalloc.py
Created March 27, 2018 23:34
python tracemalloc display top 10
def display_top(snapshot, key_type='lineno', limit=10):
snapshot = snapshot.filter_traces((
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
))
top_stats = snapshot.statistics(key_type)
print("Top %s lines" % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
@Tjorriemorrie
Tjorriemorrie / youtube.py
Created November 7, 2017 23:42
convert youtube to mp3
from __future__ import unicode_literals
import youtube_dl
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
@Tjorriemorrie
Tjorriemorrie / example.py
Last active July 28, 2017 03:06
tensorforce
import os
from collections import deque
import numpy as np
from tensorforce import Configuration
from tensorforce.agents.random_agent import RandomAgent
from tensorforce.agents import TRPOAgent
from tensorforce.core.networks import layered_network_builder
state = np.array([0] * 9, dtype='float')
@Tjorriemorrie
Tjorriemorrie / example.py
Created October 16, 2016 19:56
python copy and deepcopy
from copy import copy, deepcopy
class A(object):
def __init__(self):
print 'init'
self.v = 10
self.z = [2,3,4]
def __copy__(self):
cls = self.__class__
@Tjorriemorrie
Tjorriemorrie / lru_cache.py
Created October 11, 2016 23:53
LRU cache from hettinger for p27
"""
LRU Cache
Recipe created by Raymond Hettinger
http://code.activestate.com/recipes/578078-py26-and-py30-backport-of-python-33s-lru-cache/
TODO: refactor out xpt.adt.cachedict
"""
from collections import namedtuple
from functools import update_wrapper
from threading import RLock
@Tjorriemorrie
Tjorriemorrie / foo.py
Created September 8, 2016 05:20
python inheritance with args
class A(object):
def __init__(self, astr='noA'):
print 'init A foo? {}'.format(astr)
class B_alpha(A):
def __init__(self, bstr='noB', *args, **kwargs):
print 'init B alpha bar? {}'.format(bstr)
super(B_alpha, self).__init__(*args, **kwargs)
class B_beta(A):
import argparse
import gevent
import gevent.monkey
import gevent.pool
import gevent.queue
import gevent.signal
import random
import signal
import time
import logging
@Tjorriemorrie
Tjorriemorrie / drop_all_mysql_tables.sql
Created February 29, 2016 07:33
drops all mysql tables
SET FOREIGN_KEY_CHECKS = 0;
SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
FROM information_schema.tables
WHERE table_schema = 'pinball';
SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@Tjorriemorrie
Tjorriemorrie / config.py
Last active December 15, 2015 08:21
Python logging config
LOGGING_CONFIG = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(asctime)s - %(levelname)-7s - [%(filename)s:%(funcName)s] %(message)s',
},
},
'filters': {},
'handlers': {