api design is hard - swiss python summit · 2 me creator of jedi and jedi-vim both have ~ 2000...

19

Click here to load reader

Upload: hakhue

Post on 31-Mar-2018

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

API Design is Hard

By Dave Halter @davidhalter on Github@jedidjah_ch on Twitter

Page 2: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

2

Me

● Creator of Jedi and jedi-vim● Both have ~ 2000 stars on Github● Starting to really like clean code!● Of course some parts of the API of Jedi suck.

Page 3: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

3

Influences & Inspirations

● API Design: Lessons Learned by Raimond Hettinger

● Good API Design by Alex Martelli

Page 4: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

4

Writing clean code

● Clean Code / Good Architecture● Testing (py.test/tox)● Documentation (sphinx)● Code Reviews

Page 5: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

5

API

● “Application Program Interface”

● Let's just talk about Python “interfaces”.● Are there interfaces in Python?

Page 6: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

6

API

● “Application Program Interface”

● Let's just talk about Python “interfaces”.● Are there interfaces in Python?● Yes: “abc.ABCMeta”

Page 7: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

7

Bad APIs #1

● No API● But everything has an interface

Page 8: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

8

Bad APIs #2

Going for both, it shouldn't be possible to write both:

jedi.names(source)

jedi.Script(source).names()

Decide!

Page 9: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

9

Bad APIs #3

● Inconsistency● Not following standards like

class Foo(object):

def getRange(self): # Java style

return self._range

But: BeautifulSoup 3 was still awesome.

Page 10: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

10

Think!

● Brainstorm – Design / Performance● Think about data types● Don't do IO that is not readable by other

languages, like pickle.● Simple is better than complex.

- PEP 20: The Zen of Python

Page 11: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

11

Be conservative!!!

Page 12: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

12

Private/Protected/Public

● _variable for protected● __variable for private (don't use it a lot though)● Use _ a lot!

Page 13: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

13

Named Arguments

● What is this doing:

twitter_search('python', 3, False)

● Way better:

twitter_search('python', num_results=3, retweets=False)

● In Python 3: def twitter_search(name, *, num_results=20, retweets=False):

Page 14: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

14

Properties

● Use them, but only for clear defined “getters”:

@property

def line_nr(self):

return 42

● For more compliated things:

def docstring(self):

return ... # maybe in the future parametrize

Page 15: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

15

Transitions

● Do transitions incrementally, big transitions like Python 2 → 3 are hard.

● Deprecate with Warnings & Documentation

Page 16: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

16

Transitions

def call_name(self): """ .. deprecated:: 0.8.0 Use :attr:`.name` instead. The name (e.g. 'isinstance') as a string. """ warnings.warn("Use name instead.", DeprecationWarning) [...]

Page 17: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

17

Service Oriented Architecture

● Amazon:~2002: Use service interfaces only.

“Anyone who doesn’t do this will be fired. Thank you; have a nice day!”

Page 18: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

18

Good Code

● Use what you learned in API Design for your internal API's.

● You should be able to go public with a sub-package without refactoring.

Page 19: API Design is Hard - Swiss Python Summit · 2 Me Creator of Jedi and jedi-vim Both have ~ 2000 stars on Github Starting to really like clean code! Of course some parts of the API

19

KTHXBYE

● Like writing APIs? We're looking for Python/DevOps Engineers: [email protected]

● @davidhalter on Github● @jedidjah_ch on Twitter