Skip to content

Instantly share code, notes, and snippets.

@htv2012
htv2012 / json_duplicate_keys_detection.py
Last active September 3, 2023 19:56
How to detect duplicate keys when decoding JSON string
import json
import collections
json_string = """
{
"a": 1,
"a": 2,
"b": [1,2,3],
"b": "foo",
"c": {"x":1, "y":2, "z":3, "a":4}
@htv2012
htv2012 / create_logger.py
Last active November 5, 2018 04:04
Create a logger that log to screen and optionally to a file.
import logging
INFO = logging.INFO
DEBUG = logging.DEBUG
WARNING = logging.WARNING
def create_logger(logger_name, log_level=logging.WARNING, filename=None):
""" Create a logger that log to the console, if a filename is
supplied, log to that file as well.
@htv2012
htv2012 / class_for_test_data.py
Created October 31, 2014 21:47
Using class for test data in DDT
""" Use of class for generic test data """
import unittest
from ddt import ddt, data
class TestData(object):
def __init__(self, name, **kwargs):
self._name = name
for k, v in kwargs.iteritems():
setattr(self, k, v)
@htv2012
htv2012 / long_message_tests.py
Last active August 29, 2015 14:08
Using unittest.TestCase.longMessage to provide more detail. See the example below and output. Documentation: https://docs.python.org/2/library/unittest.html#unittest.TestCase.longMessage
""" Demo the use of longMessage to give more details """
import unittest
class MyTestCase(unittest.TestCase):
def test_use_long_message(self):
""" Use longMessage for more detailed error message """
self.longMessage = True
actual_values = [0, 1, 2, 3]
@htv2012
htv2012 / list_diff.py
Created November 10, 2014 17:00
Compare Unordered Lists
from collections import Counter
import unittest
def unordered_list_diff(list1, list2):
""" Disregard of items order, compare two lists.
:param list1: List to compare
:param list2: List to compare
:return: None if the lists are identical, or a tuple of 3:
Items only in list 1 or None
@htv2012
htv2012 / html_unescape.py
Created January 8, 2015 14:39
A small utility to take in as input an HTML file and output formatted information. I used this to un-garble test failures messages.
"""
Unescape HTML junk into something readable
Usage: python html_unescape.py < input_file
"""
import fileinput
import HTMLParser
replacements = [
('\\:', ':')
@htv2012
htv2012 / return_mutable_attributes_is_dangerous.py
Created February 20, 2015 15:45
Why return a mutable attribute might be dangerous
#!/usr/bin/env python
""" Why return a mutable attribute might be dangerous """
class Car(object):
def __init__(self, owners):
self._owners = owners
def get_owners(self):
""" Return a list of owners. Getter/setters are evil, but that
@htv2012
htv2012 / closed_object.py
Last active August 29, 2015 14:19
Demo: a object that, after close, is in an invalid state. At this point, any method invocation will result in an exception. Output: http://codepad.org/34k4e7wB
class Foo(object):
def __invalid_action(self, *args, **kwargs):
raise IOError('Sorry, we are closed')
def __init__(self):
print 'create'
def read(self):
print 'read'
@htv2012
htv2012 / skeleton.py
Last active May 9, 2023 16:06
Python Skeleton
#!/usr/bin/env python3
def main():
pass
if __name__ == "__main__":
main()
@htv2012
htv2012 / Anaconda.sublime-settings
Last active August 29, 2015 14:20
Settings for Anaconda plugin
{
"anaconda_linter_underlines": false,
"pep8_max_line_length": 72,
"zzz": 1
}