From 710e0b20695089d9006bf8d91dedb666195031b6 Mon Sep 17 00:00:00 2001 From: Conrad Date: Mon, 15 Jul 2019 23:14:42 +0200 Subject: [PATCH] Prepared for packaging --- entro.py | 87 -------------------------------------------- entro.py/__init__.py | 1 + entro.py/entro.py | 38 +++++++++++++++++++ setup.py | 21 +++++++++++ 4 files changed, 60 insertions(+), 87 deletions(-) delete mode 100644 entro.py create mode 100644 entro.py/__init__.py create mode 100644 entro.py/entro.py create mode 100644 setup.py diff --git a/entro.py b/entro.py deleted file mode 100644 index 4d70839..0000000 --- a/entro.py +++ /dev/null @@ -1,87 +0,0 @@ -''' - calc_entro.py calculates the entropy of a given string or file - - This uses the negative sum of the log (to the base of 2) of the probability - times the probability of a char to occur in a certain string as the entropy. -''' - -import math -import argparse - - -# Calculates the entropy of a given string (as described in the docstring) -def calculateEntropy(input_string): - alphabet, alphabet_size, entropy = {}, 0, 0 - - for char in input_string: - if char in alphabet: - alphabet[char] += 1 - else: - alphabet[char] = 1 - alphabet_size += 1 - - for char in alphabet: - alphabet[char] = alphabet[char] / alphabet_size - entropy -= alphabet[char] * math.log(alphabet[char], 2) - - return entropy, alphabet - - -# Outputs a given entropy including the original text and the alphabet with probabilities -def printEntropy(original_string, entropy_value, alphabet_dict, simple_bool): - print('---') - if simple_bool == False: - print('Content: ' + original_string) - print('Probabilities: ' + str(alphabet_dict)) - print('Entropy: ' + str(entropy_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() - - -# List of the arguments one can use to influence the behavior of the program -parser = argparse.ArgumentParser(description='Calculate the information entropy of some strings.') - -# 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)') - -# 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 = readEntropyFile(file) - queue.append(string) - -# Interates over the collected strings and prints the entropies -for string in queue: - if args.lower != False: - string = string.lower() - elif args.upper != False: - string = string.upper() - - if args.squash != False: - string = string.replace(" ", "") - - a, b = calculateEntropy(string) - printEntropy(string, a, b, args.simple) diff --git a/entro.py/__init__.py b/entro.py/__init__.py new file mode 100644 index 0000000..ce472e3 --- /dev/null +++ b/entro.py/__init__.py @@ -0,0 +1 @@ +name = "entro.py" \ No newline at end of file diff --git a/entro.py/entro.py b/entro.py/entro.py new file mode 100644 index 0000000..3ac0589 --- /dev/null +++ b/entro.py/entro.py @@ -0,0 +1,38 @@ +import math + +# Calculates the entropy of a given string +# Returns the entropy and an alphabet with the calculated probabilities +def calculateEntropy(input_string): + alphabet, alphabet_size, entropy = {}, 0, 0 + + for char in input_string: + if char in alphabet: + alphabet[char] += 1 + else: + alphabet[char] = 1 + alphabet_size += 1 + + for char in alphabet: + alphabet[char] = alphabet[char] / alphabet_size + entropy -= alphabet[char] * math.log(alphabet[char], 2) + + return entropy, alphabet + + +# Calculates the entropy of a given string +# Returns only the entropy in bits as this is the minimal function +def calculateEntropyMin(input_string): + alphabet, alphabet_size, entropy = {}, 0, 0 + + for char in input_string: + if char in alphabet: + alphabet[char] += 1 + else: + alphabet[char] = 1 + alphabet_size += 1 + + for char in alphabet: + i = alphabet[char] / alphabet_size + entropy -= i * math.log(i, 2) + + return entropy diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..637fb3d --- /dev/null +++ b/setup.py @@ -0,0 +1,21 @@ +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +setuptools.setup( + name="entro.py-creyD", + version="1.0", + author="Conrad Großer", + author_email="grosserconrad@gmail.com", + description="Small Information Entropy Calculator", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/creyD/entro.py", + packages=setuptools.find_packages(), + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + ], +)