i am writing twisted code .. first one with deferred second one with inlineCallbacks. first one works but second doesn't .. any idea or pointers
class Echo(LineReceiver):
def lineReceived_callbacks(self, line):
print self.sendLine("i received :%s"%line)
def pp(res):
print "from callback",res
self.sendLine(str(res))
d = self.factory.dbs.getResult(line)
d.addCallback(pp)
#defer.inlineCallbacks
def lineReceived(self, line):
res = yield self.factory.dbs.getResult(line)
print res
self.sendLine(str(res))
self.factory.dbs.getResult(line) returns a deferred.
got solution..
seems like a issue in twisted.
http://twistedmatrix.com/pipermail/twisted-python/2006-October/014277.html
if lineReceived returns a deferred connection drops in defer example deferred is created but not returned and in second because using inlineCallbacks, it returns deferred always and hence connection drop .. lineReceived is basic stuff and should be workable with inlineCallbacks..
Related
I am actually a student and my teacher gave me this code to understand and use this code so i can continue to learn machine learning etc. I get that it is an AND function and i get what it is printing , the thing i cant understan is the def call(self,in_data): function and where does the "in_data" gets its value from? Because in my point of view all i see it's a blank variable that somehow helps me to complete the code. Also changing the value of the weights in the init part didnt actually change anything in this code and i am wondering why. Here is the whole code i was given to study. Thanks for helping!
import numpy as np
class Perceptron:
def __init__(self,input_length,weights=None):
if weights is None:
self.weights= np.ones(input_length)*0.5
else:
self.weights=weights
#staticmethod
def unit_step_function(x):
if x>0.5:
return 1
return 0
def __call__(self,in_data):
weighted_input=self.weights*in_data
weighted_sum=weighted_input.sum()
return Perceptron.unit_step_function(weighted_sum)
p = Perceptron(2,np.array([0.5,0.5]))
for x in [np.array([0,0]),np.array([0,1]),np.array([1,0]),np.array([1,1])]:
y=p(np.array(x))
print(x,y)
__call__ is called when you try to invoke the object as though it were a function. You can see that being done at the bottom. p is a Perceptron object, but it's being treated as a function when you write
y = p(np.array(x))
So where is the data coming from? It's the np.array(x) that's being passed in.
You can read more about the "dunder"/"magic" methods here.
I'd actually argue that this is an abuse of __call__ though. I wouldn't consider a Perceptron to be inherently function-like. I think using a normal method would be clearer:
class Perceptron:
. . .
def give_input(self, in_data): # Instead of __call__
weighted_input = self.weights*in_data
weighted_sum = weighted_input.sum()
return Perceptron.unit_step_function(weighted_sum)
for x in [np.array([0,0]),np.array([0,1]),np.array([1,0]),np.array([1,1])]:
y = p.give_input(np.array(x)) # Just a typical method call now
I was recently trying to solve a challenge on Hackerrank which asked us to figure out whether a string containing brackets (e.g. {}, (), and [] ) was balanced or not (source: https://www.hackerrank.com/challenges/balanced-brackets). I wanted to solve this using the following approach that also integrated the initial format Hackerrank provided:
import sys
def isBalanced(s):
#insert code here
if __name__ == "__main__":
t = int(raw_input().strip())
for a0 in xrange(t):
s = raw_input().strip()
result = isBalanced(s)
print result
I should also note that site has configured the following as being the standard input in order to test the functionality of the code:
3
{[()]}
{[(])}
{{[[(())]]}}
In order to get the following output:
YES
NO
YES
However, I didn't understand how to approach this code, chiefly because I did not understand why Hackerrank used the if __name__ == "__main__": clause, as I thought that this clause was only used if someone wanted their module to be executed directly rather than executed through being imported in another script (source: What does if __name__ == "__main__": do?). I also did not understand the for loop containing for a0 in xrange(t): since a0 is not used within the for loop for anything, so I'm really unsure how the standard input would be processed.
So I ended up looking up the solution on the discussion page, and here it is below:
lefts = '{[('
rights = '}])'
closes = { a:b for a,b in zip(rights,lefts)}
def valid(s):
stack = []
for c in s:
if c in lefts:
stack.append(c)
elif c in rights:
if not stack or stack.pop() != closes[c]:
return False
return not stack # stack must be empty at the end
t = int(raw_input().strip())
for a0 in xrange(t):
s = raw_input().strip()
if valid(s):
print 'YES'
else:
print 'NO'
This code also confuses me, as the writer claimed to utilize a data structure known as a "stack" (although it seems to be just a Python list to me). And although the writer removed the if __name__ == "__main__": statement, they retained the for a0 in xrange(t):, which I'm not sure how it processes the standard input's integer and corresponding strings.
Furthermore, although the isBalanced function confuses me because it returns not stack. In a hash comment on the return statement of the function, the writer also states the # stack must be empty at the end. What does that mean? And how could this list be empty if, during the clause if c in lefts:, the stack is appended with the character of the string that is being iterated in the for-loop. So why would the function return not stack? Wouldn't it be consistent to return True so that the function would act as a Boolean tester (i.e. would return true if a certain object adhered to certain criteria, and false if the the object did not)?
I am still relatively new to coding so there are a lot of principles I am not familiar with. Can anyone illuminate as to how this would work?
iam not sure how your code works .. if name == "main": do?). just exlained where you use of Before executing the code, it will define a few special variables. For example, if the python interpreter is running that module (the source file) as the main program, it sets the special name variable to have a value "main". If this file is being imported from another module, name will be set to the module's name
I am new to pragramming and python. This is something like what I have.
Def some_function():
Print "stuff"
Def another_function(x):
running = True
While running:
x
another_function(some_function())
Why does it only print "stuff" the first time going through the loop?
I read some stuff that talked about late binding but not sure if that is what this is or how to fix it in my example.
You didn't pass the function, you called the function and passed its value. So it printed stuff before you ever got into the loop.
To refer to a function without calling it, you leave off the (). So it should be:
another_function(some_function);
Then in another_function, you have to call the function:
def another_function(x):
running = True
while running:
x()
My question is probably pretty basic but still I can't get a solution in the official doc. I have defined a Celery chain inside my Django application, performing a set of tasks dependent from eanch other:
chain( tasks.apply_fetching_decision.s(x, y),
tasks.retrieve_public_info.s(z, x, y),
tasks.public_adapter.s())()
Obviously the second and the third tasks need the output of the parent, that's why I used a chain.
Now the question: I need to programmatically revoke the 2nd and the 3rd tasks if a test condition in the 1st task fails. How to do it in a clean way? I know I can revoke the tasks of a chain from within the method where I have defined the chain (see thisquestion and this doc) but inside the first task I have no visibility of subsequent tasks nor of the chain itself.
Temporary solution
My current solution is to skip the computation inside the subsequent tasks based on result of the previous task:
#shared_task
def retrieve_public_info(result, x, y):
if not result:
return []
...
#shared_task
def public_adapter(result, z, x, y):
for r in result:
...
But this "workaround" has some flaw:
Adds unnecessary logic to each task (based on predecessor's result), compromising reuse
Still executes the subsequent tasks, with all the resulting overhead
I haven't played too much with passing references of the chain to tasks for fear of messing up things. I admit also I haven't tried Exception-throwing approach, because I think that the choice of not proceeding through the chain can be a functional (thus non exceptional) scenario...
Thanks for helping!
I think I found the answer to this issue: this seems the right way to proceed, indeed. I wonder why such common scenario is not documented anywhere, though.
For completeness I post the basic code snapshot:
#app.task(bind=True) # Note that we need bind=True for self to work
def task1(self, other_args):
#do_stuff
if end_chain:
self.request.callbacks[:] = []
....
Update
I implemented a more elegant way to cope with the issue and I want to share it with you. I am using a decorator called revoke_chain_authority, so that it can revoke automatically the chain without rewriting the code I previously described.
from functools import wraps
class RevokeChainRequested(Exception):
def __init__(self, return_value):
Exception.__init__(self, "")
# Now for your custom code...
self.return_value = return_value
def revoke_chain_authority(a_shared_task):
"""
#see: https://gist.github.com/bloudermilk/2173940
#param a_shared_task: a #shared_task(bind=True) celery function.
#return:
"""
#wraps(a_shared_task)
def inner(self, *args, **kwargs):
try:
return a_shared_task(self, *args, **kwargs)
except RevokeChainRequested, e:
# Drop subsequent tasks in chain (if not EAGER mode)
if self.request.callbacks:
self.request.callbacks[:] = []
return e.return_value
return inner
This decorator can be used on a shared task as follows:
#shared_task(bind=True)
#revoke_chain_authority
def apply_fetching_decision(self, latitude, longitude):
#...
if condition:
raise RevokeChainRequested(False)
Please note the use of #wraps. It is necessary to preserve the signature of the original function, otherwise this latter will be lost and celery will make a mess at calling the right wrapped task (e.g. it will call always the first registered function instead of the right one)
As of Celery 4.0, what I found to be working is to remove the remaining tasks from the current task instance's request using the statement:
self.request.chain = None
Let's say you have a chain of tasks a.s() | b.s() | c.s(). You can only access the self variable inside a task if you bind the task by passing bind=True as argument to the tasks' decorator.
#app.task(name='main.a', bind=True):
def a(self):
if something_happened:
self.request.chain = None
If something_happened is truthy, b and c wouldn't be executed.
Okay, so I'm working on a scheduler and I was thinking of something like, timeOut(3,print,'hello') and it would print hello every three seconds, I have tried some methods but all failed. Also Using time.sleep for this wouldn't quite work because I need to run other tasks as well besides just one
Edit:
I found out how to do what I needed, sorry for being confusing but this did the trick for what I needed, thanks for answering everyone.
class test:
def __init__(self):
self.objectives = set()
class Objective:
pass
def interval(self,timeout,function,*data):
newObjective = self.Objective()
newObjective.Class = self
newObjective.timeout = time.time()+timeout
newObjective.timer = timeout
newObjective.function = function
newObjective.repeate = True
newObjective.data = data
self.objectives.add(newObjective)
return True
def runObjectives(self):
timeNow = time.time()
for objective in self.objectives:
timeout = objective.timer
if objective.timeout <= timeNow:
objective.function(*objective.data)
if objective.repeate:
objective.timeout = timeNow + timeout
self.main()
else:
self.objectives.remove(objective)
print('removed')
def main(self):
while True:
self.runObjectives()
The standard library includes a module called sched for scheduling. It can be adapted to work in a variety of environments using the delayfunc constructor parameter. Using it your question would likely read:
def event():
scheduler.enter(3, 0, event, ()) # reschedule
print('hello')
Now it depends on how you run the other tasks. Are you using an event loop? It probably has a similar scheduling mechanism (at least twisted has callLater and GObject has timeout_add). If all else fails, you can spawn a new thread and execute a sched.scheduler with time.sleep there.