Skip to main content

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.

Metadata

Release files for sorting-by-average 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sorting-by-average 0.1.0
File Size Uploaded Provenance Actions
sorting_by_average-0.1.0.tar.gz 7.9 kB Without provenance Details for sorting_by_average-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sorting-by-average 0.1.0
File Interpreter ABI Platform Actions
sorting_by_average-0.1.0-py3-none-any.whl Python 3 none any Details for sorting_by_average-0.1.0-py3-none-any.whl

Total release size: 14.5 kB

Release files / sorting_by_average-0.1.0.tar.gz

Download URL sorting_by_average-0.1.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
7070eabb1b26c2410081d69a40fb6787ebda4f8a76b2086fcae575ffac441cd8
BLAKE2b-256 checksum
How to use checksums
ee08625bb9a6c1c533a79d74b30a130dd8683e7cb594da22dfc5ff4d86970ace
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.5

Release files / sorting_by_average-0.1.0-py3-none-any.whl

Download URL sorting_by_average-0.1.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
da51afea039e6ce089382d59a26f46db415529daaf680b2c23297043811f1f9c
BLAKE2b-256 checksum
How to use checksums
5bc1c821396e347fa8085677a1bc84f4839e046a659a955b80dcbef36cb6234f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.5

Security for sorting-by-average 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page