Skip to main content

Python library for sorting collections of integers, dictionaries, or tuples based on their average values.

Project description

SortByAverage Library

SortByAverage is a powerful and intuitive Python library for sorting collections of integers, dictionaries, or tuples based on their average values. It provides flexible sorting options, supports custom data transformations via external functions, and seamlessly handles complex datasets, making it perfect for tasks like medical data analysis, network monitoring, or statistical processing.

Key Features

  • Sort arrays of integers, dictionaries, or tuples by calculating average values.
  • Extract and sort dictionary values using customizable keys for precision.
  • Apply external functions to transform or filter data before sorting.
  • Retrieve original datasets with sorted and calculated values included.
  • Simple, lightweight API for quick integration.

Installation

Install the library using pip:

pip install sortbyaverage

Usage Examples

Sorting Medical Data by Average Heart Rate

Sort a dataset of patient records by their average heart rate, stored as a list of dictionaries:

from sortbyaverage import SortByAverage

medical_data = [
    {"patient_id": 1, "heart_rate": 75},
    {"patient_id": 2, "heart_rate": 80},
    {"patient_id": 3, "heart_rate": 65},
    {"patient_id": 4, "heart_rate": 90}
]

sorter = SortByAverage(
    data=medical_data,
    dict_key_value="heart_rate",
    dict_key_index="patient_id"
)
sorted_data = sorter.sort()
print(sorted_data)

Output:

[65, 75, 80, 90]

Sorting Network Latency Data

Sort network latency data from multiple nodes, stored as a list of dictionaries:

network_data = [
    {"node": "A", "latency": 120},
    {"node": "B", "latency": 110},
    {"node": "C", "latency": 150},
    {"node": "D", "latency": 130}
]

sorter = SortByAverage(
    data=network_data,
    dict_key_value="latency",
    dict_key_index="node"
)
sorted_data = sorter.sort()
print(sorted_data)

Output:

[110, 120, 130, 150]

Custom Data Processing with External Functions

Transform data before sorting using external functions. For example, multiply heart rate values by 2:

def process_values(data, **kwargs):
    return [value * 2 for value in data]

sorter = SortByAverage(
    data=medical_data,
    dict_key_value="heart_rate",
    dict_key_index="patient_id",
    module_name="processing_module",
    function_name="process_values",
    is_i_called=True
)
sorted_data = sorter.sort()
print(sorted_data)

Output:

[130, 150, 160, 180]

The is_i_called parameter allows the external function to access iteration counts for dynamic, step-based processing.

Retrieving Original Data with Calculated Averages

Retrieve the original dataset with sorted values and additional calculated averages using get_original_data_from_dict:

sorted_full_data = sorter.get_original_data_from_dict()
print(sorted_full_data)

Output:

[
    {"patient_id": 3, "heart_rate": 65, "calculated_value": 130},
    {"patient_id": 1, "heart_rate": 75, "calculated_value": 150},
    {"patient_id": 2, "heart_rate": 80, "calculated_value": 160},
    {"patient_id": 4, "heart_rate": 90, "calculated_value": 180}
]

This method preserves the original data structure while adding transformed and sorted values under a specified key.

Contributing

I welcome contributions! Please submit issues or pull requests on the GitHub repository.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sorting_by_average-0.1.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sorting_by_average-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file sorting_by_average-0.1.0.tar.gz.

File metadata

  • Download URL: sorting_by_average-0.1.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.5

File hashes

Hashes for sorting_by_average-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7070eabb1b26c2410081d69a40fb6787ebda4f8a76b2086fcae575ffac441cd8
MD5 15c40d530b72bf4891f4feea2b2217f8
BLAKE2b-256 ee08625bb9a6c1c533a79d74b30a130dd8683e7cb594da22dfc5ff4d86970ace

See more details on using hashes here.

File details

Details for the file sorting_by_average-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sorting_by_average-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da51afea039e6ce089382d59a26f46db415529daaf680b2c23297043811f1f9c
MD5 b2f370d05fcda3ea290bee02c03966c0
BLAKE2b-256 5bc1c821396e347fa8085677a1bc84f4839e046a659a955b80dcbef36cb6234f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page