r/Python • u/[deleted] • Jul 12 '15
Build an API under 30 lines of code with Python and Flask
https://impythonist.wordpress.com/2015/07/12/build-an-api-under-30-lines-of-code-with-python-and-flask/26
u/cjf4 Jul 12 '15
That was very poorly written.
15
u/badsectors Jul 12 '15
Python developer who earns $ 1,00,000 per annum
what kind of number format is that?
13
10
u/johnbokma Jul 13 '15
Lakh or lac: https://en.wikipedia.org/wiki/Lakh
3
2
u/autowikibot Jul 13 '15
Lakh:
A lakh or lac (/ˈlæk/ or /ˈlɑːk/; abbreviated L) is a unit in the Indian numbering system equal to one hundred thousand: 105). In the Indian convention of digit grouping, it is written as 1,00,000. For example, in India 150,000 rupees becomes 1.5 lakh rupees, written as ₹1,50,000 or INR 1,50,000.
It is widely used both in official and other contexts in Bangladesh, India, Myanmar, Nepal, Pakistan, and Sri Lanka. It is often used in Indian, Pakistani, and Sri Lankan English. In Pakistan, the word lakh is used mostly in local languages rather than in English media.
Relevant: Lakh Bahosi ^Sanctuary | Seyah ^Lakh | Lakh-e ^Sefid | Lakh-e ^Shak
Parent commenter can toggle ^NSFW or ^delete. Will also delete on comment score of -1 or less. | FAQs | ^Mods | Call ^Me
10
u/omgplsno Jul 13 '15
Also, SQL INJECTION.
1
u/alex_s64 Jul 13 '15
yeah but it's only 30 lines non-production :)
5
u/omgplsno Jul 13 '15
There's no excuse not to sanitize input! It's literally a manner of adding a comma to the cursor.execute(...) call.
1
u/justin-8 Jul 14 '15
What exactly would you add to the cursor.execute call to sanitize it?
3
u/omgplsno Jul 15 '15 edited Jul 15 '15
Like I said. A comma.
conn.execute("select * from salaries where Department='%s'"%department_name.upper())...is bad. Whereas:
conn.execute("select * from salaries where Department=?", (department_name.upper(),))...is good!
See the header paragraphs in the manual. https://docs.python.org/2/library/sqlite3.html
1
0
2
0
Jul 12 '15
[deleted]
1
u/badsectors Jul 12 '15
Where in the Flask docs did you read that?
add_resourceis part of the Flask-RESTful package and not Flask itself anyways.
17
Jul 12 '15
I know I can be a bit of a hard-ass in code reviews but:
You don't use
requestordumps.Class names are usually
UpperCamelnotUnique_Style.Metadoesn't tell you about what the endpoint does.DepartmentListwould be more descriptive.What happens if I query the
Salaryview for' OR 1 = 1 OR Department = '? I'm not sure I can do aDROP DATABASEor whatever the sqllite equivalent was, but I feel like I could put some nastyLIKE,CONCAT, subselects, and other functions to lock the DB up. I thinkparamsis the way to do it though you may have to useSessionfor that.for i in query.cursorwill grab rows as a stream when you want all of the data. I might just add afetchall()here too to avoid having the db connection open more than it needs to be. I think the case for leaving the connection open and fetching one at a time is when you'll know what you want when you find it (and then youconn.close()). I seem to have some old belief that sqlite doesn't do multiple threads, but the internet says I'm wrong.Why no schema definition?
4
Jul 13 '15
Not to mention, dangling connections all over the place. There's a Flask extension to bridge SQLAlchemy as well. No reason not to use it.
1
Jul 13 '15
I wasn't sure whether the unclosed connections would be closed at garbage collection time or not. The flask docs didn't give me any hints other than if you haven't fetched all of the responses, you should
close()to avoid issues with some engines.1
1
0
u/alex_s64 Jul 13 '15
It's 30 lines of non production code, it's just an example. None of that matters.
4
u/Anon_8675309 Jul 13 '15
When you're writing an article and saying things like, "Developing an API with Python is a very easy task when compared to other languages. So,sit back and grab this skill for you. Take my words ,this skill is hot right now in the market.", then, yeah, it doesn matter.
-10
u/Neceros Jul 13 '15 edited Jul 13 '15
I really dislike function names LikeThis. It looks sloppy, and like_this is far easier to read. No need to use capitals. Why did they do this as a standard? So stupid.
Oh, you said class names. I have no objection to those. I only mean def names.
Edit: I'd love to know why you think I should get downvotes for this. It's entirely subjective, what we're talking about. The very least you could do is tell me why you think I'm wrong.
0
Jul 13 '15
[deleted]
2
1
u/Neceros Jul 13 '15
Is there a reason for that? I think it's more difficult to read, personally. I'm used to everything being lower case, because of unix.
1
8
6
u/trymas Jul 13 '15
Sorry, but it's very bad example, with 'screaming' title.
Importing sqlalchemy, but not using it's ORM. SQL injections, class names should be in camel notation, etc.
Title is completely irrelevant. Basically what this article tried to show is 'How to setup hello world program for this REST API library, BTW it could be done in 30 lines (and with SQL injections)'.
4
u/Anon_8675309 Jul 13 '15
Why do people care so much about how many lines of code this takes (within reason). I mean, 30, 50 75, who cares? This is not a very good metric of how simple something is. Not only that, what is being hidden away that you will eventually need in order to do something more? How painful will it be then? Some frameworks get nasty after the TODO example is complete.
Flask is simple, powerful and wonderful to use, but just stop with the lines of code metric already. It's not useful.
3
u/wot-teh-phuck Really, wtf? Jul 13 '15
/departments/dept/<name>
I'm personally not a big fan of non-uniform names. Why not just have /deparments/police ?
2
u/suudo Jul 13 '15
Or someone (well, /u/jknupp) did the exact same thing you just did, but better https://github.com/jeffknupp/sandman
2
u/herr_corvax Jul 14 '15
Methinks the same example could be done with flask-restless in even fewer without risking sql injection.
1
u/istinspring Jul 13 '15
I usually use http://python-eve.org (mongodb since it heavily used for the web scraping tasks) or https://flask-restless.readthedocs.org/en/latest/
0
Jul 12 '15
[deleted]
7
u/Zren Jul 13 '15 edited Jul 13 '15
Are there any tests that include a db query? Wouldn't that bottleneck the entire request?
Edit: My point is that it doesn't matter if your wgsi server takes microseconds if your db calls take milliseconds. This is an API webserver after all, it's going to be hitting a db. You're probably going to heavily modify your code when you start scaling anyways. Use whichever is easiest to write in.
3
Jul 13 '15
Well I think falcon is trying to compare differences between wsgi frameworks, makes sense to cut out as mitch as possible.
2
u/timClicks Jul 13 '15
Do you know why its benchmarks are so much better than the others? Also, does it generally sit behind a web server or is deployment fairly straightforward?
6
Jul 13 '15
Because Falcon is indeed micro-framework, the entire source code is very small with very little functionality, if you want tiny wrapper for processing wsgi requests falcon is a way to go. At the end of the day, when you finish the app you look back and see that you have rewritten most of Django/Flask functionality into Falcon you end up realizing that your app is not so speedy anymore.
People tend to forgot that there is a reason for flask being slower than falcon: it does a lot of helpful magic in between.
1
u/herr_corvax Jul 14 '15
From the falcon docs it is faster primarily when you use the Cython extensions and compile it
1
1
u/RisingStar Jul 12 '15
Oh... That's really nice. I was about to deploy an API and that looks really cool! Will dig into it and do some testing.
1
u/Vidiris Jul 13 '15
Upvote because Falcon. We are using it in one of our company APIs and it's doing great.
1
u/wot-teh-phuck Really, wtf? Jul 13 '15
Does that also work for Jython? Couldn't see any mention of that on the landing page..
1
u/Neceros Jul 13 '15
Sure, but since I'm already using Flask (and I think it's amazing) this works well.
0
Jul 12 '15
[deleted]
9
Jul 13 '15
I've always found it odd that programmers obsess over the number of lines.
In C++, I tend to put each
{in a single line, and I never write anifstatement without declaring its scope. That increases my lines of code significantly - but it's much easier for me to come back to the code and read, so it is for others.5
4
u/danhakimi Jul 13 '15
The point is, he's trying to teach you how easy it can be. Yes, if you do it well, it will probably have more than two functions and will probably do interesting things and probably have a comment or two. But this is an introductory tutorial, and, for that purpose, the shorter the read, the better.
2
u/Anon_8675309 Jul 13 '15
Sadly, he's not teaching anything but bad practices. "Look 30 lines!", well, yeah, because it's junk code.
19
u/Sakacoco Jul 13 '15
Can't understand how this has so many upvotes...
Feels like written in 10 minutes
Not respecting any Python conventions
Using Flask and SQLAlchemy without the benefits of Flask-SQLAlchemy
Bad SQL queries
Bad practices overall
I would have much prefered a concrete example on how to use Flask-Restful with this, implementing all the CRUD and the RequestParser of the library.
Please try to improve on your grammar (mine isn't perfect either but at least I check my punctuation), you sound enthusiastic so there's that already :p
Anyway.