Skip to content

Commit

Permalink
new unit and random functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed May 9, 2024
1 parent fa446a3 commit f6f510b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
### Upcoming

#### Enhancements
- Chart legend's will support more than 8 Pioreactors.
- UI performance improvements.
- Upgraded to React 18
Expand All @@ -8,6 +10,13 @@
- Upgrade paho-mqtt to 2.0
- faster `pio kill`
- faster job start from UI
- reduce the number of pop-ups in the UI, so they'll be less distractions.
- more humane error messages.
- Pioreactor's chart colors are consistent across charts in the Overview.
- added `random()` to profile expressions. This returns a number between 0 and 1.
- added `unit()` to profile expressions. This returns the pioreactor unit name. Useful for `if` statements in the common block.

#### Bug fixes
- fix `pio plugins` on workers


Expand Down
18 changes: 16 additions & 2 deletions pioreactor/experiment_profiles/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
# flake8: noqa
from __future__ import annotations

import math
from random import random

from msgspec import DecodeError
from msgspec.json import decode

from .sly import Lexer
from .sly import Parser
from pioreactor.pubsub import subscribe
from pioreactor.whoami import get_assigned_experiment_name
from pioreactor.whoami import get_unit_name
from pioreactor.whoami import is_active


Expand All @@ -34,6 +38,7 @@ class ProfileLexer(Lexer):
# != is the same as not
tokens = {
NAME,
FUNCTION,
AND,
OR,
NOT,
Expand All @@ -54,6 +59,8 @@ class ProfileLexer(Lexer):
# Tokens
UNIT_JOB_SETTING = r"([a-zA-Z_\$][a-zA-Z0-9_]*:){2,}([a-zA-Z_\$][a-zA-Z0-9_]*\.)*[a-zA-Z_\$][a-zA-Z0-9_]*"

FUNCTION = r"[a-zA-Z_$][a-zA-Z0-9_]*\(\)"

NAME = r"[a-zA-Z_$][a-zA-Z0-9_]*"
NAME["and"] = AND
NAME["or"] = OR
Expand Down Expand Up @@ -141,11 +148,18 @@ def expr(self, p):
def expr(self, p):
return not p.expr

@_("FUNCTION")
def expr(self, p):
if p.FUNCTION == "random()":
return random()
elif p.FUNCTION == "unit()":
return get_unit_name()

@_("NAME")
def expr(self, p):
if p.NAME == "True":
if p.NAME.lower() == "true":
return True
elif p.NAME == "False":
elif p.NAME.lower() == "false":
return False
else:
return p.NAME
Expand Down
6 changes: 6 additions & 0 deletions pioreactor/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,17 @@ def test_calculator():
assert parse_profile_expression("-1.5 * 2.0") == -3.0
assert parse_profile_expression("-1.5 * -2.0") == 3.0
assert parse_profile_expression("-1.5 / -2.0") == 0.75
assert 0 <= parse_profile_expression("random()") <= 1.0
assert 25 <= parse_profile_expression("25 + (25 * random())") <= 50

with pytest.raises(ZeroDivisionError):
assert parse_profile_expression("-1.5 / 0") == 0.75


def test_unit_function():
assert parse_profile_expression(f"unit() == {unit}")


def test_mqtt_fetches_with_calculations():
experiment = "_testing_experiment"
publish(
Expand Down

0 comments on commit f6f510b

Please sign in to comment.