| 1 |
"""Wizard tests from petrounias.org |
| 2 |
|
| 3 |
http://www.petrounias.org/articles/2014/09/16/pickling-python-collections-with-non-built-in-type-keys-and-cycles/ |
| 4 |
|
| 5 |
Includes functionality to assist with adding compatibility to jsonpickle. |
| 6 |
|
| 7 |
""" |
| 8 |
|
| 9 |
import collections |
| 10 |
|
| 11 |
from jsonpickle import decode, encode |
| 12 |
|
| 13 |
|
| 14 |
class World: |
| 15 |
def __init__(self): |
| 16 |
self.wizards = [] |
| 17 |
|
| 18 |
|
| 19 |
class Wizard: |
| 20 |
def __init__(self, world, name): |
| 21 |
self.name = name |
| 22 |
self.spells = collections.OrderedDict() |
| 23 |
world.wizards.append(self) |
| 24 |
|
| 25 |
def __cmp__(self, other): |
| 26 |
for (ka, va), (kb, vb) in zip(self.spells.items(), other.spells.items()): |
| 27 |
cmp_name = cmp(ka.name, kb.name) # noqa: F821 |
| 28 |
if cmp_name != 0: |
| 29 |
print(f'Wizards cmp: {ka.name} != {kb.name}') |
| 30 |
return cmp_name |
| 31 |
for sa, sb in zip(va, vb): |
| 32 |
cmp_spell = cmp(sa, sb) # noqa: F821 |
| 33 |
if cmp_spell != 0: |
| 34 |
print(f'Spells cmp: {sa.name} != {sb.name}') |
| 35 |
return cmp_spell |
| 36 |
return cmp(self.name, other.name) # noqa: F821 |
| 37 |
|
| 38 |
def __eq__(self, other): |
| 39 |
for (ka, va), (kb, vb) in zip(self.spells.items(), other.spells.items()): |
| 40 |
if ka.name != kb.name: |
| 41 |
print(f'Wizards differ: {ka.name} != {kb.name}') |
| 42 |
return False |
| 43 |
for sa, sb in zip(va, vb): |
| 44 |
if sa != sb: |
| 45 |
print(f'Spells differ: {sa.name} != {sb.name}') |
| 46 |
return False |
| 47 |
return self.name == other.name |
| 48 |
|
| 49 |
def __hash__(self): |
| 50 |
return hash('Wizard %s' % self.name) |
| 51 |
|
| 52 |
|
| 53 |
class Spell: |
| 54 |
def __init__(self, caster, target, name): |
| 55 |
self.caster = caster |
| 56 |
self.target = target |
| 57 |
self.name = name |
| 58 |
try: |
| 59 |
spells = caster.spells[target] |
| 60 |
except KeyError: |
| 61 |
spells = caster.spells[target] = [] |
| 62 |
spells.append(self) |
| 63 |
|
| 64 |
def __cmp__(self, other): |
| 65 |
return ( |
| 66 |
cmp(self.name, other.name) # noqa: F821 |
| 67 |
or cmp(self.caster.name, other.caster.name) # noqa: F821 |
| 68 |
or cmp(self.target.name, other.target.name) # noqa: F821 |
| 69 |
) # noqa: F821 |
| 70 |
|
| 71 |
def __eq__(self, other): |
| 72 |
return ( |
| 73 |
self.name == other.name |
| 74 |
and self.caster.name == other.caster.name |
| 75 |
and self.target.name == other.target.name |
| 76 |
) |
| 77 |
|
| 78 |
def __hash__(self): |
| 79 |
return hash(f'Spell {self.name} by {self.caster.name} on {self.target.name}') |
| 80 |
|
| 81 |
|
| 82 |
def hashsum(items): |
| 83 |
return sum([hash(x) for x in items]) |
| 84 |
|
| 85 |
|
| 86 |
def compare_spells(a, b): |
| 87 |
for (ka, va), (kb, vb) in zip(a.items(), b.items()): |
| 88 |
if ka != kb: |
| 89 |
print(f'Keys differ: {ka} != {kb}') |
| 90 |
return False |
| 91 |
return True |
| 92 |
|
| 93 |
|
| 94 |
def test_without_pickling(): |
| 95 |
world = World() |
| 96 |
wizard_merlin = Wizard(world, 'Merlin') |
| 97 |
wizard_morgana = Wizard(world, 'Morgana') |
| 98 |
spell_a = Spell(wizard_merlin, wizard_morgana, 'magic-missile') |
| 99 |
spell_b = Spell(wizard_merlin, wizard_merlin, 'stone-skin') |
| 100 |
spell_c = Spell(wizard_morgana, wizard_merlin, 'geas') |
| 101 |
assert wizard_merlin.spells[wizard_morgana][0] == spell_a |
| 102 |
assert wizard_merlin.spells[wizard_merlin][0] == spell_b |
| 103 |
assert wizard_morgana.spells[wizard_merlin][0] == spell_c |
| 104 |
# Merlin has cast Magic Missile on Morgana, and Stone Skin on himself |
| 105 |
assert wizard_merlin.spells[wizard_morgana][0].name == 'magic-missile' |
| 106 |
assert wizard_merlin.spells[wizard_merlin][0].name == 'stone-skin' |
| 107 |
# Morgana has cast Geas on Merlin |
| 108 |
assert wizard_morgana.spells[wizard_merlin][0].name == 'geas' |
| 109 |
# Merlin's first target was Morgana |
| 110 |
merlin_spells = wizard_merlin.spells |
| 111 |
merlin_spells_keys = list(merlin_spells.keys()) |
| 112 |
assert merlin_spells_keys[0] in wizard_merlin.spells |
| 113 |
assert merlin_spells_keys[0] == wizard_morgana |
| 114 |
# Merlin's second target was himself |
| 115 |
assert merlin_spells_keys[1] in wizard_merlin.spells |
| 116 |
assert merlin_spells_keys[1] == wizard_merlin |
| 117 |
# Morgana's first target was Merlin |
| 118 |
morgana_spells_keys = list(wizard_morgana.spells.keys()) |
| 119 |
assert morgana_spells_keys[0] in wizard_morgana.spells |
| 120 |
assert morgana_spells_keys[0] == wizard_merlin |
| 121 |
# Merlin's first spell cast with himself as target is in the dictionary, |
| 122 |
# first by looking up directly with Merlin's instance object... |
| 123 |
assert wizard_merlin == wizard_merlin.spells[wizard_merlin][0].target |
| 124 |
# ...and then with the instance object directly from the dictionary keys |
| 125 |
assert wizard_merlin == merlin_spells[merlin_spells_keys[1]][0].target |
| 126 |
# Ensure Merlin's object is unique... |
| 127 |
assert id(wizard_merlin) == id(merlin_spells_keys[1]) |
| 128 |
# ...and consistently hashed |
| 129 |
assert hash(wizard_merlin) == hash(merlin_spells_keys[1]) |
| 130 |
|
| 131 |
|
| 132 |
def test_with_pickling(): |
| 133 |
world = World() |
| 134 |
wizard_merlin = Wizard(world, 'Merlin') |
| 135 |
wizard_morgana = Wizard(world, 'Morgana') |
| 136 |
wizard_morgana_prime = Wizard(world, 'Morgana') |
| 137 |
assert wizard_morgana.__dict__ == wizard_morgana_prime.__dict__ |
| 138 |
|
| 139 |
spell_a = Spell(wizard_merlin, wizard_morgana, 'magic-missile') |
| 140 |
spell_b = Spell(wizard_merlin, wizard_merlin, 'stone-skin') |
| 141 |
spell_c = Spell(wizard_morgana, wizard_merlin, 'geas') |
| 142 |
assert wizard_merlin.spells[wizard_morgana][0] == spell_a |
| 143 |
assert wizard_merlin.spells[wizard_merlin][0] == spell_b |
| 144 |
assert wizard_morgana.spells[wizard_merlin][0] == spell_c |
| 145 |
|
| 146 |
flat_world = encode(world, keys=True) |
| 147 |
u_world = decode(flat_world, keys=True) |
| 148 |
u_wizard_merlin = u_world.wizards[0] |
| 149 |
u_wizard_morgana = u_world.wizards[1] |
| 150 |
morgana_spells_encoded = encode(wizard_morgana.spells, keys=True) |
| 151 |
morgana_spells_decoded = decode(morgana_spells_encoded, keys=True) |
| 152 |
assert wizard_morgana.spells == morgana_spells_decoded |
| 153 |
|
| 154 |
morgana_encoded = encode(wizard_morgana, keys=True) |
| 155 |
morgana_decoded = decode(morgana_encoded, keys=True) |
| 156 |
assert wizard_morgana == morgana_decoded |
| 157 |
assert hash(wizard_morgana) == hash(morgana_decoded) |
| 158 |
assert wizard_morgana.spells == morgana_decoded.spells |
| 159 |
# Merlin has cast Magic Missile on Morgana, and Stone Skin on himself |
| 160 |
merlin_spells = u_wizard_merlin.spells |
| 161 |
assert merlin_spells[u_wizard_morgana][0].name == 'magic-missile' |
| 162 |
assert merlin_spells[u_wizard_merlin][0].name == 'stone-skin' |
| 163 |
# Morgana has cast Geas on Merlin |
| 164 |
assert u_wizard_morgana.spells[u_wizard_merlin][0].name == 'geas' |
| 165 |
# Merlin's first target was Morgana |
| 166 |
merlin_spells_keys = list(u_wizard_merlin.spells.keys()) |
| 167 |
assert merlin_spells_keys[0] in u_wizard_merlin.spells |
| 168 |
assert merlin_spells_keys[0] == u_wizard_morgana |
| 169 |
# Merlin's second target was himself |
| 170 |
assert merlin_spells_keys[1] in u_wizard_merlin.spells |
| 171 |
assert merlin_spells_keys[1] == u_wizard_merlin |
| 172 |
# Morgana's first target was Merlin |
| 173 |
morgana_spells_keys = list(u_wizard_morgana.spells.keys()) |
| 174 |
assert morgana_spells_keys[0] in u_wizard_morgana.spells |
| 175 |
assert morgana_spells_keys[0] == u_wizard_merlin |
| 176 |
# Merlin's first spell cast with himself as target is in the dict. |
| 177 |
# First try the lookup with Merlin's instance object |
| 178 |
assert u_wizard_merlin == merlin_spells[u_wizard_merlin][0].target |
| 179 |
# Next try the lookup with the object from the dictionary keys. |
| 180 |
assert u_wizard_merlin == merlin_spells[merlin_spells_keys[1]][0].target |
| 181 |
# Ensure Merlin's object is unique and consistently hashed. |
| 182 |
assert id(u_wizard_merlin) == id(merlin_spells_keys[1]) |
| 183 |
assert hash(u_wizard_merlin) == hash(merlin_spells_keys[1]) |