Skip to content

Commit

Permalink
random_picker.py : Override __deepcopy__ for cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
smallnamespace committed May 6, 2016
1 parent a40c3b7 commit 25768de
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions fireplace/dsl/random_picker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
from copy import copy
from copy import copy, deepcopy
from hearthstone.enums import CardType, Race, Rarity
from .lazynum import LazyValue

Expand All @@ -20,20 +20,30 @@ def __init__(self, **filters):
def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.filters)

def clone(self, memo):
# deepcopy functionality is here because parent __deepcopy__ is
# difficult to call from subclasses
ret = copy(self)
ret.weights = list(self.weights)
ret.filters = deepcopy(self.filters, memo)
ret.weightedfilters = deepcopy(self.weightedfilters, memo)
ret.count = self.count

return ret

def __deepcopy__(self, memo):
return self.clone(memo)

# select number of cards to fetch
def __mul__(self, other):
ret = copy(self)
ret.weight = list(self.weights)
ret.weightedfilters = list(self.weightedfilters)
ret = deepcopy(self)
ret.count = other
return ret

# add a filter set
def copy_with_weighting(self, weight, **filters):
ret = copy(self)
ret.weights = list(self.weights)
ret = deepcopy(self)
ret.weights.append(weight)
ret.weightedfilters = list(self.weightedfilters)
ret.weightedfilters.append(filters)
return ret

Expand Down Expand Up @@ -101,5 +111,10 @@ def __init__(self, *args):
super().__init__()
self._cards = args

def clone(self, memo):
ret = super().clone(memo)
ret._cards = deepcopy(self._cards, memo)
return ret

def evaluate(self, source):
return super().evaluate(source, self._cards)

0 comments on commit 25768de

Please sign in to comment.