-
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 hangman-pysimplegui
- Loading branch information
Showing
15 changed files
with
1,061 additions
and
0 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,3 @@ | ||
# Single and Double Underscores in Python Names | ||
|
||
This folder provides the code examples for the Real Python tutorial [Single and Double Underscores in Python Names](https://realpython.com/python-double-underscore/). |
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,15 @@ | ||
class ShoppingCart: | ||
def __init__(self, customer_id): | ||
self.customer_id = customer_id | ||
self.products = [] | ||
|
||
def add_product(self, product): | ||
self.products.append(product) | ||
|
||
def get_products(self): | ||
return self.products | ||
|
||
def __len__(self): | ||
return len(self.products) | ||
|
||
# Implementation... |
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,15 @@ | ||
_count = 0 | ||
|
||
|
||
def increment(): | ||
global _count | ||
_count += 1 | ||
|
||
|
||
def decrement(): | ||
global _count | ||
_count -= 1 | ||
|
||
|
||
def get_count(): | ||
return _count |
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,19 @@ | ||
# csv_data.py | ||
|
||
import csv | ||
|
||
|
||
class CSVFileManager: | ||
def __init__(self, file_path): | ||
self.file_path = file_path | ||
|
||
def read_csv(self): | ||
return self._read(delimiter=",") | ||
|
||
def read_tsv(self): | ||
return self._read(delimiter="\t") | ||
|
||
def _read(self, delimiter): | ||
with open(self.file_path, mode="r") as file: | ||
data = [row for row in csv.reader(file, delimiter=delimiter)] | ||
return data |
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 @@ | ||
class Passenger: | ||
def __init__(self, name, class_, seat): | ||
self.name = name | ||
self.class_ = class_ | ||
self.seat = seat | ||
|
||
# Implementation... |
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,26 @@ | ||
class Point: | ||
def __init__(self, x, y): | ||
self._x = x | ||
self._y = y | ||
|
||
@property | ||
def x(self): | ||
return self._x | ||
|
||
@x.setter | ||
def x(self, value): | ||
self._x = _validate(value) | ||
|
||
@property | ||
def y(self): | ||
return self._y | ||
|
||
@y.setter | ||
def y(self, value): | ||
self._y = _validate(value) | ||
|
||
|
||
def _validate(value): | ||
if not isinstance(value, int | float): | ||
raise ValueError("number expected") | ||
return value |
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,23 @@ | ||
_PI = 3.14 | ||
|
||
|
||
class Circle: | ||
def __init__(self, radius): | ||
self.radius = _validate(radius) | ||
|
||
def calculate_area(self): | ||
return round(_PI * self.radius**2, 2) | ||
|
||
|
||
class Square: | ||
def __init__(self, side): | ||
self.side = _validate(side) | ||
|
||
def calculate_area(self): | ||
return round(self.side**2, 2) | ||
|
||
|
||
def _validate(value): | ||
if not isinstance(value, int | float) or value <= 0: | ||
raise ValueError("positive number expected") | ||
return value |
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 @@ | ||
# The Python Rich Package: Unleash the Power of Console Text | ||
|
||
This repository holds the sample code for the Real Python [showcase](https://realpython.com/python-rich-package). | ||
|
||
## Dependencies | ||
|
||
You should start by creating a virtual environment: | ||
|
||
```console | ||
$ python -m venv venv | ||
$ source venv/bin/activate | ||
``` | ||
|
||
Next, install `rich` with `pip`: | ||
|
||
```console | ||
(venv) $ python -m pip install rich | ||
``` | ||
|
||
The tutorial includes static sample data for the scrolling cryptocurrency table. This is also included in this repository as `crypto_data.json`. | ||
If you want to download your own fresh data, then you'll need the `requests` package: | ||
|
||
```console | ||
(venv) $ python -m pip install requests | ||
``` | ||
|
||
The code to perform the download is in `get_crypto_data.py`. This demo code demonstrates a single request, which it then dumps to the screen. For a real-time application, you'd need to execute this request and process the data in a loop. The [CoinLore website](https://www.coinlore.com/cryptocurrency-data-api) doesn't impose rate limits, but they suggest making at most one request per second. | ||
|
||
## Author | ||
|
||
[Charles de Villiers](https://realpython.com/team/cdevilliers/) | ||
|
||
## License | ||
|
||
Distributed under the MIT license. See `LICENSE` in the root directory of this `materials` repo for more information. |
Oops, something went wrong.