Why Learning to write better Python Matters.

About me: Dan M.

  • Senior Software Engineer at the Organic Box
  • My past: Infrastructure Engineering jobs and Network Engineering jobs.
  • Went to school for Electrical Engineering (I don't do that anymore)
  • I like kitties, badminton and development.

Feel Free to reach out!

  • You can reach me on linkedin!
    https://www.linkedin.com/in/daniel-mouris-baa4b972/
  • You can reach me on twitter!
    https://twitter.com/dgmouris
    (although I mainly tweet about edmontonpy meetups)
  • You can email me at dgmouris@gmail.com
  • Slack as well (dgmouris)

Disclaimer:

You can ask questions.

I don't have all of the answers and I'm not a perfect developer.

I don't know why my slides are teal.

Disclaimer 2:
I've seen (and written) a lot of code that isn't good python.

I'm going to give you some tips to help you write better.

People who use python are nice!

What am I going to talk about:

  1. Why good code matters and why you should care about this topic
  2. What are consequences and excuses of not writing good code.
  3. Some quick tips to improve your code today.
  4. How to make 4000$ a day from HOME. If you want to do this please send social insurance number, credit card number and health care number to dgmouris@gmail.com.

 

What is pythonic code?

Basically it's just a set of "this how we write things" by the community.

Why Am I talking about this?

Why should I listen if I do security?

Take a look at:

https://www.cursivesecurity.com/blog/2017/libressl-replacement-openssl/

https://en.wikipedia.org/wiki/LibreSSL

It's important.

It's why python got to where it is today and how it's going to grow!

It's why python is used in so many communities for example:

  • Security
  • Web Development
  • Film
  • Data Science
  • A lot of other fields too!

Why should you care about pythonic code?

  1. Makes your code readable.
  2. Makes your code more maintainable.
  3. Makes your code more correct* (for other people)

Why does readability matter?

Because it'll make you more attractive, it'll make you jump higher and be more awesome.

What happens if I don't do this?

Excuses of people choose not to write better pythonic code.

Excuse #1

But I'm a (insert field) (insert title) and I don't need to know it for my job!

Excuse #2

I (just) write scripts.

 

Note: Yes you might have said this before.

Excuse #3

I'm used to writing (insert language)

Excuse #4

I'm paid to write code, not read it.

Excuse #5

My code works and it doesn't matter what people think!

How can people read my code Better?

Use Pythonic code conventions and format using Pep8.

Try to show your intention with your code.

Care about the names that you give your classes variables.

Make functions small and easy to digest.

Let's take a look at some examples...

Use "in" to check if something is in a list

Bad!

Good!

kitty = 'Marshmallow'
found = False
if kitty == 'Marshmallow' or kitty == 'Gambit' or kitty == 'Ghost':
  found = True
found = 'Marshmallow' in ['Marshmallow', 'Gambit', 'Ghost']

Better Truthful statements!

Bad!

Good!

kitten_is_cute = True
rat_is_cute = False

cuteness = [kitten_is_cute, rat_is_cute]

if kitten_cute == True:
    # pet kitty
elif rat_is_cute == False:
    # ignore rat

if len(cuteness) > 0:
    # see if animals are cute
kitten_is_cute = True
rat_is_cute = False

cute_list  = [kitten_is_cute, rat_is_cute]

if kitten_cute:
    # pet kitty
elif not rat_is_cute:
    # ignore rat

if cute_list:
    # see if animals are cute

use return to evaluate expressions

Bad!

Good!

def check_equal(x, y):
    result = False
    if x == y:
        result = True
    return result
def is_equal(first, other):
    return first == other

Better String Formatting!

Bad!

Good!

def user_info(user, cats):
    return 'Name: ' + user.name + ' Age: '+ user.age + ' number of cats:' + cats
def get_user_details(user, cats):
    return 'Name: {user.name} Age: {user.age} number of cats '.format(
        user=user,
        cats=cats
    )

List Comprehension, use generators!

Bad!

Good!

ls = list()
for element in range(10):
  if not(element % 2):
    ls.append(element)
# We may also employ a lambda function
ls = list(filter(lambda element: not(element % 2), range(10)))
def is_even(number):
    return not(number % 2)

even_numbers = [element for element in range(10) if is_even(element)]

Use Sets when comparing lists

Bad!

Good!

ls1 = [1, 2, 3, 4, 5]
ls2 = [4, 5, 6, 7, 8]
elements_in_both = []
for element in ls1:
    if element in ls2:
        elements_in_both.append(element)
print(elements_in_both)
number_bunch = [1, 2, 3, 4, 5]
second_number_bunch = [4, 5, 6, 7, 8]
numbers_in_both_bunches = list(set(number_bunch) & set(second_number_bunch))
print(numbers_in_both)

Use the dict.get() method to get value

Bad!

Good!

all_cats = None
cat = None
if 'marshmallow' in all_cats:
    auth = all_cats['marshmallow']
else:
    auth = 'No Cats Found'
cat = all_cats.get('marshmallow', 'No Cats Found')

How you can get better!

  • Idiomatic Python. Coding the smart way.

    (https://medium.com/the-andela-way/idiomatic-python-coding-the-smart-way-cc560fa5f1d6)

  • Writing Idiomatic Python.

    https://jeffknupp.com/writing-idiomatic-python-ebook/

  • Pep8

    https://www.python.org/dev/peps/pep-0008/

  • Clean Code:A Handbook of Agile Software Craftsmanship

    https://www.amazon.ca/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882/ref=pd_lpo_sbs_14_t_1?_encoding=UTF8&psc=1&refRID=N7SYPCHTQKZWNQGFSEWT

  • Test Driven Development with Python

    https://www.obeythetestinggoat.com/

  • Test Driven Development by example (written for java but applies)

    https://www.amazon.ca/Test-Driven-Development-Kent-Beck/dp/0321146530

Summary

If you write good code you'll be able to do the following:

  1. You'll be able to read it
  2. You'll be able to maintain it
  3. People will call you a mastermind
  4. People will maintain and Update your scripts
    (just kidding they'll update your code lol)
  5. You'll be a millionaire (from home)

Summary

If you write bad code you'll feel the following:

  1. You'll hate your previous self that wrote the code.
  2. You'll feel shame
  3. You won't be happy
  4. You might bankrupt a company, and take away someone else's jobs. (I've felt this)

Summary

If you're me you like my cats

Marshmallow

Ghost

Gambit

Questions?

Questions?

Made with Slides.com