From 6fbfea9c9a3e82d50f3c9aa8b1e298fe6c407555 Mon Sep 17 00:00:00 2001 From: ptrstr Date: Sat, 3 Oct 2020 10:49:26 -0400 Subject: [PATCH] Added installation for pip --- README.md | 6 +++ entro_py_min/__main__.py | 45 +++++++++++++++++++ .../entro_py_min.py | 21 +++++++++ 3 files changed, 72 insertions(+) create mode 100644 entro_py_min/__main__.py rename entro-py-min/entro-py-min.py => entro_py_min/entro_py_min.py (61%) diff --git a/README.md b/README.md index 2eb7bd4..cce5d10 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,12 @@ Well that is basically up to you. Entropy functions are used in Computer Science *Warning:* This can only be used for calculating the entropy of strings (by alphabet). There are however other types like coin tosses of fair or unfair coins (...), but you're gonna have to write calculators for this on your own - for now. +## Installing +You can install this package easily with `pip`: +``` +$ pip install git+https://github.com/creyD/entro.py@dev_package +``` + ## Usage You can run as much calculations as you want in one run of the script. For example use it like this with a simple string: ``` diff --git a/entro_py_min/__main__.py b/entro_py_min/__main__.py new file mode 100644 index 0000000..e1fdc02 --- /dev/null +++ b/entro_py_min/__main__.py @@ -0,0 +1,45 @@ +from . import entro_py_min +import argparse + + +# List of the arguments one can use to influence the behavior of the program +parser = argparse.ArgumentParser('entro_py_min', description='Calculate the information entropy of alphabets.') + +# INPUT ARGUMENTS +parser.add_argument('strings', nargs='*', default='', type=str, help='Strings to calculate the entropy of.') +parser.add_argument('--files', nargs='*', type=str, default='', help='Provide file path(s) to calculate the entropy of.') + +# OUTPUT OPTIONS +parser.add_argument('--simple', nargs='?', type=bool, default=False, help='Determines the explicitness of the output. (True = only entropy shown)') +parser.add_argument('--max', nargs='?', type=bool, default=False, help='Includes the maximum entropy.') + +# CONVERT OPTIONS +parser.add_argument('--lower', nargs='?', type=bool, default=False, help='Converts given strings or textfiles to lowercase before calculating.') +parser.add_argument('--upper', nargs='?', type=bool, default=False, help='Converts given strings or textfiles to uppercase before calculating.') +parser.add_argument('--squash', nargs='?', type=bool, default=False, help='Removes all whitespaces before calculating.') +args = parser.parse_args() + +# Prepares the queue of different strings +queue = [] + +# Add all the provided strings to the list +for string in args.strings: + queue.append(string) + +# Add all the provided files to the list +for file in args.files: + string = entro_py_min.readEntropyFile(file) + queue.append(string) + +# Interates over the collected strings and prints the entropies +for string in queue: + if args.lower: + string = string.lower() + elif args.upper: + string = string.upper() + + if args.squash: + string = string.replace(" ", "") + + a, b, c = entro_py_min.calculateEntropy(string) + entro_py_min.printEntropy(string, a, b, args.simple, (False if not args.max else c)) diff --git a/entro-py-min/entro-py-min.py b/entro_py_min/entro_py_min.py similarity index 61% rename from entro-py-min/entro-py-min.py rename to entro_py_min/entro_py_min.py index 3ac0589..d03cf53 100644 --- a/entro-py-min/entro-py-min.py +++ b/entro_py_min/entro_py_min.py @@ -1,5 +1,6 @@ import math + # Calculates the entropy of a given string # Returns the entropy and an alphabet with the calculated probabilities def calculateEntropy(input_string): @@ -36,3 +37,23 @@ def calculateEntropyMin(input_string): entropy -= i * math.log(i, 2) return entropy + + +# Outputs a given entropy including the original text and the alphabet with probabilities +def printEntropy(original_string, entropy_value, alphabet_dict, simple_bool, max_value): + print('---') + if not simple_bool: + print('Content: ' + original_string) + print('Probabilities: ' + str(alphabet_dict)) + print('Entropy: ' + str(entropy_value) + ' bits') + if max_value: + print('Maximum Entropy: ' + str(max_value) + ' bits') + print('---') + + +# Reads a file by a given path +def readEntropyFile(path_string): + f = open(path_string, 'r') + content = f.read().replace('\n', ' ') + f.close() + return content.strip()