python - Using hasattr() when the attribute is not known -


i'm trying combine 2 applications visualising binaries, binvis , droidcolors. since droidcolors in c, i'm trying make binvis replicate functionality offered droidcolors.

here relevant snippets code i've written far. complete file here: https://pastebin.com/0c7fu9w2.

class _color:     def __init__(self, data, **kwargs):         self.data = data         key, value in kwargs.items():             setattr(self, key, value)         s = list(set(data))         s.sort()         self.symbol_map = {v : (i, v) in enumerate(s)}      def __len__(self):         return len(self.data)      def point(self, x):         if hasattr(self, 'class_defs') , (self.class_defs[0] <= x <= self.class_defs[1]):             return self.return_droidcolors_classdefs(x)         elif hasattr(self, 'dat') , (self.dat[0] <= x <= self.dat[1]):             return self.return_droidcolors_data(x)         elif hasattr(self, 'field_ids') , (self.field_ids[0] <= x <= self.field_ids[1]):             return self.return_droidcolors_field_ids(x)         elif hasattr(self, 'header') , (self.header[0] <= x <= self.header[1]):             return self.return_droidcolors_header(x)         elif hasattr(self, 'links') , (self.links[0] <= x <= self.links[1]):             return self.return_droidcolors_links(x)         elif hasattr(self, 'proto_ids') , (self.proto_ids[0] <= x <= self.proto_ids[1]):             return self.return_droidcolors_proto_ids(x)         elif hasattr(self, 'method_ids') , (self.method_ids[0] <= x <= self.method_ids[1]):             return self.return_droidcolors_method_ids(x)         elif hasattr(self, 'string_ids') , (self.string_ids[0] <= x <= self.string_ids[1]):             return self.return_droidcolors_string_ids(x)         elif hasattr(self, 'type_ids') , (self.type_ids[0] <= x <= self.type_ids[1]):             return self.return_droidcolors_type_ids(x)         elif hasattr(self, 'block') , (self.block[0] <= x <= self.block[1]):             return self.block[2]         else:             return self.getpoint(x)  # --- snip ---  class colordroidcolors(_color):     def getpoint(self, x):         return [0, 0, 0]  # --- snip ---  dex_handle = dexparser(args[0]) dex_header_info = dex_handle.header_info()  dex_class_defs = (dex_header_info['class_defs_off'], dex_header_info['class_defs_off'] + (64 * dex_header_info['class_defs_size']) - 1) dex_data       = (dex_header_info['data_off'], dex_header_info['data_off'] + dex_header_info['data_size'] - 1) dex_field_ids  = (dex_header_info['field_ids_off'], dex_header_info['field_ids_off'] + (8 * dex_header_info['field_ids_size']) - 1) dex_header     = (0x0, dex_header_info['header_size'] - 1) dex_links      = (link_off, link_off + dex_header_info['link_size'] - 1) dex_proto_ids  = (dex_header_info['proto_ids_off'], dex_header_info['proto_ids_off'] + (12 * dex_header_info['proto_ids_size']) - 1) dex_method_ids = (dex_header_info['method_ids_off'], dex_header_info['method_ids_off'] + (8 * dex_header_info['method_ids_size']) - 1) dex_string_ids = (dex_header_info['string_ids_off'], dex_header_info['string_ids_off'] + (4 * (dex_header_info['string_ids_size']) - 1)) dex_type_ids   = (dex_header_info['type_ids_off'], dex_header_info['type_ids_off'] + (4 * dex_header_info['type_ids_size']) - 1)  kwarg_dict = {     "class_defs": dex_class_defs,     "dat": dex_data,     "field_ids": dex_field_ids,     "header": dex_header,     "links": dex_links,     "proto_ids": dex_proto_ids,     "method_ids": dex_method_ids,     "string_ids": dex_string_ids,     "type_ids": dex_type_ids,     }  i, item in enumerate(dex_handle.protoids_list()):     if item[2]:         kwarg_dict['dex_proto_params_' + str(i)] = (item[2], (0x0 + item[2]) * 4 + 4)  csource = colordroidcolors(     d,     **kwarg_dict ) 

i'm passing 10000+ kwargs method, , require way test within method if kwarg exists , depending on same, return value. kwargs have particular prefix, followed sequential numbers starting 1. tried using loop, slowed program down crawl.

could please suggest me way out?

use getattr() , list of attributes test:

attrs = ['class_defs', 'dat', 'field_ids', ..., 'block'] name in attrs:     value = getattr(self, name, none)     if value not none , value[0] <= x <= value[1]         if name != 'block':             method_name = 'return_droidcolors_' + name             return getattr(self, method_name)(x)         else:             return value[2]  # none of attributes matched return self.getpoint(x) 

note in __init__ method, use self.__dict__.update(kwargs) rather loop through dictionary , call setattr() each time.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -