Python Indolor

Ronie Uliana

http://slides.com/ronie/python-indolor

Por quê?

Ronie Uliana - Python Indolor

Você precisa fazer algo em Python e não está feliz com isso. 

O que?

Posso mostrar alguns idiomas do Python

   (Idioma?)

o_O

Ronie Uliana - Python Indolor

Quem?

Ronie Uliana

Data Engineer

Mestrando Mackenzista

99ner

ex-VAGAS.com

Ruby, Scala, Java, C#, Racket (Scheme), Javascript (sem orgulho), Python (um pouco), R (masomenos), Rust, Haskell (bem mal), Factor (oi?)

Coleções idiomáticas

Listas, Filas, Hashes, Sets

Ronie Uliana - Python Indolor

Muito uso!

Transformar!

Ronie Uliana - Python Indolor

Loop

Idiomático

letters = ["a", "b", "c", "d"]
result = []
for i in range(0, len(letters)):
    result.append(letters[i].upper())
print(result)
letters = ["a", "b", "c", "d"]
result = [e.upper() for e in letters]
print(result)
letters = ["a", "b", "c", "d"]
result = map(str.upper, letters)
print(list(result))

ou isso

Filtrar!

Ronie Uliana - Python Indolor

Loop

Idiomático

letters = ["A", "a", "B", "b"]
result = []
for i in range(0, len(letters)):
    if letters[i].isupper():
        result.append(letters[i])
print(result)
letters = ["A", "a", "B", "b"]
result = [e for e in letters if e.isupper]
print(result)
letters = ["A", "a", "B", "b"]
result = filter(str.isupper, letters)
print(list(result))

ou isso

Lists, Sets & Hashes!

Ronie Uliana - Python Indolor

letters = ["A", "a", "B", "b"]
result1 = [   e.upper() for e in letters]
result2 = {   e.upper() for e in letters}
result3 = {e: e.upper() for e in letters}
print(result1)
print(result2)
print(result3)

Percorrer pares!

Ronie Uliana - Python Indolor

result = {k: v for (k, v) in zip("abc", [1, 2, 3])}
print(result)

itertools \o/

Ronie Uliana - Python Indolor

from itertools import *

def edges_of_complete_graph(vertices):
    return combinations(vertices, 2)

def edges_of_directed_complete_graph(vertices):
    return permutations(vertices, 2)

print(list(edges_of_complete_graph(5)))
print(list(edges_of_directed_complete_graph(5)))

collections \o/

Ronie Uliana - Python Indolor

from collections import *

words = ["a", "vida", "a", "lida", "da", "vida"]
bag = Counter(words)
print(bag)
print(bag.most_common(2))

common = Counter(dict(bag.most_common(2)))
print(bag - common)

Slicing 

Ronie Uliana - Python Indolor

words = ["um", "dois", "três", "pim"]
print(words[0])
print(words[-1])
print(words[:-1])
print(words[1:3])
print(words[::2])

Mais...

Ronie Uliana - Python Indolor

Como agrupar elementos?

Como percorrer consecutivos (janela)?

Como percorrer em grupos?

Um arcabouço mental

Programas possuem estrutura

 

Duas estruturas:

PISO

DRY

Ronie Uliana - Python Indolor

PISO = Pig In, Sausage Out

 ,._
@"_.)~  => ()____) => (linguiça)
  !!  

"entra porco, sai linguiça"

Ronie Uliana - Python Indolor

Wikipedia -> tokens (15 linhas)

Ronie Uliana - Python Indolor

from collections import Counter
from lxml import html
import requests, re, math

# Screen Scraping
page = requests.get("https://pt.wikipedia.org/wiki/Python")
tree = html.fromstring(page.content)
paragraphs = tree.cssselect("#mw-content-text > p")
paragraphs = [e.text_content() for e in paragraphs]
text = " ".join(paragraphs)

# Token and stopwords
words = re.findall(r"\w+", text)
tokens = Counter(words)
print(tokens.most_common(10))
stopwords = Counter({"de": math.inf, "a": math.inf, "o": math.inf})
tokens = tokens - stopwords
print(tokens.most_common(10))
pip install cssselect
 ,._
@"_.)~
  !!  

DRY = Don't Repeat Yourself

  ,._      ,._      ,._
 @"_.)~ + @"_.)~ = @"_.)~ x2
   !!       !!       !!

Ronie Uliana - Python Indolor

Criando...

Ronie Uliana - Python Indolor

from itertools import *

vertices = ["a", "b", "c", "d"]
complete = combinations(vertices, 2)
complete = map(sorted, complete)
complete = map(tuple, complete)
complete = set(complete)
print(complete)

my_graph = [("c", "d"), ("b", "d")]
my_graph = map(sorted, my_graph)
my_graph = map(tuple, my_graph)
my_graph = set(my_graph)
complement = complete - my_graph
print(complement)

my_graph

complete

complement

Observando...

Ronie Uliana - Python Indolor

from itertools import *

vertices = ["a", "b", "c", "d"]
complete = combinations(vertices, 2)
complete = map(sorted, complete)
complete = map(tuple, complete)
complete = set(complete)
print(complete)

my_graph = [("c", "d"), ("b", "d")]
my_graph = map(sorted, my_graph)
my_graph = map(tuple, my_graph)
my_graph = set(my_graph)
complement = complete - my_graph
print(complement)
 ,._
@"_.)~
  !!
  +    
 ,._
@"_.)~
  !!

Abstraindo...

Ronie Uliana - Python Indolor

from itertools import *

def to_graph(edges):
    edges = map(sorted, edges)
    edges = map(tuple, edges)
    return set(edges)

vertices = ["a", "b", "c", "d"]

complete = to_graph(combinations(vertices, 2))
my_graph = to_graph([("c", "d"), ("b", "d")])

complement = complete - my_graph
print(complement)
 ,._
@"_.)~ x2
  !!

Tudo junto!

Recuperar 2 páginas da Wikipedia e ver quais são os termos em comum.

Ronie Uliana - Python Indolor

Quantas linhas?

18 linhas

Ronie Uliana - Python Indolor

from collections import Counter
from lxml import html
import requests, re, math

stopwords = Counter({e: math.inf for e in ["de", "da", "do", "e"]})

def wikipedia_pt(name):
    page = requests.get("https://pt.wikipedia.org/wiki/" + name)
    tree = html.fromstring(page.content)
    paragraphs = tree.cssselect("#mw-content-text > p")
    paragraphs = [e.text_content() for e in paragraphs]
    return " ".join(paragraphs)

def to_bag(text):
    words = re.findall(r"\w+", text)
    return Counter(words)

def wiki2bag(name):
    return to_bag(wikipedia_pt(name)) - stopwords

python = wiki2bag("Python")
ruby = wiki2bag("Ruby_(linguagem_de_programação)")
print(python & ruby)

Obrigado!

Perguntas?

Ronie Uliana - Python Indolor

Python Indolor

By Ronie Uliana

Python Indolor

Você precisa fazer algo em Python e não está feliz com isso? Talvez essas pequenas coisas ajudem.

  • 2,189