Skip to content

Ability to compose a template via subclass#345

Draft
davecap wants to merge 8 commits intomasterfrom
f-template-subclass
Draft

Ability to compose a template via subclass#345
davecap wants to merge 8 commits intomasterfrom
f-template-subclass

Conversation

@davecap
Copy link
Contributor

@davecap davecap commented May 24, 2020

Adds the ability to define templates as (very limited) classes rather than JSON. The main benefit is making it easier to create unit tests, and to write expressions with syntax highlighting.

Example of a template with pytest tests:

import solvebio as sb

sb.login(api_host="https://solvebio.api.solvebio.com")


class MyTemplate(sb.DatasetTemplate):
    # Define all field parameters in the decorator
    @sb.DatasetTemplate.field(is_transient=True)
    def hello(self, record):
        # NOTE: Use comments instead of docstrings, as docstrings are interpreted as expressions.
        # returns are optional, but can be helpful for unit testing
        return "hello"

    @sb.DatasetTemplate.field(depends_on=["hello"])
    def world(self, record):
        # Functions must be "one-liner" expressions, but can have line-breaks.
        return (
            record.hello +
            ' world'
        )

    # Functions must be one-liner expressions!
    # @sb.DatasetTemplate.field()
    # def invalid(self, record):
    #     if 1 + 1 == 2:
    #         return "this is an invalid expression"


def test_template_e2e():
    tpl = MyTemplate()

    assert len(tpl.fields) == 2

    ann = sb.Annotator(tpl.fields).annotate([{}])
    for row in ann:
        assert row.get('hello') is None
        assert row['world'] == 'hello world'


def test_hello():
    tpl = MyTemplate()

    assert tpl.hello(record={}) == 'hello'


def test_evaluate_fields():
    tpl = MyTemplate()
    assert tpl.hello.field.evaluate() == 'hello'
    # TODO: record support
    # assert tpl.world.field.evaluate(record={'hello': 'hello'}) == 'hello world'


if __name__ == "__main__":
    # TODO: get_or_create() does not work yet, coming soon!
    m = MyTemplate.get_or_create(
        name="My Template",
        version="1.0.0-beta.1"
    )
    print(m)

@davecap davecap requested a review from jsh2134 May 26, 2020 00:13
@davecap davecap force-pushed the f-template-subclass branch from e8fc956 to 517f1d5 Compare May 26, 2020 14:44
@jsh2134 jsh2134 removed their request for review August 13, 2020 15:20
if not self.get('expression'):
return None

return Expression(self['expression'], client=self._client).evaluate(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is 👍

else:
self.refresh_from(self.request('patch', self.instance_url(),
data=self.serialize(self)))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this

self[self.ID_ATTR] = id

for k, v in list(params.items()):
self[k] = v
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this moved from somwehere?

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