mirror of
https://github.com/hoshikawa2/python_redaction.git
synced 2026-03-03 16:19:37 +00:00
new functionality: adding the capacity for treat Numeric Values
This commit is contained in:
@@ -38,7 +38,7 @@ We can then, from a list of Strings (Logs for example), have a poorly formatted
|
||||
|
||||
This code has more logical complexity:
|
||||
|
||||

|
||||

|
||||
|
||||
It works with flags:
|
||||
|
||||
@@ -46,6 +46,8 @@ It works with flags:
|
||||
- **flag_attribute**: It marks that an attribute was found
|
||||
- **flag_dois_pontos**: It marks that the algorithm is prepared to find the content of an attribute (the content is after the :)
|
||||
- **flag_colchetes**: It marks that an attribute items values will be analyzed
|
||||
- **flag_descobre_tipo**: It marks for find the type of an attribute (String or Numeric)
|
||||
- **flag_string**: If True, the attribute will be a String; If False, it's a Numeric attribute
|
||||
|
||||
>**Note**: The attribute to be redacted NEED to be a String. If the attribute is a set of attributes, the algorithm will ignore all the set. In the example code **redact.py**, the attribute **items** is in the list but it will not redact anything.
|
||||
|
||||
@@ -61,7 +63,7 @@ What the algorithm performs:
|
||||
|
||||
: need to respect a JSON association
|
||||
|
||||
'value' is the value between '', yes the value needs to be a String
|
||||
'value' is the value between '' for a String, or a Numeric value (Integer or Double with a . and no '')
|
||||
|
||||
|
||||
So you can use the class **Redaction.py** in you code, like this:
|
||||
|
||||
37
Redaction.py
37
Redaction.py
@@ -33,6 +33,8 @@ class Redaction():
|
||||
# flag_vezes = 0
|
||||
flag_dois_pontos = False
|
||||
flag_colchetes = False
|
||||
flag_string = True
|
||||
flag_descobre_tipo = False
|
||||
i = 0
|
||||
z = pattern
|
||||
str_acc = ""
|
||||
@@ -45,7 +47,9 @@ class Redaction():
|
||||
print("except")
|
||||
if (message[i] == ":" and not flag_aspas and flag_attribute):
|
||||
flag_dois_pontos = True
|
||||
if (flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos):
|
||||
flag_descobre_tipo = True
|
||||
if ((flag_aspas and message[i] != "'" and message[i] != "\"" and flag_attribute and flag_dois_pontos)
|
||||
or (message[i] in "0123456789." and flag_attribute and flag_dois_pontos and not flag_string)):
|
||||
str_acc = str_acc + message[i]
|
||||
message = message[0:i] + "*" + message[i + 1:len(message) + i]
|
||||
if (message[i] == "{" and flag_dois_pontos and not flag_aspas):
|
||||
@@ -53,25 +57,48 @@ class Redaction():
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
flag_colchetes = False
|
||||
flag_descobre_tipo = False
|
||||
flag_string = True
|
||||
if ((message[i] == "}" or message[i] == "]") and not flag_aspas):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
flag_colchetes = False
|
||||
flag_descobre_tipo = False
|
||||
flag_string = True
|
||||
str_acc = ""
|
||||
if (flag_dois_pontos and not flag_aspas and message[i] == "["):
|
||||
flag_colchetes = True
|
||||
if (message[i] == "," and not flag_aspas and not flag_colchetes):
|
||||
if ((message[i] == "}" or message[i] == "]") and not flag_aspas and not flag_string):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
flag_colchetes = False
|
||||
flag_descobre_tipo = False
|
||||
flag_string = True
|
||||
str_acc = ""
|
||||
if (flag_dois_pontos and not flag_aspas and message[i] == "["):
|
||||
flag_colchetes = True
|
||||
if (message[i] == "," and not flag_aspas and not flag_colchetes and not flag_string):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_aspas = False
|
||||
flag_colchetes = False
|
||||
flag_descobre_tipo = False
|
||||
flag_string = True
|
||||
str_acc = ""
|
||||
if ((message[i] == "'" or message[i] == "\"")):
|
||||
flag_aspas = not flag_aspas
|
||||
if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0 and not flag_colchetes):
|
||||
if (flag_descobre_tipo):
|
||||
flag_string = True
|
||||
flag_descobre_tipo = False
|
||||
if (message[i] in "01234567890." and flag_descobre_tipo):
|
||||
flag_string = False
|
||||
flag_descobre_tipo = False
|
||||
str_acc = str_acc + message[i]
|
||||
message = message[0:i] + "*" + message[i + 1:len(message) + i]
|
||||
if (flag_aspas == False and flag_attribute == True and flag_dois_pontos and len(str_acc) > 0 and not flag_colchetes and flag_string):
|
||||
flag_attribute = False
|
||||
flag_dois_pontos = False
|
||||
flag_descobre_tipo = False
|
||||
str_acc = ""
|
||||
i = i + 1
|
||||
return message
|
||||
|
||||
BIN
images/repl_value3.png
Normal file
BIN
images/repl_value3.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 192 KiB |
@@ -18,12 +18,14 @@ ATTRIBUTE_PATTERNS = [
|
||||
"original",
|
||||
"type",
|
||||
"solicitacaoPagador",
|
||||
"expiracao", #Não funciona para atributos numéricos, apenas para String
|
||||
"expiracao",
|
||||
"chave",
|
||||
"description",
|
||||
"items",
|
||||
"example",
|
||||
"required"
|
||||
"required",
|
||||
"x-scope",
|
||||
"maxLength"
|
||||
]
|
||||
|
||||
Messages = [
|
||||
@@ -37,7 +39,7 @@ Messages = [
|
||||
"User's date of birth: 04/29/1990",
|
||||
"User's IP address: 192.168.1.1",
|
||||
"User's API key: a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6",
|
||||
'{ "nome": "João Silva", "cpf": "123.456.789-00", "teste": "Teste Valor", "valor": 200.00, "original": "Original Valor", "type": "Tipo Valor", "solicitacaoPagador": "Solicitação Pagador", "expiracao": "2021-12-31", "chave": "Chave Valor", "description": "Descrição Valor", "items": ["Item1", "Item2"], "example": "Exemplo Valor"}'
|
||||
'{ "nome": "João Silva", "cpf": "123.456.789-00", "maxLength": [1.0, 2.0, 3.0], "teste": "Teste Valor", "valor": 200.00, "original": "Original Valor", "type": "Tipo Valor", "solicitacaoPagador": "Solicitação Pagador", "expiracao": "2021-12-31", "chave": "Chave Valor", "description": "Descrição Valor", "items": ["Item1", "Item2"], "example": "Exemplo Valor"}'
|
||||
]
|
||||
|
||||
redaction = Redaction.Redaction()
|
||||
|
||||
Reference in New Issue
Block a user