{"id":21492134,"url":"https://github.com/colddsam/addition-subtraction-package","last_synced_at":"2025-03-17T11:23:26.302Z","repository":{"id":218507951,"uuid":"746582010","full_name":"colddsam/Addition-Subtraction-Package","owner":"colddsam","description":"With the `AddSub` class, you can switch between addition and subtraction operations dynamically, providing flexibility for various scenarios requiring different operations.","archived":false,"fork":false,"pushed_at":"2024-01-22T09:59:06.000Z","size":31,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-23T18:14:36.313Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/colddsam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-22T09:49:50.000Z","updated_at":"2024-01-22T09:53:07.000Z","dependencies_parsed_at":null,"dependency_job_id":"2cf2e851-1c2c-43da-a355-32ba8096eb5c","html_url":"https://github.com/colddsam/Addition-Subtraction-Package","commit_stats":null,"previous_names":["colddsam/addition-subtraction-package"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colddsam%2FAddition-Subtraction-Package","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colddsam%2FAddition-Subtraction-Package/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colddsam%2FAddition-Subtraction-Package/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/colddsam%2FAddition-Subtraction-Package/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/colddsam","download_url":"https://codeload.github.com/colddsam/Addition-Subtraction-Package/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244022636,"owners_count":20385133,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T15:22:48.410Z","updated_at":"2025-03-17T11:23:26.250Z","avatar_url":"https://github.com/colddsam.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":" # Python Class for Dynamically Switching Between Addition and Subtraction Operations: AddSub\n\n---\n\n## Introduction\n\nIn this tutorial, we will create a Python class named `AddSub` that allows us to dynamically switch between addition and subtraction operations. This class provides a flexible way to perform different operations on numbers based on a specified method.\n\n## Code Implementation\n\n```python\nclass AddSub:\n    def __init__(self, method_val: int = 1) -\u003e None:\n        \"\"\"\n        This is the constructor of the AddSub class.\n        It takes an optional parameter method_val, which specifies the default method to be used for operations.\n        If method_val is not an integer, it is set to 1 (addition) by default.\n        If method_val is less than 0 or greater than 1, it is also set to 1 by default.\n        \"\"\"\n\n        if type(method_val) != type(1):\n            self.method = 1\n            print('only integer elements accepted')\n        elif ((method_val \u003c= 1) and (method_val \u003e= 0)):\n            self.method = method_val\n        else:\n            self.method = 1\n            print(\"Please enter a valid method by default addition operation has set\")\n\n    def operation(self, first_number: int = 0, second_number: int = 0) -\u003e int:\n        \"\"\"\n        This method performs the operation specified by the method attribute on the two given numbers.\n        If the method attribute is 0, subtraction is performed.\n        If the method attribute is 1, addition is performed.\n        \"\"\"\n\n        if (self.method == 0):\n            return first_number - second_number\n        elif (self.method == 1):\n            return first_number + second_number\n\n    def change_method(self, method_val: int) -\u003e None:\n        \"\"\"\n        This method changes the method attribute to the given value.\n        If the given value is not an integer, the method attribute is not changed.\n        If the given value is less than 0 or greater than 1, the method attribute is also not changed.\n        \"\"\"\n\n        if (type(method_val) != type(1)):\n            print('only integer elements accepted, previous method has unchanged')\n        elif ((method_val \u003c= 1) and (method_val \u003e= 0)):\n            self.method = method_val\n        else:\n            print(\"Please enter a valid method, previous method has unchanged\")\n\n\n# Usage:\n\n# Creating an instance of the AddSub class\nadd_sub = AddSub()\n\n# Performing addition using the default method\nresult = add_sub.operation(10, 20)\nprint(\"Addition result:\", result)  # Output: 30\n\n# Changing the method to subtraction\nadd_sub.change_method(0)\n\n# Performing subtraction using the new method\nresult = add_sub.operation(20, 10)\nprint(\"Subtraction result:\", result)  # Output: 10\n\n# Changing the method to an invalid value\nadd_sub.change_method(2)\n\n# Performing addition using the previous method (addition)\nresult = add_sub.operation(100, 200)\nprint(\"Addition result (using previous method):\", result)  # Output: 300\n```\n\n## Explanation\n\n1. The `__init__` method is the constructor of the `AddSub` class. It takes an optional parameter `method_val` that specifies the default method to be used for operations.\n2. In the `__init__` method, we check if `method_val` is an integer. If it is not, we print an error message and set `method` to 1 (addition).\n3. We also check if `method_val` is within the valid range of 0 (subtraction) and 1 (addition). If it is not, we print an error message and set `method` to 1.\n4. The `operation` method takes two integer parameters `first_number` and `second_number` and performs the operation specified by the `method` attribute on the two given numbers.\n5. The `change_method` method takes an integer parameter `method_val` and changes the `method` attribute to the given value, provided that it is a valid value.\n6. In the usage section, we create an instance of the `AddSub` class and perform addition and subtraction operations using the default method.\n7. We then change the method to subtraction using the `change_method` method and perform a subtraction operation.\n8. We try to change the method to an invalid value, but the `change_method` method prevents this and prints an error message.\n9. Finally, we perform an addition operation using the previous method (addition) and demonstrate that the method has not changed.\n\n## Conclusion\n\nThe `AddSub` class provides a flexible way to dynamically switch between addition and subtraction operations. This can be useful in various scenarios where you need to perform different operations based on certain conditions or user input.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolddsam%2Faddition-subtraction-package","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcolddsam%2Faddition-subtraction-package","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcolddsam%2Faddition-subtraction-package/lists"}