mirror of
https://github.com/cedricbonhomme/Stegano.git
synced 2025-05-12 17:18:30 +02:00
Introduces some type hints.
This commit is contained in:
parent
3e923e49a8
commit
0da9d9882e
3 changed files with 25 additions and 29 deletions
|
@ -30,7 +30,7 @@ from PIL import Image
|
||||||
|
|
||||||
from stegano import tools
|
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
|
"""Hide a message (string) in an image with the
|
||||||
LSB (Least Significant Bit) technique.
|
LSB (Least Significant Bit) technique.
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -27,8 +27,9 @@ __license__ = "GPLv3"
|
||||||
|
|
||||||
import math
|
import math
|
||||||
import itertools
|
import itertools
|
||||||
|
from typing import Iterator
|
||||||
|
|
||||||
def identity():
|
def identity() -> Iterator[int]:
|
||||||
"""f(x) = x
|
"""f(x) = x
|
||||||
"""
|
"""
|
||||||
n = 0
|
n = 0
|
||||||
|
@ -36,7 +37,7 @@ def identity():
|
||||||
yield n
|
yield n
|
||||||
n += 1
|
n += 1
|
||||||
|
|
||||||
def Dead_Man_Walking():
|
def Dead_Man_Walking() -> Iterator[int]:
|
||||||
"""Dead Man Walking.
|
"""Dead Man Walking.
|
||||||
"""
|
"""
|
||||||
n = 0
|
n = 0
|
||||||
|
@ -44,7 +45,7 @@ def Dead_Man_Walking():
|
||||||
yield n + 7
|
yield n + 7
|
||||||
n += 2
|
n += 2
|
||||||
|
|
||||||
def triangular_numbers():
|
def triangular_numbers() -> Iterator[int]:
|
||||||
"""http://oeis.org/A000217
|
"""http://oeis.org/A000217
|
||||||
Triangular numbers: a(n) = C(n+1,2) = n(n+1)/2 = 0+1+2+...+n.
|
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
|
yield (n*(n+1))//2
|
||||||
n += 1
|
n += 1
|
||||||
|
|
||||||
def fermat():
|
def fermat() -> Iterator[int]:
|
||||||
"""https://oeis.org/A000215
|
"""https://oeis.org/A000215
|
||||||
Generate the n-th Fermat Number.
|
Generate the n-th Fermat Number.
|
||||||
"""
|
"""
|
||||||
|
@ -62,7 +63,7 @@ def fermat():
|
||||||
yield y
|
yield y
|
||||||
y = pow(y-1,2)+1
|
y = pow(y-1,2)+1
|
||||||
|
|
||||||
def mersenne():
|
def mersenne() -> Iterator[int]:
|
||||||
"""https://oeis.org/A001348
|
"""https://oeis.org/A001348
|
||||||
Generate 2^n - 1.
|
Generate 2^n - 1.
|
||||||
"""
|
"""
|
||||||
|
@ -71,10 +72,10 @@ def mersenne():
|
||||||
yield y
|
yield y
|
||||||
y = 2*y + 1
|
y = 2*y + 1
|
||||||
|
|
||||||
def eratosthenes():
|
def eratosthenes() -> Iterator[int]:
|
||||||
"""Generate the prime numbers with the sieve of Eratosthenes.
|
"""Generate the prime numbers with the sieve of Eratosthenes.
|
||||||
"""
|
"""
|
||||||
d = {}
|
d = {} # type: dict[int, int]
|
||||||
for i in itertools.count(2):
|
for i in itertools.count(2):
|
||||||
if i in d:
|
if i in d:
|
||||||
for j in d[i]:
|
for j in d[i]:
|
||||||
|
@ -84,7 +85,7 @@ def eratosthenes():
|
||||||
d[i * i] = [i]
|
d[i * i] = [i]
|
||||||
yield i
|
yield i
|
||||||
|
|
||||||
def eratosthenes_composite():
|
def eratosthenes_composite() -> Iterator[int]:
|
||||||
"""Generate the composite numbers with the sieve of Eratosthenes.
|
"""Generate the composite numbers with the sieve of Eratosthenes.
|
||||||
"""
|
"""
|
||||||
p1 = 3
|
p1 = 3
|
||||||
|
@ -93,7 +94,7 @@ def eratosthenes_composite():
|
||||||
yield n
|
yield n
|
||||||
p1 = p2
|
p1 = p2
|
||||||
|
|
||||||
def carmichael():
|
def carmichael() -> Iterator[int]:
|
||||||
"""https://oeis.org/A002997
|
"""https://oeis.org/A002997
|
||||||
Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime to n.
|
Composite numbers n such that a^(n-1) == 1 (mod n) for every a coprime to n.
|
||||||
"""
|
"""
|
||||||
|
@ -104,7 +105,7 @@ def carmichael():
|
||||||
else:
|
else:
|
||||||
yield m
|
yield m
|
||||||
|
|
||||||
def ackermann(m, n):
|
def ackermann(m: int, n: int) -> int:
|
||||||
"""Ackermann number.
|
"""Ackermann number.
|
||||||
"""
|
"""
|
||||||
if m == 0:
|
if m == 0:
|
||||||
|
@ -114,7 +115,7 @@ def ackermann(m, n):
|
||||||
else:
|
else:
|
||||||
return ackermann(m - 1, ackermann(m, n - 1))
|
return ackermann(m - 1, ackermann(m, n - 1))
|
||||||
|
|
||||||
def fibonacci():
|
def fibonacci() -> Iterator[int]:
|
||||||
"""https://oeis.org/A000045
|
"""https://oeis.org/A000045
|
||||||
A generator for Fibonacci numbers, goes to next number in series on each call.
|
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, ...
|
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
|
yield a
|
||||||
a, b = b, a + b
|
a, b = b, a + b
|
||||||
|
|
||||||
def syracuse(l=15):
|
def syracuse(l: int = 15) -> Iterator[int]:
|
||||||
"""Generate the sequence of Syracuse.
|
"""Generate the sequence of Syracuse.
|
||||||
"""
|
"""
|
||||||
y = l
|
y = l
|
||||||
|
@ -136,18 +137,11 @@ def syracuse(l=15):
|
||||||
else:
|
else:
|
||||||
y = 3*y + 1
|
y = 3*y + 1
|
||||||
|
|
||||||
def log_gen():
|
def log_gen() -> Iterator[int]:
|
||||||
"""Logarithmic generator.
|
"""Logarithmic generator.
|
||||||
"""
|
"""
|
||||||
y = 1
|
y = 1
|
||||||
while True:
|
while True:
|
||||||
adder = max(1, math.pow(10, int(math.log10(y))))
|
adder = max(1, math.pow(10, int(math.log10(y))))
|
||||||
yield int(y)
|
yield int(y)
|
||||||
y = y + adder
|
y = y + int(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
|
|
||||||
|
|
|
@ -27,9 +27,10 @@ __license__ = "GPLv3"
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import itertools
|
import itertools
|
||||||
|
from typing import List, Iterator, Tuple, Union
|
||||||
from functools import reduce
|
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.
|
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:]
|
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.
|
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]
|
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.
|
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)
|
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.
|
Set Least Significant Bit of a colour component.
|
||||||
"""
|
"""
|
||||||
return component & ~1 | int(bit)
|
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.
|
Returns an iterator which groups n items at a time.
|
||||||
Any final partial tuple will be padded with the fillvalue
|
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)
|
it = iter(items)
|
||||||
return itertools.zip_longest(*[it] * n, fillvalue=fillvalue)
|
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
|
Convert a binary file (OGG, executable, etc.) to a
|
||||||
printable string.
|
printable string.
|
||||||
|
@ -93,7 +95,7 @@ def binary2base64(binary_file):
|
||||||
encoded_string = base64.b64encode(bin_file.read())
|
encoded_string = base64.b64encode(bin_file.read())
|
||||||
return encoded_string.decode()
|
return encoded_string.decode()
|
||||||
|
|
||||||
def base642binary(b64_fname):
|
def base642binary(b64_fname: str) -> bytes:
|
||||||
"""
|
"""
|
||||||
Convert a printable file to a binary file.
|
Convert a printable file to a binary file.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Reference in a new issue