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 contextlib | |
import shutil | |
from pathlib import Path | |
@contextlib.contextmanager | |
def replace_file(orig, replace_with): | |
if not replace_with.is_file(): | |
raise ValueError("{} does not exist.".format(replace_with)) | |
if orig.is_file(): |
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
def is_exe(filepath): | |
return os.path.isfile(filepath) and os.access(filepath, os.X_OK) |
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 os | |
import hashlib | |
def hash_dir(path): | |
""" | |
Create a hash out of a directory that handles renamed files. | |
A hash of the contents of a file will not capture a hash of the filename. | |
This function will add filenames to the hash so that we can know if 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
import fnmatch | |
import os | |
matches = [] | |
for root, dirnames, filenames in os.walk("src"): | |
for filename in fnmatch.filter(filenames, "*.c"): | |
matches.append(os.path.join(root, filename)) |
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 http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery | |
String.prototype.format = String.prototype.f = function() { | |
var s = this, | |
i = arguments.length; | |
while (i--) { | |
s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); | |
} | |
return s; |
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 http://tech.novapost.fr/django-unit-test-your-views-en.html | |
# or https://tech.people-doc.com/django-unit-test-your-views.html | |
# | |
# django test client is an integration test, not a unit test so we | |
# need a way to test views. | |
import unittest | |
from django.test import RequestFactory | |
def setup_view(view, request, *args, **kwargs): |
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 django.forms.models import model_to_dict | |
class ModelDiffMixin(object): | |
""" | |
A model mixin[1] that tracks model fields' values and provide some useful api | |
to know what fields have been changed. | |
[1] http://stackoverflow.com/questions/1355150/django-when-saving-how-can-you-check-if-a-field-has-changed | |
""" |
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
# urls.py | |
def register_routes(router, apps): | |
""" | |
Register DRF router routes from urls.py in the provided apps. | |
>>> router = routers.DefaultRouter() | |
>>> register_routes(router, ['app1', 'app2', 'app3]) | |
""" | |
for app in apps: | |
urls = importlib.import_module(app+".urls") |
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
class DictDiffer(object): | |
""" | |
Calculate the difference between two dictionaries as: | |
(1) items added | |
(2) items removed | |
(3) keys same in both but changed values | |
(4) keys same in both and unchanged values | |
http://stackoverflow.com/questions/1165352/fast-comparison-between-two-python-dictionary/1165552#1165552 | |
""" |
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
def show_urls(urllist: list, indent_depth: int = 0, indent_char: str = " "): | |
""" | |
Prints a list of urls for this project. | |
:param urllist: A list of url patterns. Typically `urlpatterns` imported from `urls.py` | |
:param indent_depth: How far to indent for each sub-level of urls | |
:param indent_char: The charcter to use to indent. | |
""" | |
for entry in urllist: | |
print(indent_char * indent_depth, entry.regex.pattern, end="") |
OlderNewer