Skip to content

Instantly share code, notes, and snippets.

@coreyhermanson
Last active June 9, 2021 12:06
Show Gist options
  • Save coreyhermanson/568129cc1c52e34cf85ae3ac5bc77835 to your computer and use it in GitHub Desktop.
Save coreyhermanson/568129cc1c52e34cf85ae3ac5bc77835 to your computer and use it in GitHub Desktop.
Python CodeBook

Python CodeBook

Best Practices

IF Statements

  • One line ‘IF’ statement: 'Yes' if fruit == 'Apple' else 'No'

Dictionaries

  • Check for 'None' values in a dictionary. If key = None, remove the key from the dictionary
for key in list(body):
    if not body[key]:
        body.pop(key)

Requests module

  • Need SSL certificate for SSL error, otherwise set: verify=false

Classes

Functions

  • tqdm library for timing programs
for company in tqdm(no_match_list_one,  desc='Stage 2 matching', leave=True):
	{logic}

Fuzzywuzzy library for string matching

Enumerate

for enum_name, event in enumerate(range(events)):
    print("Enum: {} || Event: {}".format(enum_name, event))
  	# Enum: 0 || Event: 0
  	# Enum: 1 || Event: 1
  	# Enum: 2 || Event: 2
   	# Enum: 3 || Event: 3
  	# Enum: 4 || Event: 4

for enum_name, event in enumerate(range(5), 1):
	print("Enum: {} || Event: {}".format(enum_name, event))
  	# Enum: 1 || Event: 0
  	# Enum: 2 || Event: 1
  	# Enum: 3 || Event: 2
  	# Enum: 4 || Event: 3
  	# Enum: 5 || Event: 4

Requests module

Decorators

Data Structures

  • Data type conversion = 2 ways, implicit (coercion) and explicit (casting)
    • implicit = 4.0 / 2 = 2.0 (automatically converts to float)
    • explicit = str(4) or float(4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment