python:sample_program_for_highlighting
Differences
This shows you the differences between two versions of the page.
python:sample_program_for_highlighting [2016/07/27 22:20] – created mithat | python:sample_program_for_highlighting [2016/07/27 22:32] (current) – mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
<code python> | <code python> | ||
- | # Exponent Calculator | + | #!/ |
- | # Calculate a base to an exponent' | + | """ |
- | # Mithat Konar | + | |
+ | |||
+ | |||
+ | from flask import abort | ||
+ | from flask_restful import Resource, reqparse, inputs | ||
+ | from api_models import audio_model | ||
+ | |||
+ | class AudioAPI(Resource): | ||
+ | """ | ||
+ | |||
+ | def __init__(self, | ||
+ | | ||
+ | self.model = model | ||
+ | super(AudioAPI, | ||
+ | |||
+ | # API methods | ||
+ | def get(self): | ||
+ | """ | ||
+ | Return full state.""" | ||
+ | return { | ||
+ | ' | ||
+ | ' | ||
+ | }, 200, {' | ||
+ | |||
+ | def put(self): | ||
+ | """ | ||
+ | Set state passed in header JSON. Return changed new state. | ||
+ | {' | ||
+ | {' | ||
+ | parser = reqparse.RequestParser() # used to parse requests | ||
+ | parser.add_argument('isMute' | ||
+ | parser.add_argument(' | ||
+ | type=int, | ||
+ | help=" | ||
+ | | ||
+ | | ||
+ | args = parser.parse_args() | ||
+ | new_state = {} | ||
+ | |||
+ | if args.isMute is not None: | ||
+ | try: | ||
+ | # convert arg to boolean, set and add to new_state: | ||
+ | is_mute = inputs.boolean(args.isMute) | ||
+ | new_state[' | ||
+ | except ValueError as err: | ||
+ | abort(400, str(err)) | ||
+ | |||
+ | # additional level validation | ||
+ | if args.level is not None: | ||
+ | try: | ||
+ | was_muted = self.model.get_mute() | ||
+ | new_state[' | ||
+ | if was_muted: # add changed mute state to returned data. | ||
+ | new_state[' | ||
+ | except ValueError as err: | ||
+ | abort(400, str(err)) | ||
+ | |||
+ | return new_state, \ | ||
+ | 200, {' | ||
- | base = int(input(' | ||
- | exponent = int(input(' | ||
- | result = base ** exponent | ||
- | print(str(base) + ' to the power of ' + str(exponent) + ' is ' + str(result)) | ||
</ | </ |
python/sample_program_for_highlighting.txt · Last modified: 2016/07/27 22:32 by mithat