Quantcast
Channel: How to use a dot "." to access members of dictionary? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by James McGuigan for How to use a dot "." to access members of dictionary?

$
0
0

The implemention used by kaggle_environments is a function called structify.

class Struct(dict):    def __init__(self, **entries):        entries = {k: v for k, v in entries.items() if k != "items"}        dict.__init__(self, entries)        self.__dict__.update(entries)    def __setattr__(self, attr, value):        self.__dict__[attr] = value        self[attr] = value# Added benefit of cloning lists and dicts.def structify(o):    if isinstance(o, list):        return [structify(o[i]) for i in range(len(o))]    elif isinstance(o, dict):        return Struct(**{k: structify(v) for k, v in o.items()})    return o

This may be useful for testing AI simulation agents in games like ConnectX

from kaggle_environments import structifyobs  = structify({ 'remainingOverageTime': 60, 'step': 0, 'mark': 1, 'board': [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]})conf = structify({ 'timeout': 2, 'actTimeout': 2, 'agentTimeout': 60, 'episodeSteps': 1000, 'runTimeout': 1200, 'columns': 7, 'rows': 6, 'inarow': 4, '__raw_path__': '/kaggle_simulations/agent/main.py' })def agent(obs, conf):  action = obs.step % conf.columns  return action

Viewing all articles
Browse latest Browse all 40

Trending Articles