metaprogramming

create custom namedtuple type with extra features

Deadly 提交于 2019-12-30 19:14:29
问题 I'd like to create my own type of build-in namedtuple that has some extra features. Let's say we create a class: from collections import namedtuple MyClass = namedtuple('MyClass', 'field1 field2') It`s immutable, readable and simple. Now I can create instances of MyClass: myobj = MyClass(field1 = 1, field2 = 3.0) print(myobj.field1, myobj.field2) My extra requirement is when instance is created I'd like to check if field1 is int type and field2 is float . For example if user try to create

Can I define a __repr__ for a class rather than an instance? [duplicate]

爱⌒轻易说出口 提交于 2019-12-30 17:23:31
问题 This question already has answers here : How to create a custom string representation for a class object? (5 answers) Closed 3 years ago . Can I define a __repr__ for a class rather than an instance? For example, I'm trying to do this class A(object): @classmethod def __repr__(cls): return 'My class %s' % cls What I get is In [58]: a=A() In [59]: a Out[59]: My class <class '__main__.A'> In [60]: A Out[60]: __main__.A I'm trying to get the output of line 60 to look like "My Class A", not for

Change innerHTML set on the fly

[亡魂溺海] 提交于 2019-12-30 13:39:06
问题 I need to change on the fly the value set on every node using the innerHTML. The closest solution I found is: ... Object.defineProperty(Element.prototype, 'innerHTML', { set: function () { // get value (ok) var value = arguments[0]; // change it (ok) var new_value = my_function(value); // set it (problem) this.innerHTML = new_value; // LOOP } } ... But obviously it's an infinite loop. Is there a way to call the original innerHTML set? I also try the Proxy way but i could not make it work.

Change innerHTML set on the fly

若如初见. 提交于 2019-12-30 13:38:21
问题 I need to change on the fly the value set on every node using the innerHTML. The closest solution I found is: ... Object.defineProperty(Element.prototype, 'innerHTML', { set: function () { // get value (ok) var value = arguments[0]; // change it (ok) var new_value = my_function(value); // set it (problem) this.innerHTML = new_value; // LOOP } } ... But obviously it's an infinite loop. Is there a way to call the original innerHTML set? I also try the Proxy way but i could not make it work.

String-interning at compiletime for profiling

旧时模样 提交于 2019-12-30 11:51:21
问题 Context I am working on an instrumenting profiler, that enables you to name different measurements by string. So for example: MEASURE_SCOPE(text_rendering_code); ... MEASURE_SCOPE(password_hashing); ... MEASURE_START(system_call); ... MEASURE_STOP(system_call); where the macros would be defined like this: #define MEASURE_START(name) save_start_event(get_timestamp(), #name); #define MEASURE_STOP(name) save_stop_event(get_timestamp(), #name); #define MEASURE_SCOPE(name) Profiling_Class object#

How are respond_to and respond_to_missing different?

喜你入骨 提交于 2019-12-30 06:51:10
问题 I'm confused when to use each of this methods. From respond_to? documentation: Returns true if obj responds to the given method. Private methods are included in the search only if the optional second parameter evaluates to true. If the method is not implemented, as Process.fork on Windows, File.lchmod on GNU/Linux, etc., false is returned. If the method is not defined, respond_to_missing? method is called and the result is returned. And respond_to_missing? : Hook method to return whether the

Extending a class method in a module

允我心安 提交于 2019-12-30 06:25:29
问题 I'm playing with ruby's metaprogramming features, and I'm finding it a bit hairy. I'm trying to wrap a method call using a module. Currently, I'm doing this: module Bar module ClassMethods def wrap(method) class_eval do old_method = "wrapped_#{method}".to_sym unless respond_to? old_method alias_method old_method, method define_method method do |*args| send old_method, *args end end end end end def self.included(base) base.extend ClassMethods end end class Foo include Bar def bar(arg = 'foo')

How to loop through a boost::mpl::list?

不想你离开。 提交于 2019-12-30 06:12:07
问题 This is as far as I've gotten, #include <boost/mpl/list.hpp> #include <algorithm> namespace mpl = boost::mpl; class RunAround {}; class HopUpAndDown {}; class Sleep {}; template<typename Instructions> int doThis(); template<> int doThis<RunAround>() { /* run run run.. */ return 3; } template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; } template<> int doThis<Sleep>() { /* zzz.. */ return -2; } int main() { typedef mpl::list<RunAround, HopUpAndDown, Sleep> acts; // std::for

What is the python attribute get and set order?

穿精又带淫゛_ 提交于 2019-12-30 04:37:12
问题 Python provides us many possibilities on instance/class attribute, for example: class A(object): def __init__(self): self.foo = "hello" a = A() There are many ways to access/change the value of self.foo : direct access a.foo inner dict a.__dict__['foo'] get and set a.__get__ and a.__set__ ,of course there two are pre-defined methods. getattribute a.__getattribute__ __getattr__ and __setattr__ maybe more. While reading source code, I always get lost of what's their ultimate access order? When

monkey patching vs class_eval?

社会主义新天地 提交于 2019-12-30 03:59:06
问题 class String def hello "world" end end String.class_eval { def world "hello" end } "a".world => "hello" "b".hello => "world" They seems to do the same thing -- adding a method to a existing class. So what's the difference? 回答1: With class_eval you can do more dynamic things: >> met = "hello" #=> "hello" >> String.class_eval "def #{met} ; 'hello' ; end" #=> nil >> "foo".hello #=> "hello" 回答2: class_eval do conceptually class reopening (or monkey patching). There are mostly syntactic