<?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 Mark Lavin on Medium]]></title>
        <description><![CDATA[Stories by Mark Lavin on Medium]]></description>
        <link>https://medium.com/@markdlavin?source=rss-b46afa4da29f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*fqb167cmu2tFbXsj</url>
            <title>Stories by Mark Lavin on Medium</title>
            <link>https://medium.com/@markdlavin?source=rss-b46afa4da29f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:00:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@markdlavin/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[Pricing Option Credit Spreads with Python]]></title>
            <link>https://medium.datadriveninvestor.com/pricing-option-credit-spreads-with-python-6d6acb6b1609?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6d6acb6b1609</guid>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[options-trading]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[finance]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Tue, 16 Feb 2021 16:38:44 GMT</pubDate>
            <atom:updated>2021-02-16T16:38:44.745Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oY5AOwj2U-gDQEqR" /><figcaption>Photo by <a href="https://unsplash.com/@chrisliverani?utm_source=medium&amp;utm_medium=referral">Chris Liverani</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p><a href="https://medium.com/datadriveninvestor/exploring-option-credit-spreads-6c8748e6b734">In an earlier post</a> I mentioned I was playing around with option trading strategies. Now I have some more progress. First, a quick TL;DR from the previous work. The plan is to sell out of the money call options and then buy them at a higher strike, netting the difference as profit. To hedge this position I’ll buy the underlying stock.</p><p>Now I want to show how I’ve been searching the market data for opportunities. There are a lot of competing factors so I’ll have to weigh them against each other. I want:</p><ul><li>Maximal upfront premium payment</li><li>Buy/sell ratios that net near zero Δ and Γ with large positive Θ</li><li>Minimal cash requirements in stock purchase and options margin</li><li>Mid-Low stock volatility</li><li>Sufficient stock and options liquidity</li></ul><p>To simplify the search and minimize risk, I’m going to focus on option spreads between two immediate strikes. This will also keep our margin costs down though there is also less premium differences. To limit the time horizon, I’m going to focus on buying the spread a week before the options expire, hoping that they expire worthless. The plan would then be to look for the next set of options to roll into that trade for the following week.</p><p><strong><em>Disclaimer:</em></strong><em> Again I’m not a financial advisor and this is not financial advice. Options and equity trading holds risks, some of which I will discuss here. You should do your own research before investing into the stock or options market. I’ve been trading this strategy on paper as an academic exercise because I think it’s fun an interesting. You are free to use the code below for your own purpose and research but it’s provided without warranty either expressed or implied. Do want you want with your money but you’ve been warned.</em></p><h3>Gathering the Necessary Data</h3><p>I mentioned previously that I didn’t have a great source of market data but I settled on something that is workable. Yahoo Finance has free market data and there are a number of Python libraries which support accessing it. The one I settled on is <a href="http://theautomatic.net/yahoo_fin-documentation/">yahoo_fin</a>. While Yahoo does provide the implied volatility for the options, I didn’t find them to be particularly good. That’s to say when I would price the options with that implied volatility to calculate Δ, Γ, and Θ, my prices would be pretty far off from the market prices. So the method I chose instead was to take the last option price and calculate the implied volatility myself. Then taking that implied volatility I would price the option again (which should give me back the last price) to get the greek values. For the option pricing, I’m using the Python binding for <a href="https://quantlib-python-docs.readthedocs.io/en/latest/">QuantLib</a>. While their documentation isn’t great, it was enough for me to do what I needed to do.</p><pre># Standard library imports<br>import datetime<br>import re</pre><pre># Third-party library requirements<br>import pandas as pd<br>import QuantLib as ql<br>from yahoo_fin.options import get_calls<br>from yahoo_fin.stock_info import get_quote_table</pre><pre># Hard-coded ticker and expiration for example purposes<br>ticker = &quot;AAPL&quot;<br>expiration = datetime.date(2021, 2, 12)</pre><pre># Get the current stock price and dividend rate<br>info = get_quote_table(ticker)<br>current_price = info[&quot;Quote Price&quot;]<br>yield_re = re.compile(r&quot;\((?P&lt;value&gt;(\d+\.\d+))%\)&quot;)<br>try:<br>    dividend_yield = float(<br>        yield_re.search(info[&quot;Forward Dividend &amp; Yield&quot;])[&quot;value&quot;]<br>    )<br>except (KeyError, ValueError, TypeError):<br>    dividend_yield = 0.0</pre><pre># Fetch call option chain prices<br>calls = get_calls(ticker, expiration.strftime(&quot;%B %d, %Y&quot;))</pre><pre># Setup instruments for Black-Scholes pricing<br>today = ql.Date.todaysDate()<br>underlying = ql.SimpleQuote(current_price)<br>exercise = ql.AmericanExercise(<br>    today,<br>    ql.Date(expiration.day, expiration.month, expiration.year)<br>)<br>dividendYield = ql.FlatForward(<br>    today, dividend_yield, ql.Actual360()<br>)<br>riskFreeRate = ql.FlatForward(today, 0.0008913, ql.Actual360())</pre><pre>def create_option(row):<br>    volatility = ql.BlackConstantVol(<br>        today,<br>        ql.UnitedStates(),<br>        row[&quot;volatility&quot;],<br>        ql.Business252()<br>    )<br>    option = ql.VanillaOption(<br>        ql.PlainVanillaPayoff(ql.Option.Call, row[&quot;Strike&quot;]),<br>        exercise<br>    )<br>    process = ql.BlackScholesMertonProcess(<br>        ql.QuoteHandle(underlying),<br>        ql.YieldTermStructureHandle(dividendYield),<br>        ql.YieldTermStructureHandle(riskFreeRate),<br>        ql.BlackVolTermStructureHandle(volatility),<br>    )<br>    # Don&#39;t use the quoted implied vol<br>    # Calculate it out from the last price<br>    imp_vol = option.impliedVolatility(row[&quot;Last Price&quot;], process)<br>    implied_volatility = ql.BlackConstantVol(<br>        today,<br>        ql.UnitedStates(),<br>        imp_vol,<br>        ql.Business252()<br>    )<br>    process = ql.BlackScholesMertonProcess(<br>        ql.QuoteHandle(underlying),<br>        ql.YieldTermStructureHandle(dividendYield),<br>        ql.YieldTermStructureHandle(riskFreeRate),<br>        ql.BlackVolTermStructureHandle(implied_volatility),<br>    )<br>    option.setPricingEngine(<br>        ql.FdBlackScholesVanillaEngine(process, 1000, 1000)<br>    )<br>    return {<br>        &quot;Name&quot;: row[&quot;Contract Name&quot;],<br>        &quot;Strike&quot;: row[&quot;Strike&quot;],<br>        &quot;Last&quot;: row[&quot;Last Price&quot;],<br>        &quot;Bid&quot;: row[&quot;Bid&quot;],<br>        &quot;Ask&quot;: row[&quot;Ask&quot;],<br>        &quot;NPV&quot;: option.NPV(),<br>        &quot;Delta&quot;: option.delta(),<br>        &quot;Gamma&quot;: option.gamma(),<br>        &quot;Theta&quot;: option.theta() / 365,<br>        &quot;Volatility&quot;: imp_vol * 100,<br>    }</pre><pre># Filter down to only OTM strikes<br>calls = calls[calls[&quot;Strike&quot;] &gt;= current_price * 1.025]<br>calls = calls[calls[&quot;Strike&quot;] &lt;= current_price * 1.10]<br># Parse out implied volatility<br>calls = calls.assign(<br>    volatility=lambda x: x[&quot;Implied Volatility&quot;].str.rstrip(&quot;%&quot;).astype(&quot;float&quot;) / 100,<br>)<br># Price options and calculate greeks<br>options = calls.apply(create_option, axis=1, result_type=&quot;expand&quot;)</pre><p>Along with yahoo_fin and QuantLib, this makes use of <a href="https://pandas.pydata.org/pandas-docs/stable/index.html">pandas</a> for some data manipulation and filtering. This fetches the current market prices of the stocks and options for the given ticker and expiry. It filters down to only strikes which are between 2.5% and 10% out of the money (OTM) and calculates the Black-Scholes price.</p><p>One piece of market data that I need here is the risk free interest rate. Because I’m focused on options which are one week out, I decided to use the <a href="https://www.global-rates.com/en/interest-rates/libor/american-dollar/usd-libor-interest-rate-1-week.aspx">1 week LIBOR rate</a> which is what is shown in the code above. This isn’t grabbed automatically and has to be updated each week.</p><h3>Minimizing Risk and Maximizing Profits</h3><p>With the market data and now the greek values for each option, I start pricing out each option pair trying to find one with the best value. Again I’m limiting this to only strikes which are next to one another in the chain but this could be expanded to search for wider spreads.</p><pre># This code is a continuation of the same file from above<br># Pair up options with the next available option<br>pairs = pd.concat(<br>    [<br>        options.add_suffix(&quot;_1&quot;),<br>        options.shift(-1).add_suffix(&quot;_2&quot;)<br>    ],<br>    axis=1<br>)<br>pairs = pairs[pairs[&quot;Name_2&quot;].notna()]</pre><pre>def maximize_theta(row):<br>    bid, ask = row[&quot;Bid_1&quot;], row[&quot;Ask_2&quot;]</pre><pre>    strike_1, strike_2 = row[&quot;Strike_1&quot;], row[&quot;Strike_2&quot;]<br>    delta_1, delta_2 = row[&quot;Delta_1&quot;], row[&quot;Delta_2&quot;]<br>    gamma_1, gamma_2 = row[&quot;Gamma_1&quot;], row[&quot;Gamma_2&quot;]<br>    theta_1, theta_2 = row[&quot;Theta_1&quot;], row[&quot;Theta_2&quot;]</pre><pre>    def calculate_values(sell):</pre><pre>        buy = round(gamma_1 * sell / gamma_2)<br>        credit = (bid * sell * 100) - (ask * buy * 100) - ((sell + buy) * 0.65)<br>        shares = -1 * round(delta_2 * buy * 100 - delta_1 * sell * 100)<br>        delta = delta_2 * buy * 100 - delta_1 * sell * 100 + shares<br>        gamma = gamma_2 * buy * 100 - gamma_1 * sell * 100<br>        theta = theta_2 * buy * 100 - theta_1 * sell * 100<br>        share_cost = shares * current_price<br>        margin = strike_2 * buy * 100 - strike_1 * sell * 100<br>        return {<br>            &quot;Sell Contract&quot;: f&quot;{row[&#39;Strike_1&#39;]} @ {bid}&quot;,<br>            &quot;Sell Amount&quot;: sell,<br>            &quot;Buy Contract&quot;: f&quot;{row[&#39;Strike_2&#39;]} @ {ask}&quot;,<br>            &quot;Buy Amount&quot;: buy,<br>            &quot;Shares&quot;: shares,<br>            &quot;Share Cost&quot;: share_cost,<br>            &quot;Margin&quot;: margin,<br>            &quot;Credit&quot;: credit,<br>            &quot;Cost Ratio&quot;: credit / (share_cost + margin),<br>            &quot;Net Delta&quot;: delta,<br>            &quot;Net Gamma&quot;: gamma,<br>            &quot;Net Theta&quot;: theta,<br>            &quot;Risk Ratio&quot;: theta / (delta ** 2 + gamma ** 2) ** 0.5,<br>        }</pre><pre>    trades = []<br>    sell = 1<br>    values = calculate_values(sell)<br>    while (abs(values[&quot;Share Cost&quot;]) + abs(values[&quot;Margin&quot;])) &lt; 2500:<br>        if values[&quot;Shares&quot;] &gt; 0 and values[&quot;Credit&quot;] &gt; 0:<br>            trades.append(values)<br>        sell += 1<br>        values = calculate_values(sell)<br>    <br>    if trades:<br>        results = pd.DataFrame.from_records(trades)<br>        results.sort_values(<br>            by=[&quot;Risk Ratio&quot;, &quot;Cost Ratio&quot;],<br>            ascending=False,<br>            inplace=True<br>        )<br>        return results.iloc[0]<br>    return None</pre><pre>results = pairs.apply(maximize_theta, axis=1, result_type=&quot;expand&quot;)<br>results = results[results[&quot;Shares&quot;].notna()]<br>print(results)</pre><p>This is a brute force search. It continually increases the number of contracts I would sell until it puts us out of our upper bound for total share cost and margin. Here I’ve set it to $2,500. This could probably be smarter than it is so I might be missing some opportunities, but it works for now and it’s simple to understand how it works.</p><p>For each pair, I’m taking the top trade based our two ratios that I’ve come up with. First, I’m sorting by the risk ratio. I’ve defined this to be:</p><pre>Risk Ratio = (Net Θ) / √(Net Δ^2 + Net Γ^2)</pre><p>This doesn’t really have any meaning in the options pricing sense, but it does what I want it to do. In particular it rewards trades for high positive Θ paired with near zero Δ and Γ, regardless of whether those are positive or negative. This is another area worth some experimentation, but I like this for an early iteration.</p><p>The next ratio I care about is the cost ratio which has been defined as:</p><pre>Cost Ratio = (Net Credit) / (Share Cost + Margin Required)</pre><p>This rewards trades for giving us a high upfront premium credit when compared to the total amount of money required to hold the trade. Putting our money into one trade means that it won’t be able for other opportunities which might come up. If I can net a 2% credit on one spread and 1.5% on another which roughly equal risk then I’d prefer the 2%. I haven’t run this to scan every equity available but some spot checking and running against a dozen or so and 1-2% seems to be a solid percentage. Here’s an example output looking at 2/12 AAPL calls.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ECIgoNxNod1k4YxRUAkjHw.png" /><figcaption>Best AAPL option spreads for the 2/12/2021 expiration</figcaption></figure><p>Here the best spread is selling a single 141 call for $53 and buying a 142 call for $40, netting $13 upfront less $1.30 in fees for $11.70. To hedge the position, there is a purchase of 4 shares. This example was priced on Sunday 2/7 and now we can see how this trade went. If you bought the open on Monday 2/8 at $136.03 it would cost you $544.12 and selling the 141/142 spread with a $0.13 difference nets you $11.70 after fees, but requires you to keep $100 of margin. The AAPL stock closed at $135.37 on 2/12 making both of these options expire out of the money. If you sold at the close then you lost a total of $2.64 on the stock position for a net profit of $9.06 for the week and a 1.4% return on the required $644.12 capital.</p><h3>A Strategy Coming Together</h3><p>While not fully automated, this is should be really helpful to continue to test this strategy going forward. The idea is to use this script each Friday or weekend to look for spread opportunities on options expiring the following Friday. That is entering new trades each Monday and unwinding them on Friday. If there are no good opportunities for a given week then the capital would just sit idle. I’m not sure how well this will scale. At least for now my searches are for fairly small positions. While it seems feasible to get 1–3% a week on $1000-$2000, it’s less clear if you can find enough good opportunities to really expand this if you are hoping to invest $10K or beyond. That doesn’t limit its fun as an experiment, but for now there seems to be a limit on the earning potential since you can’t really compound your gains if you can’t reinvest them.</p><p>What hasn’t yet been addressed from the initial set of points is the stock volatility and liquidity. That should be the next area of focus and research while testing out this strategy. In the short term, it will likely manifest as a set of stocks which are used to scan each week.</p><p>Not quitting my day job just yet and I’m continuing to test this out on paper, making note of the trades this prescribes and what the final outcomes would be. If I can find a good (and free) source of the historical option pricing then I could do some larger scale back-testing. I hope you find this helpful and interesting and please leave a response if you have ideas on how to improve this work. Happy trading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6d6acb6b1609" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/pricing-option-credit-spreads-with-python-6d6acb6b1609">Pricing Option Credit Spreads with Python</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Exploring Option Credit Spreads]]></title>
            <link>https://medium.datadriveninvestor.com/exploring-option-credit-spreads-6c8748e6b734?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/6c8748e6b734</guid>
            <category><![CDATA[stock-market]]></category>
            <category><![CDATA[options-trading]]></category>
            <category><![CDATA[stocks]]></category>
            <category><![CDATA[finance]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Fri, 05 Feb 2021 01:36:10 GMT</pubDate>
            <atom:updated>2021-02-06T04:26:12.408Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yNDQs_Pl47tqQlah" /><figcaption>Photo by <a href="https://unsplash.com/@markusspiske?utm_source=medium&amp;utm_medium=referral">Markus Spiske</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The recent GameStop short squeeze piqued my interest in the world of finance. Back before I started software development in Python, I worked at a hedge fund administrator pricing over-the-counter (OTC) derivatives. This was mostly interest rate swaps, swaptions, and credit default swaps. We never dipped much into equities because those are readily traded on exchanges and our focus was on products which were sold outside of exchanges. But watching Reddit trying to put pressure on the hedge funds brought me back to those days back in 2008–2009 pricing swaps in NYC and I really wanted to flex those muscles again.</p><p><strong>Disclaimer: </strong><em>I’m not a financial advisor and this is not financial advice. Options and equity trading holds risks, some of which I will discuss here. You should do your own research before investing into the stock or options market. I don’t hold the positions described in this example. I’m going to warn you up front that this post is a mess of math. If you don’t know the mechanics of a stock option and wanted to learn how they work, then you are in the wrong place. However, if you want to nerd out on pricing mechanics and portfolio hedging, then you are in for a treat. With that out of the way, let’s dive in.</em></p><p>I started looking into options trading strategies and one that really stuck out to me was credit spreads. This is when you sell options at one strike price and buy them back at a different strike price, netting a profit on the difference in premiums. You can position these to either be bullish (expecting the stock price to go up) or bearish (expecting the stock prices to go down). But I’m pretty risk-adverse (and lazy) when it comes to my investments. I don’t research and pick individual stocks so, my retirement and investments are mostly in index funds. For my options strategy I wanted something that would help me lock in that initial premium spread with less worry about the direction of the stock price.</p><h3>How an Option is Priced</h3><p>There are a few factors which go into determining the price of an option: the strike price (set when you buy the contract), the current underlying stock price, the stock volatility, the stock dividend rate, the risk free interest rate, and the time until the option expires. As someone interested in trading options, I have little to no control over most of those factors except the strike price and expiration of the contracts I choose to buy or sell. Also as time goes on, the price of an option will decay over time, everything else remaining equal. That’s because there is less time for the price to move where the option might be exercised. Either the option price slowly dwindles to worthless (out of the money) or it gets locked into the difference between the strike and current stock prices (in the money). This is the theta (Θ) of an option: the price sensitivity of the option with respect to time. Thinking of the option price as a function of all these factors, Θ is the first derivative of the price with respect to time.</p><p>Theta decay is greater the closer the strike price is to the current stock price and the theta gets smaller as you go up and down the option chain from there. Going back to our spread strategy, that means if we sell out of the money call options at one strike and buy call options at a higher strike, then our net position should have a net positive theta and we expect it to gain value as we move towards the expiration of the options. In an ideal world, the stock price stays relatively flat or goes down and both of the options would expire worthless having already pocketed the premium difference for our trouble. Keen eyed readers might have noticed that this is a net short position on the stock price.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*56blJCpL2BQdcifDizL2hA.gif" /></figure><p>The worst case scenario for us is that the option we sold ends in the money and the one we bought does not. In that case we are on the hook to sell shares we don’t have at a loss. This downside is somewhat limited because if the price goes higher then we can exercise our purchased options to offset and we only lose the difference in strikes multiplied by the number of contracts multiplied by 100 (because each contract is for 100 shares). If we make our spreads too far out of the money so this doesn’t happen, then the premiums are all nearly 0 and we can’t make any profit on the spread.</p><h3>Quantifying Risk</h3><p>How do we quantify or mitigate this risk? That comes down to more of the option greeks: delta (Δ) and gamma (Γ). Δ is the option price sensitivity with respect to the change in the option price, or the first derivative of the option price with respect to the underlying price. Γ is the second derivative of the option price with respect to the underlying price. If we can create a spread which is close to Δ and Γ neutral, then our trade will be less sensitive to price movements in the stock. Let’s ground these concepts in some real numbers pulled from end of day prices on Jan. 29, 2021.</p><pre>AAPL stock price: $131.96<br>AAPL 2/5 Call @ 140: $1.15 (Δ 0.218 Γ 0.03088 Θ -0.20163)<br>AAPL 2/5 Call @ 141: $0.98 (Δ 0.1915 Γ 0.02843 Θ -0.18764)</pre><p>If we sell 2 contracts @ 140 and buy 2 contracts @ 141 then we net a premium difference of $34.00 ($1.15 * 100 * 2 - $0.98 * 100 * 2), with typical fees of $0.65/contract that’s a net of $31.40. We can also compute the greeks for this net position:</p><pre>Net Δ = 0.1915 * 2 * 100 - 0.218 * 2 * 100 = -5.3<br>Net Γ = 0.02843 * 2 * 100 - 0.03088 * 2 * 100 = -0.49<br>Net Θ = -0.18764 * 2 * 100 - -0.20163 * 2 * 100 = 2.798</pre><p>This isn’t exactly zero Γ but it’s fairly small. As we hoped our net Θ is positive. But our Δ is -5.3 indicating that we expect to lose money as the stock price goes up. The most we can lose is $168.60 ($200 less the $31.40 we gained up front). One way we can counter that is by buying shares of the underlying stock. The stock price has a Δ of 1 and a Γ of 0 so buying 5 shares of Apple (AAPL) costs $659.80 nets us:</p><pre>Net Δ = 0.1915 * 2 * 100 - 0.218 * 2 * 100 + 5 = -0.3<br>Net Γ = 0.02843 * 2 * 100 - 0.03088 * 2 * 100 = -0.49<br>Net Θ = -0.18764 * 2 * 100 - -0.20163 * 2 * 100 = 2.798</pre><p>While still slightly net negative, this is close to what we were hoping to achieve which is a price neutral option spread that expects to net value off the time decay. The best case for this new portfolio is if the price ends at $140. In this case we keep the premiums and we’ve made an additional $40.20 from the stock price change for a total of $71.60. One of the worst cases is when the price lands on $141. Then the bought option is worthless and we lose $200 from the options we sold. We still gain $45.20 from the stock and the upfront premiums but the net loss is still $123.40.</p><p>Seems like we are living the dream of higher upside with less downside, but that isn’t the whole story. Without the stock purchase, we would have always made our premium no matter how low the stock price dropped. But now we run the risk that the stock price drops significantly and it eats into our gains from the premium and even lead to losses that previously we didn’t need to worry about. This trade will net nothing when the price falls to $125.68 before it starts to take losses. If we were confident that that price was going to fall then we wouldn’t have purchased the stock so it’s a nice trade off from my perspective.</p><h3>Is This Worth It?</h3><p>This example has focused on call options but the same concepts apply to puts. It does need a few things to make it work as a viable trading strategy. First, it needs low trade commissions. We’ve assumed that equity trades are free and that options trade at $0.65 a contract. From what I could see that’s common for low priced retail brokerages.</p><p>Second, we need good market data. When I worked in derivative pricing, collecting and reviewing market data to construct our interest rate curves and volatility tables was 80%+ of our daily work. I don’t have a great answer for this yet. Getting option trade prices is not a problem but the volatility and greek data is not as easy to find. I was able to pull this example from the barchart.com website but their APIs are all gated.</p><p>Third, this strategy requires patience. This isn’t a get rich quick scheme and it doesn’t have a lot of upside. Trading these spreads is about capturing small amounts of value with as little risk as possible. Small steps and constant improvement matches my approach to running and software development. I think that’s why this strategy appeals to me.</p><p>In our scenario we needed $659.80 to purchase the stock and would have needed another $200 to cover the margin for a total of $859.80 to make at most $71.60. Even with only the premiums at $31.40 that’s the opportunity to make 3.65% on the capital for a week’s worth of holding our breath and hoping for the best. Annualized that 3.65% a week becomes 545% a year. On the other hand losing $123.40 would wipe out all the gains from four similarly sized gains. You need to be right far more times than you are wrong so finding the best opportunities will be key to making the most of your capital.</p><p>Fourth, volatility isn’t hedged. Our best outcomes are when the stock price is relatively flat. Keeping the time frame short can help reduce risk of large price movements, but it’s still a risk. Picking stocks that have a low volatility would help, but that also reduces the option prices and our opportunity for favorable spreads. Finding the right balance is an area to continue to explore.</p><p>Fifth, the stocks and options need sufficient liquidity. The further out of the money we go, the less people are willing to take the trade. That can mean high bid/ask spreads on trying to buy and sell, and also that our trades might not go through in a timely fashion.</p><p>Finally, there are some logistical problems to trading this strategy as a retail investor. Most retail brokerages require special approvals to trade options or access any margin. Most have 4 possible levels and to trade spreads you often need to get to level 3. It could be hard to convince your brokerage that you know what you are doing from reading on post on Medium. The capital requirements to attain each level are unclear, but that could be another barrier for retail investors not willing to risk a large portion of their savings.</p><h3>Next Steps</h3><p>I’m continuing to explore this strategy and I plan to follow up with another post about collecting the necessary data, calculating the option greeks, and finding good opportunities. I’m still uncertain if this is something I’m ready to put my hard earned dollars behind, but it feels good to get back to something that shaped my early career path. Follow along if you are interested in learning more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6c8748e6b734" width="1" height="1" alt=""><hr><p><a href="https://medium.datadriveninvestor.com/exploring-option-credit-spreads-6c8748e6b734">Exploring Option Credit Spreads</a> was originally published in <a href="https://medium.datadriveninvestor.com/">DataDrivenInvestor</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Build Your Own Dystopian Future on AWS]]></title>
            <link>https://markdlavin.medium.com/build-your-own-dystopian-future-on-aws-af240d4c1d14?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/af240d4c1d14</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[facial-recognition]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Thu, 05 Mar 2020 01:14:03 GMT</pubDate>
            <atom:updated>2020-03-05T01:14:03.500Z</atom:updated>
            <content:encoded><![CDATA[<h4>Become the surveillance state leveraging facial recognition software from AWS Rekognition.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OXIOCfbk08PfyeW_" /><figcaption>Photo by <a href="https://unsplash.com/@matthewhenry?utm_source=medium&amp;utm_medium=referral">Matthew Henry</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Facial recognition is a controversial topic. While the technology itself is impressive, it raises a number of privacy concerns, and it’s ripe for potential abuse from bad actors. In my effort to better understand the current capabilities of facial recognition, I came across AWS Rekognition. I was amazed (and somewhat terrified) at how easily I was able to put together a proof-of-concept API leveraging this service to find pictures matching a new reference image. I’m going to walk you through that process so that you too can better understand how this can be used, for good or for evil.</p><p>In this example, I’m going to be using Python 3.7 and its AWS related packages. AWS provides libraries for their services in a number of languages on top of their HTTPS endpoints, so it should be possible to recreate much of this in your language of choice. To follow this example on your own, you’ll need an AWS account. We are going to create an S3 bucket to store images. As images are added to this bucket, we’ll connect a Lambda trigger to index any faces detected in the image in a Rekognition collection. With this collection of images, we’ll expose a single API endpoint via API Gateway and Lambda, which takes a new image and looks to see if there is a matching face in our collection. While this should fit within the free-tier usage, you may see charges on your account related if you leave this running or if it sees heavy usage. I’ll touch on how to clean this up before the end of the article.</p><p>To get things started we’ll create a new <a href="https://chalice.readthedocs.io/en/latest/quickstart.html">Chalice project</a>. Chalice is a Python framework for building and deploying serverless applications written and maintained by AWS.</p><pre>$ pyenv virtualenv 3.7.6 rekognition-demo<br>$ pyenv activate rekognition-demo<br>(rekognition-demo) $ pip install chalice boto3 awscli<br>(rekognition-demo) $ chalice new-project rekognition-demo<br>(rekognition-demo) $ cd rekognition-demo<br>(rekognition-demo) $ echo &quot;boto3&quot; &gt;&gt; requirements.txt</pre><p>This assumes some familiarity working with Python and virtual environments to isolate your dependencies. If you aren’t, I recommend checking out my post on <a href="https://medium.com/@markdlavin/my-python-environment-setup-fcddd0e99e5b">how I manage my local Python environment</a>. This won’t be an exhaustive tutorial on the Chalice framework, and what you’ll learn here is focused on the primary goal of creating our API. That being said, this can serve as a nice introduction or sample project if you are interested in exploring Chalice.</p><p>With this initial project scaffolding out of the way, we’re ready to start building out this API.</p><h3>Target Acquired</h3><p>We’re going to avoid going down a particularly dark route that surveillance technology might take us. Instead we are going to use facial recognition to turn the surveillance state around. I went through the photos on my local p<a href="https://www.facebook.com/pg/ApexPolice/photos/">olice department Facebook page</a> and downloaded images which depicted only officers in uniform, making an effort to avoid any which had clear images of non-police faces. It’s fine for them to contain multiple faces just as long as they are all officers. This was a manual and somewhat tedious process, but it got the job done. I stored them all locally in a data directory inside my Chalice project directory. We’ll sync these to S3 once we have the logic in place to index them.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*saan9LwOejTkUIr76uZmUg.png" /></figure><p>S3 fires events when items are added or removed from a bucket, and you can connect those to Lambda functions, SNS notifications, and other actions. Chalice allows us to bind our app to functions which trigger off of these events. When deployed it will make use of these event triggers. Let’s replace the app.py which was generated by Chalice and update it with the following:</p><pre># app.py<br>import logging</pre><pre>import boto3<br>from chalice import Chalice</pre><pre>app = Chalice(app_name=&quot;rekognition-demo&quot;)<br>app.log.setLevel(logging.DEBUG)</pre><pre>BUCKET = &quot;rekognition-apex-police&quot;</pre><pre>COLLECTION = &quot;apex-police&quot;</pre><pre>rekognition = boto3.client(&quot;rekognition&quot;)</pre><pre><a href="http://twitter.com/app">@app</a>.on_s3_event(bucket=BUCKET, events=[&quot;s3:ObjectCreated:*&quot;])<br>def handle_new_image(event):<br>    response = rekognition.index_faces(<br>        CollectionId=COLLECTION,<br>        Image={&quot;S3Object&quot;: {&quot;Bucket&quot;: event.bucket, &quot;Name&quot;: event.key}},<br>        ExternalImageId=event.key,<br>        MaxFaces=10,<br>        QualityFilter=&quot;AUTO&quot;,<br>        DetectionAttributes=[&quot;ALL&quot;],<br>    )<br>    app.log.debug(f&quot;Indexed {len(response[&#39;FaceRecords&#39;])} faces from {event.key}&quot;)</pre><p>This binds the handle_new_imagefunction to S3 events from our bucket named rekognition-apex-police. <em>Note that since bucket names must be globally unique, if you want to recreate this yourself you will need to update with a new bucket name.</em> As new images are uploaded, it puts them into our Rekognition collection named apex-police by using <a href="https://docs.aws.amazon.com/rekognition/latest/dg/API_IndexFaces.html">IndexFaces</a>. This can take either the image bytes or a reference to an S3 object (as we’ve done here) to add it to the index. Later when we try to match new images, the existence of a match in the index will let us know that this person is part of the police department.</p><p>Chalice has a cool feature where it discovers the AWS resources you are using through your boto3 calls and will automatically add permissions for those resources on the deploy. Sadly it’s not perfect and doesn’t quite work here. While it will add the necessary permissions for Rekognition, in order for the call to work, the Lambda function will also need some S3 permissions that won’t get detected here. Thankfully there is a way to manually set the permissions so we’ll do that in .chalice/policy-dev.json.</p><pre># .chalice/policy-dev.json<br>{<br>  &quot;Version&quot;: &quot;2012-10-17&quot;,<br>  &quot;Statement&quot;: [<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;rekognition:CreateCollection&quot;,<br>        &quot;rekognition:DescribeCollection&quot;,<br>        &quot;rekognition:IndexFaces&quot;,<br>        &quot;rekognition:SearchFacesByImage&quot;<br>      ],<br>      &quot;Resource&quot;: [&quot;*&quot;]<br>    },<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [&quot;s3:GetObject&quot;, &quot;s3:ListBucket&quot;],<br>      &quot;Resource&quot;: &quot;arn:aws:s3:::rekognition-apex-police/*&quot;<br>    },<br>    {<br>      &quot;Effect&quot;: &quot;Allow&quot;,<br>      &quot;Action&quot;: [<br>        &quot;logs:CreateLogGroup&quot;,<br>        &quot;logs:CreateLogStream&quot;,<br>        &quot;logs:PutLogEvents&quot;<br>      ],<br>      &quot;Resource&quot;: &quot;arn:aws:logs:*:*:*&quot;<br>    }<br>  ]<br>}</pre><p>Also we need to change autogen_policy to false in the pre-generated .chalice/config.json.</p><pre># .chalice/config.json<br>{<br>  &quot;version&quot;: &quot;2.0&quot;,<br>  &quot;app_name&quot;: &quot;rekognition-demo&quot;,<br>  &quot;stages&quot;: {<br>    &quot;dev&quot;: {<br>      &quot;api_gateway_stage&quot;: &quot;api&quot;,<br>      &quot;autogen_policy&quot;: false<br>    }<br>  }<br>}</pre><p>Check out the Chalice docs for more information about setting your project permissions: <a href="http://chalice.readthedocs.io/en/latest/">http://chalice.readthedocs.io/en/latest/</a></p><p>With this ready we can do our first deployment and sync our locally saved images to the S3 bucket.</p><pre># Configure your AWS API credentials<br>(rekognition-demo) $ aws configure<br># Create the S3 bucket<br>(rekognition-demo) $ aws s3api create-bucket --bucket rekognition-apex-police<br># Create the Rekognition collection<br>(rekognition-demo) $ aws rekognition create-collection --collection-id apex-police<br># Deploy our app<br>(rekognition-demo) $ chalice deploy<br>...<br># Sync the local images to S3<br>(rekognition-demo) $ aws s3 sync data s3://rekognition-apex-police<br>upload: data/71349130_2620493271306848_3732197409082572800_n.jpg to s3://rekognition-apex-police/71349130_2620493271306848_3732197409082572800_n.jpg<br>...<br>upload: data/81421938_2819939351362238_4027235321283346432_o.jpg to s3://rekognition-apex-police/81421938_2819939351362238_4027235321283346432_o.jpg</pre><p>Checking the logs we can see that everything is working as expected.</p><pre>(rekognition-demo) $ chalice logs --name handle_new_image</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AbYVwhZDpMwJP2OW6PAXUQ.png" /></figure><p>Now we are ready for for the next phase which is exposing an API to let us check if a new image contains a face of a local police officer.</p><h3>Recognizing Users</h3><p>Now that we have our database of faces, we can start to create the API to check image against our database for matches. Rekognition provides a <a href="https://docs.aws.amazon.com/rekognition/latest/dg/API_SearchFacesByImage.html">SeachFacesByImage</a> endpoint for just this purpose. Like IndexFaces, it can take either a reference to an S3 image or the bytes for the image we want to search. In this case we are going to get the image bytes out of the current request. Unfortunately, the API gateway and Chalice is structured to work with JSON request bodies rather than multipart/form encoded data. To work around this, rather than posting the image directly we are going to base64 encode the image data into a JSON body.</p><pre># app.py continued<br># Replacing top level imports<br>import base64<br>import logging</pre><pre>import boto3<br>from botocore.exceptions import ClientError<br>from chalice import BadRequestError, Chalice</pre><pre># Previous section would be here ...</pre><pre><a href="http://twitter.com/app">@app</a>.route(&quot;/&quot;, methods=[&quot;POST&quot;])<br>def check_image():<br>    request = app.current_request<br>    if &quot;image&quot; not in request.json_body:<br>        raise BadRequestError(&quot;No image given&quot;)<br>    try:<br>        image = base64.b64decode(request.json_body[&quot;image&quot;])<br>    except binascii.Error:<br>        raise BadRequestError(&quot;Invalid image given&quot;)<br>    try:<br>        response = rekognition.search_faces_by_image(<br>            CollectionId=COLLECTION, Image={&quot;Bytes&quot;: image},<br>        )<br>        result = {<br>            &quot;matches&quot;: [<br>                match[&quot;Face&quot;][&quot;ExternalImageId&quot;] for match in response[&quot;FaceMatches&quot;]<br>            ],<br>        }<br>    except ClientError:<br>        result = {<br>            &quot;matches&quot;: [],<br>        }<br>    return result</pre><p>After some initial validation that we have what looks like a valid image, the image bytes are passed to Rekognition to search for any matching faces in the collection. If there are matches they are returned including the original file name in the response. The Rekognition docs note that this search will work best if there is only a single face in the image</p><p>To get this running for us to test, we’ll need to do another deploy. Chalice will see that we have created a route and wire that to a new API Gateway endpoint.</p><pre>(rekognition-demo) $ chalice deploy<br>Creating deployment package.<br>Updating policy for IAM role: rekognition-demo-dev-handle_new_image<br>Updating lambda function: rekognition-demo-dev-handle_new_image<br>Configuring S3 events in bucket rekognition-apex-police to function rekognition-demo-dev-handle_new_image<br>Creating IAM role: rekognition-demo-dev-api_handler<br>Creating lambda function: rekognition-demo-dev<br>Creating Rest API<br>Resources deployed:<br>  - Lambda ARN: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev-handle_new_image<br>  - Lambda ARN: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev<br>  - Rest API URL: <a href="https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/">https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/</a></pre><p>With the REST API up, we can test against a couple images: one that we expect should match and another which shouldn’t match. For the matching image, I pulled the chief of police’s head shot from the Town of Apex website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*4vJVnzfpXW2KZPdFkwYVFQ.jpeg" /><figcaption>Apex Police Chief John W. Letteney</figcaption></figure><p>To send this to the API, it needs to be base64 encoded inside the JSON body under the image key.</p><pre>$ (echo -n &#39;{&quot;image&quot;: &quot;&#39;; base64 Police\ Chief\ John\ Letteney.jpg; echo &#39;&quot;}&#39;) |<br>curl -H &quot;Content-Type: application/json&quot; -d @- <a href="https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/">https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/</a><br>{&quot;matches&quot;:[&quot;72434429_2645492565473585_3429136798620581888_o.jpg&quot;,&quot;82393140_2845334582156048_6102571155162398720_n.jpg&quot;,&quot;82647288_2851484764874363_5294281172166115328_n.jpg&quot;,&quot;83177039_2881712408518265_7297622762774855680_n.jpg&quot;,&quot;85026759_2893680170654822_8181472211329089536_o.jpg&quot;,&quot;71349130_2620493271306848_3732197409082572800_n.jpg&quot;]}</pre><p>✨ It worked! ✨ Not surprisingly, our collection of Facebook pictures had several which included the chief. Now we can test it against an image which shouldn’t match to check for potential false positives. For this we’ll use this picture of me dressed as a Charizard</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Zae4tNApDmviu-bAOQfWGQ.jpeg" /><figcaption>Mark as a Pokemon (not a cop)</figcaption></figure><p>Running this through a similar command to base64 encode and send to the API yields no matches as expected.</p><pre>$ (echo -n &#39;{&quot;image&quot;: &quot;&#39;; base64 pokemark.jpg; echo &#39;&quot;}&#39;) |<br>curl -H &quot;Content-Type: application/json&quot; -d @ <a href="https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api/">https://xrviuv0vtf.execute-api.us-east-1.amazonaws.com/api</a>/<br>{&quot;matches&quot;:[]}</pre><h3>Clean Up</h3><p>We can clean this up to avoid any overuse of this API and unexpected Amazon bills using chalice delete.</p><pre>(rekognition-demo) $ chalice delete<br>Deleting Rest API: xrviuv0vtf<br>Deleting function: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev<br>Deleting IAM role: rekognition-demo-dev-api_handler<br>Deleting function: arn:aws:lambda:us-east-1:759954240342:function:rekognition-demo-dev-handle_new_image<br>Deleting IAM role: rekognition-demo-dev-handle_new_image</pre><p>This will remove the Lambda functions, API Gateway, and IAM roles that the framework created. It won’t automatically remove the S3 bucket or the Rekognition collection which we created. Those can be removed using the AWS CLI tools similar to how we created them. Note that the S3 bucket needs to be empty first.</p><pre># Remove the S3 bucket<br>(rekognition-demo) $aws s3 rm s3://rekognition-apex-police --recursive<br>(rekognition-demo) $ aws s3api delete-bucket --bucket rekognition-apex-police<br># Remove the Rekognition collection<br>(rekognition-demo) $ aws rekognition delete-collection --collection-id apex-police</pre><p>That takes care of all the AWS resources we created, so let’s wrap it up.</p><h3>Build the Future You Want to See</h3><p>The next steps with this API would be to integrate it into a system to process images or video of people which we want to determine if they are Apex police officers or not. Maybe this could be incorporated into a mobile app that would let me determine if a someone is an off-duty or non-uniformed officer. We’ll need to periodically update our images to account for new officers. Right now that’s a fairly manual process, but it could be automated as well.</p><p>While Rekognition is certainly impressive in its capabilities and ease of use (at least in this simple case), I’m not sure I’ll need it. Maybe a project will come up where this feels like a good solution. I expect that developers will face difficult ethical choices in the future about how to make use of this technology. It’s not hard to imagine how such a system could be used or abused for other purposes. You could integrate it into a security system to identify returning vs new customers, highlight VIPs, or flag potential shoplifters. It’s easy to be amazed by the possibilities of a new technology without thinking about its impact on the people it will be used on or for. There are certainly interesting ways this technology can be used to enhance customer experience and an equal number of ways this can be used to exclude or repress people. Which will you choose?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=af240d4c1d14" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Death of a Side Project]]></title>
            <link>https://medium.com/better-programming/death-of-a-side-project-44b77cb7ae92?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/44b77cb7ae92</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[side-project]]></category>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[entrepreneurship]]></category>
            <category><![CDATA[hobby]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Sat, 15 Feb 2020 14:50:07 GMT</pubDate>
            <atom:updated>2020-02-17T19:45:34.654Z</atom:updated>
            <content:encoded><![CDATA[<h4>The classic mistake of ruining a hobby by attempting to monetize it</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FcI5CvQ1dZO-71_NSkDkuw.png" /><figcaption>Professor Dogwood homepage.</figcaption></figure><p>This month, I finally shut down one of my side projects, Professor Dogwood. It was a site built to price decklists for the Pokemon Trading Card Game (TCG). Four years ago, I had high hopes and such excitement making and launching it, but it’s clear now that it’s time to end it. I haven’t updated it recently, and while it did get some minor traffic, I don’t think anyone will miss it. This isn’t the story of a million-dollar startup failure, and the only thing I’ve really lost is time. Here’s a recap of the birth and death of a small side project.</p><h3>How It Started</h3><p>In 2016, <em>Pokemon Go</em> exploded and the Pokemon hype was at a recent high. My family and I enjoyed playing together that summer. As fall came around, I had an idea to get a few starter decks and learn to play the Pokemon TCG with my oldest daughter, who was five at the time. I felt it would be a fun way to get her to practice her basic math and reading skills while also creating some quality father-daughter time.</p><p>The first few months were great. We played at home with our new decks and online as well. We watched YouTube videos from <a href="https://www.youtube.com/channel/UC9k2vZA_jd83-4gMzRupYrQ">OmniPoke</a> and started to build decks of our own. We started going to local game stores to play with our local Pokemon community. We played a few tournaments and pre-releases, and my daughter even won one for her age group. All this time, we were learning more about the cards and growing our collection.</p><p>As with many things in my life, when I get going with something, I want to dive in deep. When I was running, I knew that I would run a marathon (and I have run several). I took my triathlon phase all the way to finishing an Ironman twice. With the Pokemon TCG, I wanted to be the very best — better than anyone had ever been. I would watch tournaments on Twitch and read articles online. A new set would release or a new deck would win a tournament and I would want to try it out. But my daughter would go with me to the game shops and sometimes not play. She just wanted to talk to the other kids and look at the cards. That should have been a good signal to me, but it didn’t slow me down.</p><h3>What It Was</h3><p>As my obsession with this game grew, I began to realize the potentially ridiculous amount of money I was spending on this game. Now, as far as TCGs go, Pokemon is among the cheapest, but I was making decks for two of us. This wasn’t going to put us into financial ruin, but there were better ways we could have been spending that money. Certainly, as my daughter’s interest declined, we were no longer getting the same value from new cards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_ubNcVJZss1KkdBS3ZKwsA.png" /><figcaption>Screenshot of the price calculator.</figcaption></figure><p>To fuel this obsession, I had an idea for a site to help price new decks. I would create the decks in <em>PTCGO</em> and use the export format to get a plain text version of the list. You could paste in that format and the back end would parse it and look up prices for each of the cards. It took a few weekends to get that logic down. I was able to grab card data and images from <a href="https://pokemontcg.io/">https://pokemontcg.io/</a> and mesh that together with prices from <a href="http://pokemonprices.com/">http://pokemonprices.com/</a>.</p><p>Similar sites exist for Magic, like <a href="https://www.mtggoldfish.com/">MTGGoldfish</a>. In fact, the owners of that site launched <a href="https://www.pokegoldfish.com/">PokeGoldfish</a> just weeks after I launched Professor Dogwood. I’ll admit that made me want to quit right then. It felt like some validation of the idea that others had recognized the same need. At the same time, I was working a full-time job and knew my little site wouldn’t compete with the effort they were going to be able to dedicate to theirs — and that was disheartening.</p><p>But I couldn’t quit. I never expected that this site would take off and make me rich, but I did have a dream or a hope that it would generate enough traffic and revenue to fuel this new hobby. I wasn’t looking for it to help me quit my day job. At the time, it felt like a reasonable goal that this could generate a few extra dollars a month so I could buy some new cards guilt-free. Hosting it was costing me little to nothing because it was on an existing VPS I was already paying for. In another weekend or so, I was able to get prices through Amazon and turned everything into affiliate links. After six months without a single sale, that was shut down and I switched back to the original price API.</p><h3>Where It Failed</h3><p>Getting dropped from the Amazon Affiliate program was a turning point for me, but the reality was the site was likely dead from the start. Amazon doesn’t have great prices or selection on trading card singles, and I should have focused on TCGPlayer or a similar dedicated site. While I had the technical ability to build the necessary features for the site, I didn’t have a vision of what that would be beyond that first calculator. I thought of things that maybe someone might want, but I never talked to anyone who might use it for feedback or suggestions. I had some half-assed attempts to engage with the community through Twitter, but I never engaged on the more popular forums like <a href="https://www.pokebeach.com/">PokeBeach</a>, <a href="https://www.facebook.com/groups/virbankcity/">Virbank City Gym</a>, or <a href="https://www.reddit.com/r/pkmntcg/">Reddit</a>. I’m so generally terrible at self-promotion that I don’t think I even mentioned its existence once at our local game stores. In my mind, it was just so obviously needed at the time that people would search for it, find it, and use it.</p><p>Hosting the site cost very little, but it did need a little maintenance from time to time — particularly after a new set released. It primarily failed when I tried to expand beyond the original vision I had into something that wasn’t going to be sustainable for me in the long term. In an attempt to drive more traffic, I wrote a series of articles about different cards in the game and how they are used. It was a ton of work and they were fine for what they were. The problem was that the content quickly fell out of date when new sets came in or rotated out. This greatly increased the maintenance time for the site. It also wasn’t something that I really wanted to be doing. It was something I felt like I needed to do if I wanted the site to take off. Even as I had stopped playing the game, I would update these articles from time to time. Each update reminded me of the dreams I had at the beginning that would never be realized. Eventually, it wore me down and I just stopped updating. This year, when I got the domain renewal notice, I knew it was time to just let it go.</p><h3>What I Learned</h3><p>This story has sounded pretty negative, but no project can be a complete failure if you take something away from it. For me, this project was an opportunity to experiment with new technologies as well as try new things with previous ones. I built the site using <a href="https://docs.aiohttp.org/en/stable/">aiohttp</a>, which was new for me. Even though this site had effectively only one API endpoint, working with async requests challenged me in ways Django hadn’t at the time. I really liked some of the choices that framework makes and I’ve used it on a few small projects since then. This was also my first experience using the Amazon product search APIs. I tried new approaches with the Docker build for deployment. The site design used Material Design, which I had used previously, but this helped expand my knowledge and experience.</p><p>On the non-technical side, I’ve learned to set better goals for myself and my projects. I know myself better and I’ve learned to put better guardrails around letting my hobbies become wild obsessions. Now, I still love to go really deep and learn everything I can about a new hobby, whether it’s board games, 3D printing, miniature painting, or <em>StarCraft II</em>, just to name a few. But I won’t make the same mistake of trying to monetize it. Professor Dogwood started to turn a fun hobby and time with my daughter into work. I know better than to let that happen again.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N0Eod7eH8E9fp4CKVaOvIw.png" /><figcaption>Trello board full of half-finished ideas.</figcaption></figure><p>Looking back, I also have a much greater understanding and respect for the role of product management on a team. Development is so much easier for me when I can envision the end-user and how they are going to benefit from this work. For small side projects, that’s fine if it’s just for me. But here, once I got past the things I wanted, I hit a wall. I was far less motivated to add things when I didn’t have anyone asking for them. I wasn’t really interested in doing the things I thought the site “needed” and I also never confirmed whether the site would have had a better chance for success if I had done those things. I need that feedback loop from the users and I know that it makes for better software. I’ll certainly take that with me into my future projects, both personally and professionally.</p><h3>I Didn’t Catch ’Em All</h3><p>So this is it. I’m closing this weird chapter and moving on to another. The domain has expired, and if someone dares, they are welcome to pick it up and try to revive this idea. Someone reached out to me a year or so ago about open-sourcing the project. <a href="https://github.com/mlavin/decklist">I did</a>, but no contributions ever came out of it. I’ve sold off most of my Pokemon cards, only keeping single copies of some of the cooler ultra- and hyper-rare cards for our binder. We have a few remaining starter decks, and if the mood strikes us, we can always dust those off for a game.</p><p>While the project didn’t turn out the way I had hoped, I learned a few things. I have great memories of weekends playing with my girls, and with this project out of the way, I’ll have more time to make some more.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44b77cb7ae92" width="1" height="1" alt=""><hr><p><a href="https://medium.com/better-programming/death-of-a-side-project-44b77cb7ae92">The Death of a Side Project</a> was originally published in <a href="https://betterprogramming.pub/">Better Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding Your First Mechanical Keyboard]]></title>
            <link>https://medium.com/swlh/your-first-mechanical-keyboard-79f6c3e24398?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/79f6c3e24398</guid>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[mechanical-keyboards]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[keyboard]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Wed, 05 Feb 2020 13:55:29 GMT</pubDate>
            <atom:updated>2020-02-05T21:53:58.731Z</atom:updated>
            <content:encoded><![CDATA[<h4>I did the research on finding your first keyboard so you don’t have to.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JhEoC-7TlT8GgjDYuawIhQ.jpeg" /><figcaption>My TAB75 with Ice Cream keycaps 🍦 💖</figcaption></figure><p>After a lot of internal debate and research, I decided to get a mechanical keyboard. I don’t claim to be a keyboard enthusiast and this post isn’t meant to convince you to get one. In fact, you probably don’t need one. A mechanical keyboard won’t make you a better programmer, writer, or gamer. But if you have any interest in them but feel overwhelmed about where to even begin getting one, then you’ve come to the right place. I’m going to tell you how I went through the process of selecting my board and keycaps from an absolute n00b perspective. I had some great help and guidance from friends of mine and I want to pay it forward. We’re going to sort through the endless acronyms and terminology together .After reading this you should be armed with the necessary information to select your own keyboard, if you like.</p><p>Let’s start with why I wanted to get one in the first place. I’d heard all the hype. I have a couple friends who had them and raved about them. They loved the touch and sound of their keys and the layout that fit their needs. But what finally got me was wanting inject some personality into my computer setup. In the same way some people love to decorate their laptop with stickers, when I looked at my desk I wanted it to feel more like my own space. I spend nearly all day at work in front of a computer and more time at home. Looking at my laptop and monitor everything is so black and grey. I wanted bright color and fun. I didn’t want stock, out-of-the-box, cookie cutter options. I wanted something that felt like me where I could sit down at my desk and feel joy. This process of selecting the layout, switches, and keys I wanted, putting it all together was tedious at times, but it lead to something that feels my own and I’m happy with the result.</p><p>There are a lot of interwoven choices to make when it comes to mechanical keyboards. I didn’t realize how some of the choices I made would end up impacting my future options. Here I’m going to walk through questions you can ask yourself, a bit like a flowchart to determine a good starting place for you. Then instead of browsing through hundreds of options, you’ll have a better sense of how to narrow down your search and find a good fit. I’m not trying to sell you anything, just giving you the tools you need to be able to read an item description and know better what it is that you are getting.</p><h3>Size and Shape</h3><p>The shape of keyboard that you want is going to depend on how you use your keyboard whether that’s gaming, programming, writing, web browsing, etc. I took a minimalist approach to get the smallest keyboard I was comfortable with while keeping the keys that I wanted. To follow this route, you should first ask yourself if you want a number pad or not. If you do, then your best option is to get a standard/full-size layout. If you don’t need a number pad, then the next question would be if you want the arrow keys. If you want arrow keys, the I would recommend looking for a ten-keyless (TKL) layout. If you don’t want/need the arrow keys, then a compact/60% layout could be a good fit for you. These are the three most common sizes and they are a good place to start.</p><p>For me, I found myself in an odd place where I did want the arrow keys but I wanted something smaller than a TKL size. I also really wanted the function row. So I didn’t take my own advice and I found a nice middle ground called a 75% layout. While I love the layout, it did make finding keys a little bit more difficult. It also really narrowed my options on the available manufacturers, increasing the price options that I had. That’s why in general I would recommend starting with one of the three most common layouts. Similarly, you might be interested in a split or ergonomic keyboard. If you are then go for it, but be aware you may end up with fewer options when it comes to selecting caps which fit in your layout.</p><h3>The Perfect Touch</h3><p>The mechanical in mechanical keyboard, refers to the switches. There are lots of different types and they are what give the key press its feel and sound. While there are plenty of key manufactures, I’m going to save you time and say for your first keyboard to focus on Cherry MX. No offense to other manufacturers, but Cherry MX seems to be the standard to the point that others will boast that theirs are MX-compatible. The keycaps will fit into the top of the switch, so the type of switch you get will determine which keycaps you can buy. Since these are the most common and easiest to find, going with Cherry MX gives you the most options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*-5bXv21CkDm3DyiomjVp2w.jpeg" /><figcaption>My switch tester</figcaption></figure><p>Within the Cherry MX line there are three main types of switch: tactile, linear, and clicky. I could describe the feel of each, but honestly you need to feel them for yourself. The advice I got and I’ll pass on here is to get a switch tester. Maybe your keyboard loving friend or co-worker already has one that you can borrow. I picked up one on <a href="https://www.amazon.com/gp/product/B07CHSKY13/ref=ppx_yo_dt_b_asin_title_o06_s00?ie=UTF8&amp;psc=1">Amazon for about $15</a> (the current price might differ). It comes with nine keys each with a different Cherry MX switch, three of each type each of those with a different amount of stiffness. Tapping each of the keys gave a feel for which one seemed the most natural to me. For me, the Cherry Black, which is a mid-stiffness linear switch, was the most natural feel. #teamlinear</p><h3>Adding Your Personality</h3><p>The part I was most excited about was the caps themselves. While the switches and layout have the most functional customization, having something that you can look down at and makes you smile has its own value. As you search for keycap sets, you be bombarded with different types of plastics (ABS and PBT) and lettering methods (dye sublimated, double-shot, and laser etching). Now these plastics are supposed to have a different feel to them, but it’s slight. And the different methods of lettering can impact the crispness of the letters and the potential for wear after use. But in my search, this felt like noise. These differences seemed far too subtle for someone looking to get their first keyboard. I would recommend not worrying about these kinds of options. It’s great to know these options are there, but if the other pieces fit and like the look, then get them and leave researching plastics for another day.</p><p>What you do what to look for, other than a color scheme that you like, is that they fit the layout that you have selected. Caps come in different profiles and sizes. The profile of the cap determines it’s slope and the size determines its width. While there are many different profiles with varying feels, it helped me to only consider either sculpted (OEM, Cherry) or uniform profiles (DSA, XDA). Enthusiasts will know that there are many more shapes and profiles, but in my research I found these four to be the most common and I would recommend focusing these as options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*eQQIuqzqG66UF9aS" /><figcaption>Photo by <a href="https://unsplash.com/@floriankrumm?utm_source=medium&amp;utm_medium=referral">Florian Krumm</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Sculpted profiles have different key shapes depending on the row. The top rows pitch the keys down towards the home row, the middle rows are roughly flat, and the bottom rows pitch away from the user towards the home row. This makes the face of the key slant towards your fingers, assuming they are coming from the home row position, which gives a natural feel when typing. The different feel of the keys can also help to keep your figures in the correct position or to re-center your hands if you move them from the home row. The two most common sculpted profiles you’ll find are Cherry (not to be confused with the switch name) and OEM. Cherry is a slightly lower profile, but for the sake of simplicity I would recommend focusing on sculpted vs uniform instead of OEM vs Cherry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*HerMfiv_nDAdHhXt_QlpdQ.jpeg" /><figcaption>Side profile of my XDA caps</figcaption></figure><p>With uniform profiles, each key has the same slant regardless of its position. While the overall ergonomic idea of this shape is less clear to me, it makes them easy to fit into non-standard layouts because the keys from the set can be moved to any row without issue. My TAB75 came DSA keycaps, likely due to it’s less common layout, and because of that I biased my search towards uniform profile caps. The most common uniform profiles are DSA and XDA. Between them XDA is slightly taller, but for my consideration on a first keyboard I considered them roughly equivalent.</p><p>When it comes to key sizes, the main aspect here is the legend/control keys (control, shift, space, enter, tab, etc). The sizes of keys are relative to the size of a letter key which is considered 1U. So your layout might have a 1.5U shift key (meaning one and a half the size of a key) or a 2U shift key (meaning twice the size of a letter key). Again, going towards a common layout (full/TKL/compact) will help avoid some of these issues/consideration. Lots of cap sets you will find have multiple size options so that could be used on a range of layouts. There would be nothing worse in this experience than spending money on a board and caps that you love only to find they don’t fit. So before you hit that purchase button just do a double check on the legend sizes of the layout and the keycap set.</p><h3>More Help</h3><p>There is plenty more that I haven’t touched on here, like back-lighting or programmability. So you have lots more to discover on your own if you wish. If you do want to go down that route, I recommend reading the wiki from <a href="https://www.reddit.com/r/MechanicalKeyboards/wiki/">/r/MechanicalKeyboards/</a> on Reddit. It has plenty of guides for aspects that I purposefully glossed over here as well as recommendations on where to buy everything. If you are specifically interested in the board and keys I’ve chosen, then you purchased the board from <a href="https://mechanicalkeyboards.com/shop/index.php?l=product_detail&amp;p=4556">mechnicalkeyboards.com</a> and the keys from <a href="https://kbdfans.com/">KBDFans</a> on <a href="https://www.aliexpress.com/item/33058165890.html">AliExpress</a>. Enjoy your search and I hope you get a happy result as I did.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79f6c3e24398" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/your-first-mechanical-keyboard-79f6c3e24398">Finding Your First Mechanical Keyboard</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Why (and How) I Use Vim]]></title>
            <link>https://levelup.gitconnected.com/why-and-how-i-use-vim-da322260aa6c?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/da322260aa6c</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[vim]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Wed, 29 Jan 2020 17:20:25 GMT</pubDate>
            <atom:updated>2020-07-23T17:45:43.701Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FQWFjwx8K1svM9WQKDE7tQ.png" /><figcaption>My beautiful vim config</figcaption></figure><p>When I started programming, I never imagined myself using vim. As an undergrad, my advisor did everything on the command line and watching him work was mesmerizing. The cursor moved and things happened and I didn’t understand at all how it worked, but there was an elegance to it. I think that he used emacs rather than vim, but that’s neither here nor there. His muscle memory powering his keystrokes to accomplish tasks with ease.</p><p>In college and my first few years on the job, I used Eclipse (yes even for Python). Eclipse felt like what a programmer should be using. This mammoth IDE with a million features and plugins. Menus and options that I had no idea what they meant. Maybe I never took the time I needed to really learn it, but honestly, I just think it wasn’t for me.</p><p>Realizing that I didn’t use much of what Eclipse was meant to offer, I moved on to gedit and later to Sublime Text. Both great editors for Python and they served me well for years. I added a few plugins and a color scheme I liked, and I was right at home. There wasn’t much reason to change.</p><p>But then I remember the day I decided to give vim a try. I was pairing with a co-worker who used vim and watching him work brought back those memories. It made me want to give it a try. Could my fingers learn to dance too? I asked for him his vim config and he helped me get started. As I went through the configuration I tried to remove things I didn’t understand or didn’t feel like I needed. My productivity wasn’t great the first week or so, but slowly I did build that muscle memory and settled on a configuration that worked for me. My fingers began to dance on the keys as I poured out new features and fixed bugs. This felt like a tool for me.</p><p>My configuration has changed a bit since then. I like to keep my customization low because when I had more, I found myself frustrated when I would ssh onto a server and my key bindings weren’t there (though thankfully I find myself having to do that far less nowadays). The universality of vim is one of the things I like about it and with too much configuration I felt that I was taking away from it.</p><p>I also love the focus of having a single file open (I rarely split panes) and the purposeful choice to switch from one to the next. In my previous editors, I used to have dozens of files open and click around through the tabs frantically to find what I need. With vim, I keep my fingers on the keyboard, slow down, and focus on the task at hand. I think it makes me a more mindful programmer. When I create a new file, the vast darkness of the background stares back at me, unbroken by menus or sidebars. There are not references or traces of other files to be seen. Just wide open dark calling to be filled with bright code. It’s not what I imagined I would like or use, but then again much of my career has been filled with the same kind of happy surprises.</p><p>If my story inspires you to try vim, then this will walk you though my configuration with a focus on Python programming and web development. If you want to stop here, then I would still recommend you take a moment to reflect on what tools you use, why you use them, and any small changes you could make to improve them. As developers we spend so much time in these types of tools, it makes a huge difference to have something that fits your mindset and personal style. For me, that style has evolved over time, and I expect it will continue to evolve. Taking that time to reevaluate or reaffirm our choices can make us better programmers.</p><h3>Changing Up Defaults</h3><p>To start there are some small changes to what the editor displays (current mode, line numbers) and how it accepts new line characters. This makes vim behave the same on both Mac and Linux. I prefer having a long undo history over keeping lots of temp files so those backup files are disabled and I bump this to 1000 undo levels. The <a href="https://github.com/tpope/vim-sensible">vim-sensible</a> plugin includes a few more vim default changes which I like and it keeps my config smaller.</p><pre>set ttyfast<br>set showmode<br>set showcmd<br>set title<br>set number<br>set hidden</pre><pre>&quot; This is what files look like<br>set ffs=unix,dos,mac</pre><pre>&quot; Update find path to search subdirectories<br>set path=$PWD/**</pre><pre>&quot; No backups<br>set nobackup<br>set nowritebackup<br>set nowb<br>set noswapfile</pre><pre>&quot; Keep lots of history/undo<br>set undolevels=1000</pre><pre>&quot; Files to ignore<br>&quot; Python<br>set wildignore+=*.pyc,*.pyo,*/__pycache__/*<br>&quot; Erlang<br>set wildignore+=*.beam<br>&quot; Temp files<br>set wildignore+=*.swp,~*<br>&quot; Archives<br>set wildignore+=*.zip,*.tar</pre><p>At the bottom of this section you’ll see some additional file types which I’ve told vim to ignore. I still ignore .beam files from a few months when I was playing around with Erlang. I probably don’t need it, but I can’t bring myself to remove it. It’s that dream that maybe, one day I’ll have the time to dive back in and learn more Erlang *sigh*.</p><h3>Colors</h3><p>I’m a huge fan of the <a href="https://monokai.pro/">monokai</a> color scheme from TextMate, which was later adopted by Sublime Text. When I made the switch that’s one thing I wanted to keep. The vim port renames it slightly, but it has the same feel.</p><pre>set t_Co=256<br>colorscheme molokai</pre><pre>set colorcolumn=80,100</pre><pre>highlight ColorColumn ctermbg=238 guigb=#23272</pre><p>Beyond the color scheme, I also configure a column highlight at 80 and 100 characters. This is a bit less relevant now that I auto-format most of my files, but it’s still a good guideline for line length for files that won’t auto-format. I’ve always felt like 80 characters was too short, and while 100 might be too long, having both as a guideline helps me make good choices about when to wrap a line when I start typing in the “danger stripe”.</p><h3>Tabs and Whitespace</h3><p>Since Python is my programming language of choice and it has meaningful whitespace, having good whitespace settings in my editor is quite important. I like to show tabs explicitly different from spaces and I also like to highlight trailing whitespace characters, those these are usually cleaned up by auto-formatter. For Python I always use 4 spaces as a tab, following the PEP 8 recommendation, and for the longest time this is what I used for all file types (other than Makefiles which require tabs). Now my tabstop/shiftwidth settings are a bit more nuanced. My team at work prefers 2 spaces for JS, CSS, HTML, and JSON and I’ve learned to like it too.</p><pre>&quot; Tab sanity<br>set expandtab<br>set tabstop=4<br>set shiftwidth=4</pre><pre>&quot; Show hidden characters, tabs, trailing whitespace<br>set list<br>set listchars=tab:→\ ,trail:·,nbsp:·</pre><pre>&quot; Different tab/space stops&quot;<br>autocmd Filetype yaml setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd Filetype html setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd Filetype css setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd Filetype scss setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd Filetype json setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd Filetype javascript setlocal tabstop=2 shiftwidth=2 softtabstop=2 expandtab<br>autocmd FileType make setlocal noexpandtab</pre><p>I’m slowly collecting more and more types which use two spaces instead of four, but I doubt I’ll ever make that switch for Python. However, as two seems to becoming more and more the norm for me, I might make it the default and explicitly set Python to four spaces rather than the other way around. This is an area of my config I’m watching as it grows to see if that makes sense.</p><h3>New Shortcuts</h3><p>Shortcuts are an area where I haven’t spent much time customizing vim. Maybe I should do more, but it hasn’t felt necessary. However, there is one shortcut I can’t live without:</p><pre>&quot; Remap escape<br>inoremap jk &lt;Esc&gt;</pre><p>When in insert mode, this maps the <em>jk</em> key combination to the escape key, which exits you from insert mode. This is a pretty popular remap and I love it. It keeps your fingers on the home row and let’s you keep working. It’s particularly nice on the Macbook Pro which has the fake escape key on the task bar. If you only add one shortcut/remap to vim, this should be it.</p><h3>Enabling Plugins</h3><p>There are numerous ways to install vim plugins: Vundle, Pathogen, vim-plug, and now native packages in vim 8. My configuration uses Vundle because that’s what my co-worker used and it’s worked for me. There is a little bit of scripting to make this auto-install plugins:</p><pre>set exrc<br>set secure<br>&quot; Set up Vundle on first install - Vundle, in turn, installs all other plugins<br>let iCanHazVundle=1<br>let vundle_readme=expand(&#39;~/.vim/bundle/vundle/README.md&#39;)<br>if !filereadable(vundle_readme)<br>    echo &quot;Installing Vundle..&quot;<br>    echo &quot;&quot;<br>    silent !mkdir -p ~/.vim/bundle<br>    silent !git clone <a href="https://github.com/gmarik/vundle">https://github.com/gmarik/vundle</a> ~/.vim/bundle/vundle<br>    let iCanHazVundle=0<br>endif<br>set rtp+=~/.vim/bundle/vundle/<br>call vundle#rc()</pre><pre>Plugin &#39;gmarik/vundle&#39;<br>Plugin &#39;tpope/vim-fugitive&#39;<br>Plugin &#39;tpope/vim-commentary&#39;<br>Plugin &#39;tpope/vim-sensible&#39;<br>&quot; Python + Theme (molokai)<br>Plugin &#39;sentientmachine/Pretty-Vim-Python&#39;<br>&quot; JavaScript<br>Plugin &#39;jelera/vim-javascript-syntax&#39;<br>&quot; CSS and SCSS<br>Plugin &#39;cakebaker/scss-syntax.vim&#39;<br>&quot; Asynchronous Lint Engine<br>Plugin &#39;dense-analysis/ale&#39;</pre><pre>if iCanHazVundle == 0<br>    echo &quot;Installing Bundles, please ignore key map error messages&quot;<br>    echo &quot;&quot;<br>    :BundleInstall<br>endif<br></pre><p><a href="https://github.com/tpope/vim-fugitive">vim-fugitive</a> allows you to work with git from inside of vim. For the most part I still use git in another terminal but it is nice to inspect the status, diff, or blame within vim for some additional context.</p><p>As previously noted, vim-sensible updates a few common defaults and feels like an easy include for most vim users.</p><p><a href="https://github.com/tpope/vim-commentary">vim-commentary</a> helps comment out blocks of code, as it’s name might suggest. I use this more than I care to admit to comment/uncomment out large blocks of code.</p><p><a href="https://github.com/tpope/vim-sensible">vim-sensible</a> includes a few more vim default changes so if you were expecting some those have been set in an earlier section, this is where they were set.</p><p><a href="https://github.com/sentientmachine/Pretty-Vim-Python">Pretty-Vim-Python</a>, <a href="https://github.com/jelera/vim-javascript-syntax">vim-javascript-syntax</a>, and <a href="https://github.com/cakebaker/scss-syntax.vim">scss-syntax</a> are syntax binds for Python, JS and SCSS/CSS respectively. These aren’t particularly flashy but they are necessary if you want proper language syntax highlighting. You don’t need to use these versions exactly but some form of them. The molokai color scheme I use also comes from Pretty-Vim-Python.</p><p><a href="https://github.com/dense-analysis/ale">ALE</a> is the linting engine I use. I recently made the switch over from <a href="https://github.com/vim-syntastic/syntastic">syntastic</a> when I started incorporating auto-formatting into my workflow. Having incorporated auto-formatting into my workflow, I think I would have a hard time going back. I remember cringing when I would open an old file or something from a legacy project and my editor would covered red warnings. The stress of the choice between taking the time to clean it up or leaving it a mess was never fun. Now that stress is largely gone by having ALE fix 80% of those issues automatically.</p><p>You’ll note that this doesn’t include some of the popular/common plugins like <a href="https://github.com/kien/ctrlp.vim">ctrlp</a> and <a href="https://github.com/preservim/nerdtree">nerdtree</a>. I tried both of these at one point or another but ultimately they weren’t for me. I’m tempted to revisit ctrlp since I think that was the closer of the two for my style of file navigation.</p><h3>Plugin Configuration</h3><p>ALE is the only plugin above that requires configuration. Either they don’t have any configuration options or I’m simply happy with the defaults. As noted above, ALE is an auto-formatting and linting tool. In order to do its job effectively, it needs to be configured for the languages that you want it to evaluate. I have two global fixes that I make to all file types and that’s to remove any trailing new lines at the bottom of the file and to trim any trailing whitespace in each line. Previously I would highlight these as errors without automatically cleaning them up, but it’s easier now for me to remove them.</p><pre>&quot; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>&quot; Plugin Configuration<br>&quot; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</pre><pre>&quot; ALE Configuration<br>let g:ale_fixers = {<br>\    &#39;*&#39;: [&#39;remove_trailing_lines&#39;, &#39;trim_whitespace&#39;],<br>\   &#39;css&#39;: [&#39;prettier&#39;],<br>\   &#39;javascript&#39;: [&#39;prettier&#39;, &#39;eslint&#39;],<br>\   &#39;json&#39;: [&#39;prettier&#39;],<br>\   &#39;less&#39;: [&#39;prettier&#39;],<br>\   &#39;python&#39;: [&#39;black&#39;, &#39;isort&#39;],<br>\   &#39;scss&#39;: [&#39;prettier&#39;],<br>\   &#39;yaml&#39;: [&#39;prettier&#39;],<br>\}<br>let g:ale_linters = {<br>\   &#39;css&#39;: [&#39;prettier&#39;],<br>\   &#39;javascript&#39;: [&#39;prettier&#39;, &#39;eslint&#39;],<br>\   &#39;json&#39;: [&#39;prettier&#39;],<br>\   &#39;less&#39;: [&#39;prettier&#39;],<br>\   &#39;python&#39;: [&#39;flake8&#39;],<br>\   &#39;scss&#39;: [&#39;prettier&#39;],<br>\   &#39;yaml&#39;: [&#39;prettier&#39;],<br>\}<br>let g:ale_fix_on_save = 1</pre><p>These configurations do rely on a few external tools to be installed. For Python, I use <a href="https://black.readthedocs.io/en/stable/">black</a>, <a href="https://isort.readthedocs.io/en/stable/">isort</a>, and <a href="https://flake8.readthedocs.io/en/stable/">flake8</a>. These are installed globally in the shell with pip. For JS, I use <a href="https://prettier.io/">prettier</a> and <a href="https://eslint.org/">eslint</a>. Again these get installed globally with npm. prettier also has support for a number or related web languages/file formats, like CSS, JSON, and YAML. Between the two they cover nearly all the file types I interact with on a daily basis.</p><h3>I Can’t :q You</h3><p>The process of writing all of this out gave me even more time to reflect on the options I’ve made with my configuration and a good opportunity to reorganize it into more logical blocks. I’m always looking for small changes I can make to improve my process and flow. Even though the start was rough, the switch to vim has paid off for me. You can find my full vimrc file hosted in my Github account: <a href="https://github.com/mlavin/dotfiles/blob/master/vimrc">https://github.com/mlavin/dotfiles/blob/master/vimrc</a>. I hope that after reading this you’ll give it a try and make it your own.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da322260aa6c" width="1" height="1" alt=""><hr><p><a href="https://levelup.gitconnected.com/why-and-how-i-use-vim-da322260aa6c">Why (and How) I Use Vim</a> was originally published in <a href="https://levelup.gitconnected.com/">Level Up Coding</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Designing 3D Printed Board Game Inserts With Python]]></title>
            <link>https://medium.com/swlh/designing-3d-printed-board-game-inserts-with-python-d8e3a98d58f9?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d8e3a98d58f9</guid>
            <category><![CDATA[board-games]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[3d-printing]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[3d-modeling]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Sat, 18 Jan 2020 17:20:40 GMT</pubDate>
            <atom:updated>2020-01-24T07:38:32.337Z</atom:updated>
            <content:encoded><![CDATA[<h3>Designing 3D Printed Board Game Inserts with Python</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzUHzxWYx9leoCDMD3tECg.jpeg" /><figcaption>My finished insert for Burgle Bros</figcaption></figure><p>I love playing board games. I play at least once a week with a regular group and much more than that with my wife after the kids are in bed. More of my most recent hobbies have revolved around gaming, like miniature painting or dabbling back into Magic the Gathering (don’t judge me). When I received a 3D printer as a Christmas gift, I knew exactly what I wanted to do with it: make board game pieces and inserts. Having a great insert can really help with the setup/tear-down time for a game, and that means more time for playing.</p><p>I had made a few inserts before out of foam core, and they turned out pretty good. But the problem with foam core is that cutting it felt imprecise. Maybe I’m just bad at measuring, or at using an EXACTO knife. Also the 5 mm foam core I was using started to cut into the amount of space I had to work with, particularly if I wanted to make lots of small holds. I was hopeful that 3D printing would allow me better precision to make more complex designs than I could do with foam core.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Myiri4BSv6ykRzSzT-g8Jw.jpeg" /><figcaption>Setup game of Burgle Bros</figcaption></figure><p>That was the dream, but I needed a place to start. One of the first foam inserts I made was for a game called <a href="https://www.fowers.games/products/burgle-bros">Burgle Bros </a>by Tim Fowers. It’s a fantastic cooperative game when you work together as a gang of thieves robbing a bank. The art is amazing and it comes in this really cute box. By cute I mean both that it’s great looking but also it’s <strong>tiny</strong>. We would always joke that trying to get all the pieces back into the box is the game after the game. In any case I had a design for an existing insert I had previously made for this game. Now all I had to do was turn that design into something I could print.</p><h3>3D Printing Primer</h3><p>Many articles have been written about how 3D printers work so let me skip to the most relevant parts. My Ender3 printer and most FDM printers use g-code to program their movements. Where the nozzle should be, how hot should the nozzle or the bed be, and when and how much filament to extract is all controlled by g-code. I know very little about g-code, and thankfully you don’t need to either if you want to print something. The way most prints are made is by running a 3D model, like an STL file, through a program called a slicer to output the necessary g-code to print. I use a free slicer called <a href="https://ultimaker.com/software/ultimaker-cura">Cura</a> which I’ve configured for my printer and the filament I’m using. That process itself took a lot of trial and error, but that’s not the topic for today.</p><p>With all that we get the heart of the problem. If we want to 3D print something then we need a model of it. There are plenty of models available on sites like <a href="https://www.thingiverse.com/">Thingiverse</a> but someone has to be the one to start to create these designs. If you are capable with 3D modeling software then you should feel right at home. I did some early experiments with <a href="https://www.tinkercad.com/">TinkerCAD</a>, but I quickly became frustrated with repetitiveness of what I wanted to do. I just wanted to make some boxes and cut holes out of them. Using a GUI interface felt so imprecise, so I started looking for a better way.</p><h3>Generating STL Files with Python</h3><p>When I first looked into 3D printing with Python, I thought that I could go straight to generating the necessary g-code. I quickly abandoned that approach. There weren’t a lot of good resources and ultimately g-code isn’t a great format to share because it contains machine and filament specific configuration build into it. When researching how I could programmatically create STL files, I came across <a href="http://www.openscad.org/">OpenSCAD</a> and later <a href="https://github.com/SolidCode/SolidPython">SolidPython</a>. OpenSCAD has a scripting language built into it that can be used to generate solid shapes by building them up as the composition and intersection of other shapes. SolidPython is a Python library which allows you to write Python which can be converted into OpenSCAD, which can generate the STL files.</p><p>While I could have used the OpenSCAD language directly, using Python to generate it instead allowed me to get working quickly without having to learn a new language or program on top of the things I was already learning about using the slicer and my printer. At this point I was looking for anything to lower the barrier to entry. With any project, I like to see incremental progress and once I have something working, I can go back, reevaluate, and refine what I’ve done.</p><h3>Making Our First Box</h3><p>Let’s go ahead and make our first open box. That’s going to be a fundamental building block for all of our board game inserts. This is where my previous experiment with TinkerCAD helped me to visualize what I needed to do. An open box can be created out of two shapes: the outer rectangular solid which forms the bottom and walls and the inner rectangular solid which is the negative space which forms the inside of the box. The inner solid needs to be slightly smaller than the outer and that difference will determine the thickness of the walls. We can adjust the thickness of the bottom by how we position the box vertically inside. Here I decided to make both the walls and the bottom 3 mm thick.</p><pre># box.py<br>import solid<br># Size for our box<br>length, width, height = 100, 150, 50<br># Wall thickness<br>wall = 3<br>box = solid.cube(size=[length, width, height])<br># Inner box needs to be smaller based on the wall size<br>inside = solid.cube(<br>    size=[length - 2 * wall, width - 2 * wall, height - wall])<br># Offset inner box<br>inside = solid.translate([wall, wall, wall])(inside)<br># Remove inner box from the outer to create the empty space<br>box += solid.hole()(inside)<br>solid.scad_render_to_file(box, &quot;box.scad&quot;, include_orig_code=False)</pre><p>Running this script will output a box.scad file which we can convert to an STL file using OpenSCAD.</p><pre># Make sure you have SolidPython installed<br>$ python -m pip install solidpython<br># Run the above script saved as box.py<br>$ python box.py<br># Convert scad to stl<br>$ openscad -o box.stl box.scad</pre><p>Now this STL file is ready for our slicer. We can make this a little fancier by rounding the corners. I’m not sure why this makes it feel fancier to me. Maybe I’m showing my age as a web developer back to when rounded corners were all the rage. In any case, to round each of the corners we’ll add a cylinder to each of the corners. Shout out to <a href="https://www.thingiverse.com/WilliamAAdams/about">William Adams on Thingiverse</a> for his <a href="https://www.thingiverse.com/thing:9347">SCAD example post</a> for the basis of this logic.</p><pre># box.py (continued)</pre><pre>def roundbox(size, radius):<br>    &quot;&quot;&quot;Box with rounded edges.&quot;&quot;&quot;</pre><pre>    x, y, z = size<br>    # Adjust the box size to maintain the original dimensions<br>    x = x - radius * 2<br>    y = y - radius * 2<br>    z = z - 1<br>    return solid.minkowski()(<br>        solid.cube(size=[x, y, z]),<br>        solid.cylinder(r=radius),<br>    )</pre><p>To make use of this for the example box, you would replace the calls <em>solid.cube() </em>calls with <em>roundbox</em>. We’ll be making use of this function for our insert.</p><h3>Creating the Burgle Bros Insert</h3><p>Once we have the logic to create an open box, we aren’t too far from what we want for our insert. As I mentioned, I had previously created this insert in foam core so I had a plan from the start. My insert was made from two thin boxes, one with four equal compartments for the tokens and another with three compartments for the dice, guard pawns, and player pawns. As the foam core is about about 5 mm thick and the printed walls were going to be only 3 mm thick, I had to adjust the sizes slightly. That gave me more room to work with in this already cramped box. Both boxes are 171 mm x 60 mm x 15 mm, but have different sized wells inside of them to hold the different pieces.</p><pre># box.py (continued)</pre><pre>wall = 3<br># Tokens holder<br>x, y, z = 171, 60, 15<br>box = roundbox([x, y, z], radius=2)<br># Make 4 equal size wells<br>wells = 4<br>size = (x - (wells + 1) * wall) / wells<br>well = roundbox([size, y - wall * 2, z - wall], radius=2)<br>for i in range(wells):<br>    box += solid.hole()(solid.translate(<br>        [wall * (i + 1) + size * i, wall, wall])(well))<br># Output box to SCAD file<br>solid.scad_render_to_file(<br>    box, &quot;tokens.scad&quot;, include_orig_code=False)</pre><pre># Character, guards and dice holder<br>x, y, z = 171, 60, 15<br>box = roundbox([x, y, z], radius=2)<br># Character holder (77 mm)<br>characters = roundbox([77, y - wall * 2, z - wall], radius=2)<br>box += solid.hole()(solid.translate([wall, wall, wall])(characters))<br># Guard holder (52 mm)<br>guards = roundbox([52, y - wall * 2, z - wall], radius=2)<br>box += solid.hole()(solid.translate(<br>    [wall * 2 + 77, wall, wall])(guards))<br># Dice holder (30 mm)<br>dice = roundbox([30, y - wall * 2, z - wall], radius=2)<br>box += solid.hole()(solid.translate(<br>    [wall * 3 + 77 + 52, wall, wall])(dice))<br># Output box to SCAD file<br>solid.scad_render_to_file(<br>    box, &quot;characters.scad&quot;, include_orig_code=False)</pre><p>Combining this with our previous code and running it will generate the SCAD files which can again be converted into STL files using OpenSCADs command line interface.</p><pre># Run the above script saved as box.py<br>$ python box.py<br># Convert scad to stl<br>$ openscad -o tokens.stl tokens.scad<br>$ openscad -o characters.stl characters.scad</pre><p>Now we have two STL files that we can load into our slicer of choice. The print settings that you use will depend on your printer, but I printed mine at 0.2 mm layer thickness which gave enough accuracy for these simple shapes. The shapes don’t have any vertical overhang so they don’t require any supports. You may want to include a skirt or brim for better bed adhesion. Some of my early tests had problems with the bottom layers curling. That was addressed by lowering the nozzle temperature, which will depend on the material you are using.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2sjNV1F0VmVqTfhJDsDZkw.png" /></figure><p>They turned out great. Even though the end to end time is probably longer than it would have taken me to make it out of foam core, it’s much less labor intensive and the sizing is much more precise then I could get with foam core. The Python here is pretty rough but a good first pass and hopefully clear enough to be a starting point for you if you want to start making your own creations.</p><p>It’s great when multiple hobbies or interests can come together. Having a 3D printer to make these inserts is a complete luxury. With a little math, some planning, and patience, you can make well fitting inserts. For me, programming the logic in Python made it much easier for me to understand and get it done. It’s a fun challenge if you like math and puzzles like me. I’ll be following up with some of my more complex insert designs in future posts, so if you enjoyed this one, be on the look out for those.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8e3a98d58f9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swlh/designing-3d-printed-board-game-inserts-with-python-d8e3a98d58f9">Designing 3D Printed Board Game Inserts With Python</a> was originally published in <a href="https://medium.com/swlh">The Startup</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[My Python Environment Setup]]></title>
            <link>https://markdlavin.medium.com/my-python-environment-setup-fcddd0e99e5b?source=rss-b46afa4da29f------2</link>
            <guid isPermaLink="false">https://medium.com/p/fcddd0e99e5b</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Mark Lavin]]></dc:creator>
            <pubDate>Sat, 11 Jan 2020 16:52:59 GMT</pubDate>
            <atom:updated>2020-01-11T16:52:59.564Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K2lUtyv1epvPd2gEHaefbA.png" /></figure><p>I’ve been working with Python for more than a decade now and my local environment setup has gone through numerous iterations over that time. In the past few years I’ve used both Linux (Ubuntu) and OS X for my primary development operation system at work. Recently I setup a new Linux laptop and spent the time to better document how I had everything setup and configured. I chose tools which I could setup the same on both my personal Ubuntu machine as well as my Mac for work. I hope this can help you if you are new to Python and want a good starting place for your development environment. I don’t have any recent experience using Python on Windows so if that’s you, sorry I can’t help.</p><h3>Installing Python</h3><p>If you are using a Mac or Linux system then you likely already have some version of Python installed. However, the first thing I do when setting up a development machine for Python is to have a way to install different versions of Python and make sure that I’m not using or modifying the system level Python install. When both my work and personal machines ran Ubuntu I was happy to do this using apt repositories but when I switched to a Mac at work this was no longer an option. That’s when I started using <a href="https://github.com/pyenv/pyenv"><em>pyenv</em></a> and it’s been great. It’s easy to install on a Mac through Homebrew and easy enough to install on Linux through the git checkout. Having something that works consistently on both operating systems is important to me. It lowers the cognitive switching required when transitioning from my work laptop to my personal laptop. I wish I had made that transition sooner. I clung onto using the OS packing tools for so long it really hurt my ability to work with developers who preferred to use OS X for development. This is a consistent setup that can work for me or any of my team members and that’s a big win.</p><p>Once this is setup (please refer to the <em>pyenv</em> docs on how to do that for your system), I’ll go ahead and install the latest version of Python, which is 3.8.1 at the time of this writing, and make that my default Python version instead of the system Python.</p><pre># List out all available versions to install<br>$ pyenv install --list<br># Install Python 3.8.1<br>$ pyenv install 3.8.1<br># Make this the default instead of the system Python<br>$ pyenv global 3.8.1<br># Check that it worked<br>$ python<br>Python 3.8.1 (default, Jan 10 2020, 12:45:58)<br>[GCC 7.4.0] on linux<br>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.<br>&gt;&gt;&gt;</pre><p>Hooray! We now have installed the latest release of Python and we aren’t risking breaking our system Python. That’s a good start.</p><h3>Managing Virtual Environments</h3><p>I’m always juggling multiple Python projects at once. At work I’ve got different client projects and at home I have various small side projects or experiments and I need to keep their dependencies separate. Some people like to put their virtualenvs into their project directories, similar to what NodeJS would do with its node_modules. If you go that route then you might be fine using the built-in <a href="https://docs.python.org/3/library/venv.html"><em>venv</em></a> module but that’s not my preference. I prefer to keep my project directories clean of the virtualenv so instead, I’ve paired <em>pyenv</em> with <a href="https://github.com/pyenv/pyenv-virtualenv"><em>pyenv-virtualenv</em></a> to manage virtual environments so that I can isolate any package dependencies from project to project. This is managed by the same teams that manage <em>pyenv</em> and has similar installation methods (Homebrew or git checkout). Once you get through that setup (again see their documentation for the most up to date installation methods) we can create our first virtualenv.</p><pre># Create a new virtualenv<br>$ pyenv virtualenv 3.8.1 new-project<br># Activate the virtualenv before install packages<br>$ pyenv activate new-project</pre><p>Now we have an isolated environment to install packages for our next project. You will need to activate this virtualenv each time you open a new shell to work on this project. To make this easier you can set a .python-version file in your project directory with the name of your virtualenv and it will activate when you enter the directory.</p><pre># Set the virtualenv name (should be done in the project directory)<br>$ echo &quot;new-project&quot; &gt; .python-version</pre><p>If you are working on a team project you might not want to include this file in your repository unless everyone can agree on using the same virtual environment name.</p><h3>Linting Tools</h3><p>The last piece of my Python setup is linting tools. This is an optional step but it’s one that I’ve gotten a lot of value out of recently. There are three packages that I install and configure globally (in the new default Python, not the system one) so that I can incorporate them into my text editor. These are:</p><ul><li><a href="https://black.readthedocs.io/en/stable/">black</a></li><li><a href="http://flake8.pycqa.org/en/stable/">flake8</a></li><li><a href="https://isort.readthedocs.io/en/stable/">isort</a></li></ul><p>All of these can be installed with <em>pip</em>:</p><pre># Check confirm I&#39;m not using the system Python<br>$ python --version<br>Python 3.8.1<br># Install the latest versions with pip<br>$ python -m pip install black flake8 isort</pre><p>Each of these serve a complementary role in improving my Python code. <em>black</em> is an auto-formatting tool to ensure your code fits with PEP 8 style. I originally hesitated using it because I didn’t like some of the choices that it made, in particular I still like single quotes over double quotes as the default. However, I’ve since embraced the freedom of not having to think about tedious things like how to wrap long lines or large imports. <em>black</em> does all that for me within the comfort of my editor on save. I encourage you to let go of control and let <em>black</em> auto-format your code for you. It’s very freeing.</p><p><em>isort</em> fits a similar role as <em>black</em> but it’s focused on sorting imports. It cleanly sorts imports alphabetically and groups them into standard library, third-party packages, and internal packages. It might just be confirmation bias but it feels like conflicting import order is a common source of merge conflicts on the teams I’ve worked on. Once I fully embraced auto-formatting it was a natural step to continue on with <em>isort</em>. It again removes one more choice I have to make when coding. <em>isort</em> does require a little configuration to play nicely with <em>black</em>.</p><pre># This should live as ~/.isort.cfg<br>[settings]<br>multi_line_output=3<br>include_trailing_comma=True<br>force_grid_wrap=0<br>use_parentheses=True<br>line_length=88</pre><p>Now I don’t have to think about how to sort my imports or work to wrap my lines properly but I can still make plenty of mistakes in my code. While <em>flake8</em> doesn’t automatically fix issues that it finds, it does help highlight potential problems in my code. <em>flake8</em> helps me find issues like unused imports, unused variables, or variables that are used but never defined. Again that helps me find issues before I try to run the application or test suite saves me time of jumping back and forth finding errors. Here I also let the conventions from black take precedence and I’ve configured flake8 to ignore issues when they would be in conflict:</p><pre># This should live as ~/.config/flake8<br>[flake8]<br>max-line-length = 88<br>extend-ignore = E203,E231,W503,W504</pre><p>I have mine configured globally but will also configure these on a project by project basis in the case where teams might have slightly different conventions. The only downside that I’ve seen so far when adopting these tools is that the first save/clean up of an existing file can create a lot of noise or unrelated changes. So far it’s been worth it for me and if you’ve never used <em>black</em>, <em>isort</em>, or <em>flake8</em> then I would encourage you to dive in and give them a try.</p><p>I hope you enjoyed this little journey into how I like to setup my Python environment. Be on the look out for follow up posts on how I configure my editor or more project specific setup. Ping me if you found this helpful or if there are other useful tools that I can include in my setup.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcddd0e99e5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>