Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Programe para uma interface, não para uma imple...
Search
Diego Garcia
October 17, 2016
Programming
0
190
Programe para uma interface, não para uma implementação
Lightning talk apresentada na Python Brasil 12
Diego Garcia
October 17, 2016
Tweet
Share
More Decks by Diego Garcia
See All by Diego Garcia
Um overview sobre GIT
drgarcia1986
0
38
Modelos de concorrência e paralelismo em Python
drgarcia1986
0
94
Modelos de concorrência em Python.
drgarcia1986
0
180
Bot do Telegram
drgarcia1986
1
210
Boas práticas em REST APIs
drgarcia1986
1
1.4k
Conteinerizando aplicações Python
drgarcia1986
0
430
Testes de carga com Locust
drgarcia1986
1
440
Other Decks in Programming
See All in Programming
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
940
カクヨムAndroidアプリのリブート
numeroanddev
0
430
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
120
GraphRAGの仕組みまるわかり
tosuri13
7
450
XSLTで作るBrainfuck処理系
makki_d
0
210
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
0
140
Go1.25からのGOMAXPROCS
kuro_kurorrr
1
790
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
1
300
イベントストーミングから始めるドメイン駆動設計
jgeem
4
870
C++20 射影変換
faithandbrave
0
500
AIネイティブなプロダクトをGolangで挑む取り組み
nmatsumoto4
0
120
「ElixirでIoT!!」のこれまでとこれから
takasehideki
0
370
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Building Adaptive Systems
keathley
43
2.6k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
Thoughts on Productivity
jonyablonski
69
4.7k
KATA
mclloyd
29
14k
Intergalactic Javascript Robots from Outer Space
tanoku
271
27k
Designing for Performance
lara
609
69k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
The Power of CSS Pseudo Elements
geoffreycrofte
77
5.8k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
657
60k
Into the Great Unknown - MozCon
thekraken
39
1.9k
Typedesign – Prime Four
hannesfritz
42
2.7k
Transcript
Programe para uma interface, não para uma implementação #pybr12
Quem sou eu?
• Desenvolvedor a ~8 anos. • Desenvolvedor web a ~5
anos. • Pythonista a ~3 anos. • Django, Tornado, Aiohttp, etc. • Vimmer (#vim <3). • Aspirante a Gopher (#Golang <3). • Backend developer at LuizaLabs. Diego Garcia
Programar para uma interface significa se importar com “o que
faz?” e não com o “como faz?”
Exemplo de interface
class Storage: def get(self, key): pass def set(self, key, value):
pass
Exemplo de uso da interface
class Reader: def __init__(self, storage): self.storage = storage def get_info(self):
return self.storage.get('info')
Exemplo de implementação concreta
from redis import StrictRedis class RedisStorage: def __init__(self): self.redis =
StrictRedis() def get(self, key): return self.redis.get(key) def set(self, key, value): self.redis.set(key, value)
• RedisStorage • MemcachedStorage • DatabaseStorage • FileStorage • FakeStorage
• ...
Exemplo de carregamento dinâmico
from importlib import import_module def get_storage_backend(path): module_name, class_name = path.rsplit('.',
1) module = import_module(module_name) backend_class = getattr(module, class_name) return backend_class() storage = get_storage_backend( 'storages.redis.RedisStorage' )
Quem se preocupa com isso? • Django (ORM, Cache, etc).
• Asyncio (EventLoop). • SqlAchemy (Connectors, Engines, etc). • ...
Programar para uma interface já me salvou em uma blackfriday!
Trocamos a API de cálculo de frete só virando uma chave.
Obrigado! Se tiverem perguntas me procurem :) Twitter: @drgarcia1986 |
Github: /drgarcia1986