portugython 0.4.0__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {portugython-0.4.0 → portugython-0.5.0}/CHANGELOG.md +80 -42
- {portugython-0.4.0 → portugython-0.5.0}/PKG-INFO +96 -100
- {portugython-0.4.0 → portugython-0.5.0}/README.md +94 -98
- {portugython-0.4.0 → portugython-0.5.0}/portugython/__init__.py +5 -4
- portugython-0.5.0/portugython/_codec.py +99 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/_importador.py +18 -16
- portugython-0.5.0/portugython/chaves.py +31 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/transpilador.py +22 -22
- portugython-0.5.0/portugython-codec.pth +1 -0
- {portugython-0.4.0 → portugython-0.5.0}/pyproject.toml +5 -2
- portugython-0.5.0/tests/exemplo_codec.ptpy +22 -0
- portugython-0.5.0/tests/test_codec.py +245 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/test_portugython.py +1 -1
- {portugython-0.4.0 → portugython-0.5.0}/.gitignore +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/LICENSE +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/abstrato.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/aleatorio.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/argumentos.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/assincronico.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/bisecao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/calendario.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/caminho.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/classe_dados.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/codificacao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/colecoes.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/compressao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/configuracao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/contexto.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/copia.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/csv_pt.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/depuracao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/enumeracao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/estatistica.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/estrutura.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/expressao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/formatacao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/funcional.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/hash_pt.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/identificador.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/iteradores.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/json.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/matematica.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/py.typed +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/rede.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/registro.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/serializacao.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/sistema.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/soquete.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/subprocesso.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/tempo.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/threads.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/portugython/tipagem.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/__init__.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/ola_mundo.ptpy +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/test_importador.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/test_submodulos.py +0 -0
- {portugython-0.4.0 → portugython-0.5.0}/tests/test_transpilador.py +0 -0
|
@@ -1,21 +1,58 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
## 0.
|
|
3
|
+
## 0.5.0 (2026-06-26)
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
translates Python keywords to Portuguese, enabling fully Portuguese Python syntax.
|
|
5
|
+
Adiciona **codec de fonte `portugues`**: a forma mais natural de escrever Python em português.
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
Adicione `# -*- coding: portugues -*-` ao topo de qualquer arquivo `.py` e escreva
|
|
8
|
+
Python com palavras-chave em português diretamente. Não é necessária nenhuma ferramenta
|
|
9
|
+
extra além do Python padrão.
|
|
9
10
|
|
|
10
|
-
|
|
11
|
-
- `traduzir_para_portugues(codigo)` - inverse translation
|
|
12
|
-
- `executar_texto(codigo)` - translate and execute a Portuguese Python string
|
|
13
|
-
- `executar_arquivo(caminho)` - translate and execute a `.ptpy` file
|
|
14
|
-
- `mostrar_traducao(codigo)` - print the translated Python source
|
|
15
|
-
- `MAPA_PALAVRAS_CHAVE` - full keyword translation table
|
|
16
|
-
- `PALAVRAS_RESERVADAS` - frozenset of reserved Portuguese keywords
|
|
11
|
+
Exemplo completo:
|
|
17
12
|
|
|
18
|
-
|
|
13
|
+
```python
|
|
14
|
+
# -*- coding: portugues -*-
|
|
15
|
+
de chaves importe *
|
|
16
|
+
|
|
17
|
+
defina fatorial(n):
|
|
18
|
+
se n <= 1:
|
|
19
|
+
retorne 1
|
|
20
|
+
senao:
|
|
21
|
+
retorne n * fatorial(n - 1)
|
|
22
|
+
|
|
23
|
+
para i em intervalo(1, 6):
|
|
24
|
+
escreva(f"{i}! = {fatorial(i)}")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Novos módulos:
|
|
28
|
+
|
|
29
|
+
- `portugython._codec`: codec `portugues` que traduz palavras-chave antes do parse;
|
|
30
|
+
registrado automaticamente via `portugython-codec.pth` ao instalar.
|
|
31
|
+
- `portugython.chaves`: re-exporta todos os identificadores em português do
|
|
32
|
+
portugython para uso com `from chaves import *`.
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
Corrige acentuação em toda a documentação (README, CHANGELOG, docstrings).
|
|
37
|
+
Sem alterações na API ou no código.
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
Adiciona módulo `transpilador`: transpilador de código-fonte baseado no tokenizador do
|
|
42
|
+
Python que traduz palavras-chave para português, permitindo sintaxe Python completamente
|
|
43
|
+
em português.
|
|
44
|
+
|
|
45
|
+
Novo módulo `portugython.transpilador`:
|
|
46
|
+
|
|
47
|
+
- `traduzir(codigo)` - traduz código Python em português para Python padrão
|
|
48
|
+
- `traduzir_para_portugues(codigo)` - tradução inversa
|
|
49
|
+
- `executar_texto(codigo)` - traduz e executa uma string de código em português
|
|
50
|
+
- `executar_arquivo(caminho)` - traduz e executa um arquivo `.ptpy`
|
|
51
|
+
- `mostrar_traducao(codigo)` - imprime o código Python equivalente
|
|
52
|
+
- `MAPA_PALAVRAS_CHAVE` - tabela completa de tradução de palavras-chave
|
|
53
|
+
- `PALAVRAS_RESERVADAS` - frozenset de palavras-chave reservadas em português
|
|
54
|
+
|
|
55
|
+
Traduções de palavras-chave (34 palavras):
|
|
19
56
|
|
|
20
57
|
se -> if senaose -> elif senao -> else
|
|
21
58
|
para -> for enquanto -> while parar -> break
|
|
@@ -32,15 +69,15 @@ Keyword translations (34 keywords):
|
|
|
32
69
|
assincronico -> async aguardar -> await
|
|
33
70
|
Verdadeiro -> True Falso -> False Nenhum -> None
|
|
34
71
|
|
|
35
|
-
|
|
72
|
+
Novo ponto de entrada CLI: `ptpy` executa arquivos `.ptpy` diretamente:
|
|
36
73
|
|
|
37
74
|
ptpy meu_programa.ptpy
|
|
38
75
|
ptpy -c 'para i em intervalo(3): escreva(i)'
|
|
39
|
-
ptpy --mostrar meu_programa.ptpy #
|
|
76
|
+
ptpy --mostrar meu_programa.ptpy # exibe o Python equivalente
|
|
40
77
|
|
|
41
|
-
|
|
78
|
+
Hook de importação estendido para carregar arquivos `.ptpy` pelo sistema de importação normal.
|
|
42
79
|
|
|
43
|
-
|
|
80
|
+
Exemplo de programa `.ptpy`:
|
|
44
81
|
|
|
45
82
|
```python
|
|
46
83
|
defina fatorial(n):
|
|
@@ -53,11 +90,12 @@ para i em intervalo(1, 6):
|
|
|
53
90
|
escreva(f"{i}! = {fatorial(i)}")
|
|
54
91
|
```
|
|
55
92
|
|
|
93
|
+
## 0.3.0 (2026-06-26)
|
|
56
94
|
|
|
95
|
+
Adiciona hook de importação para que todos os módulos em português funcionem como
|
|
96
|
+
importações de nível superior.
|
|
57
97
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
After `import portugython`, the full stdlib-level syntax works:
|
|
98
|
+
Após `import portugython`, a sintaxe completa em nível de stdlib funciona:
|
|
61
99
|
|
|
62
100
|
import aleatorio
|
|
63
101
|
from matematica import raiz_quadrada, PI
|
|
@@ -67,25 +105,26 @@ After `import portugython`, the full stdlib-level syntax works:
|
|
|
67
105
|
from dados import serializar, deserializar # json
|
|
68
106
|
from subprocesso import executar
|
|
69
107
|
from caminho import Caminho
|
|
70
|
-
# ...
|
|
108
|
+
# ... todos os 36 módulos
|
|
71
109
|
|
|
72
|
-
|
|
110
|
+
Novos exports:
|
|
73
111
|
- `portugython.instalar()` - instala o gancho (chamado automaticamente)
|
|
74
112
|
- `portugython.desinstalar()` - remove o gancho
|
|
75
113
|
- `portugython.listar_modulos()` - retorna o mapa de nomes
|
|
76
114
|
|
|
115
|
+
## 0.2.0 (2026-06-26)
|
|
77
116
|
|
|
117
|
+
Cobertura completa da stdlib. 36 submódulos no total, todos importáveis via
|
|
118
|
+
`from portugython import <modulo>`:
|
|
78
119
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
**New in 0.2.0:**
|
|
120
|
+
**Novos em 0.2.0:**
|
|
82
121
|
- `caminho` (pathlib): Caminho com existe, eh_arquivo, listar, ler_texto, escrever_texto, etc.
|
|
83
122
|
- `json`: serializar, deserializar, serializar_arquivo, deserializar_arquivo
|
|
84
123
|
- `estatistica` (statistics): media, mediana, moda, desvio_padrao, covariancia, correlacao, quantis
|
|
85
124
|
- `enumeracao` (enum): Enumeracao, EnumeracaoInteira, EnumeracaoBandeira, automatico, unico
|
|
86
125
|
- `classe_dados` (dataclasses): classe_dados, campo, para_dicionario, para_tupla, substituir
|
|
87
126
|
- `contexto` (contextlib): gerenciador, suprimir, fechar_ao_sair, pilha_saida
|
|
88
|
-
- `tipagem` (typing): re-exporta todos os tipos com nomes em
|
|
127
|
+
- `tipagem` (typing): re-exporta todos os tipos com nomes em português
|
|
89
128
|
- `csv_pt` (csv): ler_csv, escrever_csv, LeitorCSV, EscritorCSV, LeitorDicionario
|
|
90
129
|
- `registro` (logging): obter_registrador, configurar_registro, depurar, informar, avisar, erro
|
|
91
130
|
- `argumentos` (argparse): AnalisadorArgumentos com adicionar_argumento, analisar
|
|
@@ -108,14 +147,13 @@ Full stdlib coverage. 36 submodules total, all importable via `from portugython
|
|
|
108
147
|
- `estrutura` (struct): empacotar, desempacotar, calcular_tamanho
|
|
109
148
|
- `rede` (urllib + http): baixar_url, abrir_url, analisar_url, codificar_url, codificar_parametros
|
|
110
149
|
|
|
111
|
-
|
|
112
|
-
-
|
|
113
|
-
- 49
|
|
114
|
-
|
|
150
|
+
Também em 0.2.0:
|
|
151
|
+
- Correção: alias de builtin `identificador` renomeado para `obter_id` para evitar shadowing do submódulo uuid
|
|
152
|
+
- 49 testes cobrindo todos os módulos
|
|
115
153
|
|
|
116
154
|
## 0.1.0 (2026-06-26)
|
|
117
155
|
|
|
118
|
-
-
|
|
156
|
+
- Adiciona conjunto completo de submódulos da stdlib com traduções em português:
|
|
119
157
|
- `aleatorio` (random): escolha, embaralhe, amostra, gauss, normal, etc.
|
|
120
158
|
- `matematica` (math): raiz_quadrada, seno, cosseno, fatorial, mdc, mmc, etc.
|
|
121
159
|
- `sistema` (os + sys): diretorio_atual, listar_diretorio, existe, sair, etc.
|
|
@@ -124,21 +162,21 @@ Also in 0.2.0:
|
|
|
124
162
|
- `iteradores` (itertools): encadeie, combine, permute, acumule, agrupe, etc.
|
|
125
163
|
- `funcional` (functools): reduzido, parcial, cache, compor, negado, etc.
|
|
126
164
|
- `expressao` (re): compilar, buscar, substituir, encontrar_todos, Padrao, etc.
|
|
127
|
-
-
|
|
128
|
-
-
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
132
|
-
- 30
|
|
165
|
+
- Reescrita do `__init__.py` com nomes em português limpos (remove sufixos `_pt`)
|
|
166
|
+
- Adiciona classe `Conjunto` com métodos em português
|
|
167
|
+
- Expande `Texto`, `Lista`, `Dicionario` com mais métodos
|
|
168
|
+
- Adiciona mais tipos de exceção: ErroArquivo, ErroPermissao, ErroRecursao, ErroPausa
|
|
169
|
+
- Versão mínima do Python elevada para >=3.10
|
|
170
|
+
- 30 testes cobrindo todos os módulos
|
|
133
171
|
|
|
134
172
|
## 0.0.15 (2026-06-26)
|
|
135
173
|
|
|
136
|
-
-
|
|
137
|
-
-
|
|
138
|
-
-
|
|
139
|
-
-
|
|
140
|
-
-
|
|
174
|
+
- Adiciona traduções em português dos builtins e funções principais do Python
|
|
175
|
+
- Introduz classes Lista, Dicionario, Texto com métodos em português
|
|
176
|
+
- Adiciona funções auxiliares: se(), para_cada(), filtro()
|
|
177
|
+
- Adiciona classes de exceção em português
|
|
178
|
+
- Expande a suíte de testes
|
|
141
179
|
|
|
142
180
|
## 0.0.14 (2022-11-28)
|
|
143
181
|
|
|
144
|
-
-
|
|
182
|
+
- Última versão anterior no PyPI.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: portugython
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 0.5.0
|
|
4
|
+
Summary: Escreva Python em Português: codec de sintaxe, módulos e funções traduzidos
|
|
5
5
|
Project-URL: Homepage, https://github.com/spacemany2k38/portugython
|
|
6
6
|
Project-URL: Bug Tracker, https://github.com/spacemany2k38/portugython/issues
|
|
7
7
|
Author: Victor Kolis
|
|
@@ -31,22 +31,74 @@ Description-Content-Type: text/markdown
|
|
|
31
31
|
[](https://pypi.org/project/portugython/)
|
|
32
32
|
[](https://opensource.org/licenses/MIT)
|
|
33
33
|
|
|
34
|
-
Escreva Python em
|
|
34
|
+
Escreva Python em Português. Portugython traduz funções, tipos e módulos da biblioteca padrão do Python para nomes em português, e inclui um **codec de sintaxe** que permite escrever palavras-chave (`se`, `para`, `defina`, etc.) nativamente em qualquer arquivo `.py`.
|
|
35
35
|
|
|
36
|
-
##
|
|
36
|
+
## Instalação
|
|
37
37
|
|
|
38
38
|
```bash
|
|
39
39
|
pip install portugython
|
|
40
40
|
```
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
O codec é registrado automaticamente ao instalar. Basta adicionar `# -*- coding: portugues -*-` ao topo do arquivo.
|
|
43
|
+
|
|
44
|
+
## Uso: Python em Português nativo
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
# -*- coding: portugues -*-
|
|
48
|
+
de chaves importe *
|
|
49
|
+
|
|
50
|
+
defina fatorial(n):
|
|
51
|
+
se n <= 1:
|
|
52
|
+
retorne 1
|
|
53
|
+
senao:
|
|
54
|
+
retorne n * fatorial(n - 1)
|
|
55
|
+
|
|
56
|
+
defina verificar_primo(n):
|
|
57
|
+
se n < 2:
|
|
58
|
+
retorne Falso
|
|
59
|
+
para i em intervalo(2, n):
|
|
60
|
+
se n % i == 0:
|
|
61
|
+
retorne Falso
|
|
62
|
+
retorne Verdadeiro
|
|
63
|
+
|
|
64
|
+
primos = [x para x em intervalo(2, 20) se verificar_primo(x)]
|
|
65
|
+
escreva("Primos:", primos)
|
|
66
|
+
|
|
67
|
+
para n em intervalo(1, 6):
|
|
68
|
+
escreva(f"{n}! = {fatorial(n)}")
|
|
69
|
+
|
|
70
|
+
tente:
|
|
71
|
+
resultado = 10 / 0
|
|
72
|
+
exceto ZeroDivisionError como err:
|
|
73
|
+
escreva("Erro capturado:", err)
|
|
74
|
+
finalmente:
|
|
75
|
+
escreva("Pronto!")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Salve como `programa.py` e execute com `python programa.py`. Nenhuma ferramenta extra necessária.
|
|
79
|
+
|
|
80
|
+
> **Nota sobre palavras ambíguas**: `e`, `ou`, `de`, `com`, `em` são palavras-chave
|
|
81
|
+
> reservadas em arquivos com `# -*- coding: portugues -*-`. Evite usá-las como
|
|
82
|
+
> nomes de variáveis (use `err` em vez de `e` para exceções, por exemplo).
|
|
83
|
+
|
|
84
|
+
## Como funciona
|
|
85
|
+
|
|
86
|
+
O codec `portugues` é registrado automaticamente via `portugython-codec.pth` ao instalar
|
|
87
|
+
o pacote. Quando Python encontra `# -*- coding: portugues -*-`, chama o codec para
|
|
88
|
+
decodificar o arquivo antes de parsear, traduzindo palavras-chave em português para
|
|
89
|
+
Python padrão de forma transparente.
|
|
90
|
+
|
|
91
|
+
O módulo `chaves` exporta todos os aliases de funções em português (`escreva`, `leia`,
|
|
92
|
+
`intervalo`, `tamanho`, `Lista`, `Dicionario`, etc.) para uso direto via `from chaves import *`.
|
|
93
|
+
|
|
94
|
+
## Uso básico sem codec
|
|
43
95
|
|
|
44
96
|
```python
|
|
45
97
|
from portugython import *
|
|
46
98
|
|
|
47
|
-
escreva("
|
|
48
|
-
nome = leia("Qual
|
|
49
|
-
escreva("
|
|
99
|
+
escreva("Olá, Mundo!")
|
|
100
|
+
nome = leia("Qual é seu nome? ")
|
|
101
|
+
escreva("Olá,", nome)
|
|
50
102
|
|
|
51
103
|
numeros = Lista([1, 2, 3, 4, 5])
|
|
52
104
|
numeros.adicione(6)
|
|
@@ -60,9 +112,9 @@ resultado = se(tamanho(pares) > 0, "tem pares", "sem pares")
|
|
|
60
112
|
escreva(resultado)
|
|
61
113
|
```
|
|
62
114
|
|
|
63
|
-
##
|
|
115
|
+
## Módulos da biblioteca padrão
|
|
64
116
|
|
|
65
|
-
Cada
|
|
117
|
+
Cada módulo da stdlib tem uma versão em português que pode ser importada separadamente.
|
|
66
118
|
|
|
67
119
|
### aleatorio (random)
|
|
68
120
|
|
|
@@ -159,7 +211,7 @@ print(p.x, p.y)
|
|
|
159
211
|
```python
|
|
160
212
|
from portugython import iteradores
|
|
161
213
|
|
|
162
|
-
#
|
|
214
|
+
# Combinatória
|
|
163
215
|
for combo in iteradores.combine([1, 2, 3, 4], 2):
|
|
164
216
|
print(combo)
|
|
165
217
|
|
|
@@ -167,7 +219,7 @@ for combo in iteradores.combine([1, 2, 3, 4], 2):
|
|
|
167
219
|
for par in iteradores.produto("AB", "12"):
|
|
168
220
|
print(par)
|
|
169
221
|
|
|
170
|
-
#
|
|
222
|
+
# Acumulação
|
|
171
223
|
totais = list(iteradores.acumule([1, 2, 3, 4, 5]))
|
|
172
224
|
|
|
173
225
|
# Encadeamento
|
|
@@ -179,21 +231,21 @@ unido = list(iteradores.encadeie([1, 2], [3, 4], [5]))
|
|
|
179
231
|
```python
|
|
180
232
|
from portugython import funcional
|
|
181
233
|
|
|
182
|
-
#
|
|
234
|
+
# Redução
|
|
183
235
|
total = funcional.reduzido(lambda a, b: a + b, range(1, 11))
|
|
184
236
|
|
|
185
|
-
#
|
|
237
|
+
# Função parcial
|
|
186
238
|
multiplicar_por_3 = funcional.parcial(lambda a, b: a * b, 3)
|
|
187
239
|
print(multiplicar_por_3(7))
|
|
188
240
|
|
|
189
|
-
#
|
|
241
|
+
# Memoização
|
|
190
242
|
@funcional.cache
|
|
191
243
|
def fibonacci(n):
|
|
192
244
|
if n < 2:
|
|
193
245
|
return n
|
|
194
246
|
return fibonacci(n - 1) + fibonacci(n - 2)
|
|
195
247
|
|
|
196
|
-
#
|
|
248
|
+
# Composição
|
|
197
249
|
dobrar = lambda x: x * 2
|
|
198
250
|
somar_um = lambda x: x + 1
|
|
199
251
|
dobrar_e_somar = funcional.compor(somar_um, dobrar)
|
|
@@ -213,26 +265,26 @@ if m:
|
|
|
213
265
|
# Todos os resultados
|
|
214
266
|
numeros = expressao.encontrar_todos(r"\d+", "1 mais 2 igual 3")
|
|
215
267
|
|
|
216
|
-
#
|
|
268
|
+
# Substituição
|
|
217
269
|
limpo = expressao.substituir(r"\s+", " ", "texto com espacos")
|
|
218
270
|
|
|
219
|
-
#
|
|
271
|
+
# Padrão reutilizável
|
|
220
272
|
email = expressao.Padrao(r"[\w.+-]+@[\w-]+\.[a-z]{2,}")
|
|
221
273
|
if email.encontrar("contato@example.com"):
|
|
222
|
-
print("
|
|
274
|
+
print("E-mail válido")
|
|
223
275
|
```
|
|
224
276
|
|
|
225
|
-
##
|
|
277
|
+
## Referência de builtins
|
|
226
278
|
|
|
227
|
-
### Entrada /
|
|
228
|
-
|
|
|
279
|
+
### Entrada / Saída
|
|
280
|
+
| Português | Python |
|
|
229
281
|
|-----------|--------|
|
|
230
282
|
| `escreva` | `print` |
|
|
231
283
|
| `leia` | `input` |
|
|
232
284
|
| `abra` | `open` |
|
|
233
285
|
|
|
234
286
|
### Tipos
|
|
235
|
-
|
|
|
287
|
+
| Português | Python |
|
|
236
288
|
|-----------|--------|
|
|
237
289
|
| `inteiro` | `int` |
|
|
238
290
|
| `texto` | `str` |
|
|
@@ -245,14 +297,14 @@ if email.encontrar("contato@example.com"):
|
|
|
245
297
|
| `intervalo` | `range` |
|
|
246
298
|
|
|
247
299
|
### Constantes
|
|
248
|
-
|
|
|
300
|
+
| Português | Python |
|
|
249
301
|
|-----------|--------|
|
|
250
302
|
| `Verdadeiro` | `True` |
|
|
251
303
|
| `Falso` | `False` |
|
|
252
304
|
| `Nenhum` | `None` |
|
|
253
305
|
|
|
254
|
-
###
|
|
255
|
-
|
|
|
306
|
+
### Funções de sequência
|
|
307
|
+
| Português | Python |
|
|
256
308
|
|-----------|--------|
|
|
257
309
|
| `tamanho` | `len` |
|
|
258
310
|
| `soma` | `sum` |
|
|
@@ -266,99 +318,43 @@ if email.encontrar("contato@example.com"):
|
|
|
266
318
|
| `mapeado` | `map` |
|
|
267
319
|
| `filtrado` | `filter` |
|
|
268
320
|
|
|
269
|
-
### Operadores
|
|
270
|
-
|
|
|
321
|
+
### Operadores lógicos
|
|
322
|
+
| Português | Python |
|
|
271
323
|
|-----------|--------|
|
|
272
324
|
| `nao(x)` | `not x` |
|
|
273
325
|
| `e(a, b)` | `a and b` |
|
|
274
326
|
| `ou(a, b)` | `a or b` |
|
|
275
327
|
|
|
276
|
-
###
|
|
277
|
-
|
|
|
328
|
+
### Funções utilitárias
|
|
329
|
+
| Português | Python |
|
|
278
330
|
|-----------|--------|
|
|
279
331
|
| `se(c, a, b)` | `a if c else b` |
|
|
280
332
|
| `para_cada(f, it)` | `list(map(f, it))` |
|
|
281
333
|
| `filtro(f, it)` | `list(filter(f, it))` |
|
|
282
334
|
| `reduzido(f, it)` | `functools.reduce(f, it)` |
|
|
283
335
|
|
|
284
|
-
## Transpilador
|
|
336
|
+
## Transpilador (API programática)
|
|
285
337
|
|
|
286
|
-
|
|
287
|
-
(palavras-chave) para portugues. Isso permite escrever programas Python
|
|
288
|
-
inteiramente em portugues.
|
|
289
|
-
|
|
290
|
-
### CLI: ptpy
|
|
291
|
-
|
|
292
|
-
```bash
|
|
293
|
-
ptpy meu_programa.ptpy # executa um arquivo .ptpy
|
|
294
|
-
ptpy -c 'para i em intervalo(3): escreva(i)' # executa inline
|
|
295
|
-
ptpy --mostrar meu_programa.ptpy # imprime o Python equivalente
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
### API Python
|
|
338
|
+
Além do codec, `portugython.transpilador` expõe funções para tradução programática:
|
|
299
339
|
|
|
300
340
|
```python
|
|
301
341
|
from portugython import traduzir, executar_texto, executar_arquivo
|
|
302
342
|
|
|
303
|
-
#
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
retorne 1
|
|
308
|
-
senao:
|
|
309
|
-
retorne n * fatorial(n - 1)
|
|
310
|
-
"""
|
|
311
|
-
print(traduzir(codigo_pt))
|
|
312
|
-
# def fatorial(n):
|
|
313
|
-
# if n <= 1:
|
|
314
|
-
# return 1
|
|
315
|
-
# else:
|
|
316
|
-
# return n * fatorial(n - 1)
|
|
343
|
+
# Ver o Python equivalente
|
|
344
|
+
print(traduzir("se x > 0:\n retorne x"))
|
|
345
|
+
# if x > 0:
|
|
346
|
+
# return x
|
|
317
347
|
|
|
318
348
|
# Executar diretamente
|
|
319
349
|
executar_texto("para i em intervalo(3):\n escreva(i)")
|
|
320
350
|
|
|
321
351
|
# Executar um arquivo .ptpy
|
|
322
|
-
executar_arquivo("
|
|
352
|
+
executar_arquivo("programa.ptpy")
|
|
323
353
|
```
|
|
324
354
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
```python
|
|
328
|
-
# fatorial.ptpy
|
|
329
|
-
importe portugython
|
|
330
|
-
|
|
331
|
-
defina fatorial(n):
|
|
332
|
-
se n <= 1:
|
|
333
|
-
retorne 1
|
|
334
|
-
senao:
|
|
335
|
-
retorne n * fatorial(n - 1)
|
|
336
|
-
|
|
337
|
-
defina verificar_primo(n):
|
|
338
|
-
se n < 2:
|
|
339
|
-
retorne Falso
|
|
340
|
-
para i em intervalo(2, n):
|
|
341
|
-
se n % i == 0:
|
|
342
|
-
retorne Falso
|
|
343
|
-
retorne Verdadeiro
|
|
344
|
-
|
|
345
|
-
primos = [x para x em intervalo(2, 20) se verificar_primo(x)]
|
|
346
|
-
escreva("Primos:", primos)
|
|
347
|
-
|
|
348
|
-
para n em intervalo(1, 8):
|
|
349
|
-
escreva(f"{n}! = {fatorial(n)}")
|
|
350
|
-
|
|
351
|
-
tente:
|
|
352
|
-
resultado = 10 / 0
|
|
353
|
-
exceto ZeroDivisionError como err:
|
|
354
|
-
escreva("Erro capturado:", err)
|
|
355
|
-
finalmente:
|
|
356
|
-
escreva("Pronto!")
|
|
357
|
-
```
|
|
355
|
+
## Tabela de palavras-chave
|
|
358
356
|
|
|
359
|
-
### Tabela de palavras-chave
|
|
360
357
|
|
|
361
|
-
| Portugues | Python | | Portugues | Python |
|
|
362
358
|
|-----------|--------|-|-----------|--------|
|
|
363
359
|
| `se` | `if` | | `senao` | `else` |
|
|
364
360
|
| `senaose` | `elif` | | `para` | `for` |
|
|
@@ -379,17 +375,17 @@ finalmente:
|
|
|
379
375
|
| `Verdadeiro` | `True` | | `Falso` | `False` |
|
|
380
376
|
| `Nenhum` | `None` | | | |
|
|
381
377
|
|
|
382
|
-
> **Nota**: Palavras como `e`, `ou`, `de`, `com` e `em`
|
|
383
|
-
> reservadas no contexto de arquivos `.ptpy`. Evite
|
|
384
|
-
>
|
|
385
|
-
>
|
|
378
|
+
> **Nota**: Palavras como `e`, `ou`, `de`, `com` e `em` são palavras-chave
|
|
379
|
+
> reservadas no contexto de arquivos `.ptpy`. Evite usá-las como nomes de
|
|
380
|
+
> variáveis nesses arquivos, assim como não se usa `if` ou `for` como
|
|
381
|
+
> variáveis em Python normal.
|
|
386
382
|
|
|
387
383
|
## Contribuir
|
|
388
384
|
|
|
389
|
-
1. Crie um fork deste
|
|
385
|
+
1. Crie um fork deste repositório
|
|
390
386
|
2. Crie uma branch nova a partir de main
|
|
391
387
|
3. Envie um pull request
|
|
392
388
|
|
|
393
|
-
##
|
|
389
|
+
## Licença
|
|
394
390
|
|
|
395
391
|
MIT
|