Skip to content

Instantly share code, notes, and snippets.

View GadgetSteve's full-sized avatar

Steve (Gadget) Barnes GadgetSteve

  • Bridgend, UK
View GitHub Profile
@GadgetSteve
GadgetSteve / where_am_i.py
Last active June 27, 2022 06:42
Importable execution position in python
"""
Importable get called from file & location.
Based on a tweet by Ned Batchelder @nedbat
"""
import inspect
def where_am_i():
""" Return the current filename and line number. """
bf = inspect.currentframe().f_back
return (bf.f_globals.get("__file__", "interactive"), bf.f_lineno)
@GadgetSteve
GadgetSteve / lineno_demo.py
Last active June 26, 2022 08:28
How to get the line number in python
# coding: utf-8
# Originally from a tweet by Ned Batchelder @nedbat
import inspect
def __line__():
""" Return the current line number. """
return inspect.currentframe().f_back.f_lineno
if __name__ == "__main__":
print(f"At {__line__()} of {__file__}")
@GadgetSteve
GadgetSteve / cgi-post.py
Created October 30, 2021 09:42 — forked from SteveBarnes-BH/cgi-post.py
CGI Debugging - sometimes you have a web page that uses CGI and you need to see what data is being POSTed.
#!/usr/bin/env python
"""
This script is based on https://cgi.tutorial.codepoint.net/file-upload but with changes as needed.
To use:
- Download the web page to be debugged.
- Edit it to replace `action="/cgi-bin/cgi-post"` with `action="/cgi-bin/cgi-post.py"` (on Windows on Linux you can simply rename this file to not have .py at the end.
- place in a cgi-bin subdirectory of the one where you saved the web page
- start a simple web server with ` python -m http.server 8001 --bind localhost --cgi`
@GadgetSteve
GadgetSteve / list_licenses.py
Last active January 27, 2021 16:27 — forked from WLPhoenix/list_licenses.py
Python license lister
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import pkg_resources
def get_pkg_license(pkgname):
"""
Given a package reference (as from requirements.txt),
@GadgetSteve
GadgetSteve / py_preview.ps1
Created November 7, 2020 11:57
Tell explorer to preview python files as text
Set-ItemProperty Registry::HKEY_CLASSES_ROOT\.py -Name PerceivedType -Value text
Set-ItemProperty Registry::HKEY_CLASSES_ROOT\.pyw -Name PerceivedType -Value text
@GadgetSteve
GadgetSteve / demo.svg
Created March 1, 2020 15:29
Create image like Picture from: E. Lucon Oscillateurs coupl ́es, d ́esordre et
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@GadgetSteve
GadgetSteve / RIPy2prompt.py
Created December 29, 2018 08:44
Python 2 countdown prompt
#!/usr/bin/env python
# encoding utf-8
"""
If you set your PYTHONSTARTUP environment variable to point to this file
with an absolute path then it will change your python 2 prompt to countdown
to the End Of Life of Python 2.
Copyright Steve Barnes <GadgetSteve@hotmail.com> 2017
License: Creative Commons
"""
@GadgetSteve
GadgetSteve / VerCheck.py
Created July 26, 2017 20:38
Reminder of LooseVersion
from distutils.version import LooseVersion
if LooseVersion(requests.__version__) <= LooseVersion('2.2.0'):
raise SystemExit("This program requires Python requests 2.2.0 or later.")
@GadgetSteve
GadgetSteve / timed_sshot.py
Created July 13, 2017 09:17
Demo of timed screenshots
import datetime
import date
import pyscreenshot
INTERVAL = 30 # Time in seconds between screenshots
while True:
now = datetime.datetime.now() # Get timestamp
filename = now.strftime('screenshot_%Y_%m_%d_%H_%M_%S.png') # Create a filename
pyscreenshot.grab_to_file(filename) # take the screenshot
time.sleep(INTERVAL)
@GadgetSteve
GadgetSteve / dump_licences.py
Created October 23, 2016 09:28
Dump out a grouped list of the licences of the installed packages
"""
This gist should allow you to print a dictionary of licence types that you have installed
with a list the packages using each licence.
"""
from __future__ import print_function
import pip
import collections
info = [i for i in pip.commands.show.search_packages_info([ # takes a list of the names as a parameter
d.project_name for d in pip.utils.get_installed_distributions() # This should give us a list of the installed package names