Skip to content

Instantly share code, notes, and snippets.

@sagnew
Created May 23, 2017 20:58
Show Gist options
  • Save sagnew/95da136c22ec2ac702eade9b2badbbd3 to your computer and use it in GitHub Desktop.
Save sagnew/95da136c22ec2ac702eade9b2badbbd3 to your computer and use it in GitHub Desktop.
Python debugging code challenge
import re
from flask import Flask, request
from twilio import twiml
app = Flask(__name__)
@app.route('/sms', methods=['POST']): # This decorator has no function. There should be a function def on the next line.
message_body = request.form['Body']
response_message = ('Thanks for playing :) Hit up @Sagnewshreds '
'on Twitter for questions!')
error_message = ('Please enter a 4 digit hex address followed by a '
'2 digit hex value to write to that address.\n\n'
'Example: "0779: 1f"')
message_list = message_body.split(' ')
hex_pattern = re.compile('^[0-9a-F]+$') # This regex is straight up invalid.
if len(message_list) == 2:
address, value = message_list
if hex_pattern.match(address) and hex_pattern.match(value):
with f as open('address.txt', 'w'): # F and the open() call need to switch places.
f.write(address)
with open('value.txt') as f: # value.txt should be opened with the 'w' flag as above.
f.write(value)
else:
response_message = error_message
else:
response_message = error_message
resp = twiml.Response()
resp.message(response_message)
return str(resp)
if __name__ == __main__: # '__main__' should be a string.
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment