I know it's been here a lot of times but I didn't find the right answer for my case.
First: I'm making an easy database system (for myself) that will run with no hashes etc. (for now at least). Now I got stuck.
import sys
import os
filename = ""
database = ""
path = ""
table = ""
class Nollty:
returns = 0
errors = 0
def __init__(self, filename, database):
self.filename = filename
self.database = database
self.path = self.filename + "databases/" + self.database
openfile = open(self.path + "/db_required", "r")
if not openfile.errors:
self.returns = 1
if not os.path.exists(self.path + "/db_required"):
self.returns = 0
openfile.close();
def select(self, table):
errors = 0
self.table = table
openfile = open(self.path + "/" + self.table, "r")
if not openfile.errors:
errors = 1
if not os.path.exists(self.path + "/" + self.table):
errors = 0
openfile.close();
nollty = Nollty("", "test")
if nollty.returns == 1:
print "Successfully connected to the database!"
query = nollty.select("aaa_auto")
if query.errors == 0:
print "Successfully chosen the table!"
The error output is:
Traceback (most recent call last):
File "/home/spotrudloff/Python/Nollty/nollty.py", line 40, in <module>
if query.errors == 0:
AttributeError: 'NoneType' object has no attribute 'errors'
The problem is probably that I'm PHP programmer, and I learnt Python today in a few hours (so my thinking is still "PHPy").
Thanks for all the responses.
Your use of returns and errors as class variables doesn't seem like a good idea. There is only one instance of each of those variables, no matter how many instances of Nollty you create. Instead, do this:
def __init__(self, filename, database):
self.returns = 0
self.errors = 0
# rest of __init__
Next, the use of returns to indicate a return value doesn't seem like a good idea either. In Python, one would normally raise an exception to indicate a problem in the constructor. That way, the caller can't simply ignore a problem by forgetting to check returns.
Similarly, use an exception from select() to indicate a problem with the parameter. My recommendation would be to eliminate both returns and errors.
You're also not returning any value at all from select, so that's why query ends up being None (in Python, None is a special value). You either want to return something useful from select(), or don't assign its result to anything if it doesn't return a useful value.
select() doesn't return an explicit value, so it has a NoneType return value. Change your code so that select() will return either 1 or 0, depending on the success or failure of your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With