Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
@mikeckennedy
mikeckennedy / loops_vs_comps_speed.py
Created February 20, 2022 17:05
Are list comprehensions faster than loops?
import datetime
count = 10_000_000
def with_loop():
lst = []
for n in range(1, count):
if n % 2 == 0:
lst.append(n)
import datetime
def run_with_no_ex(count: int):
idx = 0
while idx < count:
idx += 1
s = "".upper()
def run_with_ex(count: int):
# Requires pip install unsync
import time
from unsync import unsync
import multiprocessing as mp
import sys
#Start and end numbers
start_number = 1
num_processes = mp.cpu_count()
@mikeckennedy
mikeckennedy / watch_this.py
Last active June 30, 2021 06:30
Add C# += / -= event subscriptions to Python using the Events package
from events import Events # Note you must pip install events
class Person:
def __init__(self, name: str, age: int, city: str):
self.__events = Events(('on_name_changed', 'on_age_changed', 'on_location_changed'))
self.on_name_changed = self.__events.on_name_changed
self.on_age_changed = self.__events.on_age_changed
self.on_location_changed = self.__events.on_location_changed
@mikeckennedy
mikeckennedy / speed_of_exceptions.py
Created July 9, 2020 18:02
Simple example to test how much faster or slower it is to simply try and convert then catch an error vs. testing up front.
import datetime
import random
from typing import List
def main():
random.seed(172)
count = 1_000_000
data = build_data(count)
run_with_except(data)
import asyncio
import time
from unsync import unsync
@unsync
async def heartbeat():
while True:
start = time.time()
import asyncio
import time
async def heartbeat():
while True:
start = time.time()
await asyncio.sleep(1)
delay = time.time() - start - 1
print(f'heartbeat delay = {delay:.3f}s')
@mikeckennedy
mikeckennedy / markdown-subtemplate-example.md
Created February 19, 2020 01:35
This is the subtemplate in markdown with imports for https://talkpython.fm/beginners - notice the md imports of other templates.

What's this course about and how is it different?

Most courses teach you the facts of programming and Python. Here is how a loop is constructed. Here is how you test a condition and make your program choose one path or another. Often they assume that you are familiar with programming concepts such as data types, loops, functions, and so on and that you just need to learn the details of how to do this in Python.

This course is not most courses. If you want ground up coverage of

@mikeckennedy
mikeckennedy / speedy.py
Created January 20, 2020 22:46
Compare the speed of a complex multiple if test and set containment replement.
import datetime
def main():
count = 1000
run_sets(count)
run_ifs(count)
def run_sets(times):
@mikeckennedy
mikeckennedy / rss.xml.pt
Created December 18, 2019 20:37
RSS Feed template for Talk Python
<?xml version="1.0" encoding="UTF-8"?>
<rss
xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
version="2.0"
xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Talk Python To Me - Python conversations for passionate developers</title>
<description>Talk Python to Me is a weekly podcast hosted by Michael Kennedy.
The show covers a wide array of Python topics as well as many related