So i've been attempting to program a home bar tending program that takes materials that you have in your home and compiles a list of drinks that you can possibly make. The problem I'm having is that i have no clue how to make the search function and the compiling function. As of now I have a simple list of materials that have been assigned to various numerical values ranging from 000 to 500. It looks something like this currently:

407 = 'Soy sauce'
408 = 'Sparkling white wine'
409 = 'Spiced rum'
410 = 'Sprite'
411 = 'Squirt'
412 = 'St. Hallvard'
413 = 'Stout'
414 = 'Strawberries'
415 = 'Strawberry juice'
416 = 'Strawberry liqueur'
417 = 'Strawberry schnapps'


I also have another file that contains the drink names which have been assigned to several of the numerical values in the materials file. It looks something like this:

'Ozone' = '1.5 oz' 017, 'Splash' 052, '3 oz' 424, 'Splash' 410
#which would translate to 1.5 oz of Amaretto, Splash of beer, 3 oz of Sweet and sour, Splash of Sprite

I have thought about rearranging the list so it is in a dictionary format but i'm not sure of how i'd link two corresponding dictionaries. If you guys have a better way to this I'm always open to changing my code. I am relatively new to python so anything would really help me. Thanks for your time.

Recommended Answers

All 4 Replies

I would recommend a class:

>>> class Drink(object):

   def __init__(self, name, ingr_list):
       self.ingr_list = ingr_list

   def __str__(self):
       s = ", ".join([self.ingr_list[x] + " of " + x for x in self.ingr_list])
       return s

>>> d = Drink("Ozone", {"Amaretto":"1.5 oz", "beer": "Splash", "Sweet and sour": "3 oz", "Sprite": "Splash"})
>>> print d
3 oz of Sweet and sour, Splash of beer, 1.5 oz of Amaretto, Splash of Sprite

That should be a lot easier to work with.

Jeff

ok i kind of get what the code is saying but do you mind breaking it down for me. Also how would I fit this into a GUI and where does the list of ingredients come in if at all. This is what I'm working with as of now:

from Tkinter import *;
from ScrolledText import *;
 
# Initialize program components:
def init():
    # Create and configure the main window:
    global window;
    window = Tk();
    window.title('Be Your Own Bartender');
    createWidgets();
    positionWidgets();
    configureWidgets();
    # Display the window:
    window.mainloop();
 
def createWidgets():
    global inputMats; inputMats = Label(window)
    global inputEntry; inputEntry = Entry(window)
    global submitButton; submitButton = Button(window)
    global scrolledText; scrolledText = ScrolledText(window)
    global clearButton; clearButton = Button(window)
def positionWidgets():
    global clearButton
    clearButton.grid(row = 0, column = 3, padx = 3, pady = 3)
    global inputMats, inputEntry
    inputMats.grid(row = 0, column = 0, padx = 3, pady = 3)
    inputEntry.grid(row = 0, column = 1, padx = 3, pady = 3)
    global submitButton
    submitButton.grid(row = 0, column = 2, padx = 3)
    global scrolledText
    scrolledText.grid(row = 3, column = 0, padx = 3, pady = 3, columnspan = 4)
def configureWidgets():
    global inputText, inputMats, inputEntry;
    inputText = StringVar(); inputText.set('0');
    inputEnrty.configure(
        textvariable = inputText, width = 20);
    inputMats.configure(text = 'Your Materials', width = 11, borderwidth = 2);
    global scrolledText
    scrolledText.configure(borderwidth = 2, relief = SUNKEN)
    global submitButton, clearButton;
    submitButton.configure(text = 'Submit', width = 10, command = submitAction);
    clearButton.configure(text = 'Clear', width = 10, command = clearAction);


#This retreaves the mats for the drinks.
def sumbitAction():
    global inputEntry;
    

#This clears the current drinks.
def clearAction():
    global inputEntry, scrolledText;
    inputEntry.set('0');
    scrolledText.delete('1.0', END)


# Start execution:
init();

With all those semi-colons, I'm betting you're a C programmer by habit. :) The semis are unnecessary.

If you are coming from a pure C background, you'll need to start thinking in terms of classes and objects. Here's how:

from Tkinter import *
from ScrolledText import *
 
# Initialize program components:
class MyFrame(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.createWidgets()
        self.positionWidgets()
        self.configureWidgets()

    def createWidgets(self):
        self.inputMats = Label(self)
        self.inputEntry = Entry(self)
        self.submitButton = Button(self)
        self.scrolledText = ScrolledText(self)
        self.clearButton = Button(self, text="Hi")

    def positionWidgets(self):
        self.inputMats.grid(row = 0, column = 0, padx = 3, pady = 3)
        self.inputEntry.grid(row = 0, column = 1, padx = 3, pady = 3)
        self.submitButton.grid(row = 0, column = 2, padx = 3)
        self.scrolledText.grid(row = 3, column = 0,
                               padx = 3, pady = 3, columnspan = 4)
        self.clearButton.grid(row = 0, column = 3, padx = 3, pady = 3)
        
    def configureWidgets(self):
        pass
        
        
app = Tk()
app.frame=MyFrame(app)
app.frame.grid()
app.mainloop()

Notice that all of the global variables are gone. That's intentional; you should rarely if ever need them.

Instead, your application frame is an object that takes care of itself through the methods createWidgets(), etc.

The code proper begins in line 32. An app is created, then an application frame, and then the whole thing is turned loose.

Hope that helps,
Jeff

Now about the code I posted in #2:

* the first line defines a class called "Drink" that serves as a template for all Drink objects. You can create as many Drinks as you want, and they will all have separate namespaces (heaps) from each other.

* The __init__(self, ...) method is one of two "magic methods" that are commonly used in Python. __init__() is automagically called every time a new object is created (in C++ parlance, it's a constructor). In this general, __init__'s job is to set up the object's data properties.
* __str__ is the other "magic method" you need to know about. It is automagically called any time you print an object or ask for a str() representation of that object. It simply returns whatever string you tell it to return.

* "d = Drink("Ozone", {"Amaretto":"1.5 oz", "beer": "Splash", "Sweet and sour": "3 oz", "Sprite": "Splash"})"

this creates a new Drink object. The parameters, the string and the dictionary, are passed to __init__, who knows what to do with them: the string becomes d.name, while the dictionary becomes d.ingr.

Notice that inside methods, the object's name is "self", whereas in your main code you will always supply the real name of the object: "d" in this case.

* "print d" then automagically calls d.__str__(), which returns the string that you see on the screen.

Here's a general idea of how they would hook into your GUI:

* The GUI will allow the user to specify drink name and ingredients. When the user is done, he will hit the submitButton. The callback for the submitButton (all buttons have callback functions attached to them -- check out the Tkinter manual) will then get the name and ingredients from the widgets and create a new Drink() object from those. And then you add that Drink() to the database.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.