-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into python-wav-files
- Loading branch information
Showing
47 changed files
with
505 additions
and
342 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# How to Write Beautiful Python Code With PEP 8 | ||
|
||
This repository contains code snippets shown in the tutorial on [How to Write Beautiful Python Code With PEP 8](https://realpython.com/python-pep8/). | ||
|
||
You can uncomment the code that isn't PEP 8 compliant in the following files if you want to test the errors, linters, and autoformatters described in the tutorial: | ||
|
||
- [`mixed_indentation.py`](./mixed_indentation.py) | ||
- [`trailing_whitespace.py`](./trailing_whitespace.py) | ||
- [`unfashionable.py`](./unfashionable.py) | ||
|
||
After uncommenting the code, you can then run, lint, and format the files locally. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"""This is a docstring.""" | ||
|
||
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Uncomment the code below | ||
# for an example of code with mixed indentation: | ||
|
||
# def mixed_indentation(greet=True): | ||
# if greet: | ||
# print("Hello") # Indented with 4 spaces. | ||
# print("World") # Indented with a tab. | ||
|
||
# mixed_indentation() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def quadratic(a, b, c): | ||
"""Solve quadratic equation via the quadratic formula. | ||
A quadratic equation has the following form: | ||
ax**2 + bx + c = 0 | ||
There always two solutions to a quadratic equation: x_1 & x_2. | ||
""" | ||
x_1 = (-b + (b**2 - 4 * a * c) ** (1 / 2)) / (2 * a) | ||
x_2 = (-b - (b**2 - 4 * a * c) ** (1 / 2)) / (2 * a) | ||
|
||
return x_1, x_2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Uncomment the code below | ||
# for an example of code with a trailing whitespace: | ||
|
||
# x = 1 + 2 + \ # noqa w291 | ||
# 3 + 4 | ||
|
||
# print(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
numbers = [1, 2, 3, 4] | ||
|
||
|
||
def add_all_numbers_from_collection( | ||
number_one, number_two, number_three, number_four | ||
): | ||
return number_one + number_two + number_three + number_four | ||
|
||
|
||
print(add_all_numbers_from_collection(*numbers)) | ||
|
||
# The commented-out code shows the code before linting and formatting: | ||
|
||
# import math | ||
|
||
# numbers = [1,2,\ | ||
# 3,4] | ||
|
||
# def add_all_numbers_from_collection( | ||
# number_one, number_two, number_three, | ||
# number_four): | ||
# return number_one+number_two + number_three +number_four | ||
|
||
# print (add_all_numbers_from_collection( *numbers )) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
def calculate_variance(numbers): | ||
sum_numbers = 0 | ||
for number in numbers: | ||
sum_numbers = sum_numbers + number | ||
mean = sum_numbers / len(numbers) | ||
|
||
sum_squares = 0 | ||
for number in numbers: | ||
sum_squares = sum_squares + number**2 | ||
mean_squares = sum_squares / len(numbers) | ||
|
||
return mean_squares - mean**2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,11 @@ | ||
# Primer on Python Decorators | ||
|
||
Code examples from the [Primer on Python Decorators](https://realpython.com/primer-on-python-decorators/) tutorial on [Real Python](https://realpython.com/). | ||
Here you can find code examples from the [Primer on Python Decorators](https://realpython.com/primer-on-python-decorators/) tutorial on [Real Python](https://realpython.com/). | ||
|
||
## Decorators | ||
|
||
As noted in the article, most decorators are stored in the file [`decorators.py`](https://github.com/realpython/materials/blob/master/primer-on-python-decorators/decorators.py). The exceptions are those decorators that depend on third party packages ([Flask](http://flask.pocoo.org/) and [Pint](https://pint.readthedocs.io/)), which are available in [`decorators_flask.py`](https://github.com/realpython/materials/blob/master/primer-on-python-decorators/decorators_flask.py) and [`decorators_unit.py`](https://github.com/realpython/materials/blob/master/primer-on-python-decorators/decorators_unit.py), respectively. | ||
You can find most decorators in the file `decorators.py`. The exceptions are those decorators that depend on the third-party packages ([Flask](http://flask.pocoo.org/) and [Pint](https://pint.readthedocs.io/)). These decorators are available in `secret_app.py`, `validate_input.py`, and `units.py`. | ||
|
||
## Examples | ||
|
||
Most of the code examples from the article are available in the [`examples.py`](https://github.com/realpython/materials/blob/master/primer-on-python-decorators/examples.py) file. | ||
|
||
## Cheat Sheet | ||
|
||
We’ve put together a short & sweet Python decorators cheat sheet for you that summarizes the techniques explained in this tutorial: | ||
|
||
[Get the decorators cheat sheet »](https://realpython.com/optins/view/decorators-cheatsheet/) | ||
You can find many code examples from the tutorial in separate Python files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import math | ||
|
||
from decorators import debug | ||
|
||
math.factorial = debug(math.factorial) | ||
|
||
|
||
def approximate_e(terms=18): | ||
return sum(1 / math.factorial(n) for n in range(terms)) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(approximate_e(5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
class Circle: | ||
def __init__(self, radius): | ||
self.radius = radius | ||
|
||
@property | ||
def radius(self): | ||
"""Get value of radius""" | ||
return self._radius | ||
|
||
@radius.setter | ||
def radius(self, value): | ||
"""Set radius, raise error if negative""" | ||
if value >= 0: | ||
self._radius = value | ||
else: | ||
raise ValueError("radius must be non-negative") | ||
|
||
@property | ||
def area(self): | ||
"""Calculate area inside circle""" | ||
return self.pi() * self.radius**2 | ||
|
||
def cylinder_volume(self, height): | ||
"""Calculate volume of cylinder with circle as base""" | ||
return self.area * height | ||
|
||
@classmethod | ||
def unit_circle(cls): | ||
"""Factory method creating a circle with radius 1""" | ||
return cls(1) | ||
|
||
@staticmethod | ||
def pi(): | ||
"""Value of π, could use math.pi instead though""" | ||
return 3.1415926535 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from decorators import debug, timer | ||
|
||
|
||
class TimeWaster: | ||
@debug | ||
def __init__(self, max_num): | ||
self.max_num = max_num | ||
|
||
@timer | ||
def waste_time(self, num_times): | ||
for _ in range(num_times): | ||
sum([number**2 for number in range(self.max_num)]) | ||
|
||
|
||
@timer | ||
class TimeWaster2: | ||
def __init__(self, max_num): | ||
self.max_num = max_num | ||
|
||
def waste_time(self, num_times): | ||
for _ in range(num_times): | ||
sum([i**2 for i in range(self.max_num)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.