Skip to content
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

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

Closed
MarioMey opened this issue Jan 22, 2021 · 1 comment
Closed

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

MarioMey opened this issue Jan 22, 2021 · 1 comment

Comments

@MarioMey
Copy link
Contributor

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? 😁

@upgradeQ
Copy link
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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants