libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Samuel Marks (JIRA)" <j...@apache.org>
Subject [jira] [Comment Edited] (LIBCLOUD-705) Pickling of Nodes isn't supported - TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled
Date Sat, 16 May 2015 01:43:01 GMT

    [ https://issues.apache.org/jira/browse/LIBCLOUD-705?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14546486#comment-14546486
] 

Samuel Marks edited comment on LIBCLOUD-705 at 5/16/15 1:42 AM:
----------------------------------------------------------------

Sounds good to me. In the meantime I've hacked together a solution to turn the Node into a
dictionary (for JSON):

{code:title=node_to_dict.py|borderStyle=solid}
from types import (DictType, ListType, TupleType, BooleanType, FloatType, StringType,
                   UnicodeType, IntType, NoneType, LongType, MethodType, ClassType)

# Types which can be easily serialised
normal_types = (DictType, ListType, TupleType, BooleanType, FloatType,
                StringType, UnicodeType, IntType, NoneType, LongType)


def is_instance_method(obj):
    """Checks if an object is a bound method on an instance.

    From: http://stackoverflow.com/a/1260881
    """
    if not isinstance(obj, MethodType):
        return False  # Not a method
    if obj.im_self is None:
        return False  # Method is not bound
    if issubclass(obj.im_class, type) or obj.im_class is ClassType:
        return False  # Method is a classmethod
    return True


node_to_dict = lambda node: {
    attr: {'driver': (lambda s: s[s.find("'") + 1:s.rfind("'")])(str(type(getattr(node, attr)))),
           'extra': _extra_to_dict(getattr(node, attr))}.get(attr, getattr(node, attr))
    for attr in dir(node)
    if not attr.startswith('_') and not is_instance_method(getattr(node, attr))
}


def _extra_to_dict(extra):
    extra = {
        attr: getattr(extra, attr)
        for attr in dir(extra)
        if not attr.startswith('_') and not is_instance_method(getattr(extra, attr))
        and type(getattr(extra, attr)) in normal_types
    }
    for key in ('secret', 'key'):
        extra.pop(key, None)
    if 'network_interfaces' in extra:
        extra['network_interfaces'] = [{'name': interface.name, 'id': interface.id}
                                       for interface in extra['network_interfaces']]
    return extra
{code}


was (Author: samuelmarks):
Sounds good to me. In the meantime I've hacked together a solution to turn the Node into a
dictionary (for JSON):

```
from types import (DictType, ListType, TupleType, BooleanType, FloatType, StringType,
                   UnicodeType, IntType, NoneType, LongType, MethodType, ClassType)

# Types which can be easily serialised
normal_types = (DictType, ListType, TupleType, BooleanType, FloatType,
                StringType, UnicodeType, IntType, NoneType, LongType)


def is_instance_method(obj):
    """Checks if an object is a bound method on an instance.

    From: http://stackoverflow.com/a/1260881
    """
    if not isinstance(obj, MethodType):
        return False  # Not a method
    if obj.im_self is None:
        return False  # Method is not bound
    if issubclass(obj.im_class, type) or obj.im_class is ClassType:
        return False  # Method is a classmethod
    return True


node_to_dict = lambda node: {
    attr: {'driver': (lambda s: s[s.find("'") + 1:s.rfind("'")])(str(type(getattr(node, attr)))),
           'extra': _extra_to_dict(getattr(node, attr))}.get(attr, getattr(node, attr))
    for attr in dir(node)
    if not attr.startswith('_') and not is_instance_method(getattr(node, attr))
}


def _extra_to_dict(extra):
    extra = {
        attr: getattr(extra, attr)
        for attr in dir(extra)
        if not attr.startswith('_') and not is_instance_method(getattr(extra, attr))
        and type(getattr(extra, attr)) in normal_types
    }
    for key in ('secret', 'key'):
        extra.pop(key, None)
    if 'network_interfaces' in extra:
        extra['network_interfaces'] = [{'name': interface.name, 'id': interface.id}
                                       for interface in extra['network_interfaces']]
    return extra
```

> Pickling of Nodes isn't supported - TypeError: a class that defines __slots__ without
defining __getstate__ cannot be pickled
> -----------------------------------------------------------------------------------------------------------------------------
>
>                 Key: LIBCLOUD-705
>                 URL: https://issues.apache.org/jira/browse/LIBCLOUD-705
>             Project: Libcloud
>          Issue Type: Wish
>          Components: Compute
>            Reporter: Samuel Marks
>              Labels: features
>
> TypeError: a class that defines __slots__ without defining __getstate__ cannot be pickled.
> Class is libcloud.compute.base.Node.
> Use case: pushing nodes created in libcloud into a queue for processing with other tools



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Mime
View raw message