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.6.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.6-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: uf-0.0.6.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.6.tar.gz
Algorithm Hash digest
SHA256 e9144a87572bef84961cecb71519468751431a8535d2478bb1c638e1c74b65b8
MD5 d94cff4fc247210c743e8834120e36ae
BLAKE2b-256 b934a27708f9f4e8d3ee2aca34c9ceb66d4519517c3255412800e2689e16bbc8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: uf-0.0.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 33707b2b72a6e6cb9193b4df1c1fbb412a4d2f35e70e53da716e27066a655f46
MD5 d65d5cbbe6b4532a07ff094a99aa5465
BLAKE2b-256 ed56cdd4e7692be52ca0b4ca534dfbacfa859e7e37b011e8b88ae8e002af5042

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