factory

how do i subclass threading.Event?

北慕城南 提交于 2019-12-07 12:25:46
问题 In Python 2.7.5: from threading import Event class State(Event): def __init__(self, name): super(Event, self).__init__() self.name = name def __repr__(self): return self.name + ' / ' + self.is_set() I get: TypeError: Error when calling the metaclass bases function() argument 1 must be code, not str Why? Everything I know about threading.Event I learned from: http://docs.python.org/2/library/threading.html?highlight=threading#event-objects What does it mean when it says that threading.Event()

Programmatically create arithmetic special methods in Python, (aka factory function HOWTO)

﹥>﹥吖頭↗ 提交于 2019-12-07 06:38:25
问题 My idea is to create particular function objects that can be summed/subtracted/... together, returning a new function object that has the same properties. This example code, hopefully, demonstrates the idea: from FuncObj import Func # create some functions quad = Func(lambda x: x**2) cube = Func(lambda x: x**3) # now combine functions as you like plus = quad + cube minus = quad - cube other = quad * quad / cube # and these can be called plus(1) + minus(32) * other(5) I have written the

Concise way to enforce implementation of factory in Scala

假如想象 提交于 2019-12-07 03:52:06
问题 Let us assume we have a trait T . What is the best way to achieve the following: Everybody who writes an implementation of T should be forced to provide a possibility that allows a parameter-free initialization of T , i.e., we probably have to enforce the implementation of a configurable factory. All logic/data that only depends on the actual initialization parameters (of a certain implementation A of T ) should be handled/stored centrally, but should be available in both the factory and A .

Unity with factory method who need parameters

吃可爱长大的小学妹 提交于 2019-12-07 03:27:12
问题 I want to register a type in an Unity container with a factory method who needs paramters. These parameters are to be resolved by unity but only at runtime. Factory method code : public static IApp Create(IOne, ITwo) {...} Register code : container.RegisterType(typeof(IApp), new InjectionFactory(f => App.Create(???, ???))); What do I need to replace the '???' ? More informations : My Unity configuration is made in two phases. First phase (application is starting), I register all my object but

Using traits with a factory

*爱你&永不变心* 提交于 2019-12-07 03:13:09
问题 I'm currently discovering scala and I was wondering if I could use traits with a factory. I tried this : abstract class Foo { ... } object Foo { def apply() = new Bar private class Bar extends Foo { ... } } Foo() with MyTrait // Not working I guess it's because with must be preceded by new . So is there any way to do this ? Thank you 回答1: No it is too late, the instance is already created when the apply() method returns. What you can do is using the traits inside the factory method. The code

Returning generic without knowing type

落花浮王杯 提交于 2019-12-07 01:15:30
问题 I have a situation where I have a class that accepts an instance of a certain object type in its generic type parameter. The layout is something like this: public abstract BaseClass { ... } public DiamondClass : BaseClass { ... } public SilverClass : BaseClass { ... } public Handler<T> where T : BaseClass { ... } I want to be able to create a method to return an instance of Handler<DiamondClass> or Handler<BaseClass> without defining the type upon input. I've tried something along these lines

Objective-C: Use singleton vs. use class as an object?

懵懂的女人 提交于 2019-12-06 17:55:43
问题 I've been wondering in what cases it is really necessary to adopt the singleton pattern in objective-C (e.g., define a dedicated class and create a single instance), that using the class as an object won't do. Particularly, I'm thinking of the following solution: Define and use appropriate class methods, instead of instance methods on the singleton instance; Use static variables (file-scope globals), instead of instance variables of the singleton instance; Use the class object when

Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory

狂风中的少年 提交于 2019-12-06 15:05:08
I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface: public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator(Type objectType); } public interface IObjectCreator { T CreateObject<T>(IDataRow data); bool SupportsType(Type type); } Implementation of the factory class could look like this, though I am not sure this is the way to go: public interface ObjectCreatorFactory:IObjectCreatorFactory { IEnumerable

python setattr for dynamic method creator with decorator

谁说胖子不能爱 提交于 2019-12-06 13:21:40
I have a class which has multiple methods defined. import mat class Klass(object): @mat.sell(mat.CanSet): def method1(self): return None @mat.sell(mat.CanSet): def method2(self): return 'value2' Imagine I have 10 methods that I need to populate for this 'Klass'. I want to generate these methods without explicitely writing them all. So I want to do a factory that does setattr for each method. Problem is that I do following and the last method has the last value. Each do not get its related value but value10. Also below solution does not implement the decorator, I have no idea how to do assign

Passing arguments in constructor with boost factory

冷暖自知 提交于 2019-12-06 12:26:54
I am fairly new with the whole factory implementation and as a result maybe my question will sound wrong and not very well defined. So, with few words I want to have a boost factoty in order to initialize a banch fo derived classes and so far I have managed to do so for classes with empty constructors. Let me present to my current boost factory implementation for two small classes: Base.h: #ifndef BASE_H_ #define BASE_H_ #include <vector> #include <map> #include "boost/function.hpp" class base { protected: typedef boost::function <base *()> basefactory; public: base(); virtual ~base(); int a;