Adding this expressions #23

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
1 participant
@tomerfiliba

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/  <==
Adding the `this` expression (backported from construct3). The `this`
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 join this conversation on GitHub. Already have an account? Sign in to comment