This is the basic library for xkit.
### History
- V0.3.0
- wrapper SLIP
- Supports UDP and multicast
- Supports ANSI Escape Codes
### Install
```sh
pip install genlib
```
### ANSI Escape Codes
- Foreground color settings
- ANSIEC.FG.\<BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE>
- ANSIEC.FG.\<BRIGHT_BLACK | BRIGHT_RED | BRIGHT_GREEN | BRIGHT_YELLOW | BRIGHT_BLUE | BRIGHT_MAGENTA | BRIGHT_CYAN | BRIGHT_WHITE>
- ANSIEC.FG.rgb(r, g, b)
- Background color settings
- ANSIEC.BG.\<BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE>
- ANSIEC.BG.\<BRIGHT_BLACK | BRIGHT_RED | BRIGHT_GREEN | BRIGHT_YELLOW | BRIGHT_BLUE | BRIGHT_MAGENTA | BRIGHT_CYAN | BRIGHT_WHITE>
- ANSIEC.BG.rgb(r, g, b)
- Styles
- ANSIEC.OP.\<RESET | BOLD | UNDER_LINE | REVERSE>
- Screen
- ANSIEC.OP.\<CLEAR | CLEAR_LINE>
- Cursor Navigation
- ANSIEC.OP.\<TOP>
- ANSIEC.OP.\<up(n) | down(n) | right(n) | left(n) | next_line(n) | prev_line(n) | to(row, column)>
```python
from genlib.ansiec import ANSIEC
import time, sys, random
def loading(count):
all_progress = [0] * count
sys.stdout.write("\n" * count)
colors = [0] * count
for i in range(count):
colors[i] = ANSIEC.FG.rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
while any(x < 100 for x in all_progress):
time.sleep(0.01)
unfinished = [(i, v) for (i, v) in enumerate(all_progress) if v < 100]
index, _ = random.choice(unfinished)
all_progress[index] += 1
sys.stdout.write(ANSIEC.OP.left(27))
sys.stdout.write(ANSIEC.OP.up(count))
for i, progress in enumerate(all_progress):
sys.stdout.write(colors[i])
width = progress // 4
print(f"[{'#'* width}{' ' * (25 - width)}] {ANSIEC.OP.RESET}{width*4}%")
sys.stdout.write(ANSIEC.OP.RESET)
def main():
loading(4)
if __name__ == "__main__":
main()
```
### Serial Line Internet Protocol
```python
from genlib.slip import Slip
def test_encode_decode(slip):
data = "0.9992676 0.02764893 0.02612305 0.0"
packet_send = slip.encode(data)
print(packet_send)
packet_recv = slip.decode(packet_send)[0]
print(packet_recv, packet_recv == data)
def test_stream_decode(slip):
data = []
data.append(b'\xc00.9992676 0.02764893 0.02612305 0.0\xc0')
data.append(b'\xc00.99')
data.append(b'92676 0.02764893 0.02612305 0.0\xc0')
data.append(b'\xc00.9992676 ')
data.append(b'0.02764893 0.02612305 0.0\xc0')
data.append(b'\xc00.9992676 0.')
data.append(b'02764893 0.02612305 0.0\xc0')
data.append(b'\xc00.9992676 0.02764893 0.0')
data.append(b'2612305 0.0\xc0')
data.append(b'\xc00.9992676 0.02764893 0.02612305 0.0\xc0')
for d in data:
for packet in slip.decode(d):
print(packet)
def main():
slip = Slip()
test_encode_decode(slip)
test_stream_decode(slip)
if __name__ == "__main__":
main()
```
### Multicast
**Multicast recv**
```python
import copy
from genlib.ansiec import ANSIEC
from genlib.udp import MulticastReceiver
MAX_RECV_LINE = 15
recv_data_list = []
def on_async_chatting_recv(receiver, message):
recv_data_list.append(copy.deepcopy(message))
if len(recv_data_list) > MAX_RECV_LINE:
recv_data_list.pop(0)
for i, data in enumerate(recv_data_list[: min(len(recv_data_list), MAX_RECV_LINE)]):
print(ANSIEC.OP.to(i+1,1) + ANSIEC.OP.CLEAR_LINE, end='')
print(ANSIEC.OP.to(i+1,1) + ANSIEC.FG.BRIGHT_YELLOW + f">>> RECEIVE {data.remote}: {data.payload}")
print(ANSIEC.OP.to(MAX_RECV_LINE, 1) + ANSIEC.FG.BRIGHT_BLUE)
def main():
receiver = MulticastReceiver()
print(ANSIEC.OP.CLEAR, end='')
receiver.onRecv(on_async_chatting_recv, unpickling=True)
receiver.loopStart()
while True:
try:
data = input(f"{ANSIEC.OP.to(MAX_RECV_LINE+1, 1) + ANSIEC.FG.BRIGHT_BLUE + ANSIEC.OP.CLEAR_LINE}{receiver.remote}: ")
if data:
receiver.sendTo(data) #Multiple unicast
except KeyboardInterrupt:
break
receiver.loopStop()
if __name__ == "__main__":
main()
```
**Multicast send**
```python
import copy
from genlib.ansiec import ANSIEC
from genlib.udp import MulticastSender
MAX_RECV_LINE = 15
recv_data_list = []
def on_async_chatting_recv(sender, message): #Multiple Unicast
recv_data_list.append(copy.deepcopy(message))
if len(recv_data_list) > MAX_RECV_LINE:
recv_data_list.pop(0)
for i, data in enumerate(recv_data_list[: min(len(recv_data_list), MAX_RECV_LINE)]):
print(ANSIEC.OP.to(i+1,1) + ANSIEC.OP.CLEAR_LINE, end='')
print(ANSIEC.OP.to(i+1,1) + ANSIEC.FG.BRIGHT_RED + f">>> RECEIVE {data.remote}: {data.payload}")
print(ANSIEC.OP.to(MAX_RECV_LINE, 1) + ANSIEC.FG.BRIGHT_BLUE)
def main():
sender = MulticastSender()
print(ANSIEC.OP.CLEAR, end='')
sender.onRecv(on_async_chatting_recv, unpickling=True)
sender.loopStart()
print(ANSIEC.OP.CLEAR, end='')
while True:
try:
data = input(f"{ANSIEC.OP.to(MAX_RECV_LINE+1, 1) + ANSIEC.FG.BRIGHT_BLUE + ANSIEC.OP.CLEAR_LINE}{sender.group}: ")
if data:
sender.sendTo(data) #Only Multicast
except KeyboardInterrupt:
break
sender.loopStop()
if __name__ == "__main__":
main()
```
Raw data
{
"_id": null,
"home_page": "https://github.com/planxstudio/xkit",
"name": "genlib",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "xkit",
"author": "chanmin.park",
"author_email": "devcamp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/87/db/5db0956889fa0e7af64fd9a847d04554b375dd9d91d745593a8b0794c934/genlib-0.3.1.1.tar.gz",
"platform": null,
"description": "This is the basic library for xkit.\r\n\r\n### History\r\n- V0.3.0\r\n - wrapper SLIP\r\n - Supports UDP and multicast\r\n - Supports ANSI Escape Codes \r\n\r\n### Install\r\n```sh\r\npip install genlib\r\n```\r\n\r\n### ANSI Escape Codes\r\n- Foreground color settings\r\n - ANSIEC.FG.\\<BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE>\r\n - ANSIEC.FG.\\<BRIGHT_BLACK | BRIGHT_RED | BRIGHT_GREEN | BRIGHT_YELLOW | BRIGHT_BLUE | BRIGHT_MAGENTA | BRIGHT_CYAN | BRIGHT_WHITE>\r\n - ANSIEC.FG.rgb(r, g, b)\r\n- Background color settings\r\n - ANSIEC.BG.\\<BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE>\r\n - ANSIEC.BG.\\<BRIGHT_BLACK | BRIGHT_RED | BRIGHT_GREEN | BRIGHT_YELLOW | BRIGHT_BLUE | BRIGHT_MAGENTA | BRIGHT_CYAN | BRIGHT_WHITE>\r\n - ANSIEC.BG.rgb(r, g, b)\r\n- Styles\r\n - ANSIEC.OP.\\<RESET | BOLD | UNDER_LINE | REVERSE>\r\n- Screen\r\n - ANSIEC.OP.\\<CLEAR | CLEAR_LINE>\r\n- Cursor Navigation\r\n - ANSIEC.OP.\\<TOP>\r\n - ANSIEC.OP.\\<up(n) | down(n) | right(n) | left(n) | next_line(n) | prev_line(n) | to(row, column)>\r\n\r\n```python\r\nfrom genlib.ansiec import ANSIEC\r\nimport time, sys, random\r\n\r\ndef loading(count):\r\n all_progress = [0] * count\r\n sys.stdout.write(\"\\n\" * count) \r\n colors = [0] * count\r\n for i in range(count):\r\n colors[i] = ANSIEC.FG.rgb(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))\r\n \r\n while any(x < 100 for x in all_progress):\r\n time.sleep(0.01)\r\n unfinished = [(i, v) for (i, v) in enumerate(all_progress) if v < 100]\r\n index, _ = random.choice(unfinished)\r\n all_progress[index] += 1\r\n \r\n sys.stdout.write(ANSIEC.OP.left(27))\r\n sys.stdout.write(ANSIEC.OP.up(count))\r\n for i, progress in enumerate(all_progress): \r\n sys.stdout.write(colors[i])\r\n width = progress // 4\r\n print(f\"[{'#'* width}{' ' * (25 - width)}] {ANSIEC.OP.RESET}{width*4}%\")\r\n sys.stdout.write(ANSIEC.OP.RESET)\r\n\r\ndef main():\r\n loading(4)\r\n\r\nif __name__ == \"__main__\":\r\n main() \r\n```\r\n\r\n### Serial Line Internet Protocol\r\n```python\r\nfrom genlib.slip import Slip\r\n\r\ndef test_encode_decode(slip):\r\n data = \"0.9992676 0.02764893 0.02612305 0.0\"\r\n packet_send = slip.encode(data)\r\n print(packet_send)\r\n packet_recv = slip.decode(packet_send)[0]\r\n print(packet_recv, packet_recv == data)\r\n\r\ndef test_stream_decode(slip):\r\n data = []\r\n data.append(b'\\xc00.9992676 0.02764893 0.02612305 0.0\\xc0')\r\n data.append(b'\\xc00.99')\r\n data.append(b'92676 0.02764893 0.02612305 0.0\\xc0')\r\n data.append(b'\\xc00.9992676 ')\r\n data.append(b'0.02764893 0.02612305 0.0\\xc0')\r\n data.append(b'\\xc00.9992676 0.')\r\n data.append(b'02764893 0.02612305 0.0\\xc0')\r\n data.append(b'\\xc00.9992676 0.02764893 0.0')\r\n data.append(b'2612305 0.0\\xc0')\r\n data.append(b'\\xc00.9992676 0.02764893 0.02612305 0.0\\xc0')\r\n \r\n for d in data:\r\n for packet in slip.decode(d):\r\n print(packet)\r\n\r\ndef main():\r\n slip = Slip()\r\n \r\n test_encode_decode(slip)\r\n test_stream_decode(slip)\r\n \r\nif __name__ == \"__main__\":\r\n main() \r\n```\r\n\r\n### Multicast\r\n\r\n**Multicast recv**\r\n```python\r\nimport copy\r\nfrom genlib.ansiec import ANSIEC\r\nfrom genlib.udp import MulticastReceiver\r\n \r\n\r\nMAX_RECV_LINE = 15\r\nrecv_data_list = []\r\n\r\ndef on_async_chatting_recv(receiver, message):\r\n recv_data_list.append(copy.deepcopy(message))\r\n if len(recv_data_list) > MAX_RECV_LINE:\r\n recv_data_list.pop(0)\r\n\r\n for i, data in enumerate(recv_data_list[: min(len(recv_data_list), MAX_RECV_LINE)]):\r\n print(ANSIEC.OP.to(i+1,1) + ANSIEC.OP.CLEAR_LINE, end='')\r\n print(ANSIEC.OP.to(i+1,1) + ANSIEC.FG.BRIGHT_YELLOW + f\">>> RECEIVE {data.remote}: {data.payload}\")\r\n \r\n print(ANSIEC.OP.to(MAX_RECV_LINE, 1) + ANSIEC.FG.BRIGHT_BLUE)\r\n\r\ndef main():\r\n receiver = MulticastReceiver()\r\n print(ANSIEC.OP.CLEAR, end='')\r\n receiver.onRecv(on_async_chatting_recv, unpickling=True)\r\n receiver.loopStart()\r\n \r\n while True:\r\n try:\r\n data = input(f\"{ANSIEC.OP.to(MAX_RECV_LINE+1, 1) + ANSIEC.FG.BRIGHT_BLUE + ANSIEC.OP.CLEAR_LINE}{receiver.remote}: \")\r\n if data:\r\n receiver.sendTo(data) #Multiple unicast\r\n except KeyboardInterrupt:\r\n break\r\n \r\n receiver.loopStop()\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\n**Multicast send**\r\n```python\r\nimport copy\r\nfrom genlib.ansiec import ANSIEC\r\nfrom genlib.udp import MulticastSender\r\n\r\n\r\nMAX_RECV_LINE = 15\r\nrecv_data_list = []\r\n\r\ndef on_async_chatting_recv(sender, message): #Multiple Unicast\r\n recv_data_list.append(copy.deepcopy(message))\r\n if len(recv_data_list) > MAX_RECV_LINE:\r\n recv_data_list.pop(0)\r\n \r\n for i, data in enumerate(recv_data_list[: min(len(recv_data_list), MAX_RECV_LINE)]):\r\n print(ANSIEC.OP.to(i+1,1) + ANSIEC.OP.CLEAR_LINE, end='')\r\n print(ANSIEC.OP.to(i+1,1) + ANSIEC.FG.BRIGHT_RED + f\">>> RECEIVE {data.remote}: {data.payload}\")\r\n \r\n print(ANSIEC.OP.to(MAX_RECV_LINE, 1) + ANSIEC.FG.BRIGHT_BLUE)\r\n\r\ndef main():\r\n sender = MulticastSender() \r\n print(ANSIEC.OP.CLEAR, end='')\r\n sender.onRecv(on_async_chatting_recv, unpickling=True)\r\n sender.loopStart()\r\n \r\n print(ANSIEC.OP.CLEAR, end='')\r\n\r\n while True:\r\n try:\r\n data = input(f\"{ANSIEC.OP.to(MAX_RECV_LINE+1, 1) + ANSIEC.FG.BRIGHT_BLUE + ANSIEC.OP.CLEAR_LINE}{sender.group}: \")\r\n if data:\r\n sender.sendTo(data) #Only Multicast\r\n except KeyboardInterrupt:\r\n break\r\n \r\n sender.loopStop()\r\n \r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n",
"bugtrack_url": null,
"license": null,
"summary": "This is the basic library for xkit.",
"version": "0.3.1.1",
"project_urls": {
"Homepage": "https://github.com/planxstudio/xkit"
},
"split_keywords": [
"xkit"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "b16ac9e018812cc6f896674d14fb1b3f5bccd46af61a6a95c947f59a394def2d",
"md5": "d646165df73b23eaf04ce491e732b3e8",
"sha256": "526d7fd3e5f2f7fa2be99e71485221ab6f16ee4bcd0105b62e73d0a0b3663b3b"
},
"downloads": -1,
"filename": "genlib-0.3.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d646165df73b23eaf04ce491e732b3e8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 7292,
"upload_time": "2024-05-20T07:27:13",
"upload_time_iso_8601": "2024-05-20T07:27:13.216594Z",
"url": "https://files.pythonhosted.org/packages/b1/6a/c9e018812cc6f896674d14fb1b3f5bccd46af61a6a95c947f59a394def2d/genlib-0.3.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "87db5db0956889fa0e7af64fd9a847d04554b375dd9d91d745593a8b0794c934",
"md5": "41035a7eeec4a4ec63c8b25898d880b1",
"sha256": "880cfcc7389f7225a90b63f72080c0cce6eb5e10ab247c3ff0c54a60b22e912c"
},
"downloads": -1,
"filename": "genlib-0.3.1.1.tar.gz",
"has_sig": false,
"md5_digest": "41035a7eeec4a4ec63c8b25898d880b1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7956,
"upload_time": "2024-05-20T07:27:14",
"upload_time_iso_8601": "2024-05-20T07:27:14.602214Z",
"url": "https://files.pythonhosted.org/packages/87/db/5db0956889fa0e7af64fd9a847d04554b375dd9d91d745593a8b0794c934/genlib-0.3.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-20 07:27:14",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "planxstudio",
"github_project": "xkit",
"github_not_found": true,
"lcname": "genlib"
}