Python 3-cookbook notes: Chapter 3 digital date and time

Keywords: Python Programming

Each section of Python 3-cookbook discusses the optimal solution of Python 3 in a certain kind of problem by three parts: problem, solution and discussion, or how to better use the data structure, function, class and other features of Python 3 in a certain kind of problem. This book is of great help to deepen the understanding of Python 3 and improve python programming ability, especially how to improve the performance of Python programs. If you have time, it is strongly recommended to read it.

For the purpose of learning notes, the content of this article is only part of the book according to their own work needs and usual use, and most of the sample codes in this article are directly pasted with the original code, of course, the code has been verified in the environment of Python 3.6. If you are interested, please read the full text.

python3-cookbook: https://python3-cookbook.readthedocs.io/zh_CN/latest/index.html

 

3.2 perform precise floating point operations

Python's floating-point calculation has errors, but in most fields, such errors are allowed, and they are the fastest in performance. However, if your program needs to provide very accurate calculation, and can tolerate certain performance loss, such as the calculation in the financial field, you can use the Decimal module. Note that the Decimal module needs to use words The form of a string represents a number.

In addition, if you are performing rounding or other calculation operations, you can consider using the methods in the built-in function round or math module.

>>> a = 4.2
>>> b = 2.1
>>> a + b
6.300000000000001
>>> (a + b) == 6.3
False
>>> (a + b) > 6.3
True
>>> 
>>> from decimal import Decimal
>>> a = Decimal('4.2')
>>> b = Decimal('2.1')
>>> a + b
Decimal('6.3')
>>> print(a + b)
6.3
>>> (a + b) == Decimal('6.3')
True
>>> 
>>> # decimal Rounding operation of
>>> from decimal import localcontext
>>> a = Decimal(1.3)
>>> a
Decimal('1.3000000000000000444089209850062616169452667236328125')
>>> a = Decimal('1.3')
>>> a
Decimal('1.3')
>>> b = Decimal('1.7')
>>> print(a / b)
0.7647058823529411764705882353
>>> with localcontext() as ctx:
    ctx.prec = 3
    print(a / b)

    
0.765
>>> 

 

 

3.3 format output of numbers

The format operation of numbers, including floating-point numbers and Decimal number objects, can use the format method of the built-in format function or string. The general form of the specified width and precision is [< > ^]? Width [,]? (. digits)?, which means that the front part is optional, < > ^ is the alignment, width is the width, comma is the thousand character, and. digits is the precision.

>>> x = 1234.56789
>>> format(x, '0.2f')
'1234.57'
>>> format(x, '>10.1f')
'    1234.6'
>>> format(x, '<10.1f')
'1234.6    '
>>> format(x, '^10.1f')
'  1234.6  '
>>> format(x, ',')
'1,234.56789'
>>> format(x, '0,.1f')
'1,234.6'
>>> format(x, 'e')
'1.234568e+03'
>>> format(x, '0.2E')
'1.23E+03'
>>> 'The value is {:0,.2f}'.format(x)
'The value is 1,234.57'
>>> 

 

 

3.4 binary integer

If you want to convert an integer to a binary, octal, or hexadecimal number string, use the built-in bin(), oct(), or hex() function, or use the format function for conversion. Of course, if you want to convert the corresponding decimal number to a decimal integer, use int() directly.

>>> x = 1234
>>> # Prefix with corresponding base: 0 b,0o,0x
>>> bin(x)
'0b10011010010'
>>> oct(x)
'0o2322'
>>> hex(x)
'0x4d2'
>>> # No corresponding base representation prefix: 0 b,0o,0x
>>> format(x, 'b')
'10011010010'
>>> format(x, 'o')
'2322'
>>> format(x, 'x')
'4d2'
>>> # Convert to decimal
>>> int('4d2', 16)
1234
>>> int('10011010010', 2)
1234
>>> int('2322', 8)
1234
>>> 

 

 

Packing and unpacking from 3.5 bytes to large integers

Conversion from byte string to integer is not commonly used, but if you encounter it, you can also consider this solution, that is, use the int.from_bytes() method and the int.to_bytes() method.

>>> data = b'\x00\x124V\x00x\x90\xab\x00\xcd\xef\x01\x00#\x004'
>>> int.from_bytes(data, 'little')
69120565665751139577663547927094891008
>>> int.from_bytes(data, 'big')
94522842520747284487117727783387188
>>> x = 94522842520747284487117727783387188
>>> x.bit_length() / 8
14.625
>>> x.to_bytes(15, 'little')
b'4\x00#\x00\x01\xef\xcd\x00\xab\x90x\x00V4\x12'
>>> x.to_bytes(15, 'big')
b'\x124V\x00x\x90\xab\x00\xcd\xef\x01\x00#\x004'
>>> 

 

 

3.15 string to date

When converting a string to a date, you can use the strptime() method in datetime, but write down this section specifically, because it is said that this method is implemented in pure Python, and the efficiency is not very good. If you know the format of a string, you are recommended to write a set of code for parsing and conversion. I also have this experience in normal use, especially after the conversion times are too many It's especially obvious.

>>> from datetime import datetime
>>> text = '2012-09-20'
>>> datetime.strptime(text, '%Y-%m-%d')
datetime.datetime(2012, 9, 20, 0, 0)
>>> 

Posted by Steve Angelis on Fri, 21 Feb 2020 01:52:28 -0800