Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions patterns/structural/flyweight_with_metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ def __new__(mcs, name, parents, dct):
def _serialize_params(cls, *args, **kwargs):
"""
Serialize input parameters to a key.
Simple implementation is just to serialize it as a string
Simple implementation is just to serialize it as a string. ``repr`` keeps
``("1", "0")``, ``("10",)`` and ``(1,)`` apart, and sorting the keyword
arguments makes the key independent of their order.
"""
args_list = list(map(str, args))
args_list.extend([str(kwargs), cls.__name__])
key = "".join(args_list)
return key
return repr((cls.__name__, args, sorted(kwargs.items())))

def __call__(cls, *args, **kwargs):
key = FlyweightMeta._serialize_params(cls, *args, **kwargs)
Expand Down
26 changes: 26 additions & 0 deletions tests/structural/test_flyweight_with_metaclass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from patterns.structural.flyweight_with_metaclass import Card2


def test_same_arguments_share_an_instance():
Card2.pool.clear()
assert Card2("10", "h", a=1) is Card2("10", "h", a=1)


def test_different_arguments_do_not_share_an_instance():
Card2.pool.clear()
assert Card2("10", "h", a=1) is not Card2("10", "h", a=2)


def test_argument_boundaries_are_part_of_the_key():
Card2.pool.clear()
assert Card2("1", "0") is not Card2("10")


def test_argument_types_are_part_of_the_key():
Card2.pool.clear()
assert Card2(1) is not Card2("1")


def test_keyword_argument_order_does_not_matter():
Card2.pool.clear()
assert Card2(a=1, b=2) is Card2(b=2, a=1)