<?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: YairMorr</title>
    <description>The latest articles on DEV Community by YairMorr (@yairmorr).</description>
    <link>https://dev.to/yairmorr</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%2F191829%2Fa493011c-3a09-40bc-a410-ecc157d4efa8.jpg</url>
      <title>DEV Community: YairMorr</title>
      <link>https://dev.to/yairmorr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yairmorr"/>
    <language>en</language>
    <item>
      <title>How I learned a Python trick from a JavaScript book</title>
      <dc:creator>YairMorr</dc:creator>
      <pubDate>Wed, 10 Jul 2019 11:32:24 +0000</pubDate>
      <link>https://dev.to/yairmorr/how-i-learned-a-python-trick-from-a-javascript-book-4l4p</link>
      <guid>https://dev.to/yairmorr/how-i-learned-a-python-trick-from-a-javascript-book-4l4p</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l-tzSaRg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Anaasd4ZbU2oKvS6cDj33vQ.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l-tzSaRg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Anaasd4ZbU2oKvS6cDj33vQ.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’m working on a nice little tool for project management in Autodesk Maya. It has a few drop down menus that contain folders and files inside a project root directory. This tool automates a few steps and helps the user quickly switch from one project to another without browsing for files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--e9EGLtjE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AwfA0Hgq8oPMdhFc4Cuwlqw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--e9EGLtjE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AwfA0Hgq8oPMdhFc4Cuwlqw.png" alt=""&gt;&lt;/a&gt;Maya Project Browser, how it looks as it is in development&lt;/p&gt;

&lt;p&gt;While working on this tool, I ran into an annoying scope issue with my algorithm when iterating over a loop, that prevented me from assigning the value I wished to a variable. I wanted to track an index value for each drop down menu, and instead I got the same value for all menus.&lt;/p&gt;

&lt;p&gt;I then remembered reading on a very similar issue on my favourite JavaScript book series — &lt;em&gt;You Don’t Know JS&lt;/em&gt; by &lt;em&gt;Kyle Simposon&lt;/em&gt;. In his book &lt;a href="https://www.amazon.com/dp/B00IV3J2A2/ref=cm_sw_r_cp_awdb_t1_qMBjDbYMR0TFP"&gt;Scope &amp;amp; Closures&lt;/a&gt;, he explains that while assigning a variable to a function from inside of a loop captures a copy of that variable, it is still sharing the same scope. This means that each function call inside my loop refers to a copy of the variable that eventually points to the same value. But I write my tool in Python and not JS, as it is the language used in Maya for writing scripts.&lt;/p&gt;

&lt;p&gt;From my experience in visual effects, problems are usually similar across different platforms, and the solutions are usually similar as well. But can this bit of wisdom serve me in the world of code? Not surprisingly, the answer is yes! I figured if this scope issue happens in JavaScript, it might as well happen in Python.&lt;/p&gt;

&lt;p&gt;Simpson’s solution in Scope &amp;amp; Closures to this problem was to pass that variable to the same function, but enclosing it inside an IIFE. Now you can assign that variable to a local variable inside the IIFE (which is inside the loop). The local variable now holds a copy of the value for each iteration and passes that value to the function and it does not change after the loop iterates again and again.&lt;/p&gt;

&lt;p&gt;In Python, as far as I can tell, there are no IIFEs and no blocks of code, like in JavaScript. To solve this issue, I just enclosed the code inside the loop in a new method and called this method within the loop. This new method essentially serves as an inner scope, similar to JavaScript’s IIFE, which “locks” the value I require and pass it down safely to the method inside.&lt;/p&gt;

&lt;p&gt;This is basically it! Following below is a detailed explanation of how I implemented this in my code.&lt;/p&gt;

