Download - Naming Convention in Python

Transcript
Page 1: Naming Convention in Python

A Python Æsthetic

Page 2: Naming Convention in Python

Video: http://pyvideo.org/video/1599/ Slides: http://rhodesmill.org/brandon/slides/2012-11-pyconca/

Page 3: Naming Convention in Python

Video: http://pyvideo.org/video/1676/ Slides: http://rhodesmill.org/brandon/slides/2013-03-pycon/

Page 4: Naming Convention in Python

Name the Ducks

• Well-factored Nouns

• Relentless Verbs

• Sin of Synecdoche

• Problem of Pluralization

Page 5: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)

Page 6: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)!!def download_page(page)! ... = client.get(page)

Page 7: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)!!def download_page(url)! page = client.get(url)

Page 8: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)!!def download_page(url):! page = client.get(url)! process_page(page)!!def process_page(page):! ... = page.content

Page 9: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)!!def download_page(url):! page = client.get(url)! process_page(page)!!def process_page(response):! page = response.content

Page 10: Naming Convention in Python

Well-Factored Nouns

def main():! page = 'http://twitter.com/uranusjr/'! download_page(page)!!def download_page(url):! page = requests.get(url)! process_page(page)!!def process_page(response):! page = response.content

Page 11: Naming Convention in Python

Well-Factored Nouns

def main():! url = 'http://twitter.com/uranusjr/'! download_page(url)!!def download_page(url):! response = requests.get(url)! process_page(response)!!def process_page(response):! page = response.content

Page 12: Naming Convention in Python

Well-Factored Nouns

• Plan ahead (when you can)

• Refactor often

• Challenge yourself

Page 13: Naming Convention in Python

Relentless Verbs

def database():! # What does this even do?!!def get_database():! # Gets an instance for me!!!def create_database():! # Creates a database and returns! # it for me!

Page 14: Naming Convention in Python

Relentless Verbs

def database():! # What does this even do?!!def get_database():! # Gets an instance for me!!!def create_database():! # Creates a database and returns! # it for me!

Page 15: Naming Convention in Python

Relentless Verbs

document.md5()! ! ! # Probably okay!!document.get_md5()!!document.calculate_md5()

Page 16: Naming Convention in Python

[P]racticality beats purity.

Page 17: Naming Convention in Python

[P]racticality beats purity.reversed(iterable)!!'a'.upper()!!

len(iterable)!!min(iterable)!

Page 18: Naming Convention in Python

Relentless Verbs

• Functions act

• Verbose verbs can be dropped

• Practicality beats purity

Page 19: Naming Convention in Python

Sin of Synecdoche

# In medialib.py:!def fetch_songs(urls):! ...!!# In songlib.py:!def fetch(urls):! ...

Page 20: Naming Convention in Python

Sin of Synecdoche

# The caller has their choice:!!from songlib import fetch!fetch(...)!!# or:!!import songlib!songlib.fetch(...)

Page 21: Naming Convention in Python

Sin of Synecdoche

• Be obvious

• Don’t repeat yourself

• Let users choose

Page 22: Naming Convention in Python

Problem of Pluralization

>>> connections![<SQLConnection at 0xb72ff4d8>,! <SQLConnection at 0xb72ff4d0>,! <SQLConnection at 0xb72ff4f0>]!!>>> connections!{'master': <SQLConnection at 0xb72ff4d8>,! 'backup': <SQLConnection at 0xb72ff4f0>}

Page 23: Naming Convention in Python

datum

data

datumdatum

datum

dataset

data

datumdatumdatum

data

datumdatumdatum

data

datum

datasets

datadatudatudatu

datadatudatudatu

datadatadatudatudatu

datadatudatudatu

datadataset

datadatudatudatu

datadatudatudatu

data

Page 24: Naming Convention in Python

Anti-Plural

connection_set!connection_list!connection_dict!!def close_all(connection_seq):! ...!!def reset_all(connection_map):! ...

Page 25: Naming Convention in Python

I still favor pluralsAnd documentation is always necessary.

Page 26: Naming Convention in Python

Problem of Pluralization

• Plurals can be ambiguous

• There are only so many expressions

• Total anti-plural may be too much

Page 27: Naming Convention in Python

My Problem

Page 28: Naming Convention in Python

My Problem

def user_profile(request, username):! user = request.user! if user.is_authenticated:! if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user

Page 29: Naming Convention in Python

My Problem

def user_profile(request, username):! user = request.user! if user.is_authenticated:! if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user

Page 30: Naming Convention in Python

My Problem

>>> user.is_authenticated!<bound method User.is_authenticated of <User: admin>>

Page 31: Naming Convention in Python

My Problem

def user_profile(request, username):! user = request.user! if user.is_authenticated():! if user.username == username:! # Show personal page! else:! # Show public page! else:! # Show page for anonymous user

Page 32: Naming Convention in Python
Page 33: Naming Convention in Python

Descriptive Boolean

# is + adjective = method!user.is_authenticated()!!!# No!!user.is_active!!# Clearly a noun.!user.activeness

Page 34: Naming Convention in Python

Watch Out

# is + noun = variable?!>>> user.is_staff!True!!# But...!>>> '1'.isdigit!<built-in method isdigit of str object at 0x106fa63a0>

Page 35: Naming Convention in Python
Page 36: Naming Convention in Python

My Current Solution# is + anything = method!user.is_authenticated()!!!# WRONG!method.is_action = True!!# Considered correct (but ugly)!method.action_flag = True!!# Maybe use something else?!method.action_info = None

Page 37: Naming Convention in Python

Python lacked booleans until 2.2.1

Maybe we can get away with it.

Page 38: Naming Convention in Python

Similarly Problematic

# Makes more sense being a variable!request.url!!# Obviously needs processing. Okay.!document.md5()!!# ???!rectangle.left!rectangle.center

Page 39: Naming Convention in Python

No Perfect Solution# Parentheses do not matter in Ruby!user.authenticated?!!# But things can get funky quickly!proc_obj = proc { user.authenticated? }!proc_obj.call!!# Python can be inconvenient!user.is_authenticated()!!# But obvious things stay obvious!func = user.is_authenticated!func()

Page 40: Naming Convention in Python

FIRE QUESTIONS