eapyTool 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: eapyTool
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: A Python utility library for math, algebra, data structures, and everyday tools
5
5
  Project-URL: Homepage, https://github.com/eapy/eapy
6
6
  Project-URL: Repository, https://github.com/eapy/eapy
@@ -63,6 +63,9 @@ A Python utility library that extends the standard library with math, algebra, d
63
63
  - `Stack` — LIFO stack (push/pop/peek)
64
64
  - `Queue` — FIFO queue (enqueue/dequeue)
65
65
  - `Trie` — prefix tree (insert/search/words_with_prefix)
66
+ - `LinkedList` — singly linked list (append/prepend/remove/find)
67
+ - `CircularBuffer` — fixed-size circular buffer
68
+ - `PriorityQueue` — min-heap priority queue
66
69
 
67
70
  ### Base Conversion
68
71
  - `baseConvert(n, from_base, to_base)` — arbitrary base conversion (2-36)
@@ -107,6 +110,23 @@ A Python utility library that extends the standard library with math, algebra, d
107
110
  ### Random Utilities
108
111
  - `randomChoice`, `shuffle`, `randomString`, `randomInt`, `randomFloat` — enhanced random helpers
109
112
 
113
+ ### File I/O
114
+ - `readJson`, `writeJson` — JSON file helpers
115
+ - `readCsv`, `writeCsv` — CSV file helpers
116
+
117
+ ### Encoding
118
+ - `base64Encode`, `base64Decode` — Base64 encoding
119
+ - `urlEncode`, `urlDecode` — URL encoding
120
+
121
+ ### Math Extensions
122
+ - `factorial` — factorial
123
+ - `matrixInverse` — matrix inverse (Gauss-Jordan)
124
+ - `interpolate` — linear interpolation
125
+
126
+ ### Regex Utilities
127
+ - `extractEmails`, `extractURLs` — extract from text
128
+ - `replaceMultiple` — multi-pattern replacement
129
+
110
130
  ## Installation
111
131
 
112
132
  ```bash
@@ -40,6 +40,9 @@ A Python utility library that extends the standard library with math, algebra, d
40
40
  - `Stack` — LIFO stack (push/pop/peek)
41
41
  - `Queue` — FIFO queue (enqueue/dequeue)
42
42
  - `Trie` — prefix tree (insert/search/words_with_prefix)
43
+ - `LinkedList` — singly linked list (append/prepend/remove/find)
44
+ - `CircularBuffer` — fixed-size circular buffer
45
+ - `PriorityQueue` — min-heap priority queue
43
46
 
44
47
  ### Base Conversion
45
48
  - `baseConvert(n, from_base, to_base)` — arbitrary base conversion (2-36)
@@ -84,6 +87,23 @@ A Python utility library that extends the standard library with math, algebra, d
84
87
  ### Random Utilities
85
88
  - `randomChoice`, `shuffle`, `randomString`, `randomInt`, `randomFloat` — enhanced random helpers
86
89
 
90
+ ### File I/O
91
+ - `readJson`, `writeJson` — JSON file helpers
92
+ - `readCsv`, `writeCsv` — CSV file helpers
93
+
94
+ ### Encoding
95
+ - `base64Encode`, `base64Decode` — Base64 encoding
96
+ - `urlEncode`, `urlDecode` — URL encoding
97
+
98
+ ### Math Extensions
99
+ - `factorial` — factorial
100
+ - `matrixInverse` — matrix inverse (Gauss-Jordan)
101
+ - `interpolate` — linear interpolation
102
+
103
+ ### Regex Utilities
104
+ - `extractEmails`, `extractURLs` — extract from text
105
+ - `replaceMultiple` — multi-pattern replacement
106
+
87
107
  ## Installation
88
108
 
89
109
  ```bash
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "eapyTool"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "A Python utility library for math, algebra, data structures, and everyday tools"
9
9
  readme = "README.md"
10
10
  license = "MPL-2.0"
@@ -1,6 +1,6 @@
1
1
  """eapy - A Python utility library for math, algebra, data structures, and everyday tools."""
2
2
 
3
- __version__ = "0.2.0"
3
+ __version__ = "0.3.0"
4
4
 
