Skip to content

Instantly share code, notes, and snippets.

@jackiect
Last active April 26, 2016 08:31
Show Gist options
  • Save jackiect/4a6fc5ba88f1d06443c8aa587ab03bdd to your computer and use it in GitHub Desktop.
Save jackiect/4a6fc5ba88f1d06443c8aa587ab03bdd to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from flask import request, url_for
from flask.ext.api import FlaskAPI, status, exceptions
app = FlaskAPI(__name__)
notes = {
0: '购物',
1: 'build the codez',
2: 'paint the door',
}
def note_repr(key):
return {
'url': request.host_url.rstrip('/') + url_for('notes_detail', key=key),
'text': notes[key]
}
@app.route("/", methods=['GET', 'POST'])
def notes_list():
"""
List or create notes.
"""
if request.method == 'POST':
note = str(request.data.get('text', ''))
idx = max(notes.keys()) + 1
notes[idx] = note
return note_repr(idx), status.HTTP_201_CREATED
# request.method == 'GET'
return [note_repr(idx) for idx in sorted(notes.keys())]
@app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE'])
def notes_detail(key):
"""
Retrieve, update or delete note instances.
"""
if request.method == 'PUT':
note = str(request.data.get('text', ''))
notes[key] = note
return note_repr(key)
elif request.method == 'DELETE':
notes.pop(key, None)
return '', status.HTTP_204_NO_CONTENT
# request.method == 'GET'
if key not in notes:
raise exceptions.NotFound()
return note_repr(key)
if __name__ == "__main__":
app.run(debug=True)
#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import request, url_for
from flask.ext.api import FlaskAPI, status, exceptions
app = FlaskAPI(__name__)
notes = {
0: 'do the shopping购物',
1: 'build the codez',
2: 'paint the door',
}
def note_repr(key):
return {
'url': request.host_url.rstrip('/') + url_for('notes_detail', key=key),
'text': notes[key]
}
@app.route("/", methods=['GET', 'POST'])
def notes_list():
"""
List or create notes.
"""
if request.method == 'POST':
note = str(request.data.get('text', ''))
idx = max(notes.keys()) + 1
notes[idx] = note
return note_repr(idx), status.HTTP_201_CREATED
# request.method == 'GET'
return [note_repr(idx) for idx in sorted(notes.keys())]
@app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE'])
def notes_detail(key):
"""
Retrieve, update or delete note instances.
"""
if request.method == 'PUT':
note = str(request.data.get('text', ''))
notes[key] = note
return note_repr(key)
elif request.method == 'DELETE':
notes.pop(key, None)
return '', status.HTTP_204_NO_CONTENT
# request.method == 'GET'
if key not in notes:
raise exceptions.NotFound()
return note_repr(key)
if __name__ == "__main__":
app.run(debug=True)
@jackiect
Copy link
Author

  • simplest version
name = 'hello world from example'
print type(name)  # <type 'str'>
print name        # hello world from example
  • add some non-english chars, a SyntaxError will be raised
name = 'helló wörld from example'  # name = u'helló wörld from example' does not help
print type(name)
print name

# SyntaxError: Non-ASCII character...
# means you need add head-comment # encoding: utf-8
  • so you need add head-comment # encoding: utf-8 or # -*- coding: utf-8 -*-(which is more standard) to fix it
# encoding: utf-8

name = 'helló wörld from example'
print type(name)  # <type 'str'>
print name        # hello world from example
  • but # encoding: utf-8 won't change the fact that unicode and str are different in Python 2.7
# encoding: utf-8

name = u'helló wörld from example'
print type(name)  # <type 'unicode'>
print name        # hello world from example
  • so you need the __future__ package
# encoding: utf-8
from __future__ import unicode_literals

name = 'helló wörld from example'  # name = u'helló wörld from example' is just in vain, they have the same effect
print type(name)  # <type 'unicode'>
print name        # hello world from example
  • if you want to go back to str type, you need explicitly specify b before ''
# encoding: utf-8
from __future__ import unicode_literals

name = b'helló wörld from example'
print type(name)  # <type 'str'>
print name        # hello world from example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment