How to test a Django app - python

I´m quite new at python/django development and I would like to know what's the best way to test these kinds of applications. I've been doing a little research and I found libraries like magicmock or minimock but I don't really have any idea if they're good or bad or what are the best practices for Django. Can anyone give me some tips? The project I'm working on it's this one https://github.com/Smart-Studio/fbclm-backend/tree/develop , just in case someone wants to take a look a it.
Thanks!

The Official Django Tutorial touches on testing in chapter 5.
The book Test-Driven Development with Python by Harry Percival (O’Reilly) is available free online. It is a tutorial on building a Django project with complete, comprehensive testing. Be warned, it is very slow paced, sometimes frustrating. The author is dead serious about testing every little thing, and he knows his stuff. There is much to be learned from him.
Edit: Some of the remarks above are not fair. I was about a third of the way through Percival's book when I gave this answer. The author's style of teaching is not slow. The pace is comfortable. The author does lead the reader to type some code only to replace it later, but this does help to understand how the process being tested actually works. For someone who is unfamiliar with TDD, the building process can feel painfully slow. You can write several lines of testing code for every single line of application code, before even writing any of the application code. The author doesn't test "every little thing". He gives the reader guidelines to help decide what does and doesn't need to be tested, and there are several things that need to be tested if you really want good coverage.
I apologize for taking this out of the Stackoverflow context for answers. In this case, the crime had already been committed and this edit was required to fix the bug in my answer.

Related

How to get comfortable with documentation?

I am a novice and I google every little problem I have. Sometimes the answer might be easily availible but I seem to go over easier fixes like blogs or stackoverflow.
I'm curious as to how the rest of you go about solving your problems.
The reason why I ask this is because I'm tired of writing hackey code strung along from sources. It seems like the biggest hurdle is that I don't understand how to find what I'm looking for in the documentation directly.
Sorry, if this doesn't belong here. Still new!
I will give you my answer, which really isn't from a master, but someone with a similar question not too long ago.
First, I don't think documentation is going to do much to help you become "less hacky." Yes, if you read the entire Python docs you will pick up some useful things, but if you're like most people you will probably just be overwhelmed and forget most of what you have read.
Googling is not a bad thing, especially if you are learning alone - in fact, it might be one of the best ways to learn. The docs aren't necessarily better than SO or Google - docs are also not generally geared so much at helping you solve problems as other sources are. The less common the thing you are trying to do, the more the docs will help.
If you have someone you know that is also learning, pair programming (combined with Googling) can be effective; I understand it is also how a lot leading companies get their programmers to work.
If you are looking for something a bit more "high level," I would read something on test-driven and especially behaviourally-driven development in your target language(s). Test-writing is an extremely important skill, helps your code be "cleaner" and DRYer, and what no one tells you starting out is that most of the lines of (modern) commercial code-bases are mostly tests. To my mind, moving out of "hacky" is largely learning to deal with design problems, which you can do most easily by learning test-driven development.
You can also try to learn a bit more about common design patterns through (1) "cookbooks" (eg. the Python cookbook, Automate the Boring Stuff with Python, Two Scoops of Django) or (2) doing math/logic problems so that you understand how to boil a problem down into its nuts and bolts. A lot of the hack events on HackerRank, for example, really come down to removing Os by thinking through a problem and simplifying algorithms which requires high-level mathematical / logical (i.e. proofs) reasoning abilities.
There are also some good (free/cheap) online courses for this sort of thing (eg. Agile Development Using Ruby on Rails) which might be worth your while to check out.
Fundamentally though, solving problems, with code or not, is a creative exercise. There is nothing wrong with "stringing together" solutions from various sources - just ask an artist if they generally work with or without "inspiration" from other works, the world, etc. They usually do. It's not all meant to come from you and only you.
Finally, someone will probably give you flack about the nature of the question you have asked here. SO questions are supposed to elicit generally non-subjective answers... unless things have changed a lot.

How do I get an overview and a methodology for programming in Python

