Popular recipes tagged "meta:loc=16"http://code.activestate.com/recipes/tags/meta:loc=16/2016-11-10T10:23:11-08:00ActiveState Code RecipesUnit Testing Nested Functions (Python)
2016-11-10T10:23:11-08:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580716-unit-testing-nested-functions/
<p style="color: grey">
Python
recipe 580716
by <a href="/recipes/users/4182236/">Alfe</a>
(<a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/unittests/">unittests</a>).
Revision 3.
</p>
<p>Python allows the declaration of nested functions. These are typically hard to unit test because using just the normal ways of calling they cannot be called from outside their surrounding function. So they cannot be considered a clearly separated unit and thus cannot be unit tested.</p>
<p>This is a drawback of using them, so many developers (especially the ones deep into test driven development who strive to have a high unit test coverage) tend to avoid them in favor for standalone functions which can be called from the unit tests without any hassle.</p>
<p>But not all solutions with nested functions can be written as elegant with standalone functions. Nested functions are powerful insofar that they can access the local variables of the surrounding function without any need to pass them into the nested function, thus the code can in many cases stay neat and tidy while using a standalone function instead might raise the need to pass the complete context in form of a bunch of parameters. Also, using nested functions makes their local usage clear to any reader and keeps the name space tight.</p>
<p>But at least in the standard CPython (i. e. not necessarily in Jython, etc.) the implementation of functions (and methods) allows to find the nested function's code, wrap it properly to give it its needed context and then call it from the outside. I wrote a small module which helps doing exactly this.</p>
Publish a Windows Process List to PDF with xtopdf (Batch)
2015-12-27T20:45:32-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/579142-publish-a-windows-process-list-to-pdf-with-xtopdf/
<p style="color: grey">
Batch
recipe 579142
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/pdfwriter/">pdfwriter</a>, <a href="/recipes/tags/pdf_generation/">pdf_generation</a>, <a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/process_management/">process_management</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/windows/">windows</a>).
</p>
<p>This recipe shows how you can generate a Windows process list or task list (basically, a list of running processes, with some information about each of them), to a PDF file, using the Windows TASKLIST command along with the xtopdf toolkit. The list is sorted in ascending order of memory usage of the processes, before writing it to PDF.</p>
<p>It differs somewhat from other xtopdf recipes, in that no additional code needs to be written, over and above what is already in the xtopdf package. We just have to use the needed commands there, in a series of commands or a pipeline.</p>
<p>However, one can still write additional code, by modifying the program used (StdinToPDF.py), if needed, to customize the PDF output.</p>
A Pythonic ratio that equals pi (Python)
2016-06-26T20:38:24-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580686-a-pythonic-ratio-that-equals-pi/
<p style="color: grey">
Python
recipe 580686
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
.
</p>
<p>This shows how to find two integer values whose ratio approximates the mathematical constant, pi, somewhat accurately.</p>
BMI Code (Python)
2016-02-20T11:58:12-08:00Josh Nationhttp://code.activestate.com/recipes/users/4193667/http://code.activestate.com/recipes/580615-bmi-code/
<p style="color: grey">
Python
recipe 580615
by <a href="/recipes/users/4193667/">Josh Nation</a>
.
</p>
<p>I am creating a BMI calculator for a class assignment. My BMI keeps getting 0.0 no matter what I enter. Please Help!</p>
pick all combinations of items in buckets (Python)
2015-09-05T07:39:42-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/579098-pick-all-combinations-of-items-in-buckets/
<p style="color: grey">
Python
recipe 579098
by <a href="/recipes/users/4184815/">yota</a>
(<a href="/recipes/tags/combinatorics/">combinatorics</a>).
</p>
<p>Let be a number of buckets, containing each, a variable number of items. This function return all combinations possible of one item picked out of each bucket</p>
<p>example, with three buckets {ba, be, bi}, {ka, ko, ku, ke} and {to, ty}, the function enumerate as such: </p>
<pre class="prettyprint"><code> 0. ba-ka-to
1. ba-ka-ty
2. ba-ko-to
3. ba-ko-ty
4. ba-ku-to
5. ba-ku-ty
6. ba-ke-to
7. ba-ke-ty
8. be-ka-to
9. be-ka-ty
10. be-ko-to
11. be-ko-ty
12. be-ku-to
13. be-ku-ty
14. be-ke-to
15. be-ke-ty
16. bi-ka-to
17. bi-ka-ty
18. bi-ko-to
19. bi-ko-ty
20. bi-ku-to
21. bi-ku-ty
22. bi-ke-to
23. bi-ke-ty
</code></pre>
Hex Dump (Python)
2015-05-29T10:44:47-07:00Wenlong Liuhttp://code.activestate.com/recipes/users/4192273/http://code.activestate.com/recipes/579064-hex-dump/
<p style="color: grey">
Python
recipe 579064
by <a href="/recipes/users/4192273/">Wenlong Liu</a>
(<a href="/recipes/tags/dump/">dump</a>, <a href="/recipes/tags/hex/">hex</a>, <a href="/recipes/tags/python/">python</a>).
Revision 4.
</p>
<p>Hex dump a sequence of bytes.</p>
<p>Sample output:</p>
<pre class="prettyprint"><code> 0000000000: 33 C0 8E D0 BC 00 7C 8E C0 8E D8 BE 00 7C BF 00 3.....|. .....|..
0000000010: 06 B9 00 02 FC F3 A4 50 68 1C 06 CB FB B9 04 00 .......P h.......
0000000020: BD BE 07 80 7E 00 00 7C 0B 0F 85 0E 01 83 C5 10 ....~..| ........
0000000030: E2 F1 CD 18 88 56 00 55 C6 46 11 05 C6 46 10 00 .....V.U .F...F..
0000000040: B4 41 BB AA 55 CD 13 5D 72 0F 81 FB 55 AA 75 09 .A..U..] r...U.u.
0000000050: F7 C1 01 00 74 03 FE 46 10 66 60 80 7E 10 00 74 ....t..F .f`.~..t
0000000060: 26 66 68 00 00 00 00 66 FF 76 08 68 00 00 68 00 &fh....f .v.h..h.
0000000070: 7C 68 01 |h.
</code></pre>
Python text-to-speech with pyttsx (Python)
2014-03-02T21:35:15-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578839-python-text-to-speech-with-pyttsx/
<p style="color: grey">
Python
recipe 578839
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/pyttsx/">pyttsx</a>, <a href="/recipes/tags/speech/">speech</a>, <a href="/recipes/tags/text_to_speech/">text_to_speech</a>).
</p>
<p>This recipe shows how to generate computer speech from text using the pyttsx Python library. Text-to-speech, or speech synthesis, has many useful applications.</p>
Determine bytes needed to hold integer (Python3) (Python)
2013-11-03T18:35:18-08:00teddy_khttp://code.activestate.com/recipes/users/4187115/http://code.activestate.com/recipes/578766-determine-bytes-needed-to-hold-integer-python3/
<p style="color: grey">
Python
recipe 578766
by <a href="/recipes/users/4187115/">teddy_k</a>
(<a href="/recipes/tags/bytes/">bytes</a>, <a href="/recipes/tags/conversion/">conversion</a>, <a href="/recipes/tags/integer/">integer</a>).
</p>
<p>Convenience function to determine the number of bytes needed for a specified integer (split into multiple lines for clarity's sake ... could just as easily be a one-liner).</p>
<p>If this is already a defined function within Python, please let me know--a quick web search turned up nothing.</p>
Split string sample (Batch)
2013-08-13T11:29:44-07:00greg zakharovhttp://code.activestate.com/recipes/users/4184115/http://code.activestate.com/recipes/578638-split-string-sample/
<p style="color: grey">
Batch
recipe 578638
by <a href="/recipes/users/4184115/">greg zakharov</a>
(<a href="/recipes/tags/split/">split</a>, <a href="/recipes/tags/string/">string</a>).
</p>
<p>Target: we have a task to split the string ("this is a string") on the characters. Possible solution:</p>
Calculator (Python)
2013-07-10T17:28:17-07:00superducktoxichttp://code.activestate.com/recipes/users/4187037/http://code.activestate.com/recipes/578599-calculator/
<p style="color: grey">
Python
recipe 578599
by <a href="/recipes/users/4187037/">superducktoxic</a>
.
</p>
<p>Put any numbers in and the program will give you the answer.</p>
Simple Python Script to Ask & Validate Password (Python)
2013-01-19T04:04:30-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578427-simple-python-script-to-ask-validate-password/
<p style="color: grey">
Python
recipe 578427
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>This snippet of code was written as part of an article - <a href="http://thelivingpearl.com/2013/01/18/password-protecting-your-python-application/">Password Protecting You Python Application</a></p>
Simple Python Password Generator (Python)
2013-01-02T14:34:20-08:00Captain DeadBoneshttp://code.activestate.com/recipes/users/4184772/http://code.activestate.com/recipes/578394-simple-python-password-generator/
<p style="color: grey">
Python
recipe 578394
by <a href="/recipes/users/4184772/">Captain DeadBones</a>
(<a href="/recipes/tags/password/">password</a>, <a href="/recipes/tags/password_generator/">password_generator</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>I was coding this for an article a wrote about <a href="http://thelivingpearl.com/2013/01/02/generating-and-checking-passwords-in-python/">generating passwords in python</a>.</p>
Simple invocation of shell commands (Python)
2011-10-21T06:44:35-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577891-simple-invocation-of-shell-commands/
<p style="color: grey">
Python
recipe 577891
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
(<a href="/recipes/tags/processes/">processes</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Some simple wrappers around the subprocess functions for use in system administration utilities that frequently need to interpolate trusted data into shell commands (e.g. filenames from directory listings, etc):</p>
<pre class="prettyprint"><code>import shellcmd
return_code = shellcmd.shell_call('ls -l {}', dirname)
listing = shellcmd.check_shell_output('ls -l {}', dirname)
</code></pre>
<p>Each function invokes the subprocess function of the same name with <code>shell=True</code> and the supplied command string. Any positional and keyword arguments provided to the call are interpolated into the command string with the <code>str.format</code> method.</p>
entry_point decorator - no more if __name__ == '__main__' (Python)
2011-07-14T20:42:35-07:00yoav glaznerhttp://code.activestate.com/recipes/users/4178625/http://code.activestate.com/recipes/577791-entry_point-decorator-no-more-if-__name__-__main__/
<p style="color: grey">
Python
recipe 577791
by <a href="/recipes/users/4178625/">yoav glazner</a>
(<a href="/recipes/tags/main/">main</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>Use this decorator to remove the UGLY "if __name__ == '__main__':"</p>
Create module directly from a filesystem path (Python)
2011-07-15T04:13:59-07:00Nick Coghlanhttp://code.activestate.com/recipes/users/2035254/http://code.activestate.com/recipes/577792-create-module-directly-from-a-filesystem-path/
<p style="color: grey">
Python
recipe 577792
by <a href="/recipes/users/2035254/">Nick Coghlan</a>
.
</p>
<p>Given a filesystem path, use it to create a valid Python module. Based on <code>runpy.run_path()</code>, so accepts Python source files, compiled Python files and directories and zipfiles containing __main__.py files as valid targets.</p>
Calculation of total number of lines (Bash)
2011-05-23T10:55:45-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577717-calculation-of-total-number-of-lines/
<p style="color: grey">
Bash
recipe 577717
by <a href="/recipes/users/4168519/">Shao-chuan Wang</a>
(<a href="/recipes/tags/line/">line</a>).
</p>
<p>This bash script enables you to calculate the total number of lines of source code.</p>
<p>Example usage:
1. calculate total number of lines of java source files.
$ line.sh java</p>
<ol>
<li>calculate total number of lines of python source files.
$ line.sh py</li>
</ol>
C# Image Resize (Java)
2010-08-06T20:56:16-07:00John Hurlimanhttp://code.activestate.com/recipes/users/4174599/http://code.activestate.com/recipes/577347-c-image-resize/
<p style="color: grey">
Java
recipe 577347
by <a href="/recipes/users/4174599/">John Hurliman</a>
(<a href="/recipes/tags/csharp/">csharp</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/resize/">resize</a>).
</p>
<p>Resize a GDI+ image using C#</p>
Javascript - JSON Parser (only for study) (JavaScript)
2010-07-27T19:36:50-07:00Danillo Souzahttp://code.activestate.com/recipes/users/4174445/http://code.activestate.com/recipes/577337-javascript-json-parser-only-for-study/
<p style="color: grey">
JavaScript
recipe 577337
by <a href="/recipes/users/4174445/">Danillo Souza</a>
(<a href="/recipes/tags/json/">json</a>, <a href="/recipes/tags/parse/">parse</a>).
</p>
<p>JSON parser for any object in the script.</p>
<p>OBS: Need a way to identify methods.</p>
Infinite Stream Divisor (Python)
2010-07-21T12:50:17-07:00Narayana Chikkamhttp://code.activestate.com/recipes/users/4174427/http://code.activestate.com/recipes/577326-infinite-stream-divisor/
<p style="color: grey">
Python
recipe 577326
by <a href="/recipes/users/4174427/">Narayana Chikkam</a>
(<a href="/recipes/tags/divisor/">divisor</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/stream/">stream</a>).
</p>
<p>Maintain an F.S.A to keep track of the consequent remainders as states, input symbols as driving actions on each state. O(N) is the time complexity to find the given large string [in some radix(R), for some specific divisor(D)], where N is the length of the Input String which confirms to the Language Rules under the alphabet. O(R*D) is the space complexity to keep the F.S.A in memory for lookup!</p>
Minimalistic Memoization (Python)
2010-05-06T16:59:52-07:00Ahmed El Deebhttp://code.activestate.com/recipes/users/4173897/http://code.activestate.com/recipes/577219-minimalistic-memoization/
<p style="color: grey">
Python
recipe 577219
by <a href="/recipes/users/4173897/">Ahmed El Deeb</a>
(<a href="/recipes/tags/caching/">caching</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/recursion/">recursion</a>).
</p>
<p>Minimalistic Memoization in python, just works, doesn't take care of cleaning up the cache however.</p>