Skip to content

Instantly share code, notes, and snippets.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@JamesPHoughton
JamesPHoughton / rotate_label.md
Last active December 3, 2023 18:20
How to create rotary labels in networkx circular layout
g = nx.complete_graph([
    "playground equipment", "evanescent champagne", "curved spacetime", 
    "magic flute", "market returns", "spotty memory",
    "languid feeling", "include numpy as np", "acidic chemical", 
    "downton abbey", "tumble weeds", "precede the cause"])
node_locs = nx.circular_layout(g)
theta = {k: np.arctan2(v[1], v[0]) * 180/np.pi for k, v in node_locs.items() }
@JamesPHoughton
JamesPHoughton / Vensim_Bugs.md
Created August 9, 2017 14:46
Known Bugs in Vensim

Known Vensim Bugs

  1. Passing zero delay time into a Delay fails silently
@JamesPHoughton
JamesPHoughton / make_python_identifier.py
Last active February 28, 2024 01:40
Converts an arbitrary string into something that can be used as a python identifier. Checks for namespace conflicts and conflicts with python and specified reserved words.
def make_python_identifier(string, namespace=None, reserved_words=None,
convert='drop', handle='force'):
"""
Takes an arbitrary string and creates a valid Python identifier.
If the python identifier created is already in the namespace,
or if the identifier is a reserved word in the reserved_words
list, or is a python default reserved word,
adds _1, or if _1 is in the namespace, _2, etc.
Parameters
""" Scraping with xpath
Here's a basic demo on how I scraped a page using xpath to get specific pieces of info.
While I've only accessed a few single pieces of info on the page, xpath can also
get you a list of all the elements that meet a specific path, and will return them as
a list or iterable.
A tool you might like for debugging your xpath expressions is: xpath-helper
https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl
"""
class MinModel(object):
########## boilerplate stuff from the existing pysd #########
def __init__(self):
self._stocknames = [name[:-5] for name in dir(self) if name[-5:] == '_init']
self._stocknames.sort() #inplace
self._dfuncs = [getattr(self, 'd%s_dt'%name) for name in self._stocknames]
self.state = dict(zip(self._stocknames, [None]*len(self._stocknames)))
self.reset_state()
self.functions = functions.Functions(self)
@JamesPHoughton
JamesPHoughton / gist:0f4f269e93a2b85958d8
Created September 15, 2014 14:23
Recursively unpack zip files in python
from zipfile import ZipFile
def unpack_zip(zipfile='', path_from_local=''):
filepath = path_from_local+zipfile
extract_path = filepath.strip('.zip')+'/'
parent_archive = ZipFile(filepath)
parent_archive.extractall(extract_path)
namelist = parent_archive.namelist()
parent_archive.close()
for name in namelist: