Best Python code snippet using localstack_python
features.py
Source:features.py  
...117             lambda x, y: (x[0] + y[0], x[1] + y[1])) \118        .map(lambda (key, (value_sum, count)): (key, value_sum / count))119def generate_features(rdd):120    pc_trade_count_rdd = \121        fix_keys(rdd, pc_trade_count(rdd), 'pc_trade_count', 'pc')122    pc_trade_duration_rdd = \123        fix_keys(rdd, pc_trade_duration(rdd), 'pc_trade_duration', 'pc')124    pc_amount_rdd = pc_amount(rdd)125    pc_net_pnl_rdd = pc_net_pnl(rdd)126    pc_pnl_per_amount_rdd = \127        fix_keys(rdd, pnl_per_amount(pc_net_pnl_rdd, pc_amount_rdd),128            'pc_pnl_per_amount', 'pc')129    pc_amount_rdd = fix_keys(rdd, pc_amount_rdd, 'pc_amount', 'pc')130    pc_net_pnl_rdd = fix_keys(rdd, pc_net_pnl_rdd, 'pc_net_pnl', 'pc')131    p_trade_count_rdd = fix_keys(rdd, p_trade_count(rdd), 'p_trade_count', 'p')132    p_trade_duration_rdd = \133        fix_keys(rdd, p_trade_duration(rdd), 'p_trade_duration', 'p')134    p_amount_rdd = p_amount(rdd)135    p_net_pnl_rdd = p_net_pnl(rdd)136    p_pnl_per_amount_rdd = \137        fix_keys(rdd, pnl_per_amount(p_net_pnl_rdd, p_amount_rdd),138            'p_pnl_per_amount', 'p')139    p_amount_rdd = fix_keys(rdd, p_amount_rdd, 'p_amount', 'p')140    p_net_pnl_rdd = fix_keys(rdd, p_net_pnl_rdd, 'p_net_pnl', 'p')141    c_trade_count_rdd = fix_keys(rdd, c_trade_count(rdd), 'c_trade_count', 'c')142    c_trade_duration_rdd = \143        fix_keys(rdd, c_trade_duration(rdd), 'c_trade_duration', 'c')144    c_amount_rdd = c_amount(rdd)145    c_net_pnl_rdd = c_net_pnl(rdd)146    c_pnl_per_amount_rdd = \147        fix_keys(rdd, pnl_per_amount(c_net_pnl_rdd, c_amount_rdd),148            'c_pnl_per_amount', 'c')149    c_amount_rdd = fix_keys(rdd, c_amount_rdd, 'c_amount', 'c')150    c_net_pnl_rdd = fix_keys(rdd, c_net_pnl_rdd, 'c_net_pnl', 'c')151    l_trade_count_rdd = fix_keys(rdd, l_trade_count(rdd), 'l_trade_count', 'l')152    l_trade_duration_rdd = \153        fix_keys(rdd, l_trade_duration(rdd), 'l_trade_duration', 'l')154    l_amount_rdd = l_amount(rdd)155    l_net_pnl_rdd = l_net_pnl(rdd)156    l_pnl_per_amount_rdd = \157        fix_keys(rdd, pnl_per_amount(l_net_pnl_rdd, l_amount_rdd),158            'l_pnl_per_amount', 'l')159    l_amount_rdd = fix_keys(rdd, l_amount_rdd, 'l_amount', 'l')160    l_net_pnl_rdd = fix_keys(rdd, l_net_pnl_rdd, 'l_net_pnl', 'l')161    return [pc_trade_count_rdd, pc_amount_rdd, pc_net_pnl_rdd, \162            pc_trade_duration_rdd, pc_pnl_per_amount_rdd, p_trade_count_rdd, \163            p_amount_rdd, p_net_pnl_rdd, p_trade_duration_rdd, \164            p_pnl_per_amount_rdd, c_trade_count_rdd, c_amount_rdd, \165            c_net_pnl_rdd, c_trade_duration_rdd, c_pnl_per_amount_rdd, \166            l_trade_count_rdd, l_amount_rdd, l_net_pnl_rdd, \167            l_pnl_per_amount_rdd, l_trade_duration_rdd, l_trade_duration_rdd]168def normalize_feature(rdd, name):169    max_value = rdd.map(lambda (_, body): body[name]).max()170    min_value = rdd.map(lambda (_, body): body[name]).min()171    count = rdd.count()172    average = rdd \173        .map(lambda (_, body): body[name]) \174        .reduce(lambda a, b: a + b) / float(count)175    return rdd.map(lambda (key, body):176        (key, modify_record(body,177            update=(name,178                float(body[name] - average) / (max_value - min_value)))))179def fix_keys(rdd, feature, name, kind):180    keys = get_keys(rdd)181    if 'pc' == kind:182        return normalize_feature(keys \183            .map(lambda (provider_id, currency_pair, transaction_type, country):184                ((provider_id, currency_pair, transaction_type), country)) \185            .join(feature) \186            .map(lambda ((187                provider_id, currency_pair, transaction_type), (country, value)):188                    (make_key((provider_id, currency_pair, transaction_type, country)),189                        {190                            'feature_id': make_key((provider_id, currency_pair, transaction_type, country)),191                            'provider_id': provider_id,192                            'currency_pair': currency_pair,193                            'transaction_type': transaction_type,...xml_parser.py
Source:xml_parser.py  
...11import dill12base_dir = '~/'13tournament_dir = os.path.join(base_dir, "tournament_5/traces")14saving_dir = os.path.join(base_dir, "rcds.pck")15def fix_keys(odict):16    """17    only used to fix keys form xml files18    :param odict:19    :return:20    """21    keys = [k for k in odict]22    for key in keys:23        odict[key.split('@')[-1]] = odict.pop(key)24    # print(dic)25    return odict26def parse_xml(xml_bstr):27    # tree = ET.parse(path)28    # xdict = xmltodict.parse(ET.tostring(tree.getroot()))29    st = time.time()30    xdict = xmltodict.parse(xml_bstr)31    # print(time.time() - st)32    return xdict['rts.Trace']['entries']['rts.TraceEntry']33def parse_gs(gs):34    """35    parse game state from traces36    :param gs:37    :return:38    """39    st = time.time()40    gs = json.loads(json.dumps(gs))41    gs = fix_keys(gs)42    gs['pgs'] = gs.pop('rts.PhysicalGameState')43    pgs = fix_keys(gs['pgs'])44    players = pgs['players'] = pgs['players'].pop('rts.Player')45    for p in players:46        fix_keys(p)47        p['ID'] = int(p['ID'])48        p['resources'] = int(p['resources'])49    units = pgs['units'] = pgs['units'].pop('rts.units.Unit')50    for u in units:51        fix_keys(u)52        u['ID'] = int(u['ID'])53        u['player'] = int(u['player'])54        u['x'] = int(u['x'])55        u['y'] = int(u['y'])56        u['resources'] = int(u['resources'])57        u['hitpoints'] = int(u['hitpoints'])58    if gs['actions'] is None:59        gs['actions'] = []60    else:61        gs['actions'] = gs['actions'].pop('action')62    # actions = gs['actions']['action']63    if isinstance(gs['actions'], dict):64        gs['actions'] = [gs['actions']]65    # print('actions:', gs['actions'])66    for a in gs['actions']:67        fix_keys(a)68        # print(a)s69        a['ID'] = int(a.pop('unitID'))70        a['action'] = a.pop('UnitAction')71        fix_keys(a['action'])72        ua = a['action']73        ua['type'] = int(ua['type'])74        if 'parameter' in ua.keys():75            ua['parameter'] = int(ua['parameter'])76        if 'x' in ua.keys():77            ua['x'], ua['y'] = int(ua['x']), int(ua['y'])78    gs['time'] = int(gs['time'])79    pgs['width'] = int(pgs['width'])80    pgs['height'] = int(pgs['height'])81    # input()82    # gs = json.loads(json.dumps(gs))83    # print(gs)84    # TODO: this costs a lot time!85    ans = from_dict(data_class=GameState, data=gs)...Lab4_helper.py
Source:Lab4_helper.py  
...26    return tree27# if you want to print like me :)28def print_tree(tree):29    mytree = copy.deepcopy(tree)30    def fix_keys(tree):31        if type(tree) != dict:32            if type(tree) == np.int64:33                return int(tree)34        new_tree = {}35        for key in list(tree.keys()):36            if type(key) == np.int64:37                new_tree[int(key)] = tree[key]38            else:39                new_tree[key] = tree[key]40        for key in new_tree.keys():41            new_tree[key] = fix_keys(new_tree[key])42        return new_tree43    mytree = fix_keys(mytree)44    print(json.dumps(mytree, indent=4, sort_keys=True))45def generate_rules(tree):46    rules = []47    # Your solution here48    return rules49def select_split2(X,y):50    col = None51    gr = None52    return gr,col53def make_tree2(X,y,min_split_count=5):54    tree = {}55    # Your solution here56    return tree57def make_prediction(rules,x,default):...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
