diff --git a/python-double-underscore/README.md b/python-double-underscore/README.md new file mode 100644 index 0000000000..e5603ec144 --- /dev/null +++ b/python-double-underscore/README.md @@ -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/). diff --git a/python-double-underscore/cart.py b/python-double-underscore/cart.py new file mode 100644 index 0000000000..4ccd2bf5a4 --- /dev/null +++ b/python-double-underscore/cart.py @@ -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... diff --git a/python-double-underscore/count.py b/python-double-underscore/count.py new file mode 100644 index 0000000000..4fe666c85b --- /dev/null +++ b/python-double-underscore/count.py @@ -0,0 +1,15 @@ +_count = 0 + + +def increment(): + global _count + _count += 1 + + +def decrement(): + global _count + _count -= 1 + + +def get_count(): + return _count diff --git a/python-double-underscore/csv_data.py b/python-double-underscore/csv_data.py new file mode 100644 index 0000000000..e018c5692c --- /dev/null +++ b/python-double-underscore/csv_data.py @@ -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 diff --git a/python-double-underscore/passenger.py b/python-double-underscore/passenger.py new file mode 100644 index 0000000000..aa98178745 --- /dev/null +++ b/python-double-underscore/passenger.py @@ -0,0 +1,7 @@ +class Passenger: + def __init__(self, name, class_, seat): + self.name = name + self.class_ = class_ + self.seat = seat + + # Implementation... diff --git a/python-double-underscore/point.py b/python-double-underscore/point.py new file mode 100644 index 0000000000..c9fb0261eb --- /dev/null +++ b/python-double-underscore/point.py @@ -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 diff --git a/python-double-underscore/shapes.py b/python-double-underscore/shapes.py new file mode 100644 index 0000000000..b92546885e --- /dev/null +++ b/python-double-underscore/shapes.py @@ -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