Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tombulled/num
Integer utility library
https://github.com/tombulled/num
Last synced: 23 days ago
JSON representation
Integer utility library
- Host: GitHub
- URL: https://github.com/tombulled/num
- Owner: tombulled
- License: mit
- Created: 2021-12-30T13:28:57.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-13T20:34:23.000Z (almost 3 years ago)
- Last Synced: 2023-03-05T18:53:57.686Z (almost 2 years ago)
- Language: Python
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# num
Integer utility library## Usage
### `sign`
Get the sign of an integer```python
>>> num.sign(1234)
1
>>> num.sign(-1234)
-1
>>> num.sign(0)
0
```### `length`
Count the number of digits in an integer```python
>>> num.length(1234)
4
>>> num.length(-1234)
-4
>>> num.length(0xdeadbeef, base=16)
8
```### `separate`
Separate the digits of an integer```python
>>> num.separate(1234)
[1, 2, 3, 4]
>>> num.separate(-1234)
[-1, -2, -3, -4]
>>> num.separate(0xdead, base=16)
[0xd, 0xe, 0xa, 0xd]
```### `join`
Join two integers```python
>>> num.join(12, 34)
1234
>>> num.join(-12, -34)
-1234
>>> num.join(0xde, 0xad, base=16)
0xdead
```### `join_all`
Join an iterable of integers into a single integer```python
>>> num.join_all([1, 2, 3, 4])
1234
>>> num.join_all([-1, -2, -3, -4])
-1234
>>> num.join_all([0xd, 0xe, 0xa, 0xd], base=16)
0xdead
```### `weight`
Weight an iterable of integers```python
>>> num.weight([1, 2, 3, 4])
[1, 20, 300, 4000]
>>> num.weight([-1, -2, -3, -4])
[-1, -20, -300, -4000]
>>> num.weight([0xa, 0xb, 0xc, 0xd], base=16)
[0xa, 0xb0, 0xc00, 0xd000]
```### `get`
Retrieve the integer at a specific index in an integer```python
>>> num.get(1234, 0)
1
>>> num.get(-1234, -1)
-4
```### `convert`
Convert an integer between bases```python
>>> num.convert(1011, to_base=2)
0b1011
>>> num.convert(0x789, from_base=16)
789
>>> num.convert(0x1011, from_base=16, to_base=2)
0b1011
```### `shift`
Shift an integer```python
>>> num.shift(123, amount=2)
12300
>>> num.shift(-123, amount=-2)
-1
>>> num.shift(0xabc, base=16)
0xabc0
>>> num.shift(123, amount=-1)
12
>>> num.shift(0xabc0, amount=-1, base=16)
0xabc
```### `positive`
Convert an integer to its positive form```python
>>> num.positive(1234)
1234
>>> num.positive(-1234)
1234
```### `negative`
Convert an integer to its negative form```python
>>> num.negative(1234)
-1234
>>> num.negative(-1234)
-1234
```### `toggle`
Toggle the sign of an integer```python
>>> num.toggle(1234)
-1234
>>> num.toggle(-1234)
1234
```