Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
theramkm committed Dec 3, 2024
0 parents commit 1570c3b
Show file tree
Hide file tree
Showing 6 changed files with 487 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Python Tests

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install pytest
- name: Run tests
run: |
pytest
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Virtual Environment
venv/
env/
ENV/

# IDE specific files
.idea/
.vscode/
*.swp
*.swo
.DS_Store

# Test coverage
.coverage
htmlcov/
.pytest_cache/
.ruff_cache/
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# AdvancedNumber Class Assignment

![Python Tests](https://github.com/<your-username>/<repo-name>/workflows/Python%20Tests/badge.svg)

## Overview
This project implements a custom numerical type in Python called `AdvancedNumber` that demonstrates the use of Python's special methods (magic methods) to create a rich, fully-featured numeric type.

## Features

### 1. Basic Operations
- Initialize with integer or float values
- Human-readable string representation
- Developer-focused representation

### 2. Arithmetic Operations
- Addition (`+`)
- Subtraction (`-`)
- Multiplication (`*`)
- Division (`/`)
- Modulus (`%`)
- Works with both `AdvancedNumber` objects and regular numbers

### 3. Comparison Operations
- Less than (`<`)
- Less than or equal to (`<=`)
- Greater than (`>`)
- Greater than or equal to (`>=`)
- Equal to (`==`)
- Not equal to (`!=`)

### 4. Advanced Features
- Hashable (can be used in sets and as dictionary keys)
- Boolean conversion
- Callable objects (returns square of the value)
- Custom string formatting
- Cleanup notification on object destruction

## Installation

1. Clone this repository:
```bash
git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>
```

2. Create a virtual environment (optional but recommended):
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```

3. Install dependencies:
```bash
pip install pytest
```

## Usage

```python
# Create AdvancedNumber objects
num1 = AdvancedNumber(5)
num2 = AdvancedNumber(3)

# Arithmetic operations
result = num1 + num2 # AdvancedNumber(8)
product = num1 * 2 # AdvancedNumber(10)

# Comparison
is_greater = num1 > num2 # True

# Callable behavior
squared = num1() # 25

# Custom formatting
formatted = f"{num1:.2f}" # "5.00"
hex_format = f"{num1:#x}" # "0x5"
```

## Testing

Run the tests using pytest:

```bash
pytest
```

## Project Structure

```
.
├── advanced_number.py # Main implementation
├── test_assignment.py # Test suite
├── .gitignore # Git ignore file
├── README.md # This file
└── .github/
└── workflows/
└── python-tests.yml # GitHub Actions workflow
```

## Requirements
- Python 3.9 or higher
- pytest (for running tests)

## Contributing
This is an assignment project. Please do not contribute directly.

152 changes: 152 additions & 0 deletions S21 Class Assignment.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Assignment Problem Statement:\n",
"You are tasked with creating a Python class named `AdvancedNumber` that models a custom numerical type with rich functionality using Python's special methods. \n",
"\n",
"#### Functional Requirements:\n",
"1. **Initialization and String Representation**:\n",
" - Initialize an `AdvancedNumber` object with a single integer or float value.\n",
" - Implement `__str__` and `__repr__` to return human-readable and developer-focused representations, respectively.\n",
"\n",
"2. **Arithmetic Operators**:\n",
" - Overload `+`, `-`, `*`, `/`, and `%` to work with another `AdvancedNumber` or a plain integer/float.\n",
"\n",
"3. **Comparison Operators**:\n",
" - Overload `<`, `<=`, `>`, `>=`, `==`, and `!=` to compare the stored value with another `AdvancedNumber` or plain numbers.\n",
"\n",
"4. **Hashing and Boolean Conversion**:\n",
" - Make the `AdvancedNumber` objects hashable and ensure they work in sets and as dictionary keys.\n",
" - Convert an `AdvancedNumber` to a boolean (`True` if non-zero, `False` otherwise).\n",
"\n",
"5. **Callable Behavior**:\n",
" - Make the object callable. When called, it should return the stored value squared.\n",
"\n",
"6. **Custom Formatting**:\n",
" - Support custom string formatting using the `__format__` method:\n",
" - `{obj:.2f}` should format the number with two decimal places.\n",
" - `{obj:#x}` should return the hexadecimal representation if the number is an integer.\n",
"\n",
"7. **Destructor**:\n",
" - Print a message like `\"AdvancedNumber with value <value> is being destroyed\"` when an object is deleted.\n",
"\n",
"---\n",
"\n",
"### Provided `pytest` File\n",
"```python\n",
"import pytest\n",
"from advanced_number import AdvancedNumber\n",
"\n",
"def test_initialization():\n",
" obj = AdvancedNumber(5)\n",
" assert obj.value == 5\n",
"\n",
"def test_str_repr():\n",
" obj = AdvancedNumber(5)\n",
" assert str(obj) == \"Value: 5\"\n",
" assert repr(obj) == \"AdvancedNumber(5)\"\n",
"\n",
"def test_arithmetic_operations():\n",
" obj1 = AdvancedNumber(10)\n",
" obj2 = AdvancedNumber(5)\n",
" assert (obj1 + obj2).value == 15\n",
" assert (obj1 - obj2).value == 5\n",
" assert (obj1 * 2).value == 20\n",
" assert (obj1 / obj2).value == 2.0\n",
" assert (obj1 % obj2).value == 0\n",
"\n",
"def test_comparison_operations():\n",
" obj1 = AdvancedNumber(10)\n",
" obj2 = AdvancedNumber(5)\n",
" assert obj1 > obj2\n",
" assert obj2 < obj1\n",
" assert obj1 != obj2\n",
" assert obj1 >= AdvancedNumber(10)\n",
" assert obj2 <= AdvancedNumber(5)\n",
"\n",
"def test_hashable():\n",
" obj1 = AdvancedNumber(10)\n",
" obj2 = AdvancedNumber(10)\n",
" obj_set = {obj1, obj2}\n",
" assert len(obj_set) == 1\n",
"\n",
"def test_boolean_conversion():\n",
" assert bool(AdvancedNumber(10)) is True\n",
" assert bool(AdvancedNumber(0)) is False\n",
"\n",
"def test_callable():\n",
" obj = AdvancedNumber(4)\n",
" assert obj() == 16\n",
"\n",
"def test_custom_formatting():\n",
" obj = AdvancedNumber(10)\n",
" assert format(obj, \".2f\") == \"10.00\"\n",
" assert format(obj, \"#x\") == \"0xa\"\n",
"\n",
"def test_destruction(capsys):\n",
" obj = AdvancedNumber(5)\n",
" del obj\n",
" captured = capsys.readouterr()\n",
" assert \"AdvancedNumber with value 5 is being destroyed\" in captured.out\n",
"```\n",
"\n",
"---\n",
"\n",
"### GitHub Actions Workflow File (`.github/workflows/python-tests.yml`)\n",
"```yaml\n",
"name: Python Tests\n",
"\n",
"on:\n",
" push:\n",
" branches:\n",
" - main\n",
" pull_request:\n",
" branches:\n",
" - main\n",
"\n",
"jobs:\n",
" test:\n",
" runs-on: ubuntu-latest\n",
"\n",
" steps:\n",
" - uses: actions/checkout@v3\n",
" - name: Set up Python\n",
" uses: actions/setup-python@v4\n",
" with:\n",
" python-version: '3.9'\n",
"\n",
" - name: Install dependencies\n",
" run: |\n",
" python -m pip install --upgrade pip\n",
" pip install pytest\n",
"\n",
" - name: Run tests\n",
" run: |\n",
" pytest\n",
"```\n",
"\n",
"### Instructions for Students:\n",
"1. Create a new repository on GitHub and clone it to your local machine.\n",
"2. Implement the `AdvancedNumber` class in a file named `advanced_number.py`.\n",
"3. Add the provided `pytest` file as `test_advanced_number.py`.\n",
"4. Add the provided GitHub Actions workflow file to `.github/workflows/`.\n",
"5. Push your implementation to the GitHub repository and verify your tests run automatically."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit 1570c3b

Please sign in to comment.