Пример #1
0
 def subscribe(self):
     print('开始订阅')
     utils.click(self.driver, 'cn.xuexi.android:id/comm_head_xuexi_mine')
     utils.click(self.driver, 'cn.xuexi.android:id/my_subscribe_tv')
     utils.click(
         self.driver,
         '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.TextView[2]'
     )
     time.sleep(2)
     tmp = 'tmp.png'
     while self.subscription_counter:
         element = utils.find_element(
             self.driver,
             '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.FrameLayout[1]/android.widget.LinearLayout[2]/android.widget.ImageView'
         )
         element.save_screenshot(tmp)
         if utils.compare_images(tmp, 'checked.png'):
             utils.swipe_up(self.driver, y_ratio=5 / 9)
             if utils.find_element(
                     self.driver,
                     '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.TextView'
             ):
                 print('看到底线了')
                 utils.click(
                     self.driver,
                     '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.HorizontalScrollView/android.widget.LinearLayout/android.support.v7.app.ActionBar.Tab[2]/android.widget.TextView'
                 )
         else:
             if utils.click(
                     self.driver,
                     '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.support.v4.view.ViewPager/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ListView/android.widget.FrameLayout[1]/android.widget.LinearLayout[2]'
             ):
                 self.subscription_counter -= 1
                 print(f'订阅了:{utils.get_text(self.driver, "")}')
Пример #2
0
    def update_translation_file(self, filename, trans_items, lang_code):
        fullpath = os.path.join(self.target, filename)
        try:
            doc = minidom.parse(fullpath)
        except (IOError, expat.ExpatError):
            # FIXME: New file needs to be created
            '''
            impl = minidom.getDOMImplementation()
            doc = impl.createDocument(None, 'root', None)
            '''
            return

        root = doc.documentElement
        for trans_item in trans_items:
            translation = trans_item['translations'][lang_code]
            if not translation:
                data = find_element(doc, root, 'data',
                                    {'name': trans_item['id']})
                if data:
                    root.removeChild(data)
                continue
            data, created = find_or_create_element(doc, root, 'data',
                                                   {'name': trans_item['id']})
            if created:
                data.setAttribute('xml:space', 'preserve')
            value, created = find_or_create_element(doc, data, 'value')
            if not created:
                for n in value.childNodes:
                    value.removeChild(n)
            value.appendChild(doc.createTextNode(translation))

        with open(fullpath, 'w') as f:
            f.write(doc.toxml().encode('utf-8'))
Пример #3
0
 def play_audio(self, item, title):
     print(f'点开了视频《{title}》')
     get_duration = lambda: utils.get_text(self.driver, item + '/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TextView') or \
                            utils.get_text(self.driver, item + '/android.view.ViewGroup/android.widget.LinearLayout[2]/android.widget.TextView')
     duration_text = get_duration()
     if not duration_text:
         utils.swipe_up(self.driver)
         duration_text = get_duration()
     duration_text = duration_text.split(':')
     duration = int(duration_text[0]) * 60 + int(duration_text[1])
     print(f'这个视频要看:{duration}秒')
     utils.click(self.driver, item)
     while True:
         start = time.time()
         time.sleep(1)
         self.wait(duration)
         dots = 1
         replay = '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout[1]/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.FrameLayout[3]/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout'
         while not utils.find_element(self.driver, replay):
             print('再等会' + dots * '.', end='\r', flush=True)
             dots = max((dots + 1) % 6, 1)
             time.sleep(1)
         print('视频放完了')
         self.audiuo_time -= time.time() - start
         if self.audio_counter == 1 and self.audiuo_time > 0:
             print(f'虽然视频看完了,但是还剩下{int(self.audiuo_time)}秒,所以重看一次')
             time.sleep(random.uniform(0.8, 1.8))
             if utils.click(self.driver, replay):
                 self.wait(duration)
         else:
             break
     self.audio_counter -= 1
     self.audio.append(title)
     print(f'看完惹,还有{self.audio_counter}个要看')
     self.driver.back()
