Skip to content

Instantly share code, notes, and snippets.

Steven Loria sloria

Block or report user

Report or block sloria

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@sloria
sloria / bobp-python.md
Created Oct 16, 2013
A "Best of the Best Practices" (BOBP) guide to developing in Python.
View bobp-python.md

The Best of the Best Practices (BOBP) Guide for Python

A "Best of the Best Practices" (BOBP) guide to developing in Python.

In General

Values

  • "Build tools for others that you want to be built for you." - Kenneth Reitz
  • "Simplicity is alway better than functionality." - Pieter Hintjens
@sloria
sloria / recorder.py
Last active Dec 6, 2019
WAV recording functionality using pyaudio
View recorder.py
# -*- coding: utf-8 -*-
'''recorder.py
Provides WAV recording functionality via two approaches:
Blocking mode (record for a set duration):
>>> rec = Recorder(channels=2)
>>> with rec.open('blocking.wav', 'wb') as recfile:
... recfile.record(duration=5.0)
Non-blocking mode (start and stop recording):
View tfidf.py
import math
from text.blob import TextBlob as tb
def tf(word, blob):
return blob.words.count(word) / len(blob.words)
def n_containing(word, bloblist):
return sum(1 for blob in bloblist if word in blob)
def idf(word, bloblist):
View Unknown fields.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@sloria
sloria / cloudSettings
Last active Nov 1, 2019
Visual Studio Code Settings Sync Gist
View cloudSettings
{"lastUpload":"2019-11-01T14:05:13.818Z","extensionVersion":"v3.4.3"}
@sloria
sloria / dropping_py2.md
Last active Sep 16, 2019
Checklist for dropping Python 2 in my libraries
View dropping_py2.md
  • Add pyupgrade to .pre-commit-config.yaml.

If supporting py35, use --py3-plus instead of --py36-plus.

- repo: https://github.com/asottile/pyupgrade
  rev: ...latest version...
  hooks:
  - id: pyupgrade
    args: [--py36-plus]
@sloria
sloria / daily-downloads.sql
Last active Sep 3, 2019
BigQuery scheduled query for daily marshmallow downloads
View daily-downloads.sql
WITH
dls AS (
SELECT
DATE_SUB(DATE(@run_time), INTERVAL 1 DAY) AS date,
file.project AS package,
details.installer.name AS installer,
details.python AS python_version,
CAST(SPLIT(details.python, '.')[
OFFSET
(0)] AS string) AS python_major,
View lazy_init.py
# Better
class Person:
def __init__(self, name, occupation):
self.name = name
self.occupation = occupation
self._relatives = None
@property
def relatives(self):
if self._relatives is None:
View env.py
import os
import re
import urlparse
import json as pyjson
NOTSET = object()
def shortcut(cast, **kwargs):
def method(self, var, default=NOTSET):
View gist:5895679
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
@classmethod
def polar(cls, r, theta):
return cls(r * cos(theta),
r * sin(theta))
point = Point.polar(r=13, theta=22.6)
You can’t perform that action at this time.