5 Mins Read  May 26, 2015  Cuelogic

Python Tips & Tricks (Hacks)

Python is utilized by bigger companies mostly that can evaluate vast data sets; it is also used for system automation, web applications, big data, analytics, and security software. This article aims to show off some lesser-known tricks to put you on the path to faster development, easier debugging, and general fun.

TIPS:

1) For a critical code you should depend on an external package

Python makes many tasks easier, however it is not continuously possible to provide the best performance with time critical tasks. Utilizing a C, C++ programming language external package for time-critical tasks can improve application performance. These packages are platform-specific, so that you need the appropriate package for the platform you’re using. concisely, this solution gives up some application portability in exchange for performance that you can obtain only by programming directly to the underlying host.

Packages that helps to increase your app performance. They all operate in different ways:

2) Use keys for sorts

Old Python sorting code out there that will cost you time in creating a custom sort and speed in actually performing the sort during runtime. The best way to sort items is to use keys and the default sort() method whenever possible.

Ex:

In following example, each case the list is sorted according to the index you select as part of the key argument. This approach works just as well with strings as it does with numbers.

import operator somelist = [(1, 5, 8), (6, 2, 4), (9, 7, 5)] somelist.sort(key=operator.itemgetter(0)) somelist #Output = [(1, 5, 8), (6, 2, 4), (9, 7, 5)] somelist.sort(key=operator.itemgetter(1)) somelist #Output = [(6, 2, 4), (1, 5, 8), (9, 7, 5)] somelist.sort(key=operator.itemgetter(2)) somelist #Output = [(6, 2, 4), (9, 7, 5), (1, 5, 8)],

3) Optimizing loops

With Python, you can rely on a wealth of techniques for making loops run faster. However, one method developers often miss is to avoid the use of dots within a loop.

For example, consider the following code:

lowerlist = [‘this’, ‘is’, ‘lowercase’] upper = str.upper upperlist = [] append = upperlist.append for word in lowerlist: append(upper(word)) print(upperlist) #Output = [‘THIS’, ‘IS’, ‘LOWERCASE’]

Here the aim is to reduce the amount of work that Python performs within loops because the interpreted nature of Python can really slow it down in those instances. Whenever you make a call to str.upper, Python evaluates the method. Despite, if you place the evaluation in a variable, the value is already known and Python can perform tasks faster.

4) Multiple coding approach

Using precisely the same coding approach every time you create an application will almost certainly result in some situations where the application runs slower than it might. Try a little experimentation as part of the profiling process. For example, when managing items in a dictionary, you can take the safe approach of determining whether the item already exists and update it or you can add the item directly and then handle the situation where the item doesn’t exist as an exception.

Consider this example: 

n = 16 myDict = {} for i in range(0, n): char = ‘abcd'[i%4] if char not in myDict: myDict[char] = 0 myDict[char] += 1 print(myDict)

This code will generally run faster when myDict is empty to start with. However, whenmyDict is usually filled with data, an alternative approach works better.

n = 16 myDict = {} for i in range(0, n): char = ‘abcd'[i%4]

try:

myDict[char] += 1 except KeyError: myDict[char] = 1 print(myDict)

The output of {‘d’: 4, ‘c’: 4, ‘b’: 4, ‘a’: 4} is the same in both cases. The only difference is how the output is obtained. Thinking outside the box and creating new coding techniques can help you obtain faster results with your applications.

5) Use a newer version

In general, every new version of Python included optimizations that make it faster than the previous version. You need to use the new libraries you obtained to use with the new version of Python and then check your application for breaking changes. The limiting factor is whether your favorite libraries have also made the move to the newer version of Python. Rather than asking whether the move should be made, the key question is determined when a new version has sufficient support to make a move viable. You need to verify that your code still runs. Only after you make the required corrections will you notice any difference.

However, if you just ensure your application runs with the new version, you could miss out on new features found in the update. Once you make the move, profile your application under the new version, check for problem areas, and then update those areas to use new version features first. Users will see a larger performance gain earlier in the upgrade process.

6) Cross compile your app:

When working with a cross-compiler, be sure it supports the version of Python you work with. To make this solution work, you need both a Python interpreter and a C++ compiler.

Tricks:
1) Build strings using str.join not +

Suppose you wanted to build the string ‘0–1–2–3–4’.

You could use a for-loop like this: 

result = ” for i in range(5): result = result + str(i) if i < 4: result = result + ‘–‘ But there are at least three problems here: (1) it can be written more succinctly, (2) it uses ‘+’ instead of ‘str.join’ and worst of all, (3) it has to test if i is the last item before adding the separator –.
The better way to build a string with a repeated separator is to use str.join: >>> ‘–‘.join(map(str,range(5))) ‘0—1–2–3–4’

‘–‘is a string. ‘—’.joinis the string’s join method. The join method expects one argument, which is a sequence of strings.

>>> map(str, range(5)) [‘0’, ‘1’, ‘2’, ‘3’, ‘4’]

2) Dictionaries within dictionaries:

This amazing trick for nesting dictionaries as values within other dictionaries to an arbitrary depth

import collections def tree(): return collections.defaultdict(tree)

3) Build lists:

duplicate – change [5]to the list and 5to the multiplier you need 

>>> [5]*5 [5, 5, 5, 5, 5]

comprehension – change x**2 to the value and range(5)to the list you need

>>> [x**2 for x in range(5)] [0, 1, 4, 9, 16]

mapping – change intto the function and [‘1’, ‘2’, ‘3’]to the list you need

>>> map(int, [‘1’, ‘2’, ‘3’]) [1, 2, 3]

All of the above hacks can help you develop faster Python apps. However there are no silver bullets. None of the tips will work every time. Some work better than others with specific versions of Python even the platform can make a difference.

Recommended Content

Go Back to Main Page