<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Learneds | DCMST</title><link>https://portfolio.devcrumbs.com/learned/</link><atom:link href="https://portfolio.devcrumbs.com/learned/index.xml" rel="self" type="application/rss+xml"/><description>Learneds</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><lastBuildDate>Mon, 22 May 2023 00:00:00 +0000</lastBuildDate><image><url>https://portfolio.devcrumbs.com/media/icon_hu28290437db960aa4e7d19bb9f7230401_6937_512x512_fill_lanczos_center_3.png</url><title>Learneds</title><link>https://portfolio.devcrumbs.com/learned/</link></image><item><title>Overwriting dictionary key</title><link>https://portfolio.devcrumbs.com/learned/overwriting-dictionary-keys/</link><pubDate>Mon, 22 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/overwriting-dictionary-keys/</guid><description>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">mylist = [&amp;#39;word1&amp;#39;, &amp;#39;word2&amp;#39;, &amp;#39;word3&amp;#39;]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">def get_word_by_type(word_list):
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> pokedex = {}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for word_name in word_list:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> url = f&amp;#39;https://myapi.co/api/word/{word_name}&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> response = requests.get(url)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> weight = response.json()[&amp;#39;weight&amp;#39;]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> abilities = []
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for ability in response.json()[&amp;#39;abilities&amp;#39;]:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> abilities.append(ability[&amp;#39;ability&amp;#39;][&amp;#39;name&amp;#39;].title())
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> for t in response.json()[&amp;#39;types&amp;#39;]:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> worddex.setdefault(t[&amp;#39;type&amp;#39;][&amp;#39;name&amp;#39;], {})[word_name] = {&amp;#39;Language&amp;#39;: language, &amp;#39;Length&amp;#39; : length}}
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> return worddex
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">get_word_by_type(mylist)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Shallow clone with Git</title><link>https://portfolio.devcrumbs.com/learned/shallow-clone-git/</link><pubDate>Fri, 19 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/shallow-clone-git/</guid><description>&lt;p>If you only want to check the latest states, without downloading anything:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">git clone -–depth &amp;lt;depth in numbers&amp;gt; &amp;lt;url&amp;gt;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Also for a single branch:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">git clone &amp;lt;url&amp;gt; --branch &amp;lt;branch-to-clone&amp;gt; --single-branch &lt;span class="o">[&lt;/span>folder-to-store&lt;span class="o">]&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Convert and merge xlsx files into a db</title><link>https://portfolio.devcrumbs.com/learned/merge-convert-xlsx-db/</link><pubDate>Wed, 17 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/merge-convert-xlsx-db/</guid><description>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">#!/bin/bash&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Loop through all xlsx files in the current directory&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">for&lt;/span> file in *.xlsx
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">do&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># Convert xlsx file to csv format using xlsx2csv&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> xlsx2csv &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="nv">$file&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span> &lt;span class="s2">&amp;#34;&lt;/span>&lt;span class="si">${&lt;/span>&lt;span class="nv">file&lt;/span>&lt;span class="p">%.xlsx&lt;/span>&lt;span class="si">}&lt;/span>&lt;span class="s2">.csv&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">done&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Combine all csv files into a single file&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">cat *.csv &amp;gt; combined.csv
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">#create database&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sqlite3 combined.db &lt;span class="s">&amp;lt;&amp;lt;EOF
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">CREATE TABLE combined (
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s"> Entry VARCHAR,
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s"> Ref FLOAT,
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s"> ...
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">);
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">EOF&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1"># Import the content from the combined.csv file&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">sqlite3 combined.db &lt;span class="s">&amp;lt;&amp;lt;EOF
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">.mode csv
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">.import combined.csv combined
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="s">EOF&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Kill a process running on port x on Linux</title><link>https://portfolio.devcrumbs.com/learned/kill-process-on-port/</link><pubDate>Tue, 16 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/kill-process-on-port/</guid><description>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">sudo &lt;span class="nb">kill&lt;/span> -9 &lt;span class="sb">`&lt;/span>sudo lsof -t -i:&lt;span class="o">[&lt;/span>x&lt;span class="o">]&lt;/span>&lt;span class="sb">`&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>where &lt;code>[x]&lt;/code> is the port number&lt;/p></description></item><item><title>IRI vs. URI and why are they not very common</title><link>https://portfolio.devcrumbs.com/learned/iri-vs-uri/</link><pubDate>Mon, 15 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/iri-vs-uri/</guid><description>&lt;p>URI stands for Uniform Resource Identifier, and IRI stands for Internationalized Resource Identifier. While they are similar, there is a slight difference between the two.&lt;/p>
&lt;p>URI is a generic term that encompasses both Uniform Resource Locators (URLs) and Uniform Resource Names (URNs). URLs are commonly used to locate resources on the internet and often include a network location (e.g., &lt;a href="http://portfolio.devcrumbs.com" target="_blank" rel="noopener">http://portfolio.devcrumbs.com&lt;/a>) along with the specific path to the resource. URNs, on the other hand, are persistent identifiers that are independent of the resource&amp;rsquo;s location and can be used to identify a resource even if it has moved.&lt;/p>
&lt;p>IRI, on the other hand, is an extension of URI that allows for international characters and non-ASCII characters to be used in the identifier. It was introduced to support non-English languages and characters in web addresses. IRIs are encoded using UTF-8, which enables the use of characters from various scripts, including Chinese, Arabic, Cyrillic, and many others.&lt;/p>
&lt;h2 id="why-iri-is-not-very-common-on-the-internet">Why IRI is not very common on the Internet?&lt;/h2>
&lt;p>Backward compatibility poses significant challenges, and there is a strong consensus to avoid disrupting the functioning of the Internet. A typical HTTP request involves traversing numerous layers of software and hardware, making it uncertain whether IRIs (Internationalized Resource Identifiers) will be supported at each stage. In order to ensure compatibility with the current URI (Uniform Resource Identifier) infrastructure, IRIs are typically converted to URIs using percent-encoded ASCII values before transmission. Essentially, this involves employing the encodeURIComponent function.&lt;/p></description></item><item><title>Circular cropping from BASH</title><link>https://portfolio.devcrumbs.com/learned/circle-cropping-bash/</link><pubDate>Fri, 12 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/circle-cropping-bash/</guid><description>&lt;p>The drawing should be ~ half of the original image&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">convert input.png &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> -gravity Center &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> &lt;span class="se">\(&lt;/span> -size 1024x1024 &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> xc:Black &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> -fill White &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> -draw &lt;span class="s1">&amp;#39;circle 500 500 500 1&amp;#39;&lt;/span> &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> -alpha Copy &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> &lt;span class="se">\)&lt;/span> -compose CopyOpacity -composite &lt;span class="se">\
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="se">&lt;/span> -trim output.png
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Modified grep command</title><link>https://portfolio.devcrumbs.com/learned/modified-grep-command/</link><pubDate>Wed, 10 May 2023 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/modified-grep-command/</guid><description>&lt;p>When executing the command &lt;code>ps aux | grep -rI process&lt;/code>, the displayed results appear as follows:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl"> &lt;span class="m">8176&lt;/span> process
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="m">8189&lt;/span> grep -rI process
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The second entry in the result corresponds to the command we just executed. Although this is typically not problematic, it can lead to issues if you intend to use this command within a bash if statement to check whether &lt;code>process&lt;/code> is running. In such cases, the command would return true even if &lt;code>process&lt;/code> is not running.&lt;/p>
&lt;p>To ensure that the command returns nothing when &lt;code>process&lt;/code> is not running, you can modify it as follows:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">ps aux &lt;span class="p">|&lt;/span> grep -rI &lt;span class="o">[&lt;/span>p&lt;span class="o">]&lt;/span>rocess
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The addition of brackets in the regex causes grep to interpret the query in the same way, but it prevents the literal string &lt;code>[p]rocess&lt;/code> from matching &lt;code>process_name&lt;/code>. Consequently, the modified command produces the following desired output:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-bash" data-lang="bash">&lt;span class="line">&lt;span class="cl">&lt;span class="m">8176&lt;/span> process
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This behavior is preferable for certain use cases.&lt;/p></description></item><item><title>Python conditional import in external library</title><link>https://portfolio.devcrumbs.com/learned/conditional-import-external-library/</link><pubDate>Wed, 11 May 2022 00:00:00 +0000</pubDate><guid>https://portfolio.devcrumbs.com/learned/conditional-import-external-library/</guid><description>&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-python" data-lang="python">&lt;span class="line">&lt;span class="cl">&lt;span class="kn">from&lt;/span> &lt;span class="nn">logging&lt;/span> &lt;span class="kn">import&lt;/span> &lt;span class="n">getLogger&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kn">import&lt;/span> &lt;span class="nn">foo&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">try&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kn">import&lt;/span> &lt;span class="nn">bar&lt;/span> &lt;span class="k">as&lt;/span> &lt;span class="nn">br&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">BAR_INSTALLED&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kc">True&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">msg&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;bar is imported.&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="k">except&lt;/span> &lt;span class="ne">ImportError&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">BAR_INSTALLED&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kc">False&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="n">msg&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="s2">&amp;#34;bar is not available.&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">logger&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="n">getLogger&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">__main__&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="n">logger&lt;/span>&lt;span class="o">.&lt;/span>&lt;span class="n">info&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">msg&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item></channel></rss>