|
| 1 | +# PyMap |
| 2 | + |
| 3 | +[](https://github.com/Walkaisa/PyMap) |
| 4 | +[](https://github.com/Walkaisa/PyMap/blob/master/LICENSE.txt) |
| 5 | +[](https://github.com/Walkaisa/PyMap/releases/latest) |
| 6 | +[](https://github.com/Walkaisa/PyMap) |
| 7 | +[](https://walkaisa.dev/discord) |
| 8 | + |
| 9 | +PyMap is a Python module that provides a simple and efficient key-value data structure. With PyMap, you can easily manage, manipulate, and retrieve key-value pairs. This README.md file provides a detailed guide on how to use PyMap, including example usages and responses for all its functions. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +To install PyMap, you can use `pip`. Make sure you have `Python 3.12` installed. |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install pymap |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +To get started with PyMap, you'll need to import the Map class and create an instance. You can provide data to initialize the map with, or you can initialize it with no data. |
| 22 | + |
| 23 | +```python |
| 24 | +from pymap import Map |
| 25 | + |
| 26 | +class User: |
| 27 | + def __init__(self, id: str, name: str, age: int) -> None: |
| 28 | + self.id: str = id |
| 29 | + self.name: str = name |
| 30 | + self.age: int = age |
| 31 | + |
| 32 | + def __repr__(self) -> str: |
| 33 | + return f"User(id={self.id}, name={self.name}, age={self.age})" |
| 34 | + |
| 35 | + |
| 36 | +data = [ |
| 37 | + ("c8a43542-e330-416d-8a92-edcbbcaf2c3f", User("c8a43542-e330-416d-8a92-edcbbcaf2c3f", "John", 25)), |
| 38 | + ("a136571b-0afd-483d-b8a5-422515363856", User("a136571b-0afd-483d-b8a5-422515363856", "Alice", 50)), |
| 39 | + ("76746904-0dbe-461a-8b0d-3fd27485b23f", User("76746904-0dbe-461a-8b0d-3fd27485b23f", "Tim", 75)) |
| 40 | +] |
| 41 | + |
| 42 | +users = Map[str, User](data) # For no data, use `users = Map[str, User]()` instead. |
| 43 | +print(users) # Output: Map({'c8a43542-e330-416d-8a92-edcbbcaf2c3f': User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25), ...}) |
| 44 | +``` |
| 45 | + |
| 46 | +## Available Getters |
| 47 | + |
| 48 | +### Map.size |
| 49 | + |
| 50 | +The `Map.size -> int` getter allows you to retrieve the number of key-value pairs in the map. |
| 51 | + |
| 52 | +```python |
| 53 | +size = users.size |
| 54 | +print(size) # Output: 3 |
| 55 | +``` |
| 56 | + |
| 57 | +### Map.items |
| 58 | + |
| 59 | +The `Map.items -> Dict[str, User]` getter allows you to retrieve all key-value pairs in the map. |
| 60 | + |
| 61 | +```python |
| 62 | +items = users.items |
| 63 | +print(items) # Output: {'c8a43542-e330-416d-8a92-edcbbcaf2c3f': User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25), ...} |
| 64 | +``` |
| 65 | + |
| 66 | +## Available Methods |
| 67 | + |
| 68 | +### Map.set() |
| 69 | + |
| 70 | +The `Map.set(key: str, value: User) -> Dict[str, User]` function allows you to set a key-value pair in the map. If the key already exists, the value will be overwritten. If the key does not exist, a new key-value pair will be created. |
| 71 | + |
| 72 | +```python |
| 73 | +users = users.set("4809f036-7d99-4437-af16-7395941e1637", User("4809f036-7d99-4437-af16-7395941e1637", "Bob", 100)) |
| 74 | +print(users) # Output: {'4809f036-7d99-4437-af16-7395941e1637': User(id=4809f036-7d99-4437-af16-7395941e1637, name=Bob, age=100), ...} |
| 75 | +``` |
| 76 | + |
| 77 | +### Map.get() |
| 78 | + |
| 79 | +The `Map.get(key: str) -> User | None` function allows you to retrieve a value from the map. If the key does not exist, it will return `None`. |
| 80 | + |
| 81 | +```python |
| 82 | +user = users.get("4809f036-7d99-4437-af16-7395941e1637") |
| 83 | +print(user) # Output: User(id=4809f036-7d99-4437-af16-7395941e1637, name=Bob, age=100) |
| 84 | +``` |
| 85 | + |
| 86 | +### Map.has() |
| 87 | + |
| 88 | +The `Map.has(key: str) -> bool` function allows you to check if a key exists in the map. |
| 89 | + |
| 90 | +```python |
| 91 | +exists = users.has("4809f036-7d99-4437-af16-7395941e1637") |
| 92 | +print(exists) # Output: True |
| 93 | +``` |
| 94 | + |
| 95 | +### Map.delete() |
| 96 | + |
| 97 | +The `Map.delete(key: str) -> bool` function allows you to delete a key-value pair from the map. If the key does not exist, it will return `False` otherwise it will return `True`. |
| 98 | + |
| 99 | +```python |
| 100 | +deleted = users.delete("4809f036-7d99-4437-af16-7395941e1637") |
| 101 | +print(deleted) # Output: True |
| 102 | +``` |
| 103 | + |
| 104 | +### Map.keys() |
| 105 | + |
| 106 | +The `Map.keys() -> Tuple[str]` function allows you to retrieve all keys from the map. |
| 107 | + |
| 108 | +```python |
| 109 | +keys = users.keys() |
| 110 | +print(keys) # Output: ('c8a43542-e330-416d-8a92-edcbbcaf2c3f', ...) |
| 111 | +``` |
| 112 | + |
| 113 | +### Map.values() |
| 114 | + |
| 115 | +The `Map.values() -> Tuple[User]` function allows you to retrieve all values from the map. |
| 116 | + |
| 117 | +```python |
| 118 | +values = users.values() |
| 119 | +print(values) # Output: (User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25), ...) |
| 120 | +``` |
| 121 | + |
| 122 | +### Map.entries() |
| 123 | + |
| 124 | +The `Map.entries() -> Tuple[Tuple[str, User]]` function allows you to retrieve all key-value pairs from the map. |
| 125 | + |
| 126 | +```python |
| 127 | +entries = users.entries() |
| 128 | +print(entries) # Output: (('c8a43542-e330-416d-8a92-edcbbcaf2c3f', User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25)), ...) |
| 129 | +``` |
| 130 | + |
| 131 | +### Map.first() |
| 132 | + |
| 133 | +The `Map.first() -> User | None` function allows you to retrieve the first value from the map. If the map is empty, it will return `None`. |
| 134 | + |
| 135 | +```python |
| 136 | +user = users.first() |
| 137 | +print(user) # Output: User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25) |
| 138 | +``` |
| 139 | + |
| 140 | +### Map.last() |
| 141 | + |
| 142 | +The `Map.last() -> User | None` function allows you to retrieve the last value from the map. If the map is empty, it will return `None`. |
| 143 | + |
| 144 | +```python |
| 145 | +user = users.last() |
| 146 | +print(user) # Output: User(id=76746904-0dbe-461a-8b0d-3fd27485b23f, name=Tim, age=75) |
| 147 | +``` |
| 148 | + |
| 149 | +### Map.find() |
| 150 | + |
| 151 | +The `Map.find(callback: Callable[[User], bool]) -> User | None` function allows you to find a value in the map. If the value is found, it will return the value otherwise it will return `None`. |
| 152 | + |
| 153 | +```python |
| 154 | +user = users.find(lambda user: user.name == "Alice") |
| 155 | +print(user) # Output: User(id=a136571b-0afd-483d-b8a5-422515363856, name=Alice, age=50) |
| 156 | +``` |
| 157 | + |
| 158 | +### Map.filter() |
| 159 | + |
| 160 | +The `Map.filter(callback: Callable[[User], bool]) -> Tuple[User]` function allows you to filter values in the map. |
| 161 | + |
| 162 | +```python |
| 163 | +users = users.filter(lambda user: user.age > 50) |
| 164 | +print(users) # Output: (User(id=76746904-0dbe-461a-8b0d-3fd27485b23f, name=Tim, age=75), ...) |
| 165 | +``` |
| 166 | + |
| 167 | +### Map.random() |
| 168 | + |
| 169 | +The `Map.random() -> User | None` function allows you to retrieve a random value from the map. If the map is empty, it will return `None`. |
| 170 | + |
| 171 | +```python |
| 172 | +user = users.random() |
| 173 | +print(user) # Output: User(id=c8a43542-e330-416d-8a92-edcbbcaf2c3f, name=John, age=25) |
| 174 | +``` |
| 175 | + |
| 176 | +### Map.clear() |
| 177 | + |
| 178 | +The `Map.clear() -> int` function allows you to clear all key-value pairs from the map. It will return the number of key-value pairs that were deleted. |
| 179 | + |
| 180 | +```python |
| 181 | +cleared = users.clear() |
| 182 | +print(cleared) # Output: 3 |
| 183 | +``` |
| 184 | + |
| 185 | +# License |
| 186 | + |
| 187 | +PyMap is licensed under the [MIT License](LICENSE.txt) |
0 commit comments