<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Start Coding Now - python</title><link href="https://startcodingnow.com/" rel="alternate"/><link href="https://startcodingnow.com/category/python/feed" rel="self"/><id>https://startcodingnow.com/</id><updated>2024-08-24T00:00:00-07:00</updated><entry><title>Understanding the Template Method Pattern in Python</title><link href="https://startcodingnow.com/template-method-design-pattern" rel="alternate"/><published>2024-08-24T00:00:00-07:00</published><updated>2024-08-24T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2024-08-24:/template-method-design-pattern</id><summary type="html">&lt;p class="first last"&gt;Template your business logic algorithms to swap out different services without breaking your application.&lt;/p&gt;
</summary><content type="html">&lt;div class="section" id="introduction"&gt;
&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;I found myself wanting to write an extensible class for handling a few fancy new APIs out there. There are a &lt;strong&gt;ton&lt;/strong&gt; that do the same thing, and the field is constantly evolving, so it's useful to be able to easily switch from one to the next.&lt;/p&gt;
&lt;p&gt;I chose the Template Method design pattern. This is useful when you have a series of steps that need to be executed in a specific order, but the implementation of some steps may vary.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="what-is-the-template-method-pattern"&gt;
&lt;h2&gt;What is the Template Method Pattern?&lt;/h2&gt;
&lt;p&gt;The Template Method pattern defines the skeleton of an algorithm in a base class but lets subclasses override specific steps of the algorithm without changing its structure. It's called "template method" because it defines a template for an algorithm's steps.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="a-simple-example-coffee-and-tea"&gt;
&lt;h2&gt;A Simple Example: Coffee and Tea&lt;/h2&gt;
&lt;p&gt;Let's illustrate this pattern with a simple example of brewing beverages. We'll create a base class for brewing beverages and then implement specific classes for coffee and tea.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;abc&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;ABC&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;abc&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;abstractmethod&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Beverage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ABC&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;boil_water&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;brew&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pour_in_cup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_condiments&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;boil_water&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Boiling water"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;pour_in_cup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Pouring into cup"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;brew&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

    &lt;span class="nd"&gt;@abstractmethod&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_condiments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coffee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Beverage&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;brew&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Dripping coffee through filter"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_condiments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Adding sugar and milk"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Tea&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Beverage&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;brew&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Steeping the tea"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;add_condiments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Adding lemon"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Usage&lt;/span&gt;
&lt;span class="n"&gt;coffee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Coffee&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;tea&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Tea&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Making coffee:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;coffee&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nb"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;Making tea:"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tea&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In this example, &lt;tt class="docutils literal"&gt;Beverage&lt;/tt&gt; is our abstract base class that defines the template method &lt;tt class="docutils literal"&gt;prepare()&lt;/tt&gt;. This method outlines the steps for preparing a beverage, but leaves the implementation of &lt;tt class="docutils literal"&gt;brew()&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;add_condiments()&lt;/tt&gt; to the subclasses.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="benefits-of-the-template-method-pattern"&gt;
&lt;h2&gt;Benefits of the Template Method Pattern&lt;/h2&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Reduce duplication by implementing the common parts of the algorithm in the base class.&lt;/li&gt;
&lt;li&gt;Subclasses can easily override certain steps without changing the overall algorithm structure.&lt;/li&gt;
&lt;li&gt;The base class handles the algorithm's structure, while the subclasses focus on the specific details of each step.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As long as you're clear on the data structure you want your methods to return, you can swap in many different subclasses.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="real-world-applications"&gt;
&lt;h2&gt;Real-World Applications&lt;/h2&gt;
&lt;p&gt;The Template Method pattern is widely used in frameworks and libraries. For instance:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;In web frameworks, the base controller class might define a template method for handling HTTP requests, with subclasses implementing specific behaviors for different routes.&lt;/li&gt;
&lt;li&gt;In testing frameworks, the setup-run-teardown process is often implemented as a template method, allowing test classes to override specific parts of this process.&lt;/li&gt;
&lt;li&gt;When calling external services, your template transforms the API response into the data structure your application is expecting.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="section" id="example-template-class"&gt;
&lt;h3&gt;Example Template Class&lt;/h3&gt;
&lt;p&gt;You might want to call a nutrition facts API to get info about food, but not feel locked in to a single API provider. You could write a &lt;tt class="docutils literal"&gt;NutritionFacts&lt;/tt&gt; base class which outlines the &lt;tt class="docutils literal"&gt;get_nutrition_facts&lt;/tt&gt; method and returns a Django &lt;tt class="docutils literal"&gt;NutritionInfo&lt;/tt&gt; model object, outlining the &lt;tt class="docutils literal"&gt;fat&lt;/tt&gt;, &lt;tt class="docutils literal"&gt;carbohydrates&lt;/tt&gt;, and &lt;tt class="docutils literal"&gt;protein&lt;/tt&gt; in a particular meal.&lt;/p&gt;
&lt;p&gt;Then, when you're fed up (get it?) with the &lt;a class="reference external" href="https://spoonacular.com/food-api"&gt;Spoonacular API&lt;/a&gt;, you can implement the &lt;a class="reference external" href="https://www.edamam.com/"&gt;Edamam API&lt;/a&gt; with .&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="conclusion"&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The Template Method pattern allows for the creation of flexible and reusable code structures, making it easier to implement variations of an algorithm while keeping the overall process consistent. By understanding and applying this pattern, you can write more maintainable and extensible code in your Python projects.&lt;/p&gt;
&lt;p&gt;Remember, while design patterns like the Template Method are useful, they should be applied judiciously. If you're only making one class, it might not be helpful to make that inherit from a base class.&lt;/p&gt;
&lt;p&gt;Best of luck in your design endeavors.&lt;/p&gt;
&lt;/div&gt;
</content><category term="python"/><category term="design-patterns"/></entry><entry><title>Custom Dictionary Representation of Python Dataclass</title><link href="https://startcodingnow.com/dataclass-asdict-factory" rel="alternate"/><published>2024-07-10T00:00:00-07:00</published><updated>2024-07-10T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2024-07-10:/dataclass-asdict-factory</id><summary type="html">&lt;p class="first last"&gt;Customize your dataclass dictionary generation.&lt;/p&gt;
</summary><content type="html">&lt;p&gt;I've really enjoyed the shortcuts provided by Python's &lt;a class="reference external" href="https://docs.python.org/3/library/dataclasses.html"&gt;dataclasses&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Recently, I found myself working with an API that required camelCase keys, but I wanted to keep my Python keys in snake_case as I find it &lt;em&gt;comforting&lt;/em&gt;.&lt;/p&gt;
&lt;div class="section" id="creating-a-dataclass"&gt;
&lt;h2&gt;Creating a dataclass&lt;/h2&gt;
&lt;p&gt;I created the following dataclass for working with cell locations in Google Sheets:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;dataclasses&lt;/span&gt;