&lt;p&gt;My script has a main class that contains all the methods and variables for the tool.&lt;br&gt;&lt;br&gt;
The first method is &lt;em&gt;initUI()&lt;/em&gt;, which creates a window and adds some buttons and the dropdown menus (Maya’s &lt;em&gt;cmds.optionMenu()&lt;/em&gt;) and stores them in a dictionary called &lt;em&gt;self.menus&lt;/em&gt;.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;em&gt;cmds.optionMenu()&lt;/em&gt; function in line 6 above takes a label for the dropdown menu, which is &lt;em&gt;“Menu”+str(i)&lt;/em&gt;. The &lt;em&gt;cc&lt;/em&gt; attribute passes a callback event handler that executes once a user selects an item from the menu. To pass in additional custom parameters, I use a lambda function and call my event handler &lt;em&gt;self.indexFolders()&lt;/em&gt; within it.&lt;br&gt;&lt;br&gt;
The important part here is the variable &lt;em&gt;i&lt;/em&gt; that is passed to every optionMenu’s &lt;em&gt;indexFolders()&lt;/em&gt; method. It holds the dropdown menu’s index, the variable I wish to use later when I want to update the content of my menus.&lt;/p&gt;

&lt;p&gt;As it is now, once the script executes, every dropdown menu’s &lt;em&gt;indexFolders()&lt;/em&gt; has &lt;em&gt;i&lt;/em&gt; passed in each iteration of the loop, but eventually every &lt;em&gt;indexFolders()&lt;/em&gt; holds the value &lt;em&gt;i&lt;/em&gt; had at the last iteration of the loop, instead of the value &lt;em&gt;i&lt;/em&gt; has at the time of declaration. This means that every time I select an item from any optionMenu object, I get the same value (in this case, 3), instead of an index from 0 to 3 for each menu respectively.&lt;/p&gt;

&lt;p&gt;In Scopes &amp;amp; Enclosures, Simpson suggests enclosing the body of the loop in a block {} or an IIFE. The solution I found in Python was to put my code in another function and call it within the loop, like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The &lt;em&gt;createMenu()&lt;/em&gt; method has the exact line of code as in the previous snippet’s line 6. I just changed the name of the variable from the loop’s global &lt;em&gt;i&lt;/em&gt; to the method’s local &lt;em&gt;level&lt;/em&gt;. Now I call &lt;em&gt;createMenu()&lt;/em&gt; from within my for loop and assign it &lt;em&gt;i&lt;/em&gt; every time, and &lt;em&gt;createMenu()&lt;/em&gt; assigns it’s local &lt;em&gt;level&lt;/em&gt; to the event handler with the value I intended it to have — the index for the loop.&lt;br&gt;&lt;br&gt;
Now each optionMenu object has a different value, based on it’s index, and I can use that value to know exactly which menus to update!&lt;/p&gt;

&lt;p&gt;Learning JavaScript is great, and it’s even better when the knowledge I acquire helps me indirectly solving problems with other languages and establishing a deeper understanding of coding and computer science.&lt;/p&gt;

</description>
      <category>scopes</category>
      <category>maya</category>
      <category>python</category>
      <category>javascript</category>
    </item>
    <item>
      <title>CS101 —Python exercise: counting days between two dates</title>
      <dc:creator>YairMorr</dc:creator>
      <pubDate>Thu, 06 Jun 2019 13:20:22 +0000</pubDate>
      <link>https://dev.to/yairmorr/cs101-python-exercise-counting-days-between-two-dates-3jh</link>
      <guid>https://dev.to/yairmorr/cs101-python-exercise-counting-days-between-two-dates-3jh</guid>
      <description>&lt;h3&gt;
  
  
  CS101 — Python exercise: counting days between two dates
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ThRBjtjX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ax8me-NapICbyazKHYvhX9w.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ThRBjtjX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2Ax8me-NapICbyazKHYvhX9w.jpeg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As part of Udacity’s &lt;a href="https://eu.udacity.com/course/intro-to-computer-science--cs101"&gt;Intro to Computer Science online course&lt;/a&gt; that I’m taking, I ran into this interesting exercise. The goal was to create a function that takes two dates as input and counts the days between them. The first date is always earlier than the second. My solution was probably not the most elegant and my code could be shorter and more efficient. The logic however was the important thing to understand, and that is also what I want to focus on in this article. This exercise was written in Python.&lt;/p&gt;

&lt;p&gt;If we put the two input dates on a timeline, the algorithm is working from the outside in. First it calculates the days in the months of each end of the timeline, then the days in each month for each year and then the days in all the years in between. The longer the distance is between the two input dates, the more conditions necessary for the algorithm to calculate the days correctly.&lt;/p&gt;

&lt;h4&gt;
  
  
  1st step — Counting the days inside each month
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, &lt;em&gt;daysCount&lt;/em&gt; is the general counter of the days between the two days, which I used throughout my function and eventually what the function returned. I assigned it to the &lt;em&gt;day2&lt;/em&gt; argument. I then subtracted the amount of days in &lt;em&gt;month1&lt;/em&gt; from &lt;em&gt;day1&lt;/em&gt; to get the count of days for the rest of &lt;em&gt;month1&lt;/em&gt;. I used the list &lt;em&gt;monthsCommonYear&lt;/em&gt; to get the exact amount of days in each month (I tend to leap years later on). This is the first because it occurs in every scenario.&lt;/p&gt;

&lt;p&gt;This step returns the result for two input scenarios (I’m using DD/MM/YYYY format here):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The two input dates have the same year and same month (i.e. 12.8.2012–28.8.2012)&lt;/li&gt;
&lt;li&gt;The two input dates have the same year but sequential months (12.8.2012–15.9.2012)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, if the input dates are for the same year but the months are different, the function goes to the next step.&lt;/p&gt;

&lt;h4&gt;
  
  
  2nd step — Same year, different month
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In this step, if the months are not sequential and the year is the same, I count the days between the first and the last month (i.e 12.8.2012–24.12.2012)&lt;/p&gt;

&lt;h4&gt;
  
  
  3rd step — Sequential years
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, if the time between the input years is 1 year or more, I add whole months to my &lt;em&gt;daysCount&lt;/em&gt;. for &lt;em&gt;year1&lt;/em&gt;, I add the next month (&lt;em&gt;month1+1&lt;/em&gt;) till the end of the year. For &lt;em&gt;year2&lt;/em&gt;, I count months from (&lt;em&gt;month2–1&lt;/em&gt;) till the beginning of the year. I take care not to count the days in the months I counted previously. I also take care to count the correct months in my &lt;em&gt;monthsCommonYear&lt;/em&gt; list, knowing my arguments can go from 1 to 12, but my list index goes from 0 to 11.&lt;/p&gt;

&lt;h4&gt;
  
  
  Distant years
&lt;/h4&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The last step is pretty straightforward. I add to my &lt;em&gt;daysCount&lt;/em&gt; the days in each year between &lt;em&gt;year1&lt;/em&gt; and &lt;em&gt;year2&lt;/em&gt;. The function eventually returns &lt;em&gt;daysCount&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Accounting for leap years
&lt;/h4&gt;

&lt;p&gt;This exercise had an additional challenge, which made it even more interesting: the function has to tell if a year is common or leap and calculate the days accordingly. It was actually easier than I thought. The Udacity instructions linked to a &lt;a href="http://en.wikipedia.org/wiki/Leap_year#Algorithm"&gt;Wikipedia article&lt;/a&gt; that offered the exact algorithm I needed and that was easily converted to a function like the one below.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The comments in this snippet are taken straight from the description in Wikipedia.&lt;br&gt;&lt;br&gt;
I used this function in two types of scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;To determine which month list to iterate over — leap or common.&lt;/li&gt;
&lt;li&gt;To calculate the amount of days in the last scenario — when there’s a difference of more than 1 year between the two input dates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I called &lt;em&gt;checkLeapYear()&lt;/em&gt; 5 times in my function: 4 times to choose which month list to use (common or leap) and 1 time to add the years to my count. That’s why I preferred &lt;em&gt;checkLeapYear()&lt;/em&gt; to return a boolean instead of the number of days in each year (365 or 366).&lt;/p&gt;

&lt;p&gt;Here’s the first part of the function, updated to calculate leap years. Notice I added another list for leap year months (the only difference is 29 days in February).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The code I wrote for this exercise is long and encumbered, and perhaps later in my studies I will refactor it to something shorter and more efficient. The important thing for me here was exploring the logic behind the solution, rather than the way it was implemented.&lt;/p&gt;

</description>
      <category>logic</category>
      <category>python</category>
      <category>exercise</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
