Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I pickled my tuple

I'm still new to python. I'm working the framework for a larger project. This program makes you think of either a circle or square, then it ask four questions, then decides on an answer.

I'm on the last step of the framework, but ran into a problem. I get "global name 'qas1' is not defined"

Line 50 in getQuestion question = 'qas' Global name 'qas' is not defined

This happend when I tried to pickle my tuples.

Here is my loading program to create the pickle file that contains my tuples:

import cPickle
import os

qas1 = [
('Are you more like Waffle or a Pancake'), 
('1. Waffle', 1, 0),
('2. Pancake', 0, 1)
]

qas2 = [
('Do you have a straight edge?'),
('1. Yes', 1, 0),
('2. No', 0, 1)
]

qas3 = [
('Are you simliar in shape to a lolipop?'),
('1. Yes', 0, 1),
('2. No', 1, 0)
]
qas4 = [
('Is the world rounded like a planet, or flat like a map?'),
('1. Rounded', 0, 1),
("2. Flat", 1, 0)
]

def hasFile():
    print 'I see the file'
    qas_file = open('qas.dat', 'r')
    qas1 = cPickle.load(qas_file)
    qas2 = cPickle.load(qas_file)
    qas3 = cPickle.load(qas_file)
    qas4 = cPickle.load(qas_file)
    qas_file.close
    confirmer()

def noFile():
    print 'I dont see a file...'
    saver()

def confirmer():
    print qas1
    print qas2
    print qas3
    print qas4

def saver():
    qas_file = open('qas.dat', 'w')
    print 'No worries, creating one now'
    cPickle.dump(qas1, qas_file)
    cPickle.dump(qas2, qas_file)
    cPickle.dump(qas3, qas_file)
    cPickle.dump(qas4, qas_file)
    qas_file.close
    print 'all done'

fname = "qas.dat"
if os.path.isfile(fname):
    hasFile()
else:
    noFile()

The code worked okay, but when I tried to use the file that it created I ran into problems.

import cPickle

#Counters
counterCircle = 0
counterSquare = 0

# tuples
def hasFile():
    print 'I see the file'
    qas_file = open('qas.dat', 'r')
    qas1 = cPickle.load(qas_file)
    qas2 = cPickle.load(qas_file)
    qas3 = cPickle.load(qas_file)
    qas4 = cPickle.load(qas_file)
    qas_file.close



#varibles Im made to assign
messageDisplayed = 0
question = 'beer'

#prints to screen   
def showQaA():
    print question[0]
    print question[1][0]
    print question[2][0]

#recieves and implements responses
def getResponce():
    global counterCircle
    global counterSquare
    global qas1, qas2, qas3, qas4
    ansew = raw_input('>> ')
    if ansew == "1":
        counterSquare = counterSquare + question[1][1]#(+1)
        counterCircle = counterCircle + question[1][2]#(+0)
    elif ansew == "2":
        counterSquare = counterSquare + question[2][1]#(+0)
        counterCircle = counterCircle + question[2][2]#(+1)
    print counterCircle
    print counterSquare


#Gets the current tuple infomation to display (Will be more advanced)
def getQuestion():
    global question
    if messageDisplayed == 0:
        question = qas1
    elif messageDisplayed == 1:
        question = qas2
    elif messageDisplayed == 2:
        question = qas3
    elif messageDisplayed == 3:
        question = qas4
    else:
        print 'youre screwd'

#figures out and prints results
def results():
    print "This is the circle results", counterCircle
    print "This is the square results", counterSquare
    if counterSquare < counterCircle:
        print "You are a circle!"
    elif counterSquare > counterCircle:
        print "You are a square!"
    else:
        print "You are the elusive squircle!"

#mainLoop currently set to 4 questions
hasFile()
while messageDisplayed <=3:
    getQuestion()
    showQaA()
    getResponce()
    messageDisplayed +=1
results()

It is a lot of code to look at, I know. When the program first loads the name qas1 it reconizes that it is a tuple, but when I try to pass the properties to 'question' in getQuestion(): it forgets what they were. Any ideals what the problem is?

like image 794
user2922016 Avatar asked Mar 31 '14 02:03

user2922016


People also ask

Can you pickle a tuple?

The pickle library will pickle each component of the tuple separately, and will call the callable on the provided arguments to construct the new object during the process of unpickling.

What is pickled object?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

What does pickling do Python?

Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to convert a python object (list, dict, etc.)

Does pickle compress?

Pickling doesn't compress data — Pickling an object won't compress it. Sure, the file will be smaller when compared to CSV, but you can compress the data manually for maximum efficiency.


1 Answers

In your second file, qas[1-4] are all local to the hasFile function. Make them global, and your code will work:

def hasFile():
   global qas1, qas2, qas3, qas4
   # etc

The same bug occurs in the first code, but it is harder to notice - the four variables are still assigned only in the function - this shadows the identically-named global variables, leaving them unchanged. However, since loading them isn't expected to change their content, it appears to work.

like image 149
lvc Avatar answered Oct 17 '22 07:10

lvc