Skip to content

Instantly share code, notes, and snippets.

@yothinix
Created January 23, 2017 03:07
Show Gist options
  • Save yothinix/92cc418d2a952a7c624e8c58bc3f8e7a to your computer and use it in GitHub Desktop.
Save yothinix/92cc418d2a952a7c624e8c58bc3f8e7a to your computer and use it in GitHub Desktop.
python refactoring blog
class Animal:
def __init__(self, *, has_scales=False, lays_eggs=False, drinks_milk=False):
self.has_scales = has_scales
self.lays_eggs = lays_eggs
self.drinks_milk = drinks_milk
class Animal:
def __init__(self, age, *, has_scales=False, lays_eggs=False, drinks_milk=False):
self.age = age
self.has_scales = has_scales
self.lays_eggs = lays_eggs
self.drinks_milk = drinks_milk
class Pet:
def __init__(self, name, animal):
self.name = name
self.animal = animal
self.treats_eaten = 0
def give_treats(self, count):
self.treats_eaten += count
@property
def needs_heat_lamp(self):
return (
self.animal.has_scales and
self.animal.lays_eggs and
not self.animal.drinks_milk)
@property
def age(self):
raise AttributeError('Must use animal.age')
@age.setter
def age(self, new_age):
raise AttributeError('Must assign animal.age')
@property
def age(self):
raise AttributeError('Must use animal.age')
class OystersGood:
def __init__(self, month):
lowered = month.lower()
self.r = lowered.endswith('r')
self.ary = lowered.endswith('ary')
self._result = self.r or self.ary
def __bool__(self): # aka __nonzero__
return self._result
...
time_for_oysters = OystersGood(month)
if time_for_oysters: # Calls __bool__
print('%s: oysters' % month)
# test
test = OystersGood('November')
assert test
assert test.r
assert not test.ary
def oysters_good(month):
lowered = month.lower()
return (
lowered.endswith('r') or
lowered.endswith('ary'))
...
# extract boolean expression as function
if oysters_good(month):
print('%s: oysters' % month)
# extract boolean expression as variable from function
time_for_oysters = oysters_good(month)
if time_for_oysters:
print('%s: oysters' % month)
lowered = month.lower()
ends_in_r = lowered.endswith('r')
ends_in_ary = lowered.endswith('ary')
if ends_in_r or ends_in_ary:
print('%s: oysters' % month)
class Pet:
def __init__(self, name, age_or_animal, maybe_animal=None):
self.name = name
if maybe_animal is not None:
warnings.warn('Should specify "age" for Animal')
self.animal = maybe_animal
self.animal.age = age_or_animal
else:
self.animal = age_or_animal
if (month.lower().endswith('r') or
month.lower().endswith('ary')):
print('%s: oysters' % month)
class Pet:
@property
def has_scales(self):
warnings.warn('Use animal.has_scales instead')
return self.animal.has_scales
class Pet:
def give_treats(self, count):
self.treats_eaten += count
class Pet:
def __init__(self, name, age, *,
has_scales=False,
lays_eggs=False,
drinks_milk=False):
self.name = name
self.age = age
self.treats_eaten = 0
self.has_scales = has_scales
self.lays_eggs = lays_eggs
self.drinks_milk = drinks_milk
@property
def needs_heat_lamp(self):
return (
self.has_scales and
self.lays_eggs and
not self.drinks_milk)
class Pet:
def __init__(self, name, age, animal=None, **kwargs):
self.name = name
self.age = age
self.treats_eaten = 0
if kwargs and animal is not None:
raise TypeError('Must supply either an Animal instance or keyword arguments')
if animal is None:
warnings.warn('Must directly pass an Animal instance')
animal = Animal(**kwargs)
@age.setter
def age(self, new_age):
warnings.warn('Should assign animal.age')
self.animal.age = new_age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment