Skip to content

Instantly share code, notes, and snippets.

View jonathaneunice's full-sized avatar

Jonathan Eunice jonathaneunice

View GitHub Profile
@jonathaneunice
jonathaneunice / misc_encoding.py
Created August 1, 2019 20:05
Unknown Encoding Reader
def read_file_with_BOM_misc_encoding(filepath, encoding='utf_32_be utf_32_le utf-16le utf-16be utf-8'):
"""
Attempt to guess the proper encoding by trying them in a sequence.
Wider encodings should be tried before narrower.
"""
for enc in encoding.split():
# try each encoding in turn, take results of first to not blow up
try:
with open(filepath, mode='r', encoding=enc) as f:
text = f.read()
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@jonathaneunice
jonathaneunice / typing_example.py
Created December 28, 2017 21:16
Typing Example
from typing import Dict, List, Union
ValueType = Union[str, int, List[str]]
order_data: Dict[str, ValueType] = {
'one': 1,
'two': 'two',
'many': ['this', 'is', 'many', 'strings'],
'keywords': ["test_1", "test_2"]
}
@jonathaneunice
jonathaneunice / mentor-promo.md
Created August 13, 2017 15:48
Mentoring Promo

And now some obligatory self-promotion:

I've completed over 350 successful Codementor sessions, with [top-notch reviews][1].

I've contributed to the core Python project (CPython), have many published Python modules, and have completed many Python challenge problems at sites like HackerRank:

@jonathaneunice
jonathaneunice / headstart.md
Last active June 21, 2017 01:02
Head Start

Accelerating Problem Resolution

To speed things along, drop me

  1. an example of the data or code you're having trouble with,
  2. a short explanation of what's not working the way you want.

You can get files to me via:

/**
* CSS imperative declarations.
* E.g. cssi('div#one.two.three')
* yields { element: 'div',
* id: 'one',
* class: 'two three',
* }
* Classes are not split out into an array
from itertools import *
def groups_of_n(iterable, n):
"""
Collect data into fixed-length chunks or blocks. Generates (yields)
a sequence of iterators. Useful for segmenting very large sequences
into more manageable chunks. If the final chunk is of less than size n,
no worries. It will return what there is. NB This is *much* more

Dirty Data Still Dirty
Sad Panda Still Sad

The New York Stock Exchange (NYSE) historical records contain multiple, absurdly novice errors. It's dirty data, referring to days that don't exist, and giving multiple inconsistent answers for over a month of trading days. And this is the data set of record, from the world's leading financial

from random import choice, randint
import string
import time
import sys
_PY3 = sys.version_info[0] == 3
@jonathaneunice
jonathaneunice / gist:0b2a5892ce2eaa834a91
Created October 21, 2014 17:46
Capture stdout and stderr conveniently from within Python programs
import contextlib
import sys
from StringIO import StringIO
# Output capturing helpers thanks to
# test.test_support and http://stackoverflow.com/a/18844643/240490
# extended by Jonathan Eunice to add easy saving to a file (including
# auto-save if a filepath is provided in the constructor)
# This is not quite polished enough to make it into a full PyPI