Skip to main content

Utils to work with python objects

Project description

uf

Utils to work with python objects

To install: pip install uf

Description

The uf package provides a collection of utilities designed to facilitate operations on Python objects. These utilities include functions for copying attributes between objects, checking and modifying method types, serializing objects with compression, and dynamically managing object attributes. The package is structured to aid in reflection, introspection, and dynamic modification of objects, which can be particularly useful in advanced Python programming scenarios such as metaprogramming or dynamic attribute manipulation.

Main Functionalities

  • Attribute Copying: Copy specified attributes from one object to another, optionally raising an error if the source object does not have one of the requested attributes.
  • Class and Instance Utilities: Determine if a method is a class method, list properties and methods of classes and objects, and inject methods into objects at runtime.
  • Serialization: Compress and decompress serialized Python objects using pickle and zlib for efficient storage or transmission.
  • Attribute Management: Set attributes on objects dynamically from a dictionary, check for attributes, and differentiate between callable and non-callable attributes.

Usage Examples

Copying Attributes

Copy attributes from one object to another with control over error handling when attributes are missing:

class Source:
    x = 10
    y = 20

class Target:
    x = 5

# Copy 'x' and 'y' from Source to Target
copy_attrs(Target, Source, ['x', 'y'])
print(Target.x)  # Output: 10
print(Target.y)  # Output: 20

Checking and Injecting Methods

Check if a method is a class method and inject a new method into an instance:

class MyClass:
    @classmethod
    def cls_method(cls):
        print("This is a class method.")

# Check if 'cls_method' is a class method
print(is_classmethod(MyClass, 'cls_method'))  # Output: True

# Inject a new method into an instance
instance = MyClass()
def new_method(self):
    print("This is a new method.")
inject_method(instance, new_method)
instance.new_method()  # Output: "This is a new method."

Serialization and Compression

Serialize and compress an object, then decompress and deserialize:

my_data = {'key': 'value'}
compressed = zpickle_dumps(my_data)
original = zpickle_loads(compressed)
print(original)  # Output: {'key': 'value'}

Dynamic Attribute Management

Set attributes on an object dynamically and check for their existence:

class MyObject:
    pass

obj = MyObject()
set_attributes(obj, {'new_attr': 123, 'another_attr': 'hello'})
print(obj.new_attr)  # Output: 123
print(has_attributes(obj, ['new_attr', 'another_attr']))  # Output: True

Function and Class Documentation

Each function in the uf package is documented with docstrings, providing a detailed explanation of its behavior, parameters, and usage examples. These docstrings can be viewed directly in the source code or through Python's built-in help system using help(function_name).

For further details and advanced usage, users are encouraged to refer to the source code which is well-commented and structured to be self-explanatory.

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

uf-0.0.5.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uf-0.0.5-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file uf-0.0.5.tar.gz.

File metadata

  • Download URL: uf-0.0.5.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for uf-0.0.5.tar.gz
Algorithm Hash digest
SHA256 c3aa937c37d28d2d17de2edf377cd95dac35436fd6911def113e9428c605da0f
MD5 c149034b4c150d937d77540997149162
BLAKE2b-256 e43abd5abdbdbac339174cd1fc5bd1ddd07fc3d6e7f1416d2e1eb67996b057f6

See more details on using hashes here.

File details

Details for the file uf-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: uf-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for uf-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 acc2668d2a203c148a7bd8096766aaa031876dae4e6ee8f6c3fe8d8333e562f5
MD5 f1d33c5e4124ea50aedeb09b98829b85
BLAKE2b-256 7ebd7aa20395b39e2ffc9210c56eff25957747d15670dc6a49905566294349fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page