From 0da9d9882e8c6e14a32d916c96aea0a986d9bf57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bonhomme?= Date: Thu, 9 Mar 2017 08:11:12 +0100 Subject: [PATCH] Introduces some type hints. --- stegano/lsb/lsb.py | 2 +- stegano/lsbset/generators.py | 36 +++++++++++++++--------------------- stegano/tools.py | 16 +++++++++------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/stegano/lsb/lsb.py b/stegano/lsb/lsb.py index ac17d1f..a2bd745 100755 --- a/stegano/lsb/lsb.py +++ b/stegano/lsb/lsb.py @@ -30,7 +30,7 @@ from PIL import Image from stegano import tools -def hide(input_image_file, message, auto_convert_rgb=False): +def hide(input_image_file: str, message, auto_convert_rgb: bool = False): """Hide a message (string) in an image with the LSB (Least Significant Bit) technique. """ diff --git a/stegano/lsbset/generators.py b/stegano/lsbset/generators.py index 8c2d217..59093c1 100644 --- a/stegano/lsbset/generators.py +++ b/stegano/lsbset/generators.py @@ -27,8 +27,9 @@ __license__ = "GPLv3" import math import itertools +from typing import Iterator -def identity(): +def identity() -> Iterator[int]: """f(x) = x """ n = 0 @@ -36,7 +37,7 @@ def identity(): yield n n += 1 -def Dead_Man_Walking(): +def Dead_Man_Walking() -> Iterator[int]: """Dead Man Walking. """ n = 0 @@ -44,7 +45,7 @@ def Dead_Man_Walking(): yield n + 7 n += 2 -def triangular_numbers(): +def triangular_numbers() -> Iterator[int]: """http://oeis.org/A000217 Triangular numbers: a(n) = C(n+1,2) = n(n+1)/2 = 0+1+2+...+n. """ @@ -53,7 +54,7 @@ def triangular_numbers(): yield (n*(n+1))//2 n += 1 -def fermat(): +def fermat() -> Iterator[int]: """https://oeis.org/A000215 Generate the n-th Fermat Number. """ @@ -62,7 +63,7 @@ def fermat(): yield y y = pow(y-1,2)+1 -def mersenne(): +def mersenne() -> Iterator[int]: """https://oeis.org/A001348 Generate 2^n - 1. """ @@ -71,10 +72,10 @@ def mersenne(): yield y y = 2*y + 1 -def eratosthenes(): +def eratosthenes() -> Iterator[int]: """Generate the prime numbers with the sieve of Eratosthenes. """ - d = {} + d = {} # type: dict[int, int] for i in itertools.count(2): if i in d: for j in d[i]: @@ -84,7 +85,7 @@ def eratosthenes(): d[i * i] = [i] yield i -def eratosthenes_composite(): +def eratosthenes_composite() -> Iterator[int]: """Generate the composite numbers with the sieve of Eratosthenes. """ p1 = 3 @@ -93,7 +94,7 @@ def eratosthenes_composite(): yield n p1 = p2 -def carmichael(): +def carmichael() -> Iterator[int]: """https://oeis.org/A002997 Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime to n. """ @@ -104,7 +105,7 @@ def carmichael(): else: yield m -def ackermann(m, n): +def ackermann(m: int, n: int) -> int: """Ackermann number. """ if m == 0: @@ -114,7 +115,7 @@ def ackermann(m, n): else: return ackermann(m - 1, ackermann(m, n - 1)) -def fibonacci(): +def fibonacci() -> Iterator[int]: """https://oeis.org/A000045 A generator for Fibonacci numbers, goes to next number in series on each call. This generator start at 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, ... @@ -124,7 +125,7 @@ def fibonacci(): yield a a, b = b, a + b -def syracuse(l=15): +def syracuse(l: int = 15) -> Iterator[int]: """Generate the sequence of Syracuse. """ y = l @@ -136,18 +137,11 @@ def syracuse(l=15): else: y = 3*y + 1 -def log_gen(): +def log_gen() -> Iterator[int]: """Logarithmic generator. """ y = 1 while True: adder = max(1, math.pow(10, int(math.log10(y)))) yield int(y) - y = y + adder - -if __name__ == "__main__": - # Point of entry in execution mode. - f = fibonacci() - for x in range(13): - #print(next(f), end=' ') # 0 1 1 2 3 5 8 13 21 34 55 89 144 - pass + y = y + int(adder) diff --git a/stegano/tools.py b/stegano/tools.py index a17be95..b573fe7 100755 --- a/stegano/tools.py +++ b/stegano/tools.py @@ -27,9 +27,10 @@ __license__ = "GPLv3" import base64 import itertools +from typing import List, Iterator, Tuple, Union from functools import reduce -def a2bits(chars): +def a2bits(chars: str) -> str: """ Converts a string to its bits representation as a string of 0's and 1's. @@ -38,7 +39,7 @@ def a2bits(chars): """ return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:] -def a2bits_list(chars): +def a2bits_list(chars: str) -> List[str]: """ Convert a string to its bits representation as a list of 0's and 1's. @@ -60,19 +61,20 @@ def a2bits_list(chars): """ return [bin(ord(x))[2:].rjust(8,"0") for x in chars] -def bs(s): +def bs(s: int) -> str: """ Converts an int to its bits representation as a string of 0's and 1's. """ return str(s) if s<=1 else bs(s>>1) + str(s&1) -def setlsb(component, bit): +def setlsb(component: int, bit: str) -> int: """ Set Least Significant Bit of a colour component. """ return component & ~1 | int(bit) -def n_at_a_time(items, n, fillvalue): +def n_at_a_time(items: List[int], n: int, fillvalue: str) \ + -> Iterator[Tuple[Union[int, str]]]: """ Returns an iterator which groups n items at a time. Any final partial tuple will be padded with the fillvalue @@ -83,7 +85,7 @@ def n_at_a_time(items, n, fillvalue): it = iter(items) return itertools.zip_longest(*[it] * n, fillvalue=fillvalue) -def binary2base64(binary_file): +def binary2base64(binary_file: str) -> str: """ Convert a binary file (OGG, executable, etc.) to a printable string. @@ -93,7 +95,7 @@ def binary2base64(binary_file): encoded_string = base64.b64encode(bin_file.read()) return encoded_string.decode() -def base642binary(b64_fname): +def base642binary(b64_fname: str) -> bytes: """ Convert a printable file to a binary file. """