Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Impossible error
#1
Ok, to start, this code WORKED perfectly fine:
from flask import Flask, render_template
from random import randint
from math import floor
import csv
import json

app = Flask(__name__)

@app.route("/")
def main():
	cards = GetCards()
	maxValue = GetMaxValue()
	return render_template("main.html", cards=cards, maxValue=maxValue)

if __name__ == "__main__":
	app.run()

class CardObject(object):
	name = '';
	cardSet = '';
	rarity = '';
	isFoil = False;
	qty = 0;
	buyValue = 0.00;
	sellValue = 0.00;
	color = '';
	number = 0;

class PackObject(object):
	name = '';
	cardSet = '';
	isFoil = False;
	buyValue = 0.00;
	subtractedFromCollection = False;

def createCardObject(name,cardSet,rarity,isFoil,qty,buyValue,sellValue,color,number):
	cardObject = CardObject();
	cardObject.name = name;
	cardObject.cardSet = cardSet;
	cardObject.rarity = rarity;
	cardObject.isFoil = isFoil;
	cardObject.qty = qty;
	cardObject.buyValue = buyValue;
	cardObject.sellValue = sellValue;
	cardObject.color = color;
	cardObject.number = number;
	return cardObject;

def createPackObject(name,cardSet,buyValue,isFoil,subtractedFromCollection):
	packObject = PackObject();
	packObject.name = name;
	packObject.cardSet = cardSet;
	packObject.isFoil = isFoil;
	packObject.buyValue = buyValue;
	packObject.subtractedFromCollection = subtractedFromCollection;
	return packObject;

def importCollection():
        csvarray = [];
	with open('/var/www/DemonicaGames/DemonicaGames/static/MTG UMA.csv') as csv_file:
                #The next line is only commented out because the editor sees it as an open quote and makes the script harder to read
                #csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"');
                for row in csv_reader:
                        csvarray.append(','.join(row));
	collection = [];

	for x in range(len(csvarray)):
		striplist = csvarray[x].strip('\n');
		name = striplist.split(',')[0];
		cardSet = striplist.split(',')[1];
		rarity = striplist.split(',')[2];
        qty = striplist.split(',')[3];
		buyValue = striplist.split(',')[4];
		isFoil = striplist.split(',')[5];
		color = striplist.split(',')[6];
		number = striplist.split(',')[7];
		sellValue = striplist.split(',')[8];
		card = createCardObject(name,cardSet,rarity,isFoil,qty,buyValue,sellValue,color,number);
		collection.append(card);

	return collection;

def GetCards():

	commonList = [];
	uncommonList = [];
	rareList = [];
	overStock = [];
	sets = [];
	pack = [];
	nonFoil = [];
	common1 = '';
	common2 = '';
	common3 = '';
	common4 = '';
	common5 = '';
	uncommon1 = '';
	uncommon2 = '';
	uncommon3 = '';
	rare = '';
	commonSet1 = '';
	commonSet2 = '';
	commonSet3 = '';
	commonSet4 = '';
	commonSet5 = '';
	uncommonSet1 = '';
	uncommonSet2 = '';
	uncommonSet3 = '';
	rareSet = '';
	name = '';
	test = '';
	commonIsFoil1 = False;
	commonIsFoil2 = False;
	commonIsFoil3 = False;
	commonIsFoil4 = False;
	commonIsFoil5 = False;
	uncommonIsFoil1 = False;
	uncommonIsFoil2 = False;
	uncommonIsFoil3 = False;
	rareIsFoil = False;
	complete = 0;
	total = 0;
	commonPrice1 = 0.00;
	commonPrice2 = 0.00;
	commonPrice3 = 0.00;
	commonPrice4 = 0.00;
	commonPrice5 = 0.00;
	uncommonPrice1 = 0.00;
	uncommonPrice2 = 0.00;
	uncommonPrice3 = 0.00;
	rarePrice = 0.00;
	maxValue = GetMaxValue();
        packValue = 0.00;


	while complete != 2:

		complete = 0;
		fullCollection = importCollection();

		for lngRecordCounter in range(len(fullCollection)):
			overStock.append(fullCollection[lngRecordCounter]);
			
                if (randint(1,100) <= 99):
                    i = 0;                       
                    while i < len(overStock):
                        if overStock[i].isFoil == True:
                            del overStock[i];
                        else:
                            i += 1;
                            
		for lngRecordCounter in range(len(overStock)):
			if overStock[lngRecordCounter].rarity == 'C':
				commonList.append(overStock[lngRecordCounter]);
		for lngRecordCounter in range(len(overStock)):
			if overStock[lngRecordCounter].rarity == 'U':
				uncommonList.append(overStock[lngRecordCounter]);
		for lngRecordCounter in range(len(overStock)):
			if overStock[lngRecordCounter].rarity == 'R' or overStock[lngRecordCounter].rarity == 'M':
				rareList.append(overStock[lngRecordCounter]);

                random = randint(0,len(commonList));
                common1 = commonList[random].name;
                commonSet1 = commonList[random].cardSet;
                commonIsFoil1 = commonList[random].isFoil;
                commonPrice1 = commonList[random].buyValue;

                i = 0;
                name = common1.split('(')[0];
                while i < len(commonList):
                    test = commonList[i].name.split('(')[0];
                    if test == name:
                        del commonList[i];
                    else:
                        i += 1;

                random = randint(0,len(commonList));
                common2 = commonList[random].name;
                commonSet2 = commonList[random].cardSet;
                commonIsFoil2 = commonList[random].isFoil;
                commonPrice2 = commonList[random].buyValue;

                i = 0;                       
                name = common2.split('(')[0];
                while i < len(commonList):
                    test = commonList[i].name.split('(')[0];
                    if test == name:
                        del commonList[i];
                    else:
                        i += 1;

                random = randint(0,len(commonList));
                common3 = commonList[random].name;
                commonSet3 = commonList[random].cardSet;
                commonIsFoil3 = commonList[random].isFoil;
                commonPrice3 = commonList[random].buyValue;

                i = 0;                       
                name = common3.split('(')[0];
                while i < len(commonList):
                    test = commonList[i].name.split('(')[0];
                    if test == name:
                        del commonList[i];
                    else:
                        i += 1;

                random = randint(0,len(commonList));
                common4 = commonList[random].name;
                commonSet4 = commonList[random].cardSet;
                commonIsFoil4 = commonList[random].isFoil;
                commonPrice4 = commonList[random].buyValue;

                i = 0;                       
                name = common4.split('(')[0];
                while i < len(commonList):
                    test = commonList[i].name.split('(')[0];
                    if test == name:
                        del commonList[i];
                    else:
                        i += 1;

                random = randint(0,len(commonList));
                common5 = commonList[random].name;
                commonSet5 = commonList[random].cardSet;
                commonIsFoil5 = commonList[random].isFoil;
                commonPrice5 = commonList[random].buyValue;

                random = randint(0,len(uncommonList));
                uncommon1 = uncommonList[random].name;
                uncommonSet1 = uncommonList[random].cardSet;
                uncommonIsFoil1 = uncommonList[random].isFoil;
                uncommonPrice1 = uncommonList[random].buyValue;

                i = 0;                       
                name = uncommon1.split('(')[0];
                while i < len(uncommonList):
                    test = uncommonList[i].name.split('(')[0];
                    if test == name:
                        del uncommonList[i];
                    else:
                        i += 1;

                random = randint(0,len(uncommonList));
                uncommon2 = uncommonList[random].name;
                uncommonSet2 = uncommonList[random].cardSet;
                uncommonIsFoil2 = uncommonList[random].isFoil;
                uncommonPrice2 = uncommonList[random].buyValue;

                i = 0;                       
                name = uncommon2.split('(')[0];
                while i < len(uncommonList):
                    test = uncommonList[i].name.split('(')[0];
                    if test == name:
                        del uncommonList[i];
                    else:
                        i += 1;

                random = randint(0,len(uncommonList));
                uncommon3 = uncommonList[random].name;
                uncommonSet3 = uncommonList[random].cardSet;
                uncommonIsFoil3 = commonList[random].isFoil;
                uncommonPrice3 = uncommonList[random].buyValue;

                random = randint(0,len(rareList));
                rare = rareList[random].name;
                rareSet = rareList[random].cardSet;
                rareIsFoil = rareList[random].isFoil;
                rarePrice = rareList[random].buyValue;

                packValue = float(commonPrice1) + float(commonPrice2) + float(commonPrice3) + float(commonPrice4) + float(commonPrice5) + float(uncommonPrice1) + float(uncommonPrice2) + float(uncommonPrice3) + float(rarePrice);

                sets.append(commonSet1);
                sets.append(commonSet2);
                sets.append(commonSet3);
                sets.append(commonSet4);
                sets.append(commonSet5);
                sets.append(uncommonSet1);
                sets.append(uncommonSet2);
                sets.append(uncommonSet3);
                sets.append(rareSet);

                for i in range(len(sets)):
                    if sets[i] == "LEA" or sets[i] == "LEB" or sets[i] == "UNL" or sets[i] == "ARN" or sets[i] == "ATQ" or sets[i] == "REV" or sets[i] == "LEG" or sets[i] == "DRK" or sets[i] == "FEM":
                        complete += 1;
                        break;

                if packValue < maxValue:
                    complete += 1;

        pack.append(common1 + ", " + commonSet1 + ", $" + commonPrice1 + ", " + commonIsFoil1);
        pack.append(common2 + ", " + commonSet2 + ", $" + commonPrice2 + ", " + commonIsFoil2);
        pack.append(common3 + ", " + commonSet3 + ", $" + commonPrice3 + ", " + commonIsFoil3);
        pack.append(common4 + ", " + commonSet4 + ", $" + commonPrice4 + ", " + commonIsFoil4);
        pack.append(common5 + ", " + commonSet5 + ", $" + commonPrice5 + ", " + commonIsFoil5);
        pack.append(uncommon1 + ", " + uncommonSet1 + ", $" + uncommonPrice1 + ", " + uncommonIsFoil1);
        pack.append(uncommon2 + ", " + uncommonSet2 + ", $" + uncommonPrice2 + ", " + uncommonIsFoil2);
        pack.append(uncommon3 + ", " + uncommonSet3 + ", $" + uncommonPrice3 + ", " + uncommonIsFoil3);
        pack.append(rare + ", " + rareSet + ", $" + rarePrice + ", " + rareIsFoil);
        pack.append("Pack Value = $" + str(packValue));

	return pack;

