Skip to content

Instantly share code, notes, and snippets.

View kevinhowbrook's full-sized avatar
☯️
...probably writing a test

Kevin kevinhowbrook

☯️
...probably writing a test
View GitHub Profile
@kevinhowbrook
kevinhowbrook / example1.py
Last active June 2, 2018 14:01
An example of building up RGB values #onmedium
for i,letter in enumerate(letters):
# Only three numbers needed for RGB so use the first three values as a base
# eg: [10, 4, 21]
n = 255.0 / 26.0 # make sure we don't get a value above 255. (z = 26. 25 *10 = 260 but 26 * 9.8 = 248
if i < 3:
# add the first three values to a new list (number)
letterPosition = alphabet.index(letter) * n # *n here to force a good large number base
number.append(letterPosition)
# Use the remaining values to fine tune the first three we have so it will be a better variant
elif i >= 3 and i < 6:
@kevinhowbrook
kevinhowbrook / gridcalc.py
Last active June 2, 2018 14:01
#onmedium
def calculate_m():
# get a list of possible squared numbers, this give us the number of
# rows and columns to use for the canvas, eg 4 = 2 x 2, 6 = 3 x 3...
# Return: [] of squared numbers up to 10000 (hopefully we don't get that high!)
m = []
for i in range(1,10000):
m.append(i*i)
return(m)
def grid(n,m):
j = 0 # vertical counter
k = 0 # horizontal counter
for i,v in enumerate(convert()):
if i % squares_per_row == 0 and i != 0: # if i is a multiple of squares per row
j += 1 # increments after the squares per row is hit like 0,0,0,1,1,1,2,2,2...
if i % squares_per_row == 0 and i != 0:
k = 0 # increments after the squares per row is hit like 0,1,2,0,1,2...
points = ((k*s,j*s), (k*s, j*s+s),(k*s+s,j*s+s),(k*s+s, j*s)) #set points with incrementing values :/
draw.polygon((points[0], points[1], points[2], points[3]), outline='black', fill = (v[0],v[1],v[2])) # outline='red', fill='blue'
#borders so redraw the same plots with white lines
@kevinhowbrook
kevinhowbrook / wagtail-process-tips.md
Last active March 26, 2019 18:44
Wagtail local process tips

pipenv for function/code completion

When using vagrant, vscode won't be able to map to the vm, well not yet anyway. A way around this is to checkout a branch called pipenv, run pipenv --python 3 to create a virtual environment. Then run pipenv install -r requirements.txt to install everything in the project.

After doing this, in vscode ctrl+p then search for >python: select interpreter. Select the virtual env python, it will look something like this: /home/yourname/.local/share/virtualenvs/project-foo-JbRjzqlX/bin/python.

Just don't commit the pipenv stuff.

Flake8

Place the following in .git/hooks/pre-commit

from django.db import models
from wagtail.core.models import Page
from wagtail.admin.edit_handlers import FieldPanel
class HomePage(Page):
publication_date = models.DateTimeField(null=True, blank=True)
updated_date = models.DateTimeField("Modified date", blank=True, null=True)
@kevinhowbrook
kevinhowbrook / pltips.md
Last active September 30, 2019 11:10
Helpful pattern lab tips

Some various tips on writing yaml for template tags.

Looping over a template tag

HTML

{% social_media_links as social_links %}
<ul class="footer__social-links">
    {% for link in social_links %}
@kevinhowbrook
kevinhowbrook / mock.md
Last active March 21, 2020 20:15
Python Django mock request eg

How to test a class method using mock:

You can patch the entire requests module with:

@mock.patch('requests.get')

But it's better to keep it specific as per:

@mock.patch('rca.shortcourses.access_planit.requests.get')
@kevinhowbrook
kevinhowbrook / make_pages.py
Created June 25, 2020 21:35
wagtail management command for pages with images
from django.core.management.base import BaseCommand
from wagtail.core.models import Page
from django.core.files.images import ImageFile
from projects.models import ProjectPage
from home.models import HomePage
from images.models import CustomImage
from io import BytesIO
import requests
from django.core.files.images import ImageFile
@kevinhowbrook
kevinhowbrook / speedy.md
Created July 9, 2020 14:39
Speed up django tests

add the following to test settings

# By default, Django uses a computationally difficult algorithm for passwords hashing.
# We don't need such a strong algorithm in tests, so use MD5
PASSWORD_HASHERS = [
    "django.contrib.auth.hashers.MD5PasswordHasher",
]