class
What are the benefits of using Classes in VBA? [closed]
The advantage of using classes instead of just subroutines is that classes create a level of abstraction that allow you to write cleaner code. Admittedly, if you’ve never used classes before in VBA, there is a learning curve, but I believe it’s certainly worth the time to figure it out. One key indication that you … Read more
How to stub time.sleep() in Python unit testing
You can use mock library in your tests. import time from mock import patch class MyTestCase(…): @patch(‘time.sleep’, return_value=None) def my_test(self, patched_time_sleep): time.sleep(666) # Should be instant
How to keep track of class instances?
One way to keep track of instances is with a class variable: class A(object): instances = [] def __init__(self, foo): self.foo = foo A.instances.append(self) At the end of the program, you can create your dict like this: foo_vars = {id(instance): instance.foo for instance in A.instances} There is only one list: >>> a = A(1) >>> … Read more
What is a DynamicClassAttribute and how do I use it?
New Version: I was a bit disappointed with the previous answer so I decided to rewrite it a bit: First have a look at the source code of DynamicClassAttribute and you’ll probably notice, that it looks very much like the normal property. Except for the __get__-method: def __get__(self, instance, ownerclass=None): if instance is None: # … Read more
How to make an Abstract Class inherit from another Abstract Class in Python?
Have a look at abc module. For 2.7: link. For 3.6: link Simple example for you: from abc import ABC, abstractmethod class A(ABC): def __init__(self, value): self.value = value super().__init__() @abstractmethod def do_something(self): pass class B(A): @abstractmethod def do_something_else(self): pass class C(B): def do_something(self): pass def do_something_else(self): pass
How do I get the name of the class containing a logging call in Python?
For a rather easy, pythonic way to get the class name to output with your logger, simply use a logging class. import logging # Create a base class class LoggingHandler: def __init__(self, *args, **kwargs): self.log = logging.getLogger(self.__class__.__name__) # Create test class A that inherits the base class class testclassa(LoggingHandler): def testmethod1(self): # call self.log.<log level> … Read more
Java – Abstract class to contain variables?
I would have thought that something like this would be much better, since you’re adding a variable, so why not restrict access and make it cleaner? Your getter/setters should do what they say on the tin. public abstract class ExternalScript extends Script { private String source; public void setSource(String file) { source = file; } … Read more