Skip to content

Instantly share code, notes, and snippets.

@JuniorPolegato
Created April 18, 2017 15:09
Show Gist options
  • Save JuniorPolegato/e953d3425c434bbd6910e7a55b7335e0 to your computer and use it in GitHub Desktop.
Save JuniorPolegato/e953d3425c434bbd6910e7a55b7335e0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Para substituir caracteres com acentos e outros por equivalente ascii
from unicodedata import normalize
# Caracteres para ocultar em ASCII (minúsculos) e o caracter oculto
OCULTAR = 'aeiou'
C = '*'
# Tentar traduzir todos os caracteres para ASCII, se não conseguir colocar '?'
def ascii_minusculas(texto):
t = map(lambda c: normalize('NFKD', c).encode('ascii', 'ignore').decode(),
texto.lower())
return ''.join(map(lambda c: c if c else '?', t))
# Oculta os caracteres em ASCII solicitados
def cript(texto):
texto_ascii_minusculas = ascii_minusculas(texto)
print(texto_ascii_minusculas, '<--- texto_ascii_minusculas') # Debug
cript_map = map(lambda c: c in OCULTAR, texto_ascii_minusculas)
return ''.join(map(lambda x: [x[1], C][x[0]], zip(cript_map, texto)))
# Se rodando como principal (não módulo importado), executa o código abaixo
if __name__ == '__main__':
# texto = input("Digite um texto: ")
texto = 'A esperança é a última que morre! Æ 1º vem o desespero...'
print("Trocando `%s´ por `%s´..." % (OCULTAR, C))
print(texto, '<--- texto')
print(cript(texto), '<--- cript')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment