我们从Python开源项目中,提取了以下48个代码示例,用于说明如何使用parse.parse()。
def handle_select_answer(self): if self.current_question is None: return TextSendMessage(text='????????????' '?????????????????') reply = parse('???{answer}', self.message)['answer'] logger.info('SELECT{Q: %s, A: %s}', self.current_question.uid, reply) is_correct = self.current_question.answer == reply self.user.save_answer(self.current_question, is_correct) score = self.user.get_current_score() if self.current_question.answer == reply: response = TextSendMessage(text=f'{_.CORRECT} ?? {score} ?') else: response = TextSendMessage( text=f'{_.WRONG} ?? {score} ?' ) return [response, *self.ask_next_question()]
def load(self): absfilename=tkFileDialog.askopenfilename(title="Choose an algorithm file",filetypes=[("Algorithm",("*.f90","*.F90")),("All files","*.*")]) # the parser requires us to be in the same directory. This should be fixed. path,filename=os.path.split(absfilename) os.chdir(path) ast,invokeInfo=parse(filename,api="gunghoproto") self.algtext.delete(1.0, END) self.psy=PSyFactory("gunghoproto").create(invokeInfo) # ************************************* # USE invoke object (or some unique text from the invoke) as the tag so each object gets its own callback? # need to link invoke objects to line numbers (need to be provided by parser) # hacky temporary alternative just count invokes for the moment invokeCount=0 for line in str(ast).split("\n"): if "invoke" in line.lower(): tag="invoke"+str(invokeCount) self.algtext.insert(tk.INSERT, line+"\n", tag) bind=Bind(self.algtext,tag,self.psy.invokes.invokeList[invokeCount],self.psytext,self.interact,self.c,self._canvasControl) self.algtext.tag_bind(tag,"<Enter>", bind.entry) self.algtext.tag_bind( tag, '<Leave>', bind.leave ) self.algtext.tag_bind( tag, '<ButtonPress-1>', bind.button ) invokeCount+=1 else: self.algtext.insert(tk.INSERT, line+"\n")
def test_cli(): with temp_chdir() as d: settings = copy_default_settings() settings['cli'] = True create_package(d, 'invalid-name', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['entry_point'] == ( '\n' " 'entry_points': {\n" " 'console_scripts': [\n" " 'invalid-name = invalid_name.cli:invalid_name',\n" ' ],\n' ' },\n' )
def test_licenses_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['licenses'] = ['mit', 'apache2'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['license_info'] == ( 'both\n\n' '- [MIT License](https://choosealicense.com/licenses/mit)\n' '- [Apache License, Version 2.0](https://choosealicense.com/licenses/apache-2.0)' '\n\nat your option' )
def test_badges_single(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['readme']['badges'] = [ { 'image': 'https://img.shields.io/pypi/v/ok.svg', 'target': 'https://pypi.org/project/ok', 'alt': 'Latest PyPI version' } ] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == ( '\n' '.. image:: https://img.shields.io/pypi/v/ok.svg\n' ' :target: https://pypi.org/project/ok\n' ' :alt: Latest PyPI version\n' '\n' )
def test_build_matrix_mixed(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['ci'] = ['travis'] settings['pyversions'] = ['3.6', '2.7', 'pypy3', 'pypy'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, '.travis.yml')) parsed = parse(TEMPLATE, contents) assert parsed['build_matrix'] == ( '\n - python: 2.7' '\n env: TOXENV=py27' '\n - python: 3.6' '\n env: TOXENV=py36' '\n - python: pypy2.7-5.8.0' '\n env: TOXENV=pypy' '\n - python: pypy3.5-5.8.0' '\n env: TOXENV=pypy3' )
def _rerun_stalled_tasks(self, tasks): now = datetime.datetime.now(pytz.UTC) for task in tasks: timeout = datetime.timedelta(seconds=task.job.timeout) if not timeout: continue res = parse.parse(TASK_TAKEN_FMT, task.status.description) if not res: continue taken_on = dateutil.parser.parse(res['date']) extra = datetime.timedelta(seconds=STALE_TASK_EXTRA_TIME) deadline = taken_on + timeout + extra if deadline > now: continue taken_by = res['runner_id'] logger.debug("Task %s on PR %d is stale, recreating. Was " "taken on %s by %s timeout %ds.", task.name, task.pull.pull.number, taken_on, taken_by, timeout) Status.create(task.repo, task.pull, task.name, 'unassigned', '', 'pending')
def test_p04(self): """Dot has higher precedence than other operations""" s = "a+b.c.d;" t = parse.parse(s) u = [parse.expr_stmt(id=1, expr=[('+', parse.ident(name='a', lineno=1, lexpos=0), parse.field(expr=parse.field(expr=parse.ident(name='b', lineno=1, lexpos=2), ident=parse.ident(name='.c', lineno=1, lexpos=3)), ident=parse.ident(name='.d', lineno=1, lexpos=5)))])] self.assertEqual(t,u) # def test_p05(self): # """Iterate over LHS nodes (TBD)""" # s = "[foo(A.x(B.y)).bar(C.z).bzz,hello.world] =1;" # t = parse.parse_buf(s) # u = ["foo",".bar",".bzz","hello",".world"] # self.assertEqual([v[1] for v in dataflow.lhs(t[1][1])],u) #
def python_version(path_to_python): if not path_to_python: return None try: c = delegator.run([path_to_python, '--version'], block=False) except Exception: return None output = c.out.strip() or c.err.strip() @parse.with_pattern(r'.*') def allow_empty(text): return text TEMPLATE = 'Python {}.{}.{:d}{:AllowEmpty}' parsed = parse.parse(TEMPLATE, output, dict(AllowEmpty=allow_empty)) if parsed: parsed = parsed.fixed else: return None return u"{v[0]}.{v[1]}.{v[2]}".format(v=parsed)
def match_parse(self, frame): for pattern in reversed(self._index_parse): if isinstance(frame.data.text, str): match_string = frame.data.text else: match_string = frame.name res = string_parse( format=pattern, string=match_string) if not res: continue handlers = self._handlers[pattern] data = frame.data data.update(**res.named) data.update({'args': res.fixed}) frame.data = data return frame, handlers else: return frame, set()
def handle_select_identity(self): kktix_id = parse(f'{REGISTER}:{{kktix_id}}', self.postback)['kktix_id'] if kktix_id != 'CANCEL': self.user = self.call(users.add_user_im, serial=kktix_id) self.call(im.complete_registration_session) logger.info('REGISTER: %s', self.user.uid) return [ TextSendMessage(text='??????????'), *self.ask_next_question() ] else: return TextSendMessage(text=f'??? email ??????')
def simple_command(format, *args, **kwargs): def validate_format(content): parsed = parse(format, content.strip()) if parsed is None: return (False, {}) return (True, parsed.named) return validator(validate_format, *args, **kwargs)
def on_message(client, userdata, msg): # Called when a MQTT message is received. print('{0}: {1}'.format(msg.topic, str(msg.payload))) # Handle a target request. if msg.topic == TOPIC_TARGET: # Try to parse out two numbers from the payload. These are the # screen x and screen y coordinates for the target command. result = parse.parse('{:d},{:d}', msg.payload.decode('ascii')) if result is not None: # Got a valid pair of numbers, use the laser model to target that # position. model.target(result[0], result[1])
def test_name(): with temp_chdir() as d: settings = copy_default_settings() settings['name'] = 'Don Quixote' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['name'] == 'Don Quixote'
def test_name_none(): with temp_chdir() as d: settings = copy_default_settings() settings['name'] = '' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) expected_author = get_user() or 'U.N. Owen' assert parsed['name'] == expected_author
def test_package_name(): with temp_chdir() as d: settings = copy_default_settings() create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['package_name'] == 'ok'
def test_readme(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'rst' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['readme_file'] == 'README.rst'
def test_package_url(): with temp_chdir() as d: settings = copy_default_settings() settings['vc_url'] = 'https://github.com/me' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['package_url'] == 'https://github.com/me/ok'
def test_license_single(): with temp_chdir() as d: settings = copy_default_settings() settings['licenses'] = ['mit'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['license'] == 'MIT'
def test_license_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['licenses'] = ['mit', 'apache2'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['license'] == 'MIT/Apache-2.0'
def test_license_classifiers_single(): with temp_chdir() as d: settings = copy_default_settings() settings['licenses'] = ['mit'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['license_classifiers'] == "\n 'License :: OSI Approved :: MIT License',"
def test_pyversions_single(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['3.6'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['pyversions'] == "\n 'Programming Language :: Python :: 3.6',"
def test_pyversions_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['3.6', '2.7'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['pyversions'] == ( "\n 'Programming Language :: Python :: 2.7'," "\n 'Programming Language :: Python :: 3.6'," )
def test_pypy(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['3.6', 'pypy3'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['pypy'] == "\n 'Programming Language :: Python :: Implementation :: PyPy',\n"
def test_pypy_none(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['3.6'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'setup.py')) parsed = parse(TEMPLATE, contents) assert parsed['pypy'] == '\n'
def test_title(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['title'] == 'ok'
def test_package_name(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['package_name'] == 'ok'
def test_supported_versions_none(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['pyversions'] = [] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.7/3.5+ and PyPy'
def test_supported_versions_py2_single(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['pyversions'] = ['2.7'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.7'
def test_supported_versions_py2(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['pyversions'] = ['2.7', '2.6'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.6-2.7'
def test_supported_versions_all(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['pyversions'] = ['pypy3', 'pypy', '3.4', '3.3', '3.6', '3.5', '2.7'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.7/3.3+ and PyPy'
def test_licenses_single(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' settings['licenses'] = ['mit'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['license_info'] == ( 'the\n[MIT License](https://choosealicense.com/licenses/mit)' )
def test_badges_none(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['readme']['format'] = 'md' settings['readme']['badges'] = [] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == '\n'
def test_badges_basic(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'md' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == '\n'
def test_badges_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['readme']['format'] = 'md' settings['readme']['badges'] = [ { 'image': 'https://img.shields.io/pypi/v/ok.svg', 'target': 'https://pypi.org/project/ok', 'alt': 'Latest PyPI version' }, { 'image': 'https://img.shields.io/pypi/l/ok.svg', 'target': 'https://choosealicense.com/licenses', 'alt': 'License' } ] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == ( '\n' '[![Latest PyPI version](https://img.shields.io/pypi/v/ok.svg)](https://pypi.org/project/ok)\n' '[![License](https://img.shields.io/pypi/l/ok.svg)](https://choosealicense.com/licenses)\n' '\n' )
def test_badges_params(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['readme']['format'] = 'md' settings['readme']['badges'] = [ { 'image': 'https://img.shields.io/pypi/v/ok.svg', 'target': 'https://pypi.org/project/ok', 'alt': 'Latest PyPI version', 'style': 'flat-square' }, { 'image': 'https://img.shields.io/pypi/l/ok.svg', 'target': 'https://choosealicense.com/licenses', 'alt': 'License', 'style': 'flat-square' } ] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.md')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == ( '\n' '[![Latest PyPI version](https://img.shields.io/pypi/v/ok.svg?style=flat-square)]' '(https://pypi.org/project/ok)\n' '[![License](https://img.shields.io/pypi/l/ok.svg?style=flat-square)]' '(https://choosealicense.com/licenses)\n' '\n' )
def test_title(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'rst' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['title'] == 'ok' assert parsed['header_marker'] == '=='
def test_package_name(): with temp_chdir() as d: settings = copy_default_settings() settings['readme']['format'] = 'rst' create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['package_name'] == 'ok'
def test_supported_versions_none(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = [] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.7/3.5+ and PyPy'
def test_supported_versions_py2(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['2.7', '2.6'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.6-2.7'
def test_supported_versions_py3(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['3.5', '3.6', '3.3', '3.4'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '3.3+'
def test_supported_versions_all(): with temp_chdir() as d: settings = copy_default_settings() settings['pyversions'] = ['pypy3', 'pypy', '3.4', '3.3', '3.6', '3.5', '2.7'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['supported_versions'] == '2.7/3.3+ and PyPy'
def test_licenses_single(): with temp_chdir() as d: settings = copy_default_settings() settings['licenses'] = ['mit'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['license_info'] == ( 'the\n`MIT License <https://choosealicense.com/licenses/mit>`_' )
def test_licenses_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['licenses'] = ['mit', 'apache2'] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['license_info'] == ( 'both\n\n' '- `MIT License <https://choosealicense.com/licenses/mit>`_\n' '- `Apache License, Version 2.0 <https://choosealicense.com/licenses/apache-2.0>`_' '\n\nat your option' )
def test_badges_basic(): with temp_chdir() as d: settings = copy_default_settings() create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == '\n'
def test_badges_multiple(): with temp_chdir() as d: settings = copy_default_settings() settings['basic'] = False settings['readme']['badges'] = [ { 'image': 'https://img.shields.io/pypi/v/ok.svg', 'target': 'https://pypi.org/project/ok', 'alt': 'Latest PyPI version' }, { 'image': 'https://img.shields.io/pypi/l/ok.svg', 'target': 'https://choosealicense.com/licenses', 'alt': 'License' } ] create_package(d, 'ok', settings) contents = read_file(os.path.join(d, 'README.rst')) parsed = parse(TEMPLATE, contents) assert parsed['badges'] == ( '\n' '.. image:: https://img.shields.io/pypi/v/ok.svg\n' ' :target: https://pypi.org/project/ok\n' ' :alt: Latest PyPI version\n' '\n' '.. image:: https://img.shields.io/pypi/l/ok.svg\n' ' :target: https://choosealicense.com/licenses\n' ' :alt: License\n' '\n' )