Skip to content

Commit 6b98ea5

Browse files
author
Walkaisa
committed
init
0 parents  commit 6b98ea5

10 files changed

Lines changed: 586 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Default files to ignore for Python projects.
2+
__pycache__
3+
.vscode
4+
.venv
5+
6+
# Extra files to ignore for Python projects.
7+
/*.json
8+
/*.py

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) [2023-2023] [Walkaisa]
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
# PyMap
2+
3+
[![Status](https://img.shields.io/badge/status-Not_Released-e44d3c.svg)](https://github.com/Walkaisa/PyMap)
4+
[![License](https://img.shields.io/github/license/Walkaisa/PyMap)](https://github.com/Walkaisa/PyMap/blob/master/LICENSE.txt)
5+
[![Release](https://img.shields.io/github/v/release/Walkaisa/PyMap.svg)](https://github.com/Walkaisa/PyMap/releases/latest)
6+
[![Downloads](https://img.shields.io/github/downloads/Walkaisa/PyMap/total.svg)](https://github.com/Walkaisa/PyMap)
7+
[![Discord](https://img.shields.io/discord/996889527698341978?label=discord)](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)

poetry.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pymap/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
PyMap: A simple `key-value` data structure.
3+
"""
4+
5+
from pymap.utils import *
6+
from pymap.core import *
7+
8+
__version__ = Util.get_project_version()
9+
__all__ = [
10+
"Map",
11+
"Util",
12+
"__version__"
13+
]

0 commit comments

Comments
 (0)