Skip to content

Instantly share code, notes, and snippets.

View MattWoodhead's full-sized avatar

MattWoodhead

  • UK
View GitHub Profile
@MattWoodhead
MattWoodhead / setup.py
Created June 10, 2017 09:30
A cx_freeze setup script that works for executables for python 3.5+ based apps that use the tkinter graphics framework.
"""
A cx_freeze setup script that works for executables for python 3.5+ based
apps that use the tkinter graphics framework.
i.e. fixes the "KeyError: 'TCL_Library'" error
"""
import os
import sys
from cx_Freeze import setup, Executable
# set location tkinter path variables using os
@MattWoodhead
MattWoodhead / tkinter_progress.py
Created June 11, 2017 17:33
tkinter progress bar example with threading
"""
An examnple of the use of threading to allow simultaneous operations in a
tkinter gui (which is locked to a single thread)
"""
import threading
import tkinter as tk
from tkinter import ttk
@MattWoodhead
MattWoodhead / RoundingDiv.py
Last active June 24, 2017 11:05
A comparison of methods for rounding up a dvision to the nearest whole number
"""
A comparison of methods for rounding upwards on dvision operations. Suprisingly, the fastest method was multiplying
the numerator in the division operation by negative one, using floor division, and then multiplying again by
negative one to arrive at the rounded up answer
Further testing is required on larger numbers (and maybe complex numbers?) to see if the results scale well.
"""
import math
import numpy
@MattWoodhead
MattWoodhead / TkinterDynamicButtons.py
Created June 24, 2017 11:09
Dynamiclaly create buttons from a dictionary
"""
dynamically create buttons with commands in a tkinter gui using a dictionary
"""
import os
import tkinter as tk
# Dictionary of button names and filepaths
PATHS = {"A path": r"C:\Users\Matt\Google Drive\Python\Test\file_1.txt",
"B path": r"C:\Users\Matt\Google Drive\Python\Test\file_2.txt",
@MattWoodhead
MattWoodhead / ItertoolsCycleExample
Created June 24, 2017 11:42
Itertools cylcle example. All hail Raymond H!
import itertools
a = itertools.cycle(range(3))
letters = "ABCDEFG"
for num, alpha in zip(a, letters):
print(f"{num} - {alpha}") # f-strings are awesome
@MattWoodhead
MattWoodhead / ImprovedCheckbar.py
Last active June 25, 2017 09:42
An improvement on a common tkinter 'Checkbar', allowing the setting of values after instancing.
"""
An improvement on the tkinter Checkbar example,
available from http://www.python-course.eu/tkinter_checkboxes.php
Author: Matt Woodhead
"""
import tkinter as tk
from tkinter import ttk
@MattWoodhead
MattWoodhead / ExplorerOnItem.py
Created June 25, 2017 14:21
Open windows explorer with a specific folder or file selected
import subprocess
def explorer_on_file(url):
""" opens a windows explorer window """
subprocess.Popen(f'explorer /select,"{url}"')
explorer_on_file(r"C:\Python\python.exe")
@MattWoodhead
MattWoodhead / local_ip.py
Created July 9, 2017 10:40
OS agnostic method to find local IP address. Requires internet connection.
import socket
def ip_check():
""" uses the cockets module to find the local IP address """
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80)) # Ping Google DNS server
ip_address = s.getsockname()[0]
s.close()
return ip_address
@MattWoodhead
MattWoodhead / caseless_dict_search.py
Last active July 24, 2017 20:46
Simple case insensitive key matching for dictionaries
def caseless_dict_search(dictionary, search_string):
""" performs a case insensitive key search on a standard dictionary """
# A dict matching lower case and actual case representations of the Keys
keys_dict = {k.casefold(): k for k in dictionary.keys()}
# Using the get() function to search dict with builtin error handling
return dictionary.get(keys_dict.get(search_string.lower(), None), None)
test_dict = {"A": 0, "B": 1, "c": 2, "d":3}
INPUT = "D" # Note case does not match any of the keys in the above dict
result = caseless_dict_search(test_dict, INPUT)
@MattWoodhead
MattWoodhead / python_dict_forgiveness.py
Created July 24, 2017 20:39
Demonstration of Python's built in get() method for dictionaries
""" Easier to ask forgiveness than permission """
my_dict = {"key_0": 0,
"key_1": 10,
"key_2": 20,
"key_3": 30,
"key_4": 40,
}