{"id":329262,"date":"2022-02-08T09:00:28","date_gmt":"2022-02-08T09:00:28","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=329262"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=329262","title":{"rendered":"<span>\u041a\u0430\u043a \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0442\u044c \u0436\u0438\u0442\u044c \u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u0441\u044f \u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"901\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/157\/f54\/293\/157f54293fa64f92bae11d8e0a270210.png\" data-width=\"1600\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0438\u0448\u0435\u0448\u044c \u043d\u0430 Python, \u0440\u0435\u0434\u043a\u043e \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0448\u044c\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u044d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u043d\u043e, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439 \u043d\u0438\u0437\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a <code>Out of memory<\/code> \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0451\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0451\u043c\u043e\u0432, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043f\u043e\u0441\u043e\u0440\u0435\u0432\u043d\u0443\u0435\u043c\u0441\u044f \u0441 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u043d\u0430 bash).<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043d\u0430\u0434\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0444\u0430\u0439\u043b \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.write(''.join(str_list))<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c, \u043d\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u043c\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0439\u043c\u0451\u0442 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a. \u042d\u0442\u043e\u0433\u043e \u043b\u0435\u0433\u043a\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=writelines#io.IOBase.writelines\" rel=\"noopener noreferrer nofollow\">writelines<\/a>:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.writelines(str_list)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. \u041a\u043e\u0448\u043c\u0430\u0440\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.write('\\n'.join(str_list) + '\\n')<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043d\u043e \u043e\u043d\u0430 \u0435\u0449\u0451 \u0440\u0430\u0437 \u0438 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 <code>\\n<\/code> \u0432 \u043a\u043e\u043d\u0435\u0446 (\u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c \u0442\u0438\u043f\u0430\u043c).<\/p>\n<p>\u041b\u0443\u0447\u0448\u0435 \u0432\u0437\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#print\" rel=\"noopener noreferrer nofollow\">print<\/a> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0439 <code>str_list<\/code> \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     print(*str_list, sep='\\n', file=fh)<\/code><\/pre>\n<p>\u0421\u0438\u043c\u0432\u043e\u043b \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 \u043a\u043e\u043d\u0446\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u2014 \u0437\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>end<\/code>. \u0412\u043e\u043e\u0431\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u044b \u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u044b:<\/p>\n<pre><code class=\"python\">prefix, suffix = 'foo', 'bar\\n' with open('output', 'w') as fh:     print(prefix, end='', file=fh)     print(*str_list, sep=suffix+prefix, end=suffix)<\/code><\/pre>\n<p>\u0415\u0449\u0451 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0442\u043e, \u0447\u0442\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <code>join<\/code>, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">>>> print(*range(10), sep=', ') 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 >>> print('A', 10, b'B', object(), sep='-') A-10-b'B'-&lt;object object at 0x7f9aa43686c0><\/code><\/pre>\n<p>\u0421\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 <code>__str__<\/code>  \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<details class=\"spoiler\">\n<summary>\u0415\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0442\u0440\u044e\u043a \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 print. \u0412\u0440\u044f\u0434 \u043b\u0438 \u043e\u043d \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>sep<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0438\u043b\u0438 <code>None<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043d\u0435\u043e\u0431\u044b\u0447\u043d\u043e\u043c \u0432\u0438\u0434\u0435:<\/p>\n<pre><code class=\"python\">class Sep(str): def __init__(self, iter_): super().__init__() self.iter = iter_          def __str__(self):     return next(self.iter)<\/code><\/pre>\n<p>\u0421 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435\u0439 \u0438 \u0443\u043c\u0435\u0449\u0430\u0442\u044c \u0438\u0445 \u0432 \u043e\u0434\u0438\u043d \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">>>> import itertools as it >>> sep = Sep(it.cycle((', ', ', ', ',\\n'))) >>> print(*range(12), sep=sep) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 >>> d = {'Name': 'John', 'Surname': 'Wick'} >>> sep = Sep(it.cycle((': ', '\\n'))) >>> print(*it.chain.from_iterable(d.items()), sep=sep) Name: John Surname: Wick<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0421 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0441\u0442\u0440\u043e\u043a\u0430\u043c. \u0421 \u043d\u0438\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0451 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=io#io.StringIO\" rel=\"noopener noreferrer nofollow\">StringIO<\/a> \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f <code>io<\/code>. \u041e\u043d\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0442\u0435 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435\/\u0437\u0430\u043f\u0438\u0441\u044c \u0444\u0430\u0439\u043b, \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>print<\/code> \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>file<\/code>. \u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>StringIO<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0430 \u0444\u0430\u0439\u043b\u0430. \u0414\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434 <code>getvalue()<\/code>. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u043a\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u043c\u0443 \u0442\u0438\u043f\u0443, \u0430 <code>StringIO<\/code> \u2014 \u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u043c\u0443. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u00ab\u0444\u0430\u0439\u043b\u0430\u00bb \u0438 \u043f\u0440\u043e\u0431\u0435\u0436\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c:<\/p>\n<pre><code class=\"python\">>>> import io >>> o = io.StringIO() >>> o.writelines(['aina\\n', 'peina\\n', 'para\\n']) >>> print(*range(10), file=o) >>> o.getvalue() 'aina\\npeina\\npara\\n0 1 2 3 4 5 6 7 8 9\\n' >>> o.tell() 36 >>> o.seek(0) 0 >>> for line in o: print(line, end='') ...  aina peina para 0 1 2 3 4 5 6 7 8 9<\/code><\/pre>\n<p>\u0423 <code>StringIO<\/code> \u0435\u0441\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0439\u0442\u0430\u043c\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=io#io.BytesIO\" rel=\"noopener noreferrer nofollow\">BytesIO<\/a>. \u041f\u0440\u0430\u0432\u0434\u0430, \u0434\u043b\u044f \u0431\u0430\u0439\u0442\u043e\u0432 \u0435\u0441\u0442\u044c \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u043e\u043b\u0435\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html?highlight=bytearray#bytearray\" rel=\"noopener noreferrer nofollow\">bytearray<\/a>. \u042d\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u0430 \u043d\u0435 \u0444\u0430\u0439\u043b. \u0415\u0449\u0451 <code>bytearray<\/code> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html?highlight=bytearray#bytearray\" rel=\"noopener noreferrer nofollow\">memoryview<\/a>. \u042d\u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u0433\u0434\u0435 \u0441\u0440\u0430\u0437\u0438\u043c\u0441\u044f \u0441\u043e \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u0432 \u043d\u0435\u0440\u0430\u0432\u043d\u043e\u0439 \u0431\u0438\u0442\u0432\u0435. \u0418\u0442\u0430\u043a, \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u0439\u043b\u043e\u0432, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0430\u0439\u0442 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0441\u043e\u043a\u0435\u0442. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 bash&#8217;e:<\/p>\n<pre><code class=\"bash\">$ cat file.1 file.2 | sed 's\/something\/something_new\/g' | nc -q0 ::1 8000<\/code><\/pre>\n<p>\u041d\u0430 8000 \u043f\u043e\u0440\u0442\u0443 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0443\u0449\u0435\u043d <code>netcat<\/code>, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u0441\u0451 \u0432 <code>\/dev\/null<\/code>:<\/p>\n<pre><code class=\"bash\">$ nc -lp 8000 -6 -k > \/dev\/null<\/code><\/pre>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0435\u043a, \u043d\u0435 \u0434\u0443\u043c\u0430\u044f \u0447\u0442\u043e \u0438 \u0437\u0430\u0447\u0435\u043c \u0442\u0430\u043c \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f. <\/p>\n<details class=\"spoiler\">\n<summary>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u0445 \u0435\u0433\u043e \u0438\u043c\u044f catsednc2.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import sys import socket   OLD = b'alert' NEW = b'ok'   def main():     content_list = []     for filename in sys.argv[1:]:         with open(filename, 'rb') as fhandler:             content_list.append(fhandler.read())     content = b''.join(content_list)     content = content.replace(OLD, NEW)     sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)     sock.connect(('::1', 8000))     sock.sendall(content)     sock.close()   if __name__ == '__main__':     main()<\/code><\/pre>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430\u0437\u043e\u0432\u0451\u043c catsednc.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import os import io import sys import socket import itertools as it   OLD = b'alert' NEW = b'ok'   def read_data(filenames):     sizes = [os.path.getsize(name) for name in filenames]     # \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u0443\u0434\u0430 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u043e\u0432     data = bytearray(sum(sizes))     # memoryview \u0434\u0430\u0451\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043a \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438     # \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044e     mview = memoryview(data)     # \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f\u043c\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0443\u0444\u0444\u0435\u0440\u0430     offsets = [0]     offsets.extend(it.accumulate(sizes))     for name, offset in zip(filenames, offsets):         with open(name, 'rb') as fhandler:             # readinto \u0447\u0438\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e             # \u0432 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0444\u0435\u0440 \u0431\u0435\u0437 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430             fhandler.readinto(mview[offset:])     return data   def sub(data, output, old, new):     prev_offset = 0     old_len = len(old)     # \u0437\u0434\u0435\u0441\u044c memorview \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u0435\u0437\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445     # \u0431\u0435\u0437 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f     mview = memoryview(data)     while True:         try:             # \u0438\u0449\u0435\u043c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f              # \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f             offset = data.index(old, prev_offset)         except ValueError:             # \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0445\u0432\u043e\u0441\u0442 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 old             output.write(mview[prev_offset:])             break         # \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0431\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c\u0438 old         output.write(mview[prev_offset:offset])         # \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 old \u043f\u0438\u0448\u0435\u043c new         output.write(new)         prev_offset = offset + old_len   def main():     filenames = sys.argv[1:]     data = read_data(filenames)     sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)     sock.connect(('::1', 8000))     # \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0441\u043e\u043a\u0435\u0442, \u043a\u0430\u043a \u0432 \u0444\u0430\u0439\u043b     output = sock.makefile(mode='wb')     sub(data, output, OLD, NEW)   if __name__ == '__main__':     main() <\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0412 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0434\u0432\u0430\u0436\u0434\u044b: \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 <code>join<\/code> \u0438 <code>replace<\/code>. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u2014 \u043d\u0438 \u0440\u0430\u0437\u0443. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u0441\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430 \u0434\u0432\u0443\u0445 \u0444\u0430\u0439\u043b\u0430\u0445 \u043f\u043e \u043f\u043e\u043b\u0442\u043e\u0440\u0430 \u0433\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u0430:<\/p>\n<pre><code class=\"bash\">$ ls -l file* -rw-rw-r-- 1 znahar znahar 1521240266 \u0444\u0435\u0432  5 16:04 file1 -rw-rw-r-- 1 znahar znahar 1521238713 \u0444\u0435\u0432  5 16:53 file2 $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" .\/catsednc.py file1 file2 0:02.89s time,2980796K memory,94% CPU $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" .\/catsednc2.py file1 file2 0:06.85s time,8923064K memory,92% CPU $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" cat file1 file2 | sed 's\/alert\/ok\/g' | nc -q0 ::1 8000 0:07.34s time,2256K memory,25% CPU <\/code><\/pre>\n<p>\u041a\u0430\u043a\u0438\u0435 \u043e\u0442\u0441\u044e\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0432\u044b\u0432\u043e\u0434\u044b:<\/p>\n<ul>\n<li>\n<p>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u0435\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0440\u0430\u0432\u043d\u043e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u0434\u0432\u0430\u0436\u0434\u044b \u043a\u043e\u043f\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0435\u0442 \u0432 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a \u0447\u0438\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u044b, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u042d\u0442\u043e \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0444\u0430\u0439\u043b\u043e\u0432, \u043d\u043e \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e \u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 C.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/650273\/\"> https:\/\/habr.com\/ru\/post\/650273\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043f\u0438\u0448\u0435\u0448\u044c \u043d\u0430 Python, \u0440\u0435\u0434\u043a\u043e \u0437\u0430\u0434\u0443\u043c\u044b\u0432\u0430\u0435\u0448\u044c\u0441\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u044d\u0442\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0443, \u043d\u043e, \u0441 \u0434\u0440\u0443\u0433\u043e\u0439, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u0447\u0438\u043d\u043e\u0439 \u043d\u0438\u0437\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043e\u0448\u0438\u0431\u043e\u043a <code>Out of memory<\/code> \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0451\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0451\u043c\u043e\u0432, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 (\u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u043f\u043e\u0441\u043e\u0440\u0435\u0432\u043d\u0443\u0435\u043c\u0441\u044f \u0441 \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u043d\u0430 bash).<\/p>\n<p>\u041d\u0430\u0447\u043d\u0451\u043c \u0441 \u0442\u0438\u043f\u043e\u0432\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043d\u0430\u0434\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0444\u0430\u0439\u043b \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0440\u043e\u043a \u0431\u0435\u0437 \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.write(''.join(str_list))<\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0439 \u043a\u043e\u0434 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c, \u043d\u043e \u0435\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u043f\u0435\u0440\u0435\u0434 \u0437\u0430\u043f\u0438\u0441\u044c\u044e \u043c\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0439\u043c\u0451\u0442 \u0441\u0442\u043e\u043b\u044c\u043a\u043e \u0436\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a. \u042d\u0442\u043e\u0433\u043e \u043b\u0435\u0433\u043a\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=writelines#io.IOBase.writelines\" rel=\"noopener noreferrer nofollow\">writelines<\/a>:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.writelines(str_list)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438. \u041a\u043e\u0448\u043c\u0430\u0440\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     fh.write('\\n'.join(str_list) + '\\n')<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u043d\u043e \u043e\u043d\u0430 \u0435\u0449\u0451 \u0440\u0430\u0437 \u0438 \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 <code>\\n<\/code> \u0432 \u043a\u043e\u043d\u0435\u0446 (\u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c \u0442\u0438\u043f\u0430\u043c).<\/p>\n<p>\u041b\u0443\u0447\u0448\u0435 \u0432\u0437\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#print\" rel=\"noopener noreferrer nofollow\">print<\/a> \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0439 <code>str_list<\/code> \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432:<\/p>\n<pre><code class=\"python\">with open('output', 'w') as fh:     print(*str_list, sep='\\n', file=fh)<\/code><\/pre>\n<p>\u0421\u0438\u043c\u0432\u043e\u043b \u043d\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d \u0432 \u043a\u043e\u043d\u0446\u0435 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u2014 \u0437\u0430 \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>end<\/code>. \u0412\u043e\u043e\u0431\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u044b \u0438 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u044b:<\/p>\n<pre><code class=\"python\">prefix, suffix = 'foo', 'bar\\n' with open('output', 'w') as fh:     print(prefix, end='', file=fh)     print(*str_list, sep=suffix+prefix, end=suffix)<\/code><\/pre>\n<p>\u0415\u0449\u0451 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0442\u043e, \u0447\u0442\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 <code>join<\/code>, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u044b\u0445 \u0442\u0438\u043f\u043e\u0432, \u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">>>> print(*range(10), sep=', ') 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 >>> print('A', 10, b'B', object(), sep='-') A-10-b'B'-&lt;object object at 0x7f9aa43686c0><\/code><\/pre>\n<p>\u0421\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u043c\u0435\u0442\u043e\u0434\u0430 <code>__str__<\/code>  \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.<\/p>\n<details class=\"spoiler\">\n<summary>\u0415\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0442\u0440\u044e\u043a \u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 print. \u0412\u0440\u044f\u0434 \u043b\u0438 \u043e\u043d \u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u043e\u0432\u043e\u0433\u043e \u043a\u043e\u0434\u0430.<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>sep<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0438\u043b\u0438 <code>None<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u043d\u0435\u043e\u0431\u044b\u0447\u043d\u043e\u043c \u0432\u0438\u0434\u0435:<\/p>\n<pre><code class=\"python\">class Sep(str): def __init__(self, iter_): super().__init__() self.iter = iter_          def __str__(self):     return next(self.iter)<\/code><\/pre>\n<p>\u0421 \u0435\u0433\u043e \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435\u0439 \u0438 \u0443\u043c\u0435\u0449\u0430\u0442\u044c \u0438\u0445 \u0432 \u043e\u0434\u0438\u043d \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">>>> import itertools as it >>> sep = Sep(it.cycle((', ', ', ', ',\\n'))) >>> print(*range(12), sep=sep) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 >>> d = {'Name': 'John', 'Surname': 'Wick'} >>> sep = Sep(it.cycle((': ', '\\n'))) >>> print(*it.chain.from_iterable(d.items()), sep=sep) Name: John Surname: Wick<\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0421 \u0444\u0430\u0439\u043b\u0430\u043c\u0438 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u043f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u0441\u0442\u0440\u043e\u043a\u0430\u043c. \u0421 \u043d\u0438\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0451 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=io#io.StringIO\" rel=\"noopener noreferrer nofollow\">StringIO<\/a> \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f <code>io<\/code>. \u041e\u043d\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0442\u0435 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0447\u0442\u043e \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435\/\u0437\u0430\u043f\u0438\u0441\u044c \u0444\u0430\u0439\u043b, \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>print<\/code> \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 <code>file<\/code>. \u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>StringIO<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0434\u0435\u0441\u043a\u0440\u0438\u043f\u0442\u043e\u0440\u0430 \u0444\u0430\u0439\u043b\u0430. \u0414\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u043c\u0435\u0442\u043e\u0434 <code>getvalue()<\/code>. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0441\u0442\u0440\u043e\u043a\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u043c\u0443 \u0442\u0438\u043f\u0443, \u0430 <code>StringIO<\/code> \u2014 \u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u043c\u0443. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u00ab\u0444\u0430\u0439\u043b\u0430\u00bb \u0438 \u043f\u0440\u043e\u0431\u0435\u0436\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c:<\/p>\n<pre><code class=\"python\">>>> import io >>> o = io.StringIO() >>> o.writelines(['aina\\n', 'peina\\n', 'para\\n']) >>> print(*range(10), file=o) >>> o.getvalue() 'aina\\npeina\\npara\\n0 1 2 3 4 5 6 7 8 9\\n' >>> o.tell() 36 >>> o.seek(0) 0 >>> for line in o: print(line, end='') ...  aina peina para 0 1 2 3 4 5 6 7 8 9<\/code><\/pre>\n<p>\u0423 <code>StringIO<\/code> \u0435\u0441\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0439\u0442\u0430\u043c\u0438 <a href=\"https:\/\/docs.python.org\/3\/library\/io.html?highlight=io#io.BytesIO\" rel=\"noopener noreferrer nofollow\">BytesIO<\/a>. \u041f\u0440\u0430\u0432\u0434\u0430, \u0434\u043b\u044f \u0431\u0430\u0439\u0442\u043e\u0432 \u0435\u0441\u0442\u044c \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0431\u043e\u043b\u0435\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html?highlight=bytearray#bytearray\" rel=\"noopener noreferrer nofollow\">bytearray<\/a>. \u042d\u0442\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430\u044f \u0431\u0430\u0439\u0442\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430, \u0430 \u043d\u0435 \u0444\u0430\u0439\u043b. \u0415\u0449\u0451 <code>bytearray<\/code> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0447\u0435\u0440\u0435\u0437 <a href=\"https:\/\/docs.python.org\/3\/library\/stdtypes.html?highlight=bytearray#bytearray\" rel=\"noopener noreferrer nofollow\">memoryview<\/a>. \u042d\u0442\u043e \u043c\u044b \u043f\u0440\u043e\u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0438\u0440\u0443\u0435\u043c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u0433\u0434\u0435 \u0441\u0440\u0430\u0437\u0438\u043c\u0441\u044f \u0441\u043e \u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a\u043e\u043c \u0432 \u043d\u0435\u0440\u0430\u0432\u043d\u043e\u0439 \u0431\u0438\u0442\u0432\u0435. \u0418\u0442\u0430\u043a, \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0430\u0439\u043b\u043e\u0432, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0430\u0439\u0442 \u043d\u0430 \u0434\u0440\u0443\u0433\u0443\u044e \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0441\u043e\u043a\u0435\u0442. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 bash&#8217;e:<\/p>\n<pre><code class=\"bash\">$ cat file.1 file.2 | sed 's\/something\/something_new\/g' | nc -q0 ::1 8000<\/code><\/pre>\n<p>\u041d\u0430 8000 \u043f\u043e\u0440\u0442\u0443 \u0432 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0443\u0449\u0435\u043d <code>netcat<\/code>, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u0432\u0441\u0451 \u0432 <code>\/dev\/null<\/code>:<\/p>\n<pre><code class=\"bash\">$ nc -lp 8000 -6 -k > \/dev\/null<\/code><\/pre>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u0435\u0437 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u0435\u043a, \u043d\u0435 \u0434\u0443\u043c\u0430\u044f \u0447\u0442\u043e \u0438 \u0437\u0430\u0447\u0435\u043c \u0442\u0430\u043c \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u0442\u0441\u044f. <\/p>\n<details class=\"spoiler\">\n<summary>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u0445 \u0435\u0433\u043e \u0438\u043c\u044f catsednc2.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import sys import socket   OLD = b'alert' NEW = b'ok'   def main():     content_list = []     for filename in sys.argv[1:]:         with open(filename, 'rb') as fhandler:             content_list.append(fhandler.read())     content = b''.join(content_list)     content = content.replace(OLD, NEW)     sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)     sock.connect(('::1', 8000))     sock.sendall(content)     sock.close()   if __name__ == '__main__':     main()<\/code><\/pre>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u043d\u0430\u0437\u043e\u0432\u0451\u043c catsednc.py<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"python\">import os import io import sys import socket import itertools as it   OLD = b'alert' NEW = b'ok'   def read_data(filenames):     sizes = [os.path.getsize(name) for name in filenames]     # \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0431\u043b\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u0443\u0434\u0430 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0444\u0430\u0439\u043b\u043e\u0432     data = bytearray(sum(sizes))     # memoryview \u0434\u0430\u0451\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043a \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438     # \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044e     mview = memoryview(data)     # \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f\u043c\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u0431\u0443\u0444\u0444\u0435\u0440\u0430     offsets = [0]     offsets.extend(it.accumulate(sizes))     for name, offset in zip(filenames, offsets):         with open(name, 'rb') as fhandler:             # readinto \u0447\u0438\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e             # \u0432 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0431\u0443\u0444\u0444\u0435\u0440 \u0431\u0435\u0437 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430             fhandler.readinto(mview[offset:])     return data   def sub(data, output, old, new):     prev_offset = 0     old_len = len(old)     # \u0437\u0434\u0435\u0441\u044c memorview \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u0435\u0437\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445     # \u0431\u0435\u0437 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f     mview = memoryview(data)     while True:         try:             # \u0438\u0449\u0435\u043c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f              # \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f             offset = data.index(old, prev_offset)         except ValueError:             # \u0434\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0445\u0432\u043e\u0441\u0442 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 old             output.write(mview[prev_offset:])             break         # \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0431\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c\u0438 old         output.write(mview[prev_offset:offset])         # \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u0430\u043c\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 old \u043f\u0438\u0448\u0435\u043c new         output.write(new)         prev_offset = offset + old_len   def main():     filenames = sys.argv[1:]     data = read_data(filenames)     sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)     sock.connect(('::1', 8000))     # \u0431\u0443\u0434\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u0432 \u0441\u043e\u043a\u0435\u0442, \u043a\u0430\u043a \u0432 \u0444\u0430\u0439\u043b     output = sock.makefile(mode='wb')     sub(data, output, OLD, NEW)   if __name__ == '__main__':     main() <\/code><\/pre>\n<\/div>\n<\/details>\n<p>\u0412 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0434\u0432\u0430\u0436\u0434\u044b: \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 <code>join<\/code> \u0438 <code>replace<\/code>. \u0412\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u2014 \u043d\u0438 \u0440\u0430\u0437\u0443. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u043c, \u043a\u0430\u043a \u044d\u0442\u043e \u043e\u0442\u0440\u0430\u0437\u0438\u0442\u0441\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043d\u0430 \u0434\u0432\u0443\u0445 \u0444\u0430\u0439\u043b\u0430\u0445 \u043f\u043e \u043f\u043e\u043b\u0442\u043e\u0440\u0430 \u0433\u0438\u0433\u0430\u0431\u0430\u0439\u0442\u0430:<\/p>\n<pre><code class=\"bash\">$ ls -l file* -rw-rw-r-- 1 znahar znahar 1521240266 \u0444\u0435\u0432  5 16:04 file1 -rw-rw-r-- 1 znahar znahar 1521238713 \u0444\u0435\u0432  5 16:53 file2 $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" .\/catsednc.py file1 file2 0:02.89s time,2980796K memory,94% CPU $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" .\/catsednc2.py file1 file2 0:06.85s time,8923064K memory,92% CPU $ \/usr\/bin\/time -f \"\\t%Es time,\\t%MK memory,\\t%P CPU\" cat file1 file2 | sed 's\/alert\/ok\/g' | nc -q0 ::1 8000 0:07.34s time,2256K memory,25% CPU <\/code><\/pre>\n<p>\u041a\u0430\u043a\u0438\u0435 \u043e\u0442\u0441\u044e\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0442 \u0432\u044b\u0432\u043e\u0434\u044b:<\/p>\n<ul>\n<li>\n<p>\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u043a\u0440\u0438\u043f\u0442 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u0435\u0433\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e \u0440\u0430\u0432\u043d\u043e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0444\u0430\u0439\u043b\u043e\u0432;<\/p>\n<\/li>\n<li>\n<p>\u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043a\u0440\u0438\u043f\u0442, \u0434\u0432\u0430\u0436\u0434\u044b \u043a\u043e\u043f\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u044f\u0435\u0442 \u0432 \u0442\u0440\u0438 \u0440\u0430\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043e\u0434\u043d\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0438\u043a \u0447\u0438\u0442\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043e\u043d\u0432\u0435\u0439\u0435\u0440\u044b, \u0447\u0435\u0440\u0435\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u042d\u0442\u043e \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0444\u0430\u0439\u043b\u043e\u0432, \u043d\u043e \u043d\u0435\u0433\u0430\u0442\u0438\u0432\u043d\u043e \u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0443\u0442\u0438\u043b\u0438\u0442\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 C.<\/p>\n<\/li>\n<\/ul>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0451. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/650273\/\"> https:\/\/habr.com\/ru\/post\/650273\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-329262","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329262"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329262\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}