Changed indentation and wrote docstring for each function.

This commit is contained in:
2017-12-22 12:07:49 +01:00
parent 662c8678ee
commit 6bdf904a40

View File

@@ -3,7 +3,7 @@
# @Author: KevinMidboe
# @Date: 2017-11-01 15:57:23
# @Last Modified by: KevinMidboe
# @Last Modified time: 2017-12-22 11:16:09
# @Last Modified time: 2017-12-22 12:07:18
import re
import logging
@@ -24,6 +24,9 @@ SYMBOLS = {
__all__ = ('ColorizeFilter', )
class ColorizeFilter(logging.Filter):
"""
Class for setting specific colors to levels of severity for log output
"""
color_by_level = {
logging.DEBUG: 'yellow',
@@ -40,7 +43,8 @@ class ColorizeFilter(logging.Filter):
return True
def sanitize(string, ignore_characters=None, replace_characters=None):
"""Sanitize a string to strip special characters.
"""
Sanitize a string to strip special characters.
:param str string: the string to sanitize.
:param set ignore_characters: characters to ignore.
@@ -63,24 +67,31 @@ def sanitize(string, ignore_characters=None, replace_characters=None):
return string
def return_re_match(string, re_statement):
"""
Helper function for checking if a string contains a given regex statement
:param str string, str re_statement: string we want to check and regex string we
check for in string
:return: sanitized string of the match we found
:rtype: str
"""
if string is None:
return
m = re.search(re_statement, string)
# This is here for a edge case for piratebay torrent dates
if 'Y-day' in m.group():
return datetime.now().strftime('%m-%d %Y')
return sanitize(m.group(), '\xa0', ' ')
# Can maybe be moved away from this class
# returns a number that is either the value of multiple_pages
# or if it exceeds total_pages, return total_pages.
def pagesToCount(multiple, total):
if (multiple > total):
return total
return multiple
def representsInteger(str):
"""
Checks if a string only contains integers
:param str str: string we want to check only has integers
:return: if string only contains integers
:rtype: boolean
"""
try:
int(str)
return True
@@ -121,6 +132,13 @@ def deHumansize(s):
return int(num * prefix[letter])
def humansize(nbytes):
"""
Translates a size in bytes to a human readable size format
:param int nbytes: integer of size of torrent file
:return: size in bytes in a human readable format
:rtype: str
"""
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']
i = 0
while nbytes >= 1024 and i < len(suffixes)-1:
@@ -128,3 +146,4 @@ def humansize(nbytes):
i += 1
f = ('%.2f' % nbytes).rstrip('0').rstrip('.')
return '{} {}'.format(f, suffixes[i])