def GetMaxValue():
        deductions = GetDeductions();
	totalSold = 0;
	valueSold = totalSold * 4.99;
	if valueSold - deductions < 9.98:
                maxValue = 9.98;
        else:
                maxValue = valueSold - deductions;
        return maxValue;

def GetDeductions():
        csvarray = [];
	total = 0.00;
	#read in values from csv and create an array
	with open('/var/www/DemonicaGames/DemonicaGames/static/deductions.csv') as csv_file:
                #csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"');
                for row in csv_reader:
                        csvarray.append(', '.join(row));

	for x in range(len(csvarray)):
		striplist = csvarray[x].strip('\n');
		name = striplist.split(',')[0];
		cardSet = striplist.split(',')[1];
		buyValue = striplist.split(',')[2];
		if float(buyValue) >= 4.99:
                       total += float(buyValue);
		isFoil = striplist.split(',')[3];
	return total;
Again, my website worked perfect with the previous code, now I updated some of the code to make it simpler and easier to read by making GetCard() a returnable function and renaming the original GetCards() to Repack():
from flask import Flask, render_template
from random import randint
from math import floor
import csv
import json

app = Flask(__name__)

@app.route("/")
def main():
	cards = Repack()
	maxValue = GetMaxValue()
	return render_template("main.html", cards=cards, maxValue=maxValue)

if __name__ == "__main__":
	app.run()

class CardObject(object):
	name = '';
	cardSet = '';
	rarity = '';
	isFoil = False;
	qty = 0;
	buyValue = 0.00;
	sellValue = 0.00;
	color = '';
	number = 0;

class PackObject(object):
	name = '';
	cardSet = '';
	isFoil = False;
	buyValue = 0.00;
	subtractedFromCollection = False;

def createCardObject(name,cardSet,rarity,isFoil,qty,buyValue,sellValue,color,number):
	cardObject = CardObject();
	cardObject.name = name;
	cardObject.cardSet = cardSet;
	cardObject.rarity = rarity;
	cardObject.isFoil = isFoil;
	cardObject.qty = qty;
	cardObject.buyValue = buyValue;
	cardObject.sellValue = sellValue;
	cardObject.color = color;
	cardObject.number = number;
	return cardObject;

def createPackObject(name,cardSet,buyValue,isFoil,subtractedFromCollection):
	packObject = PackObject();
	packObject.name = name;
	packObject.cardSet = cardSet;
	packObject.isFoil = isFoil;
	packObject.buyValue = buyValue;
	packObject.subtractedFromCollection = subtractedFromCollection;
	return packObject;

