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
point
Template module to implement a template string with a replacement symbol ($).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
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
point
threading module, background processing can be performed while the main program is running.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')
queue module to feed requests from other threads to the main thread.point
logging moduleimport 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
weakref allows you to track an object without generating a referenceimport 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'
module provides a list-like object ʻarray that contains only data in the same room.
from array import array
a = array('H', [4000, 10, 700, 22222])
print(sum(a))
# 26932
print(a[1:3])
# array('H', [10, 700])
collections module brings adeque object to the left end where ʻappend and pop` are faster than the list, but the central reference is slower.
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)
bisect module with functions to manipulate sorted lists.heapq module provides a function that implements the heap based on a regular listdecimal module provides the data type Decimal for computing in floating point decimal numbers.float typeRecommended Posts