1

I get the following error when I run my code at the line that I marked with a #<---:

TypeError: 'GamePlayer' object is not subscriptable.

Apparently, not subscriptable means I don't have a certain method implemented. I don't know how this applies to my code, or how to fix it. The function second from the top is where the error occurs.

The value passed to that function is: ["b'direction:north", 'fromTime:2013-06-12 16:32:27.102400', 'fromY:0', 'fromX:0', "identity:1,'"] (I'm not sure how to get rid of the b'), but I'm sure it's not causing the problem.

@staticmethod
def data_line_to_dict(data):
    decoded = dict()
    data_peices = str(data).split(', ')
    print(data_peices)
    for d in data_peices:
        key_val = d.split(':')
        print(key_val)
        decoded[key_val[0]] = key_val[1]
    return decoded

@staticmethod
def create_from_data_line(data): #The value of data is: ["b'direction:north", 'fromTime:2013-06-12 16:32:27.102400', 'fromY:0', 'fromX:0', "identity:1,'"]
    dictionary_data = GamePlayer.data_line_to_dict(data.strip())
    p = GamePlayer()
    p.set_variable('fromX', int(p['fromX'])) #<--- where error occurs
    p.set_variable('fromY', int(p['fromY']))
    date = datetime.datetime.strptime(p['fromTime'], '%Y-%m-%d %H:%M:%S')
    p.set_variable('fromTime', data)
    p.set_variable('identity', int(p['identity']))
    return p

def create_data_line(self):
    final = ""
    for k in self.values:
        v = self.values[k]
        final += str(k) + ":" + str(v)
        final += ", "
    return final

def set_variable(self, variable, value):
    self.values[variable] = value

def get_variable(self, variable):
    return self.values[variable]

def get_microseconds_in_direction(self):
    now = datetime.datetime.today()
    diff = now - self.get_value['fromDate']
    return diff.microseconds
Community
  • 1
  • 1
sinθ
  • 11,093
  • 25
  • 85
  • 121
  • Also, the `b` in your data is because you're forcibly converting `bytes` to `str`. Use `data.decode("utf-8")` (or whatever encoding you're using) instead of `str(data)`, or open your file in "r" mode instead of "rb". – Brendan Long Jun 12 '13 at 20:45

2 Answers2

1

The problem is that you're using the [] operator on GamePlayer (p['fromX']), but you didn't implement it. I assume you meant to do p.get_variable("fromX")?

I'd recommend not doing this though. What's wrong with just naming it p.fromX? Your get_variable and set_variable functions are just reimplementing basic features of Python.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
  • *palm to forehead* I realized that I meant to make it `dictionary_data['fromX']`, but must have typed `p` by accident. – sinθ Jun 12 '13 at 21:19
0

Try p.values['fromX']or p.get_variable('fromX')

shawn
  • 336
  • 1
  • 3