Skip to content

Instantly share code, notes, and snippets.

View jamescalam's full-sized avatar
👻

James Briggs jamescalam

👻
View GitHub Profile
html += f"""
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<div class="col-lg-12 text-left">
<h1 class="mt-5">{fullpath[-1]}</h1>
<p class="lead">
@jamescalam
jamescalam / breadcrumb.py
Last active May 5, 2020 19:36
Dynamic breadcrumb navbar with Python.
# initialise the breadcrumb and add link to top-level readme
html += f"""
<!-- Breadcrumb Navigation -->
<nav aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item active"><a href="../readme.html">Readme</a></li>
"""
# loop through each layer in full path and add each to breadcrumb
for i, layer in enumerate(fullpath):
@jamescalam
jamescalam / functions.py
Created May 5, 2020 18:30
Dynamic creation of function documentation with Python in HTML.
html = """
<h2>Functions</h2>
<br>
<ul>
""" # initialise html object
# iterate through and add function sections
for name in funcs:
html += f"""
<li class="list-group-item" id="func_{name}">
@jamescalam
jamescalam / classes.py
Created May 5, 2020 18:49
Dynamic creation of class buttons in HTML using Python.
# if length of classes is not zero
if len(classes) > 0:
# add class section start
html += """
<h2>Classes</h2>
<div class="row">
<!-- Class Buttons -->
<div class="col-4">
<div class="list-group" id="list-mod" role="tablist">
"""
@jamescalam
jamescalam / end.html
Created May 5, 2020 19:31
Final part of dynamically build docs.
<!-- Bootstrap core JavaScript -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
@jamescalam
jamescalam / notify_message.py
Last active January 31, 2022 06:36
Example of using Python's email module to build an email message object containing a subject, body, attachments, and images.
import os
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
def message(subject="Python Notification", text="", img=None, attachment=None):
# build message contents
msg = MIMEMultipart()
msg['Subject'] = subject # add in the subject
@jamescalam
jamescalam / notify_smtp.py
Last active June 3, 2020 08:15
Function used to send a MIMEMultipart email object (msg) to your own email using Python's smtplib library.
import smtplib
import socket
def send(msg, server='smtp-mail.outlook.com', port='587'):
# contain following in try-except in case of momentary network errors
try:
# initialise connection to email server, the default is Outlook
smtp = smtplib.SMTP(server, port)
# this is the 'Extended Hello' command, essentially greeting our SMTP or ESMTP server
smtp.ehlo()
@jamescalam
jamescalam / synth_format_data.py
Created May 14, 2020 08:06
Formatting image data into a tensorflow dataset object
def format_data(data, buffer, batchsize, colour=False):
# if using grayscale, must make array 3-D with depth/z = 1
if not colour:
data = [np.reshape(x, (x.shape[0], x.shape[1], 1)) for x in data]
data = tf.data.Dataset.from_tensor_slices(data) # convert to TF dataset object
# shuffle the data and place into batches
data = data.shuffle(buffer).batch(batchsize, drop_remainder=True)
@jamescalam
jamescalam / synth_import_image.py
Created May 14, 2020 08:26
Import, colour-conversion, and down-sampling of 360p resolution images.
def downsample_rgb(array, downsample):
# function to downsample RGB array dimensions without affecting colour dims
r = skimage.measure.block_reduce(array[:, :, 0], (downsample, downsample), np.mean)
g = skimage.measure.block_reduce(array[:, :, 1], (downsample, downsample), np.mean)
b = skimage.measure.block_reduce(array[:, :, 2], (downsample, downsample), np.mean)
return np.stack((r, g, b), axis=-1) # return each downsampled array stacked in
def import_image(path, colour, downsample):
# load the image
image = Image.open(path)
@jamescalam
jamescalam / generator_optimiser_loss.py
Last active May 14, 2020 13:05
Methods used in the generator class for a DCGAN.
# this should be placed outside the class, as it will be used by the discriminator too
cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=True)
# the following are contained within the Generator class
def optimiser(self, learning_rate):
self.opt = tf.optimizers.Adam(learning_rate)
def loss(self, fake_preds):
# calculate the loss with binary cross-entropy
fake_loss = cross_entropy(tf.ones_like(fake_preds), fake_preds)