Пример #4
0
def create_uni_method(op_name, klass, jacobians = None):
    """
    Creates a new univariate special method, such as A.__neg__() <=> -A,
    for target class. The method is called __op_name__.
    """
    # This function will become the actual method.
    op_modules = [operator, __builtin__]
    op_names = [ op_name, op_name + '_']

    op_function_base = find_element( op_names,op_modules, error_on_fail = True)
    #many such functions do not take keyword arguments, so we need to wrap them 
    def op_function(self):
        return op_function_base(self)
    
    def new_method(self):
        # This code creates a Deterministic object.
        if not check_special_methods():
            raise NotImplementedError, 'Special method %s called on %s, but special methods have been disabled. Set pymc.special_methods_available to True to enable them.'%(op_name, str(self))
            
        jacobian_formats = {'self' : 'transformation_operation'}
        return pm.Deterministic(op_function,
                                'A Deterministic returning the value of %s(%s)'%(op_name, self.__name__),
                                '('+op_name+'_'+self.__name__+')',
                                parents = {'self':self},
                                trace=False,
                                plot=False, 
                                jacobians=jacobians,
                                jacobian_formats = jacobian_formats)
    # Make the function into a method for klass. 
    
    new_method.__name__ = '__'+op_name+'__'
    setattr(klass, new_method.__name__, UnboundMethodType(new_method, None, klass))
Пример #5
0
    def update_source_file(self, source_filename, trans_items):
        fullpath = os.path.join(self.target, source_filename)
        try:
            doc = minidom.parse(fullpath)
        except (IOError, expat.ExpatError):
            raise

        root = doc.documentElement
        for trans_item in trans_items:
            data = find_element(doc, root, 'data', {'name': trans_item['id']})
            if data:
                comment, created = find_or_create_element(doc, data, 'comment')
                for n in comment.childNodes:
                    comment.removeChild(n)
                comment.appendChild(doc.createTextNode('t'))

        with open(fullpath, 'w') as f:
            f.write(doc.toxml().encode('utf-8'))
Пример #6
0
def create_rl_bin_method(op_name, klass,  jacobians = {}):
    """
    Creates a new binary special method with left and right versions, such as
        A.__mul__(B) <=> A*B,
        A.__rmul__(B) <=> [B*A if B.__mul__(A) fails]
    for target class. The method is called __op_name__.
    """
    # Make left and right versions.
    for prefix in ['r','']:
        # This function will became the methods.
        op_modules = [operator, __builtin__]
        op_names = [ op_name, op_name + '_']

        op_function_base = find_element( op_names, op_modules, error_on_fail = True)
        #many such functions do not take keyword arguments, so we need to wrap them 
        def op_function(a, b):
            return op_function_base(a, b)
            
        def new_method(self, other, prefix=prefix):
            if not check_special_methods():
                raise NotImplementedError, 'Special method %s called on %s, but special methods have been disabled. Set pymc.special_methods_available to True to enable them.'%(op_name, str(self))
            # This code will create one of two Deterministic objects.
            if prefix == 'r':
                parents = {'a':other, 'b':self}
                
            else:
                parents = {'a':self, 'b':other}
            jacobian_formats = {'a' : 'broadcast_operation',
                               'b' : 'broadcast_operation'}
            return pm.Deterministic(op_function,
                                    'A Deterministic returning the value of %s(%s,%s)'%(prefix+op_name,self.__name__, str(other)),
                                    '('+'_'.join([self.__name__,prefix+op_name,str(other)])+')',
                                    parents,
                                    trace=False,
                                    plot=False,
                                    jacobians = jacobians,
                                    jacobian_formats = jacobian_formats)
        # Convert the functions into methods for klass.
        new_method.__name__ = '__'+prefix+op_name+'__'
        setattr(klass, new_method.__name__, UnboundMethodType(new_method, None, klass))
def get_patterns_from_dict(event_elements):
    """
    将提取出的事件要素转换成特征
    :param event_elements: 字典形式的事件要素
    :return patterns: 字典形式的特征
    """
    patterns = dict()

    # 从事件要素中的"加刑因素"提取出三个特征:01死亡人数、02重伤人数、03轻伤人数
    patterns["01死亡人数"], patterns["02重伤人数"], patterns["03轻伤人数"] = extract_seg(
        "".join(event_elements["加刑因素"]))

    # 从事件要素中的"主次责任"提取出特征:04责任认定
    patterns["04责任认定"] = find_element(event_elements["主次责任"], "全部责任")

    # 从事件要素中的"加刑因素"提取出8个特征
    patterns["05是否酒后驾驶"] = find_element(event_elements["加刑因素"], "酒")
    patterns["06是否吸毒后驾驶"] = find_element(event_elements["加刑因素"], "毒")
    patterns["07是否无证驾驶"] = find_element(event_elements["加刑因素"], "驾驶证", "证")
    patterns["08是否无牌驾驶"] = find_element(event_elements["加刑因素"], "牌照", "牌")
    patterns["09是否不安全驾驶"] = find_element(event_elements["加刑因素"], "安全")
    patterns["10是否超载"] = find_element(event_elements["加刑因素"], "超载")
    patterns["11是否逃逸"] = find_element(event_elements["加刑因素"], "逃逸", "逃离")
    patterns["是否初犯偶犯"] = 1 - int(find_element(event_elements["加刑因素"], "前科"))

    # 从事件要素中的"减刑因素"提取出7个特征
    patterns["12是否抢救伤者"] = find_element(event_elements["减刑因素"], "抢救", "施救")
    patterns["13是否报警"] = find_element(event_elements["减刑因素"], "报警", "自首", "投案")
    patterns["14是否现场等待"] = find_element(event_elements["减刑因素"], "现场", "等候")
    patterns["15是否赔偿"] = find_element(event_elements["减刑因素"], "赔偿")
    patterns["16是否认罪"] = find_element(event_elements["减刑因素"], "认罪")
    patterns["17是否如实供述"] = find_element(event_elements["减刑因素"], "如实")
    if patterns["是否初犯偶犯"] == 0:
        patterns["18是否初犯偶犯"] = "0"
    else:
        patterns["18是否初犯偶犯"] = "1"
    return patterns
