Fwd: Eliminate "extra" variable

Igor Korot ikorot01 at gmail.com
Sun Dec 8 02:14:03 EST 2013


OK, here is the complete function code that I'm trying to improve.

def MyFunc(self, originalData):
     self.dates = []
      data = {}
      dateStrs = []
      for i in xrange(0, len(originalData)):
            dateStr, freq, source = originalData[i]
            data[str(dateStr)]  = {source: freq}
            dateStrs.append(dateStr)
     for i in xrange(0, len(dateStrs) - 1):
           currDateStr = str(dateStrs[i])
           nextDateStr = str(dateStrs[i + 1])
           self.dates.append(currDateStr)
           currDate = datetime.datetime.strptime(currDateStr, '%Y-%m-%d')
           nextDate = datetime.datetime.strptime(nextDateStr, '%Y-%m-%d')
           if nextDate - curDate < datetime.timedelta(days=31):
               d = currDate + datetime.timedelta(days=1)
               while d < nextDate:
                   self.dates.append(d.strftime('%Y-%m-%d'))
                   d = d + datetime.timedelta(days=1)
     lastDateStr = dateStrs[-1]
     self.dates.append(str(lastDateStr))
     return data

As you can see there is many conversion going on and there's unneeded
dateStrs which is used just to loop thru the dictionary and get the 2
consecutive keys.

This I'm trying to avoid to make this function perform faster and
consume less memory.

This code was written a long time ago before me and now I'm trying to
improve it.

I'm thinking of using the suggestion of Tim Chase which will eliminate
the extra unneeded variables and will let me also eliminate a lot of
back and force conversions.
But if you see a better way - please share.

Thank you.


On Fri, Dec 6, 2013 at 5:36 PM, Igor Korot <ikorot01 at gmail.com> wrote:
> OK, people, thank you everybody for the input.
>
> Here is the more explanation that I think is in order:
>
> The originalData comes from either SQLite DB or mySQL DB. Since first
> uses strings and second uses datetime I am using str() to make the
> data consistent.
> Now the query is this:
>
> SELECT date, freq, value FROM my_table ORDER BY date;
>
> and so I do know that the originalData is already presorted (ordered).
> So sorting is not a problem here.
>
> Now what I'm looking for here is check whether the time difference
> between the currDateStr and nextDateStr is 30 days. If its do some
> calculation and pass the dictionary with dates as strings to the next
> function.
> That function is inside wxPython (GUI) which will do plotting. In  the
> plot function the dates will be used as the labels for the x-axis. So
> this is another reason to keep dates as strings.
>
> I hope I made it a little clear and hopefully someone will give me
> Python example of how do I simplify this code to eliminate un-needed
> variables.
>
> Thank you.
>
>
> On Fri, Dec 6, 2013 at 5:19 PM, Igor Korot <ikorot01 at gmail.com> wrote:
>> Hi, Gary,
>>
>> On Fri, Dec 6, 2013 at 3:28 PM, Gary Herron
>> <gary.herron at islandtraining.com> wrote:
>>> On 12/06/2013 11:37 AM, Igor Korot wrote:
>>>>
>>>> Hi, ALL,
>>>> I have following code:
>>>>
>>>> def MyFunc(self, originalData):
>>>>       data = {}
>>>>       dateStrs = []
>>>>       for i in xrange(0, len(originalData)):
>>>>             dateStr, freq, source = originalData[i]
>>>>             data[str(dateStr)]  = {source: freq}
>>>>             dateStrs.append(dateStr)
>>>>      for i in xrange(0, len(dateStrs) - 1):
>>>>            currDateStr = str(dateStrs[i])
>>>>            nextDateStrs = str(dateStrs[i + 1])
>>>>
>>>>
>>>> It seems very strange that I need the dateStrs list just for the
>>>> purpose of looping thru the dictionary keys.
>>>> Can I get rid of the "dateStrs" variable?
>>>>
>>>> Thank you.
>>>
>>>
>>> You want to build a list, but you don't want to give that list a name?  Why
>>> not?  And how would you refer to that list in the second loop if it didn't
>>> have a name?
>>>
>>> And concerning that second loop:  What are you trying to do there? It looks
>>> like a complete waste of time.  In fact, with what you've shown us, you can
>>> eliminate the variable dateStrs, and both loops and be no worse off.
>>>
>>> Perhaps there is more to your code than you've shown to us ...
>>
>> What I want here is to do something like this:
>>
>> def MyFunc(self, originalData):
>>       data = {}
>>       for i in xrange(0, len(originalData)):
>>             dateStr, freq, source = originalData[i]
>>             data[str(dateStr)]  = {source: freq}
>>      for i in xrange(0, len(data) - 1):
>>            currDateStr = str(data[i].key())             // I need a syntax here
>>            nextDateStrs = str(data[i + 1].key())    // I need a syntax here
>>
>> Thank you.
>>
>>>
>>> Gary Herron
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list