I am having an issue with Python throwing an AttributeError on accessing a variable.
The code is below, redacted for clarity.
class mycollection(object):
"""
Collection of stuff.
"""
#"compile-time" define class variables.
__slots__ = ["stuff_list"]
def __init__(self):
self.stuff_list = []
def add_stuff(self, stuff):
self.stuff_list.append(stuff)
#later on..
collection = mycollection()
stuff = stuff()
collection.add_stuff(stuff)
Generating this error.
Traceback (most recent call last):
File "", line 210, in <module>
main()
File "", line 206, in main
thestuff = load_file(inputfile, filetype)
File "pyyft.py", line 121, in load_file
collection.add_stuff(stuff)
File "pyyft.py", line 55, in add_test
self.stuff_list.append(stuff)
AttributeError: stuff_list
Checking through the documentation, I don't understand why this error is arising.
__ini__ should be __init__
Wouldn't this be "more Pythonic"?
collection.stuff_list.append(test_stuff)
Related
I am creating a nbt file whose root has two tags:
DataVersion. It must be a tag_Int.
size. You must set a tag_List of 3 tag_ints.
I did a few tests and determined that an error occurs when I try to pass a tag_List to the file.
This is my code:
import nbtlib as nbt
class Structure(nbt.File):
def __init__(Self, data_version, size):
#super().__init__({"DataVersion":nbt.Int(data_version), "size":nbt.List(map(nbt.Int, size))})
super().__init__({"DataVersion":nbt.Int(data_version), "size":nbt.List(map(nbt.Int, size))})
structure = Structure(data_version=1952, size=(0, 0, 0))
structure.save("prueba.nbt")
This is the error:
Traceback (most recent call last):
File "D:\studios dante 2\MODULES\Games\structurenbt\module.py", line 10, in <module>
structure.save("prueba.nbt")
File "C:\Python38-32\lib\site-packages\nbtlib\nbt.py", line 132, in save
self.write(buff, byteorder or self.byteorder)
File "C:\Python38-32\lib\site-packages\nbtlib\tag.py", line 420, in write
tag.write(buff, byteorder)
File "C:\Python38-32\lib\site-packages\nbtlib\tag.py", line 365, in write
write_numeric(BYTE, self.subtype.tag_id, buff, byteorder)
File "C:\Python38-32\lib\site-packages\nbtlib\tag.py", line 84, in write_numeric
buff.write(fmt[byteorder].pack(value))
struct.error: required argument is not an integer
Can someone help me to fix the problem? I use python 3.8
I already fixed it, turns out I had the nbtlib out of date. I updated it and it works!
I am trying to use marshmallow_dataclass in a python (3.9) test in pycharm to try it (marshmallow_dataclass) out and get an error.
Not sure what I am doing wrong. I have installed marshmallow_dataclass (8.1.0)
I have so far been successful in using marshmallow (3.8.0) and marshmallow_oneofschema (2.1.0) so far
(new to python but not programming)
Code is:
import marshmallow_dataclass
import unittest
class Tests(unittest.TestCase):
def test_serialize(self):
print("Hello")
Error is:
Traceback (most recent call last):
File "C:\Program Files\JetBrains\PyCharm Community Edition 2020.2.3\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py", line 35, in <module>
sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING))
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 100, in __init__
self.parseArgs(argv)
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 147, in parseArgs
self.createTests()
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\main.py", line 158, in createTests
self.test = self.testLoader.loadTestsFromNames(self.testNames,
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 220, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 220, in <listcomp>
suites = [self.loadTestsFromName(name, module) for name in names]
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 154, in loadTestsFromName
module = __import__(module_name)
File "C:\xxxx\dev\repos\python\solar\pvoutput-publisher\test\test_serialization\test_delme.py", line 1, in <module>
import marshmallow_dataclass
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\marshmallow_dataclass\__init__.py", line 59, in <module>
import typing_inspect
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\typing_inspect.py", line 25, in <module>
from typing_extensions import Literal
File "C:\xxxx\AppData\Local\Programs\Python\Python39\lib\site-packages\typing_extensions.py", line 2084, in <module>
def TypeAlias(self, parameters):
TypeError: __init__() missing 1 required positional argument: 'doc'```
I hit similar error on python 3.9 alpha release (3.9.0a5), I am not sure the version of typing_extensions.py in your environment (maybe it is v3.7.4.3 ?).
The root cause is that :
the function TypeAlias() is decorated directly with the class _TypeAliasForm, which is subclass of typing._SpecialForm in python stardard library
typing._SpecialForm.__init__() in your python environment has extra argument doc , since the class _TypeAliasForm is a decorator for TypeAlias(), Python couldn't provide extra argument doc when instantiating _TypeAliasForm on decorating process.
In short, your typing_extensions.py is out of sync with your python interpreter, see the source code for detail:
TypeAlias() and _TypeAliasForm class .
typing._SpecialForm.__init__() in python 3.9.0a5
typing._SpecialForm.__init__() in python 3.9.0a6
It seems that the issue happens from python 3.9.0a1 to 3.9.0a5, so the solutions are :
upgrade your python to final release version
if you are not allowed to upgrade python for some other reasons, maybe you can monkey-patch typing._SpecialForm , here is the example for monkey-patching :
from typing import _SpecialForm
origin_getitem = _SpecialForm.__getitem__
def patch_init(self, name, doc=None):
if doc is None and callable(name):
if hasattr(name, '__doc__'):
doc = name.__doc__
self._name = name
self._doc = doc
def patch_getitem(self, parameters):
item = None
if callable(self._name):
item = self._name(self=self, parameters=parameters)
else:
item = origin_getitem(self=self, parameters=parameters)
return item
if not hasattr(_SpecialForm.__init__, 'patched'):
_SpecialForm.__init__ = patch_init
setattr(_SpecialForm.__init__, 'patched', True)
if not hasattr(_SpecialForm.__getitem__, 'patched'):
_SpecialForm.__getitem__ = patch_getitem
setattr(_SpecialForm.__getitem__, 'patched', True)
So, i've got the following setup which throws me the errors posted in the end of the question. Can somebody tell me why? I'm sure the error would make total sense when somebody explains it, but i just can't figure it out myself.
baseCmds.py
class base():
....
mainCmds.py
import baseCmds
reload(baseCmds)
class main(baseCmds.base):
def __init__(self):
baseCmds.base.__init__(self)
....
simpleCmds.py
import baseCmds
reload(baseCmds)
class simple(baseCmds.base):
def __init__(self):
baseCmds.base.__init__(self)
....
build.py
import mainCmds
import simpleCmds
reload(mainCmds)
reload(simpleCmds)
m = mainCmds.main()
s = simpleCmds.simple()
Then depending on the order of the reload() statements in build.py I get either
# Traceback (most recent call last):
# File "<string>", line 12, in <module>
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 62, in <module>
# build()
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 59, in build
# buildRig(assetName="TestGuy", assetType="character", debug=0)
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 19, in buildRig
# m = mainCmds.main()
# File "C:/Users/Vasko/Documents/maya/scripts\vsRigging\rigCmds\bodyCmds\mainCmds.py", line 14, in __init__
# baseCmds.base.__init__(self)
# TypeError: unbound method __init__() must be called with base instance as first argument (got main instance instead)
or
# Traceback (most recent call last):
# File "<string>", line 12, in <module>
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 62, in <module>
# build()
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 59, in build
# buildRig(assetName="TestGuy", assetType="character", debug=0)
# File "C:\\Users\\Vasko\\Documents\\maya\\scripts\\vsRigging\\builds\\vsTest\\characters\\TestGuy\\body.py", line 20, in buildRig
# s = simpleCmds.simple()
# File "C:/Users/Vasko/Documents/maya/scripts\vsRigging\rigCmds\basicCmds\simpleCmds.py", line 7, in __init__
# baseCmds.base.__init__(self)
# TypeError: unbound method __init__() must be called with base instance as first argument (got simple instance instead)
import blinker
from blinker import signal
class Ticket():
#classmethod
def update(cls):
pass
ticket_created = signal('CREATED')
ticket_created.connect(Ticket.update)
This snippet of code works well on Python 2.7. But does not work on Python 2.6. I get this trace:
Traceback (most recent call last):
File "btest.py", line 10, in <module>
ticket_created.connect(Ticket.update)
File "/home/gavika/env2/lib/python2.6/site-packages/blinker/base.py", line 113, in connect
receiver_ref = reference(receiver, self._cleanup_receiver)
File "/home/gavika/env2/lib/python2.6/site-packages/blinker/_utilities.py", line 134, in reference
weak = callable_reference(object, callback)
File "/home/gavika/env2/lib/python2.6/site-packages/blinker/_utilities.py", line 145, in callable_reference
return BoundMethodWeakref(target=object, on_delete=callback)
File "/home/gavika/env2/lib/python2.6/site-packages/blinker/_saferef.py", line 143, in __new__
base.__init__(target, on_delete, *arguments, **named)
File "/home/gavika/env2/lib/python2.6/site-packages/blinker/_saferef.py", line 185, in __init__
self.weak_self = weakref.ref(im_self, remove)
TypeError: cannot create weak reference to 'classobj' object
Is there a way I could get this to work in 2.6 environment?
Yes, new-style classes is the answer.
class Ticket(object):
...
Solves the issue.
i am trying to migrate an application from gtk2 to gtk3 using GObject Introspection (gi.repository).
The problem i have now is creating an Atk.Relation in an accessibility method that looks like this:
from gi.repository import Atk
def atk_acc(obj, lbl):
atk_obj = obj.get_accessible()
atk_l = lbl.get_accessible()
relation_set = atk_l.ref_relation_set()
relation = Atk.Relation.new([atk_obj], 1, Atk.RelationType.LABEL_FOR)
relation_set.add(relation)
When i run the application, it breaks with an exception:
Traceback (most recent call last):
File "/home/erick/Desarrollo/git/canaima-instalador/canaimainstalador/main.py", line 237, in siguiente
CFG['w'].next('Metodo', Metodo, (CFG), PasoMetodo(CFG))
File "/home/erick/Desarrollo/git/canaima-instalador/canaimainstalador/pasos/metodo.py", line 63, in __init__
atk_acc(self.cmb_discos, self.lbl1)
File "/home/erick/Desarrollo/git/canaima-instalador/canaimainstalador/mod_accesible.py", line 37, in atk_acc
relation = Atk.Relation.new([atk_obj], 1, Atk.RelationType.LABEL_FOR)
File "/usr/lib/python2.7/dist-packages/gi/types.py", line 72, in constructor
return info.invoke(cls, *args, **kwargs)
TypeError: Expected Atk.Object, but got GObjectMeta
In ATK documentation says that the first argument of Atk.Relation.new is an array.
What i'm doing wrong here?