-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaning Up typing, examples, and imports (#14)
* using `import` instead of `from` imports to cleanup namespace * Cleaning up typing * Added examples on how to use `ModifiableItemsDict` * Cleaning up code and optimizing imports * Updating Version in setup.py * General fixes to the examples * changed `typing.Type` to use `typing.TypeVar` for 3.6 compatibility * Changing doctest import * fixing imports * Renaming `modifiable_items_dict` directory to `modifiable_items_dictionary` * changing version on setup.py * changed coverage directory * removed doctest-modules for pytest * Added back doctest to pytest * Removed redundant import from doctest * changed pytest to only test in tests/ directory
- Loading branch information
Showing
9 changed files
with
400 additions
and
317 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ jobs: | |
python -m pip install -r requirements-test.txt | ||
- name: Test | ||
run: | | ||
pytest --doctest-modules | ||
pytest tests/ |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import ipaddress | ||
import multiprocessing.pool | ||
import string | ||
import time | ||
|
||
import modifiable_items_dictionary | ||
|
||
|
||
def simple_example(): | ||
import modifiable_items_dictionary | ||
|
||
def _add_1(_value): | ||
if isinstance(_value, int): | ||
_value += 1 | ||
return _value | ||
|
||
def _case_fold_string(_value): | ||
if isinstance(_value, str): | ||
_value = _value.casefold() | ||
return _value | ||
|
||
modifiable_items_dictionary.ModifiableItemsDict._key_modifiers = [str.casefold] | ||
modifiable_items_dictionary.ModifiableItemsDict._value_modifiers = (_add_1, _case_fold_string) | ||
# Or | ||
# modifiable_items_dict.ModifiableItemsDict._key_modifiers = staticmethod(_lower) | ||
# modifiable_items_dict.ModifiableItemsDict._value_modifiers = [_lower, _add_1] | ||
|
||
modifiable_items_dict = modifiable_items_dictionary.ModifiableItemsDict({"lower": 1, "UPPER": 2}, CamelCase=3, | ||
snake_case="FoUR") | ||
|
||
print(modifiable_items_dict) # {'lower': 2, 'upper': 3, 'camelcase': 4, 'snake_case': 'four'} | ||
|
||
del modifiable_items_dictionary["LOWER"] | ||
del modifiable_items_dictionary["UPPER"] | ||
modifiable_items_dict.pop("SNAKE_CAse") | ||
|
||
modifiable_items_dict["HeLLO"] = 5 | ||
|
||
print(modifiable_items_dict) # {'camelcase': 4, 'hello': 6} | ||
|
||
|
||
def simple_inheritance_example(): | ||
"""This example shows how to create a Host Dictionary that will casefold keys, strip keys, and convert values to ip addresses | ||
""" | ||
|
||
# Inherit from `ModifiableItemsDict` and set the `_key_modifiers` and `_value_modifiers` class variables. | ||
class HostDict(modifiable_items_dictionary.ModifiableItemsDict): | ||
_key_modifiers = [str.casefold, str.strip] | ||
_value_modifiers = [ipaddress.ip_address] | ||
# Or | ||
# _value_modifiers = @staticmethod(ipaddress.ip_address) | ||
|
||
browsers = HostDict({" GooGle.com ": "142.250.69.206", " duckDUCKGo.cOM ": "52.250.42.157"}) | ||
|
||
print(browsers) # {'google.com': IPv4Address('142.250.69.206'), 'duckduckgo.com': IPv4Address('52.250.42.157')} | ||
|
||
_old_browser = browsers.pop(" gOOgle.com ") | ||
# or | ||
# del host_dict[" GooGle.com "] | ||
|
||
browsers[" BrAvE.com "] = "2600:9000:234c:5a00:6:d0d2:780:93a1" | ||
|
||
print( | ||
browsers) | ||
# {'duckduckgo.com': IPv4Address('52.250.42.157'), 'brave.com': IPv6Address('2600:9000:234c:5a00:6:d0d2:780:93a1')} | ||
|
||
|
||
def threading_example(): | ||
"""This example shows how to use Threading with `ModifiableItemsDict`.""" | ||
|
||
pool = multiprocessing.pool.ThreadPool(10) | ||
|
||
def _slow_function(x): | ||
time.sleep(.05) | ||
return x | ||
|
||
class TimeDictWithThreading(modifiable_items_dictionary.ModifiableItemsDict): | ||
_key_modifiers = (_slow_function,) | ||
_value_modifiers = (_slow_function,) | ||
_map_function = pool.imap_unordered | ||
# or if order matters | ||
# _map_function = pool.imap | ||
|
||
class TimeDict(modifiable_items_dictionary.ModifiableItemsDict): | ||
_key_modifiers = (_slow_function,) | ||
_value_modifiers = (_slow_function,) | ||
|
||
iterable = {_letter: _index for _index, _letter in enumerate(string.ascii_letters)} | ||
|
||
# Without Threading | ||
start = time.perf_counter() | ||
TimeDict(iterable) | ||
end = time.perf_counter() | ||
print(f"{end - start:.2f} seconds") # 5.54 seconds | ||
|
||
# With Threading | ||
start = time.perf_counter() | ||
TimeDictWithThreading(iterable) | ||
end = time.perf_counter() | ||
print(f"{end - start:.2f} seconds") # 0.64 seconds | ||
|
||
|
||
def main(): | ||
simple_example() | ||
simple_inheritance_example() | ||
threading_example() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from modifiable_items_dictionary.modifiable_items_dictionary import ModifiableItemsDict | ||
|
||
__all__ = (ModifiableItemsDict.__name__,) |
Oops, something went wrong.