Tip: Developers using command prompt to run the program

If you are a developer and if you are like me who compiles/interprets the program using command prompt, If you are invoking command prompt and if you are navigating all the way to the location of the folder that contains the program, then this is a cool tip for you.

Instead of  ”Type cmd in run” and navigate to whatever folder the program is located in.

Do this:

Navigate directly to the folder where the program is located.(Note: Dont use cmd, just navigate directly).

Click Shift button on your keyboard and then Click the right mouse button holding the shift button. Context menu will open now. In the context menu, click
Open command window here“.

Here is the screenshot.

command prompt in same folder

I hope this saves some time for you.

Do you have a cool tip to share, then post it here in the comment box.

Fast Python tips from Guido Van Rossum

Guido Van Rossum is a computer programmer and author of Python Programming Language. On his Google + page, he shared 6 tips for faster python.

- Avoid overengineering datastructures. Tuples are better than objects (try namedtuple too though). Prefer simple fields over getter/setter functions.

- Built-in datatypes are your friends. Use more numbers, strings, tuples, lists, sets, dicts. Also check out the collections library, esp. deque.

- Be suspicious of function/method calls; creating a stack frame is expensive.

- Don’t write Java (or C++, or Javascript, …) in Python.

- Are you sure it’s too slow? Profile before optimizing!

- The universal speed-up is rewriting small bits of code in C. Do this only when all else fails.

Also make sure you read the comments on his Google+ page.

Project Euler Solution 1 in Python

My solution for the first challenge of Project Euler.

print sum([each for each in range(1,1000) if each % 3 == 0 or each % 5 == 0])

Time it takes to run : 0.0041 ms.

Full code that I used to calculate time.

import time
a = time.clock()
print sum([each for each in range(1,1000) if each % 3 == 0 or each % 5 == 0])
print time.clock() – a

Pyflakes Basic Setup Guide

If you want to catch the syntactic errors, then pyflakes helps you find the syntactic errors.

Reference : programmers.stackexchange.com/questions/15468/what-are-the-drawbacks-of-python
Read the first answer posted and the comments for the answer.

To download pyflakes, visit pypi.python.org/pypi/pyflakes and click the link under File and download the file. Uncompress it. Then type python setup.py install in the command prompt by navigating to the setup.py file in the pyflakes folder.

pyflakes installation

 

Now if you run your python program using PyFlakes, it will not run. You will get an error message like this:

pyflakes is not recognized as an internal or external program, operable program or batch file.

pyflakes not recognized as an internal or external command error

 

Follow the procedure written by Abhi in the hyperlink mentioned below.

stackoverflow.com/a/3523306

Now, if you run the program using pyflakes, it should work.

pyflakes error finding run the program

 

Practical Django Projects Book Chapter 4

Previous… Chapter 3

Error Description 1:

When you complete the tasks mentioned in the page 47 and 48. When you try to run the command

If you get this error

 

Solution:

Follow these steps:

If  you are using windows 7 operating system. Then open the Python IDLE and do the following

If you are confused, here is the screenshot.

 

Practical Django Projects No module named Coltrane
Now run python manage.py syncdb again.

If you still get this error, then the possible solution is:

Solution:

You folder structure should be like this:

 

Folder Structure of coltrane

 

You have to create the coltrane folder inside the cms folder. Open you cms folder, you will notice another cms folder, you have to create the coltrane folder beside that folder like this:

Practical Django Projects Book Errors for Django 1.4

If you are practicing the code from the book Practical Django Projects with the Django version 1.4, then you will get some errors due to version change. The second edition of the Practical Django Projects is updated for Django 1.1. The latest version of Django at the time of writing this post is Django 1.4.

You can find the complete source code for Django 1.4 at github:
Chapter 3

These are the following errors for Chapter 3. You can find the solutions below the error description.

Chapter 3:

Error Description 1:

No FlatPage matches the given query.

Solution: If you get this error, then the error is not with the version. You have to include a forward slash at the end of the page that you requested. If you remember, in the regular expression you defined in the URLConf(urls.py), you used $ after /

In regular expressions, $ means end of the string. If you want to request a page in this case you put a / at the end of the page path.

For example: Instead of requesting

which will raise the error,
request the page in this manner.

 Error Description 2:

No module named search

Solution: 

If you get this error, then the problem is with the code given in the book due to the version change.

The author tells us to add the following code in the urls.py file.

Change it to

Notice that we have removed the cms. In django-1.4 version, cms is not necessary.

Error Description 3:

In the chapter 3, when you create the SearchKeyword model and as per the instructions of the book, if you  add ‘cms.search’ in the INSTALLED_APPS in the settings.py file in the cms directory and run python manage.py syncdb. You will get this error:

Error: No module names search

Solution:

Instead of adding

in the INSTALLED_APPS in the settings.py file, simply add

Now run python manage.py syncdb.

Error Description 4:

Import Error at /admin/

No module named search.models

Solution:

The author asks us to create admin.py file with some code. He din’t mention the location of the admin.py file. Here our goal is to display the SearchKeyword in the admin interface. So create the admin.py file in the search directory.

Instead of

Use

Countine… Part 2: Chapter 4

Find all the modules loaded in Python

To find all the modules loaded by python, execute the following script:

If you run the above code, you will can see all the modules that are loaded.