Skip to content

Instantly share code, notes, and snippets.

View melvinkcx's full-sized avatar

Melvin Koh melvinkcx

View GitHub Profile
@melvinkcx
melvinkcx / solution_deferred_update.py
Created June 30, 2019 06:37
Filtering Dictionary in Python
"""
Solution #3: Deferred Update
Risk: What if an Exception is raised half way?
"""
coffees = get_menu()
keys_to_be_removed = []
for code, details in coffees.items():
if details.get("sugar_free") is True:
keys_to_be_removed.append(code)
@melvinkcx
melvinkcx / solution_copy_of_dict_keys.py
Created June 30, 2019 06:33
Filtering Dictionary In Python 3
"""
Solution #2: Create a copy of keys
"""
coffees = get_menu()
key_copy = tuple(coffees.keys()) # Feel free to use any iterable collection
for k in key_copy:
if coffees[k].get("sugar_free") is True:
del coffees[k]
@melvinkcx
melvinkcx / solution_shallow_copy_of_dict.py
Created June 30, 2019 06:27
Filtering Dictionary in Python
"""
Solution #1: Create a shallow copy of dict
"""
coffees = get_menu() # I use this function to generate my dict object
coffee_copy = {**coffees} # Create a shallow copy
for code, details in coffee_copy.items():
if details.get("sugar_free") is True:
del coffees[code]
@melvinkcx
melvinkcx / erronous_example.py
Created June 30, 2019 06:22
Filtering Dictionary in Python 3
from typing import Dict
def get_menu() -> Dict[str, dict]:
return {
"TIMMY_BLACK": {
"item": "Timmy's Coffee Barista's Black",
"sugar_free": True,
"with_milk": False,
},
"TIMMY_LATTE": {
@melvinkcx
melvinkcx / growing_dict.py
Created June 30, 2019 06:15
Filtering Dictionary in Python 3.7
import random
data = {
1: 1,
2: 2,
3: 3
}
# This will raise an Error
for k in data:
@melvinkcx
melvinkcx / __init__decorated.py
Last active June 30, 2019 03:59
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.
"""
from .constants import CONFIG_NAME, LOGGING_LEVEL
from .helpers import ConfigHelper as _ConfigHelper
@refactored('get_logging_level() is refactored and deprecated.')
@melvinkcx
melvinkcx / decorators.py
Last active June 30, 2019 04:00
Snippet for "Why Refactoring? How to Refactor/Restructure Python Package?" https://hackernoon.com/why-refactoring-how-to-restructure-python-package-51b89aa91987
"""
decorators.py
"""
def refactored_class(message):
def cls_wrapper(cls):
class Wrapped(cls, object):
def __init__(self, *args, **kwargs):
warnings.warn(message, FutureWarning)
super(Wrapped, self).__init__(*args, **kwargs)
return Wrapped
@melvinkcx
melvinkcx / __init__half_baked_2.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 __init__.py
This is where backward compatibility code lives.
This is to ensure the refactored package supports
old way of import.
"""
from .constants import CONFIG_NAME, LOGGING_LEVEL
from .helpers import ConfigHelper as _ConfigHelper
@melvinkcx
melvinkcx / __init__half_baked.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 __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 / helpers.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 helpers.py
"""
class ConfigHelper(
AbstractBaseConfigHelper,
LoggingConfigMixin
):
pass