<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andy Pickup on Medium]]></title>
        <description><![CDATA[Stories by Andy Pickup on Medium]]></description>
        <link>https://medium.com/@andypickup101?source=rss-48ba418b451d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jQ6VLn8H5Z7CuuWrea7s9Q.jpeg</url>
            <title>Stories by Andy Pickup on Medium</title>
            <link>https://medium.com/@andypickup101?source=rss-48ba418b451d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 03:30:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andypickup101/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Claude Dashboards ... Anthropic are trolling you!]]></title>
            <link>https://medium.com/@andypickup101/claude-dashboards-anthropic-are-trolling-you-55df1fbf8792?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/55df1fbf8792</guid>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[claude]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 19 Oct 2026 09:35:27 GMT</pubDate>
            <atom:updated>2026-10-19T09:35:27.578Z</atom:updated>
            <content:encoded><![CDATA[<p>Here&#39;s my take on Claude Dashboards ... Anthropic are trolling you!</p><p>Building dashboards has never been difficult. You can do it in Excel. I&#39;ve used SSRS, Crystal Reports, Business Objects Server, Power BI, Qlik, Tableau, ThoughtSpot and now Sigma. Probably some others I&#39;ve long since forgotten too!</p><p>For years we&#39;ve been building with these tools and generating pretty little graphs for PowerPoints. Each time a new iteration comes along we get prettier little graphs - d3.js anyone?!</p><p>Now AI has decided what we need next is mega pretty moving graphs.</p><blockquote>OMG wow you non data team person, didn&#39;t you do well? Here&#39;s a biscuit and pat on the head!</blockquote><p>Just one question...</p><p><strong>SO WHAT?!</strong></p><p>I could show a business user that new deals created this week is 8,835 on an Excel sheet, a bar chart, a line graph broken down by country, a pie chart (fml), a 3D pie chart (wtf), an animated heat map by hour or territory, a bar chart race or or or I could even write it down on a piece of paper and stick it right in their face!!!</p><p><em>(But only if it&#39;s in dark mode, blue highlighter on black paper amiright?!)</em></p><p>Buuuuuut … <strong>SO WHAT?!</strong></p><p>What does that 8,835 mean? What does the business need to do next?</p><ul><li>Is it better than last week?</li><li>Is it worse then the week before?</li><li>How is it trending?</li><li>What are the contributing factors?</li><li>What are the levers to pull to move this number?</li><li>Has speed of activation changed?</li><li>Are all these deals suspected nonsense that won’t convert?</li><li>Did marketing hit diminishing returns? Again?</li></ul><p><strong>WHAT DOES IT ALL MEAN BASIL?!?!?!</strong></p><p>Extracting the number and outputting it is not, and never has been, the hard part.</p><p>The hard part is helping the business understand it, and actually <em>do</em> something following it.</p><p>That’s where most data teams fall down, they see actionability of data as outside their remit, so business users flap around and struggle until a snake oil salesman comes around and sells them a prettier version of 8,835 to stick on a PowerPoint that they show to their boss - who <em>also</em> doesn’t know what to do with that number.</p><p><strong>The worst thing?</strong></p><p>How desperate BI vendors are and how eagerly they&#39;ve just jumped into bed with AI providers on this. And I&#39;m looking at you Sigma!</p><p>How much did Anthropic pay you to integrate with Claude Dashboards in like week 1? Without security permissions being built into the platform by user or data model? Just a massive free for all. Because you’ve failed to deliver the platform AI features you promised us a year ago.</p><p>What possible motivation could Anthropic have for paying BI vendors for access?</p><p>Could it be because we&#39;re all using sandboxed data platforms to run Claude analysis where our data stays private and never gets sent to the third party vendor?</p><p>Could it be that now they can get in via the front door our users have completely and willingly shifted terrabytes of data from a secure vault into the hands of an AI vendor, completely ignoring the rules (such as they are) around data sovereignty? All as part of Big Techs new data grab initiative?</p><blockquote>But the opportunity to the business is massive Andy, get on board!</blockquote><p>I&#39;m sure in an Anthropic boardroom this year, someone asked how to make this happen, and someone said &quot;<em>mega pretty moving graphs</em>&quot;.</p><p>And they became the Pied Piper to incompetent business users desperately trying to keep their jobs kicking the can of actionability down the road another year.</p><p>So next time you think about reaching for Claude Dashboards ... stop and have a good think about the question you&#39;re trying to answer, what you&#39;re actually going to <em>do</em> next, and whether it&#39;s really worth handing over all your customer’s PII to the evil billionaires just for a shiny animated 3D pie chart.</p><p>You&#39;re being trolled. Don&#39;t fall for it.</p><p><em>* At time of writing, Claude’s data retention settings don’t apply to Claude Dashboards. They appear to be ingesting all of it!</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8WxnA85ieuQypx3zZ4V9lA.jpeg" /><figcaption>Google Gemini said this post was a banger and this image would be perfect and I trusted it implicitly!</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=55df1fbf8792" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Entering the world of Snowflake Cortex Code]]></title>
            <link>https://medium.com/@andypickup101/entering-the-world-of-snowflake-cortex-code-310131ba077c?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/310131ba077c</guid>
            <category><![CDATA[modern-data-stack]]></category>
            <category><![CDATA[snowflake-cortex]]></category>
            <category><![CDATA[snowflake]]></category>
            <category><![CDATA[dbt]]></category>
            <category><![CDATA[martech]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 06 Apr 2026 07:59:16 GMT</pubDate>
            <atom:updated>2026-04-06T07:59:38.042Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OVFh2l8OHzbbeqEuSMYnzA.webp" /><figcaption>Snowflake CoCo CLI (from snowflake.com)</figcaption></figure><p>It’s been a while huh?! My career went wild! Haven’t had a lot of time, or bandwidth really, for keeping up the posts. Still using <em>Python</em> but the last 2 years have largely been a mixture of <em>C#</em> and <em>Unity</em>, <em>SQL</em> and <em>Snowflake</em>, <em>dbt</em> and a lot of BI via <em>Sigma</em>.</p><p>The job I had when I started the previous series was a <strong>Head of Systems and Data</strong> in a TV rental startup that ran out of cash and forced me to pivot into a <strong>Data Engineer</strong> role to avoid ending up on the streets!</p><blockquote>After over a decade of senior level titles, sometimes you gotta suck it up and just take a job right?!</blockquote><p>But sometimes, that isn’t always a bad thing. I’ve been building. From the ground up. A new data platform in a new data team that has seen me jump to <strong>Head of Data Engineering</strong> and then to <strong>Head of Data Operations</strong>, with a path open for a <strong>Director</strong> role. So not too bad a pivot!</p><blockquote>And mostly, I’m excited again!</blockquote><p>This part of tech is a really interesting place to be right now. I’ve spent most of my 20 year career in the world of data and SQL but I’ve often found it fairly stagnant.</p><p>However, I think we’ve just had one of those step-change moments that will transform MarTech forever!</p><p>I&#39;ve been working with MarTech for rapidly approaching a decade. The last major step-change was GA4 and Events. You could argue that wasn&#39;t successful. You could also argue that it completely disrupted the industry creating a clear separation between those marketers who could barely operate Excel and those who understood the seismic opportunity ahead of them. The fact the latter is about 1% of people in marketing is kinda why it failed I guess! (j/k)</p><p>I think what we&#39;ll see happen after this next step-change is three things;</p><ul><li>a massive exodus out of the industry as people are either made redundant or quit to go do something else</li><li>folks move sideways in organisations, into departments like Finance where they can continue to use Excel like it’s 1998</li><li>a re-enablement of those who didn’t quite understand GA4 but can now utilise AI to bridge that detailed technical gap</li></ul><p>When I first started building data platforms and ingesting predominantly marketing data via API it felt like a lot of manual work for what is basically a handful of core platforms that everyone is using.</p><blockquote>So why isn’t there something cookie cutter? One ring to rule them all.</blockquote><p>All that seemed to exist was platforms like <em>Supermetrics</em> that black boxed your data and wouldn’t let you link it up to your own data without you sending it to them. No thanks!</p><p>Then I used <em>FiveTran</em> and its <em>dbt</em> transformations for marketing and social. And that&#39;s great, really helpful, but solving a fraction of the problem. It&#39;s like a starter for 10!</p><p>We ingest data from the <em>Facebook API</em> into 96 tables. We use 9 of these, disabled the rest. It would be a gargantuan effort to model these. And then linking them up to the data from <em>Google</em>, <em>TikTok</em>, <em>Bing</em>, <em>Pinterest</em> etc is even more work. But there must be some insights in this data somewhere that we can make better use of.</p><p>Further, we need to tie all of that data with our first party data from our CRM and WMS platforms to really understand the value. And our call centre stack too while we&#39;re at it. This is a lot of work that requires a 30-50 headcount in the Data Team. And it requires a lot of time!</p><blockquote>Or...at least it used to.</blockquote><p>The step-change I&#39;m talking about is <em>Snowflake Cortex Code</em>!</p><p>This thing isn&#39;t just good, it&#39;s something else. It&#39;s other worldly. If you know what you&#39;re doing. In the wrong hands this thing has the potential to be an unmitigated disaster for business. Seriously, don&#39;t let the guy who thinks he knows data get hold of this! With great power comes great responsibility and all that. Don&#39;t let your business be thwarted by someone&#39;s out of control ego trip!</p><p><em>Snowflake CoCo</em> is the perfect application of an LLM. It&#39;s not AI. These things aren&#39;t. It&#39;s a top notch pattern matcher. Look at data in places A,B,C then spot the commonality and recommend some stuff from your massive bank of observed data actions. Seriously, stop treating LLMs like a black box, they&#39;re not magic. Actually understand how they work and take control!</p><p>Having <em>CoCo</em> join the dots between disparate schemas and ERDs and actual data right across the entire pipeline rather than in siloed bursts is the missing link for data modelling. Well, almost.</p><p>What we&#39;ve found in our testing is pretty much what you&#39;d expect. The context is the actual missing link.</p><p>How else is <em>CoCo</em> supposed to know that our attribution sucks? Or that the <em>HubSpot</em> UTM matching was imported incorrectly and nothing worked for months? Or that the 301s weren&#39;t set up properly? Or that the cookie banner wasn&#39;t compatible with <em>GCMv2</em> for months or on one site wasn&#39;t set up at all?</p><p>How else is <em>CoCo</em> to know that our <em>GA4</em> traffic was double counting due to years of poor <em>GTM</em> hygiene and configuration? Or that our integration with <em>Conversions API</em> wasn&#39;t done properly? Or that the value we send for value based bidding has been completely made up and over values every single lead?</p><p>Yeah. It doesn&#39;t know that. And can&#39;t guess it either. It can spot a change in the data when we fixed it. But it can&#39;t identify it wasn&#39;t working and it can&#39;t identify the individual fixes we had running over time until the eventual big fix. It just doesn&#39;t know. It can&#39;t.</p><p>The missing link is this context that needs to be stored somewhere in your stack. Otherwise you&#39;re still going to get the wrong answers.</p><p>But in the right hands, with the right approach and the right context, this thing is going to blow MarTech apart. And it won&#39;t be paywalled behind some SaaS provider. Well apart from Snowflake obvs!</p><p>And this definitely feels like something that deserves some time carving out for more articles. One thing positive I will say for LLMs, they’re a great leveller. You don’t need to be a consultant with decades of experience. Like everything else my 20 year career has taught me — understand the problem you want to solve, then find the most appropriate technical solution. Absolutely nothing has changed on this front!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=310131ba077c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing in Python with Dev Containers — Part 5: Working with PostgreSQL Locally]]></title>
            <link>https://medium.com/@andypickup101/developing-in-python-with-dev-containers-part-5-working-with-postgresql-locally-945538835f0d?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/945538835f0d</guid>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 30 Dec 2024 07:49:15 GMT</pubDate>
            <atom:updated>2024-12-30T07:49:15.126Z</atom:updated>
            <content:encoded><![CDATA[<h4>Setting up PostgreSQL in a local container and ensuring network connectivity with our application’s devcontainer</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*delQidY7zMKE2SSP.png" /><figcaption>From the official Docker guide on using PostgreSQL</figcaption></figure><p>In Part 6 I’ll be working through a TDD refactoring example using data stored in a <strong>PostgreSQL </strong>database. You can either use an instance of <strong>PostgreSQL </strong>you’ve installed locally/in the cloud and skip to the end of this article for the <strong>FastAPI </strong>code or run through the whole article to setup a local container running <strong>PostgreSQL </strong>that we’ll link to <strong>FastAPI</strong>.</p><h4>Setup</h4><p>Hold on to your hats, there’s a lot to setup here and it may not work first time, some things are dependant on how “clean” your machine is and whether you’ve messed with <strong>Docker </strong>settings. If you recall in Part 4 I had just recently rebuilt my laptop, so it’s as clean as it’s likely to get!</p><p>First thing you need to do is make sure you’re out of the devcontainer and just sitting in <strong>WSL</strong>. Hit <strong>Shift+Ctrl+P</strong> and type <strong>wsl</strong> and select <strong>Dev Containers: Reopen Folder in WSL</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*SPx_soEKSkLUzO8qcB9tig.png" /><figcaption>Visual Studio Code — back out to WSL</figcaption></figure><h4>PostgreSQL</h4><p>We need to create a new container running the official <strong>PostgreSQL </strong>image. There are many ways to do this, I’ve opted for the simplest to maximise success while following along but I wouldn’t normally recommend doing it this way as part of your day to day workflow.</p><p>Make sure you’re on your <strong>host</strong> and not inside the devcontainer, and then open the <strong>Terminal </strong>and run these commands in the root of your application folder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/744/1*tDVqqPgCZthjoeU-MoNiSQ.png" /><figcaption>Windows Terminal — file structure setup</figcaption></figure><p>Back in <strong>VSC</strong>, it should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/369/1*wEicn0DN2bi5JFsIU3abNg.png" /><figcaption>Visual Studio Code — file structure</figcaption></figure><p>In the <em>.env</em> file, add these values:</p><pre>POSTGRES_USER=testuser<br>POSTGRES_PASSWORD=testpass<br>POSTGRES_DB=testdb</pre><p>We’re going to push these variables into our <strong>PostgreSQL </strong>image, they’ll be used on initial container creation to get everything up and running.</p><blockquote>For a full list of supported environment variables, refer to the documentation here: <a href="https://hub.docker.com/_/postgres">https://hub.docker.com/_/postgres</a></blockquote><p>In the <em>docker-compose.yml</em> file, add this config:</p><pre>services:<br>  postgres01:<br>    image: postgres<br>    restart: unless-stopped<br>    volumes:<br>      - psql-data:/tmp/dev/pgsql/data<br>    env_file:<br>        - .env<br>    expose:<br>      - &quot;5432&quot;<br>    ports:<br>      - &quot;5432:5432&quot;<br><br>volumes:<br>  psql-data:</pre><p>We’re creating a new service called <strong>postgres01</strong> and pointing it at a new volume (we’ll let <strong>Docker </strong>handle this for now) and then injecting the <em>.env</em> file and setting the default ports.</p><p>Finally, in the <em>migrations.sql</em> file, add these two scripts:</p><pre>-- create the table<br>CREATE TABLE IF NOT EXISTS public.test_user<br>(<br>    user_id integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),<br>    first_name character varying(50) COLLATE pg_catalog.&quot;default&quot; NOT NULL,<br> last_name character varying(50) COLLATE pg_catalog.&quot;default&quot; NULL,<br>    CONSTRAINT test_user_pkey PRIMARY KEY (user_id)<br>);<br><br>-- insert test values<br>INSERT INTO test_user (first_name, last_name) VALUES<br>(&#39;John&#39;, &#39;Doe&#39;),<br>(&#39;Jane&#39;, &#39;Smith&#39;),<br>(&#39;Michael&#39;, &#39;Johnson&#39;),<br>(&#39;Emily&#39;, &#39;Davis&#39;),<br>(&#39;David&#39;, &#39;Brown&#39;),<br>(&#39;Sarah&#39;, &#39;Miller&#39;),<br>(&#39;James&#39;, &#39;Wilson&#39;),<br>(&#39;Jessica&#39;, &#39;Moore&#39;),<br>(&#39;Daniel&#39;, &#39;Taylor&#39;),<br>(&#39;Laura&#39;, &#39;Anderson&#39;);</pre><p>Here we’re creating a new table for our users and inserting a handful of test entries, generated randomly.</p><p>Hit <strong>Shift+Ctrl+P</strong> and type <strong>compose up</strong> and then select <strong>Docker: Compose Up</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/1*UugWTyR74mahUPXPAM8z8g.png" /><figcaption>Visual Studio Code — Docker Compose</figcaption></figure><p>This will spin up a new stack called <em>dbservice</em>, with a container called <em>postgres01–1</em> (or similar):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/799/1*lYBjcdkw1mi0DtAxfM6KKQ.png" /><figcaption>Docker Desktop — the postgres01 service is running</figcaption></figure><p>Click the <strong>options</strong> menu on the container row and select <strong>Open in terminal</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/292/1*GjIyWKv9XDJLQ0tUsCg9Fw.png" /><figcaption>Docker Desktop — container options menu</figcaption></figure><p>Now run this command:</p><pre>export | grep POST</pre><p>This will (hopefully) confirm the injection of <em>.env</em> has worked:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/266/1*use1z3ljoltW0NS2LvDWDQ.png" /><figcaption>Docker Desktop — container terminal output</figcaption></figure><blockquote>If you haven’t already installed pgAdmin, download and install it from here: <a href="https://www.pgadmin.org/">https://www.pgadmin.org/</a></blockquote><p>Open <strong>pgAdmin </strong>and register a new server. You can call this whatever you like, the important part is the <strong>Connection </strong>tab:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*-oth2zjHsO6g9Lv-IW87LA.png" /><figcaption>pgAdmin — new server registration</figcaption></figure><p>Hit <strong>Save </strong>and it should attempt to connect, if not, connect manually. Once connected, you should see that the <em>testdb</em> has been created automatically:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/213/1*qlsp-pJsaPt6sBa0CsRoQQ.png" /><figcaption>pgAdmin — server view</figcaption></figure><p>Select <em>testdb </em>and hit <strong>Alt+Shift+Q</strong> to open a new query window. Paste in the <em>migrations.sql</em> scripts and run them (<strong>F5</strong>).</p><p>To confirm this has worked, run this script:</p><pre>SELECT * FROM test_user;</pre><p>You should see our ten fake users:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*mWUPQALAgbxSh-yM0a4LhA.png" /><figcaption>pgAdmin — results of our select query</figcaption></figure><p>If that was a bit much, don’t worry, I’ll come back to how I use <strong>Docker </strong>in my normal workflow in a later topic. Hopefully it works first time for you, if not, drop a comment and I’ll try to assist.</p><p>If you’re following from Part 4 and have the git repo configured, stage these changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*bXM61-CISF6fF6dYqSPP6A.png" /><figcaption>Visual Studio Code — git staged changes</figcaption></figure><h4>FastAPI</h4><p>Before we can connect to <strong>PostgreSQL </strong>we need to add the <strong>psycopg2</strong> package to our <em>requirements.txt</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/212/1*1FLnci6lVQmXMZaYKFeTVg.png" /><figcaption>Visual Studio Code — requirements.txt</figcaption></figure><p>Then we need to update our <em>devcontainer.env</em> file and add the new connection environment variables:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/332/1*HdQUIib9UnevFLDEf9GREA.png" /><figcaption>Visual Studio Code — devcontainer.env</figcaption></figure><p>In here I tend to intentionally use a different naming convention, just in case I mess anything up and accidentally install <strong>PostgreSQL </strong>in my devcontainer, or it gets bundled into the build during the deploy process. (<em>It happens, right?!</em>).</p><p>For user, password and database, they’re the same values you used in <em>.env</em>. But for the host, we’re using <strong>host.docker.internal</strong>. When <strong>psycopg2 </strong>makes the connection, you’d expect this to be <strong>localhost:5432</strong> but that won’t work, the devcontainer will interpret this as itself. Using this alias tells the devcontainer to go back to our host on port 5432 and then it’ll be passed through to localhost on our host machine.</p><p>I’d like to confidently say this will work for you, but I’ve been using <strong>Docker </strong>for many years now and networking is a total pain! I’m honestly surprised this worked first time for me when writing this. Maybe they finally improved it?! If it doesn’t work, Google is your friend here. You’ll likely need to mess around with bridge networking configs.</p><p>Let’s rebuild our container. Hit <strong>Ctrl+Shift+P</strong> and type <strong>rebuild </strong>and select <strong>Dev Containers: Rebuild Container Without Cache</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*IWy26PlP4AGsh72kYLFt8Q.png" /><figcaption>Visual Studio Code — rebuild the container</figcaption></figure><p>That should spin up quickly and then you can open the <strong>Terminal</strong> and type the same export command as above:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*2Yx8Sz9sg9zdOJxVC0eAAQ.png" /><figcaption>Visual Studio Code — terminal</figcaption></figure><p>Phew!</p><p>Now we need to read these environment variables into our application. I do this in two stages, and I’ll explain why in part 6.</p><p>First I create a <em>settings.py</em> file inside the <em>api </em>folder, I import the <strong>os </strong>package and create a new dict to hold my environment variables:</p><pre>import os<br><br>POSTGRES_CONFIG = {<br>    &quot;host&quot;: os.getenv(&quot;POSTGRESQL_HOST&quot;),<br>    &quot;database&quot;: os.getenv(&quot;POSTGRESQL_DATABASE&quot;),<br>    &quot;user&quot;: os.getenv(&quot;POSTGRESQL_USER&quot;),<br>    &quot;password&quot;: os.getenv(&quot;POSTGRESQL_PASSWORD&quot;)<br>}</pre><p>Then I create a <em>config.py</em> file inside the <em>api </em>folder and create a function that will pull values out of this POSTGRES_CONFIG dict and return them in the format needed for <strong>psycopg2</strong>’s connect function:</p><pre>from api.settings import POSTGRES_CONFIG<br><br>def postgres_config():<br>    return {<br>        &quot;host&quot;: POSTGRES_CONFIG.get(&quot;host&quot;),<br>        &quot;database&quot;: POSTGRES_CONFIG.get(&quot;database&quot;),<br>        &quot;user&quot;: POSTGRES_CONFIG.get(&quot;user&quot;),<br>        &quot;password&quot;: POSTGRES_CONFIG.get(&quot;password&quot;)<br>    }</pre><p>Note: it looks exactly the same as the settings structure, but you’ll see an example later on how you can customise at this level.</p><p>Stage these changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/353/1*02yK3g9ZdpOLCGYxblUDNA.png" /><figcaption>Visual Studio Code — git staged changes</figcaption></figure><p>Now let’s test a basic select query, called via a route. To do this we need to add some imports at the top of our<em> main.py</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*YJrSwUyk3o-V8J1eXFJhMA.png" /><figcaption>Visual Studio Code — add some imports to main.py</figcaption></figure><p>Here we’re importing our config function and some <strong>psycopg2 </strong>packages. Let’s create a new route:</p><pre>@app.get(&quot;/test_sql_query&quot;)<br>def test_sql_query():<br>    conn = None<br>    try:<br>        params = postgres_config()<br>        conn = psycopg2.connect(**params)<br>        cur = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)<br>        sql_stmt = f&quot;SELECT * FROM test_user;&quot;<br>        cur.execute(sql_stmt)<br>        results = cur.fetchall()<br>        cur.close()<br>        conn.close()<br>        return {&quot;results&quot;: results}<br>    except (Exception, psycopg2.DatabaseError) as error:<br>        if conn is not None:<br>            conn.close()<br>        return {&quot;error&quot;: str(error)}<br>    finally:<br>        if conn is not None:<br>            conn.close()</pre><p>We’ve added a simple try/except/finally block to handle failure, we’ve pulled the return value of postgres_config() into our params object, pushed this into the pscyopg2.connect() function, created a cursor, created a sql statement and then executed it and fetched the results. Then we clean up and return the results.</p><p>In your browser, call the route: <a href="http://127.0.0.1:8000/test_sql_query">http://127.0.0.1:8000/test_sql_query</a></p><p>It should return the user data in JSON format:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/262/1*JJqRQB3EqPONVta0yzU1rA.png" /><figcaption>Browser — results</figcaption></figure><h4>Bonus</h4><p>If you’re interested, add some breakpoints to the code and let’s take a look at what’s happening under the hood here:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*M1RglXcW1S7qlmEuYWxDoA.png" /><figcaption>Visual Studio Code — breakpoints added</figcaption></figure><p>Pause on line 40 and look at our <em>params </em>object in the debug window:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/362/1*_marja_pQD5RCHml40zNSg.png" /><figcaption>Visual Studio Code — debug window — params</figcaption></figure><p>Pause on line 41 and look at how our <em>conn </em>object is updated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*7Z7CQsVeQ4yqcmqnwcuPYw.png" /><figcaption>Visual Studio Code — psycopg2 connection</figcaption></figure><p>If you jump over to <strong>pgAdmin </strong>and take a look at the <strong>State </strong>tab in the <strong>Dashboard </strong>window you should see our new connection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*XZbSXYFlhZkdPWJ0oEUisg.png" /><figcaption>pgAdmin — our app is connected (bottom line)</figcaption></figure><p>Note: you can add an extra param to our <em>config.py</em> file to pass in our <em>application_name </em>as part of the connection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*zuyk4zakWl7fD03fOCM5BA.png" /><figcaption>Visual Studio Code — updated config</figcaption></figure><p>And in <strong>pgAdmin </strong>this is now much clearer:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*e1s0F6A21tCNvE2pE0iYqg.png" /><figcaption>pgAdmin — updated application name</figcaption></figure><p>Pause on line 44, look at the <em>cur </em>object and you can see it’s linked to the <em>conn </em>object and it’s executed the <em>sql_stmt </em>statement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*sd25PJk6yuUG_i7K3GC5iw.png" /><figcaption>Visual Studio Code — cursor debug</figcaption></figure><p>And in <strong>pgAdmin</strong>, we can see our connection shows the query being executed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kd-l38ORipwM1OCh2ttjPg.png" /><figcaption>pgAdmin — running query</figcaption></figure><p>Pause on line 45 and look at the <em>results </em>object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/662/1*xx_hWzjR7jLwnlGROUdiYA.png" /><figcaption>Visual Studio Code — results debug</figcaption></figure><p>Because we’re using the <strong>psycopg2.extras </strong>package when we create our cursor, we’re able to specify that we want the data returned as an <em>OrderedDict </em>i.e. with column headings.</p><p>If you don’t specify the <em>cursor_factory</em>, you just get a list of results without the column headings:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*nAq23WDX0GEXwV5E97SZxg.png" /><figcaption>Visual Studio Code — results without headings</figcaption></figure><p>That makes actually using the data much harder and at risk of the underlying query or table structure being changed and breaking your application:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/210/1*D9SP_mUx6UHFkuRHKCPE2Q.png" /><figcaption>Visual Studio Code — two options for working with data</figcaption></figure><p>You’ll also need to manually format the data before you return it, otherwise it looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/151/1*sLBLtOmu_xbrobKFNSeB_g.png" /><figcaption>Browser — without headings</figcaption></figure><p>Then finally just for fun, change <em>config.py</em> and swap the username to something random and call the route again. You can see the kind of error likely to be generated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*4w6AFPUQxHdnuhpbD1kDhQ.png" /><figcaption>Visual Studio Code — database connection error</figcaption></figure><p>This is useful when you’re trying to debug why this doesn’t work.</p><p>Stage your changes and commit everything:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*yowfEokBNBU1D3WmbauZqQ.png" /><figcaption>Visual Studio Code — changes committed</figcaption></figure><p>If you’re ending here, remember to stop the <em>postgres01</em> container in <strong>Docker Desktop</strong> manually, it won’t happen when you close VSC like the devcontainer does.</p><blockquote>Ok, with all the prep work out of the way you’re now ready to move on to Part 6, which will be coming ASAP! In the meantime, like, clap, subscribe, comment, however it works here, do that, it’s always appreciated when I get the notifications. Thanks!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=945538835f0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing in Python with Dev Containers — Part 4: Installing WSL, Ubuntu and Docker]]></title>
            <link>https://medium.com/@andypickup101/developing-in-python-with-dev-containers-part-4-installing-wsl-ubuntu-and-docker-c3b920b1f9de?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c3b920b1f9de</guid>
            <category><![CDATA[ubuntu]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[wsl]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devcontainer]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Tue, 29 Oct 2024 13:00:04 GMT</pubDate>
            <atom:updated>2024-10-29T13:02:32.696Z</atom:updated>
            <content:encoded><![CDATA[<h3>Developing in Python with Dev Containers — Part 4: Installing WSL, Ubuntu and Docker</h3><h4>Setting up a fresh installation of Windows with WSL, Ubuntu and Docker for use with dev containers in Visual Studio Code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bw8IvADI8TBWh-3dKbt1Ng.jpeg" /><figcaption>AI generated illustration of containerisation. Hmmm…</figcaption></figure><p>As “luck” would have it I’ve had to factory reset my work laptop. A windows update borked the USB4 Root Router (I think) which tanked the speeds of all of my USB ports, making using a dock impossible. The whole system would stutter, you couldn’t reliably use video or audio. It sucked. Thanks Microsoft!</p><p>I tried everything, uninstalling the updates, re-installing them, patching, focusing on specific drivers, just wasn’t having any of it. And of course, no mention of issues in the KB. So factory reset was my nuclear option.</p><p>But, this has given me an opportunity to run through the latest method of installing WSL on a fresh laptop. Because I always forget to document it! I’ll also cover Docker, Visual Studio Code and GitHub setup and integration. I didn’t really cover these in <a href="https://andypickup.com/developing-in-python-with-dev-containers-part-1-setup-f1aeb89cbfed">Part 1</a>.</p><h4>WSL + Ubuntu</h4><p>Windows Terminal (with the tabs) now seems to be the default which is nice. Run this as <strong>administrator </strong>and use a <strong>Powershell </strong>tab. Then run:</p><pre>wsl --install</pre><p>By default this will install <strong>Ubuntu</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*thMICV6-sEqJ7n7mTrPkCQ.png" /><figcaption>Windows Terminal post WSL installation</figcaption></figure><p>You’ll need to reboot, and then it’ll finish the <strong>Ubuntu </strong>installation.</p><p>However in my case this didn’t actually install <strong>Ubuntu </strong>like it should’ve done. If you run this:</p><pre>wsl -l -v</pre><p>And you see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*kV7q3NcMQ-wRQ9D8rIXu6g.png" /><figcaption>Windows Terminal — error with default WSL distro</figcaption></figure><p>Then run this:</p><pre>wsl --list --online</pre><p>And you should see something similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*p3CZooce752aMmTihUNEtA.png" /><figcaption>Windows Terminal — list of available Linux distros on WSL</figcaption></figure><p>Now you can run this:</p><pre>wsl --install -d Ubuntu-24.04</pre><p>And reboot again.</p><p>Open your Terminal again, open a new tab and select <strong>Ubuntu 24.04.1 LTS</strong> (or whichever distro you installed):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/283/1*T_tbL3Wel40DILZ1m1n1YQ.png" /><figcaption>Windows Terminal — Add Tab context menu</figcaption></figure><p>This should start the installation, enter a username and a password and it should finish successfully.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1023/1*dT6aZtbByECMmtquie91lA.png" /><figcaption>Windows Terminal — Ubuntu Installation complete</figcaption></figure><p>Now when you go back to <strong>Powershell </strong>and run:</p><pre>wsl -l -v</pre><p>You should see this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/398/1*2SeoIOhEHB-amP_Tj5Q5hg.png" /><figcaption>Windows Terminal — WSL 2 installed and detected</figcaption></figure><p>At this point I immediately install the updates, and you should make sure this is done regularly. Run the following command and enter Y when prompted:</p><pre>sudo apt update &amp;&amp; sudo apt upgrade</pre><p>Next I go to <strong>Settings </strong>&gt; <strong>Startup </strong>&gt; <strong>Default profile</strong> and set this to <strong>Ubuntu</strong>, just because I mostly only work in WSL. If I do use <strong>Powershell </strong>I use either <strong>VSC </strong>or <strong>ISE</strong>.</p><h4>Docker</h4><p>Now we can install <strong>Docker</strong>. Download from <a href="https://docs.docker.com/desktop/install/windows-install/">here</a> or if this link stops working, head to the <strong>Docker</strong> website and download it manually.</p><p>When the installer launches, make sure “<em>Use WSL 2 instead of Hyper-V (recommended)</em>” is selected:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*dzfBqN3NyPK-lPUtB6Kh2A.png" /><figcaption>Docker Installation — Configuration</figcaption></figure><p>Once installed, you’ll need to reboot. Again. Sigh.</p><p>After the reboot, <strong>Docker </strong>should auto start. Accept the service agreement and sign in.</p><p>Open the Terminal again, it should start in WSL and type <em>docker</em>. All being well that’s working perfectly first time. If not, let me know and I’ll try to help.</p><p>We’ll pause here with Docker, other stuff will come up in later parts when we deal with networking between services.</p><h4>Visual Studio Code</h4><p>Super quick, download from <a href="https://code.visualstudio.com/">here</a>. I enabled both of the <em>Add “Open with Code” actions</em>, but otherwise left everything as default.</p><p>When VSC first starts it’ll detect WSL and ask if you want to install the extensions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*kPSBI9dtmBTD3OVqS67N5w.png" /><figcaption>Visual Studio Code — WSL extension prompt</figcaption></figure><p>Open the Terminal, create and navigate into a folder and run “<em>code .</em>”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o4IqjtqlwrgHH2QaiEMaFw.png" /><figcaption>Windows Terminal — VSC Integration installation</figcaption></figure><p>This will open VSC within a WSL session. As you’ll recall from Part 1, this forms the basis of the dev container setup workflow.</p><h4>Git + GitHub</h4><p>Before you begin, make sure you’re signed in to <strong>GitHub </strong>in a browser, it makes the next steps quicker.</p><p>Download Git for Windows from <a href="https://git-scm.com/downloads/win">here</a>. Follow the installation through, I made the following selections:</p><ul><li>Added daily updates check</li><li>Added Git bash profile to Terminal — not that I ever use it, but it seemed a cool feature!</li><li>Set the default editor to Visual Studio Code</li><li>Set the default branch to “main”</li></ul><p>I left everything else as default.</p><p>Back in VSC, I install the <strong>GitHub Pull Requests</strong> extension and then in my VSC Accounts menu, I sign in to <strong>GitHub</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*QBCBsE4S6VHAH5YqYH7bcA.png" /><figcaption>Visual Studio Code — Accounts Menu</figcaption></figure><p>This extension is now multi functional, so the original <strong>GitHub </strong>extension has been deprecated. Would be clearer if they renamed it!</p><p>Open the VSC Terminal and configure your default Git settings:</p><pre>$ git config --global user.name &quot;John Doe&quot;<br>$ git config --global user.email johndoe@example.com<br>$ git config --global init.defaultBranch main</pre><p>Note, I ticked the box during installation to handle the default branch, but it didn’t work so I’m running the command manually in WSL. Now you can clone repos, make changes and push them back to <strong>GitHub</strong>.</p><h4>Progress so far…</h4><p>I’m going to quickly re-hash through parts 1–3 to ensure we’re fully set up as expected.</p><ul><li>I created a new folder structure and opened VSC:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*Hngv3Z7UCDfQwmnf2RJS2w.png" /><figcaption>Windows Terminal — creating folders</figcaption></figure><ul><li>I opened a VSC Terminal and ran git init:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*0Nz6X1XseHe4rFMJeRfcxw.png" /><figcaption>Visual Studio Code Terminal — Init git</figcaption></figure><ul><li>I installed the <strong>Remote Development</strong> extension pack and added the dev container config for <strong>Python 3.12</strong>.</li><li>I updated the devcontainer.json file and added post-create.sh and both requirements files:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*vbKQql8r_j5dnSl0cv1Pyg.png" /><figcaption>Visual Studio Code Terminal — new files added</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*pA8vaY8Ucy8vtV9kDa11CQ.png" /><figcaption>Visual Studio Code — Folder Structure</figcaption></figure><ul><li>I reopened in the container</li></ul><p>That loaded up without any issues! Let’s commit our changes to git using the <strong>Source Control</strong> panel in VSC:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*MAzwhBp6D7jeE_oPVW-6WA.png" /><figcaption>Visual Studio Code — Source Control</figcaption></figure><p>I’ll dig into this more in a future part, but I tend to <strong>Stage </strong>&gt; <strong>Commit </strong>&gt; <strong>Publish</strong>. I’ll also usually have pre-commit hooks, but we’ll get to those!</p><p>When you publish for the first time it asks for a public or private repo, you can customise the name of the repo here, otherwise it will default to the folder name you’re working in:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*rj9VLBStkQLV1NwTf1tfNA.png" /><figcaption>Visual Studio Code — repo selection</figcaption></figure><p>It will then pops up a window asking for <strong>Sign in to GitHub</strong>. Here I switch to <strong>Token </strong>and use my <strong>Personal Access Token</strong>, which eliminates the need to keep doing this. Tokens expire and I just refresh when needed. I may cover this in more detail in another part.</p><p>Over in <strong>GitHub </strong>you’ll see the new repo has been created (I left this as the default folder name):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*n_EfUOjTMjZkql7D0rliIw.png" /><figcaption>GitHub — new repo added</figcaption></figure><ul><li>Next I added <strong>FastAPI </strong>and the first <em>main.py</em> file, ran <strong>uvicorn</strong> and tested in the browser:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/155/1*4q6XtSEXSE7DQojeCxO78A.png" /><figcaption>Browser response</figcaption></figure><ul><li>Then I added the debug config files, updated the rest of the code from <a href="https://andypickup.com/developing-in-python-with-dev-containers-part-2-a-simple-fastapi-project-with-step-debugging-e52599b7ce61">part 2</a> and tested it. Looks good:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*c1rP_q-wiaqkxkRxy3DQ6w.png" /><figcaption>Browser response updated</figcaption></figure><ul><li>It’s the end of part 2 so I committed my code changes to git:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*7RTsZXPEfViKOo9xUuw0NQ.png" /><figcaption>Visual Studio Code — commit changes</figcaption></figure><p>But wait, what’s this <em>__pycache__</em> file? Well in <a href="https://andypickup.com/developing-in-python-with-dev-containers-part-3-a-simple-guide-to-tdd-with-pytest-and-fastapi-6dd11fb1f1f4">part 3</a> we looked at how we can use a variable to suppress these, but if you do want to keep them and not commit them to git, that’s where the <em>.gitignore</em> file can be really useful. I’m going to prep for the rest of part 3 here, so add the new <em>.gitignore</em> file at the root of the project and add these lines:</p><pre>.devcontainer/*.env<br>.devcontainer/env-vars*<br><br>__pycache__<br>__pycache__/*<br>.pytest_cache<br>.pytest_cache/*<br><br>.coverage<br>coverage.xml</pre><p>Save and see what we’re now committing to git:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*tYBDw31qdqvJPKaFQpOmKA.png" /><figcaption>Visual Studio Code — updated changes</figcaption></figure><ul><li><strong>Stage </strong>&gt; <strong>Commit </strong>&gt; <strong>Sync</strong></li><li>Running through part 3 I updated the config, added the new folder structure, added the pytest files and rebuilt the container.</li><li>Then I setup the Test Configuration. I got an error here, <strong>httpx</strong> wasn’t installed because I didn’t add it to requirements and the container rebuild dropped it. I re-installed it, and added it to requirements.txt. I’ve added this to the comments in part 3.</li><li>I followed the rest of the steps through until the section where I need to add a complicated config to launch.json to handle the updates to coverage when step debugging. I read recently in a VSC release note that this had finally been fixed and I’m pleased to see that it actually works first time! Shocker!</li></ul><p>At the end of part 3, I tested what I’d done so far using the FastAPI TestClient however it looks like I missed the setup of this in part 3. I’ve added this as a comment but I’ll include it here also. Update conftest.py:</p><pre>import pytest<br><br>from api.main import app as api_app<br>from fastapi.testclient import TestClient<br><br>@pytest.fixture()<br>def app():<br>    app = api_app<br>    return app<br><br>@pytest.fixture()<br>def client():<br>    client = TestClient(api_app)<br>    yield client</pre><p>Now the test works as intended. Oops!</p><ul><li><strong>Stage </strong>&gt; <strong>Commit </strong>&gt; <strong>Sync </strong>all of this code:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*BfsjusuddLbe3ujzlxFJfA.png" /><figcaption>Visual Studio Code — Part 3 completed!</figcaption></figure><ul><li>Check <strong>GitHub </strong>commit history:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/403/1*zICZ1v101NpZKJzP67_kmw.png" /><figcaption>GitHub — commit history</figcaption></figure><p>I should work on my commit names, this is what happens when you’re rushing!</p><blockquote>With any luck, that has worked first time for everyone and if you’ve come to this tutorial from a blank Windows environment, this should you get you up and running in no time and you’ll be ready to develop using FastAPI, TDD and Dev Containers running on WSL and Docker like a pro! As always, if you have any questions, drop them in the comments below and I’ll answer them as quickly as I can.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c3b920b1f9de" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[So, I’m a CTO now!]]></title>
            <link>https://medium.com/@andypickup101/so-im-a-cto-now-ff24165ce894?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/ff24165ce894</guid>
            <category><![CDATA[game-development]]></category>
            <category><![CDATA[cto]]></category>
            <category><![CDATA[unity]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 26 Aug 2024 17:32:41 GMT</pubDate>
            <atom:updated>2024-08-26T17:32:41.557Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CMbcqfazWVhoaS8w47M_Kw.jpeg" /><figcaption>A gamer CTO, generated from an image of myself!</figcaption></figure><p>Somewhat unexpectedly, and coming at the cost of having any free time to keep blogging, I took on a new role as a CTO. For a freakin’ games company no less!</p><h4>Python Update</h4><p>Before we get into it, a quick note, more Python content will be coming soon. I have another 10 articles already written, on a variety of topics, I just need to test them and screenshot them and start the public GitHub repo. That’s the time consuming bit, I just don’t want to (or can’t) stick my name on sub par content.</p><h4>Life as a CTO</h4><p>Ok so first off it’s not a “real” CTO role. Maybe that’s a surprising admission to see online, but this is a company I’m helping start and the title has been given, and as yet it’s unpaid. Maybe once our first game is launched and the first coins can be collected, it’ll qualify as “real”.</p><p>Further, we’re trying hard to not really focus on job titles and hierarchy, preferring self organisation instead. That said, the company has grown rapidly in an incredibly short amount of time. There are over 30 of us now I believe. I feel an element of responsibility to not screw this up for everyone involved, make it worth their time, to really try and make it a success.</p><p><strong>I have learned things.</strong></p><p>I got into this because, as you’ll no doubt have gathered by this blog, I have a lot of knowledge in my head that I want to give out to the tech community. I have a great ability to research information, disseminate it, contextualise it, pass it on to others. I wanted to take that a step further and use this role as a way of mentoring younger talent.</p><p>I’ve recruited a lot over the last 20 years and I see the same patterns over and over. Younger candidates come out of uni and have a basic grasp of the tech but near zero ability to action it at a production level. That’s a failing of modern educational institutions by the way. I went through a degree process a few years ago, not even close enough to the real world to be useful. I quit.</p><p>I wanted to apply everything I’ve learnt through my career about project management, product development, coding, pragmatism, passion, communication, the things that matter more than being a 10x developer, beating out toxicity before it settles in etc. I wanted to ensure these candidates can get themselves onto the path to success early.</p><blockquote><strong>And in doing so, I’m also learning myself.</strong></blockquote><p>I’ve learned that I can’t do everything as CTO. I absolutely cannot do hands on coding while juggling everything else, that’s for sure. We’re coding in Unity and I’ve been using C# for the first time in over a decade and I absolutely love it all over again. But it’s a total time sink and I need to back off. I need to advise, to guide, to not do.</p><p>The same applies to all the other tasks above. I need to adopt the role of overseer. Originally I intended to be hands on, or certainly expected to have to be, but there are enough heads now that we can distribute more of this work around the team. So we will.</p><p>I have learned the importance of not only making assertive decisions in product design, but of the importance of actually communicating those, and being democratic about it, to a point.</p><p>I have learned that people aren’t naturally organised. Most need a gentle poke. And most need to be told the importance of communication. Working productively entirely remotely isn’t something that just happens.</p><p>I have learned that death by meeting is a thing. Ok, I already knew that one!</p><h4>SitRep</h4><p>It’s been a busy few months. We’ve got three games in active development right now. Two mobile games and a board game. Then we have another board game, a PC game and a mobile game in the early pipeline stage. And then I have about 9000 ideas for what to do next.</p><p>One of the first decisions I made was to build something we all know is probably a bit rubbish, and probably won’t do well. Which sounds counter intuitive, and certainly not in line with my earlier statement on quality. So why?</p><p>I want to ensure the team knows very early on that we can deliver something end to end. It’s too easy to start things and not finish them. To lose track. To go down the rabbit hole, get bored and give up. So we need to build something quickly, to a decent enough standard, and then take the learnings and apply them to something better.</p><p>The risk in trying to be perfect straight out the gate is that we’ll quickly unravel and lose motivation, and momentum.</p><p>To that end I made sure we got a prototype build out and uploaded to the Play Store within the first couple of weeks. It was important that we had something tangible in our hands. It makes it more real. It’s not just another unfinished project in GitHub.</p><p>To follow, I then set the OKR’s. Prototype complete by end of August (on track) and full product finished and ready for testing and marketing by end of September (on track). I expect we’ll release it mid to end of October.</p><h4>How did this grow so fast?</h4><p>Honestly I haven’t a clue. It’s hit a nerve though. And that gives me confidence that it might just be the right thing to do. Something that people actually want. Something different. Bringing fun and joy back to game development.</p><p>Either that or it’s because the games industry is on its knees and people are looking out for anything that might pay and are willing to take a punt on our silly little idea!</p><h4>Want to know more?</h4><p>Check out our website: <a href="https://www.gamecrafterscollective.com/">https://www.gamecrafterscollective.com/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ff24165ce894" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Potential Impact of Microsoft’s Leeds based Hyperscale Azure AI Data Centre]]></title>
            <link>https://medium.com/@andypickup101/the-potential-impact-of-microsofts-leeds-based-hyperscale-azure-ai-data-centre-f841ec0cfa86?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f841ec0cfa86</guid>
            <category><![CDATA[data-center]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[microsoft]]></category>
            <category><![CDATA[environment]]></category>
            <category><![CDATA[sustainability]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Tue, 09 Jul 2024 10:42:59 GMT</pubDate>
            <atom:updated>2024-07-09T10:42:59.707Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yKP6tbtxAQMgnu2fFEalQQ.jpeg" /><figcaption>Image generated with Microsoft Designer. Irony fully noted!</figcaption></figure><p>The news of a new Microsoft <em>hyperscale</em> Azure AI data centre landing at Skelton Grange, here in Leeds, has sent a wave of anticipation through the city. While visions of economic prosperity sound fantastic, I think a healthy dose of scepticism is essential.</p><p>Modern, AI-focused data centres present a double-edged sword, promising both economic benefits and environmental challenges and I’m keen to delve deeper into the proposition to understand — is it all too good to be true?</p><p>In this article I discuss the following areas of concern:</p><ul><li>Economic Prospects</li><li>Social Concerns</li><li>Environmental Impact</li><li>Sustainability Options</li></ul><h4><strong>Economic Boom on the Horizon?</strong></h4><p>The potential economic windfall is undeniable. The construction phase itself will create a flurry of jobs, and the ongoing operation of the data centre will necessitate a skilled workforce. This will be a significant boost for the Leeds economy, potentially attracting further investment and really establishing the city as a northern tech hub. So it has my vote!</p><p>However, as with all these big project announcements, there are crucial questions to be answered. Does Leeds currently possess the necessary talent pool to capitalise on this opportunity? If not, the initial wave of jobs could go to external recruits, resulting in little or notably reduced benefit to the local economy. For those engineers currently located in Leeds who choose to move to Microsoft, what’s the expected impact an existing local businesses and how will those roles be backfilled? Will there be too much <em>brain drain</em>?</p><p>Or, is <em>brain gain </em>a more likely outcome? The data centre could act as a magnet, attracting AI specialists and prompting universities to adapt their programs to meet the evolving landscape. Over time, Leeds could cultivate its own homegrown AI talent, ensuring long-term benefits. We’ve seen this at a national level in the games industry, but it’s not a given. We’ve seen time and time again that Leeds doesn’t always get the opportunity to step up and make good on promises made by the money men!</p><h4><strong>Social Considerations</strong></h4><p>The potential impact on the Leeds housing market shouldn’t be ignored. Data centres are unlikely to operate remotely (ironically). A sudden influx of workers into the Leeds area could push property prices upwards, making it more difficult for locals currently struggling to get onto the housing ladder.</p><p>This necessitates proactive measures from local authorities, such as long-term housing development plans, to ensure the city’s growth is inclusive. The recent change in the UK government may facilitate this of course, but it’s worth remembering that the current definition of “affordable” housing is already out of reach for a lot of people in Leeds. Will this project help solve this problem or worsen it?</p><h4><strong>The Environmental Cloud</strong></h4><p>The environmental impact of <em>standard</em> data centres and their notorious energy consumption is a major concern. AI data centres are no exception and should raise anxieties around the strain they may place on Leeds’ existing water and electricity infrastructure.</p><p>I think transparency is probably key here. With Microsoft’s commitment to building sustainable data centres, it should openly communicate its energy usage plans and collaborate with local authorities. A focus on renewable energy sources and energy-efficient cooling systems is paramount.</p><p>It might also be necessary to further invest in Leeds’ water and electricity infrastructure to handle any increased demand. As climate change worsens, we’ll see more frequent drought restrictions being enforced on local communities. How will data centres be regulated and restricted from consuming our locally generated resources and artificially exacerbating the need for blackouts for local residents?</p><h4><strong>A Collaborative Approach to a Sustainable Future</strong></h4><p>Leeds has a golden opportunity to leverage this development for a positive and sustainable future. This requires a collaborative effort and Microsoft, local authorities, and residents must engage in open discussions to ensure the data centre integrates seamlessly with the city’s existing infrastructure.</p><p>Potential solutions include:</p><ul><li><strong>Renewable Energy </strong>— rely entirely on solar or wind to reduce the strain on the grid and avoid droughts. We certainly get enough wind up here! Not so much sun…</li><li><strong>Water-Efficient Cooling </strong>— further investment in air cooling and liquid immersion cooling should help reduce water consumption. Microsoft is clearly committed to Net Zero, but, it needs to happen before the data centre opens, not 2030 — a target that’s likely to move before it arrives. Again.</li><li><strong>Local Grid Integration </strong>— feed excess energy generation back into the local grid, divert excess heat and water into local cities. If memory serves this was trialled in Sydney about a decade ago with relative success.</li><li><strong>Localised Net Zero </strong>— this absolutely must be a localised initiative. Microsoft shouldn’t be allowed to consume local resources and call it Net Zero by building a wind farm in Scotland (for example). The data centre must have a positive impact locally, it can’t just be another thinly disguised vehicle for corporate gain.</li></ul><p>I’m sure the clever folks at Leeds City Council considered the proposal from all angles before approving it. It’ll be fine.</p><h4><strong>Summary — Is It All Too Good To Be True?</strong></h4><p>Honestly, I have no idea. I’m no expert!</p><p>While the tech industry is acutely aware of the environmental cost of data centres, and companies like Microsoft are constantly innovating and exploring complete reliance on renewable energy sources, this is an ongoing race. AI continues to evolve, data centre design must also evolve to minimize environmental footprint.</p><p>You’d think AI would be able to solve many of the world’s problems, including climate change and sustainability. I just think many businesses don’t want it to. All they see is a need for rapid deployment and one-upmanship to improve their bottom lines. It’s the same story being repeated over and over. I wrote about greenwashing for my Business Studies A-Level back in 1998, nothing’s changed.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f841ec0cfa86" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing in Python with Dev Containers — Part 3: A Simple Guide to TDD with pytest and FastAPI]]></title>
            <link>https://medium.com/@andypickup101/developing-in-python-with-dev-containers-part-3-a-simple-guide-to-tdd-with-pytest-and-fastapi-6dd11fb1f1f4?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/6dd11fb1f1f4</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[devcontainer]]></category>
            <category><![CDATA[fastapi]]></category>
            <category><![CDATA[tdd]]></category>
            <category><![CDATA[pytest]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 10:24:35 GMT</pubDate>
            <atom:updated>2024-06-11T10:24:35.349Z</atom:updated>
            <content:encoded><![CDATA[<h3>Developing in Python with Dev Containers — Part 3: A Simple Guide to TDD with pytest and FastAPI</h3><h4>Getting started with Test Driven Development (TDD) in Visual Studio Code, FastAPI and pytest</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u_eTcW8vJ_7NRSC4fFdVsg.jpeg" /><figcaption>Another really bad Microsoft Designer generated image for Python and testing!</figcaption></figure><p><strong>What is TDD?</strong></p><p>That’s an entire post in itself, but to summarise you could say it’s essentially just an approach to development whereby you bring the testing phase forward as much as you can — instead of writing all your code first then wrapping minimal tests around it and shipping.</p><p>The more I work in this way, the more I start to think about making my code as simple as possible. And then the more I return to my code to make changes, the more I think about keeping those changes simple and isolating them.</p><p>From what I’ve seen over the years, the resistance to TDD is less about TDD itself and more about not being able to apply it easily to existing code. I read a great statement recently that said something like…</p><blockquote><strong>If you can’t use TDD on your code, your code is the problem!</strong></blockquote><p>In a later article I’ll be discussing red/green refactoring and digging into that statement a little more and it’ll start to make sense. But, if you can start thinking about using it on greenfield projects, future you will thank current you!</p><p><strong>My Previous Approach</strong></p><p>I’ve always been a fan of testing, albeit pretty manually. In the past I’ve usually created a dev environment and spun up a dev database into which I’ve manually entered my test data into the tables I know I’m consuming in my code. Then I’ve used step debugging through my code to make sure it’s right. Then I’ve deleted that record and created a new one for the next scenario and repeat and repeat and repeat! And same for any third party APIs, maybe even using a Postman Mock Server too.</p><p><em>That’s not very productive and it’s wide open to errors and difficult to repeat the next time you change the code! Who wants to spend 30 minutes testing a 5 minute change?!</em></p><p><strong>My New Approach</strong></p><p>For the last few months I’ve been refactoring a Python codebase and I’ve needed to do it quickly and without breaking anything (!) so I’ve taken the opportunity to get familiar with <strong>pytest</strong> and … oh it’s wonderful! That might be pushing it, it’s not the easiest thing to use, but switching my thinking around to a testing mindset <strong>before</strong> I make all my changes has made this project significantly less painful.</p><p>For the rest of this article I’ll walk you through getting up and running with the basics of <strong>pytest</strong>. I’ll extend this in a future article to really get into the nitty gritty of refactoring your code and the differences between unit testing and integration testing.</p><p><strong>Setup</strong></p><ol><li>Environment Files</li></ol><p>In the previous parts of this tutorial, once you started to run your code you’ll likely have noticed the project folder structure getting littered with some __pycache __ folders at various levels. It’s personal preference whether you leave these or not, but they annoy me when I’m developing and I get tripped up by changes that haven’t updated in the cache, so I globally disable them.</p><p>To do this, create a new file in <em>.devcontainer </em>called <em>devcontainer.env:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*JoZ20NvWP12uKLczNZmBRw.png" /><figcaption>VSCode — .devcontainer folder</figcaption></figure><p>Add these two environment variables to this file:</p><pre>PYTHONDONTWRITEBYTECODE=1<br>PYTHONBUFFERED=1</pre><p>To ensure these are created when we spin up our dev container, we need to inject this file into the dev container at runtime via our <em>devcontainer.json</em> file. Add the following <strong>runArgs </strong>line:</p><pre>{<br> &quot;name&quot;: &quot;Python 3&quot;,<br> &quot;runArgs&quot;: [&quot;--env-file&quot;,&quot;.devcontainer/devcontainer.env&quot;],<br> &quot;image&quot;: &quot;mcr.microsoft.com/devcontainers/python:1-3.12-bullseye&quot;,<br> &quot;postCreateCommand&quot;: &quot;bash .devcontainer/post-create.sh&quot;<br>}</pre><p>Save this file and if you get a prompt to rebuild the container, just ignore that for now.</p><p>2. Project Restructure</p><p>It’s good practice to separate your application files from the rest of your project configuration files (you’ll see how this expands later in this series), including our TDD files.</p><p>Create two folders in the root folder called <em>api</em> and <em>tests</em> and move <em>main.py</em> inside the <em>api</em> folder. Inside the <em>tests</em> folder, create a <em>__init__.py</em> file and a <em>conftest.py</em> file.</p><pre>mkdir api tests<br>mv main.py api/<br>cd tests/<br>touch __init__.py conftest.py</pre><p>Your folder structure should now look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*FPhcRP1_jFUpYC_ACg9lUQ.png" /><figcaption>VSCode — updated project structure with application code isolated from everything else</figcaption></figure><p>3. pytest Configuration</p><p>Add the following code to our new <em>conftest.py</em> file:</p><pre>import pytest<br><br>from api.main import app as api_app<br><br>@pytest.fixture()<br>def app():<br>    app = api_app<br>    return app</pre><p>This will come in handy later, don’t worry about it for now!</p><p>In order to start working with tests, we need to install some useful <strong>pytest </strong>packages.</p><p><strong><em>Important</em></strong><em>: Testing is a development practice and you should isolate any development related packages away from the main application packages. This is why we created a requirements-dev.txt file right at the start of this series. Typically you would only use this file locally and as part of your CI/CD pipeline. These development packages should </em><strong><em>NEVER </em></strong><em>end up in your production environment!</em></p><p>Edit <em>requirements-dev.txt</em> and add these two packages:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/232/1*w0hjYXRepBdtdslvoLJwGQ.png" /><figcaption>VSCode — pytest packages added to requirements-dev.txt</figcaption></figure><p>Now you can rebuild your container — press <strong>Ctrl+Shift+P</strong> and type <em>rebuild </em>and then select <strong>Rebuild Container Without Cache</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*Td4WiYOU498N4eDiuy0PNQ.png" /><figcaption>VSCode — dev container rebuild options</figcaption></figure><p>4. VSCode Test Suite Configuration</p><p>You can test entirely manually (I’ll cover that below) but this series is specifically about using VSCode so we included a number of Microsoft Python extensions in our dev container customisations config, one of which enables the <strong>Test Explorer</strong> extension:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/386/1*-BMGrcSyfuDRg9BWbtsxTA.png" /><figcaption>VSCode — Testing extension</figcaption></figure><p>Click <strong>Configure Python Tests</strong> and then in the command bar select <strong>pytest</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*kKtJAbncTTr3blCozN5iRw.png" /><figcaption>VSCode — supported Python testing frameworks</figcaption></figure><p>It’ll ask where your tests are going to be placed, we need to point this at our new <em>tests</em> folder:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*L0TMpYHBN01RxVYbk4sulw.png" /><figcaption>VSCode — test folder configuration</figcaption></figure><p>The <strong>Test Explorer</strong> extension should now have updated to your project folder name:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/383/1*rTxZpNPQn6faA1UnOqP55w.png" /></figure><p><em>In my case, this is pydcpt03 because I’ve split the code files out for this tutorial to preserve them. I’ll be adding to a git repo to support this tutorial in a future part!</em></p><p>Ok, so that’s the bulk of the configuration done but before we get into testing FastAPI directly, let’s just make sure basic Python testing is working.</p><p><em>I always like to tick off the basics before jumping into the deeper work. It can be pretty frustrating thinking something isn’t working and it’s a complex fix, only to realise 4 hours later it was actually easily solved by ticking off these basics!</em></p><p>Inside our <em>api</em> folder, create a <em>functions.py</em> file and add this code to it:</p><pre>def check_number(value: int):<br>    try:<br>        output = 0<br>        if value % 2 == 0:<br>            output += 1<br><br>        if value % 100 == 0:<br>            output += 2<br><br>        return output<br>    except Exception as exc:<br>        print(exc)<br>        raise exc</pre><p><em>Just to be clear, this is a pointless function, but it’s a useful illustration for testing because it has two pathways that can be followed.</em></p><p>This function accepts an integer and if it’s even we increment our output and if it’s divisible by 100 we increment our output. We then return our output to the caller. We also have some error handling here.</p><p>Test Cases:</p><ul><li>If we submit 4, we’d expect to get 1 returned</li><li>If we submit 5, we’d expect to get 0 returned</li><li>If we submit 200, we’d expect to get 3 returned</li><li>If we submit 417, we’d expect to get 2 returned</li></ul><p>To test this in the “old” way I’d probably just call this function from another file and push in my numbers and forget about it. Then if I ever needed to change this function, I’d do the same again — <strong>BUT</strong> — it might be 6 months down the line and I forgot that I tested it with 417 and so a change that explicitly checks for odd numbers and increments our output is going to fail the test, only I won’t know that! And that may or may not have dire consequences once this code hits production!!</p><p>So this is where <strong>pytest</strong> comes in! <strong>Automated, repeatable tests.</strong></p><p>To create the test cases highlighted above, create a <em>functions</em> folder inside our <em>tests</em> folder and then create a <em>test_check_number.py</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/253/1*ZHXbSgP_ylQeJ9ymmvWQUA.png" /><figcaption>VSCode — our first test file!</figcaption></figure><p>Add the following code code to <em>test_check_number.py</em>:</p><pre>from api.functions import check_number<br><br><br>def test_large_even_number():<br>    output = check_number(200)<br>    assert output == 3</pre><p><em>Note 1, you can call the subfolders inside the tests folder whatever you like, if it makes sense to you to match your route structure, do that, if it’s more relevant to do it by module, do that. But you do need to prefix your files with </em><strong><em>test_</em></strong><em> or they’ll be ignored by the test discovery and won’t be run automatically.</em></p><p><em>Note 2, I’ll cover the distinction between unit tests and integration tests later, this is usually the first tier of subfolders inside my tests folder, before looking at functions, routes and services etc.</em></p><p>This test code will pass in a value of 200, it will then assert that the value returned from our function is 3.</p><p>Save the file and you should see that next to line 3 a little green arrow has appeared:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*TWza1lQfdg6QGxeDVcDHaQ.png" /><figcaption>VSCode — test runner prompt</figcaption></figure><p>If you right click this arrow you’ll see the options we can run on this test. Click <strong>Reveal in Test Explorer</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/468/1*hndF_RR1_KBVcvekJSKXTw.png" /><figcaption>VSCode — Testing context menu</figcaption></figure><p><strong>Test Explorer</strong> will now open with this test selected, which is especially useful when you’re working on a project with hundreds of test cases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/1*KEm7yY_gjZ1b-YG9BSSnng.png" /><figcaption>VSCode — Test Discovery</figcaption></figure><p>As you add or change tests, this extension keeps refreshing itself. If it runs into an error it’ll break and direct you to the logs so you can see what’s wrong e.g. you’re importing a file that doesn’t exist or your syntax is incorrect:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*vnQ-Nn-0yE8w5XFG1EWiDQ.png" /><figcaption>VSCode — Test Discovery failure</figcaption></figure><p>To illustrate this, I added some bad code to cause a failure (line 3) and if we take a look at the output generated, we can see the problem:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/776/1*7ONo1daiLJrjeCGezlxhyg.png" /><figcaption>VSCode — Test Discovery output</figcaption></figure><p>Alongside each test, we have three options — <strong>Run Test</strong>, <strong>Debug Test</strong> and (converse to the context menu and equally useful) <strong>Go to Test</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/382/1*UnSYbaJ4q3Jhl1LYD5sKQg.png" /><figcaption>VSCode — Test options</figcaption></figure><p>Click <strong>Run Test</strong>.</p><p>If you look at the Test Results panel that has opened you can see that the test ran successfully:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/498/1*OSM4bUNUA3H8MsWgkDNekA.png" /><figcaption>VSCode — test results</figcaption></figure><p>Ok, so it looks like it did what we wanted but how can we be really sure? Well, also included in our devcontainer extensions list is <strong>Coverage Gutters </strong>and you should see on the Status panel at the bottom of VSCode a section which should say <strong>Watch</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/440/1*muddkB7AglArYtryBsYgaw.png" /><figcaption>VSCode — Status panel with Coverage Gutters enabled</figcaption></figure><p>Click <strong>Watch</strong> and then you should get an error in the bottom right, expand it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*LAJo_22Edkj4uYUcRlkKVA.png" /><figcaption>VSCode — Coverage Gutters warning</figcaption></figure><p>Before I explain what’s going on here, let’s step back and run through the coverage process manually. Swap over to your terminal and run <strong>pytest </strong>manually with these arguments:</p><pre>pytest tests --cov-report xml:coverage.xml --cov api</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*RheAU3Um-qAeQRdc6HOHZg.png" /><figcaption>VSCode Terminal — pytest CLI output</figcaption></figure><p>This command has run <strong>pytest </strong>and generated the coverage report files that are needed by <strong>Coverage Gutters</strong>. These files should be in our <em>root </em>directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/1*p7vIRPhpecvtH-zVLSHx1w.png" /><figcaption>VSCode — coverage report files</figcaption></figure><p>If you take a look at <em>coverage.xml</em> you can kind of see what it’s doing:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*asoVDFpi8eQicphT5IJ-UA.png" /><figcaption>VSCode — coverage.xml showing a hit for each line of code in our function</figcaption></figure><p>What’s interesting here though is that if you click on our open <em>functions.py</em> file it’s also updated the status of <strong>Coverage Gutters</strong> in our Status panel:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*8qhEHlcakPjaZbMBPdhiRw.png" /><figcaption>VSCode — Coverage Gutters with 72% completion</figcaption></figure><p>The extension is showing us that we have 72% coverage, but let’s jump back to our code so we can see exactly what that means:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/324/1*7WmovVAa4O_VqlluypAPOg.png" /><figcaption>VSCode — gutters now showing red and green</figcaption></figure><p>We now have these green and red lines in the gutters next to each line, what’s going on?</p><p>Before I explain that, I need to highlight a problem I have with this plugin. In part 2 of this series I discussed step debugging, try adding a breakpoint to the lines of this code, you can’t. You have to <strong>right click</strong> and select <strong>Add Breakpoint</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*_6YFAtpqnJJw5kEion7CBQ.png" /><figcaption>VSCode — Step Debugging Context menu</figcaption></figure><p>This gets annoying really quickly! But there’s an easy fix, in the <strong>Extensions </strong>window find <strong>Coverage Gutters</strong> and open its <strong>settings </strong>then scroll right to the bottom, you should have these three options:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*Nvj51QZFCuRPL-sGPUsZZw.png" /></figure><p>I turn off gutter coverage and instead turn on line coverage. Ruler coverage is basically gutters on the right hand side. Close settings and now our code window has updated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*23muOtpqhVp7R2vrJyNJww.png" /><figcaption>VSCode — line level coverage highlighting</figcaption></figure><p>This is another personal preference, but I absolutely love working like this. It’s much clearer for a start but hopefully as you progress through the rest of the series you’ll start to see why I love it so much!</p><p>To answer both of these open questions in one, this extension is reading our generated <em>coverage.xml</em> file (which didn’t exist until we manually ran <strong>pytest</strong>, hence the first error) and if the line has a “hit” i.e. Python ran it, this extension marks it green, otherwise it marks it red.</p><p>What we can see here is that passing in a value of 200 hits both of our conditional statements and then returns without generating an exception. We’ve “ticked off” our code as working for this particular scenario!</p><p>But how can we prove that’s the case though? Aside from this code being logically obvious, we need to be sure that these lines aren’t always being hit, so we can try one of the other test cases that only hits one of the conditional statements. Add another test to <em>test_check_number.py</em>:</p><pre>from api.functions import check_number<br><br><br>def test_large_even_number():<br>    output = check_number(200)<br>    assert output == 3<br><br><br>def test_small_even_number():<br>    output = check_number(4)<br>    assert output == 1</pre><p>When you save this file, you’ll see another green arrow appear and <strong>Test Explorer</strong> adds the test after the last one. Click the <strong>Run Test </strong>arrow:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/699/1*wAqxeYCUAUxLteDu0v2c6A.png" /><figcaption>VSCode — test updated, coverage not correct</figcaption></figure><p>Hmm, that’s not right, the coverage hasn’t updated properly, I wouldn’t have expected line 10 to have been hit by this test. This is because <strong>Test Explorer</strong> doesn’t set any command arguments to generate the coverage reports, it’s just running <strong>pytest</strong> by default. To fix this, add a <em>setup.cfg </em>file to the root folder and add this code:</p><pre>[tool:pytest]<br>addopts = --cov=api --cov-report=term-missing --cov-report=xml --cov-report=html</pre><p><em>In addition to the arguments we used when manually testing from the CLI, I’ve included HTML report generation and summarised file level terminal output. Again, both of these are personal preference.</em></p><p>When you save this file, <strong>pytest</strong> refreshes and generates new coverage files. You should now see the <em>htmlcov</em> folder in your project, which if you preview <em>index.html</em> looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*YS9g4vjTCF0EMQaSWKVg0Q.png" /><figcaption>VSCode — HTML file context menu</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*CM4r3vulkQwLraA0wGMadA.png" /></figure><p>This output is based on the initial refresh of our coverage i.e. without any tests being run. Run all tests. Open <strong>Test Explorer</strong> and hit <strong>Run Tests</strong> at the top:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*jpRDNp7j72KgT8pket1YTA.png" /><figcaption>VSCode — Test Explorer run tests</figcaption></figure><p>Now refresh the <em>index.html</em> preview:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*6BgGMWWEt3Gdbn6vr8a9jA.png" /><figcaption>VSCode Preview — HTML Coverage Report</figcaption></figure><p><em>I don’t normally use the HTML report for anything, I prefer the CLI version, but it can be useful to generate and save as an artifact during CI/CD for other team members to quickly review.</em></p><p>Your folder structure should now look something like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*aTOsXUXSXzS3qjnLLz4sUA.png" /><figcaption>VSCode — folder structure</figcaption></figure><p>Let’s run <em>test_small_even_number()</em> again and take a look at the coverage:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/692/1*1A5Y9G2HFJ7O1qf08Oip9A.png" /><figcaption>VSCode — coverage updated for the second test</figcaption></figure><p>That’s better. Now it shows that for this test we didn’t hit line 8 as expected and the assertion passed, confirming our output was 1. We can also see in our coverage report that coverage has dropped to 67%:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*T46UTnnG7bglOPEgfoaEZA.png" /><figcaption>VSCode — HTML Coverage Report updated for test 2</figcaption></figure><p>I’ve been using this method of development extensively recently and I find it incredibly useful for building out functions, catching exceptions, thinking about all the possible test cases I could expect.</p><p>But as with most things do try to keep it simple, there’s no point testing for what happens if a float or a string comes in here, you can of course, but it’s your code so you can control that. If you or someone else on your team changes some code down the line, you can make sure it’ll bomb out correctly, so in the interest of showing you how to test a failure, let’s see what happens when we submit a string. Add a new function called <em>test_string() </em>save the file and then run the test:</p><pre>def test_string():<br>    output = check_number(&quot;123&quot;)<br>    assert output == 1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B7kaLrx3PJt_4-GP49nJbg.png" /><figcaption>VSCode — pytest failure as TypeError not expected</figcaption></figure><p>If we look at our <em>functions.py</em> file we can see the test ran as expected and generated an exception:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/1*fy07pw6GiJuCzoCxNt3AzQ.png" /><figcaption>VSCode — exception lines hit as expected</figcaption></figure><p>But our test actually failed because we’re still asserting an output of 1 rather than an exception. If you decided you did want to keep this as a valid test you can, but you need to validate that it failed.</p><p>At the top of the file add <em>import pytest</em> and then rename the <em>test_string() </em>function to <em>test_exception()</em>. Let’s assume we don’t know why it’s failing, we’ve submitted “123” and (incorrectly!) we expect a ValueError to be raised:</p><pre>import pytest<br>from api.functions import check_number<br><br><br>def test_large_even_number():<br>    output = check_number(200)<br>    assert output == 3<br><br><br>def test_small_even_number():<br>    output = check_number(4)<br>    assert output == 1<br><br><br>def test_exception():<br>    with pytest.raises(ValueError):<br>        output = check_number(&quot;123&quot;)</pre><p>Run this test and you’ll see that the test fails again with the same error. In a real life scenario, it might not always be clear which Exception is being raised, or you might have multiple except blocks in your code. If this is the case, how else could we look at what’s going on here? Step debugging with <strong>pytest</strong> to the rescue!</p><p>There’s a compatibility issue between VSCode step debugging and pytest. When debugging VSCode <em>does</em> add command line arguments, but they’re not the ones we want. As a result, they conflict with our coverage reports. So we need to temporarily disable them when we enter step debugging and then enable them again when we’re done so normal test runs work properly. And we need this to be automated!</p><p><strong><em>This is a bit complicated, if anyone has a better way of handling this, let me know in the comments!</em></strong></p><p>Inside our <em>.vscode</em> folder, open the <em>launch.json</em> file and add a new debug config, note the two task lines:</p><pre>{<br>    &quot;version&quot;: &quot;0.2.0&quot;,<br>    &quot;configurations&quot;: [<br>        {<br>            &quot;name&quot;: &quot;Python Debugger: FastAPI&quot;,<br>            ...<br>        },<br>        {<br>            &quot;name&quot;: &quot;Python Debugger: pytest&quot;,<br>            &quot;type&quot;: &quot;debugpy&quot;,<br>            &quot;request&quot;: &quot;launch&quot;,<br>            &quot;program&quot;: &quot;${file}&quot;,<br>            &quot;purpose&quot;: [&quot;debug-test&quot;],<br>            &quot;console&quot;: &quot;integratedTerminal&quot;,<br>            &quot;justMyCode&quot;: false,<br>            &quot;cwd&quot;: &quot;${workspaceFolder}&quot;,<br>            &quot;env&quot;: {<br>                &quot;PYTHONDONTWRITEBYTECODE&quot;: &quot;1&quot;,<br>                &quot;PYTHONBUFFERED&quot;: &quot;1&quot;<br>            },<br>            &quot;jinja&quot;: true,<br>            &quot;preLaunchTask&quot;: &quot;disable-coverage&quot;,<br>            &quot;postDebugTask&quot;: &quot;enable-coverage&quot;<br>        }<br>    ]<br>}</pre><p>Now add a <em>tasks.json</em> file to the <em>.vscode</em> folder:</p><pre>{<br>    &quot;version&quot;: &quot;2.0.0&quot;,<br>    &quot;tasks&quot;: [<br>        {<br>            &quot;label&quot;: &quot;disable-coverage&quot;,<br>            &quot;command&quot;: &quot;bash ./disable-coverage.sh&quot;,<br>            &quot;type&quot;: &quot;shell&quot;<br>        },<br>        {<br>            &quot;label&quot;: &quot;enable-coverage&quot;,<br>            &quot;command&quot;: &quot;bash ./enable-coverage.sh&quot;,<br>            &quot;type&quot;: &quot;shell&quot;<br>        }<br>    ]<br>}</pre><p>In <em>launch.json</em> we’re telling the debugger to call a task before debugging and another task after debugging. These tasks are configured in <em>tasks.json</em> and will execute two bash scripts.</p><p><em>Note, you could put the command directly in here but whenever it’s multi-line I use a bash file.</em></p><p>In the root of the project, create a <em>disable-coverage.sh</em> file:</p><pre>echo [tool:pytest] &gt; setup.cfg<br>echo addopts = --no-cov &gt;&gt; setup.cfg</pre><p>Then create an <em>enable-coverage.sh</em> file:</p><pre>echo [tool:pytest] &gt; setup.cfg<br>echo addopts = --cov=api --cov-report=term-missing --cov-report=xml --cov-report=html &gt;&gt; setup.cfg</pre><p>These two files are essentially just swapping the <em>setup.cfg</em> file between two different versions of it.</p><p>Here’s our updated folder structure:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/216/1*vAsgXeyQo9Dg3xSOSaS3yA.png" /><figcaption>VSCode — added tasks to the debug config</figcaption></figure><p>In <em>functions.py</em>, add a breakpoint to line 12:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/336/1*0JFeGyVqmFb1ukpoqfxFPQ.png" /><figcaption>VSCode — breakpoint added</figcaption></figure><p>Note that each time you save a test or make changes to code, it will reset the coverage and all lines are reset to no hits and show up as red.</p><p>Instead of running our <em>test_exception</em> test, we want to debug the process. In our <em>launch.json</em> we added the purpose <strong>[“debug-test”]</strong> which tells VSCode to use this config whenever we enter step debugging from a test. Right click the green arrow and select <strong>Debug Test</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/357/1*59XZ98jk--TJJK8tPMQHgg.png" /><figcaption>VSCode — Debug Test</figcaption></figure><p>VSCode should pause on line 12 and you’ll be able to see the error in the debug window:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/889/1*BxmYnVCbCaVsU1o3hYHK1w.png" /><figcaption>VSCode — Step Debugging a test</figcaption></figure><p>Here we can see that the Debugger being used is our new pytest config as we expected, we’re paused on line 12 and the value of exc is actually a TypeError and not a ValueError.</p><p><em>If for any reason this doesn’t work, switch to the Debug extension and manually select this config, then debug the test. It worked differently on my two PCs!</em></p><p>Before we continue, quickly add a breakpoint to line 13 and press F5 and check the terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*nALVWalphefJMIht8rXhzw.png" /><figcaption>VSCode Terminal — output of test</figcaption></figure><p>This is really useful once you add logging to your code. You can’t see output when running a test, only when you step debug a test. More on logging in another article.</p><p>Press <strong>F5</strong> to continue. The Terminal window will be showing the output of our tasks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*jKts2pGYbL22TLq6y2MQ5A.png" /><figcaption>VSCode Terminal — task output</figcaption></figure><p>In our test file, we can see our test is still failing because we’re validating the wrong exception. Update the test to check for TypeError:</p><pre>def test_exception():<br>    with pytest.raises(TypeError):<br>        output = check_number(&quot;123&quot;)</pre><p>Now run all the tests:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*j3NMPgJdNJU3ZZMJE6GkfA.png" /><figcaption>VSCode — testing complete, full coverage</figcaption></figure><p>Full coverage! Refresh <em>index.html</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*ymzES8rGSkXAPoR9cw7IIQ.png" /><figcaption>VSCode — updated coverage report</figcaption></figure><p>That was a really simple function with a really simple set of tests, but hopefully you’ve learned how to get up and running with pytest, how to write basic tests and how to debug tests when they’re failing.</p><p>Let’s apply what we’ve learned to our <strong>FastAPI </strong>application and test the items route again.</p><p>Inside our <em>tests</em> folder create a routes folder and a <em>test_items.py</em> file:</p><pre>cd tests/<br>mkdir routes<br>cd routes/<br>touch test_items.py</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/238/1*WWqmSI_ijcMvAVAZjXRGEg.png" /><figcaption>VSCode — updated tests folder</figcaption></figure><p>In our <em>test_items.py</em> file add this code:</p><pre>def test_items(app, client):<br>    response = client.get(&quot;/items/12345?q=somequery&quot;)<br>    assert response.status_code == 200<br>    assert response.json() == {&quot;item_id&quot;: 12345, &quot;q&quot;: &quot;somequery&quot;}</pre><p>At the start of this article we added a couple of fixtures to our test suite. I’ll cover these in more detail later but we added a call to our app object and we pre-created a TestClient object, passing in our app fixture. Using TestClient instead of httpx.Client gives us a few more options specific to testing.</p><p>If you go ahead and run this test, you’ll see that it executes successfully. You can also add breakpoints to the tests themselves and step through these lines individually, which is incredibly useful the more complex your tests start to become.</p><p>Add a breakpoint to line 3 and then right click the arrow and select <strong>Debug Test</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1015/1*L9ISrE7wQ5UTCqlT69H4TA.png" /><figcaption>VSCode — Test step debugging</figcaption></figure><p>You’ll see in our debug window the objects we’re creating when running this test. Expand the <strong>client</strong> object and review the properties:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*xwwDD_PiVh1-gbV_kmoCtg.png" /><figcaption>FastAPI client fixture — sets base_url to http://testserver</figcaption></figure><p>Expand the <strong>response</strong> object and review the properties:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*ItQv8jJurz_4Vz1Dsscj4g.png" /><figcaption>Response object — with content</figcaption></figure><p>Just like step debugging our actual code when calling external services, we can step debug our tests to validate our application’s output.</p><blockquote>Hopefully that all made sense and what you should now have is a solid base from which to start building out your <strong>FastAPI </strong>application using <strong>TDD </strong>and <strong>pytest</strong>. If you have any questions, drop them in the comments below and I’ll answer them as quickly as I can.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6dd11fb1f1f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing in Python with Dev Containers — Part 2: A Simple FastAPI project with Step Debugging]]></title>
            <link>https://medium.com/@andypickup101/developing-in-python-with-dev-containers-part-2-a-simple-fastapi-project-with-step-debugging-e52599b7ce61?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e52599b7ce61</guid>
            <category><![CDATA[devcontainer]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[debugging]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 22 Apr 2024 10:33:14 GMT</pubDate>
            <atom:updated>2024-04-22T10:33:14.079Z</atom:updated>
            <content:encoded><![CDATA[<h3>Developing in Python with Dev Containers — Part 2: A Simple FastAPI project with Step Debugging</h3><h4>A quick guide to setting up a FastAPI project with debugging using Visual Studio Code</h4><p>Part 1 in this series covered the initial setup of the Dev Container configuration. You can find it <a href="https://andypickup.com/developing-in-python-with-dev-containers-part-1-setup-f1aeb89cbfed">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-nHBEM_1HjEK1bfVv___Q.jpeg" /><figcaption>A lame attempt at illustrating step debugging using Python, thanks Microsoft Designer!</figcaption></figure><h4><strong>FastAPI Installation</strong></h4><p>FastAPI is the new(ish) kid on the block, quickly replacing Flask as the go-to for back-end API development.</p><p><em>There are opinions (always!) around whether FastAPI has cracked API end to end yet, some recommending PyNest as a wrapper over native FastAPI. I like what PyNest is doing, but I’m not sure it’s there yet, it’s missing a lot of functionality that I’d rely on when building an API. I’m a big fan of MVC so PyNest looks promising, but I’ll discuss options here in another part of this series.</em></p><p>First, let’s check our currently installed Python packages by running pip freeze in a new terminal window and save them to our blank <em>requirements.txt</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*ywWyjFEJ7akHBc4nkjaD5g.png" /><figcaption>VSCode Terminal — pip freeze &gt; requirements.txt</figcaption></figure><p>This should update our <em>requirements.txt</em> file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*Z_T8mYrhCetQY9mosB7INw.png" /><figcaption>Updated requirements.txt</figcaption></figure><p>Install FastAPI and the Uvicorn ASGI server using pip:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*EKzBYI4ozBh2ZkRrjDy-dQ.png" /><figcaption>VSCode Terminal — installing fastapi and uvicorn</figcaption></figure><p>You should see output that looks something like this when it’s done:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*66ZE-aFVWsjSGQmADgRHyA.png" /><figcaption>VSCode Terminal — pip install output</figcaption></figure><p>Let’s freeze those requirements again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/280/1*2ozyu4FWJwaLtUUtk5WY9g.png" /><figcaption>Updated requirements.txt file</figcaption></figure><p><em>Note: you don’t always have to freeze, you could just version fastapi and when you next build the container it’ll run the relevant installations. That’s usually how I do it, but I’m highlighting here how the environment changes but can be controlled at a very granular level.</em></p><h4>FastAPI First Example</h4><p>Next create a <em>main.py</em> file and enter this code I’ve butchered from the FastAPI tutorial:</p><pre>from fastapi import FastAPI<br><br>app = FastAPI()<br><br><br>@app.get(&quot;/&quot;)<br>def read_root():<br>    return {&quot;Hello&quot;: &quot;World&quot;}</pre><p>Run the Uvicorn server from the terminal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/604/1*MrQv3TbXeUyKcHKZn2X6mA.png" /><figcaption>VSCode Terminal — running Uvicorn</figcaption></figure><p>You’ll note that it’s started on port 8000, this is automatically detected and forwarded by VSCode so you should get a pop-up asking you to open the browser:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*Z6OLRp050PEEg51hWqs9ow.png" /><figcaption>VSCode Pop-up — Port detected, open in browser</figcaption></figure><p>When you click this and your browser has loaded you should see the JSON output:</p><pre>{<br>  &quot;Hello&quot;:&quot;World&quot;<br>}</pre><p>And the terminal will update with some basic output from Uvicorn:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*CU2tx9xBEFfDjl8XmJjG6g.png" /><figcaption>VSCode Terminal — Uvicorn output</figcaption></figure><p>Let’s make a quick change to <em>main.py</em> and save:</p><pre>from fastapi import FastAPI<br><br>app = FastAPI()<br><br><br>@app.get(&quot;/&quot;)<br>def read_root():<br>    return {<br>        &quot;Hello&quot;: &quot;World&quot;,<br>        &quot;How Cool&quot;: &quot;Is This?&quot;<br>    }</pre><p>The Terminal should show that Uvicorn has reloaded (because we used the reload flag when we started it):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/591/1*h7i9wKiwi_LP4CPabQQQag.png" /><figcaption>VSCode Terminal — Uvicorn reloaded</figcaption></figure><p>And if we refresh our browser, we’ll see our updated JSON:</p><pre>{<br>  &quot;Hello&quot;:&quot;World&quot;,<br>  &quot;How Cool&quot;:&quot;Is This?&quot;<br>}</pre><p>Press<strong> Ctrl+C</strong> in the terminal to exit Uvicorn.</p><h4><strong>What if we need to debug as we go?</strong></h4><p>I’m a big fan of step debugging and have been since I first learned C#.NET some 20 years ago. I’ve worked with developers (both colleagues and agency staff) who prefer to litter print statements and exit commands through the code and it drives me insane! Especially when they leave them in and deploy to prod!!</p><p>When VSCode first launched, it was severely lacking in the debugging area. But over the last 2 years it’s come on in leaps and bounds and extended to Remote Containers. And thank goodness!!</p><p>Press <strong>Shift+Ctrl+P</strong> and start typing <strong>debug </strong>and select <strong>Add Configuration</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*-DKZnA483I1IflZeXK2mdw.png" /><figcaption>VSCode Command Palette — add debug configuration</figcaption></figure><p>Then select <strong>Python Debugger</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*A00xSo25qhjTJO-INwNsTg.png" /><figcaption>VSCode Command Palette — select Python debugger</figcaption></figure><p>You’ll see there are a number of pre-defined configurations for all the popular frameworks. Select <strong>FastAPI</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*rm7e_Er5aaLHnFhWhFvTAg.png" /><figcaption>VSCode Command Palette — select pre-defined debug configuration</figcaption></figure><p>This will add a <em>.vscode</em> folder with a <em>launch.json</em> file which should look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*8ANrHXl2OMekkk-ELh2Oeg.png" /><figcaption>launch.json</figcaption></figure><p><em>Note: Microsoft recently deprecated their previous debug configurations and are now forcing through debugpy. I’ve only used it on one project in anger so far, but it’s pretty decent and definitely an improvement with fewer issues.</em></p><p>Back in our <em>main.py</em> file, let’s add a breakpoint on line 8 for our return statement:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*ri3mUnb-SVYM6hLax-VKWA.png" /><figcaption>Breakpoint added to line 8</figcaption></figure><p>Then let’s open the Debug window and you should see the configuration has been selected already:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/388/1*4xU6h8WlQio-KbPR-XcMgA.png" /><figcaption>List of debug configurations</figcaption></figure><p><em>Note: I would normally have a few of these, usually around testing or different parameters for starting the server.</em></p><p>Press <strong>F5</strong> to debug. You’ll see it opens a new terminal window for debugging and starts Uvicorn:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/952/1*V2xGo5LNBcoJTwIBcvb_7w.png" /><figcaption>VSCode Terminal — Debug window</figcaption></figure><p>Refresh the browser and now instead of just sending the response, VSCode should pause on line 8:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*iyq-tWdN69YzJg1zhWbf4w.png" /><figcaption>VSCode — paused at our breakpoint</figcaption></figure><p>Press <strong>F5 </strong>again and it’ll continue and return the JSON to the browser.</p><h4>Step Debugging Use Cases</h4><p>There’s not a lot of use for this right now, so if you’re not familiar with step debugging, here’s how you would potentially use it when building an application.</p><p>Let’s copy the rest of the FastAPI tutorial code and add in the <strong>/items/{item_id}</strong> route:</p><pre>from typing import Union<br><br>from fastapi import FastAPI<br><br>app = FastAPI()<br><br><br>@app.get(&quot;/&quot;)<br>def read_root():<br>    return {<br>        &quot;Hello&quot;: &quot;World&quot;,<br>        &quot;How Cool&quot;: &quot;Is This?&quot;<br>    }<br><br><br>@app.get(&quot;/items/{item_id}&quot;)<br>def read_item(item_id: int, q: Union[str, None] = None):<br>    return {&quot;item_id&quot;: item_id, &quot;q&quot;: q}</pre><p>Add a breakpoint to line 18 and save the file to reload Uvicorn. In the browser, update the URL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/392/1*7WVHIWz1jcQXeL3QO72sKQ.png" /><figcaption>Chrome — new URL for the items endpoint, passing in an item ID</figcaption></figure><p>Now VSCode pauses on line 18:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/531/1*Iprvwcp7dj19zixccAtXpg.png" /><figcaption>Debugging paused on line 18</figcaption></figure><p>But if you take a look in the Debug window, you can see the local variables we’ve created in this function, including the item_id parameter we’re passing through in the URL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*SWjFp0Jg9omzlUtK4B0rQg.png" /><figcaption>Debug window showing local variables with their current values</figcaption></figure><p>Press <strong>F5 </strong>to step to the end and the browser should be showing you the new payload with your item ID:</p><pre>{<br>  &quot;item_id&quot;:123456,<br>  &quot;q&quot;:null<br>}</pre><p>Again, there’s nothing special happening here so how else can we use this? I use it a lot when there could be unexpected behaviours e.g. maybe I have an idea of what should happen when I do something, but I’m not actually sure or there’s an error and I don’t know why.</p><p>Let’s say we’re connecting to a third party API. It could be something that returns a payload corresponding to the parameters you submit or it could be something that returns something random. Let’s look at the latter scenario for which we’ll need to install the <strong>httpx </strong>library:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/571/1*24IlAQ0bjMI3SOY7YP9uJQ.png" /><figcaption>VSCode Terminal — installing httpx package via pip</figcaption></figure><p>Add the import to <em>main.py</em> and let’s use a simple URL I like to use that returns a random UUID:</p><pre>from typing import Union<br><br>from fastapi import FastAPI<br><br>import httpx<br><br>app = FastAPI()<br>url = &quot;http://httpbin.org/uuid&quot;</pre><p>Before we do anything else, let’s open this URL in the browser and see what it’s likely to return:</p><pre>{<br>  &quot;uuid&quot;: &quot;b89b7d9c-ff5f-4d6c-ad17-829611bc4693&quot;<br>}</pre><p>Really simple JSON object that we can interact with. But for now let’s do a straight response pass through. Add this function to your <em>main.py</em> file:</p><pre>@app.get(&quot;/test_get_request&quot;)<br>def read_root():<br>    client = httpx.Client()<br>    response = client.get(url)<br>    return response</pre><p>Add breakpoints to lines 27 and 28 and press <strong>F5 </strong>to debug. In the browser, update the URL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*3UXjrH3BHV8DcTUxKWaaQA.png" /><figcaption>Chrome — updated endpoint for our test request</figcaption></figure><p>VSCode should be paused on line 27. If we look at our Debug window now, we can see that it’s created the <em>client</em> object:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/1*29rfqK_LHO-7ZdrVSaHhzA.png" /><figcaption>VSCode Debug — local client variable created</figcaption></figure><p>We can expand this <em>client</em> object to see all of its properties:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8qbsKPSH73DqvrAtIAYnYA.png" /><figcaption>VSCode Debug — expanded client object</figcaption></figure><p>I find this to be much easier than trawling through package documentation or endless tutorials. It allows me to very quickly get an understanding of how to use a package and the objects it creates.</p><p>Press <strong>F5 </strong>to make the request and let’s look at the created <em>response </em>object that the <em>client </em>object has returned:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*xHnjkpqpBZq2WSHP5N4Jog.png" /><figcaption>VSCode Debug — expanded response object</figcaption></figure><p>You can see a number of interesting properties here:</p><ul><li><strong>summary </strong>— we can see we have <strong>Response </strong>object with a status of 200 OK, as we’d expect!</li><li><strong>content </strong>— a byte stream of the content we saw in the browser</li><li><strong>is_</strong> boolean flags — we can use these when checking the responses we’re getting back and handle unexpected behaviour</li><li><strong>status_code</strong> — the integer value which we can use in if statements, typically I’d do something like <em>if status_code != 200 raise Exception(“blah blah blah”)</em></li><li><strong>text </strong>— the raw text of the body</li></ul><p>Press <strong>F5 </strong>to step to the end and return the response:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/791/1*Zkg2-kxV4zi1IxI1rXasPg.png" /><figcaption>VSCode Terminal — an error occured returning the response</figcaption></figure><p>Uh-oh! We got an error! This is because we’re just returning the <em>response </em>object back to the caller without doing anything with it.</p><p>What could we do here? Let’s try returning the <em>text</em> property of the <em>response </em>object, update the code to this:</p><pre>@app.get(&quot;/test_get_request&quot;)<br>def read_root():<br>    client = httpx.Client()<br>    response = client.get(url)<br>    return response.text</pre><p>Save and let Uvicorn reload, then refresh the browser. You should see this:</p><pre>&quot;{\n  \&quot;uuid\&quot;: \&quot;a6667ca7-0565-4efa-a7bd-0de61b646b31\&quot;\n}\n&quot;</pre><p>Which isn’t very nice is it? And it’s certainly not the JSON we were expecting. What else can we do?</p><p>Well, from my days of using Flask I know that the <strong>requests </strong>library will return a <em>response </em>object from which I can extract its JSON using the<em> json()</em> method. Let’s see if <strong>httpx </strong>has a similar function. Refresh the browser and step through the request, then look at the <em>response </em>object in the debug window and expand its <em>function variables</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*tDpDGk4qJx_2qOguxGJgow.png" /><figcaption>VSCode Debug — expanded response function variables</figcaption></figure><p>Aha, a <em>json </em>method, let’s use this instead. Update the code:</p><pre>@app.get(&quot;/test_get_request&quot;)<br>def read_root():<br>    client = httpx.Client()<br>    response = client.get(url)<br>    return response.json()</pre><p>Press <strong>F5 </strong>and refresh the browser, then step through to the end. You should now have the JSON returned correctly:</p><pre>{<br>  &quot;uuid&quot;: &quot;c3224e81-e5cf-4265-82ba-c14aaf47a919&quot;<br>}</pre><p>Sure, I could’ve just referred to the docs or searched Google, but where’s the fun in that?! Obviously for successful requests it has limited usage but when you’re dealing with an API returning an exception, it’s useful to drill in and understand exactly what it’s doing.</p><p>Further, and I’ll cover this later in this series, whenever I’m working with third party services I’ll make sure I understand the various payloads it’s likely to return, especially when there are exceptions, and make a decision over whether I want to output some of this content to my error logs or not. Rather than just dumping the response out entirely, I like to keep the logs a little cleaner so I’ll try and pull out specific fields and suppress the utterly meaningless content you can often see in some of these things.</p><p>As an example, you could receive this from an API when your credentials are incorrect:</p><pre>{<br>    &quot;error&quot;: &quot;err_auth_1001&quot;,<br>    &quot;message&quot;: &quot;Invalid credentials&quot;,<br>    &quot;detail&quot;: &quot;Either the username or the password submitted in the request are incorrect.&quot;,<br>    &quot;help&quot;: &quot;https://help.developer.com/help/error/err_auth_1001&quot;<br>}</pre><p>I could just dump all of this out to my logs, or I could just extract the message and the detail and push that into my own object with any caller parameters I’ve used. I use the <strong>json-logging</strong> package a lot because it allows me to dump JSON to <strong>stdout</strong>, which I then pass into <strong>Cloudwatch</strong>. I can then search <strong>Cloudwatch </strong>using my fields as filters. More on this later!</p><p>Hopefully what you’ve seen here is enough to show you the value of step debugging. Additionally you’ve seen how easy this is to setup in Visual Studio Code using Dev Container. I’ve personally wasted hours and days in the past trying to get the right environment setup. Only to then change it for another project and then not be able to get it back again. Dev Containers have literally saved me hours and feel like I have superpowers with the speed at which I can develop applications.</p><blockquote>Have you used dev containers before? Has this series made you want to consider using them moving forward? Let me know any questions you have and I’ll do my best to respond quickly!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e52599b7ce61" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Developing in Python with Dev Containers — Part 1: Setup]]></title>
            <link>https://medium.com/@andypickup101/developing-in-python-with-dev-containers-part-1-setup-f1aeb89cbfed?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1aeb89cbfed</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[devcontainer]]></category>
            <category><![CDATA[wsl-2]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 15 Apr 2024 10:27:48 GMT</pubDate>
            <atom:updated>2024-04-15T10:27:48.817Z</atom:updated>
            <content:encoded><![CDATA[<h3>Developing in Python with Dev Containers — Part 1: Setup</h3><h4>A quick guide to getting up and running with Python using a dev container in Visual Studio Code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fabz8f2W9ZWmTtC3SrYCMg.jpeg" /><figcaption>A lame attempt at illustrating Python dev containers, thanks Microsoft Designer!</figcaption></figure><p><strong>What exactly are Dev Containers?</strong></p><p>At the risk of teaching anyone to suck eggs, dev containers are essentially just isolated replicas of the environments where your application will sit. Well, at an operating system level, less so the hardware.</p><p>But yes, environments, plural. I’ve read many articles where people talk about using a dev container to write their code, then they ship to production using a separate Dockerfile and wonder why it doesn’t work! My preferred approach is to build multiple containers for these environments; development, unit testing and production debugging — just in case!</p><p>I’ll be digging into this more and discussing why in a subsequent article, but you can of course stick to a single dev container because these shouldn’t normally veer too far from production, otherwise what would be the point?!</p><p><strong>What is the point?</strong></p><p>Using a dev container allows you and any other developer(s) to quickly clone a code repo and start developing without worrying about conflicting versions of operating systems, languages or packages.</p><p><em>Yes, Python handles this really well through virtual environments, but I still prefer to use dev containers, they come in really handy when you start thinking about the whole development lifecycle.</em></p><p><em>Which raises the question of whether you should be using a virtual environment in a dev container or not. I’m inclined to say not. Remember that containers are immutable, and virtual environments are designed for avoiding package collision over the long term. If that’s an issue you face in containerisation, you’re probably not doing it right!</em></p><p>Using <strong>Visual Studio Code</strong> as your IDE, you can extend the configuration to install common extensions and provision useful settings. This has been proven to save days of developer onboarding. It eliminates questions like “what version of Python should this run on?”, “what linting tool are we using?”, “why isn’t my testing config working?” and “how do I debug this?” etc.</p><p>Further, by closely trying to replicate your production environment, you can deal with any issues early in the development chain, e.g. upgrading a package that throws something else out. And by using a single environment config, you can then escalate issues to other developers who should be able to quickly replicate and resolve. Away with the days of “it works on my machine”!</p><p><strong>Pre-requisites</strong></p><p>Nothing fancy, I’m running a mix of Windows 10 and 11 across devices, with WSL installed using Ubuntu, Docker Desktop and Visual Studio Code.</p><p>It’ll be useful to have Git installed too. I use Git for Windows and then have this setup in WSL, with VSCode handling the auth setup. Maybe I’ll write a dedicated article on this later, but it’s changed a lot of over the years so it might not be as troublesome as my original setup was!</p><p>You’ll also need to install the Remote Development extension pack from Microsoft:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W0CvsAOEbv6BqRLEwkeOUg.png" /></figure><p>I install this globally rather than at project level as I use it across all my projects. In addition to developing in WSL and dev containers, you can also connect to virtual machines via SSH, including those in the cloud. It seems archaic now, but was useful when I needed it.</p><p><strong>Project Setup</strong></p><ul><li>Open <strong>WSL </strong>and make sure you’re using <strong>Ubuntu</strong></li><li>You should be in your home folder, in which I’ve created a <em>projects </em>subfolder and then a <em>medium </em>subfolder. Create a new folder for your application, navigate into it and open <strong>VSCode</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/570/1*fsrXpnxwFohYgEIA3ujD3g.png" /><figcaption>WSL Terminal — create folder structure</figcaption></figure><p>The first thing I do is create the initial dev container config, <em>before</em> I start writing my application code.</p><ul><li>Press <strong>Shift+Ctrl+P</strong> and start typing <strong>dev</strong>, then select <strong>Add Dev Container Configuration Files</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*XBoZXua4SblcG1tYNVzzEw.png" /><figcaption>VSCode Command Palette — search for dev containers</figcaption></figure><ul><li>Then start typing <strong>Python </strong>and select <strong>Python 3</strong>:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*lPhgGOtZOoiz2X5j5yyimw.png" /><figcaption>VSCode Command Palette — search for Python</figcaption></figure><ul><li>Then choose whichever version you want to develop on, I’ll be using <strong>3.12-bullseye</strong> for this series:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/1*F-kF3PQ1643bMvoPZA3V7g.png" /><figcaption>VSCode Command Palette — select Python container image</figcaption></figure><p>This will create a <em>.devcontainer</em> folder with a <em>devcontainer.json</em> file inside. This json file is super basic and just gives the container a name and the path to the container image. Other common options are shown but commented out:</p><pre>// For format details, see https://aka.ms/devcontainer.json.<br>// For config options, see the README at:<br>// https://github.com/devcontainers/templates/tree/main/src/python<br>{<br> &quot;name&quot;: &quot;Python 3&quot;,<br> // Or use a Dockerfile or Docker Compose file.<br> // More info: https://containers.dev/guide/dockerfile<br> &quot;image&quot;: &quot;mcr.microsoft.com/devcontainers/python:1-3.12-bullseye&quot;<br><br> // Features to add to the dev container.<br> // More info: https://containers.dev/features.<br> // &quot;features&quot;: {},<br><br> // Use &#39;forwardPorts&#39; to make a list of ports inside the<br> // container available locally.<br> // &quot;forwardPorts&quot;: [],<br><br> // Use &#39;postCreateCommand&#39; to run commands after the container<br> // is created.<br> // &quot;postCreateCommand&quot;: &quot;pip3 install --user -r requirements.txt&quot;,<br><br> // Configure tool-specific properties.<br> // &quot;customizations&quot;: {},<br><br> // Uncomment to connect as root instead.<br> // More info: https://aka.ms/dev-containers-non-root.<br> // &quot;remoteUser&quot;: &quot;root&quot;<br>}</pre><p><em>It’s worth highlighting that the dev container image we’re using here is </em><strong><em>NOT </em></strong><em>an image you should be using in production. This is just to get you up and running, I’ll cover that switch to production readiness in another article.</em></p><p>You should see a pop-up asking you to Reopen the window in the dev container. Ignore this for now.</p><p><em>Some Important Things To Note:</em></p><ul><li>Line 5 — you don’t have to use this standard image, you can instead link to more complicated environment setups using a Dockerfile or a Docker Compose file so you can launch other services like Postgres and Redis. We’ll get to that!</li><li>Line 9 — I don’t normally use features from the start, they are useful in some instances which we’ll cover later e.g. developing for cloud services like AWS and Azure, or using Github Actions but generally speaking they’re great for development but not so great for prod env replication. I tend to only use them when I’m working on that specific use case, for which I’ll have a dedicated dev container config created.</li><li>Line 12 — If you need port forwarding you can enable this here. Uncomment this line and set it to whatever port you need e.g. it should match your debug config and it should avoid conflicting with other running containers. For a default FastAPI application, it’ll pick up the default port 8000 so you don’t need to change this.</li><li>Line 15 — I use postCreateCommand a lot when I’m not using a Dockerfile, it’s useful for linking out to a bash file that can run multiple commands. See below.</li><li>Line 18 — I use customisations to tailor my VSCode initialisation and get the extensions I use all the time installed plus any other useful settings. See below.</li><li>Line 21 — When I’m using a Dockerfile I tend to create a non-root user and then I’ll make sure that’s referenced here.</li></ul><p>For simplicity, clear out all the comments and you should be here:</p><pre>{<br> &quot;name&quot;: &quot;Python 3&quot;,<br> &quot;image&quot;: &quot;mcr.microsoft.com/devcontainers/python:1-3.12-bullseye&quot;,<br> &quot;remoteUser&quot;: &quot;vscode&quot;<br>}</pre><p><strong>Customisations</strong></p><p>This is probably the second most useful aspect of using dev containers. As I find new extensions that make me more productive, I always make a point of updating my base container config for all future projects and I’ll update any older projects whenever I dip into them.</p><p>The most common extensions I use are ToDo-Tree, Git Graph, Git Lens, Python and Pylance, Coverage Gutters, JSON, Markdown, Postman, Github Actions, Shell Format, Trailing Spaces and most recently Github Copilot. Then I’ve got some other useful helpers here too:</p><pre>{<br> &quot;name&quot;: &quot;Python 3&quot;,<br> &quot;image&quot;: &quot;mcr.microsoft.com/devcontainers/python:1-3.12-bullseye&quot;,<br> &quot;remoteUser&quot;: &quot;vscode&quot;,<br> &quot;customizations&quot;: {<br>  &quot;vscode&quot;: {<br>   &quot;settings&quot;: {},<br>   &quot;extensions&quot;: [<br>    &quot;gruntfuggly.todo-tree&quot;,<br>    &quot;ms-azuretools.vscode-docker&quot;,<br>    &quot;samuelcolvin.jinjahtml&quot;,<br>    &quot;mhutchie.git-graph&quot;,<br>    &quot;ms-python.python&quot;,<br>    &quot;ms-python.vscode-pylance&quot;,<br>    &quot;ryanluker.vscode-coverage-gutters&quot;,<br>    &quot;meezilla.json&quot;,<br>    &quot;eamodio.gitlens&quot;,<br>    &quot;emmanuelbeziat.vscode-great-icons&quot;,<br>    &quot;dongido.sync-env&quot;,<br>    &quot;yzhang.markdown-all-in-one&quot;,<br>    &quot;postman.postman-for-vscode&quot;,<br>    &quot;github.vscode-github-actions&quot;,<br>    &quot;foxundermoon.shell-format&quot;,<br>    &quot;shardulm94.trailing-spaces&quot;,<br>    &quot;github.vscode-pull-request-github&quot;,<br>    &quot;github.copilot&quot;,<br>    &quot;github.copilot-chat&quot;<br>   ]<br>  }<br> }<br>}</pre><p>Note that these are tailored to Python development. I have others I use for things like Terraform and SQL. Using the customisations setting allows me to keep this separate for each type of project rather than just having them all globally installed (and potentially getting in the way!).</p><p>You can also add IDE settings in here, but I prefer to keep these within the <em>.vscode</em> folder, which we’ll get to shortly. I’ll also cover using settings in the devcontainer so you can see the difference.</p><p><strong>postCreateCommand</strong></p><p>As mentioned above, when not using a Dockerfile, I’ll make use of the postCreateCommand setting to run a bash script with a selection of additional commands.</p><pre>{<br>  &quot;name&quot;: &quot;Python 3&quot;,<br>  &quot;image&quot;: &quot;mcr.microsoft.com/devcontainers/python:1-3.12-bullseye&quot;,<br>  &quot;postCreateCommand&quot;: &quot;bash .devcontainer/post-create.sh&quot;,<br>  &quot;remoteUser&quot;: &quot;vscode&quot;,<br>  &quot;customizations&quot;: {<br>  // ...<br>  }<br>}</pre><p>The bash script I tend to use when getting started:</p><pre>#!/usr/bin/env bash<br>pip3 install -r requirements.txt<br>pip3 install -r requirements-dev.txt</pre><p>Quickly open the <strong>VSCode </strong>terminal window and create these two files:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/921/1*d5lHKUjaKTKL-7ED2ATuBQ.png" /><figcaption>VSCode Terminal — create empty requirements files</figcaption></figure><p>Make sure you have Docker Desktop running and press <strong>Shift+Ctrl+P</strong> and select <strong>Reopen in Container</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/602/1*YKBe6pyCDKfAnv2nbhCylA.png" /><figcaption>VSCode Command Palette — Reopen in container</figcaption></figure><p>This will take a few minutes while it downloads and builds the container, but you can click this popup to see the log:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*B-BSKbDVT0I1l90z6lECHQ.png" /><figcaption>VSCode Pop-up — show dev container log</figcaption></figure><p>Which will show you the progress, useful to see what takes the longest:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*Nm0TPcHu5LUPI-c8N7dMow.png" /><figcaption>VSCode Terminal — container build logs</figcaption></figure><p>Once complete, you’ll see this message:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/595/1*VQ7I9AfQX9dHRpbAUAsI7w.png" /><figcaption>VSCode Terminal — build complete</figcaption></figure><p>Just press any key to close!</p><p>And if you take a look at your <strong>Extensions</strong>, you’ll see everything we specified in the <em>devcontainer.json</em> file has been pre-installed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/395/1*33GnLnet9LaCd54NNZZ0Hw.png" /><figcaption>VSCode Extensions — pre-installed in the container</figcaption></figure><p>You’re ready to get started with some Python development. For the rest of this series I’m going to be building out a FastAPI project, with some TDD and unit testing, deploying to ECS with Terraform using Github Actions and then covering some advanced topics like message queuing with Celery and Cron job management.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1aeb89cbfed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ADHD Awareness Month — October 2022]]></title>
            <link>https://medium.com/@andypickup101/adhd-awareness-month-october-2022-1ec0494b533c?source=rss-48ba418b451d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1ec0494b533c</guid>
            <category><![CDATA[mental-health]]></category>
            <category><![CDATA[adhd]]></category>
            <category><![CDATA[nhs]]></category>
            <dc:creator><![CDATA[Andy Pickup]]></dc:creator>
            <pubDate>Mon, 31 Oct 2022 08:43:54 GMT</pubDate>
            <atom:updated>2022-10-31T08:43:54.798Z</atom:updated>
            <content:encoded><![CDATA[<h3>ADHD Awareness Month — October 2022</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*mRL_62p86sctVI_vtSZV6Q.png" /><figcaption>ADHD Awareness October 2022</figcaption></figure><p>A year has passed, I’m a little older, a little wiser, I’m starting to get hair in really weird places…</p><p>Ahem… it’s been a year since I was officially diagnosed with ADHD. Only a year?! It’s felt like a lifetime! But there’s just about enough time to slot this into October, which is ADHD Awareness Month.</p><p>Honestly, I took the diagnosis badly. I’ve had some tough times in my 40 (+1) years in this world but I’ve never felt as broken as I did immediately following my diagnosis. I’ve never felt like everything that has come before has been so totally wrong. Felt like I’ve been doing everything wrong. Living life wrong. Chasing the wrong dreams.</p><p>The diagnosis and subsequent research extravaganza (read: hyper-focusing and literally reading the entire internet to play catch up) helped me realise just how much I’d been masking to fit in. Everybody masks, of course they do, it’s literally requirement #1 to not get fired within an hour. But I’ve been masking like I’m on steroids.</p><p>I remember working with a client once, and he’d constantly be up and down, pacing the meeting room. When he was out of the room people would be like “he’s so weird, just sit still!” and I’d nod, but inside I’d be pushing down that urge to bounce around the room I’ve been stuck in for the last 4 hours!!</p><p>And then there was a question on the ADHD survey, how often do you get up and move around and I’m thinking… never… because I’ve aggressively trained myself to not do that so I’m not seen as super weird by neurotypical people!</p><p>There are two types of ADHD;</p><ul><li>Inattention without hyperactivity</li><li>Hyperactivity without inattention</li></ul><p>Oh and I lied, there’s a third. Combination! I got that one. At first it felt like I was doubling up and taking it to extremes. But that’s not it at all, I just display symptoms from both categories making it impossible to be put in either box, but enough to be diagnosed with ADHD. I think that’s the diplomatic way to write that!</p><p>In January I started medication. First up, Lisdexamfetamine. 10mg, then 20, 30, 40 then 50. Oh man. 50s! I was bouncing off the ceiling dude! I didn’t get on with those so well. Thank goodness for WFH, my colleagues would’ve probably tranquilized me!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*-2O9oVE0_ebaTl-57xW4Qg.gif" /><figcaption><a href="https://www.pinterest.co.uk/pin/852235929457907739/">https://www.pinterest.co.uk/pin/852235929457907739/</a></figcaption></figure><p>Then I swapped over to Medikinet. Much better! These are the ones! They’re still not quite right but they’ve made a big difference. I’ve got a review this week and I’ll explore tweaking the prescription slightly and hopefully nail it down. I’m slightly concerned that you usually only get them for a year, I’m not sure I want to go back to not having them. They really have been game changing.</p><p>In addition to medication, I’ve been in therapy for the last few months working on those feelings of brokenness. I’m mostly over it. It’s largely misplaced emotions. ADHD is not a “bad thing”. The issue is almost certainly how the world treats those who are different. There are a lot of strengths with ADHD. Which brings me to my next point.</p><p>ADHD Influencers!</p><p>Oh man, where to start?!</p><p>Ok, so the first thing you have to keep in mind, take these people with a pinch of salt. They’re absolutely not medical professionals. If they haven’t written a book, done a ton of research, cited their sources, it’s probably hearsay they picked up on Reddit or an interpretation of their own counsellors.</p><p>I don’t mean to be unnecessarily critical but WOW! It’s been eye opening!</p><p>They probably mean well, but they’re often young and inexperienced and probably haven’t quite had a full serving of just how horrible this world can be yet, so when they post their little memes about how workplaces need to be more accommodating to people with ADHD, they likely don’t realise just how much damage they could be causing.</p><p>Often these posts are summary level, avoiding the complexities, the whys, the wherefores, and they end up coming across as “here’s what workplaces need to provision to allow ADHD people to keep up”. Keep up?! It’s re-enforcing the stigma. It’s potentially more damaging. In smaller businesses, chances are that employers won’t want a bar of anyone with “requirements”, and if you’re diagnosed during employment, you could well be met with a “this doesn’t change anything” type response!</p><p>Instead, these posts should continually highlight what I guess can be referred to as the “superpowers” of ADHD. For me personally, I’ve come to understand there are many parts of my ADHD that potentially explain why I’m so good at what I do for a living. Super fast decision making, constant bigger picture visibility while moving the chess pieces at detail level, multi tasking, getting things done, insane productivity levels, creative approaches to mundane issues. If you can figure out how to hone these skills you’ll be a superstar.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fn2EVEYmeSqg%3Fstart%3D122%26feature%3Doembed%26start%3D122&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dn2EVEYmeSqg&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fn2EVEYmeSqg%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/196959691bc6471d7af021fe9c4cf42f/href">https://medium.com/media/196959691bc6471d7af021fe9c4cf42f/href</a></iframe><p>During my ADHD diagnosis, they pre-tested for autism/ASD. I possibly/probably have ASD and I need further tests for that too. I still haven’t done that. See below for why!</p><p>But what’s interesting is that in 25% of ADHD cases, it’s never just ADHD, it’s often linked to something else like ASD. I’d been seeing my current therapist before lockdown. When I went back to her, I asked her if she’d suspected anything and she was like, oh yeah, I totally wrote on my piece of paper “probably has Asperger’s”! But obviously they can’t really tell you directly, you have to figure it out yourself and get diagnosed.</p><p>Which brings me to the final point. I’m incredibly fortunate at this time in my life. I have a good job that pays well. I went private for my diagnosis. It cost me several thousands of hard earned pounds, but saved me 2 years on a waiting list. Huge chunk of my savings. I couldn’t wait until I was transferred back to the NHS!! The ASD diagnosis will likely be another £1,500.</p><p>But that’s ridiculous. It shouldn’t be this way. It shouldn’t have taken until 40 for me to get diagnosed. It shouldn’t be the case that I had to nearly bankrupt myself to get the care I so desperately needed. I’m still paying £100 a month for my private therapy sessions.</p><p>The NHS desperately needs mental health funding. Every person in this country with ADHD or ASD needs the opportunity to get themselves diagnosed. Quickly. And that’s not going to happen with this current government. It’s pathetic and it needs sorting out ASAP. Spoiler, more of the same won’t cut it. It’s time for change!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1ec0494b533c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>