[CentralOH] Code Review / Advice
Brian Costlow
brian.costlow at gmail.com
Thu Nov 19 22:37:37 EST 2015
And, last one for today.
Messing about with filter I didn't find a way to improve the clarity. I did
notice I used a builtin (object) for a function arg. Oops. Fixed.
import inspect
import ipaddress
import json
from types import MethodType
def filter_private_and_methods(inspected_object):
for k, v in inspect.getmembers(inspected_object):
if not (k.startswith('_') or isinstance(v, MethodType)):
yield (k, v)
ipnet = ipaddress.IPv4Network('192.168.0.0/24')
filtered = {k: str(v) for k, v in filter_private_and_methods(ipnet)}
print(ipnet)
print(json.dumps(filtered, sort_keys=True, indent=4, separators=(',', ':
')))
On Thu, Nov 19, 2015 at 9:14 PM, Brian Costlow <brian.costlow at gmail.com>
wrote:
> The boolean logic was wrong, but due to order of ops quirk produced
> correct output.
>
> Fixed (but really could probably be refactored better
> using itertools.filterfalse)
>
> import inspect
> import ipaddress
> import json
> from types import MethodType
>
>
> def filter_private_and_methods(object):
> for k, v in inspect.getmembers(object):
> if not (k.startswith('_') or isinstance(v, MethodType)):
> yield (k, v)
>
>
> ipnet = ipaddress.IPv4Network('192.168.0.0/24')
> filtered = {k: str(v) for k, v in filter_private_and_methods(ipnet)}
>
> print(ipnet)
> print(json.dumps(filtered, sort_keys=True, indent=4, separators=(',', ':
> ')))
>
>
> On Thu, Nov 19, 2015 at 8:55 PM, Brian Costlow <brian.costlow at gmail.com>
> wrote:
>
>> Here is a first cut.
>>
>> In general, clarity wins over brevity for Python, so comprehensions and
>> other one-liners should be easy to read.
>> Add a couple of additional modules from the standard library to make
>> things easier.
>>
>> import inspect
>> import ipaddress
>> import json
>> from types import MethodType
>>
>>
>> def filter_private_and_methods(object):
>> for k, v in inspect.getmembers(object):
>> if not k.startswith('_') and not isinstance(v, MethodType):
>> yield (k, v)
>>
>>
>> ipnet = ipaddress.IPv4Network('192.168.0.0/24')
>> filtered = {k: str(v) for k, v in filter_private_and_methods(ipnet)}
>>
>> print(ipnet)
>> print(json.dumps(filtered, sort_keys=True, indent=4, separators=(',', ':
>> ')))
>>
>> Cheers,
>>
>> Brian
>>
>> On Thu, Nov 19, 2015 at 11:36 AM, <eponymous at insight.rr.com> wrote:
>>
>>> Forgive me if this is not an appropriate use of this list.
>>>
>>> I have written some code to enumerate the “public’ printable attributes
>>> of an Object and output them in JSON format.
>>>
>>> The code works, but seems both inefficient and like something for which
>>> there is already an undiscovered solution in the standard libraries. In
>>> particular, I dislike the conversion of types and attributes to strings
>>> before testing a condition.
>>>
>>> I have included the code below, but you may prefer to view it on pastebin
>>>
>>> http://pastebin.com/MiFAhwYK
>>>
>>> All constructive feedback is welcome whether on topic or not.
>>>
>>> ----
>>>
>>> import ipaddress
>>> import json
>>>
>>> ipnet = ipaddress.IPv4Network('192.168.0.0/24')
>>>
>>> print(ipnet)
>>>
>>> def ipAttr(obj,att):
>>> # Returns true for all non method attributes which are public
>>> return not str(att).startswith('_') and 'method' not in
>>> str(type(getattr(obj,str(att))))
>>>
>>> def ipDict(obj):
>>> return {att: str(getattr(obj,att)) for att in dir(obj) if
>>> ipAttr(obj,att)}
>>>
>>> print(json.dumps(ipDict(ipnet),sort_keys=True, indent=4,
>>> separators=(',', ': ')))
>>>
>>> Thanks
>>>
>>> Josh
>>> ---
>>>
>>> 192.168.0.0/24
>>> {
>>> "broadcast_address": "192.168.0.255",
>>> "compressed": "192.168.0.0/24",
>>> "exploded": "192.168.0.0/24",
>>> "hostmask": "0.0.0.255",
>>> "is_global": "False",
>>> "is_link_local": "False",
>>> "is_loopback": "False",
>>> "is_multicast": "False",
>>> "is_private": "True",
>>> "is_reserved": "False",
>>> "is_unspecified": "False",
>>> "max_prefixlen": "32",
>>> "netmask": "255.255.255.0",
>>> "network_address": "192.168.0.0",
>>> "num_addresses": "256",
>>> "prefixlen": "24",
>>> "version": "4",
>>> "with_hostmask": "192.168.0.0/0.0.0.255",
>>> "with_netmask": "192.168.0.0/255.255.255.0",
>>> "with_prefixlen": "192.168.0.0/24"
>>> }
>>>
>>> _______________________________________________
>>> CentralOH mailing list
>>> CentralOH at python.org
>>> https://mail.python.org/mailman/listinfo/centraloh
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20151119/198bd70c/attachment.html>
More information about the CentralOH
mailing list