<?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>Forem: Reece Dunham</title>
    <description>The latest articles on Forem by Reece Dunham (@rdil).</description>
    <link>https://forem.com/rdil</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%2F168003%2F1886aba7-00a4-444e-8b4d-55a0499c3f15.png</url>
      <title>Forem: Reece Dunham</title>
      <link>https://forem.com/rdil</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rdil"/>
    <language>en</language>
    <item>
      <title>Python I/O made easy with filehandlers</title>
      <dc:creator>Reece Dunham</dc:creator>
      <pubDate>Fri, 27 Sep 2019 15:15:29 +0000</pubDate>
      <link>https://forem.com/rdil/python-i-o-made-easy-with-filehandlers-4jmd</link>
      <guid>https://forem.com/rdil/python-i-o-made-easy-with-filehandlers-4jmd</guid>
      <description>&lt;p&gt;Hello everybody,&lt;br&gt;
Today I am here to explain how my library, filehandlers, makes basic Python I/O easier for everyone.&lt;/p&gt;

&lt;p&gt;Let me start off by saying that Python's &lt;code&gt;open&lt;/code&gt; built-in function can be confusing. While it is well documented on 3rd party websites, integration in general can be difficult. The result of using the function is a &lt;code&gt;io.TextIOWrapper&lt;/code&gt; object, which by itself does have documentation, but still can be hard for beginners and advanced Python developers alike.&lt;/p&gt;

&lt;p&gt;(Before you can continue, filehandlers needs to be installed. You will need to install the &lt;code&gt;filehandlers&lt;/code&gt; package with pip (&lt;a href="https://packaging.python.org/tutorials/installing-packages/"&gt;guide&lt;/a&gt;), or add it to your &lt;code&gt;requirements.txt&lt;/code&gt;.)&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating Files
&lt;/h2&gt;

&lt;p&gt;Let's start off with the bare minimum, creating a file in the working directory.&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;# import the library
# make sure you followed the steps in the parentheses above!
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt;

&lt;span class="c1"&gt;# define a "file", even if said "file" doesn't yet exist
# this call just creates an instance, nothing too spooky going on here
&lt;/span&gt;&lt;span class="n"&gt;myfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AbstractFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mycoolfile.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# create the file on the disk
&lt;/span&gt;&lt;span class="n"&gt;myfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;touch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# note in the console what great work we have done
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I created the file!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy, right?&lt;/p&gt;

&lt;h2&gt;
  
  
  Writing to Files
&lt;/h2&gt;

&lt;p&gt;Now, let's create a file, and write a hello world to it:&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;# same basic starter steps
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt;

&lt;span class="n"&gt;myfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AbstractFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;mycoolfile.txt&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# create the file on the disk
&lt;/span&gt;&lt;span class="n"&gt;myfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;touch&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="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;I created the file!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# create a manipulator object so we can change the file:
&lt;/span&gt;&lt;span class="n"&gt;manipulator_of_myfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FileManipulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;# we need to input the AbstractFile instance to this function:
&lt;/span&gt;    &lt;span class="n"&gt;myfile&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# now, we will write "hello world!" to the file:
&lt;/span&gt;&lt;span class="n"&gt;manipulator_of_myfile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write_to_file&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;hello world!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# and we are done
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Mission complete.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have created files &lt;em&gt;and&lt;/em&gt; written to files, I think it's time we read files, so we can use existing data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading Files
&lt;/h2&gt;

&lt;p&gt;Alright, let's just get right into this:&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;# this time, we'll just import the modules we need
# so there is no need to fully type out the class names inline
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;filehandlers&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AbstractFile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileManipulator&lt;/span&gt;

&lt;span class="n"&gt;myfile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AbstractFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;data.cfg&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;manipulator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FileManipulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myfile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# we just created the manipulator, so the file cache should be up to date
# if at any time you believe the file may have changed, you can run:
# my_manipulator_variable.refresh()
&lt;/span&gt;
&lt;span class="c1"&gt;# get the file's line cache
# (this will be an array, each index will be the next line, so the_cache_element[0] will be the first line, the_cache_element[1] will be the second line, etc. etc.)
&lt;/span&gt;
&lt;span class="n"&gt;the_cache_element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;manipulator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_cache&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# print the first line of the file
&lt;/span&gt;&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Line 1: &lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;the_cache_element&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# print all of the lines:
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;the_cache_element&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;line&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Super simple.&lt;/p&gt;

&lt;p&gt;If you would like to do further reading, full docs can be found &lt;a href="https://filehandlers.rdil.rocks/en/stable"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Want to contribute? The library is open source and can be found &lt;a href="https://github.com/RDIL/filehandlers"&gt;here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>python</category>
      <category>io</category>
      <category>files</category>
    </item>
    <item>
      <title>Setting up Python unittests with GitHub annotations</title>
      <dc:creator>Reece Dunham</dc:creator>
      <pubDate>Fri, 27 Sep 2019 01:21:43 +0000</pubDate>
      <link>https://forem.com/rdil/setting-up-python-unittests-with-github-annotations-3li1</link>
      <guid>https://forem.com/rdil/setting-up-python-unittests-with-github-annotations-3li1</guid>
      <description>&lt;p&gt;Hello!&lt;br&gt;
This is my first post on DEV, so I'm going to try to make this quick and simple.&lt;/p&gt;

&lt;p&gt;If you want inline examples of exactly where your code is failing, you can integrate &lt;a href="https://medium.com/cirruslabs/github-annotations-support-227d179cde31"&gt;Cirrus CI with GitHub annotations&lt;/a&gt;. This is super simple to do.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start off by writing unittests. This is super simple.&lt;/li&gt;
&lt;li&gt;Setup a basic CI pipeline (&lt;code&gt;.cirrus.yml&lt;/code&gt; file). You will want to do something like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;tests_task&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="c1"&gt;# define Docker container&lt;/span&gt;
  &lt;span class="na"&gt;container&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python:latest&lt;/span&gt;

  &lt;span class="c1"&gt;# install project requirements and the annotation result builder&lt;/span&gt;
  &lt;span class="na"&gt;install_script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
    &lt;span class="s"&gt;pip install -r ./some-requirements-file.txt&lt;/span&gt;
    &lt;span class="s"&gt;pip install unittest-xml-reporting&lt;/span&gt;

  &lt;span class="c1"&gt;# normally, you would run unittests with the main command&lt;/span&gt;
  &lt;span class="c1"&gt;# we need to build XML reports, so use this command&lt;/span&gt;
  &lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;python3 -m xmlrunner tests&lt;/span&gt;
  &lt;span class="c1"&gt;# replace tests with the name of the module your unittests are in&lt;/span&gt;

  &lt;span class="c1"&gt;# (always) upload results - even if the tests fail&lt;/span&gt;
  &lt;span class="na"&gt;always&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;unittest_results_artifacts&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="c1"&gt;# where the outputted XML files are&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;./*.xml&lt;/span&gt;
      &lt;span class="c1"&gt;# required, even though it sounds wrong&lt;/span&gt;
      &lt;span class="na"&gt;format&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;junit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that is all you need to do!&lt;br&gt;
You should then get annotations.&lt;/p&gt;

&lt;p&gt;Have a nice day! &lt;/p&gt;

</description>
      <category>python</category>
      <category>unittests</category>
      <category>cirrus</category>
      <category>ci</category>
    </item>
  </channel>
</rss>
