A simple Python OOP project that automatically assigns a unique, incrementing ID to every instance of a class — useful for tracking, debugging, and managing how many objects you have created.
InstanceCounter uses a class-level counter that increments each time a new object is created. Every instance gets a unique ID that follows the order in which it was instantiated. The counter can be reset at any time via a static method.It also includes unit tests to verify correct behaviour.
- Assigns a unique, automatically incrementing ID to each instance
- Supports resetting the class-level counter to zero
- Demonstrates Object-Oriented Programming (OOP) concept Encapsulation: the counter and ID logic are bundled inside the class, hidden from outside code.
- Demonstrates the difference between class variables (shared across all objects) and instance variables(unique to each object)
- Includes unit tests using Python’s unittest framework
- Supports Docker containerization
- Includes automated Docker build and run script
- Uses GitHub Actions for CI/CD automation
- Python 3.x
- Docker Desktop
- Bash-compatible terminal (Git Bash, WSL, or Linux terminal)
- No external libraries required (uses Python standard library only)
instance-counter/
│
├── .github/
│ └── workflows/
│ └── ci.yml # GitHub Actions CI/CD workflow
├── counter.py # Main class implementation
├── testcounter.py # Unit tests
├── Dockerfile # Docker container configuration
├── build.sh # Automated Docker build and run script
├── requirements.txt # Project requirements
└── README.md # Project documentation
Each time an InstanceCounter object is created, a class-level variable is incremented and assigned to the instance as its unique ID.
import counter
instance1 = counter.InstanceCounter()
instance2 = counter.InstanceCounter()
print(instance.id) # 1
print(instance.id) # 2
counter.InstanceCounter.reset()
instance3 = counter.InstanceCounter()
print(instance3.id) # 1git clone https://github.com/MakhutsoPoto/Instance_Counter.git
cd instance-counterpython counter.pypython test_counter.pyThis project can be built and executed inside a Docker container.
docker build -t instance-counter .docker run --rm instance-counterThe project employs shell script to automate the Docker build and execution process.
./build.shThe script:
- builds the Docker image
- runs the container
- removes the container after execution
This project uses GitHub Actions to automate:
- unit testing
- Python dependencies installs
- Docker image builds
The workflow runs automatically on every push and pull request to the main branch.
- Python 3
- Object-Oriented Programming (OOP)
- Unit Testing (unittest)
- Docker
- Bash Scripting
- Github Actions
This project demonstrates understanding of:
- The difference between class variables and instance variables,when to use each
- Object lifecycle in Python
- Object Oriented Programming principles:Encapsulation
- Basic unit testing practices with Python's unittest framework
- Docker containerization fundamentals
- Basic automation using Bash scripting
- CI/CD automation with GitHub Actions