Python sort dictionary by value

Source:https://stackabuse.com/how-to-sort-dictionary-by-value-in-python/

Method 1

dict1 = {1: 1, 2: 9, 3: 4}
sorted_values = sorted(dict1.values()) # Sort the values
sorted_dict = {}

for i in sorted_values:
    for k in dict1.keys():
        if dict1[k] == i:
            sorted_dict[k] = dict1[k]
            break

print(sorted_dict)

Method 2

dict1 = {1: 1, 2: 9, 3: 4}
sorted_dict = {}
sorted_keys = sorted(dict1, key=dict1.get)  # [1, 3, 2]

for w in sorted_keys:
    sorted_dict[w] = dict1[w]

print(sorted_dict) # {1: 1, 3: 4, 2: 9}

Python: sort an array of tuple by the second element value

Note that it works equally well with an array of arrays.

# function to return the second element of the
# two elements passed as the parameter
def sortSecond(val):
	return val[1]

# list1 to demonstrate the use of sorting
# using second key
list1 = [(1,2),(3,3),(1,1)]

# sorts the array in ascending according to
# second element
list1.sort(key=sortSecond)
print(list1)

# sorts the array in descending according to
# second element
list1.sort(key=sortSecond,reverse=True)
print(list1)

Source: https://www.geeksforgeeks.org/sort-in-python/

Python environment

To better control the development environment of your python project and ensure that your older project don’t break when updating packages, it is recommended to create a dedicated environment for your project.

For that purpose you can use python virtual environment.

Create environment:

python3 -m venv env

Activate environment:

source env/bin/activate

Jmeter beanshell: get variable type

Examples:

if (vars.get(“price_matchNr”) instanceof Integer){
log.info(“integer”);
}
if (vars.get(“price_matchNr”) instanceof String){
log.info(“string”);
}
if (Integer.parseInt(vars.get(“price_matchNr”)) instanceof int){
log.info(“int”);
}
if (Integer.valueOf(vars.get(“price_matchNr”)) instanceof Integer){
log.info(“Integer ValueOf”);
}

Tesseract initial setup

Install Tesseract:

sudo apt install tesseract-ocr

Install python3-pil

sudo apt-get install -y python3-pil

Insall pytesseract:

pip install pytesseract

Testing Tesseract.

Call the Tesseract engine on the image with image_path and convert image to text, written line by line in the command prompt by typing the following:

$ tesseract image_path stdout

To write the output text in a file:

$ tesseract image_path text_result.txt

To specify the language model name, write language shortcut after -l flag, by default it takes English language:

$ tesseract image_path text_result.txt -l eng

By default, Tesseract expects a page of text when it segments an image. If you’re just seeking to OCR a small region, try a different segmentation mode, using the –psm argument. There are 14 modes available which can be found here. By default, Tesseract fully automates the page segmentation but does not perform orientation and script detection. To specify the parameter, type the following:

$ tesseract image_path text_result.txt -l eng --psm 6

There is also one more important argument, OCR engine mode (oem). Tesseract 4 has two OCR engines — Legacy Tesseract engine and LSTM engine. There are four modes of operation chosen using the –oem option.
0    Legacy engine only.
1    Neural nets LSTM engine only.
2    Legacy + LSTM engines.
3    Default, based on what is available.

Adding Estonian language for ubuntu package:

sudo apt-get install tesseract-ocr-[lang]

Compiling Tesseract:

To run pytesseract in python script:

pip install pytesseract

Preprocessing for Tesseract

pip install opencv-python

To avoid all the ways your tesseract output accuracy can drop, you need to make sure the image is appropriately pre-processed.

This includes rescaling, binarization, noise removal, deskewing, etc.

To preprocess image for OCR, use any of the following python functions or follow the OpenCV documentation.

Improve quality of the output (Official doc)