pip freeze > requirements.txt
Tag python
🐍 Criando um server HTTP com Python
Em python 2
python -m SimpleHTTPServer 8000
Em python 3
python -m http.server 8000
Referencia:
[1] – https://ryanblunden.com/create-a-http-server-with-one-command-thanks-to-python-29fcfdcd240e
🐍 Adicionando tema ao Jupyter Notebook
Instale o pacote de temas:
pip install jupyterthemes
veja os temas disponíveis:
jt -l
Resetar o tema:
jt -r
Selecione o tema:
jt -t <nome-do-tema>
exemplo:
jt -t monokai
Referências:
[1] – https://stackoverflow.com/questions/46510192/change-the-theme-in-jupyter-notebook
[2] – https://stackoverflow.com/questions/40683123/how-to-reset-jupyter-notebook-theme-to-default
🐍 Criando um servidor para o Jupyter Notebook
Crie uma nova configuração:
jupyter notebook --generate-config
No arquivo criado coloque essas duas linhas:
c.NotebookApp.allow_origin = '*' #allow all origi
c.NotebookApp.ip = '0.0.0.0' # listen on all
E depois no terminal digite comando:
jupyter notebook --ip xx.xx.xx.xx --port 8888
Troque o xx.xx.xx.xx pelo ip da maquina.
Dica de Segurança:
Voce poderá colocar uma senha no seu Notebook digitando o comando abaixo:
c = get_config()
c.NotebookApp.password = u'sha1:6c2164fc2b22:ed55ecf07fc0f985ab46561483c0e888e8964ae6'
Voce poderá gerar sua própria senha sha1 com o seguinte código python:
from IPython.lib import passwd
password = passwd("secret")
password
Referencias:
🐍 Utilizando Virtual Environments no Python
Se não tiver instalado:
pip install virtualenv
Criar um ambiente:
virtualenv -p <versão-python> <nome da pasta>
Exemplo:
virtualenv -p python3 jmallone/
Ativar o ambiente:
cd jmallone
source bin/activate
Referência:
[1] – https://realpython.com/python-virtual-environments-a-primer/
Instalando o geckodriver
Se voce quer navegar com o selenium com o seu firefox esse drive devera ser instalado
pip install selenium
- Download geckodriver
- Copia o executavel geckodriver para o diretorio
/usr/local/bin
[1] – https://selenium-python.readthedocs.io/installation.html
SyntaxError: Non-ASCII character ‘xc3’ no Python
Coloque na primeira linha do arquivo:
# -*- coding: utf-8 -*-
Referência:
Alterando a versão do Python no Linux
Digite no terminal:
$ sudo ln -sf /usr/bin/python3.6 /usr/bin/python
Trocando o python3.6 pela pasta do Python que você quer alterar.
Referência:
[ 1 ] – https://www.vivaolinux.com.br/dica/Como-alterar-a-versao-default-padrao-do-Python-no-Linux
Instalando e usando o Ply no Python
Ply é um analisador sintático muito pratico para quem quer fazer esse tipo de coisa no python, onde ele retorna sua saída em forma de tokens, a sua instalação se da de maneira fácil através do pip.
Instalação
$ pip install ply
Exemplo
# ------------------------------------------------------------
# calclex.py
#
# tokenizer for a simple expression evaluator for
# numbers and +,-,*,/
# ------------------------------------------------------------
import ply.lex as lex
# List of token names. This is always required
tokens = (
'NUMBER',
'PLUS',
'MINUS',
'TIMES',
'DIVIDE',
'LPAREN',
'RPAREN',
)
# Regular expression rules for simple tokens
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
# A regular expression rule with some action code
def t_NUMBER(t):
r'\d+'
t.value = int(t.value)
return t
# Define a rule so we can track line numbers
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
# STRING contendo caracteres a serem ignorados(espaços e tabs)
t_ignore = ' \t'
# Se deu Erro
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Construindo o LEX
lexer = lex.lex()
# Entrada de Exemplo
data = '''
3 + 4 * 10
+ -20 *2
'''
# Passa a entrada para o analizador
lexer.input(data)
# Tokenize
while True:
tok = lexer.token()
if not tok:
break # Termina a entrada
print(tok)