In Python, the aioschedule library is often used to execute tasks at certain intervals, for example, when developing Telegram bots.
Unfortunately, the aioschedule package has not been updated for several years and has lost compatibility with the latest versions of Python. When used in Python 3.11, an error is thrown at the time of tasks execution
Passing coroutines is forbidden, use tasks explicitly.
To chek if list A is completely included in list B – all elements of list A are present in list B, we can use the “superset” and “subset” methods of the standard Python “set” class.
Converting lists to sets also allows us not to worry about checking for duplicates if they exist in the compared lists.
subset
The “subset” method acts on a smaller set and checks if it is in a larger set.
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.
Python
1
2
3
4
5
6
7
8
9
10
11
12
importzlib
long_text='Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'
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.
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:
Python
1
lst=[1,2,3,4,5]
forward:
Python
1
2
3
4
lst=lst[1:]+lst[:1]
print(lst)
[2,3,4,5,1]
and backward:
Python
1
2
3
4
lst=lst[-1:]+lst[:-1]
print(lst)
[1,2,3,4,5]
If a cyclic shift is necessary, repeat this command the required number of times in “for” cycle.
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.
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:
Python
1
2
3
4
5
6
7
a=0
print(a)
b=[1,3,7]
print(b)
forainb:
pass
print(a)
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.
Python
1
2
3
4
# output
>0# print(a)
>[1,3,7]# print(b)
>7# print(a) after loop execution
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.