Python scripting issues I've solved.
1. To set multiple threads:
import threading
way1 = threading.Thread(target=function1)
way2 = threading.Thread(target=function2)
way1.start()
way2.start()
way1.join()
way2.join()
2. Sort dictionary:
for i in sorted(dict):
new_dict[i] = dict[i]
with open("path/to/file",w) af file:
for key, value in new_dict.items():
file.write('\"%s\":\"%s\",\n' % (key, value))
file.close()
3. How to covert json bytes data to dictionary.
import json
var_json = json.load("file/path") // For file
For variable its quite easy:
var_json = json.loads(req.data)
4. How to check if dictionary element exist? // if key_name in dict_name:
5. How to delete element from dictionary. // dict_name.pop(key_name)
6. How to ignore errores in python functions:
try:
some_code_here:
except:
pass
7. Work with cognates. Try to use fuzzywuzzy or thefuzz, pymorphy, nltk, difflib, morphy.
7.1 To delete stems
from nltk.stem.snowball import RussianStemmer
stemmer = RussianStemmer()
list_no_graduation = [ stemmer.stem(x) for x in list_with_graduations ]
8.1 To create process process = multiprocessing.Process(target=your_function_name, args=(your_argument_name, secon_argument))
8.1. To start process process.start()
8.2. Example of usage:
from multiprocessing import Process
import os
import time
def task():
x = 0
while x<25:
time.sleep(2)
x+=1
print('This is from first process ', os.getpid())
def task2():
x2 = 0
while x2<30:
time.sleep(3)
x2+=1
print('This is from second process ', os.getpid())
process = Process(target=task)
process2 = Process(target=task2)
process.start()
process2.start()
8. To install $ pip install thefuzz
8.1 To use:
from thefuzz import fuzz
from thefuzz import process
8.2. To compare
fuzz.ratio("One example", "Another one example")
or
fuzz.partial_ratio("One example", "Another one example")
8.3 SIt is optimal to use fuzz.token_set_ratio(request, title)
8.4. Lib not suit for me
9. Start with import difflib
10. Use to get close results difflib.get_close_matches('some_word', ['some_words','cats','catterpillar','dog'],limit,coef) // It is usefull for me
11. Potentially I could use:
from difflib import SequenceMatcher as s
result = s(a="first string", b="some second string")
print(result.ratio())
Allow to get normal form.
12. To install
$ pip install pymorphy2
$ pip install -u pymorphy2-dicts-ru // Install dictionary
12.1 To get normalized form for list:
from pymorphy2 import MorphAnalyzer
morphy = MorphAnalyzer()
norm = [ morphy.parse(x)[0].normal_form for x in in some_list ]
13. Install $ pip install nltk
14. To delete stems
from nltk.stem import PorterStemmer
s = PorterStemmer()
root_word = s.stem(word_with_stem)
14.1 To use with another language for example russian
from nltk.stem.snowball import RussianStemmer
s = RussianStemmer()
14.2 To tokenize text
from nltk.tokenize import word_tokenize
separate_words = word_tokenize(original_text)
14.3 Get most frequent word
from nltk import FreqDist
f = FreqDist(tokenized_text)
14.3.1 To get five most common words
f.most_common(5)
14.4 To get pairs of words
text_tokenized.collocations()
14.5 I get an error while trying to use nltk.word_tokenize(text) I get LookupError Resource punkt not found. Please use the NLTK Downloader to obtain the resource. // Try to use nltk.download('punkt') // It helped
14.6 Delete stopwords
from nltk.corpus import stopwords
list = [word for word in tokenized_text if not word in stopwords.words()]
15. I have to compare to lists to get common elements. // set(list_one)&set(list_two)
16. Find how many times substring in text // just use text.count('phrase')
17. How to round up float digit. // rounded = round(float_digit, number_of_digits)
18. How to check if variable exist. if variable in locals(): // No
18.1 Try
try:
variable except NameError: print("not exist")
19. How to use many files in python programm.
Import file_name as fl
fl.function_name()
19.1 How to us globals in another file. Make separate file with globals and add in all files with functions.
20. How to get string which programe out.
try:
1/0
except Exception as e:
print(e.message)
21. When I assign one dictionary to another and implemennt some changes, the old one also changes. // Use new=old_dictionary.copy()
22. How to get last key of a dictionary. // dictionary_name[ list(dictionary_name.keys())[-1] ]
23. Check if a variable is a string // isinstance(string_variable, str)
24. How to find repeated elements in a list. // [list2 for i in list1 if list1.count(i)>1]
25. I have two datetime as strings, how to get difference in days.
25.1 I have to convert string into datetime format.
from datetime import datetime
date = "2023-12-13"
date_converted = datetime.strptime(date, '%Y-%m-%d')
25.2 Get difference like
diff = datetime_a - datetime_b
diff.days
26. I have list of strings, how to find elemet which contains part of the string? // [ i for i in Some_list if i[1:]==my_string]
27. How to connect two dictionaries // {**dict1, **dict2}
28. How to reflect list in reverse order is simple. // b = a[::-1]
29. How to sort list. // With for cycle use my_ist.insert(0, value) to append at the beginning
1. Get an error while debugging: TypeError: 'type' object is not subscriptable // comment out -> tuple[int, float, float]: