Popular recipes tagged "meta:loc=101"http://code.activestate.com/recipes/tags/meta:loc=101/2017-03-31T14:30:30-07:00ActiveState Code RecipesUnix tee-like functionality via a Python class (Python) 2017-03-31T14:30:30-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580767-unix-tee-like-functionality-via-a-python-class/ <p style="color: grey"> Python recipe 580767 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/cli/">cli</a>, <a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/commandline/">commandline</a>, <a href="/recipes/tags/linux/">linux</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/tee/">tee</a>, <a href="/recipes/tags/unix/">unix</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>The Unix tee commmand, when used in a command pipeline, allows you to capture the output of the preceding command to a file or files, while still sending it on to standard output (stdout) for further processing via other commands in a pipeline, or to print it, etc.</p> <p>This recipe shows how to implement simple tee-like functionality via a Python class. I do not aim to exactly replicate the functionality of the Unix tee, only something similar.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html" rel="nofollow">https://jugad2.blogspot.in/2017/03/a-python-class-like-unix-tee-command.html</a></p> Image Resizer (Python) 2016-05-22T16:45:52-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/580663-image-resizer/ <p style="color: grey"> Python recipe 580663 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/images/">images</a>, <a href="/recipes/tags/utilities/">utilities</a>, <a href="/recipes/tags/utility/">utility</a>). Revision 2. </p> <p>Image resizer/converter command-line utility.</p> Loading c extensions from a repository build area (Python) 2015-10-12T09:38:49-07:00Robin Beckerhttp://code.activestate.com/recipes/users/880795/http://code.activestate.com/recipes/579109-loading-c-extensions-from-a-repository-build-area/ <p style="color: grey"> Python recipe 579109 by <a href="/recipes/users/880795/">Robin Becker</a> . </p> <p>I test reportlab with multiple pythons. Rather than having to install into different virtualenvs I prefer to build my C-extensions in the repository using pythonxx setup.py build_ext and place the repository source folder onto the python path (using eg a link). However, this means I must load the extensions from somewhere other than their natural place. The BuiltExtensionFinder can be used for this.</p> Reading XML into dict-like object (Python) 2013-03-14T18:50:07-07:00Lucas Oliveirahttp://code.activestate.com/recipes/users/4185629/http://code.activestate.com/recipes/578492-reading-xml-into-dict-like-object/ <p style="color: grey"> Python recipe 578492 by <a href="/recipes/users/4185629/">Lucas Oliveira</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <ul> <li>Load XML, tree or root</li> <li>Make its children available through __getitem__</li> <li>Make its attributes available through __getattr__</li> <li>If child is requested, return an instance created with child as new root</li> <li>Make its text accessible through __getattr__, using attribute "text"</li> </ul> Ali (Java) 2013-02-17T12:54:44-08:00Alireza Hosseinihttp://code.activestate.com/recipes/users/4185286/http://code.activestate.com/recipes/578461-ali/ <p style="color: grey"> Java recipe 578461 by <a href="/recipes/users/4185286/">Alireza Hosseini</a> . </p> <h5 id="httpcodeactivestatecomrecipes578439-r1">{{{ <a href="http://code.activestate.com/recipes/578439/" rel="nofollow">http://code.activestate.com/recipes/578439/</a> (r1)</h5> <h4 id="just-a-try-using-the-thread-modules">Just a try using the thread modules.</h4> <p>import urllib as ul import bs4 as bs import urlparse as up import re as re import os.path as op import Queue as que import time import threading</p> <p>pat = re.compile('.<em>[\d]{4,7}.</em>')</p> <p>count=0</p> <p>class dldfile(threading.Thread): def __init__(self,qu1): threading.Thread.__init__(self) self.qu1=qu1 self.ad='download/1/'</p> <pre class="prettyprint"><code>def run(self): try: url,filename=self.qu1.get() url =url+self.ad #comment this line in case need to download whole web page instead of recipe ONLY... ul.urlretrieve(url,filename) global count except: print " RE-TRYING ", count= count - 1 self.qu1.put((url,filename)) self.run() finally: count= count +1 print str(count)+"("+str( threading.activeCount()) +")",filename self.qu1.task_done() </code></pre> <p>class dload(threading.Thread ): def __init__(self,qu,url = "http://code.activestate.com/recipes/langs/python/?page=" ): threading.Thread.__init__(self) self.url= url self.q =que.Queue() self.qu=qu</p> <pre class="prettyprint"><code>def run(self): ind=self.qu.get() url=self.url+str(ind) soup =bs.BeautifulSoup(''.join( ul.urlopen(url).readlines() )) bu = up.urlsplit(self.url) print 'started with the ' ,str(url).split('/')[-1], for i in soup.find_all(attrs = { "class" : "recipe-title"}): sp = up.urlsplit(i.a.get('href')) path = sp.path print path if re.search(pat, path): path = bu.scheme+'://'+bu.netloc+path filename = str(path).split('/')[-2] filename = op.join(op.abspath(op.curdir),filename+'.py') # recipe will be stored in given location </code></pre> <h4 id="filename-opjoinopabspathopcurdirfilenamehtml">filename = op.join(op.abspath(op.curdir),filename+'.html')</h4> <h4 id="uncomment-the-above-line-if-downloading-the-web-page-for-teh-recipe">uncomment the above line if downloading the web page for teh recipe</h4> <pre class="prettyprint"><code> print path self.q.put((path,filename)) self.fetch_data() time.sleep(1) self.qu.task_done() self.q.join() print 'done with the ' ,str(url).split('/')[-1], def fetch_data(self): Que1 = que.Queue() minitask =10 while not self.q.empty(): for i in range(minitask): x = dldfile(Que1) x.setDaemon(True) x.start() for j in range(minitask): Que1.put(self.q.get()) Que1.join() del x </code></pre> <p>if __name__ =='__main__': task=5 Que = que.Queue() for k in range(1,190,task): # no. of pages included under the python tag. 188 is current count and 3700+ python recipes print "\n PAGE # : {0} \t \nDeploying Fresh threads\n".format(k) for i in range(task): t = dload(Que) t.start() for j in range(task): Que.put(k+j) Que.join() Que.queue.clear() del t print "DONE\n" time.sleep(2) del Que print "Our buisness finished"</p> <h5 id="end-of-httpcodeactivestatecomrecipes578439">end of <a href="http://code.activestate.com/recipes/578439/" rel="nofollow">http://code.activestate.com/recipes/578439/</a> }}}</h5> Pipeline made of coroutines (Python) 2012-09-14T09:53:58-07:00Chaobin Tang (唐超斌)http://code.activestate.com/recipes/users/4174076/http://code.activestate.com/recipes/578265-pipeline-made-of-coroutines/ <p style="color: grey"> Python recipe 578265 by <a href="/recipes/users/4174076/">Chaobin Tang (唐超斌)</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/pipelining/">pipelining</a>, <a href="/recipes/tags/python/">python</a>). Revision 2. </p> <p>A pipeline made of several coroutines that can be turned off gracefully. </p> How to debug (deadlocked) multi threaded programs (Python) 2010-07-26T15:39:15-07:00Laszlo Nagyhttp://code.activestate.com/recipes/users/4150221/http://code.activestate.com/recipes/577334-how-to-debug-deadlocked-multi-threaded-programs/ <p style="color: grey"> Python recipe 577334 by <a href="/recipes/users/4150221/">Laszlo Nagy</a> (<a href="/recipes/tags/dead/">dead</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/stack/">stack</a>, <a href="/recipes/tags/thread/">thread</a>, <a href="/recipes/tags/trace/">trace</a>). </p> <p>Simple module that allows you to explore deadlocks in multi threaded programs.</p> Queue for managing multiple SIGALRM alarms concurrently (Python) 2012-12-06T18:58:11-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/577600-queue-for-managing-multiple-sigalrm-alarms-concurr/ <p style="color: grey"> Python recipe 577600 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/signal/">signal</a>). Revision 3. </p> <p>In asynchronous code, <em>signal.alarm()</em> is extremely useful for setting and handling timeouts and other timed and periodic tasks. It has a major limitation, however, that only one alarm function and alarm time can be set at a time; setting a new alarm disables the previous alarm. This package uses a <em>heapq</em> to maintain a queue of alarm events, allowing multiple alarm functions and alarm times to be set concurrently.</p> Simple local cache and cache decorator (Python) 2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/ <p style="color: grey"> Python recipe 577492 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Simple local cache. It saves local data in singleton dictionary with convenient interface</p> <h4 id="examples-of-use">Examples of use:</h4> <pre class="prettyprint"><code># Initialize SimpleCache({'data':{'example':'example data'}}) # Getting instance c = SimpleCache.getInstance() c.set('re.reg_exp_compiled',re.compile(r'\W*')) reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*')) # -------------------------------------------------------------- c = SimpleCache.getInstance() reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*')) # -------------------------------------------------------------- @scache def func1(): return 'OK' </code></pre> Type-Checking Function Overloading Decorator (Python) 2010-04-11T11:16:48-07:00Ryan Liehttp://code.activestate.com/recipes/users/4171032/http://code.activestate.com/recipes/577065-type-checking-function-overloading-decorator/ <p style="color: grey"> Python recipe 577065 by <a href="/recipes/users/4171032/">Ryan Lie</a> (<a href="/recipes/tags/checkng/">checkng</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/overloading/">overloading</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/type/">type</a>). Revision 2. </p> <p>The recipe presents a function overloading decorator in python that do type check. The type signature is marked with the @takes and @returns decorator, which causes the function to raise an InputParameterError exception if called with inappropriate arguments. The @overloaded function searches for the first overloads that doesn't raise TypeError or InputParameterError when called. Alternative overloads are added to the overloads list by using the @func.overload_with decorator. The order of function definition determines which function gets tried first and once it founds a compatible function, it skips the rest of the overloads list.</p> bier-soup.py, a small example of BeautifulSoup (Python) 2009-07-24T05:59:43-07:00denishttp://code.activestate.com/recipes/users/4168005/http://code.activestate.com/recipes/576841-bier-souppy-a-small-example-of-beautifulsoup/ <p style="color: grey"> Python recipe 576841 by <a href="/recipes/users/4168005/">denis</a> (<a href="/recipes/tags/beautifulsoup/">beautifulsoup</a>, <a href="/recipes/tags/bier/">bier</a>). Revision 3. </p> <p>bier-soup.py reads html tables like those in <a href="http://www.bier1.de" rel="nofollow">http://www.bier1.de</a> and writes plain text files, as a small example of BeautifulSoup</p> Polynomial explorer (Python) 2008-09-11T07:21:11-07:00Kaushik Ghosehttp://code.activestate.com/recipes/users/4166965/http://code.activestate.com/recipes/576501-polynomial-explorer/ <p style="color: grey"> Python recipe 576501 by <a href="/recipes/users/4166965/">Kaushik Ghose</a> (<a href="/recipes/tags/interactive_graphs/">interactive_graphs</a>, <a href="/recipes/tags/matplotlib/">matplotlib</a>). Revision 2. </p> <p>Polynomial explorer. You tell this module what order of polynomial you want and it will set up a figure with a graph of that polynomial plotted with x = -1 to +1. It will set up a second figure with a set of coefficient axes. You can click on the coeffiecient axes to set the coefficents. The graph will then update to reflect the values of the new coefficients.</p> <p>This code illustrates the use of mouse interaction using matplotlib</p> Transactional Set Updates (Python) 2007-04-28T03:12:59-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/511496-transactional-set-updates/ <p style="color: grey"> Python recipe 511496 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Tracks the additions and deletions to a set during update operations. Useful for large sets with few modifications.</p> A basic time profiler (Python) 2006-08-03T20:39:37-07:00Anandhttp://code.activestate.com/recipes/users/760763/http://code.activestate.com/recipes/496938-a-basic-time-profiler/ <p style="color: grey"> Python recipe 496938 by <a href="/recipes/users/760763/">Anand</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>This recipe provides a very simple time profiling module which helps you to measure actual execution time for blocks of Python code without peppering your code with many time.time() statements.</p> IPv6 Multicast (Python) 2005-10-28T11:43:52-07:00Shane Hollowayhttp://code.activestate.com/recipes/users/861616/http://code.activestate.com/recipes/442490-ipv6-multicast/ <p style="color: grey"> Python recipe 442490 by <a href="/recipes/users/861616/">Shane Holloway</a> (<a href="/recipes/tags/network/">network</a>). </p> <p>IPv6 can be tricky in many ways, and multicast makes it even more fun. This recipe is intended to give you a working implementation to start from.</p> Dupinator -- detect and delete duplicate files (Python) 2005-01-09T12:31:21-08:00Bill Bumgarnerhttp://code.activestate.com/recipes/users/2250923/http://code.activestate.com/recipes/362459-dupinator-detect-and-delete-duplicate-files/ <p style="color: grey"> Python recipe 362459 by <a href="/recipes/users/2250923/">Bill Bumgarner</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>Point this script at a folder or several folders and it will find and delete all duplicate files within the folders, leaving behind the first file found of any set of duplicates. It is designed to handle hundreds of thousands of files of any size at a time and to do so quickly. It was written to eliminate duplicates across several photo libraries that had been shared between users. As the script was a one-off to solve a very particular problem, there are no options nor is it refactoring into any kind of modules or reusable functions.</p> Decimal & Hex conversion routines (Tcl) 2005-06-03T11:28:27-07:00Chris Cornishhttp://code.activestate.com/recipes/users/861819/http://code.activestate.com/recipes/415982-decimal-hex-conversion-routines/ <p style="color: grey"> Tcl recipe 415982 by <a href="/recipes/users/861819/">Chris Cornish</a> . </p> <p>Here are a suite of hexadecimal to/from decimal conversion routines. These have been used in an application, so there should not be any errors, I trust.</p> Implementing "Future"s with decorators (Python) 2004-12-30T05:26:18-08:00Justin Shawhttp://code.activestate.com/recipes/users/1523109/http://code.activestate.com/recipes/355651-implementing-futures-with-decorators/ <p style="color: grey"> Python recipe 355651 by <a href="/recipes/users/1523109/">Justin Shaw</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 5. </p> <p>How to kick off a slow process without waiting around for the result. The process is run in the background until the value is actually needed at which time it blocks until the value is ready.</p> Some python style switches (Python) 2004-05-11T21:38:56-07:00Runsun Panhttp://code.activestate.com/recipes/users/1521341/http://code.activestate.com/recipes/269708-some-python-style-switches/ <p style="color: grey"> Python recipe 269708 by <a href="/recipes/users/1521341/">Runsun Pan</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 6. </p> <p>Python style switches. Showing several ways of making a 'switch' in python.</p> xmlgettext.py (Python) 2003-07-28T12:29:21-07:00Fritz Cizmarovhttp://code.activestate.com/recipes/users/1031326/http://code.activestate.com/recipes/212728-xmlgettextpy/ <p style="color: grey"> Python recipe 212728 by <a href="/recipes/users/1031326/">Fritz Cizmarov</a> (<a href="/recipes/tags/xml/">xml</a>). </p> <p>extract the texts from an XML-file and write it into an *.pot</p>