def importCollection():
	csvarray = [];
	
	with open('/var/www/DemonicaGames/DemonicaGames/static/MTG UMA.csv') as csv_file:
		#csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"');
		for row in csv_reader:
			csvarray.append(','.join(row));
	
	collection = [];

	for x in range(len(csvarray)):
		striplist = csvarray[x].strip('\n');
		name = striplist.split(',')[0];
		cardSet = striplist.split(',')[1];
		rarity = striplist.split(',')[2];
		qty = striplist.split(',')[3];
		buyValue = striplist.split(',')[4];
		isFoil = striplist.split(',')[5];
		color = striplist.split(',')[6];
		number = striplist.split(',')[7];
		sellValue = striplist.split(',')[8];
		card = createCardObject(name,cardSet,rarity,isFoil,qty,buyValue,sellValue,color,number);
		collection.append(card);

	return collection;

def Repack():

	tradeable = [];
	sets = [];
	pack = [];
	#[0] = Name, [1] = Set, [2] = IsFoil, [3] = BuyValue, [4] = IsDeductible
	common1 = [];
	common2 = [];
	common3 = [];
	common4 = [];
	common5 = [];
	uncommon1 = [];
	uncommon2 = [];
	uncommon3 = [];
	rare = [];
	complete = 0;
	total = 0;
	maxValue = GetMaxValue();
	packValue = 0.00;


	while complete != 2:

		complete = 0;
		fullCollection = importCollection();

		lngListCounter = 0
		for lngRecordCounter in range(len(fullCollection)):
			try:
				if int(fullCollection[lngRecordCounter].qty) > 4 and fullCollection[lngRecordCounter].rarity != '':
					tradeable.append(fullCollection[lngRecordCounter]);
					lngListCounter += 1;
			except ValueError:
				pass;
			
		if (randint(1,100) <= 99):
			i = 0;                       
			while i < len(tradeable):
				if tradeable[i].isFoil == True:
					del tradeable[i];
				else:
					i += 1;

		common1 = GetCard(tradeable, 'C')
		common2 = GetCard(tradeable, 'C')
		common3 = GetCard(tradeable, 'C')
		common4 = GetCard(tradeable, 'C')
		common5 = GetCard(tradeable, 'C')

		uncommon1 = GetCard(tradeable, 'U')
		uncommon2 = GetCard(tradeable, 'U')
		uncommon3 = GetCard(tradeable, 'U')

		rare = GetCard(tradeable, 'R')

		packValue = float(common1[3]) + float(common2[3]) + float(common3[3]) + float(common4[3]) + float(common5[3]) + float(uncommon1[3]) + float(uncommon2[3]) + float(uncommon3[3]) + float(rare[3]);

		sets.append(common1[1]);
		sets.append(common2[1]);
		sets.append(common3[1]);
		sets.append(common4[1]);
		sets.append(common5[1]);
		sets.append(uncommon1[1]);
		sets.append(uncommon2[1]);
		sets.append(uncommon3[1]);
		sets.append(rare[1]);

		for i in range(len(sets)):
			if sets[i] == "LEA" or sets[i] == "LEB" or sets[i] == "UNL" or sets[i] == "ARN" or sets[i] == "ATQ" or sets[i] == "REV" or sets[i] == "LEG" or sets[i] == "DRK" or sets[i] == "FEM":
				complete += 1;
				break;

		if packValue < maxValue:
			complete += 1;

		pack.append(common1[0] + ', ' + common1[1] + ', $' + common1[3] + ', ' + common1[2]);
		pack.append(common2[0] + ', ' + common2[1] + ', $' + common2[3] + ', ' + common2[2]);
		pack.append(common3[0] + ', ' + common3[1] + ', $' + common3[3] + ', ' + common3[2]);
		pack.append(common4[0] + ', ' + common4[1] + ', $' + common4[3] + ', ' + common4[2]);
		pack.append(common5[0] + ', ' + common5[1] + ', $' + common5[3] + ', ' + common5[2]);
		pack.append(uncommon1[0] + ', ' + uncommon1[1] + ', $' + uncommon1[3] + ', ' + uncommon1[2]);
		pack.append(uncommon2[0] + ', ' + uncommon2[1] + ', $' + uncommon2[3] + ', ' + uncommon2[2]);
		pack.append(uncommon3[0] + ', ' + uncommon3[1] + ', $' + uncommon3[3] + ', ' + uncommon3[2]);
		pack.append(rare[0] + ', ' + rare[1] + ', $' + rare[3] + ', ' + rare[2]);
		pack.append("Pack Value = $" + str(packValue));

	return pack;
	