&lt;span class="nd"&gt;@dataclass&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Coordinate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;sheet_id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;row_index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
    &lt;span class="n"&gt;column_index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To instantiate an object:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;coord&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Coordinate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# cell D2&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="turning-the-object-into-a-dictionary"&gt;
&lt;h2&gt;Turning the object into a dictionary&lt;/h2&gt;
&lt;p&gt;The &lt;a class="reference external" href="https://docs.python.org/3/library/dataclasses.html#dataclasses.asdict"&gt;dataclasses.asdict()&lt;/a&gt; function transforms your dataclass object. It reuses your declared key names. For example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;dataclasses&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asdict&lt;/span&gt;

&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"sheet_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"row_index"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"column_index"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coord&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="making-a-custom-dict-factory"&gt;
&lt;h2&gt;Making a custom dict factory&lt;/h2&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;asdict()&lt;/tt&gt; accepts an optional parameter &lt;tt class="docutils literal"&gt;dict_factory&lt;/tt&gt; which is a function that tells &lt;tt class="docutils literal"&gt;asdict&lt;/tt&gt; how to
parse the dataclass object.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="c1"&gt;# help us transform the keys strings&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;snake_case_to_camel_case&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# split character and remove underscores&lt;/span&gt;
    &lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"_"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;# capitalize the first letter of all words except the first&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
        &lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data_list&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# our method to plug into `asdict()`&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;camel_case_dict_factory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;snake_case_to_camel_case&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;field&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To use this new factory:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"sheetId"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"rowIndex"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"columnIndex"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;asdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;coord&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dict_factory&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;camel_case_dict_factory&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;# True&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;For reference, here's the &lt;a class="reference external" href="https://github.com/python/cpython/blob/f62161837e68c1c77961435f1b954412dd5c2b65/Lib/dataclasses.py#L1362"&gt;standard implementation of asdict()&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
