Skip to content

Instantly share code, notes, and snippets.

@angstwad
Last active December 2, 2024 22:37
Show Gist options
  • Select an option

  • Save angstwad/94d11c71e3837fa0e9a56ecd069bf3b0 to your computer and use it in GitHub Desktop.

Select an option

Save angstwad/94d11c71e3837fa0e9a56ecd069bf3b0 to your computer and use it in GitHub Desktop.

Revisions

  1. angstwad revised this gist Dec 2, 2024. No changes.
  2. angstwad renamed this gist Dec 2, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. angstwad revised this gist Dec 2, 2024. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Copyright 2024 Paul Durivage <pauldurivage+github@gmail.com>

    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at

    # http://www.apache.org/licenses/LICENSE-2.0

    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
  4. angstwad revised this gist Dec 2, 2024. 1 changed file with 0 additions and 13 deletions.
    13 changes: 0 additions & 13 deletions singleton-LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    # Copyright 2024 Paul Durivage <pauldurivage+github@gmail.com>

    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at

    # http://www.apache.org/licenses/LICENSE-2.0

    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
  5. angstwad renamed this gist Dec 2, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. angstwad renamed this gist Dec 2, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. angstwad created this gist Dec 2, 2024.
    13 changes: 13 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    # Copyright 2024 Paul Durivage <pauldurivage+github@gmail.com>

    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at

    # http://www.apache.org/licenses/LICENSE-2.0

    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    26 changes: 26 additions & 0 deletions singleton.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    class SingletonMixin:
    """ Threading safe singleton mixin class. Will run __init__ on subclasses for every
    invocation, so check self._initialized from __init__ to get around this if not desired.
    Use:
    class MyClass(SingletonMixin):
    def __init__(self):
    if not self._initialized:
    # initialize here
    self.foo = 'bar'
    # when done, set initialized to True
    self._initialized = True
    """
    _instances: dict[type, Any] = {}
    _lock = threading.Lock()
    _initialized: bool

    def __new__(cls):
    with SingletonMixin._lock:
    if cls not in SingletonMixin._instances:
    # Create instance
    instance = super().__new__(cls)
    SingletonMixin._instances[cls] = instance
    instance._initialized = False
    # Return existing instance
    return SingletonMixin._instances[cls]