From 356476217d47c17018a4e75afa1ac8531a186f10 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Mon, 20 Nov 2023 01:06:17 +0300 Subject: [PATCH] added profiling script, optimised imports --- .gitignore | 2 ++ akarpov/files/services/lema.py | 9 +++++++-- akarpov/files/services/preview.py | 9 ++++++++- akarpov/music/services/youtube.py | 3 +-- test_start.py | 32 +++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test_start.py diff --git a/.gitignore b/.gitignore index d71c9e4..2c1b25a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ .idea +django_setup.txt +django_setup.prof ### Python template # Byte-compiled / optimized / DLL files diff --git a/akarpov/files/services/lema.py b/akarpov/files/services/lema.py index 719921b..388d91f 100644 --- a/akarpov/files/services/lema.py +++ b/akarpov/files/services/lema.py @@ -8,20 +8,25 @@ russian_stopwords = set(stopwords.words("russian")) # Set up lemmatizers -english_lemmatizer = WordNetLemmatizer() -russian_lemmatizer = MorphAnalyzer() +english_lemmatizer = None +russian_lemmatizer = None def lemmatize_and_remove_stopwords(text, language="english"): # Tokenize the text + global english_lemmatizer, russian_lemmatizer tokens = word_tokenize(text) # Lemmatize each token based on the specified language lemmatized_tokens = [] for token in tokens: if language == "russian": + if not russian_lemmatizer: + russian_lemmatizer = MorphAnalyzer() lemmatized_token = russian_lemmatizer.parse(token)[0].normal_form else: # Default to English + if not english_lemmatizer: + english_lemmatizer = WordNetLemmatizer() lemmatized_token = english_lemmatizer.lemmatize(token) lemmatized_tokens.append(lemmatized_token) diff --git a/akarpov/files/services/preview.py b/akarpov/files/services/preview.py index 4b761c3..fd83a52 100644 --- a/akarpov/files/services/preview.py +++ b/akarpov/files/services/preview.py @@ -16,7 +16,7 @@ "Consola.ttf", ] -manager = PreviewManager(cache_path, create_folder=True) +manager = None def textfile_to_image(textfile_path) -> Image: @@ -79,7 +79,10 @@ def _font_points_to_pixels(pt): def create_preview(file_path: str) -> str: + global manager # TODO: add text image generation/code image + if not manager: + manager = PreviewManager(cache_path, create_folder=True) if manager.has_jpeg_preview(file_path): return manager.get_jpeg_preview(file_path, height=500) return "" @@ -91,6 +94,10 @@ def get_file_mimetype(file_path: str) -> str: def get_description(file_path: str) -> str: + global manager + if not manager: + manager = PreviewManager(cache_path, create_folder=True) + if manager.has_text_preview(file_path): return manager.get_text_preview(file_path) return "" diff --git a/akarpov/music/services/youtube.py b/akarpov/music/services/youtube.py index a52d676..b95cbb1 100644 --- a/akarpov/music/services/youtube.py +++ b/akarpov/music/services/youtube.py @@ -9,7 +9,6 @@ from PIL import Image from pydub import AudioSegment from pytube import Search, YouTube -from yt_dlp import YoutubeDL from akarpov.music.models import Song from akarpov.music.services.db import load_track @@ -67,7 +66,7 @@ def parse_description(description: str) -> list: def download_from_youtube_link(link: str, user_id: int) -> Song: song = None - with YoutubeDL(ydl_opts) as ydl: + with yt_dlp.YoutubeDL(ydl_opts) as ydl: info_dict = ydl.extract_info(link, download=False) title = info_dict.get("title", None) description = info_dict.get("description", None) diff --git a/test_start.py b/test_start.py new file mode 100644 index 0000000..71ef5d7 --- /dev/null +++ b/test_start.py @@ -0,0 +1,32 @@ +import cProfile +import os +import pstats + +import django + + +# Function to run the Django setup process, which you want to profile +def django_setup(): + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local") + django.setup() + + +# Create a Profile object and run the django_setup function +profiler = cProfile.Profile() +profiler.enable() +django_setup() +profiler.disable() + +# Write the stats to a .prof file +profiler.dump_stats("django_setup.prof") + +# Create a Stats object and print the sorted stats to a text file +with open("django_setup.txt", "w") as stream: + stats = pstats.Stats(profiler, stream=stream) + stats.sort_stats("cumtime") # Sort the statistics by cumulative time + stats.print_stats() # Print the statistics to the stream + +# Optionally, print the stats to the console as well +stats = pstats.Stats(profiler) +stats.sort_stats("cumtime") +stats.print_stats()