Skip to content

Instantly share code, notes, and snippets.

View melvinkcx's full-sized avatar

Melvin Koh melvinkcx

View GitHub Profile
@melvinkcx
melvinkcx / mixins.py
Last active June 30, 2019 04:01
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
in mixins.py
"""
class LoggingConfigMixin:
def is_logging_enabled():
pass
def get_logging_level():
pass
@melvinkcx
melvinkcx / abstracts.py
Last active June 30, 2019 04:01
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
in abstracts.py
"""
from abc import ABCMeta
class AbstractBaseConfigHelper:
__metaclass__ = ABCMeta
def get(self, config_name):
pass
"""
Package: utils.config.constants
after removing duplicates
"""
class ConfigHelper:
def get(self, config_name, default=None):
pass
def set(self, config_name, value):
pass
def _get_settings_helper(self):
@melvinkcx
melvinkcx / utils_config_helpers_after.py
Last active June 30, 2019 04:01
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
Package: utils.config.helpers
after restructuring
"""
def get_logging_level():
# This is duplicate, removing this
pass
class ConfigHelper:
def get(self, config_name, default=None):
pass
"""
Package: utils.config.constants
after restructuring
"""
# Inconsistent programming construct
CONFIG_NAME = {
"ENABLE_LOGGING": "enable_logging",
"LOGGING_LEVEL": "logging_level",
}
@melvinkcx
melvinkcx / utils_config_before_comment.py
Last active June 30, 2019 04:01
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
Package: utils.config
before restructuring
"""
# This looks like belongs to utils.config.constants
CONFIG_NAME = {
"ENABLE_LOGGING": "enable_logging",
"LOGGING_LEVEL": "logging_level",
}
@melvinkcx
melvinkcx / tests_backward_compatible.py
Last active June 30, 2019 04:02
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
in tests.py
Simple backward compatibility test case
"""
class ConfigHelperCompatibilityTestCase(unittest.TestCase):
def test_backward_compatibility(self):
try:
from .config import CONFIG_NAME, LOGGING_LEVEL
from .config import get_logging_level
from .config import ConfigHelper
@melvinkcx
melvinkcx / __init__backward_compatible.py
Last active June 30, 2019 04:02
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
in __init__.py
This is where backward compatibility code lives.
This is to ensure the refactored package supports
old way of import.
This is incomplete, we will revisit __init__.py later
"""
CONFIG_NAME = {}
def get_logging_level(*args, **kwargs):
pass
@melvinkcx
melvinkcx / utils_config_before.py
Last active June 30, 2019 04:02
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
Package: utils.config
before restructuring
"""
CONFIG_NAME = {
"ENABLE_LOGGING": "enable_logging",
"LOGGING_LEVEL": "logging_level",
}
def get_logging_level():
pass
@melvinkcx
melvinkcx / utils_tests_faulty.py
Last active June 30, 2019 04:02
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
Package: utils.tests
Faulty Test Cases
"""
import unittest
class UtilsTestCase(unittest.TestCase):
def setUp(self):