Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Adding this expressions #23

Closed
wants to merge 1 commit into from
Closed

Adding this expressions #23

wants to merge 1 commit into from

Conversation

tomerfiliba
Copy link

I backported the this expression that I introduced to construct3 (which I don't have time to finish). So I decided it would be a good idea to backport it. It's 100% backwards compatible, so there's no harm whatsoever, and it's much more readable and concise than those ugly lambdas.

In short:

  • instead of Field(..., lambda ctx: ctx.length) use Field(..., this.length)
  • Instead of Field(..., lambda ctx: ctx._.length * 4) use Field(..., this._.length * 4)
  • Instead of If(lambda ctx: ctx["foo"] > 17, ...), use If(this.foo > 17, ...)

I included a unit test (tests/test_this.py).

By the way, I would suggest moving the tests directory out of the package, e.g.

construct_repo/
    construct/
        core.py
        ...
    docs/
    tests/  <==

expression builds an evaluatable (callable) object directly from python
expressions, so it's less verbose and much easier to read and write.
Also, it's compatible with the existing lambda functions. Instead of
doing `lambda ctx: ctx["length"] * 2`, simply use `this.length * 2`
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant