Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code printing twice
#1
I'm making a Discord.py bot with a utilities package. In the utilities package, there are 3 files which are together causing the logic error.

""" Database.py ----------- Database format: { "SectionNameHere" : { "foo" : "bar" }, "2ndSectionNameHere" : { "foo" : "bar" }, "3rdSectionNameHere" : { "foo" : "bar" } } """ import json class database: def __init__(self, *args, **kwargs): self.args = args self.kwargs = kwargs self.data = None def set(self): with open("Resources/database.json", "r") as DB: self.clean() self._data = DB.read() self.data = json.loads(str(self._data)) def clean(self): with open("Resources/database.json", "r") as fData: oData = fData.read() with open("Resources.json", "w") as wData: wData.write(oData.replace("'", "\"")) def init(self): self.clean() self.set()
(database.json)
{ "userdata" : { "help" : "nope" } }
(Giveaway.py)
import random import Utilities.database as database class giveaway: def __init__(self, Name): self.reward = Name self.contestants = [] def contestantJoin(self, user): self.contestants.append(user) def chooseWinner(self): return random.choice(self.contestants) def delete(self): del self # If a giveaway is saved in memory, load it automaticly db = database.database() db.init() print(db.data["userdata"]["help"])
Now this should print Nope once, but instead it prints Nope twice.
Output:
nope nope
I'm using python 3.6 Win7. Any help would be appreciated! Thanks in advance!
Reply
#2
Works fine for me (2.7 and 3.6). I did put all the code in one file, and changed the file names, but I don't think that should have changed anything. How exactly are you calling the program?
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
what is the name of the first program.
Too many things named database, makes it confusing.
this code in giveaway.py will always run.
# If a giveaway is saved in memory, load it automaticly db = database.database() db.init() print(db.data["userdata"]["help"])
if you only want this to run if the program is executed from command routine, change to:
if __name__ == '__main__': # If a giveaway is saved in memory, load it automaticly db = database.database() db.init() print(db.data["userdata"]["help"])
some other items, indentation should be 4.
set is a bad name for a method as it as a python command.
you should add an empty line between methods to make code more readable.
Reply
#4
Quote:if you only want this to run if the program is executed from command routine, change to:
if __name__ == '__main__': # If a giveaway is saved in memory, load it automaticly db = database.database() db.init() print(db.data["userdata"]["help"])
Suprisingly enough, the if '__name__' == '__main__': fixed it.

Quote:Works fine for me (2.7 and 3.6). I did put all the code in one file, and changed the file names, but I don't think that should have changed anything. How exactly are you calling the program?
I'm running it from importing a different file.

(init.py)
initmsg = ["``dsconfig\nInitializing Utilities (Spooper/Utilities/__init__.py)\n\n"] initmsg.append("Initializing Database.py (Spooper/Utilities/database.py)\n") import Utilities.database as database initmsg.append("Finished Initializing Database.py (Spooper/Utilities/database.py)\n\n") initmsg.append("Initializing Giveaway.py (Spooper/Utilities/giveaway.py)\n") import Utilities.giveaway as giveaway initmsg.append("Finished Initializing Giveaway.py (Spooper/Utilities/database.py)\n\n") initmsg.append("Finished Initializing Utilities (Spooper/Utilities/__init__.py)\n") initmsg.append("\n``") initmsg = ''.join(initmsg)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing the code line number arbiel 6 2,484 Jun-30-2024, 08:01 AM
Last Post: arbiel
  I'm not sure why this code isn't printing... Can someone tell me? Wilminer4 5 5,683 Mar-01-2018, 06:58 PM
Last Post: Wilminer4

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.