Exemple #1
0
    def on_post(self, request, response):
        """Validates request and calls pig latin translation library

        Required Body Properties:
            text -- string of length > 0

        Success Response:
            Code -- 200
            Content -- { translation : string }

        Error Response:
            Code -- 400 or 500
            Content -- { title : string[, description : string]}
        """

        if request.content_length in (None, 0):
            raise falcon.HTTPBadRequest(
                'Request body is empty.',
                'The request body must contain JSON.'
            )
        doc = request.context['doc']

        if 'text' not in doc:
            raise falcon.HTTPMissingParam('text')
        elif not isinstance(doc['text'], basestring):
            raise falcon.HTTPInvalidParam(
                'The value is not a string.',
                'text'
            )
        elif len(doc['text']) == 0:
            raise falcon.HTTPInvalidParam(
                'The value is empty.',
                'text'
            )
        text = doc['text']

        try:
            translation = piglatin.to_pig_latin(text)
        except Exception,e:
            """API-specific input validation was handled before calling piglatin.to_pig_latin().
            So any errors are unexpected and given generic 500 code.
            Error message is retained, if possible"""
            description = None
            if (len(e.args) > 0):
                description = e.args[0]

            raise falcon.HTTPInternalServerError(
                'Unexpected error',
                description
            )
Exemple #2
0
# test program for english/piglatin translator

import piglatin

choice = input("(E)nglish or (P)ig Latin?\n")
action = choice[:1]
if action == 'E':
    s = input("Enter an English sentence:\n")
    new_s = piglatin.to_pig_latin(s)
    print("Pig-Latin:")
    print(new_s)
elif action == 'P':
    s = input("Enter a Pig Latin sentence:\n")
    new_s = piglatin.to_english(s)
    print("English:")
    print(new_s)
Exemple #3
0
# test program for english/piglatin translator

import piglatin

choice = input ("(E)nglish or (P)ig Latin?\n")
action = choice[:1]
if action == 'E':
    s = input("Enter an English sentence:\n")
    new_s = piglatin.to_pig_latin(s)
    print("Pig-Latin:")
    print(new_s)
elif action =='P':
    s = input("Enter a Pig Latin sentence:\n")
    new_s = piglatin.to_english(s)
    print("English:")
    print(new_s)