Skip to content

Instantly share code, notes, and snippets.

View antunesleo's full-sized avatar

Leonardo Antunes antunesleo

View GitHub Profile
@antunesleo
antunesleo / the-godfather.json
Created October 5, 2018 12:17
Creating a document with ID 1 in movies index
POST movies/_doc/1
{
"title": "The Godfather",
"year": 1972,
"director": "Francis Ford Coppola",
"stars": [
"Marlon Brando",
"Al Pacino",
"James Caan"
],
POST movies/_doc/2
{
"title": "The Dark Night",
"year": 2008,
"director": "Christopher Nolan",
"stars": [
"Christian Bale",
"Heath Ledger",
"Aaron Eckhart"
],
GET movies/_search
{
"query": {
"match": {
"director": "Nolan"
}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
GET movies/_search
{
"query": {
"match": {
"year": 2015
}
}
}
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
nomes = ["Leonardo", "Jao", "Ana"]
# Mais lento, não pythonico
nomes_com_o = []
for nome in nomes:
if 'o' in nome:
nomes_com_o.append(nome)
nomes_com_o = [nome for nome in nomes if 'o' in nomes] # Mais rápido, legível e pythonico
nomes = ["Leonardo", "Jao", "Ana"]
# Mais lento, usa mais memória pois cada vez que nomes_string
# recebe um valor um novo objeto em memória criado
nomes_string = ""
for nome in nomes:
nomes_string += nome
nomes_string = ''.join(nomes) # Mais rápido, pythonico e usa menos memória
@antunesleo
antunesleo / item-ta-na-lista.py
Last active November 4, 2018 22:15
Como verificar se um item faz parte de uma lista de maneira mais performatica em python
nomes_list = ["Leonardo", "Jao", "Ana"] # Mais devagar, usando list
if "Pythonson" in nomes_list:
print("Pythonson ta na lista de nomes")
else:
print("Pythonson nao ta na lista de nomes")
nomes_set = {"Leonardo", "João", "Ana"} # Mais rápido, usando set
@antunesleo
antunesleo / gist:dc2552f2c578670a67a57d79d357851e
Created December 11, 2018 12:17
Installing python3-pip and python3-venv
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
sudo apt-get install -y python3-venv
python3 -m venv elasticsearch_loader
source elasticsearch_loader/bin/activate
pip3 install elasticsearch_loader