Skip to content

Instantly share code, notes, and snippets.

View jamescalam's full-sized avatar
👻

James Briggs jamescalam

👻
View GitHub Profile
@jamescalam
jamescalam / smtp_example.py
Created May 26, 2020 16:53
Example code for sending an email via SMTP with TLS encryption in Python.
import smtplib
# initialize connection to our email server, we will use Outlook here
smtp = smtplib.SMTP('smtp-mail.outlook.com', port='587')
smtp.ehlo() # send the extended hello to our server
smtp.starttls() # tell server we want to communicate with TLS encryption
smtp.login('joe.bloggs@outlook.com', 'Password123') # login to our email server
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jamescalam
jamescalam / text-summarizer.py
Created November 21, 2020 12:22
Text summarization with Google's T5 model.
import torch
from transformers import AutoTokenizer, AutoModelWithLMHead
# initialize out T5 tokenizer and model
tokenizer = AutoTokenizer.from_pretrained('t5-base')
model = AutoModelWithLMHead.from_pretrained('t5-base', return_dict=True)
text = "Large chunk of text to produce summary from..." # text to summarize
# tokenize the text into input IDs
@jamescalam
jamescalam / flask_api.py
Last active August 24, 2023 07:50
A example API using Flask
from flask import Flask
from flask_restful import Resource, Api, reqparse
import pandas as pd
import ast
app = Flask(__name__)
api = Api(app)
class Users(Resource):
def get(self):
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jamescalam
jamescalam / tree.py
Created July 21, 2020 21:08
Tree datatype definition in Python.
class Tree(dict):
def __missing__(self, key):
value = self[key] = type(self)()
return value
import pyodbc
from datetime import datetime
class Sql:
"""Class used for establishing a Python to Microsoft SQL Server connection
and import/export/manipulation of data files inside the server.
"""
def __init__(self, database, server="XXVIR00012,55000"):
"""Here we are initialising our database and server parameters and
our connection to SQL server.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jamescalam
jamescalam / api_endpoints.py
Created August 29, 2020 09:22
Snippet showing multiple API endpoints setup with Flask.
class Users(Resource):
# methods go here
pass
class Locations(Resource):
# methods go here
pass
api.add_resource(Users, '/users') # '/users' is our entry point for Users
api.add_resource(Locations, '/locations') # and '/locations' is our entry point for Locations
# make a request for the trending posts in /r/Python
res = requests.get("https://oauth.reddit.com/r/python/hot",
headers=headers)
df = pd.DataFrame() # initialize dataframe
# loop through each post retrieved from GET request
for post in res.json()['data']['children']:
# append relevant data to dataframe
df = df.append({