I've started to learn Python and programming from scratch. I have not programmed before so it's a new experience. I do seem to grasp most of the concepts, from variables to definitions and modules. I still need to learn a lot more about what the different libraries and modules do and also I lack knowledge on OOP and classes in Python.
I see people who just program in Python like that's all they have ever done and I am still just coming to grips with it.
Is there a way, some tools, a logical methodology that would give me an overview or a good hold of how to handle programming problems ?
For instance, I'm trying to create a parser which we need at the office . I also need to create a spider that would collect links from various websites.
Is there a formidable way of studying the various modules to see what is needed ? Or is it just nose to the grind stone and understand what the documentation says ?
Sorry for the lengthy question..
The MIT Intro to Computer Science course on the MIT OpenCourseWare website was taught using Python. There are 24 lectures available as videos that you can watch for free.
It's kind of academic to be sure, but it would give you a very solid foundation to start from.
Start working your way through the Essential Python Reading List, which has articles on how to code in Python and how to do it well.
If you like a more academical approach try Learning Python from Mark Lutz.
For the use of standard libraries, the official docs are very good. More hands on descriptions can also be found in PYMOTW from Doug Hellmann
It might be useful to get some information on Object Oriented programming (just what is the whole class thing about, and how do you tell if your classes are good/poor/indifferent). Mark Lutz' book Learning Python has an entire Part (several chapters) on OO. If this stuff is new to you, it might be helpful to take a look. Two other books I have found quite useful: The Python Cookbook (Alex Martelli, a prolific contributor here), and the Python Essential Reference (David Beazley).
Just do your project, learning what you need to along the way. By the time you do that a couple times, you'll "get" it. And you'll only improve from there.
You can also read other peoples' code: download X that looks interesting and read through the code to understand how it works.
Those two tips will help you learn any language. Aside from that, Dive Into Python is a great resource for learning a lot about Python.

PHP Frameworks (CodeIgniter, Yii, CakePHP) vs. Django

I have to develop a site which has to accomodate around 2000 users a day and speed is a criterion for it. Moreover, the site is a user oriented one where the user will be able to log in and check his profile, register for specific events he/she wants to participate in. The site is to be hosted on a VPS server.Although I have pretty good experience with python and PHP but I have no idea how to use either of the framework. We have plenty of time to experiment and learn one of the above frameworks.Could you please specify which one would be preferred for such a scenario considering speed, features, and security of the site.
Thanks,
niting
This is a very subjective question but personally I'd recommend Django. Python is a very nice language to use and the Django framework is small, easy to use, well documented and also has a pretty active community.
This choice was made partly because of my dislike for PHP though, so take the recommendation with a pinch of salt.
Most of the frameworks out there nowadays are fast enough to serve whatever needs you will have. It really depends on in which environment you feel most comfortable. Though there are nuances here and there, MVC frameworks share a lot of the same principles, so whichever you choose to use is really a matter of which you most enjoy using.
So, if you like Python more, there's your answer. Use a Python framework, and Django is the best. If you like PHP more (which I personally don't), you've got some more decisions to make. But any of the PHP frameworks are fine. They really are. Just pick one that looks nice with comprehensive documentation and get to work.
I've worked with CakePHP and Django and I really recommend Django. I don't know too much about CodeIgniter, but I remember ruling it out when I was evaluating frameworks myself about a year ago. CakePHP seemed much more developed at the time.
First of all, the Django community is much bigger and has spent a lot of time focusing on reusable apps. This means that you get a lot of functionality for free. Pair this with the django admin, and you have a lot of things already done for you. I haven't kept up with the PHP frameworks much, but I'm pretty sure Django is also more developed.
This is more of a personal thing, but I just like Python over PHP. Compare the way models are done in CakePHP and Django: http://book.cakephp.org/view/67/Understanding-Models, http://docs.djangoproject.com/en/dev/topics/db/models/#topics-db-models. The python is clearly more readable.
Keep in mind that Django gives you an awesome ORM and builds your schema for you, i.e. you never have to touch the database if you don't want to. With the PHP frameworks, you have to do your own db design, which just slows me down at this point. You can always go in and add indexes for speed later.
This is probably the most biased, but if you are starting a new application - seriously - just stick with Django or Ruby on Rails. There is a reason everyone talks about them and they have the biggest communities and best developers behind them.
You can also check out Pinax for a lot of Django goodies.
Codeigniter it's fast and very documented also has a large community to and finaly friendly with the programmer.
CodeIgniter is a great PHP framework that is fast and has excellent documentation. Start reading through their user guide and it will give you a good idea how to work with the framework.
Extending Matchu:
Or, -If you like PHP more- its time to learn/growup about other things like Python. Its not hard to learn, and when you get started it gets very enjoyable.
Many people has done the PHPtoPython/Django port, like Mozilla, Netgeo, Nasa, TheOnion, etc.
If for the PHP part I would choose CodeIgniter - it doesn't get too much into your way. But it doesn't have any code/view/model generators out of the box, you need to type a bit.
But languages other than PHP appear to be more sexy.
I am using CodeIgniter 1.7.2 and for complex websites it's very good and powerfull, but it definitely is missing some kind of code generator which will allow for example to build an IT application in one click.
I had the impression (from watching a tutorial) that Django has it.

