Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'syntax': 0.04; 'memory.': 0.07; 'variables': 0.07; 'explanation': 0.09; 'name?': 0.09; 'strings.': 0.09; 'subject:extra': 0.09; 'variables.': 0.09; 'worse': 0.09; 'python': 0.11; 'def': 0.12; '1):': 0.16; 'igor': 0.16; 'keys.': 0.16; 'len(data)': 0.16; 'looping': 0.16; 'loops': 0.16; 'sorting': 0.16; 'sqlite': 0.16; 'str()': 0.16; 'subject:variable': 0.16; 'there?': 0.16; 'unneeded': 0.16; 'wxpython': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'all,': 0.19; 'trying': 0.19; 'written': 0.21; 'seems': 0.21; '>>>': 0.22; 'select': 0.22; 'example': 0.22; 'rid': 0.24; 'people,': 0.24; 'looks': 0.24; 'source': 0.25; 'query': 0.26; 'shown': 0.26; 'this:': 0.26; 'second': 0.26; 'pass': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'tim': 0.29; 'dec': 0.30; 'waste': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; '>>>>': 0.31; 'chase': 0.31; 'gary': 0.31; 'another': 0.32; 'url:python': 0.33; 'ago': 0.33; 'fri,': 0.33; 'skip:d 20': 0.34; 'problem': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'dates': 0.36; 'url:listinfo': 0.36; 'next': 0.36; "didn't": 0.36; 'hi,': 0.36; 'url:org': 0.36; 'list': 0.37; 'clear': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'little': 0.38; 'subject:" ': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'url:mail': 0.40; 'how': 0.40; 'everybody': 0.60; 'hope': 0.61; 'conversion': 0.61; 'subject:Fwd': 0.61; 'first': 0.61; 'you.': 0.62; 'back': 0.62; 'complete': 0.62; "you've": 0.63; 'refer': 0.63; 'more': 0.64; 'here': 0.66; 'between': 0.67; 'date,': 0.68; 'fact,': 0.69; 'us,': 0.73; '5:36': 0.84; 'order:': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=i0HGc150OFPHB+M4TDR3O7mw2FQFEqdvBGRVJgvAOOA=; b=EMvB5bPD/j3bS9wjxiiu8NhWSRwRoAvZSV37RHT5BGERWs2wWW3VEJW5nbKmOIA6jU TydP6u0w3eVxlWyR6mKpZDfheup/gkdsaJopi0ak1w/DpHt8pOdaybYvaYwCXuUJHgdG pK8+xDuldKikTU9PcUevee9ZEu2i/84styG/Zou/LLZ+4ZcZ7HC2T6Rw9V5TTuA+tXRD 6OY8Zil82IA+PKyoWRGuQn4vDuuGKIacYKzRTJe0Lg5V5bUbWXsbYv+TaWoSWpT4k03O aFJOkmijxs7Fb58Ip66mJMeeBxv0PMciaYP95GQcByDgA8fJ5VVRDWX41sSKrASu2nLV kraA== MIME-Version: 1.0 X-Received: by 10.220.97.69 with SMTP id k5mr479128vcn.45.1386486843705; Sat, 07 Dec 2013 23:14:03 -0800 (PST) In-Reply-To: References: <52A25D94.9040404@islandtraining.com> Date: Sat, 7 Dec 2013 23:14:03 -0800 Subject: Fwd: Eliminate "extra" variable From: Igor Korot To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-Mailman-Approved-At: Sun, 08 Dec 2013 13:36:41 +0100 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 130 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1386506202 news.xs4all.nl 2844 [2001:888:2000:d::a6]:54532 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:61287 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 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 wrote: >> Hi, Gary, >> >> On Fri, Dec 6, 2013 at 3:28 PM, Gary Herron >> 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