Added Altitude Pressure equation (#8909)

* Added Altitude Pressure equation

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Removed trailing whitespaces

* Removed pylint

* Fix lru_cache_pythonic.py

* Fixed spellings

* Fix again lru_cache_pythonic.py

* Update .vscode/settings.json

Co-authored-by: Christian Clauss <cclauss@me.com>

* Third fix lru_cache_pythonic.py

* Update .vscode/settings.json

Co-authored-by: Christian Clauss <cclauss@me.com>

* 4th fix lru_cache_pythonic.py

* Update physics/altitude_pressure.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* lru_cache_pythonic.py: def get(self, key: Any, /) -> Any | None:

* Delete lru_cache_pythonic.py

* Added positive and negative pressure test cases

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Bazif Rasool 2023-07-30 20:30:58 +05:30 committed by GitHub
parent d31750adec
commit 8b831cb600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 113 deletions

View File

@ -1,113 +0,0 @@
"""
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
"""
from typing import Any, Hashable
class LRUCache(dict):
def __init__(self, capacity: int) -> None:
"""
Initialize an LRU Cache with given capacity.
capacity : int -> the capacity of the LRU Cache
>>> cache = LRUCache(2)
>>> cache
{}
"""
self.remaining: int = capacity
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.
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:
raise KeyError(f"{key} not found.")
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()

View File

@ -0,0 +1,52 @@
"""
Title : Calculate altitude using Pressure
Description :
The below algorithm approximates the altitude 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=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))
if __name__ == "__main__":
import doctest
doctest.testmod()