Skip to content

Instantly share code, notes, and snippets.

View dwcoates's full-sized avatar

Dodge Coates dwcoates

  • Chess.com
  • New York, NY
View GitHub Profile

(defun my-kill-thing-at-point (thing) “Kill the `thing-at-point’ for the specified kind of THING.” (let ((bounds (bounds-of-thing-at-point thing))) (if bounds (kill-region (car bounds) (cdr bounds)) (error “No %s at point” thing))))

(defun my-kill-word-at-point () “Kill the word at point.” (interactive)

# rest at www.github.com/dwcoates/yelp-reviews/preprocess.py
import gzip
fp = gzip.open('foo.gz')
contents = fp.read() # contents now has the uncompressed bytes of foo.gz
fp.close()
u_str = contents.decode('utf-8') # u_str is now a unicode string
lines = u_str.split('\n')
for line in lines:
j = json.loads(line)
@dwcoates
dwcoates / parsing_json_2.py
Created February 14, 2017 06:19
Second snippet for you
# option 1
import gzip
fp = gzip.open('foo.gz')
x = json.load(fp.read())
# option 2
gzip.open('file.gz', 'rt', encoding='utf-8')
@dwcoates
dwcoates / read_json.py
Last active March 11, 2017 19:38
for pops
import json
import gzip
import io
def read_json(filename, gz=None):
"""
Read in comma-newline delimited json files encoded in latin-1.
"""
_io = gzip.io if gz else io
attrs = ["other_exceptions",
"deletes",
"encode_exceptions",
"english_count",
"total_fails",
"total_parses",
"file_count"]
history = dict(zip(attrs, [0] * len(attrs)))
@dwcoates
dwcoates / python-send-code-dwim.el
Last active December 15, 2017 01:01
Convenient function for sending python code to shell
(defvar python-shell-send-code-step-on-function nil
"If non-nil, step sending a function to the python interpreter.")
(defun python-shell-step-after-defun ()
(forward-paragraph)
;; when at last paragraph, don't step to beginning of "next" paragraph
(unless (equal (line-number-at-pos (point))
(progn (forward-paragraph) (line-number-at-pos (point))))
(backward-paragraph)
(forward-line 1)))
@dwcoates
dwcoates / repl.py
Created June 7, 2017 18:29
some elisp code for interactive pythoning
(defvar python-shell-send-code-step-on-function nil
"If non-nil, step sending a function to the python interpreter.")
(defun python-shell-step-after-defun ()
(forward-paragraph)
;; when at last paragraph, don't step to beginning of "next" paragraph
(unless (equal (line-number-at-pos (point))
(progn (forward-paragraph) (line-number-at-pos (point))))
(backward-paragraph)
(forward-line 1)))
@dwcoates
dwcoates / repl.el
Last active December 15, 2017 00:53
elisp code for interactive programming in python
(defvar python-shell-send-code-step-on-function nil
"If non-nil, step sending a function to the python interpreter.")
(defun python-shell-step-after-defun ()
(forward-paragraph)
;; when at last paragraph, don't step to beginning of "next" paragraph
(unless (equal (line-number-at-pos (point))
(progn (forward-paragraph) (line-number-at-pos (point))))
(backward-paragraph)
(forward-line 1)))
@dwcoates
dwcoates / sgml-copy-or-kill-element.el
Last active December 1, 2017 19:52
Copy or kill HTML element at point in Emacs
(defun sgml-copy-or-kill-element (arg)
(interactive "P")
(let ((cut-tag (lambda ()
(let ((beg (point))
(end (save-excursion (sgml-skip-tag-forward 1)
(point))))
(kill-ring-save beg end)
(when arg
(delete-region beg end))))))
(save-excursion (if (sgml-beginning-of-tag)
@dwcoates
dwcoates / track-eshell.el
Created December 13, 2017 19:51
Track eshell sessions dwim-style
(defvar eshell-previous-buffer nil)
(defvar eshell-session-alist nil)
(defun dwc-switch-to-terminal (arg)
(interactive "p")
(if (eq arg 4)
(progn
(let ((sesh (alist-get (current-buffer) eshell-session-alist)))
(if sesh
(progn