</content><category term="python"/><category term="dataclasses"/></entry><entry><title>Get Started with Python</title><link href="https://startcodingnow.com/get-started-with-python" rel="alternate"/><published>2021-11-01T00:00:00-07:00</published><updated>2021-11-01T00:00:00-07:00</updated><author><name>Lance Goyke</name></author><id>tag:startcodingnow.com,2021-11-01:/get-started-with-python</id><summary type="html">&lt;p&gt;If you’re looking to build your Python skills, there are a bunch of
resources out there. Being self-taught, I’ve gone through quite a few of
them!&lt;/p&gt;
&lt;p&gt;Here’s a list of the most memorable stuff I’ve come across, as well as
an explanation of a few basic …&lt;/p&gt;</summary><content type="html">&lt;p&gt;If you’re looking to build your Python skills, there are a bunch of
resources out there. Being self-taught, I’ve gone through quite a few of
them!&lt;/p&gt;
&lt;p&gt;Here’s a list of the most memorable stuff I’ve come across, as well as
an explanation of a few basic concepts that most people tend to find
confusing.&lt;/p&gt;
&lt;div class="section" id="books"&gt;
&lt;h2&gt;Books&lt;/h2&gt;
&lt;p&gt;A book or course is the best way to begin. Tutorials are great at some
point, but when you need an overview of the entire language, look for
something more comprehensive.&lt;/p&gt;
&lt;div class="section" id="learn-python-the-hard-way-by-zed-shaw"&gt;
&lt;h3&gt;&lt;em&gt;Learn Python the Hard Way&lt;/em&gt; by Zed Shaw&lt;/h3&gt;
&lt;p&gt;&lt;a class="reference external image-reference" href="https://shop.learncodethehardway.org/access/buy/9/"&gt;&lt;img alt="Learn Python 3 the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code by Zed A. Shaw" src="./images/learn-python-3-the-hard-way-zed-shaw.jpg"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Of all the books and such that I read, &lt;a class="reference external" href="https://shop.learncodethehardway.org/access/buy/9/"&gt;Learn Python the Hard
Way&lt;/a&gt; was my
favorite intro to Python because it has a little bit of general
programming sprinkled in.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="automate-the-boring-stuff-with-python-by-al-sweigart"&gt;
&lt;h3&gt;&lt;em&gt;Automate the Boring Stuff with Python&lt;/em&gt; by Al Sweigart&lt;/h3&gt;
&lt;p&gt;&lt;a class="reference external image-reference" href="https://automatetheboringstuff.com/"&gt;&lt;img alt="Automate the Boring Stuff with Python: Practical Programming for Total Beginners by Al Sweigart" src="./images/automate-the-boring-stuff-with-python-al-sweigart.jpg"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://automatetheboringstuff.com/"&gt;Automate the Boring Stuff with
Python&lt;/a&gt; was the first Python
book I went through and it’s also good (and free to read online!).&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="video-courses"&gt;
&lt;h2&gt;Video Courses&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external image-reference" href="https://realpython.com/courses/what-is-pip/"&gt;&lt;img alt="Beautiful artwork from Real Python’s articles on pip, the Python package manager" src="./images/What-is-PIP_Watermarked-800x450.webp"/&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You can try your luck with YouTube which usually has something great,
but Real Python traditionally has the best stuff on Python. They have a
ton of great, free articles, but they also have a membership that will
give you access to some video courses if you prefer to learn that way.
Here are two that tackle some of the most important fundamentals of
Python:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://realpython.com/courses/working-python-virtual-environments/"&gt;Virtual
environments&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://realpython.com/courses/what-is-pip/"&gt;pip - The Python package
manager&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And then once you can figure out pip, packages, and virtual
environments, your most powerful tools are just learning all the magic
that’s out there.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="useful-packages"&gt;
&lt;h2&gt;Useful Packages&lt;/h2&gt;
&lt;div class="section" id="python-standard-library"&gt;
&lt;h3&gt;Python Standard Library&lt;/h3&gt;
&lt;p&gt;The Python Standard Library has a bunch of stuff available to help you
manipulate strings, use different data structures, and more. Doug
Hellman wrote a bunch of short articles on these packages called &lt;a class="reference external" href="https://pymotw.com/3/"&gt;Python
Module of the Week&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="third-party-packages"&gt;
&lt;h3&gt;Third-Party Packages&lt;/h3&gt;
&lt;p&gt;You can get similar benefits out of learning powerful third party
packages (those unassociated with Python proper). Here are some examples
that you would just install with `pip install &lt;em&gt;package-name&lt;/em&gt;`:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;a class="reference external" href="https://www.djangoproject.com/"&gt;django&lt;/a&gt; - a framework for
building web applications&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://docs.python-requests.org/en/latest/"&gt;requests&lt;/a&gt; - helps
you make web requests&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/psf/black"&gt;black&lt;/a&gt; - a formatter to give you a
consistent, “pythonic” code style&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/jorisschellekens/borb"&gt;borb&lt;/a&gt; - for working
with PDFs&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://pillow.readthedocs.io/en/stable/"&gt;pillow&lt;/a&gt; - for working
with images&lt;/li&gt;
&lt;li&gt;&lt;a class="reference external" href="https://github.com/kkroening/ffmpeg-python"&gt;ffmpeg&lt;/a&gt; - for working
with video&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="projects"&gt;
&lt;h2&gt;Projects&lt;/h2&gt;
&lt;p&gt;Lastly, make sure you get your hands dirty! You can’t truly understand
how to structure a project until you build one!&lt;/p&gt;
&lt;p&gt;Learn Python the Hard Way and Automate the Boring Stuff with Python have
a bunch of projects that get you going.&lt;/p&gt;
&lt;p&gt;If you’re feeling ambitious, try &lt;a class="reference external" href="https://startcodingnow.com/django-projects-ideas-for-beginners/"&gt;building your own
project&lt;/a&gt;
or tinkering around with someone else’s to get going!&lt;/p&gt;
&lt;/div&gt;
</content><category term="python"/><category term="automate-the-boring-stuff-with-python"/><category term="learn-python-3-the-hard-way"/><category term="pip"/><category term="python-standard-library"/><category term="real-python"/><category term="virtual-environments"/></entry></feed>