Skip to content

Instantly share code, notes, and snippets.

@aorfanos
Last active January 19, 2022 00:46
Show Gist options
  • Save aorfanos/e632419778775dedbb148803a37a46d7 to your computer and use it in GitHub Desktop.
Save aorfanos/e632419778775dedbb148803a37a46d7 to your computer and use it in GitHub Desktop.
Prometheus alert unit testing expanding notation calculator
'''
Provide a Prometheus series value expanding notation expression such as:
- 10+0x10
- 1+10x5
- 100-10*10
- 5+5x5 40+10x5
And get a reply:
- 10 10 10 10 10 10 10 10 10 10 10
- 1 11 21 31 41 51
- 100 90 80 70 60 50 40 30 20 10 0
- 5 10 15 20 25 30 40 50 60 70 80 90
'''
import re
expanded = []
expanded_str = ""
print('Enter expanding notation formula:')
exp_not = input()
exp_not_splitted = exp_not.split(' ')
for formula in range(len(exp_not_splitted)):
match = re.search("(?P<start_int>.*)(?P<operator>\+|-)(?P<step>.*)x(?P<times>.*)", exp_not_splitted[formula])
start_int = int(match.group("start_int"))
step = int(match.group("step"))
times = int(match.group("times"))
operator = match.group("operator")
if operator == "+":
expanded.append(f"{str(start_int)} ")
for i in range(times):
start_int = start_int + step
expanded.append(f"{str(start_int)} ")
if operator == "-":
expanded.append(f"{str(start_int)} ")
for i in range(times):
start_int = start_int - step
expanded.append(f"{str(start_int)} ")
print(f"{expanded_str.join(expanded)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment