Skip to content

Instantly share code, notes, and snippets.

@simecek
Last active July 15, 2024 13:30
Show Gist options
  • Save simecek/fbe0fa354d5771d6fc82c2a3dc80696a to your computer and use it in GitHub Desktop.
Save simecek/fbe0fa354d5771d6fc82c2a3dc80696a to your computer and use it in GitHub Desktop.
named-constructors.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNM0DTERJ3bULpoyNAiGCs3",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/simecek/fbe0fa354d5771d6fc82c2a3dc80696a/named-constructors.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "VZa0ONOw_dTs",
"outputId": "88d19998-90e6-4756-80dc-a7a04b0588fd"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"US Letter - Width: 215.89999999999998 mm, Height: 279.4 mm\n",
"ISO A4 - Width: 297.25 mm, Height: 210.25 mm\n",
"ISO A5 - Width: 297.25 mm, Height: 105.125 mm\n"
]
}
],
"source": [
"class PaperSize:\n",
" @classmethod\n",
" def iso_a4(cls):\n",
" return cls.iso_a(4)\n",
"\n",
" @classmethod\n",
" def iso_a5(cls):\n",
" return cls.iso_a(5)\n",
"\n",
" @classmethod\n",
" def iso_a(cls, number):\n",
" \"\"\"Construct ISO A series paper sizes.\"\"\"\n",
" base_width, base_height = 841, 1189 # Base dimensions for A0 size in mm\n",
" if number == 0:\n",
" width, height = base_width, base_height\n",
" else:\n",
" width, height = base_height / (2 ** ((number + 1) // 2)), base_width / (2 ** (number // 2))\n",
" return cls(width, height)\n",
"\n",
"\n",
" @classmethod\n",
" def us_letter(cls):\n",
" return cls.in_inches(8.5, 11)\n",
"\n",
" @classmethod\n",
" def in_inches(cls, width_inch, height_inch):\n",
" width_mm = width_inch * 25.4\n",
" height_mm = height_inch * 25.4\n",
" return cls(width_mm, height_mm)\n",
"\n",
" def __init__(self, width_mm, height_mm):\n",
" self.width_mm = width_mm\n",
" self.height_mm = height_mm\n",
"\n",
"# Creating an instance of PaperSize using the class methods\n",
"us_letter_paper = PaperSize.us_letter()\n",
"iso_a4_paper = PaperSize.iso_a4()\n",
"iso_a5_paper = PaperSize.iso_a5()\n",
"\n",
"# Accessing the attributes and printing them\n",
"print(f\"US Letter - Width: {us_letter_paper.width_mm} mm, Height: {us_letter_paper.height_mm} mm\")\n",
"print(f\"ISO A4 - Width: {iso_a4_paper.width_mm} mm, Height: {iso_a4_paper.height_mm} mm\")\n",
"print(f\"ISO A5 - Width: {iso_a5_paper.width_mm} mm, Height: {iso_a5_paper.height_mm} mm\")"
]
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "rfdsiRuXBeBl"
},
"execution_count": null,
"outputs": []
}
]
}
@sjvrijn
Copy link

sjvrijn commented Jul 15, 2024

width, height = base_height / (2 ** (number // 2)), base_width / (2 ** ((number + 1) // 2))

should have the number vs number + 1 swapped around, i.e.,

width, height = base_height / (2 ** ((number + 1) // 2)), base_width / (2 ** (number // 2))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment