0

I have the following class in Python.

class RULES(object): TOTAL_REQUESTS = 'totalrequests' HTML_SIZE = 'htmlsize' JS_SIZE = 'jssize' JS_COUNT = 'jscount' DOMAIN_COUNT = 'domaincount' REQ_PER_DOMAIN = 'reqperdomain' DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests' IMAGE_SIZE = 'imagesize' IMAGE_COUNT = 'imagecount' HIGHEST_LATENCY_OBJECT = 'highestlatencyobject' LARGEST_SIZE_OBJECT = 'largestsizeobject' VARY_HEADER_OBJECTS = 'varyheaderobjects' ERROR_RESPONSES = 'errorresponses' COOKIE_REQUESTS = 'cookierequests' TTFB_REQUESTS = 'ttfb_requests' UNCACHED = 'uncached' UNCOMPRESSED = 'uncompressed' SPRITABLE = 'spritable' MULTIPLE_REQUESTS = 'multiplerequests' INLINABLE = 'inlinable' NO_SERVER_CACHE = 'noservercache' MORE_TTL = 'morettl' MEDIAN_LOAD_TIME = 'median_load_time' AVG_LOAD_TIME = 'avgloadtime' DNS = 'dns' HOST_TTL = 'host_ttl' ORIGIN_TTL = 'origin_ttl' THIRD_PARTY_HTTPS_REQ = 'thirdpartyhttpsreq' 

Now I need to write a method to iterate over all the attributes and return them as a list. I tried accessing individual attributes as

RULES.HOST_TTL , but am nit sure how to iterate over them. Is there a way in python to do that.

1
  • Why is that a class in the first place? Commented Nov 21, 2014 at 5:41

5 Answers 5

3

You can also use class_name.__dict__

>>> class RULES(object): ... TOTAL_REQUESTS = 'totalrequests' ... HTML_SIZE = 'htmlsize' ... JS_SIZE = 'jssize' ... JS_COUNT = 'jscount' ... DOMAIN_COUNT = 'domaincount' ... REQ_PER_DOMAIN = 'reqperdomain' ... DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests' ... IMAGE_SIZE = 'imagesize' ... IMAGE_COUNT = 'imagecount' ... HIGHEST_LATENCY_OBJECT = 'highestlatencyobject' ... LARGEST_SIZE_OBJECT = 'largestsizeobject' ... VARY_HEADER_OBJECTS = 'varyheaderobjects' ... ERROR_RESPONSES = 'errorresponses' ... COOKIE_REQUESTS = 'cookierequests' ... TTFB_REQUESTS = 'ttfb_requests' ... UNCACHED = 'uncached' ... UNCOMPRESSED = 'uncompressed' ... SPRITABLE = 'spritable' ... MULTIPLE_REQUESTS = 'multiplerequests' ... INLINABLE = 'inlinable' ... NO_SERVER_CACHE = 'noservercache' ... MORE_TTL = 'morettl' ... MEDIAN_LOAD_TIME = 'median_load_time' ... AVG_LOAD_TIME = 'avgloadtime' ... DNS = 'dns' ... HOST_TTL = 'host_ttl' ... ORIGIN_TTL = 'origin_ttl' ... THIRD_PARTY_HTTPS_REQ = 'thirdpartyhttpsreq' ... >>> RULES.__dict__ dict_proxy({'REQ_PER_DOMAIN': 'reqperdomain', '__module__': '__main__', 'JS_COUNT': 'jscount', 'AVG_LOAD_TIME': 'avgloadtime', 'DOMAIN_WITH_MAX_REQUESTS': 'domainwithmaxrequests', 'UNCACHED': 'uncached', 'NO_SERVER_CACHE': 'noservercache', 'HOST_TTL': 'host_ttl', 'MULTIPLE_REQUESTS': 'multiplerequests', 'MORE_TTL': 'morettl', 'DNS': 'dns', 'SPRITABLE': 'spritable', '__dict__': <attribute '__dict__' of 'RULES' objects>, 'LARGEST_SIZE_OBJECT': 'largestsizeobject', 'TTFB_REQUESTS': 'ttfb_requests', 'TOTAL_REQUESTS': 'totalrequests', '__weakref__': <attribute '__weakref__' of 'RULES' objects>, 'ORIGIN_TTL': 'origin_ttl', 'VARY_HEADER_OBJECTS': 'varyheaderobjects', 'MEDIAN_LOAD_TIME': 'median_load_time', 'COOKIE_REQUESTS': 'cookierequests', 'DOMAIN_COUNT': 'domaincount', 'THIRD_PARTY_HTTPS_REQ': 'thirdpartyhttpsreq', 'HTML_SIZE': 'htmlsize', 'UNCOMPRESSED': 'uncompressed', 'HIGHEST_LATENCY_OBJECT': 'highestlatencyobject', 'INLINABLE': 'inlinable', 'ERROR_RESPONSES': 'errorresponses', 'IMAGE_SIZE': 'imagesize', 'JS_SIZE': 'jssize', 'IMAGE_COUNT': 'imagecount', '__doc__': None}) 

Filter

Variable names and values:

