Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
callback - A module for simplifying callback handling/chaining/passing named arguments
-
I don't know if this has already been done, and no idea how useful this really is in practice, but I thought I'd give it a crack!
I wrote a real simple module for handling callbacks which allow you to decorate commands in a script as 'callback handlers' (basically, decorated Python functions taking any number of arguments), along with a URL generator for creating the callback URL to call the command in the current script or optionally in another script which also implements the callback handling.
The module is available here: https://gist.github.com/djbouche/5079561
Appreciate any feedback or suggestions!
Example (uses TweetBot module from https://gist.github.com/djbouche/5079739):
<pre>
import TweetBot
import callback
import syshandler = callback.InfoHandler(sys.argv)
@handler.cmd("process_next")
def process_next(tweets):
if len(tweets):
t = tweets.pop() #process next tweet on the list
#tweet, and send the list of remaining tweets down
TweetBot.tweet(t,callback.url('process_next',tweets=tweets))
else: #no more tweets
print 'Finished processing tweets!'if name == "main" and not handler.handle():
#first entry point
f = open("tweets.txt","r") #read tweets from a file called 'tweets.txt'
a = f.read()
f.close()
tweets = a.splitlines() #one line per tweet
tweets.reverse() #so we can pop them off
process_next(tweets) #start processing!
</pre> -
to format code like this:
<pre>
blah
</pre>
put the tag {pre} {/pre} around what you want to format, All caps or all lowercase replace the curly braces with angled brackets <><> -
Aha. Thank you, @Dalorbi!
-
Pretty useful cheers
-
Is there an advantage to using callbacks and context sharing instead of Python generators? My sense is that judicious use of yield lets you simplify the approach but perhaps I am missing something.
#import TweetBot # for more on Python generators see: # http://wiki.python.org/moin/Generators def tweet_feeder(file_name = 'tweets.txt'): with open(file_name) as in_file: tweets = in_file.read().splitlines() tweets.reverse() # so we can pop them off for tweet in tweets: yield tweet print('Finished processing tweets!') if __name__ == '__main__': for tweet in tweet_feeder('tweets.txt'): print(tweet) #TweetBot.tweet(tweet)