refactored a bit

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2023-03-31 17:59:14 +02:00
parent 1d42f2585e
commit 58441ebc16
No known key found for this signature in database
12 changed files with 75 additions and 71 deletions

View File

@ -10,7 +10,7 @@ import pymongo
from planetmint.config import Config
from planetmint.backend.exceptions import DuplicateKeyError, OperationError, ConnectionError
from transactions.common.exceptions import ConfigurationError
from planetmint.utils import Lazy
from planetmint.utils.lazy import Lazy
from planetmint.backend.connection import DBConnection, _kwargs_parser
logger = logging.getLogger(__name__)

View File

@ -9,7 +9,7 @@ import tarantool
from planetmint.config import Config
from transactions.common.exceptions import ConfigurationError
from planetmint.utils import Lazy
from planetmint.utils.lazy import Lazy
from planetmint.backend.connection import DBConnection
from planetmint.backend.exceptions import ConnectionError

View File

@ -3,8 +3,7 @@ import logging
import os
from decouple import config
from planetmint.utils import Singleton
from planetmint.version import __version__
from planetmint.utils.singleton import Singleton
class Config(metaclass=Singleton):

View File

@ -13,7 +13,7 @@ from planetmint.abci.parallel_validation import ParallelValidationApp
from planetmint.web import server, websocket_server
from planetmint.ipc.events import EventTypes
from planetmint.ipc.exchange import Exchange
from planetmint.utils import Process
from planetmint.utils.processes import Process
from planetmint.version import __version__
logger = logging.getLogger(__name__)

View File

44
planetmint/utils/lazy.py Normal file
View File

@ -0,0 +1,44 @@
class Lazy:
"""Lazy objects are useful to create chains of methods to
execute later.
A lazy object records the methods that has been called, and
replay them when the :py:meth:`run` method is called. Note that
:py:meth:`run` needs an object `instance` to replay all the
methods that have been recorded.
"""
def __init__(self):
"""Instantiate a new Lazy object."""
self.stack = []
def __getattr__(self, name):
self.stack.append(name)
return self
def __call__(self, *args, **kwargs):
self.stack.append((args, kwargs))
return self
def __getitem__(self, key):
self.stack.append("__getitem__")
self.stack.append(([key], {}))
return self
def run(self, instance):
"""Run the recorded chain of methods on `instance`.
Args:
instance: an object.
"""
last = instance
for item in self.stack:
if isinstance(item, str):
last = getattr(last, item)
else:
last = last(*item[0], **item[1])
self.stack = []
return last

View File

@ -10,15 +10,6 @@ import multiprocessing
import setproctitle
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
class Process(multiprocessing.Process):
"""Wrapper around multiprocessing.Process that uses
setproctitle to set the name of the process when running
@ -85,47 +76,3 @@ def pool(builder, size, timeout=None):
return pooled
class Lazy:
"""Lazy objects are useful to create chains of methods to
execute later.
A lazy object records the methods that has been called, and
replay them when the :py:meth:`run` method is called. Note that
:py:meth:`run` needs an object `instance` to replay all the
methods that have been recorded.
"""
def __init__(self):
"""Instantiate a new Lazy object."""
self.stack = []
def __getattr__(self, name):
self.stack.append(name)
return self
def __call__(self, *args, **kwargs):
self.stack.append((args, kwargs))
return self
def __getitem__(self, key):
self.stack.append("__getitem__")
self.stack.append(([key], {}))
return self
def run(self, instance):
"""Run the recorded chain of methods on `instance`.
Args:
instance: an object.
"""
last = instance
for item in self.stack:
if isinstance(item, str):
last = getattr(last, item)
else:
last = last(*item[0], **item[1])
self.stack = []
return last

View File

@ -0,0 +1,12 @@
import sys
def is_above_py39():
if sys.version_info.major == 3:
if sys.version_info.minor < 10:
return False
else:
return True
elif sys.version_info.major > 3:
return True
else:
return False

View File

@ -0,0 +1,7 @@
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]

View File

@ -3,15 +3,13 @@
# SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
# Code is Apache-2.0 and docs are CC-BY-4.0
import json
import logging
import asyncio
import sys
from planetmint.ipc.events import EventTypes
from planetmint.ipc.events import POISON_PILL
from planetmint.utils.python import is_above_py39
logger = logging.getLogger(__name__)
@ -62,14 +60,11 @@ class Dispatcher:
def get_queue_on_demand(app, queue_name: str):
if queue_name not in app:
logging.debug(f"creating queue: {queue_name}")
get_loop = asyncio.get_event_loop()
run_loop = asyncio.get_running_loop()
logging.debug(f"get loop: {get_loop}")
logging.debug(f"run loop: {run_loop}")
if( sys.version_info.major ==3 and sys.version_info.minor < 10 ):
app[queue_name] = asyncio.Queue(loop=get_loop)
elif( sys.version_info.major ==3 and sys.version_info.minor >= 10 ):
if is_above_py39():
app[queue_name] = asyncio.Queue()
else:
get_loop = asyncio.get_event_loop()
app[queue_name] = asyncio.Queue(loop=get_loop)
return app[queue_name]

View File

@ -451,7 +451,7 @@ def abci_server():
# from tendermint.abci import types_pb2 as types_v0_34_11
from planetmint.abci.application_logic import ApplicationLogic
from planetmint.utils import Process
from planetmint.utils.processes import Process
app = ABCIServer(app=ApplicationLogic())
abci_proxy = Process(name="ABCI", target=app.run)

View File

@ -141,7 +141,7 @@ def test_process_group_instantiates_and_start_processes(mock_process):
def test_lazy_execution():
from planetmint.utils import Lazy
from planetmint.utils.lazy import Lazy
lz = Lazy()
lz.split(",")[1].split(" ").pop(1).strip()
@ -164,7 +164,7 @@ def test_process_set_title():
from uuid import uuid4
from multiprocessing import Queue
from setproctitle import getproctitle
from planetmint.utils import Process
from planetmint.utils.processes import Process
queue = Queue()
uuid = str(uuid4())