>>> { k:v for k,v in RULES.__dict__.iteritems() if not k.startswith('__') } {'JS_COUNT': 'jscount', 'DOMAIN_WITH_MAX_REQUESTS': 'domainwithmaxrequests', 'UNCACHED': 'uncached', 'MULTIPLE_REQUESTS': 'multiplerequests', 'SPRITABLE': 'spritable', 'TTFB_REQUESTS': 'ttfb_requests', 'ORIGIN_TTL': 'origin_ttl', 'VARY_HEADER_OBJECTS': 'varyheaderobjects', 'MEDIAN_LOAD_TIME': 'median_load_time', 'HTML_SIZE': 'htmlsize', 'UNCOMPRESSED': 'uncompressed', 'ERROR_RESPONSES': 'errorresponses', 'IMAGE_SIZE': 'imagesize', 'AVG_LOAD_TIME': 'avgloadtime', 'REQ_PER_DOMAIN': 'reqperdomain', 'NO_SERVER_CACHE': 'noservercache', 'HOST_TTL': 'host_ttl', 'JS_SIZE': 'jssize', 'DNS': 'dns', 'LARGEST_SIZE_OBJECT': 'largestsizeobject', 'DOMAIN_COUNT': 'domaincount', 'TOTAL_REQUESTS': 'totalrequests', 'COOKIE_REQUESTS': 'cookierequests', 'IMAGE_COUNT': 'imagecount', 'MORE_TTL': 'morettl', 'HIGHEST_LATENCY_OBJECT': 'highestlatencyobject', 'INLINABLE': 'inlinable', 'THIRD_PARTY_HTTPS_REQ': 'thirdpartyhttpsreq'} 

Variable names:

>>> [ k for k,v in RULES.__dict__.iteritems() if not k.startswith('__') ] ['REQ_PER_DOMAIN', 'JS_COUNT', 'AVG_LOAD_TIME', 'DOMAIN_WITH_MAX_REQUESTS', 'UNCACHED', 'NO_SERVER_CACHE', 'HOST_TTL', 'MULTIPLE_REQUESTS', 'MORE_TTL', 'DNS', 'SPRITABLE', 'LARGEST_SIZE_OBJECT', 'TTFB_REQUESTS', 'TOTAL_REQUESTS', 'ORIGIN_TTL', 'VARY_HEADER_OBJECTS', 'MEDIAN_LOAD_TIME', 'COOKIE_REQUESTS', 'DOMAIN_COUNT', 'THIRD_PARTY_HTTPS_REQ', 'HTML_SIZE', 'UNCOMPRESSED', 'HIGHEST_LATENCY_OBJECT', 'INLINABLE', 'ERROR_RESPONSES', 'IMAGE_SIZE', 'JS_SIZE', 'IMAGE_COUNT'] 
Sign up to request clarification or add additional context in comments.

Comments

1

Use the dir function. It'll likely give you more than what you're looking for, but you can filter it (e.g. only take ALL_CAPS words).

Sample:

>>> class RULES(object): ... TOTAL_REQUESTS = 'totalrequests' ... HTML_SIZE = 'htmlsize' ... JS_SIZE = 'jssize' ... JS_COUNT = 'jscount' ... DOMAIN_COUNT = 'domaincount' ... REQ_PER_DOMAIN = 'reqperdomain' ... DOMAIN_WITH_MAX_REQUESTS = 'domainwithmaxrequests' ... IMAGE_SIZE = 'imagesize' ... >>> import re >>> all_caps = re.compile('^[A-Z_]+$') >>> [attr for attr in dir(RULES) if all_caps.match(attr)] ['DOMAIN_COUNT', 'DOMAIN_WITH_MAX_REQUESTS', 'HTML_SIZE', 'IMAGE_SIZE', 'JS_COUNT', 'JS_SIZE', 'REQ_PER_DOMAIN', 'TOTAL_REQUESTS'] 

and once you have the name, it's easy to get value:

[getattr(RULES, attr) for attr in dir(fules) if all_caps.match(attr)] 

Finally, if you're amenable to using a different type, you might be able to use an Enum here. enum wasn't added to the standard library until python3.4, but there are backports for python2.x. I believe that your request would then look like:

class RULES(enum.Enum): X = 'foo' Y = 'bar' # ... for name, attr in RULES.__members__.items(): print(name, attr.value) 

2 Comments

The internal dict attribute is the right way to obtain only the attributes as dir will return both attributes and methods.
@mdadm -- Not sure I'm following. dir simply filters and sorts the keys of the __dict__ attribute (provided a __dir__ method isn't present). So, if dir gives you methods, then __dict__ will necessarily give you them as well. (See the link to the docs on dir).
1

Try this:

print dir(RULES) print RULES.__dict__ 

First will give an List of the attributes of class including classes which it inherites. Second will give you an dictionary representation for the same

Comments

0

you might want to look into your classe's __dir__ attribute, which returns the class itself as a dictionary

Comments

0

You can use inspect to get attribute name and value.

import inspect all_attr = inspect.getmembers(RULES) [attr for attr in all_attr if not(attr[0].startswith('__') and attr[0].endswith('__'))] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.