Skip to main content

Convenience facilities for objects.

Presents:

  • flavour, for deciding whether an object resembles a mapping or sequence.
  • O, an object subclass with a nice str and convenient init.
  • Some O_* functions for working with objects, particularly O subclasses.
  • Proxy, a very simple minded object proxy intended to aid debugging.

Function copy(obj, *a, **kw)

Convenient function to shallow copy an object with simple modifications.

Performs a shallow copy of self using copy.copy.

Treat all positional parameters as attribute names, and replace those attributes with shallow copies of the original attribute.

Treat all keyword arguments as (attribute,value) tuples and replace those attributes with the supplied values.

Function flavour(obj)

Return constants indicating the ``flavour'' of an object:

  • T_MAP: DictType, DictionaryType, objects with an keys or keys attribute.
  • T_SEQ: TupleType, ListType, objects with an iter attribute.
  • T_SCALAR: Anything else.

Class O

A bare object subclass to allow storing arbitrary attributes.

It also has a nice default __str__ and __eq__ and __ne__ based on the O_attrs of the object.

Function O_attritems(o)

Generator yielding (attr,value) for relevant attributes of o.

Function O_attrs(o)

Yield attribute names from o which are pertinent to O_str.

Note: this calls getattr(o,attr) to inspect it in order to prune callables.

Function O_merge(o, _conflict=None, _overwrite=False, **kw)

Merge key:value pairs from a mapping into an O as attributes.

Ignore keys that do not start with a letter. New attributes or attributes whose values compare equal are merged in. Unequal values are passed to:

_conflict(o, attr, old_value, new_value)

to resolve the conflict. If _conflict is omitted or None then the new value overwrites the old if _overwrite is true.

Function O_str(o, no_recurse=False, seen=None)

Return a str representation of the object o.

Parameters:

  • o: the object to describe.
  • no_recurse: if true, do not recurse into the object's structure. Default: False.
  • seen: a set of previously sighted objects to prevent recursion loops.

Function obj_as_dict(o, attr_prefix=None, attr_match=None)

Return a dictionary with keys mapping to o attributes.

Class Proxy

An extremely simple proxy object that passes all unmatched attribute accesses to the proxied object.

Note that setattr and delattr work directly on the proxy, not the proxied object.

Class TrackedClassMixin

A mixin to track all instances of a particular class.

This is aimed at checking the global state of objects of a particular type, particularly states like counters. The tracking is attached to the class itself.

The class to be tracked includes this mixin as a superclass and calls:

TrackedClassMixin.__init__(class_to_track)

from its init method. Note that class_to_track is typically the class name itself, not type(self) which would track the specific subclass. At some relevant point one can call:

self.tcm_dump(class_to_track[, file])

class_to_track needs a tcm_get_state method to return the salient information, such as this from cs.resources.MultiOpenMixin:

def tcm_get_state(self):
    return {'opened': self.opened, 'opens': self._opens}

See cs.resources.MultiOpenMixin for example use.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.obj-20190103.tar.gz (5.3 kB view details)

Uploaded Source

File details

Details for the file cs.obj-20190103.tar.gz.

File metadata

  • Download URL: cs.obj-20190103.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.6

File hashes

Hashes for cs.obj-20190103.tar.gz
Algorithm Hash digest
SHA256 74949b05412251c73011323827cfe5db966984a64ebca893a7598a35e1ef7717
MD5 cbf05d8b8b124c92515f157a85eceb8f
BLAKE2b-256 4a6508d04eee0ddea21485868c8551d94ee36b4490f6b3ca658a2dc51ce80628

See more details on using hashes here.

Release history Release notifications | RSS feed

20260610

2 files

20260526

2 files

20250306

2 files

20250103

2 files

20241009

2 files

20241005

2 files

20220918

2 files

20220530

2 files

20210717

1 file

20210131

1 file

20210122

1 file

20201227

1 file

20200716

1 file

20200517

1 file

20200318

1 file

This release

20190103 This release

1 file

20170904

1 file

20160828

1 file

20150118

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page