Writing a DBMS in Python

I'm working on a basic DBMS as a pet project and planning to prototype in Python.
I figure there's a reason there are only a few Python databases, and my gut agrees that my favorite language will be too slow to act as an honest performing database, but I'm looking forward to using it to learn what I need quickly.
Would someone please contradict me? Is Python as ill-suited right now for this sort of thing as I think?
EDIT 4/16-
I've posted another getting-started-on-this-project type question if anyone is interested.
(Non-Relational) DBMS Design Resource
It's doubtful that anything you create as a pet project is going to turn out to be popular. Presumably you are mostly doing this as a learning experience and for fun.
Given these facts, there's no reason to stop yourself so early just because you think there might be performance problems. Just do it and have fun with it. The idea of a pure Python database will at least be academically interesting to others.
You can always do some performance profiling to find the bottlenecks and use the usual approaches in speeding things up (CPython, Cython, ctypes, etc.)
Don't be so quick to dismiss Python's huge benefits that you get in return for the performance hit. Namely rapid development.
If performance isn't a huge issue there's no reason Python can't do what you need, it certainly has all of the tools to do so. Designing a database certainly isn't a trivial undertaking, of course, but assuming you have the know-how and Python-fu to put in everything you need (of course, being helped by all of us here at SO ;) ) then the basic building blocks are all there.
For reference, there's at least one DBMS written in pure Python that I know of: KirbyBase