Пример #8
0
 def read_article(self, item, title):
     print(f'点开了文章《{title}》')
     utils.click(self.driver, item)
     while self.collect_counter:
         print(f'点击收藏')
         # 收藏
         self.collect_counter -= utils.click(
             self.driver,
             '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ImageView[1]'
         )
         time.sleep(random.uniform(0.8, 1.8))
         # 可能会有”我知道了“的提示
         utils.click(self.driver, 'cn.xuexi.android:id/btn_right_text')
         time.sleep(random.uniform(0.8, 1.8))
         # 取消收藏
         print(f'取消收藏')
         utils.click(
             self.driver,
             '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ImageView[1]'
         )
         time.sleep(random.uniform(0.8, 1.8))
     while self.share_counter:
         print('点击分享')
         utils.click(
             self.driver,
             '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.ImageView[2]'
         )
         time.sleep(random.uniform(0.8, 1.8))
         # 分享到微信
         if utils.click(
                 self.driver,
                 '/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.RelativeLayout/android.widget.RelativeLayout/android.support.v4.view.ViewPager/android.widget.GridView/android.widget.RelativeLayout[2]/android.widget.ImageView'
         ):
             # 双开微信?
             utils.click(
                 self.driver,
                 '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.ScrollView/android.widget.ListView/android.widget.LinearLayout/android.widget.LinearLayout[1]'
             )
             self.share_counter -= 1
         self.driver.back()
     print('开始阅读')
     while True:
         read_time = random.uniform(3, 6)
         time.sleep(read_time)
         print(f'读了{round(read_time, 2)}秒', end='\r', flush=True)
         self.article_time -= read_time
         # 观点
         if utils.find_element(
                 self.driver,
                 '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout[1]/android.view.ViewGroup/android.support.v7.widget.RecyclerView/android.widget.LinearLayout[1]/android.widget.TextView'
         ):
             print('看到评论区了,那就可以不读了')
             if self.artical_counter == 1 and self.article_time > 0:
                 read_time = random.uniform(self.article_time,
                                            self.article_time + 2)
                 print(
                     f'读完了,但距离完成任务还有{round(self.article_time, 2)}秒,所以随便等个{round(read_time, 2)}秒'
                 )
                 self.wait(read_time)
             break
         else:
             utils.swipe_up(self.driver)
     self.artical_counter -= 1
     self.articles.append(title)
     print(f'读完惹,还有{self.artical_counter}篇要读')
     self.driver.back()
Пример #9
0
tanh_jacobians = {'x' : lambda x : 1.0 - np.tanh(x)**2}

arcsinh_jacobians = {'x' : lambda x : (1+x**2)**-.5}
arccosh_jacobians = {'x' : lambda x : (x+1)**-.5*(x-1.0)**-.5}
arctanh_jacobians = {'x' : lambda x : 1.0/(1-x**2) }


def wrap_function_accum(function):
    def wrapped_function(a, axis = None):
        return function(a, axis)
    wrapped_function.__doc__ = function.__doc__
    
    return wrapped_function

for function_name in _accumulation_deterministics:
    wrapped_function = wrap_function_accum(find_element(function_name, np, error_on_fail = True))
    
    jacobians = find_element(function_name + "_jacobians", locals(), error_on_fail = True)
    
    locals()[function_name] = deterministic_from_funcs(function_name, wrapped_function, jacobians, jacobian_formats = {'a' : 'accumulation_operation'})


for function_name in _boolean_accumulation_deterministics:
    wrapped_function = wrap_function_accum(find_element(function_name, np, error_on_fail = True))

    locals()[function_name] = deterministic_from_funcs(function_name, wrapped_function)



def wrapped_function_trans(function):
    def wrapped_function(x):