{"id":329257,"date":"2023-09-20T12:48:21","date_gmt":"2023-09-20T12:48:21","guid":{"rendered":"https:\/\/www.blog.pythonlibrary.org\/?p=12113"},"modified":"2023-09-20T12:48:21","modified_gmt":"2023-09-20T12:48:21","slug":"learning-about-code-metrics-in-python-with-radon","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/09\/20\/learning-about-code-metrics-in-python-with-radon\/","title":{"rendered":"Learning About Code Metrics in Python with Radon"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>There are many different tools that you can install to help you be a better Python programmer. For example, you might install <a href=\"https:\/\/docs.pytest.org\/en\/7.4.x\/\">pytest<\/a> so that you can do unit tests of your code. Or you might install <a href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/06\/28\/an-intro-to-ruff-an-extremely-fast-python-linter\/\">Ruff, a super fast Python linter<\/a>. The focus of this article is on another tool called <a href=\"https:\/\/radon.readthedocs.io\/en\/latest\/index.html\">Radon<\/a> that helps you compute code metrics.<\/p>\n<p>You can use Radon to help you find complex code in your code base. This is known as Cyclomatic Complexity or McCabe\u2019s Complexity. According to Radon&#8217;s documentation:<\/p>\n<blockquote>\n<p>&#8220;Cyclomatic Complexity corresponds to the number of decisions a block of code contains plus 1. This number (also called McCabe number) is equal to the number of linearly independent paths through the code. This number can be used as a guide when testing conditional logic in blocks.&#8221;<\/p>\n<\/blockquote>\n<p>For example, if the number equals three, you will probably need to write at least three unit tests to have complete code coverage. Not only is this useful for figuring out how many tests to write, the cyclomatic complexity can tell you when it is time to refactor your code. You can read the full details of how Radon calculates complexity in their <a href=\"https:\/\/radon.readthedocs.io\/en\/latest\/intro.html\">documentation<\/a>.<\/p>\n<p>Experienced developers are often able to know when to refactor from their own experience, but newer engineers may need the help that a tool like this provides.<\/p>\n<p>You can also use a tool like Radon in your CI\/CD system to prevent developers from merging in overly complex code.<\/p>\n<h2>Installing Radon<\/h2>\n<p>You can install Radon using pip. Here is an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">python -m pip install radon<\/pre>\n<p>Now, let&#8217;s learn how to use Radon on your code!<\/p>\n<h2>Basic Radon Usage<\/h2>\n<p>You can call the <strong>radon<\/strong> application on the command line. Radon accepts multiple flags to control what kinds of output you receive.<\/p>\n<p>Here are the four commands that\u00a0<strong>radon<\/strong> currently can use:<\/p>\n<ul class=\"simple\">\n<li><strong class=\"command\">cc<\/strong>: compute Cyclomatic Complexity<\/li>\n<li><strong class=\"command\">raw<\/strong>: compute raw metrics<\/li>\n<li><strong class=\"command\">mi<\/strong>: compute Maintainability Index<\/li>\n<li><strong class=\"command\">hal<\/strong>: compute Halstead complexity metrics<\/li>\n<\/ul>\n<p>Let&#8217;s try running\u00a0<strong>radon<\/strong> against the popular <a href=\"https:\/\/pypi.org\/project\/black\/\">Black package<\/a> (Black is a popular code formatted for Python).<\/p>\n<p>Here is the command to run:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\black&gt; radon cc . -a -nc\r\nbrackets.py\r\n    F 225:0 is_split_before_delimiter - F\r\n    M 70:4 BracketTracker.mark - C\r\ncomments.py\r\n    F 140:0 convert_one_fmt_off_pair - D\r\n    F 208:0 generate_ignored_nodes - C\r\n    F 253:0 _generate_ignored_nodes_from_fmt_skip - C\r\nconcurrency.py\r\n    F 120:0 schedule_formatting - C\r\nfiles.py\r\n    F 309:0 gen_python_files - C\r\n    F 46:0 find_project_root - C\r\nlinegen.py\r\n    F 1133:0 normalize_invisible_parens - D\r\n    F 747:0 _maybe_split_omitting_optional_parens - D\r\n    F 1453:0 generate_trailers_to_omit - D\r\n    F 879:0 bracket_split_build_line - D\r\n    M 396:4 LineGenerator.visit_STRING - C\r\n    F 509:0 transform_line - C\r\n    F 997:0 delimiter_split - C\r\n    F 1529:0 run_transformer - C\r\n    F 1355:0 maybe_make_parens_invisible_in_atom - C\r\n    F 632:0 left_hand_split - C\r\n    M 289:4 LineGenerator.visit_simple_stmt - C\r\n    F 699:0 _first_right_hand_split - C\r\n    F 1313:0 remove_with_parens - C\r\nlines.py\r\n    M 569:4 EmptyLineTracker._maybe_empty_lines - E\r\n    M 646:4 EmptyLineTracker._maybe_empty_lines_for_class_or_def - E\r\n    F 755:0 is_line_short_enough - D\r\n    C 514:0 EmptyLineTracker - D\r\n    F 882:0 can_omit_invisible_parens - C\r\n    M 300:4 Line.has_magic_trailing_comma - C\r\n    F 846:0 can_be_split - C\r\n    M 62:4 Line.append - C\r\n    M 529:4 EmptyLineTracker.maybe_empty_lines - C\r\n    M 228:4 Line.contains_uncollapsable_type_comments - C\r\n    M 362:4 Line.append_comment - C\r\nnodes.py\r\n    F 174:0 whitespace - F\r\n    F 616:0 is_simple_decorator_trailer - C\r\n    F 573:0 is_one_sequence_between - C\r\nparsing.py\r\n    F 164:0 stringify_ast - C\r\n    F 57:0 lib2to3_parse - C\r\nstrings.py\r\n    F 173:0 normalize_string_quotes - C\r\ntrans.py\r\n    M 792:4 StringParenStripper.do_match - D\r\n    M 1388:4 StringSplitter.do_transform - D\r\n    M 2070:4 StringParenWrapper.do_transform - D\r\n    M 1064:4 BaseStringSplitter._get_max_string_length - D\r\n    M 1334:4 StringSplitter.do_splitter_match - C\r\n    M 686:4 StringMerger._validate_msg - C\r\n    M 542:4 StringMerger._merge_one_string_group - C\r\n    F 1219:0 iter_fexpr_spans - C\r\n    C 772:0 StringParenStripper - C\r\n    M 378:4 StringMerger.do_match - C\r\n    C 1801:0 StringParenWrapper - C\r\n    M 1859:4 StringParenWrapper.do_splitter_match - C\r\n    F 84:0 hug_power_op - C\r\n    C 358:0 StringMerger - C\r\n    M 1174:4 BaseStringSplitter._prefer_paren_wrap_match - C\r\n    M 1985:4 StringParenWrapper._assign_match - C\r\n    M 2032:4 StringParenWrapper._dict_or_lambda_match - C\r\n__init__.py\r\n    F 1156:0 get_features_used - F\r\n    F 443:0 main - E\r\n    F 616:0 get_sources - D\r\n    F 749:0 reformat_one - C\r\n    F 121:0 read_pyproject_toml - C\r\n    F 1094:0 _format_str_once - C\r\n    F 800:0 format_file_in_place - C\r\n\r\n62 blocks (classes, functions, methods) analyzed.\r\nAverage complexity: C (19.741935483870968)<\/pre>\n<p>In this example, you asked\u00a0<strong>radon<\/strong> to give you the Cyclomatic Complexity (cc) of the Black package. You also tacked on the <strong>-a<\/strong> or average flag and the\u00a0<strong>-n<\/strong> flag, which lets you set the minimum complexity rank to display. The default is &#8220;a&#8221;, but in this example, you set the minimum to &#8220;c&#8221;, meaning it will show ranks C to F.<\/p>\n<p>At this point, you can go through the code and start looking at the functions, methods, and classes to see what a C-ranked portion of code looks like compared with an F-ranked one. Give it a try and you&#8217;ll soon learn how to use radon to discover how complex your code is.<\/p>\n<p>There are more command line options than what is shown here. Check out the full listing of additional flag in <a href=\"https:\/\/radon.readthedocs.io\/en\/latest\/commandline.html#the-cc-command\">radon&#8217;s documentation<\/a>.<\/p>\n<p>You can also have radon measure the maintainability of your code by using the <strong>mi<\/strong> command. Here&#8217;s an example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS C:\\Users\\Mike\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\black&gt; radon mi .\r\nbrackets.py - A\r\ncache.py - A\r\ncomments.py - A\r\nconcurrency.py - A\r\nconst.py - A\r\ndebug.py - A\r\nfiles.py - A\r\nhandle_ipynb_magics.py - A\r\nlinegen.py - C\r\nlines.py - C\r\nmode.py - A\r\nnodes.py - C\r\nnumerics.py - A\r\noutput.py - A\r\nparsing.py - A\r\nreport.py - A\r\nrusty.py - A\r\nstrings.py - A\r\ntrans.py - C\r\n_width_table.py - A\r\n__init__.py - C\r\n__main__.py - A<\/pre>\n<p>The results of the\u00a0<strong>mi<\/strong> command are similar to the\u00a0<strong>cc<\/strong> command as radon will once again use letter ranks on your files to help you grade your code&#8217;s maintainability.<\/p>\n<p>Radon can also calculate some raw metrics about your code by using the\u00a0<strong>raw<\/strong> command. Here are the metrics that this command will calculate for you:<\/p>\n<ul class=\"simple\">\n<li><strong>LOC<\/strong>: the total number of lines of code<\/li>\n<li><strong>LLOC<\/strong>: the number of logical lines of code<\/li>\n<li><strong>SLOC<\/strong>: the number of source lines of code &#8211; not necessarily corresponding to the\u00a0<strong>LLOC<\/strong>\u00a0<a id=\"id3\" class=\"reference internal\" href=\"https:\/\/radon.readthedocs.io\/en\/latest\/commandline.html#wikipedia\">[Wikipedia]<\/a><\/li>\n<li><strong>comments<\/strong>: the number of Python comment lines (i.e. only single-line comments\u00a0<code class=\"docutils literal notranslate\"><span class=\"pre\">#<\/span><\/code>)<\/li>\n<li><strong>multi<\/strong>: the number of lines representing multi-line strings<\/li>\n<li><strong>blank<\/strong>: the number of blank lines (or whitespace-only ones)<\/li>\n<\/ul>\n<p>Let&#8217;s try running the\u00a0<strong>raw<\/strong> command against Black:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">PS C:\\Users\\wheifrd\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\black&gt; radon raw .\r\nbrackets.py\r\n    LOC: 375\r\n    LLOC: 197\r\n    SLOC: 253\r\n    Comments: 4\r\n    Single comments: 11\r\n    Multi: 47\r\n    Blank: 64\r\n    - Comment Stats\r\n        (C % L): 1%\r\n        (C % S): 2%\r\n        (C + M % L): 14%\r\ncache.py\r\n    LOC: 97\r\n    LLOC: 59\r\n    SLOC: 54\r\n    Comments: 2\r\n    Single comments: 5\r\n    Multi: 14\r\n    Blank: 24\r\n    - Comment Stats\r\n        (C % L): 2%\r\n        (C % S): 4%\r\n        (C + M % L): 16%\r\ncomments.py\r\n    LOC: 329\r\n    LLOC: 202\r\n    SLOC: 212\r\n    Comments: 33\r\n    Single comments: 29\r\n    Multi: 42\r\n    Blank: 46\r\n    - Comment Stats\r\n        (C % L): 10%\r\n        (C % S): 16%\r\n        (C + M % L): 23%<\/pre>\n<p>The example above has been truncated since there is a LOT of output. But this snippet gives you a good idea of what to expect when you run the\u00a0<strong>raw<\/strong> command against your code.<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Radon is a great tool to add to your toolbox. You can use it to help you figure out which parts of your source code are getting too complex. You can also use Radon in your CI\/CD infrastructure to prevent developers from checking in overly complex code. You might use this for your own open-source or commercial projects at your employer.<\/p>\n<p>Give it a try and see if you like Radon!<\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/09\/20\/learning-about-code-metrics-in-python-with-radon\/\">Learning About Code Metrics in Python with Radon<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>There are many different tools that you can install to help you be a better Python programmer. For example, you might install pytest so that you can do unit tests of your code. Or you might install Ruff, a super fast Python linter. The focus of this article is on another tool called Radon that \u2026<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/09\/20\/learning-about-code-metrics-in-python-with-radon\/\"> <span class=\"screen-reader-text\">Learning About Code Metrics in Python with Radon<\/span> Read More \u00bb<\/a><\/p>\n<p>The post <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/2023\/09\/20\/learning-about-code-metrics-in-python-with-radon\/\">Learning About Code Metrics in Python with Radon<\/a> appeared first on <a rel=\"nofollow\" href=\"https:\/\/www.blog.pythonlibrary.org\/\">Mouse Vs Python<\/a>.<\/p>\n<\/div>","protected":false},"author":2018,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Learning About Code Metrics in Python with Radon - ITTeacherITFreelance.hk","description":"There are many different tools that you can install to help you be a better Python programmer. For example, you might install pytest so that you can do unit tes"},"footnotes":""},"categories":[10700],"tags":[10713,10714,10715],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329257"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2018"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329257"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329257\/revisions"}],"predecessor-version":[{"id":329258,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329257\/revisions\/329258"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}