Skip to content

allen2c/cls-str

Repository files navigation

cls-str

PyPI version Python Version License

Convert Python classes to strings and back. Useful for serialization, configuration files, and message passing.

Installation

pip install cls-str

Quick Start

Basic Usage

from cls_str import cls_to_str, str_to_cls

# Convert class to string
from http.server import SimpleHTTPRequestHandler

class_path = cls_to_str(SimpleHTTPRequestHandler)
print(class_path)
# Output: "http.server.SimpleHTTPRequestHandler"

# Convert string back to class
restored_class = str_to_cls(class_path)
assert restored_class is SimpleHTTPRequestHandler

Nested Classes

class Outer:
    class Inner:
        pass

# Works with nested classes too
path = cls_to_str(Outer.Inner)
print(path)
# Output: "your_module.Outer.Inner"

loaded_class = str_to_cls(path)
assert loaded_class is Outer.Inner

License

MIT License

About

Magic of class-to-str

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors