Skip to content

Instantly share code, notes, and snippets.

import gevent
import gevent.pool
import gevent.event
import gevent.socket
import gevent.monkey
gevent.monkey.patch_all()
class UnhappyEyeballs(Exception):
pass
@ssanderson
ssanderson / make_addone.py
Created March 3, 2018 23:05
Manually assembling a code object
"""
$ python example.py
1 + 1 is 2
"""
from types import FunctionType
from codetransformer import Code
import codetransformer.instructions as instrs
@ssanderson
ssanderson / loop.py
Last active January 17, 2021 06:35
A Class is a Poor Man's Loop
"""
$ python loop.py
Body: 0
Body: 1
Body: 2
Body: 3
Body: 4
Skipping 5
Body: 6
Body: 7
@ssanderson
ssanderson / example.ipy
Created December 14, 2017 20:44
Copying Behavior in DataFrame.rename
In [44]: df
Out[44]:
a b
0 1 2
1 2 3
2 3 4
In [45]: renamed = df.rename(columns={'b': 'c'})
In [46]: renamed.iloc[0, 0] = 1000
@ssanderson
ssanderson / wrapping_context.py
Last active October 12, 2017 19:36
Context manager that wraps another context manager
from __future__ import print_function
from contextlib import contextmanager
@contextmanager
def some_context_manager(x):
print("Entering", x)
yield
print("Exiting", x)
@ssanderson
ssanderson / switch_demo.py
Created September 14, 2017 13:25
"Functional" Switchcase Example
from codetransformer.transformers.switchcase import switch
def foo(x):
print("Start: x={}".format(x))
with switch(x) as case:
@case(1)
def _1():
# Read and write x.
@ssanderson
ssanderson / avocadownload.py
Last active May 16, 2017 21:51
Avocado Scraper
from __future__ import print_function
import requests
import pandas as pd
from six.moves.urllib_parse import urlencode
def download_avocado_data(dest, start_date, end_date):
base_url = 'https://www.marketnews.usda.gov/mnp/fv-report-retail'
@ssanderson
ssanderson / machete_mode_redux.py
Last active September 21, 2016 04:01
More Machete Mode Debugging
import os
from unittest import TestCase
import psutil
import humanize
pid = os.getpid()
proc = psutil.Process(pid)
real_doCleanups = TestCase.doCleanups
OUTPUT_FILE = open('/home/ssanderson/pandas16_memory_usage.txt', 'w')
# td is a TemporaryDirectory that's being deleted earlier than
# expected somewhere in my test suite.
import shutil
old_rmtree = shutil.rmtree
def rmtree(path, *args, **kwargs):
if path == td.name:
import nose.tools; nose.tools.set_trace()
return old_rmtree(path, *args, **kwargs)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.