<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<title></title>
	<link href="https://koenwoortman.com/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="https://koenwoortman.com/"/>
	<generator uri="https://www.getzola.org/">Zola</generator>
	<updated>2022-08-26T00:00:00+00:00</updated>
	<id>https://koenwoortman.com/atom.xml</id>
	<entry xml:lang="en">
		<title>Create a type alias in Rust</title>
		<published>2022-08-26T00:00:00+00:00</published>
		<updated>2022-08-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-type-aliasing/" type="text/html"/>
		<id>https://koenwoortman.com/rust-type-aliasing/</id>
		<content type="html">&lt;p&gt;By using the &lt;code&gt;type&lt;&#x2F;code&gt; statement in Rust we can alias an existing native type to a newly named type.&lt;&#x2F;p&gt;
&lt;p&gt;See for example the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Age &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;u32&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; age: Age &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;28&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix `character literal may only contain one codepoint` error in Rust</title>
		<published>2022-08-25T00:00:00+00:00</published>
		<updated>2022-08-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-character-literal-may-only-contain-one-codepoint/" type="text/html"/>
		<id>https://koenwoortman.com/rust-character-literal-may-only-contain-one-codepoint/</id>
		<content type="html">&lt;p&gt;Many programming languages leave it up to you to use single quotes or double quotes for your strings. Sometimes with slight function differences.&lt;&#x2F;p&gt;
&lt;p&gt;In Rust we create a variable  of  the &lt;code&gt;character&lt;&#x2F;code&gt; type when using single quotes and use double quotes for strings.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, when we define a &amp;quot;string&amp;quot; using single quotes as follows&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;Koen&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We get the following error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;error: character literal may only contain one codepoint
&lt;&#x2F;span&gt;&lt;span&gt; --&amp;gt; src&#x2F;main.rs:2:16
&lt;&#x2F;span&gt;&lt;span&gt;  |
&lt;&#x2F;span&gt;&lt;span&gt;2 |     let name = &amp;#39;Koen&amp;#39;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead we should use double quotes when defining a string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Parse a file in Rust</title>
		<published>2022-08-24T00:00:00+00:00</published>
		<updated>2022-08-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-read-file-to-string/" type="text/html"/>
		<id>https://koenwoortman.com/rust-read-file-to-string/</id>
		<content type="html">&lt;p&gt;To parse the contents of a file in Rust we make use of &lt;code&gt;std::fs&lt;&#x2F;code&gt; from the standard library. Where &lt;code&gt;fs&lt;&#x2F;code&gt; is short for filesystem. Using &lt;code&gt;read_to_string()&lt;&#x2F;code&gt; we can read the file contents.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::fs;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; file_contents &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;fs::read_to_string(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;home&#x2F;koen&#x2F;.vimrc&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Unable to read file&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    println!(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, file_contents);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To loop over the lines in the file we can call &lt;code&gt;lines()&lt;&#x2F;code&gt; to get an iterator.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;std::fs;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; file_contents &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;fs::read_to_string(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;home&#x2F;koen&#x2F;.vimrc&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;expect&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Unable to read file&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; line &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in&lt;&#x2F;span&gt;&lt;span&gt; file_contents.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;lines&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        println!(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, line);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Parse integer from string in Rust</title>
		<published>2022-08-23T00:00:00+00:00</published>
		<updated>2022-08-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-parse-integer-from-string/" type="text/html"/>
		<id>https://koenwoortman.com/rust-parse-integer-from-string/</id>
		<content type="html">&lt;p&gt;In some cases you may receive a numeric value as a string type, for example, a value from &lt;code&gt;stdin&lt;&#x2F;code&gt; will be given as a string. In such cases you may need to convert the string to an integer before proceeding, for this we may use the &lt;code&gt;str::parse::&amp;lt;T&amp;gt;()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;42&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; the_answer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; input.parse::&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;i32&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the operation fails, meaning that the string could not be parsed into the desired type, an error is thrown. Here we catch the potential error with &lt;code&gt;.unwrap&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Alternatively we can specify the desired type in the assignment operation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;42&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; the_answer: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;i32 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; input.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;parse&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;unwrap&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Split string by whitespace in Ruby</title>
		<published>2022-08-22T00:00:00+00:00</published>
		<updated>2022-08-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-split-string-by-whitespace/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-split-string-by-whitespace/</id>
		<content type="html">&lt;p&gt;To split a string in Ruby on a whitespace character we can use the &lt;code&gt;#split&lt;&#x2F;code&gt; method. Which may take a pattern to split on as the first parameter, but splits the string on whitespace by default if no parameter is given.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Foo Bar&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.split
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; [&amp;quot;Foo&amp;quot;, &amp;quot;Bar&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result of this operation is an array containing the parts that were delimited by a whitespace character.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Clone objects in Ruby: dup vs. clone</title>
		<published>2022-08-21T00:00:00+00:00</published>
		<updated>2022-08-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-clone-object/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-clone-object/</id>
		<content type="html">&lt;p&gt;When you need to clone an object in Ruby you basically have two options. You can reach for either the &lt;code&gt;#dup&lt;&#x2F;code&gt; or &lt;code&gt;#clone&lt;&#x2F;code&gt; method. The two methods achieve mostly the same results, but they are no aliases of each other like some Ruby methods.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;#clone&lt;&#x2F;code&gt; method copies over the object attributes as well to the new object. While &lt;code&gt;#dup&lt;&#x2F;code&gt; typically uses the object class to initialize a second object.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, when cloning an object the internal state is maintained in the clone. For example whether the object was frozen or not.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;s &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Original&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;freeze
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;dup&lt;&#x2F;span&gt;&lt;span&gt;.frozen?
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; false
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; s.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;clone&lt;&#x2F;span&gt;&lt;span&gt;.frozen?
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Filter JavaScript array by object property</title>
		<published>2022-08-20T00:00:00+00:00</published>
		<updated>2022-08-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-filter-array-by-object-property/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-filter-array-by-object-property/</id>
		<content type="html">&lt;p&gt;In JavaScript we can filter arrays by object value using the &lt;code&gt;.filter()&lt;&#x2F;code&gt; method, which is available on array objects.&lt;&#x2F;p&gt;
&lt;p&gt;Lets for example assume that we have an array with the following options, and wish to filter based on the value of the &lt;code&gt;required&lt;&#x2F;code&gt; property.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;options &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  {
&lt;&#x2F;span&gt;&lt;span&gt;    name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  {
&lt;&#x2F;span&gt;&lt;span&gt;    name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;.filter()&lt;&#x2F;code&gt; method returns a new array instance, it doesn&#x27;t mutate the array on which we call the function.&lt;&#x2F;p&gt;
&lt;p&gt;We can filter out the non-required options as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;mandatory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;options.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(({&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;required&lt;&#x2F;span&gt;&lt;span&gt;}) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;required &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or do the opposite and filter out the required options.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;optional &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;options.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(({&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;required&lt;&#x2F;span&gt;&lt;span&gt;}) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;required &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP Arrow Functions</title>
		<published>2022-08-19T00:00:00+00:00</published>
		<updated>2022-08-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-arrow-functions/" type="text/html"/>
		<id>https://koenwoortman.com/php-arrow-functions/</id>
		<content type="html">&lt;p&gt;With version 7.4 PHP introduced arrow functions, which are anonymous functions with a shorter syntax. Anonymous functions mostly come in handy for callback functions, when filtering an array for example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;$positive &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_filter&lt;&#x2F;span&gt;&lt;span&gt;($numbers, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$n&lt;&#x2F;span&gt;&lt;span&gt;) =&amp;gt; $n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; [1, 8]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Arrow functions are defined using the &lt;code&gt;fn&lt;&#x2F;code&gt; keyword and can only have one expression, for which the result is used as the return value. Therefore there is no need for the &lt;code&gt;return&lt;&#x2F;code&gt; statement, it&#x27;s not even allowed actually.&lt;&#x2F;p&gt;
&lt;p&gt;Arrow functions differ from anonymous functions in that they don&#x27;t require the &lt;code&gt;use&lt;&#x2F;code&gt; keyword in order to use variables from outside the function scope.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove an element from an array in Ruby</title>
		<published>2022-08-18T00:00:00+00:00</published>
		<updated>2022-08-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-remove-element-from-array/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-remove-element-from-array/</id>
		<content type="html">&lt;p&gt;In Ruby we have multiple options for removing an element from an array. If we wish to do so by matching a given value we can use &lt;code&gt;reject!&lt;&#x2F;code&gt; or &lt;code&gt;delete_if&lt;&#x2F;code&gt;. This removes all the matching values from the array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;toppings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;mozzarella&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tomato&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pineapple&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;toppings.reject! { |&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;| n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pineapple&amp;#39; &lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; toppings.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;inspect
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; [&amp;quot;mozzarella&amp;quot;, &amp;quot;tomato&amp;quot;, &amp;quot;salami&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Add item to hash in Ruby</title>
		<published>2022-08-17T00:00:00+00:00</published>
		<updated>2022-08-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-add-item-to-hash/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-add-item-to-hash/</id>
		<content type="html">&lt;p&gt;In order to add items to a hash in Ruby we most commonly use the square bracket notation (&lt;code&gt;[ ]&lt;&#x2F;code&gt;). Which associates the given key with the value in the hash. Do keep in mind that we use the same method to update the value of existing key in the hash.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;python: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;php: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Laravel&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;ruby: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ruby on Rails&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:javascript&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Express&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But we have other options as well, we could for example use the &lt;a href=&quot;&#x2F;ruby-merge-hashes&#x2F;&quot;&gt;merge method&lt;&#x2F;a&gt;. Or the &lt;code&gt;.store()&lt;&#x2F;code&gt; method that is actually an alias for the square bracket notation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;python: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;php: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Laravel&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;ruby: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ruby on Rails&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash&lt;&#x2F;span&gt;&lt;span&gt;.store(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:javascript&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Express&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Ruby: search for value in hash</title>
		<published>2022-08-16T00:00:00+00:00</published>
		<updated>2022-08-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-search-hash-for-value/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-search-hash-for-value/</id>
		<content type="html">&lt;p&gt;Normally you probably just use keys to access values in a hash. But in some cases you might just be interested whether a hash contains a certain value, not per se at a given key.&lt;&#x2F;p&gt;
&lt;p&gt;For this use-case we have the &lt;code&gt;.value?&lt;&#x2F;code&gt; method, which returns &lt;code&gt;true&lt;&#x2F;code&gt; when a value is found in a hash and &lt;code&gt;false&lt;&#x2F;code&gt; otherwise.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;python: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;php: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Laravel&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;ruby: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ruby on Rails&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts hash&lt;&#x2F;span&gt;&lt;span&gt;.value?(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; true
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts hash&lt;&#x2F;span&gt;&lt;span&gt;.value?(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Express&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do keep in mind here that this method is case sensitive if you are searching for string values.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Iterate through a hash in Ruby</title>
		<published>2022-08-15T00:00:00+00:00</published>
		<updated>2022-08-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-iterate-through-hash/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-iterate-through-hash/</id>
		<content type="html">&lt;p&gt;Looping over the contents of a hash in Ruby is quite similar to looping over the contents of an array. We can make use of the &lt;code&gt;.each&lt;&#x2F;code&gt; method, which is actually an alias for the &lt;code&gt;.each_pair&lt;&#x2F;code&gt; method, that returns an enumerator.&lt;&#x2F;p&gt;
&lt;p&gt;The key and value of the hash contents are passed to the enumerator.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;python: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;php: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Laravel&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;ruby: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ruby on Rails&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash&lt;&#x2F;span&gt;&lt;span&gt;.each &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Write &lt;&#x2F;span&gt;&lt;span&gt;#{key}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt; with &lt;&#x2F;span&gt;&lt;span&gt;#{value}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; Write python with Django
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; Write php with Laravel
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; Write ruby with Ruby on Rails
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides the &lt;code&gt;.each&lt;&#x2F;code&gt; method we have other options available to us as well:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;each_key&lt;&#x2F;code&gt; to loop over the keys in a hash.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;each_value&lt;&#x2F;code&gt; to loop over the values in a hash.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;each_with_index&lt;&#x2F;code&gt; to loop over the key and values in a hash with a corresponding index.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Merge hashes in Ruby</title>
		<published>2022-08-14T00:00:00+00:00</published>
		<updated>2022-08-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-merge-hashes/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-merge-hashes/</id>
		<content type="html">&lt;p&gt;If we wish to merge two hashes together in Ruby we can make use of the &lt;code&gt;.merge()&lt;&#x2F;code&gt; and &lt;code&gt;.merge!()&lt;&#x2F;code&gt; methods. The former method returns a new hash object, the latter adds the contents of the given hash to the hash object on which the method is called.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;merging-hashes-wish-merge&quot;&gt;Merging hashes wish .merge()&lt;&#x2F;h2&gt;
&lt;p&gt;Since &lt;code&gt;.merge()&lt;&#x2F;code&gt; returns a new hash object, the original hashes are kept intact. Allowing us to reuse them at another place in our code.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;hash1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;a: 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;b: 2&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;hash2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;c: 3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;d: 4&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;merged &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; hash1.merge(hash2)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; merged
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; hash1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {:a=&amp;gt;1, :b=&amp;gt;2, :c=&amp;gt;3, :d=&amp;gt;4}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {:a=&amp;gt;1, :b=&amp;gt;2}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;merging-hashes-wish-merge-1&quot;&gt;Merging hashes wish .merge!()&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;.merge!()&lt;&#x2F;code&gt; on the other hand mutates the hash on which we call the method, &lt;code&gt;hash1&lt;&#x2F;code&gt; in this case. Thus, there is not really a point in assigning the return value to a new variable.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;hash1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;a: 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;b: 2&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;hash2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;c: 3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;d: 4&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;hash1.merge!(hash2)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; hash1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {:a=&amp;gt;1, :b=&amp;gt;2, :c=&amp;gt;3, :d=&amp;gt;4}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;duplicate-keys&quot;&gt;Duplicate keys&lt;&#x2F;h2&gt;
&lt;p&gt;When we merge hashes with duplicate keys the value of the given hash is taken, not the hash on which we call the merge method. Both &lt;code&gt;.merge()&lt;&#x2F;code&gt; and &lt;code&gt;.merge!()&lt;&#x2F;code&gt; work in this manner.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;hash1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;hash: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;number one&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;hash2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;hash: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;number two&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; hash1.merge(hash2)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; result
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {:hash=&amp;gt;&amp;quot;number two&amp;quot;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Split a string in Rust</title>
		<published>2022-08-13T00:00:00+00:00</published>
		<updated>2022-08-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-split-string/" type="text/html"/>
		<id>https://koenwoortman.com/rust-split-string/</id>
		<content type="html">&lt;p&gt;In Rust we can use the method &lt;code&gt;.split()&lt;&#x2F;code&gt; to split a string by a given character.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; parts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;index&#x2F;home&#x2F;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or the method &lt;code&gt;.split_whitespace()&lt;&#x2F;code&gt; if we wish to split the string by a whitespace.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; parts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;home sweet home&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split_whitespace&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both methods return an iterator, over which we can loop for example.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Update a Ruby gem with bundler</title>
		<published>2022-08-12T00:00:00+00:00</published>
		<updated>2022-08-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-bundler-update-gem-version/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-bundler-update-gem-version/</id>
		<content type="html">&lt;p&gt;Bundler is generally the package manager you would use in your Ruby projects. When installing a gem you probably got the latest version installed on your system. But when time goes by, newer versions get released making it time to upgrade to later versions again!&lt;&#x2F;p&gt;
&lt;p&gt;To update a gem with bundler we can run the following command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bundle update &amp;lt;name&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Connect to NordVPN on login in Linux</title>
		<published>2022-08-11T00:00:00+00:00</published>
		<updated>2022-08-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-connect-to-nordvpn-on-login/" type="text/html"/>
		<id>https://koenwoortman.com/linux-connect-to-nordvpn-on-login/</id>
		<content type="html">&lt;p&gt;Since remote working has become more and more my standard, I&#x27;ve been connecting to more sketchy Wi-Fi networks than I would like. Think of flex working places and coffee shops. My VPN of choice for these places has been &lt;a href=&quot;https:&#x2F;&#x2F;www.dpbolvw.net&#x2F;bs122y1A719PRQQWYUTYQPRTZRUZYZ&quot;&gt;NordVPN&lt;&#x2F;a&gt;. Manually connecting each time gets too annoying after a couple times, luckily we can automate connecting to NordVPN using their &lt;a href=&quot;https:&#x2F;&#x2F;support.nordvpn.com&#x2F;Connectivity&#x2F;Linux&#x2F;1325531132&#x2F;Installing-and-using-NordVPN-on-Debian-Ubuntu-Raspberry-Pi-Elementary-OS-and-Linux-Mint.htm&quot;&gt;command line tools for Linux&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After running the &lt;code&gt;nordvpn login&lt;&#x2F;code&gt; command, we can run &lt;code&gt;nordvpn connect&lt;&#x2F;code&gt; to connect to the VPN. The latter command is the one I&#x27;m interested in when it comes to automatically connect after logging in.&lt;&#x2F;p&gt;
&lt;p&gt;To run a command after login in Linux we have several options, such as creating a service for your init system, using &lt;code&gt;@reboot&lt;&#x2F;code&gt; in a cron job or adding a Startup Application via a GUI. For simplicity I&#x27;m just adding the connect command to the &lt;code&gt;~&#x2F;.profile&lt;&#x2F;code&gt; file to do the trick.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.profile
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;nordvpn connect
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;~&#x2F;.profile&lt;&#x2F;code&gt; file is typically used to set variables for the user&#x27;s environment. This file is sourced after login.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Join an array of strings by a comma in Ruby</title>
		<published>2022-08-10T00:00:00+00:00</published>
		<updated>2022-08-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-join-array-with-comma/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-join-array-with-comma/</id>
		<content type="html">&lt;p&gt;To join an array to a string we can use the Ruby &lt;code&gt;.join()&lt;&#x2F;code&gt; method. This method takes as its argument the separator character to place between the elements in the array. Thus, to join array elements by a comma we can pass &lt;code&gt;&amp;quot;, &amp;quot;&lt;&#x2F;code&gt; as an argument to &lt;code&gt;.join()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;].join(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;, &amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;a, b, c&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove an installed Ruby gem</title>
		<published>2022-08-09T00:00:00+00:00</published>
		<updated>2022-08-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-remove-installed-gem/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-remove-installed-gem/</id>
		<content type="html">&lt;p&gt;To install a gem from RubyGems we can use the &lt;code&gt;install&lt;&#x2F;code&gt; subcommand. To remove a previously installed gem again we can use the &lt;code&gt;uninstall&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ gem uninstall foo
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To list all the available &lt;code&gt;gem&lt;&#x2F;code&gt; commands we can use:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ gem help commands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Base64 encode a Ruby hash</title>
		<published>2022-08-08T00:00:00+00:00</published>
		<updated>2022-08-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-base64-encode-hash/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-base64-encode-hash/</id>
		<content type="html">&lt;p&gt;The best option I&#x27;ve found to base64 encode a hash in Ruby is by first converting the hash to a JSON string. After which we can base64 encode it. This can be easily decoded by another program by first base64 decode the string and next parse the result as JSON.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;base64&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;json&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;id: 1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;slug: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;boo&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Base64&lt;&#x2F;span&gt;&lt;span&gt;.encode64(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;JSON&lt;&#x2F;span&gt;&lt;span&gt;.dump(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;hash&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; &amp;quot;eyJpZCI6MSwic2x1ZyI6ImJvbyJ9\n&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Make sure to require both the &lt;code&gt;base64&lt;&#x2F;code&gt; and &lt;code&gt;json&lt;&#x2F;code&gt; modules. The result of the operation can be decoded as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;JSON&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;load&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Base64&lt;&#x2F;span&gt;&lt;span&gt;.decode64(res))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {&amp;quot;id&amp;quot;=&amp;gt;1, &amp;quot;slug&amp;quot;=&amp;gt;&amp;quot;boo&amp;quot;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Ruby: Check if string contains substring</title>
		<published>2022-08-07T00:00:00+00:00</published>
		<updated>2022-08-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-check-if-string-contains-substring/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-check-if-string-contains-substring/</id>
		<content type="html">&lt;p&gt;To check whether a Ruby string contains a given substring the &lt;code&gt;include?&lt;&#x2F;code&gt; string method seems most convenient. You can pass it the substring as an argument and it returns either &lt;code&gt;true&lt;&#x2F;code&gt; or &lt;code&gt;false&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;rubylation&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;include?&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ruby&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;We found a match!&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; We found a match!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;include?&lt;&#x2F;code&gt; method looks for an exact match of the substring in the string. Therefore it is case sensitive for example.&lt;&#x2F;p&gt;
&lt;p&gt;If you are looking for a more advanced way of looking for a substring in a string you can look at regular expressions.&lt;&#x2F;p&gt;
&lt;p&gt;This returns the index of the match if found, if the string doesn&#x27;t contain a match for the regular expression &lt;code&gt;nil&lt;&#x2F;code&gt; is returned.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;rubylation&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=~ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&#x2F;Ruby&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; nil
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;rubylation&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=~ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&#x2F;Ruby&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;i
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>String interpolation in Ruby</title>
		<published>2022-08-06T00:00:00+00:00</published>
		<updated>2022-08-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-string-interpolation/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-string-interpolation/</id>
		<content type="html">&lt;p&gt;To use string interpolation in Ruby we must use a double quoted string, in which we can interpolate variable using &lt;code&gt;#{}&lt;&#x2F;code&gt;. Such as:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;you &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Robot&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hey &lt;&#x2F;span&gt;&lt;span&gt;#{you}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Hey Robot
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;String interpolation is a nice alternative to string concatenation.&lt;&#x2F;p&gt;
&lt;p&gt;In Ruby we can use the plus-sign (&lt;code&gt;+&lt;&#x2F;code&gt;) to do string concatenation. Which is basically glueing strings together as one. See for example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Koen&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Hello &amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;name
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;Hello Koen&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Although this works fine, if you need to concatenate multiple strings this quickly can get a bit messy. See for example the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;first_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Koen&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;last_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Woortman&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Hello &amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; first_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39; &amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span&gt; last_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;!&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;Hello Koen Woortman!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Obviously we can do the above a bit smarter, by combining the first and last name in one variable before doing string concatenation. But I hope you get the point.&lt;&#x2F;p&gt;
&lt;p&gt;Enter string interpolation, which is allowed in double quoted strings. Thus using &lt;code&gt;&amp;quot;&amp;quot;&lt;&#x2F;code&gt; instead of &lt;code&gt;&#x27;&#x27;&lt;&#x2F;code&gt;. To achieve the same as above with string interpolation see the following code example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;first_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Koen&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;last_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Woortman&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hello &lt;&#x2F;span&gt;&lt;span&gt;#{first_name} #{last_name}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;Hello Koen Woortman!&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Custom CLI commands in Ruby on Rails</title>
		<published>2022-08-05T00:00:00+00:00</published>
		<updated>2022-08-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rails-custom-cli-command/" type="text/html"/>
		<id>https://koenwoortman.com/rails-custom-cli-command/</id>
		<content type="html">&lt;p&gt;Web frameworks like Django or Laravel allow you to create custom CLI commands for your application. Which allow you to do some run some tasks, or mutations on your models for example.&lt;&#x2F;p&gt;
&lt;p&gt;Ruby on Rails doesn&#x27;t support the creation of commands in a similar manner. That would be, that is, creating commands isn&#x27;t built into Rails in a standardized way.&lt;&#x2F;p&gt;
&lt;p&gt;How you would accomplish the same, and (sort of) create a custom CLI command with Ruby on Rails is by making use of the &lt;code&gt;rails runner&lt;&#x2F;code&gt; command. Which allows you to run a file by file path or run a line of code directly.&lt;&#x2F;p&gt;
&lt;p&gt;The option to run a file by path is the option we&#x27;re interested in here. This allows you to run a piece of code like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rails runner &amp;quot;lib&#x2F;commands&#x2F;update_users.rb&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Vue.js: Specify multiple types for component props</title>
		<published>2022-08-04T00:00:00+00:00</published>
		<updated>2022-08-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-multiple-prop-types/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-multiple-prop-types/</id>
		<content type="html">&lt;p&gt;Vue doesn&#x27;t enforce a specific type for the component props you define. Even if you specify a property to be of type &lt;code&gt;Number&lt;&#x2F;code&gt; you can potentially still pass a string. It&#x27;s up to you. It does, however, spawn some useful console warnings in development.&lt;&#x2F;p&gt;
&lt;p&gt;In some cases your component might accept multiple prop types. We can specify which ones by using an array containing the accepted types. As is shown in the following example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  props: {
&lt;&#x2F;span&gt;&lt;span&gt;    value: {
&lt;&#x2F;span&gt;&lt;span&gt;      type: [&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;      required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Rename a column in a Laravel migration</title>
		<published>2022-08-03T00:00:00+00:00</published>
		<updated>2022-08-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-migration-rename-column/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-migration-rename-column/</id>
		<content type="html">&lt;p&gt;To rename a database column in Laravel we need a database migration.&lt;&#x2F;p&gt;
&lt;p&gt;The method we want for this is &lt;code&gt;renameColumn()&lt;&#x2F;code&gt; which takes takes the current column name as its first parameter and the new preferred column name as a second parameter.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;RenameCurrentNameColumn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;Migration
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;up&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;::table(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tasks&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Blueprint &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$table&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;            $table-&amp;gt;renameColumn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;current_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;new_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;down&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;::table(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tasks&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Blueprint &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$table&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;            $table-&amp;gt;renameColumn(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;new_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;current_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Obviously renaming the column does require you to change the references in your code. If changing all the references is too much work, &lt;a href=&quot;https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;9.x&#x2F;eloquent-mutators#defining-an-accessor&quot;&gt;an accessor might make your life easier&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Note: This action does require you to have the dependency &lt;code&gt;doctrine&#x2F;dbal&lt;&#x2F;code&gt; installed.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: Sum values in a multidimensional array</title>
		<published>2022-08-02T00:00:00+00:00</published>
		<updated>2022-08-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-sum-values-in-multidimensional-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-sum-values-in-multidimensional-array/</id>
		<content type="html">&lt;p&gt;To sum up all the values in a multidimensional array that live under a specific key we make use of two array functions.&lt;&#x2F;p&gt;
&lt;p&gt;First, we call the function &lt;code&gt;array_column()&lt;&#x2F;code&gt;, which returns to us an array of all the values found for a specific key.&lt;&#x2F;p&gt;
&lt;p&gt;Second, we use the method &lt;code&gt;array_sum()&lt;&#x2F;code&gt; to sum up all those values.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$products &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    [
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pen&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;price&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    ],
&lt;&#x2F;span&gt;&lt;span&gt;    [
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Paper&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;price&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    ],
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$sum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_sum&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_column&lt;&#x2F;span&gt;&lt;span&gt;($products, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;price&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; 8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove a foreign key constraint in a Laravel migration</title>
		<published>2022-08-01T00:00:00+00:00</published>
		<updated>2022-08-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-migration-remove-foreign-key-constraint/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-migration-remove-foreign-key-constraint/</id>
		<content type="html">&lt;p&gt;Dropping a foreign key constraint might sound like an odd thing to do. But in some cases you need to. For example, when you turn a foreign key field into a polymorphic relationship.&lt;&#x2F;p&gt;
&lt;p&gt;In such a case you want to use the &lt;code&gt;dropForeign()&lt;&#x2F;code&gt; method, which takes the name of the foreign key constraint in your database. This name takes the following form: &lt;code&gt;{table}_{column}_foreign&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, if you would have a foreign key constraint in the &lt;code&gt;tasks&lt;&#x2F;code&gt; table for the &lt;code&gt;user_id&lt;&#x2F;code&gt; column, you could drop the key constraint using the following migration.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;up&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;::table(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tasks&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Blueprint &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$table&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;        $table-&amp;gt;dropForeign(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tasks_user_id_foreign&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove duplicate values from a JavaScript array</title>
		<published>2022-07-31T00:00:00+00:00</published>
		<updated>2022-07-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-duplicates-from-array/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-duplicates-from-array/</id>
		<content type="html">&lt;p&gt;There are multiple ways of removing duplicate values from an array in JavaScript. You can for example use the &lt;code&gt;.filter()&lt;&#x2F;code&gt; or &lt;code&gt;.reduce()&lt;&#x2F;code&gt; methods. But the most convenient way I&#x27;ve found is by using Sets.&lt;&#x2F;p&gt;
&lt;p&gt;Set theory is a mathematical branch, in which a set represents a collection of unique elements. Thus, each element can only occur once in a set. Perfect for the use-case of removing duplicates from an array it seems.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Mercury&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Venus&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Venus&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Earth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;unique &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...new &lt;&#x2F;span&gt;&lt;span&gt;Set(values)];
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(unique);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; (3) [&amp;#39;Mercury&amp;#39;, &amp;#39;Venus&amp;#39;, &amp;#39;Earth&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can initialize a Set object and pass our array containing duplicate values. By using the spread operator (&lt;code&gt;...&lt;&#x2F;code&gt;) we can turn our Set object back into an array, which now only contains unique values.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Return Laravel validation errors as JSON</title>
		<published>2022-07-30T00:00:00+00:00</published>
		<updated>2022-07-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-validation-errors-as-json/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-validation-errors-as-json/</id>
		<content type="html">&lt;p&gt;When you submit data to your Laravel API you probably don&#x27;t want the validation errors returned to you as HTML.&lt;&#x2F;p&gt;
&lt;p&gt;If this happens to you, you can tell Laravel to return the validation errors as a JSON by setting the HTTP header &lt;code&gt;&#x27;Accept: application&#x2F;json&#x27;&lt;&#x2F;code&gt; in your request.&lt;&#x2F;p&gt;
&lt;p&gt;If you do need to return the validation messages somehow as JSON by yourself, you can add the following in one of your controller methods.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;($validator-&amp;gt;fails()) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;response()-&amp;gt;json($validator-&amp;gt;messages(), &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Response&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;HTTP_UNPROCESSABLE_ENTITY&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use Bootstrap for pagination links in Laravel</title>
		<published>2022-07-29T00:00:00+00:00</published>
		<updated>2022-07-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-use-bootstrap-for-pagination-links/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-use-bootstrap-for-pagination-links/</id>
		<content type="html">&lt;p&gt;Before Laravel V8 the pagination links were by default styled using Bootstrap. But since Laravel version 8 this default changed to Tailwind CSS. If you not already upgraded to version 8 this might be something to pay attention to.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, if you prefer to you can still make use of Bootstrap styling for pagination links. We need to make a change to the &lt;code&gt;AppServiceProvider&lt;&#x2F;code&gt; for this.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;for-laravel-9&quot;&gt;For Laravel 9&lt;&#x2F;h2&gt;
&lt;p&gt;For Laravel v9 we can choose the Bootstrap version. In the &lt;code&gt;boot()&lt;&#x2F;code&gt; method we can call either the &lt;code&gt;useBootstrapFive()&lt;&#x2F;code&gt; or &lt;code&gt;useBootstrapFour()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;namespace &lt;&#x2F;span&gt;&lt;span&gt;App\Providers;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ServiceProvider&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Pagination\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Paginator&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;AppServiceProvider &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;ServiceProvider
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;boot&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; For Bootstrap v5
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Paginator&lt;&#x2F;span&gt;&lt;span&gt;::useBootstrapFive();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; For Bootstrap v4
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Paginator&lt;&#x2F;span&gt;&lt;span&gt;::useBootstrapFour();
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;for-laravel-8&quot;&gt;For Laravel 8&lt;&#x2F;h2&gt;
&lt;p&gt;For Laravel v8, in the &lt;code&gt;boot()&lt;&#x2F;code&gt; method call &lt;code&gt;useBootstrap()&lt;&#x2F;code&gt; to make use of Bootstrap styled pagination views.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;namespace &lt;&#x2F;span&gt;&lt;span&gt;App\Providers;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ServiceProvider&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Pagination\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Paginator&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;AppServiceProvider &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;ServiceProvider
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;boot&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Paginator&lt;&#x2F;span&gt;&lt;span&gt;::useBootstrap();
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Bitwise AND operator in PHP (&amp;)</title>
		<published>2022-07-28T00:00:00+00:00</published>
		<updated>2022-07-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-bitwise-and-operator/" type="text/html"/>
		<id>https://koenwoortman.com/php-bitwise-and-operator/</id>
		<content type="html">&lt;p&gt;For the bitwise AND operator in PHP the ampersand sign is used (&lt;code&gt;&amp;amp;&lt;&#x2F;code&gt;). Together with the other bitwise operators in PHP, the AND operator allows us to perform logical operators.&lt;&#x2F;p&gt;
&lt;p&gt;The AND operator only results in &lt;code&gt;true&lt;&#x2F;code&gt; if both operands are &lt;code&gt;true&lt;&#x2F;code&gt;, or &lt;code&gt;1&lt;&#x2F;code&gt; instead of &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The truth table of the AND operator in PHP looks as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: Get the class name of an object without namespace</title>
		<published>2022-07-27T00:00:00+00:00</published>
		<updated>2022-07-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-get-class-name-without-namespace/" type="text/html"/>
		<id>https://koenwoortman.com/php-get-class-name-without-namespace/</id>
		<content type="html">&lt;p&gt;We can get the qualified namespaced class name of an object by using the &lt;code&gt;get_class()&lt;&#x2F;code&gt; function. Assuming that our object&#x27;s class has a namespace at least. If we wish to get just the class name we can use the reflection class to get the short name of the class.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; get_class($obj);
&lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &amp;quot;App\Models\Project&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; (new \ReflectionClass($obj))-&amp;gt;getShortName();
&lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &amp;quot;Project&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option would be to actually get the qualified namespaced class name of the object and get the class name via string manipulation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;gt;&amp;gt;&amp;gt; end(explode(&amp;#39;\\&amp;#39;, get_class($obj)));
&lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &amp;quot;Project&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Do note that we need to escape the backslash.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a Zip file using PHP</title>
		<published>2022-07-26T00:00:00+00:00</published>
		<updated>2022-07-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-create-zip-file/" type="text/html"/>
		<id>https://koenwoortman.com/php-create-zip-file/</id>
		<content type="html">&lt;p&gt;The archive extensions &amp;quot;Zip&amp;quot; allows you to create Zip archives with PHP. We can easily create Zip files by making use of the &lt;code&gt;ZipArchive&lt;&#x2F;code&gt; class.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$zip &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ZipArchive&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$zip-&amp;gt;open(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;.&#x2F;destination.zip&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ZipArchive&lt;&#x2F;span&gt;&lt;span&gt;::&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;CREATE&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; addFile(&amp;#39;file path&amp;#39;, &amp;#39;file name in zip archive&amp;#39;);
&lt;&#x2F;span&gt;&lt;span&gt;$zip-&amp;gt;addFile(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;.&#x2F;README.md&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;README.md&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$zip-&amp;gt;close();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After &amp;quot;opening&amp;quot; the Zip archive we can start adding files, at last we need to call the close method to properly save the changes.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove a key from the request input in Laravel</title>
		<published>2022-07-25T00:00:00+00:00</published>
		<updated>2022-07-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-remove-key-from-request-inputs/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-remove-key-from-request-inputs/</id>
		<content type="html">&lt;p&gt;From the data that is submitted to the controller we often can save all the fields to the model. In some cases a field doesn&#x27;t belong to the model, we wish to use it for something else.&lt;&#x2F;p&gt;
&lt;p&gt;In such a case we can remove the field from the request data. By using the &lt;code&gt;remove()&lt;&#x2F;code&gt; method for instance.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    $request-&amp;gt;request-&amp;gt;remove(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;input_field&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or we can retrieve the inputs except for a given set of fields.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    $data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$request-&amp;gt;except([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;input_field&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix the `403 This Action Is Unauthorized` error in Laravel</title>
		<published>2022-07-24T00:00:00+00:00</published>
		<updated>2022-07-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-fix-403-action-unauthorized/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-fix-403-action-unauthorized/</id>
		<content type="html">&lt;p&gt;When submitting a form, or sending an POST, PUT or PATCH request you may encounter a Laravel error page saying:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;403 | THIS ACTION IS UNAUTHORIZED.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As the error says, this means the user isn&#x27;t authorized to make the request.&lt;&#x2F;p&gt;
&lt;p&gt;If you are using a custom Form Request class, this is probably the place to look. Make sure that the &lt;code&gt;authorize()&lt;&#x2F;code&gt; method you&#x27;ll find there returns &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;authorize&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Laravel: Apply validation rules only if the field is present</title>
		<published>2022-07-23T00:00:00+00:00</published>
		<updated>2022-07-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-validate-field-only-if-present/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-validate-field-only-if-present/</id>
		<content type="html">&lt;p&gt;Validation rules in Laravel allow you to require fields and apply other validation rules. But in some cases you want to only validate the fields that are present in a request. For example, in an API that supports partial updates of a resource; PATCH requests in REST API&#x27;s more specifically.&lt;&#x2F;p&gt;
&lt;p&gt;In such cases you can add the &lt;code&gt;sometimes&lt;&#x2F;code&gt; rule to the validator rule list. This makes sure that the other validation rules are only applied if the field is present in the request.&lt;&#x2F;p&gt;
&lt;p&gt;For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;rules&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;sometimes|required|max:255&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    ];
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The combination of &lt;code&gt;sometimes&lt;&#x2F;code&gt; and &lt;code&gt;required&lt;&#x2F;code&gt; seems odd at first hand but is valid. The &lt;code&gt;required&lt;&#x2F;code&gt; rule makes sure that the &lt;code&gt;field&lt;&#x2F;code&gt; cannot be &lt;code&gt;null&lt;&#x2F;code&gt;, the &lt;code&gt;sometimes&lt;&#x2F;code&gt; rule allows the field to be kept out of the request.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Bash: Assign default value for a positional parameter</title>
		<published>2022-07-22T00:00:00+00:00</published>
		<updated>2022-07-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-default-value-for-positional-parameter/" type="text/html"/>
		<id>https://koenwoortman.com/bash-default-value-for-positional-parameter/</id>
		<content type="html">&lt;p&gt;In a bash script that uses parameters we can set a default value for our arguments. We can do so by making use of a special way of parameter expansion: &lt;code&gt;${parameter:-default}&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If the parameter is not set or null bash expands the parameter as &lt;em&gt;default&lt;&#x2F;em&gt;. Otherwise the value of the parameter is maintained.&lt;&#x2F;p&gt;
&lt;p&gt;For example, let&#x27;s assume we have the following script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;var&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${1&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;Hello}&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$var, user&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we call our script without positional parameters, the value &lt;code&gt;$1&lt;&#x2F;code&gt; is not set and therefore we set the variable &lt;code&gt;var&lt;&#x2F;code&gt; to the default of &lt;code&gt;&amp;quot;Hello&amp;quot;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;Hello, user
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we instead call our script with &lt;code&gt;&amp;quot;Goodbye&amp;quot;&lt;&#x2F;code&gt; as the first positional argument, &lt;code&gt;var&lt;&#x2F;code&gt; is expanded to this value.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;script Goodbye
&lt;&#x2F;span&gt;&lt;span&gt;Goodbye, user
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Difference between web and api routes in Laravel</title>
		<published>2022-07-21T00:00:00+00:00</published>
		<updated>2022-07-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-difference-between-web-and-api-routes/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-difference-between-web-and-api-routes/</id>
		<content type="html">&lt;p&gt;After creating a brand new Laravel (I&#x27;m using v9) project you&#x27;ll find multiple files in the routes directory.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;laravel-project $ tree routes&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;routes&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;├── api.php
&lt;&#x2F;span&gt;&lt;span&gt;├── channels.php
&lt;&#x2F;span&gt;&lt;span&gt;├── console.php
&lt;&#x2F;span&gt;&lt;span&gt;└── web.php
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;channels.php&lt;&#x2F;code&gt; file is for broadcasting and &lt;code&gt;console.php&lt;&#x2F;code&gt; for command closures. The other two, &lt;code&gt;api.php&lt;&#x2F;code&gt; and &lt;code&gt;web.php&lt;&#x2F;code&gt;, are pretty similar and both for web routes. But how are they different? For this we need to reach for the &lt;code&gt;RouteServiceProvider&lt;&#x2F;code&gt; (see below).&lt;&#x2F;p&gt;
&lt;p&gt;In the &lt;code&gt;boot()&lt;&#x2F;code&gt; method of the &lt;code&gt;RouteServiceProvider&lt;&#x2F;code&gt; we see that both the api and web routes are registered. They are registered with a couple differences though.&lt;&#x2F;p&gt;
&lt;p&gt;As you can see below there are three notable differences:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;There is rate limiting configured for api routes.&lt;&#x2F;li&gt;
&lt;li&gt;Api routes use the &lt;code&gt;api&lt;&#x2F;code&gt; middleware group where web routes use the &lt;code&gt;web&lt;&#x2F;code&gt; middleware group.&lt;&#x2F;li&gt;
&lt;li&gt;Api have the &lt;code&gt;api&lt;&#x2F;code&gt; prefix, thus routes from the &lt;code&gt;api.php&lt;&#x2F;code&gt; are prefixed with &lt;code&gt;api&#x2F;&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The different middleware groups can be found in &lt;code&gt;app&#x2F;Http&#x2F;Kernel.php&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;RouteServiceProvider &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;ServiceProvider
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ..
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * Define your route model bindings, pattern filters, and other route configuration.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;@return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; void
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;boot&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        $this-&amp;gt;configureRateLimiting();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        $this-&amp;gt;routes(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                -&amp;gt;prefix(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                -&amp;gt;group(base_path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;routes&#x2F;api.php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;web&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;                -&amp;gt;group(base_path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;routes&#x2F;web.php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * Configure the rate limiters for the application.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;@return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; void
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;protected &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;configureRateLimiting&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;RateLimiter&lt;&#x2F;span&gt;&lt;span&gt;::for(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Limit&lt;&#x2F;span&gt;&lt;span&gt;::perMinute(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;60&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;by($request-&amp;gt;user()?-&amp;gt;id ?: $request-&amp;gt;ip());
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a variable exists in Laravel Blade templates</title>
		<published>2022-07-20T00:00:00+00:00</published>
		<updated>2022-07-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-check-if-variable-exist-in-blade/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-check-if-variable-exist-in-blade/</id>
		<content type="html">&lt;p&gt;This might now be a common use-case, but let&#x27;s say for example you need to check if a Laravel Blade layout template whether a variable exists.&lt;&#x2F;p&gt;
&lt;p&gt;In regular PHP code you can use the &lt;code&gt;isset()&lt;&#x2F;code&gt; function for this. In Blade there is a directive which allows for such a check.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;@isset($obj)
&lt;&#x2F;span&gt;&lt;span&gt;{{ $obj }}
&lt;&#x2F;span&gt;&lt;span&gt;@endisset
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you need to check whether a variable exists in conjunction with another check you can just use the regular &lt;code&gt;@if&lt;&#x2F;code&gt; directive.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;@if(isset($obj) &amp;amp;&amp;amp; gettype($ob) === &amp;#39;object&amp;#39;)
&lt;&#x2F;span&gt;&lt;span&gt;{{ $obj }}
&lt;&#x2F;span&gt;&lt;span&gt;@endif
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: How to check whether a key exists in an array</title>
		<published>2022-07-19T00:00:00+00:00</published>
		<updated>2022-07-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-key-exists-in-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-key-exists-in-array/</id>
		<content type="html">&lt;p&gt;Wonder how you to check whether a key exists in an array? You can say what you want about PHP but this is one clear function name: &lt;code&gt;array_key_exists()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$arr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;key&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;value&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_key_exists&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;key&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $arr)) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;No need to worry about &amp;quot;Undefined array key&amp;quot; errors&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt; if you wish to access an array value by its key in an assignment operation you can use the &lt;a href=&quot;&#x2F;php-null-coalescing-operator&#x2F;&quot;&gt;null coalescing operator&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to raise custom exceptions in Ruby</title>
		<published>2022-07-18T00:00:00+00:00</published>
		<updated>2022-07-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-custom-exception-class/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-custom-exception-class/</id>
		<content type="html">&lt;p&gt;You can get quite far with throwing generic exceptions, by just passing a string to &lt;code&gt;raise&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;There was a timeout&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But, creating your own Exception hierarchies allows you to be more specific in catching and handling them.&lt;&#x2F;p&gt;
&lt;p&gt;All custom errors classes you define should inherit from &lt;code&gt;StandardError&lt;&#x2F;code&gt; or one of its subclasses.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;HttpTimeoutError &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt; &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;StandardError
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span&gt;HttpTimeoutError
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Laravel: hot-reload Blade templates with Vite</title>
		<published>2022-07-17T00:00:00+00:00</published>
		<updated>2022-07-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-vitejs-hot-reload-blade-templates/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-vitejs-hot-reload-blade-templates/</id>
		<content type="html">&lt;p&gt;Since the release of version 0.3 of the Laravel vite-plugin Vite can do a full page reload after changes to a Blade template are made.&lt;&#x2F;p&gt;
&lt;p&gt;So first things first, make sure that you upgrade to the right or last version of the vite-plugin.&lt;&#x2F;p&gt;
&lt;p&gt;Second, add &lt;code&gt;refresh: true&lt;&#x2F;code&gt; to the configuration of the laravel plugin in the &lt;code&gt;vite.config.js&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ defineConfig } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vite&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;laravel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;laravel-vite-plugin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;defineConfig&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  plugins: [
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;laravel&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;      input: [
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;resources&#x2F;css&#x2F;app.css&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;resources&#x2F;js&#x2F;app.js&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      ],
&lt;&#x2F;span&gt;&lt;span&gt;      refresh: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    }),
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Last but not least, try it out by running &lt;code&gt;npm run dev&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Laravel: Set the Content-Type header to application&#x2F;json</title>
		<published>2022-07-16T00:00:00+00:00</published>
		<updated>2022-07-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-set-content-type-header-json/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-set-content-type-header-json/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;Content-Type&lt;&#x2F;code&gt; header is added to responses to indicate the MIME type of the response data. For JSON API&#x27;s the MIME type to return is &lt;code&gt;application&#x2F;json&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In Laravel we can use the &lt;code&gt;header()&lt;&#x2F;code&gt; method to the response object, which allows us to set the &lt;code&gt;Content-Type&lt;&#x2F;code&gt; header to &lt;code&gt;application&#x2F;json&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;response()-&amp;gt;header(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Content-Type&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;application&#x2F;json&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A quicker way is to use the &lt;code&gt;json()&lt;&#x2F;code&gt; method which sets this header for you.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;response()-&amp;gt;json($data);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If this doesn&#x27;t yield the expected content type, your middleware might interfere.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Iterate over the keys in an object using Object.keys()</title>
		<published>2022-07-15T00:00:00+00:00</published>
		<updated>2022-07-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-iterate-over-object-keys/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-iterate-over-object-keys/</id>
		<content type="html">&lt;p&gt;An easy way to loop over the keys in a JavaScript object is by extracting out the keys as an array using &lt;code&gt;Object.keys()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After doing so we can loop over the result as a regular array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Object&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;keys&lt;&#x2F;span&gt;&lt;span&gt;({ a: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, c: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; (3) [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;of &lt;&#x2F;span&gt;&lt;span&gt;keys) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides the approach above we can directly loop over key value pairs in an object as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;obj &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{ a: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, c: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;[key, value] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;of &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Object&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;entries&lt;&#x2F;span&gt;&lt;span&gt;(obj)) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check the type of a variable in JavaScript using `typeof`</title>
		<published>2022-07-14T00:00:00+00:00</published>
		<updated>2022-07-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-check-type-of-a-variable/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-check-type-of-a-variable/</id>
		<content type="html">&lt;p&gt;To check the type of a given variable or value we use the &lt;code&gt;typeof&lt;&#x2F;code&gt; operator, which returns the type as a string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;type &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= typeof &lt;&#x2F;span&gt;&lt;span&gt;input;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(type);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;#39;string&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The possible return values of the &lt;code&gt;typeof&lt;&#x2F;code&gt; operator are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;number&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;string&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;object&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;bigint&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;symbol&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;function&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;There is a gotcha here, odd enough the type of the value &lt;code&gt;null&lt;&#x2F;code&gt; is &lt;code&gt;&amp;quot;object&amp;quot;&lt;&#x2F;code&gt;, for the historic reason why I recommend to read why at &lt;a href=&quot;https:&#x2F;&#x2F;2ality.com&#x2F;2013&#x2F;10&#x2F;typeof-null.html&quot;&gt;2ality.com&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove duplicate values from an array in PHP</title>
		<published>2022-07-13T00:00:00+00:00</published>
		<updated>2022-07-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-remove-duplicates-from-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-remove-duplicates-from-array/</id>
		<content type="html">&lt;p&gt;To remove duplicate values from an array in PHP we can use the built-in array function &lt;code&gt;array_unique()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This function requires the array to remove values from as a first argument. The second argument is optional and allows you to change how array elements are compared.&lt;&#x2F;p&gt;
&lt;p&gt;The function returns a new array, the original is not mutated by &lt;code&gt;array_unique()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;array_unique(array $array, int $flags = SORT_STRING): array
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_unique&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you are using Laravel collections, you might want to fall back on the &lt;code&gt;unique()&lt;&#x2F;code&gt; method that can be called on collections; &lt;a href=&quot;https:&#x2F;&#x2F;laravel.com&#x2F;docs&#x2F;master&#x2F;collections#method-unique&quot;&gt;see the docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP - Check if array contains a value</title>
		<published>2022-07-12T00:00:00+00:00</published>
		<updated>2022-07-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-value-in-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-value-in-array/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;in_array()&lt;&#x2F;code&gt; function in PHP tells you whether a value is present in a given array. By default it does a loose comparison, meaning that it evaluates to &lt;code&gt;true&lt;&#x2F;code&gt; if you check whether a string &lt;code&gt;&amp;quot;1&amp;quot;&lt;&#x2F;code&gt; is present in an array with the number one: &lt;code&gt;[1]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;in_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; true
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;in_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; true
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;in_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can do a strict comparison by passing &lt;code&gt;true&lt;&#x2F;code&gt; as a third argument to the &lt;code&gt;in_array()&lt;&#x2F;code&gt; function&amp;quot;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;in_array&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a number is an integer in JavaScript</title>
		<published>2022-07-11T00:00:00+00:00</published>
		<updated>2022-07-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-check-if-variable-is-integer/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-check-if-variable-is-integer/</id>
		<content type="html">&lt;p&gt;In certain cases you need to check whether the number that is passed as an argument or so is an integer.&lt;&#x2F;p&gt;
&lt;p&gt;For such a use-case we use the method &lt;code&gt;Number.isInteger()&lt;&#x2F;code&gt;. Which returns &lt;code&gt;true&lt;&#x2F;code&gt; if the number is an integer and &lt;code&gt;false&lt;&#x2F;code&gt; otherwise.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;isInteger&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;42&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Seems pretty straight forward but there are a couple gotchas to be aware of.&lt;&#x2F;p&gt;
&lt;p&gt;This one is probably as expected but integers as a string evaluate to &lt;code&gt;false&lt;&#x2F;code&gt;. If you are expecting an integer from a form field you should parse it first to an integer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;isInteger&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;42&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Floating-point looking values which represent an integer evaluate to &lt;code&gt;true&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;isInteger&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;42.0&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same as above holds up when the precision limit of floats is exceeded, therefore the following evaluates to &lt;code&gt;true&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;isInteger&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;42.000000000000001&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But with one less zero it evaluates to &lt;code&gt;false&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;isInteger&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;42.00000000000001&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reverse a string in JavaScript</title>
		<published>2022-07-10T00:00:00+00:00</published>
		<updated>2022-07-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-reverse-string/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-reverse-string/</id>
		<content type="html">&lt;p&gt;JavaScript arrays can be easily reversed by using the &lt;code&gt;.reverse()&lt;&#x2F;code&gt; array method. Unfortunately such a method does not exist for strings in JavaScript. This &lt;code&gt;.reverse()&lt;&#x2F;code&gt; method for arrays  does prove to be useful when needing to reverse a string as well however.&lt;&#x2F;p&gt;
&lt;p&gt;In order to reverse a string we can first split it into an array for single characters, then reverse the array and finally glue it back together using the &lt;code&gt;.join()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;stressed&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;str.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;  [&amp;#39;s&amp;#39;, &amp;#39;t&amp;#39;, &amp;#39;r&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;s&amp;#39;, &amp;#39;s&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;d&amp;#39;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;str.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;reverse&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; [&amp;#39;d&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;s&amp;#39;, &amp;#39;s&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;r&amp;#39;, &amp;#39;t&amp;#39;, &amp;#39;s&amp;#39;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;str.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;desserts&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Note that the &lt;code&gt;.reverse()&lt;&#x2F;code&gt; method reverses the array in-place. Therefor there is no need to do a reassignment as we do with &lt;code&gt;.split()&lt;&#x2F;code&gt; and &lt;code&gt;.join()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We can chain these method calls together into a one-liner as well:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;stressed&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;reverse&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;desserts&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Git worktree workflow with release branches</title>
		<published>2022-07-09T00:00:00+00:00</published>
		<updated>2022-07-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-worktree-workflow-with-release-branches/" type="text/html"/>
		<id>https://koenwoortman.com/git-worktree-workflow-with-release-branches/</id>
		<content type="html">&lt;p&gt;In most projects I use a branching model as described by &lt;a href=&quot;https:&#x2F;&#x2F;nvie.com&#x2F;posts&#x2F;a-successful-git-branching-model&#x2F;#the-main-branches&quot;&gt;Vincent Driessen&lt;&#x2F;a&gt; with a &lt;code&gt;dev&lt;&#x2F;code&gt;, &lt;code&gt;staging&lt;&#x2F;code&gt; and &lt;code&gt;prod&lt;&#x2F;code&gt; branch.&lt;&#x2F;p&gt;
&lt;p&gt;For new feature development I branch off of &lt;code&gt;dev&lt;&#x2F;code&gt;, and once those changes are considered adequate &lt;code&gt;dev&lt;&#x2F;code&gt; gets promoted to &lt;code&gt;staging&lt;&#x2F;code&gt;. In most cases this works fine.&lt;&#x2F;p&gt;
&lt;p&gt;In some cases however a hotfix needs to be applied, for which I preferably branch off from staging. To make sure that not fully tested features don&#x27;t land prematurely in production.&lt;&#x2F;p&gt;
&lt;p&gt;This can cause some hassle in my local git repo. With switching from &lt;code&gt;dev&lt;&#x2F;code&gt; to &lt;code&gt;staging&lt;&#x2F;code&gt; I need to make sure my &lt;code&gt;node_modules&lt;&#x2F;code&gt; are properly and there are no migrations applied which should not be applied on staging yet. Don&#x27;t wanna mess around if I already need to apply a hotfix.&lt;&#x2F;p&gt;
&lt;p&gt;Enter &lt;a href=&quot;https:&#x2F;&#x2F;git-scm.com&#x2F;docs&#x2F;git-worktree&quot;&gt;git worktrees&lt;&#x2F;a&gt;. Git worktrees allow you to checkout multiple worktrees in different directories while using the same repository. Thus, I have a separate directory for the &lt;code&gt;staging&lt;&#x2F;code&gt; branch which have their own node_modules assuming that you added that directory to the .gitignore.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;add-a-worktree&quot;&gt;Add a worktree&lt;&#x2F;h2&gt;
&lt;p&gt;To create a new git worktree to your repository you can run the following command in your repository.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git worktree add &amp;lt;PATH&amp;gt; &amp;lt;BRANCH&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets for example say I have a repository in the folder &lt;code&gt;~&#x2F;myproject&lt;&#x2F;code&gt;. To add a worktree on the same level for the &lt;code&gt;staging&lt;&#x2F;code&gt; branch I run the following command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;~&#x2F;myproject$ git worktree add ..&#x2F;myproject-staging staging
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix `Configure your bundler to alias &#x27;vue&#x27;` in Vite.js</title>
		<published>2022-07-08T00:00:00+00:00</published>
		<updated>2022-07-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vitejs-fix-configure-your-bundler-to-alias-vue/" type="text/html"/>
		<id>https://koenwoortman.com/vitejs-fix-configure-your-bundler-to-alias-vue/</id>
		<content type="html">&lt;p&gt;You may encounter the following Vue warning saying you need to create an alias for &lt;code&gt;vue&lt;&#x2F;code&gt;. Especially when you depend on some third party Vue plugins this is lurking.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;[Vue warn]: Component provided template option but runtime compilation is not supported in this build of Vue. Configure your bundler to alias &amp;quot;vue&amp;quot; to &amp;quot;vue&#x2F;dist&#x2F;vue.esm-bundler.js&amp;quot;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to resolve this warning we can create an &lt;a href=&quot;&#x2F;vitejs-module-aliases&#x2F;&quot;&gt;alias in our vite config&lt;&#x2F;a&gt;. The alias as defined below maps Vue imports to &lt;code&gt;vue&#x2F;dist&#x2F;vue.esm-bundler.js&lt;&#x2F;code&gt; which was given as a hint in the warning.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ defineConfig } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vite&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;vue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;@vitejs&#x2F;plugin-vue&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;path&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;defineConfig&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  plugins: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;vue&lt;&#x2F;span&gt;&lt;span&gt;()],
&lt;&#x2F;span&gt;&lt;span&gt;  resolve: {
&lt;&#x2F;span&gt;&lt;span&gt;    alias: {
&lt;&#x2F;span&gt;&lt;span&gt;      vue: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vue&#x2F;dist&#x2F;vue.esm-bundler.js&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Module aliases in Vite.js</title>
		<published>2022-07-07T00:00:00+00:00</published>
		<updated>2022-07-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vitejs-module-aliases/" type="text/html"/>
		<id>https://koenwoortman.com/vitejs-module-aliases/</id>
		<content type="html">&lt;p&gt;Coming from Webpack we know the use of aliases to change how modules are resolved. Think for example about using the &lt;code&gt;@&lt;&#x2F;code&gt; as an alias of the &lt;code&gt;src&lt;&#x2F;code&gt; directory in your project, which is often found in Vue projects.&lt;&#x2F;p&gt;
&lt;p&gt;Vite.js comes with support for module aliases as well. Allowing us to resolve a module using an alias.&lt;&#x2F;p&gt;
&lt;p&gt;To create such an alias in Vite.js we need to add some configuration to our project&#x27;s &lt;code&gt;vite.config.[js|ts]&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ defineConfig } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vite&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;vue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;@vitejs&#x2F;plugin-vue&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;path&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;defineConfig&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  plugins: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;vue&lt;&#x2F;span&gt;&lt;span&gt;()],
&lt;&#x2F;span&gt;&lt;span&gt;  resolve: {
&lt;&#x2F;span&gt;&lt;span&gt;    alias: {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;@&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: path.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;join&lt;&#x2F;span&gt;&lt;span&gt;(__dirname, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;src&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By adding the above alias we can do imports starting from the &lt;code&gt;src&lt;&#x2F;code&gt; directory by using &lt;code&gt;@&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Bash: loop over arguments passed to script</title>
		<published>2022-07-06T00:00:00+00:00</published>
		<updated>2022-07-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-loop-over-arguments/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-loop-over-arguments/</id>
		<content type="html">&lt;p&gt;To loop over the arguments that are passed to the bash script we can make use of the special parameters &lt;code&gt;$@&lt;&#x2F;code&gt;. This special parameter expands to the passed positional arguments starting from position one. Thus excluding the script name at position zero.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; argument &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$@&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$argument&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;done
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The special parameter &lt;code&gt;$*&lt;&#x2F;code&gt; could be an alternative to &lt;code&gt;$@&lt;&#x2F;code&gt;. However, &lt;code&gt;$@&lt;&#x2F;code&gt; handles arguments containing spaces correctly.&lt;&#x2F;p&gt;
&lt;p&gt;When no arguments are passed &lt;code&gt;$@&lt;&#x2F;code&gt; expands to nothing, leaving you with an empty string.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to use environment variables in Vite</title>
		<published>2022-07-05T00:00:00+00:00</published>
		<updated>2022-07-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vitejs-how-to-use-environment-variables/" type="text/html"/>
		<id>https://koenwoortman.com/vitejs-how-to-use-environment-variables/</id>
		<content type="html">&lt;p&gt;The Vite.js frontend build tool makes use of &lt;code&gt;.env&lt;&#x2F;code&gt; files at the root of your project in which you may define your environment variables.&lt;&#x2F;p&gt;
&lt;p&gt;Once you make a production build, Vite statically replaces the references to your environment variables by their values.&lt;&#x2F;p&gt;
&lt;p&gt;Compared to other build systems there are two points you need to take into account when referencing your environment variables.&lt;&#x2F;p&gt;
&lt;p&gt;First, you can access your environment via &lt;code&gt;import.meta.env&lt;&#x2F;code&gt;. This returns an object containing the available environment variables known to Vite. In other build systems or JavaScript code bases you may be used to access your env variables via &lt;code&gt;process.env&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using &lt;code&gt;process.env&lt;&#x2F;code&gt; in Vite may yield the error: &amp;quot;&lt;em&gt;ReferenceError:  process is not defined&lt;&#x2F;em&gt;&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;Secondly, the user defined environment variables are prefixed with &lt;code&gt;VITE_&lt;&#x2F;code&gt;. Or another string if you overwrite the default &lt;code&gt;envPrefix&lt;&#x2F;code&gt; &lt;a href=&quot;https:&#x2F;&#x2F;vitejs.dev&#x2F;config&#x2F;#envprefix&quot;&gt;config option&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, given you have the following &lt;code&gt;.env&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;API_BASE_URL=https:&#x2F;&#x2F;api.example.com
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can reference the defined environment variable in your code using the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;baseUrl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= import&lt;&#x2F;span&gt;&lt;span&gt;.meta.env.VITE_API_BASE_URL
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>JavaScript: Split a string on a character</title>
		<published>2022-07-04T00:00:00+00:00</published>
		<updated>2022-07-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-split-string-by-character/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-split-string-by-character/</id>
		<content type="html">&lt;p&gt;We use the &lt;code&gt;.split()&lt;&#x2F;code&gt; method of String objects to divide a string up into substrings. Where the &lt;code&gt;substring()&lt;&#x2F;code&gt; method uses an index, we can use &lt;code&gt;split()&lt;&#x2F;code&gt; to separate a string based on a specific character.&lt;&#x2F;p&gt;
&lt;p&gt;In order to split a JavaScript on a dot character we must pass it as the separator string to the &lt;code&gt;split()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;IP &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;168.192.0.1&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;segments &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;IP.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(segments);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; (4) [&amp;#39;168&amp;#39;, &amp;#39;192&amp;#39;, &amp;#39;0&amp;#39;, &amp;#39;1&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you probably noticed the &lt;code&gt;.split()&lt;&#x2F;code&gt; method did split on all the occurrences of the dot character. With a second parameter we can control the amount of elements that are returned to us.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;[subdomain, domain] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api.example.com&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;split&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(subdomain);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;#39;api&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(domain);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;#39;example&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see the left over substrings are not included in the result array.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if any arguments are passed to a bash script</title>
		<published>2022-07-03T00:00:00+00:00</published>
		<updated>2022-07-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-check-if-any-arguments-are-given/" type="text/html"/>
		<id>https://koenwoortman.com/bash-check-if-any-arguments-are-given/</id>
		<content type="html">&lt;p&gt;In some cases your bash script may rely on arguments that are passed from the command line. If sensible defaults are not an option you can exit your script when no arguments are passed.&lt;&#x2F;p&gt;
&lt;p&gt;To do so we make use of the special variable &lt;code&gt;$#&lt;&#x2F;code&gt; which contain the number of positional arguments passed to the script. If this number is equal (&lt;code&gt;-eq&lt;&#x2F;code&gt;) to zero we can exit our script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#&#x2F;bin&#x2F;env bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span&gt;$# &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-eq&lt;&#x2F;span&gt;&lt;span&gt; 0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Pass at least one argument&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$@&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Run the Vite dev server on a custom port</title>
		<published>2022-07-02T00:00:00+00:00</published>
		<updated>2022-07-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vitejs-run-dev-server-on-different-port/" type="text/html"/>
		<id>https://koenwoortman.com/vitejs-run-dev-server-on-different-port/</id>
		<content type="html">&lt;p&gt;The Vite development server defaults to the network port &lt;code&gt;3000&lt;&#x2F;code&gt;. Or it tries to find an available port if it is already taken, unless the &lt;code&gt;strictPort&lt;&#x2F;code&gt; option is set to &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Since port &lt;code&gt;3000&lt;&#x2F;code&gt; is not uncommon for a development server it might already be taken by your Ruby on Rails development server.&lt;&#x2F;p&gt;
&lt;p&gt;A first option is to pass the port as a CLI option. Assuming that &lt;code&gt;npm run dev&lt;&#x2F;code&gt; will start the development server you can use the following to start on port 8000.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npm run dev -- --port 8000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;Personally I prefer this over setting a port in the package.json file. This way I don&#x27;t dictate which port other developers in the team have to use in their development environment.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Another option would be add the according configuration to your &lt;code&gt;vite.config.[js|ts]&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; vite.config.js
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ defineConfig } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vite&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;defineConfig&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;  server: {
&lt;&#x2F;span&gt;&lt;span&gt;    port: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;8000&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of hardcoding a specific as I did above you could for example check for an environment variable &lt;code&gt;PORT&lt;&#x2F;code&gt;. Which again you would set when running the &lt;code&gt;npm run dev&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set the required PHP version with composer</title>
		<published>2022-07-01T00:00:00+00:00</published>
		<updated>2022-07-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/composer-require-php-version/" type="text/html"/>
		<id>https://koenwoortman.com/composer-require-php-version/</id>
		<content type="html">&lt;p&gt;You probably know composer as the package manager for PHP. But it very conveniently lets you set the required or minimally required PHP version for your project as well. This helps to ensure that you get the right version of other package dependencies as well, with support for your PHP version.&lt;&#x2F;p&gt;
&lt;p&gt;In order to set the PHP version in the &lt;code&gt;composer.json&lt;&#x2F;code&gt; file we add it in &lt;code&gt;require&lt;&#x2F;code&gt; as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;require&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;php&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;gt;=7.4&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix &#x27;No database selected&#x27; error in MySQL</title>
		<published>2022-06-30T00:00:00+00:00</published>
		<updated>2022-06-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/mysql-fix-no-database-selected-1046/" type="text/html"/>
		<id>https://koenwoortman.com/mysql-fix-no-database-selected-1046/</id>
		<content type="html">&lt;p&gt;When trying to run an SQL query in MySQL you may receive a &#x27;&lt;em&gt;No database selected&lt;&#x2F;em&gt;&#x27; error message.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; SELECT &lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM&lt;&#x2F;span&gt;&lt;span&gt; posts;
&lt;&#x2F;span&gt;&lt;span&gt;ERROR &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1046&lt;&#x2F;span&gt;&lt;span&gt; (3D000): No database selected
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What this error means is that you tried to run a query but didn&#x27;t specify on which database. Since MySQL servers can contain multiple databases.&lt;&#x2F;p&gt;
&lt;p&gt;A first way of getting around this error is by prefixing the table name by the database name. Thus, if my database is called &lt;code&gt;blog&lt;&#x2F;code&gt; and the table I wish to query &lt;code&gt;posts&lt;&#x2F;code&gt; I would be able to run the following query.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; SELECT &lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;blog&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;posts&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we plan to run multiple queries then it is probably more convenient to specify which database to use, using the &lt;code&gt;USE&lt;&#x2F;code&gt; statement. When we do so there is no need anymore to prefix each table name with the database name.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; USE blog;
&lt;&#x2F;span&gt;&lt;span&gt;Database changed
&lt;&#x2F;span&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; SELECT &lt;&#x2F;span&gt;&lt;span&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM&lt;&#x2F;span&gt;&lt;span&gt; posts;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are blanking on the database name in your MySQL server you can &lt;a href=&quot;&#x2F;mysql-show-all-databases&#x2F;&quot;&gt;list the databases&lt;&#x2F;a&gt; using the &lt;code&gt;SHOW&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; SHOW DATABASES;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Database           |
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| information_schema |
&lt;&#x2F;span&gt;&lt;span&gt;| blog               |
&lt;&#x2F;span&gt;&lt;span&gt;| mysql              |
&lt;&#x2F;span&gt;&lt;span&gt;| performance_schema |
&lt;&#x2F;span&gt;&lt;span&gt;| sys                |
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;--------------------+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt; rows &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in set&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;00&lt;&#x2F;span&gt;&lt;span&gt; sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Redirect to a different route in Laravel</title>
		<published>2022-06-29T00:00:00+00:00</published>
		<updated>2022-06-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-return-redirect-response/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-return-redirect-response/</id>
		<content type="html">&lt;p&gt;Redirecting to a different route is a common practice after you have submitted a form successfully. In Laravel we can achieve this by returning with the &lt;code&gt;redirect()&lt;&#x2F;code&gt; helper in our controllers.&lt;&#x2F;p&gt;
&lt;p&gt;As a first argument to the &lt;code&gt;redirect()&lt;&#x2F;code&gt; function we can pass the URI to redirect to.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;redirect(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;posts&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are not redirecting to an external site it is probably neater to use named routes instead. Which would look like the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;store&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;redirect()-&amp;gt;route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;posts.index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Render HTML safe content in Laravel Blade</title>
		<published>2022-06-28T00:00:00+00:00</published>
		<updated>2022-06-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-blade-render-html-safe/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-blade-render-html-safe/</id>
		<content type="html">&lt;p&gt;Laravel Blade automatically escapes HTML tags if you render content using the double braces (&lt;code&gt;{{ ... }}&lt;&#x2F;code&gt;). Blade uses the native PHP function &lt;code&gt;htmlspecialchars()&lt;&#x2F;code&gt; to do so.&lt;&#x2F;p&gt;
&lt;p&gt;This is a good thing, because if users enter valid HTML, Blade makes sure that this won&#x27;t be rendered as actual HTML (or JavaScript more harmfully). Instead the contents are shown as regular text, thus preventing cross site scripting.&lt;&#x2F;p&gt;
&lt;p&gt;In certain cases however, you want to be able to render HTML using blade. When you know what you are doing. In such cases we shouldn&#x27;t be using the double braces syntax, but &lt;code&gt;{!! ... !!}&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;p&gt;In your blade templates you can use the following syntax to render unescaped data.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  {!! $html !!}
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;body&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Return a 404 response in Laravel</title>
		<published>2022-06-27T00:00:00+00:00</published>
		<updated>2022-06-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-return-http-404-not-found-status-code/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-return-http-404-not-found-status-code/</id>
		<content type="html">&lt;p&gt;Laravel has multiple options for you to a 404 response. Some more generic, some pretty specific to querying for a particular model.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-abort&quot;&gt;Using abort()&lt;&#x2F;h2&gt;
&lt;p&gt;One pretty easy method is by using the &lt;code&gt;abort()&lt;&#x2F;code&gt; helper to raise an exception which will be handled by the Laravel exception handler.&lt;&#x2F;p&gt;
&lt;p&gt;All you need for this is to call the &lt;code&gt;abort()&lt;&#x2F;code&gt; method with the 404 status code as its argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;show&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$id&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  $model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;::find($id);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;($questionModel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    abort(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;manually-set-the-status-code&quot;&gt;Manually set the status code&lt;&#x2F;h2&gt;
&lt;p&gt;Another option would be to return a response with a manually set http status, so 404 in our case.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;show&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$id&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  $model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;::find($id);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;($questionModel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;response([], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This approach works also well if we wish to return a json response with status code. In such a case we can chain the &lt;code&gt;json()&lt;&#x2F;code&gt; method which sets the correct &lt;code&gt;Content-Type&lt;&#x2F;code&gt; header for us as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;show&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$id&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  $model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;::find($id);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;($questionModel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;response()-&amp;gt;json([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;error&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;data&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;[]], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;find-or-fail-with-404&quot;&gt;Find or fail with 404&lt;&#x2F;h2&gt;
&lt;p&gt;If you are specifically checking whether a model exists, and return a 404 based on this, you can more conveniently reach for the &lt;code&gt;findOrFail()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;p&gt;If the model doesn&#x27;t exist in our queryset this throws a &lt;code&gt;ModelNotFoundException&lt;&#x2F;code&gt; which will automatically cause a 404 HTTP response to be returned.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;show&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Request &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$request&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$id&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  $model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;::findOrFail($id);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Delete all Laravel models in a table</title>
		<published>2022-06-26T00:00:00+00:00</published>
		<updated>2022-06-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-remove-all-models-in-table/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-remove-all-models-in-table/</id>
		<content type="html">&lt;p&gt;I&#x27;m assuming you know that removing all the Laravel models from a table is a dangerous operation. You may not only delete the models you expect, but also other models with a foreign key with cascading delete.&lt;&#x2F;p&gt;
&lt;p&gt;That being said. My first assumption was to run &lt;code&gt;Model::all()-&amp;gt;delete();&lt;&#x2F;code&gt;, however this spawned an exception with message: &lt;em&gt;&#x27;Method Illuminate\Database\Eloquent\Collection::delete does not exist.&#x27;&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It turns out that &lt;code&gt;all()&lt;&#x2F;code&gt; already runs the SQL query and returns a collection. Instead you can use the &lt;code&gt;truncate()&lt;&#x2F;code&gt; method to remove all the models in a table.&lt;&#x2F;p&gt;
&lt;p&gt;You can either call this method directly on the model:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Model&lt;&#x2F;span&gt;&lt;span&gt;::truncate();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or via the &lt;code&gt;DB&lt;&#x2F;code&gt; facade:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;DB&lt;&#x2F;span&gt;&lt;span&gt;::table(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;models&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;truncate();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Removing packages with pipenv</title>
		<published>2022-06-25T00:00:00+00:00</published>
		<updated>2022-06-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-pipenv-remove-package/" type="text/html"/>
		<id>https://koenwoortman.com/python-pipenv-remove-package/</id>
		<content type="html">&lt;p&gt;If you are already familiar with uninstalling packages with pip, then removing packages from your virtual environment with pipenv should look familiar.&lt;&#x2F;p&gt;
&lt;p&gt;To remove a package from our virtualenv with &lt;code&gt;pipenv&lt;&#x2F;code&gt; we run the &lt;code&gt;uninstall&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ pipenv uninstall http
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We can uninstall multiple packages at once with pipenv in the same way, by listing out the other packages to remove:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ pipenv uninstall http django flask
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create custom error classes in JavaScript</title>
		<published>2022-06-24T00:00:00+00:00</published>
		<updated>2022-06-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-custom-error-classes/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-custom-error-classes/</id>
		<content type="html">&lt;p&gt;Creating your own exception hierarchy for your JavaScript can be convenient in error monitoring and catching exceptions.&lt;&#x2F;p&gt;
&lt;p&gt;To set up your own error classes in JavaScript we need to extend the base &lt;code&gt;Error&lt;&#x2F;code&gt; type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span&gt;ApiRequestError &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Error &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;constructor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    super(message);
&lt;&#x2F;span&gt;&lt;span&gt;    this.name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;ApiRequestError&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, in our JavaScript code we can throw errors of our newly created error type.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;throw new &lt;&#x2F;span&gt;&lt;span&gt;ApiRequestError(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Failed to fetch resource.&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Update installed packages on Ubuntu</title>
		<published>2022-06-23T00:00:00+00:00</published>
		<updated>2022-06-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-server-update-packages/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-server-update-packages/</id>
		<content type="html">&lt;p&gt;When you run an Ubuntu server it is good practice to regularly update the installed packages. To ensure you pull in the latest security patches. Ubuntu allows for some automatic package upgrades with the &lt;code&gt;unattended-upgrades&lt;&#x2F;code&gt; package. But knowing to upgrade packages manually cannot hurt.&lt;&#x2F;p&gt;
&lt;p&gt;Upgrading packages on Ubuntu is done using the &lt;code&gt;apt&lt;&#x2F;code&gt; package manager. And you most likely need &lt;code&gt;sudo&lt;&#x2F;code&gt; permissions to run the following commands.&lt;&#x2F;p&gt;
&lt;p&gt;It is probably best to first refresh the Ubuntu package index. We do this using the &lt;code&gt;apt update&lt;&#x2F;code&gt; command. Next we do the actual upgrading of our packages using &lt;code&gt;apt upgrade&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt update
&lt;&#x2F;span&gt;&lt;span&gt;$ apt upgrade
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In some exceptional cases a server reboot is required before the upgrades take effect. In the case of a Linux kernel upgrade for example.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ reboot
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get a random item from an array in PHP</title>
		<published>2022-06-22T00:00:00+00:00</published>
		<updated>2022-06-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-get-random-element-from-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-get-random-element-from-array/</id>
		<content type="html">&lt;p&gt;When picking a random item from an array in PHP there is no need to generate a random index (or key) yourself. PHP wraps this functionality in the built-in &lt;code&gt;array_rand()&lt;&#x2F;code&gt; function, which uses a pseudo random number generator.&lt;&#x2F;p&gt;
&lt;p&gt;This &lt;code&gt;array_rand()&lt;&#x2F;code&gt; function finds a random key in an array, it doesn&#x27;t return a random element. Therefore we need to retrieve the random element from the array using the key that was returned by &lt;code&gt;array_rand()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$dice &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$throw &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_rand&lt;&#x2F;span&gt;&lt;span&gt;($dice);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$dice[$throw];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if PHP array is empty using count()</title>
		<published>2022-06-21T00:00:00+00:00</published>
		<updated>2022-06-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-array-is-empty/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-array-is-empty/</id>
		<content type="html">&lt;p&gt;Checking whether an array is empty comes down to checking whether there are any elements in the array. Therefore a logical approach is to count the elements in the array and check if this number is equal to zero.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$arr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[];
&lt;&#x2F;span&gt;&lt;span&gt;$isEmpty &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;count&lt;&#x2F;span&gt;&lt;span&gt;($arr) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But, since empty arrays in PHP are considered &#x27;falsy&#x27; we can just check whether it evaluates to &lt;code&gt;true&lt;&#x2F;code&gt; or &lt;code&gt;false&lt;&#x2F;code&gt; in a boolean context.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$arr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span&gt;$arr)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Empty&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: Check if a variable exists</title>
		<published>2022-06-20T00:00:00+00:00</published>
		<updated>2022-06-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-variable-exists/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-variable-exists/</id>
		<content type="html">&lt;p&gt;Referencing a variable that hasn&#x27;t been set in PHP fails rather silently. You might see a PHP warning like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;PHP Warning:  Undefined variable $var
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To check whether a variable has been set in PHP we use the &lt;code&gt;isset()&lt;&#x2F;code&gt; function, which returns &lt;code&gt;false&lt;&#x2F;code&gt; if the passed variable has not been set.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;isset&lt;&#x2F;span&gt;&lt;span&gt;($var) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Variable has not been set&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Enums in TypeScript</title>
		<published>2022-06-19T00:00:00+00:00</published>
		<updated>2022-06-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/typescript-enums/" type="text/html"/>
		<id>https://koenwoortman.com/typescript-enums/</id>
		<content type="html">&lt;p&gt;Enums, short for enumerated types, are data structures containing a collection of named constants. For these named constants you don&#x27;t need to assign an underlying value yourself, this is handled by TypeScript.&lt;&#x2F;p&gt;
&lt;p&gt;To define an enum we start with the &lt;code&gt;enum&lt;&#x2F;code&gt; keyword followed by the name we wish to use for the enum. Between braces we define the types that make up the enum.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;enum &lt;&#x2F;span&gt;&lt;span&gt;State {
&lt;&#x2F;span&gt;&lt;span&gt;  Loading,
&lt;&#x2F;span&gt;&lt;span&gt;  Error,
&lt;&#x2F;span&gt;&lt;span&gt;  Success,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The advantage of using enums in our code is that we don&#x27;t have to come up with arbitrary values for constants by ourselves. TypeScript abstracts this away, so that we can use just a readable enum.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span&gt;application.state &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;State.Loading;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(application.state &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span&gt;State.Loading) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;showSpinner&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What create mode 100644 means in git</title>
		<published>2022-06-18T00:00:00+00:00</published>
		<updated>2022-06-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-what-is-create-mode-100644/" type="text/html"/>
		<id>https://koenwoortman.com/git-what-is-create-mode-100644/</id>
		<content type="html">&lt;p&gt;Ever noticed something like the following in your terminal after making a git commit?&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git commit
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;  create mode 100644 src&#x2F;a-file.md
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So there are three parts to the line above:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;create mode&lt;&#x2F;code&gt; says that a new file was create in your git repository, you may encounter &lt;code&gt;delete mode&lt;&#x2F;code&gt; here as well.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;100644&lt;&#x2F;code&gt; is a bit cryptic way of showing the file permissions, the number 100644 means that this is a regular file.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;src&#x2F;a-file.md&lt;&#x2F;code&gt; this one is rather simple, it is the file path of the newly created file in the git repository.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;So what is this number 100644? Files can have different &#x27;modes&#x27;, meaning that files come as different types on your system.&lt;&#x2F;p&gt;
&lt;p&gt;Some files can be executable as a script, others are symlinks and others are regular text files. Each of these types are represented with a number as 100644. You will see the number 120000 for symbolic links for example and 040000 for directories.&lt;&#x2F;p&gt;
&lt;p&gt;On Unix systems we use these permissions to give users and user groups different levels of access to certain files and directories. These types of permissions are not relevant in git, therefore git uses only a subset of the regular file permissions.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the length of an array in PHP</title>
		<published>2022-06-17T00:00:00+00:00</published>
		<updated>2022-06-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-array-length/" type="text/html"/>
		<id>https://koenwoortman.com/php-array-length/</id>
		<content type="html">&lt;p&gt;To find the amount of elements in an array we use the PHP array function &lt;code&gt;count()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In its most basic form we can call the &lt;code&gt;count()&lt;&#x2F;code&gt; function with an array as its first argument and it returns the number of elements in the array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo count&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; 3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For associative arrays we can use the same count function as well to get the amount of values it contains.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo count&lt;&#x2F;span&gt;&lt;span&gt;([
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Where it gets interesting is the recursive counting option that is provided. This allows to not just count the plain values, but also the contents of nested arrays.&lt;&#x2F;p&gt;
&lt;p&gt;The value returned might seem unintuitive, since both the array and its contents are included in the count.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo count&lt;&#x2F;span&gt;&lt;span&gt;([
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;e&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;f&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;COUNT_RECURSIVE&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; 5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>HTTP Redirects with plain PHP</title>
		<published>2022-06-16T00:00:00+00:00</published>
		<updated>2022-06-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-http-redirect/" type="text/html"/>
		<id>https://koenwoortman.com/php-http-redirect/</id>
		<content type="html">&lt;p&gt;With PHP we can redirect the user to a different website or URL by means of the &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Headers&#x2F;Location&quot;&gt;Location&lt;&#x2F;a&gt; HTTP header. This response header gives an indication to the browser where to redirect the user to.&lt;&#x2F;p&gt;
&lt;p&gt;In order to send back HTTP headers in PHP we need the built-in &lt;code&gt;header()&lt;&#x2F;code&gt; network function of PHP. This function allows you to send back a raw HTTP header.&lt;&#x2F;p&gt;
&lt;p&gt;As mentioned above we set the HTTP headers raw, so we don&#x27;t separate the header name and value as a key-value pair or so. Instead we set both the name and value in a single string.&lt;&#x2F;p&gt;
&lt;p&gt;This looks like the following in a PHP script:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Location: https:&#x2F;&#x2F;github.com&#x2F;koenwoortman&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do make sure that you don&#x27;t write any output to the browser before setting the header, otherwise the script will fail.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;redirect-with-response-code&quot;&gt;Redirect with response code&lt;&#x2F;h2&gt;
&lt;p&gt;PHP is smart enough to change the HTTP response status code for you when you set the &lt;code&gt;Location&lt;&#x2F;code&gt; header. Instead of a regular 200 response the code &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTTP&#x2F;Status&#x2F;302&quot;&gt;302&lt;&#x2F;a&gt; is returned to indicate a redirect.&lt;&#x2F;p&gt;
&lt;p&gt;However, we set a different response code if we find another more suitable. For example:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;301&lt;&#x2F;code&gt; to indicate that the location of the requested resource has been changed permanently.&lt;&#x2F;li&gt;
&lt;li&gt;Or &lt;code&gt;307&lt;&#x2F;code&gt; to indicate a temporarily moved resource.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;We can set the response code as the third argument to the &lt;code&gt;header()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; HTTP 301 - Moved Permanently
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Location: https:&#x2F;&#x2F;github.com&#x2F;koenwoortman&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;301&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The response code is the third argument to the &lt;code&gt;header()&lt;&#x2F;code&gt; function, we can pass the response code as an integer. The second argument in the &lt;code&gt;replace&lt;&#x2F;code&gt; option which defaults to &lt;code&gt;true&lt;&#x2F;code&gt; when not set manually. It indicates whether the header should be replaced if it has been set already.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;redirect-http-to-https&quot;&gt;Redirect HTTP to HTTPS&lt;&#x2F;h2&gt;
&lt;p&gt;Probably a more suitable place to redirect HTTP traffic to HTTPS is in the webserver configuration. But, if necessary, a redirect in PHP does the job too.&lt;&#x2F;p&gt;
&lt;p&gt;First, make sure you only redirect to HTTPS if you are on actually on HTTP. To prevent your script from getting in an infinite redirect loop (ERR_TOO_MANY_REDIRECTS).&lt;&#x2F;p&gt;
&lt;p&gt;Second, we can get the current host with URI from the &lt;code&gt;$_SERVER&lt;&#x2F;code&gt; automatic global variable. This we can use to redirect to HTTPS.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;isset&lt;&#x2F;span&gt;&lt;span&gt;($_SERVER[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;HTTPS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]))
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    $currentUrl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;{$_SERVER[&amp;#39;HTTP_HOST&amp;#39;]}{$_SERVER[&amp;#39;REQUEST_URI&amp;#39;]}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Location: https:&#x2F;&#x2F;{$currentUrl}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;redirect-to-relative-uri&quot;&gt;Redirect to relative URI&lt;&#x2F;h2&gt;
&lt;p&gt;Besides redirects to absolute URIs it is perfectly legal to redirect to relative URIs as well. This allows you to redirect traffic to a different page on the same website.&lt;&#x2F;p&gt;
&lt;p&gt;In this case we only need to pass the path as the header value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Location: &#x2F;home&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if PHP script is served over HTTP or HTTPS</title>
		<published>2022-06-15T00:00:00+00:00</published>
		<updated>2022-06-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-http-or-https/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-http-or-https/</id>
		<content type="html">&lt;p&gt;We can find out whether a request is made to our PHP script over HTTP or HTTPS by checking whether &lt;code&gt;$_SERVER[&#x27;HTTPS&#x27;]&lt;&#x2F;code&gt; is set. This value in the &lt;code&gt;$_SERVER&lt;&#x2F;code&gt; superglobal is set to a non-empty value if the script runs on HTTPS.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore we can check whether this value has been set or not, based on this we can determine if the script is server over HTTP or HTTPS.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$protocol &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;isset&lt;&#x2F;span&gt;&lt;span&gt;($_SERVER[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;HTTPS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]) ? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;https&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;http&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$protocol;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; &amp;#39;http&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix `zsh: permission denied: ..` going up one directory</title>
		<published>2022-06-14T00:00:00+00:00</published>
		<updated>2022-06-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-permission-denied-dot-dot/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-permission-denied-dot-dot/</id>
		<content type="html">&lt;p&gt;If you try to move up one directory in your Zsh shell using the &lt;code&gt;..&lt;&#x2F;code&gt; but see the stated permission denied error there is a good chance you are used to an Oh-My-Zsh alias.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;..&lt;&#x2F;code&gt; command is not an existing Zsh shell command, thus you can expect the following to happen in a plain Zsh shell.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ ..
&lt;&#x2F;span&gt;&lt;span&gt;zsh: permission denied: ..
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But luckily, if Zsh can do this then so can we. We can make this work again by adding a Zsh shell alias to our &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; file. In which we create an alias for &lt;code&gt;cd ..&lt;&#x2F;code&gt; as &lt;code&gt;..&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;alias &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;..&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cd ..&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do not forget to source your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; file after making the change, or to restart your shell.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Alternative to the ternary operator in Rust</title>
		<published>2022-06-13T00:00:00+00:00</published>
		<updated>2022-06-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-ternary-operator/" type="text/html"/>
		<id>https://koenwoortman.com/rust-ternary-operator/</id>
		<content type="html">&lt;p&gt;In contrast to many C-like languages Rust doesn&#x27;t support the ternary &lt;code&gt;?:&lt;&#x2F;code&gt; syntax. For the time being at least. Instead, if-else statements can be used as expressions, which does make ternary operations in Rust fairly easy to read.&lt;&#x2F;p&gt;
&lt;p&gt;Since if-else statements can be used as an expression in Rust we can directly use them in return statements and assignment operations.&lt;&#x2F;p&gt;
&lt;p&gt;See for example an if-else expression directly in a &lt;code&gt;return&lt;&#x2F;code&gt; statements.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return if&lt;&#x2F;span&gt;&lt;span&gt; x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;{ success } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{ failure };
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or you may use an if-else expression in an assignment operations.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= if&lt;&#x2F;span&gt;&lt;span&gt; x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true &lt;&#x2F;span&gt;&lt;span&gt;} &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else &lt;&#x2F;span&gt;&lt;span&gt;{ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false &lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>MySQL: Show the tables in your database</title>
		<published>2022-06-12T00:00:00+00:00</published>
		<updated>2022-06-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/mysql-show-tables-in-database/" type="text/html"/>
		<id>https://koenwoortman.com/mysql-show-tables-in-database/</id>
		<content type="html">&lt;p&gt;Inspecting the structure of your database is probably something that is more convenient with a GUI application. However, sometimes you need to do without. In that case you can jump into the MySQL shell to list the tables of your database. I&#x27;m a happy user of the &lt;a href=&quot;https:&#x2F;&#x2F;www.mycli.net&#x2F;&quot;&gt;MyCLI&lt;&#x2F;a&gt; shell MySelf.&lt;&#x2F;p&gt;
&lt;p&gt;To list the tables of the tables in your MySQL database you can run the following snippet of SQL.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; SHOW TABLES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM&lt;&#x2F;span&gt;&lt;span&gt; [database_name];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above requires you to specify the database name. However, if you already changed the database to the preferred one this last part is optional. You change or select a database by running &lt;code&gt;USE &amp;lt;db&amp;gt;&lt;&#x2F;code&gt; first. Afterwards just a &lt;code&gt;SHOW TABLES&lt;&#x2F;code&gt; is enough.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; USE [database_name];
&lt;&#x2F;span&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; SHOW TABLES;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running this against an actual MySQL database should give an output similar to the following.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW TABLES FROM blog_dev;
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Tables_in_blog_dev |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| comments           |
&lt;&#x2F;span&gt;&lt;span&gt;| posts              |
&lt;&#x2F;span&gt;&lt;span&gt;| topics             |
&lt;&#x2F;span&gt;&lt;span&gt;| users              |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;4 rows in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Make Laravel model with migration, controller and factory</title>
		<published>2022-06-11T00:00:00+00:00</published>
		<updated>2022-06-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-make-model-with-migration-controller-factory/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-make-model-with-migration-controller-factory/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;artisan make:{...}&lt;&#x2F;code&gt; commands provide you with some sweet scaffolding. You can quickly create a model, controller, test, command, etc. with a single command. What makes this even more convenient is that you can create classes related to the thing you make all at once.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, creating a migration, controller and factory for a model you wish to generate can be done with just a single command. To do so we need to pass the options &lt;code&gt;-mcf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan make:model Post -mcf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;These short options stand for:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-m&lt;&#x2F;code&gt; A migrations for the new model.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-c&lt;&#x2F;code&gt; A controller for the new model.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-f&lt;&#x2F;code&gt; A factory for the new model.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Instead of the short options you can use the long options instead for a bit more verbosity.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan make:model Post --migration --controller --factory
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All the options are detailed in the command help, which you can see by passing the &lt;code&gt;--help&lt;&#x2F;code&gt; flag.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan make:model --help
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This shows an &lt;code&gt;--all&lt;&#x2F;code&gt; option as well, which aggregates the creation of a migration, seeder, factory, policy and resource controller for the new model.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan make:model Post --all
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP Spaceship Operator (&lt;=&gt;)</title>
		<published>2022-06-10T00:00:00+00:00</published>
		<updated>2022-06-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-spaceship-operator/" type="text/html"/>
		<id>https://koenwoortman.com/php-spaceship-operator/</id>
		<content type="html">&lt;p&gt;One of the new features introduced in PHP 7 was the spaceship operator (&lt;code&gt;&amp;lt;=&amp;gt;&lt;&#x2F;code&gt;). Quite new to PHP but the spaceship operator has been present in other programming languages like &lt;a href=&quot;&#x2F;ruby-spaceship-operator&#x2F;&quot;&gt;Ruby&lt;&#x2F;a&gt; and Perl for a while.&lt;&#x2F;p&gt;
&lt;p&gt;The spaceship operator evaluates to either &lt;code&gt;-1&lt;&#x2F;code&gt;, &lt;code&gt;0&lt;&#x2F;code&gt; or &lt;code&gt;1&lt;&#x2F;code&gt;. Let&#x27;s consider two operarands &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; being compared by the spaceship operator: &lt;code&gt;a &amp;lt;=&amp;gt; b&lt;&#x2F;code&gt;. This may give the following results:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;0&lt;&#x2F;code&gt; if &lt;code&gt;a&lt;&#x2F;code&gt; and &lt;code&gt;b&lt;&#x2F;code&gt; are equal, thus if &lt;code&gt;a == b&lt;&#x2F;code&gt; is &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;1&lt;&#x2F;code&gt; if &lt;code&gt;a&lt;&#x2F;code&gt; is greater than &lt;code&gt;b&lt;&#x2F;code&gt;, thus if &lt;code&gt;a &amp;gt; b&lt;&#x2F;code&gt; is &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;-1&lt;&#x2F;code&gt; if &lt;code&gt;a&lt;&#x2F;code&gt; is lower than &lt;code&gt;b&lt;&#x2F;code&gt;, thus if &lt;code&gt;a &amp;lt; b&lt;&#x2F;code&gt; is &lt;code&gt;true&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Pay attention to the fact that the spaceship operator uses loose comparisons. Thus, &lt;code&gt;1 &amp;lt;=&amp;gt; &amp;quot;1&amp;quot;&lt;&#x2F;code&gt; will result in &lt;code&gt;0&lt;&#x2F;code&gt; as well.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove leading whitespaces from a JavaScript string</title>
		<published>2022-06-09T00:00:00+00:00</published>
		<updated>2022-06-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-leading-whitespaces/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-leading-whitespaces/</id>
		<content type="html">&lt;p&gt;In JavaScript you often deal with input given by users, think of form data for example. In such cases you may wish to trim leading whitespaces of the given inputs.&lt;&#x2F;p&gt;
&lt;p&gt;To specifically remove only the leading whitespaces, whitespaces at the start of the string, you can use the &lt;code&gt;.trimStart()&lt;&#x2F;code&gt; string method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;  monday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;input &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;input.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;trimStart&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(input)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;#39;monday&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;.trimStart()&lt;&#x2F;code&gt; keeps the original String object intact and returns a new String object with the leading spaces stripped off.&lt;&#x2F;p&gt;
&lt;p&gt;If you prefer to also remove the trailing whitespaces at the end of the string, you can just use the &lt;code&gt;.trim()&lt;&#x2F;code&gt; method instead. This takes care of both the spaces at the beginning and end of the string.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Disable beep&#x2F;bell in tmux</title>
		<published>2022-06-08T00:00:00+00:00</published>
		<updated>2022-06-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-disable-bell/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-disable-bell/</id>
		<content type="html">&lt;p&gt;The visual cue and audio bell are not my favorite features in tmux. Especially the audio bell while I&#x27;m listening to music. Luckily they can be disabled in the tmux config.&lt;&#x2F;p&gt;
&lt;p&gt;In order to disable the audio and visual &#x27;bell&#x27; you add the following configuration to your &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;set-option -g bell-action none
&lt;&#x2F;span&gt;&lt;span&gt;set-option -g visual-bell off
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reset local git branch to match the remote branch</title>
		<published>2022-06-07T00:00:00+00:00</published>
		<updated>2022-06-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-reset-to-remote/" type="text/html"/>
		<id>https://koenwoortman.com/git-reset-to-remote/</id>
		<content type="html">&lt;p&gt;Resetting the state of one of your local branches to a remote branch can be done with just two commands. Some caution in advance, this does contain a hard reset in which you can possibly lose your local changes. If you have changes that shouldn&#x27;t get lost you can stash them or keep them in another branch.&lt;&#x2F;p&gt;
&lt;p&gt;The first step is to make sure we are up-to-date with the refs of the remote. We do this by doing a &lt;code&gt;git fetch&lt;&#x2F;code&gt;. Second, we do a hard reset of our current branch to the state of the remote. Assuming our remote is called &lt;code&gt;origin&lt;&#x2F;code&gt; and we wish to reset to a branch called &lt;code&gt;main&lt;&#x2F;code&gt; this looks like the following.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git fetch origin
&lt;&#x2F;span&gt;&lt;span&gt;$ git reset --hard origin&#x2F;main
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Adding remotes to your git repository</title>
		<published>2022-06-06T00:00:00+00:00</published>
		<updated>2022-06-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-add-remote/" type="text/html"/>
		<id>https://koenwoortman.com/git-add-remote/</id>
		<content type="html">&lt;p&gt;When we clone a git repository, from GitHub for example, the &lt;code&gt;origin&lt;&#x2F;code&gt; remote is set up automatically for us.&lt;&#x2F;p&gt;
&lt;p&gt;But if you wish to add a second remote to your git repository, or when you created a local repository instead of cloning we can add a remote manually.&lt;&#x2F;p&gt;
&lt;p&gt;For this we need the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote add &amp;lt;remote name&amp;gt; &amp;lt;remote url&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s for example add a second remote called &lt;code&gt;upstream&lt;&#x2F;code&gt; to a forked repository:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote add upstream git@github.com:getzola&#x2F;zola.git
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To validate whether the remote was added correctly we can review the output of the &lt;code&gt;git remote -v&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote -v
&lt;&#x2F;span&gt;&lt;span&gt;origin  git@github.com:koenwoortman&#x2F;zola.git (fetch)
&lt;&#x2F;span&gt;&lt;span&gt;origin  git@github.com:koenwoortman&#x2F;zola.git (push)
&lt;&#x2F;span&gt;&lt;span&gt;upstream        git@github.com:getzola&#x2F;zola.git (fetch)
&lt;&#x2F;span&gt;&lt;span&gt;upstream        git@github.com:getzola&#x2F;zola.git (push)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Pause your PHP script for 1 second with sleep()</title>
		<published>2022-06-05T00:00:00+00:00</published>
		<updated>2022-06-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-sleep-for-1-second/" type="text/html"/>
		<id>https://koenwoortman.com/php-sleep-for-1-second/</id>
		<content type="html">&lt;p&gt;To pause the execution of your PHP for one second you may use the &lt;code&gt;sleep()&lt;&#x2F;code&gt; function. The &lt;code&gt;sleep()&lt;&#x2F;code&gt; function requires the amount of seconds as its parameter:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  fetchBitcoinPrice();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; use `1` to pause one second.
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;sleep()&lt;&#x2F;code&gt; function only allows for integers as its arguments; thus only full seconds to pause execution. If you need to pause for less then a second or for example 1.5 seconds you should reach for the &lt;code&gt;usleep()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;sleep-vs-usleep&quot;&gt;sleep() vs. usleep()&lt;&#x2F;h2&gt;
&lt;p&gt;Besides the &lt;code&gt;sleep()&lt;&#x2F;code&gt; function PHP provides another function doing something similar; &lt;code&gt;usleep()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of seconds &lt;code&gt;usleep()&lt;&#x2F;code&gt; expects the argument you pass to be in microseconds. Thus to pause for one second that would look like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  fetchBitcoinPrice();
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;usleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1000000&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; use `1000000` to pause one second.
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP null coalescing operator (??)</title>
		<published>2022-06-04T00:00:00+00:00</published>
		<updated>2022-06-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-null-coalescing-operator/" type="text/html"/>
		<id>https://koenwoortman.com/php-null-coalescing-operator/</id>
		<content type="html">&lt;p&gt;The null coalescing operator was introduced as one of the new features in &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;migration70.new-features.php&quot;&gt;PHP 7.0&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;It provides you some syntactic sugar for setting fallback values in cases where a value is &lt;code&gt;NULL&lt;&#x2F;code&gt;. Before it was common to handle such cases with a ternary (&lt;code&gt;... ? … : …&lt;&#x2F;code&gt;) in combination with &lt;code&gt;isset()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$quantity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;isset&lt;&#x2F;span&gt;&lt;span&gt;($_POST[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;quantity&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]) ? $_POST[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;quantity&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;] : &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With the null coalescing operator we can write the above as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$quantity &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$_POST[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;quantity&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;] ?? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the first operand (value before the &lt;code&gt;??&lt;&#x2F;code&gt;) exists and its value does not equal &lt;code&gt;NULL&lt;&#x2F;code&gt; then &lt;code&gt;$quantity&lt;&#x2F;code&gt; is set to this value. Otherwise &lt;code&gt;$quantity&lt;&#x2F;code&gt; is set to the second operand (value after the &lt;code&gt;??&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: &#x27;never&#x27; return type</title>
		<published>2022-06-03T00:00:00+00:00</published>
		<updated>2022-06-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-never-return-type/" type="text/html"/>
		<id>https://koenwoortman.com/php-never-return-type/</id>
		<content type="html">&lt;p&gt;With PHP 8.1 the &lt;code&gt;never&lt;&#x2F;code&gt; return type was introduced. At a first glance the existence of this kind of return type seems somewhat what. What is the use of functions that never return something?&lt;&#x2F;p&gt;
&lt;p&gt;You use the &lt;code&gt;never&lt;&#x2F;code&gt; type to indicate that your function either:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;never terminates, think of an indented infinite loop for example.&lt;&#x2F;li&gt;
&lt;li&gt;terminates with an exception.&lt;&#x2F;li&gt;
&lt;li&gt;quits the program using the &lt;code&gt;exit()&lt;&#x2F;code&gt; function.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The first option is shortly demonstrated in the snippet below. Where the program never terminates on its own.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(): &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;never &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;sleep&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Fetching data...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For a reference in the documentation you may follow &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;language.types.declarations.php#language.types.declarations.never&quot;&gt;this link&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use `init` to create a Cargo package in the current folder</title>
		<published>2022-06-02T00:00:00+00:00</published>
		<updated>2022-06-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-cargo-new-project-in-current-directory/" type="text/html"/>
		<id>https://koenwoortman.com/rust-cargo-new-project-in-current-directory/</id>
		<content type="html">&lt;p&gt;To set up a new Cargo package you probably reach &lt;code&gt;cargo new&lt;&#x2F;code&gt; first. However, this operation requires you to supply a path.&lt;&#x2F;p&gt;
&lt;p&gt;If you need to create a Cargo package in an already existing directory you should be using &lt;code&gt;cargo init&lt;&#x2F;code&gt; instead. Which can set up a package in the current directory.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cd folder
&lt;&#x2F;span&gt;&lt;span&gt;$ cargo init
&lt;&#x2F;span&gt;&lt;span&gt;  Created binary (application) package
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Version release channels in Rust</title>
		<published>2022-06-01T00:00:00+00:00</published>
		<updated>2022-06-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-release-channels/" type="text/html"/>
		<id>https://koenwoortman.com/rust-release-channels/</id>
		<content type="html">&lt;p&gt;Via &lt;code&gt;rustup&lt;&#x2F;code&gt; you may install Rust from different release channels. Rust releases follow a release train schedule. Meaning that all developments are done on the &lt;code&gt;master&lt;&#x2F;code&gt;branch and versions are released periodically on a tight schedule, instead of based on a certain feature set that makes up a version.&lt;&#x2F;p&gt;
&lt;p&gt;Rust uses three different release channels:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;nightly&lt;&#x2F;code&gt;: an automated daily release.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;beta&lt;&#x2F;code&gt;: every six weeks the changes from the &lt;code&gt;master&lt;&#x2F;code&gt; branch are taken and released to the &lt;code&gt;beta&lt;&#x2F;code&gt; channel.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;stable&lt;&#x2F;code&gt;: another six weeks after the &lt;code&gt;beta&lt;&#x2F;code&gt; release was created those changes are merged to the &lt;code&gt;stable&lt;&#x2F;code&gt; channel.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Update your local Rust version with rustup</title>
		<published>2022-05-31T00:00:00+00:00</published>
		<updated>2022-05-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-update-version/" type="text/html"/>
		<id>https://koenwoortman.com/rust-update-version/</id>
		<content type="html">&lt;p&gt;The way built-in to &lt;code&gt;rustup&lt;&#x2F;code&gt; to update all your local Rust channels is by using the &lt;code&gt;rustup update&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rustup update
&lt;&#x2F;span&gt;&lt;span&gt;info: syncing channel updates for &amp;#39;stable-x86_64-unknown-linux-gnu&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;info: syncing channel updates for &amp;#39;1.58-x86_64-unknown-linux-gnu&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;info: checking for self-updates
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  stable-x86_64-unknown-linux-gnu unchanged - rustc 1.61.0 (fe5b13d68 2022-05-18)
&lt;&#x2F;span&gt;&lt;span&gt;    1.58-x86_64-unknown-linux-gnu unchanged - rustc 1.58.1 (db9d1b20b 2022-01-20)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;info: cleaning up downloads &amp;amp; tmp directories
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This command searches and installs updates for all Rust channels present on your machine.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Downgrade to specific Rust version with rustup</title>
		<published>2022-05-30T00:00:00+00:00</published>
		<updated>2022-05-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-install-specific-version/" type="text/html"/>
		<id>https://koenwoortman.com/rust-install-specific-version/</id>
		<content type="html">&lt;p&gt;If you manage your Rust environment with &lt;code&gt;rustup&lt;&#x2F;code&gt; then downgrading to a specific version can be done with a single command, the same goes for upgrading to a specific version for that matter.&lt;&#x2F;p&gt;
&lt;p&gt;We can pass the desired version as an argument to the &lt;code&gt;rustup install&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rustup install 1.58
&lt;&#x2F;span&gt;&lt;span&gt;info: syncing channel updates for &amp;#39;1.58-x86_64-unknown-linux-gnu&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;info: latest update on 2022-01-20, rust version 1.58.1 (db9d1b20b 2022-01-20
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When no version number is specified to the &lt;code&gt;install&lt;&#x2F;code&gt; subcommand &lt;code&gt;rustup&lt;&#x2F;code&gt; defaults to the latest available version.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Rust: Get the current compiler version</title>
		<published>2022-05-29T00:00:00+00:00</published>
		<updated>2022-05-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-get-current-compiler-version/" type="text/html"/>
		<id>https://koenwoortman.com/rust-get-current-compiler-version/</id>
		<content type="html">&lt;p&gt;The version of the Rust compiler can be found by making use of the &lt;code&gt;rustc&lt;&#x2F;code&gt; command in your terminal. You need to pass either the &lt;code&gt;--version&lt;&#x2F;code&gt; long option or the &lt;code&gt;-V&lt;&#x2F;code&gt; short option (capital &amp;quot;v&amp;quot;).&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rustc --version
&lt;&#x2F;span&gt;&lt;span&gt;rustc 1.61.0 (fe5b13d68 2022-05-18)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To find the current version number in your Rust code you can make use of the &lt;a href=&quot;https:&#x2F;&#x2F;docs.rs&#x2F;version_check&#x2F;latest&#x2F;version_check&#x2F;&quot;&gt;version_check&lt;&#x2F;a&gt; crate, which calls the &lt;code&gt;rustc&lt;&#x2F;code&gt; command behind the scenes and parses the output.&lt;&#x2F;p&gt;
&lt;p&gt;The command above shows the default compiler version, meaning the compiler you will default to when running the &lt;code&gt;rustc&lt;&#x2F;code&gt; command. You may have different versions installed though, the default one can be set using the &lt;code&gt;rustup default &amp;lt;version&amp;gt;&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;To list other available Rust version you may run the &lt;code&gt;rustup show&lt;&#x2F;code&gt; command to show the installed compiler toolchains or profiles.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use Composer with a specific PHP version</title>
		<published>2022-05-28T00:00:00+00:00</published>
		<updated>2022-05-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/composer-use-specific-php-version/" type="text/html"/>
		<id>https://koenwoortman.com/composer-use-specific-php-version/</id>
		<content type="html">&lt;p&gt;Are you using different PHP versions on your local development machine just like me? Then you might have encountered the following error when installing packages with composer, again, just like me.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ composer require intervention&#x2F;image:^2.7
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;Your requirements could not be resolved to an installable set of packages.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Problem 1
&lt;&#x2F;span&gt;&lt;span&gt;  - Root composer.json requires php ^7.4 but your php version (8.1.5) does not satisfy that requirement.
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Composer defaults to the default PHP version. That is, the &lt;a href=&quot;&#x2F;bash-script-shebang&#x2F;&quot;&gt;shebang comment&lt;&#x2F;a&gt; at the top of the executable composer file indicates that the &lt;code&gt;php&lt;&#x2F;code&gt; binary should be used.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;#!&#x2F;usr&#x2F;bin&#x2F;env php
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can find out which version this is by running &lt;code&gt;php --version&lt;&#x2F;code&gt; on your terminal.&lt;&#x2F;p&gt;
&lt;p&gt;Now, in order to use the PHP version of your preference you should run the &lt;code&gt;composer&lt;&#x2F;code&gt; binary with the desired &lt;code&gt;php&lt;&#x2F;code&gt; binary. For this you need the exact path to the &lt;code&gt;composer&lt;&#x2F;code&gt; binary. On my machine the &lt;code&gt;composer&lt;&#x2F;code&gt; binary is located at &lt;code&gt;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;composer&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Thus the command would look the following for me. Using PHP version 7.4.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;php7.4 &#x2F;usr&#x2F;local&#x2F;bin&#x2F;composer require intervention&#x2F;image:2.7.2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To find out where composer is installed on your machine you can run &lt;code&gt;which composer&lt;&#x2F;code&gt; in your terminal:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ which composer
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;usr&#x2F;local&#x2F;bin&#x2F;composer
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To make this a bit easier you can make this a one step process as well.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php7.4 $(which composer) require intervention&#x2F;image:2.7.2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are required to run this command often then you might want to make it an alias in your shell.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP: Split strings based on a character</title>
		<published>2022-05-27T00:00:00+00:00</published>
		<updated>2022-05-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-explode-string/" type="text/html"/>
		<id>https://koenwoortman.com/php-explode-string/</id>
		<content type="html">&lt;p&gt;The way to split up a string on a certain character into an array of substrings is by means of the built-in PHP string function &lt;code&gt;explode()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Consider the following example in which a URL path is split on a slash (&lt;code&gt;&#x27;&#x2F;&#x27;&lt;&#x2F;code&gt;) into multiple URL segments.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;blog&#x2F;php&#x2F;split-strings&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;$segments &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;explode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $path);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($segments);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;     [0] =&amp;gt; blog
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;     [1] =&amp;gt; php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;     [2] =&amp;gt; split-strings
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;explode()&lt;&#x2F;code&gt; function requires a delimiter string as a first argument and the string to split as its second argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;explode(string $separator, string $string): array
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;It is not obligatory to use a single character as a separator, it is perfectly fine to split the string on &lt;code&gt;https:&#x2F;&#x2F;&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
&lt;p&gt;Potentially we can supply an optional third argument to &lt;code&gt;explode()&lt;&#x2F;code&gt; as well, this is the maximum amount of items we wish get returned in our array of results.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;blog&#x2F;php&#x2F;split-strings&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;$segments &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;explode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $path, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($segments);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;     [0] =&amp;gt; blog
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt;     [1] =&amp;gt; php&#x2F;split-strings
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In contrary to exploding a string based on a character, we can also &lt;a href=&quot;&#x2F;php-join-array-by-string-character&#x2F;&quot;&gt;implode&lt;&#x2F;a&gt; a string. This implodes an array of strings together into one string joined by a specified character.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use .slice() to remove the last character of JavaScript Strings</title>
		<published>2022-05-26T00:00:00+00:00</published>
		<updated>2022-05-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-last-character-from-string/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-last-character-from-string/</id>
		<content type="html">&lt;p&gt;The most convenient way to remove the last character from a JavaScript String object is by making use of the &lt;code&gt;.slice()&lt;&#x2F;code&gt; &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;String&#x2F;slice&quot;&gt;method&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;Koe&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The two arguments passed to &lt;code&gt;slice()&lt;&#x2F;code&gt; mean the following:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;0&lt;&#x2F;code&gt; we pass as the first argument to &lt;code&gt;.slice()&lt;&#x2F;code&gt; is the starting-index; from where we want to start slicing.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;-1&lt;&#x2F;code&gt;, the second argument, is the end-index which indicates up to which character we wish to slice off characters. With negative one we specify the last character of the string.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;.slice()&lt;&#x2F;code&gt; method returns a new string object, leaving the original unchanged.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;description-of-slice&quot;&gt;Description of slice()&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;.slice()&lt;&#x2F;code&gt; method extracts a certain portion of a string. To specify this portion we need to pass an index to indicate where to start slicing and optionally one for where to end slicing. If we don&#x27;t specify an end-index it is assumed that we want to slice to the end of the string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;.prototype.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(beginIndex, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt;endIndex)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result that gets returned to us by &lt;code&gt;.slice()&lt;&#x2F;code&gt; is a new String object, the original string &lt;strong&gt;won&#x27;t be mutated&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, to truly remove the last character from the string that is held by a variable we should reassign the result to the original variable.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(name);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;Koe&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;negative-indexing&quot;&gt;Negative indexing&lt;&#x2F;h2&gt;
&lt;p&gt;One part that might be confusing is the negative index of &lt;code&gt;-1&lt;&#x2F;code&gt;. With negative indexes we start at the end of the string and go further backwards once we decrease the negative index.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, a negative index of &lt;code&gt;-1&lt;&#x2F;code&gt; will return the last character of a string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;n&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Slicing off just the last character can be done by first passing the negative string length of &lt;code&gt;-4&lt;&#x2F;code&gt; and again &lt;code&gt;-1&lt;&#x2F;code&gt; as the end-index.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;Koe&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;use-substring-to-remove-the-last-character&quot;&gt;Use substring() to remove the last character&lt;&#x2F;h2&gt;
&lt;p&gt;Alternatively we could use the &lt;code&gt;.substring()&lt;&#x2F;code&gt; method instead of &lt;code&gt;.slice()&lt;&#x2F;code&gt;. The downside to this approach is that we cannot make use of negative indexing. Therefore we should find out the necessary end-index via the length of the string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;name.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;substring&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, name.length &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(name);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;Koe&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;remove-the-first-character&quot;&gt;Remove the first character&lt;&#x2F;h2&gt;
&lt;p&gt;We learned to remove the last character from a string, by using the same &lt;code&gt;.slice()&lt;&#x2F;code&gt; we can just as quickly remove the first character from the string.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of passing a negative index to the &lt;code&gt;.slice()&lt;&#x2F;code&gt; method we can pass a positive end-index to the slice of characters from the start of the string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;=&amp;gt; &amp;quot;oen&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The end-index is non-inclusive, meaning that we slice the string &lt;strong&gt;until&lt;&#x2F;strong&gt; the index and &lt;strong&gt;not&lt;&#x2F;strong&gt; up to and including.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Install .deb files in Ubuntu</title>
		<published>2022-05-25T00:00:00+00:00</published>
		<updated>2022-05-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-install-deb-file/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-install-deb-file/</id>
		<content type="html">&lt;p&gt;Deb packages are a vital part of Ubuntu. The Ubuntu package repositories, from which you install applications, consist of thousands of Deb packages.&lt;&#x2F;p&gt;
&lt;p&gt;If you run &lt;code&gt;apt install&lt;&#x2F;code&gt; you basically download and unpack a Deb package. But besides the abstraction provided by the &lt;code&gt;apt&lt;&#x2F;code&gt; command you can install a downloaded .deb file yourself as well.&lt;&#x2F;p&gt;
&lt;p&gt;To install a .deb package file on Ubuntu you use the &lt;code&gt;dpkg&lt;&#x2F;code&gt; command. To install it requires the &lt;code&gt;-i&lt;&#x2F;code&gt; option and you need to specify the file path to the .deb file as an argument.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ dpkg -i ~&#x2F;Downloads&#x2F;my_package.deb
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Make sure to only install .deb packages from a trusted source. Anyone can package software.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Delete local git branches with -d</title>
		<published>2022-05-24T00:00:00+00:00</published>
		<updated>2022-05-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-delete-local-branch/" type="text/html"/>
		<id>https://koenwoortman.com/git-delete-local-branch/</id>
		<content type="html">&lt;p&gt;We can delete a branch by using the &lt;code&gt;-d&lt;&#x2F;code&gt; option to the &lt;code&gt;branch&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -d &amp;lt;branch-name&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;-d&lt;&#x2F;code&gt; option is short for &lt;code&gt;--delete&lt;&#x2F;code&gt; here.&lt;&#x2F;p&gt;
&lt;p&gt;Git does run some security checks before the local branch will be deleted. If git blocks your delete you can include the &lt;code&gt;--force&lt;&#x2F;code&gt; flag in the above command as well.&lt;&#x2F;p&gt;
&lt;p&gt;There is a shortcut which combines the &lt;code&gt;--delete&lt;&#x2F;code&gt; and &lt;code&gt;--force&lt;&#x2F;code&gt; flags: &lt;code&gt;-D&lt;&#x2F;code&gt; (capital d). This option forces the branch to be deleted.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -D &amp;lt;branch-name&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do have some caution when force deleting branches in git, there is probably a good reason why git blocks you from deleting the local branch.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;delete-the-remote-branch&quot;&gt;Delete the remote branch&lt;&#x2F;h2&gt;
&lt;p&gt;Now that the branch has been removed from our local git repository we should keep our remote in mind as well. That is, we can remove the branch from our remote repository as well.&lt;&#x2F;p&gt;
&lt;p&gt;This might feel a bit unintuitive, but in order to remove the branch from our remote we should to our remote with the &lt;code&gt;--delete&lt;&#x2F;code&gt; option. Assumed that our remote is named &lt;code&gt;origin&lt;&#x2F;code&gt; we can remove the remote branch as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git push origin --delete &amp;lt;remote-branch&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you remove branches from a remote branch make sure that your team mates or CI&#x2F;CD doesn&#x27;t rely on the presence of this branch.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use --show-current to print the current git branch</title>
		<published>2022-05-23T00:00:00+00:00</published>
		<updated>2022-05-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-print-current-branch-name/" type="text/html"/>
		<id>https://koenwoortman.com/git-print-current-branch-name/</id>
		<content type="html">&lt;p&gt;Since Git version 2.22 you can easily get the current branch name in your git repository with the &lt;code&gt;--show-current&lt;&#x2F;code&gt; option to the &lt;code&gt;branch&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --show-current
&lt;&#x2F;span&gt;&lt;span&gt;main
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Git version 2.22 was released in 2019, so by now the above will probably be available to you. If not there is still the old way of doing things. In the old way of doing things you may get the current branch name via the &lt;code&gt;rev-parse&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rev-parse --abbrev-ref HEAD
&lt;&#x2F;span&gt;&lt;span&gt;main
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And then there is also just the &lt;code&gt;git branch&lt;&#x2F;code&gt; command which might serve you just fine. Instead of just the current branch this command shows you all the local branch and prefixes the currently active branch with an asterisk (&lt;code&gt;*&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch
&lt;&#x2F;span&gt;&lt;span&gt;  dev
&lt;&#x2F;span&gt;&lt;span&gt;* main
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use add() to append items to a Set in Python</title>
		<published>2022-05-22T00:00:00+00:00</published>
		<updated>2022-05-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-append-to-set/" type="text/html"/>
		<id>https://koenwoortman.com/python-append-to-set/</id>
		<content type="html">&lt;p&gt;Even though Sets are quite similar to Lists in Python, there is a differently named method to call when appending items to a set. Instead of the &lt;code&gt;append()&lt;&#x2F;code&gt; method we use &lt;code&gt;add()&lt;&#x2F;code&gt; for sets.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;LETTERS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;p&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;i&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;z&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;LETTERS.add(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(LETTERS)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; {&amp;#39;i&amp;#39;, &amp;#39;p&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;z&amp;#39;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;First, Sets are unordered in Python. Therefore the &lt;code&gt;print()&lt;&#x2F;code&gt; function displays the letters in a different order then how the set was created.&lt;&#x2F;p&gt;
&lt;p&gt;Second, in Set theory the items in a set should be unique. Therefore we cannot have the same item twice in a set. This will not show an error, you will just not notice a change to the set.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;LETTERS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;p&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;i&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;z&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(LETTERS)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; {&amp;#39;i&amp;#39;, &amp;#39;p&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;z&amp;#39;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;LETTERS.add(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;z&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(LETTERS)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; {&amp;#39;i&amp;#39;, &amp;#39;p&amp;#39;, &amp;#39;a&amp;#39;, &amp;#39;z&amp;#39;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see in the example above, the letter &#x27;z&#x27; is only present once after we tried to add a second &#x27;z&#x27; to the set.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set your default terminal editor in Linux</title>
		<published>2022-05-21T00:00:00+00:00</published>
		<updated>2022-05-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-set-default-terminal-editor/" type="text/html"/>
		<id>https://koenwoortman.com/linux-set-default-terminal-editor/</id>
		<content type="html">&lt;p&gt;While others may have this the other way around, I feel clumsy in everything that doesn&#x27;t have vim-like keybindings. Mainly speaking about you here &lt;code&gt;nano&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily we can configure our preferred terminal editor for our shell environment. Whether it is &lt;code&gt;nano&lt;&#x2F;code&gt;, &lt;code&gt;vim&lt;&#x2F;code&gt;, &lt;code&gt;emacs&lt;&#x2F;code&gt; or something exotic. This setting is taken from the &lt;code&gt;EDITOR&lt;&#x2F;code&gt; and &lt;code&gt;VISUAL&lt;&#x2F;code&gt; environment variables.&lt;&#x2F;p&gt;
&lt;p&gt;The difference between the &lt;code&gt;EDITOR&lt;&#x2F;code&gt; and &lt;code&gt;VISUAL&lt;&#x2F;code&gt; variables is that the editor you set to &lt;code&gt;EDITOR&lt;&#x2F;code&gt; should be able to work with more advanced interactive terminal functionality.&lt;&#x2F;p&gt;
&lt;p&gt;These variables are set for your shell environment, so depending on whether you use bash, zsh or fish you should edit the correct shell configuration file. For bash that is &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt;, and for zsh &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;set-nano-as-your-editor-in-bash&quot;&gt;Set nano as your editor in bash&lt;&#x2F;h2&gt;
&lt;p&gt;Assumed you use bash as your shell, add the following to your &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt; file to set &lt;code&gt;nano&lt;&#x2F;code&gt; as your terminal editor.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.bashrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;EDITOR&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nano&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;VISUAL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nano&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;set-vim-as-your-editor-in-bash&quot;&gt;Set vim as your editor in bash&lt;&#x2F;h2&gt;
&lt;p&gt;Assumed you use bash as your shell, add the following to your &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt; file to set &lt;code&gt;vim&lt;&#x2F;code&gt; as your terminal editor.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.bashrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;EDITOR&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vi&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;VISUAL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vim&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;set-emacs-as-your-editor-in-bash&quot;&gt;Set emacs as your editor in bash&lt;&#x2F;h2&gt;
&lt;p&gt;Assumed you use bash as your shell, add the following to your &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt; file to set &lt;code&gt;emacs&lt;&#x2F;code&gt; as your terminal editor.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.bashrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;EDITOR&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;emacs -nw&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;VISUAL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;emacs&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Disable the beep sound in ZSH</title>
		<published>2022-05-20T00:00:00+00:00</published>
		<updated>2022-05-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-no-beep/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-no-beep/</id>
		<content type="html">&lt;p&gt;In order to disable the beep or bell sound in your Zsh shell add the following &lt;code&gt;unsetopt beep&lt;&#x2F;code&gt; to your &lt;code&gt;.zshrc&lt;&#x2F;code&gt; config file.&lt;&#x2F;p&gt;
&lt;p&gt;The beep sound in your Zsh shell helps to indicate whenever you are doing something wrong. For example, it goes off when there are no autocomplete results. Although this might be useful for some, I find it a bit annoying.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily we can disable the beep in our shell configuration.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# No bell: Shut up Zsh
&lt;&#x2F;span&gt;&lt;span&gt;unsetopt beep
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above configuration only disables beep sounds in your Zsh shell. You might still encounter a beep sound in other shells, like bash, or in a terminal text editor like vim.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;As a quick reference:&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;To disable the bells in Vim add the following to your &lt;code&gt;~&#x2F;.vimrc&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; noerrorbells
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to disable the beep in your bash shell add the following to your &lt;code&gt;~&#x2F;.inputrc&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; bell-style none
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use -m or -M to rename a local git branch</title>
		<published>2022-05-19T00:00:00+00:00</published>
		<updated>2022-05-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-rename-local-branch/" type="text/html"/>
		<id>https://koenwoortman.com/git-rename-local-branch/</id>
		<content type="html">&lt;p&gt;Branch names in git are not at all static. Especially if you have not yet pushed a branch to a git remote you are free to rename your branches as often as you like. In my workflow I often prefix branch names with &lt;code&gt;wip-&lt;&#x2F;code&gt; while working on a new feature.&lt;&#x2F;p&gt;
&lt;p&gt;First we will checkout the branch we wish to rename, use &lt;code&gt;git checkout&lt;&#x2F;code&gt; as the first step:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git checkout wip-new-feature
&lt;&#x2F;span&gt;&lt;span&gt;$ git branch --show-current
&lt;&#x2F;span&gt;&lt;span&gt;wip-new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we are on the branch we wish to rename we can use the -m option of the &lt;code&gt;branch&lt;&#x2F;code&gt; subcommand to give the branch a new name. The &lt;code&gt;-m&lt;&#x2F;code&gt; option is actually short for &lt;code&gt;--move&lt;&#x2F;code&gt;, that might make it easier to remember.&lt;&#x2F;p&gt;
&lt;p&gt;To move the current git branch to a new name you use the command &lt;code&gt;git branch -m &amp;lt;new-name&amp;gt;&lt;&#x2F;code&gt;. See the following example:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --show-current
&lt;&#x2F;span&gt;&lt;span&gt;wip-new-feature
&lt;&#x2F;span&gt;&lt;span&gt;$ git branch -m new-feature
&lt;&#x2F;span&gt;&lt;span&gt;$ git branch --show-current
&lt;&#x2F;span&gt;&lt;span&gt;new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;force-a-new-name&quot;&gt;Force a new name&lt;&#x2F;h2&gt;
&lt;p&gt;If you already have a branch with this name you may encounter an error saying that a branch with that name already exists.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -m new-feature
&lt;&#x2F;span&gt;&lt;span&gt;fatal: A branch named &amp;#39;new-feature&amp;#39; already exists.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You have different ways to get around this error. Obviously you can rename the other branch first using the steps shown above. Or you can force the rename, by using the &lt;code&gt;--force&lt;&#x2F;code&gt; option or &lt;code&gt;-M&lt;&#x2F;code&gt; (capital m).&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -M new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;renaming-branches-directly&quot;&gt;Renaming branches directly&lt;&#x2F;h2&gt;
&lt;p&gt;Although we chose to checkout the branch we wished to rename, this is not mandatory. We can rename a branch as well by supplying both the current branch name and the new branch name.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -m  wip-new-feature new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;push-a-renamed-branch&quot;&gt;Push a renamed branch&lt;&#x2F;h2&gt;
&lt;p&gt;All the examples above only move the local branch to a new name. To have this newly named branch on our remote as well we need to push the branch. We will do so while directly setting the upstream branch we track:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git push origin -u new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The second step is to remove the old name from the remote. But you want to have some &lt;strong&gt;caution&lt;&#x2F;strong&gt; here. If team members or your continuous integration solution relies on this branch don&#x27;t just delete it.&lt;&#x2F;p&gt;
&lt;p&gt;If that is all taken care of you can remove the remote branch as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git push origin --delete wip-new-feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Learn Python for-loops</title>
		<published>2022-05-18T00:00:00+00:00</published>
		<updated>2022-05-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-for-loop/" type="text/html"/>
		<id>https://koenwoortman.com/python-for-loop/</id>
		<content type="html">&lt;p&gt;The Python for-loop loops over the contents of any iterable Python objects. Iterable Python objects include first and foremost list objects, other iterable types include &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;library&#x2F;string.html&quot;&gt;Strings&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;tutorial&#x2F;datastructures.html#tuples-and-sequences&quot;&gt;Tuples&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;tutorial&#x2F;datastructures.html#sets&quot;&gt;Sets&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;For-loops in Python are implemented according to the syntax &lt;code&gt;for &amp;lt;item&amp;gt; in &amp;lt;iterable&amp;gt;:&lt;&#x2F;code&gt;, this is demonstrated in the following example in which we loop over a list of strings:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;letter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;letter is:&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, letter)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;letter is: a&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;letter is: b&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;letter is: c&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above example we loop over a list of letters that we create on the fly. Each element is available to us inside the for-loop by the variable name we used. In our case we used the variable name &lt;code&gt;letter&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After the last iteration of the for-loop the variable &lt;code&gt;letter&lt;&#x2F;code&gt; is still available to you holding the last value of the list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(letter)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;c&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;exit-for-loops-early&quot;&gt;Exit for loops early&lt;&#x2F;h2&gt;
&lt;p&gt;Depending on the size of the iterable, loops can be memory and&#x2F;or time consuming processes. When you don&#x27;t need to loop through all the items you can choose to exit out of a for-loop early by using the &lt;code&gt;break&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;p&gt;By using the &lt;code&gt;break&lt;&#x2F;code&gt; statement you can manually stop processing the items in the for-loop.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;]:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;% &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Found an even number: &amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, number)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;break
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(number, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is odd&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;1 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;3 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;Found an even number: 4&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The for-loop above loops over a list of numbers and breaks out of the loop once the expression &lt;code&gt;number % 2 == 0&lt;&#x2F;code&gt; evaluates as &lt;code&gt;true&lt;&#x2F;code&gt;. As you see, the items in the list that come after this even number are not processed. This is due to the &lt;code&gt;break&lt;&#x2F;code&gt; statements, which makes us exit the for-loop.&lt;&#x2F;p&gt;
&lt;p&gt;If you wish to &lt;strong&gt;not&lt;&#x2F;strong&gt; exit out of the but just skip the current iteration you should go with the &lt;code&gt;continue&lt;&#x2F;code&gt; statement instead. We cover how to use the &lt;code&gt;continue&lt;&#x2F;code&gt; statement in for-loops below.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;continue-to-the-next-iteration&quot;&gt;Continue to the next iteration&lt;&#x2F;h2&gt;
&lt;p&gt;Now, let&#x27;s discuss a slightly different statement which lets us continue to the next iteration; &lt;code&gt;continue&lt;&#x2F;code&gt;. This statement won&#x27;t make us break out of the for-loop all together, but it lets us break out of just the current iteration.&lt;&#x2F;p&gt;
&lt;p&gt;In other words, with the &lt;code&gt;continue&lt;&#x2F;code&gt; statement we can continue to the next item in the list or other iterable object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;]:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;% &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;continue
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(number, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is odd&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;1 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;3 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;7 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;11 is odd&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the output of the code snippet above you will notice that the number &lt;code&gt;4&lt;&#x2F;code&gt; is skipped. Since the if statement evaluates as &lt;code&gt;true&lt;&#x2F;code&gt; for the number &lt;code&gt;4&lt;&#x2F;code&gt; we continue &lt;em&gt;before&lt;&#x2F;em&gt; the &lt;code&gt;print()&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;continue&lt;&#x2F;code&gt; statement terminates further execution of the for-loop its body and continues to the next iteration.&lt;&#x2F;p&gt;
&lt;p&gt;In contrast to &lt;code&gt;break&lt;&#x2F;code&gt; we do process the other items in the list.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;access-the-index-in-for-loops&quot;&gt;Access the index in for loops&lt;&#x2F;h2&gt;
&lt;p&gt;If you are familiar with C-like languages you probably came across for-loops with a signature like &lt;code&gt;for (i = 0; i &amp;lt; 10; i++)&lt;&#x2F;code&gt;. While I personally favour the readability of for-loops in Python, having a variable available which can be used as an index or loop iteration counter can be convenient.&lt;&#x2F;p&gt;
&lt;p&gt;In Python we can get a loop index, starting at &lt;code&gt;0&lt;&#x2F;code&gt;, by using the &lt;code&gt;enumerate()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;zero&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;one&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;two&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;three&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;index, number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;enumerate&lt;&#x2F;span&gt;&lt;span&gt;(numbers):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(index, number)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;0 zero&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;1 one&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;2 two&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;3 three&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;enumerate()&lt;&#x2F;code&gt; function gives you two variables for your for-loop; the &lt;strong&gt;counter&lt;&#x2F;strong&gt; for the current iteration and second the corresponding &lt;strong&gt;value&lt;&#x2F;strong&gt; for the iteration.&lt;&#x2F;p&gt;
&lt;p&gt;If you are truly looking for a variable that counts instead of an index then maybe you prefer a variant for which you define the counter variable yourselves.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;count &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;11&lt;&#x2F;span&gt;&lt;span&gt;]:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;% &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;continue
&lt;&#x2F;span&gt;&lt;span&gt;    count &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Found&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, count, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;odd numbers&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;Found 4 odd numbers&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;loop-through-dictionaries&quot;&gt;Loop through dictionaries&lt;&#x2F;h2&gt;
&lt;p&gt;As I mentioned at the top of this post, we can use for-loops to loop through other types of Python data structures as well. One example that might be useful is looping over the contents of a dictionary.&lt;&#x2F;p&gt;
&lt;p&gt;To loop over key-value pairs of a Python dictionary we use the &lt;code&gt;.items()&lt;&#x2F;code&gt; method available on dictionaries, which returns a tuple with both the key and the value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nul&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;zero&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;een&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;one&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;twee&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;two&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;drie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;three&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;dutch, english &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;numbers.items():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(dutch, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is Dutch for&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, english)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides looping over the keys and values of the dictionary together, we can loop over the keys or values individually as well.&lt;&#x2F;p&gt;
&lt;p&gt;In order to loop over the keys of a dictionary we use the &lt;code&gt;.keys()&lt;&#x2F;code&gt; method available on dictionaries. This returns the dictionary keys to you as a list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nul&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;zero&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;een&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;one&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;twee&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;two&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;drie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;three&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;dutch_number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;numbers.keys():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(dutch_number)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;nul&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;een&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;twee&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;drie&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To loop over the values of a dictionary the same applies as for keys, but instead we use the &lt;code&gt;.values()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nul&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;zero&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;een&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;one&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;twee&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;two&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;drie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;three&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;english_number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;numbers.values():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(english_number)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;zero&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;one&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;two&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;quot;three&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove all `null` values from an array in PHP</title>
		<published>2022-05-17T00:00:00+00:00</published>
		<updated>2022-05-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-array-remove-all-null-values/" type="text/html"/>
		<id>https://koenwoortman.com/php-array-remove-all-null-values/</id>
		<content type="html">&lt;p&gt;A convenient method to remove all &lt;code&gt;null&lt;&#x2F;code&gt; values from an array in PHP is by using the &lt;code&gt;array_filter()&lt;&#x2F;code&gt; function. As the name suggests the method filters a given array, if you don&#x27;t supply your own filter function PHP filters out all values that evaluate as &lt;code&gt;false&lt;&#x2F;code&gt; in a condition; empty strings, &lt;code&gt;0&lt;&#x2F;code&gt;, &lt;code&gt;false&lt;&#x2F;code&gt;, &lt;code&gt;null&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To explicitly only filter out the &lt;code&gt;null&lt;&#x2F;code&gt; values you should pass your own callback function to the &lt;code&gt;array_filter()&lt;&#x2F;code&gt; function that only returns &lt;code&gt;false&lt;&#x2F;code&gt; if a given value is equal to &lt;code&gt;null&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using an arrow function that would look like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_filter&lt;&#x2F;span&gt;&lt;span&gt;($values, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$value&lt;&#x2F;span&gt;&lt;span&gt;) =&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;is_null&lt;&#x2F;span&gt;&lt;span&gt;($value));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [0] =&amp;gt; a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [1] =&amp;gt; b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [2] =&amp;gt; c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As I mentioned above, if you don&#x27;t supply a callback function yourself PHP filters out all values that evaluate as &lt;code&gt;false&lt;&#x2F;code&gt;. So that includes other values besides &lt;code&gt;null&lt;&#x2F;code&gt; as well. But maybe that&#x27;s just what you need. In that case, see the example below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;, []];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_filter&lt;&#x2F;span&gt;&lt;span&gt;($values);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [0] =&amp;gt; a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Instal Xdebug for PHP on Ubuntu 20.04</title>
		<published>2022-05-16T00:00:00+00:00</published>
		<updated>2022-05-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-install-php-xdebug/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-install-php-xdebug/</id>
		<content type="html">&lt;p&gt;The Xdebug extension for PHP is included in the main repositories of Ubuntu 20.04. So with an easy &lt;code&gt;apt install&lt;&#x2F;code&gt; you should be set with an extension for the same PHP version as currently shipping in the Ubuntu repositories.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt install php-xdebug
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Personally I get my PHP binaries on Ubuntu from the PPA package repository of Ondřej Surý (&lt;code&gt;ppa:ondrej&#x2F;php&lt;&#x2F;code&gt;). This allows me to easily use multiple PHP versions on my local machine to work on multiple projects requiring different PHP versions.&lt;&#x2F;p&gt;
&lt;p&gt;If you have a similar setup, make sure to install the Xdebug extension for the right PHP version. For example for PHP 7.4:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt install php7.4-xdebug
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option would be to install Xdebug from PECL (PHP Extension Community Library).&lt;&#x2F;p&gt;
&lt;p&gt;If you have PECL locally setup you can install Xdebug as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ pecl install xdebug
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Python XOR (^) operator</title>
		<published>2022-05-15T00:00:00+00:00</published>
		<updated>2022-05-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-xor-operator/" type="text/html"/>
		<id>https://koenwoortman.com/python-xor-operator/</id>
		<content type="html">&lt;p&gt;The XOR operator (^) is one of the bitwise operators in Python. Bitwise operators allow for logical operation in your Python code.&lt;&#x2F;p&gt;
&lt;p&gt;XOR stands for &amp;quot;exclusive or&amp;quot; here, the &amp;quot;exclusive or&amp;quot; logical operation only evaluates to true if the two given inputs differ.&lt;&#x2F;p&gt;
&lt;p&gt;Thus, if one is true and one is false. This is not to be confused with the bitwise OR operator (|), which evaluates to true if one of the two inputs or both are true.&lt;&#x2F;p&gt;
&lt;p&gt;The truth table of the XOR operator in Python looks as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; False
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; True
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;^ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; False
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>PHP check if string ends with substring</title>
		<published>2022-05-14T00:00:00+00:00</published>
		<updated>2022-05-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-check-if-string-ends-with-substring/" type="text/html"/>
		<id>https://koenwoortman.com/php-check-if-string-ends-with-substring/</id>
		<content type="html">&lt;p&gt;Do you need to find out whether a PHP string ends with a certain substring?&lt;&#x2F;p&gt;
&lt;p&gt;Checking whether a PHP string ends with a certain substring can be as easy as calling &lt;code&gt;str_ends_with()&lt;&#x2F;code&gt; or somewhat more complex logic using &lt;code&gt;substr_compare()&lt;&#x2F;code&gt; depending on your PHP version.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;for-php-8&quot;&gt;For PHP 8&lt;&#x2F;h2&gt;
&lt;p&gt;PHP 8.0 introduced the new &lt;code&gt;str_ends_with()&lt;&#x2F;code&gt; function which is perfect for this use-case. It works just as expected, first you pass the string you wish to inspect and second the substring you are looking for. If the substring is present then the result will be &lt;code&gt;true&lt;&#x2F;code&gt;, otherwise it will be &lt;code&gt;false&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; PHP 8+
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;str_ends_with(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The check is case-sensitive. In case you need to be case-insensitive you could use &lt;code&gt;strtolower()&lt;&#x2F;code&gt; to lowercase the entire string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; PHP 8+
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;str_ends_with(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza Salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;str_ends_with(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strtolower&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza Salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is one catch here, that is when checking whether your string ends with an empty string. This will always return true, since all strings end with an empty string.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;PS. Besides the &lt;code&gt;str_ends_with()&lt;&#x2F;code&gt; function, PHP 8 also introduced a new &lt;code&gt;str_starts_with()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;for-php-7&quot;&gt;For PHP 7&lt;&#x2F;h2&gt;
&lt;p&gt;The approach for PHP 7 is somewhat more cryptic. You may use the &lt;code&gt;substr_compare()&lt;&#x2F;code&gt; function to compare a substring against the string you wish to check from a certain offset.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; PHP 7
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;substr_compare&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strlen&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; -&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Join array items by a string character in PHP</title>
		<published>2022-05-13T00:00:00+00:00</published>
		<updated>2022-05-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-join-array-by-string-character/" type="text/html"/>
		<id>https://koenwoortman.com/php-join-array-by-string-character/</id>
		<content type="html">&lt;p&gt;Coming from Python I got quited used to the &lt;code&gt;.join()&lt;&#x2F;code&gt; method that allows you to join array items together as a string separated by a specified character.&lt;&#x2F;p&gt;
&lt;p&gt;Although not called join in PHP, PHP does provide an equivalent function called &lt;code&gt;implode()&lt;&#x2F;code&gt;. This being the opposite of the &lt;code&gt;explode()&lt;&#x2F;code&gt; function which splits a string on a specified character into an array of substrings.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;implode()&lt;&#x2F;code&gt; function requires a string separator and the array to join as a string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$reserved &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;app&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;home&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;about&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo implode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;,&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $reserved);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;quot;app,home,about&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What you use as a separator is up to you obviously, see here an example with dashes instead of comma&#x27;s.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$reserved &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;app&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;home&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;about&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo implode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $reserved);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;quot;app-home-about&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To join the array items together as a single word string you could use an empty string as the separator:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$reserved &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;app&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;home&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;about&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo implode&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, $reserved);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;quot;apphomeabout&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, in this case the string separator is optional, and you can achieve the same by not providing a separator string at all.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$reserved &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;app&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;home&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;about&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo implode&lt;&#x2F;span&gt;&lt;span&gt;($reserved);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;quot;apphomeabout&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solve `NameError: name &#x27;np&#x27; is not defined` in Python</title>
		<published>2022-05-12T00:00:00+00:00</published>
		<updated>2022-05-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-name-error-np-is-not-defined/" type="text/html"/>
		<id>https://koenwoortman.com/python-name-error-np-is-not-defined/</id>
		<content type="html">&lt;p&gt;If you came across the error message &amp;quot;NameError: name &#x27;np&#x27; is not defined&amp;quot; you probably try to call a &lt;code&gt;numpy&lt;&#x2F;code&gt; function. The &lt;code&gt;numpy&lt;&#x2F;code&gt; package is often abbreviated to &lt;code&gt;np&lt;&#x2F;code&gt; in its import statement.&lt;&#x2F;p&gt;
&lt;p&gt;See the following piece of code for example, it tries to call the &lt;code&gt;.abs()&lt;&#x2F;code&gt; function on &lt;code&gt;np&lt;&#x2F;code&gt;. However, &lt;code&gt;np&lt;&#x2F;code&gt; is not defined in the scope of our Python script&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;numpy
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(np.abs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; NameError: name &amp;#39;np&amp;#39; is not defined
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead you could call the &lt;code&gt;.abs()&lt;&#x2F;code&gt; function on the &lt;code&gt;numpy&lt;&#x2F;code&gt; module as intended.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;numpy
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(numpy.abs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But the answer your are most likely looking for is the following, where you import the &lt;code&gt;numpy&lt;&#x2F;code&gt; module under a different name: &lt;code&gt;np&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;numpy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;np
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(np.abs(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Ignore node_modules folder when using the `tree` command</title>
		<published>2022-05-11T00:00:00+00:00</published>
		<updated>2022-05-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tree-command-ignore-node-modules/" type="text/html"/>
		<id>https://koenwoortman.com/tree-command-ignore-node-modules/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;tree&lt;&#x2F;code&gt; is a handy command-line utility for listing the contents of a folder with the contents of all the subfolders. However convenient, this can get out of hand rather quickly, especially in a Node.js project with a buffy &lt;code&gt;node_modules&#x2F;&lt;&#x2F;code&gt; folder.&lt;&#x2F;p&gt;
&lt;p&gt;You may ignore a folder in the output of &lt;code&gt;tree&lt;&#x2F;code&gt; by using the &lt;code&gt;-I&lt;&#x2F;code&gt; (capital i) option. Thus, ignoring the node modules would be like &lt;code&gt;tree -I node_modules&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ tree -I node_modules
&lt;&#x2F;span&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;├── components
&lt;&#x2F;span&gt;&lt;span&gt;│   └── PostsPage.vue
&lt;&#x2F;span&gt;&lt;span&gt;├── nuxt.config.js
&lt;&#x2F;span&gt;&lt;span&gt;├── package.json
&lt;&#x2F;span&gt;&lt;span&gt;├── package-lock.json
&lt;&#x2F;span&gt;&lt;span&gt;├── pages
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── index.vue
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── page
&lt;&#x2F;span&gt;&lt;span&gt;│   │   └── _page.vue
&lt;&#x2F;span&gt;&lt;span&gt;│   └── _slug.vue
&lt;&#x2F;span&gt;&lt;span&gt;├── static
&lt;&#x2F;span&gt;&lt;span&gt;│   └── favicon.ico
&lt;&#x2F;span&gt;&lt;span&gt;└── utils
&lt;&#x2F;span&gt;&lt;span&gt;    └── fetchPostsMixin.js
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;5 directories, 9 files
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides the &lt;code&gt;node_modules&#x2F;&lt;&#x2F;code&gt; folder there might be other folders you wish to ignore in the output of &lt;code&gt;tree&lt;&#x2F;code&gt;, a &lt;code&gt;dist&#x2F;&lt;&#x2F;code&gt; folder for example that may contain the output of a build.&lt;&#x2F;p&gt;
&lt;p&gt;You separate the directories you pass to the ignore option by a &lt;code&gt;|&lt;&#x2F;code&gt; symbol. Make sure to put the entire list of folders between quotes, otherwise the &lt;code&gt;|&lt;&#x2F;code&gt; is interpreted by your shell as output redirection.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ tree -I &amp;#39;node_modules|dist&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides ignoring certain directories there are other options to trim the output of the &lt;code&gt;tree&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;-d&lt;&#x2F;code&gt; option will only list directories and no files: &lt;code&gt;tree -I node_modules -d&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;The &lt;code&gt;-L&lt;&#x2F;code&gt; option will only list directories down to a given level of depth: &lt;code&gt;tree -I node_modules -L 2&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the last item in a JavaScript Array</title>
		<published>2022-05-10T00:00:00+00:00</published>
		<updated>2022-05-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-last-item-of-array/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-last-item-of-array/</id>
		<content type="html">&lt;p&gt;There are different approaches to accessing the last array item in JavaScript, which to choose depends on whether you wish to remove that element from the array or just access it.&lt;&#x2F;p&gt;
&lt;p&gt;First let&#x27;s see how you just access the last item of an array, without removing it from that array. You can go for either the &lt;code&gt;slice()&lt;&#x2F;code&gt; method or use the array length to find the last index.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;using-the-array-length&quot;&gt;Using the array length&lt;&#x2F;h2&gt;
&lt;p&gt;Using the array length to find the last index of the array is probably the most straight forward way. Since array indexes start counting at &lt;code&gt;0&lt;&#x2F;code&gt; you should do the length minus &lt;code&gt;1&lt;&#x2F;code&gt; to access the last element.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;days &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;mo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;tu&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;we&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;th&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;fr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;days[days.length &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(last);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;#39;fr&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you run this operation on an empty array the result will be &lt;code&gt;undefined&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;days &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;days[days.length &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(last);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; undefined
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;using-slice&quot;&gt;Using slice()&lt;&#x2F;h2&gt;
&lt;p&gt;With &lt;code&gt;slice()&lt;&#x2F;code&gt; you access the last element when you provide the negative index -1 as a parameter.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;days &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;mo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;tu&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;we&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;th&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;fr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;days.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(last);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; [&amp;#39;fr&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see the result of this operation is not just the last element, but another array containing just the last element.&lt;&#x2F;p&gt;
&lt;p&gt;To just get the last element, you should use &lt;code&gt;[0]&lt;&#x2F;code&gt; in combination with &lt;code&gt;slice()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;days &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;mo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;tu&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;we&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;th&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;fr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;days.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(last);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;#39;fr&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Running this operation on an empty array results in &lt;code&gt;undefined&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;[].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;slice&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; undefined
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;by-popping-off-the-last-element&quot;&gt;By popping off the last element&lt;&#x2F;h2&gt;
&lt;p&gt;Another option would be to use the &lt;code&gt;.pop()&lt;&#x2F;code&gt; method. Instead of just accessing the last element, this method removes the last element from the array and returns it. In some cases this is just what you need.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;days &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;mo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;tu&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;we&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;th&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;fr&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;last &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;days.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;pop&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(last);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;#39;fr&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(days);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; (4) [&amp;#39;mo&amp;#39;, &amp;#39;tu&amp;#39;, &amp;#39;we&amp;#39;, &amp;#39;th&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove key and value from an associative array in PHP</title>
		<published>2022-05-09T00:00:00+00:00</published>
		<updated>2022-05-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-remove-keys-from-associative-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-remove-keys-from-associative-array/</id>
		<content type="html">&lt;p&gt;To remove a key and its respective value from an associative array in PHP you can use the &lt;code&gt;unset()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$mascots &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ElePHPant&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Geeko&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;openSUSE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Go&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;unset&lt;&#x2F;span&gt;&lt;span&gt;($mascots[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($mascots);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [ElePHPant] =&amp;gt; php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [Geeko] =&amp;gt; openSUSE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As the name of the function suggests, you use the &lt;code&gt;unset()&lt;&#x2F;code&gt; function to unset a given variable or in this case an array key with its value.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;remove-multiple-keys-from-associative-array&quot;&gt;Remove multiple keys from associative array&lt;&#x2F;h2&gt;
&lt;p&gt;Removing multiple keys from associative array can be done using the &lt;code&gt;unset()&lt;&#x2F;code&gt; as well. You can pass as many keys to unset as arguments to the &lt;code&gt;unset()&lt;&#x2F;code&gt; function. See the example below where two keys are dropped from the associative array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$mascots &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ElePHPant&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Geeko&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;openSUSE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Go&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;unset&lt;&#x2F;span&gt;&lt;span&gt;($mascots[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;], $mascots[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Geeko&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($mascots);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [ElePHPant] =&amp;gt; php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However useful, the approach above might get somewhat tedious when you need to remove multiple keys from the associative array. In that case there is another option, the &lt;code&gt;array_diff()&lt;&#x2F;code&gt; function. The &lt;code&gt;array_diff()&lt;&#x2F;code&gt; function compares the array you pass it as its first argument and returns an array with the values not present in the array you pass it in the second array.&lt;&#x2F;p&gt;
&lt;p&gt;In contrast to the other options I present here this approach require you to specify the values for which you remove the keys (and values). Instead of keys for which you wish to remove the values (and keys).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$mascots &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ElePHPant&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Geeko&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;openSUSE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Go&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_diff&lt;&#x2F;span&gt;&lt;span&gt;($mascots, [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;openSUSE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Go&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($values);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [ElePHPant] =&amp;gt; php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This last approach seems especially convenient if you need to remove the keys (and values) dynamically in your code.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;remove-all-keys-from-associative-array&quot;&gt;Remove all keys from associative array&lt;&#x2F;h2&gt;
&lt;p&gt;To remove all keys from an associative PHP array is to basically turn the array into a regular numerically indexed array. This can be achieved by grabbing just the values from the associative PHP array.&lt;&#x2F;p&gt;
&lt;p&gt;Since associative arrays in PHP are ordered, just like numerically indexed arrays, we can grab just the values and maintain the original order of the array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$mascots &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ElePHPant&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Geeko&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;openSUSE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Gopher&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Go&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_values&lt;&#x2F;span&gt;&lt;span&gt;($mascots);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($values);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [0] =&amp;gt; php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [1] =&amp;gt; openSUSE
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [2] =&amp;gt; Go
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above code sample creates a new array from the values of &lt;code&gt;$mascots&lt;&#x2F;code&gt; and stores the result in the variable &lt;code&gt;$values&lt;&#x2F;code&gt;. The &lt;code&gt;$values&lt;&#x2F;code&gt; array is a regular numerically indexed array, thus all the keys from the associative array &lt;code&gt;$mascots&lt;&#x2F;code&gt; are not present anymore.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the first value in an associative PHP array</title>
		<published>2022-05-08T00:00:00+00:00</published>
		<updated>2022-05-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-first-value-in-associative-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-first-value-in-associative-array/</id>
		<content type="html">&lt;p&gt;Not only numerically indexed arrays but also associative arrays are ordered in PHP. Meaning, that you can be certain that the keys in an associative array will maintain their respective order.&lt;&#x2F;p&gt;
&lt;p&gt;This allows you not only reliably access the first value in an array, but also the first key. To do so you can use the &lt;code&gt;array_key_first()&lt;&#x2F;code&gt; function that was introduced in PHP 7.3, this you may use to access the first value in an associative.&lt;&#x2F;p&gt;
&lt;p&gt;To get the first value in a multidimensional array you first get the first key, next you use this key to access the first value.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-14&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Monday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-15&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Tuesday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-16&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Wednesday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$first &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$data[array_key_first($data)];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$first;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; Monday
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As with about everything you have multiple options. You could for example first grab only the values in the array, and take the first element from there as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$data &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-14&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Monday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-15&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Tuesday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;2022-02-16&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Wednesday&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo array_values&lt;&#x2F;span&gt;&lt;span&gt;($data)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; Monday
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This one-line approach might be a nice alternative when it comes to code readability. It is somewhat more expansive in terms of performance however.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a  Nuxt.js blog with the content module</title>
		<published>2022-05-07T00:00:00+00:00</published>
		<updated>2022-05-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/nuxtjs-create-blog-with-content-module/" type="text/html"/>
		<id>https://koenwoortman.com/nuxtjs-create-blog-with-content-module/</id>
		<content type="html">&lt;p&gt;Let&#x27;s start with a disappointing disclaimer; I&#x27;m not using Nuxt.js to run my blog, currently I use the static site generator &lt;a href=&quot;&#x2F;replacing-hugo-with-zola&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt;. I did use the Nuxt content module to set up a blog for a different project though.&lt;&#x2F;p&gt;
&lt;p&gt;Although the &lt;a href=&quot;https:&#x2F;&#x2F;content.nuxtjs.org&#x2F;&quot;&gt;documentation&lt;&#x2F;a&gt; of &lt;code&gt;nuxt&#x2F;content&lt;&#x2F;code&gt; is fine, it does leave some practical topics uncovered. I hope to help with some of them here in this manual.&lt;&#x2F;p&gt;
&lt;p&gt;The final (but unstyled) version is available on my GitHub:
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;koenwoortman&#x2F;nuxt-content-blog-template&quot;&gt;koenwoortman&#x2F;nuxt-content-blog-template&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You can preview the final version at Netlify &lt;a href=&quot;https:&#x2F;&#x2F;voluble-granita-ee5a6b.netlify.app&#x2F;&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h2&gt;
&lt;p&gt;At the time of writing Nuxt v3 does not yet have a stable release, therefore I will just stick to Nuxt v2. I assume most of the code will be easily adjustable to fit in your Nuxt 3 project in the future.&lt;&#x2F;p&gt;
&lt;p&gt;Besides other file formats, the content module allows you to publish web pages from Markdown files. Markdown being the most suitable of the supported file formats I&#x27;ll just focus on that here.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Markdown is a plain text markup language that can be compiled to HTML. It has the benefit over HTML of being fairly readable without the HTML tags.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;To provide some extra meta data to your blog posts you can start your Markdown files with a block of YAML code, called Front Matter. We&#x27;ll use that extensively here too. The Front Matter YAML block should always start on the first line of of your Markdown blog post, see the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;markdown&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-markdown &quot;&gt;&lt;code class=&quot;language-markdown&quot; data-lang=&quot;markdown&quot;&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;span&gt;title: &amp;quot;My First Blog Post&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Hello World
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All together this allows you to set up a file based headless CMS, which you can host freely on platforms such as Netlify or Vercel. But we will get into deployments later.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-started&quot;&gt;Getting started&lt;&#x2F;h2&gt;
&lt;p&gt;If you create a new Nuxt project from scratch with &lt;code&gt;create-nuxt-app&lt;&#x2F;code&gt;, which is what I will do here, the installer prompts you for whether the content module should be added. I am happily adding the content module in this way.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npx create-nuxt-app nuxt-blog
&lt;&#x2F;span&gt;&lt;span&gt;create-nuxt-app v3.7.1
&lt;&#x2F;span&gt;&lt;span&gt;✨  Generating Nuxt.js project in nuxt-blog
&lt;&#x2F;span&gt;&lt;span&gt;? Project name: nuxt-blog
&lt;&#x2F;span&gt;&lt;span&gt;? Programming language: JavaScript
&lt;&#x2F;span&gt;&lt;span&gt;? Package manager: Npm
&lt;&#x2F;span&gt;&lt;span&gt;? UI framework: None
&lt;&#x2F;span&gt;&lt;span&gt;? Nuxt.js modules:
&lt;&#x2F;span&gt;&lt;span&gt; ◯ Axios - Promise based HTTP client
&lt;&#x2F;span&gt;&lt;span&gt; ◯ Progressive Web App (PWA)
&lt;&#x2F;span&gt;&lt;span&gt;❯◉ Content - Git-based headless CMS
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you already have an existing Nuxt project however you can add the content module by running an NPM install:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npm install @nuxt&#x2F;content
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After this is done make sure to add &lt;code&gt;&#x27;@nuxt&#x2F;content&#x27;&lt;&#x2F;code&gt; to the modules in your &lt;code&gt;nuxt.config.js&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; nuxt.config.js
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  target: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;static&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;  modules: [
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;@nuxt&#x2F;content&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides that I am going to assume here that the build target is set to &lt;code&gt;&#x27;static&#x27;&lt;&#x2F;code&gt;, this allows us later on to host our blog as a static site (Jamstack).&lt;&#x2F;p&gt;
&lt;p&gt;To keep things simple I removed a couple directories from the Nuxt project root such that is looks like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;├── content
&lt;&#x2F;span&gt;&lt;span&gt;│   └── hello.md
&lt;&#x2F;span&gt;&lt;span&gt;├── nuxt.config.js
&lt;&#x2F;span&gt;&lt;span&gt;├── package.json
&lt;&#x2F;span&gt;&lt;span&gt;├── package-lock.json
&lt;&#x2F;span&gt;&lt;span&gt;├── pages
&lt;&#x2F;span&gt;&lt;span&gt;│   └── index.vue
&lt;&#x2F;span&gt;&lt;span&gt;└── static
&lt;&#x2F;span&gt;&lt;span&gt;    └── favicon.ico
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you just scaffolded your project with the &lt;code&gt;create-nuxt-app&lt;&#x2F;code&gt; command the &lt;code&gt;content&lt;&#x2F;code&gt; folder should be present, if not go ahead and create it manually.&lt;&#x2F;p&gt;
&lt;p&gt;The content module looks for files in the &lt;code&gt;content&#x2F;&lt;&#x2F;code&gt; directory in our project root, so make sure that it is present.&lt;&#x2F;p&gt;
&lt;p&gt;Now that has been taken care of, let&#x27;s continue by displaying a list of blog posts.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;start-with-a-list-of-posts&quot;&gt;Start with a list of posts&lt;&#x2F;h2&gt;
&lt;p&gt;To display a fairly basic and not styled list of blog posts you may add the following code to the &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;index.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;post in posts&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:key&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;post.slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;`&#x2F;${post.slug}`&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {{ post.title }} &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;$content().fetch();
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{ posts };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Without any CSS styling this looks like the following, not very pretty but it gets the job done. I will leave the styling up to you.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;&#x2F;nuxtjs-create-blog-with-content-module&#x2F;posts-list.png&quot; alt=&quot;List of blog posts&quot; title=&quot;List of blog posts&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I understand this requires some explaining. First we want to fetch our Markdown content in the &lt;code&gt;asyncData&lt;&#x2F;code&gt; hook from Nuxt so that we get our content before the page is rendered.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;asyncData&lt;&#x2F;code&gt; is a hook provided by Nuxt that is only available on pages. Generally you use it for data fetching, the return value will be merged into the normal Vue local state of your component.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;We fetch the content via the content module which is available to us as &lt;code&gt;$content&lt;&#x2F;code&gt;, the &lt;code&gt;.fetch()&lt;&#x2F;code&gt; method call speaks for itself.&lt;&#x2F;p&gt;
&lt;p&gt;Second, you may have noticed already that we make use of the &lt;code&gt;.slug&lt;&#x2F;code&gt; and &lt;code&gt;.title&lt;&#x2F;code&gt; properties of our blog posts, but where do they come from?&lt;&#x2F;p&gt;
&lt;p&gt;Besides the variables that we can define ourselves in the Front Matter of our blog posts, Nuxt content injects some variables itself. You can find a list of these variables &lt;a href=&quot;https:&#x2F;&#x2F;content.nuxtjs.org&#x2F;writing#front-matter&quot;&gt;here&lt;&#x2F;a&gt;. One of these injected variables in the &lt;code&gt;slug&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;By default the slug is set to the filename of your Markdown file without the file extension, the slug field is therefore useful to use for the unique URL of the blog post.&lt;&#x2F;p&gt;
&lt;p&gt;The other variable, &lt;code&gt;title&lt;&#x2F;code&gt;, should be defined in the Front Matter of a blog post. I have used the default &lt;code&gt;hello.md&lt;&#x2F;code&gt; file which came with the installation which has the title &amp;quot;Getting started&amp;quot;. See below the corresponding Front Matter of this &lt;code&gt;hello.md&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;Getting started
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Empower your NuxtJS application with @nuxt&#x2F;content module: write in a content&#x2F; directory and fetch your Markdown, JSON, YAML and CSV files through a MongoDB like API, acting as a Git-based Headless CMS.&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;exclude-unused-fields&quot;&gt;Exclude unused fields&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;fetch()&lt;&#x2F;code&gt; method includes all the variables for all the posts that are fetched. Currently that is not such a big deal. But if we were to have a couple dozen blog posts with large bodies, this becomes an expansive operation.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore we can limit the variables that we fetch here to only the &lt;code&gt;slug&lt;&#x2F;code&gt; and &lt;code&gt;title&lt;&#x2F;code&gt;. Since this are the only two properties we use in our list of blog posts.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;$content().only([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]).fetch();
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{ posts };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you add properties to your list of blog posts, such as the published date or so, &lt;strong&gt;do not forget to include them&lt;&#x2F;strong&gt; in the array you pass to &lt;code&gt;only()&lt;&#x2F;code&gt;. Otherwise those values will be &lt;code&gt;undefined&lt;&#x2F;code&gt;, hope this saves you some debugging time.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;display-a-blog-post&quot;&gt;Display a blog post&lt;&#x2F;h2&gt;
&lt;p&gt;Now that we got our list of blog posts covered we can focus our attention on displaying single blog posts.&lt;&#x2F;p&gt;
&lt;p&gt;The first thing we need is a new file, I will use the file &lt;code&gt;pages&#x2F;_slug.vue&lt;&#x2F;code&gt; for this. Meaning that all our blog routes will come directly after the domain, without a &lt;code&gt;blog&#x2F;&lt;&#x2F;code&gt;, &lt;code&gt;articles&lt;&#x2F;code&gt; or &lt;code&gt;posts&#x2F;&lt;&#x2F;code&gt; prefix. Obviously you can do so if you prefer, you should simply create a file &lt;code&gt;pages&#x2F;blog&#x2F;_slug.vue&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;_slug.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{{ post.title }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-content &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:document&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;post&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;article&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;slug &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;params.slug;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;post &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;$content(slug).fetch();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{ post };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To go over our &lt;code&gt;asyncData()&lt;&#x2F;code&gt; function briefly. We mainly make use of dynamic pages here (pages with names starting with an underscore), the URL parameter name is equal to the file name we used. In our case this URL parameter name is &lt;code&gt;slug&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We use the slug from the URL to fetch the corresponding markdown file from our &lt;code&gt;content&#x2F;&lt;&#x2F;code&gt; directory.&lt;&#x2F;p&gt;
&lt;p&gt;Furthermore in our template, all the magic here is provided by the &lt;code&gt;&amp;lt;nuxt-content&amp;gt;&lt;&#x2F;code&gt; component which renders your markdown file.&lt;&#x2F;p&gt;
&lt;p&gt;Again, with minimal styling this results in the following page.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;&#x2F;nuxtjs-create-blog-with-content-module&#x2F;first-post.png&quot; alt=&quot;First blog post&quot; title=&quot;First blog post&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Personally I am not a big fan of the live editing feature that is enabled by default. You disable this in your &lt;code&gt;nuxt.config.js&lt;&#x2F;code&gt; by setting the &lt;code&gt;liveEdit&lt;&#x2F;code&gt; setting to &lt;code&gt;false&lt;&#x2F;code&gt; in the &lt;code&gt;conent&lt;&#x2F;code&gt; configuration. See the &lt;a href=&quot;https:&#x2F;&#x2F;content.nuxtjs.org&#x2F;configuration#liveedit&quot;&gt;docs&lt;&#x2F;a&gt; for more information.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h3 id=&quot;table-of-contents&quot;&gt;Table of contents&lt;&#x2F;h3&gt;
&lt;p&gt;With long blog posts, such as this one, a table of contents is convenient for the reader. The content module adds ID attributes to headers. These IDs can be used to jump to an anchor tag on the same page.&lt;&#x2F;p&gt;
&lt;p&gt;The table of contents is available as a property of your post object. By looping over its contents you can add the table of contents to your template as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  ...
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;link of post.toc&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:key&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;link.id&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;`#${link.id}`&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{{ link.text }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  ...
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;seo-fields&quot;&gt;SEO Fields&lt;&#x2F;h3&gt;
&lt;p&gt;If you write a blog you probably care about SEO to a certain degree. Adding an HTML title and meta description are high on the list here.&lt;&#x2F;p&gt;
&lt;p&gt;To set HTML head tags in Nuxt you should define a &lt;code&gt;head()&lt;&#x2F;code&gt; method in your page component.&lt;&#x2F;p&gt;
&lt;p&gt;The place to set such variables is most likely in the Front Matter of your Markdown file. The one Markdown file which was added during the installation provided both a title and description luckily:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;Getting started
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Empower your NuxtJS application with @nuxt&#x2F;content module: write in a content&#x2F; directory and fetch your Markdown, JSON, YAML and CSV files through a MongoDB like API, acting as a Git-based Headless CMS.&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The variables from the Front Matter are accessible as properties on the post object. Thus in our &lt;code&gt;head()&lt;&#x2F;code&gt; method we can get their respective values as &lt;code&gt;this.post.title&lt;&#x2F;code&gt; and &lt;code&gt;this.post.description&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Using these values in the &lt;code&gt;head()&lt;&#x2F;code&gt; method is fairly basic JavaScript.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;slug &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;params.slug;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;post &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;$content(slug).fetch();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{ post };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;head&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        title: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;${this.post.title}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt; | My Blog`&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        meta: [
&lt;&#x2F;span&gt;&lt;span&gt;          {
&lt;&#x2F;span&gt;&lt;span&gt;            hid: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;description&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            name: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;description&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            content: this.post.description,
&lt;&#x2F;span&gt;&lt;span&gt;          },
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;      };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;pagination&quot;&gt;Pagination&lt;&#x2F;h2&gt;
&lt;p&gt;Next up it is time to add pagination to our list of blog posts, but first I&#x27;ll add some other blog posts to paginate from some Lorem Ipsum paragraphs. I&#x27;ll continue with the following seven blog posts.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;content
&lt;&#x2F;span&gt;&lt;span&gt;├── aenean-vel-mattis.md
&lt;&#x2F;span&gt;&lt;span&gt;├── donec-elementum.md
&lt;&#x2F;span&gt;&lt;span&gt;├── hello.md
&lt;&#x2F;span&gt;&lt;span&gt;├── lorem-ipsum.md
&lt;&#x2F;span&gt;&lt;span&gt;├── nullam-facilisis.md
&lt;&#x2F;span&gt;&lt;span&gt;├── quisque-a-egestas.md
&lt;&#x2F;span&gt;&lt;span&gt;└── ut-nisl-augue.md
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we have some blog posts to paginate on we need to set up a new page template: &lt;code&gt;pages&#x2F;page&#x2F;_page.vue&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;pages
&lt;&#x2F;span&gt;&lt;span&gt;├── index.vue
&lt;&#x2F;span&gt;&lt;span&gt;├── page
&lt;&#x2F;span&gt;&lt;span&gt;│   └── _page.vue
&lt;&#x2F;span&gt;&lt;span&gt;└── _slug.vue
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is where the refactoring begins.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;fetching-posts-from-a-mixin&quot;&gt;Fetching posts from a mixin&lt;&#x2F;h3&gt;
&lt;p&gt;Both components &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; and the new &lt;code&gt;pages&#x2F;page&#x2F;_page.vue&lt;&#x2F;code&gt; should do practically the same thing. Fetching the blog posts in the &lt;code&gt;asyncData&lt;&#x2F;code&gt; method that is, so it would be great if we could make the code we implemented for &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; reusable. The approach I&#x27;ll take here is via Vue mixins.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Mixins are a native Vue feature that allow you to distribute reusable functionalities between Vue components. You can find the Vue documentation &lt;a href=&quot;https:&#x2F;&#x2F;v2.vuejs.org&#x2F;v2&#x2F;guide&#x2F;mixins.html&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;For this create a new file in a new &lt;code&gt;utils&#x2F;&lt;&#x2F;code&gt; directory, I&#x27;ll name the file &lt;code&gt;fetchPostsMixin.js&lt;&#x2F;code&gt;. Thus we should be import this file in our components from &lt;code&gt;&#x27;@&#x2F;utils&#x2F;fetchPostsMixin&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We move the &lt;code&gt;asyncData&lt;&#x2F;code&gt; method from our &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; component to this file, pay attention to &lt;code&gt;error&lt;&#x2F;code&gt; which we now destructure from the first parameter too. We will need that later.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; utils&#x2F;fetchPostsMixin.js
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;error &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{ posts };
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both our &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; and &lt;code&gt;pages&#x2F;page&#x2F;_page.vue&lt;&#x2F;code&gt; components can now be adjusted to use the &lt;code&gt;asyncData&lt;&#x2F;code&gt; method from the mixin as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;index.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;page&#x2F;_page.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; ... &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;fetchPostsMixin &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;@&#x2F;utils&#x2F;fetchPostsMixin&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    mixins: [fetchPostsMixin],
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;extend-the-asyncdata-method&quot;&gt;Extend the asyncData method&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we only need to make changes in one method instead of two, lets get to it.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll post the entire method first, and explain the lines separately. You can go ahead and copy the following to the file &lt;code&gt;utils&#x2F;fetchPostsMixin.js&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; utils&#x2F;fetchPostsMixin.js
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;error &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; We will only show 3 posts per page
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;POSTS_PER_PAGE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Get the page number from the URL, fallback on 1 otherwise.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;parseInt&lt;&#x2F;span&gt;&lt;span&gt;(params.page) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; We need the total for pagination.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;totalAmountOfPosts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;()).length;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Calculate the last page number.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;lastPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ceil&lt;&#x2F;span&gt;&lt;span&gt;(totalAmountOfPosts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span&gt;POSTS_PER_PAGE);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; If we try to access a page without posts throw a 404.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;lastPage) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;({ statusCode: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;, message: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;No articles for this page.&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; We want to skip the right amount of posts
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; so we only grab the posts for the current page.
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;POSTS_PER_PAGE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;(currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Fetch the posts for the current page
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;(POSTS_PER_PAGE)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span&gt;(offset)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Return the posts of the current page with
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; some other convenient properties
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      posts,
&lt;&#x2F;span&gt;&lt;span&gt;      totalAmountOfPosts,
&lt;&#x2F;span&gt;&lt;span&gt;      currentPage,
&lt;&#x2F;span&gt;&lt;span&gt;      firstPage: currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      lastPage: currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span&gt;lastPage,
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I hope the constant &lt;code&gt;POSTS_PER_PAGE&lt;&#x2F;code&gt; speaks for itself, so the next line is:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;parseInt&lt;&#x2F;span&gt;&lt;span&gt;(params.page) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;|| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since we named our pagination template &lt;code&gt;pages&#x2F;page&#x2F;_page.vue&lt;&#x2F;code&gt; we can access the URL parameter by its name &lt;code&gt;page&lt;&#x2F;code&gt;. However, URL parameters are available to us as a string but we want it as an integer here. For our route &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; the &lt;code&gt;page&lt;&#x2F;code&gt; parameter is undefined, so therefore we fallback on &lt;code&gt;1&lt;&#x2F;code&gt; to display the posts of the first page there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;totalAmountOfPosts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;()).length;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to calculate what our last page should be we need the total amount of blog posts. We fetch them without any of their properties (&lt;code&gt;.only([])&lt;&#x2F;code&gt;) to minimize the overhead. Do notice that the promise should be resolved first before we can call &lt;code&gt;.length&lt;&#x2F;code&gt; on the result, that accounts for the extra pair of parenthesis.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;lastPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ceil&lt;&#x2F;span&gt;&lt;span&gt;(totalAmountOfPosts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&#x2F; &lt;&#x2F;span&gt;&lt;span&gt;POSTS_PER_PAGE);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To get the last page we divide the total amount of posts by the posts we wish to show on a page. We round this number up, to cover the use-case of the last page which might contain less posts.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;lastPage) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;error&lt;&#x2F;span&gt;&lt;span&gt;({ statusCode: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404&lt;&#x2F;span&gt;&lt;span&gt;, message: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;No articles for this page.&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we try to navigate to an URL with a page parameter higher then the total amount of pages we have no blog posts to show. In such a case we throw a 404 error.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;offset &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;POSTS_PER_PAGE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span&gt;(currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;- &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From the second page onwards we need some offset to the blog posts we fetch, otherwise each page would contain the same blog posts. We pass this number to the &lt;code&gt;.skip()&lt;&#x2F;code&gt; method later.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;(POSTS_PER_PAGE)
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span&gt;(offset)
&lt;&#x2F;span&gt;&lt;span&gt;  .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next is the actual fetching of our blog posts. We added the limit of posts to fetch and skip to a certain offset based on the current page.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  posts,
&lt;&#x2F;span&gt;&lt;span&gt;  totalAmountOfPosts,
&lt;&#x2F;span&gt;&lt;span&gt;  currentPage,
&lt;&#x2F;span&gt;&lt;span&gt;  firstPage: currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  lastPage: currentPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span&gt;lastPage,
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally we return the posts of the current page together with some convenient properties for the pagination.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;make-a-reusable-component&quot;&gt;Make a reusable component&lt;&#x2F;h3&gt;
&lt;p&gt;So far we made our &lt;code&gt;asyncData&lt;&#x2F;code&gt; method reusable, so it would be a shame if we would need to duplicate our template. Time to make a component for this, I&#x27;ll call it &lt;code&gt;components&#x2F;PostsPage.vue&lt;&#x2F;code&gt; and you can see the code below. This is also where the extra properties we return in our mixin come in handy, below the list of posts we include navigation links.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Page {{ currentPage }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;post in posts&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:key&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;post.slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;`&#x2F;${post.slug}`&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {{ post.title }} &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;!firstPage&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;`&#x2F;page&#x2F;${currentPage - 1}`&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;gt;← Previous&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;!lastPage&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;`&#x2F;page&#x2F;${currentPage + 1}`&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &amp;gt;Next →&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nuxt-link
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    props: {
&lt;&#x2F;span&gt;&lt;span&gt;      posts: {
&lt;&#x2F;span&gt;&lt;span&gt;        type: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Array&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      currentPage: {
&lt;&#x2F;span&gt;&lt;span&gt;        type: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Number&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      firstPage: {
&lt;&#x2F;span&gt;&lt;span&gt;        type: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      lastPage: {
&lt;&#x2F;span&gt;&lt;span&gt;        type: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        required: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both our &lt;code&gt;pages&#x2F;index.vue&lt;&#x2F;code&gt; and &lt;code&gt;pages&#x2F;page&#x2F;_page.vue&lt;&#x2F;code&gt; components can now be adjusted to make use of this &lt;code&gt;PostsPage&lt;&#x2F;code&gt; component.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;index.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- pages&#x2F;page&#x2F;_page.vue --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;posts-page
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:posts&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;posts&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:currentPage&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;currentPage&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:firstPage&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;firstPage&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:lastPage&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;lastPage&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;fetchPostsMixin &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;@&#x2F;utils&#x2F;fetchPostsMixin&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;PostsPage &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;@&#x2F;components&#x2F;PostsPage.vue&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    components: { PostsPage },
&lt;&#x2F;span&gt;&lt;span&gt;    mixins: [fetchPostsMixin],
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Though, still not at all pretty without any styling it gets the job done:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;&#x2F;nuxtjs-create-blog-with-content-module&#x2F;paginated-posts-list.png&quot; alt=&quot;Paginated posts list&quot; title=&quot;Paginated posts list&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h3 id=&quot;order-by-publishing-date&quot;&gt;Order by publishing date&lt;&#x2F;h3&gt;
&lt;p&gt;Without adding any sortation the order of posts in our list looks somewhat arbitrary. The most common sortation of your blog posts is likely to be on date in descending order, from newest to older.&lt;&#x2F;p&gt;
&lt;p&gt;For this we need to adjust two things. First, in the Front Matter of &lt;strong&gt;all&lt;&#x2F;strong&gt; our blog posts we need to specify a publishing date. Second, we need to chain the sortation method when fetching our content.&lt;&#x2F;p&gt;
&lt;p&gt;I&#x27;ll call this Front Matter variable just &amp;quot;date&amp;quot;, you can pick something else like &amp;quot;publishedAt&amp;quot; as well. What is important here is that you stay consistent in defining the variable in all your posts with the same name.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yaml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yaml &quot;&gt;&lt;code class=&quot;language-yaml&quot; data-lang=&quot;yaml&quot;&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;Getting started
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2022-04-30
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;description&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Empower your NuxtJS application with @nuxt&#x2F;content module: write in a content&#x2F; directory and fetch your Markdown, JSON, YAML and CSV files through a MongoDB like API, acting as a Git-based Headless CMS.&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;---
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we need to change how we fetch our posts, therefore we need to be in our mixin. There are two changes that need to be made. Adding the &amp;quot;date&amp;quot; variable to the array we pass in the &lt;code&gt;.only()&lt;&#x2F;code&gt; method and chaining the &lt;code&gt;.sortBy()&lt;&#x2F;code&gt; method with the correct variable name and order.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; utils&#x2F;fetchPostsMixin.js
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;error &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;date&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;sortBy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;date&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;desc&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;(POSTS_PER_PAGE)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span&gt;(offset)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;exclude-future-posts&quot;&gt;Exclude future posts&lt;&#x2F;h2&gt;
&lt;p&gt;Writing blog posts in advance is a common practice. Setting the date variable that we defined in the previous section to a future date is not the problem. However, these posts show up in our list of posts as well.&lt;&#x2F;p&gt;
&lt;p&gt;To filter the list of posts we can include the &lt;code&gt;.where()&lt;&#x2F;code&gt; method, which allows us to filter the content with a MongoDB-like query syntax.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; utils&#x2F;fetchPostsMixin.js
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;asyncData&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;params&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;error &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;today &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Date().&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;valueOf&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;posts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slug&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;title&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;date&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;where&lt;&#x2F;span&gt;&lt;span&gt;({ date: { $lte: today } })
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;sortBy&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;date&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;desc&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;limit&lt;&#x2F;span&gt;&lt;span&gt;(POSTS_PER_PAGE)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;skip&lt;&#x2F;span&gt;&lt;span&gt;(offset)
&lt;&#x2F;span&gt;&lt;span&gt;      .&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;include-in-static-build&quot;&gt;Include in static build&lt;&#x2F;h2&gt;
&lt;p&gt;One of the great upsides of creating a blog with Nuxt compared to a WordPress to name one is the ability to host your blog cheaply as a static site. This takes little effort, you can publish scheduled posts via a webhook, is extremely scalable, you pay just for the domain name and continues integration to name just a couple upsides.&lt;&#x2F;p&gt;
&lt;p&gt;To generate a static site from your Nuxt project you need to run the command &lt;code&gt;npm run generate&lt;&#x2F;code&gt;. Nuxt version 2.14 introduced a crawler which would gather the needed routes from the links present in your code.&lt;&#x2F;p&gt;
&lt;p&gt;In previous versions parameterized routes were not generated in a static build. If you are for some reason still on an older version and can&#x27;t upgrade you can configure the &lt;code&gt;generate&lt;&#x2F;code&gt; command to include your content pages as follows in the &lt;code&gt;nuxt.config.js&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  target: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;static&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;  generate: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;routes&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;{ $content } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;@nuxt&#x2F;content&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;contentPages &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;$content&lt;&#x2F;span&gt;&lt;span&gt;({ deep: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true &lt;&#x2F;span&gt;&lt;span&gt;}).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;only&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;path&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]).&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetch&lt;&#x2F;span&gt;&lt;span&gt;();
&lt;&#x2F;span&gt;&lt;span&gt;      contentPages &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;contentPages.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;        file.path &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;index&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;file.path
&lt;&#x2F;span&gt;&lt;span&gt;      );
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;contentPages;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To actually deploy our static site we can go to Netlify or Vercel for example. I&#x27;m going with Netlify here.&lt;&#x2F;p&gt;
&lt;p&gt;Assumed you created a git repository for your blog you can connect it to a Netlify site. Here you need to make sure you configure the right build command: &lt;code&gt;npm run generate&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;&#x2F;nuxtjs-create-blog-with-content-module&#x2F;netlify-build-settings.png&quot; alt=&quot;Netlify deploy settings&quot; title=&quot;Netlify deploy settings&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h2&gt;
&lt;p&gt;Now the content parsing is done there are a couple other concerns you can think about.&lt;&#x2F;p&gt;
&lt;p&gt;You could consider adding some structure to the &lt;code&gt;content&#x2F;&lt;&#x2F;code&gt; folder for example. You can pass this subfolder to &lt;code&gt;$content()&lt;&#x2F;code&gt; when fetching posts. See the &lt;a href=&quot;https:&#x2F;&#x2F;content.nuxtjs.org&#x2F;fetching#contentpath-options&quot;&gt;docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Now we serve our blog posts as a slug directly after the root domain. You could consider a routing pattern like &lt;code&gt;...com&#x2F;blog&#x2F;&amp;lt;slug&amp;gt;&lt;&#x2F;code&gt;. To achieve this the page templates should be moved to a &lt;code&gt;blog&lt;&#x2F;code&gt; subfolder in your pages directory.&lt;&#x2F;p&gt;
&lt;p&gt;I left the styling all up to you, to get a head start you can grab the &lt;code&gt;nuxt-content-docs&lt;&#x2F;code&gt; theme as described in the &lt;a href=&quot;https:&#x2F;&#x2F;content.nuxtjs.org&#x2F;themes&#x2F;docs&quot;&gt;docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The final product is available here:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;koenwoortman&#x2F;nuxt-content-blog-template&quot;&gt;Get the source code&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;voluble-granita-ee5a6b.netlify.app&quot;&gt;Preview on Netlify&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What are .deb packages in Ubuntu</title>
		<published>2022-05-06T00:00:00+00:00</published>
		<updated>2022-05-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-what-is-a-deb-package/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-what-is-a-deb-package/</id>
		<content type="html">&lt;p&gt;There is a chance that you came across the &lt;code&gt;.deb&lt;&#x2F;code&gt; file extension if you use Ubuntu or another Debian-based operating system. But what are these files and why do I need them?&lt;&#x2F;p&gt;
&lt;p&gt;For Ubuntu and many other Linux distributions you often install applications from a package repository. These package repositories contain compiled applications in a certain package format, the format Ubuntu uses is the &#x27;Deb&#x27; package format.&lt;&#x2F;p&gt;
&lt;p&gt;A &#x27;Deb&#x27;, short for Debian, package contains the files necessary for the application with some meta data. In Linux, files are placed in a folder based on their use: binaries may go in &lt;code&gt;&#x2F;usr&#x2F;bin&lt;&#x2F;code&gt; and configuration files may go in the &lt;code&gt;&#x2F;etc&lt;&#x2F;code&gt; folder for example. Besides the meta data in the Deb package, the Deb package specifies where to place which files on your system.&lt;&#x2F;p&gt;
&lt;p&gt;When you run &lt;code&gt;apt install&lt;&#x2F;code&gt; the process above is basically what happens, a .deb file is downloaded from a package repository and then properly unpacked.&lt;&#x2F;p&gt;
&lt;p&gt;You may install a .deb file yourself as well using the &lt;code&gt;dpkg&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ dpkg -i &amp;lt;file_path&amp;gt;.deb
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A small security notice about the above, only install packages from trusted sources :).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Replace all `null` values in an array in PHP</title>
		<published>2022-05-05T00:00:00+00:00</published>
		<updated>2022-05-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-replace-all-null-values-in-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-replace-all-null-values-in-array/</id>
		<content type="html">&lt;p&gt;To replace all occurrences of &lt;code&gt;null&lt;&#x2F;code&gt; in an array with another value you can use the &lt;code&gt;array_map()&lt;&#x2F;code&gt; function that is built into PHP. This function applies a callback function to each item in your array and returns a new array with all the return values of that callback function.&lt;&#x2F;p&gt;
&lt;p&gt;As a first argument &lt;code&gt;array_map()&lt;&#x2F;code&gt; takes the callback function, as a second argument it takes an array of values on which the callback function should be applied.&lt;&#x2F;p&gt;
&lt;p&gt;To replace all &lt;code&gt;null&lt;&#x2F;code&gt; values by another value, the letter &amp;quot;c&amp;quot; in this example, you can take the following snippet:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$REPLACEMENT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_map&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$value&lt;&#x2F;span&gt;&lt;span&gt;) =&amp;gt; $value ?? $REPLACEMENT,
&lt;&#x2F;span&gt;&lt;span&gt;  $values
&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($result);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [0] =&amp;gt; a
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [1] =&amp;gt; b
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [2] =&amp;gt; c
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;     [3] =&amp;gt; d
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Keep in mind here that in the above snippet I&#x27;m using two features that are at the time still fairly recent: the null coalescing operator and an arrow function.&lt;&#x2F;p&gt;
&lt;p&gt;The null coalescing operator returns the first operand, except for when it is &lt;code&gt;null&lt;&#x2F;code&gt; in which case it falls back to the second operand.&lt;&#x2F;p&gt;
&lt;p&gt;PHP arrow functions were introduced in PHP 7.4 and provide a more comprehensive syntax to closures, also known as anonymous functions.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove the first character from a string in PHP</title>
		<published>2022-05-04T00:00:00+00:00</published>
		<updated>2022-05-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-remove-first-character-from-string/" type="text/html"/>
		<id>https://koenwoortman.com/php-remove-first-character-from-string/</id>
		<content type="html">&lt;p&gt;To remove the first character from a string in PHP your easiest best is probably the &lt;code&gt;substr()&lt;&#x2F;code&gt; function, short for substring.&lt;&#x2F;p&gt;
&lt;p&gt;As a first argument the &lt;code&gt;substr()&lt;&#x2F;code&gt; takes a string, as its second argument the offset for your substring. To remove just the first character of the string you want to use an offset of 1, string indexes start at 0. So to have your string start from the second character you need to use an offset of 1.&lt;&#x2F;p&gt;
&lt;p&gt;Take a look at the following example which removes the &lt;code&gt;@&lt;&#x2F;code&gt; character from a string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$account &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;@koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;$username &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;substr&lt;&#x2F;span&gt;&lt;span&gt;($account, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$username;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; &amp;quot;koen&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Install packages from the Composer lock file</title>
		<published>2022-05-03T00:00:00+00:00</published>
		<updated>2022-05-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-composer-install-from-lockfile/" type="text/html"/>
		<id>https://koenwoortman.com/php-composer-install-from-lockfile/</id>
		<content type="html">&lt;p&gt;When coming from a Node.js environment you&#x27;re probably familiar with lock files, whether it&#x27;s the package-lock.json or yarn.lock file.&lt;&#x2F;p&gt;
&lt;p&gt;To install the exact packages as specified in your composer.lock file you should run:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ composer install
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the composer.lock file isn&#x27;t present then one is generated as part of the install command.&lt;&#x2F;p&gt;
&lt;p&gt;To upgrade package versions, which will update your composer.lock file, you should run:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ composer upgrade
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Installing packages from your lock file ensures that you get exactly the same dependency versions as specified in the lock file.&lt;&#x2F;p&gt;
&lt;p&gt;In a Node.js environment you need to use a separate command or a command argument to install packages from your lockfile, depending on whether you use npm or yarn.&lt;&#x2F;p&gt;
&lt;p&gt;With composer this is the default behavior for the &lt;code&gt;install&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Resolve JSON files as modules in TypeScript</title>
		<published>2022-05-02T00:00:00+00:00</published>
		<updated>2022-05-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/typescript-import-json-file-as-module/" type="text/html"/>
		<id>https://koenwoortman.com/typescript-import-json-file-as-module/</id>
		<content type="html">&lt;p&gt;Using JSON files and its contents is a useful practice in Node.js projects.&lt;&#x2F;p&gt;
&lt;p&gt;TypeScript version 2.9 (which released in 2018) allows for JSON file imports with the introduction of the &lt;code&gt;resolveJsonModule&lt;&#x2F;code&gt; compiler option, this provides type-safety and allows for handy autocompletion using intellisense in your editor.&lt;&#x2F;p&gt;
&lt;p&gt;If you would try to import a JSON file you can expect an error message like: &amp;quot;Cannot find module &#x27;.&#x2F;package.json&#x27;. Consider using &#x27;--resolveJsonModule&#x27; to import module with &#x27;.json&#x27; extension.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;As the error message suggest the option you want to use is &lt;code&gt;resolveJsonModule&lt;&#x2F;code&gt;. You can set this compiler option to &lt;code&gt;true&lt;&#x2F;code&gt; in the &lt;code&gt;tsconfig.json&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;include&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;src&#x2F;**&#x2F;*.json&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;compilerOptions&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;resolveJsonModule&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;  }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Optionally you need to adjust the &lt;code&gt;&amp;quot;include&amp;quot;&lt;&#x2F;code&gt; property of your JSON as well, as shown in the example above.&lt;&#x2F;p&gt;
&lt;p&gt;With that being done, you should be able to import a JSON file as a JavaScript module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;package &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;.&#x2F;package.json&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Getters and Setters for computed properties in Vue.js</title>
		<published>2022-05-01T00:00:00+00:00</published>
		<updated>2022-05-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-setters-for-computed-properties/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-setters-for-computed-properties/</id>
		<content type="html">&lt;p&gt;By default computed properties are getter-only, that is, you can access the property but not assign it a value. The value should, normally, be derived from other reactive properties. Vue makes sure to re-evaluate the computed property once a reactive property used in the computed property changes.&lt;&#x2F;p&gt;
&lt;p&gt;If you were to try to assign a value to a computed property you will receive the Runtime Error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Computed property &amp;quot;...&amp;quot; was assigned to but it has no setter.`
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One thing to consider if you want to assign a value to a computed property is whether your computed property shouldn&#x27;t be a data property instead, or can achieve the same result using a watcher.&lt;&#x2F;p&gt;
&lt;p&gt;But if computed properties really suit your use-case then you can provide a custom getter and setter function to the computed property.&lt;&#x2F;p&gt;
&lt;p&gt;You may do so by turning the computed property into an object instead of just a method. This object should contain both a setter and a getter method, the setter method takes the value that you assign the computed property as the first argument.&lt;&#x2F;p&gt;
&lt;p&gt;Take the following as an example, it uses the Vue options API, and defines a custom getter and setter for the &lt;code&gt;slug&lt;&#x2F;code&gt; computed property.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;slugify &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slugify&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        slugValue: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    computed: {
&lt;&#x2F;span&gt;&lt;span&gt;      slug: {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;          this.slugValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;slugify(value);
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;slugify(this.slugValue);
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;created&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        this.slug &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hey There&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are using the composition API then the approach doesn&#x27;t differ as much. You achieve the same result by providing an object with getter and setter methods to the &lt;code&gt;computed()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;setup&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;slugify &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;slugify&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ ref, computed } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;vue&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;slugValue &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;ref(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;John&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;slug &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;computed({
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; getter
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;get&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;slugify(slugValue.value);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; setter
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Note: we are using destructuring assignment syntax here.
&lt;&#x2F;span&gt;&lt;span&gt;      slugValue.value &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;slugify(value);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove key and value from a JavaScript object</title>
		<published>2022-04-30T00:00:00+00:00</published>
		<updated>2022-04-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-key-from-object/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-key-from-object/</id>
		<content type="html">&lt;p&gt;Setting an object property value to &lt;code&gt;undefined&lt;&#x2F;code&gt; is sometimes not enough, when you rely somewhere on the result of &lt;code&gt;Object.keys()&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
&lt;p&gt;In such a case you want to remove the key and value from the object all together. For this you may use the JavaScript &lt;code&gt;delete&lt;&#x2F;code&gt; operator.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s say you have the following object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;ingredients &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  basil: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  salmon: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  tomato: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to delete the &lt;code&gt;tomato&lt;&#x2F;code&gt; property of the &lt;code&gt;ingredients&lt;&#x2F;code&gt; object you may use the following piece of code:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;delete &lt;&#x2F;span&gt;&lt;span&gt;ingredients.tomato;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This results in the following object:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(ingredients);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; { basil: true, salmon: true }
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you have a multi-word property key you should reference the property a little different, using the square braces syntax:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;delete &lt;&#x2F;span&gt;&lt;span&gt;object[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;my property&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Foreign Id to same table in Laravel migrations</title>
		<published>2022-04-29T00:00:00+00:00</published>
		<updated>2022-04-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-foreign-id-to-same-table/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-foreign-id-to-same-table/</id>
		<content type="html">&lt;p&gt;Creating foreign Id&#x27;s in Laravel abstracts away quite a lot if you adhere to the naming conventions. That is; when you&#x27;re about to add a foreign Id called &lt;code&gt;user_id&lt;&#x2F;code&gt;, Laravel assumes from the Id name you want to reference the &lt;code&gt;id&lt;&#x2F;code&gt; from the &lt;code&gt;users&lt;&#x2F;code&gt; table.&lt;&#x2F;p&gt;
&lt;p&gt;Setting up such a relation to the same table is an option too, but it requires you to overwrite this default behavior. See the following, where a foreign Id is created called &lt;code&gt;parent_id&lt;&#x2F;code&gt;. If we would Laravel try to figure out which table this should reference then it would try to create a foreign key constrained to a table called &lt;code&gt;parents&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;$table-&amp;gt;foreignId(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;parent_id&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;constrained()-&amp;gt;onDelete(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cascade&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead, you want to specify the table name your are referencing as the first argument to the &lt;code&gt;constrained()&lt;&#x2F;code&gt; method:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;$table-&amp;gt;foreignId(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;parent_id&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;constrained(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;onDelete(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cascade&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Such a pattern is useful when you have models that have somewhat of a recursive structure; a directory model that has a parent directory for example.&lt;&#x2F;p&gt;
&lt;p&gt;See for a full migration the example below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Database\Migrations\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Migration&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Database\Schema\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Blueprint&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return new &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;Migration &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * Run the migrations.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;@return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; void
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;up&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;::create(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Blueprint &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;$table&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;            $table-&amp;gt;id();
&lt;&#x2F;span&gt;&lt;span&gt;            $table-&amp;gt;foreignId(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;parent_id&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;constrained(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;onDelete(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cascade&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;            $table-&amp;gt;timestamps();
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * Reverse the migrations.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     * &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;@return&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; void
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;     *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;down&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Schema&lt;&#x2F;span&gt;&lt;span&gt;::dropIfExists(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a model was destroyed in a Laravel unit test</title>
		<published>2022-04-28T00:00:00+00:00</published>
		<updated>2022-04-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-unit-test-assert-model-destroyed/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-unit-test-assert-model-destroyed/</id>
		<content type="html">&lt;p&gt;Just like testing whether a &lt;a href=&quot;&#x2F;laravel-unit-test-assert-model-created&#x2F;&quot;&gt;model was created&lt;&#x2F;a&gt; in some cases you might want to validate whether a model was destroyed after an action. You might test for this after calling a destroy() endpoint of an JSON API for example.&lt;&#x2F;p&gt;
&lt;p&gt;Laravel has a convenient database assertion method in its unit testing library to help you with this; namely the &lt;code&gt;assertModelMissing()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;p&gt;Under the hood the &lt;code&gt;assertModelMissing()&lt;&#x2F;code&gt; assertion method calls the &lt;code&gt;assertDatabaseHas()&lt;&#x2F;code&gt; method to validate whether the primary key of the model passed as an argument is missing in the database.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;namespace &lt;&#x2F;span&gt;&lt;span&gt;Tests\Feature\Controllers\PageController;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;App\Models\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Page&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Tests\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;TestCase&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;PageControllerDestroyTest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;TestCase
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;it_can_destroy_a_page&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Setup the models we need using test factories
&lt;&#x2F;span&gt;&lt;span&gt;        $user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;::factory()-&amp;gt;create();
&lt;&#x2F;span&gt;&lt;span&gt;        $page &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Page&lt;&#x2F;span&gt;&lt;span&gt;::factory()-&amp;gt;create([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;user_id&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$user-&amp;gt;id]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Make the request
&lt;&#x2F;span&gt;&lt;span&gt;        $token &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$user-&amp;gt;createToken(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;test&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;plainTextToken;
&lt;&#x2F;span&gt;&lt;span&gt;        $response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$this-&amp;gt;withToken($token)-&amp;gt;deleteJson(route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages.destroy&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [$page-&amp;gt;id]));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Make our assertions
&lt;&#x2F;span&gt;&lt;span&gt;        $response-&amp;gt;assertStatus(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;204&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        $this-&amp;gt;assertModelMissing($page);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to use v-memo</title>
		<published>2022-04-27T00:00:00+00:00</published>
		<updated>2022-04-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-how-to-use-v-memo/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-how-to-use-v-memo/</id>
		<content type="html">&lt;p&gt;Most of the Vue directives that are available have been in existence for a while. In Vue version 3.2 a new one was introduced however: &lt;code&gt;v-memo&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Vue directives are special HTML attributes that allow you to control how Vue renders your template.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The &lt;code&gt;v-memo&lt;&#x2F;code&gt; directive allows you to memoize parts of your Vue template. Simply put, memoization is a technique to optimise programs for performance by letting functions return the same value without re-evaluating when the same inputs are given.&lt;&#x2F;p&gt;
&lt;p&gt;In a reactive frontend framework it is easy to imagine a use-case for this. Since Vue rerenders your components automatically when needed, there is a great chance that sometimes more code is executed than strictly necessary. This is where &lt;code&gt;v-memo&lt;&#x2F;code&gt; comes in.&lt;&#x2F;p&gt;
&lt;p&gt;In its most basic form the &lt;code&gt;v-memo&lt;&#x2F;code&gt; directive can be set on a HTML element and takes an array as its argument. When Vue rerenders your template, but every value in the array is the same as during the previous render then Vue skips that HTML element and all other elements inside.&lt;&#x2F;p&gt;
&lt;p&gt;See for example the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    {{ currentPageTitle }}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-memo&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;[username, loggedIn]&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;span &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;loggedIn&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; {{ username }} &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;span&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;router-link &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-else to&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&#x2F;login&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt; Login &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;router-link&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nav&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;header&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Obviously, you want to update the page title in the above template whenever you change from page. However, the username or login link only require a rerender whenever either the username changes or when the user is logged in&#x2F;out.&lt;&#x2F;p&gt;
&lt;p&gt;However useful, the above example won&#x27;t save you much time. A more useful case would be when dealing with large lists for example; so combining &lt;code&gt;v-memo&lt;&#x2F;code&gt; with &lt;code&gt;v-for&lt;&#x2F;code&gt;. That would look as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;item in items&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:key&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;item.id&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-memo&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;[lastItem.id]&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      {{ item.title }}
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For more information on &lt;code&gt;v-memo&lt;&#x2F;code&gt;, you can read the excellent &lt;a href=&quot;https:&#x2F;&#x2F;vuejs.org&#x2F;api&#x2F;built-in-directives.html#v-memo&quot;&gt;Vue docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Loop through the keys of a PHP array</title>
		<published>2022-04-26T00:00:00+00:00</published>
		<updated>2022-04-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-loop-through-keys-of-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-loop-through-keys-of-array/</id>
		<content type="html">&lt;p&gt;In most cases you probably want to loop over the values in an array. But in some cases you want to loop over the keys as well. In my case this happened when writing data to a CSV file and I used the keys for the column headers.&lt;&#x2F;p&gt;
&lt;p&gt;Probably the most convenient way is by using the &lt;code&gt;array_keys()&lt;&#x2F;code&gt; array function. This function takes an array as its argument and returns an array of the keys you used. Next you can loop over that returned array as you would with any other array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;user&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;language&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nl&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;array_keys&lt;&#x2F;span&gt;&lt;span&gt;($values);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; keys =&amp;gt; [&amp;#39;user&amp;#39;, &amp;#39;language&amp;#39;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;foreach &lt;&#x2F;span&gt;&lt;span&gt;($keys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;$key) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option would be to use the &lt;code&gt;foreach&lt;&#x2F;code&gt; loop with a key value pair, which would give you both the key and the value at each iteration of the for loop.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;user&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;language&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;nl&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;foreach &lt;&#x2F;span&gt;&lt;span&gt;($values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;$key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$value) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Assert if a Laravel model exists in your tests</title>
		<published>2022-04-25T00:00:00+00:00</published>
		<updated>2022-04-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-unit-test-assert-model-created/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-unit-test-assert-model-created/</id>
		<content type="html">&lt;p&gt;Testing HTTP endpoints in your Laravel application works quite conveniently, there are enough tools available to quickly set up the code to test a response and its JSON structure for example.&lt;&#x2F;p&gt;
&lt;p&gt;Besides checking for response codes, in some cases I like to check for whether a model was actually persisted in the database. Often I like this added security check on authorized destroy endpoints.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily there is a method for this as well that makes it easy to check whether a model does actually exist in the database; &lt;code&gt;assertModelExists()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;namespace &lt;&#x2F;span&gt;&lt;span&gt;Tests\Feature\Controllers\PageController;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;App\Models\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Page&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Tests\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;TestCase&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;PageControllerDestroyTest &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;TestCase
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;test_authentication_required&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Setup the models we need using test factories
&lt;&#x2F;span&gt;&lt;span&gt;        $page &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Page&lt;&#x2F;span&gt;&lt;span&gt;::factory()-&amp;gt;create();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Make the request
&lt;&#x2F;span&gt;&lt;span&gt;        $response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;$this-&amp;gt;deleteJson(route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pages.destroy&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [$page-&amp;gt;id]));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Make our assertions
&lt;&#x2F;span&gt;&lt;span&gt;        $response-&amp;gt;assertStatus(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;401&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        $this-&amp;gt;assertModelExists($page);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;assertModelExists()&lt;&#x2F;code&gt; test method makes use of the &lt;code&gt;assertDatabaseHas()&lt;&#x2F;code&gt; method and just checks whether a model exists in the database with the same primary key.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove empty strings from a JavaScript Array</title>
		<published>2022-04-24T00:00:00+00:00</published>
		<updated>2022-04-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-empty-string-from-array/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-empty-string-from-array/</id>
		<content type="html">&lt;p&gt;The most convenient method I found to remove empty strings from an array in JavaScript is by using the &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Global_Objects&#x2F;Array&#x2F;filter&quot;&gt;filter method&lt;&#x2F;a&gt; for arrays.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;a&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;b&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;c&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;letter);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The filter() method creates a new Array instance containing the values that evaluate as &lt;code&gt;true&lt;&#x2F;code&gt; in the callback function that is passed as an argument. Thus all values that are &lt;a href=&quot;&#x2F;javascript-what-are-falsy-values&#x2F;&quot;&gt;falsy&lt;&#x2F;a&gt;, like &lt;code&gt;false&lt;&#x2F;code&gt; and &lt;code&gt;null&lt;&#x2F;code&gt; for example, would be filtered out array above as well.&lt;&#x2F;p&gt;
&lt;p&gt;More explicitly you can filter out empty strings by checking whether a value is not equal to an empty string, this makes sure that you remove &lt;strong&gt;only&lt;&#x2F;strong&gt; empty strings from the array:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;a&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;b&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;c&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;letter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As I briefly mentioned above, the &lt;code&gt;filter()&lt;&#x2F;code&gt; method creates a new Array instance, so it doesn&#x27;t make changes in-place. Which means that if you access your array as a variable you might want to reassign its value. Otherwise the initial array would be still intact.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;a&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;b&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;c&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;letters.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;letter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; The value of &amp;#39;letters&amp;#39; isn&amp;#39;t changed
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(letters); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Instead you should assign the result to &amp;#39;letters&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; or a different variable.
&lt;&#x2F;span&gt;&lt;span&gt;letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;letters.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;letter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(letters); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; [&amp;#39;a&amp;#39;, &amp;#39;b&amp;#39;, &amp;#39;c&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Generate a new Laravel APP_KEY</title>
		<published>2022-04-23T00:00:00+00:00</published>
		<updated>2022-04-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-generate-app-key/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-generate-app-key/</id>
		<content type="html">&lt;p&gt;One of the more important variables you&#x27;ll find in your &lt;code&gt;.env&lt;&#x2F;code&gt; file after creating a new Laravel project is the &lt;code&gt;APP_KEY&lt;&#x2F;code&gt;. The &lt;code&gt;APP_KEY&lt;&#x2F;code&gt; is a random character string that is used, amongst other things, to encrypt cookies. It is &lt;strong&gt;not&lt;&#x2F;strong&gt; used to encrypt your passwords by the way, Laravel uses Hashing for this.&lt;&#x2F;p&gt;
&lt;p&gt;When you clone a repository, or when you deploy your Laravel project you&#x27;ll probably find yourself in need to generate a new &lt;code&gt;APP_KEY&lt;&#x2F;code&gt;. Luckily there is an &lt;code&gt;artisan&lt;&#x2F;code&gt; that will generate a new &lt;code&gt;APP_KEY&lt;&#x2F;code&gt; for you and automatically set it in your &lt;code&gt;.env&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan key:generate
&lt;&#x2F;span&gt;&lt;span&gt;Application key set successfully.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you prefer to copy the key yourself somewhere you can use the &lt;code&gt;--show&lt;&#x2F;code&gt; flag to print the key to your terminal.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan key:generate --show
&lt;&#x2F;span&gt;&lt;span&gt;base64:9IhKvpWXZ1tuhpN3ENFDLurQpEj4fngQvVr5KWu4rFo=
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are on Heroku you can use this &lt;code&gt;--show&lt;&#x2F;code&gt; flag as well to directly set a environment variable.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ heroku config:set APP_KEY=$(php artisan --no-ansi key:generate --show)
&lt;&#x2F;span&gt;&lt;span&gt;Setting APP_KEY and restarting ⬢ ... done, v13
&lt;&#x2F;span&gt;&lt;span&gt;APP_KEY: base64:9IhKvpWXZ1tuhpN3ENFDLurQpEj4fngQvVr5KWu4rFo=
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What are Vue directives</title>
		<published>2022-04-22T00:00:00+00:00</published>
		<updated>2022-04-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-what-are-directives/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-what-are-directives/</id>
		<content type="html">&lt;p&gt;If you have been using Vue for some time you must have seen them in your Vue template code; special keywords in your HTML starting with &lt;code&gt;v-&lt;&#x2F;code&gt;. These special HTML attributes are called Vue directives and they allow you to modify how Vue renders your template.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;HTML attributes just are some special modifiers for your HTML tags, in the &lt;code&gt;&amp;lt;img&amp;gt;&lt;&#x2F;code&gt; tag for example you can set the source of the image by specifying a value to the &lt;code&gt;src&lt;&#x2F;code&gt; attribute. Resulting in something like: &lt;code&gt;&amp;lt;img src=&amp;quot;logo.png&amp;quot;&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Take for example the &lt;code&gt;v-if&lt;&#x2F;code&gt; directive, this directive requires an argument that will be evaluated as true or false. If the expression that is passed evaluates to true then Vue renders that part of your template. If the expression evaluates as false then Vue will make sure that that part won&#x27;t show up on your webpage.&lt;&#x2F;p&gt;
&lt;p&gt;The directives that are built-in to Vue are shown at &lt;a href=&quot;https:&#x2F;&#x2F;vuejs.org&#x2F;api&#x2F;built-in-directives.html&quot;&gt;this page of the Vue documentation&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;They built-in directives are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;v-text&lt;&#x2F;li&gt;
&lt;li&gt;v-html&lt;&#x2F;li&gt;
&lt;li&gt;v-show&lt;&#x2F;li&gt;
&lt;li&gt;v-if &#x2F; v-else-if &#x2F; v-else&lt;&#x2F;li&gt;
&lt;li&gt;v-for&lt;&#x2F;li&gt;
&lt;li&gt;v-on (with shorthand &lt;code&gt;@&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;v-bind (with shorthand &lt;code&gt;:&lt;&#x2F;code&gt;)&lt;&#x2F;li&gt;
&lt;li&gt;v-model&lt;&#x2F;li&gt;
&lt;li&gt;v-slot&lt;&#x2F;li&gt;
&lt;li&gt;v-pre&lt;&#x2F;li&gt;
&lt;li&gt;v-once&lt;&#x2F;li&gt;
&lt;li&gt;v-memo&lt;&#x2F;li&gt;
&lt;li&gt;v-cloak&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Besides the directives that are built-in to Vue, some Vue plugins may provide their own directives as well.&lt;&#x2F;p&gt;
&lt;p&gt;You can create your own &lt;a href=&quot;https:&#x2F;&#x2F;vuejs.org&#x2F;guide&#x2F;reusability&#x2F;custom-directives.html&quot;&gt;custom directives&lt;&#x2F;a&gt; too, this allows you to reuse some logic that is written around DOM actions. Such as setting some CSS classes or focusing on an input field.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Instal SWI-Prolog on Ubuntu 20.04</title>
		<published>2022-04-21T00:00:00+00:00</published>
		<updated>2022-04-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-install-swi-prolog/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-install-swi-prolog/</id>
		<content type="html">&lt;p&gt;For a University class about Multi-agent system, which turned out to be quite an interesting topic, I did need to install SWI-Prolog. All the Windows people in class had an easy time with just downloading the &lt;code&gt;.exe&lt;&#x2F;code&gt; file the professor provided.&lt;&#x2F;p&gt;
&lt;p&gt;It required some searching the SWI-Prolog website for instructions on how to &lt;a href=&quot;https:&#x2F;&#x2F;www.swi-prolog.org&#x2F;build&#x2F;PPA.html&quot;&gt;install their software on Ubuntu&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Depending on whether you prefer the &lt;code&gt;stable&lt;&#x2F;code&gt; or &lt;code&gt;development&lt;&#x2F;code&gt; binaries you first need to add the correct Personal Package Archives (PPA) repository.&lt;&#x2F;p&gt;
&lt;p&gt;For the &lt;code&gt;stable&lt;&#x2F;code&gt; binaries (which is probably what you want) you need to run the following command in your terminal:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt-add-repository ppa:swi-prolog&#x2F;stable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For the development version of SWI-Prolog, which is released about every two to four weeks, you can add the &lt;code&gt;devel&lt;&#x2F;code&gt; PPA:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt-add-repository ppa:swi-prolog&#x2F;devel
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next you should run an &lt;code&gt;apt update&lt;&#x2F;code&gt; to download package information about the newly added PPA repository.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt update
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At last you can install SWI-Prolog itself.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt install swi-prolog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;in-short&quot;&gt;In short&lt;&#x2F;h2&gt;
&lt;p&gt;In short, you can install SWI-Prolog stable on Ubuntu by running the following three commands in your terminal.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt-add-repository ppa:swi-prolog&#x2F;stable
&lt;&#x2F;span&gt;&lt;span&gt;$ sudo apt update
&lt;&#x2F;span&gt;&lt;span&gt;$ sudo apt install swi-prolog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Component template should contain exactly one root element in Vue.js</title>
		<published>2022-04-20T00:00:00+00:00</published>
		<updated>2022-04-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-template-should-contain-exactly-one-root-element/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-template-should-contain-exactly-one-root-element/</id>
		<content type="html">&lt;p&gt;Since Vue version 3 your template can contain multiple root elements. That is, inside the &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; part of a Vue component you can have multiple HTML elements at the root. See the example below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;This works in Vue 3&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;This is a second root element.&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, if you are working in a Vue 2 codebase you will the following error when trying the code above:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Component template should contain exactly one root element.
&lt;&#x2F;span&gt;&lt;span&gt;If you are using v-if on multiple elements,
&lt;&#x2F;span&gt;&lt;span&gt;use v-else-if to chain them instead.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;use-a-single-root-element&quot;&gt;Use a single root element&lt;&#x2F;h2&gt;
&lt;p&gt;In this case you should move the two root elements in your template to a single wrapper element. For example, lets wrap the above in a single &lt;code&gt;&amp;lt;div&amp;gt;&lt;&#x2F;code&gt; element.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- This is now the root element  --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;This works in Vue 2 as well&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;This is not a root element anymore&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;p&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;with-conditionals&quot;&gt;With conditionals&lt;&#x2F;h2&gt;
&lt;p&gt;The error message does hint on the fact that the problem could be caused by unchained conditional directives; using multiple &lt;code&gt;v-if&lt;&#x2F;code&gt;&#x27;s instead of chaining them with a &lt;code&gt;v-else&lt;&#x2F;code&gt; and&#x2F;or &lt;code&gt;v-else-if&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Lets consider the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;loading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Page is loading&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;error &amp;amp;&amp;amp; !loading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Whoops an error occurred&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;!error &amp;amp;&amp;amp; !loading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Success!&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since using just &lt;code&gt;v-if&lt;&#x2F;code&gt;&#x27;s on your root elements you could potentially end up in a situation were multiple conditions evaluate to true. Which would result in multiple root elements, which Vue 2 cannot properly deal with. Giving the error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Errors compiling template:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Component template should contain exactly one root element.
&lt;&#x2F;span&gt;&lt;span&gt;If you are using v-if on multiple elements,
&lt;&#x2F;span&gt;&lt;span&gt;use v-else-if to chain them instead.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;1  |
&lt;&#x2F;span&gt;&lt;span&gt;2  |  &amp;lt;h1 v-if=&amp;quot;loading&amp;quot;&amp;gt;Page is loading&amp;lt;&#x2F;h1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |
&lt;&#x2F;span&gt;&lt;span&gt;3  |  &amp;lt;h1 v-if=&amp;quot;error &amp;amp;&amp;amp; !loading&amp;quot;&amp;gt;Whoops an error occurred&amp;lt;&#x2F;h1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
&lt;&#x2F;span&gt;&lt;span&gt;4  |  &amp;lt;h1 v-if=&amp;quot;!error &amp;amp;&amp;amp; !loading&amp;quot;&amp;gt;Success!&amp;lt;&#x2F;h1&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead you want to use a &lt;code&gt;v-else-if&lt;&#x2F;code&gt; and a &lt;code&gt;v-else&lt;&#x2F;code&gt; here. Which would look like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;loading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Page is loading&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-else-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;error &amp;amp;&amp;amp; !loading&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Whoops an error occurred&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-else&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Success!&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set a default value for JSON fields in Laravel migrations</title>
		<published>2022-04-19T00:00:00+00:00</published>
		<updated>2022-04-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-default-value-for-json-fields/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-default-value-for-json-fields/</id>
		<content type="html">&lt;p&gt;Laravel allows you to create JSON fields in migrations, which are supported in most up-to-date relational databases. In the &amp;quot;simpler&amp;quot; SQLite database, that I often use in early stages of a new project, JSON is stored as ordinary text.&lt;&#x2F;p&gt;
&lt;p&gt;And since you can create JSON columns in your migrations you may want to populate them with a default value in your migration too. This might work a little differently than expected. Since you&#x27;re interacting with the database table directly instead of via a model you can&#x27;t take advantage of Laravel&#x27;s field casting in your migrations.&lt;&#x2F;p&gt;
&lt;p&gt;Instead you need to provide valid JSON as a default value, using the &lt;code&gt;json_encode()&lt;&#x2F;code&gt; function in PHP is of great use here.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;$table-&amp;gt;json(&amp;#39;meta&amp;#39;)-&amp;gt;default(json_encode([]));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you prefer you can directly enter a string as well, which is what &lt;code&gt;json_encode()&lt;&#x2F;code&gt; does for you.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;$table-&amp;gt;json(&amp;#39;meta&amp;#39;)-&amp;gt;default(&amp;quot;[]&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix `linker `cc` not found` Rust error on Ubuntu</title>
		<published>2022-04-18T00:00:00+00:00</published>
		<updated>2022-04-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-fix-linker-cc-not-found-ubuntu/" type="text/html"/>
		<id>https://koenwoortman.com/rust-fix-linker-cc-not-found-ubuntu/</id>
		<content type="html">&lt;p&gt;&lt;code&gt;rustup&lt;&#x2F;code&gt;, the &lt;a href=&quot;https:&#x2F;&#x2F;www.rust-lang.org&#x2F;tools&#x2F;install&quot;&gt;Rust installer&lt;&#x2F;a&gt;, lets you easily install Rust on your system without too much hassle. And it does it very well. However, you might still see an error popping up when you run the &lt;code&gt;rustup&lt;&#x2F;code&gt; command. One of which might be: &lt;code&gt;linker &#x27;cc&#x27; not found&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The Rust installer for Linux(this includes WSL) doesn&#x27;t check whether you have the necessary compiler dependencies installed. If you are new to this than your safest bet is probably to go for the gcc toolchain (gcc: GNU Compiler Collection).&lt;&#x2F;p&gt;
&lt;p&gt;Using &lt;code&gt;apt&lt;&#x2F;code&gt;, for Ubuntu and Debian, you can install gcc by installing the &lt;code&gt;build-essential&lt;&#x2F;code&gt; package:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt install build-essential
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix failed to push some refs error when pushing to git remote</title>
		<published>2022-04-17T00:00:00+00:00</published>
		<updated>2022-04-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-failed-to-push-some-refs-error/" type="text/html"/>
		<id>https://koenwoortman.com/git-failed-to-push-some-refs-error/</id>
		<content type="html">&lt;p&gt;Pushing to a git remote may show you a frightening message sometimes, saying that your push is rejected by the remote.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git push
&lt;&#x2F;span&gt;&lt;span&gt;To github.com:koenwoortman&#x2F;some-repo.git
&lt;&#x2F;span&gt;&lt;span&gt; ! [rejected]        main -&amp;gt; main (non-fast-forward)
&lt;&#x2F;span&gt;&lt;span&gt;error: failed to push some refs to &amp;#39;git@github.com:koenwoortman&#x2F;some-repo.git&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;hint: Updates were rejected because the tip of your current branch is behind
&lt;&#x2F;span&gt;&lt;span&gt;hint: its remote counterpart. Integrate the remote changes (e.g.
&lt;&#x2F;span&gt;&lt;span&gt;hint: &amp;#39;git pull ...&amp;#39;) before pushing again.
&lt;&#x2F;span&gt;&lt;span&gt;hint: See the &amp;#39;Note about fast-forwards&amp;#39; in &amp;#39;git push --help&amp;#39; for details.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All this means is that your local branch is one or multiple commits behind compared to the branch you are pushing to. Maybe another developer pushed his or her commits? Or was it you from another machine? The GitHub dependabot might have been the cause as well.&lt;&#x2F;p&gt;
&lt;p&gt;Anyway, before you can push directly you need to get those commits you are lacking. I personally prefer to rebase (depending on the amount conflicts I am expecting):&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git fetch &amp;amp;&amp;amp; git rebase origin&#x2F;{the branch you need}
&lt;&#x2F;span&gt;&lt;span&gt;First, rewinding head to replay your work on top of it...
&lt;&#x2F;span&gt;&lt;span&gt;Applying: Setup unit tests
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;After the rebase you should be good to go when pushing again.&lt;&#x2F;p&gt;
&lt;p&gt;Running a &lt;code&gt;git pull&lt;&#x2F;code&gt; should work as well, but I don&#x27;t like the additional merge commits that are included in that approach.&lt;&#x2F;p&gt;
&lt;p&gt;You could take another route and not directly push, but create a new branch and make a pull request. This requires you to push your changes to a new remote branch first and then create the pull request.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Amend to git commit without changing the commit message</title>
		<published>2022-04-16T00:00:00+00:00</published>
		<updated>2022-04-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-amend-to-commit-without-changing-message/" type="text/html"/>
		<id>https://koenwoortman.com/git-amend-to-commit-without-changing-message/</id>
		<content type="html">&lt;p&gt;While coding I’m quite often adding changes to a local (work-in-progress) commit. Although it is not that problematic to re-save the current message it can become an annoyance when repeated enough times.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily you can skip the step of editing the commit message when amending changes to a git commit. You can skip editing the commit message by using the &lt;code&gt;--no-edit&lt;&#x2F;code&gt; flag:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git commit --amend --no-edit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can combine the above command with a &lt;code&gt;git add .&lt;&#x2F;code&gt; to add &lt;strong&gt;all&lt;&#x2F;strong&gt; your current changes directly to the last commit you have made in your repository.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git add . &amp;amp;&amp;amp; git commit --amend --no-edit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;make-it-an-alias&quot;&gt;Make it an alias&lt;&#x2F;h2&gt;
&lt;p&gt;Alright, that allows for skipping the commit message when amending to a git commit but typing that out doesn&#x27;t save us time :). Maybe a search in previously run commands will do. But you can also choose to either make a git alias or an alias in your terminal shell.&lt;&#x2F;p&gt;
&lt;p&gt;In order to make a git alias you need to make a change to your git config, you can either do that via a command or by opening the git config file. Via the command line it would be like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git config --global alias.amend-no-edit &amp;#39;commit --amend --no-edit&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The other option would be to add an alias directly to your git config using a text editor. This file can be located in &lt;a href=&quot;https:&#x2F;&#x2F;git-scm.com&#x2F;book&#x2F;en&#x2F;v2&#x2F;Getting-Started-First-Time-Git-Setup&quot;&gt;different locations&lt;&#x2F;a&gt;, also depending on your operating system.&lt;&#x2F;p&gt;
&lt;p&gt;Adding a git alias to your git config would look something like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;~ % cat ~&#x2F;.gitconfig
&lt;&#x2F;span&gt;&lt;span&gt;[user]
&lt;&#x2F;span&gt;&lt;span&gt;        name = …
&lt;&#x2F;span&gt;&lt;span&gt;        email = …
&lt;&#x2F;span&gt;&lt;span&gt;[alias]
&lt;&#x2F;span&gt;&lt;span&gt;        amend-no-edit = commit --amend --no-edit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Obviously you can choose something other than &lt;code&gt;amend-no-edit&lt;&#x2F;code&gt;. What matters here is that both options allow you to run the command as:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git amend-no-edit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option would be to add a shell alias instead of a git alias. The above requires you to still use the &lt;code&gt;git&lt;&#x2F;code&gt; command, using a shell alias you can also get rid of that. In order to add a shell alias you need to add the example below to your shell&#x27;s config file, which config file depends on the shell you are using (bash, zsh, fish, etc.)&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;alias &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;amend&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;git commit --amend --no-edit&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By adding the above to your shell&#x27;s config file you can run the&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ amend
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Print the current year in a Vue template</title>
		<published>2022-04-15T00:00:00+00:00</published>
		<updated>2022-04-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-print-current-year-in-template/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-print-current-year-in-template/</id>
		<content type="html">&lt;p&gt;Printing the current year can be useful in for example a copyright statement in your website footer. Rendering the current year in your Vue.js template doesn&#x27;t require much more than calling a plain JavaScript function.&lt;&#x2F;p&gt;
&lt;p&gt;In the example below we render the current year by calling a computed property from our template.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;footer&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;© {{ currentYear }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;footer&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    computed: {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;currentYear&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return new &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Date&lt;&#x2F;span&gt;&lt;span&gt;().getFullYear();
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;Date&lt;&#x2F;code&gt; object we creates a date representation using the date and time that is configured locally in the user&#x27;s browser taking its timezone into account.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Fix `install requested operation requires elevation` when installing WSL</title>
		<published>2022-04-14T00:00:00+00:00</published>
		<updated>2022-04-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/wsl-install-requested-operation-requires-elevation/" type="text/html"/>
		<id>https://koenwoortman.com/wsl-install-requested-operation-requires-elevation/</id>
		<content type="html">&lt;p&gt;Installing Windows Subsystem for Linux requires you to run the command &lt;code&gt;wsl --install&lt;&#x2F;code&gt; in the command prompt or Windows PowerShell. However, such an operation requires special permissions. Like you need &lt;code&gt;sudo&lt;&#x2F;code&gt; for certain commands in Linux. In Windows you don’t obtain these permissions by using a command like &lt;code&gt;sudo&lt;&#x2F;code&gt;. Instead you should run your terminal program as administrator.&lt;&#x2F;p&gt;
&lt;p&gt;Running a program, the Windows PowerShell in my case, as Administrator can be done by a right-mouse-click on the program icon.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;wsl-run-powershell-as-administrator.png&quot; alt=&quot;Run PowerShell as Administrator&quot; title=&quot;Run PowerShell as Administrator&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Generate only a package-lock.json file with npm</title>
		<published>2022-04-13T00:00:00+00:00</published>
		<updated>2022-04-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/npm-generate-package-lock-file-only/" type="text/html"/>
		<id>https://koenwoortman.com/npm-generate-package-lock-file-only/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;package-lock.json&lt;&#x2F;code&gt; file is a useful tool for installing &lt;a href=&quot;&#x2F;npm-install-from-package-lock&#x2F;&quot;&gt;packages in a continuous integration environment&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Normally the &lt;code&gt;package-lock.json&lt;&#x2F;code&gt; file gets updated whenever you run &lt;code&gt;npm install&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you wish to locally generate a new &lt;code&gt;package-lock.json&lt;&#x2F;code&gt; without installing or updating the dependencies you can use a special option to the &lt;code&gt;npm install&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;Since &lt;code&gt;npm&lt;&#x2F;code&gt; version 6, don’t confuse this with your node.js version, you can use the &lt;code&gt;--package-lock-only&lt;&#x2F;code&gt; option to create a new lock file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npm install --package-lock-only
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using this option won&#x27;t install new node modules, even though using the &lt;code&gt;install&lt;&#x2F;code&gt; subcommand does suggest so.&lt;&#x2F;p&gt;
&lt;p&gt;To find out which &lt;code&gt;npm&lt;&#x2F;code&gt; version you are using you can use:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npm --version
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Pass multiple arguments when calling a Vuex action</title>
		<published>2022-04-12T00:00:00+00:00</published>
		<updated>2022-04-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-vuex-action-pass-multiple-parameters/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-vuex-action-pass-multiple-parameters/</id>
		<content type="html">&lt;p&gt;At some point you will want to pass more than one argument to a Vuex action. However, adding more arguments to your action is not the way to go in Vuex. Instead you should wrap all the data you wish to pass in a single payload. Thus, what you probably want to use is a JavaScript object.&lt;&#x2F;p&gt;
&lt;p&gt;See the example below, where a username and password are to be passed to the &lt;code&gt;authenticate&lt;&#x2F;code&gt; Vuex action. Both username and password should be wrapped in a JavaScript object, this object can be passed as one argument to the Vuex action.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ mapActions } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;vuex&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;      username: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      password: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    };
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  methods: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mapActions&lt;&#x2F;span&gt;&lt;span&gt;([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;authenticate&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]),
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;logIn&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;payload &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        username: this.username,
&lt;&#x2F;span&gt;&lt;span&gt;        password: this.password,
&lt;&#x2F;span&gt;&lt;span&gt;      };
&lt;&#x2F;span&gt;&lt;span&gt;      this.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;authenticate&lt;&#x2F;span&gt;&lt;span&gt;(payload);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In your Vuex action you can access the &lt;code&gt;username&lt;&#x2F;code&gt; and &lt;code&gt;password&lt;&#x2F;code&gt; as how you would access any property of a regular JavaScript object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;authenticate&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;payload&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      api.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;post&lt;&#x2F;span&gt;&lt;span&gt;({
&lt;&#x2F;span&gt;&lt;span&gt;        username: payload.username,
&lt;&#x2F;span&gt;&lt;span&gt;        password: payload.password,
&lt;&#x2F;span&gt;&lt;span&gt;      });
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you prefer to do so you can use JavaScript Object Destructuring to clean up the action a bit.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;authenticate&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;, { &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;username&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      api.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;post&lt;&#x2F;span&gt;&lt;span&gt;({ username, password });
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Dispatch an action in another Vuex module</title>
		<published>2022-04-11T00:00:00+00:00</published>
		<updated>2022-04-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-vuex-dispatch-action-in-other-module/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-vuex-dispatch-action-in-other-module/</id>
		<content type="html">&lt;p&gt;Splitting up your Vuex stores in different modules can help a lot to make sense of a larger Vue codebase. But every now and then you need a way to communicate between those modules, dispatching actions is the way to go. However, dispatching a Vuex action in a different module does require you to pass an extra argument.&lt;&#x2F;p&gt;
&lt;p&gt;In order to call an action in a different Vuex module you need to pass &lt;code&gt;{root: true}&lt;&#x2F;code&gt; as the &lt;strong&gt;third&lt;&#x2F;strong&gt; argument of the &lt;code&gt;dispatch()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;p&gt;See the following example that dispatches the &lt;code&gt;add&lt;&#x2F;code&gt; action in the &lt;code&gt;toppings&lt;&#x2F;code&gt; module:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;prepareSalamiPizza&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;dispatch &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;dispatch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;toppings&#x2F;add&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;salami&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, { root: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Do keep in mind that &lt;code&gt;{root: true}&lt;&#x2F;code&gt; should always be the third argument in &lt;code&gt;dispatch()&lt;&#x2F;code&gt; even if the action you call doesn’t require a payload. In such a case it is up to you what you pass as the second argument, or payload, to &lt;code&gt;dispatch()&lt;&#x2F;code&gt;. A sensible option, in my opinion, would be &lt;code&gt;null&lt;&#x2F;code&gt; or an empty object(&lt;code&gt;{}&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Install npm packages from package-lock file</title>
		<published>2022-04-10T00:00:00+00:00</published>
		<updated>2022-04-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/npm-install-from-package-lock/" type="text/html"/>
		<id>https://koenwoortman.com/npm-install-from-package-lock/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;yarn&lt;&#x2F;code&gt; package manager for node modules has the &lt;code&gt;--frozen-lockfile&lt;&#x2F;code&gt; option for its &lt;code&gt;install&lt;&#x2F;code&gt; command. Using this option will ensure that the exact package versions are installed as they are defined in the &lt;code&gt;yarn.lock&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;Just like &lt;code&gt;yarn&lt;&#x2F;code&gt; with its &lt;code&gt;yarn.lock&lt;&#x2F;code&gt; file, &lt;code&gt;npm&lt;&#x2F;code&gt; allows you to install the packages as defined in the &lt;code&gt;package-lock.json&lt;&#x2F;code&gt; file with the &lt;code&gt;ci&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ npm ci
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the name of the subcommand is confusing to you; &lt;code&gt;ci&lt;&#x2F;code&gt; is short for continuous integration. This since it was introduced to increase the performance and reliability of builds in a continuous integration (and continuous deployment) process.&lt;&#x2F;p&gt;
&lt;p&gt;Using either &lt;code&gt;npm ci&lt;&#x2F;code&gt; or &lt;code&gt;yarn&lt;&#x2F;code&gt;&#x27;s &lt;code&gt;--frozen-lockfile&lt;&#x2F;code&gt; option is useful when installing dependencies in production or in a continuous integration environment because it guarantees that you will get the same versions of packages that you used in local development. This makes it less likely that you encounter new bugs in your production builds.&lt;&#x2F;p&gt;
&lt;p&gt;The performance boost is due to the fact that the &lt;code&gt;ci&lt;&#x2F;code&gt; command bypasses the package.json file and no version resolutions are required: the &lt;code&gt;package-lock.json&lt;&#x2F;code&gt; defines the exact required packages.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Call Vuex action from another action</title>
		<published>2022-04-09T00:00:00+00:00</published>
		<updated>2022-04-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-vuex-call-action-from-another-action/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-vuex-call-action-from-another-action/</id>
		<content type="html">&lt;p&gt;Calling a vuex action from another action requires the use of a special function: &lt;code&gt;dispatch&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;dispatch&lt;&#x2F;code&gt; function is available from the &lt;code&gt;context&lt;&#x2F;code&gt; that is passed as a first argument to your Vuex actions. You can call another Vuex action by passing the name of that action as a string as the first argument of &lt;code&gt;dispatch&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;walk&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      context.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;dispatch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;goForward&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;goForward&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or you can use JavaScript Object Destructuring to access &lt;code&gt;dispatch&lt;&#x2F;code&gt; directly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;walk&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;dispatch &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;dispatch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;goForward&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;goForward&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;dispatch-with-arguments&quot;&gt;Dispatch with arguments&lt;&#x2F;h2&gt;
&lt;p&gt;Arguments to the Vuex action you wish to call can be passed as second argument to dispatch:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;walk&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;dispatch &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;speed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;4km&#x2F;h&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;dispatch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;goForward&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, speed);
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;goForward&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;context&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;payload&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; The value of the payload is: &amp;#39;4km&#x2F;h&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;All that the arguments that you wish to pass to another action should be in the payload. Vuex doesn&#x27;t allow you to add more arguments, instead you can wrap the other data in a JavaScript object.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;dispatch-in-other-module&quot;&gt;Dispatch in other module&lt;&#x2F;h2&gt;
&lt;p&gt;If you are working with Vuex modules, you may at some point want to call an action in another module. In that case you need to pass &lt;code&gt;{ root: true }&lt;&#x2F;code&gt; as a 3rd argument to &lt;code&gt;dispatch&lt;&#x2F;code&gt;. Order matters here, so even if the Vuex action you wish to call doesn&#x27;t require a payload you still need to pass something (an empty object or &lt;code&gt;null&lt;&#x2F;code&gt; will do).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;store &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= new &lt;&#x2F;span&gt;&lt;span&gt;Vuex.Store({
&lt;&#x2F;span&gt;&lt;span&gt;  actions: {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;walk&lt;&#x2F;span&gt;&lt;span&gt;({ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;dispatch &lt;&#x2F;span&gt;&lt;span&gt;}) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;speed &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;4km&#x2F;h&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;dispatch&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;movement&#x2F;goForward&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, speed, { root: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Difference between v-if and v-show in Vue.js</title>
		<published>2022-04-08T00:00:00+00:00</published>
		<updated>2022-04-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-difference-between-v-if-and-v-show/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-difference-between-v-if-and-v-show/</id>
		<content type="html">&lt;p&gt;To manipulate your Vue template you use Vue directives; special HTML attributes which are processed by Vue. Now in order to conditionally show or hide parts of your template there are two options which function similarly at first glance: &lt;code&gt;v-if&lt;&#x2F;code&gt; and &lt;code&gt;v-show&lt;&#x2F;code&gt;. Functionally they work practically the same, however the difference between the &lt;code&gt;v-if&lt;&#x2F;code&gt; and &lt;code&gt;v-show&lt;&#x2F;code&gt; directives is behind the scenes.&lt;&#x2F;p&gt;
&lt;p&gt;The difference between the two is in how the hidden parts of your template are rendered. Which ultimately determines which directive you should use in a certain use-case.&lt;&#x2F;p&gt;
&lt;p&gt;They differ as follows:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;v-if&lt;&#x2F;strong&gt; completely removes the part of your template to conditionally hide from your final HTML code. Once the condition evaluates to true, Vue adds the part of your template that should be conditionally shown to your HTML code.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;v-show&lt;&#x2F;strong&gt; on the other hand will always add the part that is conditionally hidden to your DOM. Based on the passed condition the CSS styling &lt;code&gt;display: none;&lt;&#x2F;code&gt; is added to the element. Once the condition evaluates to true the CSS styling is removed from the element.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore the weigh-off is often in how you expect the conditional part of your template to change during the time it is mounted. For &lt;code&gt;v-show&lt;&#x2F;code&gt; there is a penalty in the initial rendering time. Vue will always render the conditional part of your template, even if it is directly hidden using CSS. For &lt;code&gt;v-if&lt;&#x2F;code&gt; you pay the price whenever the condition changes, in such a case Vue removes or adds HTML to your DOM which is more expensive than toggling a CSS property.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;So in short;&lt;&#x2F;strong&gt; if you expect the condition to change (often) while the component is mounted use &lt;code&gt;v-show&lt;&#x2F;code&gt;. If you either show or hide parts of your template based on a (fairly) static props value for example use &lt;code&gt;v-if&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What are `falsy` values in JavaScript</title>
		<published>2022-04-07T00:00:00+00:00</published>
		<updated>2022-04-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-what-are-falsy-values/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-what-are-falsy-values/</id>
		<content type="html">&lt;p&gt;While learning JavaScript you may have come across something like &amp;quot;value X is falsy&amp;quot;. This relates to how the value is evaluated in a Boolean context, which is mainly in if-statements but also when &lt;a href=&quot;&#x2F;javascript-convert-value-to-boolean&#x2F;&quot;&gt;casting to a boolean value&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The JavaScript values that evaluate to &lt;code&gt;false&lt;&#x2F;code&gt; in a Boolean context are:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Value&lt;&#x2F;th&gt;&lt;th&gt;Type&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;false&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Boolean&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;&#x27;&#x27;&lt;&#x2F;code&gt;, &lt;code&gt;&amp;quot;&amp;quot;&lt;&#x2F;code&gt;, &lt;code&gt;``&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;String&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Number&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-0&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Number&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;NaN&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Number&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;0n&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;BigInt&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;null&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Object&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;undefined&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Undefined&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;document.all&lt;&#x2F;code&gt; (Don&#x27;t ask me why)&lt;&#x2F;td&gt;&lt;td&gt;HTMLAllCollection&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Notice here that empty arrays and empty objects are not considered falsy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;if-there-is-falsy-what-about-truthy&quot;&gt;If there is falsy, what about truthy&lt;&#x2F;h2&gt;
&lt;p&gt;Obviously when there are falsy values, there must be truthy values too. Which, logically, are values that evaluate to &lt;code&gt;true&lt;&#x2F;code&gt; in a Boolean context. These truthy values are just all the other values which are not considered falsy.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Casting values to booleans in JavaScript</title>
		<published>2022-04-06T00:00:00+00:00</published>
		<updated>2022-04-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-convert-value-to-boolean/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-convert-value-to-boolean/</id>
		<content type="html">&lt;p&gt;Converting values to a different type in JavaScript might work a little different than expected if you come from another programming language. JavaScript doesn’t have a specific syntax for type casting, which is present in a language like PHP to name one.&lt;&#x2F;p&gt;
&lt;p&gt;Instead you instance an object of your preferred type and pass the value to cast as an argument. So in the case of booleans you would write something similar the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hello&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;ball &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That will convert all values to &lt;code&gt;true&lt;&#x2F;code&gt; expect for &lt;a href=&quot;&#x2F;javascript-what-are-falsy-values&#x2F;&quot;&gt;JavaScript&#x27;s falsy-values&lt;&#x2F;a&gt;. With the falsy-values being: &lt;code&gt;undefined&lt;&#x2F;code&gt;, &lt;code&gt;null&lt;&#x2F;code&gt;, &lt;code&gt;false&lt;&#x2F;code&gt;, &lt;code&gt;0&lt;&#x2F;code&gt;, &lt;code&gt;-0&lt;&#x2F;code&gt;, &lt;code&gt;0n&lt;&#x2F;code&gt;, &lt;code&gt;NaN&lt;&#x2F;code&gt; and &lt;code&gt;&amp;quot;&amp;quot;&lt;&#x2F;code&gt;. Note here that an empty array or empty object are not considered falsy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;double-negate&quot;&gt;Double-negate&lt;&#x2F;h2&gt;
&lt;p&gt;While using &lt;code&gt;Boolean()&lt;&#x2F;code&gt; is at least super explicit there is a shorter syntax too which might has your preference. And that is using a double negation (&lt;code&gt;!!&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;bool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= !!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hello&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;ball &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= !!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The logic behind the double negation is that using negate once (&lt;code&gt;!&lt;&#x2F;code&gt;) will evaluate the following expression to a boolean value, but also negates it. Negating that result, which is already a boolean value, undoes the first negation.&lt;&#x2F;p&gt;
&lt;p&gt;See if you can crack the following, if you do you should be good to go when it comes to using double negation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Boolean&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; false
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!!&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solve `&#x27;v-if&#x27; should be moved to the wrapper element` in Vue.js</title>
		<published>2022-04-05T00:00:00+00:00</published>
		<updated>2022-04-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-v-if-should-be-moved-to-wrapper-element/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-v-if-should-be-moved-to-wrapper-element/</id>
		<content type="html">&lt;p&gt;When you use the eslint-plugin-vue package, which is the official ESLint plugin for Vue, to format your Vue code you will encounter some warnings and errors here and there. Such as the following one:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;This &amp;#39;v-if&amp;#39; should be moved to the wrapper element.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This error or warning occurs whenever you use both the &lt;code&gt;v-for&lt;&#x2F;code&gt; and &lt;code&gt;v-if&lt;&#x2F;code&gt; directive on the same HTML element. You might do so whenever you prefer to hide or not render the list based on a certain condition. As the warning suggest, the preferred way of doing this is by moving dat conditional to HTML element that contains the list with the &lt;code&gt;v-for&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So instead of:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- Vue ESLint doesn&amp;#39;t like this --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;item in items&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;showItems&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{{ item }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The ESLint rule prefers this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;lt;!-- And prefers you to do this --&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;showItems&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;item in items&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;      &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{{ item }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using &lt;code&gt;v-show&lt;&#x2F;code&gt; instead of &lt;code&gt;v-if&lt;&#x2F;code&gt; will not cause this warning as well. They do have different performance penalties you should be aware of.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;item in items&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;v-show&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;showItems&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;{{ item }}&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can disable the &lt;code&gt;[vue&#x2F;no-use-v-if-with-v-for]&lt;&#x2F;code&gt; rule all together by adding the following to the &lt;code&gt;rules&lt;&#x2F;code&gt; section of you ESLint config file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;js&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-js &quot;&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vue&#x2F;no-use-v-if-with-v-for&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;off&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For more information, read the &lt;a href=&quot;https:&#x2F;&#x2F;eslint.vuejs.org&#x2F;rules&#x2F;no-use-v-if-with-v-for.html&quot;&gt;docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use `await` in Vue components with async methods</title>
		<published>2022-04-04T00:00:00+00:00</published>
		<updated>2022-04-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-async-and-await-in-methods/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-async-and-await-in-methods/</id>
		<content type="html">&lt;p&gt;The already not-so-recent-anymore ES7 changes to JavaScript introduced the &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt; keywords. They allow for some syntactic sugar around JavaScript Promises.&lt;&#x2F;p&gt;
&lt;p&gt;They can make asynchronous code more easy to read, and therefore worthy to look into for your Vue.js methods.&lt;&#x2F;p&gt;
&lt;p&gt;You can use &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt; for your lifecycle hooks, methods and watcher functions. One exception here is computed properties, which don’t allow the use of &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In order to use &lt;code&gt;await&lt;&#x2F;code&gt; in the methods of you Vue component you prefix the method name with the &lt;code&gt;async&lt;&#x2F;code&gt; keyword. See the following example, where you see custom async methods as well as async lifecycle hooks such as &lt;code&gt;mounted&lt;&#x2F;code&gt; and &lt;code&gt;created&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;data&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        token: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;undefined&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        user: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;undefined&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;      };
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;created&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      this.token &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;fetchUser();
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mounted&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      this.user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;this.fetchUser();
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    methods: {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;getToken&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;token &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Cookie&lt;&#x2F;span&gt;&lt;span&gt;.read(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;token&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;token;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fetchUser&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;api.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;user&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, { token: this.token });
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;user;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;browser-support&quot;&gt;Browser support&lt;&#x2F;h2&gt;
&lt;p&gt;Although the &lt;code&gt;async&lt;&#x2F;code&gt; and &lt;code&gt;await&lt;&#x2F;code&gt; should be available in all recently updated modern browsers, you might want to double check the &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;JavaScript&#x2F;Reference&#x2F;Statements&#x2F;async_function#browser_compatibility&quot;&gt;browser support&lt;&#x2F;a&gt; just in case.&lt;&#x2F;p&gt;
&lt;p&gt;If you need to support Internet Explorer in you Vue app than using a JavaScript transpiler like &lt;a href=&quot;https:&#x2F;&#x2F;babeljs.io&#x2F;&quot;&gt;Babel&lt;&#x2F;a&gt; will allow you to use &lt;code&gt;async&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;await&lt;&#x2F;code&gt; in your Vue methods.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Pass data to components in Vue.js with props</title>
		<published>2022-04-03T00:00:00+00:00</published>
		<updated>2022-04-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-pass-data-to-component/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-pass-data-to-component/</id>
		<content type="html">&lt;p&gt;Once you start to split up your Vue application in multiple components, you will at some point want to share variable values from a component to one of its child components. Probably the most straightforward way of passing data to child components in your Vue application is by using component props.&lt;&#x2F;p&gt;
&lt;p&gt;Before you start with props it is good to understand that they have a one-way dataflow, changes are only propagated from parent to childs.&lt;&#x2F;p&gt;
&lt;p&gt;Whenever you try to change the value of a prop in a child component those changes are not applied to the parent component. To reason from the other direction, whenever props change in the parent component, changes to props in a child component are not persisted. So you are better off to &lt;strong&gt;treat props as read-only data&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you do need reactivity there, you can potentially use props to set an initial value for a data property of your child component.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;define-props-using-the-options-api&quot;&gt;Define props using the options API&lt;&#x2F;h2&gt;
&lt;p&gt;In the child component you need to define the props under the &lt;code&gt;props&lt;&#x2F;code&gt; key in your script section.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ChildComponent.vue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    props: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;name&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;    computed: {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;welcome&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`Hello &lt;&#x2F;span&gt;&lt;span&gt;${this.name}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From the parent component you can set the props value of the child component by using attributes. You can either set a hardcoded value or bind a value using &lt;code&gt;:&lt;&#x2F;code&gt;, the shorthand of &lt;code&gt;v-bind&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;child-component &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;:name&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;userName&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;child-component &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Koen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The props are directly available under &lt;code&gt;this&lt;&#x2F;code&gt; in your child component, so in the above example we can access the name using &lt;code&gt;this.name&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;define-props-using-the-composition-api&quot;&gt;Define props using the composition API&lt;&#x2F;h2&gt;
&lt;p&gt;Using the composition API which is new in Vue 3 you can pass the props to as the first argument to you &lt;code&gt;setup()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ChildComponent.vue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    props: [&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;name&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;],
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;setup&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;props&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you use a setup script (&lt;code&gt;&amp;lt;script setup&amp;gt;&lt;&#x2F;code&gt;) in single file components you can make use of the &lt;code&gt;defineProps()&lt;&#x2F;code&gt; function to define your component props.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;setup&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ChildComponent.vue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;props &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;defineProps([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;name&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;typed-props&quot;&gt;Typed props&lt;&#x2F;h2&gt;
&lt;p&gt;Besides defining props as an array of strings there is an object notation available as well. Which allows to define the property type as well. Your options for the types are &lt;code&gt;String&lt;&#x2F;code&gt;, &lt;code&gt;Number&lt;&#x2F;code&gt;, &lt;code&gt;Boolean&lt;&#x2F;code&gt;, &lt;code&gt;Array&lt;&#x2F;code&gt;, &lt;code&gt;Object&lt;&#x2F;code&gt; and &lt;code&gt;Function&lt;&#x2F;code&gt;. This type is not in any way enforced by Vue, so passing a number while a string is expected will not break your component per-se. However, it will log a friendly warning to the developer console in your browser. Which may helps to spot errors early on.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ChildComponent.vue
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    props: {
&lt;&#x2F;span&gt;&lt;span&gt;      name: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    computed: {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;welcome&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`Hello &lt;&#x2F;span&gt;&lt;span&gt;${this.name}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;      },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;vuex-or-pinia-global-state&quot;&gt;Vuex or Pinia Global state&lt;&#x2F;h2&gt;
&lt;p&gt;Sharing data with child components is useful. But it can get messy pretty quick as well. Imagine sharing data using props to a child component, which in turn shares it with one of its child components, and that child component shares it with another child component which eventually will be the one that displays that value.&lt;&#x2F;p&gt;
&lt;p&gt;For such a case you are better off using an alternative to component props, that alternative should probably be a global state management library like &lt;a href=&quot;https:&#x2F;&#x2F;vuex.vuejs.org&#x2F;&quot;&gt;Vuex&lt;&#x2F;a&gt; or &lt;a href=&quot;https:&#x2F;&#x2F;pinia.vuejs.org&#x2F;&quot;&gt;Pinia&lt;&#x2F;a&gt;. Both libraries allow you to share reactive data of your application across all components. Whenever such a global state item changes value, Vue takes care of the re-rendering of components automatically for you.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get an element by its ID using Vue.js</title>
		<published>2022-04-02T00:00:00+00:00</published>
		<updated>2022-04-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vuejs-get-element-by-id-in-component/" type="text/html"/>
		<id>https://koenwoortman.com/vuejs-get-element-by-id-in-component/</id>
		<content type="html">&lt;p&gt;With plain JavaScript you find yourself regularly jumping into the DOM, to get the attributes of an element, add event handlers and other operations. For these actions there is a good chance you would have used the good old JavaScript function &lt;code&gt;document.getElementById&lt;&#x2F;code&gt;, which would hand you back the HTML element you wish to manipulate.&lt;&#x2F;p&gt;
&lt;p&gt;The Vue framework, luckily, hides most of the DOM operations behind a friendly API. Instead of needing to call &lt;code&gt;addEventListener(&amp;quot;click&amp;quot;, function () {...}&lt;&#x2F;code&gt;, you can bind an event directly to an HTML element using &lt;code&gt;v-on:click=”...”&lt;&#x2F;code&gt; or its shorthand notation &lt;code&gt;@click=”...”&lt;&#x2F;code&gt;. Or when you need to access a value of a form input, you can easily set up a data property in your Vue component and bind it to the form input using &lt;code&gt;v-model&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;However, once in a while I do need to access DOM elements directly. When I need to get the dimensions in pixels of an element for example. For such a use-case you &lt;em&gt;can&lt;&#x2F;em&gt; fall back on using the native JavaScript &lt;code&gt;document.getElementById&lt;&#x2F;code&gt; function again. But Vue has something built-in for such a use-case as well, namely &lt;strong&gt;template refs&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;defining-template-refs&quot;&gt;Defining template refs&lt;&#x2F;h2&gt;
&lt;p&gt;A template ref, short for reference I assume, allows you to easily access an HTML element in your Vue code. A template ref is created by adding the special &lt;code&gt;ref=”...”&lt;&#x2F;code&gt; attribute to an element in your template.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;img &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;picture&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example.png&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, once the component is &lt;strong&gt;mounted&lt;&#x2F;strong&gt; we can access our &lt;code&gt;img&lt;&#x2F;code&gt; element using the &lt;code&gt;picture&lt;&#x2F;code&gt; reference. But how to do so depends on whether using the options API or the new composition API which was introduced in Vue 3.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;accessing-refs-using-the-options-api&quot;&gt;Accessing refs using the options API&lt;&#x2F;h2&gt;
&lt;p&gt;In the options API your template references are accessible as a property of &lt;code&gt;this.$refs&lt;&#x2F;code&gt;. So in our example we can example we can access the &lt;code&gt;img&lt;&#x2F;code&gt; element via &lt;code&gt;this.$refs.picture&lt;&#x2F;code&gt;. Do keep in mind that this element only exists after the component in mounted, so try to access the &lt;code&gt;picture&lt;&#x2F;code&gt; ref in the &lt;code&gt;created&lt;&#x2F;code&gt; lifecycle hook will return &lt;code&gt;null&lt;&#x2F;code&gt; instead of the HTML element.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;img &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;picture&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example.png&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export default &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mounted&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;height &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;this.$refs.picture.clientHeight;
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  };
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;accessing-refs-using-the-composition-api&quot;&gt;Accessing refs using the composition API&lt;&#x2F;h2&gt;
&lt;p&gt;The composition API obviously allows for accessing template refs as well. However, you need to pay a bit more attention to detail here. In your &lt;code&gt;setup&lt;&#x2F;code&gt; script, or &lt;code&gt;setup()&lt;&#x2F;code&gt; function if you prefer that, you need to define a variable named the same as your ref and assign it to &lt;code&gt;ref(null)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;After the component is mounted, you can access the ref using the variable you defined. Where this differs from the options API is that your HTML element lives under &lt;code&gt;&amp;lt;ref&amp;gt;.value&lt;&#x2F;code&gt; instead of directly under &lt;code&gt;&amp;lt;ref&amp;gt;&lt;&#x2F;code&gt;. In our example the HTML element itself can be accessed as &lt;code&gt;picture.value&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;img &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;ref&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;picture&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;src&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example.png&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;&#x2F;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;setup&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;{ ref, onMounted } &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;vue&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; IMPORTANT: Variable should be named the same as the ref.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;picture &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;ref(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;null&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  onMounted(() &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;height &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;picture.value.clientHeight;
&lt;&#x2F;span&gt;&lt;span&gt;  });
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;script&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>String casing in PHP</title>
		<published>2021-12-21T00:00:00+00:00</published>
		<updated>2021-12-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-string-casing/" type="text/html"/>
		<id>https://koenwoortman.com/php-string-casing/</id>
		<content type="html">&lt;h2 id=&quot;lowercase-the-whole-string&quot;&gt;Lowercase the whole string&lt;&#x2F;h2&gt;
&lt;p&gt;In order to lowercase a PHP string you need the &lt;code&gt;strtolower()&lt;&#x2F;code&gt; function. It takes a string as a parameter and returns the lowercase result of the string passed as a parameter.&lt;&#x2F;p&gt;
&lt;p&gt;See the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strtolower&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;The Netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; the netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The result depends on the locale that is set, in the default English locale that is set in &amp;quot;C&amp;quot; some special characters are not lowercased such as the &amp;quot;Ä&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;See the docs: &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;function.strtolower.php&quot;&gt;strtolower&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;uppercase-the-whole-string&quot;&gt;Uppercase the whole string&lt;&#x2F;h2&gt;
&lt;p&gt;To uppercase an entire string you need the PHP function &lt;code&gt;strtoupper()&lt;&#x2F;code&gt;. It takes a string as a parameter and returns the lowercase result of the string passed as a parameter.&lt;&#x2F;p&gt;
&lt;p&gt;See the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strtoupper&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;The Netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; THE NETHERLANDS
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;See the docs: &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;function.strtoupper.php&quot;&gt;strtoupper&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;uppercase-the-first-letter-of-a-string&quot;&gt;Uppercase the first letter of a string&lt;&#x2F;h2&gt;
&lt;p&gt;Besides functions that work on the entire string PHP provides functions that work on just a substring of the string that is passed as a parameter, one of them is the &lt;code&gt;ucfirst()&lt;&#x2F;code&gt; function (short for uppercase the first letter).&lt;&#x2F;p&gt;
&lt;p&gt;See the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucfirst&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;the netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should keep in mind that this function only changes the case of the first letter in a string. So take a look at the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucfirst&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;THE NETHERLANDS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; THE NETHERLANDS
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So in order to only have the first letter uppercased it might be preferable, depending on the strings you expect, to combine &lt;code&gt;ucfirst()&lt;&#x2F;code&gt; with &lt;code&gt;strtolower()&lt;&#x2F;code&gt; as the following example demonstrates:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucfirst&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strtolower&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;THE NETHERLANDS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;See the docs: &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;function.ucfirst.php&quot;&gt;ucfirst&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;h2 id=&quot;uppercase-the-first-letter-of-each-word&quot;&gt;Uppercase the first letter of each word&lt;&#x2F;h2&gt;
&lt;p&gt;Besides upper casing the first letter of a string you can uppercase the first character of each word in a string with &lt;code&gt;ucwords()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucwords&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;the netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The Netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just as with &lt;code&gt;ucfirst()&lt;&#x2F;code&gt; only the casing of the first letters are changed. Other letters that are already uppercased maintain their casing. So in order to only have the first letter uppercased it might be safer to combine &lt;code&gt;ucwords()&lt;&#x2F;code&gt; with &lt;code&gt;strtolower()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucwords&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strtolower&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;THE NETHERLANDS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The Netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What the ucwords function considers words is determined by the value of a second parameter to this function. This second parameter sets the characters that are considered word separators, meaning that the characters in a string &lt;em&gt;after&lt;&#x2F;em&gt; such a character are considered to be a different word.&lt;&#x2F;p&gt;
&lt;p&gt;The default value of this separator parameter is set to &lt;code&gt;&amp;quot; \t\r\n\f\v&amp;quot;&lt;&#x2F;code&gt;, notice the space in there as well. The other values are in order the escape sequences of the: horizontal tab, carriage return, newline, form-feed and vertical tab.&lt;&#x2F;p&gt;
&lt;p&gt;We can overwrite this second parameter as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucwords&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;the-netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The-netherlands
&lt;&#x2F;span&gt;&lt;span&gt;$nl &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;ucwords&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;the-netherlands&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$nl;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;-&amp;gt; The-Netherlands
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;See the docs: &lt;a href=&quot;https:&#x2F;&#x2F;www.php.net&#x2F;manual&#x2F;en&#x2F;function.ucwords.php&quot;&gt;ucwords&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving `cannot assign twice to immutable variable` in Rust</title>
		<published>2021-12-20T00:00:00+00:00</published>
		<updated>2021-12-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/rust-cannot-assign-twice-to-immutable-variable/" type="text/html"/>
		<id>https://koenwoortman.com/rust-cannot-assign-twice-to-immutable-variable/</id>
		<content type="html">&lt;p&gt;If you encountered the error &lt;code&gt;cannot assign twice to immutable variable&lt;&#x2F;code&gt; you most likely tried to reassign a value to an immutable variable. Variables in Rust are by default immutable, meaning that the variable only gets a value assigned at the time that the variable is declared.&lt;&#x2F;p&gt;
&lt;p&gt;Consider the following main function for example, in which we create variable &lt;code&gt;a&lt;&#x2F;code&gt; and set its value to &lt;code&gt;0&lt;&#x2F;code&gt;. On the next line we try to set the value of &lt;code&gt;a&lt;&#x2F;code&gt; to &lt;code&gt;1&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This code spawns the following error:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;error[E0384]: cannot assign twice to immutable variable `a`
&lt;&#x2F;span&gt;&lt;span&gt; --&amp;gt; src&#x2F;main.rs:3:5
&lt;&#x2F;span&gt;&lt;span&gt;  |
&lt;&#x2F;span&gt;&lt;span&gt;2 |     let a = 0;
&lt;&#x2F;span&gt;&lt;span&gt;  |         -
&lt;&#x2F;span&gt;&lt;span&gt;  |         |
&lt;&#x2F;span&gt;&lt;span&gt;  |         first assignment to `a`
&lt;&#x2F;span&gt;&lt;span&gt;  |         help: consider making this binding mutable: `mut a`
&lt;&#x2F;span&gt;&lt;span&gt;3 |     a = 1;
&lt;&#x2F;span&gt;&lt;span&gt;  |     ^^^^^ cannot assign twice to immutable variable
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;For more information about this error, try `rustc --explain E0384`.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to fix this problem we have two options available:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Make the variable mutable.&lt;&#x2F;li&gt;
&lt;li&gt;Rewrite the code so that you don’t need to reassign a value to an immutable variable.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Let&#x27;s stick to the first option for now; declaring a mutable variable. Which is also the suggestion given to us by the Rust compiler. In order to declare a mutable variable in Rust you use the &lt;code&gt;mut&lt;&#x2F;code&gt; keyword after &lt;code&gt;let&lt;&#x2F;code&gt;, &lt;code&gt;mut&lt;&#x2F;code&gt; being short for mutable.&lt;&#x2F;p&gt;
&lt;p&gt;So in order to solve the problem as seen in the previous example we can rewrite our program as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rust&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rust &quot;&gt;&lt;code class=&quot;language-rust&quot; data-lang=&quot;rust&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;mut&lt;&#x2F;span&gt;&lt;span&gt; a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;    a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Immutable variables cannot be modified once they have been declared. This is one of Rust’s features that provide your code with type safety. However, sometimes mutable variables are just very convenient.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List installed packages with `yum` on CentOS</title>
		<published>2021-12-17T00:00:00+00:00</published>
		<updated>2021-12-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/centos-yum-list-installed-packages/" type="text/html"/>
		<id>https://koenwoortman.com/centos-yum-list-installed-packages/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;yum&lt;&#x2F;code&gt; package manager is the package manager of choice to install RPM packages on CentOS, Red Hat and Amazon Linux. No coincidence there since these three Linux distributions are closely related to each other.&lt;&#x2F;p&gt;
&lt;p&gt;Besides just installing, updating and removing packages on your system &lt;code&gt;yum&lt;&#x2F;code&gt; conveniently provides a command to see which packages are currently installed as well.&lt;&#x2F;p&gt;
&lt;p&gt;To see which packages are installed on your system you can use the &lt;code&gt;yum list&lt;&#x2F;code&gt; command, see the following example:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ yum list installed
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You might need to prefix the command with &lt;code&gt;sudo&lt;&#x2F;code&gt; depending on how the permissions are set up on your system.&lt;&#x2F;p&gt;
&lt;p&gt;Since the output of the command is probably quite long you may prefer to use a terminal text viewer like &lt;code&gt;less&lt;&#x2F;code&gt; or &lt;code&gt;view&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ yum list installed | less
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;check-if-a-certain-package-is-installed-with-yum&quot;&gt;Check if a certain package is installed with yum&lt;&#x2F;h2&gt;
&lt;p&gt;To search for a specific package in your installed packages, this is useful if you need to check whether a certain package is actually installed on your system, you may pipe (&lt;code&gt;|&lt;&#x2F;code&gt;) the output to the &lt;code&gt;grep&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;For example, the following commands will list the installed packages of which the name contains the substring “python”:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ yum list installed | grep &amp;quot;python&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;list-the-installed-packages-with-rpm&quot;&gt;List the installed packages with &lt;code&gt;rpm&lt;&#x2F;code&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;If you can use &lt;code&gt;yum&lt;&#x2F;code&gt; you probably have access to the &lt;code&gt;rpm&lt;&#x2F;code&gt; command as well. RPM is both a package manager as well as a file format for RPM packages. RPM is currently short for RPM Package Manager and at first as Red Hat Package Manager.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;rpm&lt;&#x2F;code&gt; package manager is more a low-level tool for managing packages, but for listing installed packages it is just as useful.&lt;&#x2F;p&gt;
&lt;p&gt;To list all installed packages you use the options &lt;code&gt;-q&lt;&#x2F;code&gt; and &lt;code&gt;-a&lt;&#x2F;code&gt;. The &lt;code&gt;-q&lt;&#x2F;code&gt; option is used to query the installed packages, the &lt;code&gt;-a&lt;&#x2F;code&gt; option tells &lt;code&gt;rpm&lt;&#x2F;code&gt; to query all packages. So in order to list all the installed packages you use the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rpm -qa
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To search for a specific package that might be installed you run the command without the &lt;code&gt;-a&lt;&#x2F;code&gt; option and provide the package name you would like to search for. For example see the following command which searches for the “python3” package:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ rpm -q python3
&lt;&#x2F;span&gt;&lt;span&gt;python3-3.7.10-1.amzn2.0.1.x86_64
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And once again: you might need to prefix the command with &lt;code&gt;sudo&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to generate an .nvmrc file with the current Node.js version</title>
		<published>2021-11-18T00:00:00+00:00</published>
		<updated>2021-11-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/nodejs-create-nvmrc-file/" type="text/html"/>
		<id>https://koenwoortman.com/nodejs-create-nvmrc-file/</id>
		<content type="html">&lt;p&gt;In the &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file you may specify the Node.js version you would prefer to use in your project. Normally you create this file in your project&#x27;s root directory. The Node Version Manager (&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nvm-sh&#x2F;nvm&quot;&gt;nvm&lt;&#x2F;a&gt;) respects the version set in this file. Meaning the nvm subcommands &lt;code&gt;use&lt;&#x2F;code&gt;, &lt;code&gt;install&lt;&#x2F;code&gt;, &lt;code&gt;exec&lt;&#x2F;code&gt;, &lt;code&gt;run&lt;&#x2F;code&gt;, etc. will use the version that is set in the &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;In order to set your current Node.js version in the &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file you may run the following command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;node -v &amp;gt; .nvmrc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The line above makes use of the &lt;code&gt;&amp;gt;&lt;&#x2F;code&gt;-redirection operator of your shell to write the standard output of the &lt;code&gt;node -v&lt;&#x2F;code&gt; command to the &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file. If the file doesn’t exist yet it will be created. If it did exist however, the contents of the file will be overwritten.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;It is important&lt;&#x2F;strong&gt; to take into account that nvm does not change your Node.js version automatically once you are located in a directory containing a &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file, the subcommands of nvm just take this file into account. So you still need to run &lt;code&gt;nvm use&lt;&#x2F;code&gt; in order to set the right version of Node.js using nvm. The &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nvm-sh&#x2F;nvm#deeper-shell-integration&quot;&gt;nvm docs&lt;&#x2F;a&gt; do show you how you can automate this step for the shell you are using.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;use-the-latest-node-lts-version&quot;&gt;Use the latest Node LTS version&lt;&#x2F;h2&gt;
&lt;p&gt;Besides setting an exact version in your .nvmrc file there are other alternatives too. You may set your Node.js version to the latest long term support (lts) version for example:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;echo &amp;quot;lts&#x2F;*&amp;quot; &amp;gt; .nvmrc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;use-the-latest-node-version&quot;&gt;Use the latest Node version&lt;&#x2F;h2&gt;
&lt;p&gt;Another option would be to default to the latest available option, you do this by just setting the contents of the &lt;code&gt;.nvmrc&lt;&#x2F;code&gt; file to “node”:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;echo &amp;quot;node&amp;quot; &amp;gt; .nvmrc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Replacing hugo by zola as static site generator for my blog</title>
		<published>2021-10-31T00:00:00+00:00</published>
		<updated>2021-10-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/replacing-hugo-with-zola/" type="text/html"/>
		<id>https://koenwoortman.com/replacing-hugo-with-zola/</id>
		<content type="html">&lt;p&gt;After going from Jekyll to Gatsby to Hugo I now landed on the &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt; static site generator. Static site generator hopping is the new Distro hopping I guess :).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-jekyll&quot;&gt;1. Jekyll&lt;&#x2F;h2&gt;
&lt;p&gt;I started with &lt;a href=&quot;https:&#x2F;&#x2F;jekyllrb.com&#x2F;&quot;&gt;Jekyll&lt;&#x2F;a&gt; just because it was easy and I was pretty curious about Github Pages. There were quite some themes laying around to be used and it just worked. I basically discarded my Jekyll site for the following reasons; content changes with the &lt;code&gt;serve&lt;&#x2F;code&gt; command were a bit slow; around 2 seconds. I used to run this command without the &lt;code&gt;--incremental&lt;&#x2F;code&gt; option, which only re-builds pages that changed because I encountered a couple flaws with it. I am not completely sure anymore what those flaws were and a good chance that they have been fixed already so don’t let that discourage you.&lt;&#x2F;p&gt;
&lt;p&gt;Besides that, as a young developer doing front-end work from time to time I wanted something fancier. Enter Gatsby.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;2-gatsby&quot;&gt;2. Gatsby&lt;&#x2F;h2&gt;
&lt;p&gt;Second in line was &lt;a href=&quot;https:&#x2F;&#x2F;www.gatsbyjs.com&#x2F;&quot;&gt;Gatsby&lt;&#x2F;a&gt; as my static site generator. I just grabbed the &lt;a href=&quot;https:&#x2F;&#x2F;www.gatsbyjs.com&#x2F;starters&#x2F;gatsbyjs&#x2F;gatsby-starter-blog&quot;&gt;official blog starter&lt;&#x2F;a&gt;, added a dark mode toggle and ran with it. It served me pretty well for a while. However… the main thing that bugged me was GraphQL.Whenever I wanted to customize something apparently trivial I found myself spending hours.&lt;&#x2F;p&gt;
&lt;p&gt;Besides that the build time was quite slow, more than two minutes or so. I started using Netlify to host my site and I am not yet willing to go past my 300 free build minutes, I am a little cheap I guess :).&lt;&#x2F;p&gt;
&lt;p&gt;It was also a little bit too fancy, which is quite ironically one of the reasons I chose Gatsby. Enter something more boring: Hugo.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-hugo&quot;&gt;3. Hugo&lt;&#x2F;h2&gt;
&lt;p&gt;Wow &lt;a href=&quot;https:&#x2F;&#x2F;gohugo.io&#x2F;&quot;&gt;Hugo&lt;&#x2F;a&gt; fast! I went from 2 minutes of build time in Netlify to around 30 seconds of which the actual build command takes less than a second. I just grabbed the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;nanxiaobei&#x2F;hugo-paper&quot;&gt;paper theme&lt;&#x2F;a&gt; and customized it a little.&lt;&#x2F;p&gt;
&lt;p&gt;Mainly for my own archive, here is a screenshot of the previous Hugo site. It is a bit plain but it did the job very well.
&lt;img src=&quot;&#x2F;img&#x2F;v3-blog-hugo.png&quot; alt=&quot;Hugo blog&quot; title=&quot;Screenshot of version 3 of my blog made with hugo&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So why did I move away from Hugo? Probably partially because I was bored or so. But second was the templating language in Hugo. It probably isn’t all too difficult but sitting down and figuring out how it worked just got in the way of the thing I wanted to do.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;4-giving-zola-a-try-now&quot;&gt;4. Giving Zola a try now&lt;&#x2F;h2&gt;
&lt;p&gt;After Googling around a bit I found &lt;a href=&quot;https:&#x2F;&#x2F;www.getzola.org&#x2F;&quot;&gt;Zola&lt;&#x2F;a&gt;. At the time of writing it seems to be a bit early in development. But it works pretty nice. In terms of being fast it’s pretty close to Hugo and it uses a templating language that’s inspired by Jinja2 and the Django templating language, which I know already so that made me pretty happy.&lt;&#x2F;p&gt;
&lt;p&gt;I gave Zola a try and gave my site a make-over, inspired by the &lt;a href=&quot;https:&#x2F;&#x2F;adidoks.netlify.app&#x2F;&quot;&gt;Adidoks theme&lt;&#x2F;a&gt;. Putting it live today, being quite happy with the end result.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Async generator functions in Python</title>
		<published>2021-09-22T00:00:00+00:00</published>
		<updated>2021-09-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-async-generator-functions/" type="text/html"/>
		<id>https://koenwoortman.com/python-async-generator-functions/</id>
		<content type="html">&lt;p&gt;Asynchronous generator functions are part of Python version 3.6, they were introduced by &lt;a href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0525&#x2F;&quot;&gt;PEP-525&lt;&#x2F;a&gt;. Asynchronous generator functions are much like regular asynchronous functions except that they contain the &lt;code&gt;yield&lt;&#x2F;code&gt; keyword in the function body. Which in turn, makes them much like regular generators, except for that you can use the &lt;code&gt;await&lt;&#x2F;code&gt; keyword in there as well.&lt;&#x2F;p&gt;
&lt;p&gt;When calling an asynchronous generator function, the result that is returned is an asynchronous generator object. In contrast to calling regular asynchronous functions which return a coroutine object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;async_generator&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;yield
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(async_generator())
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;lt;async_generator object async_generator at 0x7f4eb88b5c10&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;async_function&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(async_function())
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;lt;coroutine object async_function at 0x7f4eb8f52d40&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since the asynchronous generator is, no surprise, asynchronous you are allowed to use the &lt;code&gt;await&lt;&#x2F;code&gt; keyword inside the asynchronous generator.&lt;&#x2F;p&gt;
&lt;p&gt;You can use this, for example, to send out HTTP requests in the asynchronous generator and yielding the response.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;async &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;download_pages&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;urls&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;url &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;urls:
&lt;&#x2F;span&gt;&lt;span&gt;        response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= await &lt;&#x2F;span&gt;&lt;span&gt;get_response(url)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;yield &lt;&#x2F;span&gt;&lt;span&gt;response
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides asynchronous iterables you can use asynchronous generators with the &lt;code&gt;async for&lt;&#x2F;code&gt;-loop as well.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving `Failed to execute &#x27;importNode&#x27; on &#x27;Document&#x27;` in Alpine.js</title>
		<published>2021-08-12T00:00:00+00:00</published>
		<updated>2021-08-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/alpinejs-failed-execute-importnode-on-document/" type="text/html"/>
		<id>https://koenwoortman.com/alpinejs-failed-execute-importnode-on-document/</id>
		<content type="html">&lt;p&gt;When you encounter the following error you most likely are not using a &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag in a for loop in Alpine.js.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Alpine Expression Error: Failed to execute &amp;#39;importNode&amp;#39; on &amp;#39;Document&amp;#39;: parameter 1 is not of type &amp;#39;Node&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Let&#x27;s for example consider the following Alpine component.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-data&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;{ users: [&amp;#39;You&amp;#39;, &amp;#39;Me&amp;#39;, &amp;#39;Someone Else&amp;#39;] }&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;user in users&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-text&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;user&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;At least in Alpine.js version 3 this will show the error as mentioned above and not render any list items. This is due to the fact that &lt;code&gt;x-for&lt;&#x2F;code&gt; directives should only be declared on &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tags, which contain only one root element. So the above example can be fixed as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-data&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;{ users: [&amp;#39;You&amp;#39;, &amp;#39;Me&amp;#39;, &amp;#39;Someone Else&amp;#39;] }&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-for&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;user in users&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-text&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;user&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The Alpine.js &lt;a href=&quot;https:&#x2F;&#x2F;alpinejs.dev&#x2F;directives&#x2F;for&quot;&gt;docs&lt;&#x2F;a&gt; define two rules you should keep in mind when using &lt;code&gt;x-for&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;x-for &lt;strong&gt;must&lt;&#x2F;strong&gt; be declared on a &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; element&lt;&#x2F;li&gt;
&lt;li&gt;That &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; element &lt;strong&gt;must&lt;&#x2F;strong&gt; have only one root element&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag is a special HTML5 tag that is not visually rendered on a webpage directly. Instead it is meant to be instantiated with JavaScript once a page is loaded. You can think of it as an elements that just holds content, which you can render dynamically and reuse multiple times if you need to. A side note here is that Internet Explorer does not &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;template#browser_compatibility&quot;&gt;support&lt;&#x2F;a&gt; the usage of the &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;further-readings&quot;&gt;Further Readings:&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;alpinejs.dev&#x2F;directives&#x2F;for&quot;&gt;Alpine docs on x-for&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving `Cannot read property &#x27;cloneNode&#x27; of undefined` in Alpine.js</title>
		<published>2021-08-11T00:00:00+00:00</published>
		<updated>2021-08-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/alpinejs-cannot-read-property-clonenode-of-undefined/" type="text/html"/>
		<id>https://koenwoortman.com/alpinejs-cannot-read-property-clonenode-of-undefined/</id>
		<content type="html">&lt;p&gt;If you are developing with Alpine.js you may encounter the following error at some point:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Alpine Expression Error: Cannot read property &amp;#39;cloneNode&amp;#39; of undefined
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Most likely you are using the &lt;code&gt;x-if&lt;&#x2F;code&gt; directive on an HTML element other than &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt;. In Alpine.js, the &lt;code&gt;x-if&lt;&#x2F;code&gt; directive should &lt;strong&gt;only&lt;&#x2F;strong&gt; be applied on &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; elements and not on the element you wish to hide or display with conditional rendering.&lt;&#x2F;p&gt;
&lt;p&gt;So let&#x27;s consider the following element for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;page === &amp;#39;&amp;#39;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Home&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to get the above to work properly we need to wrap the &lt;code&gt;h1&lt;&#x2F;code&gt; in a &lt;code&gt;template&lt;&#x2F;code&gt; tag. As is shown in the following example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;x-if&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;page === &amp;#39;&amp;#39;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;Home&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h1&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;template&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag is a special HTML5 tag that is not visually rendered on a webpage directly. Instead it is meant to be instantiated with JavaScript once a page is loaded. You can think of it as an elements that just holds content, which you can render dynamically and reuse multiple times if you need to. A side note here is that Internet Explorer does not &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;HTML&#x2F;Element&#x2F;template#browser_compatibility&quot;&gt;support&lt;&#x2F;a&gt; the usage of the &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag.&lt;&#x2F;p&gt;
&lt;p&gt;Alternatively you can possibly use &lt;code&gt;x-show&lt;&#x2F;code&gt; instead of &lt;code&gt;x-if&lt;&#x2F;code&gt; if it better suits your use-case. The &lt;code&gt;x-show&lt;&#x2F;code&gt; directive uses CSS to hide elements and therefore doesn&#x27;t require the use of the &lt;code&gt;&amp;lt;template&amp;gt;&lt;&#x2F;code&gt; tag. Alpine does so by setting the display property to &amp;quot;none&amp;quot;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;further-readings&quot;&gt;Further Readings:&lt;&#x2F;h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;alpinejs.dev&#x2F;directives&#x2F;if&quot;&gt;Alpine docs on x-if&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;alpinejs.dev&#x2F;directives&#x2F;show&quot;&gt;Alpine docs on x-show&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Print object as dict in a Django template</title>
		<published>2021-05-26T00:00:00+00:00</published>
		<updated>2021-05-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-templates-print-model-as-dict/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-templates-print-model-as-dict/</id>
		<content type="html">&lt;p&gt;When you need to debug an object or model instance in Django templates spinning up a debugger is probably the most appropriate thing to do. However, debugging with print statements can be just so convenient. Just make sure to not push the print statements to production, it will drive your fellow devs mad.&lt;&#x2F;p&gt;
&lt;p&gt;Printing objects in Python gets you their string representation, you can easily get around that by using &lt;code&gt;.__dict__&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;u &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;User()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;u.__dict__
&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;_state&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;django.db.models.base.ModelState &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;object &lt;&#x2F;span&gt;&lt;span&gt;at &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0x7fadd961d820&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;id&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;password&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;last_login&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;username&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;first_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;last_name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;email&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_staff&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_active&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;date_joined&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: datetime.datetime(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2021&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;26&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;501219&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;tzinfo&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;UTC&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;)}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In Django templates this trick doesn&#x27;t work unfortunately. If you try something like the code below you end up with an &lt;code&gt;TemplateSyntaxError&lt;&#x2F;code&gt; saying: &lt;code&gt;Variables and attributes may not begin with underscores: &#x27;object.__dict__&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;{% extends &amp;#39;layouts&#x2F;base.html&amp;#39; %} {% block content %} {{ object.__dict__ }} {%
&lt;&#x2F;span&gt;&lt;span&gt;endblock content %}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One way around this, for debugging purposes, is with a custom template filter. Lets adjust the above example somewhat, we will load a tag library called &lt;code&gt;debug&lt;&#x2F;code&gt; and add the &lt;code&gt;as_dict&lt;&#x2F;code&gt; template filter to the template:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;{% extends &amp;#39;layouts&#x2F;base.html&amp;#39; %} {% load debug %} {% block content %} {{
&lt;&#x2F;span&gt;&lt;span&gt;object|as_dict }} {% endblock content %}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;debug&lt;&#x2F;code&gt; tag library is not yet registered, actually it doesn&#x27;t even exist yet. In order to register the tag library we will add is as a library to the TEMPLATES settings in &lt;code&gt;settings.py&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;TEMPLATES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;OPTIONS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;libraries&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:{
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;debug&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;project.templatetags.debug&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            }
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we need to create the library we added to the settings, with the path: &lt;code&gt;project&#x2F;templatetags&#x2F;debug.py&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In this templatetags library we add the &lt;code&gt;as_dict&lt;&#x2F;code&gt; filter we used before in our template.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;template
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;register &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;template.Library()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;as_dict&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;value&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;value.__dict__
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;register.filter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;as_dict&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, as_dict)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set the current user in a Django CreateView</title>
		<published>2021-05-25T00:00:00+00:00</published>
		<updated>2021-05-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-set-current-user-create-view/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-set-current-user-create-view/</id>
		<content type="html">&lt;p&gt;Often when creating a model via a form in Django you want to relate that object to the currently logged in user, or the business to which the current user relates to in its turn.&lt;&#x2F;p&gt;
&lt;p&gt;You may set the current user for a new object in the &lt;code&gt;form_valid&lt;&#x2F;code&gt; method of a &lt;code&gt;CreateView&lt;&#x2F;code&gt;. Make sure to require a login, otherwise there is no current user to be set.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib.auth.mixins &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;LoginRequiredMixin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.views.generic.edit &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;CreateView
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;projects.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;CredentialsCreateView&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;LoginRequiredMixin&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;CreateView&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Project
&lt;&#x2F;span&gt;&lt;span&gt;    fields &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;title&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;form_valid&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;form&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        form.instance.user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.request.user
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;super&lt;&#x2F;span&gt;&lt;span&gt;().form_valid(form)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Adding a placeholder to form fields in Django</title>
		<published>2021-05-24T00:00:00+00:00</published>
		<updated>2021-05-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-form-field-placeholder/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-form-field-placeholder/</id>
		<content type="html">&lt;p&gt;HTML placeholders in form inputs can be super useful to let the user know what kind of value you are expecting. Setting a placeholder attribute on a HTML element is straightforward, but in Django you normally let a Form class deal with rendering your HTML form.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore, in order to set a placeholder value for a form field in Django you should pass the placeholder as an attribute to a form widget.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;forms
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;ApiCredentialsForm&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;forms.Form&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;forms.CharField(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;widget&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;forms.PasswordInput(
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;attrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;placeholder&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;*******&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}))
&lt;&#x2F;span&gt;&lt;span&gt;    token &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;forms.CharField(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;widget&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;forms.PasswordInput(
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;attrs&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;placeholder&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;*******&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;}))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Restart a service with SaltStack when a config file changes</title>
		<published>2021-05-23T00:00:00+00:00</published>
		<updated>2021-05-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/saltstack-reload-service-on-config-changes/" type="text/html"/>
		<id>https://koenwoortman.com/saltstack-reload-service-on-config-changes/</id>
		<content type="html">&lt;p&gt;In Linux many services require a restart when a config file is changed. Having to do this manually every time you change a configuration can be a pain. So doing this automatically would be great. With SaltStack you can achieve this by using &lt;a href=&quot;https:&#x2F;&#x2F;docs.saltproject.io&#x2F;en&#x2F;latest&#x2F;ref&#x2F;states&#x2F;requisites.html#watch&quot;&gt;watch&lt;&#x2F;a&gt; in a state.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yml &quot;&gt;&lt;code class=&quot;language-yml&quot; data-lang=&quot;yml&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nginx&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;service.running&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;reload&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;enable&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;span&gt;    - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;watch&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;file&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;nginx
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;file.managed&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&#x2F;etc&#x2F;nginx&#x2F;sites-enabled&#x2F;website
&lt;&#x2F;span&gt;&lt;span&gt;    - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;source&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;salt:&#x2F;&#x2F;nginx&#x2F;website
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By &amp;quot;watching&amp;quot; a different state Salt makes sure to restart the service when the specified state has changed.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Render template directly from Django middleware</title>
		<published>2021-05-22T00:00:00+00:00</published>
		<updated>2021-05-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-render-template-from-middleware/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-render-template-from-middleware/</id>
		<content type="html">&lt;p&gt;In Django you normally specify templates to be rendered from a view class or function. Instead you can render a template directly from the Django middleware as well, as an alternative to redirecting to a different view.&lt;&#x2F;p&gt;
&lt;p&gt;For example you may want to render a maintenance template if your site is in maintenance mode. In that case you can directly call the render function that can be imported from &lt;code&gt;django.shortcuts&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This example may look like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.conf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;settings
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.shortcuts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;render
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;MaintenanceMiddleware&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;get_response&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        self.get_response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;get_response
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__call__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;settings.IN_MAINTENANCE:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;render(request, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;maintenance.html)&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;return&lt;&#x2F;span&gt;&lt;span&gt; self.get_response(request)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Target minions by multiple grains with Salt</title>
		<published>2021-05-21T00:00:00+00:00</published>
		<updated>2021-05-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/saltstack-target-by-multiple-grains/" type="text/html"/>
		<id>https://koenwoortman.com/saltstack-target-by-multiple-grains/</id>
		<content type="html">&lt;p&gt;With Salt there is a whole host of ways to target minions. In most cases I prefer to target minions based on certain grains.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ salt -G &amp;#39;role:webserver&amp;#39; test.ping
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, this allows you to only target minions by a single grain. In order to target minions based on multiple grains you use &lt;strong&gt;compound matchers&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ salt -C &amp;#39;G@role:webserver and G@role:worker&amp;#39; test.ping
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;See the salt &lt;a href=&quot;https:&#x2F;&#x2F;docs.saltproject.io&#x2F;en&#x2F;latest&#x2F;topics&#x2F;targeting&#x2F;compound.html&quot;&gt;docs&lt;&#x2F;a&gt; for more information about compound matchers.&lt;&#x2F;p&gt;
&lt;p&gt;You may combine these compound expressions to define &lt;a href=&quot;https:&#x2F;&#x2F;docs.saltproject.io&#x2F;en&#x2F;latest&#x2F;topics&#x2F;targeting&#x2F;nodegroups.html&quot;&gt;node groups&lt;&#x2F;a&gt; in the configuration of the salt-master. This configuration is a YAML file which is by default located at &lt;code&gt;&#x2F;etc&#x2F;salt&#x2F;master&lt;&#x2F;code&gt;, on the salt-master obviously.&lt;&#x2F;p&gt;
&lt;p&gt;An node group definition in the master configuration would look like the following for example:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yml &quot;&gt;&lt;code class=&quot;language-yml&quot; data-lang=&quot;yml&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;nodegroups&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;django&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;G@role:webserver and G@role:worker&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By defining this &lt;code&gt;django&lt;&#x2F;code&gt; node group you can target all the matching minions with the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ salt -N django test.ping
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Hiding admin pages using Django middleware</title>
		<published>2021-05-20T00:00:00+00:00</published>
		<updated>2021-05-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-hide-admin-middleware/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-hide-admin-middleware/</id>
		<content type="html">&lt;p&gt;You normally render Django templates from your views. However, in some cases you may want to short circuit something and not let a request get that far into your application.&lt;&#x2F;p&gt;
&lt;p&gt;The guys on the Django Chat Podcast made a case in a &lt;a href=&quot;https:&#x2F;&#x2F;djangochat.com&#x2F;episodes&#x2F;security-3YyW_Jy7&quot;&gt;security episode&lt;&#x2F;a&gt; for changing the Django admin URL prefix to &lt;em&gt;anything&lt;&#x2F;em&gt; other than &lt;code&gt;&#x2F;admin&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Of course this does not make the Django admin itself any more secure, however you exclude some script kiddies scanning the web for Django sites (by looking for the Django admin at &lt;code&gt;&#x2F;admin&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;I have been taking a different &amp;quot;security through obscurity&amp;quot; approach for making the Django admin unavailable for others. Instead of changing the URL of the admin I raise a 404 in a custom middleware class to non-superuser when they try to access an admin route. In this case, superusers have to be logged in first via the website itself before they can access the admin pages.&lt;&#x2F;p&gt;
&lt;p&gt;This middleware class looks like the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.http &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Http404
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;AdminPagesMiddleware&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;get_response&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        self.get_response &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;get_response
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__call__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;request.path.startswith(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;admin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;and not &lt;&#x2F;span&gt;&lt;span&gt;request.user.is_superuser:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span&gt;Http404()
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;self.get_response(request)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We should not forget to register the middleware as well :).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;MIDDLEWARE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;project.middleware.auth_middleware.AdminPagesMiddleware&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And you definitely want tests for this middleware.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.urls &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;reverse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.test &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;TestCase, Client
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib.auth &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;get_user_model
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;AdminPagesMiddlewareTestCase&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;TestCase&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;test_throws_404_for_anonymous_users&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.client.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;admin&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;follow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;r.status_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;test_throws_404_for_users&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        get_user_model().objects.create_user(
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko@test.com&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pass&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;is_superuser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        self.client.login(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;username&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pass&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.client.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;admin&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;follow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;r.status_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;404
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;test_accessible_for_superusers&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        get_user_model().objects.create_user(
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko@test.com&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pass&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;is_superuser&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        self.client.login(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;username&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ko&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pass&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        r &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.client.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;admin&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;follow&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;assert &lt;&#x2F;span&gt;&lt;span&gt;r.status_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;200
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Update a single field on a Django model</title>
		<published>2021-05-19T00:00:00+00:00</published>
		<updated>2021-05-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-update-single-field/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-update-single-field/</id>
		<content type="html">&lt;p&gt;Updating a single field in Django might have your preference over updating all model fields to the database. This is somewhat more beneficial in terms of performance for example. You specify the fields that should be updated by passing the &lt;code&gt;update_fields&lt;&#x2F;code&gt; keyword argument to the &lt;code&gt;.save()&lt;&#x2F;code&gt; method containing a list of field names.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;user.is_active &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False
&lt;&#x2F;span&gt;&lt;span&gt;user.save(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;update_fields&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_active])&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Of course Django provides excellent &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;ref&#x2F;models&#x2F;instances&#x2F;#specifying-which-fields-to-save&quot;&gt;docs&lt;&#x2F;a&gt; on this particular feature.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Comments in bash scripts</title>
		<published>2021-05-18T00:00:00+00:00</published>
		<updated>2021-05-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-comments/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-comments/</id>
		<content type="html">&lt;p&gt;Comments in bash, or in other shells, start with a hashtag(&lt;code&gt;#&lt;&#x2F;code&gt;). Everything after the hashtag is ignored, so you can have comments that span the entire line or use comments inline after a command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Create a file if it doesn&amp;#39;t exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span&gt;$file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  touch $file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Create the file using the touch command
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Bash does not support multiline comments like many other languages. If you want comments that span multiple lines you should add a &lt;code&gt;#&lt;&#x2F;code&gt; before each line.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Create a file if it doesn&amp;#39;t exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# if [[ ! -f $file ]]; then
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#   touch $file
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you really want comments that span multiple lines you may misuse HEREDOC strings.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&amp;lt;COMMENT
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;You may use HEREDOC to
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;write multiline comments
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;COMMENT
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Pro tip!&lt;&#x2F;strong&gt; you can use comments in your terminal as well. This way you can store them in your shell history without having to run them for example.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ # git rev-list --max-parents=0 HEAD | tail -n 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What are &#x27;shells&#x27; in Linux</title>
		<published>2021-05-17T00:00:00+00:00</published>
		<updated>2021-05-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-what-is-a-shell/" type="text/html"/>
		<id>https://koenwoortman.com/linux-what-is-a-shell/</id>
		<content type="html">&lt;p&gt;You might have opened your terminal many times before. This program is more factually called a &lt;em&gt;terminal emulator&lt;&#x2F;em&gt;. These terminal emulators are most often pre-installed and allow you to interact with your shell, I am writing this in the &lt;code&gt;gnome-terminal&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
&lt;p&gt;A shell basically is the following two things:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;An interpreter for shell commands:&lt;&#x2F;strong&gt; A shell allows you to execute shell commands via both an interactive shell prompt and via shell scripts.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;A programming language:&lt;&#x2F;strong&gt; A shell provides basic programming capabilities to combine shell commands in a more complex manner, with features including variables and flow control structures.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Examples of shells on Linux, and other UNIX-like systems, include bash, zsh, fish and ksh.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;bash&#x2F;manual&#x2F;html_node&#x2F;What-is-a-shell_003f.html&quot;&gt;This page&lt;&#x2F;a&gt; at the GNU website describes the shell at is base as a &amp;quot;macro processor that executes commands&amp;quot;. This term &amp;quot;marco processor&amp;quot; refers to the capability of shells to expand text and symbols into larger expressions.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>What is this shebang comment starting with #!</title>
		<published>2021-05-16T00:00:00+00:00</published>
		<updated>2021-05-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-shebang/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-shebang/</id>
		<content type="html">&lt;p&gt;If you have seen an executable script for a UNIX system good chance the first line started with &lt;code&gt;#!&lt;&#x2F;code&gt;. This is a special comment called the &amp;quot;shebang&amp;quot; and it specifies the interpreter that should be used to execute the file with.&lt;&#x2F;p&gt;
&lt;p&gt;So if you are looking at a bash script you may see &lt;code&gt;#!&#x2F;bin&#x2F;bash&lt;&#x2F;code&gt;, if you are looking at a Python script you may see &lt;code&gt;#!&#x2F;usr&#x2F;bin&#x2F;python3&lt;&#x2F;code&gt; as shebang.&lt;&#x2F;p&gt;
&lt;p&gt;The shebang comment uses the following structure:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;#![path to interpreter] [possible arguments]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may also encounter a shebang that is pointing to the interpreter &lt;code&gt;&#x2F;usr&#x2F;bin&#x2F;env&lt;&#x2F;code&gt;. This is useful in case you are not sure where your preferred interpreter is located on the system that might run the script, whether it is a server in the AWS cloud or the laptop of a colleague. It makes your script portable so to say since by using &lt;code&gt;&#x2F;usr&#x2F;bin&#x2F;env&lt;&#x2F;code&gt; PATH is searched for the command you pass as an argument. The following example shows how this shebang with the &lt;code&gt;bash&lt;&#x2F;code&gt; interpreter.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;#!&#x2F;usr&#x2F;bin&#x2F;env bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case no shebang comment is present the default interpreter used depends on the shell you are using. Bash uses itself as a fallback when no shebang is specified. While Zsh uses &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; as a fallback on the other hand, what &lt;code&gt;&#x2F;bin&#x2F;sh&lt;&#x2F;code&gt; actually refers to depends on your system.&lt;&#x2F;p&gt;
&lt;p&gt;Also make sure your script has executable permissions before you execute it. You may do this with the &lt;code&gt;chmod&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ chmod u+x .&#x2F;script.sh
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Update a field for all objects in a Django QuerySet</title>
		<published>2021-05-15T00:00:00+00:00</published>
		<updated>2021-05-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-update-field-for-all-objects-in-queryset/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-update-field-for-all-objects-in-queryset/</id>
		<content type="html">&lt;p&gt;In some cases you may want to update a field, or multiple fields, for a collection of Django models in a QuerySet object. For those use-cases you may use the &lt;code&gt;.update()&lt;&#x2F;code&gt; method on the QuerySet object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;User.objects.filter(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;last_login&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;).update(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;is_active&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;.update()&lt;&#x2F;code&gt; method allows you to update fields for all the objects in your QuerySet. This is illustrated in the following example, which uses a &lt;code&gt;.filter()&lt;&#x2F;code&gt; method to select the objects which should be updated. However, if you want to apply a change to all objects you can use &lt;code&gt;.all()&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;p&gt;When you run this line in the Django shell you might be surprised by the return value of the statement. The &lt;code&gt;.update()&lt;&#x2F;code&gt; method returns the number of rows matched by the query, which is not necessarily equal to the amount of rows updated.&lt;&#x2F;p&gt;
&lt;p&gt;There is a gotcha when updating objects like this though. This method directly runs an SQL statement without calling the &lt;code&gt;save()&lt;&#x2F;code&gt; method on your model and therefore not call any pre_save or post_save signals.&lt;&#x2F;p&gt;
&lt;p&gt;Of course Django provides excellent &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;topics&#x2F;db&#x2F;queries&#x2F;#updating-multiple-objects-at-once&quot;&gt;docs&lt;&#x2F;a&gt; on this particular feature.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check for future posts in Hugo templates</title>
		<published>2021-05-14T00:00:00+00:00</published>
		<updated>2021-05-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/hugo-template-check-if-future-post/" type="text/html"/>
		<id>https://koenwoortman.com/hugo-template-check-if-future-post/</id>
		<content type="html">&lt;p&gt;I have been happily running by blog using the Hugo static site generator. Hosted on Netlify, works like a charm. However, lately I has an issue where I, by accident, had set the publishing date for a post 10 years in the future. Not that big a deal, but in order to prevent this error in the future I did want to add a visual clue to future posts.&lt;&#x2F;p&gt;
&lt;p&gt;I never really researched the Hugo templating language very well. I just picked a &lt;a href=&quot;https:&#x2F;&#x2F;themes.gohugo.io&#x2F;hugo-paper&#x2F;&quot;&gt;theme from Hugo Themes&lt;&#x2F;a&gt;, slightly modified some CSS and it just worked without any other effort.&lt;&#x2F;p&gt;
&lt;p&gt;Using an example from the &lt;a href=&quot;https:&#x2F;&#x2F;gohugo.io&#x2F;templates&#x2F;introduction&#x2F;#example-show-only-upcoming-events&quot;&gt;Hugo website&lt;&#x2F;a&gt; I was able to add a visual clue to future posts.&lt;&#x2F;p&gt;
&lt;p&gt;Lets for example say that my markdown post has the following YAML Front Matter at the top of the posts.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;yml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-yml &quot;&gt;&lt;code class=&quot;language-yml&quot; data-lang=&quot;yml&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;title&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Check for future posts in Hugo templates&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;date&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2031-05-14
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;lastmod&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2031-05-14
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;I have been happily running by blog...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, in my templates I check check for that &lt;code&gt;date&lt;&#x2F;code&gt; property which is set in the future. I did this with &lt;code&gt;{{- if ge .Date.Unix now.Unix }}&lt;&#x2F;code&gt;. See the following snippet which adds some text after a title for future posts:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h2&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  {{ .Title }} {{- if ge .Date.Unix now.Unix }}
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;sup&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;[future]&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;sup&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  {{- end }}
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;h2&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Defining constants in bash scripts</title>
		<published>2021-05-13T00:00:00+00:00</published>
		<updated>2021-05-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-constants/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-constants/</id>
		<content type="html">&lt;p&gt;If you are coming from a different programming language you are probably familiar to the concept of constants already. A constant is just like a variable except for the fact that its value does not change during the execution of your script.&lt;&#x2F;p&gt;
&lt;p&gt;This makes sense in scripts for which you have to define the total amount of months in a year for example. This is a number that is not likely to change anytime soon.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;TOTAL_MONTHS&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;12
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you might have noticed the &lt;code&gt;TOTAL_MONTHS&lt;&#x2F;code&gt; constant is written in all caps, this is actually just a convention and nothing prevents you from changing its value. Using all caps does not enforce a readonly value in bash so if you would like to change &lt;code&gt;TOTAL_MONTHS&lt;&#x2F;code&gt; to &lt;code&gt;11&lt;&#x2F;code&gt; you are free to do so:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ TOTAL_MONTHS=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;12&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$ echo $TOTAL_MONTHS
&lt;&#x2F;span&gt;&lt;span&gt;12
&lt;&#x2F;span&gt;&lt;span&gt;$ TOTAL_MONTHS=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;11&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$ echo $TOTAL_MONTHS
&lt;&#x2F;span&gt;&lt;span&gt;11
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Your shell doesn&#x27;t complain at all about this as you see.&lt;&#x2F;p&gt;
&lt;p&gt;This all caps convention is consistent with how environment variables and shell variables are defined. An example of an environment variable would be &lt;code&gt;$PATH&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;enforcing-read-only-values&quot;&gt;Enforcing read only values&lt;&#x2F;h2&gt;
&lt;p&gt;If you need to enforce a readonly value you may do so by using the &lt;code&gt;declare&lt;&#x2F;code&gt; or &lt;code&gt;readonly&lt;&#x2F;code&gt; commands. These two commands work very similar but differ in the default scope for which the variables are available. The &lt;code&gt;readonly&lt;&#x2F;code&gt; command defaults to a global scope where &lt;code&gt;declare&lt;&#x2F;code&gt; uses a local scope by default.&lt;&#x2F;p&gt;
&lt;p&gt;The following snippet illustrates what happens when you try to reassign a readonly variable.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ declare&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -r&lt;&#x2F;span&gt;&lt;span&gt; TOTAL_MONTHS=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;12&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$ echo $TOTAL_MONTHS
&lt;&#x2F;span&gt;&lt;span&gt;12
&lt;&#x2F;span&gt;&lt;span&gt;$ declare&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -r&lt;&#x2F;span&gt;&lt;span&gt; TOTAL_MONTHS=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;11&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;bash: declare: TOTAL_MONTHS: readonly variable
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>See which environment variables are set with `printenv`</title>
		<published>2021-05-12T00:00:00+00:00</published>
		<updated>2021-05-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-see-environment-variables/" type="text/html"/>
		<id>https://koenwoortman.com/bash-see-environment-variables/</id>
		<content type="html">&lt;p&gt;Data in your shell environment is either stored as a shell variable or an environment variable. Shell variables are globally set and readable within bash. Environment variables on the other hand are available to all processes or programs on your system.&lt;&#x2F;p&gt;
&lt;p&gt;You can use the &lt;code&gt;printenv&lt;&#x2F;code&gt; command in order to print the environment variables with their values. This &lt;code&gt;printenv&lt;&#x2F;code&gt; command will exclusively print environment variables.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ printenv
&lt;&#x2F;span&gt;&lt;span&gt;COLORTERM=truecolor
&lt;&#x2F;span&gt;&lt;span&gt;DBUS_SESSION_BUS_ADDRESS=unix:path=&#x2F;run&#x2F;user&#x2F;1000&#x2F;bus
&lt;&#x2F;span&gt;&lt;span&gt;DEFAULTS_PATH=&#x2F;usr&#x2F;share&#x2F;gconf&#x2F;ubuntu.default.path
&lt;&#x2F;span&gt;&lt;span&gt;DESKTOP_SESSION=ubuntu
&lt;&#x2F;span&gt;&lt;span&gt;DISPLAY=:0
&lt;&#x2F;span&gt;&lt;span&gt;EDITOR=vim
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To print both the shell and environment variables you may use the &lt;code&gt;set&lt;&#x2F;code&gt; builtin available in bash. You may use the &lt;code&gt;set&lt;&#x2F;code&gt; builtin as well to change the value of shell attributes.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ set
&lt;&#x2F;span&gt;&lt;span&gt;BASH=&#x2F;usr&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extquote:force_fignore:globasciiranges:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
&lt;&#x2F;span&gt;&lt;span&gt;BASH_ALIASES=()
&lt;&#x2F;span&gt;&lt;span&gt;BASH_ARGC=([0]=&amp;quot;0&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;BASH_ARGV=()
&lt;&#x2F;span&gt;&lt;span&gt;BASH_CMDS=()
&lt;&#x2F;span&gt;&lt;span&gt;BASH_LINENO=()
&lt;&#x2F;span&gt;&lt;span&gt;BASH_SOURCE=()
&lt;&#x2F;span&gt;&lt;span&gt;BASH_VERSINFO=([0]=&amp;quot;5&amp;quot; [1]=&amp;quot;0&amp;quot; [2]=&amp;quot;17&amp;quot; [3]=&amp;quot;1&amp;quot; [4]=&amp;quot;release&amp;quot; [5]=&amp;quot;x86_64-pc-linux-gnu&amp;quot;)
&lt;&#x2F;span&gt;&lt;span&gt;BASH_VERSION=&amp;#39;5.0.17(1)-release&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;COLORTERM=truecolor
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Data types in bash scripts</title>
		<published>2021-05-11T00:00:00+00:00</published>
		<updated>2021-05-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-data-types/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-data-types/</id>
		<content type="html">&lt;p&gt;Whether you pass data to a script as an argument or assign values to a variable. Bash is not &lt;strong&gt;that&lt;&#x2F;strong&gt; aware of data types, bash basically treats it all as strings. However, bash may allow arithmetic operations and comparisons on variables based on the context in which the variable is used.&lt;&#x2F;p&gt;
&lt;p&gt;The fact that bash does not differentiate between strings and integers or floats allows you to be flexible in your shell scripts. Be aware of subtle errors though.&lt;&#x2F;p&gt;
&lt;p&gt;In practice you can enforce an integer of you like by using the command &lt;code&gt;declare&lt;&#x2F;code&gt; with &lt;code&gt;-i&lt;&#x2F;code&gt; as an options. However, this isn&#x27;t used that often in shell scripts.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;declare -i week
&lt;&#x2F;span&gt;&lt;span&gt;week=7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More effectively, if you need a numeric value you can test it in an if-statement.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if ! &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$week&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=~ &lt;&#x2F;span&gt;&lt;span&gt;^&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;9&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span&gt;+$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span&gt;;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Week is not numeric&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving `permission denied` error for shell scripts</title>
		<published>2021-05-10T00:00:00+00:00</published>
		<updated>2021-05-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-execute-file-permission-denied-error/" type="text/html"/>
		<id>https://koenwoortman.com/bash-execute-file-permission-denied-error/</id>
		<content type="html">&lt;p&gt;If try to run a script, whether it is written in Python, bash or something else, you may encounter an error saying &lt;code&gt;Permission denied&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Lets say for example you have a file called &lt;code&gt;script.sh&lt;&#x2F;code&gt; which runs perfectly fine when you run it using a interpreter like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash .&#x2F;script.sh
&lt;&#x2F;span&gt;&lt;span&gt;Hello World
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, you prefer to run it without the &lt;code&gt;bash&lt;&#x2F;code&gt; and use just the file name. Or later even as a command instead by adding it to your PATH.&lt;&#x2F;p&gt;
&lt;p&gt;In that case you may see the following error.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;script.sh
&lt;&#x2F;span&gt;&lt;span&gt;bash: .&#x2F;script.sh: Permission denied
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This error indicates that the &lt;code&gt;execute&lt;&#x2F;code&gt; file permission is missing. In order to add this &lt;code&gt;execute&lt;&#x2F;code&gt; permission you use the &lt;code&gt;chmod&lt;&#x2F;code&gt; command, which is short for &lt;u&gt;&lt;strong&gt;ch&lt;&#x2F;strong&gt;&lt;&#x2F;u&gt;ange file &lt;u&gt;&lt;strong&gt;mod&lt;&#x2F;strong&gt;&lt;&#x2F;u&gt;e. By running the following command we can make the script executable.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ chmod u+x .&#x2F;script.sh
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And as we now see this indeed did the trick. Make sure you have added the shebang comment though.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;script.sh
&lt;&#x2F;span&gt;&lt;span&gt;Hello World
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Vertical align within flexboxes with Tailwind CSS</title>
		<published>2021-05-09T00:00:00+00:00</published>
		<updated>2021-05-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tailwind-css-vertical-align-flexbox/" type="text/html"/>
		<id>https://koenwoortman.com/tailwind-css-vertical-align-flexbox/</id>
		<content type="html">&lt;p&gt;In order to align contents vertically in a flexbox in Tailwind CSS you need to have &lt;code&gt;align-items: center;&lt;&#x2F;code&gt; set on the flex container. You may add this by using the class &lt;code&gt;items-center&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;flex items-center&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Keep in mind that in order to see a visual difference the flex container needs a height bigger than its child element. So you may do this by adding the class &lt;code&gt;min-h-screen&lt;&#x2F;code&gt;, which sets the minimal screen height to &lt;code&gt;100vh&lt;&#x2F;code&gt;. Other options like &lt;code&gt;h-40&lt;&#x2F;code&gt;, just picking a random height here, work too for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;flex items-center min-h-screen&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to completely center the child element, so horizontally as well, you need to have &lt;code&gt;justify-content: center&lt;&#x2F;code&gt; set as well on the flex container. To do so you use the &lt;code&gt;justify-center&lt;&#x2F;code&gt; class in Tailwind. Now the same applies to width here as we had before with the height. In order to see a visual difference make sure that the width of the parent element is bigger than the width of the child element. You may do so by adding the class &lt;code&gt;w-full&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;flex items-center justify-center min-h-screen w-full&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;div&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Horizontally center `.container` in Tailwind CSS</title>
		<published>2021-05-08T00:00:00+00:00</published>
		<updated>2021-05-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tailwind-css-align-container-center-horizontally/" type="text/html"/>
		<id>https://koenwoortman.com/tailwind-css-align-container-center-horizontally/</id>
		<content type="html">&lt;p&gt;When coming from other CSS frameworks like Bootstrap or Bulma the &lt;code&gt;container&lt;&#x2F;code&gt; class in Tailwind CSS might work a little different than expected. In Tailwind CSS the container element is not horizontally centered by default, in contrast to Bootstrap or Bulma.&lt;&#x2F;p&gt;
&lt;p&gt;Frameworks like Bootstrap or Bulma center the &lt;code&gt;container&lt;&#x2F;code&gt; by settings the left and right margin to &lt;code&gt;auto&lt;&#x2F;code&gt;. You can do the same in Tailwind by using the &lt;code&gt;mx-auto&lt;&#x2F;code&gt; class to set the horizontal margins of an element to &lt;code&gt;auto&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;class&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;container mx-auto&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option is to solve this in your &lt;code&gt;tailwind.config.js&lt;&#x2F;code&gt; file. See the example below:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;module&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;exports &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  future: {},
&lt;&#x2F;span&gt;&lt;span&gt;  purge: [],
&lt;&#x2F;span&gt;&lt;span&gt;  theme: {
&lt;&#x2F;span&gt;&lt;span&gt;    container: {
&lt;&#x2F;span&gt;&lt;span&gt;      center: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;  },
&lt;&#x2F;span&gt;&lt;span&gt;  variants: {},
&lt;&#x2F;span&gt;&lt;span&gt;  plugins: [],
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Find out where bash is installed</title>
		<published>2021-05-07T00:00:00+00:00</published>
		<updated>2021-05-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-where-is-bash-installed/" type="text/html"/>
		<id>https://koenwoortman.com/linux-where-is-bash-installed/</id>
		<content type="html">&lt;p&gt;To find out where &lt;code&gt;bash&lt;&#x2F;code&gt;, or any other shell program, is installed on your system you may use the &lt;code&gt;which&lt;&#x2F;code&gt; command. &lt;code&gt;which&lt;&#x2F;code&gt; locates commands in your PATH.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ which bash
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;usr&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;PATH is an environment variable in your system that defines a list of directories in which your shell looks for executable commands when you enter them. As you see from the output of our &lt;code&gt;which&lt;&#x2F;code&gt; command, an occurrence of &lt;code&gt;bash&lt;&#x2F;code&gt; is found in the directory &lt;code&gt;&#x2F;usr&#x2F;bin&lt;&#x2F;code&gt;. In my case this is where the program &lt;code&gt;bash&lt;&#x2F;code&gt; is located.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;where-command-in-zsh&quot;&gt;Where command in zsh&lt;&#x2F;h2&gt;
&lt;p&gt;If you are using the zsh shell instead of bash you have another option besides the &lt;code&gt;which&lt;&#x2F;code&gt; command. The zsh shell comes with a &lt;code&gt;where&lt;&#x2F;code&gt; shell builtin that you can use as alternative to &lt;code&gt;which&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ where bash
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;usr&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Find out what your default shell is</title>
		<published>2021-05-06T00:00:00+00:00</published>
		<updated>2021-05-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-what-is-my-default-shell/" type="text/html"/>
		<id>https://koenwoortman.com/linux-what-is-my-default-shell/</id>
		<content type="html">&lt;p&gt;In order to see what your default terminal shell is, you can best be in the terminal itself. To see your default shell go into the terminal and type:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ echo $SHELL
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;echo&lt;&#x2F;code&gt; command prints out the value of the &lt;code&gt;$SHELL&lt;&#x2F;code&gt; environment variable. This &lt;code&gt;$SHELL&lt;&#x2F;code&gt; variable holds the login shell of the current user logged in on the system. On linux systems the preferred shell by a user is stored in the file &lt;code&gt;&#x2F;etc&#x2F;passwd&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;See for example this snippet of my &lt;code&gt;passwd&lt;&#x2F;code&gt; file. As you see the last section of the line is &lt;code&gt;&#x2F;bin&#x2F;bash&lt;&#x2F;code&gt;, the login shell for my user.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ cat &#x2F;etc&#x2F;passwd
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;koen:x:1000:1000:,,,:&#x2F;home&#x2F;koen:&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You may find something odd when it comes to the file path to bash: &lt;code&gt;&#x2F;bin&#x2F;bash&lt;&#x2F;code&gt;. When you use the &lt;code&gt;which&lt;&#x2F;code&gt; command to find out where a program is located you may see a different path, in my case: &lt;code&gt;&#x2F;usr&#x2F;bin&#x2F;bash&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ which bash
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;usr&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is due to some legacy reasons. In older systems &lt;code&gt;bash&lt;&#x2F;code&gt; was located in &lt;code&gt;&#x2F;bin&lt;&#x2F;code&gt;. To stay compliant with these older systems &lt;code&gt;&#x2F;bin&lt;&#x2F;code&gt; is actually a symlink to &lt;code&gt;&#x2F;usr&#x2F;bin&lt;&#x2F;code&gt;. To validate this you run the command &lt;code&gt;ls -l &#x2F;bin&lt;&#x2F;code&gt; and you see in the output &lt;code&gt;&#x2F;bin -&amp;gt; usr&#x2F;bin&lt;&#x2F;code&gt;. This arrow indicates that &lt;code&gt;&#x2F;bin&lt;&#x2F;code&gt; is a symlink to &lt;code&gt;&#x2F;usr&#x2F;bin&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ ls -l &#x2F;bin
&lt;&#x2F;span&gt;&lt;span&gt;lrwxrwxrwx 1 root root 7 Aug  4  2020 &#x2F;bin -&amp;gt; usr&#x2F;bin
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;change-the-default-shell&quot;&gt;Change the default shell&lt;&#x2F;h2&gt;
&lt;p&gt;If you prefer to use a different shell than bash you are free to do so. Lets for example change the default shell to zsh. In order to do so you use the command &lt;code&gt;chsh&lt;&#x2F;code&gt;, short of &lt;strong&gt;ch&lt;&#x2F;strong&gt;ange &lt;strong&gt;sh&lt;&#x2F;strong&gt;ell?&lt;&#x2F;p&gt;
&lt;p&gt;Keep in mind that you don&#x27;t run this command with &lt;code&gt;sudo&lt;&#x2F;code&gt;, since you want to change the shell for your current user not for the root user.&lt;&#x2F;p&gt;
&lt;p&gt;If you have &lt;code&gt;zsh&lt;&#x2F;code&gt; installed you can use it as your default shell with the following command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ chsh -s $(which zsh)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to have this command take effect you at least need to restart your terminal, and maybe even need to log out first.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check the version of bash</title>
		<published>2021-05-05T00:00:00+00:00</published>
		<updated>2021-05-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-get-version/" type="text/html"/>
		<id>https://koenwoortman.com/bash-get-version/</id>
		<content type="html">&lt;p&gt;To find your bash version go in your terminal and echo out the &lt;code&gt;$BASH_VERSION&lt;&#x2F;code&gt; shell variable.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ echo $BASH_VERSION
&lt;&#x2F;span&gt;&lt;span&gt;5.0.17(1)-release
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This &lt;code&gt;$BASH_VERSION&lt;&#x2F;code&gt; shell variable is set as long as you are using bash as your shell variable. So if you are using zsh or fish as a shell environment this variable is not set. In that case you can use the &lt;code&gt;--version&lt;&#x2F;code&gt; flag on the command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash --version
&lt;&#x2F;span&gt;&lt;span&gt;GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
&lt;&#x2F;span&gt;&lt;span&gt;Copyright (C) 2019 Free Software Foundation, Inc.
&lt;&#x2F;span&gt;&lt;span&gt;License GPLv3+: GNU GPL version 3 or later &amp;lt;http:&#x2F;&#x2F;gnu.org&#x2F;licenses&#x2F;gpl.html&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;This is free software; you are free to change and redistribute it.
&lt;&#x2F;span&gt;&lt;span&gt;There is NO WARRANTY, to the extent permitted by law.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can invoke &lt;code&gt;bash&lt;&#x2F;code&gt; as a command like this because bash actually is just another binary program on your system like e.g. &lt;code&gt;cat&lt;&#x2F;code&gt;, &lt;code&gt;nano&lt;&#x2F;code&gt;, &lt;code&gt;tail&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The bash program is located in a folder on your operating system that is included in your PATH. To see where bash is installed you can run the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ which bash
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;usr&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see, for me the bash program&#x2F;command is located in a folder called &lt;em&gt;bin&lt;&#x2F;em&gt;, which is inside a folder called &lt;em&gt;usr&lt;&#x2F;em&gt; which is located at the root of my file system.&lt;&#x2F;p&gt;
&lt;p&gt;Some programs show the version info with the flag &lt;code&gt;-v&lt;&#x2F;code&gt;. If you add the &lt;code&gt;-v&lt;&#x2F;code&gt; flag to the &lt;code&gt;bash&lt;&#x2F;code&gt; command bash will run in verbose mode, in this mode bash will show you as much information as possible. You can safely test this out if you like, you will see another shell get launched showing your shell configuration at startup.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the total amount of git commits via the command line</title>
		<published>2021-05-04T00:00:00+00:00</published>
		<updated>2021-05-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-total-commit-count-via-command-line/" type="text/html"/>
		<id>https://koenwoortman.com/git-total-commit-count-via-command-line/</id>
		<content type="html">&lt;p&gt;When you browse GitHub repositories you nicely see some statistics about the repository including the total amount of commits made for a branch. This may give an indication on how actively a project is or was, this is definitely not always the case.&lt;&#x2F;p&gt;
&lt;p&gt;You can get this total number of commits locally via the command line as well. You can get this number via the &lt;code&gt;rev-list&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;p&gt;In order to get the total commits count of your currently active branch you may use &lt;code&gt;HEAD&lt;&#x2F;code&gt; combined with the &lt;code&gt;--count&lt;&#x2F;code&gt; flag.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rev-list HEAD --count
&lt;&#x2F;span&gt;&lt;span&gt;1233
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of &lt;code&gt;HEAD&lt;&#x2F;code&gt; you can specify a branch name as well, whether it is your current branch or another one like &lt;code&gt;main&lt;&#x2F;code&gt; in the example below.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rev-list main --count
&lt;&#x2F;span&gt;&lt;span&gt;1202
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And it works with remotes as well. Keep in mind however that you may need to &lt;code&gt;fetch&lt;&#x2F;code&gt; first before you see the correct number.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rev-list origin&#x2F;main --count
&lt;&#x2F;span&gt;&lt;span&gt;1237
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you would like to get the count of unique commits on all branches combined you use the &lt;code&gt;--all&lt;&#x2F;code&gt; flag instead of the branch name.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rev-list --all --count
&lt;&#x2F;span&gt;&lt;span&gt;1868
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Using the terminal in ChromeOS</title>
		<published>2021-05-03T00:00:00+00:00</published>
		<updated>2021-05-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/chromeos-terminal/" type="text/html"/>
		<id>https://koenwoortman.com/chromeos-terminal/</id>
		<content type="html">&lt;p&gt;I got myself a Chromebook lately and since it is based on Linux I couldn&#x27;t help looking for a terminal, just for fun.&lt;&#x2F;p&gt;
&lt;p&gt;And for sure, there is one. With the keycode &lt;code&gt;CTRL+ALT+T&lt;&#x2F;code&gt; a Terminal app launches.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-terminal.png&quot; alt=&quot;ChromeOS Terminal&quot; title=&quot;ChromeOS Terminal&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;However, when typing the &lt;code&gt;help&lt;&#x2F;code&gt; command as suggested the result is somewhat disappointing.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-terminal-help.png&quot; alt=&quot;ChromeOS Terminal Help&quot; title=&quot;ChromeOS Terminal Help&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Is this all? And yes, when trying to launch &lt;code&gt;vim&lt;&#x2F;code&gt; the disappointment increases.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-terminal-no-vim.png&quot; alt=&quot;ChromeOS Terminal No Vim&quot; title=&quot;ChromeOS Terminal Help No Vim&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;But after Googling around somewhat I found a Beta feature called &amp;quot;Linux development environment&amp;quot;. Fingers crossed.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-linux-setup.png&quot; alt=&quot;ChromeOS Linux setup&quot; title=&quot;ChromeOS Linux setup&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;After the installation a terminal launched that looks more promising.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-linux-terminal.png&quot; alt=&quot;ChromeOS Linux Terminal&quot; title=&quot;ChromeOS Linux Terminal&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;And indeed, this is more like it :).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;chromeos-linux-vim.png&quot; alt=&quot;ChromeOS Linux Vim&quot; title=&quot;ChromeOS Linux Vim&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Group objects in a list by property value in Python</title>
		<published>2021-05-02T00:00:00+00:00</published>
		<updated>2021-05-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-list-group-by-object-property/" type="text/html"/>
		<id>https://koenwoortman.com/python-list-group-by-object-property/</id>
		<content type="html">&lt;p&gt;Lately I came across a problem for which I had to group objects of a single list by the value of a property of these objects. After trying some things I settled on the &lt;code&gt;groupby&lt;&#x2F;code&gt; function from the &lt;code&gt;itertools&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;p&gt;Lets consider the following example, we have a Letter class with two properties: a string to specify the character and a boolean to specify wether the letter is a vowel or not. We want to group Letter objects based on their &lt;code&gt;vowel&lt;&#x2F;code&gt; property.&lt;&#x2F;p&gt;
&lt;p&gt;This class looks as the following, the &lt;code&gt;__repr__&lt;&#x2F;code&gt; is just there to have a neat string representation when we later print out the objects.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Letter&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;vowel&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        self.char &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;char
&lt;&#x2F;span&gt;&lt;span&gt;        self.vowel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;vowel
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__repr__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;self.char
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next we create a list with Letter objects. Not the entire alphabet, the first couple letters will do.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;e&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now we group our &lt;code&gt;letters&lt;&#x2F;code&gt; list containing &lt;code&gt;Letter&lt;&#x2F;code&gt; objects based on the value of their &lt;code&gt;.vowel&lt;&#x2F;code&gt; property. This grouping is done by the key function we pass to &lt;code&gt;groupby&lt;&#x2F;code&gt; as a second argument, the list itself being the first argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;itertools &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;groupby
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;sorted_letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;sorted&lt;&#x2F;span&gt;&lt;span&gt;(letters, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;: letter.vowel)
&lt;&#x2F;span&gt;&lt;span&gt;grouped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;key, result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;groupby(
&lt;&#x2F;span&gt;&lt;span&gt;    sorted_letters, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;: letter.vowel)]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(grouped)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [[b, c, d], [a, e]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One gotcha is that the list, or any other iterable, needs to be sorted before passing it to the &lt;code&gt;groupby&lt;&#x2F;code&gt; function. Otherwise your groups end up with a segmented result. To illustrate see the following example where the sorting is commented out. As you see the &lt;code&gt;groupby&lt;&#x2F;code&gt; makes the groups consecutively.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;itertools &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;groupby
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# letters = sorted(letters, key=lambda letter: letter.vowel)
&lt;&#x2F;span&gt;&lt;span&gt;grouped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;key, result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;groupby(
&lt;&#x2F;span&gt;&lt;span&gt;    letters, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;: letter.vowel)]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(grouped)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [[a], [b, c, d], [e]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Our entire code sample looks like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;itertools &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;groupby
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Letter&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;vowel&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        self.char &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;char
&lt;&#x2F;span&gt;&lt;span&gt;        self.vowel &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;vowel
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__repr__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;) -&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;self.char
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;b&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;c&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;d&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Letter(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;e&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;sorted_letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;sorted&lt;&#x2F;span&gt;&lt;span&gt;(letters, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;: letter.vowel)
&lt;&#x2F;span&gt;&lt;span&gt;grouped &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;key, result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;groupby(
&lt;&#x2F;span&gt;&lt;span&gt;    sorted_letters, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;key&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;letter&lt;&#x2F;span&gt;&lt;span&gt;: letter.vowel)]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(grouped)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [[b, c, d], [a, e]]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List installed PostgreSQL extensions with version</title>
		<published>2021-05-01T00:00:00+00:00</published>
		<updated>2021-05-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-list-installed-extensions-with-version/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-list-installed-extensions-with-version/</id>
		<content type="html">&lt;p&gt;With extensions you can add some extra functionality to your PostgreSQL database server. Which is a great but you generally don&#x27;t want to have lots of extensions around which you are not using anymore. Therefore it is useful to clean them up once every while. To list the installed PostgreSQL extensions you need to run a SQL statement. For this you can login to the Postgres shell.&lt;&#x2F;p&gt;
&lt;p&gt;Now, to show the installed PostgreSQL extensions with their version numbers you run the following SQL query:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;postgres&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# SELECT name, default_version, installed_version FROM pg_available_extensions WHERE installed_version IS NOT NULL;
&lt;&#x2F;span&gt;&lt;span&gt;  name   | default_version | installed_version
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;---------+-----------------+-------------------
&lt;&#x2F;span&gt;&lt;span&gt; plpgsql | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;             | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt; postgis | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;           | &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; row)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Timeout for a random amount of seconds in JavaScript</title>
		<published>2021-04-30T00:00:00+00:00</published>
		<updated>2021-04-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-timeout-random-amount-of-seconds/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-timeout-random-amount-of-seconds/</id>
		<content type="html">&lt;p&gt;Waiting is boring but with some randomness I might become a little more fun. In JavaScript you use the &lt;code&gt;setTimeout&lt;&#x2F;code&gt; function to run some code in X amount of milliseconds.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;setTimeout&lt;&#x2F;span&gt;&lt;span&gt;(() &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  window.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;alert&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Done waiting&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;floor&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;random&lt;&#x2F;span&gt;&lt;span&gt;() &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;* &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;10000&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With &lt;code&gt;Math.random()&lt;&#x2F;code&gt; we get random floating point number between 0 and 1, including 0. On order to get from a floating point number between 0 and 1 to a proper amount of milliseconds we multiply by &lt;code&gt;10000&lt;&#x2F;code&gt;, which will give us a timeout in a range between 0 seconds and 10 seconds.&lt;&#x2F;p&gt;
&lt;p&gt;When the timeout finished the callback function, passed as the first parameter to &lt;code&gt;setTimeout&lt;&#x2F;code&gt;, is called.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create databases in PostgreSQL</title>
		<published>2021-04-29T00:00:00+00:00</published>
		<updated>2021-04-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-create-database/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-create-database/</id>
		<content type="html">&lt;p&gt;Creating a database in PostgreSQL is done via an SQL statement, and you need the &lt;code&gt;CREATEDB&lt;&#x2F;code&gt; role to be able to do this. In its simplest form the SQL statement looks as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;CREATE DATABASE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;myproject&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Afterwards you may &lt;a href=&quot;&#x2F;postgresql-switch-database-with-connect&#x2F;&quot;&gt;connect to the database&lt;&#x2F;a&gt; with the meta command: &lt;code&gt;\c &amp;lt;database_name&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;What creating a database in PostgreSQL actually does is making a copy from a template database. A template database is exactly what the name suggests it is, it serves as a template for a newly created database. PostgreSQL copies copies over the settings and data from the template to the database you create.&lt;&#x2F;p&gt;
&lt;p&gt;PostgreSQL comes with two template databases: &lt;code&gt;template0&lt;&#x2F;code&gt; and &lt;code&gt;template1&lt;&#x2F;code&gt;, and uses &lt;code&gt;template1&lt;&#x2F;code&gt; by default when you create a new database. The &lt;code&gt;template0&lt;&#x2F;code&gt; is supposed to be a sort of safety net, if you mess something up while customizing &lt;code&gt;template1&lt;&#x2F;code&gt; you can still recover it from &lt;code&gt;template0&lt;&#x2F;code&gt;. Therefore it is advised to make all your customizations in &lt;code&gt;template1&lt;&#x2F;code&gt; and never change &lt;code&gt;template0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;When creating a database you can specify your preferred template database.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;CREATE DATABASE &lt;&#x2F;span&gt;&lt;span&gt;myproject TEMPLATE &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;django_template&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Removing `li` elements from a list with JavaScript</title>
		<published>2021-04-28T00:00:00+00:00</published>
		<updated>2021-04-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-remove-li-elements-from-ul/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-remove-li-elements-from-ul/</id>
		<content type="html">&lt;p&gt;Without having to reach for jQuery, Vue, React or another fancy library you can easily remove the contents of a HTML list with just plain JavaScript. Lets assume that we have the following list in HTML.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is a couple ways we can go about removing the &lt;code&gt;li&lt;&#x2F;code&gt; elements, but we need to start with a selector to store our list in a variable. Since the list has the ID &lt;code&gt;example&lt;&#x2F;code&gt; we can use that to find the list in the DOM.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;list &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;document.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Maybe the easiest way to empty the list is by setting its innerHTML. This will erase all the contents of the list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;list.innerHTML &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will leave you with an empty list, as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;While effective, this approach is somewhat rigorous too. As mentioned before, it will erase all the contents of the list. So if you had something else in your list, like a &lt;code&gt;div&lt;&#x2F;code&gt; for example, it is removed as well.&lt;&#x2F;p&gt;
&lt;p&gt;Of course there is a way to just remove the &lt;code&gt;li&lt;&#x2F;code&gt; elements too. With a &lt;code&gt;for&lt;&#x2F;code&gt; loop we can remove just remove the &lt;code&gt;li&lt;&#x2F;code&gt; elements. We use a different selector here to get all the &lt;code&gt;li&lt;&#x2F;code&gt;&#x27;s of the list instead of the list itself.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span&gt;listElements &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;document.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;querySelectorAll&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;#example li&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;var &lt;&#x2F;span&gt;&lt;span&gt;i &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;; (li &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;listElements[i]); i&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;++&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  li.parentNode.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;removeChild&lt;&#x2F;span&gt;&lt;span&gt;(li);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reference Django settings from your code</title>
		<published>2021-04-27T00:00:00+00:00</published>
		<updated>2021-04-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-reference-settings-from-code/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-reference-settings-from-code/</id>
		<content type="html">&lt;p&gt;Accessing your application settings in Django can be useful for several reasons. You may want to check whether you are running in debug mode or you may want to access some keys specific to your current environment. To access your settings you import a &lt;code&gt;settings&lt;&#x2F;code&gt; object from &lt;code&gt;django.conf&lt;&#x2F;code&gt;, instead of importing the &lt;code&gt;settings&lt;&#x2F;code&gt; module itself.&lt;&#x2F;p&gt;
&lt;p&gt;Note that &lt;code&gt;django.conf.settings&lt;&#x2F;code&gt; is an object and not a python module. Therefore you cannot import individual settings but you should access them via the settings object. This is shown in the example below.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.conf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;settings
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;credentials.update({
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;STRIPE_PUBLIC_KEY&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: settings.STRIPE_PUBLIC_KEY
&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you are defining your own settings, which you are free to do, make sure to define them using an all uppercase name.&lt;&#x2F;p&gt;
&lt;p&gt;The settings object is only for reading settings, not for changing them. The only place where you should change your settings is in your Django settings module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.conf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;settings
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# PLEASE DON&amp;#39;T
&lt;&#x2F;span&gt;&lt;span&gt;settings.STRIPE_PUBLIC_KEY &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Convert negative number to positive in JavaScript</title>
		<published>2021-04-26T00:00:00+00:00</published>
		<updated>2021-04-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-convert-negative-number-to-positive/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-convert-negative-number-to-positive/</id>
		<content type="html">&lt;p&gt;Sometimes you may need to turn a negative number into a positive one. Of course there is the high(or elementary?) school mathematics approach: multiplying two negative numbers gives a positive.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;let &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*= -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(x); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But this only guarantees a positive number if you are sure that your variable is negative. Otherwise you will a negative and positive number which gives a negative obviously.&lt;&#x2F;p&gt;
&lt;p&gt;In that case you may use the &lt;code&gt;Math.abs()&lt;&#x2F;code&gt; function that comes with JavaScript to convert negative number to positive. The &lt;code&gt;Math.abs()&lt;&#x2F;code&gt; function returns the absolute value of the number you pass it as a parameter.&lt;&#x2F;p&gt;
&lt;p&gt;If you pass a negative number to &lt;code&gt;Math.abs()&lt;&#x2F;code&gt; it returns the negation of that number. When you pass it a positive number it returns the number as is, and the same goes for when you pass the number zero.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;abs&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 5
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;abs&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 5
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;Math&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;abs&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By using &lt;code&gt;Math.abs()&lt;&#x2F;code&gt; you are ensured to have a positive number returned to you.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the sum of all values in an array in JavaScript</title>
		<published>2021-04-25T00:00:00+00:00</published>
		<updated>2021-04-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-get-sum-of-values-in-array/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-get-sum-of-values-in-array/</id>
		<content type="html">&lt;p&gt;JavaScript unfortunately doesn&#x27;t implement a method on Array&#x27;s to sum up its values, not currently at least. This is in contrast to for example Python which has the &lt;code&gt;sum()&lt;&#x2F;code&gt; function or the &lt;code&gt;array_sum()&lt;&#x2F;code&gt; function in PHP. In the old school JavaScript days the way to go was by using a good old for-loop.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily we can sum up the values in an array rather quickly with the &lt;code&gt;reduce()&lt;&#x2F;code&gt; method. The &lt;code&gt;reduce()&lt;&#x2F;code&gt; method executes a callback function on all elements in the array except for the first element, unless you pass an initial value to &lt;code&gt;reduce()&lt;&#x2F;code&gt; as a second argument. The first element in the array is used as the initial value(&lt;code&gt;accumulator&lt;&#x2F;code&gt;) if you don&#x27;t specify one yourself.&lt;&#x2F;p&gt;
&lt;p&gt;If you need to sum up the values in an array you can use the following callback function in the &lt;code&gt;reduce()&lt;&#x2F;code&gt; method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;6&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;sum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;values.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;accumulator&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;current&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;accumulator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;current);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(sum); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 40
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To understand the accumulator and current parameters better you may read along with the following example which logs both in each iteration.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Before reduce&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;sum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;values.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;accumulator&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;current&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;accumulator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;current;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;`accumulator: ${accumulator}, current: ${current}, result: ${result}`
&lt;&#x2F;span&gt;&lt;span&gt;  );
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;result;
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;After reduce&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Before reduce
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; accumulator: 4, current: 3, result: 7
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; accumulator: 7, current: 2, result: 9
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; accumulator: 9, current: 1, result: 10
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; After reduce
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(sum); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 10
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see above the elements in the are iterated through from the second element, with the first element being the accumulator in the first iteration. And the result, or better the return value, of each iteration is used as accumulator for the next iteration and eventually as the return value of &lt;code&gt;reduce()&lt;&#x2F;code&gt; itself.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;reduce-on-empty-arrays&quot;&gt;Reduce on empty arrays&lt;&#x2F;h2&gt;
&lt;p&gt;You may encounter a situation where you cannot be certain that you call reduce on an array with elements. If you call &lt;code&gt;.reduce()&lt;&#x2F;code&gt; on an empty array you will encounter the following &lt;code&gt;TypeError&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;[].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;accumulator&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;current&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;accumulator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;current);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Uncaught TypeError: Reduce of empty array with no initial value
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Possibly you can prevent this by checking the array length first. But a cleaner solution, in my opinion, is to provide a initial value to &lt;code&gt;reduce()&lt;&#x2F;code&gt; as a second argument which is a valid accumulator too. So in the case of summing up values it is safe to use &lt;code&gt;0&lt;&#x2F;code&gt; as initial value, since it won&#x27;t affect the value of the sum if the array does contain values.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;[].&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;accumulator&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;current&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;accumulator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;current, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; 0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;be-aware-with-different-types&quot;&gt;Be aware with different types&lt;&#x2F;h2&gt;
&lt;p&gt;Of course, be aware with the usual gotcha&#x27;s when summing up values in JavaScript. Consider the following variation for example where the last element of the array is not an integer but a string. In this case JavaScript sums up 10, 9, 8 and 7 to a value of 34, when the string 6 is encountered will execute a string concatenation instead of summing up 6 as an integer. Resulting in the value of &lt;code&gt;sum&lt;&#x2F;code&gt; being equal to the string &lt;code&gt;346&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;values &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;10&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;9&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;8&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;7&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;6&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;sum &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;values.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;reduce&lt;&#x2F;span&gt;&lt;span&gt;((&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;accumulator&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;current&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;accumulator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;current);
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(sum); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 346
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;console&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;log&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;typeof &lt;&#x2F;span&gt;&lt;span&gt;sum);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List all remotes in a git repository</title>
		<published>2021-04-24T00:00:00+00:00</published>
		<updated>2021-04-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-list-all-remotes/" type="text/html"/>
		<id>https://koenwoortman.com/git-list-all-remotes/</id>
		<content type="html">&lt;p&gt;You may have multiple git remotes configured for you repository. Like me in my forked Django repository on GitHub, where I have set the original Django repository as a second remote. You can easily list git remotes on the command line with the &lt;code&gt;git remote&lt;&#x2F;code&gt; command. This command just shows the local names of the remote repositories. As you can see in the following example:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote
&lt;&#x2F;span&gt;&lt;span&gt;origin
&lt;&#x2F;span&gt;&lt;span&gt;upstream
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can get more information out of the command. By adding &lt;code&gt;-v&lt;&#x2F;code&gt; you get information about the remote urls as well, &lt;code&gt;-v&lt;&#x2F;code&gt; being short for &lt;code&gt;--verbose&lt;&#x2F;code&gt;. The following lists git remotes with urls:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote -v
&lt;&#x2F;span&gt;&lt;span&gt;origin  git@github.com:koenwoortman&#x2F;django.git (fetch)
&lt;&#x2F;span&gt;&lt;span&gt;origin  git@github.com:koenwoortman&#x2F;django.git (push)
&lt;&#x2F;span&gt;&lt;span&gt;upstream        git@github.com:django&#x2F;django.git (fetch)
&lt;&#x2F;span&gt;&lt;span&gt;upstream        git@github.com:django&#x2F;django.git (push)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Django: generic detail view must be called with either an object pk or a slug in the URLconf</title>
		<published>2021-04-23T00:00:00+00:00</published>
		<updated>2021-04-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-generic-detail-view-must-be-called-with-object-pk-or-slug/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-generic-detail-view-must-be-called-with-object-pk-or-slug/</id>
		<content type="html">&lt;p&gt;There is great magic involved in Django its class-based views, the good kind of magic. The kind of magic that saves me lots of time and lines of code. However, using them requires you to follow some conventions.&lt;&#x2F;p&gt;
&lt;p&gt;Such a conventions in Django is using properly named url variables when using detail views. View classes like &lt;code&gt;DetailView&lt;&#x2F;code&gt;, &lt;code&gt;DeleteView&lt;&#x2F;code&gt;, &lt;code&gt;UpdateView&lt;&#x2F;code&gt; rely on the presence of either a url variable with the name &lt;code&gt;pk&lt;&#x2F;code&gt; or &lt;code&gt;slug&lt;&#x2F;code&gt;. These classes inherit a method called &lt;code&gt;get_object&lt;&#x2F;code&gt; from the &lt;code&gt;SingleObjectMixin&lt;&#x2F;code&gt; class. This &lt;code&gt;get_object&lt;&#x2F;code&gt; method is used to find the right model instance to be displayed in the view.&lt;&#x2F;p&gt;
&lt;p&gt;So lets consider the following example: a detail view for a blog post model.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# blog&#x2F;views.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.views &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;generic
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;blog.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Post
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;DetailView&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;generic.DetailView&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    model &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Post
&lt;&#x2F;span&gt;&lt;span&gt;    template_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;blog&#x2F;detail.html&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to comply with the requirements from the &lt;code&gt;get_object&lt;&#x2F;code&gt; method we should add a url containing a variable named either &lt;code&gt;pk&lt;&#x2F;code&gt; or &lt;code&gt;slug&lt;&#x2F;code&gt;. Otherwise we would get an &lt;code&gt;AttributeError&lt;&#x2F;code&gt; from Django with the message: &lt;code&gt;Generic detail view DetailView must be called with either an object pk or a slug in the URLconf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;When using the &lt;code&gt;path()&lt;&#x2F;code&gt; function you should keep the following format in mind when adding url variables: &lt;code&gt;&amp;lt;variable type:variable name&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you prefer to use the primary key for you DetailView you should add it in the &lt;code&gt;urlpatterns&lt;&#x2F;code&gt; like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# urls.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.urls &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;path
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;blog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;views
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;urlpatterns &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;lt;int:pk&amp;gt;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, views.DetailView.as_view()),
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you prefer to use a slug for your DetailView you should first make sure your model implements a SlugField. Then add the slug as &lt;code&gt;&amp;lt;slug:slug&amp;gt;&lt;&#x2F;code&gt;, bit confusing maybe but both the variable type and name are supposed to be named &lt;code&gt;slug&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# urls.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.urls &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;path
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;blog &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;views
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;urlpatterns &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;lt;slug:slug&amp;gt;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, views.DetailView.as_view()),
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List active SSH sessions via the command-line in Linux</title>
		<published>2021-04-22T00:00:00+00:00</published>
		<updated>2021-04-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-list-current-ssh-sessions/" type="text/html"/>
		<id>https://koenwoortman.com/linux-list-current-ssh-sessions/</id>
		<content type="html">&lt;p&gt;Your best bet to show the active SSH sessions on a system is probably to combine the &lt;code&gt;netstat&lt;&#x2F;code&gt; command with &lt;code&gt;grep&lt;&#x2F;code&gt;. The &lt;code&gt;netstat&lt;&#x2F;code&gt; command is used to show statistics on network connections, not just limited to SSH. The &lt;code&gt;netstat&lt;&#x2F;code&gt; command does require root privileges.&lt;&#x2F;p&gt;
&lt;p&gt;By piping the output of &lt;code&gt;netstat&lt;&#x2F;code&gt; to &lt;code&gt;grep&lt;&#x2F;code&gt; you can filter connections properly to just list active SSH sessions. The command looks as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo netstat -tnpa | grep &amp;#39;ESTABLISHED.*sshd&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More generally, you can use the commands &lt;code&gt;users&lt;&#x2F;code&gt;, &lt;code&gt;who&lt;&#x2F;code&gt; and &lt;code&gt;w&lt;&#x2F;code&gt; to list the currently logged in users on the system. Each displaying a different level of details.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;users&lt;&#x2F;code&gt; commands just displays a list of usernames separated by spaces.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ users
&lt;&#x2F;span&gt;&lt;span&gt;koen
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;who&lt;&#x2F;code&gt; command&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ who
&lt;&#x2F;span&gt;&lt;span&gt;koen     pts&#x2F;0        Apr 18 16:28 (80.113.224.189)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;w&lt;&#x2F;code&gt; command&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ w
&lt;&#x2F;span&gt;&lt;span&gt;16:28:47 up 21 days,  5:05,  1 user,  load average: 0.30, 0.13, 0.07
&lt;&#x2F;span&gt;&lt;span&gt;USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
&lt;&#x2F;span&gt;&lt;span&gt;koen     pts&#x2F;0    80.113.224.189   16:28    7.00s  0.19s  0.01s w
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Stop tracking a remote git branch</title>
		<published>2021-04-21T00:00:00+00:00</published>
		<updated>2021-04-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-stop-tracking-remote-branch/" type="text/html"/>
		<id>https://koenwoortman.com/git-stop-tracking-remote-branch/</id>
		<content type="html">&lt;p&gt;Setting an upstream branch can be useful, it tracks if you are commits behind or ahead and you can run &lt;code&gt;git push&lt;&#x2F;code&gt; without the need to specify a remote and branch. However, there might be a place and time where you rather undo this branch tracking. The &lt;code&gt;branch&lt;&#x2F;code&gt; subcommand in git has an option to unset the upstream branch, which you use to stop tracking the remote branch. The command is used as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --unset-upstream [&amp;lt;branchname&amp;gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In practice that would look like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --unset-upstream main
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Keep in mind that you specify the local branch and not the remote branch, so in this case &lt;code&gt;main&lt;&#x2F;code&gt; instead of &lt;code&gt;origin&#x2F;main&lt;&#x2F;code&gt;. Which makes sense because we are detaching the local branch from its remote, not the remote branch from our local branch. The remote repository is completely unaware of the fact that you are locally tracking some of its branches.&lt;&#x2F;p&gt;
&lt;p&gt;You can run the command without specifying a branch name as well. In that case git assumes that you want to stop tracking a remote for your currently active branch.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --unset-upstream
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set the upstream branch for an existing branch in git</title>
		<published>2021-04-20T00:00:00+00:00</published>
		<updated>2021-04-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-set-upstream-for-existing-branch/" type="text/html"/>
		<id>https://koenwoortman.com/git-set-upstream-for-existing-branch/</id>
		<content type="html">&lt;p&gt;Probably the most convenient way to set an upstream branch is to do so when you push the branch. You do so by adding the &lt;code&gt;-u&lt;&#x2F;code&gt; option to the &lt;code&gt;push&lt;&#x2F;code&gt; command, with &lt;code&gt;-u&lt;&#x2F;code&gt; being the short option for &lt;code&gt;--set-upstream&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git push -u &amp;lt;remote&amp;gt; &amp;lt;branch&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For an existing branch you might want to set the upstream branch without having to push at the same time. You can set the upstream branch as well using the command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch (--set-upstream-to=&amp;lt;upstream&amp;gt; | -u &amp;lt;upstream&amp;gt;) [&amp;lt;branchname&amp;gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In practice that would look like the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch -u origin&#x2F;dev
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Delete multiple git branches at once</title>
		<published>2021-04-19T00:00:00+00:00</published>
		<updated>2021-04-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-delete-multiple-branches-at-once/" type="text/html"/>
		<id>https://koenwoortman.com/git-delete-multiple-branches-at-once/</id>
		<content type="html">&lt;p&gt;Cleaning up your local branches is one of my, somewhat boring, recurring tasks. Especially for projects with multiple developers(and running &lt;code&gt;git fetch&lt;&#x2F;code&gt; often) my list of local branches gets quite large.&lt;&#x2F;p&gt;
&lt;p&gt;To remove a git branch you use the &lt;code&gt;git branch -D &amp;lt;branch_names&amp;gt;&lt;&#x2F;code&gt; command. You can pass multiple branch names to this command, separated by a space. Unfortunately &lt;code&gt;git branch -D&lt;&#x2F;code&gt; doesn&#x27;t allow globbing patterns, unlike the &lt;code&gt;rm&lt;&#x2F;code&gt; command for example.&lt;&#x2F;p&gt;
&lt;p&gt;The tab completion in Zsh already goes a long way in making deleting multiple branches faster. But we can do better. With &lt;code&gt;git branch --format &amp;quot;%(refname:short)&amp;quot; | grep &amp;quot;&amp;lt;pattern&amp;gt;&amp;quot; | xargs git branch -D&lt;&#x2F;code&gt; we can use the &lt;code&gt;grep&lt;&#x2F;code&gt; command to specify a pattern for the branch names.&lt;&#x2F;p&gt;
&lt;p&gt;Look at the following for example, which removes git branches with names starting with &lt;code&gt;fix&#x2F;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --format &amp;quot;%(refname:short)&amp;quot; | grep &amp;quot;^fix&#x2F;*&amp;quot; | xargs git branch -D
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you rather have some security first you can run the following command first, which displays the list of branches without deleting them.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch --format &amp;quot;%(refname:short)&amp;quot; | grep &amp;quot;^fix&#x2F;*&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;removing-local-branch-with-prune&quot;&gt;Removing local branch with prune&lt;&#x2F;h2&gt;
&lt;p&gt;Another option would be to use &lt;code&gt;prune&lt;&#x2F;code&gt;. With &lt;code&gt;prune&lt;&#x2F;code&gt; you prune away the branches that don&#x27;t exist on the remote anymore. So the local branches that are not present on the remote repository anymore will be deleted.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git remote prune origin
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List packages that can be upgraded in Ubuntu</title>
		<published>2021-04-18T00:00:00+00:00</published>
		<updated>2021-04-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-list-upgradable-packages-with-apt/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-list-upgradable-packages-with-apt/</id>
		<content type="html">&lt;p&gt;Whether it is your local machine or a production server, it is good practice to keep the packages on your system up-to-date. To just see which packages can be updated you can use the commands &lt;code&gt;apt upgrade --dry-run&lt;&#x2F;code&gt; and &lt;code&gt;apt list --upgradable&lt;&#x2F;code&gt;. The former shows which packages should have been upgraded once you run &lt;code&gt;apt upgrade&lt;&#x2F;code&gt;. The latter lists all the packages that can be upgraded.&lt;&#x2F;p&gt;
&lt;p&gt;You can do a dry-run as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt upgrade --dry-run
&lt;&#x2F;span&gt;&lt;span&gt;Reading package lists... Done
&lt;&#x2F;span&gt;&lt;span&gt;Building dependency tree
&lt;&#x2F;span&gt;&lt;span&gt;Reading state information... Done
&lt;&#x2F;span&gt;&lt;span&gt;Calculating upgrade... Done
&lt;&#x2F;span&gt;&lt;span&gt;The following packages have been kept back:
&lt;&#x2F;span&gt;&lt;span&gt;  mysql-client mysql-server
&lt;&#x2F;span&gt;&lt;span&gt;The following packages will be upgraded:
&lt;&#x2F;span&gt;&lt;span&gt;  google-chrome-stable libjs-underscore libpci3 pciutils
&lt;&#x2F;span&gt;&lt;span&gt;4 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
&lt;&#x2F;span&gt;&lt;span&gt;Inst google-chrome-stable [89.0.4389.114-1] (89.0.4389.128-1 Google:1.0&#x2F;stable [amd64])
&lt;&#x2F;span&gt;&lt;span&gt;Inst pciutils [1:3.6.4-1] (1:3.6.4-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-updates [amd64]) []
&lt;&#x2F;span&gt;&lt;span&gt;Inst libpci3 [1:3.6.4-1] (1:3.6.4-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-updates [amd64])
&lt;&#x2F;span&gt;&lt;span&gt;Inst libjs-underscore [1.9.1~dfsg-1] (1.9.1~dfsg-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-security [all])
&lt;&#x2F;span&gt;&lt;span&gt;Conf google-chrome-stable (89.0.4389.128-1 Google:1.0&#x2F;stable [amd64])
&lt;&#x2F;span&gt;&lt;span&gt;Conf pciutils (1:3.6.4-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-updates [amd64])
&lt;&#x2F;span&gt;&lt;span&gt;Conf libpci3 (1:3.6.4-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-updates [amd64])
&lt;&#x2F;span&gt;&lt;span&gt;Conf libjs-underscore (1.9.1~dfsg-1ubuntu0.20.04.1 Ubuntu:20.04&#x2F;focal-security [all])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And list the upgradable packages like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ apt list --upgradable
&lt;&#x2F;span&gt;&lt;span&gt;Listing... Done
&lt;&#x2F;span&gt;&lt;span&gt;google-chrome-stable&#x2F;stable 89.0.4389.128-1 amd64 [upgradable from: 89.0.4389.114-1]
&lt;&#x2F;span&gt;&lt;span&gt;libjs-underscore&#x2F;focal-security,focal-security 1.9.1~dfsg-1ubuntu0.20.04.1 all [upgradable from: 1.9.1~dfsg-1]
&lt;&#x2F;span&gt;&lt;span&gt;libpci3&#x2F;focal-updates 1:3.6.4-1ubuntu0.20.04.1 amd64 [upgradable from: 1:3.6.4-1]
&lt;&#x2F;span&gt;&lt;span&gt;mysql-client&#x2F;focal-updates,focal-updates,focal-security,focal-security 8.0.23-0ubuntu0.20.04.1 amd64 [upgradable from: 5.7.30-1ubuntu18.04]
&lt;&#x2F;span&gt;&lt;span&gt;mysql-server&#x2F;focal-updates,focal-updates,focal-security,focal-security 8.0.23-0ubuntu0.20.04.1 amd64 [upgradable from: 5.7.30-1ubuntu18.04]
&lt;&#x2F;span&gt;&lt;span&gt;pciutils&#x2F;focal-updates 1:3.6.4-1ubuntu0.20.04.1 amd64 [upgradable from: 1:3.6.4-1]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you are looking for a specific package you can pipe the output to the &lt;code&gt;grep&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;apt list --upgradable | grep &amp;quot;mysql&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;mysql-client&#x2F;focal-updates,focal-updates,focal-security,focal-security 8.0.23-0ubuntu0.20.04.1 amd64 [upgradable from: 5.7.30-1ubuntu18.04]
&lt;&#x2F;span&gt;&lt;span&gt;mysql-server&#x2F;focal-updates,focal-updates,focal-security,focal-security 8.0.23-0ubuntu0.20.04.1 amd64 [upgradable from: 5.7.30-1ubuntu18.04]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;No need to pay too much attention to the warning in this case :-).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use one directory for HTML templates in Django</title>
		<published>2021-04-17T00:00:00+00:00</published>
		<updated>2021-04-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-one-directory-for-templates/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-one-directory-for-templates/</id>
		<content type="html">&lt;p&gt;From which directory Django loads HTML templates is configured with the &lt;code&gt;TEMPLATES&lt;&#x2F;code&gt; variable in &lt;code&gt;settings.py&lt;&#x2F;code&gt;. When you create a fresh Django application &lt;code&gt;TEMPLATES&lt;&#x2F;code&gt; is configured to look for a &lt;code&gt;templates&lt;&#x2F;code&gt; folder in the apps you create to load HTML templates from.&lt;&#x2F;p&gt;
&lt;p&gt;Where Django apps are supposed to be pretty self-containing, my experience is that I have quite some overlap in HTML components on the template side between apps. Think of some shared layouts or generic partials for lists for example.&lt;&#x2F;p&gt;
&lt;p&gt;Therefore I prefer a &lt;code&gt;templates&lt;&#x2F;code&gt; directory outside my apps for some projects. You can configure this by settings &lt;code&gt;&#x27;DIRS&#x27;&lt;&#x2F;code&gt; in the &lt;code&gt;TEMPLATES&lt;&#x2F;code&gt; setting. The following example shows how to configure Django so that it will look for templates in a &lt;code&gt;templates&lt;&#x2F;code&gt; directory in the project root.&lt;&#x2F;p&gt;
&lt;p&gt;The example sets &lt;code&gt;&#x27;APP_DIRS&#x27;&lt;&#x2F;code&gt; to &lt;code&gt;False&lt;&#x2F;code&gt; so Django won&#x27;t look for templates in apps. You can keep this set to &lt;code&gt;True&lt;&#x2F;code&gt; as well if you prefer.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# settings.py
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;TEMPLATES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;BACKEND&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.template.backends.django.DjangoTemplates&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;DIRS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;            os.path.join(BASE_DIR, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;templates&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        ],
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;APP_DIRS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;OPTIONS&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;context_processors&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.template.context_processors.debug&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.template.context_processors.request&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.auth.context_processors.auth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.messages.context_processors.messages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;            ],
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Installing Python packages from a GitHub repo with Pipenv</title>
		<published>2021-04-16T00:00:00+00:00</published>
		<updated>2021-04-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-pipenv-install-package-from-github-repository/" type="text/html"/>
		<id>https://koenwoortman.com/python-pipenv-install-package-from-github-repository/</id>
		<content type="html">&lt;p&gt;Besides installing packages from PyPI you can directly install packages from, for example, GitHub. This feature is not exclusive to Pipenv, pip can do the same just as well.&lt;&#x2F;p&gt;
&lt;p&gt;For side-projects I use (mostly) just by myself I feel pretty save running on the development branch of the Django web framework. To install Django its development branch, called &lt;code&gt;main&lt;&#x2F;code&gt; for Django, you need the location of the git repository.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Besides Git Pipenv should work with the version control systems Bazaar, Subversion and Mercurial as well.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The location of the Django git repository is &lt;code&gt;https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;django.git&lt;&#x2F;code&gt; for HTTP and &lt;code&gt;git@github.com:django&#x2F;django.git&lt;&#x2F;code&gt; for SSH. The &lt;code&gt;pipenv install&lt;&#x2F;code&gt; command expects the following format: &lt;code&gt;&amp;lt;vcs_type&amp;gt;+&amp;lt;scheme&amp;gt;:&#x2F;&#x2F;&amp;lt;repository&amp;gt;@&amp;lt;branch_or_tag&amp;gt;#egg=&amp;lt;package_name&lt;&#x2F;code&gt;. Pipenv recommends you to add the &lt;code&gt;-e&lt;&#x2F;code&gt; option to the install command to install as an editable dependency.&lt;&#x2F;p&gt;
&lt;p&gt;Following the above format the Django install command will look like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ pipenv install -e git+https:&#x2F;&#x2F;github.com&#x2F;django&#x2F;django.git@main#egg=Django
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Installing Django using SSH looks like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ pipenv install -e git+ssh:&#x2F;&#x2F;git@github.com&#x2F;django&#x2F;django.git@main#egg=Django
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you don&#x27;t specify the &lt;code&gt;egg&lt;&#x2F;code&gt; the installation fails with the warning: &amp;quot;pipenv requires an #egg fragment for version controlled dependencies.&amp;quot;&lt;&#x2F;p&gt;
&lt;p&gt;For more information on this topic you can &lt;a href=&quot;https:&#x2F;&#x2F;pipenv-fork.readthedocs.io&#x2F;en&#x2F;latest&#x2F;basics.html#a-note-about-vcs-dependencies&quot;&gt;read the docs&lt;&#x2F;a&gt; :-).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Uninstalling packages on Ubuntu with `apt`</title>
		<published>2021-04-15T00:00:00+00:00</published>
		<updated>2021-04-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ubuntu-uninstall-packages-with-apt-remove/" type="text/html"/>
		<id>https://koenwoortman.com/ubuntu-uninstall-packages-with-apt-remove/</id>
		<content type="html">&lt;p&gt;Everyone in a while it doesn&#x27;t hurt to remove some of the clutter on your Linux system. With the command &lt;code&gt;apt remove &amp;lt;package, ...&amp;gt;&lt;&#x2F;code&gt; you can remove packages from your system. Besides &lt;code&gt;remove&lt;&#x2F;code&gt; you have the &lt;code&gt;apt purge &amp;lt;package, ...&amp;gt;&lt;&#x2F;code&gt; command available to you as well, which removes configuration settings for that package as well.&lt;&#x2F;p&gt;
&lt;p&gt;Removing a package from the command line with &lt;code&gt;apt&lt;&#x2F;code&gt; goes as follows:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt remove pipenv
&lt;&#x2F;span&gt;&lt;span&gt;Reading package lists... Done
&lt;&#x2F;span&gt;&lt;span&gt;Building dependency tree
&lt;&#x2F;span&gt;&lt;span&gt;Reading state information... Done
&lt;&#x2F;span&gt;&lt;span&gt;The following packages were automatically installed and are no longer required:
&lt;&#x2F;span&gt;&lt;span&gt;  ipset libipset13 python3-appdirs python3-distlib python3-filelock python3-importlib-metadata python3-more-itertools python3-virtualenv python3-virtualenv-clone python3-zipp xsltproc
&lt;&#x2F;span&gt;&lt;span&gt;Use &amp;#39;sudo apt autoremove&amp;#39; to remove them.
&lt;&#x2F;span&gt;&lt;span&gt;The following packages will be REMOVED:
&lt;&#x2F;span&gt;&lt;span&gt;  pipenv
&lt;&#x2F;span&gt;&lt;span&gt;0 upgraded, 0 newly installed, 1 to remove and 24 not upgraded.
&lt;&#x2F;span&gt;&lt;span&gt;After this operation, 12,8 MB disk space will be freed.
&lt;&#x2F;span&gt;&lt;span&gt;Do you want to continue? [Y&#x2F;n]
&lt;&#x2F;span&gt;&lt;span&gt;(Reading database ... 267202 files and directories currently installed.)
&lt;&#x2F;span&gt;&lt;span&gt;Removing pipenv (11.9.0-1) ...
&lt;&#x2F;span&gt;&lt;span&gt;Processing triggers for man-db (2.9.1-1) ...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;One interesting line you may see in the commands output is &amp;quot;The following packages were automatically installed and are no longer required&amp;quot;. The packages that are listed below are packages which were installed as a dependency of the package we removed which are no longer necessary now. They are orphaned so to say.&lt;&#x2F;p&gt;
&lt;p&gt;To remove these former dependencies you run the command that is suggested in the output as well:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo apt autoremove
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving `command not found: pipenv`</title>
		<published>2021-04-14T00:00:00+00:00</published>
		<updated>2021-04-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-pipenv-command-not-found/" type="text/html"/>
		<id>https://koenwoortman.com/python-pipenv-command-not-found/</id>
		<content type="html">&lt;p&gt;It may that you installed &lt;code&gt;pipenv&lt;&#x2F;code&gt; and when you try to run it you see a message saying something like: &lt;code&gt;command not found: pipenv&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Most likely you installed pipenv with pip(&lt;code&gt;pip install pipenv&lt;&#x2F;code&gt;) without having the directory where &lt;code&gt;pip&lt;&#x2F;code&gt; installs local dependencies in your PATH. PATH is a shell variable that defines the folders where your shell environment looks for commands. In Linux pip probably installs the &lt;code&gt;pipenv&lt;&#x2F;code&gt; command in &lt;code&gt;~&#x2F;.local&#x2F;bin&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;One way around this that would probably work is to run &lt;code&gt;pip install pipenv&lt;&#x2F;code&gt; with &lt;code&gt;sudo&lt;&#x2F;code&gt;. However, if you do so for all packages you run arbitrary python code with superuser rights. With &lt;code&gt;pipenv&lt;&#x2F;code&gt; I am kinda assuming this is safeish. But you are better off solving this issue for good by adding &lt;code&gt;~&#x2F;.local&#x2F;bin&lt;&#x2F;code&gt; to your PATH.&lt;&#x2F;p&gt;
&lt;p&gt;The file where you need to update your PATH depends on whether you use Bash, ZSH or anything else as a shell.&lt;&#x2F;p&gt;
&lt;p&gt;For bash open the file &lt;code&gt;~&#x2F;.bashrc&lt;&#x2F;code&gt; and add the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;PATH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;.local&#x2F;bin:$PATH&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For Zsh open the file &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; and add the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;.local&#x2F;bin&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;PATH
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List tables in Postgres database</title>
		<published>2021-04-13T00:00:00+00:00</published>
		<updated>2021-04-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-list-tables/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-list-tables/</id>
		<content type="html">&lt;p&gt;To list the tables in a Postgres database you use the &lt;code&gt;\dt&lt;&#x2F;code&gt; meta-command, short for &amp;quot;display tables&amp;quot;? First things first, in order to get a list of tables in your database you first login to the Postgres shell and connect to the database you&#x27;re interested in. You login to the Postgres shell with the &lt;code&gt;psql&lt;&#x2F;code&gt; command. Afterwards, you can &lt;a href=&quot;&#x2F;postgresql-switch-database-with-connect&#x2F;&quot;&gt;connect to a database&lt;&#x2F;a&gt; with &lt;code&gt;\c&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;$ psql
&lt;&#x2F;span&gt;&lt;span&gt;psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1))
&lt;&#x2F;span&gt;&lt;span&gt;Type &amp;quot;help&amp;quot; for help.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;postgres=# \c myproject
&lt;&#x2F;span&gt;&lt;span&gt;You are now connected to database &amp;quot;myproject&amp;quot; as user &amp;quot;postgres&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;myproject=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we are connected to the right database we can show the tables with the &lt;code&gt;\dt&lt;&#x2F;code&gt; command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;myproject=# \dt
&lt;&#x2F;span&gt;&lt;span&gt;         List of relations
&lt;&#x2F;span&gt;&lt;span&gt; Schema |  Name  | Type  |  Owner
&lt;&#x2F;span&gt;&lt;span&gt;--------+--------+-------+----------
&lt;&#x2F;span&gt;&lt;span&gt; public | users  | table | postgres
&lt;&#x2F;span&gt;&lt;span&gt; public | pizzas | table | postgres
&lt;&#x2F;span&gt;&lt;span&gt;(2 rows)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;myproject=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When you run this meta-command you might see a message saying &amp;quot;Did not find any relations&amp;quot;. You get this message when you have a database for which there aren&#x27;t any tables yet.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;myproject=# \dt
&lt;&#x2F;span&gt;&lt;span&gt;Did not find any relations.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Switch databases in PostgreSQL with `\connect`</title>
		<published>2021-04-12T00:00:00+00:00</published>
		<updated>2021-04-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-switch-database-with-connect/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-switch-database-with-connect/</id>
		<content type="html">&lt;p&gt;With good ol&#x27; MySQL I was used to switch database with &lt;code&gt;use &amp;lt;db_name&amp;gt;;&lt;&#x2F;code&gt;. Postgres has a different way to switch databases, you do so using one of its meta-commands. Once you are in the Postgres terminal, you enter using the &lt;code&gt;psql&lt;&#x2F;code&gt; command, you can switch databases with &lt;code&gt;\connect &amp;lt;db_name&amp;gt;&lt;&#x2F;code&gt; command or with the shorter &lt;code&gt;\c &amp;lt;db_name&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;postgres=# \c myproject
&lt;&#x2F;span&gt;&lt;span&gt;You are now connected to database &amp;quot;myproject&amp;quot; as user &amp;quot;postgres&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;myproject=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;postgres=# \connect myproject
&lt;&#x2F;span&gt;&lt;span&gt;You are now connected to database &amp;quot;myproject&amp;quot; as user &amp;quot;postgres&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;myproject=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to &lt;a href=&quot;&#x2F;postgresql-list-databases&#x2F;&quot;&gt;list the available databases&lt;&#x2F;a&gt; you use the &lt;code&gt;\l&lt;&#x2F;code&gt; or &lt;code&gt;\list&lt;&#x2F;code&gt; meta-command.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List databases in PostgreSQL</title>
		<published>2021-04-11T00:00:00+00:00</published>
		<updated>2021-04-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-list-databases/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-list-databases/</id>
		<content type="html">&lt;p&gt;To list the databases in Postgres we are going to use the interactive terminal. To do so you might need to switch to the Postgres user with &lt;code&gt;sudo su postgres&lt;&#x2F;code&gt;. You can enter the Postgres terminal with the &lt;code&gt;psql&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;$ psql
&lt;&#x2F;span&gt;&lt;span&gt;psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1))
&lt;&#x2F;span&gt;&lt;span&gt;Type &amp;quot;help&amp;quot; for help.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;postgres=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we are in the shell we can use one of Postgres meta-commands to list the databases. The one we need in this case is &lt;code&gt;\l&lt;&#x2F;code&gt;, which is short for &lt;code&gt;\list&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;postgres=# \l
&lt;&#x2F;span&gt;&lt;span&gt;                                       List of databases
&lt;&#x2F;span&gt;&lt;span&gt;     Name     |  Owner   | Encoding |   Collate   |    Ctype    |       Access privileges
&lt;&#x2F;span&gt;&lt;span&gt;--------------+----------+----------+-------------+-------------+-------------------------------
&lt;&#x2F;span&gt;&lt;span&gt; myproject    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
&lt;&#x2F;span&gt;&lt;span&gt; postgres     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
&lt;&#x2F;span&gt;&lt;span&gt; template0    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c&#x2F;postgres                  +
&lt;&#x2F;span&gt;&lt;span&gt;              |          |          |             |             | postgres=CTc&#x2F;postgres
&lt;&#x2F;span&gt;&lt;span&gt; template1    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c&#x2F;postgres                  +
&lt;&#x2F;span&gt;&lt;span&gt;              |          |          |             |             | postgres=CTc&#x2F;postgres
&lt;&#x2F;span&gt;&lt;span&gt;(4 rows)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List users in PostgreSQL</title>
		<published>2021-04-10T00:00:00+00:00</published>
		<updated>2021-04-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-list-users/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-list-users/</id>
		<content type="html">&lt;p&gt;Listing all the users of a PostgreSQL database is easiest via the interactive terminal. You can start the interactive PostgreSQL terminal with the &lt;code&gt;psql&lt;&#x2F;code&gt; on the command line. If this throws an error with the message &lt;code&gt;psql: error: FATAL: role &amp;quot;...&amp;quot; does not exist&lt;&#x2F;code&gt; you probably haven&#x27;t yet created a database role for the operating system user you are logged in as. You might want to fix that too. In the mean time you can probably switch to the postgres user with a command like &lt;code&gt;sudo su postgres&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Once you get in the Postgres shell you can use the &lt;code&gt;\du&lt;&#x2F;code&gt; meta-command to show the existing PostgreSQL users. With &lt;code&gt;du&lt;&#x2F;code&gt; being short for &amp;quot;display users&amp;quot; I imagine.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;postgres=# \du
&lt;&#x2F;span&gt;&lt;span&gt;                                       List of roles
&lt;&#x2F;span&gt;&lt;span&gt;    Role name |                         Attributes                         | Member of
&lt;&#x2F;span&gt;&lt;span&gt;--------------+------------------------------------------------------------+-----------
&lt;&#x2F;span&gt;&lt;span&gt; postgres     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
&lt;&#x2F;span&gt;&lt;span&gt; sitetemplate |                                                            | {}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;postgres=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re just interested in information about a single user you can filter this list on the username.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;postgres=# \du postgres
&lt;&#x2F;span&gt;&lt;span&gt;                                   List of roles
&lt;&#x2F;span&gt;&lt;span&gt; Role name |                         Attributes                         | Member of
&lt;&#x2F;span&gt;&lt;span&gt;-----------+------------------------------------------------------------+-----------
&lt;&#x2F;span&gt;&lt;span&gt; postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;postgres=#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;listing-postgres-users-with-sql&quot;&gt;Listing Postgres users with SQL&lt;&#x2F;h2&gt;
&lt;p&gt;If you rather take the SQL route over the meta-command there is an option too. If you have rights to query from &lt;code&gt;pg_catalog&lt;&#x2F;code&gt; at least, which shouldn&#x27;t be a problem if you use the default &lt;code&gt;postgres&lt;&#x2F;code&gt; user.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;postgres&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# SELECT usename FROM pg_catalog.pg_user;
&lt;&#x2F;span&gt;&lt;span&gt;     usename
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;------------------
&lt;&#x2F;span&gt;&lt;span&gt; postgres
&lt;&#x2F;span&gt;&lt;span&gt; sitetemplate
&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt; rows)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;postgres&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>The Python walrus operator</title>
		<published>2021-04-09T00:00:00+00:00</published>
		<updated>2021-04-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-walrus-operator/" type="text/html"/>
		<id>https://koenwoortman.com/python-walrus-operator/</id>
		<content type="html">&lt;p&gt;Since the &lt;a href=&quot;https:&#x2F;&#x2F;docs.python.org&#x2F;3&#x2F;whatsnew&#x2F;3.8.html&quot;&gt;release&lt;&#x2F;a&gt; of Python 3.8 we can assign values to variables within an expression using the walrus operator(&lt;code&gt;:=&lt;&#x2F;code&gt;), also more formally known as the assignment expressions.&lt;&#x2F;p&gt;
&lt;p&gt;An advantage of being able to assign a variable in an if-statement that you only use within the scope of that if statement. This often allows you to write shorter code. For example you could write:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(exit_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:= &lt;&#x2F;span&gt;&lt;span&gt;command.run()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;exited with non-zero status (&lt;&#x2F;span&gt;&lt;span&gt;{exit_code}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;)&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Instead of this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;exit_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;command.run()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;exit_code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;exited with non-zero status (&lt;&#x2F;span&gt;&lt;span&gt;{exit_code}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;)&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same logic can be useful in while structures too. The following for example is a while loop that requires user input, and quits the while loop when &amp;quot;exit&amp;quot; is entered.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;while &lt;&#x2F;span&gt;&lt;span&gt;(line &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;input&lt;&#x2F;span&gt;&lt;span&gt;()) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;exit&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    f.write(line)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Relative line numbers in Visual Studio Code</title>
		<published>2021-04-08T00:00:00+00:00</published>
		<updated>2021-04-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vs-code-relative-line-numbers/" type="text/html"/>
		<id>https://koenwoortman.com/vs-code-relative-line-numbers/</id>
		<content type="html">&lt;p&gt;The Vim extension in Visual Studio Code is one of the extension I use most pleasurably. Especially in with the Neovim Integration enabled. However, in (neo)vim I prefer to use &lt;a href=&quot;&#x2F;vim-relative-line-numbers&#x2F;&quot;&gt;relative line numbers&lt;&#x2F;a&gt;. It makes jumping and using commands that span multiple lines much easier.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily, using relative line numbers in Visual Studio Code doesn&#x27;t require you to install another extension. It is supported in VS Code itself.&lt;&#x2F;p&gt;
&lt;p&gt;Open up the settings with &lt;code&gt;Ctrl+,&lt;&#x2F;code&gt; or via &lt;code&gt;Preferences &amp;gt; Settings&lt;&#x2F;code&gt;. In here look for the setting called &amp;quot;Editor: Line Numbers&amp;quot; and set it to &amp;quot;relative&amp;quot; for relative line numbers.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;vscode-relative-line-numbers-settings.png&quot; alt=&quot;Visual Studio Code command palette&quot; title=&quot;Visual Studio Code command palette&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If your prefer to change the settings in the &lt;code&gt;settings.json&lt;&#x2F;code&gt; you can do so by adding the following line.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;editor.lineNumbers&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;relative&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That is all :).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Solving Django warning `Auto-created primary key used when not defining a primary key type`</title>
		<published>2021-04-07T00:00:00+00:00</published>
		<updated>2021-04-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-auto-created-primary-key-used-when-not-defining-primary-key-type/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-auto-created-primary-key-used-when-not-defining-primary-key-type/</id>
		<content type="html">&lt;p&gt;While playing around with the newly released Django 3.2 I noticed a warning I didn&#x27;t see before in a website I migrated from Django 3.1 (Warning &lt;code&gt;models.W042&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;System check identified some issues:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;WARNINGS:
&lt;&#x2F;span&gt;&lt;span&gt;users.User: (models.W042) Auto-created primary key used when not defining a primary key type, by default &amp;#39;django.db.models.AutoField&amp;#39;.
&lt;&#x2F;span&gt;&lt;span&gt;HINT: Configure the DEFAULT_AUTO_FIELD setting or the UsersConfig.default_auto_field attribute to point to a subclass of AutoField, e.g. &amp;#39;django.db.models.BigAutoField&amp;#39;.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The warning is introduced with &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;3.2&#x2F;releases&#x2F;3.2&#x2F;#customizing-type-of-auto-created-primary-keys&quot;&gt;a new feature&lt;&#x2F;a&gt; in Django 3.2 which allows to change the default field of primary which are automatically added by Django if you don&#x27;t explicitly define one.&lt;&#x2F;p&gt;
&lt;p&gt;You can explicitly define a primary key by adding &lt;code&gt;primary_key=True&lt;&#x2F;code&gt; to a model field. If you don&#x27;t, Django adds a primary key itself called &lt;code&gt;id&lt;&#x2F;code&gt;. Before Django 3.2, the primary key field that Django would add was of type &lt;code&gt;&#x27;django.db.models.AutoField&#x27;&lt;&#x2F;code&gt;. Since Django 3.2 the default has been changed to &lt;code&gt;&#x27;django.db.models.BigAutoField&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;BigAutoField&lt;&#x2F;code&gt; probably represents a 64-bit positive integer in your database. Where the &lt;code&gt;AutoField&lt;&#x2F;code&gt; most likely a 32-bit positive integer. This change in Django implies that you will be able to store a whole lot more rows in the database table of the model.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;solution&quot;&gt;Solution&lt;&#x2F;h2&gt;
&lt;p&gt;In order to get rid of the warning you need to set the &lt;code&gt;DEFAULT_AUTO_FIELD&lt;&#x2F;code&gt; in the settings to the field you prefer for primary keys. If you rather keep the Django 3.1 default you should set the auto-field value to &lt;code&gt;&#x27;django.db.models.AutoField&#x27;&lt;&#x2F;code&gt;. This can also prevent some unwanted migrations in the future. You can either set the value in your settings:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;DEFAULT_AUTO_FIELD &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.db.models.AutoField&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or set it per app in the &lt;code&gt;apps.py&lt;&#x2F;code&gt; file:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.apps &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;AppConfig
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;UsersConfig&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;AppConfig&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    default_auto_field &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.db.models.AutoField&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can of course also explicitly set the primary on the model itself.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>`Class has no &#x27;objects&#x27; member` error with Pylint</title>
		<published>2021-04-06T00:00:00+00:00</published>
		<updated>2021-04-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-pylint-class-has-no-objects-member/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-pylint-class-has-no-objects-member/</id>
		<content type="html">&lt;p&gt;When you&#x27;re using Pylint with Django you may encounter warnings like &lt;code&gt;Class ‘Question’ has no ‘objects’ member pylint(no-member)&lt;&#x2F;code&gt;. Pylint helps you to find possible problems with your code, in this case it doesn&#x27;t mean there is an error per se.&lt;&#x2F;p&gt;
&lt;p&gt;In order to use Pylint effectively with Django make sure that you have the Django plugin for Pylint installed. Since that is just a Python package you can install it with &lt;code&gt;pip&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ pip install pylint-django
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re using a Pylint extension in your editor you might need to add some config settings.&lt;&#x2F;p&gt;
&lt;p&gt;With Visual Studio Code you can add the following snippet to your editor settings.json. To open these settings with the shortcut &lt;code&gt;ctrl-shift-p&lt;&#x2F;code&gt; (or &lt;code&gt;cmd-shift-p&lt;&#x2F;code&gt; on Mac) for the Command Palette and run the command: &lt;strong&gt;Preferences: Open Settings (JSON)&lt;&#x2F;strong&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;vscode-command-open-settings.png&quot; alt=&quot;Visual Studio Code command palette&quot; title=&quot;Visual Studio Code command palette&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This opens up the settings JSON file. Here you should either look for the &lt;code&gt;python.linting.pylintArgs&lt;&#x2F;code&gt; or add it yourself and make sure that &lt;code&gt;--load-plugins=pylint_django&lt;&#x2F;code&gt; is passed as an argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;python.linting.pylintArgs&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: [
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;--load-plugins=pylint_django&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  ],
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another possible issue with Visual Studio Code can be Python interpreter that is used. This should be same Python for which you installed &lt;code&gt;pylint-django&lt;&#x2F;code&gt;. This is especially something you want to pay attention to if you&#x27;re using a virtual environment. If so, make sure that you select the Python interpreter in your virtual environment. You can do this by running the &lt;strong&gt;Python: Select Interpreter&lt;&#x2F;strong&gt; command that can be found in the VS Code Command Palette as well.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Running sudo commands without a password</title>
		<published>2021-04-05T00:00:00+00:00</published>
		<updated>2021-04-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-sudo-command-without-password/" type="text/html"/>
		<id>https://koenwoortman.com/linux-sudo-command-without-password/</id>
		<content type="html">&lt;p&gt;The fact that a password is required to run a command with sudo is a useful safety mechanism, which I definitely value on production environments. On my local development machine however, I manage well without the password. You can do so by adding a line like &lt;code&gt;%koen ALL=(ALL:ALL) NOPASSWD: ALL&lt;&#x2F;code&gt; to the sudoers config file, where you swap out my username of course.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;First a word on &lt;code&gt;sudo&lt;&#x2F;code&gt; itself, &lt;code&gt;sudo&lt;&#x2F;code&gt; stands for &amp;quot;superuser do&amp;quot; and it is a program that allows you, if you have the permissions, to run commands as a different user. Most often it is used to run commands as the root user.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;&lt;code&gt;sudo&lt;&#x2F;code&gt; is just another program that comes installed on your UNIX machine that is configured in the config file located at &lt;code&gt;&#x2F;etc&#x2F;sudoers&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Do &lt;u&gt;not&lt;&#x2F;u&gt; edit this file with normal text editor because it can break the sudo command.&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;You should use the special &lt;code&gt;visudo&lt;&#x2F;code&gt; command&#x2F;editor to edit the &lt;code&gt;&#x2F;etc&#x2F;sudoers&lt;&#x2F;code&gt; config file since it validates whether you have no syntax errors.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo visudo
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This opens up the &lt;code&gt;&#x2F;etc&#x2F;sudoers&lt;&#x2F;code&gt; with your preferred editor, and I guess it defaults to vi(m) since the command is &lt;code&gt;visudo&lt;&#x2F;code&gt; :). If you rather use a different editor to edit the sudoers file you can do so by setting the &lt;code&gt;EDITOR&lt;&#x2F;code&gt; environment variable. For example, the following opens the sudoers file with &lt;code&gt;nano&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo EDITOR=nano visudo
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Whether you use vim, nano or something else. The command opens a config file looking somewhat like the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;# User privilege specification
&lt;&#x2F;span&gt;&lt;span&gt;root    ALL=(ALL:ALL) ALL
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Members of the admin group may gain root privileges
&lt;&#x2F;span&gt;&lt;span&gt;%admin ALL=(ALL) ALL
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Allow members of group sudo to execute any command
&lt;&#x2F;span&gt;&lt;span&gt;%sudo   ALL=(ALL:ALL) ALL
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to be able to run &lt;code&gt;sudo&lt;&#x2F;code&gt; without a password you need to add the line &lt;code&gt;%koen ALL=(ALL:ALL) NOPASSWD: ALL&lt;&#x2F;code&gt; to this sudoers file. Where you swap &lt;code&gt;koen&lt;&#x2F;code&gt; for your own username of course. You can get your username by using the command &lt;code&gt;id -un&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So adding that line for your current user leaves the config file something like the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;# User privilege specification
&lt;&#x2F;span&gt;&lt;span&gt;root    ALL=(ALL:ALL) ALL
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Members of the admin group may gain root privileges
&lt;&#x2F;span&gt;&lt;span&gt;%admin ALL=(ALL) ALL
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Allow members of group sudo to execute any command
&lt;&#x2F;span&gt;&lt;span&gt;%sudo   ALL=(ALL:ALL) ALL
&lt;&#x2F;span&gt;&lt;span&gt;%koen   ALL=(ALL:ALL) NOPASSWD: ALL
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Count the amount of child nodes of an HTML element with JavaScript</title>
		<published>2021-04-04T00:00:00+00:00</published>
		<updated>2021-04-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/javascript-count-child-nodes-of-html-element/" type="text/html"/>
		<id>https://koenwoortman.com/javascript-count-child-nodes-of-html-element/</id>
		<content type="html">&lt;p&gt;It is quite simple to count the amount of direct child nodes of an HTML element with regular JavaScript without jQuery or any other fancy stuff.&lt;&#x2F;p&gt;
&lt;p&gt;You can call the &lt;a href=&quot;https:&#x2F;&#x2F;developer.mozilla.org&#x2F;en-US&#x2F;docs&#x2F;Web&#x2F;API&#x2F;Element&#x2F;childElementCount&quot;&gt;.childElementCount&lt;&#x2F;a&gt; property on the element to get the amount of child nodes. Take the following snippets for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;html&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-html &quot;&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;...&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;li&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;ul&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;javascript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-javascript &quot;&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span&gt;document.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;getElementById&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;example&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;).childElementCount;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; =&amp;gt; 2
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;document.getElementById(&#x27;example&#x27;)&lt;&#x2F;code&gt; returns a parent node, on which we call the &lt;code&gt;childElementCount&lt;&#x2F;code&gt; returning the amount of child nodes.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Resolving `ModuleNotFoundError: No module named &#x27;numpy&#x27;`</title>
		<published>2021-04-03T00:00:00+00:00</published>
		<updated>2021-04-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-modulenotfounderror-no-module-named-numpy/" type="text/html"/>
		<id>https://koenwoortman.com/python-modulenotfounderror-no-module-named-numpy/</id>
		<content type="html">&lt;p&gt;When you encounter the error &lt;code&gt;ModuleNotFoundError: No module named &#x27;numpy&#x27;&lt;&#x2F;code&gt; you most likely have not, or not correctly, installed numpy.&lt;&#x2F;p&gt;
&lt;p&gt;If you haven&#x27;t installed it yet you can do so by running:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ python3&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -m&lt;&#x2F;span&gt;&lt;span&gt; pip install numpy
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Assuming that &lt;code&gt;python&lt;&#x2F;code&gt; still refers to a version of python 2 on your machine. Otherwise there is no need to run the install with &lt;code&gt;python3&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The problem lies somewhere else if the above install command displays a line like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;Requirement already satisfied: numpy in &#x2F;home&#x2F;koen&#x2F;.local&#x2F;lib&#x2F;python3.8&#x2F;site-packages (1.20.2)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In that case &lt;code&gt;numpy&lt;&#x2F;code&gt; is actually installed but python just cannot seem to find it. Possible causes can be:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;that you run your python code with a different python version for which you installed numpy.&lt;&#x2F;li&gt;
&lt;li&gt;that not yet activated your python virtual environment yet.&lt;&#x2F;li&gt;
&lt;li&gt;not yet select the right python interpreter in your code editor&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Start the Windows Terminal in the WSL home directory</title>
		<published>2021-04-02T00:00:00+00:00</published>
		<updated>2021-04-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/wsl-start-windows-terminal-in-home-directory/" type="text/html"/>
		<id>https://koenwoortman.com/wsl-start-windows-terminal-in-home-directory/</id>
		<content type="html">&lt;p&gt;I have bought myself a cheap Windows laptop lately. Mostly because it comes in handy to have an installation of Microsoft Word. But the Windows Subsystem for Linux (WSL) caught my interest too. Without too much of a hassle you can get yourself a Linux distro to run in Linux. Next, the very first thing you should do is get the &lt;a href=&quot;https:&#x2F;&#x2F;aka.ms&#x2F;terminal&quot;&gt;Windows Terminal&lt;&#x2F;a&gt; from the Microsoft Store. It just works some much nicer than cmd.&lt;&#x2F;p&gt;
&lt;p&gt;One thing that really annoys me when opening up my WSL in the Windows Terminal is the fact that it does not open in the home directory of the Linux installation. This flaw can luckily be fixed in the &lt;code&gt;settings.json&lt;&#x2F;code&gt; file of the Windows Terminal. You should be able to open this settings file by using the &lt;code&gt;&amp;lt;CTRL&amp;gt;+,&lt;&#x2F;code&gt; shortcut in the Windows Terminal.&lt;&#x2F;p&gt;
&lt;p&gt;By adding &lt;code&gt;&amp;quot;commandline&amp;quot; : &amp;quot;wsl.exe ~&amp;quot;&lt;&#x2F;code&gt; to the WSL profile you can get your WSL to open in the home directory. This makes the settings file looks something like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;json&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-json &quot;&gt;&lt;code class=&quot;language-json&quot; data-lang=&quot;json&quot;&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$schema&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;https:&#x2F;&#x2F;aka.ms&#x2F;terminal-profiles-schema&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;defaultProfile&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;profiles&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;defaults&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Put settings here that you want to apply to all profiles.
&lt;&#x2F;span&gt;&lt;span&gt;        },
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;list&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;        [
&lt;&#x2F;span&gt;&lt;span&gt;            {
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;guid&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;{07b52e3e-de2c-5db4-bd2d-ba144ed6c273}&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;hidden&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;name&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Ubuntu-20.04&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;source&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Windows.Terminal.Wsl&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;colorScheme&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Tango Light&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;                &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;commandline&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;wsl.exe ~&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;            },
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        ]
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;...&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Clear the contents of a file from the command line</title>
		<published>2021-04-01T00:00:00+00:00</published>
		<updated>2021-04-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-clear-file-contents/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-clear-file-contents/</id>
		<content type="html">&lt;p&gt;From the command-line you can empty a file without opening an editor and removing all the lines. This can be useful when you have a bulky log file you don&#x27;t need to keep for example.&lt;&#x2F;p&gt;
&lt;p&gt;With the &lt;code&gt;truncate&lt;&#x2F;code&gt; command you can shrink the file to a specified size. If you specify a size of &lt;code&gt;0&lt;&#x2F;code&gt; you effectively end up with an empty file. This even works when the file is currently being written to.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ truncate -s 0 huge.log
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A second option is to use the output redirection operator(&lt;code&gt;&amp;gt;&lt;&#x2F;code&gt;). You use the output redirection operator to write standard output to a file, and overwrite the file if it already exists.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ &amp;gt; huge.log
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Split the zshrc file into multiple files</title>
		<published>2021-03-31T00:00:00+00:00</published>
		<updated>2021-03-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-split-zshrc-into-multiple-files/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-split-zshrc-into-multiple-files/</id>
		<content type="html">&lt;p&gt;When you get a long way with configuring ZSH your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; can get a bit bulky, filled with aliases and more.&lt;&#x2F;p&gt;
&lt;p&gt;At some point you might prefer to split up your &lt;code&gt;.zshrc&lt;&#x2F;code&gt; file into multiple files. You can easily do so by creating a config directory with files that you source from your &lt;code&gt;.zshrc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;I chose the location &lt;code&gt;&amp;quot;$HOME&#x2F;.config&#x2F;zsh&#x2F;config.d&#x2F;&amp;quot;&lt;&#x2F;code&gt; to store separated config files, here I have the following files for example: &lt;code&gt;aliases.zsh&lt;&#x2F;code&gt;, &lt;code&gt;history.zsh&lt;&#x2F;code&gt;, &lt;code&gt;fzf.zsh&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;From my &lt;code&gt;.zshrc&lt;&#x2F;code&gt; I loop over all the files in &lt;code&gt;&amp;quot;$HOME&#x2F;.config&#x2F;zsh&#x2F;config.d&#x2F;&lt;&#x2F;code&gt; with a &lt;code&gt;.zsh&lt;&#x2F;code&gt; file extension and source them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Load seperated config files
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; conf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;.config&#x2F;zsh&#x2F;config.d&#x2F;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span&gt;.zsh&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;source &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${conf}&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;done
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;unset&lt;&#x2F;span&gt;&lt;span&gt; conf
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Command history in ZSH</title>
		<published>2021-03-30T00:00:00+00:00</published>
		<updated>2021-03-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-command-history/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-command-history/</id>
		<content type="html">&lt;p&gt;If you&#x27;re a user of oh-my-zsh most of the history are taken care of already. However, when you&#x27;re setting up ZSH from scratch yourself you need to configure some settings before the command history is up and running.&lt;&#x2F;p&gt;
&lt;p&gt;First, the file where the history will be stored. Unlike Bash, Zsh doesn&#x27;t provide a default location for where to store command history. So you need to set it yourself in your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file. I went for the file name &lt;code&gt;.zsh_history&lt;&#x2F;code&gt; but feel free to set this to whatever you prefer, it also doesn&#x27;t need be located in your home director.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# The file where the history is stored
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;HISTFILE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;.zsh_history&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Second is related to the length of the history file; how much command you will keep in history. There are two environment variables that relate to this. First is the &lt;code&gt;HISTSIZE&lt;&#x2F;code&gt; variable, which defines how many commands are loaded into memory from the history file. Second is the &lt;code&gt;SAVEHIST&lt;&#x2F;code&gt; variable, which defines how many commands are stored in the history file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Number of events loaded into memory
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;HISTSIZE&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;10000
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Number of events stored in the zsh history file
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;SAVEHIST&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;10000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That covers at least the basics. Next are a couple options I personally prefer as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Do not save duplicate commands to history
&lt;&#x2F;span&gt;&lt;span&gt;setopt HIST_IGNORE_ALL_DUPS
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Do not find duplicate command when searching
&lt;&#x2F;span&gt;&lt;span&gt;setopt HIST_FIND_NO_DUPS
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;clear-history&quot;&gt;Clear history&lt;&#x2F;h2&gt;
&lt;p&gt;Since the ZSH history is just kept in a file, clearing the history is just clearing the file. You can easily clear the contents of a file with a single command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ truncate&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s&lt;&#x2F;span&gt;&lt;span&gt; 0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${HISTFILE}&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;show-all-historic-commands&quot;&gt;Show all historic commands&lt;&#x2F;h2&gt;
&lt;p&gt;To show the entire ZSH history you can open the history file with your preferred pager:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ less &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${HISTFILE}&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Quick cd&#x27;ing in ZSH by setting a `cdpath`</title>
		<published>2021-03-29T00:00:00+00:00</published>
		<updated>2021-03-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-cdpath/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-cdpath/</id>
		<content type="html">&lt;p&gt;There are directories I am &lt;code&gt;cd&lt;&#x2F;code&gt;&#x27;ing to more often than others. When my early Linux days when I was using the Bash shell I often used &lt;code&gt;cd&lt;&#x2F;code&gt; commands like: &lt;code&gt;cd ..&#x2F;..&#x2F;..&#x2F;Code&#x2F;blog&lt;&#x2F;code&gt;. For me, &lt;code&gt;~&#x2F;Code&lt;&#x2F;code&gt; is where all my development projects are located, so I often switch to subdirectories of this &lt;code&gt;~&#x2F;Code&lt;&#x2F;code&gt; directory. The ZSH shell allows you to add other directories to the search path of the &lt;code&gt;cd&lt;&#x2F;code&gt;, by doing this you can &lt;code&gt;cd&lt;&#x2F;code&gt; into subdirectories of &lt;code&gt;~&#x2F;Code&lt;&#x2F;code&gt; (in my case) from any location.&lt;&#x2F;p&gt;
&lt;p&gt;In order to add directories to the search path of the &lt;code&gt;cd&lt;&#x2F;code&gt; command you should set &lt;code&gt;cdpath&lt;&#x2F;code&gt; in your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;setopt auto_cd
&lt;&#x2F;span&gt;&lt;span&gt;cdpath&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;($HOME&#x2F;Code)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By adding the above, you are able to directly change to a subdirectory of &lt;code&gt;~&#x2F;Code&lt;&#x2F;code&gt; from any location. You should see these subdirectories too in your autosuggestion and get tabcompletion as well.&lt;&#x2F;p&gt;
&lt;p&gt;You can add as many directories to the &lt;code&gt;cdpath&lt;&#x2F;code&gt; array as you would like:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;setopt auto_cd
&lt;&#x2F;span&gt;&lt;span&gt;cdpath&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;($HOME&#x2F;Code $HOME&#x2F;.config)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Available data types in SQLite databases</title>
		<published>2021-03-28T00:00:00+00:00</published>
		<updated>2021-03-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/sqlite-data-types/" type="text/html"/>
		<id>https://koenwoortman.com/sqlite-data-types/</id>
		<content type="html">&lt;p&gt;SQLite databases come with just five primitive data types, which are called storage classes. Which, to be fair, is enough to do the basics :). These five storage classes in SQLite are: &lt;code&gt;INTEGER&lt;&#x2F;code&gt;, &lt;code&gt;REAL&lt;&#x2F;code&gt;, &lt;code&gt;TEXT&lt;&#x2F;code&gt;, &lt;code&gt;NULL&lt;&#x2F;code&gt; and &lt;code&gt;BLOB&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So all values you store in SQLite belong to one of these storage classes:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;&lt;div style=&quot;width:110px&quot;&gt;Storage class&lt;&#x2F;div&gt;&lt;&#x2F;th&gt;&lt;th&gt;Description&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;INTEGER&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Whole numbers both positive and negative. With a size of 1, 2, 3, 4, 6, or 8 bytes. SQLite manages the sizing automatically based on the value you store.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;REAL&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;8-byte decimal floating point numbers&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;TEXT&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Unlimited storage of text characters (strings)&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;NULL&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Null values, representing missing or unknown information.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;BLOB&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Binary objects of unlimited size.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;p&gt;Typing in SQLite works a little different than most other relational database systems like MySQL and PostgreSQL. Where MySQL and PostgreSQL use static typing on a column basis, SQLite uses a dynamic typing approach where columns have just a &lt;em&gt;type affinity&lt;&#x2F;em&gt;. In SQLite the actual types are defined on cell, or value, basis instead of on a column basis.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;You can thing of the &amp;quot;type affinity&amp;quot; of a column in SQLite as the recommended data type you store in that column. But do keep in mind that SQLite may cast data before storing based on a columns type affinity.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The &amp;quot;type affinities&amp;quot; that SQLite uses are: &lt;code&gt;NUMERIC&lt;&#x2F;code&gt;, &lt;code&gt;INTEGER&lt;&#x2F;code&gt;, &lt;code&gt;REAL&lt;&#x2F;code&gt;, &lt;code&gt;TEXT&lt;&#x2F;code&gt; and &lt;code&gt;NONE&lt;&#x2F;code&gt;. As which storage class your data ends up in SQLite does depend the type affinity a column has. To learn more about the rules SQLite uses to cast data based on type affinities you can best read the &lt;a href=&quot;https:&#x2F;&#x2F;www.sqlite.org&#x2F;datatype3.html&quot;&gt;SQLite docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Open a SQLite database in the terminal</title>
		<published>2021-03-27T00:00:00+00:00</published>
		<updated>2021-03-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/sqlite-open-db-in-terminal/" type="text/html"/>
		<id>https://koenwoortman.com/sqlite-open-db-in-terminal/</id>
		<content type="html">&lt;p&gt;SQLite databases are quite convenient in web development, when prototyping a simple website for example. It comes as the default database for a couple big web frameworks like Ruby on Rails and Django. Mind that none of them also encourage you to use SQLite in production.&lt;&#x2F;p&gt;
&lt;p&gt;There are graphical database browsers available for SQLite, but you can open a SQLite database in the terminal as well. To open a SQLite database in shell mode you enter the &lt;code&gt;sqlite3&lt;&#x2F;code&gt; command followed by the path to your database file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sqlite3 db.sqlite3
&lt;&#x2F;span&gt;&lt;span&gt;SQLite version 3.31.1 2020-01-27 19:55:54
&lt;&#x2F;span&gt;&lt;span&gt;Enter &amp;quot;.help&amp;quot; for usage hints.
&lt;&#x2F;span&gt;&lt;span&gt;sqlite&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can now run SQL queries from the SQLite shell. Besides that there are many commands available, all starting with a period(&lt;code&gt;.&lt;&#x2F;code&gt;). One of those commands is actually mentioned when you open the shell: &lt;code&gt;.help&lt;&#x2F;code&gt;. When you run this &lt;code&gt;.help&lt;&#x2F;code&gt; command you will see a list of available commands in the SQLite shell.&lt;&#x2F;p&gt;
&lt;p&gt;A couple useful SQLite shell commands are:&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Command&lt;&#x2F;th&gt;&lt;th&gt;Description&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.databases&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;List names and files of attached databases&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.dump&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Dump the database in a SQL text format&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.excel&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Display the output of next command in spreadsheet&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.exit&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Exit this program&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.help&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Show a list of available commands&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.save FILE&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Write in-memory database into FILE&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.schema&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Show the CREATE statements&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;.tables&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;List names of tables&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Filter lists in Python</title>
		<published>2021-03-26T00:00:00+00:00</published>
		<updated>2021-03-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-filter-list/" type="text/html"/>
		<id>https://koenwoortman.com/python-filter-list/</id>
		<content type="html">&lt;p&gt;If you need to filter a list in Python than the built-in &lt;code&gt;filter()&lt;&#x2F;code&gt; function is probably what you&#x27;re looking for. The &lt;code&gt;filter()&lt;&#x2F;code&gt; function takes a function as its first argument and an iterable as its second argument.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;An iterable is basically an object you can iterate through. Like in a for-loop for example.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The &lt;code&gt;filter()&lt;&#x2F;code&gt; function does return a filter object, which is iterable as well. So you can directly use the result in a for loop for example. It isn&#x27;t a list however, so list slicing on filter results will break your code.&lt;&#x2F;p&gt;
&lt;p&gt;The function that &lt;code&gt;filter()&lt;&#x2F;code&gt; takes as its first argument is used to evaluate whether elements should be present in the filtered result. If you use &lt;code&gt;None&lt;&#x2F;code&gt; instead of a filter function that all elements that evaluate to &lt;code&gt;False&lt;&#x2F;code&gt; are filtered out. As the example below shows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;things &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Dog&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Remove Falsy values from list
&lt;&#x2F;span&gt;&lt;span&gt;true_things &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, things))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(true_things)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [5, &amp;#39;Dog&amp;#39;, True]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;filter()&lt;&#x2F;code&gt; function keeps the original list intact, it doesn&#x27;t actually remove values from the list you pass it.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;But most often you want to be a little more specific when filtering a list. You can do this by writing your own function that returns &lt;code&gt;True&lt;&#x2F;code&gt; for elements that you want to keep and &lt;code&gt;False&lt;&#x2F;code&gt; for elements that can be filtered out.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;is_positive&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;number&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(is_positive, numbers)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [1, 2]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;filter-using-a-lambda-function&quot;&gt;Filter using a lambda function&lt;&#x2F;h2&gt;
&lt;p&gt;Filtering using a lambda function can be a way to achieve the same result more concisely. Lambda functions are actually expressions, therefore you can create them at their point of use. In our case that would be directly as the first argument of the &lt;code&gt;filter()&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;n&lt;&#x2F;span&gt;&lt;span&gt;: n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, numbers)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [1, 2]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;filter-list-by-dict-value&quot;&gt;Filter list by dict value&lt;&#x2F;h2&gt;
&lt;p&gt;With a custom filter function we can basically filter on anything. Just as such you can filter dictionaries in a list by on of its values.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;particles &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;neutron&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;charge&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;proton&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;charge&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;name&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;electron&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;charge&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;is_positive&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;particle&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;particle[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;charge&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(is_positive, particles)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [{&amp;#39;name&amp;#39;: &amp;#39;proton&amp;#39;, &amp;#39;charge&amp;#39;: 1}]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;filter-list-by-object-property&quot;&gt;Filter list by object property&lt;&#x2F;h2&gt;
&lt;p&gt;The example of the previous section can easily be translated to work with objects too. Where we in this case filter on a property of the object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Particle&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__init__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;name&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;charge&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        self.name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;name
&lt;&#x2F;span&gt;&lt;span&gt;        self.charge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;charge
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;__repr__&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;lt;Particle: &lt;&#x2F;span&gt;&lt;span&gt;{self.name}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;particles &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    Particle(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;neutron&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Particle(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;proton&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;    Particle(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;electron&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;),
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;is_positive&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;particle&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;particle.charge &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(is_positive, particles)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(result))
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;lt;Particle: proton&amp;gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;filter-with-list-comprehension&quot;&gt;Filter with list comprehension&lt;&#x2F;h2&gt;
&lt;p&gt;Using list comprehensions is ofter considered to be the more Pythonic way of doing things. And filtering a list using list comprehension works fine too.&lt;&#x2F;p&gt;
&lt;p&gt;Reusing one of the examples above using list comprehension looks as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;result &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;n &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;numbers &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;n  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(result)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In contrary to the &lt;code&gt;filter()&lt;&#x2F;code&gt; function, list comprehension obviously returns a list. So there is no need to wrap the result in a &lt;code&gt;list()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Return JSON response from a Django view</title>
		<published>2021-03-25T00:00:00+00:00</published>
		<updated>2021-03-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-view-return-json-response/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-view-return-json-response/</id>
		<content type="html">&lt;p&gt;When making an API with Django I go straight to &lt;a href=&quot;https:&#x2F;&#x2F;www.django-rest-framework.org&#x2F;&quot;&gt;Django REST framework&lt;&#x2F;a&gt;. However, you don&#x27;t need it at all to just return a JSON response from Django.&lt;&#x2F;p&gt;
&lt;p&gt;When you are rendering a template in Django your view probably returns an instance of the &lt;code&gt;HttpResponse&lt;&#x2F;code&gt; class. It is a common way to print some text on a webpage:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.http &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;HttpResponse
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;HttpResponse(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Just rendering a string :)&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you invoke the &lt;code&gt;render&lt;&#x2F;code&gt; shortcut function you are actually returning a &lt;code&gt;HttpResponse&lt;&#x2F;code&gt; too. The &lt;code&gt;render&lt;&#x2F;code&gt; function renders the template file to a string and returns a &lt;code&gt;HttpResponse&lt;&#x2F;code&gt; with the rendered template as its content.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.shortcuts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;render
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;render(request, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;blog&#x2F;index.html&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Besides &lt;code&gt;HttpResponse&lt;&#x2F;code&gt; Django comes with a &lt;code&gt;JsonResponse&lt;&#x2F;code&gt; class as well. This &lt;code&gt;JsonResponse&lt;&#x2F;code&gt; is actually a subclass of &lt;code&gt;HttpResponse&lt;&#x2F;code&gt;. The &lt;code&gt;JsonResponse&lt;&#x2F;code&gt; transforms the data you pass to it to a JSON string and sets the content type HTTP header to &lt;code&gt;application&#x2F;json&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;To return JSON data from a Django view you swap out the &lt;code&gt;HttpResponse&lt;&#x2F;code&gt; for &lt;code&gt;JsonResponse&lt;&#x2F;code&gt; and pass it the data that needs to be transformed to JSON.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.http &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;JsonResponse
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;index&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;request&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;JsonResponse({&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;text&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Just rendering some JSON :)&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;})
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use the email field as username in Django</title>
		<published>2021-03-24T00:00:00+00:00</published>
		<updated>2021-03-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-email-as-username/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-email-as-username/</id>
		<content type="html">&lt;p&gt;Django comes with its own &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;ref&#x2F;contrib&#x2F;auth&#x2F;#user-model&quot;&gt;user model&lt;&#x2F;a&gt; which plays a part in the authentication system of Django. By default these Django users require a unique username for authentication. If you prefer to use the email instead of the username to login you can do so by implementing a custom user model.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Adding this custom user model is something you preferably do before you create migrations for the first time.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;1-create-a-custom-user-model&quot;&gt;1. Create a custom user model&lt;&#x2F;h2&gt;
&lt;p&gt;The first step is creating a custom user model. I often prefer to create a &lt;code&gt;users&lt;&#x2F;code&gt; app in my Django projects when I need a custom user model. But feel free to pick another name for this app, something like &lt;code&gt;auth&lt;&#x2F;code&gt; makes sense too. Anyway, you can create an app with the following command, I will stick to an app named &lt;code&gt;users&lt;&#x2F;code&gt; in this post:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py startapp users
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Next, we need to add the newly created &lt;code&gt;users&lt;&#x2F;code&gt; app to our &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt; in &lt;code&gt;settings.py&lt;&#x2F;code&gt;. You can add the app as both &lt;code&gt;users&lt;&#x2F;code&gt; or &lt;code&gt;&#x27;users.apps.UsersConfig&#x27;&lt;&#x2F;code&gt;, there is a &lt;a href=&quot;&#x2F;python-django-installed-apps-name-vs-appconfig&#x2F;&quot;&gt;slight functional difference&lt;&#x2F;a&gt; though. But that&#x27;s not relevant for now.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# settings.py
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;INSTALLED_APPS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.admin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.auth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.contenttypes&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.sessions&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.messages&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.contrib.staticfiles&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Local apps
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users.apps.UsersConfig&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With our new &lt;code&gt;users&lt;&#x2F;code&gt; app added to the &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt; we can create the a custom user model. In &lt;code&gt;users&#x2F;models.py&lt;&#x2F;code&gt; we will make a new class called &lt;code&gt;User&lt;&#x2F;code&gt; which is a subclass of &lt;code&gt;django.contrib.auth.models.AbstractUser&lt;&#x2F;code&gt;. In our &lt;code&gt;User&lt;&#x2F;code&gt; class we will set the &lt;code&gt;username&lt;&#x2F;code&gt; property to &lt;code&gt;None&lt;&#x2F;code&gt;, so that we don&#x27;t get it from &lt;code&gt;AbstractUser&lt;&#x2F;code&gt;. Next we add the &lt;code&gt;email&lt;&#x2F;code&gt; property as a unique &lt;code&gt;EmailField&lt;&#x2F;code&gt; to the user model.&lt;&#x2F;p&gt;
&lt;p&gt;By setting the &lt;code&gt;USERNAME_FIELD&lt;&#x2F;code&gt; you let Django know which field to use &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;topics&#x2F;auth&#x2F;customizing&#x2F;#django.contrib.auth.models.CustomUser.USERNAME_FIELD&quot;&gt;to uniquely identify users&lt;&#x2F;a&gt;. The &lt;code&gt;REQUIRED_FIELDS&lt;&#x2F;code&gt; are the &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;topics&#x2F;auth&#x2F;customizing&#x2F;#django.contrib.auth.models.CustomUser.REQUIRED_FIELDS&quot;&gt;required fields which you will be prompted for&lt;&#x2F;a&gt; when you create a user via the &lt;code&gt;createsuperuser&lt;&#x2F;code&gt; command, but you exclude the &lt;code&gt;USERNAME_FIELD&lt;&#x2F;code&gt; ans password from this list. We set because for &lt;code&gt;AbstractUser&lt;&#x2F;code&gt; the field &lt;code&gt;email&lt;&#x2F;code&gt; is part of this list, which is now our &lt;code&gt;USERNAME_FIELD&lt;&#x2F;code&gt; :).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.db &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;models
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib.auth.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;AbstractUser
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.utils.translation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;gettext_lazy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;_
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;AbstractUser&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    username &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;    email &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;models.EmailField(_(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;email address&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;unique&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    USERNAME_FIELD &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;email&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    REQUIRED_FIELDS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Before we can make our migrations we need to go back to &lt;code&gt;settings.py&lt;&#x2F;code&gt; and set &lt;code&gt;AUTH_USER_MODEL&lt;&#x2F;code&gt; to the new user model class we just made. If you picked different names, make sure to use the following format: &lt;code&gt;&amp;lt;APP_NAME&amp;gt;.&amp;lt;CUSTOM_USER_MODEL_CLASS&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# settings.py
&lt;&#x2F;span&gt;&lt;span&gt;AUTH_USER_MODEL &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users.User&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With that we should be good to go to make migrations.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py makemigrations
&lt;&#x2F;span&gt;&lt;span&gt;Migrations for &amp;#39;users&amp;#39;:
&lt;&#x2F;span&gt;&lt;span&gt;  users&#x2F;migrations&#x2F;0001_initial.py
&lt;&#x2F;span&gt;&lt;span&gt;    - Create model User
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and run them:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py migrate
&lt;&#x2F;span&gt;&lt;span&gt;Operations to perform:
&lt;&#x2F;span&gt;&lt;span&gt;  Apply all migrations: admin, auth, contenttypes, sessions, users
&lt;&#x2F;span&gt;&lt;span&gt;Running migrations:
&lt;&#x2F;span&gt;&lt;span&gt;  Applying contenttypes.0001_initial... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying contenttypes.0002_remove_content_type_name... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0001_initial... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0002_alter_permission_name_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0003_alter_user_email_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0004_alter_user_username_opts... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0005_alter_user_last_login_null... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0006_require_contenttypes_0002... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0007_alter_validators_add_error_messages... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0008_alter_user_username_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0009_alter_user_last_name_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0010_alter_group_name_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0011_update_proxy_permissions... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying auth.0012_alter_user_first_name_max_length... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying users.0001_initial... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying admin.0001_initial... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying admin.0002_logentry_remove_auto_add... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying admin.0003_logentry_add_action_flag_choices... OK
&lt;&#x2F;span&gt;&lt;span&gt;  Applying sessions.0001_initial... OK
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;2-adjust-the-user-manager&quot;&gt;2. Adjust the user manager&lt;&#x2F;h2&gt;
&lt;p&gt;We created a custom user model, but need to make a change to the &lt;code&gt;UserManager&lt;&#x2F;code&gt; as well. If you would run the &lt;code&gt;createsuperuser&lt;&#x2F;code&gt; command now it would exit with the error:&lt;code&gt;TypeError: create_superuser() missing 1 required positional argument: &#x27;username&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You might want to create a separate file for this in your app, but for convenience I am adding the manager to &lt;code&gt;models.py&lt;&#x2F;code&gt;. The Django docs have a fine &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;topics&#x2F;auth&#x2F;customizing&#x2F;#a-full-example&quot;&gt;example&lt;&#x2F;a&gt; on this too.&lt;&#x2F;p&gt;
&lt;p&gt;For our custom user manager we will first extend the &lt;code&gt;BaseUserManager&lt;&#x2F;code&gt; class from Django. &lt;strong&gt;Secondly,&lt;&#x2F;strong&gt; we add the line &lt;code&gt;objects = UserManager()&lt;&#x2F;code&gt; to our custom user model.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.db &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;models
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib.auth.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;AbstractUser
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.utils.translation &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;gettext_lazy &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;as &lt;&#x2F;span&gt;&lt;span&gt;_
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib.auth.base_user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseUserManager
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;UserManager&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseUserManager&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    use_in_migrations &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;_create_user&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;extra_fields&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;email:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ValueError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Users require an email field&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        email &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.normalize_email(email)
&lt;&#x2F;span&gt;&lt;span&gt;        user &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.model(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;email, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;extra_fields)
&lt;&#x2F;span&gt;&lt;span&gt;        user.set_password(password)
&lt;&#x2F;span&gt;&lt;span&gt;        user.save(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;using&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;self._db)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;user
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;create_user&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;extra_fields&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        extra_fields.setdefault(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_staff&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        extra_fields.setdefault(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;self._create_user(email, password, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;extra_fields)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;create_superuser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;email&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;password&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;extra_fields&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        extra_fields.setdefault(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_staff&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        extra_fields.setdefault(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;extra_fields.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_staff&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;is not &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ValueError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Superuser must have is_staff=True.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;extra_fields.get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;is_superuser&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;is not &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;ValueError&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Superuser must have is_superuser=True.&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;self._create_user(email, password, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span&gt;extra_fields)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;User&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;AbstractUser&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    username &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;    email &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;models.EmailField(_(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;email address&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;unique&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    objects &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;UserManager()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    USERNAME_FIELD &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;email&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    REQUIRED_FIELDS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;With that you should be able to run &lt;code&gt;python manage.py createsuperuser&lt;&#x2F;code&gt; again.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-user-admin&quot;&gt;3. User admin&lt;&#x2F;h2&gt;
&lt;p&gt;Now, to confirm that the email field is used as a username field we can open-up the Django admin. If you see the field &amp;quot;Email address&amp;quot; instead of &amp;quot;Username&amp;quot; you should be fine.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;&#x2F;img&#x2F;django-admin-email-username.png&quot; alt=&quot;Django admin with email as username&quot; title=&quot;Django admin with email as username&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;But once you log in however, the user admin isn&#x27;t available since we are using a custom user model now. To fix this we&#x27;ll make changes to &lt;code&gt;users&#x2F;admin.py&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.contrib &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;admin
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;users.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;User
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@admin.register(User)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;UserAdmin&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;admin.ModelAdmin&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Register apps by package name vs dotted path in Django</title>
		<published>2021-03-23T00:00:00+00:00</published>
		<updated>2021-03-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-installed-apps-name-vs-dotted-path/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-installed-apps-name-vs-dotted-path/</id>
		<content type="html">&lt;p&gt;When you create a new app in Django with &lt;code&gt;python manage.py startapp &amp;lt;app name&amp;gt;&lt;&#x2F;code&gt; you need to register it by adding it to the &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt; in the &lt;code&gt;settings.py&lt;&#x2F;code&gt; file. I often forget the last step :).&lt;&#x2F;p&gt;
&lt;p&gt;You can add an app in two ways to &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt;, either by adding the package name or the dotted path to a subclass of &lt;code&gt;AppConfig&lt;&#x2F;code&gt;. According to the &lt;a href=&quot;https:&#x2F;&#x2F;docs.djangoproject.com&#x2F;en&#x2F;dev&#x2F;ref&#x2F;settings&#x2F;#installed-apps&quot;&gt;Django docs&lt;&#x2F;a&gt; the latter is preferred.&lt;&#x2F;p&gt;
&lt;p&gt;The following shows both ways of registering a Django app.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;INSTALLED_APPS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;blog&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;posts.apps.PostsConfig&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So what is the difference between the two? The latter option, with dotted path, allows you to add configuration options to your app. For example, you can override the &lt;code&gt;ready&lt;&#x2F;code&gt; method where you can registering signals. The first, just the package name, will use the configuration from the class &lt;code&gt;django.apps.AppConfig&lt;&#x2F;code&gt;. You might not need to change the app config right away but using the dotted path allows you to be more flexible in the future.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reload Postgres config from psql</title>
		<published>2021-03-22T00:00:00+00:00</published>
		<updated>2021-03-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/postgresql-reload-config/" type="text/html"/>
		<id>https://koenwoortman.com/postgresql-reload-config/</id>
		<content type="html">&lt;p&gt;After you&#x27;ve made a change in the Postgres settings you may need to restart the postgres service. The command you would use for this depends on your init system, if you&#x27;re using systemd the command &lt;code&gt;sudo service postgresql restart&lt;&#x2F;code&gt; will probably do.&lt;&#x2F;p&gt;
&lt;p&gt;If your change doesn&#x27;t require a full restart but just a reload, there is a convenient command available in &lt;code&gt;psql&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;postgres=# SELECT pg_reload_conf();
&lt;&#x2F;span&gt;&lt;span&gt; pg_reload_conf
&lt;&#x2F;span&gt;&lt;span&gt;----------------
&lt;&#x2F;span&gt;&lt;span&gt; t
&lt;&#x2F;span&gt;&lt;span&gt;(1 row)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Set the EDITOR environment variable in ZSH</title>
		<published>2021-03-21T00:00:00+00:00</published>
		<updated>2021-03-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-set-editor-environment-variable/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-set-editor-environment-variable/</id>
		<content type="html">&lt;p&gt;When you start to use the command line more often you notice that quite some shell programs launch a text editor like nano or vim for you to edit a file or something in that regard. Most often these shell programs check for the shell environment variable &lt;code&gt;EDITOR&lt;&#x2F;code&gt; to see your preferred text editor for these type of tasks.&lt;&#x2F;p&gt;
&lt;p&gt;To see to what value &lt;code&gt;EDITOR&lt;&#x2F;code&gt; is set you just &lt;code&gt;echo&lt;&#x2F;code&gt; it out. Mind the &lt;code&gt;$&lt;&#x2F;code&gt; sign before &lt;code&gt;EDITOR&lt;&#x2F;code&gt; when you reference a variable in the shell.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ echo $EDITOR
&lt;&#x2F;span&gt;&lt;span&gt;vim
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can set the &lt;code&gt;EDITOR&lt;&#x2F;code&gt; variable per command like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ EDITOR=nano git commit
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What is unfortunate about setting it per command is that it doesn&#x27;t persist. In order to make this setting permanent you can set the default editor in the &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file. This config file is specific for the ZSH shell so if you were to use bash for example you need to configure it somewhere else.&lt;&#x2F;p&gt;
&lt;p&gt;To make the EDITOR setting permanent you need to &lt;code&gt;export&lt;&#x2F;code&gt; it in your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;EDITOR&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;nano
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Afterwards the setting doesn&#x27;t take immediate effect. You can either close and reopen your terminal or source &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ source ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Change the default git editor</title>
		<published>2021-03-20T00:00:00+00:00</published>
		<updated>2021-03-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-change-default-editor/" type="text/html"/>
		<id>https://koenwoortman.com/git-change-default-editor/</id>
		<content type="html">&lt;p&gt;Many git commands can launch a text editor, for example the &lt;code&gt;git commmit&lt;&#x2F;code&gt; command will launch one for you to enter the commit message. Git uses the editor that one of the environment variables &lt;code&gt;VISUAL&lt;&#x2F;code&gt; or &lt;code&gt;EDITOR&lt;&#x2F;code&gt; are set to in your shell. If none of them are set git will default to the &lt;code&gt;vi&lt;&#x2F;code&gt; editor.&lt;&#x2F;p&gt;
&lt;p&gt;Of course you can set one of the &lt;code&gt;VISUAL&lt;&#x2F;code&gt; or &lt;code&gt;EDITOR&lt;&#x2F;code&gt; environment variables in your shell. But you can define your preferred editor also directly in the git config. You can change the git config file or via a command:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;txt&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-txt &quot;&gt;&lt;code class=&quot;language-txt&quot; data-lang=&quot;txt&quot;&gt;&lt;span&gt;$ git config --global core.editor nvim
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The git config file is either located in your home directory, named as: &lt;code&gt;~&#x2F;.gitconfig&lt;&#x2F;code&gt;. Or you can use the location &lt;code&gt;$XDG_CONFIG_HOME&#x2F;git&#x2F;config&lt;&#x2F;code&gt;, which most likely is &lt;code&gt;~&#x2F;git&#x2F;config&lt;&#x2F;code&gt;. This config file uses the TOML configuration language. You can add a default editor as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;toml&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-toml &quot;&gt;&lt;code class=&quot;language-toml&quot; data-lang=&quot;toml&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;git&#x2F;config or ~&#x2F;.gitconfig
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[user]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;email &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;koen@ko.en&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;Koen Woortman&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[core]
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;editor &lt;&#x2F;span&gt;&lt;span&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;nvim&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The value you set &lt;code&gt;editor&lt;&#x2F;code&gt; to is actually the shell command used to launch the editor. So you are not just bound to terminal editors, you can for example use &lt;a href=&quot;&#x2F;git-visual-studio-code-as-editor&#x2F;&quot;&gt;visual studio code as git editor&lt;&#x2F;a&gt; as well.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Editor&lt;&#x2F;th&gt;&lt;th&gt;Config value&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;nano&lt;&#x2F;td&gt;&lt;td&gt;nano&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;vim&lt;&#x2F;td&gt;&lt;td&gt;vim&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;neovim&lt;&#x2F;td&gt;&lt;td&gt;nvim&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;emacs&lt;&#x2F;td&gt;&lt;td&gt;emacs&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;sublime text&lt;&#x2F;td&gt;&lt;td&gt;subl -n -w&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;atom&lt;&#x2F;td&gt;&lt;td&gt;atom --wait&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;vscode&lt;&#x2F;td&gt;&lt;td&gt;code --wait&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Install a specific version of a package with pip</title>
		<published>2021-03-19T00:00:00+00:00</published>
		<updated>2021-03-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-pip-install-specific-version-of-package/" type="text/html"/>
		<id>https://koenwoortman.com/python-pip-install-specific-version-of-package/</id>
		<content type="html">&lt;p&gt;Most often installing the latest version of a package will do. For some cases you require an older version of a package, for example when the newest version of the package you&#x27;d like to install isn&#x27;t compatible with the Python version you&#x27;re running.&lt;&#x2F;p&gt;
&lt;p&gt;If you don&#x27;t specify a version when installing a package than pip will go for the latest version available.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ pip install httpie
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To specify the version of the package you like you use the same scheme as you might be familiar with from the &lt;code&gt;requirements.txt&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ pip install httpie==2.3.0
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You install the specific version with &lt;code&gt;&amp;lt;package name&amp;gt;==&amp;lt;version&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the SQL query from a Django queryset object</title>
		<published>2021-03-18T00:00:00+00:00</published>
		<updated>2021-03-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-get-sql-from-queryset/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-get-sql-from-queryset/</id>
		<content type="html">&lt;p&gt;The Django ORM abstracts away most of the database stuff. But while debugging or profiling it can be useful to see how Django executes a query.&lt;&#x2F;p&gt;
&lt;p&gt;You can easily get the raw SQL query by accessing the &lt;code&gt;.query&lt;&#x2F;code&gt; property of a queryset object.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;users &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;User.objects.all()
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;(users.query)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;SELECT &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;users_user&amp;quot;.&amp;quot;id&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;password&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;last_login&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;is_superuser&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;first_name&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;last_name&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;is_staff&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;is_active&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;date_joined&amp;quot;, &amp;quot;users_user&amp;quot;.&amp;quot;email&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;users_user&amp;quot;&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The example above displays this in the Django shell, for a query that gets all the users.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Using the Zsh shell in Vim mode</title>
		<published>2021-03-17T00:00:00+00:00</published>
		<updated>2021-03-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-vim-mode/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-vim-mode/</id>
		<content type="html">&lt;p&gt;Whenever available I love to reach to the Vim keybindings. The Zsh line editor not being an exception. &lt;strong&gt;&lt;em&gt;Vim all the things!&lt;&#x2F;em&gt;&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Enabling the Vim keybindings is a one-liner in the &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file. But to get it working somewhat more smoothy some other tweaks are necessary too in my opinion. I&#x27;m sure there are some &lt;code&gt;oh-my-zsh&lt;&#x2F;code&gt; plugins available for this too if you use that.&lt;&#x2F;p&gt;
&lt;p&gt;First off, let&#x27;s indeed add that one-liner to the &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Use vi mode
&lt;&#x2F;span&gt;&lt;span&gt;bindkey&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -v
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The next time you open your shell you are using vi mode. You can exit insert mode with &lt;code&gt;ESC&lt;&#x2F;code&gt; and enter it again with &lt;code&gt;i&lt;&#x2F;code&gt;, and other keybindings you&#x27;re expecting are available too.&lt;&#x2F;p&gt;
&lt;p&gt;There is somewhat of a delay between pressing a key combination and the actual effect. For me this timeout takes a bit too long. You can change it by setting the &lt;code&gt;KEYTIMEOUT&lt;&#x2F;code&gt; environment variable in your &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; file too.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Use vi mode
&lt;&#x2F;span&gt;&lt;span&gt;bindkey&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -v
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;KEYTIMEOUT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What is missing for me is a proper mode indicator. I&#x27;ve moved the code for the indicator to a separate &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;koenwoortman&#x2F;creme-fraiche-zsh-theme&quot;&gt;zsh theme&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The code that did the trick for me is pasted in bellow.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;## Init
&lt;&#x2F;span&gt;&lt;span&gt;setopt PROMPT_SUBST
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;## Options
&lt;&#x2F;span&gt;&lt;span&gt;THEME_PROMPT_PREFIX&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;${THEME_PROMPT_PREFIX&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;}
&lt;&#x2F;span&gt;&lt;span&gt;THEME_VI_INS_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;${THEME_VI_INS_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;λ&amp;#39;}
&lt;&#x2F;span&gt;&lt;span&gt;THEME_VI_CMD_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;${THEME_VI_CMD_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;:-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ᐅ&amp;#39;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;## Set symbol for the initial mode
&lt;&#x2F;span&gt;&lt;span&gt;THEME_VI_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${THEME_VI_INS_MODE_SYMBOL}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# on keymap change, define the mode and redraw prompt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;zle-keymap-select&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${KEYMAP}&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;vicmd&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;    THEME_VI_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${THEME_VI_CMD_MODE_SYMBOL}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;    THEME_VI_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${THEME_VI_INS_MODE_SYMBOL}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;  zle reset-prompt
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;zle&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -N&lt;&#x2F;span&gt;&lt;span&gt; zle-keymap-select
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# reset to default mode at the end of line input reading
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;zle-line-finish&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  THEME_VI_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${THEME_VI_INS_MODE_SYMBOL}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;zle&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -N&lt;&#x2F;span&gt;&lt;span&gt; zle-line-finish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Fix a bug when you C-c in CMD mode, you&amp;#39;d be prompted with CMD mode indicator
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# while in fact you would be in INS mode.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Fixed by catching SIGINT (C-c), set mode to INS and repropagate the SIGINT,
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# so if anything else depends on it, we will not break it.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;TRAPINT&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;  THEME_VI_MODE_SYMBOL&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;${THEME_VI_INS_MODE_SYMBOL}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;$(( &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;128 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;$1 ))
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;PROMPT&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;$THEME_PROMPT_PREFIX%f%B%F{240}%1~%f%b %(?.%F{green}$THEME_VI_MODE_SYMBOL.%F{red}$THEME_VI_MODE_SYMBOL) &amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Confirmation when running rm in Zsh</title>
		<published>2021-03-16T00:00:00+00:00</published>
		<updated>2021-03-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-confirm-rm/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-confirm-rm/</id>
		<content type="html">&lt;p&gt;Better safe than sorry right? To prevent yourself from stupid mistakes when removing files you can add the &lt;code&gt;-i&lt;&#x2F;code&gt; option to the &lt;code&gt;rm&lt;&#x2F;code&gt; command. This will ask for a confirmation before removing the file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ rm&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -i&lt;&#x2F;span&gt;&lt;span&gt; secrets.txt
&lt;&#x2F;span&gt;&lt;span&gt;rm: remove regular file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;secrets.txt&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can now press &lt;code&gt;y&lt;&#x2F;code&gt; to indeed remove the file. For directories this works the same.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ mkdir secrets
&lt;&#x2F;span&gt;&lt;span&gt;$ vim secrets&#x2F;dirty.txt
&lt;&#x2F;span&gt;&lt;span&gt;$ rm&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -ir&lt;&#x2F;span&gt;&lt;span&gt; secrets
&lt;&#x2F;span&gt;&lt;span&gt;rm: descend into directory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;secrets&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; y
&lt;&#x2F;span&gt;&lt;span&gt;rm: remove regular file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;secrets&#x2F;dirty.txt&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; y
&lt;&#x2F;span&gt;&lt;span&gt;rm: remove directory &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;secrets&amp;#39;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; y
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Right, it works, but a confirmation for each individual directory and file is a bit overkill if you&#x27;d ask me. Using the capital &lt;code&gt;I&lt;&#x2F;code&gt; option instead fixes this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ mkdir secrets
&lt;&#x2F;span&gt;&lt;span&gt;$ vim secrets&#x2F;dirty.txt
&lt;&#x2F;span&gt;&lt;span&gt;$ rm&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -Ir&lt;&#x2F;span&gt;&lt;span&gt; secrets
&lt;&#x2F;span&gt;&lt;span&gt;rm: remove 1 argument recursively&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?&lt;&#x2F;span&gt;&lt;span&gt; y
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Typing out this &lt;code&gt;-I&lt;&#x2F;code&gt; is something that I&#x27;d probably forget at some point. So instead I decided to make it an alias for the &lt;code&gt;rm&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;To do this open up the &lt;code&gt;~&#x2F;.zshrc&lt;&#x2F;code&gt; config file, or wherever you add your aliases, and add the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.zshrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;alias &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;rm&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;rm -I&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Somewhat more trivial but maybe useful too is that the &lt;code&gt;cp&lt;&#x2F;code&gt; and &lt;code&gt;mv&lt;&#x2F;code&gt; commands both accept this &lt;code&gt;-i&lt;&#x2F;code&gt; option too.&lt;&#x2F;p&gt;
&lt;p&gt;Now what if you unintentionally removed a file anyway? Then I hope you checked the file in to version control.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Running commands in Zsh without having to wait</title>
		<published>2021-03-15T00:00:00+00:00</published>
		<updated>2021-03-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-run-command-in-background/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-run-command-in-background/</id>
		<content type="html">&lt;p&gt;In Zsh, and for Bash this works the same, you can send commands to the background. This unblocks your terminal for other commands you&#x27;d like to run. For example in the case of a slow command or a process that keeps running like a development server.&lt;&#x2F;p&gt;
&lt;p&gt;You can send a command to the background by appending an &lt;code&gt;&amp;amp;&lt;&#x2F;code&gt; at the end of the command. So like this for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ hugo server &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp;
&lt;&#x2F;span&gt;&lt;span&gt;[1] 96545
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This sends the hugo development server to the background and shows the process ID. If you&#x27;d like you can use this process ID to lookup information about the process.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ ps&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -p&lt;&#x2F;span&gt;&lt;span&gt; 96545
&lt;&#x2F;span&gt;&lt;span&gt;    PID TTY          TIME CMD
&lt;&#x2F;span&gt;&lt;span&gt;  96545 pts&#x2F;8    00:00:01 hugo
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or more practically kill the process.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ kill 96545
&lt;&#x2F;span&gt;&lt;span&gt;[1]  + done       hugo server
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To bring back the process to the foreground you just use the &lt;code&gt;fg&lt;&#x2F;code&gt; command. This brings up the last process you ran in the background.&lt;&#x2F;p&gt;
&lt;p&gt;You might have noticed that the background process does write to the terminal while being in the background. Which has a useful purpose obviously. This way you get possible warnings and other information about the running process that might require your attention.&lt;&#x2F;p&gt;
&lt;p&gt;However, if you would rather have the process running quietly you can redirecting the output of the command to &lt;code&gt;&#x2F;dev&#x2F;null&lt;&#x2F;code&gt;. As is shown in the example below. Just be aware that you might miss useful information about the running process when you do this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ hugo server &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;dev&#x2F;null &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if string contains substring in PHP</title>
		<published>2021-03-14T00:00:00+00:00</published>
		<updated>2021-03-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-string-contains-substring/" type="text/html"/>
		<id>https://koenwoortman.com/php-string-contains-substring/</id>
		<content type="html">&lt;p&gt;In order to check if a string contains a word, letter (any kind of substring) you use the &lt;code&gt;strpos()&lt;&#x2F;code&gt; function. Which looks for the first occurrence of the substring in the string you want to search. It returns the position of the first occurrence in the string and &lt;code&gt;false&lt;&#x2F;code&gt; if the substring couldn&#x27;t be found.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;strpos&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Graphpaper&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;!== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Word contains &amp;quot;php&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Word does not contains &amp;quot;php&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Word contains &amp;quot;php&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since PHP 8 you have some new sugar to check for substrings, the &lt;code&gt;str_contains()&lt;&#x2F;code&gt; which was introduced in PHP 8.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if&lt;&#x2F;span&gt;&lt;span&gt;(str_contains(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Graphpaper&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Word contains &amp;quot;php&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Word does not contains &amp;quot;php&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Word contains &amp;quot;php&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the length of a string in PHP</title>
		<published>2021-03-13T00:00:00+00:00</published>
		<updated>2021-03-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-length-of-string/" type="text/html"/>
		<id>https://koenwoortman.com/php-length-of-string/</id>
		<content type="html">&lt;p&gt;This might be one of the easiest things you&#x27;ll ever do, in PHP to be precise. To get the length of a string in PHP you use the &lt;code&gt;strlen()&lt;&#x2F;code&gt; function. It takes a string as its argument and returns the length of the string, the amount of individual characters with spaces, tabs, line breaks, etc. included.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo strlen&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;four&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Easy peasy.&lt;&#x2F;p&gt;
&lt;p&gt;In case you want to check if a string is empty you could better use the &lt;code&gt;empty()&lt;&#x2F;code&gt; function. Which checks whether a variable is empty.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the last element of an array in PHP</title>
		<published>2021-03-12T00:00:00+00:00</published>
		<updated>2021-03-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/php-last-element-of-array/" type="text/html"/>
		<id>https://koenwoortman.com/php-last-element-of-array/</id>
		<content type="html">&lt;p&gt;For getting the last element of an array in PHP you have multiple options, which function slightly differently. The most obvious ones are &lt;code&gt;end()&lt;&#x2F;code&gt;, &lt;code&gt;array_slice()&lt;&#x2F;code&gt; and &lt;code&gt;array_pop()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;getting-the-last-element-with-end&quot;&gt;Getting the last element with end()&lt;&#x2F;h2&gt;
&lt;p&gt;The &lt;code&gt;end()&lt;&#x2F;code&gt; function sets the internal pointer of the array to its last element and returns it&#x27;s value. It works pretty straight forward, after this function call the array is still intact and you can do with the return value what you&#x27;d like; assign it to a variable, print it, e.g.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$dogs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Lassie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Hachi&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Bolt&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo end&lt;&#x2F;span&gt;&lt;span&gt;($dogs);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;#39;Bolt&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;getting-the-last-element-with-array-slice&quot;&gt;Getting the last element with array_slice()&lt;&#x2F;h2&gt;
&lt;p&gt;With the &lt;code&gt;array_slice()&lt;&#x2F;code&gt; function you extract slices from an existing array. You can use &lt;code&gt;-1&lt;&#x2F;code&gt; as offset to get just the last item of the array you pass as the first argument. A side note&lt;&#x2F;p&gt;
&lt;p&gt;Be aware that &lt;code&gt;array_slice()&lt;&#x2F;code&gt; returns a new array. So in order to actually get the last element of the original array you need to get the value at index 0 of the return value from &lt;code&gt;array_slice()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$dogs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Lassie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Hachi&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Bolt&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo array_slice&lt;&#x2F;span&gt;&lt;span&gt;($dogs, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;#39;Bolt&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;getting-the-last-element-with-array-pop&quot;&gt;Getting the last element with array_pop()&lt;&#x2F;h2&gt;
&lt;p&gt;This one functions slightly differently. The &lt;code&gt;array_pop()&lt;&#x2F;code&gt; function removes the last element from an array and returns it. So it works perfectly fine to get the last element of the array, just make sure you also actually meant to remove it from the array.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$dogs &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Lassie&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Hachi&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Bolt&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo array_pop&lt;&#x2F;span&gt;&lt;span&gt;($dogs);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; &amp;#39;Bolt&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print_r&lt;&#x2F;span&gt;&lt;span&gt;($dogs);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Array
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#    (
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#        [0] =&amp;gt; Lassie
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#        [1] =&amp;gt; Hachi
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#    )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Removing unused node modules with yarn</title>
		<published>2021-03-11T00:00:00+00:00</published>
		<updated>2021-03-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/yarn-remove-all-unused-packages/" type="text/html"/>
		<id>https://koenwoortman.com/yarn-remove-all-unused-packages/</id>
		<content type="html">&lt;p&gt;Every now and then I install a node module to test something out. Or haven&#x27;t decided which package to use for a certain use-case and install multiple packages that do the same thing.&lt;&#x2F;p&gt;
&lt;p&gt;You can remove those packages from the dependencies section in your package.json, but that doesn&#x27;t remove the package from the &lt;code&gt;node_modules&lt;&#x2F;code&gt; folder. Yarn has an autoclean command that removes unnecessary dependencies.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ yarn autoclean
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;what-about-npm&quot;&gt;What about npm?&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;code&gt;npm&lt;&#x2F;code&gt; offers a similar command to remove unnecessary dependencies; &lt;code&gt;prune&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ npm prune
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Installing Lua on Ubuntu 20.04</title>
		<published>2021-03-10T00:00:00+00:00</published>
		<updated>2021-03-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/lua-install-on-ubuntu/" type="text/html"/>
		<id>https://koenwoortman.com/lua-install-on-ubuntu/</id>
		<content type="html">&lt;p&gt;The programming language Lua is somewhat lesser known. Though, it has been around since 1993. What got me interested in learning some Lua is that Neovim allows you to use it as a substitute for VimL in your config file.&lt;&#x2F;p&gt;
&lt;p&gt;By default Lua isn&#x27;t available on Ubuntu. Luckily it&#x27;s available in the repositories and therefore easy installable via &lt;code&gt;apt&lt;&#x2F;code&gt;. Currently, the latest available version of Lua on Ubuntu 20.04 is version 5.3. You can install it using the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ sudo apt install lua5.3
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If all went well you can now start the Lua shell with the &lt;code&gt;lua&lt;&#x2F;code&gt; command in your terminal.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ lua
&lt;&#x2F;span&gt;&lt;span&gt;Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Feel free to enter &lt;code&gt;print(&#x27;Hello World&#x27;)&lt;&#x2F;code&gt; in this terminal, if that makes the tutorial feel more complete ;).&lt;&#x2F;p&gt;
&lt;p&gt;Once you&#x27;re done you can exit out the Lua shell by pressing &lt;code&gt;CTRL+C&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;building-lua-from-source&quot;&gt;Building Lua from source&lt;&#x2F;h2&gt;
&lt;p&gt;You can easily build Lua from source. The [https:&#x2F;&#x2F;www.lua.org&#x2F;download.html](download page on the Lua website) contains more information on this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ curl&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -R -O&lt;&#x2F;span&gt;&lt;span&gt; http:&#x2F;&#x2F;www.lua.org&#x2F;ftp&#x2F;lua-5.4.2.tar.gz
&lt;&#x2F;span&gt;&lt;span&gt;$ tar zxf lua-5.4.2.tar.gz
&lt;&#x2F;span&gt;&lt;span&gt;$ cd lua-5.4.2
&lt;&#x2F;span&gt;&lt;span&gt;$ make all test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Ruby Ternary Operator</title>
		<published>2021-03-09T00:00:00+00:00</published>
		<updated>2021-03-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-ternary-operator/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-ternary-operator/</id>
		<content type="html">&lt;p&gt;The ternary operator in Ruby is a C-style conditional expression that allows you to quickly define a conditional.&lt;&#x2F;p&gt;
&lt;p&gt;You only need one line and it comes in handy when you need to do a variable assignment based on a condition.&lt;&#x2F;p&gt;
&lt;p&gt;The expression takes the form of: &lt;code&gt;&amp;lt;condition&amp;gt; ? &amp;lt;value if true&amp;gt; : &amp;lt;value if false&amp;gt;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Let&#x27;s for example say that we want to assign the name of a bird based on the trait that it flies.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of writing an if&#x2F;else like this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= if&lt;&#x2F;span&gt;&lt;span&gt; bird.can_fly
&lt;&#x2F;span&gt;&lt;span&gt;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pidgin&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ostrich&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;       &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can write it as follows with the ternary operator.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; bird.can_fly &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pidgin&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ostrich&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;nested-ternary-operator&quot;&gt;Nested Ternary Operator&lt;&#x2F;h2&gt;
&lt;p&gt;If you want to go crazy you can nest a ternary operator as well, since it&#x27;s just an expression.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; bird.can_fly &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pidgin&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;(bird.can_swin &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Penguin&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Ostrich&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Comparisons with the Ruby spaceship operator</title>
		<published>2021-03-08T00:00:00+00:00</published>
		<updated>2021-03-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-spaceship-operator/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-spaceship-operator/</id>
		<content type="html">&lt;p&gt;The spaceship operator (&lt;code&gt;&amp;lt;=&amp;gt;&lt;&#x2F;code&gt;) is a comparison operator that is implemented by Ruby&#x27;s &lt;code&gt;Comparable&lt;&#x2F;code&gt; mixin. With the spaceship operator you can do a three-way comparison, to do the comparisons &lt;code&gt;&amp;lt;&lt;&#x2F;code&gt;, &lt;code&gt;==&lt;&#x2F;code&gt;, and &lt;code&gt;&amp;gt;&lt;&#x2F;code&gt; in a single operation. It returns either -1, 0 or 1 depending on if the other object is respectively; less than, equal to, or greater than the other object.&lt;&#x2F;p&gt;
&lt;p&gt;With strings, where the string length is used for the comparison, it gives the following results for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rb&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rb &quot;&gt;&lt;code class=&quot;language-rb&quot; data-lang=&quot;rb&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ab&amp;#39;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; -1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ab&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ab&amp;#39;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt;  0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ab&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt;  1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This is internally used by Ruby for sorting arrays. Ruby will swap the places of two adjacent elements in an array if the &lt;code&gt;&amp;lt;=&amp;gt;&lt;&#x2F;code&gt; comparison has a result of 1. If the result is either -1 or 0 it will leave the elements in place.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rb&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rb &quot;&gt;&lt;code class=&quot;language-rb&quot; data-lang=&quot;rb&quot;&gt;&lt;span&gt;letters &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ab&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;a&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;abc&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; letters.sort!.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;inspect
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;quot;a&amp;quot;, &amp;quot;ab&amp;quot;, &amp;quot;abc&amp;quot;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;implementing-the-comparable-mixin&quot;&gt;Implementing the Comparable mixin&lt;&#x2F;h2&gt;
&lt;p&gt;You can include the &lt;code&gt;Comparable&lt;&#x2F;code&gt; mixin in your own classes too. If you do so you get the implementation for the conventional comparison operators (&lt;code&gt;&amp;lt;&lt;&#x2F;code&gt;, &lt;code&gt;&amp;lt;=&lt;&#x2F;code&gt;, &lt;code&gt;==&lt;&#x2F;code&gt;, &lt;code&gt;&amp;gt;=&lt;&#x2F;code&gt;, and &lt;code&gt;&amp;gt;&lt;&#x2F;code&gt;) and the method &lt;code&gt;between?&lt;&#x2F;code&gt; for free, since these make use of the &lt;code&gt;&amp;lt;=&amp;gt;&lt;&#x2F;code&gt; operator. Unless you overwrite them.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;rb&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-rb &quot;&gt;&lt;code class=&quot;language-rb&quot; data-lang=&quot;rb&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Book
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;include &lt;&#x2F;span&gt;&lt;span&gt;Comparable
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;attr &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:pages
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;&amp;lt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;other&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    pages &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;=&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; other.pages
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;initialize&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;str&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    @str &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; str
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You should return &lt;code&gt;nil&lt;&#x2F;code&gt; if the objects are not comparable using the &lt;code&gt;&amp;lt;=&amp;gt;&lt;&#x2F;code&gt; operator.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Using relative line numbers in vim</title>
		<published>2020-12-31T00:00:00+00:00</published>
		<updated>2020-12-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vim-relative-line-numbers/" type="text/html"/>
		<id>https://koenwoortman.com/vim-relative-line-numbers/</id>
		<content type="html">&lt;p&gt;Writing code in an editor without line numbers is like fries without salt. It&#x27;s not that I&#x27;m using line numbers that often, but it just doesn&#x27;t look right.&lt;&#x2F;p&gt;
&lt;p&gt;Enabling them in VIM can be done in the &lt;code&gt;.vimrc&lt;&#x2F;code&gt; by adding the following line.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;vim&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-vim &quot;&gt;&lt;code class=&quot;language-vim&quot; data-lang=&quot;vim&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; ~&#x2F;.vimrc
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable line numbers
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or you can run it as an command like &lt;code&gt;:set number&lt;&#x2F;code&gt; to enable it in your current vim session. Changing the setting like this just won&#x27;t persist. So when you close and open vim again the line numbers are gone again.&lt;&#x2F;p&gt;
&lt;p&gt;Either way, the result this has is the following. Line numbers are shown in our &lt;code&gt;.vimrc&lt;&#x2F;code&gt; file and any other file we open.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;vim&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-vim &quot;&gt;&lt;code class=&quot;language-vim&quot; data-lang=&quot;vim&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; ~&#x2F;.vimrc
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; number &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable line numbers
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Since VIM version 7.3, and in Neovim too, we can show relative line numbers. Which helps for commands that strike multiple lines. We can enable this by adding &lt;code&gt;set relativenumber&lt;&#x2F;code&gt; to our &lt;code&gt;.vimrc&lt;&#x2F;code&gt;. As with regular line number this works as a command too.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;vim&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-vim &quot;&gt;&lt;code class=&quot;language-vim&quot; data-lang=&quot;vim&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; ~&#x2F;.vimrc
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; number          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable line numbers
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;4 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; relativenumber  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable relative line numbers
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we added the setting for relative line numbers, our line numbers as shown as below. For the current line we are at we see the actual line number, for other lines we see the distance from our current line.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;vim&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-vim &quot;&gt;&lt;code class=&quot;language-vim&quot; data-lang=&quot;vim&quot;&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; ~&#x2F;.vimrc
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3   &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; number          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable line numbers
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt; relativenumber  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot; Enable relative line numbers
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pay attention to the &lt;code&gt;set number&lt;&#x2F;code&gt; setting as well, without it only the relative numbers are shown. So the current line would otherwise be numbered &lt;code&gt;0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;jeffkreeftmeijer.com&#x2F;vim-number&#x2F;&quot;&gt;Jeff Kreeftmeijer wrote more extensively about line numbers in vim&lt;&#x2F;a&gt; and open-sourced a sweet &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jeffkreeftmeijer&#x2F;vim-numbertoggle&quot;&gt;vim plugin&lt;&#x2F;a&gt; for automatically switching between absolute and relative line numbers.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Show all databases in your MySQL database</title>
		<published>2020-12-30T00:00:00+00:00</published>
		<updated>2020-12-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/mysql-show-all-databases/" type="text/html"/>
		<id>https://koenwoortman.com/mysql-show-all-databases/</id>
		<content type="html">&lt;p&gt;The most common way to show the databases on your system is by using the &lt;code&gt;SHOW DATABASES&lt;&#x2F;code&gt; statement. This shows a list of all databases that your database user has access to.&lt;&#x2F;p&gt;
&lt;p&gt;So in my case there are databases showing up which MySQL uses for internal use. Like for managing users and privileges for example.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW DATABASES;
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Database           |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| information_schema |
&lt;&#x2F;span&gt;&lt;span&gt;| mysql              |
&lt;&#x2F;span&gt;&lt;span&gt;| performance_schema |
&lt;&#x2F;span&gt;&lt;span&gt;| production         |
&lt;&#x2F;span&gt;&lt;span&gt;| staging            |
&lt;&#x2F;span&gt;&lt;span&gt;| sys                |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;6 rows in set
&lt;&#x2F;span&gt;&lt;span&gt;Time: 0.029s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;SHOW SCHEMAS&lt;&#x2F;code&gt; statement is a synonym for &lt;code&gt;SHOW DATABASES&lt;&#x2F;code&gt;. So you might as well use that, it is shorter to type :).&lt;&#x2F;p&gt;
&lt;p&gt;You can if you need to pass a pattern to this query as well. To filter out some of the resulting rows you&#x27;ll get.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;SHOW DATABASES LIKE &amp;#39;%schema&amp;#39;;
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Database (%schema) |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| information_schema |
&lt;&#x2F;span&gt;&lt;span&gt;| performance_schema |
&lt;&#x2F;span&gt;&lt;span&gt;+--------------------+
&lt;&#x2F;span&gt;&lt;span&gt;2 rows in set
&lt;&#x2F;span&gt;&lt;span&gt;Time: 0.018s
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove a MySQL user</title>
		<published>2020-12-29T00:00:00+00:00</published>
		<updated>2020-12-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/mysql-remove-user/" type="text/html"/>
		<id>https://koenwoortman.com/mysql-remove-user/</id>
		<content type="html">&lt;p&gt;To remove a user and its privileges from a MySQL database you use the &lt;code&gt;DROP USER&lt;&#x2F;code&gt; statement. With MySQL you can create multiple users and assign different permissions. But it&#x27;s a good idea to not keep unused accounts around.&lt;&#x2F;p&gt;
&lt;p&gt;You can delete a user in MySQL with the following statement.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; DROP USER &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;morty&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;@&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;localhost&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This would have dropped the user &lt;code&gt;morty&lt;&#x2F;code&gt; from the host &lt;code&gt;localhost&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Keep in mind that this change takes effect after the sessions of the deleted user is closed. If the dropped user has an open session, the user isn&#x27;t dropped until its session is closed.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;Ofcourse you can&#x27;t just go dropping users left and right. In order to do so you at least need either the global &lt;code&gt;CREATE USER&lt;&#x2F;code&gt; or &lt;code&gt;DELETE&lt;&#x2F;code&gt; privilege in mysql system schema.&lt;&#x2F;p&gt;
&lt;p&gt;You can &lt;a href=&quot;&#x2F;mysql-show-user-privileges&#x2F;&quot;&gt;list your user privileges in MySQL as well.&lt;&#x2F;a&gt; For this you need the &lt;code&gt;SHOW GRANTS&lt;&#x2F;code&gt; statement.&lt;&#x2F;p&gt;
&lt;p&gt;This shows the grants of the currently logged in user.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW GRANTS;
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Grants for koen@localhost                         |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GRANT ALL PRIVILEGES ON *.* TO &amp;#39;koen&amp;#39;@&amp;#39;localhost&amp;#39; |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;1 row in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the user doesn&#x27;t have the sufficient rights to drop a user you should be able to drop it as the &lt;code&gt;root&lt;&#x2F;code&gt; user.&lt;&#x2F;p&gt;
&lt;p&gt;One thing that might comes in handy is a query to show the users which are currently present. In order to do so you can query the &lt;code&gt;mysql.user&lt;&#x2F;code&gt; table.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sql&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sql &quot;&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;mysql&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt; SELECT&lt;&#x2F;span&gt;&lt;span&gt; User, Host &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;FROM &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;mysql&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;---------------+-----------+
&lt;&#x2F;span&gt;&lt;span&gt;| User          | Host      |
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;---------------+-----------+
&lt;&#x2F;span&gt;&lt;span&gt;| koen          | localhost |
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;mysql&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;session&lt;&#x2F;span&gt;&lt;span&gt; | localhost |
&lt;&#x2F;span&gt;&lt;span&gt;| &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;mysql&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;sys&lt;&#x2F;span&gt;&lt;span&gt;     | localhost |
&lt;&#x2F;span&gt;&lt;span&gt;| root          | localhost |
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;---------------+-----------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a file exists with Bash</title>
		<published>2020-12-28T00:00:00+00:00</published>
		<updated>2020-12-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-check-if-file-exists/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-check-if-file-exists/</id>
		<content type="html">&lt;p&gt;In your Bash script you&#x27;ll have to deal with files at some point. So knowing for sure whether a file exists or not comes in handy at some point. You can test for these conditions in an if statement luckily.&lt;&#x2F;p&gt;
&lt;p&gt;Normally, in Bash, the preferred way to test this condition is by using double square brackets(&lt;code&gt;[[ … ]]&lt;&#x2F;code&gt;), which is the improved version of the single square brackets (&lt;code&gt;[ … ]&lt;&#x2F;code&gt;). Do keep in mind however that the double square brackets might not be available in other shells. So for portability you might want to prefer single square brackets over the double ones.&lt;&#x2F;p&gt;
&lt;p&gt;To test for the existence of a file we can use the &lt;code&gt;-f&lt;&#x2F;code&gt; file test operator. The &lt;code&gt;-f&lt;&#x2F;code&gt; operator returns true if the file for the specified path exists and is a actual file, so not a directory.&lt;&#x2F;p&gt;
&lt;p&gt;In the script below an example is shown, in which a file is created first and secondly the existence is validated.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file
&lt;&#x2F;span&gt;&lt;span&gt;touch ~&#x2F;dirty_secrets.txt
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Check whether the file exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets.txt&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;You have dirty secrets&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And now run the script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ .&#x2F;dirty-secrets
&lt;&#x2F;span&gt;&lt;span&gt;You have dirty secrets
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There are a couple things that the &lt;code&gt;-f&lt;&#x2F;code&gt; operator does and doesn’t take into account. It does check if the specified file is actually a file. So if the specified path is an existing directory it still returns false. It doesn&#x27;t check whether the file is a symlink or not. So if the specified path is a path to a symlink, it does return true.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;test-if-a-file-doesn-t-exist&quot;&gt;Test if a file doesn&#x27;t exist&lt;&#x2F;h2&gt;
&lt;p&gt;For checking if a file is not existing we can do something similar. In order to check whether a file doesn’t exist we negate the condition with an exclamation mark.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. Now we remove the file
&lt;&#x2F;span&gt;&lt;span&gt;rm ~&#x2F;dirty_secrets.txt
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. And check whether the file exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets.txt&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Good boy, no dirty secrets&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets see the result of the updated version of the script now.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ .&#x2F;dirty-secrets
&lt;&#x2F;span&gt;&lt;span&gt;Good boy, no dirty secrets
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;create-file-if-it-doesn-t-exist&quot;&gt;Create file if it doesn&#x27;t exist&lt;&#x2F;h2&gt;
&lt;p&gt;A more likely use-case is that you want to create a file if it doesn’t exist yet. With a small modification we can do just that.&lt;&#x2F;p&gt;
&lt;p&gt;We can create the file by using the &lt;code&gt;touch&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. Store the files path in a variable
&lt;&#x2F;span&gt;&lt;span&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets.txt&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Create a file if it doesn&amp;#39;t exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;! &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span&gt;$file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  touch $file
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Created file &amp;#39;$file&amp;#39;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets see what happens when we run this version of the script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ .&#x2F;dirty-secrets
&lt;&#x2F;span&gt;&lt;span&gt;Created file &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;home&#x2F;koen&#x2F;dirty_secrets.txt&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;test-if-a-folder-exist&quot;&gt;Test if a folder exist&lt;&#x2F;h2&gt;
&lt;p&gt;Testing for the existence of a directory is the same but uses a different operator. Where we used &lt;code&gt;-f&lt;&#x2F;code&gt; to check for the existence of a file, we use &lt;code&gt;-d&lt;&#x2F;code&gt; for directories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file
&lt;&#x2F;span&gt;&lt;span&gt;mkdir ~&#x2F;dirty_secrets
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Check whether the directory exist
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-d &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;You have dirty secrets&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we run the script we get the following output.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ .&#x2F;dirty-secrets
&lt;&#x2F;span&gt;&lt;span&gt;You have dirty secrets
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;check-multiple-files-at-once&quot;&gt;Check multiple files at once&lt;&#x2F;h2&gt;
&lt;p&gt;Checking for multiple files at once can be done as well. In order to do so you combine the conditions with &lt;code&gt;&amp;amp;&amp;amp;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets&#x2F;mine.txt&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-f &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;dirty_secrets&#x2F;ours.txt&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;You have dirty secrets&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;file-test-operators&quot;&gt;File test operators&lt;&#x2F;h2&gt;
&lt;p&gt;In the above examples we used only the &lt;code&gt;-f&lt;&#x2F;code&gt; and &lt;code&gt;-d&lt;&#x2F;code&gt; operators. But there are a couple other useful operators for dealing with files. They are summed up in the table below.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Operator&lt;&#x2F;th&gt;&lt;th&gt;Returns true when&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-f&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file exists and is a regular file&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-d&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file exists and is a directory&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-e&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file exists&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-s&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file is not zero size&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-L&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file is a symbolic link&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-S&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file is a socket&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;-x&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;file has execute permissions&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h2 id=&quot;learning-more&quot;&gt;Learning more&lt;&#x2F;h2&gt;
&lt;p&gt;The best way to learn more is to use Bash. A lot. Don&#x27;t forget that Google is your friend.&lt;&#x2F;p&gt;
&lt;p&gt;In case you learn well from books I would recommend these.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1593279523&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1593279523&amp;amp;linkId=9cf8e9ac8913afbf3dcc8c591ad0780a&quot;&gt;The Linux Command Line: A Complete Introduction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0596009658&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0596009658&amp;amp;linkId=80e3fbe123ef781b1b84db41e56eb17c&quot;&gt;Learning the bash Shell: Unix Shell Programming&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Add a directory to PATH in ZSH</title>
		<published>2020-12-27T00:00:00+00:00</published>
		<updated>2020-12-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zsh-add-directory-to-path/" type="text/html"/>
		<id>https://koenwoortman.com/zsh-add-directory-to-path/</id>
		<content type="html">&lt;p&gt;The place where you want to add directories to your PATH is in the &lt;code&gt;.zshrc&lt;&#x2F;code&gt; file. Since this file gets loaded once you enter the shell, you make sure your settings don’t disappear when restarting the shell.&lt;&#x2F;p&gt;
&lt;p&gt;The more generic way to add a directory to the path is to export a PATH variable in your &lt;code&gt;.zshrc&lt;&#x2F;code&gt; file. This approach works just as well in the Bash shell. You set this PATH variable equal to a string of paths separated by a colon(&lt;code&gt;:&lt;&#x2F;code&gt;). Often you want to include the current value of PATH in this string as well, by doing this you add paths instead of overwriting PATH.&lt;&#x2F;p&gt;
&lt;p&gt;For example, if you would like to add the paths &lt;code&gt;~&#x2F;bin&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;home&#x2F;koen&#x2F;.local&#x2F;bin&lt;&#x2F;code&gt; to your PATH you would add the following to your &lt;code&gt;.zshrc&lt;&#x2F;code&gt;. You can make use of tilde expansion here, as a shortcut for your home directory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;PATH&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;~&#x2F;bin:&#x2F;home&#x2F;koen&#x2F;.local&#x2F;bin:$PATH
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;ZSH does also offer another way to add paths to the PATH variable, like it is a list of some sort.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;~&#x2F;bin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+=&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;home&#x2F;koen&#x2F;.local&#x2F;bin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;export &lt;&#x2F;span&gt;&lt;span&gt;PATH
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;so-what-exactly-is-this-path&quot;&gt;So what exactly is this $PATH?&lt;&#x2F;h2&gt;
&lt;p&gt;PATH is an environment variable where you tell your system to look for executables when you enter a command in the shell.&lt;&#x2F;p&gt;
&lt;p&gt;You can inspect the value of the PATH variable by running the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ echo $PATH
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This outputs a string with locations like &lt;code&gt;&#x2F;usr&#x2F;bin&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;bin&lt;&#x2F;code&gt;. When you install a program like git, the executable program for git is placed in one of these directories. So that your system can actually find the commands you run.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Show user privileges in MySQL</title>
		<published>2020-12-23T00:00:00+00:00</published>
		<updated>2020-12-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/mysql-show-user-privileges/" type="text/html"/>
		<id>https://koenwoortman.com/mysql-show-user-privileges/</id>
		<content type="html">&lt;p&gt;First off make sure you&#x27;re logged in to MySQL. You can do this via a GUI, like MySQL Workbench, or via the MySQL shell:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ mysql -u &amp;lt;user&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that you&#x27;re logged in to MySQL you can simply show the grants of the current user by running one of the following query.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW GRANTS;
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Grants for koen@localhost                         |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GRANT ALL PRIVILEGES ON *.* TO &amp;#39;koen&amp;#39;@&amp;#39;localhost&amp;#39; |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;1 row in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to find the grants given to another user you need to pass that user and the host to &lt;code&gt;SHOW GRANTS&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW GRANTS FOR &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39;;
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Grants for root@localhost                                           |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GRANT ALL PRIVILEGES ON *.* TO &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39; WITH GRANT OPTION |
&lt;&#x2F;span&gt;&lt;span&gt;| GRANT PROXY ON &amp;#39;&amp;#39;@&amp;#39;&amp;#39; TO &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39; WITH GRANT OPTION        |
&lt;&#x2F;span&gt;&lt;span&gt;+---------------------------------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;2 rows in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By the way, if you need inspiration for querying users you can query the &lt;code&gt;mysql.user&lt;&#x2F;code&gt; table.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SELECT Host, User FROM mysql.user;
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+---------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Host      | User          |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+---------------+
&lt;&#x2F;span&gt;&lt;span&gt;| localhost | koen          |
&lt;&#x2F;span&gt;&lt;span&gt;| localhost | mysql.session |
&lt;&#x2F;span&gt;&lt;span&gt;| localhost | mysql.sys     |
&lt;&#x2F;span&gt;&lt;span&gt;| localhost | root          |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+---------------+
&lt;&#x2F;span&gt;&lt;span&gt;4 rows in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Unfortunately getting all grants for a specific user isn&#x27;t as easy passing just the user and not the host :(.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SHOW GRANTS FOR &amp;#39;root&amp;#39;;
&lt;&#x2F;span&gt;&lt;span&gt;ERROR 1141 (42000): There is no such grant defined for user &amp;#39;root&amp;#39; on host &amp;#39;%&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The closest thing I came across to showing all the grants for a user is the query &lt;code&gt;SELECT * FROM information_schema.user_privileges;&lt;&#x2F;code&gt;. This does show the grants per privilege.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;mysql&amp;gt; SELECT * FROM information_schema.user_privileges;
&lt;&#x2F;span&gt;&lt;span&gt;+-----------------------------+---------------+-------------------------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GRANTEE                     | TABLE_CATALOG | PRIVILEGE_TYPE          | IS_GRANTABLE |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------------------------+---------------+-------------------------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;| &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39;          | def           | SELECT                  | YES          |
&lt;&#x2F;span&gt;&lt;span&gt;| &amp;#39;root&amp;#39;@&amp;#39;localhost&amp;#39;          | def           | INSERT                  | YES          |
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;| &amp;#39;koen&amp;#39;@&amp;#39;localhost&amp;#39;          | def           | CREATE TABLESPACE       | NO           |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------------------------+---------------+-------------------------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;86 rows in set (0,00 sec)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Join two Python tuples</title>
		<published>2020-12-22T00:00:00+00:00</published>
		<updated>2020-12-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-join-two-tuples/" type="text/html"/>
		<id>https://koenwoortman.com/python-join-two-tuples/</id>
		<content type="html">&lt;p&gt;Tuples in Python are immutable, meaning they can&#x27;t be modified once they are created. Adding elements or reordering elements is a no-no. So methods like &lt;code&gt;.append()&lt;&#x2F;code&gt; or &lt;code&gt;.sort()&lt;&#x2F;code&gt; which you can use on lists are not available on tuples.&lt;&#x2F;p&gt;
&lt;p&gt;Meaning that in order to add elements to a tuple you need to merge two tuples together by using the &lt;code&gt;+=&lt;&#x2F;code&gt; operator.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;toppings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tuna&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cheese&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salami&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;pepper&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;toppings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+= &lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tomato&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(toppings)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; (&amp;#39;tuna&amp;#39;, &amp;#39;cheese&amp;#39;, &amp;#39;salami&amp;#39;, &amp;#39;pepper&amp;#39;, &amp;#39;tomato&amp;#39;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Pay attention to the trailing comma in &lt;code&gt;(&#x27;tomato&#x27;,)&lt;&#x2F;code&gt;, without the comma this would be evaluated as a string.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>6 Non-Programming Books for Programmers</title>
		<published>2020-12-21T00:00:00+00:00</published>
		<updated>2020-12-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/non-programming-books-for-programmers/" type="text/html"/>
		<id>https://koenwoortman.com/non-programming-books-for-programmers/</id>
		<content type="html">&lt;p&gt;Over the years I&#x27;ve collected a pretty extensive library of books. With the majority of them being about software development or a closely related topic. But once every while I find myself reading something totally different. The following is an updated list of non-programming books that made me a better programmer.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;health&quot;&gt;Health&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;why-we-sleep&quot;&gt;Why We Sleep&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3nyw1yI&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=1501144316&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=1501144316&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3nyw1yI&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For as long as I&#x27;ve been in software development there have always been developers around me who seem to have their brightest moments during the night. There is always &lt;strong&gt;that&lt;&#x2F;strong&gt; guy making commits at 2 AM. Granted, working at night does have its advantages. There are a lot less distractions and the bright screen seems to keep you awake pretty well, besides the caffeine I mean. Whether it is beneficial to your code or not, not needing a lot of sleep somehow seemed to be a cool thing as well.&lt;&#x2F;p&gt;
&lt;p&gt;But oh boy, bad things happen to your body when you&#x27;re getting less than 7 hours of sleep a night. Neuroscientist Matthew Walker describes in a very approachable manner the effects that sleep has on our creativity, memory, decision making, eating habits and more. Even scarier is the increased chance for Alzheimer’s disease.&lt;&#x2F;p&gt;
&lt;p&gt;In the long term good work doesn’t come from working till late at night, for me it comes from a well rested mind. After sticking to a more regular sleeping schedule I noticed that good sleep was the one thing that influences about everything. I had more energy, it was easier to focus, my mood became better and I got way more done while being less stressed.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;There is nothing &lt;em&gt;lazy&lt;&#x2F;em&gt; about getting enough sleep and not getting up early. Genetically there is actually such a thing as morning and evening people.&lt;&#x2F;li&gt;
&lt;li&gt;Not getting enough sleep is terrible for your immune system. It also more than doubled your chances of getting cancer.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;productivity&quot;&gt;Productivity&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;eat-that-frog&quot;&gt;Eat That Frog&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;38nTyfg&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=162656941X&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=162656941X&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;38nTyfg&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Whatever your definition is of time management, you’ll likely need to do some of it. There is quite some dry material out there with regard to time management. But in my opinion this book does a great job in cutting out the unnecessary stuff and delivers practical advice on just a couple pages per subject.&lt;&#x2F;p&gt;
&lt;p&gt;Procrastination was kind of my thing and I took some pride in it too. But doing things last minute mostly leads to stress. While that stress can give you a great rush, at some point it just gets exhausting. In the book are 21 tools to help you become more productive. You’ll likely don’t need all of them but some will certainly help you on a daily basis.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Do the most important task early in the day. You will feel great for the rest of the day, instead of anxiously looking forward to something big.&lt;&#x2F;li&gt;
&lt;li&gt;Don&#x27;t try to do too much important task on a day. Stick to a maximum of two important tasks.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h3 id=&quot;the-4-hour-workweek&quot;&gt;The 4-Hour Workweek&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;2JdoVAV&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=0307465357&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=0307465357&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;2JdoVAV&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Despite the title, if you like your job and aren’t necessary looking to work 4 hours a week this book is still a good read. Out of all the books this is the one that might have influenced me the most and I revisit it for a re-read every now and then. This book covers a wide range of topics, you can use the tips and tricks to retire early and travel around the world. But for me the most important takeaway was the notion of effectiveness. Instead of increasing the input hours focus on your per-hour output.&lt;&#x2F;p&gt;
&lt;p&gt;Efficiency is important, but &lt;em&gt;what&lt;&#x2F;em&gt; you’re doing is always more important than &lt;em&gt;how&lt;&#x2F;em&gt; you do it. Most of the people who get more done than you don’t work harder or more, they work on the right things.&lt;&#x2F;p&gt;
&lt;p&gt;Another key concept of the book is automation. As a developer this was hard not to get excited about. From scripts that write boring statistics to spreadsheets to this blog article being automatically deployed by webhook to Netlify. There is just a feeling of satisfaction you get when you can just blindly ignore something knowing it gets done.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Put your energy and efforts in the things that actually matter.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;psychology&quot;&gt;Psychology&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;power-of-habit&quot;&gt;Power of Habit&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3aqaMvp&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=081298160X&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=081298160X&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3aqaMvp&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since starting my first full-time job as a developer I picked up a couple bad habits along the way, and I am talking about everything from debugging with console.logs&#x2F;print statements to having a terrible diet. Phasing-out the bad habits overtime can be challenging. You can start off great, but before you know it you fall back in your old patterns.&lt;&#x2F;p&gt;
&lt;p&gt;Turns out there is a science to this. And often it is easier to trick your mind into taking on a better habit than trying to fight it off.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Meaningful goals are achieved by discipline and consistency. Turning things into a habit makes the hard work maintainable.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;collaboration&quot;&gt;Collaboration&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;how-to-win-friends-and-influence-people&quot;&gt;How to Win Friends and Influence People&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3nBQAKz&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=0671027034&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=0671027034&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;3nBQAKz&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The title was a bit off putting for me before buying the book. But since it seemed to be somewhat of a classic I gave it a try anyway and wasn’t disappointed. I did have the feeling it was written more with a management type of person in mind as its reader. However, since you will work together with others at some point, getting advice on relations with others can’t hurt. Although all principles in the book are not ones you couldn’t have come up with yourself, it is nice to get them in a comprehensive list form.&lt;&#x2F;p&gt;
&lt;p&gt;A lot of issues and frustrations that come from code are personal opinions. While the actual code has been someone else his or her hard work. So the general advice here is; don’t let pull requests drag you into a hostile place.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Don’t criticize or complain as a first response.&lt;&#x2F;li&gt;
&lt;li&gt;Give sincere appreciation.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;business&quot;&gt;Business&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;it-doesn-t-have-to-be-crazy-at-work&quot;&gt;It Doesn&#x27;t Have to Be Crazy at Work&lt;&#x2F;h3&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;2WuW2TN&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;&#x2F;&#x2F;ws-na.amazon-adsystem.com&#x2F;widgets&#x2F;q?_encoding=UTF8&amp;ASIN=0062874780&amp;Format=_SL250_&amp;ID=AsinImage&amp;MarketPlace=US&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=koensw-20&amp;language=en_US&quot; &gt;&lt;&#x2F;a&gt;&lt;img src=&quot;https:&#x2F;&#x2F;ir-na.amazon-adsystem.com&#x2F;e&#x2F;ir?t=koensw-20&amp;language=en_US&amp;l=li3&amp;o=1&amp;a=0062874780&quot; width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; alt=&quot;&quot; style=&quot;border:none !important; margin:0px !important;&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;amzn.to&#x2F;2WuW2TN&quot; target=&quot;_blank&quot; class=&quot;external-link&quot;&gt;Amazon&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;p&gt;For way too long I thought that if you’re not doing stand-ups and two week sprints you’re doing software development wrong. It so happens that the people at Basecamp have a totally different view on software development and seem to get an incredible amount of work done. Lucky for us they wrote a book about how they do it.&lt;&#x2F;p&gt;
&lt;p&gt;They have a clear stance on what they think work should be. Instead of the two week rushes they opt for sanity and calmness in the office. Emphasizing that you should be protective of your time. Working 8-hours of work a day is plenty to get a whole lot done. That is, if you’re not spending half of it in irrelevant meetings and answering Slack messages.&lt;&#x2F;p&gt;
&lt;p&gt;Whether it is or isn’t crazy at your work. It is good to know there are multiple ways to approach software development and be successfully doing so.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Learnings&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You will likely get the most done when you have long stretches of uninterrupted time to work.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remap the ctrl+b tmux prefix to ctrl+a</title>
		<published>2020-12-20T00:00:00+00:00</published>
		<updated>2020-12-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-remap-ctrl-b-to-ctrl-a/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-remap-ctrl-b-to-ctrl-a/</id>
		<content type="html">&lt;p&gt;When coming from GNU screen the &lt;code&gt;ctrl+b&lt;&#x2F;code&gt; tmux uses by default is somewhat hard to get used to. Besides that it isn&#x27;t as nicely reachable as the &lt;code&gt;ctrl+a&lt;&#x2F;code&gt; prefix used by GNU screen.&lt;&#x2F;p&gt;
&lt;p&gt;Adjusting this prefix takes a couple lines of configuration in the &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; config file. Following the example in the tmux man pages, we make the following addition in the tmux config.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;unbind-key C-b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;set-option -g prefix C-a
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;bind-key C-a send-prefix
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a custom Django manage.py command</title>
		<published>2020-12-19T00:00:00+00:00</published>
		<updated>2020-12-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-create-custom-command/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-create-custom-command/</id>
		<content type="html">&lt;p&gt;When you used Django before you undoubtedly used a subcommand of &lt;code&gt;manage.py&lt;&#x2F;code&gt;, such as &lt;code&gt;runserver&lt;&#x2F;code&gt; and &lt;code&gt;migrate&lt;&#x2F;code&gt;. You can also create custom django commands that can be invoked via &lt;code&gt;manage.py&lt;&#x2F;code&gt;. This can be useful for administrative tasks in your application. Like running a background job for example.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;create-a-command&quot;&gt;Create a command&lt;&#x2F;h2&gt;
&lt;p&gt;Django looks for custom management commands in the folder &lt;code&gt;management&#x2F;commands&lt;&#x2F;code&gt; of the apps you registered in the &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt; setting. The command itself is just a python file in the &lt;code&gt;commands&lt;&#x2F;code&gt; folder.&lt;&#x2F;p&gt;
&lt;p&gt;If we would have a plants app in our Django application that should have a &lt;code&gt;waterplants&lt;&#x2F;code&gt; command, to water the plants, we add a &lt;code&gt;waterplants.py&lt;&#x2F;code&gt; file to the &lt;code&gt;commands&lt;&#x2F;code&gt; folder.&lt;&#x2F;p&gt;
&lt;p&gt;So that our plants app has the following files. Just an fyi, files starting with a underscore(&lt;code&gt;_&lt;&#x2F;code&gt;) are not registered as custom management command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ tree plants
&lt;&#x2F;span&gt;&lt;span&gt;plants
&lt;&#x2F;span&gt;&lt;span&gt;├── admin.py
&lt;&#x2F;span&gt;&lt;span&gt;├── apps.py
&lt;&#x2F;span&gt;&lt;span&gt;├── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;├── management
&lt;&#x2F;span&gt;&lt;span&gt;│   └── commands
&lt;&#x2F;span&gt;&lt;span&gt;│       └── waterplants.py
&lt;&#x2F;span&gt;&lt;span&gt;├── migrations
&lt;&#x2F;span&gt;&lt;span&gt;│   └── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;├── models.py
&lt;&#x2F;span&gt;&lt;span&gt;├── tests.py
&lt;&#x2F;span&gt;&lt;span&gt;└── views.py
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;code&gt;$ tree plants&lt;&#x2F;code&gt; lol :)&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;If you run &lt;code&gt;python manage.py help&lt;&#x2F;code&gt; you should see your new command as available subcommand.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py help
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Type &amp;#39;manage.py help &amp;lt;subcommand&amp;gt;&amp;#39; for help on a specific subcommand.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Available subcommands:
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;[plants]
&lt;&#x2F;span&gt;&lt;span&gt;    waterplants
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However, running the command at this state doesn&#x27;t do a whole lot. Actually, it throws an exception:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;AttributeError: module &#x27;plants.management.commands.waterplants&#x27; has no attribute &#x27;Command&#x27;&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;What Django expects a class &lt;code&gt;Command&lt;&#x2F;code&gt; which is a subclass of &lt;code&gt;BaseCommand&lt;&#x2F;code&gt; in this &lt;code&gt;waterplants.py&lt;&#x2F;code&gt; we just created. To get rid of this exception can past in the following piece of code.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# .&#x2F;plants&#x2F;management&#x2F;commands&#x2F;waterplants.py
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.core.management.base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseCommand
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseCommand&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Water the plants&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;pass
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;handle&lt;&#x2F;code&gt; method is the entry point of the command. When the command is invoked by Django the &lt;code&gt;handle&lt;&#x2F;code&gt; method will be called.&lt;&#x2F;p&gt;
&lt;p&gt;As you see we have set a &lt;code&gt;help&lt;&#x2F;code&gt; property for the Command class. This string is used in the help message for the management command if we pass the &lt;code&gt;--help&lt;&#x2F;code&gt; flag. We see that we get some optional arguments for free from the BaseCommand class.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py waterplants --help
&lt;&#x2F;span&gt;&lt;span&gt;usage: manage.py waterplants [-h] [--version] [-v {0,1,2,3}]
&lt;&#x2F;span&gt;&lt;span&gt;                             [--settings SETTINGS] [--pythonpath PYTHONPATH]
&lt;&#x2F;span&gt;&lt;span&gt;                             [--traceback] [--no-color] [--force-color]
&lt;&#x2F;span&gt;&lt;span&gt;                             [--skip-checks]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Water the plants
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;optional arguments:
&lt;&#x2F;span&gt;&lt;span&gt;  -h, --help            show this help message and exit
&lt;&#x2F;span&gt;&lt;span&gt;  --version             show program&amp;#39;s version number and exit
&lt;&#x2F;span&gt;&lt;span&gt;  -v {0,1,2,3}, --verbosity {0,1,2,3}
&lt;&#x2F;span&gt;&lt;span&gt;                        Verbosity level; 0=minimal output, 1=normal output,
&lt;&#x2F;span&gt;&lt;span&gt;                        2=verbose output, 3=very verbose output
&lt;&#x2F;span&gt;&lt;span&gt;  --settings SETTINGS   The Python path to a settings module, e.g.
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;quot;myproject.settings.main&amp;quot;. If this isn&amp;#39;t provided, the
&lt;&#x2F;span&gt;&lt;span&gt;                        DJANGO_SETTINGS_MODULE environment variable will be
&lt;&#x2F;span&gt;&lt;span&gt;                        used.
&lt;&#x2F;span&gt;&lt;span&gt;  --pythonpath PYTHONPATH
&lt;&#x2F;span&gt;&lt;span&gt;                        A directory to add to the Python path, e.g.
&lt;&#x2F;span&gt;&lt;span&gt;                        &amp;quot;&#x2F;home&#x2F;djangoprojects&#x2F;myproject&amp;quot;.
&lt;&#x2F;span&gt;&lt;span&gt;  --traceback           Raise on CommandError exceptions
&lt;&#x2F;span&gt;&lt;span&gt;  --no-color            Don&amp;#39;t colorize the command output.
&lt;&#x2F;span&gt;&lt;span&gt;  --force-color         Force colorization of the command output.
&lt;&#x2F;span&gt;&lt;span&gt;  --skip-checks         Skip system checks.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;accessing-models&quot;&gt;Accessing models&lt;&#x2F;h2&gt;
&lt;p&gt;Since you&#x27;re not gonna make management commands for the sake of it. You probably want them to interact with your models in some way.&lt;&#x2F;p&gt;
&lt;p&gt;There is not much special about doing this from a command compared to any other place in your Django application. You can just import the model class in your command file and interact with it as you would normally do from a view for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.core.management.base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseCommand
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;plants.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Plant
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseCommand&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Water the plants&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        plants &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Plant.objects.all()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;plant &lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;is&lt;&#x2F;span&gt;&lt;span&gt; plants&lt;&#x2F;span&gt;&lt;span style=&quot;background-color:#f92672;color:#f8f8f0;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;            plant.water()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;writing-output&quot;&gt;Writing output&lt;&#x2F;h2&gt;
&lt;p&gt;A simple &lt;code&gt;print()&lt;&#x2F;code&gt; will work. But the BaseCommand comes with two other options as well. The BaseCommand sets the &lt;code&gt;stdout&lt;&#x2F;code&gt; and &lt;code&gt;stderr&lt;&#x2F;code&gt; output streams as its properties and we can write to them by calling a &lt;code&gt;write&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.core.management.base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseCommand
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;plants.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Plant
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseCommand&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Water the plants&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        plants &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Plant.objects.all()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;plant &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;plants:
&lt;&#x2F;span&gt;&lt;span&gt;            plant.water()
&lt;&#x2F;span&gt;&lt;span&gt;            self.stdout.write(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;watered plant &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;{plant}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we added that to our command we actually see some output when we run the custom management command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py waterplants
&lt;&#x2F;span&gt;&lt;span&gt;watered plant &amp;quot;Monstera&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;watered plant &amp;quot;Ficus&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;watered plant &amp;quot;Cactus&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you like you can also add some colors to the output. If we were to change the line that writes to stdout to:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;self.stdout.write(self.style.SUCCESS(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;watered plant &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;{plant}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We would see the output having green text when rerunning the command.&lt;&#x2F;p&gt;
&lt;p&gt;Some useful style options you get for writing output are:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Success: &lt;code&gt;self.style.SUCCESS(&#x27;...&#x27;)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Notice: &lt;code&gt;self.style.NOTICE(&#x27;...&#x27;)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Warning: &lt;code&gt;self.style.WARNING(&#x27;...&#x27;)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;li&gt;Error: &lt;code&gt;self.style.ERROR(&#x27;...&#x27;)&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;command-errors&quot;&gt;Command errors&lt;&#x2F;h2&gt;
&lt;p&gt;When throwing exceptions in your command class you normally want to use the &lt;code&gt;CommandError&lt;&#x2F;code&gt; class that is provided by Django. Since Django will intercept it and write the error message to &lt;code&gt;stderr&lt;&#x2F;code&gt;, besides that it exits the command with a non-zero return code as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.core.management.base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseCommand, CommandError
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;plants.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Plant
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseCommand&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Water the plants&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        plants &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Plant.objects.all()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;plants:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span&gt;CommandError(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;No plants to water&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;plant &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;plants:
&lt;&#x2F;span&gt;&lt;span&gt;            plant.water()
&lt;&#x2F;span&gt;&lt;span&gt;            self.stdout.write(self.style.SUCCESS(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;watered plant &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;{plant}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;custom-return-codes&quot;&gt;Custom return codes&lt;&#x2F;h3&gt;
&lt;p&gt;As said above, the &lt;code&gt;CommandError&lt;&#x2F;code&gt; class exits the script with a non-zero return code. It returns &lt;code&gt;1&lt;&#x2F;code&gt; to be precise. Using a different return code might be useful in some cases.&lt;&#x2F;p&gt;
&lt;p&gt;To do so you can easily pass the &lt;code&gt;returncode&lt;&#x2F;code&gt; keyword argument when throwing the exception.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.core.management.base &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;BaseCommand, CommandError
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;plants.models &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Plant
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;Command&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;font-style:italic;color:#a6e22e;&quot;&gt;BaseCommand&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;help &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Water the plants&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;handle&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;args&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;options&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        plants &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Plant.objects.all()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;plants:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;raise &lt;&#x2F;span&gt;&lt;span&gt;CommandError(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;No plants to water&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;returncode&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;plant &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;plants:
&lt;&#x2F;span&gt;&lt;span&gt;            plant.water()
&lt;&#x2F;span&gt;&lt;span&gt;            self.stdout.write(self.style.SUCCESS(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;f&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;watered plant &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;{plant}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the current directory in a Bash script</title>
		<published>2020-10-31T00:00:00+00:00</published>
		<updated>2020-10-31T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-get-current-directory/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-get-current-directory/</id>
		<content type="html">&lt;p&gt;When scripting together a bash script you might need to know your current working directory at some point.&lt;&#x2F;p&gt;
&lt;p&gt;The current working directory is conveniently stored in the variable &lt;code&gt;$PWD&lt;&#x2F;code&gt; which is available in all POSIX compliant shells. Another option to get the current working directory is by using the &lt;code&gt;pwd&lt;&#x2F;code&gt; command. That &lt;code&gt;pwd&lt;&#x2F;code&gt; command is a shell builtin and available in all POSIX compliant shells as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Option 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;home&#x2F;koen
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$PWD
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Option 2
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;home&#x2F;koen&#x2F;Test
&lt;&#x2F;span&gt;&lt;span&gt;CWD&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;$(pwd)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$CWD
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;When we run this script it produced the following output. The first output being the value of &lt;code&gt;$PWD&lt;&#x2F;code&gt;. The second line being the output of &lt;code&gt;$(pwd)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ bash print_cwd
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;home&#x2F;koen
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;home&#x2F;koen&#x2F;Test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;what-about-symlinks&quot;&gt;What about symlinks?&lt;&#x2F;h3&gt;
&lt;p&gt;The example shown before doesn&#x27;t take into account symlinks. So if you run &lt;code&gt;pwd&lt;&#x2F;code&gt; or &lt;code&gt;$PWD&lt;&#x2F;code&gt; in a folder that is symlinked it doesn&#x27;t show you the physical location but the location of the symlink.&lt;&#x2F;p&gt;
&lt;p&gt;If you need the actual physical location of the directory in your script you can use the &lt;code&gt;-P&lt;&#x2F;code&gt; option on the &lt;code&gt;pwd&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;That &lt;code&gt;&#x2F;home&#x2F;koen&#x2F;Test&lt;&#x2F;code&gt; folder was actually a symlink to another location. Lets alter the script to take symlinks into account.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# In a symlinked folder
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;home&#x2F;koen&#x2F;Test
&lt;&#x2F;span&gt;&lt;span&gt;CWD&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;$(pwd)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$CWD
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Also in a symlinked folder
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;home&#x2F;koen&#x2F;Test
&lt;&#x2F;span&gt;&lt;span&gt;CWD&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;$(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;pwd &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-P&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$CWD
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And lets run it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ bash print_cwd
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;home&#x2F;koen&#x2F;Test
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;home&#x2F;koen&#x2F;Documents&#x2F;Test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now as you see, the &lt;code&gt;pwd -P&lt;&#x2F;code&gt; command outputs the physical location of the &lt;code&gt;Test&lt;&#x2F;code&gt; folder.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;learning-more&quot;&gt;Learning more&lt;&#x2F;h2&gt;
&lt;p&gt;The best way to learn more is to use Bash. A lot. Don&#x27;t forget that Google is your friend.&lt;&#x2F;p&gt;
&lt;p&gt;In case you learn well from books I would recommend these.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1593279523&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1593279523&amp;amp;linkId=9cf8e9ac8913afbf3dcc8c591ad0780a&quot;&gt;The Linux Command Line: A Complete Introduction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0596009658&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0596009658&amp;amp;linkId=80e3fbe123ef781b1b84db41e56eb17c&quot;&gt;Learning the bash Shell: Unix Shell Programming&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Swapping list items in Python</title>
		<published>2020-10-30T00:00:00+00:00</published>
		<updated>2020-10-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-swap-items-in-list/" type="text/html"/>
		<id>https://koenwoortman.com/python-swap-items-in-list/</id>
		<content type="html">&lt;p&gt;Suppose you have a list in Python, and you want to swap the positions of items in the list.&lt;&#x2F;p&gt;
&lt;p&gt;Lets take the following list for example. In which the items are clearly out of order.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;meals &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;breakfast&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dinner&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;lunch&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to the swap the positions of items in a list we need to &lt;a href=&quot;&#x2F;python-get-index-of-item-in-list&#x2F;&quot;&gt;find the indexes&lt;&#x2F;a&gt; of the items we&#x27;d like to swap. We can easily do so in this case by using the &lt;code&gt;index()&lt;&#x2F;code&gt; function available for lists.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;i, j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;meals.index(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dinner&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), meals.index(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;lunch&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In this case we want to swap &lt;code&gt;&#x27;dinner&#x27;&lt;&#x2F;code&gt; at index 1 with &lt;code&gt;&#x27;lunch&#x27;&lt;&#x2F;code&gt; at index 2. These indexes are now stored in the variables &lt;code&gt;i&lt;&#x2F;code&gt; and &lt;code&gt;j&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In Python a sweet one-liner will suffice to swap the items in the list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;meals[i], meals[j] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;meals[j], meals[i]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This puts the list &lt;code&gt;meals&lt;&#x2F;code&gt; in the proper order.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;meals &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;breakfast&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dinner&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;lunch&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;i, j &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;meals.index(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dinner&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;), meals.index(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;lunch&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;meals[i], meals[j] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;meals[j], meals[i]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(meals)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;breakfast&amp;#39;, &amp;#39;lunch&amp;#39;, &amp;#39;dinner&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Be aware that this works for lists, and not tuples. Since tuples are not mutable and lists are.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Go to the end of the file in vim</title>
		<published>2020-10-29T00:00:00+00:00</published>
		<updated>2020-10-29T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/vim-go-to-end-of-file/" type="text/html"/>
		<id>https://koenwoortman.com/vim-go-to-end-of-file/</id>
		<content type="html">&lt;p&gt;Let&#x27;s jump!&lt;&#x2F;p&gt;
&lt;p&gt;To the end of a file.&lt;&#x2F;p&gt;
&lt;p&gt;In Vim!&lt;&#x2F;p&gt;
&lt;p&gt;Before you can jump to the end if a file in Vim you need to make sure you are in NORMAL mode first. So if you&#x27;re not yet, quit out of your current mode first. Most likely you need to quit our of INSERT mode first by pressing &lt;code&gt;Esc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you did so you can jump all the way to the end of the file with:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Shift&lt;&#x2F;code&gt;+&lt;code&gt;G&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you rather just move to the bottom of the screen, your friend is:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;Shift&lt;&#x2F;code&gt;+&lt;code&gt;L&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to write a good user story</title>
		<published>2020-10-28T00:00:00+00:00</published>
		<updated>2020-10-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/agile-writing-user-stories/" type="text/html"/>
		<id>https://koenwoortman.com/agile-writing-user-stories/</id>
		<content type="html">&lt;p&gt;If you&#x27;re in software development these days you probably come across user stories at some point. In case you do, what makes a good one?&lt;&#x2F;p&gt;
&lt;p&gt;But first off, some background on what a user stories actually are and why people use them.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;The Story Of User Stories&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The story of user stories is basically a story about miscommunications on software requirements. When you&#x27;re building a software system that fills the needs of others, you base requirements of that system on those needs. To do so you need information. If that information is encapsulated in tech speak requirements start to look like; &amp;quot;Just call endpoint X and parse to Y&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;That&#x27;s not what you want, &lt;u&gt;&lt;strong&gt;if&lt;&#x2F;strong&gt;&lt;&#x2F;u&gt; someone not understanding your tech speak is reading along. Going too far to the other direction is just as worse and causing developers to not understand what they are building.&lt;&#x2F;p&gt;
&lt;p&gt;So became the user story. Which describes the value of a functionality for the system from the perspective of someone who interacts with the system. This someone is typically categorized, like for example &lt;em&gt;customer, gymnast and developer.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;An example of a user story would be: &lt;em&gt;as a customer I can pay with my credit card.&lt;&#x2F;em&gt;&lt;&#x2F;p&gt;
&lt;p&gt;So what do you gain with this? Not much per se. There are alternatives available that are just as good. The value comes from a common structure. If done properly you can rely on the fact that each user story has a similar amount of information in a known format.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;so-what-makes-a-good-user-story&quot;&gt;So what makes a good user story?&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we have a grasp of what a user story is. What makes a good one? So generally you want user stories to be:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Written from the perspective of one user.&lt;&#x2F;li&gt;
&lt;li&gt;Describing a function (not a solution).&lt;&#x2F;li&gt;
&lt;li&gt;Scoped to one function.&lt;&#x2F;li&gt;
&lt;li&gt;Describing a purpose of the function.&lt;&#x2F;li&gt;
&lt;li&gt;Not overlapping with other stories.&lt;&#x2F;li&gt;
&lt;li&gt;Contained, necessary information for developers and non-developers is present in the user story.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;learn-more&quot;&gt;Learn more&lt;&#x2F;h2&gt;
&lt;p&gt;If you can&#x27;t get enough of user stories... there are some books I can recommend:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0321205685&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0321205685&amp;amp;linkId=7442016ddba67d16efcc3f18b844b7bd&quot;&gt;User Stories Applied&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1491904909&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1491904909&amp;amp;linkId=e9978e2bcd50622548611a5df27d7f48&quot;&gt;User Story Mapping&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0735605351&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0735605351&amp;amp;linkId=9bb12175964bb322d3c0aec9628aa081&quot;&gt;Software Estimation: Demystifying the Black Art&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Get the Python version number from code</title>
		<published>2020-10-27T00:00:00+00:00</published>
		<updated>2020-10-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-get-version-number-in-code/" type="text/html"/>
		<id>https://koenwoortman.com/python-get-version-number-in-code/</id>
		<content type="html">&lt;p&gt;Outputting the Python version on the shell is straightforward, and you do it just like how you get the version of other programs.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python --version
&lt;&#x2F;span&gt;&lt;span&gt;Python 3.8.5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Getting this version number in a script can be useful as well. But you have multiple options there.&lt;&#x2F;p&gt;
&lt;p&gt;One coming close to the one we saw above is by using the &lt;code&gt;python_version()&lt;&#x2F;code&gt; function from the &lt;code&gt;platform&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;platform &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;python_version
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(python_version())
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 3.8.5
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;More detailed ones are found in the &lt;code&gt;sys&lt;&#x2F;code&gt; module.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;sys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;version
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(version)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 3.8.5 (default, Jul 28 2020, 12:59:40)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [GCC 9.3.0]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This shows a more detailed string containing version info. Together with the compiler that was used.&lt;&#x2F;p&gt;
&lt;p&gt;If you programmatically want to do something with the version number you can better use something other than &lt;code&gt;sys.version()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;That something else might be &lt;code&gt;sys.version_info&lt;&#x2F;code&gt;. It returns always five components of the version number. Nicely in a tuple.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;sys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;version_info
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(version_info)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; sys.version_info(major=3, minor=8, micro=5, releaselevel=&amp;#39;final&amp;#39;, serial=0)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;checking-for-python-version-2-or-3&quot;&gt;Checking for python version 2 or 3&lt;&#x2F;h3&gt;
&lt;p&gt;If checking for Python version 2 is still a thing, you can easily do it with the &lt;code&gt;version_info&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;sys &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;version_info
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;version_info.major &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Running Python 3&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;elif &lt;&#x2F;span&gt;&lt;span&gt;version_info.major &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Running Python 2&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Returning JSON responses with Flask</title>
		<published>2020-10-26T00:00:00+00:00</published>
		<updated>2020-10-26T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-flask-return-json-response/" type="text/html"/>
		<id>https://koenwoortman.com/python-flask-return-json-response/</id>
		<content type="html">&lt;p&gt;My most common use case for Flask is making APIs. And I like my APIs to respond with JSON. Flask comes with a &lt;code&gt;jsonify()&lt;&#x2F;code&gt; function that returns JSON as a Flask Response object.&lt;&#x2F;p&gt;
&lt;p&gt;The reason to use &lt;code&gt;jsonify()&lt;&#x2F;code&gt; over a regular &lt;code&gt;json.dumps()&lt;&#x2F;code&gt; is that &lt;code&gt;jsonify()&lt;&#x2F;code&gt; sets the &lt;code&gt;Content-Type&lt;&#x2F;code&gt; HTTP header to &lt;code&gt;application&#x2F;json&lt;&#x2F;code&gt;. Whereas the output of &lt;code&gt;json.dumps()&lt;&#x2F;code&gt; is just plain text.&lt;&#x2F;p&gt;
&lt;p&gt;An example of responding with JSON is shown below. You can pass a dict or a list to the &lt;code&gt;jsonify()&lt;&#x2F;code&gt; function. This is then transformed to a JSON string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;flask &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Flask, jsonify
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;app &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Flask(__name__)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;chopstick&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    chopstick &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;color&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;bamboo&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;left_handed&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;jsonify(chopstick)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    app.run()
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above example returns the following JSON object if we call the &lt;code&gt;&#x2F;chopstick&#x2F;&lt;&#x2F;code&gt; endpoint.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ curl localhost:5000&#x2F;chopstick&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;color&amp;quot;: &amp;quot;bamboo&amp;quot;,
&lt;&#x2F;span&gt;&lt;span&gt;  &amp;quot;left_handed&amp;quot;: true
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Alternatively you can use keyword arguments as well with &lt;code&gt;jsonify()&lt;&#x2F;code&gt;. So the following produces the same end result.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;chopstick&lt;&#x2F;span&gt;&lt;span&gt;():
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;jsonify(
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;color&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;bamboo&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;left_handed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;adding-status-codes-to-a-json-response&quot;&gt;Adding status codes to a JSON response&lt;&#x2F;h2&gt;
&lt;p&gt;This &lt;code&gt;jsonify()&lt;&#x2F;code&gt; is nice and all, but it doesn&#x27;t give us the opportunity the set a status code for the response we are returning.&lt;&#x2F;p&gt;
&lt;p&gt;Lets say for example that we want the &lt;code&gt;&#x2F;chopstick&#x2F;&lt;&#x2F;code&gt; endpoint to work with a chopstick id. In case the id isn&#x27;t found in the URI we want to return a 400 status for a bad request.&lt;&#x2F;p&gt;
&lt;p&gt;So to illustrate, let say the chopstick function is responsible for handling both the &lt;code&gt;&#x2F;chopstick&#x2F;&lt;&#x2F;code&gt; and &lt;code&gt;&#x2F;chopstick&#x2F;&amp;lt;id&amp;gt;&lt;&#x2F;code&gt; routes. If the &lt;code&gt;chopstick_id&lt;&#x2F;code&gt; is &lt;code&gt;None&lt;&#x2F;code&gt;, so not present in the URI, we return a 400 status with a appropriate error message.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;lt;int:chopstick_id&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;chopstick&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;chopstick_id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;chopstick_id:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# status 400 - bad request
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;jsonify(
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;chopstick_id,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;color&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;bamboo&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;left_handed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To do so we can use the &lt;code&gt;make_response()&lt;&#x2F;code&gt; function in Flask. We still make use of &lt;code&gt;jsonify()&lt;&#x2F;code&gt; in combination with this &lt;code&gt;make_response()&lt;&#x2F;code&gt; so that the error message is a correct JSON string.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;flask &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;Flask, jsonify, make_response
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;app &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;Flask(__name__)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopstick&#x2F;&amp;lt;int:chopstick_id&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;chopstick&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;chopstick_id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;chopstick_id:
&lt;&#x2F;span&gt;&lt;span&gt;        message &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;jsonify(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;message&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;quot;chopstick_id&amp;quot; missing&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;make_response(message, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;400&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;jsonify(
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;chopstick_id,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;color&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;bamboo&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;left_handed&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    )
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;__name__ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;__main__&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    app.run()
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we now curl the endpoint without an ID we see the error message with the 400 status code as response.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ curl&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -i&lt;&#x2F;span&gt;&lt;span&gt; localhost:5000&#x2F;chopstick&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;HTTP&#x2F;1.0 400 BAD REQUEST
&lt;&#x2F;span&gt;&lt;span&gt;Content-Type: application&#x2F;json
&lt;&#x2F;span&gt;&lt;span&gt;Content-Length: 44
&lt;&#x2F;span&gt;&lt;span&gt;Server: Werkzeug&#x2F;1.0.1 Python&#x2F;3.8.5
&lt;&#x2F;span&gt;&lt;span&gt;Date: Sun, 25 Oct 2020 09:04:22 GMT
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;message&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;chopstick_id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt; missing&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And ofcourse, the chopstick itself is returned with status 200 if we do specify an ID in the URI.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ curl&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -i&lt;&#x2F;span&gt;&lt;span&gt; localhost:5000&#x2F;chopstick&#x2F;5
&lt;&#x2F;span&gt;&lt;span&gt;HTTP&#x2F;1.0 200 OK
&lt;&#x2F;span&gt;&lt;span&gt;Content-Type: application&#x2F;json
&lt;&#x2F;span&gt;&lt;span&gt;Content-Length: 60
&lt;&#x2F;span&gt;&lt;span&gt;Server: Werkzeug&#x2F;1.0.1 Python&#x2F;3.8.5
&lt;&#x2F;span&gt;&lt;span&gt;Date: Sun, 25 Oct 2020 09:05:57 GMT
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;color&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;bamboo&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;id&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: 5,
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;left_handed&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;: true
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Flask Version&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ flask&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --version
&lt;&#x2F;span&gt;&lt;span&gt;Python 3.8.5
&lt;&#x2F;span&gt;&lt;span&gt;Flask 1.1.2
&lt;&#x2F;span&gt;&lt;span&gt;Werkzeug 1.0.1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a list is empty in Python</title>
		<published>2020-10-25T00:00:00+00:00</published>
		<updated>2020-10-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-check-if-list-is-empty/" type="text/html"/>
		<id>https://koenwoortman.com/python-check-if-list-is-empty/</id>
		<content type="html">&lt;p&gt;Testing whether a list is empty is quite straightforward in Python. In the context of a &lt;code&gt;if&lt;&#x2F;code&gt; or &lt;code&gt;while&lt;&#x2F;code&gt; statement an empty list will be evaluated as &lt;code&gt;False&lt;&#x2F;code&gt;. Like how empty strings or empty dicts are evaluated as &lt;code&gt;False&lt;&#x2F;code&gt; as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;glass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if not &lt;&#x2F;span&gt;&lt;span&gt;glass:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Glass is empty&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Glass is empty
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;a href=&quot;https:&#x2F;&#x2F;www.python.org&#x2F;dev&#x2F;peps&#x2F;pep-0008&#x2F;&quot;&gt;PEP8 style guide&lt;&#x2F;a&gt; prefers the option as shown above. But in case you rather do a more explicit check the following will do. By comparing with &lt;code&gt;[]&lt;&#x2F;code&gt; we know for sure that &lt;code&gt;glass&lt;&#x2F;code&gt; is a list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;glass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;glass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span&gt;[]:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Glass is empty&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Glass is empty
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option is using the &lt;code&gt;len()&lt;&#x2F;code&gt; function in case you rather be somewhat explicit. This way we validate that &lt;code&gt;glass&lt;&#x2F;code&gt; is at least a sequence(str, list, tuple, ...).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;glass &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;len&lt;&#x2F;span&gt;&lt;span&gt;(glass) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Glass is empty&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; Glass is empty
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Print the name of the running Bash script</title>
		<published>2020-10-24T00:00:00+00:00</published>
		<updated>2020-10-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-print-running-script-name/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-print-running-script-name/</id>
		<content type="html">&lt;p&gt;When writing a Bash script you might be wanting to get the file name of the script. For outputting an error message this is useful for example. As is shown below.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ ls --pizza
&lt;&#x2F;span&gt;&lt;span&gt;ls: unrecognized option &amp;#39;--pizza&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Try &amp;#39;ls --help&amp;#39; for more information.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The full name of the script is stored in &lt;code&gt;$0&lt;&#x2F;code&gt;. So the following script would work.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;script_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;$0
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$script_name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;bin&#x2F;h4ckerman
&lt;&#x2F;span&gt;&lt;span&gt;.&#x2F;bin&#x2F;h4ckerman
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;However it gives you the full path as shown above. For me, I am mostly interested in just the file name of the script. This is where &lt;code&gt;basename&lt;&#x2F;code&gt; comes in. &lt;code&gt;basename&lt;&#x2F;code&gt; is a shell program available, and it gives you the base name... duh.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;script_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;$(basename $0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$script_name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;bin&#x2F;h4ckerman
&lt;&#x2F;span&gt;&lt;span&gt;h4ckerman
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;That&#x27;s more like it.&lt;&#x2F;p&gt;
&lt;p&gt;Another option that produces the same result as above is by using &lt;code&gt;${0##*&#x2F;}&lt;&#x2F;code&gt;. &lt;code&gt;${0##*&#x2F;}&lt;&#x2F;code&gt; makes use of parameter expansion in Bash. With &lt;code&gt;${0##*&#x2F;}&lt;&#x2F;code&gt; you drop the longest substring (match) till the last &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; for parameter &lt;code&gt;$0&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;script_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;${0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;##*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&#x2F;}
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$script_name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ .&#x2F;bin&#x2F;h4ckerman
&lt;&#x2F;span&gt;&lt;span&gt;h4ckerman
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Another option would be to use the &lt;code&gt;$BASH_SOURCE&lt;&#x2F;code&gt; variable instead of &lt;code&gt;$0&lt;&#x2F;code&gt;. However, as the name implies, this is functional in Bash and not POSIX compliant. So it wouldn&#x27;t function per se in a shell that isn&#x27;t Bash.&lt;&#x2F;p&gt;
&lt;p&gt;But as long you know for sure the script is only to run in Bash, you might as well use it.&lt;&#x2F;p&gt;
&lt;p&gt;Also be aware that &lt;code&gt;$0&lt;&#x2F;code&gt; stores the name of how the script was called. Which isn&#x27;t the same as the path of the actual script file, in case the script was symlinked.&lt;&#x2F;p&gt;
&lt;p&gt;In case that matters for your use case you can use the following:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span&gt;$(basename $(readlink&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -nf &lt;&#x2F;span&gt;&lt;span&gt;$0))
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;learning-more&quot;&gt;Learning more&lt;&#x2F;h2&gt;
&lt;p&gt;The best way to learn more is to use Bash. A lot. Don&#x27;t forget that Google is your friend.&lt;&#x2F;p&gt;
&lt;p&gt;In case you learn well from books I would recommend these.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1593279523&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1593279523&amp;amp;linkId=9cf8e9ac8913afbf3dcc8c591ad0780a&quot;&gt;The Linux Command Line: A Complete Introduction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0596009658&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0596009658&amp;amp;linkId=80e3fbe123ef781b1b84db41e56eb17c&quot;&gt;Learning the bash Shell: Unix Shell Programming&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Replication using a Django database router</title>
		<published>2020-10-23T00:00:00+00:00</published>
		<updated>2020-10-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-replication-database-router/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-replication-database-router/</id>
		<content type="html">&lt;p&gt;In Django it is perfectly fine to interact with multiple databases in your project. Django uses the concepts database routers to determine which database should be used for which model.&lt;&#x2F;p&gt;
&lt;p&gt;These database routers can be leveraged to in a master-replica database setup.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;Be aware that this setup doesn&#x27;t take replication lag into account.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;To get starter lets first add multiple databases to the settings. A master database set to &lt;code&gt;default&lt;&#x2F;code&gt; and the replica using the &lt;code&gt;replica&lt;&#x2F;code&gt; key.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;DATABASES &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;default&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;NAME&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ENGINE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.db.backends.mysql&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;replica&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;NAME&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;ENGINE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;django.db.backends.mysql&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;    },
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You could use &lt;code&gt;master&lt;&#x2F;code&gt; instead of &lt;code&gt;default&lt;&#x2F;code&gt; as well. But Django requires that a database entry be defined with the &lt;code&gt;default&lt;&#x2F;code&gt; key is defined. If you prefer to do so you can set &lt;code&gt;default&lt;&#x2F;code&gt; to an emply dict.&lt;&#x2F;p&gt;
&lt;p&gt;Now we can move on to the database router itself. The database router should be a class that implements the methods: &lt;code&gt;db_for_read&lt;&#x2F;code&gt;, &lt;code&gt;db_for_write&lt;&#x2F;code&gt;, &lt;code&gt;allow_relation&lt;&#x2F;code&gt;, &lt;code&gt;allow_migrate&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;A database router that supports replication might look as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;random
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;from &lt;&#x2F;span&gt;&lt;span&gt;django.conf &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;import &lt;&#x2F;span&gt;&lt;span&gt;settings
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;ReplicationRouter&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;db_for_read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;model&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        Randomly pick a database to read from
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;random.choice([key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;settings.DATABASES])
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;db_for_write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;model&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        Always send write queries to the master database.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;default&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;allow_relation&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;obj1&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;obj2&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        This isn&amp;#39;t really applicable for this use-case.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;allow_migrate&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;db&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;app_label&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;model_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        Only allow migration operations on the master database, just in case.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;        &amp;quot;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;db &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;default&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;:
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;allow_relation&lt;&#x2F;code&gt; and &lt;code&gt;allow_migrate&lt;&#x2F;code&gt; aren&#x27;t really applicable for replication. Those have a use-case for when you use different databases for different Django apps.&lt;&#x2F;p&gt;
&lt;p&gt;Important for our use-case of replication are &lt;code&gt;db_for_read&lt;&#x2F;code&gt; and &lt;code&gt;db_for_write&lt;&#x2F;code&gt;. The &lt;code&gt;db_for_read&lt;&#x2F;code&gt; method should return the database that should be used for a read operation. While &lt;code&gt;db_for_write&lt;&#x2F;code&gt; specifies the database that should be used for a write operation.&lt;&#x2F;p&gt;
&lt;p&gt;First off the database for write operations. In case Django needs to execute a write operation we want this query to end up at the master database. Because we should never write to the replica!&lt;&#x2F;p&gt;
&lt;p&gt;We simply specify the key in the &lt;code&gt;DATABASES&lt;&#x2F;code&gt; dict of the master database. This way only the master database will receive write queries.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;db_for_write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;model&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;default&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Second is specifying the database read operations can be propagated to. For this we randomly select a database that is set in &lt;code&gt;DATABASES&lt;&#x2F;code&gt;. This way read queries should be evenly distributed between the master and the replica.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;db_for_read&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;self&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;model&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;**&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;hints&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;random.choice([key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;key &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;settings.DATABASES])
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This does assume that all databases are used for replication though.&lt;&#x2F;p&gt;
&lt;p&gt;The last thing we need to do is specifying the database router in the settings. You can add a database router by setting &lt;code&gt;DATABASE_ROUTERS&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;DATABASE_ROUTERS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;path.to.ReplicationRouter&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Run the Django server on a different port</title>
		<published>2020-10-22T00:00:00+00:00</published>
		<updated>2020-10-22T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-runserver-different-port/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-runserver-different-port/</id>
		<content type="html">&lt;p&gt;If you start your Django development server it runs on port 8000 by default.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ python manage.py runserver
&lt;&#x2F;span&gt;&lt;span&gt;Watching for file changes with StatReloader
&lt;&#x2F;span&gt;&lt;span&gt;Performing system checks...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;System check identified no issues (0 silenced)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;October 19, 2020 - 12:19:16
&lt;&#x2F;span&gt;&lt;span&gt;Django version 3.1.1, using settings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api.settings&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Starting development server at http:&#x2F;&#x2F;127.0.0.1:8000&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;Quit the server with CONTROL-C.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To start the development server on a different port number you pass it as an argument after &lt;code&gt;runserver&lt;&#x2F;code&gt;. To run the development server on port 8001 that would make the command &lt;code&gt;python manage.py runserver 8001&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ python manage.py runserver 8001
&lt;&#x2F;span&gt;&lt;span&gt;Watching for file changes with StatReloader
&lt;&#x2F;span&gt;&lt;span&gt;Performing system checks...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;System check identified no issues (0 silenced)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;October 19, 2020 - 12:20:38
&lt;&#x2F;span&gt;&lt;span&gt;Django version 3.1.1, using settings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api.settings&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Starting development server at http:&#x2F;&#x2F;127.0.0.1:8001&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;Quit the server with CONTROL-C.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can even run multiple development servers simultaneously this way if you&#x27;d like to do so.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;run-on-port-80&quot;&gt;Run on port 80&lt;&#x2F;h2&gt;
&lt;p&gt;Running on port 80 can be done with some special treatment. A disclaimer first, there are probably better ways to make you site available via port 80, with a reversed proxy for example.&lt;&#x2F;p&gt;
&lt;p&gt;You most likely have to use &lt;code&gt;sudo&lt;&#x2F;code&gt; in order to run the server via port 80. This is where other problems may arise. If you run the command using &lt;code&gt;sudo&lt;&#x2F;code&gt; the virtualenv isn&#x27;t taken into account. So just running &lt;code&gt;sudo python manage.py runserver 80&lt;&#x2F;code&gt; won&#x27;t work if you&#x27;re using virtualenv. Since the &lt;code&gt;python&lt;&#x2F;code&gt; command references a different binary. One way to work around that is by using the full path to the python in your virtualenv.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ sudo &#x2F;home&#x2F;..&#x2F;venv&#x2F;bin&#x2F;python manage.py runserver 80
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This might have done the trick, but there is a chance you got stuck at a next problem. If you have NGINX or Apache installed on your system than port 80 is probably in use already. Giving you the following error message.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;Error: That port is already in use
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If that&#x27;s the case you need to stop those services first. In case you&#x27;re running Apache you can most likely stop it using the following command, but it may be different depending on your operating system.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo service apache2 stop
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For NGINX the command would be.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo service nginx stop
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now you should be all set to run the Django development server on port 80. But again, look into reversed proxies.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo &#x2F;home&#x2F;..&#x2F;venv&#x2F;bin&#x2F;python manage.py runserver 80
&lt;&#x2F;span&gt;&lt;span&gt;Watching for file changes with StatReloader
&lt;&#x2F;span&gt;&lt;span&gt;Performing system checks...
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;System check identified no issues (0 silenced)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;October 19, 2020 - 12:42:55
&lt;&#x2F;span&gt;&lt;span&gt;Django version 3.1.1, using settings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;api.settings&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Starting development server at http:&#x2F;&#x2F;127.0.0.1:80&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;Quit the server with CONTROL-C.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;more-options&quot;&gt;More options&lt;&#x2F;h3&gt;
&lt;p&gt;For more options you can use the help option as well.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ python manage.py runserver --help
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Abort a git merge on conflicts</title>
		<published>2020-10-21T00:00:00+00:00</published>
		<updated>2020-10-21T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-abort-merge-on-conflict/" type="text/html"/>
		<id>https://koenwoortman.com/git-abort-merge-on-conflict/</id>
		<content type="html">&lt;p&gt;When you pull in changes from your remote there is a chance you run into merge conflicts. If a merge can&#x27;t be resolved using auto-merging your terminal can gets swamped with messages like:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;CONFLICT (content): Merge conflict in ...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ofcourse one option is to resolve the merge conflicts. However sometimes it is easier to just abort.&lt;&#x2F;p&gt;
&lt;p&gt;You can abort a git merge with conflicts with the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git merge&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --abort
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, to circumvent the merge conflicts you can use &lt;code&gt;fetch&lt;&#x2F;code&gt;. If going down the road of resolving merge conflicts isn&#x27;t an option.&lt;&#x2F;p&gt;
&lt;p&gt;Before you do this make sure that your changes are stashed.&lt;&#x2F;p&gt;
&lt;p&gt;You can fetch the latest changes from the remote, in this case called &lt;code&gt;origin&lt;&#x2F;code&gt;, with the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git fetch origin &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;branch&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To get these changes in your active branch you can do a hard reset which gives you the latest code from the remote you just fetched.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git reset&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --hard&lt;&#x2F;span&gt;&lt;span&gt; origin&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;branch&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Make multiple subdirectories at once with mkdir</title>
		<published>2020-10-20T00:00:00+00:00</published>
		<updated>2020-10-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-mkdir-multiple-subdirectories/" type="text/html"/>
		<id>https://koenwoortman.com/bash-mkdir-multiple-subdirectories/</id>
		<content type="html">&lt;p&gt;The linux shell command to make a new directory. If works pretty pretty straightforward: you want to make a directory, you give the name as an argument, and you get a directory. If you want to make some subdirectories as well however &lt;code&gt;mkdir&lt;&#x2F;code&gt; isn&#x27;t as friendly.&lt;&#x2F;p&gt;
&lt;p&gt;Lets say you want to make a directory called &lt;code&gt;sub&lt;&#x2F;code&gt;, which should have a directory in it called &lt;code&gt;way&lt;&#x2F;code&gt;, which in its turn should have a subdirectory called &lt;code&gt;sandwich&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;If you try it like this, mkdir starts to complain. It expects both the &lt;code&gt;sub&lt;&#x2F;code&gt; and &lt;code&gt;way&lt;&#x2F;code&gt; directories to exists and plans on just making the &lt;code&gt;sandwich&lt;&#x2F;code&gt; directory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ mkdir sub&#x2F;way&#x2F;sandwich
&lt;&#x2F;span&gt;&lt;span&gt;mkdir: cannot create directory ‘sub&#x2F;way&#x2F;sandwich’: No such file or directory
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The boring option is to make this command into a three-parter. Make the first, second and third directory separately.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ mkdir sub
&lt;&#x2F;span&gt;&lt;span&gt;$ mkdir sub&#x2F;way
&lt;&#x2F;span&gt;&lt;span&gt;$ mkdir sub&#x2F;way&#x2F;sandwich
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A faster way is to use the &lt;code&gt;-p&lt;&#x2F;code&gt; option with the first command. The &lt;code&gt;-p&lt;&#x2F;code&gt; option makes sure that parent directories are created as well if necessary.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -p&lt;&#x2F;span&gt;&lt;span&gt; sub&#x2F;way&#x2F;sandwich
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And there you go.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tree sub
&lt;&#x2F;span&gt;&lt;span&gt;sub
&lt;&#x2F;span&gt;&lt;span&gt;└── way
&lt;&#x2F;span&gt;&lt;span&gt;    └── sandwich
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;2 directories, 0 files
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;create-multiple-subdirectories-with-mkdir&quot;&gt;Create multiple subdirectories with mkdir&lt;&#x2F;h2&gt;
&lt;p&gt;By using braces(&lt;code&gt;{}&lt;&#x2F;code&gt;) you can create multiple subdirectories at once. This is actually called &amp;quot;Brace Expansion&amp;quot; and useful in other commands as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ mkdir&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -p&lt;&#x2F;span&gt;&lt;span&gt;  sub&#x2F;{way,system}&#x2F;sandwich
&lt;&#x2F;span&gt;&lt;span&gt;$ tree sub
&lt;&#x2F;span&gt;&lt;span&gt;sub
&lt;&#x2F;span&gt;&lt;span&gt;├── system
&lt;&#x2F;span&gt;&lt;span&gt;│   └── sandwich
&lt;&#x2F;span&gt;&lt;span&gt;└── way
&lt;&#x2F;span&gt;&lt;span&gt;    └── sandwich
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;4 directories, 0 files
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a new app in a Django project</title>
		<published>2020-10-19T00:00:00+00:00</published>
		<updated>2020-10-19T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-create-new-app/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-create-new-app/</id>
		<content type="html">&lt;p&gt;To create a new app in Django we utilize the &lt;code&gt;startapp&lt;&#x2F;code&gt; command. This creates a Django app directory with a specified app name.&lt;&#x2F;p&gt;
&lt;p&gt;The app is created in by going in the root folder of your Django project and run the following command. The root folder is where the manage.py file is by the way.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ python manage.py startapp plebapp
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This results in a new directory being created named &lt;code&gt;plebapp&lt;&#x2F;code&gt; with the file structure as shown below.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tree plebapp
&lt;&#x2F;span&gt;&lt;span&gt;plebapp
&lt;&#x2F;span&gt;&lt;span&gt;├── admin.py
&lt;&#x2F;span&gt;&lt;span&gt;├── apps.py
&lt;&#x2F;span&gt;&lt;span&gt;├── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;├── migrations
&lt;&#x2F;span&gt;&lt;span&gt;│   └── __init__.py
&lt;&#x2F;span&gt;&lt;span&gt;├── models.py
&lt;&#x2F;span&gt;&lt;span&gt;├── tests.py
&lt;&#x2F;span&gt;&lt;span&gt;└── views.py
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In order to use this app we are almost set. To actually include the app in our projects it has to be added to &lt;code&gt;INSTALLED_APPS&lt;&#x2F;code&gt; in the settings. You can reference the new app by its name.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;INSTALLED_APPS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;plebapp&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or by referencing the Config class you find in &lt;code&gt;.&#x2F;plebapp&#x2F;apps.py&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;INSTALLED_APPS &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;plebapp.apps.PlebappConfig&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remove None values from a list in Python</title>
		<published>2020-10-18T00:00:00+00:00</published>
		<updated>2020-10-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-remove-none-values-from-list/" type="text/html"/>
		<id>https://koenwoortman.com/python-remove-none-values-from-list/</id>
		<content type="html">&lt;p&gt;With Python you have multiple options to remove &lt;code&gt;None&lt;&#x2F;code&gt; values from a list. Those options include a basic for loop or by using list comprehension.&lt;&#x2F;p&gt;
&lt;p&gt;A more readable option, by personal opinion, is to use the &lt;code&gt;filter&lt;&#x2F;code&gt; function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;cocktails &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Mojito&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pina Colada&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Remove None values in list
&lt;&#x2F;span&gt;&lt;span&gt;drinks &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, cocktails))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(drinks)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;Mojito&amp;#39;, &amp;#39;Pina Colada&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;filter&lt;&#x2F;code&gt; function takes a function and iterable as parameters, giving it the following signature: &lt;code&gt;filter(function, iterable)&lt;&#x2F;code&gt;. And the &lt;code&gt;filter&lt;&#x2F;code&gt; function returns a filter object, so depending on your use-case you might want to cast it to a list again with &lt;code&gt;list()&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;All elements that in the iterable are passed to the function. If the function returns &lt;code&gt;True&lt;&#x2F;code&gt; for that specific element, that element is kept present. If it returns &lt;code&gt;False&lt;&#x2F;code&gt;, it is filtered out.&lt;&#x2F;p&gt;
&lt;p&gt;If you pass &lt;code&gt;None&lt;&#x2F;code&gt; as the function, the identity function is used instead. This causes all elements that are evaluated as &lt;code&gt;False&lt;&#x2F;code&gt; to be filtered out, so besides &lt;code&gt;None&lt;&#x2F;code&gt; empty strings, 0 and &lt;code&gt;False&lt;&#x2F;code&gt; are too. Which can cause unwanted behavior.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, true_and_false))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(true_and_false)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [True, True]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Thus, in the first example passing &lt;code&gt;None&lt;&#x2F;code&gt; as a filter function does suffice. But in our cases you might need to be more explicit when you &lt;a href=&quot;&#x2F;python-filter-list&#x2F;&quot;&gt;filter a list&lt;&#x2F;a&gt;, to do you can use list comprehension or write your own callback function.&lt;&#x2F;p&gt;
&lt;p&gt;So the following example uses a lambda function to filter out &lt;code&gt;None&lt;&#x2F;code&gt; values, but it keeps the &lt;code&gt;False&lt;&#x2F;code&gt;&#x27;s in there as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Filter out values with a lambda function
&lt;&#x2F;span&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;list&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;filter&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;lambda &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;x&lt;&#x2F;span&gt;&lt;span&gt;: x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;is not &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, true_and_false))
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(true_and_false)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [True, False, False, False, True]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And to include an example using list comprehension as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;False&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;True&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# Filter out values with list comprehension
&lt;&#x2F;span&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;for &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;true_and_false &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;x &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;is not &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(true_and_false)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [True, False, False, False, True]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a super user in Django</title>
		<published>2020-10-17T00:00:00+00:00</published>
		<updated>2020-10-17T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-django-create-super-user/" type="text/html"/>
		<id>https://koenwoortman.com/python-django-create-super-user/</id>
		<content type="html">&lt;p&gt;If you want to login to the Django admin site you need a user with &lt;code&gt;is_staff&lt;&#x2F;code&gt; or &lt;code&gt;is_superuser&lt;&#x2F;code&gt; set to true. You can create a user with the super user permissions by utilizing the &lt;code&gt;createsuperuser&lt;&#x2F;code&gt; command. This will prompt you for the required inputs for a user.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ python manage.py createsuperuser
&lt;&#x2F;span&gt;&lt;span&gt;Username (leave blank to use &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;koen&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span&gt; admin
&lt;&#x2F;span&gt;&lt;span&gt;Email address: admin@koenwoortman.com
&lt;&#x2F;span&gt;&lt;span&gt;Password: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*********
&lt;&#x2F;span&gt;&lt;span&gt;Password (again)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;*********
&lt;&#x2F;span&gt;&lt;span&gt;Superuser created successfully
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using this user account you should able to login to the Django admin site. It also acts like a general user account in a normal Django project. So you&#x27;re also able to use it as a login on your self-made website.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Append lines to the end of a file with Bash</title>
		<published>2020-10-16T00:00:00+00:00</published>
		<updated>2020-10-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-append-to-end-of-file/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-append-to-end-of-file/</id>
		<content type="html">&lt;p&gt;At some point it is gonna be useful to write to a file with Bash. You can do so in a Bash script or directly via the command-line. To do so you use the append operator(&lt;code&gt;&amp;gt;&amp;gt;&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. Set the file to write to
&lt;&#x2F;span&gt;&lt;span&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;log.txt&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Append text with &amp;#39;&amp;gt;&amp;gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;[INFO] caught a bass&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$file
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;&amp;gt;&amp;gt;&lt;&#x2F;code&gt; appends to a file or creates the file if it doesn&#x27;t exist. Don&#x27;t confuse &lt;code&gt;&amp;gt;&amp;gt;&lt;&#x2F;code&gt; with &lt;code&gt;&amp;gt;&lt;&#x2F;code&gt;, the latter completely overwrites a file erasing all current lines.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;append-command-output-to-a-file&quot;&gt;Append command output to a file&lt;&#x2F;h2&gt;
&lt;p&gt;So technically this is what we did with &lt;code&gt;echo&lt;&#x2F;code&gt; as well. Where the output of the &lt;code&gt;echo&lt;&#x2F;code&gt; command was appended to a file. You can use the output of other commands as well in combination with the append operator. Like the &lt;code&gt;ls&lt;&#x2F;code&gt; command for example.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. Set the file to write to
&lt;&#x2F;span&gt;&lt;span&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;files_and_directories.txt&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Append text with &amp;#39;&amp;gt;&amp;gt;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;ls &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$file
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;append-multiple-lines-at-once&quot;&gt;Append multiple lines at once&lt;&#x2F;h2&gt;
&lt;p&gt;Until so far we only appended single lines. But you might as well want to write multiple lines to a file at once. Tis a bit more cryptic however. But you can use &lt;code&gt;cat&lt;&#x2F;code&gt; for this.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. set the file to write to
&lt;&#x2F;span&gt;&lt;span&gt;file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;log.txt&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Append multiple lines with &amp;#39;&amp;gt;&amp;gt;&amp;#39; and &amp;#39;cat&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;cat &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&amp;lt; EOT &amp;gt;&amp;gt; &lt;&#x2F;span&gt;&lt;span&gt;$file
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;[INFO] caught a bass
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;[INFO] and released the bass
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;EOT
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;learning-more&quot;&gt;Learning more&lt;&#x2F;h2&gt;
&lt;p&gt;The best way to learn more is to use Bash. A lot. Don&#x27;t forget that Google is your friend.&lt;&#x2F;p&gt;
&lt;p&gt;In case you learn well from books I would recommend these.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1593279523&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1593279523&amp;amp;linkId=9cf8e9ac8913afbf3dcc8c591ad0780a&quot;&gt;The Linux Command Line: A Complete Introduction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0596009658&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0596009658&amp;amp;linkId=80e3fbe123ef781b1b84db41e56eb17c&quot;&gt;Learning the bash Shell: Unix Shell Programming&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Tmux sessions should be nested with care, unset $TMUX to force</title>
		<published>2020-10-15T00:00:00+00:00</published>
		<updated>2020-10-15T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-sessions-should-be-nested-with-care-unset-tmux-to-force/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-sessions-should-be-nested-with-care-unset-tmux-to-force/</id>
		<content type="html">&lt;p&gt;If you try to start a new tmux session from within a tmux session you get the error message &lt;code&gt;sessions should be nested with care, unset $TMUX to force&lt;&#x2F;code&gt;. So what does this mean?&lt;&#x2F;p&gt;
&lt;p&gt;It basically means that tmux doesn&#x27;t like it if you nest a tmux session in an already active tmux session. Doesn&#x27;t mean it is impossible to do so. You can nest a tmux session by unsetting $TMUX, helpful error message right. You do this as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ TMUX=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; tmux
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This gets you a new tmux session inside your currently active tmux window.&lt;&#x2F;p&gt;
&lt;p&gt;More likely the way to go is to &lt;a href=&quot;&#x2F;tmux-detach-sessions&#x2F;&quot;&gt;detach from your current session&lt;&#x2F;a&gt; first with &lt;code&gt;tmux detach&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;create-and-attach-to-tmux-session-in-shell-script&quot;&gt;Create and attach to tmux session in shell script&lt;&#x2F;h3&gt;
&lt;p&gt;If you&#x27;re writing a shell script that switches or attaches to a tmux session depending on whether you&#x27;re in tmux or not you can do that as follows.&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;First you check if a tmux session exists with a given name.&lt;&#x2F;li&gt;
&lt;li&gt;Create the session if it doesn&#x27;t exists.&lt;&#x2F;li&gt;
&lt;li&gt;Attach if outside of tmux, switch if you&#x27;re in tmux.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;session_name&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;sesh&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. First you check if a tmux session exists with a given name.
&lt;&#x2F;span&gt;&lt;span&gt;tmux has-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;$session_name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;2&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;dev&#x2F;null
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Create the session if it doesn&amp;#39;t exists.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span&gt;$? &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-ne&lt;&#x2F;span&gt;&lt;span&gt; 0 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  TMUX&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39; &lt;&#x2F;span&gt;&lt;span&gt;tmux new-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -d -s &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$session_name&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Attach if outside of tmux, switch if you&amp;#39;re in tmux.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-z &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$TMUX&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  tmux attach&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$session_name&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;  tmux switch-client&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$session_name&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A logical improvement to make based on this script is to get the session name from an argument to your script. So instead of hardcoding the tmux session name to &amp;quot;sesh&amp;quot; you would use the value in &lt;code&gt;$1&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Start tmux session with specified name</title>
		<published>2020-10-14T00:00:00+00:00</published>
		<updated>2020-10-14T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-start-session-with-name/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-start-session-with-name/</id>
		<content type="html">&lt;p&gt;When you start a new tmux session, by default tmux starts naming sessions with a number. So your sessions will be names 0, 1, 3,...&lt;&#x2F;p&gt;
&lt;p&gt;Often I&#x27;d like to name my accordingly, related to the project and&#x2F;or environment that I&#x27;m working on. The tmux session I have running now is called &lt;code&gt;blog&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
&lt;p&gt;You can control the name of the session by starting a new session as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux new&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s&lt;&#x2F;span&gt;&lt;span&gt; blog
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The tmux session that will be created is named &lt;code&gt;blog&lt;&#x2F;code&gt; in this case. The &lt;code&gt;-s&lt;&#x2F;code&gt; option is for session, I hope that is easy enough to remember :).&lt;&#x2F;p&gt;
&lt;p&gt;Renaming a tmux sessions is pretty straightforward as well. You can read &lt;a href=&quot;&#x2F;tmux-rename-session&#x2F;&quot;&gt;here&lt;&#x2F;a&gt; how to do so.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Start numbering tmux windows from index one</title>
		<published>2020-10-13T00:00:00+00:00</published>
		<updated>2020-10-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-start-numbering-windows-from-index-one/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-start-numbering-windows-from-index-one/</id>
		<content type="html">&lt;p&gt;With tmux the windows you create start counting from 0. I know this is kind of the default for programming related things. However in this case it doesn&#x27;t feel really intuitive to me. Mostly because I&#x27;m using these indices to move to a specific window.&lt;&#x2F;p&gt;
&lt;p&gt;You can let tmux know that you rather start numbering windows and panes from one instead of zero by putting the following in your &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Start numbering windows from index 1
&lt;&#x2F;span&gt;&lt;span&gt;set -g base-index 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Start numbering panes from index 1
&lt;&#x2F;span&gt;&lt;span&gt;setw -g pane-base-index 1
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Show current git branch in tmux status bar</title>
		<published>2020-10-12T00:00:00+00:00</published>
		<updated>2020-10-12T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-show-current-git-branch-in-status-bar/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-show-current-git-branch-in-status-bar/</id>
		<content type="html">&lt;p&gt;For a while I had my current git branch name showing in my ZSH prompt. Which you can pretty easily do if you use something like oh-my-zsh for example. But it became a bit too cluttering for me after some time. Both the width of the prompt and seeing the branch for each command bother me a bit.&lt;&#x2F;p&gt;
&lt;p&gt;Since I am practically always in a tmux session anyway I wanted to see if I could show the current git branch in the tmux status bar. The following was inspired by an &lt;a href=&quot;https:&#x2F;&#x2F;superuser.com&#x2F;a&#x2F;1085573&quot;&gt;answer on StackExchange&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;By adding the following lines to your &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file you get the current git branch in the right corner of my tmux status bar.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;set -g status-right &amp;quot;#(cd #{pane_current_path}; git rev-parse --abbrev-ref HEAD)&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;set -g status-right-length 200
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For one project we use very long branch names, generated by bitbucket based on a JIRA issue title... ugg. So I had to increase the length of &lt;code&gt;status-right&lt;&#x2F;code&gt; for the branch name not to be cut off somewhere in the middle. The number 200 is taken somewhat out of the air.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Switching tmux windows faster with alt+number</title>
		<published>2020-10-11T00:00:00+00:00</published>
		<updated>2020-10-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-faster-switching-to-windows/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-faster-switching-to-windows/</id>
		<content type="html">&lt;p&gt;I&#x27;m switching quite often. So the default tmux binding of &lt;code&gt;&amp;lt;prefix&amp;gt; &amp;lt;number&amp;gt;&lt;&#x2F;code&gt; starts to feel a bit expensive. I therefor decided that I wanted to switch tmux windows with &lt;code&gt;alt+&amp;lt;number&amp;gt;&lt;&#x2F;code&gt; instead.&lt;&#x2F;p&gt;
&lt;p&gt;This is pretty similar to how I switch workspaces in i3, my window manager (I should checkout sway once, heard it was nice). And the same as in Google Chrome actually.&lt;&#x2F;p&gt;
&lt;p&gt;You can add these alt+number keybindings by putting the following in your &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Switch windows alt+number
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-1 select-window -t 1
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-2 select-window -t 2
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-3 select-window -t 3
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-4 select-window -t 4
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-5 select-window -t 5
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-6 select-window -t 6
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-7 select-window -t 7
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-8 select-window -t 8
&lt;&#x2F;span&gt;&lt;span&gt;bind-key -n M-9 select-window -t 9
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Remapping the split window keys in tmux</title>
		<published>2020-10-10T00:00:00+00:00</published>
		<updated>2020-10-10T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-remap-split-window-keys/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-remap-split-window-keys/</id>
		<content type="html">&lt;p&gt;In tmux you can split your panes horizontally with &lt;code&gt;&amp;lt;prefix&amp;gt; &amp;quot;&lt;&#x2F;code&gt; and vertically with &lt;code&gt;&amp;lt;prefix&amp;gt; %&lt;&#x2F;code&gt; by default. I quite often forgot these keybindings so wanted to remap them to something more meaningful.&lt;&#x2F;p&gt;
&lt;p&gt;I decided on &lt;code&gt;&amp;lt;prefix&amp;gt; -&lt;&#x2F;code&gt; to split vertically and on &lt;code&gt;&amp;lt;prefix&amp;gt; \&lt;&#x2F;code&gt; to split horizontally. I would prefer &lt;code&gt;|&lt;&#x2F;code&gt; over &lt;code&gt;\&lt;&#x2F;code&gt;, but it requires me to press an extra key.&lt;&#x2F;p&gt;
&lt;p&gt;This seems counterintuitive, because well, a hyphen(&lt;code&gt;-&lt;&#x2F;code&gt;) is horizontal and a backslash(&lt;code&gt;\&lt;&#x2F;code&gt;) is vertical. But if you see how tmux splits windows it makes more sense.&lt;&#x2F;p&gt;
&lt;p&gt;A tmux window that is split horizontally looks as follows. It is split from left to right, with a pane border in the middle. Hence the backslash.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;+–––––––––+–––––––––+
&lt;&#x2F;span&gt;&lt;span&gt;|         |         |
&lt;&#x2F;span&gt;&lt;span&gt;|         |         |
&lt;&#x2F;span&gt;&lt;span&gt;|         |         |
&lt;&#x2F;span&gt;&lt;span&gt;|         |         |
&lt;&#x2F;span&gt;&lt;span&gt;|         |         |
&lt;&#x2F;span&gt;&lt;span&gt;+–––––––––+–––––––––+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can add the bindings for &lt;code&gt;&amp;lt;prefix&amp;gt; -&lt;&#x2F;code&gt; and &lt;code&gt;&amp;lt;prefix&amp;gt; \&lt;&#x2F;code&gt; by putting the following in your &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file. I unbind the default bindings as well, so I can reuse them for something else if I need to.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Split panes with \ and -
&lt;&#x2F;span&gt;&lt;span&gt;bind \\ split-window -h -c &amp;quot;#{pane_current_path}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;bind - split-window -v -c &amp;quot;#{pane_current_path}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;unbind &amp;#39;&amp;quot;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;unbind %
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Take notice that you need to escape the backslash, that&#x27;s where the &lt;code&gt;bind \\&lt;&#x2F;code&gt; comes from.&lt;&#x2F;p&gt;
&lt;p&gt;In most cases I like the new split to be in the directory as I am currently in. So I added the option &lt;code&gt;-c &amp;quot;#{pane_current_path}&amp;quot;&lt;&#x2F;code&gt; to do so. If you don&#x27;t prefer this you&#x27;re set with the following configuration.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Split panes with \ and -
&lt;&#x2F;span&gt;&lt;span&gt;bind \\ split-window -h
&lt;&#x2F;span&gt;&lt;span&gt;bind - split-window -v
&lt;&#x2F;span&gt;&lt;span&gt;unbind &amp;#39;&amp;quot;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;unbind %
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;learn-more&quot;&gt;Learn more&lt;&#x2F;h2&gt;
&lt;p&gt;A great resource for learning more about tmux are its man pages. You can read them online &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;1&#x2F;tmux&quot;&gt;here&lt;&#x2F;a&gt; or from the command-line using the following command:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ man tmux
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or take a look at this book:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1680502212&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1680502212&amp;amp;linkId=a9211b23b9fa14f7e58af4583a4d5670&quot;&gt;Tmux 2: Productive Mouse-Free Development&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Adding parameters to routes in Flask</title>
		<published>2020-10-09T00:00:00+00:00</published>
		<updated>2020-10-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-flask-routes-with-parameters/" type="text/html"/>
		<id>https://koenwoortman.com/python-flask-routes-with-parameters/</id>
		<content type="html">&lt;p&gt;Parameters in Flask routes are surrounded by &lt;code&gt;&amp;lt;&lt;&#x2F;code&gt; and &lt;code&gt;&amp;gt;&lt;&#x2F;code&gt;. The name you use for the parameter it is passed to the routing function as keyword argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;lt;resource&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;list_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;resource&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(resource)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;converter-types&quot;&gt;Converter types&lt;&#x2F;h2&gt;
&lt;p&gt;You can constrain parameters using converter types. The options for converter types are &lt;code&gt;string&lt;&#x2F;code&gt;, &lt;code&gt;int&lt;&#x2F;code&gt;, &lt;code&gt;float&lt;&#x2F;code&gt;, &lt;code&gt;path&lt;&#x2F;code&gt; and &lt;code&gt;uuid&lt;&#x2F;code&gt;. You include them as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;lt;string:resource&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;list_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;resource&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(resource)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Flask Version&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ flask&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --version
&lt;&#x2F;span&gt;&lt;span&gt;Python 3.8.5
&lt;&#x2F;span&gt;&lt;span&gt;Flask 1.1.2
&lt;&#x2F;span&gt;&lt;span&gt;Werkzeug 1.0.1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Multiple Flask routes to same function</title>
		<published>2020-10-08T00:00:00+00:00</published>
		<updated>2020-10-08T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-flask-multiple-routes-for-one-function/" type="text/html"/>
		<id>https://koenwoortman.com/python-flask-multiple-routes-for-one-function/</id>
		<content type="html">&lt;p&gt;In some cases you can reuse a Flask route function for multiple URLs. Or you want the same page&#x2F;response available via multiple URLs. In that case you can add a second route to the function by stacking a second route decorator to the function.&lt;&#x2F;p&gt;
&lt;p&gt;The code below shows how you use the a function for multiple URLs. In case you&#x27;re using &lt;a href=&quot;&#x2F;python-flask-routes-with-parameters&#x2F;&quot;&gt;parameters in one of your routes&lt;&#x2F;a&gt; make sure to provide a default value of the function argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopsticks&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;@app.route(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;chopsticks&#x2F;&amp;lt;int:chopstick_id&amp;gt;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;chopsticks_view&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;chopstick_id&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;None&lt;&#x2F;span&gt;&lt;span&gt;):
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;chopstick_id:
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Chopstick &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;{}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;.format(chopstick_id)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Chopstick list&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Flask Version&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ flask&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --version
&lt;&#x2F;span&gt;&lt;span&gt;Python 3.8.5
&lt;&#x2F;span&gt;&lt;span&gt;Flask 1.1.2
&lt;&#x2F;span&gt;&lt;span&gt;Werkzeug 1.0.1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Checkout git branches faster with fzf</title>
		<published>2020-10-07T00:00:00+00:00</published>
		<updated>2020-10-07T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-faster-branch-checkouts-with-fzf/" type="text/html"/>
		<id>https://koenwoortman.com/git-faster-branch-checkouts-with-fzf/</id>
		<content type="html">&lt;p&gt;In large git repo&#x27;s I find myself sometimes lost in switching branches. For this project I work on using Bitbucket we use branch names starting with a JIRA issue number. This makes relying on Tab-completion harder. I ain&#x27;t got time to remember JIRA issue numbers.&lt;&#x2F;p&gt;
&lt;p&gt;This is where &lt;code&gt;fzf&lt;&#x2F;code&gt; comes in, a fuzzy finder for the command-line. You can find the GitHub repo &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;junegunn&#x2F;fzf&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;You can pipe lines to &lt;code&gt;fzf&lt;&#x2F;code&gt; and it gives you an interactive input prompt you can use to filter.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git branch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; juice
&lt;&#x2F;span&gt;&lt;span&gt;  29&#x2F;29
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt;   XY-8435-create-juice-model
&lt;&#x2F;span&gt;&lt;span&gt;    XY-5193-fix-juice-form-fields
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above example just outputs the branch name you select. In this case it filtered the branches that were listed using &lt;code&gt;git branch&lt;&#x2F;code&gt; on the keyword &lt;code&gt;juice&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In order for this to be somewhat useful you need to combine it with a &lt;code&gt;git checkout&lt;&#x2F;code&gt;. In order to do so you can use the following. Where the output of &lt;code&gt;$(git branch | fzf)&lt;&#x2F;code&gt; is used as argument to the &lt;code&gt;checkout&lt;&#x2F;code&gt; subcommand.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git checkout $(git branch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf)
&lt;&#x2F;span&gt;&lt;span&gt;Switched to branch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;master&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So this already does the job. As you see above you can switch branches like this. There is a problem with this however and is has to do with the outputs of &lt;code&gt;git branch&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git branch
&lt;&#x2F;span&gt;&lt;span&gt;  develop
&lt;&#x2F;span&gt;&lt;span&gt;* master
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you see the current branch is marked with an asterisk(&lt;code&gt;*&lt;&#x2F;code&gt;). Eventhough it is unlikely I would want to try to switch my current branch. Actually I would never want that. But just to make it prone to typo&#x27;s, lets fix it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git checkout $(git branch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf)
&lt;&#x2F;span&gt;&lt;span&gt;error: pathspec &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;master&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; did not match any file(s) known to git
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If I would select &lt;code&gt;master&lt;&#x2F;code&gt;, my current branch, in &lt;code&gt;fzf&lt;&#x2F;code&gt; it would give me the error shown above. Since the output of &lt;code&gt;fzf&lt;&#x2F;code&gt; contains the asterisk too. See the snippet bellow. Where I selected master in &lt;code&gt;fzf&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git branch &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf
&lt;&#x2F;span&gt;&lt;span&gt;* master
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we have to get the branch names in some other way to not have the asterisk showing up. In git we can use a lesser known, &lt;code&gt;for-each-ref&lt;&#x2F;code&gt; command and format the output as follows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git for-each-ref refs&#x2F;heads&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --format&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;%(refname:short)&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;master
&lt;&#x2F;span&gt;&lt;span&gt;develop
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I can use that command as a substitute for &lt;code&gt;git branch&lt;&#x2F;code&gt;. If I now try to switch to my current branch I get a more appropriate message.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git checkout $(git for-each-ref refs&#x2F;heads&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --format&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;%(refname:short)&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf)
&lt;&#x2F;span&gt;&lt;span&gt;Already on &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;master&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;Your branch is up to date with &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;origin&#x2F;master&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Cool, that works too! So we are finished but it turned into one hell of a command which I don&#x27;t plan on typing out ever. Instead I created a separate script.&lt;&#x2F;p&gt;
&lt;p&gt;For me this script is at &lt;code&gt;~&#x2F;Code&#x2F;bin&#x2F;interactive-checkout&lt;&#x2F;code&gt;, this works for me because I have the directory &lt;code&gt;~&#x2F;Code&#x2F;bin&lt;&#x2F;code&gt; added to my path.&lt;&#x2F;p&gt;
&lt;p&gt;The script &lt;code&gt;interactive-checkout&lt;&#x2F;code&gt; has the following contents.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;git checkout $(git for-each-ref refs&#x2F;heads&#x2F;&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --format&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;%(refname:short)&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;fzf)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now I can use &lt;code&gt;interactive-checkout&lt;&#x2F;code&gt; as a command instead of &lt;code&gt;git checkout $(git for-each-ref refs&#x2F;heads&#x2F; --format=&#x27;%(refname:short)&#x27; | fzf)&lt;&#x2F;code&gt; and I can potentially bind it to an alias as well.&lt;&#x2F;p&gt;
&lt;p&gt;Don&#x27;t forget to make the script executable with &lt;code&gt;chmod&lt;&#x2F;code&gt; :-).&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to add an item at the beginning of a list in Python</title>
		<published>2020-10-06T00:00:00+00:00</published>
		<updated>2020-10-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-add-item-at-beginning-of-list/" type="text/html"/>
		<id>https://koenwoortman.com/python-add-item-at-beginning-of-list/</id>
		<content type="html">&lt;p&gt;Where you add an item to the end of a list with &lt;code&gt;append()&lt;&#x2F;code&gt;. You can use &lt;code&gt;insert()&lt;&#x2F;code&gt; to insert an item at the beginning of a list in Python.&lt;&#x2F;p&gt;
&lt;p&gt;The insert function takes two arguments. First the index of where the item should be inserted and second the actual item that should be inserted.&lt;&#x2F;p&gt;
&lt;p&gt;So the signature of the &lt;code&gt;insert()&lt;&#x2F;code&gt; function is as follows:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;list.insert(index, item)&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You use the &lt;code&gt;insert()&lt;&#x2F;code&gt; function actually to insert an item at any position, depending on the index you pass to it.&lt;&#x2F;p&gt;
&lt;p&gt;Since we want to add an item at the first position of the list we pass &lt;code&gt;0&lt;&#x2F;code&gt; as the first argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;pizza_toppings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cheese&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tuna&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;pizza_toppings.insert(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tomato sauce&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(pizza_toppings)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;tomato sauce&amp;#39;, &amp;#39;cheese&amp;#39;, &amp;#39;tuna&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The above example added &lt;code&gt;&amp;quot;tomato sauce&amp;quot;&lt;&#x2F;code&gt; in the first position of the &lt;code&gt;pizza_topping&lt;&#x2F;code&gt; list. Now the list makes sense.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>How to get the index of an item in a Python list</title>
		<published>2020-10-05T00:00:00+00:00</published>
		<updated>2020-10-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-get-index-of-item-in-list/" type="text/html"/>
		<id>https://koenwoortman.com/python-get-index-of-item-in-list/</id>
		<content type="html">&lt;p&gt;Suppose you want to find the index of an item in a list in Python. You can use the built-in &lt;code&gt;index()&lt;&#x2F;code&gt; function to do so. The &lt;code&gt;index()&lt;&#x2F;code&gt; function takes the item you want to search for as its first argument.&lt;&#x2F;p&gt;
&lt;p&gt;So the signature of the &lt;code&gt;index()&lt;&#x2F;code&gt; function is as follows:&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;list.index(key [, start[, end]])&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;start&lt;&#x2F;code&gt; and &lt;code&gt;end&lt;&#x2F;code&gt; arguments are optional actually. You use those to specify the range of the list you want to search. By default the &lt;code&gt;index()&lt;&#x2F;code&gt; function searches the whole lists.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;pizza_toppings &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tomato sauce&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cheese&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tuna&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;index &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;pizza_toppings.index(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cheese&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(index)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; 1
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Merge multiple Python lists into one</title>
		<published>2020-10-04T00:00:00+00:00</published>
		<updated>2020-10-04T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/python-merge-lists-together/" type="text/html"/>
		<id>https://koenwoortman.com/python-merge-lists-together/</id>
		<content type="html">&lt;p&gt;Merging Python lists together is as easy as using the &lt;code&gt;+&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;orders &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;cheeseburger&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;+ &lt;&#x2F;span&gt;&lt;span&gt;[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;soda&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(orders)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;cheeseburger&amp;#39;, &amp;#39;soda&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can leverage the &lt;code&gt;extend()&lt;&#x2F;code&gt; function for this purpose as well. You call it on the list you want to extend and pass it a list.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;orders.extend([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;fries&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;])
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(orders)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;cheeseburger&amp;#39;, &amp;#39;soda&amp;#39;, &amp;#39;fries&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Be aware that you pass it a list, not just a string. If you pass it just a string all the characters get inserted individually.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;python&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-python &quot;&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span&gt;orders.extend(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;fries&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(orders)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# =&amp;gt; [&amp;#39;cheeseburger&amp;#39;, &amp;#39;soda&amp;#39;, &amp;#39;f&amp;#39;, &amp;#39;r&amp;#39;, &amp;#39;i&amp;#39;, &amp;#39;e&amp;#39;, &amp;#39;s&amp;#39;]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Change a commit message in git</title>
		<published>2020-04-06T00:00:00+00:00</published>
		<updated>2020-04-06T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-change-commit-message/" type="text/html"/>
		<id>https://koenwoortman.com/git-change-commit-message/</id>
		<content type="html">&lt;p&gt;Messing up your commit message is something that happens every now and then. Git allows you to make changes to your last commit by using the &lt;code&gt;--amend&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git commit --amend
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will open the default editor that you configured with the message of your previous commit. In here you just change the message and exit out your editor. And voilà, commit message changed.&lt;&#x2F;p&gt;
&lt;p&gt;You can use the &lt;code&gt;-m&lt;&#x2F;code&gt; option as well, to set the message directly from the command line.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git commit --amend -m &amp;quot;Hold your horses, bugfix incoming&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;strong&gt;Be aware&lt;&#x2F;strong&gt; that &lt;code&gt;--amend&lt;&#x2F;code&gt; adds the changes you staged to the commit. So if this is not desired you need to either unstage the changes or stash them for the time being.&lt;&#x2F;p&gt;
&lt;p&gt;If you have pushed the commit to a remote before and try to push again, Git &lt;strong&gt;will reject&lt;&#x2F;strong&gt; your push. In that case you need to use &lt;code&gt;git push --force&lt;&#x2F;code&gt;. You need to be careful with this. Using &lt;code&gt;--force&lt;&#x2F;code&gt; overwrites the history of your remote. Also, if others already based their work on your commit they might not be happy.&lt;&#x2F;p&gt;
&lt;p&gt;You might have noticed that the timestamp of the commit didn&#x27;t change. If you want to update the timestamp too, you can set the &lt;code&gt;GIT_COMMITTER_DATE&lt;&#x2F;code&gt; environment variable before running the command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;GIT_COMMITTER_DATE=&amp;quot;Wed Apr 03 15:00 2020 +0100&amp;quot; git commit --amend
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If the commit message you want to change isn&#x27;t from the last commit things get more tricky. The easiest way I found is using an interactive rebase. Find the commit hash of the commit &lt;strong&gt;before&lt;&#x2F;strong&gt; the one you&#x27;d like to change.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;git rebase -i 9bef4f8
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the editor that opens, change the &lt;code&gt;pick&lt;&#x2F;code&gt; command for &lt;code&gt;edit&lt;&#x2F;code&gt; for the commit you want to change.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;edit 9e48a07 %$#^$%
&lt;&#x2F;span&gt;&lt;span&gt;pick 2142d46 Perfect...
&lt;&#x2F;span&gt;&lt;span&gt;pick 10da46d Hacky code, might be buggy
&lt;&#x2F;span&gt;&lt;span&gt;pick 5ba932b Hold your horses, bugfix incoming
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;# Rebase 9bef4f8..5ba932b onto 5ba932b (4 commands)
&lt;&#x2F;span&gt;&lt;span&gt;#
&lt;&#x2F;span&gt;&lt;span&gt;# Commands:
&lt;&#x2F;span&gt;&lt;span&gt;# ...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You are now in the past. You can make changes on top of commit &lt;code&gt;9e48a07&lt;&#x2F;code&gt;. Or... just change the commit message. We will do that for now.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git commit --amend -m &amp;quot;Enjoy your code, Sir&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we changed the message we need to rebase the commits that came next on this new change.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git rebase --continue
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So, did that work? Lets find out.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ git log --abbrev-commit --pretty=oneline
&lt;&#x2F;span&gt;&lt;span&gt;5ba932b (HEAD -&amp;gt; master) Hold your horses, bugfix incoming
&lt;&#x2F;span&gt;&lt;span&gt;10da46d Hacky code, might be buggy
&lt;&#x2F;span&gt;&lt;span&gt;2142d46 Perfect...
&lt;&#x2F;span&gt;&lt;span&gt;9e48a07 Enjoy your code, Sir
&lt;&#x2F;span&gt;&lt;span&gt;9bef4f8 We will need to rebase from here
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see, the commit message has changed. As well as the commit hash. If you find yourself amending more ofter, consider adding an alias in your gitconfig.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;[alias]
&lt;&#x2F;span&gt;&lt;span&gt;    amend = commit --amend
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reload the tmux config</title>
		<published>2020-04-03T00:00:00+00:00</published>
		<updated>2020-04-03T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-reload-config/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-reload-config/</id>
		<content type="html">&lt;p&gt;Testing out settings in your tmux.conf file would be a pain if you had to kill and restart tmux each time.&lt;&#x2F;p&gt;
&lt;p&gt;You see the changes you made by sourcing &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt;. To source the fie you need to be in command mode. To start the command mode use &lt;code&gt;&amp;lt;prefix&amp;gt; :&lt;&#x2F;code&gt;. Now type in &lt;code&gt;:source-file ~&#x2F;.tmux.conf&lt;&#x2F;code&gt; and hit enter. This should affect all your tmux sessions. Restarting the tmux server shouldn&#x27;t be necessary.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;reload-config-with-key-binding&quot;&gt;Reload config with key binding&lt;&#x2F;h3&gt;
&lt;p&gt;If you do this frequently you might want to bind the &lt;code&gt;source-file&lt;&#x2F;code&gt; command to a key combination.&lt;&#x2F;p&gt;
&lt;p&gt;By adding the following binding to the &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file you can reload the configuration with &lt;code&gt;&amp;lt;prefix&amp;gt; r&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;bind r source-file ~&#x2F;.tmux.conf; display-message &amp;quot;Reloaded config&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Change the tmux prefix to ctrl+space</title>
		<published>2020-04-01T00:00:00+00:00</published>
		<updated>2020-04-01T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-prefix-ctrl-space/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-prefix-ctrl-space/</id>
		<content type="html">&lt;p&gt;Tmux uses ctrl+b as default prefix. I rather have it set to ctrl+Space. The space bar is hard to miss. And I use it as leader in VIM, so I like the consistency.&lt;&#x2F;p&gt;
&lt;p&gt;To configure the prefix we need to make a change in the &lt;code&gt;~&#x2F;.tmux.conf&lt;&#x2F;code&gt; file. Following the example in the tmux man pages, we make the following addition in the tmux config.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;# ~&#x2F;.tmux.conf
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;unbind-key C-b
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;set-option -g prefix C-Space
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;bind-key C-Space send-prefix
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Reattach to tmux sessions</title>
		<published>2020-03-30T00:00:00+00:00</published>
		<updated>2020-03-30T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-reattach/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-reattach/</id>
		<content type="html">&lt;p&gt;When you detached from tmux and want to reattach to it again you use the &lt;code&gt;tmux attach&lt;&#x2F;code&gt; command. Without any argument this command reattaches tmux session that was the latest one to be detached.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux attach
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;attach&lt;&#x2F;code&gt; is a shorthand actually for &lt;code&gt;attach-session&lt;&#x2F;code&gt;, but both do the same thing so you can use them interchangeably.&lt;&#x2F;p&gt;
&lt;p&gt;When attaching to the latest detached session isn&#x27;t enough you need to specify a target session to attach to. For this you need to know the session name. The session name is either a number that tmux automatically assigned to it or the name you gave with when creating the session with &lt;code&gt;tmux new -s &amp;lt;session&amp;gt;&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;If you don&#x27;t know the session ID you can find a session by &lt;a href=&quot;&#x2F;tmux-list-sessions&#x2F;&quot;&gt;listing&lt;&#x2F;a&gt; all the sessions with the &lt;code&gt;tmux list-sessions&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux list-sessions
&lt;&#x2F;span&gt;&lt;span&gt;first_project: 4 windows (created Wed Mar  4 18:17:14 2020)
&lt;&#x2F;span&gt;&lt;span&gt;second_project: 2 windows (created Wed Mar  4 19:00:55 2020)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now, to reattach to the session called &lt;code&gt;first_project&lt;&#x2F;code&gt; use the &lt;code&gt;attach&lt;&#x2F;code&gt; command with the &lt;code&gt;-t&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux attach&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span&gt; first_project
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Attaching to a session while you&#x27;re in tmux doesn&#x27;t work. If you try it you will see the following error message: &lt;code&gt;sessions should be nested with care, unset $TMUX to force&lt;&#x2F;code&gt;. Inside of tmux you can switch between sessions with &lt;code&gt;tmux choose-tree&lt;&#x2F;code&gt; for example.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Rename tmux sessions</title>
		<published>2020-03-27T00:00:00+00:00</published>
		<updated>2020-03-27T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-rename-session/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-rename-session/</id>
		<content type="html">&lt;p&gt;When you start a new tmux session, by default tmux uses a numerical value to name the session. This isn&#x27;t all too clear, especially when we&#x27;re working with multiple tmux sessions at the same time. Ofcourse you can start a named tmux session with &lt;code&gt;tmux new -s sesh&lt;&#x2F;code&gt;. But the ability to rename either the default name the provided name is convenient once every while.&lt;&#x2F;p&gt;
&lt;p&gt;To rename a tmux session you can use the &lt;code&gt;tmux rename-session&lt;&#x2F;code&gt; command. With the argument for the &lt;code&gt;-t&lt;&#x2F;code&gt; option you specify which session is the target to rename. And the second argument is the new name for the session.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux rename-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span&gt; old_name new_name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The alias for &lt;code&gt;rename-session&lt;&#x2F;code&gt; is simply &lt;code&gt;rename&lt;&#x2F;code&gt;. Meaning that you get the same results as above with &lt;code&gt;tmux rename -t old_name new_name&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;When you are in currently in the session you want to rename you can omit the current sessions name. Tmux will assume that your current session needs to be renamed if you don&#x27;t specify a target with &lt;code&gt;-t&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux rename-session new_name
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This command works outside of tmux too. In that case the most recently used tmux session is renamed.&lt;&#x2F;p&gt;
&lt;p&gt;The rename functionality can be used in the tmux command mode too. To start the command mode use &lt;code&gt;&amp;lt;prefix&amp;gt; :&lt;&#x2F;code&gt;. Now enter &lt;code&gt;:rename-window new_name&lt;&#x2F;code&gt; and your current tmux session is renamed to &amp;quot;new_name&amp;quot;.&lt;&#x2F;p&gt;
&lt;p&gt;To do the above more quickly you can use a keybinding. By default &lt;code&gt;:rename-window&lt;&#x2F;code&gt; is binded to the key combination &lt;code&gt;&amp;lt;prefix&amp;gt; $&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Check if a file is a symlink with Bash</title>
		<published>2020-03-25T00:00:00+00:00</published>
		<updated>2020-03-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/bash-script-check-if-file-is-symlink/" type="text/html"/>
		<id>https://koenwoortman.com/bash-script-check-if-file-is-symlink/</id>
		<content type="html">&lt;p&gt;Your Bash script might need to determine if a file is a symlink or not. In Bash you can test this with the &lt;code&gt;-L&lt;&#x2F;code&gt; operator that returns true if the file exists and is a symlink. Lets find out how this works by making a &lt;code&gt;symchecker&lt;&#x2F;code&gt; script.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file.
&lt;&#x2F;span&gt;&lt;span&gt;touch ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Link the created file above to the ~&#x2F;bin directory.
&lt;&#x2F;span&gt;&lt;span&gt;ln&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s &lt;&#x2F;span&gt;&lt;span&gt;~&#x2F;script ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Check if ~&#x2F;bin&#x2F;script is a symlink.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-L &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;It&amp;#39;s a link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ bash symchecker
&lt;&#x2F;span&gt;&lt;span&gt;It&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;s a link!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So we see that works. But the script isn&#x27;t perfect yet. Even though we checked for an existing symlink, this doesn&#x27;t guarantee that the symlink isn&#x27;t broken. Meaning the file that was symlinked might not exist anymore.&lt;&#x2F;p&gt;
&lt;p&gt;Before we proceed, lets clean up the symlink we created. Otherwise &lt;code&gt;ln&lt;&#x2F;code&gt; will show an error message the next time we run the script. Because the link already exist.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ unlink ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets see what I mean with broken links. We&#x27;ll add a step to the symchecker script that removes the initial file but doesn&#x27;t touch the symlink.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file.
&lt;&#x2F;span&gt;&lt;span&gt;touch ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Link the created file above to the ~&#x2F;bin directory.
&lt;&#x2F;span&gt;&lt;span&gt;ln&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s &lt;&#x2F;span&gt;&lt;span&gt;~&#x2F;script ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Remove the initial file.
&lt;&#x2F;span&gt;&lt;span&gt;rm ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 4. Check if ~&#x2F;bin&#x2F;script is a symlink.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-L &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;It&amp;#39;s a link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash symchecker
&lt;&#x2F;span&gt;&lt;span&gt;It&amp;#39;s a link!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And what do you know. The file where the symlink links to is removed but the test still succeeds. To include this check we use the &lt;code&gt;-e&lt;&#x2F;code&gt; operator which checks if a file exist.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file.
&lt;&#x2F;span&gt;&lt;span&gt;touch ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Link the created file above to the ~&#x2F;bin directory.
&lt;&#x2F;span&gt;&lt;span&gt;ln&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s &lt;&#x2F;span&gt;&lt;span&gt;~&#x2F;script ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Remove the initial file.
&lt;&#x2F;span&gt;&lt;span&gt;rm ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 4. Check if ~&#x2F;bin&#x2F;script is a symlink.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-L &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;amp;&amp;amp; &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;It&amp;#39;s a link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Might be a link, but it doesn&amp;#39;t exist!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash symchecker
&lt;&#x2F;span&gt;&lt;span&gt;ln: failed to create symbolic link &amp;#39;&#x2F;home&#x2F;koen&#x2F;bin&#x2F;script&amp;#39;: File exists
&lt;&#x2F;span&gt;&lt;span&gt;Might be a link, but it doesn&amp;#39;t exist!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So that did the trick. But uh oh, we didn&#x27;t unbind the symlink before. Now that we have a check for valid symlinks, we might as well safe us some time and clean up a broken link if we find one.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. We create a file.
&lt;&#x2F;span&gt;&lt;span&gt;touch ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Link the created file above to the ~&#x2F;bin directory.
&lt;&#x2F;span&gt;&lt;span&gt;ln&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -s &lt;&#x2F;span&gt;&lt;span&gt;~&#x2F;script ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Remove the initial file.
&lt;&#x2F;span&gt;&lt;span&gt;rm ~&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 4. Check if ~&#x2F;bin&#x2F;script is a symlink.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-L &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$HOME&#x2F;bin&#x2F;script&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;It&amp;#39;s a link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;    unlink ~&#x2F;bin&#x2F;script
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Link was broken, I cleaned it up!&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Not a link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash symchecker
&lt;&#x2F;span&gt;&lt;span&gt;ln: failed to create symbolic link &amp;#39;&#x2F;home&#x2F;koen&#x2F;bin&#x2F;script&amp;#39;: File exists
&lt;&#x2F;span&gt;&lt;span&gt;Link was broken, I cleaned it up!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So it look liked it worked! But we still see the error message from &lt;code&gt;ln&lt;&#x2F;code&gt;. Actually this is because the first time we run the script, it was still there from the previous version from the symchecker script. In which we didn&#x27;t clean up. To double check, we can it a second try.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash symchecker
&lt;&#x2F;span&gt;&lt;span&gt;Link was broken, I cleaned it up!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Yup, that looks better. But... all what we have now is a script that:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;Creates a file&lt;&#x2F;li&gt;
&lt;li&gt;Creates a symlink to that file&lt;&#x2F;li&gt;
&lt;li&gt;Removes that file&lt;&#x2F;li&gt;
&lt;li&gt;Removes the symlink&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;So the end result is basically nothing.&lt;&#x2F;p&gt;
&lt;p&gt;Lets make the script sort of usable by letting it expect a file path as parameter. So we will end up with a script that checks if the specified file is a symlink or not and clean it up if it happens to be broken.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#!&#x2F;bin&#x2F;bash
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 1. Store the first parameter to the script in a variable.
&lt;&#x2F;span&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$1&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 2. Now that we expect a parameter, lets check if it was provided.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-z &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Script expects a parameter&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt; 1
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 3. Check if the parameter is a symlink.
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-L &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# 4. Check if it links to a valid path.
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;[[ &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;-e &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;; then
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path is a valid link!&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;    unlink &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Cleaned up broken link: $path&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;$path is not a symlink.&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;fi
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ bash symchecker ~&#x2F;brokenlink
&lt;&#x2F;span&gt;&lt;span&gt;Cleaned up broken link: ~&#x2F;brokenlink
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There you go. An advanced symlink checker and broken link cleaner in one. All that&#x27;s left is putting the script in your PATH and make is executable with chmod.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;learning-more&quot;&gt;Learning more&lt;&#x2F;h2&gt;
&lt;p&gt;The best way to learn more is to use Bash. A lot. Don&#x27;t forget that Google is your friend.&lt;&#x2F;p&gt;
&lt;p&gt;In case you learn well from books I would recommend these.&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;1593279523&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=1593279523&amp;amp;linkId=9cf8e9ac8913afbf3dcc8c591ad0780a&quot;&gt;The Linux Command Line: A Complete Introduction&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.amazon.com&#x2F;gp&#x2F;product&#x2F;0596009658&#x2F;ref=as_li_tl?ie=UTF8&amp;amp;tag=koensw-20&amp;amp;camp=1789&amp;amp;creative=9325&amp;amp;linkCode=as2&amp;amp;creativeASIN=0596009658&amp;amp;linkId=80e3fbe123ef781b1b84db41e56eb17c&quot;&gt;Learning the bash Shell: Unix Shell Programming&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Detach from tmux sessions</title>
		<published>2020-03-23T00:00:00+00:00</published>
		<updated>2020-03-23T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-detach-sessions/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-detach-sessions/</id>
		<content type="html">&lt;p&gt;In case you are in tmux and want to return back to you terminal you need to &lt;strong&gt;detach&lt;&#x2F;strong&gt; tmux. Doing this in the most convenient way is with the keybinding &lt;code&gt;&amp;lt;prefix&amp;gt; d&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;This will return you to your terminal and keep the tmux session running. So you can attach to it later with &lt;code&gt;tmux attach&lt;&#x2F;code&gt;. If you don&#x27;t want to keep the session running, than &lt;a href=&quot;&#x2F;tmux-kill-sessions&#x2F;&quot;&gt;kill-session&lt;&#x2F;a&gt; is what you&#x27;re looking for.&lt;&#x2F;p&gt;
&lt;p&gt;If necessary, for example when you&#x27;re scripting against tmux, you can use the command to detach as well. Running the command will detach your current tmux session. Optionally you can pass a different target session to the &lt;code&gt;-s&lt;&#x2F;code&gt; option to detach another actively running session.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux detach-client
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Tmux provides a shorter notation for the above command as well. Which is just simply &lt;code&gt;detach&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux detach
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Counting files and directories in Linux</title>
		<published>2020-03-20T00:00:00+00:00</published>
		<updated>2020-03-20T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-counting-files-and-directories/" type="text/html"/>
		<id>https://koenwoortman.com/linux-counting-files-and-directories/</id>
		<content type="html">&lt;p&gt;The &lt;code&gt;wc&lt;&#x2F;code&gt; command is one of the GNU coreutils available on Linux. &lt;code&gt;wc&lt;&#x2F;code&gt; is meant to get the amount of newlines, words, bytes and characters in a file. By combining &lt;code&gt;wc&lt;&#x2F;code&gt; with &lt;code&gt;ls&lt;&#x2F;code&gt; or &lt;code&gt;find&lt;&#x2F;code&gt; you can easily get the amount of files or directories in a directory.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;counting-all-files-and-directories&quot;&gt;Counting all files and directories&lt;&#x2F;h3&gt;
&lt;p&gt;We can count all the files and directories in a directory by passing the output of &lt;code&gt;ls&lt;&#x2F;code&gt; to &lt;code&gt;wc&lt;&#x2F;code&gt; using a pipe(&lt;code&gt;|&lt;&#x2F;code&gt;). To list one file per line we use the &lt;code&gt;-1&lt;&#x2F;code&gt; option for &lt;code&gt;ls&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@opensuse:~&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -1 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;wc&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;18
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The example above only counts the visible directories and files. In order to include hidden files or directories we have to pass the &lt;code&gt;-a&lt;&#x2F;code&gt; option to the &lt;code&gt;ls&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@opensuse:~&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -1a &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;wc&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;24
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;count-files-only&quot;&gt;Count files only&lt;&#x2F;h3&gt;
&lt;p&gt;To just count the amount of files we can&#x27;t just use the &lt;code&gt;ls&lt;&#x2F;code&gt; command, since it doesn&#x27;t make a distinction between files and directories. We can solve this with the &lt;code&gt;find&lt;&#x2F;code&gt; command. The &lt;code&gt;find&lt;&#x2F;code&gt; command is meant to, well.. find directories and files.&lt;&#x2F;p&gt;
&lt;p&gt;The &lt;code&gt;find&lt;&#x2F;code&gt; command looks less straightforward than &lt;code&gt;ls&lt;&#x2F;code&gt;. You need to specify a path, we use &lt;code&gt;.&lt;&#x2F;code&gt; to find in our current working directory. Second we specify the maximum depth &lt;code&gt;find&lt;&#x2F;code&gt; searches, we use 1 as an argument so &lt;code&gt;find&lt;&#x2F;code&gt; will only list the contents of the current directory. To only search for files we have to specify the type &lt;code&gt;f&lt;&#x2F;code&gt;. And finally, to exclude hidden files we filter out files starting with a dot.&lt;&#x2F;p&gt;
&lt;p&gt;To get the amount of files we pipe the output of the &lt;code&gt;find&lt;&#x2F;code&gt; command to &lt;code&gt;wc&lt;&#x2F;code&gt; and we get the amount of files in the current directory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@opensuse:~&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; find .&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -maxdepth&lt;&#x2F;span&gt;&lt;span&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -type&lt;&#x2F;span&gt;&lt;span&gt; f&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;[!.]*&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;wc&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;14
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;count-directories-only&quot;&gt;Count directories only&lt;&#x2F;h3&gt;
&lt;p&gt;The same trick as the example above can be used to count the directories. We have to change the argument for &lt;code&gt;-type&lt;&#x2F;code&gt; to &lt;code&gt;d&lt;&#x2F;code&gt; and it works.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@opensuse:~&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; find .&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -maxdepth&lt;&#x2F;span&gt;&lt;span&gt; 1&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -type&lt;&#x2F;span&gt;&lt;span&gt; d&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -name &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;[!.]*&amp;quot; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;wc&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;4
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Kill tmux sessions</title>
		<published>2020-03-18T00:00:00+00:00</published>
		<updated>2020-03-18T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-kill-sessions/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-kill-sessions/</id>
		<content type="html">&lt;p&gt;When you detach from a tmux session it keeps running, so you can reattach to it later again. However, if you don&#x27;t need the session again you might as well close it for good. You can do this by killing the session.&lt;&#x2F;p&gt;
&lt;p&gt;To kill a tmux sessions you use the &lt;code&gt;tmux kill-session&lt;&#x2F;code&gt; command. If you don&#x27;t specify a session tmux will kill the current or last active session. Depending on if you&#x27;re currently in an active tmux session or not.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux kill-session
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;A session to kill can be specified by using the &lt;code&gt;-t&lt;&#x2F;code&gt; option and use the session name as its argument.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux kill-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span&gt; session_one
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;kill-all-tmux-sessions&quot;&gt;Kill all tmux sessions&lt;&#x2F;h3&gt;
&lt;p&gt;The easiest way to kill all tmux sessions isn&#x27;t with the &lt;code&gt;kill-session&lt;&#x2F;code&gt; command. To kill all tmux sessions at once you can use the &lt;code&gt;kill-server&lt;&#x2F;code&gt; command. This command will besides killing all the sessions also shut down the tmux server.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux kill-server
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;kill-all-other-tmux-sessions&quot;&gt;Kill all other tmux sessions&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;kill-session&lt;&#x2F;code&gt; command has the option to kill all session except a specified one. For this you&#x27;ll need to use the &lt;code&gt;-a&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;p&gt;In case you want to kill all tmux sessions except the current one you don&#x27;t need to specify a session name. Or if you&#x27;re outside of tmux, tmux will assume you want to kill the last active session. See the command below to kill all other tmux sessions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux kill-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -a
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If that isn&#x27;t what you want you can specify the session name yourself. For this you&#x27;ll have to use the same &lt;code&gt;-t&lt;&#x2F;code&gt; option as before.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux kill-session&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -at&lt;&#x2F;span&gt;&lt;span&gt; pumpkin_session
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Use Visual Studio Code as git editor</title>
		<published>2020-03-16T00:00:00+00:00</published>
		<updated>2020-03-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-visual-studio-code-as-editor/" type="text/html"/>
		<id>https://koenwoortman.com/git-visual-studio-code-as-editor/</id>
		<content type="html">&lt;p&gt;Most of the time the Git integration of Visual Studio Code will do fine. But for other functions of Git the command-line seems more efficient. Once you use git over the command-line you&#x27;ll find yourself sometimes using a terminal text editor. Which might be fine for you. But if you&#x27;re the integrated terminal in Visual Studio Code it might feel a bit awkward. In this case you can set Visual Studio Code as the default editor for git.&lt;&#x2F;p&gt;
&lt;p&gt;During this example we will change the global git configuration. You can change the configuration on a repository level too. To do this; make sure you&#x27;re in the git repository and lose the &lt;code&gt;--global&lt;&#x2F;code&gt; option.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;set-visual-studio-code-as-git-editor&quot;&gt;Set Visual Studio code as git editor&lt;&#x2F;h3&gt;
&lt;p&gt;To set Visual Studio Code as default editor you have to run the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git config&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span&gt; core.editor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;code --wait&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This adds the following configuration to your global git config.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ini&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ini &quot;&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[core]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;editor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By passing the &lt;code&gt;--wait&lt;&#x2F;code&gt; to the code command you tell Visual Studio Code to wait for you to close the file before returning to the terminal.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;set-visual-studio-code-as-git-mergetool&quot;&gt;Set Visual Studio code as git mergetool&lt;&#x2F;h3&gt;
&lt;p&gt;Besides an editor you can set a merge tool. Ofcourse you can use Visual Studio Code for this too. This is a two step process. First you have to define a new merge tool and secondly you need to set it as the default merge tool.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git config&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span&gt; mergetool.vscode.cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;code --wait $MERGED&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$ git config&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span&gt; merge.tool vscode
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This adds the following configuration to your global git config.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ini&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ini &quot;&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[merge]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;tool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; vscode
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[mergetool &amp;quot;vscode&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;$MERGED
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;set-visual-studio-code-as-git-difftool&quot;&gt;Set Visual Studio code as git difftool&lt;&#x2F;h3&gt;
&lt;p&gt;The same goes for setting the git difftool. You will have to run the following two commands.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git config&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span&gt; mergetool.vscode.cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;code --wait $MERGED&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;$ git config&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --global&lt;&#x2F;span&gt;&lt;span&gt; merge.tool vscode
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This adds the following configuration to your global git config.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ini&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ini &quot;&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[diff]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;tool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; vscode
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[difftool &amp;quot;vscode&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;diff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;$LOCAL $REMOTE
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;&#x2F;h2&gt;
&lt;p&gt;You can change the git config to use Visual Studio Code instead of a terminal text editor. To do this in one step you can paste the following snippet in your git config file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ini&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ini &quot;&gt;&lt;code class=&quot;language-ini&quot; data-lang=&quot;ini&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[core]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;editor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[merge]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;tool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; vscode
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[mergetool &amp;quot;vscode&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;$MERGED
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[diff]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;tool &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; vscode
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;[difftool &amp;quot;vscode&amp;quot;]
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;cmd &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; code &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;wait &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;--&lt;&#x2F;span&gt;&lt;span&gt;diff &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;$LOCAL $REMOTE
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For more information read the Visual Studio Code &lt;a href=&quot;https:&#x2F;&#x2F;code.visualstudio.com&#x2F;docs&#x2F;editor&#x2F;versioncontrol#_vs-code-as-git-editor&quot;&gt;docs&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Create a file of specific size in Linux</title>
		<published>2020-03-13T00:00:00+00:00</published>
		<updated>2020-03-13T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-create-file-of-specific-size/" type="text/html"/>
		<id>https://koenwoortman.com/linux-create-file-of-specific-size/</id>
		<content type="html">&lt;p&gt;While testing an upload service for example you probably need to test it with a large file. On the Linux command-line you can create such a file with the &lt;code&gt;dd&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;p&gt;To generate a file of 10MB you can run the following command. This will create a file called &lt;code&gt;largefile.txt&lt;&#x2F;code&gt; in your current directory filled with zeros. If you prefer random data instead of zeros you will have to replace &lt;code&gt;&#x2F;dev&#x2F;zero&lt;&#x2F;code&gt; by &lt;code&gt;&#x2F;dev&#x2F;urandom&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ dd if=&#x2F;dev&#x2F;zero of=largefile.txt bs=1M count=10.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>4 ways to clean up your Laravel routes files</title>
		<published>2020-03-11T00:00:00+00:00</published>
		<updated>2020-03-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/laravel-4-ways-to-clean-routes-files/" type="text/html"/>
		<id>https://koenwoortman.com/laravel-4-ways-to-clean-routes-files/</id>
		<content type="html">&lt;p&gt;As your Laravel application grows, your routes file grows with it. And most often it doesn&#x27;t just grow with it, it becomes messy and hard to read. So cleaning it up once every while can be time well spend.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-use-resource-routes&quot;&gt;1. Use resource routes&lt;&#x2F;h2&gt;
&lt;p&gt;The first route definition you see in your &lt;code&gt;web.php&lt;&#x2F;code&gt; routes file after starting a new Laravel project is:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;view(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;welcome&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you&#x27;re running PHP 7.4 or higher, locally and on your server, you can clean this one up a little with an arrow function. The following is equivalent to the route definition above, but using an arrow function.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;fn&lt;&#x2F;span&gt;&lt;span&gt;() =&amp;gt; view(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;welcome&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But if you&#x27;d ask me routes using closures cause quite some visual noise. So I rather use controllers over closures unless it really makes no sense to make a controller for a view.&lt;&#x2F;p&gt;
&lt;p&gt;Another pro for using controllers for your routes is that you can leverage resource routes. When you define a resource route you get routes for all the CRUD actions you&#x27;ll probably need.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::resource(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;UsersController&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Just that one line generated the following routes for you.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan route:list --compact --name user
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+-------------------+----------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Method    | URI               | Action                                       |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+-------------------+----------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD  | users             | App\Http\Controllers\UsersController@index   |
&lt;&#x2F;span&gt;&lt;span&gt;| POST      | users             | App\Http\Controllers\UsersController@store   |
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD  | users&#x2F;create      | App\Http\Controllers\UsersController@create  |
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD  | users&#x2F;{user}      | App\Http\Controllers\UsersController@show    |
&lt;&#x2F;span&gt;&lt;span&gt;| PUT|PATCH | users&#x2F;{user}      | App\Http\Controllers\UsersController@update  |
&lt;&#x2F;span&gt;&lt;span&gt;| DELETE    | users&#x2F;{user}      | App\Http\Controllers\UsersController@destroy |
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD  | users&#x2F;{user}&#x2F;edit | App\Http\Controllers\UsersController@edit    |
&lt;&#x2F;span&gt;&lt;span&gt;+-----------+-------------------+----------------------------------------------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Which is super nice ofcourse, but it also means you need a controller with those same actions. That&#x27;s a low effort task luckily. By running the following artisan command you get a new controller with all seven actions.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php make:controller UsersController --resource
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;There is a chance however that you don&#x27;t need all of the actions. If that&#x27;s the case you can chain the route definition with &lt;code&gt;-&amp;gt;only([...])&lt;&#x2F;code&gt; or &lt;code&gt;-&amp;gt;except([...])&lt;&#x2F;code&gt; to get a subset of all the resource routes.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::resource(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;UsersController&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;only([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;show&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will limit the user routes to only an index and a show action. Even if you just have a couple of actions this quickly saves you lines of code.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan route:list --compact --name user
&lt;&#x2F;span&gt;&lt;span&gt;+----------+--------------+--------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Method   | URI          | Action                                     |
&lt;&#x2F;span&gt;&lt;span&gt;+----------+--------------+--------------------------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD | users        | App\Http\Controllers\UsersController@index |
&lt;&#x2F;span&gt;&lt;span&gt;| GET|HEAD | users&#x2F;{user} | App\Http\Controllers\UsersController@show  |
&lt;&#x2F;span&gt;&lt;span&gt;+----------+--------------+--------------------------------------------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;2-register-routes-in-tuples&quot;&gt;2. Register routes in tuples&lt;&#x2F;h2&gt;
&lt;p&gt;Since &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;laravel&#x2F;framework&#x2F;pull&#x2F;24385&quot;&gt;Laravel 5.6&lt;&#x2F;a&gt; we can use arrays instead of strings to define the controller and action of a route. This tuple syntax looks like the following.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [\App\Http\Controllers\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;UsersController&lt;&#x2F;span&gt;&lt;span&gt;::class, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can clean this up by adding a &lt;code&gt;use&lt;&#x2F;code&gt; statement for the &lt;code&gt;UsersController&lt;&#x2F;code&gt;. Check if your editor can hide these, because these will start to add up pretty quick when your application grows.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;App\Http\Controllers\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;UsersController&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;UsersController&lt;&#x2F;span&gt;&lt;span&gt;::class, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Or you can set the namespace of the routes file to the same namespace of your controllers. What falls in the category hacky if you&#x27;d ask me, but being a little creative can be fun sometimes :-).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;namespace &lt;&#x2F;span&gt;&lt;span&gt;App\Http\Controllers;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, [&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;UsersController&lt;&#x2F;span&gt;&lt;span&gt;::class, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Using the tuple syntax might allow you to use the &amp;quot;Go to Definition&amp;quot; and &amp;quot;Refactor&amp;quot; functions of your IDE. Which are nice benefits in (at least) my workflow compared to the string syntax which feels a bit magical.&lt;&#x2F;p&gt;
&lt;p&gt;One thing that is a bit of a bummer is that it doesn&#x27;t work with resource routes at the moment.&lt;&#x2F;p&gt;
&lt;p&gt;For more information about tuple routes, &lt;a href=&quot;https:&#x2F;&#x2F;freek.dev&#x2F;1210-a-better-way-to-register-routes-in-laravel&quot;&gt;Freek.dev&lt;&#x2F;a&gt; has a great opinion about them.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;3-split-up-the-routes-file&quot;&gt;3. Split up the routes file&lt;&#x2F;h2&gt;
&lt;p&gt;Larger applications have easily dozens of route definitions divided in multipe route groups. Lets say your application has a public section, a section that requires authentication and a section that requires admin access. In that case the routes file has a structure similar to something like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; routes&#x2F;web.php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;view(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;welcome&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::group([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;middleware&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;auth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;prefix&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;app&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::resource(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;UsersController&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;only([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;show&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::group([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;middleware&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;admin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dashboard&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;DashboardController@index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I personally find it hard to follow what is happening with all the groupings and the level of indent. Imagine managing an application with hundreds of routes. We can solve this by splitting the routes file up in three files: &lt;code&gt;routes&#x2F;web.php&lt;&#x2F;code&gt;, &lt;code&gt;routes&#x2F;app.php&lt;&#x2F;code&gt; and &lt;code&gt;routes&#x2F;admin.php&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;So we end up with the following three files:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; routes&#x2F;web.php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&#x2F;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span&gt;view(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;welcome&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; routes&#x2F;app.php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::resource(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;users&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;UsersController&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)-&amp;gt;only([&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;show&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; routes&#x2F;admin.php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::get(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;dashboard&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;DashboardController@index&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But this doesn&#x27;t work correctly just like that. If we check our available routes now we that only the one from &lt;code&gt;routes&#x2F;web.php&lt;&#x2F;code&gt; is still available.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan route:list
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------+------+---------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Domain | Method   | URI      | Name | Action  | Middleware   |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------+------+---------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | &#x2F;        |      | Closure | web          |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------+------+---------+--------------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Also notice that we got rid of all the middleware in the &lt;code&gt;routes&#x2F;app.php&lt;&#x2F;code&gt; and &lt;code&gt;routes&#x2F;admin.php&lt;&#x2F;code&gt; files. We fix that later on ;-)&lt;&#x2F;p&gt;
&lt;p&gt;For this to work we need to add the newly created routes files to the RouteServiceProvider. And in the RouteServiceProvider class you want to look for the &lt;code&gt;map()&lt;&#x2F;code&gt; function. Which, if you haven&#x27;t touched it before, should look like this:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapApiRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapWebRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;We will add calls &lt;code&gt;mapAppRoutes&lt;&#x2F;code&gt; and &lt;code&gt;mapAdminRoutes&lt;&#x2F;code&gt; to this function in which we will register the other two routes files.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;map&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapApiRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapWebRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapAppRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    $this-&amp;gt;mapAdminRoutes();
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So now we need to write those two new functions. Both should be very similar to the &lt;code&gt;mapWebRoutes&lt;&#x2F;code&gt; function that did already exist. But we need to change the file path and add back the middleware.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;protected &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mapWebRoutes&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;web&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;namespace($this-&amp;gt;namespace)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;group(base_path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;routes&#x2F;web.php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;protected &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mapAppRoutes&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;web&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;auth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;namespace($this-&amp;gt;namespace)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;group(base_path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;routes&#x2F;app.php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;protected &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;mapAdminRoutes&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;web&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;auth&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;middleware(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;admin&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;namespace($this-&amp;gt;namespace)
&lt;&#x2F;span&gt;&lt;span&gt;            -&amp;gt;group(base_path(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;routes&#x2F;admin.php&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;));
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;And voila, our route definitions are back as we had them before. Only now in clean separated files.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan route:list
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+--------------+-------------+------------------------------------------------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Domain | Method   | URI          | Name        | Action                                         | Middleware   |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+--------------+-------------+------------------------------------------------+--------------+
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | &#x2F;            |             | Closure                                        | web          |
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | dashboard    |             | App\Http\Controllers\DashboardController@index | web          |
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | users        | users.index | App\Http\Controllers\UsersController@index     | auth         |
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | users&#x2F;{user} | users.show  | App\Http\Controllers\UsersController@show      | auth         |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+--------------+-------------+------------------------------------------------+--------------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;4-routes-macros&quot;&gt;4. Routes macros&lt;&#x2F;h2&gt;
&lt;p&gt;The Marcroable trait is something you might have seen before. With macros you can basically &amp;quot;add&amp;quot; functions to the interface of a class. This is useful when you want to extend one of Laravels core classes with a function that&#x27;s really specific to your application for example.&lt;&#x2F;p&gt;
&lt;p&gt;Luckily for us, the Laravel router implements the &lt;code&gt;Macroable&lt;&#x2F;code&gt; trait. So what this allows us to do is to bundle a set of routes in a macro and register them at once in our routes file.&lt;&#x2F;p&gt;
&lt;p&gt;Lets say your application implements a service that expects data to be pushed to it from another service. An incoming webhook from GitHub for example. In that case you can bundle the route definitions with your service, and define them using a single line in your routes file.&lt;&#x2F;p&gt;
&lt;p&gt;You can define the route macro in the service provider of your GitHub service. It requires a name and an closure. And within the closure you can add route definitions as you would normally do in your routes file. Just make sure you add a &lt;code&gt;use&lt;&#x2F;code&gt; statement for the Route facade.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;public &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;boot&lt;&#x2F;span&gt;&lt;span&gt;()
&lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::macro(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;githubServiceRoutes&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::group([ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;middleware&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;validate_github_secret&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;], &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;span&gt;            &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::post(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;github&#x2F;webhook&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;GithubController@handle&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;        });
&lt;&#x2F;span&gt;&lt;span&gt;    });
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In your routes file you can now use the name you gave the macro as a static function for the Route facade.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;php&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-php &quot;&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span&gt;&amp;lt;?php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; routes&#x2F;admin.php
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;use &lt;&#x2F;span&gt;&lt;span&gt;Illuminate\Support\Facades\&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;Route&lt;&#x2F;span&gt;&lt;span&gt;::githubServiceRoutes();
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets double check with the artisan routes command. And yup, the route that we registered in the Github ServiceProvider is now available.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;$ php artisan route:list
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------------+------+----------------------------------------------+----------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;| Domain | Method   | URI            | Name | Action                                       | Middleware                 |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------------+------+----------------------------------------------+----------------------------+
&lt;&#x2F;span&gt;&lt;span&gt;|        | GET|HEAD | &#x2F;              |      | Closure                                      | web                        |
&lt;&#x2F;span&gt;&lt;span&gt;|        | POST     | github&#x2F;webhook |      | App\Http\Controllers\GithubController@handle | web,validate_github_secret |
&lt;&#x2F;span&gt;&lt;span&gt;+--------+----------+----------------+------+----------------------------------------------+----------------------------+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Using tail to follow log files</title>
		<published>2020-03-09T00:00:00+00:00</published>
		<updated>2020-03-09T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/linux-tail-follow-log-files/" type="text/html"/>
		<id>https://koenwoortman.com/linux-tail-follow-log-files/</id>
		<content type="html">&lt;p&gt;Log files are created for a reason. They come in handy when you&#x27;re debugging an unexpected HTTP response for example. But keep refreshing a log file is not an efficient way of working. To automatically see additions to a file you can use the &lt;code&gt;tail&lt;&#x2F;code&gt; command in follow mode.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;using-tail-in-follow-mode&quot;&gt;Using tail in follow mode&lt;&#x2F;h3&gt;
&lt;p&gt;The &lt;code&gt;tail&lt;&#x2F;code&gt; command is used to print the last lines in a file. By default it will show only the last 10 lines.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tail &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log
&lt;&#x2F;span&gt;&lt;span&gt;::1 - - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;27&#x2F;Feb&#x2F;2020:19:17:08 +0100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;GET &#x2F; HTTP&#x2F;1.1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; 200 17 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;-&amp;quot; &amp;quot;Mozilla&#x2F;5.0 (X11; Linux x86_64) AppleWebKit&#x2F;537.36 (KHTML, like Gecko) Chrome&#x2F;77.0.3865.90 Safari&#x2F;537.36&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The example above showed that the command finished after printing the last 10 lines. To keep the command running we need to use the &lt;code&gt;-f&lt;&#x2F;code&gt; option to follow additions added to the file.&lt;&#x2F;p&gt;
&lt;p&gt;When you pass the &lt;code&gt;-f&lt;&#x2F;code&gt; option you will see that the command keeps running and that lines get added when you make another request to NGINX.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tail&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log
&lt;&#x2F;span&gt;&lt;span&gt;::1 - - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;27&#x2F;Feb&#x2F;2020:19:19:28 +0100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;GET &#x2F; HTTP&#x2F;1.1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; 200 17 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;-&amp;quot; &amp;quot;Mozilla&#x2F;5.0 (X11; Linux x86_64) AppleWebKit&#x2F;537.36 (KHTML, like Gecko) Chrome&#x2F;77.0.3865.90 Safari&#x2F;537.36&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;tail-multiple-files&quot;&gt;Tail multiple files&lt;&#x2F;h3&gt;
&lt;p&gt;When necessary you can tail multiple files at the same time. To do this you add a second file path as an argument. This will show the file name as a header above new entries.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tail&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log &#x2F;var&#x2F;log&#x2F;nginx&#x2F;error.log
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;lt;==
&lt;&#x2F;span&gt;&lt;span&gt;::1 - - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;27&#x2F;Feb&#x2F;2020:19:24:38 +0100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;GET &#x2F; HTTP&#x2F;1.1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; 200 17 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;-&amp;quot; &amp;quot;Mozilla&#x2F;5.0 (X11; Linux x86_64) AppleWebKit&#x2F;537.36 (KHTML, like Gecko) Chrome&#x2F;77.0.3865.90 Safari&#x2F;537.36&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;filter-tail-results-with-grep&quot;&gt;Filter tail results with grep&lt;&#x2F;h3&gt;
&lt;p&gt;If you know what you&#x27;re looking for you can combine &lt;code&gt;tail&lt;&#x2F;code&gt; with &lt;code&gt;grep&lt;&#x2F;code&gt;. To search for a specific HTTP status for example. This way you&#x27;ll only see lines containing the string &amp;quot;404&amp;quot;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ sudo tail&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -f&lt;&#x2F;span&gt;&lt;span&gt; &#x2F;var&#x2F;log&#x2F;nginx&#x2F;access.log &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;| &lt;&#x2F;span&gt;&lt;span&gt;grep &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;404&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;::1 - - &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;[&lt;&#x2F;span&gt;&lt;span&gt;27&#x2F;Feb&#x2F;2020:13:13:24 +0100&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;GET &#x2F;favicon.ico HTTP&#x2F;1.1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; 404 548 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;http:&#x2F;&#x2F;example.me&#x2F;&amp;quot; &amp;quot;Mozilla&#x2F;5.0 (X11; Linux x86_64) AppleWebKit&#x2F;537.36 (KHTML, like Gecko) Chrome&#x2F;77.0.3865.90 Safari&#x2F;537.36&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>List tmux sessions</title>
		<published>2020-03-05T00:00:00+00:00</published>
		<updated>2020-03-05T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/tmux-list-sessions/" type="text/html"/>
		<id>https://koenwoortman.com/tmux-list-sessions/</id>
		<content type="html">&lt;p&gt;Tmux has the concepts of sessions, you can look at a session as a workspace for your terminal. This is convenient when you&#x27;re working on a project, creating panes and windows, but have to switch to something different in between. In this case you can just keep this specific session running so you can come back to it later. When you&#x27;re working with multiple sessions it can be useful to list the ones that are active at the moment.&lt;&#x2F;p&gt;
&lt;p&gt;To list all the sessions that are currently managed by the tmux server you can use the &lt;code&gt;tmux list-sessions&lt;&#x2F;code&gt; command. The command shows by default the name of the sessions, the amount of windows per session, when the session was created and whether the session is currently attached to a terminal.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux list-sessions
&lt;&#x2F;span&gt;&lt;span&gt;first_project: 4 windows (created Wed Mar  4 18:17:14 2020)
&lt;&#x2F;span&gt;&lt;span&gt;second_project: 2 windows (created Wed Mar  4 19:00:55 2020) (attached)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Like many tmux commands &lt;code&gt;list-sessions&lt;&#x2F;code&gt; has the shorthand too, &lt;code&gt;tmux ls&lt;&#x2F;code&gt; shows you the same information.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux ls
&lt;&#x2F;span&gt;&lt;span&gt;first_project: 4 windows (created Wed Mar  4 18:17:14 2020)
&lt;&#x2F;span&gt;&lt;span&gt;second_project: 2 windows (created Wed Mar  4 19:00:55 2020) (attached)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both of the examples above have an equivalent in the command mode of tmux. To start the command mode use &lt;code&gt;&amp;lt;prefix&amp;gt; :&lt;&#x2F;code&gt;. Now you can enter &lt;code&gt;:list-sessions&lt;&#x2F;code&gt; or &lt;code&gt;:ls&lt;&#x2F;code&gt; to see a list of active tmux sessions.&lt;&#x2F;p&gt;
&lt;p&gt;You can use these to make a custom keybinding in your &lt;code&gt;.tmux.conf&lt;&#x2F;code&gt; file. An example would be:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;bind &amp;lt;key&amp;gt; list-sessions
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;By default, &lt;code&gt;list-sessions&lt;&#x2F;code&gt; is binded to the key combination &lt;code&gt;&amp;lt;prefix&amp;gt; s&lt;&#x2F;code&gt;. You can navigate the session list with &lt;code&gt;j&lt;&#x2F;code&gt; and &lt;code&gt;k&lt;&#x2F;code&gt; and activate one by pressing &lt;code&gt;enter&lt;&#x2F;code&gt;. If your main purpose is to switch session you can try out &lt;code&gt;choose-session&lt;&#x2F;code&gt;, it is more verbose and gives you the possibility to switch to a specific window too.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;format-the-output&quot;&gt;Format the output&lt;&#x2F;h3&gt;
&lt;p&gt;You can specify the format of the session list with the &lt;code&gt;-F&lt;&#x2F;code&gt; option. When you&#x27;re scripting with tmux this can be a useful feature. For example, if your script requires the session names you can use the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -F &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;#{session_name}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;first_project
&lt;&#x2F;span&gt;&lt;span&gt;second_project
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can ofcourse use multiple variables in the format string. The output below shows the name of the active window in each session.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ tmux ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -F &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;#{session_name}:#{window_name}&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;first_project:database
&lt;&#x2F;span&gt;&lt;span&gt;second_project:server
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The FORMAT section in the man pages lists a whole lot more options for formatting the output of &lt;code&gt;list-sessions&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Type subsets with TypeScript Partials</title>
		<published>2020-03-02T00:00:00+00:00</published>
		<updated>2020-03-02T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/typescript-type-subsets-with-partials/" type="text/html"/>
		<id>https://koenwoortman.com/typescript-type-subsets-with-partials/</id>
		<content type="html">&lt;p&gt;The Partial type is one of the &lt;a href=&quot;https:&#x2F;&#x2F;www.typescriptlang.org&#x2F;docs&#x2F;handbook&#x2F;utility-types.html#partialt&quot;&gt;utility types&lt;&#x2F;a&gt; in TypeScript. You can use the Partial type to represent a subsets of the type variable you pass to it.&lt;&#x2F;p&gt;
&lt;p&gt;In an application that communicates with a REST API you ofter find yourself writing interfaces to represent a resource you want to create. If you&#x27;re about to create a new user your interface might look similar to the one below.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;interface &lt;&#x2F;span&gt;&lt;span&gt;User {
&lt;&#x2F;span&gt;&lt;span&gt;  firstName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  lastName&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;  email&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;string&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If you have all the required properties and pass them in a function that expects a User TypeScript is happy and won&#x27;t complain.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;createUser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;User) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;User &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  firstName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Winston&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  lastName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Tortoise&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;  email: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;w.tortoise@example.com&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;createUser&lt;&#x2F;span&gt;&lt;span&gt;(user);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;But now that you have sir Winston in your database and he decides to change his email address. You can&#x27;t just pass the update for a single property into a function that expects a parameter of type User. TypeScript won&#x27;t be happy and will complain.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;User) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;newMail &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;winston.tortoise@example.com&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;({ email: newMail }); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; TypeScript says NO!
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So how to solve this. Instead of creating a second interface to represent a specific user update you could give Partial a try. With the Partial utility type you create a new type from an already existing one but makes all the properties optional. To make this a bit more visual, see the snipper below of how the Partial type is defined in TypeScript.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; * Make all properties in T optional
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; *&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Partial&amp;lt;T&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  [P &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in keyof &lt;&#x2F;span&gt;&lt;span&gt;T]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?: &lt;&#x2F;span&gt;&lt;span&gt;T[P];
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So to make TypeScript happy we use the Partial utility type with &lt;code&gt;User&lt;&#x2F;code&gt; as its generic. By doing this the &lt;code&gt;updateUser&lt;&#x2F;code&gt; will only accept an object with properties in the &lt;code&gt;User&lt;&#x2F;code&gt; type. Take note that all the properties are made optional, so even an empty object will be fine in this case.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Partial&amp;lt;User&amp;gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;newMail &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;winston.tortoise@example.com&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;({ email: newMail }); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; TypeScript is happy
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;require-one-property-in-partial&quot;&gt;Require one property in partial&lt;&#x2F;h3&gt;
&lt;p&gt;Having all the fields optional is in some cases not what you want. For example, sometimes all the fields can be optional expect for an unique identifier you need to identify a specific user. This might not not be the most elegant solution but you could solve this by combining a partial with the Pick utility type. The &lt;code&gt;Pick&lt;&#x2F;code&gt; type requires a subset of properties in a type. See the snippet below of how the &lt;code&gt;Pick&lt;&#x2F;code&gt; type is implemented in TypeScript.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;**
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; * From T, pick a set of properties whose keys are in the union K
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt; *&#x2F;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;type &lt;&#x2F;span&gt;&lt;span&gt;Pick&amp;lt;T, K &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;extends keyof &lt;&#x2F;span&gt;&lt;span&gt;T&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  [P &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;in &lt;&#x2F;span&gt;&lt;span&gt;K]&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;T[P];
&lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the example below shows how to combine Pick with Partial to make all properties of &lt;code&gt;User&lt;&#x2F;code&gt; optional except for the &lt;code&gt;email&lt;&#x2F;code&gt; property.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;function &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;user&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Pick&amp;lt;Partial&amp;lt;User&amp;gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;email&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; ...
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; TypeScript says NO :(
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;({ firstName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hannah&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; TypeScript says Yes! :)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;updateUser&lt;&#x2F;span&gt;&lt;span&gt;({ firstName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Hannah&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, email: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;hannah@example.com&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;});
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;list-of-partials&quot;&gt;List of partials&lt;&#x2F;h3&gt;
&lt;p&gt;Partials can be used with as a list too. By adding a &lt;code&gt;[]&lt;&#x2F;code&gt; after the Partial type TypeScript will expect an array of partials. The items in the array of partials don&#x27;t need to have the same properties. So TypeScript is 100% fine with the example below.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;typescript&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-typescript &quot;&gt;&lt;code class=&quot;language-typescript&quot; data-lang=&quot;typescript&quot;&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;const &lt;&#x2F;span&gt;&lt;span&gt;users&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;Partial&amp;lt;User&amp;gt;[] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;[
&lt;&#x2F;span&gt;&lt;span&gt;  { firstName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;John&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; First user
&lt;&#x2F;span&gt;&lt;span&gt;  { lastName: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;James&amp;quot; &lt;&#x2F;span&gt;&lt;span&gt;}, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;&#x2F;&#x2F; Second user
&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Tagging git commits</title>
		<published>2020-02-25T00:00:00+00:00</published>
		<updated>2020-02-25T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/git-tagging-commits/" type="text/html"/>
		<id>https://koenwoortman.com/git-tagging-commits/</id>
		<content type="html">&lt;p&gt;Git provides the functionality to mark a point in your repository’s history with a &amp;quot;tag&amp;quot;. These tags are typically used to indicate the release of a version. With Git you can create two types of tags: Annotated and Lightweight. These two types differ in how they are stored in the Git repository and the amount of meta data they contain.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;annotated-tags&quot;&gt;Annotated Tags&lt;&#x2F;h3&gt;
&lt;p&gt;Annotated tags are normally what you want when tagging a release. You want this because they have more meta-data attached to them, much like regular commits. They contain a checksum, name, email, date and have a tagging message. Optionally you can sign annotated tags with your GPG key.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;lightweight-tags&quot;&gt;Lightweight Tags&lt;&#x2F;h3&gt;
&lt;p&gt;Lightweight tags point to a commit in a similar way as branches do. The difference being that a branch points to the next commit as soon as you add a commit. Lightweight tags will keep pointing to the commit that was tagged. Lightweight are useful for private use. You can use them like a bookmark for yourself.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;manage-tags&quot;&gt;Manage tags&lt;&#x2F;h2&gt;
&lt;p&gt;Git comes with a whole set of commands to deal with tags. We&#x27;ll go over all the commands you need to manage the tags in your repository.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;create-tags&quot;&gt;Create tags&lt;&#x2F;h3&gt;
&lt;p&gt;You can create a &lt;strong&gt;Lightweight&lt;&#x2F;strong&gt; tag by using the &lt;code&gt;git tag&lt;&#x2F;code&gt; command and provide a name for the tag. This creates a lightweight tag to the last commit it your currently active branch.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag lw-bookmark
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Creating &lt;strong&gt;Annotated&lt;&#x2F;strong&gt; tags is similar to creating lightweight tags. You use the same &lt;code&gt;git tag&lt;&#x2F;code&gt; command but have to add the &lt;code&gt;-a&lt;&#x2F;code&gt; option. Since annotated tags require a message Git will launch an editor for you to enter one. If you prefer you can also use the &lt;code&gt;-m&lt;&#x2F;code&gt; option to specify the message directly.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -a&lt;&#x2F;span&gt;&lt;span&gt; v0.2-annotated
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# or
&lt;&#x2F;span&gt;&lt;span&gt;$ git tag&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -a&lt;&#x2F;span&gt;&lt;span&gt; v0.2-annotated&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Release v0.2&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Both examples tag the current place in time of your Git repository. You can ofcourse also tag previous commits. This can be done by adding the commit hash after the name of the tag.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -a&lt;&#x2F;span&gt;&lt;span&gt; v0.1-annotated&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -m &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Release v0.1&amp;quot;&lt;&#x2F;span&gt;&lt;span&gt; 3f18d02
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;list-tags&quot;&gt;List tags&lt;&#x2F;h3&gt;
&lt;p&gt;To list the tags we just created we can use the &lt;code&gt;git tag&lt;&#x2F;code&gt; command without any arguments. This lists all the lightweight and annotated tags in your repository in alphabetical order.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag
&lt;&#x2F;span&gt;&lt;span&gt;lw-bookmark
&lt;&#x2F;span&gt;&lt;span&gt;v0.1-annotated
&lt;&#x2F;span&gt;&lt;span&gt;v0.2-annotated
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This listing tags gets quickly out of hand in a large Git repository. So it might be useful to list tags with a pattern. For example, if you’re only interested in seeing tags for the v0 releases you can run the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;v0.*&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;v0.1-annotated
&lt;&#x2F;span&gt;&lt;span&gt;v0.2-annotated
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;tag-details&quot;&gt;Tag details&lt;&#x2F;h3&gt;
&lt;p&gt;Listing the tags doesn’t provide a whole lot of details. It is just a list of what is available in your repository. To show more details of a tag you use the &lt;code&gt;git show&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git show v0.2-annotated
&lt;&#x2F;span&gt;&lt;span&gt;tag v0.2-annotated
&lt;&#x2F;span&gt;&lt;span&gt;Tagger: John Developer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;john.developer@example.com&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;Date:   Tue Feb 24 20:56:07 2020 +0100
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Release v0.2
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;commit 321f7dde2c3bc5458c030ee6e9ee3825d91514ca (HEAD -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; master, tag: v0.2-annotated)
&lt;&#x2F;span&gt;&lt;span&gt;Author: John Developer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;john.developer@example.com&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;Date:   Tue Feb 24 20:55:36 2020 +0100
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Add an awesome feature
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As you can see above you can see quite some details of annotated tags. You see details about the tag created and the commit that was tagged. Lightweight tags are a lot less detailed. As you can see below they just show details of the commit that was tagged.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git show lw-bookmark
&lt;&#x2F;span&gt;&lt;span&gt;commit c773c5822b253b6f483e6c63ba73ec55f61faf8f (tag: lw-bookmark)
&lt;&#x2F;span&gt;&lt;span&gt;Author: John Developer &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt;john.developer@example.com&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;Date:   Tue Feb 24 20:51:18 2020 +0100
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    Tiny bug fix
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;push-tags&quot;&gt;Push tags&lt;&#x2F;h3&gt;
&lt;p&gt;When you push changes to a remote server Git doesn&#x27;t include tags by default. If you want to push your tags you have to do this explicitly. To push a single tag to a remote server you run the command &lt;code&gt;git push [remote] [tagname]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git push origin v0.2-annotated
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you have multiple tags to push you can use the &lt;code&gt;--tags&lt;&#x2F;code&gt; option instead. But keep in mind that this will push all your tags, lightweight and annotated.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git push origin&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --tags
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;delete-tags&quot;&gt;Delete tags&lt;&#x2F;h3&gt;
&lt;p&gt;Deleting a tag from just your local repository is straight forward. It works the same for annotated and lightweight tags and you do it by running the command &lt;code&gt;git tag -d [tagname]&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;See the example below where we delete the lightweight tag that we created before.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git tag&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -d&lt;&#x2F;span&gt;&lt;span&gt; lw-bookmark
&lt;&#x2F;span&gt;&lt;span&gt;Deleted tag &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;lw-bookmark&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt; (was c773c58)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you pushed the tag to a remote server before you can delete by passing the &lt;code&gt;--delete&lt;&#x2F;code&gt; option to the Git push command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ git push&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --delete&lt;&#x2F;span&gt;&lt;span&gt; origin lw-bookmark
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Becoming productive in the Ruby IRB console</title>
		<published>2020-02-24T00:00:00+00:00</published>
		<updated>2020-02-24T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-becoming-productive-in-the-irb-console/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-becoming-productive-in-the-irb-console/</id>
		<content type="html">&lt;p&gt;With the release of Ruby 2.7 the IRB shell got somewhat of a make over. It now includes syntax highlighting, multiline editing, tab completion and RDoc integration. And with some tweaks in the &lt;code&gt;.irbrc&lt;&#x2F;code&gt; file we can improve the overall experience even more.&lt;&#x2F;p&gt;
&lt;p&gt;Since Ruby 2.7 the IRB history is by default written to a history file in your home directory. If you&#x27;re on an older version of Ruby you can enable the same behavior using the following snippet. This saves the last 1000 entries in the IRB shell.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;IRB&lt;&#x2F;span&gt;&lt;span&gt;.conf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:SAVE_HISTORY&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;1000
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Ruby versions older than 2.7 also don&#x27;t enable indentation by default. You can change this in the irbrc as well. However, this doesn&#x27;t set the indent correctly for closing &lt;code&gt;end&lt;&#x2F;code&gt;s. Which does work out of the box in Ruby 2.7.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;IRB&lt;&#x2F;span&gt;&lt;span&gt;.conf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:AUTO_INDENT&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;true
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;loading-gems&quot;&gt;Loading gems&lt;&#x2F;h2&gt;
&lt;p&gt;Some gems come in handy to have available in your shell. You can require them interactively during your IRB session. But if you find yourself doing this with the same gems you can require them in the irbrc file as well.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;faraday&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This works nice until you start using different Ruby versions with a tool like &lt;code&gt;rbenv&lt;&#x2F;code&gt;. These different Ruby versions share the same &lt;code&gt;.irbrc&lt;&#x2F;code&gt; but don&#x27;t necessarily have the same gems available. If this happens, getting some notice would be nice. We can do this by using the following snippet in your &lt;code&gt;.irbrc&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;%w{faraday faker}&lt;&#x2F;span&gt;&lt;span&gt;.each &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;lib&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;begin
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require&lt;&#x2F;span&gt;&lt;span&gt; lib
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;rescue &lt;&#x2F;span&gt;&lt;span&gt;LoadError =&amp;gt; err
&lt;&#x2F;span&gt;&lt;span&gt;    $stderr.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; err
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This will give the following message when you start the IRB console.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ irb
&lt;&#x2F;span&gt;&lt;span&gt;cannot load such file&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt; --&lt;&#x2F;span&gt;&lt;span&gt; faker
&lt;&#x2F;span&gt;&lt;span&gt;irb(main):001:0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;helper-functions&quot;&gt;Helper functions&lt;&#x2F;h2&gt;
&lt;p&gt;Since the irbrc file is interpreted as a Ruby file you are free to define all the functions you want. Say for example you get annoyed by typing &lt;code&gt;exit&lt;&#x2F;code&gt; all the time when exiting IRB. You can define a function called &lt;code&gt;q&lt;&#x2F;code&gt; that saves you three characters of your time.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;q
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;👋 See you later alligator&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To use this new time saving function you can just call it from IRB.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;$ irb
&lt;&#x2F;span&gt;&lt;span&gt;irb(main):001:0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;&amp;gt;&lt;&#x2F;span&gt;&lt;span&gt; q
&lt;&#x2F;span&gt;&lt;span&gt;👋 See you later alligator
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;changing-the-prompt&quot;&gt;Changing the prompt&lt;&#x2F;h2&gt;
&lt;p&gt;The IRB console comes with multiple prompt modes built-in, namely &lt;code&gt;:NULL&lt;&#x2F;code&gt;, &lt;code&gt;:DEFAULT&lt;&#x2F;code&gt;, &lt;code&gt;:CLASSIC&lt;&#x2F;code&gt;, &lt;code&gt;:SIMPLE&lt;&#x2F;code&gt;, &lt;code&gt;:INF_RUBY&lt;&#x2F;code&gt; and &lt;code&gt;:XMP&lt;&#x2F;code&gt;. You can try them out and set your preferred one as follows:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;IRB&lt;&#x2F;span&gt;&lt;span&gt;.conf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT_MODE&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:SIMPLE
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can easily create your own prompt mode definition.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# ~&#x2F;.irbrc
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;IRB&lt;&#x2F;span&gt;&lt;span&gt;.conf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT&lt;&#x2F;span&gt;&lt;span&gt;][&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:CUSTOM_PROMPT&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:AUTO_INDENT &lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false&lt;&#x2F;span&gt;&lt;span&gt;,          &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# disables auto-indent mode
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT_I &lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;gt;&amp;gt; &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;,            &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# simple prompt
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT_S &lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;nil&lt;&#x2F;span&gt;&lt;span&gt;,               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# prompt for continuated strings
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT_C &lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;nil&lt;&#x2F;span&gt;&lt;span&gt;,               &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# prompt for continuated statement
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:RETURN &lt;&#x2F;span&gt;&lt;span&gt;=&amp;gt; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;    ==&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;%s\n&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;# format to return value
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;IRB&lt;&#x2F;span&gt;&lt;span&gt;.conf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:PROMPT_MODE&lt;&#x2F;span&gt;&lt;span&gt;] &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:CUSTOM_PROMPT
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;For more information about changing the prompt you can read the docs &lt;a href=&quot;https:&#x2F;&#x2F;ruby-doc.org&#x2F;stdlib-2.7.0&#x2F;libdoc&#x2F;irb&#x2F;rdoc&#x2F;IRB.html#module-IRB-label-Customizing+the+IRB+Prompt&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Command-line interfaces with Ruby OptionParser</title>
		<published>2020-02-18T00:00:00+00:00</published>
		<updated>2020-10-16T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/ruby-command-line-interfaces-with-optionparser/" type="text/html"/>
		<id>https://koenwoortman.com/ruby-command-line-interfaces-with-optionparser/</id>
		<content type="html">&lt;p&gt;The Ruby standard library ships with the OptionParser class for parsing command-line options. Besides just parsing options it can take care of help messages and usage information too. This makes building command-line interfaces a breeze compared to using the ARGV constant.&lt;&#x2F;p&gt;
&lt;p&gt;To access the OptionParser class you need to require &lt;code&gt;optparse&lt;&#x2F;code&gt; at the top of you Ruby file. Don&#x27;t confuse it with a gem. It comes with Ruby, there is no need to install it separately.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You get started by creating an instance of the OptionParser class, pass it a block and call &lt;code&gt;parse!&lt;&#x2F;code&gt; on it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;I found a goldfish&amp;#39;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a goldfish
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Okay, that was pretty useless, a single line of &lt;code&gt;puts&lt;&#x2F;code&gt; would have had the same result. Lets start with adding an option to this fish finder CLI. We can store the option values in a hash by passing in into the parse method.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;params &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;{}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;parser&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;  parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-w&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-h&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--help&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;  parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-v&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--verbose&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;into:&lt;&#x2F;span&gt;&lt;span&gt; params)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; params
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb -v --water salt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; {:verbose=&amp;gt;true, :water=&amp;gt;&amp;quot;salt&amp;quot;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can note a couple things from the above example already:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;You can require options to have an argument by adding a argument name to the option specification. See the &lt;code&gt;--water TYPE&lt;&#x2F;code&gt; specification. If you want an optional argument you would use &lt;code&gt;--water [TYPE]&lt;&#x2F;code&gt; in this case.&lt;&#x2F;li&gt;
&lt;li&gt;Options you don&#x27;t pass to the script are not present in the &lt;code&gt;params&lt;&#x2F;code&gt; hash. Note the absence of &lt;code&gt;:help&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;The long option name is used as a symbol in the &lt;code&gt;params&lt;&#x2F;code&gt; hash.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now before we continue lets wrap this in a fancy FishFinder class.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;FishFinder
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fish
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;I found a goldfish&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;parse
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;parser&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-h&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--help&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-v&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--verbose&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;finder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;FishFinder&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new
&lt;&#x2F;span&gt;&lt;span&gt;finder.parse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; finder.fish
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;print-usage-information&quot;&gt;Print usage information&lt;&#x2F;h2&gt;
&lt;p&gt;The OptionParser class provides methods out of the box to help you create usage information messages. We do this by setting a banner for the parser, which is the first line in the usage output. With the seperator method you can add extra lines of text, or a blank line in this case. In the block that is passed for the &lt;code&gt;--help&lt;&#x2F;code&gt; option you see that &lt;code&gt;puts parser&lt;&#x2F;code&gt; is enough to print the usage information. We exit here as well so we don&#x27;t search for a goldfish while we only want to see the usage information.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;FishFinder
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fish
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;I found a goldfish&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;parse
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;parser&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;      parser.banner &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Usage: fish_finder.rb [options]&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.separator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pick &amp;quot;fresh&amp;quot; or &amp;quot;salt&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-h&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--help&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show this message&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; parser
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-v&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--verbose&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show fancy fish&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;finder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;FishFinder&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new
&lt;&#x2F;span&gt;&lt;span&gt;finder.parse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; finder.fish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb --help
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; Usage: fish_finder.rb [options]
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt;     --water TYPE                 Pick &amp;quot;fresh&amp;quot; or &amp;quot;salt&amp;quot;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; -h, --help                       Show this message
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; -v, --verbose                    Show extra fish
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;boolean-switch-options&quot;&gt;Boolean switch options&lt;&#x2F;h2&gt;
&lt;p&gt;We can use the &lt;code&gt;--verbose&lt;&#x2F;code&gt; flag to overwrite a default configuration. This way we can change the behavior of this fish finder by passing an option. If the &lt;code&gt;--verbose&lt;&#x2F;code&gt; of &lt;code&gt;-v&lt;&#x2F;code&gt; option is passed to the script we reward you with a fancy fish emoji.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;FishFinder
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;attr_accessor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:verbose
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;initialize
&lt;&#x2F;span&gt;&lt;span&gt;    self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fish
&lt;&#x2F;span&gt;&lt;span&gt;    fish &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;🐠&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;goldfish&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;I found a &lt;&#x2F;span&gt;&lt;span&gt;#{fish}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;parse
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;parser&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;      parser.banner &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Usage: fish_finder.rb [options]&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.separator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pick &amp;quot;fresh&amp;quot; or &amp;quot;salt&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-h&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--help&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show this message&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; parser
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-v&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--verbose&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show fancy fish&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;verbose&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;        self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; verbose
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;finder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;FishFinder&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new
&lt;&#x2F;span&gt;&lt;span&gt;finder.parse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; finder.fish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a goldfish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb --verbose
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a 🐠
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;options-with-required-arguments&quot;&gt;Options with required arguments&lt;&#x2F;h2&gt;
&lt;p&gt;By adding an all caps name to the option definition you can define a required argument for an option. This means that an &lt;code&gt;OptionParser::MissingArgument&lt;&#x2F;code&gt; exception is raised when the option is missing. You probably want to catch this and give a nice feedback message.&lt;&#x2F;p&gt;
&lt;p&gt;Now, a second issue might be that you receive an argument that you don&#x27;t expect. In our case we allow for the water types &amp;quot;fresh&amp;quot; and &amp;quot;salt&amp;quot;. We could use the build-in type coercion. So an &lt;code&gt;OptionParser::InvalidArgument&lt;&#x2F;code&gt; exception in thrown when an invalid argument is passed.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;String&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pick &amp;quot;fresh&amp;quot; or &amp;quot;salt&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In the above example you see how that would look. We require the &lt;code&gt;TYPE&lt;&#x2F;code&gt; argument to be a non-empty string. You can take a look at the &lt;a href=&quot;https:&#x2F;&#x2F;ruby-doc.org&#x2F;stdlib-2.7.0&#x2F;libdoc&#x2F;optparse&#x2F;rdoc&#x2F;OptionParser.html#class-OptionParser-label-Type+Coercion&quot;&gt;docs&lt;&#x2F;a&gt; for more type coercion options.&lt;&#x2F;p&gt;
&lt;p&gt;Since our argument values are rather specific we can check for a pattern too. See the final example for a check on salt or fresh water.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;require &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;optparse&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;class &lt;&#x2F;span&gt;&lt;span style=&quot;text-decoration:underline;color:#a6e22e;&quot;&gt;FishFinder
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;attr_accessor &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:verbose&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;:filter
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;initialize
&lt;&#x2F;span&gt;&lt;span&gt;    self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#ae81ff;&quot;&gt;false
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;fish
&lt;&#x2F;span&gt;&lt;span&gt;    fish &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span&gt;self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;? &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;🐠&amp;#39; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;: &lt;&#x2F;span&gt;&lt;span&gt;self.find_fish
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;I found a &lt;&#x2F;span&gt;&lt;span&gt;#{fish}&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;find_fish
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;self.filter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;fresh&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;goldfish&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;elsif &lt;&#x2F;span&gt;&lt;span&gt;self.filter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;== &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;salt&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;tuna&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;else
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;sturgeon&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;def &lt;&#x2F;span&gt;&lt;span style=&quot;color:#a6e22e;&quot;&gt;parse
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;OptionParser&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;parser&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;      parser.banner &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;Usage: fish_finder.rb [options]&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.separator &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--water TYPE&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&#x2F;fresh|salt&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pick &amp;quot;fresh&amp;quot; or &amp;quot;salt&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;water&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;        self.filter &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; water
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-h&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--help&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show this message&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; parser
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;exit
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;      parser.on(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;-v&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;--verbose&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Show extra fish&amp;#39;&lt;&#x2F;span&gt;&lt;span&gt;) &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;do &lt;&#x2F;span&gt;&lt;span&gt;|&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt;v&lt;&#x2F;span&gt;&lt;span&gt;|
&lt;&#x2F;span&gt;&lt;span&gt;        self.verbose &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt; v
&lt;&#x2F;span&gt;&lt;span&gt;      &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end&lt;&#x2F;span&gt;&lt;span&gt;.parse!
&lt;&#x2F;span&gt;&lt;span&gt;  &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;end
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;finder &lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;= &lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#66d9ef;&quot;&gt;FishFinder&lt;&#x2F;span&gt;&lt;span&gt;.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;new
&lt;&#x2F;span&gt;&lt;span&gt;finder.parse
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;puts&lt;&#x2F;span&gt;&lt;span&gt; finder.fish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb --water salt
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a tuna
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb --water fresh
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a goldfish
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; $ ruby fish_finder.rb
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#75715e;&quot;&gt;#=&amp;gt; I found a sturgeon
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Bash Tutorial - Starting with Bash shell basics</title>
		<published>2020-02-15T00:00:00+00:00</published>
		<updated>2020-10-28T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/starting-with-bash-shell-basics/" type="text/html"/>
		<id>https://koenwoortman.com/starting-with-bash-shell-basics/</id>
		<content type="html">&lt;p&gt;The command-line can seem quite daunting at first. There is no holding hands as provided by a graphical file browser for example. But when you get up to speed with it, there is no looking back.&lt;&#x2F;p&gt;
&lt;p&gt;On most Unix systems the the program that act as the command-line is the Bash shell. On the command-line you feed commands to Bash, and Bash makes sure the related program runs.&lt;&#x2F;p&gt;
&lt;p&gt;You can best view the shell as an interface to your operating system. It is a program that translates your input to instructions for the operating system.&lt;&#x2F;p&gt;
&lt;p&gt;Bash is common but not your only choice for a command-line shell. These days Zsh and &lt;a href=&quot;https:&#x2F;&#x2F;fishshell.com&#x2F;&quot;&gt;Fish&lt;&#x2F;a&gt; are popular as well. But those shells will not be covered here.&lt;&#x2F;p&gt;
&lt;p&gt;This tutorial goes into the basics of interacting with the command-line. After reading you should feel comfortable enough to use it for basic operations. Lets get started.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Content&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;koenwoortman.com&#x2F;starting-with-bash-shell-basics&#x2F;#navigate-through-the-file-system&quot;&gt;The command prompt&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;koenwoortman.com&#x2F;starting-with-bash-shell-basics&#x2F;#navigate-through-the-file-system&quot;&gt;Navigate through the file system&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;koenwoortman.com&#x2F;starting-with-bash-shell-basics&#x2F;#files-and-directories&quot;&gt;Files and directories&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;koenwoortman.com&#x2F;starting-with-bash-shell-basics&#x2F;#permissions&quot;&gt;Permissions&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;h2 id=&quot;the-command-prompt&quot;&gt;The command prompt&lt;&#x2F;h2&gt;
&lt;p&gt;The command prompt is the first notable thing you see when you open your terminal or console application. It will probably look somewhat like this:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can customize this to all your likings, so it is not guaranteed to look like this. But most likely it shows:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Your username on the system, &lt;code&gt;koen&lt;&#x2F;code&gt; in my case.&lt;&#x2F;li&gt;
&lt;li&gt;The name that is given to your system, this is called the hostname.&lt;&#x2F;li&gt;
&lt;li&gt;The current directory shown as a &lt;code&gt;~&lt;&#x2F;code&gt; in this example, we get into that later.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;Now that we dissected that. Lets start with what the command-line is for: running commands.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;How Do Fish Breathe Underwater?&amp;quot;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The &lt;code&gt;echo&lt;&#x2F;code&gt; command we ran here is builtin to Bash. Its basic usage is to print out text that you supplied it as argument. You see this happening when you press Enter. The argument, between quotes, was written back to you by Bash on the next line.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ echo &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;quot;How Do Fish Breathe Underwater?&amp;quot;
&lt;&#x2F;span&gt;&lt;span&gt;How Do Fish Breathe Underwater&lt;&#x2F;span&gt;&lt;span style=&quot;color:#f92672;&quot;&gt;?
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Now that we covered running commands. Lets take a look at what that &lt;code&gt;~&lt;&#x2F;code&gt; meant.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;navigate-through-the-file-system&quot;&gt;Navigate through the file system&lt;&#x2F;h2&gt;
&lt;p&gt;When you open your terminal you are likely in the &amp;quot;Home&amp;quot; directory of your user. It is practically the same as opening a graphical file browser. But with text just text instead of icons for folders and files.&lt;&#x2F;p&gt;
&lt;p&gt;To see what the actual location on the file system is that you&#x27;re at you use the &lt;code&gt;pwd&lt;&#x2F;code&gt; command. Easily to remember as &lt;u&gt;P&lt;&#x2F;u&gt;rint the name of my current &lt;u&gt;W&lt;&#x2F;u&gt;orking &lt;u&gt;D&lt;&#x2F;u&gt;irectory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ pwd
&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;home&#x2F;koen
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;What this shows is that I am in a folder called &lt;code&gt;koen&lt;&#x2F;code&gt;, which is in a folder called &lt;code&gt;home&lt;&#x2F;code&gt;, which is in de root of the file system. The root of the file system is indicated with a &lt;code&gt;&#x2F;&lt;&#x2F;code&gt; at the beginning of the path.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;listing-files-and-directories&quot;&gt;Listing files and directories&lt;&#x2F;h3&gt;
&lt;p&gt;It is nice to have a sense of where we are. It is even nicer to known what is actually there. For that use-case we have the &lt;code&gt;ls&lt;&#x2F;code&gt; command to &lt;u&gt;l&lt;&#x2F;u&gt;i&lt;u&gt;s&lt;&#x2F;u&gt;t files and directories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls
&lt;&#x2F;span&gt;&lt;span&gt;Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;This displayed all the contents that are in the directory &lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt;, which are in this case only folders and not files.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;command-options&quot;&gt;Command options&lt;&#x2F;h3&gt;
&lt;p&gt;With the use of &amp;quot;options&amp;quot; the &lt;code&gt;ls&lt;&#x2F;code&gt; command can come in many forms. Like most commands actually. You can modify the output or behavior of commands by appending these options. By passing the option &lt;code&gt;-l&lt;&#x2F;code&gt; for example you get more detailed information about files and folders.&lt;&#x2F;p&gt;
&lt;p&gt;Options come as words or as single letters. Single letter options use a single &lt;code&gt;-&lt;&#x2F;code&gt; to indicate an option, &lt;code&gt;-l&lt;&#x2F;code&gt; in this case. If you don&#x27;t use a &lt;code&gt;-&lt;&#x2F;code&gt; it is interpreted as a argument. Options that are full words use &lt;code&gt;--&lt;&#x2F;code&gt;, an example would be &lt;code&gt;ls --version&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;In this case the &lt;code&gt;-l&lt;&#x2F;code&gt; option of the &lt;code&gt;ls&lt;&#x2F;code&gt; command causes the file list to be more &amp;quot;list like&amp;quot; and it shows some scary things as well. Like &lt;code&gt;drwxr-xr-x&lt;&#x2F;code&gt;, these are file permissions which we will cover later.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;total 48
&lt;&#x2F;span&gt;&lt;span&gt;drwxrwxr-x 43 koen koen  4096 sep 22 10:37 Code
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen  4096 aug 22 09:32 Desktop
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  8 koen koen  4096 okt 18 12:35 Documents
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen 12288 okt 18 12:35 Downloads
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen  4096 mei 20 23:58 Music
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen 12288 okt 14 13:31 Pictures
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x 19 koen koen  4096 sep 15 14:07 snap
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen  4096 jun  1 19:42 Videos
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;listing-files-in-a-specific-folder&quot;&gt;Listing files in a specific folder&lt;&#x2F;h3&gt;
&lt;p&gt;Now that we have a sense of where we are. We are not just bound to exploring the contents of the current folder. By passing an argument to &lt;code&gt;ls&lt;&#x2F;code&gt; we can just as well show the contents of other folders.&lt;&#x2F;p&gt;
&lt;p&gt;To see what is hiding in the Downloads folder we pass &lt;code&gt;Downloads&lt;&#x2F;code&gt; as an argument to &lt;code&gt;ls&lt;&#x2F;code&gt;. Keep in mind here that Bash is case-sensitive. So &lt;code&gt;ls downloads&lt;&#x2F;code&gt; is &lt;strong&gt;not&lt;&#x2F;strong&gt; the same as &lt;code&gt;ls Downloads&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls Downloads
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza: A Global History.pdf&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Right that is a must-read indeed.&lt;&#x2F;p&gt;
&lt;p&gt;You should have TAB completion for this. So as soon as you typed &lt;code&gt;ls Dow&lt;&#x2F;code&gt; you should be able to press TAB, and Bash will autocomplete. This TAB completion works if there is one unique match for your argument. So if you would type &lt;code&gt;ls Do&lt;&#x2F;code&gt; and press TAB Bash nothing happens. Since both &amp;quot;Downloads&amp;quot; and &amp;quot;Documents&amp;quot; start with &amp;quot;Do&amp;quot;. If you press TAB twice though Bash will show the matching suggestions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls Do
&lt;&#x2F;span&gt;&lt;span&gt;Documents&#x2F; Downloads&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;moving-around&quot;&gt;Moving around&lt;&#x2F;h3&gt;
&lt;p&gt;That listing the files in the &lt;code&gt;Downloads&lt;&#x2F;code&gt; folder could have been done in a two-step process as well. By moving into that folder and list the files and directories from there.&lt;&#x2F;p&gt;
&lt;p&gt;To move around we use &lt;code&gt;cd&lt;&#x2F;code&gt; to &lt;u&gt;c&lt;&#x2F;u&gt;hange &lt;u&gt;d&lt;&#x2F;u&gt;irectory.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ cd Downloads
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~&#x2F;Downloads$ ls
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;Pizza: A Global History.pdf&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Notice that for the first time the command didn&#x27;t output anything. In many cases in Bash, no output is good output. For commands like &lt;code&gt;cd&lt;&#x2F;code&gt; you generally only see output when an error occurs. Like when you try to move into a non-existing directory for example.&lt;&#x2F;p&gt;
&lt;p&gt;You might have noticed that the prompt changed as well. Instead of &lt;code&gt;~&lt;&#x2F;code&gt; now &lt;code&gt;~&#x2F;Downloads&lt;&#x2F;code&gt; is shown. As mentioned earlier the prompt shows the current working directory. That tilde(&lt;code&gt;~&lt;&#x2F;code&gt;) character is an abbreviation for your &amp;quot;Home&amp;quot; directory, so &lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt; is my case.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;absolute-and-relative-paths&quot;&gt;Absolute and relative paths&lt;&#x2F;h3&gt;
&lt;p&gt;To move back to the &amp;quot;Home&amp;quot; directly you can use &lt;code&gt;cd &#x2F;home&#x2F;koen&lt;&#x2F;code&gt;, if your user is called &amp;quot;koen&amp;quot; at least. The &lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt; part is what is called an &lt;strong&gt;absolute path&lt;&#x2F;strong&gt; because starts from the root of the file system(&lt;code&gt;&#x2F;&lt;&#x2F;code&gt;) till where you want to end up. This is in contrary to navigation with &lt;strong&gt;relative paths&lt;&#x2F;strong&gt; like the double dots we saw earlier. Where you navigate relative from your current working directory or relative from your home directory.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;going-up-a-directory&quot;&gt;Going up a directory&lt;&#x2F;h3&gt;
&lt;p&gt;To get back in our home directory again for the &amp;quot;Downloads&amp;quot; we have to go one directory &amp;quot;up&amp;quot;. Therefor we use &lt;code&gt;cd ..&lt;&#x2F;code&gt;, the &lt;code&gt;..&lt;&#x2F;code&gt; reference the parent directory of the current directory. So &lt;code&gt;cd ..&lt;&#x2F;code&gt; would translate to: &lt;em&gt;change directory to the parent directory of my current working directory&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Going from &lt;code&gt;&#x2F;home&#x2F;koen&#x2F;Downloads&lt;&#x2F;code&gt; to &lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt; again.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~&#x2F;Downloads$ cd ..
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;returning-home&quot;&gt;Returning home&lt;&#x2F;h3&gt;
&lt;p&gt;Since going to your home directory is quite common this is the default for &lt;code&gt;cd&lt;&#x2F;code&gt; if you don&#x27;t provide it an argument. So instead of the &lt;code&gt;cd ..&lt;&#x2F;code&gt; from above you could actually achieve the same with just &lt;code&gt;cd&lt;&#x2F;code&gt; in this case.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;more-on-cd&quot;&gt;More on &lt;code&gt;cd&lt;&#x2F;code&gt;&lt;&#x2F;h3&gt;
&lt;p&gt;Since you&#x27;re gonna use &lt;code&gt;cd&lt;&#x2F;code&gt; lots of times. Here is a little overview of useful commands. They are assumed to be ran from the home directory for this example.&lt;&#x2F;p&gt;
&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Command&lt;&#x2F;th&gt;&lt;th align=&quot;center&quot;&gt;Where you end up&lt;&#x2F;th&gt;&lt;th&gt;Explanation&lt;&#x2F;th&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;thead&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Your home directory is the default for &lt;code&gt;cd&lt;&#x2F;code&gt;, so wherever you run this you return home.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd ~&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;The &lt;code&gt;~&lt;&#x2F;code&gt; is a shortcut for your home directory, you can use it in commands as well.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd ..&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;You go one directory up in the file system tree.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd ..&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;You go one directory up in the file system tree and from here you go in the directory &lt;code&gt;koen&lt;&#x2F;code&gt;.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd Downloads&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&#x2F;Downloads&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Go into the &lt;code&gt;Downloads&lt;&#x2F;code&gt; folder from your current directory.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd ~&#x2F;Downloads&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&#x2F;Downloads&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Go into the &lt;code&gt;Downloads&lt;&#x2F;code&gt; folder from your home directory.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;code&gt;cd &#x2F;home&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td align=&quot;center&quot;&gt;&lt;code&gt;&#x2F;home&#x2F;koen&lt;&#x2F;code&gt;&lt;&#x2F;td&gt;&lt;td&gt;Go to your home directory by using an absolute path.&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;
&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;
&lt;h2 id=&quot;files-and-directories&quot;&gt;Files and directories&lt;&#x2F;h2&gt;
&lt;p&gt;In the previous section we moved around in directories that were already there. In this section we learn to make our own directories and files using the shell and manipulate them.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;creating-files-and-directories&quot;&gt;Creating files and directories&lt;&#x2F;h3&gt;
&lt;p&gt;To make a new directory you use &lt;code&gt;mkdir&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ mkdir juice
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;As mentioned before, ofter no news is good news with Bash. We don&#x27;t actually get feedback that the directory is created but it is there, as we check with &lt;code&gt;ls&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls
&lt;&#x2F;span&gt;&lt;span&gt;Code  Desktop  Documents  Downloads  juice  Music  Pictures  snap  Videos
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;To give an example if bad news. You can move one directory up, outside of your home directory. If you try to make a directory here you normally see an error message. We get into &lt;a href=&quot;https:&#x2F;&#x2F;koenwoortman.com&#x2F;starting-with-bash-shell-basics&#x2F;#permissions&quot;&gt;permissions&lt;&#x2F;a&gt; later, but as the message says your user does not have permissions to create a directory here.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ cd ..
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:&#x2F;home$ mkdir lemonjuice
&lt;&#x2F;span&gt;&lt;span&gt;mkdir: cannot create directory ‘lemonjuice’: Permission denied
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets get back to the safety of our home folder before we continue.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:&#x2F;home$ cd
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Directories are pretty useless when they are empty. So lets fill one we created earlier with some juice recipes. For creating files we can use the &lt;code&gt;touch&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ touch juice&#x2F;apple-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Again no news is good news. We can speed things up a bit and create multiple files at once by passing multiple arguments.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ touch juice&#x2F;tomato-juiec.txt juice&#x2F;banana-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;If we now list the contents of the juice folder with &lt;code&gt;ls&lt;&#x2F;code&gt; we see the files we created with &lt;code&gt;touch&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls juice&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;apple-juice.txt  banana-smoothie.txt  tomato-juiec.txt
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;moving-files&quot;&gt;Moving files&lt;&#x2F;h3&gt;
&lt;p&gt;The command to move files and directories is &lt;code&gt;mv&lt;&#x2F;code&gt;. You use it to move files and directories around. Like how you would drag files and folders around in a graphical file browser.&lt;&#x2F;p&gt;
&lt;p&gt;To move a file or directory you pass two arguments to the &lt;code&gt;mv&lt;&#x2F;code&gt; command. First you specify what you want to move and second where you want to move it to.&lt;&#x2F;p&gt;
&lt;p&gt;Another practical use-case is renaming files so lets use the &lt;code&gt;mv&lt;&#x2F;code&gt; command to fix that typo I made before.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ mv juice&#x2F;tomato-juiec.txt juice&#x2F;tomato-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$ ls juice&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;apple-juice.txt  banana-smoothie.txt  tomato-juice.txt
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Lets say that we want to categorize a bit more. We have a smoothie in there which is sorta juice but also sorta not. To do this we&#x27;ll add a subdirectory to the juice folder and move the &lt;code&gt;banana-smoothie.txt&lt;&#x2F;code&gt; file in there.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ mkdir juice&#x2F;smoothie
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$ mv juice&#x2F;banana-smoothie.txt juice&#x2F;smoothie&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Where before we had to specify the file name to move it. Here the destination we passed to the &lt;code&gt;mv&lt;&#x2F;code&gt; is a directory. To check whether this worked we can use the &lt;code&gt;tree&lt;&#x2F;code&gt; command. This command lists directory structures. By default it is might not available though.&lt;&#x2F;p&gt;
&lt;p&gt;To check the files and folders in the juice folder we pass it as an argument to the &lt;code&gt;tree&lt;&#x2F;code&gt; command and see for yourself. In the juice folder there now is a smoothie folder containing the &lt;code&gt;banana-smoothie.txt&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ tree juice&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;juice&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;├── apple-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;├── smoothie
&lt;&#x2F;span&gt;&lt;span&gt;│   └── banana-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;└── tomato-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;1 directory, 3 files
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;copying-files-and-directories&quot;&gt;Copying files and directories&lt;&#x2F;h3&gt;
&lt;p&gt;Lets say I want to make banana-kiwi smoothies too. Which are pretty similar to banana smoothies so it would be a waste of time to rewrite the whole file. This is where to &lt;code&gt;cp&lt;&#x2F;code&gt; command comes in handy.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ cd juice&#x2F;smoothie&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~&#x2F;juice&#x2F;smoothie$ cp banana-smoothie.txt banana-kiwi-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~&#x2F;juice&#x2F;smoothie$ ls
&lt;&#x2F;span&gt;&lt;span&gt;banana-kiwi-smoothie.txt  banana-smoothie.txt
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Look at that, super easy. And it works for directories too. However, when you want to copy a directory you need to use the &lt;code&gt;-r&lt;&#x2F;code&gt; option which stands for recursively. Which basically means, copy the folder and its contents to a new location.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~&#x2F;juice$ cp&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -r&lt;&#x2F;span&gt;&lt;span&gt; smoothie&#x2F; healthy
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~&#x2F;juice$ tree
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#66d9ef;&quot;&gt;.
&lt;&#x2F;span&gt;&lt;span&gt;├── apple-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;├── healthy
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── banana-kiwi-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;│   └── banana-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;├── smoothie
&lt;&#x2F;span&gt;&lt;span&gt;│   ├── banana-kiwi-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;│   └── banana-smoothie.txt
&lt;&#x2F;span&gt;&lt;span&gt;└── tomato-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;2 directories, 6 files
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;removing-danger&quot;&gt;Removing, danger!&lt;&#x2F;h3&gt;
&lt;p&gt;Okay lets face it. I am not gonna keep the juice folder and its files. So time to learn about removing files with the &lt;code&gt;rm&lt;&#x2F;code&gt; command. Some caution is necessary here. In Bash you don&#x27;t have the safety net of a Thrash folder. Once it&#x27;s gone it&#x27;s gone.&lt;&#x2F;p&gt;
&lt;p&gt;Lets start with tomato juice since it turns out to be not very tasty. So it deserves a special treatment.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~&#x2F;juice$ rm tomato-juice.txt
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~&#x2F;juice$ ls
&lt;&#x2F;span&gt;&lt;span&gt;apple-juice.txt  healthy  smoothie
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;You can&#x27;t remove the directory you are currently in. Lets go back to your &amp;quot;Home&amp;quot; directory and remove the juices folder all together. Since we are now dealing with a directory we need to use the same &lt;code&gt;-r&lt;&#x2F;code&gt; option as for copying.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~&#x2F;juice$ cd
&lt;&#x2F;span&gt;&lt;span&gt;koen@helios:~$ rm&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -r&lt;&#x2F;span&gt;&lt;span&gt; juice&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;So the juices folder is gone now and there is no way to get it back unless we had a backup.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;hidden-files&quot;&gt;Hidden files&lt;&#x2F;h3&gt;
&lt;p&gt;A special kind of files and directories are hidden ones. Since they are hidden you might not have noticed them. Hidden files start with a &lt;code&gt;.&lt;&#x2F;code&gt;. Mostly these files are created or used by program to store data or configurations.&lt;&#x2F;p&gt;
&lt;p&gt;Bash creates these files as well, for example the &lt;code&gt;.bash_history&lt;&#x2F;code&gt; file in which your latest commands are stored. You can make these hidden files appear by adding the &lt;code&gt;-a&lt;&#x2F;code&gt; option to the &lt;code&gt;ls&lt;&#x2F;code&gt; command.&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#272822;color:#f8f8f2;&quot;&gt;&lt;code&gt;&lt;span&gt;koen@helios:~$ ls -al
&lt;&#x2F;span&gt;&lt;span&gt;total 1040
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x 35 koen koen   4096 okt 18 13:58 .
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  3 root root   4096 mei 31 12:44 ..
&lt;&#x2F;span&gt;&lt;span&gt;-rw-------  1 koen koen     11 jun 12 16:42 .bash_history
&lt;&#x2F;span&gt;&lt;span&gt;-rw-------  1 koen koen     11 jun 12 16:42 .bashrc
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x 30 koen koen   4096 okt 18 09:44 .cache
&lt;&#x2F;span&gt;&lt;span&gt;drwxrwxr-x 43 koen koen   4096 sep 22 10:37 Code
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x 45 koen koen   4096 sep 29 14:15 .config
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;span&gt;drwxr-xr-x  2 koen koen   4096 jun  1 19:42 Videos
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;permissions&quot;&gt;Permissions&lt;&#x2F;h2&gt;
&lt;p&gt;Linux and pretty much all other operating systems are set up to support multiple users. So for security reasons it makes sense that not all users can access all files.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;users-and-groups&quot;&gt;Users and groups&lt;&#x2F;h3&gt;
&lt;p&gt;As we saw before, in the output of the &lt;code&gt;ls -l&lt;&#x2F;code&gt; command my name was in there twice. The first appearance on &lt;code&gt;koen&lt;&#x2F;code&gt; indicates that the file is owned by a user called &lt;code&gt;koen&lt;&#x2F;code&gt;. The second appearance is the name of a user group, in my case the group has the same name as my user.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ ls&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -l
&lt;&#x2F;span&gt;&lt;span&gt;total 52
&lt;&#x2F;span&gt;&lt;span&gt;drwxrwxr-x 43 koen koen  4096 sep 22 10:37 Code
&lt;&#x2F;span&gt;&lt;span&gt;...
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;file-permissions&quot;&gt;File permissions&lt;&#x2F;h3&gt;
&lt;p&gt;Why those user groups are useful have to do with file permissions. The permissions as shown using the somewhat cryptic &lt;code&gt;drwxrwxr-x&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The first character in &lt;code&gt;drwxrwxr-x&lt;&#x2F;code&gt; indicates whether we are looking at a file or directory. In this case the first character is a &lt;code&gt;d&lt;&#x2F;code&gt;, for directory. For files a hyphen(&lt;code&gt;-&lt;&#x2F;code&gt;) is used.&lt;&#x2F;p&gt;
&lt;p&gt;The other characters are actually a repetition of three times three characters that specify the permissions. The characters are &lt;code&gt;r&lt;&#x2F;code&gt;, &lt;code&gt;w&lt;&#x2F;code&gt; and &lt;code&gt;x&lt;&#x2F;code&gt; and they mean the following:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;Read (r) permissions for the file or directory. This means you can view the file contents if it is a file or list the contents of the directory if it is a directory.&lt;&#x2F;li&gt;
&lt;li&gt;Write (w) permissions to the file or directory. Meaning you can change the content of the file. Or in case of a directory, you can modify its content like deleting files.&lt;&#x2F;li&gt;
&lt;li&gt;Execute (x) permissions. Meaning you can execute the file, like using it as a command. Or &lt;code&gt;cd&lt;&#x2F;code&gt; into it if it&#x27;s a directory.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;This sequence of three characters is repeated three times to indicate the permissions for the owner, user group and the others. Seeing the character means the permission is applicable, if you see a hyphen it means you don&#x27;t have that permission.&lt;&#x2F;p&gt;
&lt;p&gt;So to use &lt;code&gt;drwxrwxr-x&lt;&#x2F;code&gt; as an example. We covered &lt;code&gt;d&lt;&#x2F;code&gt; already. Next is &lt;code&gt;rwx&lt;&#x2F;code&gt; for the file owner. This means that the file owner has read, write and executable permissions for this directory. Next is again &lt;code&gt;rwx&lt;&#x2F;code&gt;, meaning that all users belonging to the group have read, write and executable permissions as well. At last is &lt;code&gt;r-x&lt;&#x2F;code&gt;, these are permissions for all users that are not the file owner and do not belong to the user group. In this case all other users have permissions to read and execute. &lt;strong&gt;Not&lt;&#x2F;strong&gt; to write because at the position of the &lt;code&gt;w&lt;&#x2F;code&gt; a hyphen is shown.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;root-user&quot;&gt;Root user&lt;&#x2F;h3&gt;
&lt;p&gt;There is one special user which is called &lt;code&gt;root&lt;&#x2F;code&gt;. This user is a superuser account which have more access rights. You use this user mostly to do administrative tasks, like installing a new package for example.&lt;&#x2F;p&gt;
&lt;p&gt;Most commonly to do so is by using a program called &lt;code&gt;sudo&lt;&#x2F;code&gt;. Which allow you to do something as super user. You put this in front of the command you would like to execute. Like this for example, you are most likely prompted for you password when you run it.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;bash&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-bash &quot;&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span&gt;koen@helios:~$ sudo mkdir &#x2F;home&#x2F;test
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
	<entry xml:lang="en">
		<title>Zypper cheat sheet</title>
		<published>2020-02-11T00:00:00+00:00</published>
		<updated>2020-02-11T00:00:00+00:00</updated>
		<link href="https://koenwoortman.com/zypper-cheat-sheet/" type="text/html"/>
		<id>https://koenwoortman.com/zypper-cheat-sheet/</id>
		<content type="html">&lt;p&gt;Zypper is the package manager of OpenSUSE and SUSE Linux. The basics are pretty easy to remember but sometimes a cheat sheet is just handy.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;package-management&quot;&gt;Package Management&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;zypper-install-in&quot;&gt;zypper install (in)&lt;&#x2F;h3&gt;
&lt;p&gt;Install a new package, if the specified package is already installed it will be updated.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper install php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same, but using the shorthand &lt;code&gt;in&lt;&#x2F;code&gt; notation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper in php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In case you need a specific version of a package you can add preferred version number right after the package name followed by an equal sign.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper in zypper=1.14.30
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;In OpenSUSE packages can be installed as pattern too. Patterns in OpenSUSE are basically a group of related packages that are required to run a specific piece of software. Like installing the desktop environment &lt;code&gt;xfce&lt;&#x2F;code&gt; for example, which requires other packages such as the X Window System.&lt;&#x2F;p&gt;
&lt;p&gt;Instead of installing these packages individually, all of these packages can all be easily installed as a pattern by the following command.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper in&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; -t&lt;&#x2F;span&gt;&lt;span&gt; pattern xfce
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-remove-rm&quot;&gt;zypper remove (rm)&lt;&#x2F;h3&gt;
&lt;p&gt;Uninstall the specified package and their, possible, dependent packages.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper rm php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The same, but using the shorthand &lt;code&gt;rm&lt;&#x2F;code&gt; notation.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper rm php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-info-if&quot;&gt;zypper info (if)&lt;&#x2F;h3&gt;
&lt;p&gt;Displays detailed information about the specified packages.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper if php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Show symbols the package requires.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper if&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --requires&lt;&#x2F;span&gt;&lt;span&gt; php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-verify-ve&quot;&gt;zypper verify (ve)&lt;&#x2F;h3&gt;
&lt;p&gt;Check whether the dependencies of installed packages on your system are satisfied and possibly installing missing dependencies.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper ve
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;update-management&quot;&gt;Update Management&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;zypper-update-up&quot;&gt;zypper update (up)&lt;&#x2F;h3&gt;
&lt;p&gt;Update installed packages with newer versions.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper up
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Update a specific package with a newer version.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper up php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-list-updates-lu&quot;&gt;zypper list-updates (lu)&lt;&#x2F;h3&gt;
&lt;p&gt;List installable updates.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper lu
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-dist-upgrade-dup&quot;&gt;zypper dist-upgrade (dup)&lt;&#x2F;h3&gt;
&lt;p&gt;Perform a distribution upgrade.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper dup
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;query-commands&quot;&gt;Query Commands&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;zypper-search-se&quot;&gt;zypper search (se)&lt;&#x2F;h3&gt;
&lt;p&gt;Search for packages matching any of the given search strings.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper se mysql
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Search using a wildcard.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper se &lt;&#x2F;span&gt;&lt;span style=&quot;color:#e6db74;&quot;&gt;&amp;#39;yast*&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Search for packages that provide a specific binary.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper se&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --provides --match-exact&lt;&#x2F;span&gt;&lt;span&gt; npm
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-packages-pa&quot;&gt;zypper packages (pa)&lt;&#x2F;h3&gt;
&lt;p&gt;Show packages which are orphaned (without repository).&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper pa&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --orphaned
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Show packages which are unneeded.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper pa&lt;&#x2F;span&gt;&lt;span style=&quot;font-style:italic;color:#fd971f;&quot;&gt; --unneeded
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-patterns-pt&quot;&gt;zypper patterns (pt)&lt;&#x2F;h3&gt;
&lt;p&gt;List all available patterns or all patterns from specified repositories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper pt
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;repository-management&quot;&gt;Repository Management&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;zypper-addrepo-ar&quot;&gt;zypper addrepo (ar)&lt;&#x2F;h3&gt;
&lt;p&gt;Add a new repository specified.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper ar
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-removerepo-rr&quot;&gt;zypper removerepo (rr)&lt;&#x2F;h3&gt;
&lt;p&gt;Delete repositories specified.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper rr
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-repos-lr&quot;&gt;zypper repos (lr)&lt;&#x2F;h3&gt;
&lt;p&gt;List all defined repositories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper lr
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-refresh-ref&quot;&gt;zypper refresh (ref)&lt;&#x2F;h3&gt;
&lt;p&gt;Refresh repositories specified by their alias, name, number, or URI.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper ref
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-clean-cc&quot;&gt;zypper clean (cc)&lt;&#x2F;h3&gt;
&lt;p&gt;Clean the local caches for all known or specified repositories.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper cc
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;package-locks&quot;&gt;Package Locks&lt;&#x2F;h2&gt;
&lt;h3 id=&quot;zypper-locks-ll&quot;&gt;zypper locks (ll)&lt;&#x2F;h3&gt;
&lt;p&gt;List currently active package locks.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper ll
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-addlock-al&quot;&gt;zypper addlock (al)&lt;&#x2F;h3&gt;
&lt;p&gt;Add a package lock.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper al php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;zypper-removelock-rl&quot;&gt;zypper removelock (rl)&lt;&#x2F;h3&gt;
&lt;p&gt;Remove specified package lock.&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;sh&quot; style=&quot;background-color:#272822;color:#f8f8f2;&quot; class=&quot;language-sh &quot;&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;$ zypper rm php7
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
</content>
	</entry>
</feed>
