Skip to content

Commit

Permalink
v0.3.4rc2
Browse files Browse the repository at this point in the history
  • Loading branch information
xyluo25 committed Aug 5, 2024
1 parent 90cd8d5 commit 9fe9608
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "pyufunc"
version = "0.3.4rc1"
version = "0.3.4rc2"
description = "pyufunc consolidates frequently used utility functions into one cohesive package"
authors = [
{name = "Xiangyong Luo", email = "luoxiangyong01@gmail.com"},
Expand Down
4 changes: 2 additions & 2 deletions pyufunc/util_network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

from ._network import get_host_ip, validate_url
from ._network import get_host_ip, validate_url, get_host_name

__all__ = ["get_host_ip", "validate_url"]
__all__ = ["get_host_ip", "validate_url", "get_host_name"]
52 changes: 37 additions & 15 deletions pyufunc/util_network/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,48 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

import contextlib
import socket
import re


def get_host_ip():
ip = ''
host_name = ''
# noinspection PyBroadException
with contextlib.suppress(Exception):
sc = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sc.connect(('8.8.8.8', 80))
ip = sc.getsockname()[0]
host_name = socket.gethostname()
sc.close()
return ip, host_name
def get_host_name() -> str:
"""Get the computer name.
Returns:
str: computer name.
computer_ip, computer_name = get_host_ip()
Examples:
>>> import pyufunc as pf
>>> pf.get_computer_name()
'Xiangyong-PC'
"""
return socket.gethostname()

def validate_url(url):
import re

def get_host_ip() -> str:
"""Get the computer IP address.
Returns:
str: computer IP address.
Examples:
>>> import pyufunc as pf
>>> pf.get_computer_ip()
'10.155.33.252'
"""
computer_name = get_host_name()
return socket.gethostbyname(computer_name)


def validate_url(url: str) -> bool:
"""Validate the URL.
Args:
url (str): the URL to validate.
Returns:
bool: True if the URL is valid, False otherwise.
"""
return bool(re.match(r'^https?:/{2}\w.+$', url))
8 changes: 7 additions & 1 deletion pyufunc/util_office/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
##############################################################

from ._email import is_valid_email, send_email
from ._printer import printer_file

__all__ = [

# .email
"is_valid_email",
"send_email"
"send_email",

# .printer
"printer_file",
]
46 changes: 46 additions & 0 deletions pyufunc/util_office/_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,50 @@
# Author/Copyright: Mr. Xiangyong Luo
##############################################################

import socket


def printer_file(fname_lst: list, host: str, port: int = 9100) -> None:
"""Send data to a network printer.
Args:
fname_lst (list): a list of files or binary data
host (str): the host of the printer
port (int, optional): printer port. Defaults to 9100.
Raises:
Exception: Invalid inputs...
Exception: Error connecting to printer: {e}
Example:
>>> import pyufunc as pf
>>> pf.network_printer(["test.txt"], "printer_host_name")
Returns:
None: None
"""

try:
net_printer = socket.socket(socket.AF_INF, socket.SOCK_STREAM)
net_printer.connect((host, port))
data_binary = []

if not fname_lst:
raise Exception("Invalid inputs...")

if isinstance(fname_lst[0], bytes):
data_binary = fname_lst

if isinstance(fname_lst[0], str):
with open(fname_lst, "rb") as f:
data_binary = f.read()

if data_binary:
for data in data_binary:
net_printer.send(data)
net_printer.close()
else:
raise Exception("Invalid inputs...")
except Exception as e:
print("Error connecting to printer: ", e)
return None

0 comments on commit 9fe9608

Please sign in to comment.