How to use and_keywords method in Gherkin-python

Best Python code snippet using gherkin-python

submit_classify_job.py

Source:submit_classify_job.py Github

copy

Full Screen

1import configparser2import argparse3import boto34import utility5import sys6from collections import OrderedDict7SPARK_EXTRA_CONFIG = [("spark.python.profile", "true"),8 ("spark.python.worker.reuse", "false"),9 ("spark.yarn.executor.memoryOverhead", "4096"),10 ("spark.driver.maxResultSize", "3g"),11 ("spark.executor.extraJavaOptions",12 "-Dlog4j.configuration=file:///etc/spark/conf/log4j.properties "13 "-XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=30 "14 "-XX:MaxHeapFreeRatio=50 -XX:+CMSClassUnloadingEnabled "15 "-XX:MaxPermSize=512M -XX:OnOutOfMemoryError='kill -9 %%p'"16 " -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/mnt/app/oom_dump_`date`.hprof")]17def submit_classify_job(job_config, cluster_id, dry_run, **kwargs):18 job_configuration = "config/classify_job.config"19 if job_config is not None and job_config.strip() != "":20 job_configuration = job_config.strip()21 config = configparser.ConfigParser()22 config.optionxform = str23 config.read(job_configuration)24 if cluster_id is None or cluster_id.strip() == "":25 cluster_id = utility.get_cluster_id(dry_run)26 else:27 cluster_id = cluster_id.strip()28 if cluster_id != "" and check_configuration(config):29 if config["job_config"].get("upload_classify_script", "False") == "True":30 utility.upload_files_to_s3([(config["job_config"]["classify_script"],31 config["job_config"]["classify_script_local_location"],32 config["job_config"]["classify_script_s3_location"])], dry_run)33 num_executors = calculate_num_executor(cluster_id, config["spark_config"]["executor_memory"])34 if num_executors < 0:35 config["spark_config"]["num_executors"] = "None"36 else:37 config["spark_config"]["num_executors"] = str(num_executors)38 config["spark_config"]["executor_cores"] = "1"39 job_argument = build_command(cluster_id, config, num_executors)40 if not dry_run:41 emr_client = boto3.client("emr")42 # warn user before removing any output43 out = config["script_arguments"]["output_location"]44 # find out which output dirs, if any, exist45 dirs_to_remove = utility.check_s3_path_exists([out])46 # create a list of the names of the directories to remove47 if dirs_to_remove:48 response = input("About to remove any existing output directories." +49 "\n\n\t{}\n\nProceed? [y/n]: ".format(50 '\n\n\t'.join(dirs_to_remove)))51 while response not in ['y', 'n']:52 response = input('Proceed? [y/n]: ')53 if response == 'n':54 print("Program Terminated. Modify config file to change " +55 "output directories.")56 sys.exit(0)57 # remove the output directories58 if not utility.remove_s3_files(dirs_to_remove):59 print("Program terminated")60 sys.exit(1)61 job_submission = emr_client.add_job_flow_steps(**job_argument)62 print("Submitted job to cluster {}. Job id is {}".format(cluster_id, job_submission["StepIds"][0]))63 else:64 print(job_argument)65def check_configuration(config):66 if not utility.check_config(config, "job_config", ["name", "action_on_failure", "classify_script",67 "classify_script_s3_location", "upload_classify_script"]):68 return False69 if not utility.check_upload_config(config["job_config"], "upload_classify_script", "classify_script",70 "classify_script_local_location", "classify_script_s3_location"):71 return False72 if not utility.check_config(config, "spark_config", ["driver_memory", "executor_memory"]):73 return False74 if not utility.check_config(config, "script_arguments", ["output_location", "region"]):75 return False76 if not utility.check_s3_region(config["script_arguments"]["region"]):77 return False78 if all(x not in config["script_arguments"] for x in ["input_location", "organism", "and_keywords", "or_keywords"]):79 print("At least one of the following script arguments are required: input_location, organism, and_keywords, "80 "or_keywords")81 return False82 return True83def calculate_num_executor(cluster_id, executor_memory):84 global SPARK_EXTRA_CONFIG85 memory_overhead = 51286 for conf in SPARK_EXTRA_CONFIG:87 if conf[0] == "spark.yarn.executor.memoryOverhead":88 memory_overhead = int(conf[1])89 memory_per_executor = int(executor_memory.strip("m")) / 1024 + memory_overhead / 102490 total_mem, total_cpu = utility.get_cluster_mem_cpu(cluster_id)91 if total_mem < 0 or total_cpu < 0:92 num_executors = -1 # dry run93 else:94 num_executors = int(total_mem / memory_per_executor)95 return num_executors96def build_command(cluster_id, config, num_executors):97 job_arguments = OrderedDict()98 job_arguments["JobFlowId"] = cluster_id99 step_arguments = OrderedDict()100 step_arguments['Name'] = config["job_config"]["name"]101 step_arguments["ActionOnFailure"] = config["job_config"]["action_on_failure"]102 hadoop_arguments = OrderedDict()103 hadoop_arguments["Jar"] = "command-runner.jar"104 command_args = ["spark-submit",105 "--deploy-mode", "cluster"]106 for config_name, config_value in SPARK_EXTRA_CONFIG:107 command_args.append("--conf")108 command_args.append("{}={}".format(config_name, config_value))109 for spark_conf in config["spark_config"]:110 command_args.append("--" + spark_conf.replace("_", "-"))111 command_args.append(config["spark_config"][spark_conf])112 command_args.append(config["job_config"]["classify_script_s3_location"].rstrip("/") + "/" +113 config["job_config"]["classify_script"])114 command_args.append("-u")115 command_args.append(config["script_arguments"]["output_location"])116 command_args.append("-d")117 command_args.append("/mnt/clf_data/GEOmetadb.sqlite")118 command_args.append("-t")119 command_args.append("/mnt/clf_data/gse_clf_title.pickle")120 command_args.append("-s")121 command_args.append("/mnt/clf_data/gse_clf_summary.pickle")122 command_args.append("-m")123 command_args.append("/mnt/clf_data/gsm_clf.pickle")124 command_args.append("-n")125 command_args.append("/mnt/app/NobleCoder.jar")126 command_args.append("-p")127 command_args.append(str(num_executors))128 command_args.append("-r")129 command_args.append(config["script_arguments"]["region"])130 if "input_location" in config["script_arguments"]:131 command_args.append("-i")132 command_args.append(config["script_arguments"]["input_location"])133 if "organism" in config["script_arguments"]:134 command_args.append("-o")135 command_args.append(config["script_arguments"]["organism"])136 if "and_keywords" in config["script_arguments"]:137 command_args.append("-ak")138 command_args.append(config["script_arguments"]["and_keywords"])139 if "or_keywords" in config["script_arguments"]:140 command_args.append("-ok")141 command_args.append(config["script_arguments"]["or_keywords"])142 hadoop_arguments['Args'] = command_args143 step_arguments["HadoopJarStep"] = hadoop_arguments144 job_arguments["Steps"] = [step_arguments]145 return job_arguments146if __name__ == "__main__":147 parser = argparse.ArgumentParser(description='Job submission script for Classifying GSE')148 parser.add_argument('--config', '-c', action="store", dest="job_config", default="", help="Job configuration file")149 parser.add_argument('--cluster-id', '-id', action="store", dest="cluster_id", help="Cluster ID for submission")150 parser.add_argument('--dry-run', '-d', action="store_true", dest="dry_run",151 help="Produce the configurations for the job flow to be submitted")152 parser.set_defaults(method=submit_classify_job)153 parser_result = parser.parse_args()...

