Skip to content

Instantly share code, notes, and snippets.

View danizen's full-sized avatar

Dan Davis danizen

View GitHub Profile
@danizen
danizen / middleware.py
Last active December 11, 2023 07:59
Django middleware to collect slow queries using force_debug_cursor
import logging
import time
from django.conf import settings
from django.db import connection
THRESHOLD = getattr(settings, 'SLOW_REQUEST_THRESHOLD', 1.0)
LOG_SQL = getattr(settings, 'SLOW_REQUEST_LOG_SQL', False)
LOG = logging.getLogger(__name__)
@danizen
danizen / loader-templatetags-loader.py
Created March 9, 2020 20:15
Django app to load from a webapp bundle
import os
import re
import logging
from django import template
from django.conf import settings
from django.utils.safestring import mark_safe
from django.templatetags.static import static
from loader.utils import BundleMap
@danizen
danizen / test_googlesearch.py
Created January 29, 2020 14:24
Examples of Python NLM Selenium Test Framework
from nlm_selenium.seleniumtest import SeleniumTestCase
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
import pytest
from nlm_selenium.configuration import SeleniumSettings
@pytest.fixture(scope='module')
def selenium_settings(pytestconfig):
s = SeleniumSettings(pytestconfig)
@danizen
danizen / tests_lib_filesystem.py
Last active December 31, 2019 22:09
An excerpt that shows how to do multiprocessing
import os
import multiprocessing
import subprocess
import sys
import traceback
def make_unreadable_file(path):
os.chmod(path, 0o000)
if sys.platform == "win32":
# Once we drop PY2 we can use `os.getlogin()` instead.
@danizen
danizen / pytest.ini
Last active December 10, 2019 18:38
How to use page objects with nlm_selenium
[pytest]
baseurl = https://ned.nih.gov
browser = chrome
@danizen
danizen / page_objects.py
Created December 2, 2019 20:54
enhance selenium page objects with wait capability and caching
"""
Adapted from the page-objects Python package addressing two perceived shortfalls:
- Lack of an easy way to get at the locator for a field, for instance for waits.
- Extra calls to driver.find_element() without any caching.
The API of this module is mostly backwards compatible with page-objects, with the following differences:
- The `root_uri` kwarg or PageObject is deprecated in favor of `base_url`, but root_uri is still supported.
- The behavior where setting the attribute sends keys seems to obscure the selenium bindings for selenium,
and are too magical, so they are removed.
- The factory methods at end are removed, because I never used it with factory methods
@danizen
danizen / settings_a1.py
Last active February 15, 2019 19:25
Towards simple settings for logging
## Example settings wodge for standard configuration
LOGGING_CONFIG = 'nlm.occs.logging.configure_logging'
# The above compiles to our current "standard" wodge, except:
# - all "local" apps become installed apps
# - the level for "local" apps becomes automatically either 'DEBUG' if settings.DEBUG else 'INFO'
LOGGING = {
'version': 1,
@danizen
danizen / demo.py
Last active February 14, 2019 20:44
Things declared at the class level are "in-scope" within a Python class
def a_decorator_that_takes_arguments(fubar):
def inner_decorator(func):
nonlocal fubar
func.thing = fubar
return func
return inner_decorator
class ThingUsingDecorator(object):
@danizen
danizen / runshell.py
Created February 14, 2019 03:44
Run a shell to check Django tests
#!/usr/bin/env python
import argparse
import os
import sys
from django.core.management import call_command
from tests.runtests import setup, teardown
def django_testshell(verbosity, test_labels):
@danizen
danizen / checkwinsafe.py
Created February 13, 2019 03:36
A tiny utility to check whether a python file has strings unsafe for output to the Windows command prompt
#!/usr/bin/env python
import argparse
import ast
import glob
import sys
class CheckStrings(ast.NodeVisitor):
filename = 'unknown'