0 dní v řadě · 0 bodů

Tahák · Python · Užitečné knihovny

secrets, string a textwrap

Bezpečné tokeny a hesla, šablony textu bez děr a zalamování řádků včetně české typografie. Tři malé knihovny pro každodenní práci s textem.

Otevřít celou lekciVšechny taháky
python
import secrets, string, textwrap
from string import Template

secrets.token_hex(16)           # 32 hex znaků
secrets.token_urlsafe(32)       # do URL
secrets.choice(abeceda)         # bezpečný výběr
secrets.randbelow(100)          # 0 až 99
secrets.compare_digest(a, b)    # porovnání tokenů

string.ascii_letters            # a-zA-Z, bez diakritiky
string.digits                   # 0-9
string.punctuation              # interpunkce

Template("Ahoj $jmeno").substitute(jmeno="Eva")
Template("$a").safe_substitute()   # chybějící nechá

textwrap.wrap(t, width=40)      # seznam řádků
textwrap.fill(t, width=40)      # jeden text
textwrap.shorten(t, width=40, placeholder="…")
textwrap.dedent(t)              # odebere odsazení
textwrap.indent(t, "> ")        # přidá předponu