<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jorge Alvarado</title>
    <description>The latest articles on DEV Community by Jorge Alvarado (@jalvaradosegura).</description>
    <link>https://dev.to/jalvaradosegura</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F794553%2F8adeb2c1-b8b6-4359-8fa7-387528b19bbc.jpg</url>
      <title>DEV Community: Jorge Alvarado</title>
      <link>https://dev.to/jalvaradosegura</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jalvaradosegura"/>
    <language>en</language>
    <item>
      <title>Create your own pre-commit hook</title>
      <dc:creator>Jorge Alvarado</dc:creator>
      <pubDate>Wed, 23 Feb 2022 15:31:49 +0000</pubDate>
      <link>https://dev.to/jalvaradosegura/create-your-own-pre-commit-hook-3kh</link>
      <guid>https://dev.to/jalvaradosegura/create-your-own-pre-commit-hook-3kh</guid>
      <description>&lt;p&gt;In this tutorial we are going to create a very basic git hook, using the awesome package &lt;a href="https://pre-commit.com/"&gt;&lt;code&gt;pre-commit&lt;/code&gt;&lt;/a&gt;, created by the one and only &lt;a href="https://github.com/asottile"&gt;Anthony Sottile&lt;/a&gt; 👏👏.&lt;/p&gt;

&lt;p&gt;The hook will be very basic. It won't have unit-tests nor will be very functional for a real project. Although it will show you the first steps you need to take in order to create something incredible.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I recommend you using a virtual environment to follow this tutorial.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Tutorial Index
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create the structure of the project&lt;/li&gt;
&lt;li&gt;Create the functionality&lt;/li&gt;
&lt;li&gt;Turn it into a python package&lt;/li&gt;
&lt;li&gt;Turn it into a hook&lt;/li&gt;
&lt;li&gt;Test it&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Create the structure of the project &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Create the following project structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── test-the-hook-in-this-folder
└── the-hook
    ├── print_arguments
    │   ├── __init__.py
    │   └── main.py
    ├── setup.cfg
    └── setup.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;test-the-hook-in-this-folder&lt;/code&gt;: Folder in which we will test our hook by the end of the tutorial (we won't use it until step 5).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;the-hook&lt;/code&gt;: Folder that contains all the files required for the package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;print_arguments&lt;/code&gt;: This is our python package (and also a spoiler of what our hook will do).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;__init__.py&lt;/code&gt;: Turn the folder that contains it into a package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;main.py&lt;/code&gt;:  Here is where the logic of the hook will be.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setup.py&lt;/code&gt;: Necessary for building our package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setup.cfg&lt;/code&gt;: Describe our package.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Create the functionality &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;We will work in the &lt;code&gt;the-hook&lt;/code&gt; folder for steps 2 to 4.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For creating the functionality, we only need to edit the &lt;code&gt;main.py&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# print_arguments/main.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;print_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;]):&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;argument&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argument&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;parser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;argparse&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ArgumentParser&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add_argument&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;filenames&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nargs&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;*&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;args&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;parser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse_args&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="nf"&gt;print_arguments&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filenames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;__main__&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes, our hook will print the arguments we pass to it. We can test its functionality:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python print_arguments/main.py arg1 arg2 arg3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arg1
arg2
arg3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. Turn it into a python package &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;To do this, we need to edit our 2 setup files.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setup.py:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# setup.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;setuptools&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;setup&lt;/span&gt;

&lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;setup.cfg:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# setup.cfg
[metadata]
name = print-arguments
description = print the arguments you pass
version = 0.1.0
author = Jorge Alvarado
author_email = alvaradosegurajorge@gmail.com
license = MIT
url = https://jorgealvarado.me

[options]
packages = find:

[options.entry_points]
console_scripts =
    print-arguments = print_arguments.main:main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Important points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;packages = find:&lt;/code&gt;: It help us find our &lt;code&gt;print_arguments&lt;/code&gt; package when packaging.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console_scripts&lt;/code&gt;: This is our entry point, with this we kind of expose our function to the world. The name of our single entry point, in this case &lt;code&gt;print-arguments&lt;/code&gt;, will be used by &lt;code&gt;pre-commit&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;You can choose other values for the rest of the points.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Install the package with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Make sure you are in &lt;code&gt;the-hook&lt;/code&gt; folder when running that command.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  4. Turn it into a hook &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;For our hook to work we need our project to be a git repository. So let's do that by running the following commands (make sure you are in the &lt;code&gt;the-hook&lt;/code&gt; folder):&lt;/p&gt;

&lt;p&gt;Initialize the git repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add every file and commit them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .; git commit -m "Create the package"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the hook part. First let's install &lt;code&gt;pre-commit&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;.pre-commit-hooks.yaml&lt;/code&gt; file inside the &lt;code&gt;the-hook&lt;/code&gt; folder and edit it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .pre-commit-hooks.yaml&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;some-id&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;some-name&lt;/span&gt;
  &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;some description&lt;/span&gt;
  &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;print-arguments&lt;/span&gt;
  &lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id&lt;/code&gt;: The id of our hook. We will need it for testing and in the future if someone wants to use our hook, the id will be required.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;name&lt;/code&gt;: The name of the hook, it is what is shown during hook execution.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;description&lt;/code&gt; (optional): Description of the hook.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;entry&lt;/code&gt;: The entry point, the executable to run. It has to match with the entry point name defined inside our &lt;code&gt;setup.cfg&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;language&lt;/code&gt;: The language of the hook, it tells &lt;code&gt;pre-commit&lt;/code&gt; how to install the hook.&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;👀 See a &lt;a href="https://pre-commit.com/#new-hooks"&gt;complete list&lt;/a&gt; of the options.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To complete this step, we need to do a commit of our hook stuff:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .; git commit -m "Create a hook"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Test it &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Time to use the &lt;code&gt;test-the-hook-in-this-folder&lt;/code&gt; folder. Change directory to that folder, once there, let's create a very little git project:&lt;/p&gt;

&lt;p&gt;Create some files&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch a.py b.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize the git repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Commit the files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .; git commit -m "Create the project"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, let's test our hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pre-commit try-repo ../the-hook some-id --verbose --all-files
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;some-name................................................................Passed
- hook id: some-id
- duration: 0.08s

a.py
b.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We got &lt;code&gt;some-name&lt;/code&gt; in the output, because that's what we defined in our &lt;code&gt;.pre-commit-hooks.yaml&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;We used &lt;code&gt;some-id&lt;/code&gt; (defined in the &lt;code&gt;.pre-commit-hooks.yaml&lt;/code&gt; file as well) in the command to refer to our hook (we could have multiple hooks).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;a.py&lt;/code&gt; and &lt;code&gt;b.py&lt;/code&gt; were printed. That's because we used the flag &lt;code&gt;--all-files&lt;/code&gt; in our command (If we didn't, the hook execution would have been skipped, because there are no new/edited files for git).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Remember that you need to be in the &lt;code&gt;test-the-hook-in-this-folder&lt;/code&gt; folder for the command just used to work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we now add a new file to our test folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;touch c.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We track it with git:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add c.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we run the command without the &lt;code&gt;--all-files&lt;/code&gt; flag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pre-commit try-repo ../the-hook some-id --verbose
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;some-name................................................................Passed
- hook id: some-id
- duration: 0.07s

c.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, only &lt;code&gt;c.py&lt;/code&gt; was printed this time, because is the only new/edited file for git, so our hook was run only against that file. This is the behavior you will normally want.&lt;/p&gt;




&lt;p&gt;Now you know how to create a pre-commit hook 🪝! Go and make cool things with this knowledge 😁.&lt;/p&gt;

&lt;p&gt;Let me know if you create a hook or something. &lt;a href="https://github.com/jalvaradosegura/version-checker"&gt;This is one&lt;/a&gt; I created a few weeks ago.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>git</category>
      <category>hook</category>
      <category>python</category>
    </item>
    <item>
      <title>Another way of working with temporary files</title>
      <dc:creator>Jorge Alvarado</dc:creator>
      <pubDate>Thu, 03 Feb 2022 13:32:30 +0000</pubDate>
      <link>https://dev.to/jalvaradosegura/another-way-of-working-with-temporary-files-2ono</link>
      <guid>https://dev.to/jalvaradosegura/another-way-of-working-with-temporary-files-2ono</guid>
      <description>&lt;p&gt;🗂 &lt;a href="https://github.com/jalvaradosegura/tmp-folder"&gt;&lt;code&gt;tmp-folder&lt;/code&gt;&lt;/a&gt; allows you to easily use a temporary folder during the execution of a function. After it is done executing, the folder will be automatically deleted (with all its content of course).&lt;/p&gt;

&lt;h2&gt;
  
  
  Install it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install tmp-folder
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Watch it in action
&lt;/h2&gt;

&lt;p&gt;All you need to do is to decorate the function that needs a temporary folder and your function will get a free extra parameter, that's your &lt;code&gt;tmp-folder&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# main.py
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;pathlib&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;tmp_folder&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;use_tmp_folder&lt;/span&gt;


&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;use_tmp_folder&lt;/span&gt;  &lt;span class="c1"&gt;# This is the decorator
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;do_some_stuff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_tmp_folder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;my_tmp_folder&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="s"&gt;"some_file.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"w"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello World"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="n"&gt;my_tmp_folder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;my_tmp_folder&lt;/span&gt;


&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;__name__&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;"__main__"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;my_tmp_folder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;do_some_stuff&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;assert&lt;/span&gt; &lt;span class="ow"&gt;not&lt;/span&gt; &lt;span class="n"&gt;my_tmp_folder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exists&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Everything went ok"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We execute the code with &lt;code&gt;python main.py&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Everything went ok
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Cool, there are no assertion errors 👏🎉🎉🎉.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key points:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The function that needs temporary stuff, is decorated with &lt;code&gt;use_tmp_folder&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;You get a new parameter in your decorated function (it can be named however you want, in this case &lt;code&gt;my_tmp_folder&lt;/code&gt;). See how when you are calling &lt;code&gt;do_some_stuff&lt;/code&gt;, you are not passing any argument, but on its definition there is a parameter, well, that's because &lt;code&gt;tmp-folder&lt;/code&gt; adds it for you.&lt;/li&gt;
&lt;li&gt;This new parameter is the temporary folder that &lt;code&gt;tmp-folder&lt;/code&gt; gives you. You can store whatever you want in there. After the decorated function is done executing: bye bye temporary folder 👋.&lt;/li&gt;
&lt;li&gt;The parameter is a &lt;code&gt;Path&lt;/code&gt; object, so you get all the cool things &lt;code&gt;pathlib&lt;/code&gt; offers.&lt;/li&gt;
&lt;li&gt;The decorator is built on top of Python's  &lt;code&gt;TemporaryDirectory&lt;/code&gt;, so you know is a safe thing to use.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;For more details see the package &lt;a href="https://jalvaradosegura.github.io/tmp-folder/"&gt;documentation&lt;/a&gt;.&lt;br&gt;
Image &lt;a href="https://www.instagram.com/circus.infernus/"&gt;author&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>productivity</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Don't forget to update that value in those files</title>
      <dc:creator>Jorge Alvarado</dc:creator>
      <pubDate>Tue, 25 Jan 2022 13:13:48 +0000</pubDate>
      <link>https://dev.to/jalvaradosegura/dont-forget-to-update-that-value-in-those-files-3i78</link>
      <guid>https://dev.to/jalvaradosegura/dont-forget-to-update-that-value-in-those-files-3i78</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;The other day, based on a project needs, I created a pre-commit hook: &lt;strong&gt;&lt;a href="https://github.com/jalvaradosegura/version-checker"&gt;version-checker&lt;/a&gt;&lt;/strong&gt;. It helps you remember to update the version of your project in different files before doing a commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;You only need to have &lt;a href="https://pre-commit.coma/"&gt;&lt;code&gt;pre-commit&lt;/code&gt;&lt;/a&gt; installed (and to use git, but who doesn't?):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install pre-commit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create a basic project structure
&lt;/h3&gt;

&lt;p&gt;Let's use a very basic project structure for the sake of the tutorial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.
├── README.md
├── setup.cfg
└── src
    └── __init__.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The files &lt;code&gt;README.md&lt;/code&gt; and &lt;code&gt;__init__.py&lt;/code&gt; will refer in some way to the version of the project, that lives in &lt;code&gt;setup.cfg&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's see the content of these files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;setup.cfg&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight conf"&gt;&lt;code&gt;&lt;span class="c"&gt;# setup.cfg
&lt;/span&gt;[&lt;span class="n"&gt;metadata&lt;/span&gt;]
&lt;span class="n"&gt;name&lt;/span&gt; = &lt;span class="n"&gt;some&lt;/span&gt;-&lt;span class="n"&gt;project&lt;/span&gt;
&lt;span class="n"&gt;version&lt;/span&gt; = &lt;span class="m"&gt;0&lt;/span&gt;.&lt;span class="m"&gt;1&lt;/span&gt;.&lt;span class="m"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;README.md&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gh"&gt;# README.md&lt;/span&gt;

&lt;span class="gh"&gt;# You are using version 0.1.0 of this project&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/__init__.py&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# src/__init__.py
&lt;/span&gt;&lt;span class="n"&gt;__version__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"0.1.0"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you update the version of your project (&lt;code&gt;setup.cfg&lt;/code&gt;) you have to remember to update it in 2 more files (&lt;code&gt;README.md&lt;/code&gt; and &lt;code&gt;__init__.py&lt;/code&gt;). That sounds like something easy to forget right? This is when the hook comes to the rescue 🦸.&lt;/p&gt;

&lt;h3&gt;
  
  
  The hook
&lt;/h3&gt;

&lt;p&gt;For setting up the hook you need to create a &lt;code&gt;.pre-commit-config.yaml&lt;/code&gt; file (like any other pre-commit hook):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# .pre-commit-config.yaml&lt;/span&gt;
&lt;span class="na"&gt;repos&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt;   &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;https://github.com/jalvaradosegura/version-checker&lt;/span&gt;
    &lt;span class="na"&gt;rev&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;v0.4.1-alpha&lt;/span&gt;
    &lt;span class="na"&gt;hooks&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt;   &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;version-checker&lt;/span&gt;
        &lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;[&lt;/span&gt;
          &lt;span class="nv"&gt;--files&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;README.md&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;src/__init__.py&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt;
          &lt;span class="nv"&gt;--grab-version-from&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;setup.cfg&lt;/span&gt;
        &lt;span class="pi"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the content is the typical config of a hook. Let's explain the &lt;code&gt;args&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;--files&lt;/code&gt;: mandatory argument. It indicates the path to the files that the hook will evaluate to contain the desired version.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;--grab-version-from&lt;/code&gt;: optional argument. It tells the hook from where to grab the version to evaluate (By default it tries to grab it from a &lt;code&gt;pyproject.toml&lt;/code&gt; file).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then we install it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pre-commit install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congrats, everything is ready, &lt;code&gt;version-checker&lt;/code&gt; won't let you forget to update the files 🎉🎉.&lt;/p&gt;

&lt;h2&gt;
  
  
  See it in action 👀
&lt;/h2&gt;

&lt;p&gt;Let's update the version in our &lt;code&gt;setup.cfg&lt;/code&gt; to &lt;code&gt;0.2.0&lt;/code&gt; and try to do a commit:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V7tPmKIs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0qemw2p3pky5lrfapur.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V7tPmKIs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0qemw2p3pky5lrfapur.png" alt="Failure Case" width="880" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The hook is basically telling you that &lt;code&gt;README.md&lt;/code&gt; and &lt;code&gt;__init__.py&lt;/code&gt; are not using the latest version of the package within their content.&lt;/p&gt;

&lt;p&gt;Update the corresponding files and try to commit again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FKu90fAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi4asr27wfy7pxgkjwlm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FKu90fAz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gi4asr27wfy7pxgkjwlm.png" alt="Success Case" width="798" height="19"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When it may be helpful and when it may not
&lt;/h2&gt;

&lt;p&gt;I created this hook because I was working in a project which had a lot of files that were referring to the version of the project: python files, markdown files, some template files and even a Jenkinsfile. For python files it wasn't hard to handle the version, thanks to &lt;code&gt;version&lt;/code&gt; from &lt;code&gt;importlib.metadata&lt;/code&gt;, but the other files were trickier...&lt;/p&gt;

&lt;p&gt;When do I think this hook may be helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If your project refers to the version of the project in multiple files with different extensions.&lt;/li&gt;
&lt;li&gt;If you are already using &lt;code&gt;pre-commit&lt;/code&gt; hooks and you don't know another tool for this purpose.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When do I think it may not:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you already use another tool for this purpose.&lt;/li&gt;
&lt;li&gt;If for some reason the hook is harming the performance of &lt;code&gt;pre-commit&lt;/code&gt; (in my experience the hook runs super fast). Take a look at &lt;a href="https://github.com/pre-commit/pre-commit/issues/2194"&gt;this&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Other options
&lt;/h2&gt;

&lt;p&gt;The other day I learned about 2 tools that seems to work for the same purpose (I haven't tried them yet):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/bumpver/"&gt;bumpver&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://pypi.org/project/bump2version/"&gt;bump2version&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And also I was shared this cool &lt;a href="https://github.com/c4urself/bump2version/blob/master/RELATED.md"&gt;list&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;Post image created by &lt;a href="https://www.instagram.com/circus.infernus/"&gt;@circus.infernus&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>git</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
