When working with other people’s packages tit can sometimes be frustrating to not know exactly what types of things are in the objects you are creating. Especially when the documentation is lacking and you have to dig through the source to find the answers you seek. Luckily in python 2.7+ (I think) there is a way to list methods and attributes of an object. Let’s take a look at one of my galaxy objects - dataSet.galaxies[0]. We can see what lies inside if we tack a __dict__ on the end like so:

In [1]: dataSet.galaxies[0].__dict__
Out[1]:
{'ALPHA': 1.2212573740203678,
 'BETA': -0.9541605597579632,
 'FMR': 82.891999999999996,
 'FMR_cv': 1.9885869336675259,
 'HMR': 44.951360000000001,
 'HMR_cv': 1.9472879450163203,
 'L': array([ -1.81083747e+11,   1.20597990e+11,   4.39587033e+10]),
 'RS_halo_d': 0,
 'RS_halo_gmass': 0,
 'RS_halo_index': -1,
 'RS_halo_isparent': 0,
 'RS_halo_mass': 0,
 'RS_halo_smass': 0,
 'Z': 0.0,
 'central': -1,
 'cm': array([ 10345.936     ,   9542.06628571,   9012.98285714]),
 'gas_mass': 108890025.46897629,
 'glist': [29820L,....],
 'index': 0,
 'max_cv': 2.2245953691479929,
 'max_vphi': 113.64515965485172,
 'sfr': 0.0,
 'slist': [],
 'stellar_mass': 0.0}

Now if you have a ton of information/attributes attached to this object, it can get pretty messy quickly. If we tack the keys() function on the end it cleans things up quite a bit

In [2]: dataSet.galaxies[0].__dict__.keys()
Out[2]:
['RS_halo_gmass',
'slist',
'cm',
'max_cv',
'RS_halo_d',
'index',
'RS_halo_mass',
'HMR',
'FMR_cv',
'HMR_cv',
'stellar_mass',
'sfr',
'FMR',
'L',
'max_vphi',
'BETA',
'glist',
'Z',
'RS_halo_isparent',
'central',
'RS_halo_smass',
'gas_mass',
'ALPHA',
'RS_halo_index']

Now don’t ask me why it’s ordered the way it is; the point is you now know every attribute contained in the object.

But what about methods? Well apparently there is an equivalent function called dir(object) that pretty much does the same thing as .__dirs__.keys()

In [3]: dir(dataSets[0])
Out[3]:
['L_STARS',
'O0',
'Ol',
'RS_dmlist',
'RS_glist',
'RS_haloIndexes',
'RS_haloSeparations',
'RS_halos',
'RS_slist',
'SORAD_PRESENT',
'TIPSY_L_convert',
'TIPSY_M_convert',
'TIPSY_V_convert',
'__class__',
'__delattr__',
'__dict__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__module__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'__weakref__',
'boxsize',
'boxsize_h',
'deleteParticleData',
'deleteParticleIDs',
'flagAge',
'flagCooling',
'flagFB',
'flagFH2',
'flagMetals',
'flagSFR',
'galaxies',
'getParticleData',
'getParticleIDs',
'gne',
'grho',
'gsigma',
'gu',
'h',
'loadData',
'n_bndry',
'n_bulge',
'n_disk',
'n_dm',
'n_gas',
'n_star',
'num_gals',
'process_galaxies',
'process_halos',
'rconvert',
'read_galaxies',
'read_rockstar',
'read_skid',
'read_sorad',
'redshift',
'saveData',
'skid_dmlist',
'skid_glist',
'skid_slist',
'snap',
'snapdir',
'snapnum',
'time',
'vfactor']

but this time we can see the methods such as read_sorad() and read_rockstar(). I’m not sure if there is a way to isolate the methods yet but regardless either command is quite helpful when dealing with another’s code.


Comments

comments powered by Disqus