Ë j¸P<¿&«tãó–—dZddlZddlZddlmZddlmZmZmZm Z m Z m Z dZ ejd«Zd„Zd d„Zd „Zdd „Zdd „Zd „Zy)aº Functions that render ASCII tables. Some generic notes about the table formatting functions in this module: - These functions were not written with performance in mind (*at all*) because they're intended to format tabular data to be presented on a terminal. If someone were to run into a performance problem using these functions, they'd be printing so much tabular data to the terminal that a human wouldn't be able to digest the tabular data anyway, so the point is moot :-). - These functions ignore ANSI escape sequences (at least the ones generated by the :mod:`~humanfriendly.terminal` module) in the calculation of columns widths. On reason for this is that column names are highlighted in color when connected to a terminal. It also means that you can use ANSI escape sequences to highlight certain column's values if you feel like it (for example to highlight deviations from the norm in an overview of calculated values). éN)Ú coerce_string)Ú ansi_stripÚ ansi_widthÚ ansi_wrapÚterminal_supports_colorsÚfind_terminal_sizeÚHIGHLIGHT_COLOR)Úformat_pretty_tableÚformat_robust_tableÚformat_rst_tableÚformat_smart_tablez ^\d+(\.\d+)?$có—|D�cgc] }t|«‘Œ}}t|«}td„|D««sGt||«}tt t |j «««}t«\}}||kr|St||«Scc}w)a7 Render tabular data using the most appropriate representation. :param data: An iterable (e.g. a :func:`tuple` or :class:`list`) containing the rows of the table, where each row is an iterable containing the columns of the table (strings). :param column_names: An iterable of column names (strings). :returns: The rendered table (a string). If you want an easy way to render tabular data on a terminal in a human friendly format then this function is for you! It works as follows: - If the input data doesn't contain any line breaks the function :func:`format_pretty_table()` is used to render a pretty table. If the resulting table fits in the terminal without wrapping the rendered pretty table is returned. - If the input data does contain line breaks or if a pretty table would wrap (given the width of the terminal) then the function :func:`format_robust_table()` is used to render a more robust table that can deal with data containing line breaks and long text. c3ó@K—|]}td„|D««–—Œy­w)c3ó$K—|]}d|v–—Œ y­w©Ú N©)Ú.0Úcs ú9/usr/lib/python3.12/site-packages/humanfriendly/tables.pyÚ z/format_smart_table...[sèø€Ð(¡a �4˜1”9¡aùs‚N)Úany©rÚrs rrz%format_smart_table..[sèø€Ð7±$¨QŒsÑ(¡aÓ(×(±$ùs‚) Únormalize_columnsrr ÚmaxÚmaprÚ splitlinesrr )ÚdataÚ column_namesrÚ pretty_tableÚ table_widthÚnum_rowsÚ num_columnss rr r >s‹€ñ2+/Ó /©$ QÔ ˜aÕ ¨$€DÐ /Ü$ \Ó2€Lô Ñ7±$Ó7Ô 7ä*¨4°Ó>ˆ äœ#œj¨,×*AÑ*AÓ*CÓDÓEˆ Ü 2Ó 4ш�+Ø ˜+Ò %àÐ ä ˜t \Ó 2Ð2ùò 0s…B c ót—|D�cgc]}t|d¬«‘Œ}}|�At|«}|r4t«r|D�cgc] }t|«‘Œ}}|jd|«t j t «}t j t«}t|«D]t\}} t| «D]a\} } t|| t| ««|| <|r|dk(rŒ)|| jttjt| ««««ŒcŒv|t!|j#««t%|«dzzdzz} | g} t|«D]½\}} |g}t| «D]m\} } d|| t| «z z}t'|| «r|jd|z| zdz«n|jd| z|zdz«|j|«Œo| jdj)|««|sŒ§|dk(sŒ­| j| «Œ¿| j| «dj)| «Scc}wcc}w) af Render a table using characters like dashes and vertical bars to emulate borders. :param data: An iterable (e.g. a :func:`tuple` or :class:`list`) containing the rows of the table, where each row is an iterable containing the columns of the table (strings). :param column_names: An iterable of column names (strings). :param horizontal_bar: The character used to represent a horizontal bar (a string). :param vertical_bar: The character used to represent a vertical bar (a string). :returns: The rendered table (a string). Here's an example: >>> from humanfriendly.tables import format_pretty_table >>> column_names = ['Version', 'Uploaded on', 'Downloads'] >>> humanfriendly_releases = [ ... ['1.23', '2015-05-25', '218'], ... ['1.23.1', '2015-05-26', '1354'], ... ['1.24', '2015-05-26', '223'], ... ['1.25', '2015-05-26', '4319'], ... ['1.25.1', '2015-06-02', '197'], ... ] >>> print(format_pretty_table(humanfriendly_releases, column_names)) ------------------------------------- | Version | Uploaded on | Downloads | ------------------------------------- | 1.23 | 2015-05-25 | 218 | | 1.23.1 | 2015-05-26 | 1354 | | 1.24 | 2015-05-26 | 223 | | 1.25 | 2015-05-26 | 4319 | | 1.25.1 | 2015-06-02 | 197 | ------------------------------------- Notes about the resulting table: - If a column contains numeric data (integer and/or floating point numbers) in all rows (ignoring column names of course) then the content of that column is right-aligned, as can be seen in the example above. The idea here is to make it easier to compare the numbers in different columns to each other. - The column names are highlighted in color so they stand out a bit more (see also :data:`.HIGHLIGHT_COLOR`). The following screen shot shows what that looks like (my terminals are always set to white text on a black background): .. image:: images/pretty-table.png T)Ú expandtabsrééÚ Úr)rrÚhighlight_column_nameÚinsertÚ collectionsÚ defaultdictÚintÚlistÚ enumeraterrÚappendÚboolÚNUMERIC_DATA_PATTERNÚmatchrÚsumÚvaluesÚlenÚallÚjoin)rr Úhorizontal_barÚ vertical_barrÚnÚwidthsÚ numeric_dataÚ row_indexÚrowÚ column_indexÚcolumnÚline_delimiterÚlinesÚlineÚpaddings rr r hs€ñh<@Ó @¹4°aÔ ˜a¨DÖ 1¸4€DÐ @ØÐÜ(¨Ó6ˆ Ù Ü'Ô)ÙBNÓOÁ,¸QÔ 5°aÕ 8À,� ÐOØ �K‰K˜˜<Ô (ä × $Ñ $¤SÓ )€FÜ×*Ñ*¬4Ó0€LÜ# Dž/‰ˆ �3Ü$-¨c¦NÑ ˆL˜&Ü#& v¨lÑ';¼ZÈÓ=OÓ#PˆF�<Ñ Ù  Y°!£^ؘ\Ñ*×1Ñ1´$Ô7K×7QÑ7QÔR\Ð]cÓRdÓ7eÓ2fÕgñ%3ð*ð $¤s¨6¯=©=«?Ó';¼cÀ&»kÈA¹oÑ'MÐPQÑ'QÑR€Nà Ð €Eä# Dž/‰ˆ �3؈~ˆÜ$-¨c¦NÑ ˆL˜&ؘV LÑ1´J¸vÓ4FÑFÑGˆGÜ�<  Ñ-Ô.Ø— ‘ ˜C '™M¨FÑ2°SÑ8Õ9à— ‘ ˜C &™L¨7Ñ2°SÑ8Ô9Ø �K‰K˜ Õ %ð %3ð � ‰ �S—X‘X˜d“^Ô$Ú ˜I¨›NØ �L‰L˜Õ (ð*ð ‡L�L�Ô à �:‰:�eÓ ÐùòG Aùò Ps …H0¸H5cóÒ—g}t|«D�cgc]}d|z‘Œ }}t«r|D�cgc] }t|«‘Œ}}|D] }g}tt|««D]t\}}|j «}d|vr|j ||›d|›�«Œ4|j ||«|j |j«j««Œv|j |«Œ¢t«\} } td„|D««} ddt| | «zz} |jdd«|j d«| jd „|D««j «Scc}wcc}w) aä Render tabular data with one column per line (allowing columns with line breaks). :param data: An iterable (e.g. a :func:`tuple` or :class:`list`) containing the rows of the table, where each row is an iterable containing the columns of the table (strings). :param column_names: An iterable of column names (strings). :returns: The rendered table (a string). Here's an example: >>> from humanfriendly.tables import format_robust_table >>> column_names = ['Version', 'Uploaded on', 'Downloads'] >>> humanfriendly_releases = [ ... ['1.23', '2015-05-25', '218'], ... ['1.23.1', '2015-05-26', '1354'], ... ['1.24', '2015-05-26', '223'], ... ['1.25', '2015-05-26', '4319'], ... ['1.25.1', '2015-06-02', '197'], ... ] >>> print(format_robust_table(humanfriendly_releases, column_names)) ----------------------- Version: 1.23 Uploaded on: 2015-05-25 Downloads: 218 ----------------------- Version: 1.23.1 Uploaded on: 2015-05-26 Downloads: 1354 ----------------------- Version: 1.24 Uploaded on: 2015-05-26 Downloads: 223 ----------------------- Version: 1.25 Uploaded on: 2015-05-26 Downloads: 4319 ----------------------- Version: 1.25.1 Uploaded on: 2015-06-02 Downloads: 197 ----------------------- The column names are highlighted in bold font and color so they stand out a bit more (see :data:`.HIGHLIGHT_COLOR`). z%s:rr)c3óNK—|]}ttt|««–—Œy­w©N)rrr)rrEs rrz&format_robust_table..sèø€ÐGÁ°u”sœ3œz¨5Ó1×2Áùs‚#%z %s Ú-rr*c3ó>K—|]}dj|«–—Œy­wr©r:)rÚbs rrz&format_robust_table..sèø€Ð8±¨A˜%Ÿ*™* QŸ-±ùó‚)rrr+r1Ústripr2ÚextendÚrstriprrrÚminr,r:) rr Úblocksr=rArErBÚ column_textÚstripped_columnr#r$Ú longest_lineÚ delimiters rr r ÂsV€ð^€FÜ'8¸Ô'FÓGÑ'F !�E˜A“IÐ'F€LÐGÜÔ!Ù:FÓG¹,°QÔ-¨aÕ0¸,ˆ ÐGóˆØˆÜ)2Ô3DÀSÓ3IÖ)JÑ %ˆL˜+Ø)×/Ñ/Ó1ˆOؘ?Ñ*à— ‘ ¨ °\Ó(BÁOÐTÕUð — ‘ ˜\¨,Ñ7Ô8Ø— ‘ ˜[×/Ñ/Ó1×<Ñ<Ó>Õ?ð*Kð � ‰ �eÕðô /Ó0Ñ€HˆkÜÑGÁÓGÓG€LؘS¤3 |°[Ó#AÑAÑB€Ià ‡M�M�!�RÔØ ‡M�M�"Ôà �>‰>Ñ8±Ó8Ó 8× >Ñ >Ó @Ð@ùò9HùâGs � E¬E$cóž—|D�cgc] }t|«‘Œ}}|r|jdt|««tjt«}|D]0}t |«D] \}}t ||t|««||<Œ"Œ2|D]>}t |«D].\}}|t|«dz ksŒ|j||«||<Œ0Œ@t|j««D��cgc] \}}d|z‘Œ } }}|r|jd| «|jd| «|j| «djd„|D««Scc}wcc}}w)a¹ Render a table in reStructuredText_ format. :param data: An iterable (e.g. a :func:`tuple` or :class:`list`) containing the rows of the table, where each row is an iterable containing the columns of the table (strings). :param column_names: An iterable of column names (strings). :returns: The rendered table (a string). Here's an example: >>> from humanfriendly.tables import format_rst_table >>> column_names = ['Version', 'Uploaded on', 'Downloads'] >>> humanfriendly_releases = [ ... ['1.23', '2015-05-25', '218'], ... ['1.23.1', '2015-05-26', '1354'], ... ['1.24', '2015-05-26', '223'], ... ['1.25', '2015-05-26', '4319'], ... ['1.25.1', '2015-06-02', '197'], ... ] >>> print(format_rst_table(humanfriendly_releases, column_names)) ======= =========== ========= Version Uploaded on Downloads ======= =========== ========= 1.23 2015-05-25 218 1.23.1 2015-05-26 1354 1.24 2015-05-26 223 1.25 2015-05-26 4319 1.25.1 2015-06-02 197 ======= =========== ========= .. _reStructuredText: https://en.wikipedia.org/wiki/ReStructuredText rr(Ú=rc3ó>K—|]}dj|«–—Œy­w)z NrMrs rrz#format_rst_table..Gsèø€Ð0©4 a�T—Y‘Y˜q—\©4ùrO) rr,r-r.r/r1rr8ÚljustÚsortedÚitemsr2r:) rr rr>rAÚindexrCÚiÚwrXs rr r s3€ñD+/Ó /©$ QÔ ˜aÕ ¨$€DÐ /ÙØ � ‰ �AÔ(¨Ó6Ô7ä × $Ñ $¤SÓ )€FÛˆÜ& sž^‰MˆE�6Ü  u¡ ¬s°6«{Ó;ˆF�5ŠMñ,ðóˆÜ& sž^‰MˆE�6Øœ˜C› 1™ Ó%Ø#Ÿ\™\¨&°©-Ó8��E’ ñ,ðô &,¨F¯L©L«NÔ%;Ô<Ñ%;™T˜Q ��q“Ð%;€IÑ<ÙØ � ‰ �A�yÔ!؇K�K��9ÔØ‡K�K� Ôà �9‰9Ñ0©4Ó0Ó 0Ð0ùò) 0ùó=s …EÃ#E cót—g}|D]0}t|«}|r|j«}|j|«Œ2|SrJ)rr&r2)rAr&ÚresultsÚvalueÚtexts rrrJs=€Ø€GۈܘUÓ#ˆÙ Ø—?‘?Ó$ˆDØ�‰�tÕð ð €Nócó&—t|dt¬«S)NT)ÚboldÚcolor)rr )Únames rr+r+Ts€Ü �T ¬OÔ <Ðrrsaðñó(Û õ/÷÷ð €ð""�r—z‘zÐ"2Ó3Ðò'3óTWòtLAó^61óró=rf