From 2bf743e9ff51651eadd242dd83d20f9e3873415b Mon Sep 17 00:00:00 2001 From: Onur ULUSOY Date: Wed, 29 May 2024 16:43:30 +0300 Subject: [PATCH] performance optimization --- tiger/tools/browser/open_url.py | 3 ++- .../telegram/as_user/delete_message.py | 9 +++++---- .../telegram/as_user/get_last_dialogs.py | 9 +++++---- .../telegram/as_user/get_last_messages.py | 11 ++++++----- .../communication/telegram/as_user/send_message.py | 11 ++++++----- .../tools/communication/telegram/as_user/signin.py | 11 ++++++----- tiger/tools/interpreter/python/check_package.py | 3 ++- tiger/tools/interpreter/python/execute.py | 3 ++- tiger/tools/interpreter/python/install_package.py | 7 ++++--- tiger/tools/interpreter/sh/execute.py | 3 ++- tiger/tools/knowledge/delete.py | 12 +++++++----- tiger/tools/knowledge/index.py | 14 ++++++++------ tiger/tools/knowledge/pull.py | 14 ++++++++------ tiger/tools/knowledge/put.py | 11 ++++++----- tiger/tools/knowledge/reset.py | 11 ++++++----- tiger/tools/search/duckduckgo.py | 5 +++-- tiger/tools/search/google.py | 3 ++- tiger/tools/search/read_website.py | 9 +++++---- tiger/tools/system/architecture.py | 3 ++- tiger/tools/system/clipboard.py | 3 ++- tiger/tools/system/os_name.py | 3 ++- tiger/tools/system/python_version.py | 3 ++- 22 files changed, 93 insertions(+), 68 deletions(-) diff --git a/tiger/tools/browser/open_url.py b/tiger/tools/browser/open_url.py index 5cd2d3e..616020d 100644 --- a/tiger/tools/browser/open_url.py +++ b/tiger/tools/browser/open_url.py @@ -1,4 +1,3 @@ -import webbrowser def open_url(url) -> bool: @@ -7,6 +6,8 @@ def open_url(url) -> bool: :param url: str: """ + import webbrowser + try: webbrowser.open(url) return True diff --git a/tiger/tools/communication/telegram/as_user/delete_message.py b/tiger/tools/communication/telegram/as_user/delete_message.py index e3c5fbc..e97acc9 100644 --- a/tiger/tools/communication/telegram/as_user/delete_message.py +++ b/tiger/tools/communication/telegram/as_user/delete_message.py @@ -1,7 +1,3 @@ -import asyncio -import time - -from telethon.sync import TelegramClient def delete_message(id: int, message: str): @@ -11,6 +7,11 @@ def delete_message(id: int, message: str): :param message: str: """ + import asyncio + import time + + from telethon.sync import TelegramClient + id = int(id) import nest_asyncio diff --git a/tiger/tools/communication/telegram/as_user/get_last_dialogs.py b/tiger/tools/communication/telegram/as_user/get_last_dialogs.py index 54f166c..062a037 100644 --- a/tiger/tools/communication/telegram/as_user/get_last_dialogs.py +++ b/tiger/tools/communication/telegram/as_user/get_last_dialogs.py @@ -1,7 +1,3 @@ -import asyncio -import time - -from telethon.sync import TelegramClient def get_last_dialogs(limit=100): @@ -10,6 +6,11 @@ def get_last_dialogs(limit=100): :param limit: (Default value = 100) """ + import asyncio + import time + + from telethon.sync import TelegramClient + import nest_asyncio nest_asyncio.apply() diff --git a/tiger/tools/communication/telegram/as_user/get_last_messages.py b/tiger/tools/communication/telegram/as_user/get_last_messages.py index 93813ca..c4bd32b 100644 --- a/tiger/tools/communication/telegram/as_user/get_last_messages.py +++ b/tiger/tools/communication/telegram/as_user/get_last_messages.py @@ -1,8 +1,3 @@ -import asyncio -import time - -from telethon.sync import TelegramClient - def get_last_messages(id: int, limit=100): """ @@ -11,6 +6,12 @@ def get_last_messages(id: int, limit=100): :param limit: (Default value = 100) """ + import asyncio + import time + + from telethon.sync import TelegramClient + + id = int(id) import nest_asyncio diff --git a/tiger/tools/communication/telegram/as_user/send_message.py b/tiger/tools/communication/telegram/as_user/send_message.py index f73ab51..dc3fc73 100644 --- a/tiger/tools/communication/telegram/as_user/send_message.py +++ b/tiger/tools/communication/telegram/as_user/send_message.py @@ -1,8 +1,3 @@ -import asyncio -import time - -from telethon.sync import TelegramClient - def send_message(id: int, message: str) -> str: """ @@ -11,6 +6,12 @@ def send_message(id: int, message: str) -> str: :param message: str: """ + import asyncio + import time + + from telethon.sync import TelegramClient + + id = int(id) import nest_asyncio diff --git a/tiger/tools/communication/telegram/as_user/signin.py b/tiger/tools/communication/telegram/as_user/signin.py index b8457f3..9a92126 100644 --- a/tiger/tools/communication/telegram/as_user/signin.py +++ b/tiger/tools/communication/telegram/as_user/signin.py @@ -1,11 +1,12 @@ -import asyncio -import time - -from telethon.sync import TelegramClient - def signin(): """ """ + import asyncio + import time + + from telethon.sync import TelegramClient + + import nest_asyncio nest_asyncio.apply() diff --git a/tiger/tools/interpreter/python/check_package.py b/tiger/tools/interpreter/python/check_package.py index 49893c7..b8558b7 100644 --- a/tiger/tools/interpreter/python/check_package.py +++ b/tiger/tools/interpreter/python/check_package.py @@ -1,6 +1,7 @@ -from importlib_metadata import distribution, PackageNotFoundError def check_package(package:str) -> bool: + from importlib_metadata import distribution, PackageNotFoundError + try: distribution(package) print(f"'{package}' is installed.") diff --git a/tiger/tools/interpreter/python/execute.py b/tiger/tools/interpreter/python/execute.py index 6d41acd..9b33861 100644 --- a/tiger/tools/interpreter/python/execute.py +++ b/tiger/tools/interpreter/python/execute.py @@ -1,7 +1,8 @@ -from IPython import get_ipython def execute(cell:str) -> str: + from IPython import get_ipython + ipython = get_ipython() result = ipython.run_cell(cell) log = str(result.result) diff --git a/tiger/tools/interpreter/python/install_package.py b/tiger/tools/interpreter/python/install_package.py index 4a30dd6..bd4c91b 100644 --- a/tiger/tools/interpreter/python/install_package.py +++ b/tiger/tools/interpreter/python/install_package.py @@ -1,8 +1,9 @@ -from subprocess import check_call -from importlib_metadata import distribution - def install_package(package:str) -> bool: + from subprocess import check_call + from importlib_metadata import distribution + + try: check_call(["pip", "install", package]) distribution(package) diff --git a/tiger/tools/interpreter/sh/execute.py b/tiger/tools/interpreter/sh/execute.py index 9a905d8..2abf4a7 100644 --- a/tiger/tools/interpreter/sh/execute.py +++ b/tiger/tools/interpreter/sh/execute.py @@ -1,7 +1,8 @@ -from subprocess import Popen, PIPE def execute(script:str) -> str: + from subprocess import Popen, PIPE + process = Popen(script, stdout=PIPE, shell=True, stderr=PIPE, universal_newlines=True) stdout, stderr = process.communicate() log = stdout diff --git a/tiger/tools/knowledge/delete.py b/tiger/tools/knowledge/delete.py index a3f7c6c..6570ee3 100644 --- a/tiger/tools/knowledge/delete.py +++ b/tiger/tools/knowledge/delete.py @@ -1,9 +1,4 @@ -import inspect -import os -import upsonic -from tinydb import Query -from tinydb import TinyDB def delete(knowledge_name: str) -> bool: @@ -12,6 +7,13 @@ def delete(knowledge_name: str) -> bool: :param knowledge_name: str: """ + import inspect + import os + + import upsonic + from tinydb import Query + from tinydb import TinyDB + folder = os.path.join(os.path.dirname(inspect.getfile(upsonic)), "upsonic_tiger_knowledge.json") db = TinyDB(folder) diff --git a/tiger/tools/knowledge/index.py b/tiger/tools/knowledge/index.py index 84d7210..c13f8d0 100644 --- a/tiger/tools/knowledge/index.py +++ b/tiger/tools/knowledge/index.py @@ -1,13 +1,15 @@ -import inspect -import os - -import upsonic -from tinydb import Query -from tinydb import TinyDB def index() -> list: """ """ + import inspect + import os + + import upsonic + from tinydb import Query + from tinydb import TinyDB + + folder = os.path.join(os.path.dirname(inspect.getfile(upsonic)), "upsonic_tiger_knowledge.json") db = TinyDB(folder) diff --git a/tiger/tools/knowledge/pull.py b/tiger/tools/knowledge/pull.py index 0981cb1..44d0260 100644 --- a/tiger/tools/knowledge/pull.py +++ b/tiger/tools/knowledge/pull.py @@ -1,9 +1,3 @@ -import inspect -import os - -import upsonic -from tinydb import Query -from tinydb import TinyDB def pull(knowledge_name: str) -> str: @@ -12,6 +6,14 @@ def pull(knowledge_name: str) -> str: :param knowledge_name: str: """ + import inspect + import os + + import upsonic + from tinydb import Query + from tinydb import TinyDB + + folder = os.path.join(os.path.dirname(inspect.getfile(upsonic)), "upsonic_tiger_knowledge.json") db = TinyDB(folder) diff --git a/tiger/tools/knowledge/put.py b/tiger/tools/knowledge/put.py index d204541..343089a 100644 --- a/tiger/tools/knowledge/put.py +++ b/tiger/tools/knowledge/put.py @@ -1,9 +1,4 @@ -import inspect -import os -import upsonic -from tinydb import Query -from tinydb import TinyDB def put(knowledge_name: str, description: str) -> bool: @@ -13,6 +8,12 @@ def put(knowledge_name: str, description: str) -> bool: :param description: str: """ + import inspect + import os + + import upsonic + from tinydb import Query + from tinydb import TinyDB folder = os.path.join(os.path.dirname(inspect.getfile(upsonic)), "upsonic_tiger_knowledge.json") db = TinyDB(folder) diff --git a/tiger/tools/knowledge/reset.py b/tiger/tools/knowledge/reset.py index 60d8c53..06ecbbd 100644 --- a/tiger/tools/knowledge/reset.py +++ b/tiger/tools/knowledge/reset.py @@ -1,13 +1,14 @@ -import inspect -import os -import upsonic -from tinydb import Query -from tinydb import TinyDB def reset() -> None: """ """ + import inspect + import os + + import upsonic + from tinydb import Query + from tinydb import TinyDB folder = os.path.join(os.path.dirname(inspect.getfile(upsonic)), "upsonic_tiger_knowledge.json") db = TinyDB(folder) diff --git a/tiger/tools/search/duckduckgo.py b/tiger/tools/search/duckduckgo.py index 88d1a0e..47cbc50 100644 --- a/tiger/tools/search/duckduckgo.py +++ b/tiger/tools/search/duckduckgo.py @@ -1,7 +1,8 @@ -from duckduckgo_search import DDGS - def search(query:str, max_number:int=20) -> list: + from duckduckgo_search import DDGS + + return [result["href"] for result in DDGS().text(query, max_results=max_number)] diff --git a/tiger/tools/search/google.py b/tiger/tools/search/google.py index a18817e..dfc6c28 100644 --- a/tiger/tools/search/google.py +++ b/tiger/tools/search/google.py @@ -1,6 +1,7 @@ -from googlesearch import search as gsearch + def google(query:str, max_number:int=20) -> list: + from googlesearch import search as gsearch return list(gsearch(query, stop=max_number)) diff --git a/tiger/tools/search/read_website.py b/tiger/tools/search/read_website.py index 4c0071a..39e9b6f 100644 --- a/tiger/tools/search/read_website.py +++ b/tiger/tools/search/read_website.py @@ -1,10 +1,11 @@ -import requests - -from bs4 import BeautifulSoup -import re def read_website(url: str, max_content_lenght: int = 5000) -> dict: + import requests + + from bs4 import BeautifulSoup + import re + html = requests.get(url).text soup = BeautifulSoup(html) meta_properties = [ diff --git a/tiger/tools/system/architecture.py b/tiger/tools/system/architecture.py index 98bc767..411a8a3 100644 --- a/tiger/tools/system/architecture.py +++ b/tiger/tools/system/architecture.py @@ -1,7 +1,8 @@ -import platform def architecture(): + import platform + return platform.architecture()[0] diff --git a/tiger/tools/system/clipboard.py b/tiger/tools/system/clipboard.py index 4594e91..a1ddfea 100644 --- a/tiger/tools/system/clipboard.py +++ b/tiger/tools/system/clipboard.py @@ -1,7 +1,8 @@ -import pyperclip + def copy(text): + import pyperclip pyperclip.copy(text) diff --git a/tiger/tools/system/os_name.py b/tiger/tools/system/os_name.py index 10999cf..b1219c3 100644 --- a/tiger/tools/system/os_name.py +++ b/tiger/tools/system/os_name.py @@ -1,6 +1,7 @@ -import platform def os_name(): + import platform + system_name = platform.system() if system_name == 'Windows': return 'Windows' diff --git a/tiger/tools/system/python_version.py b/tiger/tools/system/python_version.py index 20bcf6d..14d168b 100644 --- a/tiger/tools/system/python_version.py +++ b/tiger/tools/system/python_version.py @@ -1,7 +1,8 @@ -import platform def python_version(): + import platform + return platform.python_version()