Python standard library: second half (Python learning memo ⑨)

Output shaping

reprlib, pprint, textwrap, locale, etc.


import reprlib
import pprint
import textwrap
import locale

#reprlib is repr()In another version of, the huge container part project is omitted and displayed.
charset = reprlib.repr(set('supercalifragilisticexpialidocious'))
print(charset)
# {'a', 'c', 'd', 'e', 'f', 'g', ...}

#pprint displays the data structure in an easy-to-understand manner
t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta', 'yellow'], 'blue']]]
pprint.pprint(t, width=30)
# [[[['black', 'cyan'],
#    'white',
#    ['green', 'red']],
#   [['magenta', 'yellow'],
#    'blue']]]

# textwrap.fill()Wraps each paragraph to fit the specified width
doc = """The wrap() method is just like fill() except that it returns a list of strings instead of one big string with newlines to separate the wrapped lines."""
print(textwrap.fill(doc, width=40))
# The wrap() method is just like fill()
# except that it returns a list of strings
# instead of one big string with newlines
# to separate the wrapped lines.

#locale is a national representation(Unit etc.)Can be called
locale.setlocale(locale.LC_ALL, 'English_United States.1252')
conv = locale.localeconv()
x = 1234567.8
print(locale.format("%d", x, grouping=True))
# 1,234,567
print(locale.format_string("%s%.*f", (conv['currency_symbol'], conv['frac_digits'], x), grouping=True))
# $1,234,567.80

template

point

from string import Template
t = Template('${village}folk send $$10 to $cause.')
sentence = (t.substitute(village='Nottingham', cause='the ditch func'))

print(sentence)
# Nottinghamfolk send $10 to the ditch func.


#Is the substitute method a dictionary of placeholders?
#If you do not pass the keyword argument, KeyError will occur, so
# safe_The substitute method may be safer
#If there is no data, the placeholder will be output as it is
t = Template('Return the $item to $owner.')
d = dict(item='unladen swallow')
sentence = t.safe_substitute(d)

print(sentence)
# Return the unladen swallow to $owner.

You can also change the delimiter instead of $ in the Template subclass

Change file number


import time, os.path
from string import Template

photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']

class BatchRename(Template):
    delimiter = '%'

fmt = input('How do you rename(%d-date%n-number%f-format): ')
#How do you rename(%d-date%n-number%f-format): Ashley_%n%f 

t = BatchRename(fmt)
date = time.strftime('%d%b%y')

for i, filename in enumerate(photofiles):
    base, ext = os.path.splitext(filename)
    newname = t.substitute(d=date, n=i, f=ext)
    print('{0} --> {1}'.format(filename, newname))

# img_1074.jpg --> Ashley_0.jpg
# img_1076.jpg --> Ashley_1.jpg
# img_1077.jpg --> Ashley_2.jpg

Processing binary data records