Full Screen

Full Screen

campaign_group_tasks.py

Source:campaign_group_tasks.py Github

copy

Full Screen

1from __future__ import unicode_literals2from bloom import celery_app3from adwords_dashboard.models import Campaign4from facebook_dashboard.models import FacebookCampaign5from bing_dashboard.models import BingCampaign6from budget.models import Budget, CampaignGrouping7@celery_app.task(bind=True)8def update_campaigns_in_campaign_group(self, group_id):9 group = CampaignGrouping.objects.get(id=group_id)10 if group.group_by != 'all' and group.group_by != 'manual':11 group.update_text_grouping()12 if group.group_by == 'all':13 group.update_all_grouping()14 try:15 print('Finished campaign group ' + str(group.client.client_name) + ' ' + str(group.id))16 except AttributeError:17 print('Something happened with this group ' + str(group.id))18@celery_app.task(bind=True)19def update_budget_campaigns(self, budget_id):20 try:21 budget = Budget.objects.get(id=budget_id)22 except Budget.DoesNotExist:23 return24 print('Updating campaigns for budget ' + str(budget))25 budget.is_new = False26 budget.is_edited = False27 budget.save()28 if budget.is_default:29 account = budget.account30 budget.has_adwords = account.has_adwords31 budget.has_facebook = account.has_fb32 budget.has_bing = account.has_bing33 budget.save()34 if budget.grouping_type == 0:35 # manual, do not have to do anything36 pass37 elif budget.grouping_type == 1:38 # text39 account = budget.account40 adwords_campaigns = Campaign.objects.filter(account__in=account.adwords.all())41 facebook_campaigns = FacebookCampaign.objects.filter(account__in=account.facebook.all())42 bing_campaigns = BingCampaign.objects.filter(account__in=account.bing.all())43 positive_keywords = budget.text_includes.split(',')44 negative_keywords = budget.text_excludes.split(',')45 check_inclusions = budget.text_includes != '' and budget.text_includes is not None46 check_exclusions = budget.text_excludes != '' and budget.text_includes is not None47 aw_campaigns_in_group = []48 fb_campaigns_in_group = []49 bing_campaigns_in_group = []50 # Quick explanation51 # This function runs the same logic for each ad network52 # If we aren't checking inclusions, the default should be to have all campaigns and then remove ones53 # with negative keywords54 if budget.has_adwords:55 if not check_inclusions:56 aw_campaigns_in_group = [cmp for cmp in adwords_campaigns]57 for adwords_campaign in adwords_campaigns:58 if check_inclusions:59 for positive_keyword in positive_keywords:60 if '&' in positive_keyword:61 and_keywords = positive_keyword.split('&')62 add_it = True63 for and_keyword in and_keywords:64 if and_keyword.strip().lower() not in adwords_campaign.campaign_name.lower():65 add_it = False66 break67 if add_it and adwords_campaign not in aw_campaigns_in_group:68 aw_campaigns_in_group.append(adwords_campaign)69 else:70 if positive_keyword.strip().lower() in adwords_campaign.campaign_name.lower():71 if adwords_campaign not in aw_campaigns_in_group:72 aw_campaigns_in_group.append(adwords_campaign)73 if check_exclusions:74 for negative_keyword in negative_keywords:75 if negative_keyword.strip().lower() in adwords_campaign.campaign_name.lower():76 if adwords_campaign in aw_campaigns_in_group:77 aw_campaigns_in_group.remove(adwords_campaign)78 budget.aw_campaigns.set(aw_campaigns_in_group)79 else:80 budget.aw_campaigns.clear()81 if budget.has_facebook:82 if not check_inclusions:83 fb_campaigns_in_group = [cmp for cmp in facebook_campaigns]84 for facebook_campaign in facebook_campaigns:85 if check_inclusions:86 for positive_keyword in positive_keywords:87 if '&' in positive_keyword:88 and_keywords = positive_keyword.split('&')89 add_it = True90 for and_keyword in and_keywords:91 if and_keyword.strip().lower() not in facebook_campaign.campaign_name.lower():92 add_it = False93 break94 if add_it and facebook_campaign not in fb_campaigns_in_group:95 fb_campaigns_in_group.append(facebook_campaign)96 else:97 if positive_keyword.strip().lower() in facebook_campaign.campaign_name.lower():98 if facebook_campaign not in fb_campaigns_in_group:99 fb_campaigns_in_group.append(facebook_campaign)100 if check_exclusions:101 for negative_keyword in negative_keywords:102 if negative_keyword.strip().lower() in facebook_campaign.campaign_name.lower():103 if facebook_campaign in fb_campaigns_in_group:104 fb_campaigns_in_group.remove(facebook_campaign)105 budget.fb_campaigns.set(fb_campaigns_in_group)106 else:107 budget.fb_campaigns.clear()108 if budget.has_bing:109 if not check_inclusions:110 bing_campaigns_in_group = [cmp for cmp in bing_campaigns]111 for bing_campaign in bing_campaigns:112 if check_inclusions:113 for positive_keyword in positive_keywords:114 if '&' in positive_keyword:115 and_keywords = positive_keyword.split('&')116 add_it = True117 for and_keyword in and_keywords:118 if and_keyword.strip().lower() not in bing_campaign.campaign_name.lower():119 add_it = False120 break121 if add_it and bing_campaign not in bing_campaigns_in_group:122 bing_campaigns_in_group.append(bing_campaign)123 else:124 if positive_keyword.strip().lower() in bing_campaign.campaign_name.lower():125 if bing_campaign not in bing_campaigns_in_group:126 bing_campaigns_in_group.append(bing_campaign)127 if check_exclusions:128 for negative_keyword in negative_keywords:129 if negative_keyword.strip().lower() in bing_campaign.campaign_name.lower():130 if bing_campaign in bing_campaigns_in_group:131 bing_campaigns_in_group.remove(bing_campaign)132 budget.bing_campaigns.set(bing_campaigns_in_group)133 else:134 budget.bing_campaigns.clear()135 else:136 # all137 account = budget.account138 if budget.has_adwords:139 adwords_campaigns = Campaign.objects.filter(account__in=account.adwords.all())140 budget.aw_campaigns.set(adwords_campaigns)141 if budget.has_facebook:142 facebook_campaigns = FacebookCampaign.objects.filter(account__in=account.facebook.all())143 budget.fb_campaigns.set(facebook_campaigns)144 if budget.has_bing:145 print('budget ' + str(budget.id) + ' has bing')146 bing_campaigns = BingCampaign.objects.filter(account__in=account.bing.all())...

Full Screen

Full Screen

gherkin_template_processor.py

Source:gherkin_template_processor.py Github

copy

Full Screen

1import json2print("Make sure script is run using python3")3def generate_keyword_body(keywords):4 first_keyword = keywords[0].replace("'", "\\'")5 body = "\n\t("6 body += "'" + keywords[0].replace("'", "\\'") + "'"7 8 if len(body) > 1:9 for keyword in range(1,len(keywords)): 10 body += "\n\t\t| '{k}'".format(k=keywords[keyword].replace("'", "\\'"))11 body += "\n\t) "12 return body13 14language_data = json.load(open('languages/gherkin-languages.json'))15template = """lexer grammar GherkinLexer;16fragment LANGUAGE_KEYWORD : WS* '#' WS* 'language' WS* ':' WS*;17LANGUAGE_HEADER : LANGUAGE_KEYWORD 'en' LINE_END -> mode(DEFAULT_MODE) ;18"""19for language in language_data.keys():20 l = language_data[language]21 ul = language.upper().replace("-", "_")22 template += "\n" + ul + "_LANGUAGE_HEADER : LANGUAGE_KEYWORD '" + language + "' LINE_END -> mode(" + ul + "), type(LANGUAGE_HEADER) ;"23template += "\n//////////////////////////////////////////////////////////////////////////"24template += '''25FEATURE_KEYWORD : ('Feature'26 | 'Business Need'27 | 'Ability') ':' -> channel(HIDDEN) ;28SCENARIO_KEYWORD : ('Scenario' | 'Example') ':' -> channel(HIDDEN) ;29SCENARIO_OUTLINE_KEYWORD : 'Scenario Outline:' -> channel(HIDDEN);30BACKGROUND_KEYWORD : 'Background:' ;31EXAMPLES_KEYWORD : 'Examples:' | 'Scenarios:';32RULE_KEYWORD : 'Rule:' ;33STARTING_STEP_KEYWORD : GIVEN_KEYWORD34 | WHEN_KEYWORD35 | THEN_KEYWORD36 | WILD_KEYWORD37 ;38ALTERNATIVE_STEP_KEYWORD : AND_KEYWORD39 | BUT_KEYWORD40 | GIVEN_KEYWORD41 ;42GIVEN_KEYWORD : 'Given ' ;43WHEN_KEYWORD : 'When ' ;44THEN_KEYWORD : 'Then ' ;45WILD_KEYWORD : '* ' ;46AND_KEYWORD : 'And ';47BUT_KEYWORD : 'But ';48fragment CAPTURE_DATA : '<' ~[>\\t\\r\\n ]'>' ;49fragment DOCSTRING_DOUBLE_QUOTES : WS* '"""' (CAPTURE_DATA | ~'"' | '"' ~'"')*? '"""' LINE_END ;50fragment DOCSTRING_BACKTICKS : WS* '```' (~'`' | CAPTURE_DATA | '`' ~'`').*? '```' LINE_END;51fragment TAG : '@'~[ \\r\\n\\t@]+ ;52fragment ESCAPE_SEQUENCE : '\\\\' [\\\\|\\n]* ;53fragment CELL_CHARACTER54 : CAPTURE_DATA55 | ~[\\r\\n|\\\\]56 | ESCAPE_SEQUENCE57 ;58fragment CELL_DATA : WS* CELL_CHARACTER* '|';59DOCSTRING : DOCSTRING_DOUBLE_QUOTES | DOCSTRING_BACKTICKS ;60TAGS : WS* TAG (WS* TAG)* (COMMENT? | LINE_END);61FEATURE_TITLE : WS* FEATURE_KEYWORD ~[\\r\\n]* LINE_END ;62BACKGROUND_TITLE : WS* BACKGROUND_KEYWORD ~[\\r\\n]* COMMENT? LINE_END ;63EXAMPLES_TITLE : WS* EXAMPLES_KEYWORD ~[\\r\\n]* COMMENT? LINE_END ;64SCENARIO_TITLE : WS* SCENARIO_KEYWORD ~[\\r\\n]* LINE_END ;65SCENARIO_OUTLINE_TITLE : WS* SCENARIO_OUTLINE_KEYWORD (CAPTURE_DATA | ~[\\r\\n])* LINE_END ;66RULE_TITLE : WS* RULE_KEYWORD ~[\\r\\n]* LINE_END ;67GIVEN_STEP : WS* GIVEN_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;68WHEN_STEP : WS* WHEN_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;69THEN_STEP : WS* THEN_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;70AND_STEP : WS* AND_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;71BUT_STEP : WS* BUT_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;72WILD_STEP : WS* WILD_KEYWORD ~[ @\\r\\n|] ~[\\r\\n]* LINE_END;73DATA_ROW : WS* '|' CELL_DATA+ LINE_END ;74INVALID_LANGUAGE_HEADER : LANGUAGE_KEYWORD ~[\\r\\n]* LINE_END ;75COMMENT : WS* '#' ~[\\r\\n]* LINE_END -> channel(HIDDEN) ;76LINE_END : WS* (NEWLINE+ | EOF) -> skip;77NEWLINE : [\\r\\n] -> skip ;78WS : [ \\t] -> skip;79LONG_DESCRIPTION : WS* ~[ @\\r\\n|] ~[\\r\\n]* LINE_END ;80///////////////////////////////////////////////////81'''82for language in language_data.keys():83 print(language)84 ul = language.upper().replace('-', '_')85 ld = language_data[language]86 template += "\n//" + ld["name"]87 template += "\n//" + ld["native"]88 template += "\nmode " + ul + ";"89 # Feature90 template += "\n\t" + ul + "_FEATURE : ( "91 template += generate_keyword_body(ld["feature"]) + "':'"92 template += "\n\t\t) -> type(FEATURE_KEYWORD) ;"93 # Background94 template += """95 {lc}_BACKGROUND : (96 """.format(lc=ul)97 template += generate_keyword_body(ld["background"]) + "':'"98 template += "\n\t\t) -> type(BACKGROUND_KEYWORD);"99 # Scenario100 template += "\n\t" + ul + "_SCENARIO : (\n"101 template += generate_keyword_body(ld["scenario"]) + "':'"102 template += "\n\t) -> type(SCENARIO_KEYWORD);\n"103 # Scenario Outline104 template += "\n\t" + ul + "_SCENARIO_OUTLINE : (\n"105 template += generate_keyword_body(ld["scenarioOutline"])106 template += "\n\t) -> type(SCENARIO_OUTLINE_KEYWORD);\n"107 # Examples108 template += "\n\t" + ul + "_EXAMPLES : (\n"109 template += generate_keyword_body(ld["examples"]) + "':'"110 template += " ) -> type(EXAMPLES_KEYWORD) ;\n"111 # Rule112 template += "\n\t" + ul + "_RULE : (\n"113 template += generate_keyword_body(ld["rule"]) + "':'"114 template += " ) -> type(RULE_KEYWORD) ;\n"115 # Given116 template += "\n\t" + ul + "_GIVEN : (\n"117 given_keywords = ld["given"]118 if "* " in given_keywords: given_keywords.remove("* ")119 template += generate_keyword_body(given_keywords)120 template += " ) -> type(GIVEN_KEYWORD) ;\n"121 # When122 template += "\n\t" + ul + "_WHEN : (\n"123 when_keywords = ld["when"]124 if "* " in when_keywords: when_keywords.remove("* ")125 template += generate_keyword_body(when_keywords)126 template += " ) -> type(WHEN_KEYWORD) ;\n"127 # Then128 template += "\n\t" + ul + "_THEN : (\n"129 then_keywords = ld["then"]130 if "* " in then_keywords: then_keywords.remove("* ")131 template += generate_keyword_body(then_keywords)132 template += " ) -> type(THEN_KEYWORD) ;\n"133 # And134 template += "\n\t" + ul + "_AND : (\n"135 and_keywords = ld["and"]136 if "* " in and_keywords: and_keywords.remove("* ")137 template += generate_keyword_body(and_keywords)138 template += " ) -> type(AND_KEYWORD) ;\n"139 # But140 template += "\n\t" + ul + "_BUT : (\n"141 but_keywords = ld["but"]142 if "* " in but_keywords: but_keywords.remove("* ")143 template += generate_keyword_body(but_keywords)144 template += " ) -> type(BUT_KEYWORD) ;\n"145 # Starting step146 template += "\n\t" + ul + """_STARTING_STEP_KEYWORD : (\n147 {lc}_GIVEN148 | {lc}_WHEN149 | {lc}_THEN150 | WILD_KEYWORD151 ) -> type(STARTING_STEP_KEYWORD);152 """.format(lc=ul)153 template += "\n\t" + ul + """_ALTERNATIVE_STEP_KEYWORD : (\n154 {lc}_AND155 | {lc}_BUT156 ) -> type(ALTERNATIVE_STEP_KEYWORD);157 """.format(lc=ul)158 template += """159 {language_code}_FEATURE_TITLE : WS* {language_code}_FEATURE ~[\\r\\n]* WS* LINE_END -> type(FEATURE_TITLE) ;160 {language_code}_BACKGROUND_TITLE : WS* {language_code}_BACKGROUND ~[\\r\\n]* COMMENT? LINE_END -> type(BACKGROUND_TITLE) ;161 {language_code}_EXAMPLES_TITLE : WS* {language_code}_EXAMPLES ~[\\r\\n]* COMMENT? LINE_END -> type(EXAMPLES_TITLE);162 {language_code}_SCENARIO_TITLE : WS* {language_code}_SCENARIO ~[\\r\\n]* LINE_END -> type(SCENARIO_TITLE);163 {language_code}_SCENARIO_OUTLINE_TITLE : WS* {language_code}_SCENARIO_OUTLINE ~[\\r\\n]* LINE_END -> type(SCENARIO_OUTLINE_TITLE) ;164 {language_code}_RULE_TITLE : WS* {language_code}_RULE ~[\\r\\n]* LINE_END -> type(RULE_TITLE);165 {language_code}_GIVEN_STEP : WS* {language_code}_GIVEN ~[ @\\r\\n|] ~[\\r\\n]* LINE_END -> type(GIVEN_STEP);166 {language_code}_WHEN_STEP : WS* {language_code}_WHEN ~[ @\\r\\n|] ~[\\r\\n]* LINE_END -> type(WHEN_STEP);167 {language_code}_THEN_STEP : WS* {language_code}_THEN ~[ @\\r\\n|] ~[\\r\\n]* LINE_END -> type(THEN_STEP);168 {language_code}_AND_STEP : WS* {language_code}_AND ~[ @\\r\\n|] ~[\\r\\n]* LINE_END -> type(AND_STEP);169 {language_code}_BUT_STEP : WS* {language_code}_BUT ~[ @\\r\\n|] ~[\\r\\n]* LINE_END -> type(BUT_STEP);170""".format(language_code = ul)171 f = open("GherkinLexer.g4", "w")172 f.write(template)173 f.close()174print("GherkinLexer.g4 regenerated")...

