added profiling script, optimised imports

This commit is contained in:
Alexander Karpov 2023-11-20 01:06:17 +03:00
parent 9ac5a1f235
commit 356476217d
5 changed files with 50 additions and 5 deletions

2
.gitignore vendored
View File

@ -1,4 +1,6 @@
.idea .idea
django_setup.txt
django_setup.prof
### Python template ### Python template
# Byte-compiled / optimized / DLL files # Byte-compiled / optimized / DLL files

View File

@ -8,20 +8,25 @@
russian_stopwords = set(stopwords.words("russian")) russian_stopwords = set(stopwords.words("russian"))
# Set up lemmatizers # Set up lemmatizers
english_lemmatizer = WordNetLemmatizer() english_lemmatizer = None
russian_lemmatizer = MorphAnalyzer() russian_lemmatizer = None
def lemmatize_and_remove_stopwords(text, language="english"): def lemmatize_and_remove_stopwords(text, language="english"):
# Tokenize the text # Tokenize the text
global english_lemmatizer, russian_lemmatizer
tokens = word_tokenize(text) tokens = word_tokenize(text)
# Lemmatize each token based on the specified language # Lemmatize each token based on the specified language
lemmatized_tokens = [] lemmatized_tokens = []
for token in tokens: for token in tokens:
if language == "russian": if language == "russian":
if not russian_lemmatizer:
russian_lemmatizer = MorphAnalyzer()
lemmatized_token = russian_lemmatizer.parse(token)[0].normal_form lemmatized_token = russian_lemmatizer.parse(token)[0].normal_form
else: # Default to English else: # Default to English
if not english_lemmatizer:
english_lemmatizer = WordNetLemmatizer()
lemmatized_token = english_lemmatizer.lemmatize(token) lemmatized_token = english_lemmatizer.lemmatize(token)
lemmatized_tokens.append(lemmatized_token) lemmatized_tokens.append(lemmatized_token)

View File

@ -16,7 +16,7 @@
"Consola.ttf", "Consola.ttf",
] ]
manager = PreviewManager(cache_path, create_folder=True) manager = None
def textfile_to_image(textfile_path) -> Image: def textfile_to_image(textfile_path) -> Image:
@ -79,7 +79,10 @@ def _font_points_to_pixels(pt):
def create_preview(file_path: str) -> str: def create_preview(file_path: str) -> str:
global manager
# TODO: add text image generation/code image # TODO: add text image generation/code image
if not manager:
manager = PreviewManager(cache_path, create_folder=True)
if manager.has_jpeg_preview(file_path): if manager.has_jpeg_preview(file_path):
return manager.get_jpeg_preview(file_path, height=500) return manager.get_jpeg_preview(file_path, height=500)
return "" return ""
@ -91,6 +94,10 @@ def get_file_mimetype(file_path: str) -> str:
def get_description(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): if manager.has_text_preview(file_path):
return manager.get_text_preview(file_path) return manager.get_text_preview(file_path)
return "" return ""

View File

@ -9,7 +9,6 @@
from PIL import Image from PIL import Image
from pydub import AudioSegment from pydub import AudioSegment
from pytube import Search, YouTube from pytube import Search, YouTube
from yt_dlp import YoutubeDL
from akarpov.music.models import Song from akarpov.music.models import Song
from akarpov.music.services.db import load_track 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: def download_from_youtube_link(link: str, user_id: int) -> Song:
song = None song = None
with YoutubeDL(ydl_opts) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(link, download=False) info_dict = ydl.extract_info(link, download=False)
title = info_dict.get("title", None) title = info_dict.get("title", None)
description = info_dict.get("description", None) description = info_dict.get("description", None)

32
test_start.py Normal file
View File

@ -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()