Created
February 23, 2021 18:23
-
-
Save shamlikt/fec47aa308884c25da5129e26be3b51c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server: | |
host: "0.0.0.0" | |
port: 9091 | |
log: | |
path: "word_search.log" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
install | |
--------- | |
pip install flask, flask_restful, pyyaml | |
run | |
-------- | |
python script_name.py --config config.yaml | |
''' | |
import logging | |
import argparse | |
import os | |
import json | |
import yaml | |
from flask import Flask, request | |
from flask_restful import reqparse, abort, Api, Resource | |
def parse_args(): | |
parse = argparse.ArgumentParser() | |
parse.add_argument('-c', '--config', action="store", default="config.yaml", required=True, | |
help='''input file''') | |
return parse.parse_args() | |
def setup_logger(logger_dir=".", name='word_checker', level=logging.DEBUG): | |
if not os.path.exists(logger_dir): | |
os.makedirs(logger_dir) | |
logger_path = os.path.join(logger_dir, 'word_cheker.log') | |
logger = logging.getLogger(name) | |
logger.setLevel(level) | |
terminal_handler = logging.StreamHandler() | |
terminal_handler.setFormatter(logging.Formatter("[%(levelname)s] %(asctime)s : %(message)s")) | |
terminal_handler.setLevel(logging.DEBUG) | |
file_handler = logging.FileHandler(logger_path, mode='a') | |
file_handler.setFormatter(logging.Formatter("[%(levelname)s] %(asctime)s %(filename)s:%(lineno)s : %(message)s")) | |
file_handler.setLevel(logging.DEBUG) | |
logger.addHandler(terminal_handler) | |
logger.addHandler(file_handler) | |
return logger | |
logger = setup_logger() | |
def parse_config(config_file): | |
if not os.path.exists(config_file): | |
logger.error("Config file {} not found".format(config_file)) | |
raise ValueError("Config file {} not found".format(config_file)) | |
with open(config_file, 'r') as f: | |
config = yaml.load(f) | |
return config | |
args = parse_args() | |
config = parse_config(args.config) | |
sql_file = config['log']['path'] | |
server_host = config['server']['host'] | |
server_port = config['server']['port'] | |
app = Flask(__name__) | |
api = Api(app) | |
VOWELS = ["a", "e", "i", "o", "u"] | |
class TextAnalizer(Resource): | |
def post(self): | |
result = {'metrics':{}, | |
'error': None} | |
json_data = request.get_json(force=True) | |
logger.debug("Json data %s", json_data) | |
if not json_data or not 'text' in json_data: | |
result['error'] = "No input data found" | |
logger.error("Error in request: %s, result: %s", json_data, result) | |
return json.dumps(result), 400 | |
input_str = json_data['text'].lower() | |
result['metrics']["word_count"] = len(input_str.split(" ")) | |
result['metrics']["vowels_count"] = len([value for value in input_str if value in VOWELS]) | |
result['metrics']["consonants_count"] = len([value for value in input_str if value not in VOWELS]) | |
result['metrics']["special_count"] = len([value for value in input_str | |
if not (value.isalpha() or value.isdigit() or value.isspace())]) | |
logger.error("Returns : %s", result) | |
return result | |
api.add_resource(TextAnalizer, '/v1/api/text') | |
if __name__ == '__main__': | |
app.run(host=server_host, port=server_port) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment