{"id":1372,"date":"2021-04-02T19:34:24","date_gmt":"2021-04-03T02:34:24","guid":{"rendered":"https:\/\/coderpad-staging.io\/?post_type=programming-language&#038;p=1102"},"modified":"2023-04-17T03:46:11","modified_gmt":"2023-04-17T10:46:11","slug":"python-3","status":"publish","type":"programming-language","link":"https:\/\/coderpad.io\/languages\/python-3\/","title":{"rendered":"Python 3"},"content":{"rendered":"\n<p>Python 3 in CoderPad is generally identical to the Python 2.x environment.<\/p>\n\n\n\n<p>One small difference is that&nbsp;<code>mock<\/code>&nbsp;is available in Python 3 in the stdlib, as&nbsp;<code>unittest.mock<\/code>.<\/p>\n\n\n\n<p>Information about the Python 2.x environment is reproduced below:<\/p>\n\n\n\n<p>The Python environment is augmented with a few REPL features as well as some helpful libraries.<\/p>\n\n\n\n<p>The REPL uses&nbsp;<a href=\"http:\/\/ipython.org\/\" target=\"_blank\" rel=\"noopener\">IPython<\/a>&nbsp;to provide a REPL with history, highlighting, and autocomplete. Additionally, whenever you run scripts in CoderPad\u2019s editor, the REPL will deposit you at the exact line and state of any exceptions. If there were no errors, you will have a REPL with access to all of the variables and functions defined in your script.<\/p>\n\n\n\n<p>The libraries included and ready for importing are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/requests.readthedocs.io\/en\/master\/\" target=\"_blank\" rel=\"noopener\">requests<\/a>&nbsp;for simpler HTTP requests.<\/li>\n\n\n\n<li><a href=\"https:\/\/pypi.org\/project\/beautifulsoup4\/\" target=\"_blank\" rel=\"noopener\">beautifulsoup4<\/a>&nbsp;for HTML parsing.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.numpy.org\/\" target=\"_blank\" rel=\"noopener\">numpy<\/a>,&nbsp;<a href=\"https:\/\/www.scipy.org\/\" target=\"_blank\" rel=\"noopener\">scipy<\/a>,&nbsp;<a href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\" rel=\"noopener\">pandas<\/a>,&nbsp;<a href=\"https:\/\/scikit-learn.org\/stable\/\" target=\"_blank\" rel=\"noopener\">scikit-learn<\/a>, and&nbsp;<a href=\"https:\/\/www.statsmodels.org\/\" target=\"_blank\" rel=\"noopener\">statsmodels<\/a>&nbsp;for advanced numerical analysis. Unfortunately, plotting does not work in CoderPad\u2019s purely textual interface at this time.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"testing\">Testing<\/h3>\n\n\n\n<p>We\u2019ve got a few ways you can test your Python code in CoderPad:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The&nbsp;<a href=\"https:\/\/docs.python.org\/2\/library\/unittest.html\" target=\"_blank\" rel=\"noopener\">unittest<\/a>&nbsp;library that ships with Python by default. Here\u2019s a quick example:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestStringMethods<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_upper<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertEqual(<span class=\"hljs-string\">'foo'<\/span>.upper(), <span class=\"hljs-string\">'FOO'<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_isupper<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        self.assertTrue(<span class=\"hljs-string\">'FOO'<\/span>.isupper())\n        self.assertFalse(<span class=\"hljs-string\">'Foo'<\/span>.isupper())\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_split<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        s = <span class=\"hljs-string\">'hello world'<\/span>\n        self.assertEqual(s.split(), &#91;<span class=\"hljs-string\">'hello'<\/span>, <span class=\"hljs-string\">'world'<\/span>])\n        <span class=\"hljs-comment\"># s.split should throw when the separator is not a string<\/span>\n        <span class=\"hljs-keyword\">with<\/span> self.assertRaises(TypeError):\n            s.split(<span class=\"hljs-number\">2<\/span>)\n\nunittest.main(exit=<span class=\"hljs-literal\">False<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><a href=\"https:\/\/pytest.org\/en\/latest\/\" target=\"_blank\" rel=\"noopener\">pytest<\/a>. The above snippet of code would look like the following when written for pytest:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> pytest\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_upper<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">assert<\/span> <span class=\"hljs-string\">'foo'<\/span>.upper() == <span class=\"hljs-string\">'FOO'<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_isupper<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    <span class=\"hljs-keyword\">assert<\/span> <span class=\"hljs-string\">'FOO'<\/span>.isupper()\n    <span class=\"hljs-keyword\">assert<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-string\">'Foo'<\/span>.isupper()\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_split<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    s = <span class=\"hljs-string\">'hello world'<\/span>\n    <span class=\"hljs-keyword\">assert<\/span> s.split() == &#91;<span class=\"hljs-string\">'hello'<\/span>, <span class=\"hljs-string\">'world'<\/span>]\n    <span class=\"hljs-comment\"># s.split should throw when the separator is not a string<\/span>\n    <span class=\"hljs-keyword\">with<\/span> pytest.raises(TypeError):\n        s.split(<span class=\"hljs-number\">2<\/span>)\n\npytest.main()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><a href=\"https:\/\/github.com\/testing-cabal\/mock\" target=\"_blank\" rel=\"noopener\">mock<\/a>&nbsp;is also available if you need to stub out some behavior. <code>mock<\/code>&nbsp;can be combined with&nbsp;<code>unittest<\/code>&nbsp;and&nbsp;<code>pytest<\/code>. Here\u2019s a quick usage example:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-keyword\">from<\/span> mock <span class=\"hljs-keyword\">import<\/span> Mock\n\nmock = Mock()\nmock.method(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">3<\/span>)\nmock.method.assert_called_with(<span class=\"hljs-string\">'this should break'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><a href=\"https:\/\/hypothesis.works\/\" target=\"_blank\" rel=\"noopener\">hypothesis<\/a>&nbsp;is available for property-based testing in Python. Calling&nbsp;<code>test_decode_inverts_encode()<\/code>&nbsp;fires up Hypothesis and tries to find an input that breaks your code. You can read more about it on their website, but here\u2019s a stubbed example of how you might test that an encoding and decoding function both work:<\/li>\n<\/ol>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-keyword\">from<\/span> hypothesis <span class=\"hljs-keyword\">import<\/span> given\n<span class=\"hljs-keyword\">from<\/span> hypothesis.strategies <span class=\"hljs-keyword\">import<\/span> text\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">encode<\/span><span class=\"hljs-params\">(string)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># return encoded string<\/span>\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">decode<\/span><span class=\"hljs-params\">(string)<\/span>:<\/span>\n    <span class=\"hljs-comment\"># return decoded string<\/span>\n\n<span class=\"hljs-meta\">@given(text())<\/span>\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_decode_inverts_encode<\/span><span class=\"hljs-params\">(s)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">assert<\/span> decode(encode(s)) == s\n\ntest_decode_inverts_encode()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Displaying charts and graphs<\/h3>\n\n\n\n<p>If you want to display a chart or graph using libraries like <a href=\"https:\/\/matplotlib.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Matplotlib<\/a>, you can use a pad with <a href=\"https:\/\/coderpad.io\/languages\/python-3\/django\/\" target=\"_blank\" rel=\"noreferrer noopener\">Django<\/a> selected. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/image-82.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"519\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/image-82-1024x519.png\" alt=\"Pad questions menu is displayed with the &quot;django&quot; option highlighted.\" class=\"wp-image-29489\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-82-1024x519.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-82-300x152.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-82-768x389.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-82-18x9.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-82.png 1519w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p>Django is a Python-based web framework that will allow you to display your charts in a convenient interface. We&#8217;ve created <a href=\"https:\/\/app.coderpad.io\/sandbox?question_id=239837\" target=\"_blank\" rel=\"noreferrer noopener\">a ready-made Matplotlib sandbox pad<\/a> for you to use, all you need to do is edit the <code>view.py<\/code> file with your chart or graph code and it will display to the UI on the right via the <code>matplotlib.htm<\/code>l file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"466\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/image-83-1024x466.png\" alt=\"\" class=\"wp-image-29492\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83-1024x466.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83-300x136.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83-768x349.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83-1536x699.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83-18x8.png 18w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-83.png 1844w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you&#8217;d like to use another charting library, simply install the library using <code>pip3 install [package]<\/code> in the <strong>Shell<\/strong> tab on the bottom right corner, and edit the <code>view.py<\/code> file as needed to display the appropriate graph.<\/p>\n\n\n\n<p><a href=\"https:\/\/app.coderpad.io\/sandbox?question_id=239837\" target=\"_blank\" rel=\"noreferrer noopener\">You can access the Django sandbox pad with matplotlib installed here<\/a>. <\/p>\n\n\n\n<p>Simply click the <strong>Create Pad<\/strong> button in the bottom left to make your own copy, which you can use in your own interviews.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/image-84.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2023\/01\/image-84.png\" alt=\"A django pad with an arrow pointing to the &quot;create pad&quot; button in the bottom left corner of the pad.\" class=\"wp-image-29493\" width=\"708\" height=\"695\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-84.png 944w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-84-300x295.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-84-768x754.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2023\/01\/image-84-12x12.png 12w\" sizes=\"auto, (max-width: 708px) 100vw, 708px\" \/><\/a><\/figure>\n<\/div>","protected":false},"parent":0,"menu_order":0,"template":"","class_list":["post-1372","programming-language","type-programming-language","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/programming-language\/1372","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/programming-language"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/programming-language"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=1372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}