Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'memory.': 0.07; 'wednesday,': 0.07; 'alternatives': 0.09; 'filename': 0.09; 'indexes': 0.09; 'pages.': 0.09; 'parsing': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rejected': 0.09; 'skip:o 50': 0.09; 'subset': 0.09; 'try:': 0.09; 'url:activestate': 0.09; 'def': 0.12; 'wrote': 0.14; "'rb')": 0.16; 'dump': 0.16; 'heap': 0.16; 'present)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sane': 0.16; 'specifying': 0.16; 'temp': 0.16; 'try?': 0.16; 'files.': 0.16; 'index': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'trying': 0.19; 'properly': 0.19; 'code,': 0.22; 'memory': 0.22; 'tests': 0.22; 'header:User-Agent:1': 0.23; 'merge': 0.24; 'file.': 0.24; 'mention': 0.26; 'header:X-Complaints-To:1': 0.27; 'point': 0.28; 'skip:p 30': 0.29; 'url:code': 0.29; 'words': 0.29; 'along': 0.30; 'code': 0.31; 'posting': 0.31; 'too.': 0.31; 'closer': 0.31; 'doc': 0.31; 'explained': 0.31; 'subject:size': 0.31; 'file': 0.32; 'becomes': 0.33; 'skip:t 40': 0.33; 'skip:d 20': 0.34; 'knowledge': 0.35; 'except': 0.35; 'problem.': 0.35; 'objects': 0.35; 'but': 0.35; 'done': 0.36; 'method': 0.36; 'wrong': 0.37; 'list': 0.37; 'skip:o 20': 0.38; 'writes': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'how': 0.40; 'even': 0.60; 'read': 0.60; 'dave': 0.60; 'then,': 0.60; 'skip:o 30': 0.61; "you're": 0.61; 'complete': 0.62; "you've": 0.63; 'high': 0.63; 'different': 0.65; 'size.': 0.65; 'here': 0.66; 'determine': 0.67; 'saving': 0.69; 'below.': 0.71; 'parser,': 0.84; 'angel': 0.91; 'do:': 0.91; 'from.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Finding size of Variable Date: Wed, 05 Feb 2014 09:27:15 +0100 Organization: None References: <8e4c1ab1-e65d-483f-ad9d-6933ae2052c3@googlegroups.com> <723729ee-8e74-4d65-aa6f-742051a94101@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848784.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 137 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391588841 news.xs4all.nl 2892 [2001:888:2000:d::a6]:42414 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65473 Ayushi Dalmia wrote: > On Wednesday, February 5, 2014 12:51:31 AM UTC+5:30, Dave Angel wrote: >> Ayushi Dalmia Wrote in message: >> >> >> >> > >> >> > Where am I going wrong? What are the alternatives I can try? >> >> >> >> You've rejected all the alternatives so far without showing your >> >> code, or even properly specifying your problem. >> >> >> >> To get the "total" size of a list of strings, try (untested): >> >> >> >> a = sys.getsizeof (mylist ) >> >> for item in mylist: >> >> a += sys.getsizeof (item) >> >> >> >> This can be high if some of the strings are interned and get >> >> counted twice. But you're not likely to get closer without some >> >> knowledge of the data objects and where they come >> >> from. >> >> >> >> -- >> >> DaveA > > Hello Dave, > > I just thought that saving others time is better and hence I explained > only the subset of my problem. Here is what I am trying to do: > > I am trying to index the current wikipedia dump without using databases > and create a search engine for Wikipedia documents. Note, I CANNOT USE > DATABASES. My approach: > > I am parsing the wikipedia pages using SAX Parser, and then, I am dumping > the words along with the posting list (a list of doc ids in which the word > is present) into different files after reading 'X' number of pages. Now > these files may have the same word and hence I need to merge them and > write the final index again. Now these final indexes must be of limited > size as I need to be of limited size. This is where I am stuck. I need to > know how to determine the size of content in a variable before I write > into the file. > > Here is the code for my merging: > > def mergeFiles(pathOfFolder, countFile): > listOfWords={} > indexFile={} > topOfFile={} > flag=[0]*countFile > data=defaultdict(list) > heap=[] > countFinalFile=0 > for i in xrange(countFile): > fileName = pathOfFolder+'\index'+str(i)+'.txt.bz2' > indexFile[i]= bz2.BZ2File(fileName, 'rb') > flag[i]=1 > topOfFile[i]=indexFile[i].readline().strip() > listOfWords[i] = topOfFile[i].split(' ') > if listOfWords[i][0] not in heap: > heapq.heappush(heap, listOfWords[i][0]) At this point you have already done it wrong as your heap contains the complete data and you have done a lot of O(N) tests on the heap. This is both slow and consumes a lot of memory. See http://code.activestate.com/recipes/491285-iterator-merge/ for a sane way to merge sorted data from multiple files. Your code becomes (untested) with open("outfile.txt", "wb") as outfile: infiles = [] for i in xrange(countFile): filename = os.path.join(pathOfFolder, 'index'+str(i)+'.txt.bz2') infiles.append(bz2.BZ2File(filename, "rb")) outfile.writelines(imerge(*infiles)) for infile in infiles: infile.close() Once you have your data in a single file you can read from that file and do the postprocessing you mention below. > while any(flag)==1: > temp = heapq.heappop(heap) > for i in xrange(countFile): > if flag[i]==1: > if listOfWords[i][0]==temp: > > //This is where I am stuck. I cannot wait until memory > //error, as I need to do some postprocessing too. try: > data[temp].extend(listOfWords[i][1:]) > except MemoryError: > writeFinalIndex(data, countFinalFile, > pathOfFolder) data=defaultdict(list) > countFinalFile+=1 > > topOfFile[i]=indexFile[i].readline().strip() > if topOfFile[i]=='': > flag[i]=0 > indexFile[i].close() > os.remove(pathOfFolder+'\index'+str(i)+'.txt.bz2') > else: > listOfWords[i] = topOfFile[i].split(' ') > if listOfWords[i][0] not in heap: > heapq.heappush(heap, listOfWords[i][0]) > writeFinalIndex(data, countFinalFile, pathOfFolder) > > countFile is the number of files and writeFileIndex method writes into the > file.