This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
data = { | |
1: 1, | |
2: 2, | |
3: 3 | |
} | |
# This will raise an Error | |
for k in data: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
in helpers.py | |
""" | |
class ConfigHelper( | |
AbstractBaseConfigHelper, | |
LoggingConfigMixin | |
): | |
pass |