Skip to content

Instantly share code, notes, and snippets.

@RobinRamael
Created July 31, 2019 13:49
Show Gist options
  • Select an option

  • Save RobinRamael/b1f6d05d7c414f548bd6c1107c0f71cc to your computer and use it in GitHub Desktop.

Select an option

Save RobinRamael/b1f6d05d7c414f548bd6c1107c0f71cc to your computer and use it in GitHub Desktop.

Revisions

  1. RobinRamael created this gist Jul 31, 2019.
    37 changes: 37 additions & 0 deletions versioned_attr.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@

    class VersionedAttribute:

    def __init__(self, status_sequence):
    self.value_status_mapping = {state: None for state in status_sequence}

    def __get__(self, instance, owner):
    return self.value_for(instance.status)

    def __set__(self, instance, value):
    self.value_status_mapping[instance.status] = value

    def value_for(self, status):
    return self.value_status_mapping[status]


    PLANNING = 1
    FILLING_IN = 2
    CORRECTING = 3

    class Assignment(object):

    status = PLANNING
    start_time = VersionedAttribute([PLANNING, FILLING_IN, CORRECTING])


    a = Assignment()

    a.start_time = "around nine-ish"
    print(a.start_time)

    a.status = FILLING_IN
    a.start_time = "the fuck he only showed up after lunch and didn't do shit"
    print(a.start_time)

    a.status = PLANNING
    print(a.start_time)