【百度实习录】新人练习

练习1:

import json
try:
    d1 = json.loads('{"name":"youlin","type":{"name":"seq", "parameter":["1", "2"]}}')
    d2 = json.loads('{"name":"qixuan02","type":{"name":"seq","parameter":["1", "2"]}}')
except:
    print 'load json error!!'

class Json:
    def __init__(self):
        self.json_items = []

    def append(self, json_item):
        self.json_items.append(json_item)
        return self

    def __repr__(self):
        json_dump = ''
        for item in self.json_items:
            json_dump += str(item)
        return json_dump


class JsonItem:
    def __init__(self, reason, expected, actual):
        self.reason = reason
        self.expected = expected
        self.actual = actual

    def __repr__(self):
        return 'Reason: {0}\nExpected:{1}\nActual:{2}' \
               .format(self.reason, _format_value(self.expected), _format_value(self.actual))


def _indent(s):
    return '\n'.join('  ' + line for line in s.splitlines())


def _format_value(value):
    return _indent(json.dumps(value, sort_keys=True, indent=4))


def _is_dict_same(expected, actual, keys):
    for key in expected:
        if not key in actual:
            return False, \
                   Json().append(
                        JsonItem('Expected key "{0}" Missing from Actual'
                                      .format(key),
                                  expected,
                                  actual))
        if not key in keys:
            # have to change order
            are_same_flag, json = _are_same(expected[key], actual[key], keys)
            if not are_same_flag:
                return False, \
                       json.append(JsonItem('Different values', expected[key], actual[key]))
    return True, Json()

def _is_list_same(expected, actual, keys):
    for i in xrange(len(expected)):
        are_same_flag, json = _are_same(expected[i], actual[i], keys)
        if not are_same_flag:
            return False, \
                   json.append(
                       JsonItem('Different values', expected[i], actual[i]))
    return True, Json()


def _are_same(expected, actual, keys):
    if expected is None:
        return expected == actual, Json()

    # Ensure they are of same type
    if type(expected) != type(actual):
        return False, \
               Json().append(
                   JsonItem('Type Mismatch: Expected Type: {0}, Actual Type: {1}'
                                .format(type(expected), type(actual)),
                             expected,
                             actual))

    # Compare primitive types immediately
    if type(expected) in (int, str, bool, long, float, unicode):
        return expected == actual, Json()

    if len(expected) != len(actual):
        return False, \
                Json().append(
                    JsonItem('Length Mismatch: Expected Length: {0}, Actual Length: {1}'
                                 .format(len(expected), len(actual)),
                                  expected,
                                  actual))
 
    if isinstance(expected, dict):
        return _is_dict_same(expected, actual, keys)

    if isinstance(expected, list):
        return _is_list_same(expected, actual, keys)

    return False, Json().append(JsonItem('Unhandled Type: {0}'
                                             .format(type(expected)), expected, actual))

def json_are_same(a, b, keys=[]):
    return _are_same(a, b, keys)

print json_are_same(a1, a2, [])

练习2:

cat test.txt | awk -F '&luw' 'BEGIN {print "[BEGIN PARSE..]" }  
{
	wordstr=substr($2, 2);
	split(wordstr, words, "#");
	channelstr=substr($3, 3);
	wordstr=substr($2, 2);
	split(wordstr, words, "#");
	split($7, temp_arr, "&luctr:");
	split(temp_arr[2], temp_arr2, "&lumidcpm:");
	split(temp_arr2[1], luctrs, "#");
	times[1]=0;
	times[2]=0;
	times[3]=0;
	times[4]=0;
	times[5]=0;
	times[6]=0;
	times[7]=0;
	times[8]=0;
	times[9]=0;
	for (i in words) {
		channel=substr(channelstr, i, 1);
		times[channel]++;
		print "日志行数 "NR"  单词:"words[i]"  通道:"channel"  pctr:"luctrs[i];
	}
} 
END {
	for(i=1; i<10; i++) {
		print "Channel "i" show "times[i]" times";
	}
}'


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值