Python: how to zip a text string

In order to reduce the size of a long text string, for example, to minimize traffic when sending some text data over the Internet, it can be compressed before sending and unzipped after receiving. The size of transmitted data is significantly reduced in comparison with sending text strings in their original format.

To zip a text string in memory, we can use the “zlib” module.

Let’s use the “compress” function to compress the string. This function takes a byte string as an input parameter and returns the compressed byte string.

As we can see with this simplest example, the line size has been reduced by more than one and a half times.

Continue reading “Python: how to zip a text string”

Python: How to get the first found element in a list or None

To get the first element found in a list by some condition or None if nothing was found we can use the following construction:

With nothing found it returns None:

 

How to check what Python version is using?

To check what version of the Python interpreter is using to execute code, you can use the “version_info” command from the “sys” module:

In this example the Python version 3.5.2. is using.

Python: How to get defined classes list from module (*.py file)

The list of classes defined in the * .py file can be obtained using the built-in module “inspect”.

For example, for the “test_cls” module:

we can get a list of classes with the following code:

 

Python: How to walk through the list items in pairs from current to next

If we have a list:

in order to go through the elements of this list in pairs from the current to the next, we can use the following code:

Results:

 

Python: How to pass arguments as a list to a function that takes a variable number of arguments

Some functions take in their parameters a variable number of arguments *args, for example, the itertools.product function.

In order to pass the list as the parameters to this function, we need to use the * operator:

 

Python: How to convert a string with mixed numbers and ranges of numbers to a list

Conversion of a string with both individual numbers and ranges of numbers to the list of integer values can be made as follows:

For example a line:

Split the line by comma delimiter:

Let’s split the resulting list into two lists. In the first list, we put only the individual values. In the second – the values got from the ranges of numbers.

Combine the lists into one and drop the duplicate values.

Complete code:

 

Python: How to shift values in a list

To shift values in a list (replace the value at the first place of the list to its last place and vice versa), you need to rearrange the list with two slices.

The shift for the list:

forward:

and backward:

If a cyclic shift is necessary, repeat this command the required number of times in “for” cycle.

Python: how to save a dictionary with objects to JSON

Each object’s class to be stored in JSON in a convenient for viewing way must have the __repr__ function, which returns the text representation of the object.

The dictionary with objects:

Saving the dictionary to JSON:

 

Python: how to enclose a tuple in a tuple

Tuples in python can be nested.

If simply add one tuple to another through the concatenation operation “+”, python will merge the values of the tuples:

In order an added tuple to become nested, put a comma “,” after an added tuple:

 

Python: How to find the polygon center coordinates

Obtaining the “centroid” – convex polygon central point coordinates, from polygons points (vertices) coordinates:

The input function parameter is a tuple with coordinates of the polygon points. The function returns a tuple with the centroid coordinates:

 

The for loop counter in Python does not have a local scope

One of the for loops features in Python is that the iterated variable (counter) does not belong to the local scope of the loop.

For example, after executing the following code:

The “a” variable declared before the for loop will change its value if the variable with the same name “a”  declared as the loop counter.

It is necessary to remember about this feature, in order to avoid writing to the previously declared variable the values of the iterator of the for loop.

Python – multiline comment in PyCharm

Python language has no internal multiline comment syntax (like \* … *\ in other languages). There are two ways to solve this:

  1. Use multiline string literal syntax: ′′′

Disadvantage of this way is that such comment remains constant string and processed in finished code.

  1. If the PyCharm IDE is used to write Python code – select multiple code rows to comment and press keyshot Ctrl + / to comment all of them.

To remove comments from multiple commented strings select them and press Ctrl + / again.