diff --git a/Car.py b/Car.py new file mode 100644 index 0000000000000000000000000000000000000000..e76d00775fc8aca0a97607f1ab3587d0931d9323 --- /dev/null +++ b/Car.py @@ -0,0 +1,167 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +"""This module's docstring summary line. + +This is a multi-line docstring. Paragraphs are separated with blank lines. +Lines conform to 79-column limit. + +Module and packages names should be short, lower_case_with_underscores. +Notice that this in not PEP8-cheatsheet.py + +Seriously, use flake8. Atom.io with https://atom.io/packages/linter-flake8 +is awesome! + +See http://www.python.org/dev/peps/pep-0008/ for more PEP-8 details +""" + + +import os # STD lib imports first +import sys # alphabetical + +import some_third_party_lib # 3rd party stuff next +import some_third_party_other_lib # alphabetical + +import local_stuff # local stuff last +import more_local_stuff +import dont_import_two, modules_in_one_line # IMPORTANT! +from pyflakes_cannot_handle import * # and there are other reasons it should be avoided # noqa +# Using # noqa in the line above avoids flake8 warnings about line length! +from .Vehicle import Vehicle # Explicit relative import + + +_a_global_var = 2 # so it won't get imported by 'from foo import *' +_b_global_var = 3 + +A_CONSTANT = 'ugh.' + + +# 2 empty lines between top-level funcs + classes +def naming_convention(): + """Write docstrings for ALL public classes, funcs and methods. + + Functions use snake_case. + """ + if x == 4: # x is blue <== USEFUL 1-liner comment (2 spaces before #) + x, y = y, x # inverse x and y <== USELESS COMMENT (1 space after #) + c = (a + b) * (a - b) # operator spacing should improve readability. + dict['key'] = dict[0] = {'x': 2, 'cat': 'not a dog'} + + +class NamingConvention(object): + """First line of a docstring is short and next to the quotes. + + Class and exception names are CapWords. + + Closing quotes are on their own line + """ + + a = 2 + b = 4 + _internal_variable = 3 + class_ = 'foo' # trailing underscore to avoid conflict with builtin + + # this will trigger name mangling to further discourage use from outside + # this is also very useful if you intend your class to be subclassed, and + # the children might also use the same var name for something else; e.g. + # for simple variables like 'a' above. Name mangling will ensure that + # *your* a and the children's a will not collide. + __internal_var = 4 + + # NEVER use double leading and trailing underscores for your own names + __nooooooodontdoit__ = 0 + + # don't call anything (because some fonts are hard to distiguish): + l = 1 + O = 2 + I = 3 + + # some examples of how to wrap code to conform to 79-columns limit: + def __init__(self, width, height, + color='black', emphasis=None, highlight=0): + if width == 0 and height == 0 and \ + color == 'red' and emphasis == 'strong' or \ + highlight > 100: + raise ValueError('sorry, you lose') + if width == 0 and height == 0 and (color == 'red' or + emphasis is None): + raise ValueError("I don't think so -- values are %s, %s" % + (width, height)) + Blob.__init__(self, width, height, + color, emphasis, highlight) + + # empty lines within method to enhance readability; no set rule + short_foo_dict = {'loooooooooooooooooooong_element_name': 'cat', + 'other_element': 'dog'} + + long_foo_dict_with_many_elements = { + 'foo': 'cat', + 'bar': 'dog' + } + + # 1 empty line between in-class def'ns + def foo_method(self, x, y=None): + """Method and function names are lower_case_with_underscores. + + Always use self as first arg. + """ + pass + + @classmethod + def bar(cls): + """Use cls!""" + pass + + +class Car(Vehicle): + + def __init__(self, speed, *args, **kwargs): + # Unpacking using * operator for lists or ** for dicts + super(Car, self).__init__(*args, **kwargs) + self.speed = speed + self.odometer = 0 + self.time = 0 + + def say_state(self): + print("I'm going {} kph!".format(self.speed)) + + def accelerate(self): + self.speed += 5 + + def brake(self): + if self.speed < 5: + self.speed = 0 + else: + self.speed -= 5 + + def step(self): + self.odometer += self.speed + self.time += 1 + + def average_speed(self): + if self.time != 0: + return self.odometer / self.time + else: + pass + + +if __name__ == '__main__': + + my_car = Car() + print("I'm a car!") + while True: + action = input("What should I do? [A]ccelerate, [B]rake, " + "show [O]dometer, or show average [S]peed?").upper() + if action not in "ABOS" or len(action) != 1: + print("I don't know how to do that") + continue + if action == 'A': + my_car.accelerate() + elif action == 'B': + my_car.brake() + elif action == 'O': + print("The car has driven {} kilometers".format(my_car.odometer)) + elif action == 'S': + print("The car's average speed was {} kph".format(my_car.average_speed())) + my_car.step() + my_car.say_state() +# Newline at end of file