Skip to content

Instantly share code, notes, and snippets.

View MBlore's full-sized avatar

Martin Blore MBlore

View GitHub Profile
import threading
import time
items = []
lock = threading.Lock() # Shared lock object for our threads.
# Thread work function.
def do_work():
thread_id = threading.get_ident()
@MBlore
MBlore / perftimer.py
Created April 17, 2023 22:20
Performance Timer
import time
# PerfTimer is a class that allows us to use instances in a 'with' block thanks
# to the special '__enter__' and '__exit__' functions.
class PerfTimer:
def __init__(self, name):
self.name = name
def __enter__(self):
self.start_time = time.perf_counter()
@MBlore
MBlore / cube.py
Created March 5, 2023 13:45
3D Cube PyGame
import pygame
import math
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Set the dimensions of the window
size = (800, 600)
@MBlore
MBlore / balls.py
Created January 18, 2023 18:35
Bouncing Balls
import sys, pygame
import random
pygame.display.init()
size = width, height = 1000, 1000
background = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Balls")
@MBlore
MBlore / index.html
Created December 12, 2022 17:33
Text Parsing For Conversion to HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Post Converter</title>
</head>
<body>
<textarea id="src" cols="100" rows="50">[t]
@MBlore
MBlore / client.py
Created December 11, 2022 19:39
HTTP Python Framework Example
from fasty import *
@get("/")
def index():
return "Hello from my very own HTTP framework called Fasty!"
@get("/weather")
def weather():
return "It's raining."
@MBlore
MBlore / TimeDiff.js
Last active September 11, 2022 01:40
JS Time difference between two dates.
/**
* Returns the amount of seconds between two given dates.
* @param {Date} dtStart The start time.
* @param {Date} dtEnd The end time.
* @returns The number of seconds between the two dates.
*/
function secondsBetween(dtStart, dtEnd) {
var diff = (dtStart.getTime() - dtEnd.getTime()) / 1000;
return Math.abs(diff);
}
@MBlore
MBlore / weather.py
Created September 4, 2022 13:17
Loading the weather in Python.
import urllib.request
import json
def loadWeather(city):
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid={your API key goes here}&units=imperial'
with urllib.request.urlopen(url) as response:
data = response.read()
result = json.loads(data)
@MBlore
MBlore / Proxy.html
Created September 3, 2022 00:16
Plain JS showing how the Proxy object can be used to intercept logic.
<!DOCTYPE html>
<html>
<body>
<h1 id="cnt">0</h1>
<script type="text/javascript">
const state = {
count: 0
}
@MBlore
MBlore / Responsive.html
Created August 6, 2022 21:08
Simple Responsive Layout
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Layout Example</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<style>
/* Default styling for mobile phones (mobile first CSS) */
.container {