You can use the struct modulespack ()and ʻunpack ()` to process variable-length binary records.

Example of looping each header information of zip file



import struct

# rb:Read in binary
with open('myfile.zip', 'rb') as f:
    data = f.read()

start = 0

for i in range(3): #Shows the headers of the first three files
    start += 14
    fields = struct.unpack('<IIIHH', data[start:start+16])
    crc32, comp_size, uncomp_size, filenamesize, extra_size = fields

    start += 16
    filename = data[start:start+filenamesize]
    start += filenamesize
    extra = data[start:start+extra_size]
    print(filename, hex(crc32), comp_size, uncomp_size)

    start += extra_size + comp_size #Skip to next header

Multithread

point

import threading, zipfile

class AsyncZip(threading.Thread):
    def __init__(self, infile, outfile):
        threading.Thread.__init__(self)
        self.infile = infile
        self.outfile = outfile
    def run(self):
        f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
        f.write(self.infile)
        f.close()
        print('Finished background zip of:', self.infile)

background = AsyncZip('mydata.txt', 'myarchive.zip')
background.start()
print('The main program is running')

background.join() #Wait for the background task to finish

print('The main program was waiting for the end of background processing')

Log

point

import logging

logging.debug('Debugging information')
logging.info('Informational message')
logging.warning('Warning: config file %s not found', 'server.conf')
logging.error('Error occurred')
logging.critical('Critical error -- shutting down')

# WARNING:root:Warning: config file server.conf not found
# ERROR:root:Error occurred
# CRITICAL:root:Critical error -- shutting down

Weak reference

import weakref, gc

class A:
    def __init__(self, value):
        self.value = value
    def __repr__(self):
        return str(self.value)

a = A(10) #Generate a reference
d = weakref.WeakValueDictionary()
d['primary'] = a #Do not generate a reference
print(d['primary']) #If the object is alive, fetch it
10

del a #Delete reference
gc.collect() #Perform garbage collection

d['primary'] #Entry has been deleted automatically
# Traceback (most recent call last):
#   File ".\weakref_sample.py", line 17, in <module>
#     d['primary'] #Entry has been deleted automatically
#   File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.1520.0_x64__qbz5n2kfra8p0\lib\weakref.py", line 137, in __getitem__      
#     o = self.data[key]()
# KeyError: 'primary'

List manipulation tool


from array import array

a = array('H', [4000, 10, 700, 22222])
print(sum(a))
# 26932

print(a[1:3])
# array('H', [10, 700])

from collections import deque
d = deque(["task1", "task2", "task3"])
d.append("task4")
print("Handling", d.popleft())

unsearched = deque([starting_node])
def breadth_first_search(unsearched):
    node = unsearched.popleft()
    for m in gen_moves(node):
        if is_goal(m):
            return m
        unsearched.append(m)

Floating point calculation of decimal numbers

Recommended Posts

Python standard library: second half (Python learning memo ⑨)
Python standard library: First half (Python learning memo ⑧)
Python class (Python learning memo ⑦)
Python module (Python learning memo ④)
Python & Machine Learning Study Memo ②: Introduction of Library
Python exception handling (Python learning memo ⑥)
Python control syntax, functions (Python learning memo ②)
Input / output with Python (Python learning memo ⑤)
<For beginners> python library <For machine learning>
Interval scheduling learning memo ~ by python ~
"Scraping & machine learning with Python" Learning memo
(python) Deep Learning Library Chainer Basics Basics
Python memo
python memo
Python memo
python memo
python learning
Python memo
Python memo
Python memo
Python & Machine Learning Study Memo: Environment Preparation
[Learning memo] Basics of class by python
Python data structure and operation (Python learning memo ③)
Python & Machine Learning Study Memo ③: Neural Network
Python & Machine Learning Study Memo ④: Machine Learning by Backpropagation
Python & Machine Learning Study Memo ⑥: Number Recognition
Python 3.6 email library
LPIC201 learning memo
[Python] Learning Note 1
Python ast library
python beginner memo (9.2-10)
Django Learning Memo
Python & Machine Learning Study Memo ⑤: Classification of irises
python beginner memo (9.1)
python learning output
[Python] Standard input
★ Memo ★ Python Iroha
Python learning day 4
Predicting House Prices (Machine Learning: Second Half) ver1.1
[Python] EDA memo
Python Deep Learning
Python 3 operator memo
Not surprisingly known !? Python standard library functions groupby
Python Library notes
Python learning (supplement)
Try Juniper JUNOS PyEz (python library) Memo 1 ~ PyEz Overview ~
Deep learning × Python
[Memo] Machine learning
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
progate Python learning memo (updated from time to time)
Python & Machine Learning Study Memo ⑦: Stock Price Forecast
Python beginner memo (2)
python learning notes
[Python] Numpy memo
Python learning memo for machine learning by Chainer from Chapter 2
Can be used in competition pros! Python standard library
Memo for building a machine learning environment using Python
[Redash] Standard library cannot be used in python function
My python environment memo