Skip to content

Commit

Permalink
[akm] Clean Up
Browse files Browse the repository at this point in the history
* Add more typing information
* Tidy up imports
* PEP-8 naming
  • Loading branch information
gabbpuy committed Dec 5, 2021
1 parent 9a92749 commit a54992e
Show file tree
Hide file tree
Showing 21 changed files with 124 additions and 105 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ You can define your own units. We can get frames per second by defining frames.

```python
from quantity.unit import Unit
from quantity.quantity.quantity import Quantity
from quantity.quantity import Quantity

frame = Unit('f', "frame")
frames = Quantity(432000, "frame")
runningTime = Quantity(120 * 60, 's')
fps = frames / runningTime
running_time = Quantity(120 * 60, 's')
fps = frames / running_time
print(fps)
'60.0 f/s'
```
13 changes: 13 additions & 0 deletions quantity/PyQuantity.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Metadata-Version: 2.1
Name: PyQuantity
Version: 1.1
Summary: Quantities in Python
Home-page: UNKNOWN
Author: akm
Author-email: akm@unyx.net
License: BSD
Platform: UNKNOWN
License-File: LICENSE

UNKNOWN

14 changes: 14 additions & 0 deletions quantity/PyQuantity.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
LICENSE
README.md
setup.py
quantity/__init__.py
quantity/PyQuantity.egg-info/PKG-INFO
quantity/PyQuantity.egg-info/SOURCES.txt
quantity/PyQuantity.egg-info/dependency_links.txt
quantity/PyQuantity.egg-info/top_level.txt
quantity/quantity/__init__.py
quantity/quantity/quantity.py
test/test_prefix.py
test/test_quantity.py
test/test_quantity_config_parser.py
test/test_unit.py
1 change: 1 addition & 0 deletions quantity/PyQuantity.egg-info/dependency_links.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 2 additions & 0 deletions quantity/PyQuantity.egg-info/top_level.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

quantity
18 changes: 9 additions & 9 deletions quantity/bit_field/bit_field.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# -*- coding: utf-8 -*-
from typing import Union
"""
Numbers as BitField, this class is useful on its own, it is very useful as a superclass.
>>> class MyField(BitField):
>>> @property
>>> def subField(self):
>>> def sub_field(self):
>>> return self[2:5]
>>> @subField.setter
>>> def subField(self, value):
>>> @sub_field.setter
>>> def sub_field(self, value):
>>> self[2:5] = value
>>> x = MyField(0x14)
>>> x.subField
>>> x.sub_field
5
>>> x.subField = 7
>>> x.subField
>>> x.sub_field = 7
>>> x.sub_field
7
>>> hex(int(x))
0x1c
"""
__author__ = "akm"


class BitField:
Expand All @@ -39,7 +39,7 @@ class BitField:
def __init__(self, value: int = 0):
self.__value = value

def __getitem__(self, index: int) -> int:
def __getitem__(self, index: Union[int, slice]) -> int:
"""
Get a single bit, 0 indexed
Expand All @@ -49,7 +49,7 @@ def __getitem__(self, index: int) -> int:
return self.__getslice__(index)
return (self.__value >> index) & 1

def __setitem__(self, index: int, value: int):
def __setitem__(self, index: Union[int, slice], value: int):
"""
Set a single bit, 0 indexed
Expand Down
1 change: 0 additions & 1 deletion quantity/prefix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
__author__ = 'akm'
from .prefix import Prefix, has_power, has_prefix, get_power, get_prefix, closest_prefix
from . import prefixes
9 changes: 2 additions & 7 deletions quantity/prefix/prefix.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
__author__ = 'akm'

from typing import Union

"""
Expand Down Expand Up @@ -94,8 +92,8 @@ def generate_format(self, power: int) -> str:
elif power < 100:
t, u = divmod(power, 10)
return f'10{pPrefix}{self.supers[t]}{self.supers[u]}'
else:
return f'10^{pPrefix}{power}'

return f'10^{pPrefix}{power}'

def __repr__(self) -> str:
"""
Expand All @@ -104,9 +102,6 @@ def __repr__(self) -> str:
return self.fmt

