callable

Dynamically assigning function implementation in Python

筅森魡賤 提交于 2019-12-03 07:24:13
问题 I want to assign a function implementation dynamically. Let's start with the following: class Doer(object): def __init__(self): self.name = "Bob" def doSomething(self): print "%s got it done" % self.name def doItBetter(self): print "Done better" In other languages we would make doItBetter an anonymous function and assign it to the object. But no support for anonymous functions in Python. Instead, we'll try making a callable class instance, and assign that to the class: class Doer(object): def

Python static method is not always callable

廉价感情. 提交于 2019-12-03 06:28:43
While parsing attributes using __dict__ , my @staticmethod is not callable . Python 2.7.5 (default, Aug 29 2016, 10:12:21) [GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from __future__ import (absolute_import, division, print_function) >>> class C(object): ... @staticmethod ... def foo(): ... for name, val in C.__dict__.items(): ... if name[:2] != '__': ... print(name, callable(val), type(val)) ... >>> C.foo() foo False <type 'staticmethod'> How is this possible? How to check if a static method is callable? I provide

What is a callable object in C++?

北慕城南 提交于 2019-12-03 05:55:41
I'm currently studying boost threads. And I came across that the thread class has a constructor that accepts callable objects. What are callable objects? class CallableClass { private: // Number of iterations int m_iterations; public: // Default constructor CallableClass() { m_iterations=10; } // Constructor with number of iterations CallableClass(int iterations) { m_iterations=iterations; } // Copy constructor CallableClass(const CallableClass& source) { m_iterations=source.m_iterations; } // Destructor ~CallableClass() { } // Assignment operator CallableClass& operator = (const CallableClass

Difference between Spring MVC's @Async, DeferredResult and Callable

狂风中的少年 提交于 2019-12-03 05:40:51
问题 I've a long-running task defined in a Spring service. It is started by a Spring MVC controller. I want to start the service and return back an HttpResponse to the caller before the service ends. The service saves a file on file system at end. In javascript I've created a polling job to check service status. In Spring 3.2 I've found the @Async annotation, but I don't understand how it is different from DeferredResult and Callable . When do I have to use @Async and when should I use

Is there a way to take an argument in a callable method?

ε祈祈猫儿з 提交于 2019-12-03 04:33:12
问题 I have created a piece of code which takes an IP address (from main method in another class) and then loops through a range of IP addresses pinging each one as it goes. I have a GUI front end on this and it was crashing (hence why I've done the multithreading. My problem is I can no longer take the IP address as an argument in my ping code as its callable. I've searched all over for this and cant seem to find a way to get round this. Is there a way for a callable method to take arguments? If

What happens to a BufferedReader that doesn't get closed within a callable.call?

大兔子大兔子 提交于 2019-12-03 03:58:10
I have three questions. To explain, I was reviewing someone's code, and noticed BufferedReader s sometimes aren't being closed. Usually, Eclipse gives a warning that this is a potential memory leak (and I fix it). However, within a Callable inner class, there is no warning. class outerClass { ... public void someMethod() { Future<Integer> future = outputThreadPool.submit(new innerClass(this.myProcess.getInputStream(), threadName)); ... } class innerClass implements Callable<Integer> { private final InputStream stream; private final String prepend; innerClass(InputStream stream, String prepend)

How to get foreign key values with getattr from models

落爺英雄遲暮 提交于 2019-12-03 00:15:52
i have a model Project and i am getting the attributes of that with the following instr attr = getattr(project, 'id', None) project is the instance, id is the field and None is the default return type. my question is what if i want to get the F.K keys with this? Get customer name project.customer.name How to get customer name with the above condition? Already Tried if callable(attr): context[node][field] = '%s' % attr() Current Code context = {'project': {}} fields = ('id', 'name', 'category', 'created_by', customer) for field in fields: attr = getattr(project, field, None) if callable(attr):

Dynamically assigning function implementation in Python

感情迁移 提交于 2019-12-02 20:55:48
I want to assign a function implementation dynamically. Let's start with the following: class Doer(object): def __init__(self): self.name = "Bob" def doSomething(self): print "%s got it done" % self.name def doItBetter(self): print "Done better" In other languages we would make doItBetter an anonymous function and assign it to the object. But no support for anonymous functions in Python. Instead, we'll try making a callable class instance, and assign that to the class: class Doer(object): def __init__(self): self.name = "Bob" class DoItBetter(object): def __call__(self): print "%s got it done

Difference between Spring MVC's @Async, DeferredResult and Callable

a 夏天 提交于 2019-12-02 19:02:40
I've a long-running task defined in a Spring service. It is started by a Spring MVC controller. I want to start the service and return back an HttpResponse to the caller before the service ends. The service saves a file on file system at end. In javascript I've created a polling job to check service status. In Spring 3.2 I've found the @Async annotation, but I don't understand how it is different from DeferredResult and Callable . When do I have to use @Async and when should I use DeferredResult ? Async annotates a method so it is going to be called asynchronously. @org.springframework

Is there a way to take an argument in a callable method?

旧时模样 提交于 2019-12-02 17:54:06
I have created a piece of code which takes an IP address (from main method in another class) and then loops through a range of IP addresses pinging each one as it goes. I have a GUI front end on this and it was crashing (hence why I've done the multithreading. My problem is I can no longer take the IP address as an argument in my ping code as its callable. I've searched all over for this and cant seem to find a way to get round this. Is there a way for a callable method to take arguments? If not is there any other way to accomplish what I'm trying to do? sample of my code: public class doPing