Skip to content

Commit

Permalink
Code Tidy
Browse files Browse the repository at this point in the history
* Code tidy
* Move min Python Version forward
  • Loading branch information
gabbpuy committed Jun 5, 2023
1 parent 3b4e719 commit bf0813a
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 115 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.pyc
.idea
build/
venv/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015, Andrew K. Milton
Copyright (c) 2015 - 2023, Andrew K. Milton
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
13 changes: 0 additions & 13 deletions quantity/PyQuantity.egg-info/PKG-INFO

This file was deleted.

14 changes: 0 additions & 14 deletions quantity/PyQuantity.egg-info/SOURCES.txt

This file was deleted.

1 change: 0 additions & 1 deletion quantity/PyQuantity.egg-info/dependency_links.txt

This file was deleted.

2 changes: 0 additions & 2 deletions quantity/PyQuantity.egg-info/top_level.txt

This file was deleted.

38 changes: 18 additions & 20 deletions quantity/bit_field/bit_field.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
# -*- 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 sub_field(self):
>>> return self[2:5]
>>> @sub_field.setter
>>> def sub_field(self, value):
>>> self[2:5] = value
>>> x = MyField(0x14)
>>> x.sub_field
5
>>> x.sub_field = 7
>>> x.sub_field
7
>>> hex(int(x))
0x1c
"""


class BitField:
"""
Represents an arbitrary length bit field as a sliceable value
>>> class MyField(BitField):
>>> @property
>>> def sub_field(self):
>>> return self[2:5]
>>> @sub_field.setter
>>> def sub_field(self, value):
>>> self[2:5] = value
>>> x = MyField(0x14)
>>> x.sub_field
5
>>> x.sub_field = 7
>>> x.sub_field
7
>>> hex(int(x))
0x1c
.. Note:
arbitrary means up to 1280 bits in this case
Expand All @@ -39,7 +37,7 @@ class BitField:
def __init__(self, value: int = 0):
self.__value = value

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

def __setitem__(self, index: Union[int, slice], value: int):
def __setitem__(self, index: int | slice, value: int):
"""
Set a single bit, 0 indexed
Expand Down
87 changes: 44 additions & 43 deletions quantity/prefix/prefix.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# -*- coding: utf-8 -*-
from typing import Union

from __future__ import annotations
import math
"""
Unit prefix library for units for powers of 10. Handles multiplication and division and
returns super scripts for attaching to displays.
"""
import math



class MetaPrefix(type):
Expand All @@ -19,42 +19,6 @@ def __call__(cls, *args, **kwargs):
return obj


def has_prefix(prefix: str) -> bool:
"""
Is the prefix in the cache?
:param prefix: Prefix
:return: presence of prefix
"""
return prefix in MetaPrefix.prefix_index


def has_power(power: int) -> bool:
"""
Is this power in the cache?
:param power: Power
:return: presence of power
"""
return power in MetaPrefix.power_index


def get_power(power: int) -> int:
"""
Get the cached power index
:param power:
:return: Cached Power
"""
return MetaPrefix.power_index[power]


def get_prefix(prefix: str) -> 'Prefix':
"""
Get the cached prefix object
:param prefix: prefix
:return: Cached prefix
"""
return MetaPrefix.prefix_index[prefix]


class Prefix(metaclass=MetaPrefix):
"""
A SI power of 10 prefix.
Expand Down Expand Up @@ -104,29 +68,29 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return f'{self.prefix}'

def __rmul__(self, o) -> Union[int, float]:
def __rmul__(self, o) -> int | float:
"""
Return a scalar multiplied by us.
e.g. 5 * kilo returns 5000
"""
return o * (10 ** self.power)

def __mul__(self, o) -> Union[int, float]:
def __mul__(self, o) -> int | float:
"""
Return a scalar multiplied by us.
e.g. 5 * kilo returns 5000
"""
return o * (10 ** self.power)

def __rtruediv__(self, o) -> Union[int, float]:
def __rtruediv__(self, o) -> int | float:
"""
Return a scalar divided by us.
e.g. 5000 / kilo returns 5
"""
return float(o) / (10 ** self.power)