def __str__(self) -> str:
"""
Ascii version
"""
return f'{self.prefix}'

def __rmul__(self, o) -> Union[int, float]:
Expand Down
1 change: 0 additions & 1 deletion quantity/prefix/prefixes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
__author__ = 'akm'
from .prefix import Prefix

# A flag to remove some of the prefixes we don't like to use
Expand Down
28 changes: 12 additions & 16 deletions quantity/quantity/quantity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
# -*- coding: utf-8 -*-
__author__ = "akm"
from typing import Union, Optional

from quantity.unit.unit import Unit, get_unit, has_unit
from quantity.prefix.prefix import closest_prefix, has_prefix, get_prefix, Prefix
from quantity.unit import Unit, get_unit, has_unit, NoUnit
from quantity.prefix import closest_prefix, has_prefix, get_prefix, Prefix
import quantity.prefix.prefixes as prefixes
import quantity.unit.units as units

NO_UNIT = units.NoUnit


class Quantity:
Expand Down Expand Up @@ -45,7 +41,7 @@ class Quantity:
:param prefix: A SI power prefix to be applied to the amount.
"""

def __init__(self, amount, unit: Union[Unit, str] = NO_UNIT, prefix: Prefix = prefixes.NoPrefix):
def __init__(self, amount, unit: Union[Unit, str] = NoUnit, prefix: Prefix = prefixes.NoPrefix):
self.amount = amount
self.unit = unit
self.prefix = prefix
Expand All @@ -61,7 +57,7 @@ def __add__(self, o) -> 'Quantity':
if isinstance(o, (int, float)) and o == 0:
return self

if self.unit is NO_UNIT and isinstance(o, (int, float)):
if self.unit is NoUnit and isinstance(o, (int, float)):
return Quantity(type(o)(self) + o)

unit = self.unit + o.unit
Expand All @@ -70,7 +66,7 @@ def __add__(self, o) -> 'Quantity':
__radd__ = __add__

def __sub__(self, o) -> 'Quantity':
if self.unit is NO_UNIT and isinstance(o, (int, float)):
if self.unit is NoUnit and isinstance(o, (int, float)):
return Quantity(type(o)(self) - o)

unit = self.unit - o.unit
Expand Down Expand Up @@ -107,7 +103,7 @@ def _strip_unit(self):
"""
Remove our unit type
"""
self.unit = NO_UNIT
self.unit = NoUnit

def __find_unit(self):
"""
Expand All @@ -117,7 +113,7 @@ def __find_unit(self):
:return: :mod:`Unit` object
"""
if not self.unit:
return NO_UNIT
return NoUnit

if isinstance(self.unit, Unit):
return self.unit
Expand Down Expand Up @@ -163,7 +159,7 @@ def __eq__(self, other) -> bool:
Otherwise everything else has to match
"""
if self.unit is NO_UNIT and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float, int)):
return type(other)(self) == other

return (other.amount == self.amount and
Expand All @@ -174,28 +170,28 @@ def __ne__(self, other) -> bool:
return not (other == self)

def __lt__(self, other) -> bool:
if self.unit is NO_UNIT and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float, int)):
return type(other)(self) > other

assert other.unit is self.unit and other.prefix is self.prefix
return self.amount < other.amount

def __le__(self, other) -> bool:
if self.unit is NO_UNIT and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float, int)):
return type(other)(self) <= other

assert other.unit is self.unit and other.prefix is self.prefix
return self.amount <= other.amount

def __gt__(self, other) -> bool:
if self.unit is NO_UNIT and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float, int)):
return type(other)(self) > other

assert other.unit is self.unit and other.prefix is self.prefix
return self.amount > other.amount

def __ge__(self, other) -> bool:
if self.unit is NO_UNIT and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float, int)):
return type(other)(self) >= other

assert other.unit is self.unit and other.prefix is self.prefix
Expand Down
68 changes: 34 additions & 34 deletions quantity/quantity_config_parser/quantity_config_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Union, Callable

