Various functions useful for python and machine learning. Keep your code clean.
Project description
PYMLTUILS
About
This repository provides various functions useful for python and machine learning. The goal is to keep your code cleaner, keeping business logic out of your primary functions. For example, formatting paths is common, can often turn into many lines of code, and is done frequently throughout a script. Often people do the path formatting multiple times, mid-function, convoluting the code and distracting from the functions purpose. Further, paths aren't always formatted consistently and error handling is poor. Thus, I created the following function to format all paths consistently:
def format_path(path):
""" Format paths into a consistent format. Expand user paths.
Relative paths are converted to full paths. Directories
should all end in forward slashes. Note this will format even if
the paths don't actually exist yet.
Args:
path: Full or relative path of directory or file in question.
Returns:
Formatted full paths, relative to
"""
# If path is None (e.g. when no --arg from parser), default to same dir as the script
if path is None:
path = ""
# Expand paths
path = os.path.expanduser(path)
# Handle relative paths
path = os.path.abspath(path)
# Check if path *could be* a valid file or dir
# Note, files like MAKE or LICENCE that don't actually exist
# will be considered as dirs.
is_dir = os.path.isdir(path) or is_dir_like(path) and not os.path.isfile(path)
# Make sure dirs end with a slash
if is_dir and not path.endswith('/'):
path += "/"
return path
This function can take relative or full paths to files or directories. It ensures that empty paths and paths that are type None are handled, paths are expanded, and that directories end with a trailing slash. Using this function removes clutter from your other functions and ensures that paths are formatted consistently, resulting in cleaner, more robust code.
How to Install
Install requirements
pip install -r requirements.txt
Install the package
pip install pymlutils
How to Use
In your script, import pymlutils
. Some example usage:
import pymlutils.pymlutils as pyml
formatted_path = pyml.format_path(path)
from pymlutils.pymlutils import *
formatted_path = format_path(path)
import pymlutils.yolo as yolo
label_list = yolo.read_label(path)
from pymlutils.yolo import *
label_list = read_label(path)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pymlutils-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed26799dd96cbad760ab291cac10f158976ed9cbda0e715f31cd323cbda40fde |
|
MD5 | c39b52af39b9080488126b5524a043c0 |
|
BLAKE2b-256 | 4d61bfe04651b1a979b4ac48be10a7bdadf55b1247bc4504ef22e2166590c478 |