I can't seem to figure out what is causing the below attribute error. I am really not sure what I am doing wrong here. Any help would be great!
Traceback (most recent call last):
File "project2.py", line 140, in
movie_writter.writerow([movie.title, movie.author, movie.itunes_id, movie.itunes_URL, movie.length])
AttributeError: 'Movie' object has no attribute 'length'
class Movie(Media):
def __init__(self, diction_file):
Media.__init__(self, diction_file)
self.rating = diction_file['contentAdvisoryRating']
self.genre = diction_file['primaryGenreName']
try:
self.track_time = diction_file['trackTimeMillis']
except:
self.track_time = 0
try:
self.description = diction_file['longDescription']
except:
self.description = None
def __len__(self):
return self.track_time/1000
def title_words_num(self):
if self.description != None:
return len(self.description.split())
else:
return 0
movie_list = [Movie(diction_file) for diction_file in movie_samples]
with open('movies.csv', 'w', newline = '') as movies_csv:
movie_writter = csv.writer(movies_csv, delimiter=',')
movie_writter.writerow(["Title", "Artist", "ID", "URL", "Length"])
for movie in movie_list:
movie_writter.writerow([movie.title, movie.author, movie.itunes_id, movie.itunes_URL, movie.length])
movies_csv.close()
It means what it means: the object has no attribute length. You probably meant len(movie), which maps to __len__ magic method.
Also, keep in mind, this is not the best use for the __len__ method. It is supposed to be used for collections/containers of any kind, to show the amout of items inside. The movie duration is not an amount of things.
Related
Traceback (most recent call last):
File "C:\Users\RAC\crypto\...\blockchain.py", line 178, in <module>
blockchain = Blockchain()
^^^^^^^^^^^^
File "C:\Users\RAC\crypto\...\blockchain.py", line 49, in __init__
self.chain = [self.create_genesis_block(0)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: Blockchain.create_genesis_block() takes 1 positional argument but 2 were given
with code looking like this
class Blockchain:
def __init__(self):
self.chain = [self.create_genesis_block(0)]
self.difficulty = 4
self.nodes = dict()
self.replicated_nodes = dict()
self.coin_ledger = dict()
def create_genesis_block(self):
return Block("Genesis Block", "0", coin)
ive tried adding other arguments but as i am new to this, i havent been able to figure it out myself properly
def create_genesis_block(self): doesn't take a parameter, maybe you meant:
def create_genesis_block(self, block_num):
return Block("Genesis Block", block_num, coin)
or
#staticmethod
def create_genesis_block(block_num):
return Block("Genesis Block", block_num, coin)
when you use the self constructor, you need to initialize the class, try
instanse = Blockchain()
instanse.create_genesis_block()
I'm porting some code from IronPython to CPython (3.8) + Python.NET and I have a custom class that's broken in an odd way: it gives me an AttributeError even though the member is present in __dict__. This class derives from uuid.UUID to add support to BLE short UUIDs:
import uuid
class UUID(uuid.UUID):
"""Represents a more general version of the uuid.UUID, so we can have both
regular and short UUIDs.
"""
def __init__(self, hex=None, bytes=None, is_short=False, **kwargs):
try:
super(UUID, self).__init__(hex=hex, bytes=bytes, **kwargs)
self.__dict__['is_short'] = False
except ValueError as val_ex:
if hex is None or len(hex) != 4:
raise val_ex
# Remove braces GUIDs
hex_digits = hex.strip('{}').replace('-', '')
# remove RFC 4122 URN's 'urn:uuid:deadbeef-1234-fedc-5678-deadbeefaaaa'
hex_digits = hex_digits.replace('uuid:', '').replace('urn:', '')
if len(hex_digits) != 4:
raise ValueError('badly formed hexadecimal UUID string')
self.__dict__['int'] = int(hex, 16)
self.__dict__['is_short'] = True
if is_short is True:
self.__dict__['is_short'] = True
def __str__(self):
if self.is_short:
hex = '%04x' % self.int
return '%s' % (hex[:4])
else:
return super(UUID, self).__str__()
And I'm using it like so:
class Test_UUID(unittest.TestCase):
def test_create(self):
x = pybletestlibrary.UUID("1800")
pprint(x.__dict__)
print(x)
The above code yields:
{'int': 6144, 'is_short': True}
E
======================================================================
ERROR: test_create (__main__.Test_UUID)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test\test_pybletestlibrary.py", line 27, in test_create
print(x)
File "my_uuid.py", line 66, in __str__
hex = '%04x' % self.int
AttributeError: int
----------------------------------------------------------------------
Ran 1 test in 0.003s
FAILED (errors=1)
I can't use setattr() because uuid.UUID is immutable. I don't understand why the AttributeError as int is present in __dict__. Any suggestions?
Thanks!!
Here's what I mean:
import uuid
class UUID(uuid.UUID):
"""Represents a more general version of the uuid.UUID, so we can have both
regular and short UUIDs.
"""
def __init__(self, hex=None, bytes=None, is_short=False, **kwargs):
self.is_short = False
if len(hex) == 4:
self.is_short = True
hex = "0000"+hex+"-0000-1000-8000-00805f9b34fb"
super(UUID, self).__init__(hex=hex, bytes=bytes, **kwargs)
def __str__(self):
val = super(UUID, self).__str__()
if self.is_short:
return val[4:8]
else:
return val
You might have to override some of the other attributes, too.
Please look at the code below and the Attribute error I get. Thanks for your help.
This is the error I get.
Traceback (most recent call last):
File "ClassError.py", line 45, in <module>
if __name__ == '__main__':Main()
File "ClassError.py", line 43, in Main
printTest(i)
File "ClassError.py", line 38, in printTest
print f.FirstName
AttributeError: 'str' object has no attribute 'FirstName'
CODE
class CompKeyData():
def __init__(self,
FirstName,\
MiddleName,\
LastName):
self.FirstName = FirstName
self.MiddleName = MiddleName
self.LastName = LastName
def __repr__(self):
return repr((self.FirstName,\
self.MiddleName,\
self.LastName))
def __iter__(self):
return iter((self.FirstName,\
self.MiddleName,\
self.LastName))
def ckDataClassList(dataList):
dataObjects = []
for FirstName,\
MiddleName,\
LastName in dataList:
dataObjects.append(CompKeyData(
FirstName,\
MiddleName,\
LastName))
return dataObjects
ckData = [['John', 'Ralph', 'DuMont'], ['Jack', 'Lowry', 'Matern']]
ckClassData = ckDataClassList(ckData)
def printTest(classData):
for f in classData:
print f.FirstName
return None
def Main():
for i in ckClassData:
printTest(i)
if __name__ == '__main__':Main()
When you do
for i in ckClassData:
Each i is a CompKeyData instance. You then do:
printTest(i)
Which calls:
for f in classData:
where classData is the i you passed in.
This iterates through the individual CompKeyData instance, which (due to your implementation of __iter__) assigns FirstName, MiddleName and LastName in turn to f- each of these is a string, and doesn't have FirstName.
Instead, printTest should be:
printTest(classData):
print classData.FirstName
You don't need to explicitly return None, this happens automatically if you don't explicitly return anything else.
Also, it is worth reading PEP-0008; following this will make your code more readable.
You need to change your printTest function to the following:
def printTest(classData):
print classData.FirstName
classData is not a list of CompKeyData instances, but rather a single CompKeyData instance.
PEP8 is also certainly worth a look for python style.
I've just been reading an article that talks about implementing a parser in python:
http://effbot.org/zone/simple-top-down-parsing.htm
The general idea behind the code is described in this paper: http://mauke.hopto.org/stuff/papers/p41-pratt.pdf
Being fairly new to writing parsers in python so I'm trying to write something similar as a learning exercise. However when I attempted to try to code up something similar to what was found in the article I am getting an TypeError: unbound method TypeError. This is the first time I've encountered such an error and I've spent all day trying to figure this out but I haven't solved the issue. Here is a minimal code example (in it's entirety) that has this problem:
import re
class Symbol_base(object):
""" A base class for all symbols"""
id = None # node/token type name
value = None #used by literals
first = second = third = None #used by tree nodes
def nud(self):
""" A default implementation for nud """
raise SyntaxError("Syntax error (%r)." % self.id)
def led(self,left):
""" A default implementation for led """
raise SyntaxError("Unknown operator (%r)." % self.id)
def __repr__(self):
if self.id == "(name)" or self.id == "(literal)":
return "(%s %s)" % (self.id[1:-1], self.value)
out = [self.id, self.first, self.second, self.third]
out = map(str, filter(None,out))
return "(" + " ".join(out) + ")"
symbol_table = {}
def symbol(id, bindingpower=0):
""" If a given symbol is found in the symbol_table return it.
If the symblo cannot be found theni create the appropriate class
and add that to the symbol_table."""
try:
s = symbol_table[id]
except KeyError:
class s(Symbol_base):
pass
s.__name__ = "symbol:" + id #for debugging purposes
s.id = id
s.lbp = bindingpower
symbol_table[id] = s
else:
s.lbp = max(bindingpower,s.lbp)
return s
def infix(id, bp):
""" Helper function for defining the symbols for infix operations """
def infix_led(self, left):
self.first = left
self.second = expression(bp)
return self
symbol(id, bp).led = infix_led
#define all the symbols
infix("+", 10)
symbol("(literal)").nud = lambda self: self #literal values must return the symbol itself
symbol("(end)")
token_pat = re.compile("\s*(?:(\d+)|(.))")
def tokenize(program):
for number, operator in token_pat.findall(program):
if number:
symbol = symbol_table["(literal)"]
s = symbol()
s.value = number
yield s
else:
symbol = symbol_table.get(operator)
if not symbol:
raise SyntaxError("Unknown operator")
yield symbol
symbol = symbol_table["(end)"]
yield symbol()
def expression(rbp = 0):
global token
t = token
token = next()
left = t.nud()
while rbp < token.lbp:
t = token
token = next()
left = t.led(left)
return left
def parse(program):
global token, next
next = tokenize(program).next
token = next()
return expression()
def __main__():
print parse("1 + 2")
if __name__ == "__main__":
__main__()
When I try to run this with pypy:
Traceback (most recent call last):
File "app_main.py", line 72, in run_toplevel
File "parser_code_issue.py", line 93, in <module>
__main__()
File "parser_code_issue.py", line 90, in __main__
print parse("1 + 2")
File "parser_code_issue.py", line 87, in parse
return expression()
File "parser_code_issue.py", line 81, in expression
left = t.led(left)
TypeError: unbound method infix_led() must be called with symbol:+ instance as first argument (got symbol:(literal) instance instead)
I'm guessing this happens because I don't create an instance for the infix operations but I'm not really wanting to create an instance at that point. Is there some way I can change those methods without creating instances?
Any help explaining why this is happening and what I can do to fix the code is greatly appreciated!
Also is this behaviour going to change in python 3?
You forgot to create an instance of the symbol in your tokenize() function; when not a number, yield symbol(), not symbol:
else:
symbol = symbol_table.get(operator)
if not symbol:
raise SyntaxError("Unknown operator")
yield symbol()
With that one change your code prints:
(+ (literal 1) (literal 2))
You haven't bound new function to the instance of your object.
import types
obj = symbol(id, bp)
obj.led = types.MethodType(infix_led, obj)
See accepted answer to another SO question
I'm attempting to use Map to reference a class function, but am having difficulty with formatting/ordering. I have heard that using map is sort of obsolete so I am definitely open to alternative solutions (for loops?) Thanks in advance.
lognames = [ "C:\Users\makker1\Desktop\logs\loga.txt",
"C:\Users\makker1\Desktop\logs\logb.txt",
"C:\Users\makker1\Desktop\logs\logc.txt" ]
class LogFile:
def __init__(self,filepath):
self.logfile = open(filepath, "r")
self.head = None
def __str__(self):
return "x=" + str(self.x) + "y="+str(self.y)
def readline (self):
if self.head != None:
self.head = self.logfile.readline()
def previewline (self):
if self.head == None:
self.head = self.logfile.readline()
def close (self):
self.logfile.close()
logs = map(LogFile(self,filepath).__init__(), lognames)
heads = map(lambda log: None, logs)
>>>
Traceback (most recent call last):
File "C:\Users\makker1\Desktop\mergesort-final.py", line 30, in <module>
logs = map(LogFile(self,filepath).__init__, lognames)
NameError: name 'self' is not defined
>>>
If any more info is needed, please let me know. I realize that there are tons of posts about this very problem and have sorted through many of them with no avail.
Here is a list comprehension answer. I like this better than map().
logs = [LogFile(fname) for fname in lognames]
You don't have to call __init__ explicitly. Try:
logs = map(LogFile, lognames)
Sometimes it helps to think of a class as being callable. You can think of a class as something like the following:
def LogFile(filepath):
class _LogFile:
def __init__(self, path):
...
return _LogFile(filepath)
Basically, a class can be thought of as something that you call to create an object instance. This isn't really true, but in many cases it will appear to be.