def GetCard(collection, rarity):

	name = ''
	test = ''
	commonList = []
	uncommonList = []
	rareList = []
	common = [None] * 4
	uncommon = [None] * 4
	rare = [None] * 4
	
	if rarity == 'C':
		for lngRecordCounter in range(len(collection)):
			if collection[lngRecordCounter].rarity == 'C':
				commonList.append(collection[lngRecordCounter]);

		random = randint(0,len(commonList) - 1);
		common[0] = commonList[random].name;
		common[1] = commonList[random].cardSet;
		common[2] = commonList[random].isFoil;
		common[3] = commonList[random].buyValue;

		i = 0;
		name = common[0].split('(')[0];
		while i < len(commonList):
			test = commonList[i].name.split('(')[0];
			if test == name:
				del commonList[i];
			else:
				i += 1;

		return common
		
	elif rarity == 'U':
		for lngRecordCounter in range(len(collection)):
			if collection[lngRecordCounter].rarity == 'U':
				uncommonList.append(collection[lngRecordCounter]);

		random = randint(0,len(uncommonList) - 1);
		uncommon[0] = uncommonList[random].name;
		uncommon[1] = uncommonList[random].cardSet;
		uncommon[2] = uncommonList[random].isFoil;
		uncommon[3] = uncommonList[random].buyValue;

		i = 0;
		name = uncommon[0].split('(')[0];
		while i < len(uncommonList):
			test = uncommonList[i].name.split('(')[0];
			if test == name:
				del uncommonList[i];
			else:
				i += 1;

		return uncommon
		
	elif rarity == 'R':
		for lngRecordCounter in range(len(collection)):
			if collection[lngRecordCounter].rarity == 'R' or collection[lngRecordCounter].rarity == 'M':
				rareList.append(collection[lngRecordCounter]);

		random = randint(0,len(rareList) - 1);
		rare[0] = rareList[random].name;
		rare[1] = rareList[random].cardSet;
		rare[2] = rareList[random].isFoil;
		rare[3] = rareList[random].buyValue;

		i = 0;
		name = rare[0].split('(')[0];
		while i < len(rareList):
			test = rareList[i].name.split('(')[0];
			if test == name:
				del rareList[i];
			else:
				i += 1;

		return rare
def GetMaxValue():
	deductions = GetDeductions();
	totalSold = 0;
	valueSold = totalSold * 4.99;
	if valueSold - deductions < 9.98:
		maxValue = 9.98;
	else:
		maxValue = valueSold - deductions;
	return maxValue;

def GetDeductions():
	csvarray = [];
	total = 0.00;
	#read in values from csv and create an array
	with open('/var/www/DemonicaGames/DemonicaGames/static/deductions.csv') as csv_file:
		#csv_reader = csv.reader(csv_file, delimiter=',', quotechar='"');
	    for row in csv_reader:
		    csvarray.append(', '.join(row));

	for x in range(len(csvarray)):
		striplist = csvarray[x].strip('\n');
		name = striplist.split(',')[0];
		cardSet = striplist.split(',')[1];
		buyValue = striplist.split(',')[2];
		if float(buyValue) >= 4.99:
			total += float(buyValue);
		isFoil = striplist.split(',')[3];
	return total;
Now the fun begins, here is my error:

ERROR:flask.app:Exception on / [GET]
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/var/www/DemonicaGames/DemonicaGames/__init__.py", line 11, in main
cards = Repack()
NameError: global name 'GetCards' is not defined !?!?!?!?!

How is this an error? 'GetCards' is not defined because 'GetCards' no longer exists within the script. How am I supposed to define something that doesn't exist?

But wait, it gets better, I didn't just overwrite my original script, I saved it just in case something stupid like this came up, a script that has been running fine for a few months now. So I did what any debugger would do and went back to the original to start from a known working point.

Nope, same exact error WITH 'GetCards' defined.

File "/var/www/DemonicaGames/DemonicaGames/__init__.py", line 11, in main
cards = GetCards()
NameError: global name 'GetCards' is not defined

How am I supposed to get around something so stupid? Is this a Flask issue? A Python issue?

My apologizes for the length and the amount of text, I didn't see any other way that wouldn't just waste more of my time. I just watched all of my work take a heaping crap on itself and I'm quite frustrated.

Please keep in mind that this was a working script that was up and running on a website of mine and now that exact working script is somehow erroneous.
Reply
#2
Well, I'm an idiot. All that because I forgot to reload apache, I'm guessing that anything that relates directly to the flask side of the coding requires me to update my apache server, otherwise it gives me seemingly impossible errors.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  page impossible to scrap? :O zarize 2 3,932 Oct-03-2019, 02:44 PM
Last Post: zarize

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020