Skip to content

Instantly share code, notes, and snippets.

View jaycody's full-sized avatar

Jason Stephens jaycody

View GitHub Profile
@jaycody
jaycody / sort_dict_by_k_and_v.py
Last active November 13, 2017 10:35
Basic patterns for sorting dicts by keys or values
"""With dictionary d, of format key: word from text, value: word frequency
Sort and display the key value pairs in the following ways:
1. DISPLAY TOP 20 WORDS & THEIR FREQ - SORTED: MOST FREQUENT FIRST
2. DISPLAY ALL WORDS THAT APPEAR MORE THAN 25 TIMES & THEIR COUNT - SORTED: MOST FREQ FIRST
3. DISPLAY ALL WORDS - SORTED ALPHABETICALLY
"""
d = make_wordcount_dict_from(filename)
top_twenty_count = 0
@jaycody
jaycody / with_open_multi_files.py
Created November 13, 2017 07:16
Open multiple files with multiple comma-separated open() statements in a single with
"""Python allows putting multiple open() statements in a single with.
comma-separate multiple open() statement
thank you steveha!
https://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement
answered Feb 14 '12 at 19:33
"""
def filter(txt, oldfile, newfile):
'''\
@jaycody
jaycody / with_open_file_as_f.py
Created November 13, 2017 07:05
Pythonic file object creation
## The writable
with open('output.txt', 'w') as f:
f.write('Hi there!')
## we sure about that 'r'???
with open('readthis.txt', 'r') as f:
f.read()
@jaycody
jaycody / argv_parser.py
Last active November 13, 2017 06:24
Basic argument-parsing code to digest input from the command line.
# This basic command line argument parsing code is provided and
# calls the handle_this() and review_that() functions
# inspired by google's python class! thanks!
def main():
if len(sys.argv) != 3:
print 'usage: ./myscript.py {--option_1 | --option_2} file_to_get'
sys.exit(1)
option = sys.argv[1]
@jaycody
jaycody / sort_by_lambda_key.py
Created November 13, 2017 01:40
Sort and sorted with custom key via lambda
""" SORT options with lambda function key
Given a list of non-empty tuples, return a list sorted in increasing
order by the last element in each tuple.
e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields
[(2, 2), (1, 3), (3, 4, 5), (1, 7)]
"""
data = [(1, 7), (1, 3), (3, 4, 5), (2, 2)]
sorted_by_last = sorted(data, key=lambda tup: tup[::-1])
@jaycody
jaycody / f_tester_simple.py
Last active November 12, 2017 09:17
Function validator function compares got against expected and reports results.
#! /usr/bin/python -tt
"""simple function tester"""
def do(x):
return x
def test(got, expected):
"""Compare what you got with what you expected.
Report back please
@jaycody
jaycody / function_test_runner.py
Last active November 12, 2017 08:52
Homemade test runner unpacks test cases from a test suite, compares got v expected, reports the results
#! /usr/bin/python -tt
"""test runner
test_suite: list of test cases for each function
test case: 2-value-tuple with 1) string for function name and 2) a list of test_values
test_values: a list of got,expected value pairs saved as tuples
"""
@jaycody
jaycody / format_parameters.py
Created November 12, 2017 06:02
Parameters for spacing the format function results
""" FORMAT THE format() and UNPACK THE *args """
res = ['Homie McHomeboy', 35, 24.798] # print this
args = (10, 5, 2.2) # with these dimensions
print('{0[0]:{1}s} {0[1]:{2}d} {0[2]:{3}f}'.format(res, *args))
## where:
## 's' # format a STRING with n spaces, left justified by defau
## 'd' # format an INT reserving n spaces, right justified by default
## 'f' # format a float, reserving n spaces, and .n spaces after the decimal point, right justfied by default.
## AND UNPACK THE *args
@jaycody
jaycody / func_tester.py
Created November 11, 2017 11:09
Function for testing functions. Compares results with expected. From Google's Python class.
#! /usr/bin/python -tt
"""Function tester adapted from exercies in Google's Python class"""
def donuts(x):
return 'Number of donuts: {}'.format(x)
# Provided simple test() function used in main() to print
# what each function returns vs. what it's supposed to return.
def test(got, expected):
@jaycody
jaycody / generator_w_next.py
Created November 11, 2017 07:17
Generator with next() for running next test. Yield?
## assuming function: test_check_this()
## Generator runs each test
T = [10, -10, 'h', [], {}, '', 1.0, None, check_this('16'), '\n']
G = (test_check_this(test_val) for test_val in T)
for testable_item in T:
# run test on that testable_item
print next(G)