Skip to content

Instantly share code, notes, and snippets.

@beaugunderson
Created April 1, 2020 18:00
Show Gist options
  • Save beaugunderson/41f74aec356f662aedab8585d862a0e9 to your computer and use it in GitHub Desktop.
Save beaugunderson/41f74aec356f662aedab8585d862a0e9 to your computer and use it in GitHub Desktop.
my_thing = SomeThingModel()
# type 1
class ModifyThing:
@classmethod
def format_date(cls, thing):
return thing.created.format('MMMM ddd ...')
@classmethod
def format_thing(cls, thing):
return f'Thing {thing.id}, created {cls.format_date(thing)}'
@classmethod
def modify_thing(cls, thing):
print('Modifying', cls.format_thing(thing))
thing.field = 'new-value'
thing.save()
@classmethod
def process(cls, thing):
cls.modify_thing(thing)
ModifyThing.process(my_thing)
# type 2
class ModifyThing:
def __init__(self, thing):
self.thing = thing
def format_date(self):
return self.thing.created.format('MMMM ddd ...')
def format_thing(self):
return f'Thing {self.thing.id}, created {self.format_date()}'
def modify_thing(self):
print('Modifying', self.format_thing())
self.thing.field = 'new-value'
self.thing.save()
def process(self):
self.modify_thing()
ModifyThing(my_thing).process()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment