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”

How to add a program link to the Windows Explorer context menu

To add a program link to the Windows Explorer (“My Computer”) context menu – we need to edit the Windows registry.

The windows registry editor must be opened with administrator rights. Now we need to make the following changes:

Continue reading “How to add a program link to the Windows Explorer context menu”

Elementor widget: “show e-mail” button

Widget for Elementor page builder plugin for WordPress. It allows you not to show your e-mail on your pages explicitly to prevent gathering it by web-spiders and robots for spam. Visitors will see your e-mail address after clicking a “show e-mail” button.

Buy on Gumroad

Continue reading “Elementor widget: “show e-mail” button”

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 make directories with current data name in Total Commander

To quickly create directories with the name equal to the current date through the Total Commander:

  1. At first, let’s create a cmd script that makes a directory with the desired name:

In the first line of the script, the desired name for the directory is created in the format YYYY.MM.DD.

The second script line creates a directory with the specified name along the path passed in the input parameter %1

Continue reading “How to make directories with current data name in Total Commander”

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.

Automatic creation of Windows restore points

Creating Windows restore points is a good way to help to restore your computer if any troubles such as virus or advertising bots infection occur.

The restore point is a system image in which its current state is recorded (settings, appearance, installed programs). By creating and saving such a point, you can return your system to the recorded state at any time.

If you have opened a suspicious mail attachment accidentally, and now advertising banners appear on your computer screen (an advertising bot has been installed in your system) – with restore points you can return the system “back to the past” before the bot infects. Restore points are not absolutely troubles panacea, but very often, a “rollback” of the system saves the situation and returns the computer to a normal working state.

Continue reading “Automatic creation of Windows restore points”

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:

 

PHP: How to export HTML to DOC

When developing online projects, it is often necessary to save the HTML page as a document that can be used separately, for example, sent by e-mail, viewed and edited offline. A convenient way is to export the HTML page to one of the most commonly used text formats – doc.

The free open-source PHP-module “html_to_doc” can be used to export the HTML page to the DOC document. It can convert the HTML to the DOC document, which will be correctly processed by the text editor MS Word. If the HTML page includes images, they will be embedded in the DOC document.

The PHP module can be downloaded from https://github.com/Korchy/html_to_doc

Continue reading “PHP: How to export HTML to DOC”

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:

 

PHP: How to get the full class name with static inheritance

PHP supports the Late Static Bindings mechanism. The full class name with static inheritance we can get by calling the function:

Example:

 

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.

How to find out the Linux Debian version

To check the version of the Linux Debian installed on the server, type the following command:

The operation system response:

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:

 

How to start working with Git and GitHub

Version control systems for today are almost mandatory for any project. One of the most popular version control systems is Git. Consider working with Git in conjunction with GitHub – the largest hosting server for the deployment of IT-projects of joint development. GitHub allows publishing open source projects for free.

In this article, we start to work with Git and GitHub on the Windows operating system.

What we need, in order to start our work with Git:

  1. Download the Git distribution for windows:
    1. https://git-scm.com/download/win
  2. Install Git on the user’s computer
    1. All installing options can be set “by default”

Continue reading “How to start working with Git and GitHub”

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.

How to check code execution time in PHP

To check how match time code execution takes you need to frame checking code with following instructions:

This example result:

Executed for: 8.9997079372406 sek.

How to create symbolic links in Total Commander

Symbolic links in Windows (starts from Vista) can be created using mklink command.

Let’s make a button in Total Commander to create a symbolic link from the selected file:

  1. In a convenient location, for example in Total Commander plug-ins directory c:\TotalCommander\Plugins\ create a batch file mklink.cmd with the following content:

This command creates a symbolic link to the file transferred from the first input parameter and places it in a location from the second input parameter.

  1. Create a button in Total Commander panel by dragging this file to this panel.
  2. Right click on this button and select “Edit …”
    1. In the “Settings” field specify:

Save changes.

%P – path to the file under the cursor in active panel

%N – the name of the file under the cursor in active panel

%T – path to the location, open in a second (non-active) panel

As a result by pressing created button a symbolic link to the file under the cursor in the active Total Commander tab will be created in a location of the inactive tab with the same file name.

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.

WordPress TinyMCE Advanced Editor plugin – setting start value for numeric list

Little external plugin for Wordpress TinyMCE Advanced Editor. Adds new button to the editor menu to set start values of numeric lists.

Appends “start” attribute to “OL” tag.

Plugin usage example
Plugin usage example

Continue reading “WordPress TinyMCE Advanced Editor plugin – setting start value for numeric list”