From 0c7c59d1b40c66c8dd226347ef9b8fb7e498e259 Mon Sep 17 00:00:00 2001 From: Bazif Rasool Date: Sun, 30 Jul 2023 19:06:57 +0530 Subject: [PATCH 01/16] Added Altitude Pressure equation --- .vscode/settings.json | 4 +++- physics/altitude_pressure.py | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 physics/altitude_pressure.py diff --git a/.vscode/settings.json b/.vscode/settings.json index ef16fa1aa7ac..a7a36f481080 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,7 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ] + ], + "python.linting.pylintEnabled": true, + "python.linting.enabled": true } diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py new file mode 100644 index 000000000000..d8a7df922704 --- /dev/null +++ b/physics/altitude_pressure.py @@ -0,0 +1,40 @@ +""" +Title : Calculate altitude using Pressure + +Description : + The below algorithm approximates the altitute using Barometric formula + + +""" + + +def get_altitude_at_pressure(pressure: float) -> float: + """ + This method calculates the altitude from Pressure wrt to + Sea level pressure as reference .Pressure is in Pascals + https://en.wikipedia.org/wiki/Pressure_altitude + https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 + + H = 44330 * [1 - (P/p0)^(1/5.255) ] + + Where : + H = altitude (m) + P = measured pressure + p0 = reference pressure at sea level 101325 Pa + + Examples: + + >>> get_altitude_at_pressure(pressure=100000) + 105.47836610778828 + """ + + if pressure > 101325: + raise ValueError("Value Higer than Pressure at Sea Level !") + + return 44330 * (1-(pressure/101325)**(1/5.5255)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From a6a2ef726a4f8371b13fd69d7426050e48ed1107 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Jul 2023 13:40:22 +0000 Subject: [PATCH 02/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/altitude_pressure.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index d8a7df922704..b288c61db3d5 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -10,13 +10,13 @@ def get_altitude_at_pressure(pressure: float) -> float: """ - This method calculates the altitude from Pressure wrt to + This method calculates the altitude from Pressure wrt to Sea level pressure as reference .Pressure is in Pascals https://en.wikipedia.org/wiki/Pressure_altitude https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 - + H = 44330 * [1 - (P/p0)^(1/5.255) ] - + Where : H = altitude (m) P = measured pressure @@ -31,7 +31,7 @@ def get_altitude_at_pressure(pressure: float) -> float: if pressure > 101325: raise ValueError("Value Higer than Pressure at Sea Level !") - return 44330 * (1-(pressure/101325)**(1/5.5255)) + return 44330 * (1 - (pressure / 101325) ** (1 / 5.5255)) if __name__ == "__main__": From 8637b46b4336efbc7076078582f3f6d618613753 Mon Sep 17 00:00:00 2001 From: Bazif Rasool Date: Sun, 30 Jul 2023 19:11:53 +0530 Subject: [PATCH 03/16] Removed trailing whitespaces --- physics/altitude_pressure.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index d8a7df922704..4fac23d96e60 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -10,13 +10,13 @@ def get_altitude_at_pressure(pressure: float) -> float: """ - This method calculates the altitude from Pressure wrt to + This method calculates the altitude from Pressure wrt to Sea level pressure as reference .Pressure is in Pascals https://en.wikipedia.org/wiki/Pressure_altitude https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 - + H = 44330 * [1 - (P/p0)^(1/5.255) ] - + Where : H = altitude (m) P = measured pressure From 83397b0de8016befe06d2ba2c23deb166222f1f5 Mon Sep 17 00:00:00 2001 From: Bazif Rasool Date: Sun, 30 Jul 2023 19:15:56 +0530 Subject: [PATCH 04/16] Removed pylint --- physics/altitude_pressure.py | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 physics/altitude_pressure.py diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py new file mode 100644 index 000000000000..b288c61db3d5 --- /dev/null +++ b/physics/altitude_pressure.py @@ -0,0 +1,40 @@ +""" +Title : Calculate altitude using Pressure + +Description : + The below algorithm approximates the altitute using Barometric formula + + +""" + + +def get_altitude_at_pressure(pressure: float) -> float: + """ + This method calculates the altitude from Pressure wrt to + Sea level pressure as reference .Pressure is in Pascals + https://en.wikipedia.org/wiki/Pressure_altitude + https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702 + + H = 44330 * [1 - (P/p0)^(1/5.255) ] + + Where : + H = altitude (m) + P = measured pressure + p0 = reference pressure at sea level 101325 Pa + + Examples: + + >>> get_altitude_at_pressure(pressure=100000) + 105.47836610778828 + """ + + if pressure > 101325: + raise ValueError("Value Higer than Pressure at Sea Level !") + + return 44330 * (1 - (pressure / 101325) ** (1 / 5.5255)) + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 87e18da678cee30470deb0102133e6030d63efab Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 15:46:59 +0200 Subject: [PATCH 05/16] Fix lru_cache_pythonic.py --- other/lru_cache_pythonic.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py index 425691ef18cf..98fefa3af55d 100644 --- a/other/lru_cache_pythonic.py +++ b/other/lru_cache_pythonic.py @@ -1,16 +1,17 @@ """ This implementation of LRU Cache uses the in-built Python dictionary (dict) which from -Python 3.6 onward maintain the insertion order of keys and ensures O(1) operations on -insert, delete and access. https://docs.python.org/3/library/stdtypes.html#typesmapping +Python 3.6 onward maintains the insertion order of keys and ensures O(1) operations on +insert, delete, and access. https://docs.python.org/3/library/stdtypes.html#typesmapping """ -from typing import Any, Hashable +from collections.abc import Hashable +from typing import Any class LRUCache(dict): def __init__(self, capacity: int) -> None: """ - Initialize an LRU Cache with given capacity. - capacity : int -> the capacity of the LRU Cache + Initialize an LRU Cache with a given capacity. + capacity: the capacity of the LRU Cache >>> cache = LRUCache(2) >>> cache {} @@ -20,7 +21,7 @@ def __init__(self, capacity: int) -> None: def get(self, key: Hashable) -> Any: """ This method returns the value associated with the key. - key : A hashable object that is mapped to a value in the LRU cache. + key: A hashable object that is mapped to a value in the LRU cache. return -> Any object that has been stored as a value in the LRU cache. >>> cache = LRUCache(2) @@ -33,7 +34,8 @@ def get(self, key: Hashable) -> Any: KeyError: '2 not found.' """ if key not in self: - raise KeyError(f"{key} not found.") + msg = f"{key} not found." + raise KeyError(msg) val = self.pop(key) # Pop the key-value and re-insert to maintain the order self[key] = val return val @@ -41,7 +43,7 @@ def get(self, key: Hashable) -> Any: def put(self, key: Hashable, value: Any) -> None: """ This method puts the value associated with the key provided in the LRU cache. - key : A hashable object that is mapped to a value in the LRU cache. + key: A hashable object that is mapped to a value in the LRU cache. value: Any object that is to be associated with the key in the LRU cache. >>> cache = LRUCache(2) >>> cache.put(3,3) From 1ef703a8dd0a8ff2ca2fc36555c810372b6fa5c1 Mon Sep 17 00:00:00 2001 From: Bazif Rasool Date: Sun, 30 Jul 2023 19:18:44 +0530 Subject: [PATCH 06/16] Fixed spellings --- physics/altitude_pressure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index b288c61db3d5..76f25b1b10e6 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -2,7 +2,7 @@ Title : Calculate altitude using Pressure Description : - The below algorithm approximates the altitute using Barometric formula + The below algorithm approximates the altitude using Barometric formula """ @@ -29,7 +29,7 @@ def get_altitude_at_pressure(pressure: float) -> float: """ if pressure > 101325: - raise ValueError("Value Higer than Pressure at Sea Level !") + raise ValueError("Value Higher than Pressure at Sea Level !") return 44330 * (1 - (pressure / 101325) ** (1 / 5.5255)) From 2a9a4fc8c2b9fe7b1570ec49c54c9d6274c06de4 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 15:55:26 +0200 Subject: [PATCH 07/16] Fix again lru_cache_pythonic.py --- other/lru_cache_pythonic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py index 98fefa3af55d..9149c5f60100 100644 --- a/other/lru_cache_pythonic.py +++ b/other/lru_cache_pythonic.py @@ -18,7 +18,7 @@ def __init__(self, capacity: int) -> None: """ self.remaining: int = capacity - def get(self, key: Hashable) -> Any: + def get(self, key: Hashable: Any) -> Any | None: """ This method returns the value associated with the key. key: A hashable object that is mapped to a value in the LRU cache. From a80c46d211ea1870c252eafade1615207cf49fb8 Mon Sep 17 00:00:00 2001 From: Bazif Rasool <45148731+Bazifrasool@users.noreply.github.com> Date: Sun, 30 Jul 2023 19:27:33 +0530 Subject: [PATCH 08/16] Update .vscode/settings.json Co-authored-by: Christian Clauss --- .vscode/settings.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index a7a36f481080..9f1c0e94499f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,6 @@ { "githubPullRequests.ignoredPullRequestBranches": [ "master" - ], - "python.linting.pylintEnabled": true, - "python.linting.enabled": true + ] + } From dbda3284966547e9b84403fde6ee4dd85e50b8a2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 15:58:29 +0200 Subject: [PATCH 09/16] Third fix lru_cache_pythonic.py --- other/lru_cache_pythonic.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py index 9149c5f60100..7f178fb7d6b4 100644 --- a/other/lru_cache_pythonic.py +++ b/other/lru_cache_pythonic.py @@ -3,7 +3,6 @@ Python 3.6 onward maintains the insertion order of keys and ensures O(1) operations on insert, delete, and access. https://docs.python.org/3/library/stdtypes.html#typesmapping """ -from collections.abc import Hashable from typing import Any @@ -18,7 +17,7 @@ def __init__(self, capacity: int) -> None: """ self.remaining: int = capacity - def get(self, key: Hashable: Any) -> Any | None: + def get(self, key: Any) -> Any | None: """ This method returns the value associated with the key. key: A hashable object that is mapped to a value in the LRU cache. From eeb3186964f5429e28769fb9c330171a85868053 Mon Sep 17 00:00:00 2001 From: Bazif Rasool <45148731+Bazifrasool@users.noreply.github.com> Date: Sun, 30 Jul 2023 19:29:46 +0530 Subject: [PATCH 10/16] Update .vscode/settings.json Co-authored-by: Christian Clauss --- .vscode/settings.json | 1 - 1 file changed, 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 9f1c0e94499f..ef16fa1aa7ac 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,4 @@ "githubPullRequests.ignoredPullRequestBranches": [ "master" ] - } From ef1e63d96b5445e5f75a5f0bf3b38ebd7e37f562 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 16:01:21 +0200 Subject: [PATCH 11/16] 4th fix lru_cache_pythonic.py --- other/lru_cache_pythonic.py | 1 + 1 file changed, 1 insertion(+) diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py index 7f178fb7d6b4..d86643f2b8bf 100644 --- a/other/lru_cache_pythonic.py +++ b/other/lru_cache_pythonic.py @@ -3,6 +3,7 @@ Python 3.6 onward maintains the insertion order of keys and ensures O(1) operations on insert, delete, and access. https://docs.python.org/3/library/stdtypes.html#typesmapping """ +from collections.abc import Hashable from typing import Any From b88c7554e4d7ace296784b962138d55ae5c63c8f Mon Sep 17 00:00:00 2001 From: Bazif Rasool <45148731+Bazifrasool@users.noreply.github.com> Date: Sun, 30 Jul 2023 19:31:42 +0530 Subject: [PATCH 12/16] Update physics/altitude_pressure.py Co-authored-by: Christian Clauss --- physics/altitude_pressure.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index 76f25b1b10e6..9d80241a9205 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -31,7 +31,7 @@ def get_altitude_at_pressure(pressure: float) -> float: if pressure > 101325: raise ValueError("Value Higher than Pressure at Sea Level !") - return 44330 * (1 - (pressure / 101325) ** (1 / 5.5255)) + return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255)) if __name__ == "__main__": From 8bbf349abb9af1d891988d3462b381c47711145b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 16:17:03 +0200 Subject: [PATCH 13/16] lru_cache_pythonic.py: def get(self, key: Any, /) -> Any | None: --- other/lru_cache_pythonic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py index d86643f2b8bf..fecb173a576c 100644 --- a/other/lru_cache_pythonic.py +++ b/other/lru_cache_pythonic.py @@ -18,7 +18,7 @@ def __init__(self, capacity: int) -> None: """ self.remaining: int = capacity - def get(self, key: Any) -> Any | None: + def get(self, key: Any, /) -> Any | None: """ This method returns the value associated with the key. key: A hashable object that is mapped to a value in the LRU cache. From dbb6803ea6b37ba777e3849ee01923b2f96ab4f6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Jul 2023 16:18:44 +0200 Subject: [PATCH 14/16] Delete lru_cache_pythonic.py --- other/lru_cache_pythonic.py | 115 ------------------------------------ 1 file changed, 115 deletions(-) delete mode 100644 other/lru_cache_pythonic.py diff --git a/other/lru_cache_pythonic.py b/other/lru_cache_pythonic.py deleted file mode 100644 index fecb173a576c..000000000000 --- a/other/lru_cache_pythonic.py +++ /dev/null @@ -1,115 +0,0 @@ -""" -This implementation of LRU Cache uses the in-built Python dictionary (dict) which from -Python 3.6 onward maintains the insertion order of keys and ensures O(1) operations on -insert, delete, and access. https://docs.python.org/3/library/stdtypes.html#typesmapping -""" -from collections.abc import Hashable -from typing import Any - - -class LRUCache(dict): - def __init__(self, capacity: int) -> None: - """ - Initialize an LRU Cache with a given capacity. - capacity: the capacity of the LRU Cache - >>> cache = LRUCache(2) - >>> cache - {} - """ - self.remaining: int = capacity - - def get(self, key: Any, /) -> Any | None: - """ - This method returns the value associated with the key. - key: A hashable object that is mapped to a value in the LRU cache. - return -> Any object that has been stored as a value in the LRU cache. - - >>> cache = LRUCache(2) - >>> cache.put(1,1) - >>> cache.get(1) - 1 - >>> cache.get(2) - Traceback (most recent call last): - ... - KeyError: '2 not found.' - """ - if key not in self: - msg = f"{key} not found." - raise KeyError(msg) - val = self.pop(key) # Pop the key-value and re-insert to maintain the order - self[key] = val - return val - - def put(self, key: Hashable, value: Any) -> None: - """ - This method puts the value associated with the key provided in the LRU cache. - key: A hashable object that is mapped to a value in the LRU cache. - value: Any object that is to be associated with the key in the LRU cache. - >>> cache = LRUCache(2) - >>> cache.put(3,3) - >>> cache - {3: 3} - >>> cache.put(2,2) - >>> cache - {3: 3, 2: 2} - """ - # To pop the last value inside of the LRU cache - if key in self: - self.pop(key) - self[key] = value - return - - if self.remaining > 0: - self.remaining -= 1 - # To pop the least recently used item from the dictionary - else: - self.pop(next(iter(self))) - self[key] = value - - -def main() -> None: - """Example test case with LRU_Cache of size 2 - >>> main() - 1 - Key=2 not found in cache - Key=1 not found in cache - 3 - 4 - """ - cache = LRUCache(2) # Creates an LRU cache with size 2 - cache.put(1, 1) # cache = {1:1} - cache.put(2, 2) # cache = {1:1, 2:2} - try: - print(cache.get(1)) # Prints 1 - except KeyError: - print("Key not found in cache") - cache.put( - 3, 3 - ) # cache = {1:1, 3:3} key=2 is evicted because it wasn't used recently - try: - print(cache.get(2)) - except KeyError: - print("Key=2 not found in cache") # Prints key not found - cache.put( - 4, 4 - ) # cache = {4:4, 3:3} key=1 is evicted because it wasn't used recently - try: - print(cache.get(1)) - except KeyError: - print("Key=1 not found in cache") # Prints key not found - try: - print(cache.get(3)) # Prints value 3 - except KeyError: - print("Key not found in cache") - - try: - print(cache.get(4)) # Prints value 4 - except KeyError: - print("Key not found in cache") - - -if __name__ == "__main__": - import doctest - - doctest.testmod() - main() From 3f665985ea59c3461e3a22371dad045651670fcf Mon Sep 17 00:00:00 2001 From: Bazif Rasool Date: Sun, 30 Jul 2023 20:14:05 +0530 Subject: [PATCH 15/16] Added positive and negative pressure test cases --- physics/altitude_pressure.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index 9d80241a9205..40c96137208b 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -23,14 +23,26 @@ def get_altitude_at_pressure(pressure: float) -> float: p0 = reference pressure at sea level 101325 Pa Examples: - - >>> get_altitude_at_pressure(pressure=100000) + >>> get_altitude_at_pressure(pressure=100_000) 105.47836610778828 + >>> get_altitude_at_pressure(pressure=101_325) + 0.0 + >>> get_altitude_at_pressure(pressure=80_000) + 1855.873388064995 + >>> get_altitude_at_pressure(pressure=201_325) + Traceback (most recent call last): + ... + ValueError: Value Higher than Pressure at Sea Level ! + >>> get_altitude_at_pressure(pressure=-80_000) + Traceback (most recent call last): + ... + ValueError: Atmospheric Pressure can not be negative ! """ if pressure > 101325: raise ValueError("Value Higher than Pressure at Sea Level !") - + if pressure < 0 : + raise ValueError("Atmospheric Pressure can not be negative !") return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255)) @@ -38,3 +50,5 @@ def get_altitude_at_pressure(pressure: float) -> float: import doctest doctest.testmod() + + From e7e7f3abcff9517d0e94492d28e9f82a029cea36 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 30 Jul 2023 14:45:08 +0000 Subject: [PATCH 16/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/altitude_pressure.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/physics/altitude_pressure.py b/physics/altitude_pressure.py index 40c96137208b..65307d223fa7 100644 --- a/physics/altitude_pressure.py +++ b/physics/altitude_pressure.py @@ -41,7 +41,7 @@ def get_altitude_at_pressure(pressure: float) -> float: if pressure > 101325: raise ValueError("Value Higher than Pressure at Sea Level !") - if pressure < 0 : + if pressure < 0: raise ValueError("Atmospheric Pressure can not be negative !") return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255)) @@ -50,5 +50,3 @@ def get_altitude_at_pressure(pressure: float) -> float: import doctest doctest.testmod() - -