Skip to content

[query] Which is better to use...? #4

Closed

Description

@MarioMey

Seeing change_order.py, I realized that you used @contextmanager functions to use with statement. You didn't use them in other scripts. I would like to know why you used it. Is it better? Does it close automatically the reference or something like that?

Another question: can I ask this queries here? 😁

Activity

  1. upgradeQ commented on Jan 22, 2021

    @upgradeQ
    Owner

    @MarioMey I've used context managers a lot in Scripted-text. Yes, yes as soon as its leaves closure.
    You might also want to use ExitStack since it can select context mangers based on condition. It also reduces nesting level e.g consider this code:

    def sanic_effect(self):
        "really fast speed text scrolling(filter)"
        # add filter scroll to source if not present,
        self.update_text(self._scripted_text)
        with source_ar(self.source_name) as source, filter_ar(
            source, "py_scroll"
        ) as scroll:
            if scroll is None:
                with data_ar() as settings:
                    obs.obs_data_set_int(settings, "speed_x", 5000)
                    with p_source_ar("scroll_filter", "py_scroll", settings) as _source:
                        obs.obs_source_filter_add(source, _source)
    
            with data_ar(scroll) as filter_settings:
                obs.obs_data_set_int(filter_settings, "speed_x", 5000)
                obs.obs_source_update(scroll, filter_settings)
                if self.duration // self.refresh_rate <= 3:
                    obs.obs_source_filter_remove(source, scroll)
                    self.duration = 0

    Using ExitStack unreleased version

    def sanic_effect(self):
        "really fast speed text scrolling(filter)"
        # add filter scroll to source if not present,
        self.update_text(self._scripted_text)
        with ExitStack() as stack:
            source = stack.enter_context(source_ar(self.source_name))
            scroll = stack.enter_context(filter_ar(source, "py_scroll"))
            if scroll is None:
                settings = stack.enter_context(data_ar())
                obs.obs_data_set_int(settings, "speed_x", 5000)
                _source = stack.enter_context(
                    p_source_ar("scroll_filter", "py_scroll", settings)
                )
                obs.obs_source_filter_add(source, _source)
            filter_settings = stack.enter_context(data_ar(scroll))
            obs.obs_data_set_int(filter_settings, "speed_x", 5000)
            obs.obs_source_update(scroll, filter_settings)
            if self.duration // self.refresh_rate <= 3:
                obs.obs_source_filter_remove(source, scroll)
                self.duration = 0

    Sure, I think we can try using Discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions