Python passing variable dates to function
<p>Hello: making progress but still struggling. I have the following json:</p>

<pre><code>json =
{
  "emeter": {
    "get_daystat": {
      "day_list": [
        { "year": 2016, "month": 10, "day": 1, "energy": 0.651000 },
        { "year": 2016, "month": 10, "day": 2, "energy": 0.349000 },
        { "year": 2016, "month": 10, "day": 3, "energy": 0.481000 }
      ],
      "err_code": 0
    }
  }
}
</code></pre>

<p>I am using a linear search to find the energy value from a specific day with this function:</p>

<pre><code>parsed_json = json.loads(json)

def get_energy_value_by_date(obj, year, month, day):   
    for value in obj['emeter']['get_daystat']['day_list']:
        if value['year'] == year and value['month'] == month and value['day'] == day:
            return value['energy']


energy = get_energy_value_by_date(parsed_json, 2016, 10, 2)
</code></pre>

<p>So far so good. What I need to do next is find the energy value for various days. For example today (assume json is valid):</p>

<pre><code>import datetime
day_now = datetime.datetime.now().strftime("%d")
month_now = datetime.datetime.now().strftime("%m")
year_now = datetime.datetime.now().strftime("%Y")

parsed_json = json.loads(json)

def get_energy_value_by_date(obj, year, month, day):   
    for value in obj['emeter']['get_daystat']['day_list']:
        if value['year'] == year and value['month'] == month and value['day'] == day:
            return value['energy']


energy_today = get_energy_value_by_date(parsed_json, year_now, month_now, day_now)
print energy_today
</code></pre>

<p>When I run this script it returns</p>

<pre><code>None
</code></pre>

<p>I must be missing something basic here. What I need is the ability to pull the energy value for any day of any month of any year for further processing.</p>

<p>Thanks!</p>

<p>Baobab</p>

<p>There is a simple issue with your script: <a href="https://docs.python.org/2/library/datetime.html" rel="nofollow"><code>strftime</code></a>, according to the docs, will</p>

<blockquote>
  <p>return a string representing the date, controlled by an explicit format string.</p>
</blockquote>

<p>The keyword here being "string." See the following:</p>

<pre><code>&gt;&gt;&gt; import datetime
&gt;&gt;&gt; day_now = datetime.datetime.now().strftime("%d")
&gt;&gt;&gt; day_now
'04'
</code></pre>

<p>This does not equal the integer value of the day in your JSON file:</p>

<pre><code>&gt;&gt;&gt; '04' == 4
False
</code></pre>

<p>Therefore, the equality check will always fail and <code>None</code> will be returned. One way is to use <code>int</code> to convert this value to an integer. A better way is to use the attributes of a <code>datetime</code> object to get the integer values:</p>

<pre><code>&gt;&gt;&gt; datetime.datetime.now().year
2016
&gt;&gt;&gt; datetime.datetime.now().month
10
&gt;&gt;&gt; datetime.datetime.now().day
4
</code></pre>

<p>I would also advise passing just a <code>date</code> object to the function and unpacking it there: it prevents redundancy and cleans up the function signature. You should also use <code>date.today()</code> instead of <code>datetime.now()</code> (since time is irrelevant) and then make the comparison in one operation. The full function could be something like this:</p>

<pre><code>def get_energy_value_by_date(obj, current_day):   
    for value in obj['emeter']['get_daystat']['day_list']:
        if current_day == datetime.date(value['year'], value['month'], value['day']):
            return value['energy']

current_day = datetime.date.today()
energy_today = get_energy_value_by_date(parsed_json, current_day)
print (energy_today)
</code></pre>

python function