Popular recipes tagged "meta:loc=18"http://code.activestate.com/recipes/tags/meta:loc=18/2017-03-13T13:27:50-07:00ActiveState Code RecipesContext Manager for an Arbitrary Number of Files in Python (Python)
2017-03-13T13:27:50-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580763-context-manager-for-an-arbitrary-number-of-files-i/
<p style="color: grey">
Python
recipe 580763
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/open/">open</a>).
Revision 2.
</p>
<p>The pattern using <code>with</code> together with <code>open()</code> to automatically close a file after leaving the context is well known. To open a fixed number you can simply nest these statements or use the comma notation. For having a context which represents an arbitrary number of open files you can use the <code>ExitStack</code> class, but only for Python 3.3+.</p>
<p>For other Python versions I'm using the following class which I named <code>Files</code>. The presented implementation is only for reading files (for keeping it clear). Extending it for having various file modes should not pose a problem.</p>
Engineering notation. (Python)
2015-04-15T16:17:12-07:00Paul Sargenthttp://code.activestate.com/recipes/users/4192033/http://code.activestate.com/recipes/579046-engineering-notation/
<p style="color: grey">
Python
recipe 579046
by <a href="/recipes/users/4192033/">Paul Sargent</a>
(<a href="/recipes/tags/engineering/">engineering</a>, <a href="/recipes/tags/ipython/">ipython</a>).
</p>
<p>eng(x) returns a string representing x using the "engineering notation"</p>
How to implement lfu cache in python using threads? (Python)
2015-01-14T21:42:12-08:00testhttp://code.activestate.com/recipes/users/4191474/http://code.activestate.com/recipes/579003-how-to-implement-lfu-cache-in-python-using-threads/
<p style="color: grey">
Python
recipe 579003
by <a href="/recipes/users/4191474/">test</a>
.
</p>
<p>I need to design and implement an LFU scheduler abiding by the following core principles. In the implementation the scheduler will be a thread (instead of a process); and the processes in this simulations will be represented by objects of a custom class called Process. There are N processes with name IDs 1,2,...N of a hypothetical operating system are to be arranged in cache memory for scheduling purposes(processes are not threads orprocesses). To make this precise, let T denote the number of times that the scheduler scheduled a(ny) process since the start of the execution of the scheduler. Let p denote a process currently present in the cache, let Tp denote the number of times that process p was scheduled by the scheduler to run since the start of the execution of the scheduler. Then, fp = Tp/T is the frequency in which process p was scheduled. An LFU scheduler favours process p with the least fp and schedules it to run next. In case of ties, the process whose ID is the smallest gets to run.</p>
<p>Need to write P for the set of all processes in the system. We distinguish between two subsets of P. The set of processes in the cache, denoted by Pc, and the set of processes outside of the cache,denoted Po. In particular, we have Pc U Po = P. Below we describe the core principle the management of these two sets and migration of processes between these two sets.</p>
<p>The cache has M available slots for processes; that is |Pc|<=M. M is an arbitrary integer; and it is to be set at the time of initialisation of the cache. M may satisfy any of the predicates MN. The M slots of the cache are to be filled with the M processes whose associated frequencies are the owest amongst all members of P. In the case of ties, processes with lesser ID numbers are favoured for entrance into the cache.
The scheduler may only pick a process to run from those processes present in the cache.
In the case that M < N processes may need to be evacuated from the cache in favour of others with lower frequency.
Evacuation need not necessarily mean that the process has concluded its task (details below).
The members of Po still have their frequency updated; indeed, the parameter T increases while for each process p belongs Po the parameter Tp remains the same causing fp to decay.
Class Process: (a) Supports a run() method that simply prints: Process ran for the time. (b) The class supports a constructor whose first argument is an integer called name specifying its ID number (recall that IDs will be unique consisting of the interval [1,N]). The second argument is an integer times specifying the number of times that the process has to run (thus, if a process is forced out of the cache due to lack of space it can only be "discarded" in case it has ran times times).
The following is the evacuation policy from the cache: (a) Processes reaching their times parameter are to be evacuated and are eligible to be discarded entirely. (b) Let us assume that |Pc|=M qp, then evacuate p from he cache and add q. For control purposes (of the grading process of the assignment) a single process is to be evacuated each time. This does not exclude the possibility that evacuation may occur in succession; for instance if several members of Po have their frequency low enough. (c) Evacuation is to occur only whenever there exists a q belongs Po and p belongs Pc satisfying fq < fp and the cache is full.
A process being scheduled to run is allowed to apply its run() method once and then reevaluation of which is the next process to run is to take place. In particular, a process is not allowed to run in succession unless the scheduling policy mandates it.</p>
<p>I'm a little lost on this question!
How to implement?!?!</p>
Simple derivative solver in python (Python)
2014-03-08T12:58:26-08:00Johnhttp://code.activestate.com/recipes/users/4189390/http://code.activestate.com/recipes/578847-simple-derivative-solver-in-python/
<p style="color: grey">
Python
recipe 578847
by <a href="/recipes/users/4189390/">John</a>
(<a href="/recipes/tags/basic/">basic</a>, <a href="/recipes/tags/beginner/">beginner</a>, <a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/calculus/">calculus</a>, <a href="/recipes/tags/derivative/">derivative</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Solves monomials, might try to make one that solves polynomials in the future.</p>
Recapitalize word in the beginning of every sentence (Python)
2014-01-05T14:11:00-08:00sky kokhttp://code.activestate.com/recipes/users/4188838/http://code.activestate.com/recipes/578796-recapitalize-word-in-the-beginning-of-every-senten/
<p style="color: grey">
Python
recipe 578796
by <a href="/recipes/users/4188838/">sky kok</a>
(<a href="/recipes/tags/regex/">regex</a>).
Revision 2.
</p>
<p>Recapitalizes text, placing caps after end-of-sentence punctuation. Turning "hello world. how are you?" to "Hello world. How are you?"</p>
Simple Addition Multiplication in Python (Python)
2013-06-18T15:21:45-07:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578565-simple-addition-multiplication-in-python/
<p style="color: grey">
Python
recipe 578565
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Here is a very simple program in Python to multiply 2 numbers. I worte for my blog <a href="http://thelivingpearl.com/basic-multiplication-in-python/">Captain DeadBones Chronicles</a></p>
Directory & File Counter (Python)
2012-07-07T22:07:51-07:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578197-directory-file-counter/
<p style="color: grey">
Python
recipe 578197
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/archive/">archive</a>, <a href="/recipes/tags/old/">old</a>, <a href="/recipes/tags/utility/">utility</a>).
</p>
<p>This recipe manually counts the number of sub-directories and files can be found in a directory. It was an experimental program from long ago used to learn more about Python. This is committed for archival to be run under Python 2.5 or later versions.</p>
Credit Card Validation (Python)
2011-08-11T19:06:58-07:00Stijn de Graafhttp://code.activestate.com/recipes/users/4178055/http://code.activestate.com/recipes/577838-credit-card-validation/
<p style="color: grey">
Python
recipe 577838
by <a href="/recipes/users/4178055/">Stijn de Graaf</a>
(<a href="/recipes/tags/card/">card</a>, <a href="/recipes/tags/credit/">credit</a>, <a href="/recipes/tags/false/">false</a>, <a href="/recipes/tags/number/">number</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/true/">true</a>, <a href="/recipes/tags/validating/">validating</a>, <a href="/recipes/tags/validation/">validation</a>).
</p>
<p>Test validity of any credit card number using the LUHN method (mod 10).
Starting at the last digit and moving backwards, you add up every other digit.
Then, you double the left-out digits, and add the digits of these results to the original sum.
If this satisfies sum mod 10 == 0 then the card is valid.</p>
<p>This is also explained at <a href="http://www.beachnet.com/%7Ehstiles/cardtype.html" rel="nofollow">http://www.beachnet.com/~hstiles/cardtype.html</a></p>
Multiplication table (Python)
2011-04-21T09:12:26-07:00Boubakrhttp://code.activestate.com/recipes/users/4176416/http://code.activestate.com/recipes/577672-multiplication-table/
<p style="color: grey">
Python
recipe 577672
by <a href="/recipes/users/4176416/">Boubakr</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/multiplication/">multiplication</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/table/">table</a>).
</p>
<p>Multiplication table (Python)</p>
"Comma float" to float (Python)
2010-02-11T22:19:41-08:00Matevz Leskohttp://code.activestate.com/recipes/users/4173032/http://code.activestate.com/recipes/577054-comma-float-to-float/
<p style="color: grey">
Python
recipe 577054
by <a href="/recipes/users/4173032/">Matevz Lesko</a>
(<a href="/recipes/tags/comma/">comma</a>, <a href="/recipes/tags/float/">float</a>, <a href="/recipes/tags/numbers/">numbers</a>, <a href="/recipes/tags/string/">string</a>).
Revision 2.
</p>
<p>Convert comma separated floating point number (12,3456) to float.</p>
user and root directory logfile (Python)
2011-03-22T10:00:49-07:00justin drakehttp://code.activestate.com/recipes/users/4177409/http://code.activestate.com/recipes/577619-user-and-root-directory-logfile/
<p style="color: grey">
Python
recipe 577619
by <a href="/recipes/users/4177409/">justin drake</a>
(<a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/log/">log</a>, <a href="/recipes/tags/time/">time</a>, <a href="/recipes/tags/user/">user</a>).
</p>
<p>script to retrieve time, current user, and root of directory
output to spam.txt</p>
<p>want to add more like windows version, bios info and other useful diagnostic information </p>
<p>free to use and modify
welcome any advice or corrections
learning python hope this turns into a script to create a organized personal report of customer's pc</p>
Numerical Integration using Method of Exhaustion (Python)
2010-12-22T04:17:36-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577513-numerical-integration-using-method-of-exhaustion/
<p style="color: grey">
Python
recipe 577513
by <a href="/recipes/users/4172570/">FB36</a>
(<a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/mathematics/">mathematics</a>).
</p>
<p>Method of Exhaustion is a generalization of Archimedes Method for calculating PI.</p>
sending gmail though python code (Python)
2013-03-23T10:01:02-07:00Raghuramhttp://code.activestate.com/recipes/users/4174757/http://code.activestate.com/recipes/577371-sending-gmail-though-python-code/
<p style="color: grey">
Python
recipe 577371
by <a href="/recipes/users/4174757/">Raghuram</a>
(<a href="/recipes/tags/python_scripts/">python_scripts</a>).
Revision 2.
</p>
<p>sending gmail though your python code</p>
Convert a cmp function to a key function (Python)
2010-04-04T23:28:40-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576653-convert-a-cmp-function-to-a-key-function/
<p style="color: grey">
Python
recipe 576653
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/2_to_3/">2_to_3</a>).
Revision 5.
</p>
<p>Py3.0 transition aid. The <em>sorted()</em> builtin and the <em>list.sort()</em> method no longer accept a <em>cmp</em> function in Python 3.0. Most cases are easy to convert manually. This recipe handles the remaining cases.</p>
Next Lexographic Word (Java)
2010-07-18T11:33:38-07:00st0lehttp://code.activestate.com/recipes/users/4174421/http://code.activestate.com/recipes/577315-next-lexographic-word/
<p style="color: grey">
Java
recipe 577315
by <a href="/recipes/users/4174421/">st0le</a>
(<a href="/recipes/tags/bruteforce/">bruteforce</a>, <a href="/recipes/tags/java/">java</a>, <a href="/recipes/tags/password/">password</a>).
</p>
<p>Generates next lexographically occuring word, say "aaa" -> "aab" -> "aac"</p>
<p>Can be used for password cracking....</p>
Method Caching (Python)
2010-07-27T20:26:08-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577338-method-caching/
<p style="color: grey">
Python
recipe 577338
by <a href="/recipes/users/4174445/">Danillo Souza</a>
(<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Save each set of parameters with your respective return value in cache.</p>
Default arg special action (Python)
2010-04-14T19:52:33-07:00Ed Blakehttp://code.activestate.com/recipes/users/4173678/http://code.activestate.com/recipes/577194-default-arg-special-action/
<p style="color: grey">
Python
recipe 577194
by <a href="/recipes/users/4173678/">Ed Blake</a>
(<a href="/recipes/tags/default_arguments/">default_arguments</a>, <a href="/recipes/tags/identity/">identity</a>, <a href="/recipes/tags/oop/">oop</a>).
</p>
<p>A way to allow functions with 0, 1, and -1 arguments! For those times you want to differentiate a no value argument from a non-value argument.</p>
Komodo JS Macro - show svn blame for the current editor line (JavaScript)
2013-05-02T22:28:50-07:00Todd Whitemanhttp://code.activestate.com/recipes/users/2666241/http://code.activestate.com/recipes/577013-komodo-js-macro-show-svn-blame-for-the-current-edi/
<p style="color: grey">
JavaScript
recipe 577013
by <a href="/recipes/users/2666241/">Todd Whiteman</a>
(<a href="/recipes/tags/blame/">blame</a>, <a href="/recipes/tags/javascript/">javascript</a>, <a href="/recipes/tags/komodo/">komodo</a>, <a href="/recipes/tags/macro/">macro</a>, <a href="/recipes/tags/scc/">scc</a>, <a href="/recipes/tags/subversion/">subversion</a>, <a href="/recipes/tags/svn/">svn</a>, <a href="/recipes/tags/toddw/">toddw</a>).
Revision 4.
</p>
<p>A <a href="http://www.activestate.com/komodo">Komodo</a> JavaScript macro that can show the SCC repository information for who last changed the current line in the Komodo editor - see the screenshot below.</p>
<p><img src="http://community.activestate.com/files/images/svn_blame.png" alt="svn blame image" /></p>
Hex Dump 2 (Python)
2009-11-03T05:25:21-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/576945-hex-dump-2/
<p style="color: grey">
Python
recipe 576945
by <a href="/recipes/users/2608421/">Stephen Chappell</a>
(<a href="/recipes/tags/dump/">dump</a>, <a href="/recipes/tags/hex/">hex</a>).
</p>
<p>This is a fast hex-dumping utility written for Python 3.1 and later.</p>
<p>Written for the command line with usage information printed as needed.</p>
import foldernames from a txt file , and take ownership over these folders and subfolder , and delete them - Windows (Python)
2009-07-16T06:58:26-07:00mgarrana Garranahttp://code.activestate.com/recipes/users/4171135/http://code.activestate.com/recipes/576845-import-foldernames-from-a-txt-file-and-take-owners/
<p style="color: grey">
Python
recipe 576845
by <a href="/recipes/users/4171135/">mgarrana Garrana</a>
(<a href="/recipes/tags/script_delete_folders/">script_delete_folders</a>, <a href="/recipes/tags/script_take_ownership_folders/">script_take_ownership_folders</a>, <a href="/recipes/tags/windows_delete_folders/">windows_delete_folders</a>).
</p>
<p>this script opens a file called "folders.txt" which contains foldername each in a separate line , and then passes by these folders one by one in a loop and takes ownership of these folders and subfolders and deletes them</p>