PEP8ing whitespace

This commit is contained in:
Christopher Teutsch 2019-06-21 22:16:16 +02:00
parent 1eec302ad8
commit add06ef20d
Signed by: iwonder
GPG Key ID: 0EE33D788D50130D
2 changed files with 18 additions and 1 deletions

View File

@ -66,6 +66,7 @@ vals_group.add_argument(
nargs='?' nargs='?'
) )
def _process_stdin(argv: str, res: utils.CurrencyResult) -> None: def _process_stdin(argv: str, res: utils.CurrencyResult) -> None:
argv = argv.split() argv = argv.split()
try: try:
@ -123,6 +124,7 @@ d | date: Print the date which the data is from.
except ValueError: except ValueError:
print("The currency specified does not exist.") print("The currency specified does not exist.")
def is_float(string: str) -> bool: def is_float(string: str) -> bool:
try: try:
float(string) float(string)
@ -130,9 +132,11 @@ def is_float(string: str) -> bool:
except ValueError: except ValueError:
return False return False
def _parse_date_from_args(date_str: str) -> DTDate: def _parse_date_from_args(date_str: str) -> DTDate:
return DTDateTime.strptime(date_str).date() return DTDateTime.strptime(date_str).date()
def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) -> float: def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) -> float:
if direction == DIRECTION_FROM_EUR: if direction == DIRECTION_FROM_EUR:
result = amt * rate.ask / 1+duty result = amt * rate.ask / 1+duty
@ -142,6 +146,7 @@ def calc_result(amt: float, rate: utils.Rate, direction: int, duty: float = 0) -
raise ValueError('direction must be DIRECTION_FROM_EUR or DIRECTION_TO_EUR') raise ValueError('direction must be DIRECTION_FROM_EUR or DIRECTION_TO_EUR')
return result return result
def fmt_and_calc(amt: float, cur: str, res: utils.CurrencyResult, direction: str) -> str: def fmt_and_calc(amt: float, cur: str, res: utils.CurrencyResult, direction: str) -> str:
cur = cur.upper() cur = cur.upper()
if cur in res.rates: if cur in res.rates:

View File

@ -19,12 +19,14 @@ Rate = namedtuple('Rate', ['abbr', 'full_name', 'ask', 'bid'])
CARD_MASTERCARD = ['0'] CARD_MASTERCARD = ['0']
CARD_VISA = ['1'] CARD_VISA = ['1']
class CurrencyResult():
class CurrencyResult:
def __init__(self): def __init__(self):
self.rates = list() self.rates = list()
self.card_type = str() self.card_type = str()
self.date = None self.date = None
def _parse_rate(text: str) -> float or None: def _parse_rate(text: str) -> float or None:
if re.match('Keine Kursdaten vorhanden', text): if re.match('Keine Kursdaten vorhanden', text):
_r = None _r = None
@ -37,17 +39,20 @@ def _parse_rate(text: str) -> float or None:
_r = None _r = None
return _r return _r
def _parse_card_type(text: str) -> str: def _parse_card_type(text: str) -> str:
# Method for validating metadata from the PDF against the request data # Method for validating metadata from the PDF against the request data
text = text.split(':')[1] text = text.split(':')[1]
text = text.strip('" ') text = text.strip('" ')
return text return text
def _parse_date(text: str) -> DTDate: def _parse_date(text: str) -> DTDate:
# Method for validating metadata from the PDF against the request data # Method for validating metadata from the PDF against the request data
text = text.split(': ')[1].rstrip() text = text.split(': ')[1].rstrip()
return DTDateTime.strptime(text, '%d.%m.%Y').date() return DTDateTime.strptime(text, '%d.%m.%Y').date()
def _array_remove_empty(obj: list) -> List[str]: def _array_remove_empty(obj: list) -> List[str]:
# just a macro for removing empty or empty-string array objects # just a macro for removing empty or empty-string array objects
try: try:
@ -57,6 +62,7 @@ def _array_remove_empty(obj: list) -> List[str]:
return obj return obj
return obj return obj
def _parse_line(line: str) -> Rate or None: def _parse_line(line: str) -> Rate or None:
arr = line.split(" ") # 3 spaces = minimum separation in PDF arr = line.split(" ") # 3 spaces = minimum separation in PDF
arr = _array_remove_empty(arr) arr = _array_remove_empty(arr)
@ -97,6 +103,7 @@ def get_results_from_text(text: str, currency: str = None) -> CurrencyResult:
result.rates = rates result.rates = rates
return result return result
def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> CurrencyResult: def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> CurrencyResult:
print('Parsing data... ', end='') print('Parsing data... ', end='')
reader = PyPDF3.PdfFileReader(buf) reader = PyPDF3.PdfFileReader(buf)
@ -105,6 +112,8 @@ def get_results_from_pdf(buf: BinaryIO or str, currency: str = None) -> Currency
text += reader.getPage(num).extractText() text += reader.getPage(num).extractText()
print('Done.') print('Done.')
return get_results_from_text(text, currency=currency) return get_results_from_text(text, currency=currency)
def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # pylint: disable=dangerous-default-value def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # pylint: disable=dangerous-default-value
# pylint: disable=no-member # mechanize.Browser has some lazy-loading methods that pylint doesn't see # pylint: disable=no-member # mechanize.Browser has some lazy-loading methods that pylint doesn't see
print('Downloading rates for ' + date.strftime('%Y-%m-%d') + '... ', end='') print('Downloading rates for ' + date.strftime('%Y-%m-%d') + '... ', end='')
@ -134,6 +143,8 @@ def get_fileio(date: DTDate, card_type: List[str] = CARD_VISA) -> BinaryIO: # py
print(' Done.') print(' Done.')
# Returns an open file-like object with the PDF as contents # Returns an open file-like object with the PDF as contents
return open(rp[0], 'rb') return open(rp[0], 'rb')
def get_date() -> DTDate: def get_date() -> DTDate:
# For Sunday and Monday, use Friday's data; Saturday and Sunday are completely null # For Sunday and Monday, use Friday's data; Saturday and Sunday are completely null
if DTDate.today().weekday() in [6, 0]: if DTDate.today().weekday() in [6, 0]:
@ -143,6 +154,7 @@ def get_date() -> DTDate:
date = DTDate.today() - DTTimeDelta(1) date = DTDate.today() - DTTimeDelta(1)
return date return date
def mk_filename(date: DTDate, card_type: List[str]) -> str: def mk_filename(date: DTDate, card_type: List[str]) -> str:
# List[str] is used because I don't want to make a class for just this # List[str] is used because I don't want to make a class for just this
if card_type == CARD_MASTERCARD: if card_type == CARD_MASTERCARD: