From 4ee3b4009066c33b1ad55dcbd4796806d6719d3c Mon Sep 17 00:00:00 2001 From: mkzero Date: Tue, 10 Oct 2017 01:14:52 +0200 Subject: [PATCH 1/6] Rewrite of generate_phrase to allow arbitrary templates This will allow for LTR, non-SVO word order and other features that some languages require. --- twitterstatus.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/twitterstatus.py b/twitterstatus.py index 1d3af83..2c7f97d 100755 --- a/twitterstatus.py +++ b/twitterstatus.py @@ -44,19 +44,35 @@ def write_status(status): def generate_phrase(open_status=True): - phrase = choice(WORDLIST[0]) + " " - phrase += choice(WORDLIST[1]) + " " + """ + Generates a translated phrase to say if the hackspace is open or + closed from a given list of words using random choices if more than + one list item exists. The word order and + """ + try: + """ Get the template and test that the template doesn't contain + unknown variables """ + phrase_template = WORDLIST[6] + phrase = phrase_template.format(space=1, be=1, status=1, qualifier=1) + except IndexError: + phrase_template = "{space} {be} {status}. {qualifier}!" + except KeyError: + print("[WARN]: Loading phrase template failed, using default SVO") + phrase_template = "{space} {be} {status}. {qualifier}!" + + if open_status: + status = choice(WORDLIST[2]) + else: + status = choice(WORDLIST[3]) if open_status: - phrase += choice(WORDLIST[2]) + ". " + qualifier = choice(WORDLIST[4]) else: - phrase += choice(WORDLIST[3]) + ". " + qualifier = choice(WORDLIST[5]) - if choice([True, False]): - if open_status: - phrase += choice(WORDLIST[4]).title() + "!" - else: - phrase += choice(WORDLIST[5]).title() + "!" + phrase = phrase_template.format(space=choice(WORDLIST[0]), + be=choice(WORDLIST[1]), status=status, + qualifier=qualifier) return phrase From 6e2cef966ad5b13574034eb28145c413ebc43859 Mon Sep 17 00:00:00 2001 From: mkzero Date: Tue, 10 Oct 2017 01:17:11 +0200 Subject: [PATCH 2/6] Added test_RU as example language for new phrase template --- wordlist.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/wordlist.py b/wordlist.py index b8102cc..9266540 100644 --- a/wordlist.py +++ b/wordlist.py @@ -478,6 +478,15 @@ class MissingTranslationException(Exception): 'Αηδία', ] ], + 'test_RU': [ + ['Хакерпространство'], + [''], + ['открыто'], + ['закрыто'], + ['Большой!'], + ['плохой'], + '{space} {status}. {qualifier}!', + ] } From 864fb70335343f3d6ea65d2fea793f61be934d99 Mon Sep 17 00:00:00 2001 From: mkzero Date: Tue, 10 Oct 2017 01:17:56 +0200 Subject: [PATCH 3/6] Added template info to doc string of wordlist function --- wordlist.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wordlist.py b/wordlist.py index 9266540..7507aa1 100644 --- a/wordlist.py +++ b/wordlist.py @@ -2,6 +2,7 @@ # -*- coding: utf-8 -*- import config + class MissingTranslationException(Exception): pass @@ -501,6 +502,7 @@ def wordlist(language="en_US"): * closed * fill words - good * fill words - bad + * (optional) word order of {space}, {be}, {status}, {qualifier} """ try: wordlist = translations[language] From b8d3397003651d5ae84cfa8fd9dc847be38accb3 Mon Sep 17 00:00:00 2001 From: mkzero Date: Tue, 10 Oct 2017 01:18:40 +0200 Subject: [PATCH 4/6] PEP8 indentation --- twitterstatus.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/twitterstatus.py b/twitterstatus.py index 2c7f97d..f8f34d0 100755 --- a/twitterstatus.py +++ b/twitterstatus.py @@ -28,11 +28,14 @@ ] try: - twitter = Twitter(auth=OAuth( - config.OAUTH_TOKEN, - config.OAUTH_SECRET, - config.CONSUMER_KEY, - config.CONSUMER_SECRET)) + twitter = Twitter( + auth=OAuth( + config.OAUTH_TOKEN, + config.OAUTH_SECRET, + config.CONSUMER_KEY, + config.CONSUMER_SECRET + ) + ) except Exception as e: print('Error in twitter init: ' + e) exit(255) From aefd281b2768c47f8b05a48bc8b586a5e80c20e5 Mon Sep 17 00:00:00 2001 From: mkzero Date: Tue, 10 Oct 2017 01:23:16 +0200 Subject: [PATCH 5/6] Simplified control flow structure in generate_phrase() --- twitterstatus.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/twitterstatus.py b/twitterstatus.py index f8f34d0..ef0fbb1 100755 --- a/twitterstatus.py +++ b/twitterstatus.py @@ -65,12 +65,9 @@ def generate_phrase(open_status=True): if open_status: status = choice(WORDLIST[2]) - else: - status = choice(WORDLIST[3]) - - if open_status: qualifier = choice(WORDLIST[4]) else: + status = choice(WORDLIST[3]) qualifier = choice(WORDLIST[5]) phrase = phrase_template.format(space=choice(WORDLIST[0]), From e54686f1b6c2b962a384f17cf31f6d4175ce3070 Mon Sep 17 00:00:00 2001 From: mkzero Date: Thu, 12 Oct 2017 22:13:04 +0200 Subject: [PATCH 6/6] Removed empty line(PEP8) --- wordlist.py | 1 - 1 file changed, 1 deletion(-) diff --git a/wordlist.py b/wordlist.py index 4a7d0a8..85072e3 100644 --- a/wordlist.py +++ b/wordlist.py @@ -5,7 +5,6 @@ import json - class MissingTranslationException(Exception): pass