Skip to content

Instantly share code, notes, and snippets.

View peterjpxie's full-sized avatar

Peter Jiping Xie peterjpxie

View GitHub Profile
@peterjpxie
peterjpxie / parametrize_setup.py
Created April 10, 2022 08:41
parametrize_setup.py
"""get input test case lists for parametrized tests"""
import os
from os import path
# root_path is parent folder of Scripts folder
root_path = path.dirname(path.dirname(path.realpath(__file__)))
test_case_list = []
input_root = path.join(root_path, "inputs")
for tc in os.listdir(input_root):
if tc.startswith("test_case"):
test_case_list.append(tc)
@peterjpxie
peterjpxie / ini_to_dict.py
Created April 10, 2022 08:39
ini_to_dict.py
def ini_to_dict(input):
"""Covert a ini file to a simple dict
"""
with open(input) as f:
content = f.read()
ret_dict = {}
for line in content.split("\n"):
if " = " in line:
key, value = line.split(" = ", maxsplit=1)
ret_dict[key] = value
@peterjpxie
peterjpxie / parse_test_input.py
Last active April 23, 2022 06:49
parse_test_input.py
def parse_test_input(filename):
import re
with open(filename, "r") as f:
content = f.read()
# 3 parts split by empty line
parts = re.split("\n\n", content)
parts_len = len(parts)
# part 1: Method and url
assert len(parts[0].split()) == 2
@peterjpxie
peterjpxie / dict_to_ini.py
Created April 10, 2022 08:39
dict_to_ini.py
def dict_to_ini(dict_var, file=None):
ini_content_list = []
def iterate_dict(var, prefix=None):
"""iterate dict and convert to a list of 'key1.key2[i] = value' string"""
if isinstance(var, dict):
for k, v in var.items():
if prefix is None:
new_prefix = k # e.g. age
else:
new_prefix = prefix + "." + k # e.g. name.firstname
@peterjpxie
peterjpxie / parse_ignore_file.py
Created April 10, 2022 08:39
parse_ignore_file.py
def parse_ignore_file(file):
"""Parse ignore file and return a list of ignored keys"""
from os import path
ignore_keys = []
if path.isfile(file):
with open(file) as f:
for line in f:
if line.strip != "":
ignore_keys.append(line.strip())
return ignore_keys
@peterjpxie
peterjpxie / _test_by_input_output_text.py
Last active June 20, 2023 04:42
_test_by_input_output_text.py
import pytest
@pytest.mark.parametrize("testcase_folder", test_case_list)
def test_by_input_output_text(testcase_folder):
"""test by input and expected output text files"""
import os
from os import path
import requests
input_root = path.join(root_path, "inputs")
output_root = path.join(root_path, "outputs")
@peterjpxie
peterjpxie / _parametrize_setup.py
Created April 10, 2022 08:39
_parametrize_setup.py
"""get input test case lists for parametrized tests"""
import os
from os import path
# root_path is parent folder of Scripts folder
root_path = path.dirname(path.dirname(path.realpath(__file__)))
test_case_list = []
input_root = path.join(root_path, "inputs")
for tc in os.listdir(input_root):
if tc.startswith("test_case"):
test_case_list.append(tc)
@peterjpxie
peterjpxie / diff_simple_dict.py
Created April 10, 2022 08:39
diff_simple_dict.py
def diff_simple_dict(expected, actual, ignore=[], output_file=None):
"""Compare simple dict generated by ini_to_dict
"""
diff_list = []
for key in expected:
if key not in ignore:
# missing in actual
if key not in actual:
diff_list.append("- %s = %s" % (key, expected[key]))
# diff
@peterjpxie
peterjpxie / _test_by_input_output_text_full_simplified.py
Created April 10, 2022 08:39
_test_by_input_output_text_full_simplified.py
"""get input test case lists for parametrized tests"""
import os
from os import path
# root_path is parent folder of Scripts folder
# root_path = path.dirname(path.dirname(path.realpath(__file__)))
root_path = path.dirname(path.dirname(path.dirname(path.realpath(__file__)))) # if inside code_snippets subfolder
test_case_list = []
input_root = path.join(root_path, "inputs")
for tc in os.listdir(input_root):
if tc.startswith("test_case"):
@peterjpxie
peterjpxie / dotmap_example.py
Created December 27, 2021 06:59
dotmap_example.py
from dotmap import DotMap
person = {
"name": {
"firstname": "Peter",
"secondname": "Xie"
},
"age": 22
};