mirror of
https://github.com/cedricbonhomme/Stegano.git
synced 2025-06-28 03:06:14 +02:00
More tests for the generators.
This commit is contained in:
parent
0da9d9882e
commit
7c9530aa9d
3 changed files with 51 additions and 28 deletions
|
@ -73,7 +73,8 @@ def mersenne() -> Iterator[int]:
|
|||
y = 2*y + 1
|
||||
|
||||
def eratosthenes() -> Iterator[int]:
|
||||
"""Generate the prime numbers with the sieve of Eratosthenes.
|
||||
"""https://oeis.org/A000040
|
||||
Generate the prime numbers with the sieve of Eratosthenes.
|
||||
"""
|
||||
d = {} # type: dict[int, int]
|
||||
for i in itertools.count(2):
|
||||
|
@ -85,8 +86,9 @@ def eratosthenes() -> Iterator[int]:
|
|||
d[i * i] = [i]
|
||||
yield i
|
||||
|
||||
def eratosthenes_composite() -> Iterator[int]:
|
||||
"""Generate the composite numbers with the sieve of Eratosthenes.
|
||||
def composite() -> Iterator[int]:
|
||||
"""https://oeis.org/A002808
|
||||
Generate the composite numbers using the sieve of Eratosthenes.
|
||||
"""
|
||||
p1 = 3
|
||||
for p2 in eratosthenes():
|
||||
|
|
|
@ -31,8 +31,7 @@ from typing import List, Iterator, Tuple, Union
|
|||
from functools import reduce
|
||||
|
||||
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.
|
||||
|
||||
>>> a2bits("Hello World!")
|
||||
'010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001'
|
||||
|
@ -40,8 +39,7 @@ def a2bits(chars: str) -> str:
|
|||
return bin(reduce(lambda x, y : (x<<8)+y, (ord(c) for c in chars), 1))[3:]
|
||||
|
||||
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.
|
||||
|
||||
>>> a2bits_list("Hello World!")
|
||||
['01001000',
|
||||
|
@ -62,21 +60,18 @@ def a2bits_list(chars: str) -> List[str]:
|
|||
return [bin(ord(x))[2:].rjust(8,"0") for x in chars]
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
|
||||
>>> list(n_at_a_time([1, 2, 3, 4, 5], 2, 'X'))
|
||||
|
@ -86,8 +81,7 @@ def n_at_a_time(items: List[int], n: int, fillvalue: str) \
|
|||
return itertools.zip_longest(*[it] * n, fillvalue=fillvalue)
|
||||
|
||||
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.
|
||||
"""
|
||||
# Use mode = "rb" to read binary file
|
||||
|
@ -96,8 +90,7 @@ def binary2base64(binary_file: str) -> str:
|
|||
return encoded_string.decode()
|
||||
|
||||
def base642binary(b64_fname: str) -> bytes:
|
||||
"""
|
||||
Convert a printable file to a binary file.
|
||||
"""Convert a printable string to a binary file.
|
||||
"""
|
||||
b64_fname += '==='
|
||||
return base64.b64decode(b64_fname)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue