TypeError: 'OldSample' object has no attribute '__getitem__'

Peter Otten __peter__ at web.de
Thu Jul 2 05:25:23 EDT 2015


Hina Imran wrote:

> The data set looks something like this, The data is from openstack
> ceilometer python API
> 
>     [
>      <OldSample
>           {
>             counter_name': cpu_util',
>              user_id': 7b',
>              resource_id': ef',
>              timestamp': 2015-07-02T08:13:55',
>              counter_volume': 0.774,
>               resource_metadata':
>                                 {
>                                  ramdisk_id': None',
>                                  flavor.vcpus': 1',
>                                   OS-EXT-AZ.availability_zone': nova',
>                                   display_name': ubuntu-io',
>                                   flavor.id': 596642d8-',
>                                   status': active', ephemeral_gb': 0',
>                                   flavor.name': m1.small.io',
>                                   disk_gb': 20',
>                                   kernel_id': None',
>                                   image.id': 5776a360-',
>                                   flavor.ram': 2048',
>                                   host': host',
>                                   flavor.ephemeral': 0',
>                                    image.name': ubuntu-io',
>                                    image_ref_url': http://b8dc',
>                                    image.links':
>                                                  [
>                                                  {
>                                                  'href': 'http://19',
>                                                  'rel': 'bookmark'
>                                                  }
>                                                  ]",
>                                                  cpu_number': 1',
>                                                  flavor.disk': 20',
>                                                  root_gb': 20',
>                                                   name':
>                                                   instance-000000d3',
>                                                   memory_mb': 2048',
>                                                   instance_type':
>                                                   596642d', vcpus': 1',
>                                                   image_ref': 5776a36',
>                                                   flavor.links':
>                                                   [
>                                                   {
>                                                   'href':
>                                                   'http://192.168.26.1',
>                                                   'rel': 'bookmark' }
>                                                   ]"
>                                                   },
>                                                   source': openstack',
>                                                   counter_unit': %',
>                                                   recorded_at':
>                                                   
2015-07-02T08:13:55.485000',
>                                                   project_id': 1670f',
>                                                   message_id': 4849',
>                                                   4counter_type': gauge'
>                                                   }>]
> 
> My code looks something like this
> 
>     
>     
>     auth=v2.Password(auth_url="http://", username="user", password="pass",
>     tenant_id='id')
>     
>     # Pubnub instance initilize
>     
>     
>     
>     sess = session.Session(auth=auth,verify=False)     #
>     token = auth.get_token(sess)
>     
>     cclient = client.get_client(2, ceilometer_url="http:",
>     token=token,verify=False)
>     
>     
>     data = cclient.samples.list(meter_name ='cpu_util')
>     
>     
>     thing = {}
>     msg = {}
>     cols = []
>     
>     def __unicode__(self):
>       return unicode(self.data)
>     
>     for row in data:
>         col = {}
>         col['x'] = row['timestamp']
>         col['y'] = row['counter_volume']
>         cols.append(col)
>     
>     
>     
>     msg['columns'] = cols
>     
>     thing['message'] = msg
>     
>     print thing
>     
>     print json.dumps(thing)
> 
> My intended out out something like this
> 
>     message: {
>           columns: [
>             ["x": 123, "y": "456"],
>             ["y": 0.045, "x": "2015-06-30T15:53:55"]
>           ]
> **here x is counter_volume and Y is timestamp**
> I need the output in this way because I want to plug this data into EON
> pubnub Charting Library. Problem is that no matter what I do it keeps on
> throwing me errors.I guess I need to learn some more how to manipulate
> list of dictionaries.

The problem is that you are not working with a dict, data seems to be a 
sequence of OldSample instances. Try changing

>         col = {}
>         col['x'] = row['timestamp']
>         col['y'] = row['counter_volume']

to

        col = {}
        col['x'] = row.timestamp
        col['y'] = row.counter_volume

If that works you can simplify it to

        col = {"x": row.timestamp, "y": row.counter_volume}





More information about the Python-list mailing list