|
1261 | 1261 | These methods are \pythonilIdx{\_\_enter\_\_} and \pythonilIdx{\_\_exit\_\_}.
|
1262 | 1262 | Since \pythonilIdx{with}~blocks are nice syntactical sugar of the \python\ language, we will here play around with them a little bit.
|
1263 | 1263 |
|
1264 |
| -As example, let us create a simple API that allows us to write output in a subset of the \acrfull{XML} format~\cite{BPSMM2008EMLX1FE,K2019ITXJY,CH2013XFCAMLTMC}. |
| 1264 | +As example, let us create a simple API that allows us to write output in a subset of the \glsreset{xml}\pgls{xml} format~\cite{BPSMM2008EMLX1FE,K2019ITXJY,CH2013XFCAMLTMC}. |
1265 | 1265 | \pgls{xml} is a format for data interchange which was predominant in distributed systems the~2000s.
|
1266 | 1266 | After that, it began to fade out in favor~\cite{A2020XLATDOEOL} of \pgls{json}~\cite{E2017SE4TJDIS,RFC8259} and \pgls{yaml}~\cite{DNMAASBE2021YAMLYV1,K2019ITXJY,CGTYB2022YFFDCAIE}.
|
1267 | 1267 | It is still very relevant today, for example, as foundation of several document formats such as those used in LibreOffice~\cite{DF2024LTDF,GL2012LTSOOSSCBAFACSOL} and Microsoft~Word~\cite{MS2024MW,DR2019STFAWAUMW}, or as the basis for the SVG graphics format~\cite{DDGLMSWFJJ2011SVGSSE}.
|
|
1302 | 1302 | Therefore, they are escaped as entities~\xmlil{&} and~\xmlil{>}.
|
1303 | 1303 |
|
1304 | 1304 | \gitPython{\programmingWithPythonCodeRepo}{dunder/xml_context.py}{--args format --labels part_1}{dunder:xml_context:part_1}{%
|
1305 |
| -Part~1 of our very simply context manager-based \pgls{XML} output API.}% |
| 1305 | +Part~1 of our very simply context manager-based \pgls{xml} output API.}% |
1306 | 1306 | %
|
1307 | 1307 | \gitPython{\programmingWithPythonCodeRepo}{dunder/xml_context.py}{--args format --labels part_2}{dunder:xml_context:part_2}{%
|
1308 |
| -Part~2 of our very simply context manager-based \pgls{XML} output API.}% |
| 1308 | +Part~2 of our very simply context manager-based \pgls{xml} output API.}% |
1309 | 1309 |
|
1310 | 1310 | Would it not be nice to have a simple \pgls{API} that allows us to produce valid~\pgls{xml} and that takes care of the escaping of special characters?
|
1311 | 1311 | While countless such tools already exist {\dots} let us make our own.
|
|
1415 | 1415 | %
|
1416 | 1416 | \gitPythonAndOutputFormat{xml_style}%
|
1417 | 1417 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{dunder}{xml_user_print.py}{--args format}{dunder:xml_user_print}{%
|
1418 |
| -An example of using our simple context manager-based \pgls{XML} output API from \cref{lst:dunder:xml_context:part_1,lst:dunder:xml_context:part_2}, where the output is printed to the \pgls{stdout}.}% |
| 1418 | +An example of using our simple context manager-based \pgls{xml} output API from \cref{lst:dunder:xml_context:part_1,lst:dunder:xml_context:part_2}, where the output is printed to the \pgls{stdout}.}% |
1419 | 1419 | %
|
1420 | 1420 | \gitPythonAndOutputFormat{xml_style}%
|
1421 | 1421 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{dunder}{xml_user_file.py}{--args format}{dunder:xml_user_file}{%
|
1422 |
| -An example of using our simple context manager-based \pgls{XML} output API from \cref{lst:dunder:xml_context:part_1,lst:dunder:xml_context:part_2}, where the output is written to a text file\pythonIdx{write}\pythonIdx{IO!write}\pythonIdx{open}\pythonIdx{remove}\pythonIdx{os!remove}.}% |
| 1422 | +An example of using our simple context manager-based \pgls{xml} output API from \cref{lst:dunder:xml_context:part_1,lst:dunder:xml_context:part_2}, where the output is written to a text file\pythonIdx{write}\pythonIdx{IO!write}\pythonIdx{open}\pythonIdx{remove}\pythonIdx{os!remove}.}% |
1423 | 1423 |
|
1424 | 1424 | We now use this \pgls{API} in \cref{lst:dunder:xml_user_print,lst:dunder:xml_user_file} to basically reproduce the small \pgls{xml}~snippet that I showed you before.
|
1425 | 1425 | In the former example, we use \pythonil{print} as destination function.
|
|
1671 | 1671 | The \emph{dunder} stands for \emph{double underscore}, because the names of such methods begins and ends with~\pythonilIdx{\_\_}.
|
1672 | 1672 |
|
1673 | 1673 | Knowing about dunder methods allows us to create classes which can seamlessly be used in arithmetic expressions, in \pythonilIdx{with}~statements, as sequences to iterate over with \pythonilIdx{for}~loops, that support indexing with~\pythonil{[...]}\pythonIdx{[\idxdots]}.
|
1674 |
| -By implementing dunder methods, we can use the \python\ syntax to construct new collections, support more complex mathematical structures, ensure that resources are properly managed (and eventually disposed), or create elegant and concise APIs.% |
| 1674 | +By implementing dunder methods, we can use the \python\ syntax to construct new collections, support more complex mathematical structures, ensure that resources are properly managed (and eventually disposed), or create elegant and concise \pglspl{API}.% |
1675 | 1675 | \endhsection%
|
1676 | 1676 | %
|
1677 | 1677 | \FloatBarrier%
|
|
0 commit comments