Skip to content

Instantly share code, notes, and snippets.

View bencleary's full-sized avatar
💭
I may be slow to respond.

Ben Cleary bencleary

💭
I may be slow to respond.
View GitHub Profile
@bencleary
bencleary / main.py
Created July 16, 2024 10:29
Used for generating mock CSV's and JSON data for interview questions
import csv
import json
from datetime import datetime, timedelta
# Function to generate sensor data
def generate_sensor_data(num_rows: int):
sensor_data = []
base_time = datetime(2024, 7, 1, 0, 0)
for i in range(num_rows):
# mylibrary/library.py
class Library:
def subscribe(self, topic):
# checks to see if already subscribed
if topic in self.topics:
raise AssertionError("Topic is already subscribed too")
def wrapper(handler):
@bencleary
bencleary / decorators.py
Created May 23, 2020 19:17
A simple example of a decorator to restrict by group in Django
from django.contrib.auth.decorators import user_passes_test
from django.core.exceptions import PermissionDenied
def restrict_access_to_groups(groups: list, raise_exception: bool = False):
"""
Decorator for views that requires the user to be part of a group,
if they are not the user is not allowed into the page.
If the raise_exception parameter is given the PermissionDenied exception
is raised returning a 403 status code
@bencleary
bencleary / decorators.py
Created May 23, 2020 18:56
Login Required middleware as per the upcoming Django implementation
from functools import wraps
def login_not_required(view_func):
def wrapped(*args, **kwargs):
return view_func(*args, **kwargs)
wrapped.login_required = False
return wraps(view_func)(wrapped)
from django.http import HttpResponseRedirect
from django.conf import settings
class LoginRequiredMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
@bencleary
bencleary / angler_fish.py
Created May 20, 2020 18:19
Funny middleware for Django that redirects prowling users to a YouTube video
from django.conf import settings
from django.http import HttpResponseRedirect
class AnglerFish:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
if any(url in request.path for url in settings.ANGLER_LIGHTS):
@bencleary
bencleary / search.js
Created May 15, 2020 19:56
Generates a JSON index file that can be used for searching on your static site.
const toml = require("toml");
const fs = require("fs");
const path = require("path");
const inDirectory = path.join(__dirname, "..", "content", "posts");
const outDirectory = path.join(__dirname, "..", "static", "assets", "js");
class Index {
filePathQueue = [];
indexes = [];
from .utils import CustomerTypes
class CustomerView(DetailView):
...
def get_context_data(self, **kwargs):
...
if self.get_object().type == CustomerTypes.PROSPECT:
context["ad"] = Ad.objects.get(target=CustomerTypes.PROSPECT)
...
from enum import IntEnum
class CustomerTypes(IntEnum):
PROSPECT = 1
LEAD = 2
CUSTOMER = 3
@classmethod
def choices(cls):
return [(key.value, key.name) for key in cls]
from .utils import CustomerTypes
class Customer(models.Model):
...
type = models.IntegerField(choices=CustomerTypes.choices(), default=CustomerTypes.PROSPECT)
...
def get_customer_type_label(self):
return CustomerTypes(self.type).name.title()