In [1]:
Copied!
import time
class FizzBuzz:
def fizz(self, value: int) -> bool:
if value % 3 == 0:
return True
return False
def buzz(self, value: int) -> bool:
if value % 5 == 0:
time.sleep(5) # simulating a slow performance
return True
return False
import time
class FizzBuzz:
def fizz(self, value: int) -> bool:
if value % 3 == 0:
return True
return False
def buzz(self, value: int) -> bool:
if value % 5 == 0:
time.sleep(5) # simulating a slow performance
return True
return False
In the next cell the execution time is slightly above 5 seconds. This happens, due to the slow
performance of .buzz
.
In [2]:
Copied!
fizz_buzz = FizzBuzz()
print(fizz_buzz.fizz(3))
print(fizz_buzz.buzz(5))
fizz_buzz = FizzBuzz()
print(fizz_buzz.fizz(3))
print(fizz_buzz.buzz(5))
True True
A new feature needs to be developed. However, we want the capability to use the old function still. Due to backwards compatibility, if the new feature doesn't work or not everyone should directly use the new feature.
In [3]:
Copied!
import time
from fastfeatureflag.feature_flag import feature_flag
class FizzBuzz:
def fizz(self, value: int) -> bool:
if value % 3 == 0:
return True
return False
def old_buzz(self, value: int) -> bool:
if value % 5 == 0:
time.sleep(5) # simulating a slow performance
return True
return False
@feature_flag().shadow(run=old_buzz) # Adding a shadow
def buzz(self, value: int) -> bool:
if value % 5 == 0:
return True
return False
import time
from fastfeatureflag.feature_flag import feature_flag
class FizzBuzz:
def fizz(self, value: int) -> bool:
if value % 3 == 0:
return True
return False
def old_buzz(self, value: int) -> bool:
if value % 5 == 0:
time.sleep(5) # simulating a slow performance
return True
return False
@feature_flag().shadow(run=old_buzz) # Adding a shadow
def buzz(self, value: int) -> bool:
if value % 5 == 0:
return True
return False
The buzz
method gets a shadow. If the feature is disabled, the shadow function is run. In this case, we call the old_buzz
method. If the feature get's activated, the new function is used.
In [4]:
Copied!
fizz_buzz = FizzBuzz()
print(fizz_buzz.fizz(3))
print(fizz_buzz.buzz(5))
fizz_buzz = FizzBuzz()
print(fizz_buzz.fizz(3))
print(fizz_buzz.buzz(5))
True True
To make your life easier with enabling/disabling features, see: Configuration
Last update:
October 28, 2023
Created: October 28, 2023
Created: October 28, 2023