<?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 Alan Swenson on Medium]]></title>
        <description><![CDATA[Stories by Alan Swenson on Medium]]></description>
        <link>https://medium.com/@aswens0276?source=rss-2f1908dd9f11------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*bJ0V7gPnx5anLOLwCRZ-Wg.jpeg</url>
            <title>Stories by Alan Swenson on Medium</title>
            <link>https://medium.com/@aswens0276?source=rss-2f1908dd9f11------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 11:27:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aswens0276/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[How to claim your business in ChatGPT (and keep the facts straight)]]></title>
            <link>https://medium.com/@aswens0276/claim-your-business-in-chatgpt-91263dcb588f?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/91263dcb588f</guid>
            <category><![CDATA[small-business]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[openai]]></category>
            <category><![CDATA[business-strategy]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Fri, 20 Nov 2026 22:47:47 GMT</pubDate>
            <atom:updated>2026-11-20T22:47:47.915Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="A brass plaque rising out of a laptop screen, outlined in green light, on a dark navy background." src="https://cdn-images-1.medium.com/max/1024/1*Ni-n9qcrvqgF51aluvzH5A.png" /></figure><p>Businesses in ChatGPT launched in beta on November 10, 2026. Here is what to verify, what to align with your website, and when it is worth handing the clicks to someone else.</p><p>I am Alan Swenson, a freelance web developer in Northeast Minneapolis. Most of my work is ordinary and fussy: what the homepage says, what the contact page says, and whether those two pages describe the same company. In November 2026 that job picked up a new surface.</p><p>On November 10, OpenAI introduced <a href="https://openai.com/index/introducing-businesses-chatgpt/">Businesses in ChatGPT</a> in beta. Through Business Hub, a business can claim its listing, update the details ChatGPT should use, and see aggregated insights such as mentions, website clicks, and the themes of conversations where the site was cited. Eligible ecommerce and online service businesses in the United States can go further: product feeds, and a Business Agent that answers on the business profile. Shopify merchants can verify from Business Hub or from the Shopify admin.</p><p>Because this is still a beta, plenty of local and niche businesses have not claimed a profile. Until they do, ChatGPT fills the gaps from whatever it already has. An old directory. A city you left. A logo from the last redesign.</p><p>This is the checklist I use so the listing does not teach the model the wrong business. I also do this as a fixed $100 setup for owners who do not want to spend the afternoon in the Hub. That offer is one section near the end, with an explicit list of what it leaves out. You can do the rest yourself.</p><h3>What you can actually change</h3><p>People already ask ChatGPT which vendor to hire, what a company does, and whether a product fits. Business Hub is the control OpenAI gave businesses for those answers: current facts, supplied by you, instead of inferred from a scrape.</p><p>After you claim and verify, you can update the core details. If you are eligible, you can add a self-serve product feed with pricing and availability. The insights screens are aggregated. OpenAI says it does not hand over individual user conversations, and some of those charts appear only after there is enough volume to clear a privacy threshold.</p><p>A Business Agent is a different product. It is a labeled conversation on your ChatGPT profile, separate from the reader’s normal chat with ChatGPT. OpenAI is starting with eligible ecommerce and online service businesses in the U.S. Claiming the listing does not give you an agent. An agent that launches on top of a sloppy listing just repeats the mistake in a friendlier tone.</p><p>Claiming is the foundation. Feeds, agents, and any later ad placement sit on top of the facts you submit. If those facts contradict the website, the rest of the stack inherits the contradiction.</p><h3>A claim is not the same as being described correctly</h3><p>Business Hub is not your website, and it is not a Google Business Profile you set once. Models mix signals. If the Hub description says Minneapolis and the site says St. Paul, or the phone number in the profile does not match the contact page, you have handed the system two businesses and asked it to pick.</p><p>The fix has a plain name: <a href="https://alanswenson.dev/glossary/entity-consistency/">entity consistency</a>. Same legal name, same URL, same phone, same service area, same short description, on the site and in the Hub and on the other profiles you already keep (LinkedIn, directories, the footer of a client site). The claim is step one. Agreement across those surfaces is how a follow-up question stays attached to the right URL.</p><p>I cannot promise ChatGPT will mention you, cite you, or send you a customer. Nobody honest can. What you can do is stop publishing a second, older version of the company.</p><p>Before you pay anyone, including me, check three things:</p><ol><li>The site is live on HTTPS, under the business name you want cited, with a contact page a stranger could trust.</li><li>The city and service area on the homepage match how you actually work. Do not “tune” the Hub to a city you do not serve.</li><li>You can pass the identity check. OpenAI requires an identity verification step (currently Persona) before profile edits go live. If you cannot complete that, the listing stalls no matter who is clicking.</li></ol><h3>A checklist for one afternoon</h3><p>Block a single sitting. The form work usually fits in that window. Publication does not. OpenAI reviews and publishes edits on its own schedule, and that can take hours or longer. You do not control that clock, so do not book a campaign launch on the assumption that the profile is live tonight.</p><p><strong>Confirm you are eligible before you invest the afternoon.</strong> OpenAI says any eligible business in a place where ChatGPT is available can claim through Business Hub. That still leaves gaps: regions and categories they do not support, and owners who cannot finish identity verification. Existing OpenAI Enterprise customers and advertisers are pointed to their OpenAI representative instead of the self-serve flow.</p><p><strong>Search for the real listing, then start the claim.</strong> Look the business up in Business Hub. You want the organization you actually operate, not a near-name, an old location, or a listing someone else created from a directory. Claiming the wrong record is slower than starting clean.</p><p><strong>Verify with a method you control.</strong> OpenAI offers different proofs depending on the business. The ones I see owners get through are phone, email, a DNS TXT record on the domain, and Shopify admin for Shopify merchants. The DNS TXT record is the cleanest match to the outcome you want: proof that you control the domain on the website ChatGPT should cite. Do not verify a domain you are about to abandon.</p><p><strong>Finish the identity check yourself.</strong> Have a government ID ready and complete Persona in your own name. I will sit on a call and tell you which screen you are on. You should complete that check yourself. A stranger should not do it for you.</p><p><strong>Set the fields models repeat.</strong> Website URL, short description, category, logo, and policy links. Write the short description from the homepage you would send a customer, not from a directory you last touched in 2019. Use the logo that is on the live site today. Pick the closest honest category. A trendy category you do not belong in is a mismatch, same as a wrong city.</p><p><strong>Read the draft against the homepage and the contact page.</strong> Four items: name, URL, phone, service area. If any of them disagree, fix the site or fix the Hub before you submit. Inconsistent geography is a common way a model describes one company as two.</p><p><strong>Write down what is verified and what is still pending.</strong> Note the verification method, the URL you submitted, and where Business Hub keeps insights. You will not remember the path in a month, and OpenAI will not email you a tidy recap of your own form.</p><h3>What I would leave for later</h3><p>Leave the Business Agent until the listing matches the site. Leave the product feed until you actually sell products and you have confirmed you are in an eligible category. A feed is not a substitute for a page that says, in sentences, what you do and where you do it.</p><p>Leave ChatGPT Ads alone in week one. Sponsored placements are a different decision, they are labeled as ads, and they are a poor use of attention if the profile they land on still has last year’s phone number.</p><p>And leave the broader “AI search” rebuild for a later project. The move that matters while this product is new is small: one claimed profile, facts that match the site, and a contact path that works when someone does click through.</p><h3>If you want me to run the Hub flow</h3><p>I offer a fixed $100 setup for one business and one ChatGPT profile. You still approve verification messages, and you still complete the identity check. I drive the Business Hub clicks and compare the profile to your live site before we call it done.</p><p>For that fee I will:</p><ul><li>Check eligibility before you pay, and stop if the category, region, or verification path will not work.</li><li>Start the claim and walk through phone, email, DNS TXT, or Shopify, whichever OpenAI offers for your business.</li><li>Stay on the Persona step with you. You complete it.</li><li>Set the website URL, short description, category, logo, and policy fields so they match the live site.</li><li>Compare name, URL, phone, and service area to the homepage and the contact page.</li><li>Send short handoff notes: what was verified, what is still in review, and where to find insights.</li></ul><p>The $100 does not include ongoing monitoring, a Business Agent, a product feed, ChatGPT Ads, a legal review of your policies, or repairs to a broken website. Wrong hours, a failed SSL certificate, and thin service pages are website problems. I fix those as separate work, and I will say so before any invoice. I also will not promise a publish time, a ranking, or that an assistant will mention you. The job is claim, verification, and alignment with the site you already have.</p><p>The field-by-field version, is the <a href="https://alanswenson.dev/guides/chatgpt-business-hub-setup/">ChatGPT Business Hub setup guide</a> on my site. If you want to do the session with me, fill out the <a href="https://alanswenson.dev/?service=chatgpt-business-hub#contact">contact form</a>.</p><h3>About Me</h3><p>I am a freelance web developer in Northeast Minneapolis, serving businesses across the Twin Cities and remotely. You work directly with me, at a fixed price agreed before the work starts. The Hub setup is a small, specific job. It is not a retainer, and it is not a guarantee of customers.</p><p>OpenAI product names, Business Hub, and Businesses in ChatGPT are trademarks of OpenAI. I am not affiliated with OpenAI. This is an independent setup service for business owners.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91263dcb588f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating a Mac shortcut to mute an unmute Zoom from outside the app]]></title>
            <link>https://medium.com/@aswens0276/creating-a-mac-shortcut-to-mute-an-unmute-zoom-from-outside-the-app-5823bb79d8e2?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/5823bb79d8e2</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[mac]]></category>
            <category><![CDATA[shortcuts]]></category>
            <category><![CDATA[karabiner-elements]]></category>
            <category><![CDATA[zoom]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Fri, 21 Aug 2020 17:28:28 GMT</pubDate>
            <atom:updated>2020-08-21T17:28:28.083Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>The Problem:</strong> Multi-tasking while using Zoom and being able to quickly un-mute when my input is requested.</p><p><strong>The Solution:</strong> Create a global shortcut that works no matter which App I am using, aka Chrome, Terminal, VSCode etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*i6YcXeYjpcOiBXXp" /><figcaption>Photo by <a href="https://unsplash.com/@cwmonty?utm_source=medium&amp;utm_medium=referral">Chris Montgomery</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><strong>Step 1:</strong> Install <a href="https://karabiner-elements.pqrs.org/">Karabiner Elements</a></p><p>I researched a few different Mac apps that translate key presses but settled on <a href="https://karabiner-elements.pqrs.org/">Karabiner Elements</a>. It is open source and free and in the end had the features to accomplish my goal. This requires a little bit of programming knowledge but was customizable in that way as well.</p><p><strong>Step 2:</strong> Copy the following code and save it as a .json file. It doesn’t matter where for now. I called it zoom.json</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd7bdacac53ac9d09a0726cedac89c7a/href">https://medium.com/media/dd7bdacac53ac9d09a0726cedac89c7a/href</a></iframe><p>Description of what is happening in the code above. I have set a title and description. These are just for humans. If you don’t like mine, feel free to change it to whatever you like and it’s wont affect the usefulness of the code at all. The next important key is “from”. This is the new shortcut we are creating. In my case I have made it “left option + z”. If you would rather a different shortcut to trigger, this is where you would change it. Next is the “to” key. This is the first event that happens after your shortcut is pressed. In this case it opens the zoom.us app. If you have installed zoom in a different location you will need to change the path here. Next we have a section called “to_delayed action”. This event is delayed by 500ms by default and if you would like to change the delay length you can in the “parameters” key on line 9. If all of this is written in a human readable sentence it would read.</p><p>When left option + z is pressed by the user open zoom and start a 500ms timer, after the timer is up simulate the pressing of left command + left shift + a.</p><p>We need this short delay to allow Zoom to come into focus before hitting the shortcut that allows us to mute/unmute ourselves. If your computer is slower you may need to lengthen that delay time.</p><p><strong>Step 3:</strong> Copy the script.</p><p>Now that you know what this script does and have edited it to your exact needs we need to copy it to allow Karabiner to enable it.</p><p>Use terminal to navigate to your new script.</p><p>If you labeled your script zoom.json like I did this is the code to copy it from terminal. Else change zoom.json to what you named your script.</p><pre>cp zoom.json ~/.config/karabiner/assets/complex_modifications</pre><p><strong>Step 4:</strong> Enable your new shortcut.</p><p>Launch Karabiner-Elements. Navigate to the Complex modifications tab.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ngyu868_6OvI4esc-gHq0w.png" /></figure><p>Click “Add rule” near the bottom left.</p><p>You will see your new rule at the bottom. See screenshot.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ND1Pf6lOaXWxTFoBkvWCcQ.png" /></figure><p>Click enable. Then test it out to make sure it’s working as intended. And thats it. Enjoy!</p><p><strong>Note:</strong> If you make changes (like changing the shortcut to another key combination) you have to repeat the copy and enabling steps.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5823bb79d8e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[VSCode Pylint setup and settings for Python Flask with SQLAlchemy]]></title>
            <link>https://medium.com/@aswens0276/vscode-pylint-setup-and-settings-for-python-flask-with-sqlalchemy-7ade0f14f321?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ade0f14f321</guid>
            <category><![CDATA[python-flask]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[sqlalchemy]]></category>
            <category><![CDATA[vscode]]></category>
            <category><![CDATA[pylint]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Fri, 08 May 2020 19:16:17 GMT</pubDate>
            <atom:updated>2020-05-08T19:24:45.007Z</atom:updated>
            <content:encoded><![CDATA[<p>I wanted to share my VSCode settings for Pylint when creating a Flask app using SQLAlchemy.</p><pre>pip install pylint<br>pip install pylint-flask<br>pip install pylint-flask-sqlalchemy</pre><p>2. Next step is Select your Linter</p><pre>shift+command+P on Mac</pre><p>type ‘Python: Select Linter’ on the box that pops up and then select ‘ <strong>Pylint</strong> ‘</p><p>3. Now we need to enable the linter.</p><pre>shift+command+P on Mac</pre><p>type ‘Python: Enable Linting’ on the box that pops up and then select ‘ <strong>on</strong> ‘</p><p>4. Now Pylint will be <em>working</em> but not in the ideal way for our setup. So we want to change some settings now.</p><pre>shift+command+P on Mac</pre><p>type ‘Preferences: Open Workspace Settings’ and select it to open the file. (alternately if you want to set this up globally you will use ‘Preferences: Open Settings (JSON)’</p><p>Below is the settings to add to your settings file. The first 3 lines should already be in the file from the previous steps mainly what we are adding now is the <strong>python.linting.pylintArgs. </strong>We are adding the ‘- load-plugins’ to load the 2 plugins we installed earlier. We are also adding ‘ — init-hook’ to tell pylint where the top level of your project is. This will help it correctly identify (or really, stop identifying) import errors that aren’t really errors in Flask.</p><pre>&quot;python.linting.pylintEnabled&quot;: true,</pre><pre>&quot;python.linting.enabled&quot;: true,</pre><pre>&quot;python.linting.pylintUseMinimalCheckers&quot;: false,</pre><pre>&quot;python.linting.pylintArgs&quot;: [</pre><pre>&quot;--load-plugins&quot;,</pre><pre>&quot;pylint_flask_sqlalchemy, pylint_flask&quot;,</pre><pre>&quot;--init-hook&quot;,</pre><pre>&quot;import sys; sys.path.insert(0, &#39;/path/to/root/of/current/project&#39;)&quot;</pre><pre>]</pre><p>That’s it! At this point VSCode should be linting your project and helping you find linting problems in your code. If it does not start automatically linting you may want to restart VSCode but you probably wont have to do that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pwmiM4F4lXnYPbxv" /><figcaption>Photo by <a href="https://unsplash.com/@lazycreekimages?utm_source=medium&amp;utm_medium=referral">Michael Dziedzic</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ade0f14f321" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring Python Flask Environment Variables with Environs module]]></title>
            <link>https://medium.com/@aswens0276/refactoring-python-flask-environment-variables-with-environs-module-d0e1850c89eb?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0e1850c89eb</guid>
            <category><![CDATA[pipenv]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[travis-ci]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[automated-testing]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Wed, 20 Feb 2019 00:47:31 GMT</pubDate>
            <atom:updated>2019-02-20T00:47:31.071Z</atom:updated>
            <content:encoded><![CDATA[<p>In my last <a href="https://medium.com/@aswens0276/using-pytest-to-setup-dynamic-testing-for-your-flask-apps-postgres-database-locally-and-with-39a14c3dc421">medium post</a>, I went over my method of dynamically testing my Postgresql database using Pytest. This post is a refactoring of the way we get our environment variables from out .env file which substantially increases readability and drastically reduces the lines of code. Here is the <a href="https://github.com/AlanSwenson/flask-demo-using-environs">full project</a> on github if you want to follow along.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YpNolSGBK69bn3JP" /><figcaption>Photo by <a href="https://unsplash.com/@szmigieldesign?utm_source=medium&amp;utm_medium=referral">Lukasz Szmigiel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Why Environs?</h3><p><a href="https://github.com/sloria/environs">Environs</a> is a python module that lets us change the way we import environment variables and, in my opinion, has 3 major benefits.</p><ol><li>It is very readable and therefor easier to debug in the future and clearer for others to use if they want to reuse your code in their projects as well.</li><li>It dramatically reduces lines of code. My example goes from 67 lines to a paltry 25.</li><li>It plays VERY nicely with pipenv. You don’t have to restart your shell every time you change or add a variable to your .env</li></ol><h3>Our Starting Point</h3><p>Here is the original config.py file before refactoring.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c30176b197c8e97962989a3d7a99854/href">https://medium.com/media/2c30176b197c8e97962989a3d7a99854/href</a></iframe><p>As you can see, it is not very DRY and really needed some refactoring. This is where environs comes in.</p><h3>Here we go</h3><p>First install environs. I use pipenv so if you use another environment manager and package installer you can, of course, follow your normal procedure for installing python modules.</p><pre>pipenv install environs</pre><p>Next, in our config.py file we need to import Env from environs. Then we can use the env.read_env() method to store all the environment variables from our .env file into the env object. This is why it doesn’t matter if we change the .env file without reopening the shell. Normally, the .env variables are loaded into the shell with pipenv shell. But with environs we are loading them with read_env() instead. I really love this extra feature because it helps avoid the pain of debugging your app when the only problem was that you forgot to restart the shell.</p><pre>from environs import Env</pre><pre>env = Env()<br>env.read_env()</pre><p>When we want to read variables out of the env object we do something like this:</p><pre>env.str(&quot;YOUR_ENV_VARIABLE_NAME&quot;, default=&quot;some default value&quot;)</pre><p>This grabs the YOUR_ENV_VARIABLE_NAME variable and if none exists it sets your default value instead. And in this case I am using .str to make sure its a string.</p><h3>Prefixed Environment Variables</h3><p>With a default value, this can cut some repeated code, but what I really like is with env.prefixed . You can use prefixes of your choice in a function to grab all the variables with that prefix. Here’s how I am using it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db3813e43b3ec0726bae528733e11f5c/href">https://medium.com/media/db3813e43b3ec0726bae528733e11f5c/href</a></iframe><p>I call this function by passing it a string like DEV_ and then it will grab the variables that start with DEV_. aka DEV_SQLALCHEMY_DATABASE_URI, DEV_DEBUG and DEV_TESTING . DEV_TESTING does not exist in my .env file so it gets set to False since that is what the default says. You’ll notice that I am using env.bool since it is not a string in this case.</p><h3>Refactored</h3><p>Here is the completely refactored config.py using all the ideas shared above.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c4bb025d24da788ac56634b78d456c1/href">https://medium.com/media/9c4bb025d24da788ac56634b78d456c1/href</a></iframe><p>It is sooooo much shorter and easier to read and it just makes me feel better looking at it compared to the original.</p><p>Let’s also compare the .env files.</p><p>Here is the original:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fece75ca8f95e1cec4dca2fe8c2d0d3/href">https://medium.com/media/7fece75ca8f95e1cec4dca2fe8c2d0d3/href</a></iframe><p>And here is the refactored version:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79c004a8cbda3d840ee76b60a075c803/href">https://medium.com/media/79c004a8cbda3d840ee76b60a075c803/href</a></iframe><p>Again, easier to read and shorter.</p><p>Here is how I have set up the new environment variables in travis, since they are different with the refactor than in the previous iteration of this project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z-9inZPzI7fpIHYxV3_wEw.png" /></figure><p>Environs is very easy to use and implement and for me its a no-brainer for future projects. What have you done with Environs?</p><h3>Connect with me</h3><p>Follow me on <a href="https://github.com/AlanSwenson">Github</a> and <a href="https://twitter.com/AlanSwenson">Twitter</a> and send me a message if you have any questions or want to talk about Python/Flask/Postgres/Zappa</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0e1850c89eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Using Pytest to setup dynamic testing for your Flask app’s Postgres Database Locally and with…]]></title>
            <link>https://medium.com/@aswens0276/using-pytest-to-setup-dynamic-testing-for-your-flask-apps-postgres-database-locally-and-with-39a14c3dc421?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/39a14c3dc421</guid>
            <category><![CDATA[continuous-integration]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[travis-ci]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Thu, 31 Jan 2019 20:38:42 GMT</pubDate>
            <atom:updated>2019-01-31T20:38:42.152Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using Pytest to setup dynamic testing for your Flask app’s Postgres Database Locally and with Travis-CI</h3><p>This is my extension of the article <a href="https://xvrdm.github.io/2017/07/03/testing-flask-sqlalchemy-database-with-pytest/">Testing Flask SQLAlchemy database with pytest</a>. So for some context check that one out as well. What I wanted to do that was lacking in the article was to test multiple app configurations, specifically a development, testing, and production configuration all from the same setup. The article mentioned above ends with just a testing setup. If you would like to follow along with the code presented in this article, here it is on github <a href="https://github.com/AlanSwenson/flask-postgres-travis-dynamic-testing">flask-postgres-travis-dynamic-testing</a>. Also, if you want to know how to setup the basic app before we add the testing check out my previous article <a href="https://medium.com/@aswens0276/initializing-a-postgres-db-using-zappa-from-your-serverless-flask-lambda-function-25f42eee63fe">Initializing a Postgres DB using Zappa from your Serverless Flask Lambda Function</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*O0Y7po59TRoqLbaj" /><figcaption>Photo by <a href="https://unsplash.com/@chesteralvarez?utm_source=medium&amp;utm_medium=referral">Chester Alvarez</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>The File Tree</h3><p>Let’s start with a good ol’ file tree so you can see where all the files go. I know these help me a lot when I am learning something. You’ll see, just as in the previous article, I have setup the app to use an App Factory and Blueprints with the addition of a new test folder for our tests and supporting files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*ZOIGIe0bPbUDS_t8n-IvfQ.png" /><figcaption>File Tree for our project</figcaption></figure><h3>Closures with Yield</h3><p>What made this difficult for a code newbie like me was two-fold. First, I needed to pass my Config object to the newly created test app, but the pytest fixture does not allow me to pass variables to it. This led me to a pattern I learned about a little while ago but never had they chance to put into practice until now, Closures. This nested function lets me pass my Config variable to a function that normally would not allow it by introducing it into the scope of the function I am trying to use. Second, is this weird part that I could not find by googling and took some real head pounding to figure out. Using a yield instead of return in your closure. Here is what I came up with.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68d399225eb7a9e53c0621abe01fc943/href">https://medium.com/media/68d399225eb7a9e53c0621abe01fc943/href</a></iframe><p>Line by line, Ill explain what is going on here.</p><p>Line 8 is the pytest fixture so we can use this function on our tests later on.</p><p>Line 24 you will notice that our outer function uses yield instead of a normal return as mentioned above.</p><p>Everything below the yield is our teardown after the test is run. The session is removed, then we check to see if we used the TestingConfig for this particular test and if we did we drop all tables form the DB. We can’t check for the config_class variable as it out of scope at this point, that is why we are checking the DB itself.</p><p>Line 11 is actually creating the app given our config _class</p><p>Line 12 pushes the testing context so we can access this app</p><p>Line 14 checks to see if we are setting up with TestingConfig and if so we drop all the tables (incase some were left over somehow from a previous iteration) then we import our database models and create all the tables for use in testing. We don’t want to do any of this in for our other configurations, that would be really bad to drop all your production tables. That is why I have it set up this way. One testing setup app that handle my 3 different setup configs. But if you have a ton of testing configs you just make more config objects and test away.</p><h3>A new config.py File</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c30176b197c8e97962989a3d7a99854/href">https://medium.com/media/2c30176b197c8e97962989a3d7a99854/href</a></iframe><p>The new pattern for config.py is to make a base Config object and then create out 3 types of configs as new Config objects with either different or additional options for our app setup. I believe it looks very clean and easy to understand. (side note: you can see I have left myself a TODO: in here for the future. I love using TODO: when I think something needs to be refactored later or if I think of a new feature I am not ready to implement quite yet but don’t want to forget.)</p><p>All this works because we have our App Factory model in place here:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7dde0c147aa9e41df5c6e4764e1c453b/href">https://medium.com/media/7dde0c147aa9e41df5c6e4764e1c453b/href</a></iframe><p>Lines 13 and 14 are for extra credit if you are using <a href="https://github.com/Miserlou/Zappa">zappa</a> but you don’t need these lines if you are not. They tell zappa to use your ProductionConfig (assuming zappa is where your production app lives) But nothing else has changed from our previous setup for the app factory.</p><h3>Now for some tests:</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afe3edc5acdfa1808c34f5293b804aa5/href">https://medium.com/media/afe3edc5acdfa1808c34f5293b804aa5/href</a></iframe><p>The first thing you notice is on line 13–15 we have a decorator that lets us skip our DevelopmentConfig when we are using <a href="https://travis-ci.com/">Travis CI</a>. DevelopmentConfig is what I am using for local testing, so it isn’t relevant to Travis CI. You may also choose to skip your production config while testing on Travis but I have not chosen to do that. Locally all 3 of these tests run, on Travis CI only 2 do. These 3 tests load the relevant Config object into our test app and then check to make sure everything loaded correctly. This is very much an adaptation from the original article <a href="https://xvrdm.github.io/2017/07/03/testing-flask-sqlalchemy-database-with-pytest/">Testing Flask SQLAlchemy database with pytest</a>.</p><h3>Now for a <em>real</em> test.</h3><p>Here we will test creating the tables, inserting an item of type Model1 and then checking to make sure and row was added to the table and that there is exactly one thing in the DB. Then of course our setup drops the tables to complete. You could, of course, add more tests to this for example, querying to make sure this exact row is in the db and testing to make sure a false item isn’t in the database. You can use this to test deleting items, etc. I left it simple because the tests are literally endless for this. You can now test ALL your methods with a fresh DB every time that removes itself when done testing.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b59af34ec000995df9ac81478bed299/href">https://medium.com/media/6b59af34ec000995df9ac81478bed299/href</a></iframe><h3>The .env File</h3><p>Here is an example .env file for use locally. Obviously, you will need to change the development and production variables to suite your needs but the testing one is set exactly for this demo.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fece75ca8f95e1cec4dca2fe8c2d0d3/href">https://medium.com/media/7fece75ca8f95e1cec4dca2fe8c2d0d3/href</a></iframe><h3>Getting it all working with Travis CI</h3><p>Travis is one of my favorite things, it really makes sure all the boxes are checked before a pull request of pushing to production etc. They way we have set up our app with pytest works perfectly with travis. Check out the .travis.yml file below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24478356085550b345aa3ce5431c5095/href">https://medium.com/media/24478356085550b345aa3ce5431c5095/href</a></iframe><p>There are 3 sections with relevance to this article. The first one is addons, in while i am using postgresql: &quot;9.5&quot;, but depending on your app you may need a different version. The next is services which we are using postgresql. This lets travis know to actually load postgres so we can use it. And the third section of interest is before_script. Line 13 calls psql creates a DB named “travis_ci_test” with the user postgres and no password. This is the same as our local testing database which is why we can test in the cloud with travis. Dont forget to load you ENV variables to Travis or it will surely fail. If you are unfamiliar of how to do this, it is in the more options menu for you rpoject on Travis and then settings. It should look like this when you have them all set.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5Nplnl0xMygK8LRdmo1Aaw.png" /><figcaption>Travis CI .env setup</figcaption></figure><h3>Common mistakes</h3><p>Not adding __init__.py in your test folder. Pytest will not find you imports correctly unless you have this file in you test folder. Mine is blank but it exists.</p><p>Forgetting to set your .env variables locally or on Travis. In addition, if you are using <a href="https://github.com/pypa/pipenv">pipenv</a> like I am. It is easy to forget you need to exit and restart your shell after adding or changing .env variables.</p><h3>Connect with me</h3><p>Follow me on <a href="https://github.com/AlanSwenson">Github</a> and <a href="https://twitter.com/AlanSwenson">Twitter</a> and send me a message if you have any questions or want to talk about Python/Flask/Postgres/Zappa</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=39a14c3dc421" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Initializing a Postgres DB using Zappa from your Serverless Flask Lambda Function]]></title>
            <link>https://medium.com/@aswens0276/initializing-a-postgres-db-using-zappa-from-your-serverless-flask-lambda-function-25f42eee63fe?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/25f42eee63fe</guid>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Tue, 18 Dec 2018 03:29:25 GMT</pubDate>
            <atom:updated>2018-12-18T03:29:25.727Z</atom:updated>
            <content:encoded><![CDATA[<p>First off, why should we even use Lambda instead of EC2 or any other deployment method for your Python app? Well, I’m glad you asked. $$$$$ is the main answer. Your app will only run when someone uses it, instead of just sitting there running at 3am all by itself. And AWS gives you 1 MILLION requests for free, even after you have used up your free year. And you get 400,000 GB/Seconds for free as well. So, for a long time you literally can run your app for free. Also, it’s really fast. If you want to know more about the benefits of Lambda check out <a href="https://www.youtube.com/watch?v=gSae_ZBBbkc">this video</a>.</p><p>Zappa is a cool way (easy way) to deploy your python app to Lambda. But I found, being a code newbie, initializing some fresh tables in my PostgreSQL database on RDS from the lambda to be a bit tricky. There is <a href="https://github.com/Miserlou/zappa-django-utils">documentation</a> about how to do this with Django but doing this in Flask, I found there is no specific documentation on how to do it. With a little trial and error using <a href="https://github.com/Miserlou/Zappa#remote-function-invocation">zappa’s remote function invocation</a> documentation I have figured it out and this is my attempt to show you how it’s done.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/442/1*DGoR_iwYn_fN8FTIhBsyDA.png" /><figcaption>Project tree for Zappa Flask Example Project</figcaption></figure><p>Above is the structure for my sample project. You can check it out on <a href="https://github.com/AlanSwenson/zappa-flask-db-example">github</a>. The next part is a bit dense, but stick with me, as it gets much easier after this.</p><p>Circular imports can become a common problem when trying to set up a function that creates your DB tables. To avoid this, the project uses the <a href="http://flask.pocoo.org/docs/1.0/patterns/appfactories/#factories-extensions">app factory model</a>. In short, instead of just creating the Flask app, we use a function <strong>create_app()</strong> to build the app. This function is in <strong>project/__init__.py </strong>. We also us Flask Blueprints to setup the routes, if you want more info on blueprints check out the <a href="http://flask.pocoo.org/docs/1.0/blueprints/#blueprints">documentation</a>.</p><h3><strong>run.py</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00e2de949cb90ba5c5aaf79540099da7/href">https://medium.com/media/00e2de949cb90ba5c5aaf79540099da7/href</a></iframe><p>Very simply, this is how we run our project. Notice we are creating the Flask object with <strong>create_app()</strong> from <strong>project/__init__.py</strong>.</p><h3>project/__init__.py</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fbb18d03e495b6ad313bb7e62f54555/href">https://medium.com/media/6fbb18d03e495b6ad313bb7e62f54555/href</a></iframe><p>This is where the app factory lives. You need to do this, as this is how we can use our <strong>create_db.py</strong> to create fresh database tables without circular imports etc.</p><h3>zappa_settings.json</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/85648dc7c68720ea0c4f55227b18a148/href">https://medium.com/media/85648dc7c68720ea0c4f55227b18a148/href</a></iframe><p>Here is where you may need a little more context. First off, make sure you .gitignore this bad boy to make sure your secrets stay secret.</p><p><strong>development_stage = anything you want here</strong>, normally dev or prod or staging but you decide. It will be used to deploy your zappa and to create your db tables. (if you use <a href="https://github.com/Miserlou/Zappa#running-the-initial-setup--settings">zappa init</a>, this is part of that setup)</p><p>app_function = since we called our file run.py thats why its called run.app, but you could change it as you see fit, just change them in sync.</p><p>environment_variables = set these so that config.py knows how to access your postgreSQL DB. (this is really the only things you need to add after zappa init create you original zappa_settings.json)</p><p>There are tons more things you can set in this file, just check out the <a href="https://github.com/Miserlou/Zappa">Zappa documentation</a> for clarity.</p><h3>create_db.py</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65c79dc7bce8f46c9dcbb587de5e41ac/href">https://medium.com/media/65c79dc7bce8f46c9dcbb587de5e41ac/href</a></iframe><p>This is where the magic of this article happens. We can call the db_init() function to create all the tables in our DB. It is important that this file is on our top level and not contained within project.</p><h3>config.py</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e068f3d9c3c3c1531830e6b716a3889/href">https://medium.com/media/7e068f3d9c3c3c1531830e6b716a3889/href</a></iframe><p>Nothing special here. Just a way to get those sweet, sweet DB credentials into the app factory and not expose any secrets but pulling them from environment variables set up in zappa_settings.json</p><p>Now the fun part!</p><p>If you need help deploying you app with zappa, check out the <a href="https://github.com/Miserlou/Zappa">documentation</a>. It’s really easy and very clear. And if you are new to making a flask app check <a href="http://flask.pocoo.org/">this link</a> on creating a basic flask app to get you started. Assuming you have everything setup as I have laid out and described above, all you need is this simple little line of code to get those new tables where they belong.</p><pre>zappa invoke &lt;development_stage&gt; &quot;create_db.db_init&quot;</pre><p>This is literally it. Fill in <strong>&lt;development_stage&gt;</strong> with whatever you called it in <strong>zappa_settings.json</strong> and make sure you leave those double quotes in there. What happens is zappa finds the <strong>create_db.py</strong> file and calls the db_init function within it. I know that was a lot of words for what looks like a simple command in the end but it took me a while to figure this out based on the actual zappa documentation in combination with how Flask works. I hope this helps you with your serverless python apps in the future.</p><p>Follow me on <a href="https://github.com/AlanSwenson">Github</a> and <a href="https://twitter.com/AlanSwenson">Twitter</a> and send me a message if you have any questions or want to talk about Python/Flask/Zappa/Lambda</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zTE2jlbIlZdgt6fn" /><figcaption>Photo by <a href="https://unsplash.com/@spiritvisionstudios?utm_source=medium&amp;utm_medium=referral">Ben Rosett</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25f42eee63fe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fixing psycopg2 Warning for Flask-SQLAlchemy]]></title>
            <link>https://medium.com/@aswens0276/fixing-psycopg2-warning-for-flask-sqlalchemy-697e2430783e?source=rss-2f1908dd9f11------2</link>
            <guid isPermaLink="false">https://medium.com/p/697e2430783e</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[flask]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Alan Swenson]]></dc:creator>
            <pubDate>Thu, 22 Nov 2018 22:42:10 GMT</pubDate>
            <atom:updated>2018-11-22T22:42:10.502Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cgoNc2wXkDieN8mC" /><figcaption>Photo by <a href="https://unsplash.com/@prestongoff?utm_source=medium&amp;utm_medium=referral">Preston Goff</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>While working on my first app, an <a href="https://github.com/AlanSwenson/amz-review-scraper">Amazon Review Scraper</a>, I encountered a really annoying warning using SQLAlchemy with my Postgres database.</p><pre>UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use “pip install psycopg2-binary” instead. For details see: &lt;http://initd.org/psycopg/docs/install.html#binary-install-from-pypi&gt;.</pre><p>It’s just a warning, but every time I save something to the database there it is annoying me with its words about future incompatibility. It basically even tells me how to fix it. Here’s where the real problems start. First, if you follow the instructions and install psycopg2-binary, well, the warning persists. Now you have both! But Flask-SQLAlchemy is just ignoring the binary version and still throwing warnings at me left and right. What the warning should have told me is this. Uninstall psycopg2, then install pyscopg2-binary. So, my thoughts were to uninstall psycopg2, that makes sense right? Nope, now i get errors because there is no psycopg2 installed…..what? What about psycopg2-binary, why is it still not working? Checking my installed dependencies shows it’s there…what is happening?</p><h3><strong>How I understand what’s going on</strong></h3><p>With both packages installed, when you uninstall pyscopg2, it uninstalls both packages but psycopg2-binary still shows as being installed but that’s in name only, none of it’s contents are there anymore.</p><h3>The Fix — TL;DR</h3><p>Ok, here is what you need to do.</p><p>Uninstall <strong>BOTH</strong> pyscopg2 and psycopg2-binary.</p><p>Then, install <strong>ONLY</strong> psycopg2-binary.</p><p>Look what happened, no warnings, no errors.</p><h3>Conclusions</h3><p>I am sure it is because I am new to all of this but I could not find this information anywhere about this topic. I think people assume you would know you should uninstall psycopg2 before then install psycopg2-binary but as this was a dependency of Flask-SQLAlchemy in the first place I really didn’t know that was what I was meant to do. Hopefully this is helpful to anyone that finds themselves in this same situation as me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=697e2430783e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>