{"id":299357,"date":"2020-02-27T15:00:17","date_gmt":"2020-02-27T15:00:17","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=299357"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=299357","title":{"rendered":"\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 Python \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e ctypes"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/490244\/\"><i><b>\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/KTix\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a Python\u00bb<\/a>.<\/b><\/i><\/p>\n<hr>\n<p>  <i><b>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/b>: \u043a\u043e\u0434 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u043f\u043e\u0434 <a href=\"https:\/\/samuelstevens.me\/license-gnu\">GNU AGPLv3<\/a>.<\/i><\/p>\n<p>  \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u044d\u0442\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0435 \u0441\u043c\u043e\u0433 \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043a\u043e\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u0432\u0441\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u043e ctypes. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0447\u044c\u044e-\u0442\u043e \u0436\u0438\u0437\u043d\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435.<\/p>\n<p>  <b>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435:<\/b><\/p>\n<ol>\n<li>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438<\/li>\n<li>\u0441types<\/li>\n<li>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043f\u043e\u0434 Python <\/li>\n<li>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 Python<\/li>\n<li>\u0412\u044b\u0437\u043e\u0432 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421<\/li>\n<li>PyPy<\/li>\n<\/ol>\n<p><a name=\"habracut\"><\/a><\/p>\n<h3>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>  \u041f\u0435\u0440\u0435\u0434 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u043d\u0438\u0435\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 Python \u043d\u0430 \u0421, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 Python.<\/p>\n<h3>\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p>  \u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 Python, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a set \u0438 dict, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 \u0421. \u041e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0432\u0430\u0448\u0438 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u043a \u043a\u043b\u0430\u0441\u0441\u044b Python. \u0414\u0440\u0443\u0433\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u043c\u0438\u043c\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 set, dict, list \u0438 tuple \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u043c\u043e\u0434\u0443\u043b\u044f <i><a href=\"https:\/\/docs.python.org\/dev\/library\/collections.html#module-collections\">collections<\/a><\/i>.<\/p>\n<h3>\u0421\u043f\u0438\u0441\u043e\u0447\u043d\u044b\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f<\/h3>\n<p>  \u0412\u043c\u0435\u0441\u0442\u043e \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a \u0441\u043f\u0438\u0441\u043a\u0443 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c \u043c\u0435\u0442\u043e\u0434\u043e\u043c, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0442\u0435\u0441\u044c \u0441\u043f\u0438\u0441\u043e\u0447\u043d\u044b\u043c\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438. <\/p>\n<pre><code class=\"python\"># Slow       mapped = []       for value in originallist:           mapped.append(myfunc(value))              # Faster       mapped = [myfunc(value) in originallist]<\/code><\/pre>\n<h3>ctypes<\/h3>\n<p>  \u041c\u043e\u0434\u0443\u043b\u044c <i><a href=\"https:\/\/docs.python.org\/3\/library\/ctypes.html\">ctypes<\/a><\/i> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0430\u043c \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441 \u043a\u043e\u0434\u043e\u043c \u043d\u0430 \u0421 \u0438\u0437 Python \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0434\u0443\u043b\u044f <code>subprocess<\/code> \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432 \u0438\u0437 CLI. <\/p>\n<p>  \u0422\u0443\u0442 \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0435 \u0447\u0430\u0441\u0442\u0438: \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421 \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 <code>shared object<\/code> \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u0434\u0435 \u043d\u0430 Python \u0434\u043b\u044f \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0441 \u0442\u0438\u043f\u0430\u043c\u0438 \u0421.<\/p>\n<p>  \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0431\u0443\u0434\u0443 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043e\u0434 \u043d\u0430 Python \u0441 <i><a href=\"https:\/\/samuelstevens.me\/lcs-source\">lcs.c<\/a><\/i>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u0443\u044e \u0434\u043b\u0438\u043d\u043d\u0443\u044e \u043f\u043e\u0434\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432 \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u0430\u0445 \u0441\u0442\u0440\u043e\u043a. \u042f \u0445\u043e\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u0432 Python \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\">list1 = ['My', 'name', 'is', 'Sam', 'Stevens', '!']       list2 = ['My', 'name', 'is', 'Alex', 'Stevens', '.']              common = lcs(list1, list2)              print(common)       # ['My', 'name', 'is', 'Stevens']<\/code><\/pre>\n<p>   \u041e\u0434\u043d\u0430 \u0438\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u044d\u0442\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430 \u0421 \u2014 \u044d\u0442\u043e \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043a\u0438 \u0441\u0442\u0440\u043e\u043a \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0442\u0438\u043f\u043e\u0432 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0438\u043f, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b. \u042f \u0440\u0435\u0448\u0430\u044e \u044d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u0438 \u0434\u043b\u0438\u043d\u0443.<\/p>\n<h3>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421 \u043f\u043e\u0434 Python<\/h3>\n<p>   \u0421\u043d\u0430\u0447\u0430\u043b\u0430, \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u0421 (<i>lcs.c<\/i>) \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 <code>lcs.so<\/code>, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0432 Python. <\/p>\n<pre><code class=\"python\">gcc -c -Wall -Werror -fpic -O3 lcs.c -o lcs.o       gcc -shared -o lcs.so lcs.o<\/code><\/pre>\n<ul>\n<li>&#8212;<i>Wall <\/i>\u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f;<\/li>\n<li>&#8212;<i>Werrow <\/i>\u043e\u0431\u0435\u0440\u043d\u0435\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u0443\u043f\u0440\u0435\u0436\u0434\u0435\u043d\u0438\u044f \u0432 \u043e\u0448\u0438\u0431\u043a\u0438;<\/li>\n<li>&#8212;<i>fpic <\/i>\u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435 \u043e\u0442 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0432 Python;<\/li>\n<li>&#8212;<i><i>O3 <\/i><\/i>\u043c\u0430\u043a\u0441\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e;<\/li>\n<\/ul>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 Python, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <i>shared object<\/i>.<\/p>\n<h3>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 Python<\/h3>\n<p>  \u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0434\u0432\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0432 \u043c\u043e\u0435\u043c \u043a\u043e\u0434\u0435 \u043d\u0430 \u0421.<\/p>\n<pre><code class=\"python\">struct Sequence       {           char **items;           int length;       };              struct Cell       {           int index;           int length;           struct Cell *prev;       };<\/code><\/pre>\n<p>  \u0410 \u0432\u043e\u0442 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u043d\u0430 Python.<\/p>\n<pre><code class=\"python\">import ctypes       class SEQUENCE(ctypes.Structure):           _fields_ = [('items', ctypes.POINTER(ctypes.c_char_p)),                       ('length', ctypes.c_int)]              class CELL(ctypes.Structure):           pass              CELL._fields_ = [('index', ctypes.c_int), ('length', ctypes.c_int),                        ('prev', ctypes.POINTER(CELL))]<\/code><\/pre>\n<p>  \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043c\u0435\u0442\u043e\u043a:<\/p>\n<ul>\n<li>\u0412\u0441\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u2013 \u044d\u0442\u043e \u043a\u043b\u0430\u0441\u0441\u044b, \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u044b\u0435 \u043e\u0442 <code>ctypes.Structure<\/code>.<\/li>\n<li>\u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 <code>_fields_<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u0440\u0442\u0435\u0436\u0435\u0439. \u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u0440\u0442\u0435\u0436 \u044d\u0442\u043e (<code>&lt;variable-name&gt;<\/code>, <code>&lt;ctypes.TYPE&gt;<\/code>).<\/li>\n<li>\u0412 <code>ctypes<\/code> \u0435\u0441\u0442\u044c \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u0442\u0438\u043f\u044b <i>c_char (char) \u0438 c_char_p (*char)<\/i>.<\/li>\n<li>\u0412 <code>ctypes<\/code> \u0442\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c <code>POINTER()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0442\u0438\u043f\u0430 \u0438\u0437 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0435\u043c\u0443 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e.<\/li>\n<li>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u0432 <code>CELL<\/code>, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435, \u0430 \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044f <code>_fields_<\/code>, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0437\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0441\u0435\u0431\u044f \u0436\u0435.<\/li>\n<li>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044f \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b <code>CELL<\/code> \u0432 \u0441\u0432\u043e\u0435\u043c \u043a\u043e\u0434\u0435 \u043d\u0430 Python, \u043c\u043d\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u043d\u043e \u0443 \u043d\u0435\u0435 \u0435\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u043e\u043b\u0435. <\/li>\n<\/ul>\n<h3>\u0412\u044b\u0437\u043e\u0432 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421<\/h3>\n<p>  \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u0431\u044b\u043b \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 Python \u0432 \u043d\u043e\u0432\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043d\u0430 \u0421. \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u043d\u043e\u0432\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430 \u0421, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u0434\u0430 \u043d\u0430 Python. <\/p>\n<pre><code class=\"python\">def list_to_SEQUENCE(strlist: List[str]) -&gt; SEQUENCE:           bytelist = [bytes(s, 'utf-8') for s in strlist]           arr = (ctypes.c_char_p * len(bytelist))()           arr[:] = bytelist           return SEQUENCE(arr, len(bytelist))                     def lcs(s1: List[str], s2: List[str]) -&gt; List[str]:           seq1 = list_to_SEQUENCE(s1)           seq2 = list_to_SEQUENCE(s2)                  # struct Sequence *lcs(struct Sequence *s1, struct Sequence *s2)           common = lcsmodule.lcs(ctypes.byref(seq1), ctypes.byref(seq2))[0]                  ret = []                  for i in range(common.length):               ret.append(common.items[i].decode('utf-8'))           lcsmodule.freeSequence(common)                  return ret              lcsmodule = ctypes.cdll.LoadLibrary('lcsmodule\/lcs.so')       lcsmodule.lcs.restype = ctypes.POINTER(SEQUENCE)              list1 = ['My', 'name', 'is', 'Sam', 'Stevens', '!']       list2 = ['My', 'name', 'is', 'Alex', 'Stevens', '.']              common = lcs(list1, list2)              print(common)       # ['My', 'name', 'is', 'Stevens']<\/code><\/pre>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043c\u0435\u0442\u043e\u043a:<\/p>\n<ul>\n<li><code>**char<\/code> (\u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0440\u043e\u043a) \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0431\u0430\u0439\u0442 \u0432 Python.<\/li>\n<li>\u0412 <code>lcs.c<\/code> \u0435\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>lcs()<\/code> \u0441 \u0441\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u043e\u0439 struct Sequence <i>*lcs(struct Sequence *s1, struct Sequence *s2)<\/i>. \u0427\u0442\u043e\u0431\u044b \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>lcsmodule.lcs.restype = ctypes.POINTER(SEQUENCE)<\/code>.<\/li>\n<li>\u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u0441 \u0441\u0441\u044b\u043b\u043a\u043e\u0439 \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <i>Sequence<\/i>, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e<code> ctypes.byref()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u00ab\u043b\u0435\u0433\u043a\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u00bb \u043d\u0430 \u0432\u0430\u0448 \u043e\u0431\u044a\u0435\u043a\u0442 (\u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c <code>ctypes.POINTER()<\/code>).<\/li>\n<li><code>common.items<\/code> \u2013 \u044d\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a \u0431\u0430\u0439\u0442, \u043e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f <code>ret<\/code> \u0432 \u0432\u0438\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 <code>str<\/code>.<\/li>\n<li><i>lcsmodule.freeSequence(common)<\/i> \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c, \u0430\u0441\u0441\u043e\u0446\u0438\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0441 common. \u042d\u0442\u043e \u0432\u0430\u0436\u043d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u0431\u043e\u0440\u0447\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430 (AFAIK) \u0435\u0433\u043e \u043d\u0435 \u0441\u043e\u0431\u0435\u0440\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438.<\/li>\n<\/ul>\n<p>  \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 Python \u2013 \u044d\u0442\u043e \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u043d\u0430 \u0421 \u0438 \u043e\u0431\u0435\u0440\u043d\u0443\u043b\u0438 \u0432 Python.<\/p>\n<h3>\u041a\u043e\u0435-\u0447\u0442\u043e \u0435\u0449\u0435: PyPy<\/h3>\n<p>  <i>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0421\u0430\u043c \u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 PyPy \u043d\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f.<\/i><br \/>  \u041e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0432 \u0441\u0440\u0435\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f <a href=\"https:\/\/www.pypy.org\/\">PyPy<\/a>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 JIT-\u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 (just-in-time), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0446\u0438\u043a\u043b\u043e\u0432, \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u044f \u0438\u0445 \u0432 \u043c\u0430\u0448\u0438\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438.<\/p>\n<p>  \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043f\u043e\u044f\u0432\u044f\u0442\u0441\u044f \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0438\u043b\u0438 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0447\u0442\u043e-\u0442\u043e \u043e\u0431\u0441\u0443\u0434\u0438\u0442\u044c, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043c\u043d\u0435 (samuel.robert.stevens@gmail.com). <\/p>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435. \u0414\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0438 <a href=\"https:\/\/otus.pw\/KTix\/\">\u043d\u0430 \u043a\u0443\u0440\u0441\u0435<\/a>!<\/div>\n<p> \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\/company\/otus\/blog\/490244\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/490244\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/otus\/blog\/490244\/\"><i><b>\u041f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u043a\u0443\u0440\u0441\u0430 <a href=\"https:\/\/otus.pw\/KTix\/\">\u00ab\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a Python\u00bb<\/a>.<\/b><\/i><\/p>\n<hr>\n<p>  <i><b>\u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435<\/b>: \u043a\u043e\u0434 \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043b\u0438\u0446\u0435\u043d\u0437\u0438\u0440\u043e\u0432\u0430\u043d \u043f\u043e\u0434 <a href=\"https:\/\/samuelstevens.me\/license-gnu\">GNU AGPLv3<\/a>.<\/i><\/p>\n<p>  \u042f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u044d\u0442\u043e \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0435 \u0441\u043c\u043e\u0433 \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043a\u043e\u0433\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0432 \u0441\u0435\u0431\u0435 \u0432\u0441\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435 \u043e ctypes. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0441\u0434\u0435\u043b\u0430\u0435\u0442 \u0447\u044c\u044e-\u0442\u043e \u0436\u0438\u0437\u043d\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043b\u0435\u0433\u0447\u0435.<\/p>\n<p>  <b>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435:<\/b><\/p>\n<ol>\n<li>\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438<\/li>\n<li>\u0441types<\/li>\n<li>\u041a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u044f \u043f\u043e\u0434 Python <\/li>\n<li>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 Python<\/li>\n<li>\u0412\u044b\u0437\u043e\u0432 \u0432\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u043d\u0430 \u0421<\/li>\n<li>PyPy<\/li>\n<\/ol>\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-299357","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299357","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=299357"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299357\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}