2

I posted something similar to this question but I'm having trouble implementing the code.

I have a python program that collects data from HTML files that includes the weight, price, title of books etc. I want to sort the books into "n" packages without each package exceeding 10 pounds. I can run the program without errors and it extracts the information but I do not get any results on the packing.

Here is the code that I have, can anyone give me suggestions?

import glob
from bs4 import BeautifulSoup

class weight():
        def main():
            data = []
            for filename in glob.iglob('*.html'):
                with open(filename) as f:
                    soup = BeautifulSoup(f)

                    weight = soup.find('b', text='Shipping Weight:').next_sibling
                    data.append({})

                    return weight 

        def Bin(weight):
            def __init__(self):
                self.items = []
                self.sum = 0

            def append(self, item):
                self.items.append(item)
                self.sum += item

            def __str__(self):
                return 'Bin(sum=%d, items=%s)' % (self.sum, str(self.items))

            def pack(values, maxValue):
                values = sorted(values, reverse=True)
                bins = []

                for item in values:
            # Try to fit item into a bin
                    for bin in bins:
                        if bin.sum + item <= maxValue:
                            #print 'Adding', item, 'to', bin
                            bin.append(item)
                            break
                else:
                # item didn't fit into any bin, start a new bin
                #print 'Making new bin for', item
                    Bin = weight()
                    bin.append(item)
                    bins.append(bin)

                return bins

if __name__ == '__main__':
    import random

    def packAndShow(aList, maxValue):
        print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue, 'bins'

        bins = pack(aList, maxValue)

        print 'Solution using', len(bins), 'bins:'
        for bin in bins:
            print bin

        print

    def pack(values, maxValue):
        values = sorted(values, reverse=True)
        bins = []

        for item in values:
            # Try to fit item into a bin
                    for bin in bins:
                        if bin.sum + item <= maxValue:
                            #print 'Adding', item, 'to', bin
                            bin.append(item)
                            break
        else:
                # item didn't fit into any bin, start a new bin
                #print 'Making new bin for', item
                    Bin = weight()
                    bin.append(item)
                    bins.append(bin)

        return bins

    if __name__ == '__main__':
        import random

        def packAndShow(aList, maxValue):
            print 'List with sum', sum(aList), 'requires at least', (sum(aList)+maxValue-1)/maxValue, 'bins'

            bins = pack(aList, maxValue)

            print 'Solution using', len(bins), 'bins:'
            for bin in bins:
                print bin

            print
Community
  • 1
  • 1
DataTx
  • 1,839
  • 3
  • 26
  • 49
  • What does "I do not get any results on the packing" mean? What exactly is the problem with your code? Could you cut it down a bit (see [here](http://stackoverflow.com/help/mcve))? – jonrsharpe Apr 26 '14 at 19:04
  • Certainly. Basically I can extract the weight of books from amazon HTML files and I want to package these books into "n" packages without exceeding 10 pounds. I used the sorting algorithm from another thread (http://stackoverflow.com/questions/7392143/python-implementations-of-packing-algorithm) and the code runs but does not give any type of output. – DataTx Apr 26 '14 at 19:15
  • Could you review the indentation? `if __name__ == "__main__":` needs to be outsider the class if you want it to run. – jonrsharpe Apr 26 '14 at 19:31
  • @jonsharpe everything at if __name__ == "__main__": and below needs to be indented inwards to run properly? If that is done correctly, is the number of packages needed printed out? Do you know how would I get the package number to print out along with what books they contain? – DataTx Apr 26 '14 at 19:44
  • You seem to have duplication there, and never actually *call anything*. – jonrsharpe Apr 26 '14 at 20:31
  • ok understood im new to programming. Is there some simple way to add the weight of the books in sequential order until it reaches 10lbs? – DataTx Apr 26 '14 at 20:34
  • 1
    You don't get any results because nothing ever actually gets run. Move all `class` and `def` *before* `if __name__ ...`, then *call the functions* inside that `if` block. Also, `weight` isn't really a class (although it starts `class`) but `Bin` is (although it starts `def`). I think you need to start right back [here](https://docs.python.org/2/tutorial/). – jonrsharpe Apr 26 '14 at 20:39

1 Answers1

0

You define the class but the layout of your code never actually runs anything.

move everything after

if __name__ == "__main__": #(the first one)

BEFORE it under a function like so:

def main():
    paste the data from below

Then it will automatically run your code upon calling it.

Also I would seriously look into refocusing your code. Think smaller and simpler you're wasting a lot of keystrokes on things that could be optimized and eliminated. like defining an append function or creating class functions that are never used.

Best advice I can give you is to try something like this to get more familiar with some of the concepts. It'll eliminate a lot of the simple errors and make the debugging easier.

Amazingred
  • 1,007
  • 6
  • 14