def closest_prefix(i: Union[float, int]) -> tuple:
def closest_prefix(i: int | float) -> tuple:
"""
Reduce a number to a multiplier and a prefix.
Expand Down Expand Up @@ -159,3 +123,40 @@ def closest_prefix(i: Union[float, int]) -> tuple:
break
exponent = get_power(i)
return coefficient * mult, exponent


def has_prefix(prefix: str) -> bool:
"""
Is the prefix in the cache?
:param prefix: Prefix
:return: presence of prefix
"""
return prefix in MetaPrefix.prefix_index


def has_power(power: int) -> bool:
"""
Is this power in the cache?
:param power: Power
:return: presence of power
"""
return power in MetaPrefix.power_index


def get_power(power: int) -> int:
"""
Get the cached power index
:param power:
:return: Cached Power
"""
return MetaPrefix.power_index[power]


def get_prefix(prefix: str) -> Prefix:
"""
Get the cached prefix object
:param prefix: prefix
:return: Cached prefix
"""
return MetaPrefix.prefix_index[prefix]

32 changes: 16 additions & 16 deletions quantity/quantity/quantity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from typing import Union, Optional
from __future__ import annotations

from quantity.unit import Unit, get_unit, has_unit, NoUnit
from quantity.prefix import closest_prefix, has_prefix, get_prefix, Prefix
Expand Down Expand Up @@ -41,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] = NoUnit, prefix: Prefix = prefixes.NoPrefix):
def __init__(self, amount, unit: Unit | str = NoUnit, prefix: Prefix = prefixes.NoPrefix):
self.amount = amount
self.unit = unit
self.prefix = prefix
Expand All @@ -53,7 +53,7 @@ def __int__(self) -> int:
def __float__(self) -> float:
return float(self.amount * self.prefix)

def __add__(self, o) -> 'Quantity':
def __add__(self, o: int | float | Quantity) -> Quantity:
if isinstance(o, (int, float)) and o == 0:
return self

Expand All @@ -65,14 +65,14 @@ def __add__(self, o) -> 'Quantity':

__radd__ = __add__

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

unit = self.unit - o.unit
return Quantity(float(self) - float(o), unit)

def __mul__(self, o) -> 'Quantity':
def __mul__(self, o) -> Quantity:
if isinstance(o, Quantity):
unit = self.unit * o.unit
return Quantity(float(self) * float(o), unit)
Expand All @@ -82,7 +82,7 @@ def __mul__(self, o) -> 'Quantity':

__rmul__ = __mul__

def __truediv__(self, o) -> 'Quantity':
def __truediv__(self, o: int | float | Quantity) -> Quantity:
if isinstance(o, Quantity):
unit = self.unit / o.unit
return Quantity(float(self) / float(o), unit)
Expand All @@ -105,7 +105,7 @@ def _strip_unit(self):
"""
self.unit = NoUnit

def __find_unit(self):
def __find_unit(self) -> Unit:
"""
Take a string like kV and work out prefix and units, obviously
there is scope for collision between units and prefixes...
Expand Down Expand Up @@ -152,55 +152,55 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return f'{self.amount} {self.prefix}{self.unit}'

def __eq__(self, other) -> bool:
def __eq__(self, other: int | float| Quantity ) -> bool:
"""
Equivalence checking.
If we have no unit, then we will compare against scalars.
Otherwise everything else has to match
"""
if self.unit is NoUnit and isinstance(other, (int, float, int)):
if self.unit is NoUnit and isinstance(other, (int, float)):
return type(other)(self) == other

return (other.amount == self.amount and
other.unit is self.unit and
other.prefix is self.prefix)

def __ne__(self, other) -> bool:
def __ne__(self, other: int | float | Quantity) -> bool:
return not (other == self)

def __lt__(self, other) -> bool:
def __lt__(self, other: int | float | Quantity) -> bool:
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:
def __le__(self, other: int | float | Quantity) -> bool:
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:
def __gt__(self, other: int | float | Quantity) -> bool:
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:
def __ge__(self, other: int | float | Quantity) -> bool:
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 convert(self, unit) -> 'Quantity':
def convert(self, unit: Unit) -> Quantity:
return Quantity(self.unit.convert(unit, float(self)), unit)

def to(self, prefix: Union[Prefix, str]) -> Optional[float]:
def to(self, prefix: Prefix | str) -> float | None:
"""
Convert to different prefix (i.e. seconds to ms) with no units e.g.
Expand Down
Loading

0 comments on commit bf0813a

Please sign in to comment.