Skip to content

Simplify setting of attributes #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

swazrgb
Copy link
Contributor

@swazrgb swazrgb commented Oct 18, 2020

  • Support omitting the value for state.set('domain.value', some_attr='abc')

  • Support setting attributes like domain.value.some_attr = 'abc'

  • Support setting attributes like state.set_attr('domain.value.attr', 'abc')

@swazrgb swazrgb force-pushed the set-attr branch 2 times, most recently from f2e7472 to eff1c21 Compare October 18, 2020 05:06
* Support omitting the value for `state.set('domain.value', some_attr='abc')`

* Support setting attributes like `domain.value.some_attr = 'abc'`

* Support setting attributes like `state.set_attr('domain.value.attr', 'abc')`
@craigbarratt
Copy link
Member

This looks good - thanks for submitting.

Could you also include updates to tests for the new features, and docs to reflect these changes?

@swazrgb
Copy link
Contributor Author

swazrgb commented Oct 18, 2020

I've updated the docs.

I assume I should be looking at test_function.py and update/add tests there? I'm trying to understand the result of literal_eval(await wait_until_done(notify_q)), but am a bit lost.

I was thinking about using the following pyscript to test:

pyscript.test_attr.a = False
pyscript.test_attr.b = False

@service
def test_set_attr(key, value):
    state.set_attr(f"pyscript.test_attr.{key}", value)

@service
def test_attr_a(value):
    pyscript.test_attr.a = value

@service
def test_attr_b(value):
    pyscript.test_attr.a = value

@state_trigger("True or pyscript.test_attr")
def test_attr():
    pyscript.test_attr = "on" if any([pyscript.test1.a, pyscript.test1.b]) else "off"

@craigbarratt
Copy link
Member

It's probably sufficient to add expressions and results to tests/test_unit_eval.py.

The first test function in that file executes all the tests in the long list. The list has pairs of [source_code, expected_result].

So you could do things like:

    ["state.set_attr('x.y.attr1', 10); x.y.attr1", 10],

You should test both directions (ie, setting the variable directly and checking the value with state.get_attr etc, and also testing the new form of state.set() without a value.

The second function checks for exceptions. So you could add tests that raise specific exceptions, and confirm you get the correct error. Generally when I write those, I first put in an empty error message so the test fails, and I can see what error is generated. So long as that looks correct, I then paste that in as the expected error message.

@craigbarratt
Copy link
Member

You'll need to rebase given the changes to master.

Also, are you planning to add some tests per the discussion above?

@craigbarratt
Copy link
Member

@swazrgb - do you plan to update this PR? Or do you want me to manually merge the changes?

@craigbarratt
Copy link
Member

Manually added these changes, with a few tweaks, and added tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants