61846

Data type of GET attributes

Question:

I was reading [a link]<a href="http://flask.pocoo.org/docs/quickstart/#the-request-object" rel="nofollow">http://flask.pocoo.org/docs/quickstart/#the-request-object</a> [a link]

If I have an html which consists of a form which has an element as:

<input type="text" id = 2 name="box"></input>

And I am using Flask app to get this dat in a GET request. from the documentation I came to know I can use

searchword = request.args.get('box', '')

I want to know what is the data type of searchwork , Is it an object. If it is an object is there a way I can convert it to an integer or string because I have a function which takes an integer and I want to pass the searchword there.

Answer1:

Just build a simple app and you can get your answer.

from flask import request, Flask app = Flask(__name__) @app.route('/', methods=['POST', 'GET']) def home(): key = request.args.get('key', '') return 'key is `%s`, type(key) is %s\n' % (key, type(key)) if __name__ == '__main__': app.run()

Now run it.

$ wget -qO - 'http://localhost:5000/?key=value' key is `value`, type(key) is <type 'unicode'>

If you want to convert that into an integer, you definitely want some kind of fallback. So incorporating the other answer, you can do something like this

return 'key is `%s`, type(key) is %s, key.isdigit() is %s\n' % ( key, type(key), key.isdigit())

Trying this:

$ wget -qO - 'http://localhost:5000/?key=value' key is `value`, type(key) is <type 'unicode'>, key.isdigit() is False $ wget -qO - 'http://localhost:5000/?key=123' key is `123`, type(key) is <type 'unicode'>, key.isdigit() is True

Then you can use the appropriate methods to handle your success and failure conditions, and use int(value) to cast value into an int.

Answer2:

You can apply <a href="http://docs.python.org/2/library/stdtypes.html#str.isdigit" rel="nofollow">isdigit()</a> check:

searchword = request.args.get('box', '') if searchword.isdigit(): my_function(int(searchword))

Or, just <a href="http://docs.python.org/2/glossary.html" rel="nofollow">"ask for forgiveness"</a>:

<blockquote>

<strong>EAFP</strong>

Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements.

</blockquote> searchword = request.args.get('box', '') try: my_function(int(searchword)) except ValueError: pass

Hope that helps.

Recommend

  • Remove duplicate lines from a string in python
  • How to monitor video and https traffic using bro network security monitor
  • Why does getContextPath() under a proxy return the internal path inside HttpServlet but not inside F
  • JS loop variable scope
  • Android - findViewById() vs findViewWithTag()
  • getting exception when inserting events in android calendar
  • How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()
  • Reveal Icon Button - Bootstrap 3
  • Find a directory using wildcard in Inno Setup
  • How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in t
  • ODBC connection to an .accdb file
  • Set selected using jQuery
  • Customize jQuery submenu positioning
  • Is it possible to add a hyperlink to a UIAlertController?
  • Open a cmd program with full functionality (i/o)
  • Limit number of button clicks
  • How to make responsive images in flutter?
  • Target in barchart in dc.js
  • Detect when MathJax has finished loading in UIWebView
  • getting the values of checkboxes in a checkboxlist control
  • Special characters (ë) in JSON-response
  • How to highlighting active menu item on scroll and click?
  • Python sum values in tuple in a list in a dictionary?
  • Using loops in Jasmine (with injected service)
  • Homomorphic filtering in OpenCV
  • Getting Microsoft Calibri font on Amazon EC2 ubuntu
  • PHP: Need to close STDIN in order to read STDOUT?
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • How to define something in JavaScript [closed]
  • Typeahead.js does give me suggestions but doesn't select them
  • Neo4j…how to get a visual representation of my data?
  • How can I ssh into a server that requires 2 password authentication using python's paramiko mod
  • Bitrate JWplayer
  • Sign a Pdf using custom digital signature in Java
  • Accessing Arguments, Workflow Variables from custom activities
  • Firebase: How to read from external DB?
  • WPF custom control and direct content support
  • Create/delete users from text file using Bash script
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?