Skip to content

Commit

Permalink
Sample code for the article on underscores in Python names
Browse files Browse the repository at this point in the history
  • Loading branch information
lpozo committed Oct 30, 2023
1 parent 0529090 commit 5e497d9
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions python-double-underscore/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Single and Double Underscores in Python Names: Their Meaning

This folder provides the code examples for the Real Python tutorial [Single and Double Underscores in Python Names: Their Meaning](https://realpython.com/python-double-underscore/).
39 changes: 39 additions & 0 deletions python-double-underscore/article_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import datetime


class Article:
def __init__(self, title, author, pub_date=None):
self.title = title
self.author = author
self.pub_date = self.update_pub_date(pub_date)

def update_pub_date(self, pub_date=None):
if pub_date is None:
date = datetime.datetime.now()
else:
date = datetime.datetime.fromisoformat(pub_date)
return date

def compute_age(self):
now = datetime.datetime.now()
return now - self.pub_date


class Article:
def __init__(self, title, author, pub_date=None):
self.title = title
self.author = author
self.pub_date = self.__update_pub_date(pub_date)

def update_pub_date(self, pub_date=None):
if pub_date is None:
date = datetime.datetime.now()
else:
date = datetime.datetime.fromisoformat(pub_date)
return date

__update_pub_date = update_pub_date

def compute_age(self):
now = datetime.datetime.now()
return now - self.pub_date
21 changes: 21 additions & 0 deletions python-double-underscore/article_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import datetime


class Article:
def __init__(self, title, author, pub_date=None):
self.title = title
self.author = author
self.pub_date = self.__update_pub_date(pub_date)

def update_pub_date(self, pub_date=None):
if pub_date is None:
date = datetime.datetime.now()
else:
date = datetime.datetime.fromisoformat(pub_date)
return date

__update_pub_date = update_pub_date

def compute_age(self):
now = datetime.datetime.now()
return now - self.pub_date
15 changes: 15 additions & 0 deletions python-double-underscore/cart.py
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...
15 changes: 15 additions & 0 deletions python-double-underscore/count.py
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
19 changes: 19 additions & 0 deletions python-double-underscore/csv_data.py
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
7 changes: 7 additions & 0 deletions python-double-underscore/passenger.py
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...
26 changes: 26 additions & 0 deletions python-double-underscore/point.py
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
23 changes: 23 additions & 0 deletions python-double-underscore/shapes.py
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
7 changes: 7 additions & 0 deletions python-double-underscore/tutorial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from article_v1 import Article


class Tutorial(Article):
def update_pub_date(self, pub_date=None):
date = super().update_pub_date(pub_date)
return date.strftime("%Y-%m-%d")

0 comments on commit 5e497d9

Please sign in to comment.