Full Screen

Full Screen

squery.py

Source:squery.py Github

copy

Full Screen

1# squery - Copyright (c) 2021 Udi Finkelstein2#3# Redistribution and use in source and binary forms, with or without4# modification, are permitted provided that the following conditions are met:5#6# 1. Redistributions of source code must retain the above copyright notice, this7# list of conditions and the following disclaimer.8#9# 2. Redistributions in binary form must reproduce the above copyright notice,10# this list of conditions and the following disclaimer in the documentation11# and/or other materials provided with the distribution.12# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"13# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE15# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE16# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR18# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER19# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,20# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE21# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.22# A simple query language for searching text strings23# it accepts any words connected by and/or and their Hebrew counterparts24# it also honors ()'s.25#26# TODOs:27#28# Performance enhancements:29# Currently, we find() each token on the string to be searched.30# In the future, use one of those libs that does a combined search on multiple strings:31# https://github.com/jakerachleff/commentzwalter32# https://github.com/abusix/ahocorapy33# https://github.com/WojciechMula/pyahocorasick/34# https://github.com/Guangyi-Z/py-aho-corasick35# we would then take the search resut of each keyword and build a function36# to combine the results into a final True/False query result.37#38from lark import Lark, Transformer39import sys40class SQuery:41 # default keywords42 or_keywords = ('"or"i', '"או"')43 and_keywords = ('"and"i', '', '"וגם"')44 not_keywords = ('"not"i', '"ללא"')45 def __init__(self):46 # set default keywords47 self.set_keywords(SQuery.or_keywords, SQuery.and_keywords, SQuery.not_keywords)48 # location of keyewords in string during query49 self.loc = [] # search keywords location in text50 self.len = [] # length of search keywords51 #52 # Call this if you want to localize your and/or keywords53 #54 def compile_parser(self):55 parser_ebnf = r"""56 ?q_or : q_and (({or_kw}) q_and)*57 ?q_and: q_val (({and_kw}) q_val)*58 ?q_val: escaped_string59 | string60 | ( {not_kw} ) q_not 61 | "(" q_or ")"62 q_not: q_val63 string : STRING64 escaped_string : ESCAPED_STRING65 STRING: /\w+/66 %import common.ESCAPED_STRING67 %import common.WS68 %ignore WS69 """.format(or_kw=' | '.join(self.or_keywords), and_kw=' | '.join(self.and_keywords), not_kw=' | '.join(self.not_keywords))70 #print(parser_ebnf)71 return Lark(parser_ebnf, parser="lalr", start='q_or', lexer='standard')72 # if you want to translate and/or to a different language73 def set_keywords(self, or_kw, and_kw, not_kw):74 (self.or_keywords, self.and_keywords, self.not_keywords) = (or_kw, and_kw, not_kw)75 # Must recompile parser after keywords are updated76 self.squery_parser = self.compile_parser()77 # if you want to translate and/or to a different language78 def get_keywords(self):79 return ((self.or_keywords, self.and_keywords, self.not_keywords))80 #81 # Compile a query, return a query function that accepts a string and returns True or False82 #83 def compile(self, query):84 self.query = query85 try:86 parsed = self.squery_parser.parse(query)87 #print(parsed.pretty())88 return SQuery_transformer(self).transform(parsed)89 except:90 return None91 def outer_s_string(self, s):92 st = str(s)93 index = len(self.len)94 self.len.append(len(st))95 self.loc.append(None)96 def s_string(text):97 i = text.find(st)98 self.loc[index] = i99 return i >= 0100 return s_string101 def outer_s_and(self, *f):102 def s_and(text):103 return all([func(text) for func in f])104 return s_and105 def outer_s_or(self, *f):106 def s_or(text):107 return any([func(text) for func in f])108 return s_or109 def outer_s_not(self, func):110 def s_not(text):111 return not func(text)112 return s_not113class SQuery_transformer(Transformer):114 def __init__(self, squery):115 super().__init__()116 self.squery = squery117 def q_or(self, items):118 #print("q_or:", items)119 return self.squery.outer_s_or(*items)120 def q_and(self, items):121 #print("q_and:", items)122 return self.squery.outer_s_and(*items)123 def q_not(self, items):124 #print("q_not:", items[0])125 return self.squery.outer_s_not(items[0])126 def escaped_string(self, items):127 #print("string:", items)128 return self.squery.outer_s_string(items[0][1:-1])129 def string(self, items):130 #print("string:", items)131 return self.squery.outer_s_string(items[0])132#133# Test code134#135if __name__ == "__main__":136 sq = SQuery()137 # enable this if you wantr to localize your commands.138 #sq.set_keywords(('"or"i', '"או"'), ('"and"i', '"וגם"'), ('"not"i', '"ללא"'))139 text = "abc def ghi"140 print("Text:", text)141 for s in ("abc and ghi", "not def"):142 query = sq.compile(s)143 if query is None:144 print("Invalid query!")145 print("Query: {} Result: {}".format(s, query(text)))146 print(sq.loc)147 sys.exit(0)148 while True:149 print("Enter text (type 'exit' to exit):")150 text = input()151 if text == "exit":152 sys.exit(0)153 print("Enter query:")154 s = input()155 query = sq.compile(s)156 if query is None:157 print("Invalid query!")158 else:159 #print(query)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gherkin-python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful