Skip to content

Instantly share code, notes, and snippets.

View gerryjenkinslb's full-sized avatar

Gerry Jenkins gerryjenkinslb

  • Retired
  • Long Beach, CA
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@gerryjenkinslb
gerryjenkinslb / elapsed.py
Last active March 6, 2018 22:29
Python3 elapsed time implementations in Class, Generator, and Functions for discussion and education
# elapsed timer: as generator
# Youtube subscribe link: https://goo.gl/ZgZrZ5
# Gerry Jenkins
import time
class ElapsedTime():
def __init__(self):
# store only start time
@gerryjenkinslb
gerryjenkinslb / hexgame.py
Last active March 9, 2018 23:27
A CLI game in python 2.7 or 3+ to learn binary to hexidecimal. Also example of code to run across versions of python.
# hexgame.py: to learn the hex digits for binary patterns
# written to run in python 2.7 and 3+
# Subscribe: https://www.youtube.com/user/gjenkinslbcc?sub_confirmation=1
from __future__ import division, print_function
from random import randrange
from time import time
import sys
hex_digits = '0123456789ABCDEF'
@gerryjenkinslb
gerryjenkinslb / seq_gen.py
Created March 10, 2018 20:18
convert sequences in string simular to "1,4,8-12,14" to generator of sequence 1,4,8,9,10,11,12,14
# gen sequence i.e. "1,3,5-7,9" > [1,3,5,6,7,9]
def seq_gen(s):
for item in s.split(','):
if '-' in item:
a, b = map(int, item.split('-'))
for i in range(a,b+1):
yield i
else:
if len(item):
yield int(item)
''' comparing speed of new dataclass
with standard class attributes and slots,
running in 3.7.0b1'''
# subscribe to me: https://www.youtube.com/user/gjenkinslbcc?sub_confirmation=1
# I recommend reading PEP557: https://www.python.org/dev/peps/pep-0557/
# this code at gist: http://bit.ly/slotsVdatclass
from dataclasses import dataclass
from timeit import timeit
@gerryjenkinslb
gerryjenkinslb / graph_adj_matrix.py
Last active July 30, 2021 18:49
Python Graph implented by Adjacency Matrix
""" One Example of how to implement a Adjacency Matrix implementation of a Graph Data Structure
that matches the Abstract Data Type as defined in the eBook
https://runestone.academy/runestone/static/pythonds/index.html
and goes with the video course at http://youtube.com/gjenkinslbcc channel
"""
class Vertex(object):
"""vertex with key and optional data payload"""
@gerryjenkinslb
gerryjenkinslb / graph_adj_list.py
Last active April 6, 2018 22:37
Python Graph implented by Adjacency List
""" Graph Data Structure: Adjacency List implementation
that matches the Abstract Data Type as defined in the eBook
https://runestone.academy/runestone/static/pythonds/index.html
and goes with the video course at http://youtube.com/gjenkinslbcc channel
This code is attributed to the book
"Problem Solving with Algorithms and Data Structures using Python"
by the Authors Bradley N. Miller and David Ranum
under the Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International License (CC-BY-NC-SA)
License link: https://goo.gl/m2m1ww
@gerryjenkinslb
gerryjenkinslb / graph.py
Created May 15, 2018 19:41
Undirected Graph with edge data (python)
# Undirected Graph ADT with edge data stored as adj_vertices dict for each Vertex
# The key to each edge is a nomalized tuple of the vertex ids ordered from low to high
# edge id (eid) for v1, v2 is (v1,v2) or (v2,v1) such that the first tuple item is less than second
def edge_id(v1,v2): # normalize key v1,v2 to tuple (v_low,v_hi)
return (v1, v2) if v1 <= v2 else (v2, v1)
class Vertex(object):
from bs4 import BeautifulSoup
import re
import requests
import csv
import time
import json
# version 1.1 added handling of youtube.com/channel/
# to already handling of youtube.com/user based channels
@gerryjenkinslb
gerryjenkinslb / event_gen.py
Last active June 27, 2018 22:53
Python code to create random events where the events have different probabilites specified by a frequence table.
import random
""" Create random events where event probabilities are
set by a frequency list
This shows two functional approaches, the second being more terse
and then a object oriented approach"""
# functional approach using a closure
# setup: x = event_gen(freq_table)
# get sample: x() -> sample index
def event_gen(freq_table):