from quantity.quantity import Quantity
import quantity.unit.units as Units
from quantity.unit import NoUnit

_hex = partial(int, base=16)

Expand All @@ -14,45 +14,18 @@ class QuantityConfigParser(ConfigParser):
A :mod:`SafeConfigParser` derivative that returns quantities
"""

@staticmethod
def __quantify(converter: Callable, value: Union[float, int], unit: str) -> Quantity:
"""
Return a type cast quantity
:param converter: A conversion callable (i.e. int, float)
:param value: The value (1, 1.0, 100)
:param unit: The SI quantifier ('s', 'km')
:returns: A :mod:`Quantity` object
"""
return Quantity(converter(value), unit)

def _splitSectionItem(self, section: str, option: str) -> tuple:
"""
Split a config value into value and unit
:param section: Config section
:param option: Config option
:return: :mod:`tuple` of value and unit
"""
s = self.get(section, option)
try:
value, unit = (x.strip() for x in s.split(None, 1))
except ValueError:
value = s
unit = Units.NoUnit
return value, unit

def getAs(self, section: str, option: str, converter: Callable) -> Quantity:
def get_as(self, section: str, option: str, converter: Callable) -> Quantity:
"""
Convert a section to a coerced Quantity
:param section: Config section
:param option: Config option
:param converter: callable that returns a numeric type from a string (e.g int, float )
:return: :mod:`Quantity`
"""
value, unit = self._splitSectionItem(section, option)
value, unit = self._split_section_item(section, option)
return self.__quantify(converter, value, unit)

def getint(self, section: str, option: str, converter=int) -> Quantity:
def getint(self, section: str, option: str, converter: Callable = int) -> Quantity:
"""
A convenience method which coerces the option in the specified
section to an integer :mod:`Quantity`
Expand All @@ -62,7 +35,7 @@ def getint(self, section: str, option: str, converter=int) -> Quantity:
:param converter: a string to int callable
:return: :mod:`Quantity`
"""
return self.getAs(section, option, converter)
return self.get_as(section, option, converter)

def gethex(self, section: str, option: str, converter: Callable = _hex) -> Quantity:
"""
Expand All @@ -75,7 +48,7 @@ def gethex(self, section: str, option: str, converter: Callable = _hex) -> Quant
:param converter: a hex string to int callable
:return: :mod:`Quantity`
"""
return self.getAs(section, option, converter)
return self.get_as(section, option, converter)

def getfloat(self, section: str, option: str, converter: Callable = float) -> Quantity:
"""
Expand All @@ -87,4 +60,31 @@ def getfloat(self, section: str, option: str, converter: Callable = float) -> Qu
:param converter: a string to float callable
:return: :mod:`Quantity`
"""
return self.getAs(section, option, converter)
return self.get_as(section, option, converter)

def _split_section_item(self, section: str, option: str) -> tuple:
"""
Split a config value into value and unit
:param section: Config section
:param option: Config option
:return: :mod:`tuple` of value and unit
"""
s = self.get(section, option)
try:
value, unit = (x.strip() for x in s.split(None, 1))
except ValueError:
value = s
unit = NoUnit
return value, unit

@staticmethod
def __quantify(converter: Callable, value: Union[float, int], unit: str) -> Quantity:
"""
Return a type cast quantity
:param converter: A conversion callable (i.e. int, float)
:param value: The value (1, 1.0, 100)
:param unit: The SI quantifier ('s', 'km')
:returns: A :mod:`Quantity` object
"""
return Quantity(converter(value), unit)
3 changes: 2 additions & 1 deletion quantity/unit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
__author__ = 'akm'
from .unit import Unit, has_combined_unit, has_conversion, has_divided_unit, has_unit, get_conversion, get_unit, get_units
from .unit import (Unit, has_combined_unit, has_conversion, has_divided_unit, has_unit, get_conversion, get_unit,
get_units, NoUnit)
from . import units
Loading

0 comments on commit a54992e

Please sign in to comment.