Class Attributes Not Found Python - python-3.x

Here is my code:
Here is my error:
I'm very confused why this is happening. I'm using an instance of the class JinderBot and the __init__ function clearly sets the driver attribute. Please help point out my mistake.
I'm using PyCharm and Python 3.7.

You need to add methods to the class to access and set the attributes.
class JinderBot:
def __init__(self):
self.atrib = “foobar”
def _get(self):
return self.atrib
def _set(self, atrib)
self.atrib = atrib
bot = JinderBot()
print(bot._get())
bot._set(“updating attribute”)
print(bot._get())

Related

What is the correct way to annotate types of an injected dependency in Python

Could anyone enlighten me as to the best way to type-annotate an injected dependency in Python such that a member is correctly identified by the type-hinting of an IDE?
Take the following example:
class Type: pass
class Type1(Type):
def __init__(self, value):
self.some_member = value
class Type2(Type):
def __init__(self, value):
self.other_member = value
class Base:
def __init__(self, injected: Type):
self.injected = injected
class Derived1(Base):
def __init__(self, injected: Type1):
super().__init__(injected)
class Derived2(Base):
def __init__(self, injected: Type2):
super().__init__(injected)
What I would like is for the IDE (VS Code/PyCharm etc.) to be able to provide type hinting support when accessing the members of the injected dependency:
instance1 = Derived1(Type1(5))
instance1.injected.some_member = 2 # IDE knows nothing about `some_member`
The docs imply the best way to address this would be using TypeVar
MyType = TypeVar('MyType', bound=Type)
class Base:
def __init__(self, injected: MyType):
self._injected = injected
This unfortunately doesn't help. Any suggestions greatly appreciated.
Note that the linter itself is perfectly happy with the usage, so this seems more like a possible limitation of the type-hinting introspection in the IDE.

Python3 iteratively call members of class with reflection

Using Python3, I am trying to call the members of a class which contain is_ or has_. Their returns are hard coded to True.
Here's what I've implemented, I don't know how to actually call the stored members using the black_magic method. Can anybody please help? Many thanks.
import inspect
import unittest
class Foo:
def is_bar(self):
print(bar)
return True
def is_baz(self):
print(baz)
return True
class MyTest(unittest.TestCase):
def setUp(self):
self.object=Foo()
self.object_members=inspect.getmembers(self.object)
def test_assertions_when_typical(self):
self.assertEqual(True,True)
for member in self.object_members:
member_name, member_address = member
if "is_" in member_name or "has_" in member_name:
result=black_magic(member)
self.assertEqual(result,True)
if __name__=='__main__':
unittest.main()
I'm invoking this with:
python3 -m unittest test_python_reflection_minimal_example.py
You can use the following implementation, which works locally:
def black_magic(self, objectClass, member):
return getattr(objectClass, member[0])()
and call it using result=self.black_magic(self.object, member).
Explanation: member is a tuple containing the method name and method reference. We then use this name and invoke the method on the self.object.

Unable to inherit from QAbstractItemModel

I'm new to model/view in PyQt and I'm having trouble creating an object that inherits from the QAbstractItemModel class. Here's the class that inherits from it:
class MyCustomDataModel(QAbstractItemModel):
def __init(self, parent = None):
super(MyCustomDataModel, self).__init__(parent)
self.parent = parent
Here's another class that tries to instantiate an object of the above type:
class MyCustomType(AnotherCustomTypeThatInheritsQListWidgetItem):
def __init__(self, parent = None):
super(MyCustomType, self).__init__(parent)
# Instantiate a model:
self.dataModel = MyCustomDataModel(self)
The above instantiation is throwing the following error at runtime:
"QAbstractItemModel(QObject parent = None): argument 1 has unexpected type 'MyCustomType'" I cannot see what the problem is here, but I'm pretty new to Python / PyQt. Can someone please point out what I've done wrong? I've tried calling the instantiation line and not passing anything in but I get the same error.
This might be more appropriate as a comment instead of an answer.
Try updating the line where you pass the MyCustomType instance to MyCustomDataModel:
self.dataModel = MyCustomDataModel(self)
Instead use:
self.dataModel = MyCustomDataModel()

classes in Python: AttributeError

I`m writing simple scripts for test automation using Selenium WebDriver in Python, but the issue relates to Python, not to Selenium.
There two classes FindByXPATH_1(base) & FindByXPATH_2(derived). I want to call an attribute "driver" from the base class in a method of FindByXPATH_2, but when I ran the code the AttributeError shows up: "type object 'FindByXPATH_1' has no attribute 'driver'"
Here is the code:
class FindByXPATH_1():
def __init__(self):
self.driver_location = '/usr/local/bin/chromedriver'
self.driver = webdriver.Chrome(self.driver_location)
self.driver.get('https://letskodeit.teachable.com/p/practice')
from basics.xpath_1 import FindByXPATH_1
import basics #the classes are in two different python files
class FindByXpath_2(FindByXPATH_1):
def __init__(self):
FindByXPATH_1.__init__(self)
def find_by_starts_with(self):
starting_with = FindByXPATH_1.driver.find_elements(By. XPATH,
'//div[#class="view-school"]//h3[starts-with(#)class, "subtitle"]')
print(len(starting_with))
test = FindByXPATH_2()
test.find_by_starts_with()
After running the code I get a message "AttributeError: type object 'FindByXPATH_1' has no attribute 'driver'"
How can I call that attribute?
In this line here:
starting_with = FindByXPATH_1.driver.find_elements(By. XPATH,
'//div[#class="view-school"]//h3[starts-with(#)class, "subtitle"]')
You should be calling self.driver.find_elements otherwise you are trying to access a class variable of FindByXPATH_1 and not the instance variable driver

Create os.DirEntry

Does anyone have a hack to create an os.DirEntry object other than listing the containing directory?
I want to use that for 2 purposes:
tests
API where container and contained are queried at the same time
Yes, os.DirEntry is a low-level class not intended to be instantiated. For tests and things, you can create a mock PseudoDirEntry class that mimics the things you want, for example (taken from another answer I wrote here):
class PseudoDirEntry:
def __init__(self, name, path, is_dir, stat):
self.name = name
self.path = path
self._is_dir = is_dir
self._stat = stat
def is_dir(self):
return self._is_dir
def stat(self):
return self._stat
Even easier:
class PseudoDirEntry:
def __init__(self, path):
import os, os.path
self.path = os.path.realpath(path)
self.name = os.path.basename(self.path)
self.is_dir = os.path.isdir(self.path)
self.stat = lambda: os.stat(self.path)
In Python 3.5 os.DirEntry is not yet exposed.
Details: https://bugs.python.org/issue27038
Python 3.6 exposes os.DirEntry type, but it cannot be instantiated.
On Python 3.5 a type() on a DirEntry object returns posix.DirEntry

Resources