5
5
  from eapy.core import (
6
6
  # Timer
@@ -51,6 +51,7 @@ from eapy.core import (
51
51
  pipe, compose, curry,
52
52
  # Data structures
53
53
  Stack, Queue, Trie,
54
+ LinkedList, CircularBuffer, PriorityQueue,
54
55
  # Base conversion
55
56
  baseConvert, toBinary, toOctal, toHex,
56
57
  # Color
@@ -69,4 +70,12 @@ from eapy.core import (
69
70
  md5, sha256, hashFile,
70
71
  # Random
71
72
  randomChoice, shuffle, randomString, randomInt, randomFloat,
73
+ # File I/O
74
+ readJson, writeJson, readCsv, writeCsv,
75
+ # Encoding
76
+ base64Encode, base64Decode, urlEncode, urlDecode,
77
+ # Math extensions
78
+ factorial, matrixInverse, interpolate,
79
+ # Regex utilities
80
+ extractEmails, extractURLs, replaceMultiple,
72
81
  )
@@ -1674,4 +1674,291 @@ def randomInt(min_val: int, max_val: int) -> int:
1674
1674
 
1675
1675
  def randomFloat(min_val: float = 0.0, max_val: float = 1.0) -> float:
1676
1676
  """Return a random float in [min_val, max_val)."""
1677
- return _random.uniform(min_val, max_val)
1677
+ return _random.uniform(min_val, max_val)
1678
+
1679
+
1680
+ # ======================================================== Data structures (extended) ========================================================
1681
+
1682
+ class LinkedListNode:
1683
+ def __init__(self, value, next_node=None):
1684
+ self.value = value
1685
+ self.next = next_node
1686
+
1687
+
1688
+ class LinkedList:
1689
+ """Singly linked list."""
1690
+
1691
+ def __init__(self):
1692
+ self.head = None
1693
+ self._size = 0
1694
+
1695
+ def append(self, value):
1696
+ """Add value to the end."""
1697
+ new_node = LinkedListNode(value)
1698
+ if self.head is None:
1699
+ self.head = new_node
1700
+ else:
1701
+ node = self.head
1702
+ while node.next:
1703
+ node = node.next
1704
+ node.next = new_node
1705
+ self._size += 1
1706
+
1707
+ def prepend(self, value):
1708
+ """Add value to the beginning."""
1709
+ self.head = LinkedListNode(value, self.head)
1710
+ self._size += 1
1711
+
1712
+ def remove(self, value):
1713
+ """Remove first occurrence of value."""
1714
+ if self.head is None:
1715
+ raise ValueError(f"{value} not found")
1716
+ if self.head.value == value:
1717
+ self.head = self.head.next
1718
+ self._size -= 1
1719
+ return
1720
+ node = self.head
1721
+ while node.next:
1722
+ if node.next.value == value:
1723
+ node.next = node.next.next
1724
+ self._size -= 1
1725
+ return
1726
+ node = node.next
1727
+ raise ValueError(f"{value} not found")
1728
+
1729
+ def find(self, value):
1730
+ """Return True if value exists."""
1731
+ node = self.head
1732
+ while node:
1733
+ if node.value == value:
1734
+ return True
1735
+ node = node.next
1736
+ return False
1737
+
1738
+ def to_list(self) -> list:
1739
+ """Convert to Python list."""
1740
+ result = []
1741
+ node = self.head
1742
+ while node:
1743
+ result.append(node.value)
1744
+ node = node.next
1745
+ return result
1746
+
1747
+ def __len__(self):
1748
+ return self._size
1749
+
1750
+ def __repr__(self):
1751
+ return f"LinkedList({self.to_list()})"
1752
+
1753
+
1754
+ class CircularBuffer:
1755
+ """Fixed-size circular buffer."""
1756
+
1757
+ def __init__(self, capacity: int):
1758
+ self.capacity = capacity
1759
+ self._data = [None] * capacity
1760
+ self._start = 0
1761
+ self._end = 0
1762
+ self._size = 0
1763
+
1764
+ def push(self, value):
1765
+ """Add value, overwrites oldest if full."""
1766
+ self._data[self._end] = value
1767
+ self._end = (self._end + 1) % self.capacity
1768
+ if self._size == self.capacity:
1769
+ self._start = (self._start + 1) % self.capacity
1770
+ else:
1771
+ self._size += 1
1772
+
1773
+ def pop(self):
1774
+ """Remove and return the oldest value."""
1775
+ if self._size == 0:
1776
+ raise IndexError("Buffer is empty")
1777
+ value = self._data[self._start]
1778
+ self._start = (self._start + 1) % self.capacity
1779
+ self._size -= 1
1780
+ return value
1781
+
1782
+ def peek(self):
1783
+ """Return oldest value without removing."""
1784
+ if self._size == 0:
1785
+ raise IndexError("Buffer is empty")
1786
+ return self._data[self._start]
1787
+
1788
+ def __len__(self):
1789
+ return self._size
1790
+
1791
+ def __repr__(self):
1792
+ items = []
1793
+ idx = self._start
1794
+ for _ in range(self._size):
1795
+ items.append(self._data[idx])
1796
+ idx = (idx + 1) % self.capacity
1797
+ return f"CircularBuffer({items})"
1798
+
1799
+
1800
+ class PriorityQueue:
1801
+ """Min-heap priority queue."""
1802
+
1803
+ def __init__(self):
1804
+ self._heap = []
1805
+
1806
+ def push(self, item, priority: float = 0):
1807
+ """Add item with given priority (lower = higher priority)."""
1808
+ self._heap.append((priority, item))
1809
+ self._heap.sort(key=lambda x: x[0])
1810
+
1811
+ def pop(self):
1812
+ """Remove and return item with lowest priority value."""
1813
+ if not self._heap:
1814
+ raise IndexError("Queue is empty")
1815
+ return self._heap.pop(0)[1]
1816
+
1817
+ def peek(self):
1818
+ """Return item with lowest priority value."""
1819
+ if not self._heap:
1820
+ raise IndexError("Queue is empty")
1821
+ return self._heap[0][1]
1822
+
1823
+ def is_empty(self) -> bool:
1824
+ return len(self._heap) == 0
1825
+
1826
+ def __len__(self):
1827
+ return len(self._heap)
1828
+
1829
+ def __repr__(self):
1830
+ return f"PriorityQueue({[(p, i) for p, i in self._heap]})"
1831
+
1832
+
1833
+ # ======================================================== File I/O ========================================================
1834
+
1835
+ import json
1836
+ import csv
1837
+
1838
+
1839
+ def readJson(path: str, encoding: str = "utf-8"):
1840
+ """Read and parse a JSON file."""
1841
+ with open(path, "r", encoding=encoding) as f:
1842
+ return json.load(f)
1843
+
1844
+
1845
+ def writeJson(path: str, data, indent: int = 2, encoding: str = "utf-8"):
1846
+ """Write data to a JSON file."""
1847
+ with open(path, "w", encoding=encoding) as f:
1848
+ json.dump(data, f, indent=indent, ensure_ascii=False)
1849
+
1850
+
1851
+ def readCsv(path: str, delimiter: str = ",", encoding: str = "utf-8") -> list[list[str]]:
1852
+ """Read a CSV file and return list of rows."""
1853
+ with open(path, "r", encoding=encoding, newline="") as f:
1854
+ return list(csv.reader(f, delimiter=delimiter))
1855
+
1856
+
1857
+ def writeCsv(path: str, data: list[list], delimiter: str = ",", encoding: str = "utf-8"):
1858
+ """Write list of rows to a CSV file."""
1859
+ with open(path, "w", encoding=encoding, newline="") as f:
1860
+ writer = csv.writer(f, delimiter=delimiter)
1861
+ writer.writerows(data)
1862
+
1863
+
1864
+ # ======================================================== Encoding ========================================================
1865
+
1866
+ import base64
1867
+ import urllib.parse
1868
+
1869
+
1870
+ def base64Encode(s: str) -> str:
1871
+ """Encode string to Base64."""
1872
+ return base64.b64encode(s.encode()).decode()
1873
+
1874
+
1875
+ def base64Decode(s: str) -> str:
1876
+ """Decode Base64 string."""
1877
+ return base64.b64decode(s.encode()).decode()
1878
+
1879
+
1880
+ def urlEncode(s: str) -> str:
1881
+ """URL-encode a string."""
1882
+ return urllib.parse.quote(s)
1883
+
1884
+
1885
+ def urlDecode(s: str) -> str:
1886
+ """URL-decode a string."""
1887
+ return urllib.parse.unquote(s)
1888
+
1889
+
1890
+ # ======================================================== Math extensions ========================================================
1891
+
1892
+ def factorial(n: int) -> int:
1893
+ """Return n! (n factorial)."""
1894
+ if n < 0:
1895
+ raise ValueError("Factorial not defined for negative numbers")
1896
+ if n <= 1:
1897
+ return 1
1898
+ result = 1
1899
+ for i in range(2, n + 1):
1900
+ result *= i
1901
+ return result
1902
+
1903
+
1904
+ def matrixInverse(m: Matrix) -> Matrix:
1905
+ """Return the inverse of a square matrix using Gauss-Jordan elimination."""
1906
+ n = len(m.data)
1907
+ if any(len(row) != n for row in m.data):
1908
+ raise ValueError("Matrix must be square")
1909
+ # Augment with identity
1910
+ aug = [m.data[i][:] + [1.0 if i == j else 0.0 for j in range(n)] for i in range(n)]
1911
+ for col in range(n):
1912
+ # Find pivot
1913
+ max_row = col
1914
+ for row in range(col + 1, n):
1915
+ if abs(aug[row][col]) > abs(aug[max_row][col]):
1916
+ max_row = row
1917
+ aug[col], aug[max_row] = aug[max_row], aug[col]
1918
+ if abs(aug[col][col]) < 1e-12:
1919
+ raise ValueError("Matrix is singular")
1920
+ pivot = aug[col][col]
1921
+ for j in range(2 * n):
1922
+ aug[col][j] /= pivot
1923
+ for row in range(n):
1924
+ if row != col:
1925
+ factor = aug[row][col]
1926
+ for j in range(2 * n):
1927
+ aug[row][j] -= factor * aug[col][j]
1928
+ return Matrix([row[n:] for row in aug])
1929
+
1930
+
1931
+ def interpolate(x: float, points: list[tuple[float, float]], method: str = "linear") -> float:
1932
+ """Interpolate at x given data points. Methods: 'linear'."""
1933
+ xs = [p[0] for p in points]
1934
+ ys = [p[1] for p in points]
1935
+ if method == "linear":
1936
+ if x <= xs[0]:
1937
+ return ys[0]
1938
+ if x >= xs[-1]:
1939
+ return ys[-1]
1940
+ for i in range(len(xs) - 1):
1941
+ if xs[i] <= x <= xs[i + 1]:
1942
+ t = (x - xs[i]) / (xs[i + 1] - xs[i])
1943
+ return ys[i] + t * (ys[i + 1] - ys[i])
1944
+ raise ValueError(f"Unknown method: {method}")
1945
+
1946
+
1947
+ # ======================================================== Regex utilities ========================================================
1948
+
1949
+
1950
+ def extractEmails(text: str) -> list[str]:
1951
+ """Extract all email addresses from text."""
1952
+ return re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
1953
+
1954
+
1955
+ def extractURLs(text: str) -> list[str]:
1956
+ """Extract all URLs from text."""
1957
+ return re.findall(r'https?://[^\s<>"]+', text)
1958
+
1959
+
1960
+ def replaceMultiple(text: str, replacements: dict[str, str]) -> str:
1961
+ """Replace multiple patterns in text. Keys are patterns, values are replacements."""
1962
+ for pattern, replacement in replacements.items():
1963
+ text = text.replace(pattern, replacement)
1964
+ return text
@@ -17,6 +17,7 @@ from eapy.core import (
17
17
  toCamelCase, toSnakeCase, toPascalCase, levenshtein,
18
18
  pipe, compose, curry,
19
19
  Stack, Queue, Trie,
20
+ LinkedList, CircularBuffer, PriorityQueue,
20
21
  baseConvert, toBinary, toOctal, toHex,
21
22
  rgbToHsl, hslToRgb, rgbToHex, hexToRgb,
22
23
  bisect, newtonMethod, numericalIntegral,
@@ -27,6 +28,9 @@ from eapy.core import (
27
28
  ProgressBar,
28
29
  md5, sha256,
29
30
  randomChoice, shuffle, randomString, randomInt, randomFloat,
31
+ base64Encode, base64Decode, urlEncode, urlDecode,
32
+ factorial, matrixInverse, interpolate,
33
+ extractEmails, extractURLs, replaceMultiple,
30
34
  )
31
35
  from math import pi
32
36
 
@@ -492,3 +496,111 @@ class TestRandom:
492
496
 
493
497
  def test_randomFloat(self):
494
498
  assert 0.0 <= randomFloat(0.0, 1.0) < 1.0
499
+
500
+
501
+ # ======================================================== Data structures (extended) ========================================================
502
+
503
+ class TestLinkedList:
504
+ def test_append_and_find(self):
505
+ ll = LinkedList()
506
+ ll.append(1)
507
+ ll.append(2)
508
+ ll.append(3)
509
+ assert ll.find(2) is True
510
+ assert ll.find(5) is False
511
+ assert ll.to_list() == [1, 2, 3]
512
+
513
+ def test_remove(self):
514
+ ll = LinkedList()
515
+ ll.append(1)
516
+ ll.append(2)
517
+ ll.remove(2)
518
+ assert ll.to_list() == [1]
519
+
520
+ def test_prepend(self):
521
+ ll = LinkedList()
522
+ ll.prepend(1)
523
+ ll.prepend(0)
524
+ assert ll.to_list() == [0, 1]
525
+
526
+
527
+ class TestCircularBuffer:
528
+ def test_push_pop(self):
529
+ cb = CircularBuffer(3)
530
+ cb.push(1)
531
+ cb.push(2)
532
+ cb.push(3)
533
+ assert cb.pop() == 1
534
+ assert cb.pop() == 2
535
+
536
+ def test_overwrite(self):
537
+ cb = CircularBuffer(2)
538
+ cb.push(1)
539
+ cb.push(2)
540
+ cb.push(3) # overwrites 1
541
+ assert cb.pop() == 2
542
+ assert cb.pop() == 3
543
+
544
+
545
+ class TestPriorityQueue:
546
+ def test_order(self):
547
+ pq = PriorityQueue()
548
+ pq.push("low", priority=3)
549
+ pq.push("high", priority=1)
550
+ pq.push("mid", priority=2)
551
+ assert pq.pop() == "high"
552
+ assert pq.pop() == "mid"
553
+ assert pq.pop() == "low"
554
+
555
+
556
+ # ======================================================== Encoding ========================================================
557
+
558
+ class TestEncoding:
559
+ def test_base64(self):
560
+ encoded = base64Encode("hello")
561
+ assert base64Decode(encoded) == "hello"
562
+
563
+ def test_url(self):
564
+ encoded = urlEncode("hello world&foo=bar")
565
+ assert " " not in encoded
566
+ assert urlDecode(encoded) == "hello world&foo=bar"
567
+
568
+
569
+ # ======================================================== Math extensions ========================================================
570
+
571
+ class TestMathExtensions:
572
+ def test_factorial(self):
573
+ assert factorial(5) == 120
574
+ assert factorial(0) == 1
575
+
576
+ def test_matrixInverse(self):
577
+ m = Matrix([[1, 2], [3, 4]])
578
+ inv = matrixInverse(m)
579
+ # m * inv should be identity
580
+ result = m * inv
581
+ assert abs(result.data[0][0] - 1) < 1e-10
582
+ assert abs(result.data[1][1] - 1) < 1e-10
583
+
584
+ def test_interpolate(self):
585
+ points = [(0, 0), (10, 100)]
586
+ assert interpolate(5, points) == 50.0
587
+
588
+
589
+ # ======================================================== Regex utilities ========================================================
590
+
591
+ class TestRegex:
592
+ def test_extractEmails(self):
593
+ text = "Contact us at test@example.com or admin@site.org"
594
+ emails = extractEmails(text)
595
+ assert "test@example.com" in emails
596
+ assert "admin@site.org" in emails
597
+
598
+ def test_extractURLs(self):
599
+ text = "Visit https://example.com or http://test.org"
600
+ urls = extractURLs(text)
601
+ assert "https://example.com" in urls
602
+ assert "http://test.org" in urls
603
+
604
+ def test_replaceMultiple(self):
605
+ result = replaceMultiple("hello world", {"hello": "hi", "world": "earth"})
606
+ assert result == "hi earth"
File without changes
File without changes
File without changes
File without changes
File without changes