web2py in the future? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
Given the size of web2py and the lack of resources and corporate support, do you think it would be advisable to learn web2py as the only web development framework I know. I'm considersing learning Ruby on Rails or web2py for a website I need to create for as a school project.
web2py does have a smaller market share than competitor products but it is also much younger. I have knowledge of at least 13 consulting companies that provide web2py support. Anyway, I do believe web2py is much easier to use than other systems and therefore you will need less support that you may think. Most of the current users get their support via the web2py google group where you can find 29781 messages and almost all questions have been answered within 24 hours by one of the contributors.
Learning is bad. Sherlock Holmes explains:
"You see," he explained, "I consider
that a man's brain originally is like
a little empty attic, and you have to
stock it with such furniture as you
choose. A fool takes in all the lumber
of every sort that he comes across, so
that the knowledge which might be
useful to him gets crowded out, or at
best is jumbled up with a lot of other
things, so that he has a difficulty in
laying his hands upon it. Now the
skilful workman is very careful indeed
as to what he takes into his
brain-attic. He will have nothing but
the tools which may help him in doing
his work, but of these he has a large
assortment, and all in the most
perfect order. It is a mistake to
think that that little room has
elastic walls and can distend to any
extent. Depend upon it there comes a
time when for every addition of
knowledge you forget something that
you knew before. It is of the highest
importance, therefore, not to have
useless facts elbowing out the useful
ones."
I'm sure I'm not the only one who has wasted an inordinate amount of time wading through the many bad and poorly documented Python web frameworks trying to find one I can just use. If I was programming in Ruby or PHP I probably would have spent that time actually writing a web application. This is the curse of web development in Python.
This bit of flamebait may help:
stackoverflow.com tags about web frameworks http://spreadsheets.google.com/pub?key=tZCdBPAkC75t27UzsPdLfMg&oid=2&output=image
Omitted from the chart are the 13,000+ questions tagged [php], but let's not go there.
To be clear, even though choosing a framework for Python web development can be confusing, once you decide on one you get to program in Python. This is the blessing of web development in Python. It can be really nice.
My advice is don't accept anything less than a framework with excellent documentation. With the amount of choices out there there's no need to settle for poor, incomplete docs. Failing that, the simplest frameworks, those lacking room for any magic, are pleasant to work with and quickly learnable.
web2py may be young, but the mailing list has ~2000 messages / month, which is similar to Django and far more than Turbogears. I usually get answers to my questions within a few hours.
There is also an excellent online book, but I find the best source of information is the mailing list.
I have used both RoR, Django, Turbogears, and web2py, and find web2py the most productive.
Learning is good.
Learning something (that eventually goes away) is no loss at all. The basic skills of web development (HTML, CSS, URL-parsing, GET vs. POST) don't ever change.
Frameworks come and go. Learn as many as you can. Learn how to manage your learning so that you (a) get to the important stuff first and (b) leave the other framework stuff behind when tackling a new framework.
Every framework has it's bias (or focus). Once you figure this out, you can make use of them without all the "compare and contrast" that slows some people down. Once you've learned web2py, you have to be careful learning Django that you start fresh, with no translation from old concepts to new.
Web2py is a good one to learn. If this is going to be deployed to a server, double check it supports wsgi. Sometimes php is the way to go because you know it's supported almost anywhere.
Ask yourself what you are looking to gain from the experience. Ie, is it more important to just get the application built and running with a minimum of time and effort, or are you trying to learn about web stack architecture?
If you're just looking for results, obviously you'll have more code and documentation to borrow from if you stick with a more commonly used framework. If you grit your teeth and accept Django's view of the world, you can build very functional applications very quickly. If you can find some pre-made reusable Django apps that handle part of your problem, it'll be even faster.
But if you want to make sure you have a very solid understanding of everything in the request cycle from HTTP request handling to database access and abstraction to form generation and processing and HTML templating, you'll be bettered served with a minimal framework that forces you to think more about the architecture and has a small enough codebase that you can just read it all top to bottom and not really need documentation beyond that. In that case though, I'd advise going even deeper and building your own framework on top of a WSGI library (you don't actually want to waste time learning the intricacies of working around browser quirks if you can help it). Once you've built your own and seen where things get complicated and where the tradeoffs are, you'll be in an excellent position to judge other frameworks and decide if there's one that does things the way you want to work.
This may seem slightly off-topic, but Paul Graham has probably the best essay on this subject that I have seen: The Python Paradox.
Let me put it this way, if you want to work for me, I notice this kind of free thinking and experimentation on a resume, whether the work was commercial, academic, or otherwise. And I'm pretty sure I'm not alone.
Glad I found this thread! Cause some outdated pages and broken external links on Web2Py's website almost scared me off. But at least now I know there's a pretty good community around Web2Py.
I've just been looking through a load of Python web frameworks, and Web2Py's description sounded enticing and managed to make Django sound overly laborious. Pretty sure there are some tangible benefits to Django's design decisions avoiding "too much magic" when it comes to larger projects.
But to just throw something up on the web with err "sane defaults" sounds perfectly good to me. Instead of throwaway scripts, we can make throwaway websites to handle some temporary thing...
There should be room for an appliance style framework with no installation...
Interesting possibilities for some projects. I saw someone already got a python framework + server to work on android phones :))
For me, thanks to this thread, I will just learn both.
Another thought; if Web2Py is open source and you like what it does you might not even mind being the only user at some point in the future, since you can add features to it yourself?
Mind you, I have not used either yet, just read the docs. I think the Web2Py people should put up a blurb on their website to differentiate themselves from Django in more detail, I haven't been able to check off all my question marks for choosing the right one.
I've already used Java EE and Django. The web2py learning curve is so fast! It's incredible! Things that I was getting a time to develop in three days using java, I can do fastly using web2py. Of course, Web2py has not the same ready plugins that RoR, but, doubtless, we can do these things fastly using web2py. Therefore, is a good opportunity to start learning = )
I'm agree with S.Lott saying that:"Learning something (that eventually goes away) is no loss at all."
YEAH It's true but let me suggest that also a scholastic project should be able to reach the better support possible, otherwise could be very frustrating and a waste of time to learn and teach something not well supported, debugged, stable etc.
The time you spent, and maybe your auditors/students, should in some sense projected with an eye to the future...
just for example take a look to turbogears

Categories