当前位置: 首页>>代码示例>>Python>>正文


Python Filter.filter_exp方法代码示例

本文整理汇总了Python中Filter.filter_exp方法的典型用法代码示例。如果您正苦于以下问题:Python Filter.filter_exp方法的具体用法?Python Filter.filter_exp怎么用?Python Filter.filter_exp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Filter的用法示例。


在下文中一共展示了Filter.filter_exp方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: explore

# 需要导入模块: import Filter [as 别名]
# 或者: from Filter import filter_exp [as 别名]
def explore(filename, attrs, cond=False, wait=False, quiet=False):
    count = 0
    if quiet:
        wait = False
    with open(filename, "rb") as fp:
        # iterate through objects in file
        for obj in iterload(fp):

            # print out entire object
            if attrs[0] == "all":
                if not quiet:
                    print(json.dumps(obj, indent=2))
                count += 1
                if wait:
                    raw_input()

            # only print out the desired attributes
            else:
                # check if we are going to filter results
                if cond:
                
                    # the only arg given is conditional expression
                    if len(attrs) == 1:
                        # test the current object
                        if Flt.filter_exp(attrs[0], obj):
                            if not quiet:
                                print(json.dumps(obj, indent=2))
                            count += 1
                            if wait:
                                raw_input()
                    # the last arg is the conditional expression
                    else:
                        if Flt.filter_exp(attrs[-1], obj):
                            try:
                                stuff = {attr: obj[attr] for attr in attrs[:-1]}
                                if not quiet:
                                    print(json.dumps(stuff, indent=2))
                                count += 1
                            except KeyError:
                                print "ERROR: " + str(attrs[:-1]) + " contains an invalid attribute name "
                                exit()
                            if wait:
                                raw_input()

                # don't filter the results
                else:
                    # return only the desired attributes
                    try:
                        stuff = {attr: obj[attr] for attr in attrs}
                        if not quiet:
                            print(json.dumps(stuff, indent=2))
                        count += 1
                    except KeyError:
                        print "ERROR: " + str(attrs[:-1]) + " contains an invalid attribute name "
                        exit()
                    if wait:
                        raw_input()
        print "Total objects found: " + str(count)
开发者ID:MarkAWard,项目名称:rec-sys,代码行数:60,代码来源:edit_data.py

示例2: create_lookup_dict

# 需要导入模块: import Filter [as 别名]
# 或者: from Filter import filter_exp [as 别名]
def create_lookup_dict(infile, args, id_to_indx=None, indx_to_id=None, pick=False, cond=False):
    
    # if output files were not specified and pickling, set them
    if pick:
        if id_to_indx == None:
            id_to_indx = pickle_directory + "id_to_indx.p"
        else:
            id_to_indx = pickle_directory + id_to_indx 
        if indx_to_id == None:
            indx_to_id = pickle_directory + "indx_to_id.p" 
        else:
            indx_to_id = pickle_directory + indx_to_id 

    # set attr1/2 to correct values
    # attr1 used as unique id
    attr1 = args[0]
    attr2 = "none"
    if cond:
        if len(args) == 3:
            attr2 = args[1]
        if len(args) > 3:
            print "ERROR: invalid number of arguments"
    else:
        if len(args) == 2:
            attr2 = args[1]

    flag = 0
    if attr2.lower() == "none":
        flag = 1
        count = 0

    # lookup dictionary to hold id's and index number
    lookup = {}
    with open(infile, "rb") as fp:
        # iterate through objects in file
        for obj in iterload(fp):

            # use args[0] and args[1] as given
            if flag == 0:
                # check if we are filtering, test obj is cond=True
                if cond and not Flt.filter_exp(args[-1], obj):
                    continue

                # make sure a valid attribute name was given
                try:
                    attr_value = obj[attr1]
                except KeyError:
                    print "ERROR: %s, is not a valid attribute name " %attr1
                    exit()
                try:
                    attr2_value = obj[attr2]
                except KeyError:
                    print "ERROR: %s, is not a valid attribute name " %attr2
                    exit()
                # upadate dictionary
                if attr_value not in lookup:
                    lookup[ attr_value ] = attr2_value
                else:
                    print attr_value + " already in lookup dictionary. Skipping duplicate" 

            # use args[0] as id/attr1 and attr2 will be index
            if flag == 1:
                # check if we are filtering, test obj is cond=True
                if cond and not Flt.filter_exp(args[-1], obj):
                    continue

                # make sure a valid attribute name was given
                try:
                    attr_value = obj[attr1]
                except KeyError:
                    print "ERROR: %s, is not a valid attribute name " %attr1
                    exit()
                # upadate dictionary
                if attr_value not in lookup:
                    lookup[ attr_value ] = count
                    count += 1
                else:
                    print attr_value + " already in lookup dictionary. Skipping duplicate" 

    # only sort and add index if flag was 0
    if flag == 0:
        # sort by descending review count in to a list
        sorted_list = sorted(lookup.iteritems(), key=operator.itemgetter(1), reverse=True)
        # change the value in lookup to be index from sorted list
        for item, indx in zip(sorted_list, xrange(len(sorted_list))):
            lookup[item[0]] = indx

    # dict for looking up id from an index
    lookup2 = {y:x for x,y in lookup.iteritems()}

    # Pickle it
    if pick:
        with open(id_to_indx, "wb") as outfp:
            pickle.dump(lookup, outfp)
            print "Pickled id --> index dictionary into: " + id_to_indx
        with open(indx_to_id, "wb") as outfp:
            pickle.dump(lookup2, outfp)
            print "Pickled index --> id dictionary into: " + indx_to_id
    # print to test that everything is working
    else:
#.........这里部分代码省略.........
开发者ID:MarkAWard,项目名称:rec-sys,代码行数:103,代码来源:edit_data.py


注:本文中的Filter.filter_exp方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。